Fix doc-string of pop-to-buffer-same-window. (Bug#15492)
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276
277 #include "lisp.h"
278 #include "atimer.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301 #ifdef HAVE_WINDOW_SYSTEM
302 #include TERM_HEADER
303 #endif /* HAVE_WINDOW_SYSTEM */
304
305 #ifndef FRAME_X_OUTPUT
306 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
307 #endif
308
309 #define INFINITY 10000000
310
311 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
312 Lisp_Object Qwindow_scroll_functions;
313 static Lisp_Object Qwindow_text_change_functions;
314 static Lisp_Object Qredisplay_end_trigger_functions;
315 Lisp_Object Qinhibit_point_motion_hooks;
316 static Lisp_Object QCeval, QCpropertize;
317 Lisp_Object QCfile, QCdata;
318 static Lisp_Object Qfontified;
319 static Lisp_Object Qgrow_only;
320 static Lisp_Object Qinhibit_eval_during_redisplay;
321 static Lisp_Object Qbuffer_position, Qposition, Qobject;
322 static Lisp_Object Qright_to_left, Qleft_to_right;
323
324 /* Cursor shapes. */
325 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
326
327 /* Pointer shapes. */
328 static Lisp_Object Qarrow, Qhand;
329 Lisp_Object Qtext;
330
331 /* Holds the list (error). */
332 static Lisp_Object list_of_error;
333
334 static Lisp_Object Qfontification_functions;
335
336 static Lisp_Object Qwrap_prefix;
337 static Lisp_Object Qline_prefix;
338 static Lisp_Object Qredisplay_internal;
339
340 /* Non-nil means don't actually do any redisplay. */
341
342 Lisp_Object Qinhibit_redisplay;
343
344 /* Names of text properties relevant for redisplay. */
345
346 Lisp_Object Qdisplay;
347
348 Lisp_Object Qspace, QCalign_to;
349 static Lisp_Object QCrelative_width, QCrelative_height;
350 Lisp_Object Qleft_margin, Qright_margin;
351 static Lisp_Object Qspace_width, Qraise;
352 static Lisp_Object Qslice;
353 Lisp_Object Qcenter;
354 static Lisp_Object Qmargin, Qpointer;
355 static Lisp_Object Qline_height;
356
357 #ifdef HAVE_WINDOW_SYSTEM
358
359 /* Test if overflow newline into fringe. Called with iterator IT
360 at or past right window margin, and with IT->current_x set. */
361
362 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
363 (!NILP (Voverflow_newline_into_fringe) \
364 && FRAME_WINDOW_P ((IT)->f) \
365 && ((IT)->bidi_it.paragraph_dir == R2L \
366 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
367 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
368 && (IT)->current_x == (IT)->last_visible_x)
369
370 #else /* !HAVE_WINDOW_SYSTEM */
371 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
372 #endif /* HAVE_WINDOW_SYSTEM */
373
374 /* Test if the display element loaded in IT, or the underlying buffer
375 or string character, is a space or a TAB character. This is used
376 to determine where word wrapping can occur. */
377
378 #define IT_DISPLAYING_WHITESPACE(it) \
379 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
380 || ((STRINGP (it->string) \
381 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
382 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
383 || (it->s \
384 && (it->s[IT_BYTEPOS (*it)] == ' ' \
385 || it->s[IT_BYTEPOS (*it)] == '\t')) \
386 || (IT_BYTEPOS (*it) < ZV_BYTE \
387 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
388 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
389
390 /* Name of the face used to highlight trailing whitespace. */
391
392 static Lisp_Object Qtrailing_whitespace;
393
394 /* Name and number of the face used to highlight escape glyphs. */
395
396 static Lisp_Object Qescape_glyph;
397
398 /* Name and number of the face used to highlight non-breaking spaces. */
399
400 static Lisp_Object Qnobreak_space;
401
402 /* The symbol `image' which is the car of the lists used to represent
403 images in Lisp. Also a tool bar style. */
404
405 Lisp_Object Qimage;
406
407 /* The image map types. */
408 Lisp_Object QCmap;
409 static Lisp_Object QCpointer;
410 static Lisp_Object Qrect, Qcircle, Qpoly;
411
412 /* Tool bar styles */
413 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
414
415 /* Non-zero means print newline to stdout before next mini-buffer
416 message. */
417
418 bool noninteractive_need_newline;
419
420 /* Non-zero means print newline to message log before next message. */
421
422 static bool message_log_need_newline;
423
424 /* Three markers that message_dolog uses.
425 It could allocate them itself, but that causes trouble
426 in handling memory-full errors. */
427 static Lisp_Object message_dolog_marker1;
428 static Lisp_Object message_dolog_marker2;
429 static Lisp_Object message_dolog_marker3;
430 \f
431 /* The buffer position of the first character appearing entirely or
432 partially on the line of the selected window which contains the
433 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
434 redisplay optimization in redisplay_internal. */
435
436 static struct text_pos this_line_start_pos;
437
438 /* Number of characters past the end of the line above, including the
439 terminating newline. */
440
441 static struct text_pos this_line_end_pos;
442
443 /* The vertical positions and the height of this line. */
444
445 static int this_line_vpos;
446 static int this_line_y;
447 static int this_line_pixel_height;
448
449 /* X position at which this display line starts. Usually zero;
450 negative if first character is partially visible. */
451
452 static int this_line_start_x;
453
454 /* The smallest character position seen by move_it_* functions as they
455 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
456 hscrolled lines, see display_line. */
457
458 static struct text_pos this_line_min_pos;
459
460 /* Buffer that this_line_.* variables are referring to. */
461
462 static struct buffer *this_line_buffer;
463
464
465 /* Values of those variables at last redisplay are stored as
466 properties on `overlay-arrow-position' symbol. However, if
467 Voverlay_arrow_position is a marker, last-arrow-position is its
468 numerical position. */
469
470 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
471
472 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
473 properties on a symbol in overlay-arrow-variable-list. */
474
475 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
476
477 Lisp_Object Qmenu_bar_update_hook;
478
479 /* Nonzero if an overlay arrow has been displayed in this window. */
480
481 static bool overlay_arrow_seen;
482
483 /* Vector containing glyphs for an ellipsis `...'. */
484
485 static Lisp_Object default_invis_vector[3];
486
487 /* This is the window where the echo area message was displayed. It
488 is always a mini-buffer window, but it may not be the same window
489 currently active as a mini-buffer. */
490
491 Lisp_Object echo_area_window;
492
493 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
494 pushes the current message and the value of
495 message_enable_multibyte on the stack, the function restore_message
496 pops the stack and displays MESSAGE again. */
497
498 static Lisp_Object Vmessage_stack;
499
500 /* Nonzero means multibyte characters were enabled when the echo area
501 message was specified. */
502
503 static bool message_enable_multibyte;
504
505 /* Nonzero if we should redraw the mode lines on the next redisplay. */
506
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 bool 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 bool 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 bool 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 bool 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 bool 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 bool 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 static 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 struct glyph *
1886 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1887 int *dx, int *dy, int *area)
1888 {
1889 struct glyph *glyph, *end;
1890 struct glyph_row *row = NULL;
1891 int x0, i;
1892
1893 /* Find row containing Y. Give up if some row is not enabled. */
1894 for (i = 0; i < w->current_matrix->nrows; ++i)
1895 {
1896 row = MATRIX_ROW (w->current_matrix, i);
1897 if (!row->enabled_p)
1898 return NULL;
1899 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1900 break;
1901 }
1902
1903 *vpos = i;
1904 *hpos = 0;
1905
1906 /* Give up if Y is not in the window. */
1907 if (i == w->current_matrix->nrows)
1908 return NULL;
1909
1910 /* Get the glyph area containing X. */
1911 if (w->pseudo_window_p)
1912 {
1913 *area = TEXT_AREA;
1914 x0 = 0;
1915 }
1916 else
1917 {
1918 if (x < window_box_left_offset (w, TEXT_AREA))
1919 {
1920 *area = LEFT_MARGIN_AREA;
1921 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1922 }
1923 else if (x < window_box_right_offset (w, TEXT_AREA))
1924 {
1925 *area = TEXT_AREA;
1926 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1927 }
1928 else
1929 {
1930 *area = RIGHT_MARGIN_AREA;
1931 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1932 }
1933 }
1934
1935 /* Find glyph containing X. */
1936 glyph = row->glyphs[*area];
1937 end = glyph + row->used[*area];
1938 x -= x0;
1939 while (glyph < end && x >= glyph->pixel_width)
1940 {
1941 x -= glyph->pixel_width;
1942 ++glyph;
1943 }
1944
1945 if (glyph == end)
1946 return NULL;
1947
1948 if (dx)
1949 {
1950 *dx = x;
1951 *dy = y - (row->y + row->ascent - glyph->ascent);
1952 }
1953
1954 *hpos = glyph - row->glyphs[*area];
1955 return glyph;
1956 }
1957
1958 /* Convert frame-relative x/y to coordinates relative to window W.
1959 Takes pseudo-windows into account. */
1960
1961 static void
1962 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1963 {
1964 if (w->pseudo_window_p)
1965 {
1966 /* A pseudo-window is always full-width, and starts at the
1967 left edge of the frame, plus a frame border. */
1968 struct frame *f = XFRAME (w->frame);
1969 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1970 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1971 }
1972 else
1973 {
1974 *x -= WINDOW_LEFT_EDGE_X (w);
1975 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1976 }
1977 }
1978
1979 #ifdef HAVE_WINDOW_SYSTEM
1980
1981 /* EXPORT:
1982 Return in RECTS[] at most N clipping rectangles for glyph string S.
1983 Return the number of stored rectangles. */
1984
1985 int
1986 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1987 {
1988 XRectangle r;
1989
1990 if (n <= 0)
1991 return 0;
1992
1993 if (s->row->full_width_p)
1994 {
1995 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1996 r.x = WINDOW_LEFT_EDGE_X (s->w);
1997 r.width = WINDOW_TOTAL_WIDTH (s->w);
1998
1999 /* Unless displaying a mode or menu bar line, which are always
2000 fully visible, clip to the visible part of the row. */
2001 if (s->w->pseudo_window_p)
2002 r.height = s->row->visible_height;
2003 else
2004 r.height = s->height;
2005 }
2006 else
2007 {
2008 /* This is a text line that may be partially visible. */
2009 r.x = window_box_left (s->w, s->area);
2010 r.width = window_box_width (s->w, s->area);
2011 r.height = s->row->visible_height;
2012 }
2013
2014 if (s->clip_head)
2015 if (r.x < s->clip_head->x)
2016 {
2017 if (r.width >= s->clip_head->x - r.x)
2018 r.width -= s->clip_head->x - r.x;
2019 else
2020 r.width = 0;
2021 r.x = s->clip_head->x;
2022 }
2023 if (s->clip_tail)
2024 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2025 {
2026 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2027 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2028 else
2029 r.width = 0;
2030 }
2031
2032 /* If S draws overlapping rows, it's sufficient to use the top and
2033 bottom of the window for clipping because this glyph string
2034 intentionally draws over other lines. */
2035 if (s->for_overlaps)
2036 {
2037 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2038 r.height = window_text_bottom_y (s->w) - r.y;
2039
2040 /* Alas, the above simple strategy does not work for the
2041 environments with anti-aliased text: if the same text is
2042 drawn onto the same place multiple times, it gets thicker.
2043 If the overlap we are processing is for the erased cursor, we
2044 take the intersection with the rectangle of the cursor. */
2045 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2046 {
2047 XRectangle rc, r_save = r;
2048
2049 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2050 rc.y = s->w->phys_cursor.y;
2051 rc.width = s->w->phys_cursor_width;
2052 rc.height = s->w->phys_cursor_height;
2053
2054 x_intersect_rectangles (&r_save, &rc, &r);
2055 }
2056 }
2057 else
2058 {
2059 /* Don't use S->y for clipping because it doesn't take partially
2060 visible lines into account. For example, it can be negative for
2061 partially visible lines at the top of a window. */
2062 if (!s->row->full_width_p
2063 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2064 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2065 else
2066 r.y = max (0, s->row->y);
2067 }
2068
2069 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2070
2071 /* If drawing the cursor, don't let glyph draw outside its
2072 advertised boundaries. Cleartype does this under some circumstances. */
2073 if (s->hl == DRAW_CURSOR)
2074 {
2075 struct glyph *glyph = s->first_glyph;
2076 int height, max_y;
2077
2078 if (s->x > r.x)
2079 {
2080 r.width -= s->x - r.x;
2081 r.x = s->x;
2082 }
2083 r.width = min (r.width, glyph->pixel_width);
2084
2085 /* If r.y is below window bottom, ensure that we still see a cursor. */
2086 height = min (glyph->ascent + glyph->descent,
2087 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2088 max_y = window_text_bottom_y (s->w) - height;
2089 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2090 if (s->ybase - glyph->ascent > max_y)
2091 {
2092 r.y = max_y;
2093 r.height = height;
2094 }
2095 else
2096 {
2097 /* Don't draw cursor glyph taller than our actual glyph. */
2098 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2099 if (height < r.height)
2100 {
2101 max_y = r.y + r.height;
2102 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2103 r.height = min (max_y - r.y, height);
2104 }
2105 }
2106 }
2107
2108 if (s->row->clip)
2109 {
2110 XRectangle r_save = r;
2111
2112 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2113 r.width = 0;
2114 }
2115
2116 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2117 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2118 {
2119 #ifdef CONVERT_FROM_XRECT
2120 CONVERT_FROM_XRECT (r, *rects);
2121 #else
2122 *rects = r;
2123 #endif
2124 return 1;
2125 }
2126 else
2127 {
2128 /* If we are processing overlapping and allowed to return
2129 multiple clipping rectangles, we exclude the row of the glyph
2130 string from the clipping rectangle. This is to avoid drawing
2131 the same text on the environment with anti-aliasing. */
2132 #ifdef CONVERT_FROM_XRECT
2133 XRectangle rs[2];
2134 #else
2135 XRectangle *rs = rects;
2136 #endif
2137 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2138
2139 if (s->for_overlaps & OVERLAPS_PRED)
2140 {
2141 rs[i] = r;
2142 if (r.y + r.height > row_y)
2143 {
2144 if (r.y < row_y)
2145 rs[i].height = row_y - r.y;
2146 else
2147 rs[i].height = 0;
2148 }
2149 i++;
2150 }
2151 if (s->for_overlaps & OVERLAPS_SUCC)
2152 {
2153 rs[i] = r;
2154 if (r.y < row_y + s->row->visible_height)
2155 {
2156 if (r.y + r.height > row_y + s->row->visible_height)
2157 {
2158 rs[i].y = row_y + s->row->visible_height;
2159 rs[i].height = r.y + r.height - rs[i].y;
2160 }
2161 else
2162 rs[i].height = 0;
2163 }
2164 i++;
2165 }
2166
2167 n = i;
2168 #ifdef CONVERT_FROM_XRECT
2169 for (i = 0; i < n; i++)
2170 CONVERT_FROM_XRECT (rs[i], rects[i]);
2171 #endif
2172 return n;
2173 }
2174 }
2175
2176 /* EXPORT:
2177 Return in *NR the clipping rectangle for glyph string S. */
2178
2179 void
2180 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2181 {
2182 get_glyph_string_clip_rects (s, nr, 1);
2183 }
2184
2185
2186 /* EXPORT:
2187 Return the position and height of the phys cursor in window W.
2188 Set w->phys_cursor_width to width of phys cursor.
2189 */
2190
2191 void
2192 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2193 struct glyph *glyph, int *xp, int *yp, int *heightp)
2194 {
2195 struct frame *f = XFRAME (WINDOW_FRAME (w));
2196 int x, y, wd, h, h0, y0;
2197
2198 /* Compute the width of the rectangle to draw. If on a stretch
2199 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2200 rectangle as wide as the glyph, but use a canonical character
2201 width instead. */
2202 wd = glyph->pixel_width - 1;
2203 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2204 wd++; /* Why? */
2205 #endif
2206
2207 x = w->phys_cursor.x;
2208 if (x < 0)
2209 {
2210 wd += x;
2211 x = 0;
2212 }
2213
2214 if (glyph->type == STRETCH_GLYPH
2215 && !x_stretch_cursor_p)
2216 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2217 w->phys_cursor_width = wd;
2218
2219 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2220
2221 /* If y is below window bottom, ensure that we still see a cursor. */
2222 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2223
2224 h = max (h0, glyph->ascent + glyph->descent);
2225 h0 = min (h0, glyph->ascent + glyph->descent);
2226
2227 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2228 if (y < y0)
2229 {
2230 h = max (h - (y0 - y) + 1, h0);
2231 y = y0 - 1;
2232 }
2233 else
2234 {
2235 y0 = window_text_bottom_y (w) - h0;
2236 if (y > y0)
2237 {
2238 h += y - y0;
2239 y = y0;
2240 }
2241 }
2242
2243 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2244 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2245 *heightp = h;
2246 }
2247
2248 /*
2249 * Remember which glyph the mouse is over.
2250 */
2251
2252 void
2253 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2254 {
2255 Lisp_Object window;
2256 struct window *w;
2257 struct glyph_row *r, *gr, *end_row;
2258 enum window_part part;
2259 enum glyph_row_area area;
2260 int x, y, width, height;
2261
2262 /* Try to determine frame pixel position and size of the glyph under
2263 frame pixel coordinates X/Y on frame F. */
2264
2265 if (!f->glyphs_initialized_p
2266 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2267 NILP (window)))
2268 {
2269 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2270 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2271 goto virtual_glyph;
2272 }
2273
2274 w = XWINDOW (window);
2275 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2276 height = WINDOW_FRAME_LINE_HEIGHT (w);
2277
2278 x = window_relative_x_coord (w, part, gx);
2279 y = gy - WINDOW_TOP_EDGE_Y (w);
2280
2281 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2282 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2283
2284 if (w->pseudo_window_p)
2285 {
2286 area = TEXT_AREA;
2287 part = ON_MODE_LINE; /* Don't adjust margin. */
2288 goto text_glyph;
2289 }
2290
2291 switch (part)
2292 {
2293 case ON_LEFT_MARGIN:
2294 area = LEFT_MARGIN_AREA;
2295 goto text_glyph;
2296
2297 case ON_RIGHT_MARGIN:
2298 area = RIGHT_MARGIN_AREA;
2299 goto text_glyph;
2300
2301 case ON_HEADER_LINE:
2302 case ON_MODE_LINE:
2303 gr = (part == ON_HEADER_LINE
2304 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2305 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2306 gy = gr->y;
2307 area = TEXT_AREA;
2308 goto text_glyph_row_found;
2309
2310 case ON_TEXT:
2311 area = TEXT_AREA;
2312
2313 text_glyph:
2314 gr = 0; gy = 0;
2315 for (; r <= end_row && r->enabled_p; ++r)
2316 if (r->y + r->height > y)
2317 {
2318 gr = r; gy = r->y;
2319 break;
2320 }
2321
2322 text_glyph_row_found:
2323 if (gr && gy <= y)
2324 {
2325 struct glyph *g = gr->glyphs[area];
2326 struct glyph *end = g + gr->used[area];
2327
2328 height = gr->height;
2329 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2330 if (gx + g->pixel_width > x)
2331 break;
2332
2333 if (g < end)
2334 {
2335 if (g->type == IMAGE_GLYPH)
2336 {
2337 /* Don't remember when mouse is over image, as
2338 image may have hot-spots. */
2339 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2340 return;
2341 }
2342 width = g->pixel_width;
2343 }
2344 else
2345 {
2346 /* Use nominal char spacing at end of line. */
2347 x -= gx;
2348 gx += (x / width) * width;
2349 }
2350
2351 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2352 gx += window_box_left_offset (w, area);
2353 }
2354 else
2355 {
2356 /* Use nominal line height at end of window. */
2357 gx = (x / width) * width;
2358 y -= gy;
2359 gy += (y / height) * height;
2360 }
2361 break;
2362
2363 case ON_LEFT_FRINGE:
2364 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2365 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2366 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2367 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2368 goto row_glyph;
2369
2370 case ON_RIGHT_FRINGE:
2371 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2372 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2373 : window_box_right_offset (w, TEXT_AREA));
2374 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2375 goto row_glyph;
2376
2377 case ON_SCROLL_BAR:
2378 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2379 ? 0
2380 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2381 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2382 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2383 : 0)));
2384 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2385
2386 row_glyph:
2387 gr = 0, gy = 0;
2388 for (; r <= end_row && r->enabled_p; ++r)
2389 if (r->y + r->height > y)
2390 {
2391 gr = r; gy = r->y;
2392 break;
2393 }
2394
2395 if (gr && gy <= y)
2396 height = gr->height;
2397 else
2398 {
2399 /* Use nominal line height at end of window. */
2400 y -= gy;
2401 gy += (y / height) * height;
2402 }
2403 break;
2404
2405 default:
2406 ;
2407 virtual_glyph:
2408 /* If there is no glyph under the mouse, then we divide the screen
2409 into a grid of the smallest glyph in the frame, and use that
2410 as our "glyph". */
2411
2412 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2413 round down even for negative values. */
2414 if (gx < 0)
2415 gx -= width - 1;
2416 if (gy < 0)
2417 gy -= height - 1;
2418
2419 gx = (gx / width) * width;
2420 gy = (gy / height) * height;
2421
2422 goto store_rect;
2423 }
2424
2425 gx += WINDOW_LEFT_EDGE_X (w);
2426 gy += WINDOW_TOP_EDGE_Y (w);
2427
2428 store_rect:
2429 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2430
2431 /* Visible feedback for debugging. */
2432 #if 0
2433 #if HAVE_X_WINDOWS
2434 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2435 f->output_data.x->normal_gc,
2436 gx, gy, width, height);
2437 #endif
2438 #endif
2439 }
2440
2441
2442 #endif /* HAVE_WINDOW_SYSTEM */
2443
2444 static void
2445 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2446 {
2447 eassert (w);
2448 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2449 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2450 w->window_end_vpos
2451 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2452 }
2453
2454 /***********************************************************************
2455 Lisp form evaluation
2456 ***********************************************************************/
2457
2458 /* Error handler for safe_eval and safe_call. */
2459
2460 static Lisp_Object
2461 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2462 {
2463 add_to_log ("Error during redisplay: %S signaled %S",
2464 Flist (nargs, args), arg);
2465 return Qnil;
2466 }
2467
2468 /* Call function FUNC with the rest of NARGS - 1 arguments
2469 following. Return the result, or nil if something went
2470 wrong. Prevent redisplay during the evaluation. */
2471
2472 Lisp_Object
2473 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2474 {
2475 Lisp_Object val;
2476
2477 if (inhibit_eval_during_redisplay)
2478 val = Qnil;
2479 else
2480 {
2481 va_list ap;
2482 ptrdiff_t i;
2483 ptrdiff_t count = SPECPDL_INDEX ();
2484 struct gcpro gcpro1;
2485 Lisp_Object *args = alloca (nargs * word_size);
2486
2487 args[0] = func;
2488 va_start (ap, func);
2489 for (i = 1; i < nargs; i++)
2490 args[i] = va_arg (ap, Lisp_Object);
2491 va_end (ap);
2492
2493 GCPRO1 (args[0]);
2494 gcpro1.nvars = nargs;
2495 specbind (Qinhibit_redisplay, Qt);
2496 /* Use Qt to ensure debugger does not run,
2497 so there is no possibility of wanting to redisplay. */
2498 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2499 safe_eval_handler);
2500 UNGCPRO;
2501 val = unbind_to (count, val);
2502 }
2503
2504 return val;
2505 }
2506
2507
2508 /* Call function FN with one argument ARG.
2509 Return the result, or nil if something went wrong. */
2510
2511 Lisp_Object
2512 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2513 {
2514 return safe_call (2, fn, arg);
2515 }
2516
2517 static Lisp_Object Qeval;
2518
2519 Lisp_Object
2520 safe_eval (Lisp_Object sexpr)
2521 {
2522 return safe_call1 (Qeval, sexpr);
2523 }
2524
2525 /* Call function FN with two arguments ARG1 and ARG2.
2526 Return the result, or nil if something went wrong. */
2527
2528 Lisp_Object
2529 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2530 {
2531 return safe_call (3, fn, arg1, arg2);
2532 }
2533
2534
2535 \f
2536 /***********************************************************************
2537 Debugging
2538 ***********************************************************************/
2539
2540 #if 0
2541
2542 /* Define CHECK_IT to perform sanity checks on iterators.
2543 This is for debugging. It is too slow to do unconditionally. */
2544
2545 static void
2546 check_it (struct it *it)
2547 {
2548 if (it->method == GET_FROM_STRING)
2549 {
2550 eassert (STRINGP (it->string));
2551 eassert (IT_STRING_CHARPOS (*it) >= 0);
2552 }
2553 else
2554 {
2555 eassert (IT_STRING_CHARPOS (*it) < 0);
2556 if (it->method == GET_FROM_BUFFER)
2557 {
2558 /* Check that character and byte positions agree. */
2559 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2560 }
2561 }
2562
2563 if (it->dpvec)
2564 eassert (it->current.dpvec_index >= 0);
2565 else
2566 eassert (it->current.dpvec_index < 0);
2567 }
2568
2569 #define CHECK_IT(IT) check_it ((IT))
2570
2571 #else /* not 0 */
2572
2573 #define CHECK_IT(IT) (void) 0
2574
2575 #endif /* not 0 */
2576
2577
2578 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2579
2580 /* Check that the window end of window W is what we expect it
2581 to be---the last row in the current matrix displaying text. */
2582
2583 static void
2584 check_window_end (struct window *w)
2585 {
2586 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2587 {
2588 struct glyph_row *row;
2589 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2590 !row->enabled_p
2591 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2592 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2593 }
2594 }
2595
2596 #define CHECK_WINDOW_END(W) check_window_end ((W))
2597
2598 #else
2599
2600 #define CHECK_WINDOW_END(W) (void) 0
2601
2602 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2603
2604 /* Return mark position if current buffer has the region of non-zero length,
2605 or -1 otherwise. */
2606
2607 static ptrdiff_t
2608 markpos_of_region (void)
2609 {
2610 if (!NILP (Vtransient_mark_mode)
2611 && !NILP (BVAR (current_buffer, mark_active))
2612 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2613 {
2614 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2615
2616 if (markpos != PT)
2617 return markpos;
2618 }
2619 return -1;
2620 }
2621
2622 /***********************************************************************
2623 Iterator initialization
2624 ***********************************************************************/
2625
2626 /* Initialize IT for displaying current_buffer in window W, starting
2627 at character position CHARPOS. CHARPOS < 0 means that no buffer
2628 position is specified which is useful when the iterator is assigned
2629 a position later. BYTEPOS is the byte position corresponding to
2630 CHARPOS.
2631
2632 If ROW is not null, calls to produce_glyphs with IT as parameter
2633 will produce glyphs in that row.
2634
2635 BASE_FACE_ID is the id of a base face to use. It must be one of
2636 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2637 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2638 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2639
2640 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2641 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2642 will be initialized to use the corresponding mode line glyph row of
2643 the desired matrix of W. */
2644
2645 void
2646 init_iterator (struct it *it, struct window *w,
2647 ptrdiff_t charpos, ptrdiff_t bytepos,
2648 struct glyph_row *row, enum face_id base_face_id)
2649 {
2650 ptrdiff_t markpos;
2651 enum face_id remapped_base_face_id = base_face_id;
2652
2653 /* Some precondition checks. */
2654 eassert (w != NULL && it != NULL);
2655 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2656 && charpos <= ZV));
2657
2658 /* If face attributes have been changed since the last redisplay,
2659 free realized faces now because they depend on face definitions
2660 that might have changed. Don't free faces while there might be
2661 desired matrices pending which reference these faces. */
2662 if (face_change_count && !inhibit_free_realized_faces)
2663 {
2664 face_change_count = 0;
2665 free_all_realized_faces (Qnil);
2666 }
2667
2668 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2669 if (! NILP (Vface_remapping_alist))
2670 remapped_base_face_id
2671 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2672
2673 /* Use one of the mode line rows of W's desired matrix if
2674 appropriate. */
2675 if (row == NULL)
2676 {
2677 if (base_face_id == MODE_LINE_FACE_ID
2678 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2679 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2680 else if (base_face_id == HEADER_LINE_FACE_ID)
2681 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2682 }
2683
2684 /* Clear IT. */
2685 memset (it, 0, sizeof *it);
2686 it->current.overlay_string_index = -1;
2687 it->current.dpvec_index = -1;
2688 it->base_face_id = remapped_base_face_id;
2689 it->string = Qnil;
2690 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2691 it->paragraph_embedding = L2R;
2692 it->bidi_it.string.lstring = Qnil;
2693 it->bidi_it.string.s = NULL;
2694 it->bidi_it.string.bufpos = 0;
2695 it->bidi_it.w = w;
2696
2697 /* The window in which we iterate over current_buffer: */
2698 XSETWINDOW (it->window, w);
2699 it->w = w;
2700 it->f = XFRAME (w->frame);
2701
2702 it->cmp_it.id = -1;
2703
2704 /* Extra space between lines (on window systems only). */
2705 if (base_face_id == DEFAULT_FACE_ID
2706 && FRAME_WINDOW_P (it->f))
2707 {
2708 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2709 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2710 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2711 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2712 * FRAME_LINE_HEIGHT (it->f));
2713 else if (it->f->extra_line_spacing > 0)
2714 it->extra_line_spacing = it->f->extra_line_spacing;
2715 it->max_extra_line_spacing = 0;
2716 }
2717
2718 /* If realized faces have been removed, e.g. because of face
2719 attribute changes of named faces, recompute them. When running
2720 in batch mode, the face cache of the initial frame is null. If
2721 we happen to get called, make a dummy face cache. */
2722 if (FRAME_FACE_CACHE (it->f) == NULL)
2723 init_frame_faces (it->f);
2724 if (FRAME_FACE_CACHE (it->f)->used == 0)
2725 recompute_basic_faces (it->f);
2726
2727 /* Current value of the `slice', `space-width', and 'height' properties. */
2728 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2729 it->space_width = Qnil;
2730 it->font_height = Qnil;
2731 it->override_ascent = -1;
2732
2733 /* Are control characters displayed as `^C'? */
2734 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2735
2736 /* -1 means everything between a CR and the following line end
2737 is invisible. >0 means lines indented more than this value are
2738 invisible. */
2739 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2740 ? (clip_to_bounds
2741 (-1, XINT (BVAR (current_buffer, selective_display)),
2742 PTRDIFF_MAX))
2743 : (!NILP (BVAR (current_buffer, selective_display))
2744 ? -1 : 0));
2745 it->selective_display_ellipsis_p
2746 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2747
2748 /* Display table to use. */
2749 it->dp = window_display_table (w);
2750
2751 /* Are multibyte characters enabled in current_buffer? */
2752 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2753
2754 /* If visible region is of non-zero length, set IT->region_beg_charpos
2755 and IT->region_end_charpos to the start and end of a visible region
2756 in window IT->w. Set both to -1 to indicate no region. */
2757 markpos = markpos_of_region ();
2758 if (markpos >= 0
2759 /* Maybe highlight only in selected window. */
2760 && (/* Either show region everywhere. */
2761 highlight_nonselected_windows
2762 /* Or show region in the selected window. */
2763 || w == XWINDOW (selected_window)
2764 /* Or show the region if we are in the mini-buffer and W is
2765 the window the mini-buffer refers to. */
2766 || (MINI_WINDOW_P (XWINDOW (selected_window))
2767 && WINDOWP (minibuf_selected_window)
2768 && w == XWINDOW (minibuf_selected_window))))
2769 {
2770 it->region_beg_charpos = min (PT, markpos);
2771 it->region_end_charpos = max (PT, markpos);
2772 }
2773 else
2774 it->region_beg_charpos = it->region_end_charpos = -1;
2775
2776 /* Get the position at which the redisplay_end_trigger hook should
2777 be run, if it is to be run at all. */
2778 if (MARKERP (w->redisplay_end_trigger)
2779 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2780 it->redisplay_end_trigger_charpos
2781 = marker_position (w->redisplay_end_trigger);
2782 else if (INTEGERP (w->redisplay_end_trigger))
2783 it->redisplay_end_trigger_charpos =
2784 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2785
2786 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2787
2788 /* Are lines in the display truncated? */
2789 if (base_face_id != DEFAULT_FACE_ID
2790 || it->w->hscroll
2791 || (! WINDOW_FULL_WIDTH_P (it->w)
2792 && ((!NILP (Vtruncate_partial_width_windows)
2793 && !INTEGERP (Vtruncate_partial_width_windows))
2794 || (INTEGERP (Vtruncate_partial_width_windows)
2795 && (WINDOW_TOTAL_COLS (it->w)
2796 < XINT (Vtruncate_partial_width_windows))))))
2797 it->line_wrap = TRUNCATE;
2798 else if (NILP (BVAR (current_buffer, truncate_lines)))
2799 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2800 ? WINDOW_WRAP : WORD_WRAP;
2801 else
2802 it->line_wrap = TRUNCATE;
2803
2804 /* Get dimensions of truncation and continuation glyphs. These are
2805 displayed as fringe bitmaps under X, but we need them for such
2806 frames when the fringes are turned off. But leave the dimensions
2807 zero for tooltip frames, as these glyphs look ugly there and also
2808 sabotage calculations of tooltip dimensions in x-show-tip. */
2809 #ifdef HAVE_WINDOW_SYSTEM
2810 if (!(FRAME_WINDOW_P (it->f)
2811 && FRAMEP (tip_frame)
2812 && it->f == XFRAME (tip_frame)))
2813 #endif
2814 {
2815 if (it->line_wrap == TRUNCATE)
2816 {
2817 /* We will need the truncation glyph. */
2818 eassert (it->glyph_row == NULL);
2819 produce_special_glyphs (it, IT_TRUNCATION);
2820 it->truncation_pixel_width = it->pixel_width;
2821 }
2822 else
2823 {
2824 /* We will need the continuation glyph. */
2825 eassert (it->glyph_row == NULL);
2826 produce_special_glyphs (it, IT_CONTINUATION);
2827 it->continuation_pixel_width = it->pixel_width;
2828 }
2829 }
2830
2831 /* Reset these values to zero because the produce_special_glyphs
2832 above has changed them. */
2833 it->pixel_width = it->ascent = it->descent = 0;
2834 it->phys_ascent = it->phys_descent = 0;
2835
2836 /* Set this after getting the dimensions of truncation and
2837 continuation glyphs, so that we don't produce glyphs when calling
2838 produce_special_glyphs, above. */
2839 it->glyph_row = row;
2840 it->area = TEXT_AREA;
2841
2842 /* Forget any previous info about this row being reversed. */
2843 if (it->glyph_row)
2844 it->glyph_row->reversed_p = 0;
2845
2846 /* Get the dimensions of the display area. The display area
2847 consists of the visible window area plus a horizontally scrolled
2848 part to the left of the window. All x-values are relative to the
2849 start of this total display area. */
2850 if (base_face_id != DEFAULT_FACE_ID)
2851 {
2852 /* Mode lines, menu bar in terminal frames. */
2853 it->first_visible_x = 0;
2854 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2855 }
2856 else
2857 {
2858 it->first_visible_x =
2859 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2860 it->last_visible_x = (it->first_visible_x
2861 + window_box_width (w, TEXT_AREA));
2862
2863 /* If we truncate lines, leave room for the truncation glyph(s) at
2864 the right margin. Otherwise, leave room for the continuation
2865 glyph(s). Done only if the window has no fringes. Since we
2866 don't know at this point whether there will be any R2L lines in
2867 the window, we reserve space for truncation/continuation glyphs
2868 even if only one of the fringes is absent. */
2869 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2870 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2871 {
2872 if (it->line_wrap == TRUNCATE)
2873 it->last_visible_x -= it->truncation_pixel_width;
2874 else
2875 it->last_visible_x -= it->continuation_pixel_width;
2876 }
2877
2878 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2879 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2880 }
2881
2882 /* Leave room for a border glyph. */
2883 if (!FRAME_WINDOW_P (it->f)
2884 && !WINDOW_RIGHTMOST_P (it->w))
2885 it->last_visible_x -= 1;
2886
2887 it->last_visible_y = window_text_bottom_y (w);
2888
2889 /* For mode lines and alike, arrange for the first glyph having a
2890 left box line if the face specifies a box. */
2891 if (base_face_id != DEFAULT_FACE_ID)
2892 {
2893 struct face *face;
2894
2895 it->face_id = remapped_base_face_id;
2896
2897 /* If we have a boxed mode line, make the first character appear
2898 with a left box line. */
2899 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2900 if (face->box != FACE_NO_BOX)
2901 it->start_of_box_run_p = 1;
2902 }
2903
2904 /* If a buffer position was specified, set the iterator there,
2905 getting overlays and face properties from that position. */
2906 if (charpos >= BUF_BEG (current_buffer))
2907 {
2908 it->end_charpos = ZV;
2909 eassert (charpos == BYTE_TO_CHAR (bytepos));
2910 IT_CHARPOS (*it) = charpos;
2911 IT_BYTEPOS (*it) = bytepos;
2912
2913 /* We will rely on `reseat' to set this up properly, via
2914 handle_face_prop. */
2915 it->face_id = it->base_face_id;
2916
2917 it->start = it->current;
2918 /* Do we need to reorder bidirectional text? Not if this is a
2919 unibyte buffer: by definition, none of the single-byte
2920 characters are strong R2L, so no reordering is needed. And
2921 bidi.c doesn't support unibyte buffers anyway. Also, don't
2922 reorder while we are loading loadup.el, since the tables of
2923 character properties needed for reordering are not yet
2924 available. */
2925 it->bidi_p =
2926 NILP (Vpurify_flag)
2927 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2928 && it->multibyte_p;
2929
2930 /* If we are to reorder bidirectional text, init the bidi
2931 iterator. */
2932 if (it->bidi_p)
2933 {
2934 /* Note the paragraph direction that this buffer wants to
2935 use. */
2936 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2937 Qleft_to_right))
2938 it->paragraph_embedding = L2R;
2939 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2940 Qright_to_left))
2941 it->paragraph_embedding = R2L;
2942 else
2943 it->paragraph_embedding = NEUTRAL_DIR;
2944 bidi_unshelve_cache (NULL, 0);
2945 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2946 &it->bidi_it);
2947 }
2948
2949 /* Compute faces etc. */
2950 reseat (it, it->current.pos, 1);
2951 }
2952
2953 CHECK_IT (it);
2954 }
2955
2956
2957 /* Initialize IT for the display of window W with window start POS. */
2958
2959 void
2960 start_display (struct it *it, struct window *w, struct text_pos pos)
2961 {
2962 struct glyph_row *row;
2963 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2964
2965 row = w->desired_matrix->rows + first_vpos;
2966 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2967 it->first_vpos = first_vpos;
2968
2969 /* Don't reseat to previous visible line start if current start
2970 position is in a string or image. */
2971 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2972 {
2973 int start_at_line_beg_p;
2974 int first_y = it->current_y;
2975
2976 /* If window start is not at a line start, skip forward to POS to
2977 get the correct continuation lines width. */
2978 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2979 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2980 if (!start_at_line_beg_p)
2981 {
2982 int new_x;
2983
2984 reseat_at_previous_visible_line_start (it);
2985 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2986
2987 new_x = it->current_x + it->pixel_width;
2988
2989 /* If lines are continued, this line may end in the middle
2990 of a multi-glyph character (e.g. a control character
2991 displayed as \003, or in the middle of an overlay
2992 string). In this case move_it_to above will not have
2993 taken us to the start of the continuation line but to the
2994 end of the continued line. */
2995 if (it->current_x > 0
2996 && it->line_wrap != TRUNCATE /* Lines are continued. */
2997 && (/* And glyph doesn't fit on the line. */
2998 new_x > it->last_visible_x
2999 /* Or it fits exactly and we're on a window
3000 system frame. */
3001 || (new_x == it->last_visible_x
3002 && FRAME_WINDOW_P (it->f)
3003 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3004 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3005 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3006 {
3007 if ((it->current.dpvec_index >= 0
3008 || it->current.overlay_string_index >= 0)
3009 /* If we are on a newline from a display vector or
3010 overlay string, then we are already at the end of
3011 a screen line; no need to go to the next line in
3012 that case, as this line is not really continued.
3013 (If we do go to the next line, C-e will not DTRT.) */
3014 && it->c != '\n')
3015 {
3016 set_iterator_to_next (it, 1);
3017 move_it_in_display_line_to (it, -1, -1, 0);
3018 }
3019
3020 it->continuation_lines_width += it->current_x;
3021 }
3022 /* If the character at POS is displayed via a display
3023 vector, move_it_to above stops at the final glyph of
3024 IT->dpvec. To make the caller redisplay that character
3025 again (a.k.a. start at POS), we need to reset the
3026 dpvec_index to the beginning of IT->dpvec. */
3027 else if (it->current.dpvec_index >= 0)
3028 it->current.dpvec_index = 0;
3029
3030 /* We're starting a new display line, not affected by the
3031 height of the continued line, so clear the appropriate
3032 fields in the iterator structure. */
3033 it->max_ascent = it->max_descent = 0;
3034 it->max_phys_ascent = it->max_phys_descent = 0;
3035
3036 it->current_y = first_y;
3037 it->vpos = 0;
3038 it->current_x = it->hpos = 0;
3039 }
3040 }
3041 }
3042
3043
3044 /* Return 1 if POS is a position in ellipses displayed for invisible
3045 text. W is the window we display, for text property lookup. */
3046
3047 static int
3048 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3049 {
3050 Lisp_Object prop, window;
3051 int ellipses_p = 0;
3052 ptrdiff_t charpos = CHARPOS (pos->pos);
3053
3054 /* If POS specifies a position in a display vector, this might
3055 be for an ellipsis displayed for invisible text. We won't
3056 get the iterator set up for delivering that ellipsis unless
3057 we make sure that it gets aware of the invisible text. */
3058 if (pos->dpvec_index >= 0
3059 && pos->overlay_string_index < 0
3060 && CHARPOS (pos->string_pos) < 0
3061 && charpos > BEGV
3062 && (XSETWINDOW (window, w),
3063 prop = Fget_char_property (make_number (charpos),
3064 Qinvisible, window),
3065 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3066 {
3067 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3068 window);
3069 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3070 }
3071
3072 return ellipses_p;
3073 }
3074
3075
3076 /* Initialize IT for stepping through current_buffer in window W,
3077 starting at position POS that includes overlay string and display
3078 vector/ control character translation position information. Value
3079 is zero if there are overlay strings with newlines at POS. */
3080
3081 static int
3082 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3083 {
3084 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3085 int i, overlay_strings_with_newlines = 0;
3086
3087 /* If POS specifies a position in a display vector, this might
3088 be for an ellipsis displayed for invisible text. We won't
3089 get the iterator set up for delivering that ellipsis unless
3090 we make sure that it gets aware of the invisible text. */
3091 if (in_ellipses_for_invisible_text_p (pos, w))
3092 {
3093 --charpos;
3094 bytepos = 0;
3095 }
3096
3097 /* Keep in mind: the call to reseat in init_iterator skips invisible
3098 text, so we might end up at a position different from POS. This
3099 is only a problem when POS is a row start after a newline and an
3100 overlay starts there with an after-string, and the overlay has an
3101 invisible property. Since we don't skip invisible text in
3102 display_line and elsewhere immediately after consuming the
3103 newline before the row start, such a POS will not be in a string,
3104 but the call to init_iterator below will move us to the
3105 after-string. */
3106 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3107
3108 /* This only scans the current chunk -- it should scan all chunks.
3109 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3110 to 16 in 22.1 to make this a lesser problem. */
3111 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3112 {
3113 const char *s = SSDATA (it->overlay_strings[i]);
3114 const char *e = s + SBYTES (it->overlay_strings[i]);
3115
3116 while (s < e && *s != '\n')
3117 ++s;
3118
3119 if (s < e)
3120 {
3121 overlay_strings_with_newlines = 1;
3122 break;
3123 }
3124 }
3125
3126 /* If position is within an overlay string, set up IT to the right
3127 overlay string. */
3128 if (pos->overlay_string_index >= 0)
3129 {
3130 int relative_index;
3131
3132 /* If the first overlay string happens to have a `display'
3133 property for an image, the iterator will be set up for that
3134 image, and we have to undo that setup first before we can
3135 correct the overlay string index. */
3136 if (it->method == GET_FROM_IMAGE)
3137 pop_it (it);
3138
3139 /* We already have the first chunk of overlay strings in
3140 IT->overlay_strings. Load more until the one for
3141 pos->overlay_string_index is in IT->overlay_strings. */
3142 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3143 {
3144 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3145 it->current.overlay_string_index = 0;
3146 while (n--)
3147 {
3148 load_overlay_strings (it, 0);
3149 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3150 }
3151 }
3152
3153 it->current.overlay_string_index = pos->overlay_string_index;
3154 relative_index = (it->current.overlay_string_index
3155 % OVERLAY_STRING_CHUNK_SIZE);
3156 it->string = it->overlay_strings[relative_index];
3157 eassert (STRINGP (it->string));
3158 it->current.string_pos = pos->string_pos;
3159 it->method = GET_FROM_STRING;
3160 it->end_charpos = SCHARS (it->string);
3161 /* Set up the bidi iterator for this overlay string. */
3162 if (it->bidi_p)
3163 {
3164 it->bidi_it.string.lstring = it->string;
3165 it->bidi_it.string.s = NULL;
3166 it->bidi_it.string.schars = SCHARS (it->string);
3167 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3168 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3169 it->bidi_it.string.unibyte = !it->multibyte_p;
3170 it->bidi_it.w = it->w;
3171 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3172 FRAME_WINDOW_P (it->f), &it->bidi_it);
3173
3174 /* Synchronize the state of the bidi iterator with
3175 pos->string_pos. For any string position other than
3176 zero, this will be done automagically when we resume
3177 iteration over the string and get_visually_first_element
3178 is called. But if string_pos is zero, and the string is
3179 to be reordered for display, we need to resync manually,
3180 since it could be that the iteration state recorded in
3181 pos ended at string_pos of 0 moving backwards in string. */
3182 if (CHARPOS (pos->string_pos) == 0)
3183 {
3184 get_visually_first_element (it);
3185 if (IT_STRING_CHARPOS (*it) != 0)
3186 do {
3187 /* Paranoia. */
3188 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3189 bidi_move_to_visually_next (&it->bidi_it);
3190 } while (it->bidi_it.charpos != 0);
3191 }
3192 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3193 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3194 }
3195 }
3196
3197 if (CHARPOS (pos->string_pos) >= 0)
3198 {
3199 /* Recorded position is not in an overlay string, but in another
3200 string. This can only be a string from a `display' property.
3201 IT should already be filled with that string. */
3202 it->current.string_pos = pos->string_pos;
3203 eassert (STRINGP (it->string));
3204 if (it->bidi_p)
3205 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3206 FRAME_WINDOW_P (it->f), &it->bidi_it);
3207 }
3208
3209 /* Restore position in display vector translations, control
3210 character translations or ellipses. */
3211 if (pos->dpvec_index >= 0)
3212 {
3213 if (it->dpvec == NULL)
3214 get_next_display_element (it);
3215 eassert (it->dpvec && it->current.dpvec_index == 0);
3216 it->current.dpvec_index = pos->dpvec_index;
3217 }
3218
3219 CHECK_IT (it);
3220 return !overlay_strings_with_newlines;
3221 }
3222
3223
3224 /* Initialize IT for stepping through current_buffer in window W
3225 starting at ROW->start. */
3226
3227 static void
3228 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3229 {
3230 init_from_display_pos (it, w, &row->start);
3231 it->start = row->start;
3232 it->continuation_lines_width = row->continuation_lines_width;
3233 CHECK_IT (it);
3234 }
3235
3236
3237 /* Initialize IT for stepping through current_buffer in window W
3238 starting in the line following ROW, i.e. starting at ROW->end.
3239 Value is zero if there are overlay strings with newlines at ROW's
3240 end position. */
3241
3242 static int
3243 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3244 {
3245 int success = 0;
3246
3247 if (init_from_display_pos (it, w, &row->end))
3248 {
3249 if (row->continued_p)
3250 it->continuation_lines_width
3251 = row->continuation_lines_width + row->pixel_width;
3252 CHECK_IT (it);
3253 success = 1;
3254 }
3255
3256 return success;
3257 }
3258
3259
3260
3261 \f
3262 /***********************************************************************
3263 Text properties
3264 ***********************************************************************/
3265
3266 /* Called when IT reaches IT->stop_charpos. Handle text property and
3267 overlay changes. Set IT->stop_charpos to the next position where
3268 to stop. */
3269
3270 static void
3271 handle_stop (struct it *it)
3272 {
3273 enum prop_handled handled;
3274 int handle_overlay_change_p;
3275 struct props *p;
3276
3277 it->dpvec = NULL;
3278 it->current.dpvec_index = -1;
3279 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3280 it->ignore_overlay_strings_at_pos_p = 0;
3281 it->ellipsis_p = 0;
3282
3283 /* Use face of preceding text for ellipsis (if invisible) */
3284 if (it->selective_display_ellipsis_p)
3285 it->saved_face_id = it->face_id;
3286
3287 do
3288 {
3289 handled = HANDLED_NORMALLY;
3290
3291 /* Call text property handlers. */
3292 for (p = it_props; p->handler; ++p)
3293 {
3294 handled = p->handler (it);
3295
3296 if (handled == HANDLED_RECOMPUTE_PROPS)
3297 break;
3298 else if (handled == HANDLED_RETURN)
3299 {
3300 /* We still want to show before and after strings from
3301 overlays even if the actual buffer text is replaced. */
3302 if (!handle_overlay_change_p
3303 || it->sp > 1
3304 /* Don't call get_overlay_strings_1 if we already
3305 have overlay strings loaded, because doing so
3306 will load them again and push the iterator state
3307 onto the stack one more time, which is not
3308 expected by the rest of the code that processes
3309 overlay strings. */
3310 || (it->current.overlay_string_index < 0
3311 ? !get_overlay_strings_1 (it, 0, 0)
3312 : 0))
3313 {
3314 if (it->ellipsis_p)
3315 setup_for_ellipsis (it, 0);
3316 /* When handling a display spec, we might load an
3317 empty string. In that case, discard it here. We
3318 used to discard it in handle_single_display_spec,
3319 but that causes get_overlay_strings_1, above, to
3320 ignore overlay strings that we must check. */
3321 if (STRINGP (it->string) && !SCHARS (it->string))
3322 pop_it (it);
3323 return;
3324 }
3325 else if (STRINGP (it->string) && !SCHARS (it->string))
3326 pop_it (it);
3327 else
3328 {
3329 it->ignore_overlay_strings_at_pos_p = 1;
3330 it->string_from_display_prop_p = 0;
3331 it->from_disp_prop_p = 0;
3332 handle_overlay_change_p = 0;
3333 }
3334 handled = HANDLED_RECOMPUTE_PROPS;
3335 break;
3336 }
3337 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3338 handle_overlay_change_p = 0;
3339 }
3340
3341 if (handled != HANDLED_RECOMPUTE_PROPS)
3342 {
3343 /* Don't check for overlay strings below when set to deliver
3344 characters from a display vector. */
3345 if (it->method == GET_FROM_DISPLAY_VECTOR)
3346 handle_overlay_change_p = 0;
3347
3348 /* Handle overlay changes.
3349 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3350 if it finds overlays. */
3351 if (handle_overlay_change_p)
3352 handled = handle_overlay_change (it);
3353 }
3354
3355 if (it->ellipsis_p)
3356 {
3357 setup_for_ellipsis (it, 0);
3358 break;
3359 }
3360 }
3361 while (handled == HANDLED_RECOMPUTE_PROPS);
3362
3363 /* Determine where to stop next. */
3364 if (handled == HANDLED_NORMALLY)
3365 compute_stop_pos (it);
3366 }
3367
3368
3369 /* Compute IT->stop_charpos from text property and overlay change
3370 information for IT's current position. */
3371
3372 static void
3373 compute_stop_pos (struct it *it)
3374 {
3375 register INTERVAL iv, next_iv;
3376 Lisp_Object object, limit, position;
3377 ptrdiff_t charpos, bytepos;
3378
3379 if (STRINGP (it->string))
3380 {
3381 /* Strings are usually short, so don't limit the search for
3382 properties. */
3383 it->stop_charpos = it->end_charpos;
3384 object = it->string;
3385 limit = Qnil;
3386 charpos = IT_STRING_CHARPOS (*it);
3387 bytepos = IT_STRING_BYTEPOS (*it);
3388 }
3389 else
3390 {
3391 ptrdiff_t pos;
3392
3393 /* If end_charpos is out of range for some reason, such as a
3394 misbehaving display function, rationalize it (Bug#5984). */
3395 if (it->end_charpos > ZV)
3396 it->end_charpos = ZV;
3397 it->stop_charpos = it->end_charpos;
3398
3399 /* If next overlay change is in front of the current stop pos
3400 (which is IT->end_charpos), stop there. Note: value of
3401 next_overlay_change is point-max if no overlay change
3402 follows. */
3403 charpos = IT_CHARPOS (*it);
3404 bytepos = IT_BYTEPOS (*it);
3405 pos = next_overlay_change (charpos);
3406 if (pos < it->stop_charpos)
3407 it->stop_charpos = pos;
3408
3409 /* If showing the region, we have to stop at the region
3410 start or end because the face might change there. */
3411 if (it->region_beg_charpos > 0)
3412 {
3413 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3414 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3415 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3416 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3417 }
3418
3419 /* Set up variables for computing the stop position from text
3420 property changes. */
3421 XSETBUFFER (object, current_buffer);
3422 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3423 }
3424
3425 /* Get the interval containing IT's position. Value is a null
3426 interval if there isn't such an interval. */
3427 position = make_number (charpos);
3428 iv = validate_interval_range (object, &position, &position, 0);
3429 if (iv)
3430 {
3431 Lisp_Object values_here[LAST_PROP_IDX];
3432 struct props *p;
3433
3434 /* Get properties here. */
3435 for (p = it_props; p->handler; ++p)
3436 values_here[p->idx] = textget (iv->plist, *p->name);
3437
3438 /* Look for an interval following iv that has different
3439 properties. */
3440 for (next_iv = next_interval (iv);
3441 (next_iv
3442 && (NILP (limit)
3443 || XFASTINT (limit) > next_iv->position));
3444 next_iv = next_interval (next_iv))
3445 {
3446 for (p = it_props; p->handler; ++p)
3447 {
3448 Lisp_Object new_value;
3449
3450 new_value = textget (next_iv->plist, *p->name);
3451 if (!EQ (values_here[p->idx], new_value))
3452 break;
3453 }
3454
3455 if (p->handler)
3456 break;
3457 }
3458
3459 if (next_iv)
3460 {
3461 if (INTEGERP (limit)
3462 && next_iv->position >= XFASTINT (limit))
3463 /* No text property change up to limit. */
3464 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3465 else
3466 /* Text properties change in next_iv. */
3467 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3468 }
3469 }
3470
3471 if (it->cmp_it.id < 0)
3472 {
3473 ptrdiff_t stoppos = it->end_charpos;
3474
3475 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3476 stoppos = -1;
3477 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3478 stoppos, it->string);
3479 }
3480
3481 eassert (STRINGP (it->string)
3482 || (it->stop_charpos >= BEGV
3483 && it->stop_charpos >= IT_CHARPOS (*it)));
3484 }
3485
3486
3487 /* Return the position of the next overlay change after POS in
3488 current_buffer. Value is point-max if no overlay change
3489 follows. This is like `next-overlay-change' but doesn't use
3490 xmalloc. */
3491
3492 static ptrdiff_t
3493 next_overlay_change (ptrdiff_t pos)
3494 {
3495 ptrdiff_t i, noverlays;
3496 ptrdiff_t endpos;
3497 Lisp_Object *overlays;
3498
3499 /* Get all overlays at the given position. */
3500 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3501
3502 /* If any of these overlays ends before endpos,
3503 use its ending point instead. */
3504 for (i = 0; i < noverlays; ++i)
3505 {
3506 Lisp_Object oend;
3507 ptrdiff_t oendpos;
3508
3509 oend = OVERLAY_END (overlays[i]);
3510 oendpos = OVERLAY_POSITION (oend);
3511 endpos = min (endpos, oendpos);
3512 }
3513
3514 return endpos;
3515 }
3516
3517 /* How many characters forward to search for a display property or
3518 display string. Searching too far forward makes the bidi display
3519 sluggish, especially in small windows. */
3520 #define MAX_DISP_SCAN 250
3521
3522 /* Return the character position of a display string at or after
3523 position specified by POSITION. If no display string exists at or
3524 after POSITION, return ZV. A display string is either an overlay
3525 with `display' property whose value is a string, or a `display'
3526 text property whose value is a string. STRING is data about the
3527 string to iterate; if STRING->lstring is nil, we are iterating a
3528 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3529 on a GUI frame. DISP_PROP is set to zero if we searched
3530 MAX_DISP_SCAN characters forward without finding any display
3531 strings, non-zero otherwise. It is set to 2 if the display string
3532 uses any kind of `(space ...)' spec that will produce a stretch of
3533 white space in the text area. */
3534 ptrdiff_t
3535 compute_display_string_pos (struct text_pos *position,
3536 struct bidi_string_data *string,
3537 struct window *w,
3538 int frame_window_p, int *disp_prop)
3539 {
3540 /* OBJECT = nil means current buffer. */
3541 Lisp_Object object, object1;
3542 Lisp_Object pos, spec, limpos;
3543 int string_p = (string && (STRINGP (string->lstring) || string->s));
3544 ptrdiff_t eob = string_p ? string->schars : ZV;
3545 ptrdiff_t begb = string_p ? 0 : BEGV;
3546 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3547 ptrdiff_t lim =
3548 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3549 struct text_pos tpos;
3550 int rv = 0;
3551
3552 if (string && STRINGP (string->lstring))
3553 object1 = object = string->lstring;
3554 else if (w && !string_p)
3555 {
3556 XSETWINDOW (object, w);
3557 object1 = Qnil;
3558 }
3559 else
3560 object1 = object = Qnil;
3561
3562 *disp_prop = 1;
3563
3564 if (charpos >= eob
3565 /* We don't support display properties whose values are strings
3566 that have display string properties. */
3567 || string->from_disp_str
3568 /* C strings cannot have display properties. */
3569 || (string->s && !STRINGP (object)))
3570 {
3571 *disp_prop = 0;
3572 return eob;
3573 }
3574
3575 /* If the character at CHARPOS is where the display string begins,
3576 return CHARPOS. */
3577 pos = make_number (charpos);
3578 if (STRINGP (object))
3579 bufpos = string->bufpos;
3580 else
3581 bufpos = charpos;
3582 tpos = *position;
3583 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3584 && (charpos <= begb
3585 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3586 object),
3587 spec))
3588 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3589 frame_window_p)))
3590 {
3591 if (rv == 2)
3592 *disp_prop = 2;
3593 return charpos;
3594 }
3595
3596 /* Look forward for the first character with a `display' property
3597 that will replace the underlying text when displayed. */
3598 limpos = make_number (lim);
3599 do {
3600 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3601 CHARPOS (tpos) = XFASTINT (pos);
3602 if (CHARPOS (tpos) >= lim)
3603 {
3604 *disp_prop = 0;
3605 break;
3606 }
3607 if (STRINGP (object))
3608 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3609 else
3610 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3611 spec = Fget_char_property (pos, Qdisplay, object);
3612 if (!STRINGP (object))
3613 bufpos = CHARPOS (tpos);
3614 } while (NILP (spec)
3615 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3616 bufpos, frame_window_p)));
3617 if (rv == 2)
3618 *disp_prop = 2;
3619
3620 return CHARPOS (tpos);
3621 }
3622
3623 /* Return the character position of the end of the display string that
3624 started at CHARPOS. If there's no display string at CHARPOS,
3625 return -1. A display string is either an overlay with `display'
3626 property whose value is a string or a `display' text property whose
3627 value is a string. */
3628 ptrdiff_t
3629 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3630 {
3631 /* OBJECT = nil means current buffer. */
3632 Lisp_Object object =
3633 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3634 Lisp_Object pos = make_number (charpos);
3635 ptrdiff_t eob =
3636 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3637
3638 if (charpos >= eob || (string->s && !STRINGP (object)))
3639 return eob;
3640
3641 /* It could happen that the display property or overlay was removed
3642 since we found it in compute_display_string_pos above. One way
3643 this can happen is if JIT font-lock was called (through
3644 handle_fontified_prop), and jit-lock-functions remove text
3645 properties or overlays from the portion of buffer that includes
3646 CHARPOS. Muse mode is known to do that, for example. In this
3647 case, we return -1 to the caller, to signal that no display
3648 string is actually present at CHARPOS. See bidi_fetch_char for
3649 how this is handled.
3650
3651 An alternative would be to never look for display properties past
3652 it->stop_charpos. But neither compute_display_string_pos nor
3653 bidi_fetch_char that calls it know or care where the next
3654 stop_charpos is. */
3655 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3656 return -1;
3657
3658 /* Look forward for the first character where the `display' property
3659 changes. */
3660 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3661
3662 return XFASTINT (pos);
3663 }
3664
3665
3666 \f
3667 /***********************************************************************
3668 Fontification
3669 ***********************************************************************/
3670
3671 /* Handle changes in the `fontified' property of the current buffer by
3672 calling hook functions from Qfontification_functions to fontify
3673 regions of text. */
3674
3675 static enum prop_handled
3676 handle_fontified_prop (struct it *it)
3677 {
3678 Lisp_Object prop, pos;
3679 enum prop_handled handled = HANDLED_NORMALLY;
3680
3681 if (!NILP (Vmemory_full))
3682 return handled;
3683
3684 /* Get the value of the `fontified' property at IT's current buffer
3685 position. (The `fontified' property doesn't have a special
3686 meaning in strings.) If the value is nil, call functions from
3687 Qfontification_functions. */
3688 if (!STRINGP (it->string)
3689 && it->s == NULL
3690 && !NILP (Vfontification_functions)
3691 && !NILP (Vrun_hooks)
3692 && (pos = make_number (IT_CHARPOS (*it)),
3693 prop = Fget_char_property (pos, Qfontified, Qnil),
3694 /* Ignore the special cased nil value always present at EOB since
3695 no amount of fontifying will be able to change it. */
3696 NILP (prop) && IT_CHARPOS (*it) < Z))
3697 {
3698 ptrdiff_t count = SPECPDL_INDEX ();
3699 Lisp_Object val;
3700 struct buffer *obuf = current_buffer;
3701 ptrdiff_t begv = BEGV, zv = ZV;
3702 bool old_clip_changed = current_buffer->clip_changed;
3703
3704 val = Vfontification_functions;
3705 specbind (Qfontification_functions, Qnil);
3706
3707 eassert (it->end_charpos == ZV);
3708
3709 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3710 safe_call1 (val, pos);
3711 else
3712 {
3713 Lisp_Object fns, fn;
3714 struct gcpro gcpro1, gcpro2;
3715
3716 fns = Qnil;
3717 GCPRO2 (val, fns);
3718
3719 for (; CONSP (val); val = XCDR (val))
3720 {
3721 fn = XCAR (val);
3722
3723 if (EQ (fn, Qt))
3724 {
3725 /* A value of t indicates this hook has a local
3726 binding; it means to run the global binding too.
3727 In a global value, t should not occur. If it
3728 does, we must ignore it to avoid an endless
3729 loop. */
3730 for (fns = Fdefault_value (Qfontification_functions);
3731 CONSP (fns);
3732 fns = XCDR (fns))
3733 {
3734 fn = XCAR (fns);
3735 if (!EQ (fn, Qt))
3736 safe_call1 (fn, pos);
3737 }
3738 }
3739 else
3740 safe_call1 (fn, pos);
3741 }
3742
3743 UNGCPRO;
3744 }
3745
3746 unbind_to (count, Qnil);
3747
3748 /* Fontification functions routinely call `save-restriction'.
3749 Normally, this tags clip_changed, which can confuse redisplay
3750 (see discussion in Bug#6671). Since we don't perform any
3751 special handling of fontification changes in the case where
3752 `save-restriction' isn't called, there's no point doing so in
3753 this case either. So, if the buffer's restrictions are
3754 actually left unchanged, reset clip_changed. */
3755 if (obuf == current_buffer)
3756 {
3757 if (begv == BEGV && zv == ZV)
3758 current_buffer->clip_changed = old_clip_changed;
3759 }
3760 /* There isn't much we can reasonably do to protect against
3761 misbehaving fontification, but here's a fig leaf. */
3762 else if (BUFFER_LIVE_P (obuf))
3763 set_buffer_internal_1 (obuf);
3764
3765 /* The fontification code may have added/removed text.
3766 It could do even a lot worse, but let's at least protect against
3767 the most obvious case where only the text past `pos' gets changed',
3768 as is/was done in grep.el where some escapes sequences are turned
3769 into face properties (bug#7876). */
3770 it->end_charpos = ZV;
3771
3772 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3773 something. This avoids an endless loop if they failed to
3774 fontify the text for which reason ever. */
3775 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3776 handled = HANDLED_RECOMPUTE_PROPS;
3777 }
3778
3779 return handled;
3780 }
3781
3782
3783 \f
3784 /***********************************************************************
3785 Faces
3786 ***********************************************************************/
3787
3788 /* Set up iterator IT from face properties at its current position.
3789 Called from handle_stop. */
3790
3791 static enum prop_handled
3792 handle_face_prop (struct it *it)
3793 {
3794 int new_face_id;
3795 ptrdiff_t next_stop;
3796
3797 if (!STRINGP (it->string))
3798 {
3799 new_face_id
3800 = face_at_buffer_position (it->w,
3801 IT_CHARPOS (*it),
3802 it->region_beg_charpos,
3803 it->region_end_charpos,
3804 &next_stop,
3805 (IT_CHARPOS (*it)
3806 + TEXT_PROP_DISTANCE_LIMIT),
3807 0, it->base_face_id);
3808
3809 /* Is this a start of a run of characters with box face?
3810 Caveat: this can be called for a freshly initialized
3811 iterator; face_id is -1 in this case. We know that the new
3812 face will not change until limit, i.e. if the new face has a
3813 box, all characters up to limit will have one. But, as
3814 usual, we don't know whether limit is really the end. */
3815 if (new_face_id != it->face_id)
3816 {
3817 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3818 /* If it->face_id is -1, old_face below will be NULL, see
3819 the definition of FACE_FROM_ID. This will happen if this
3820 is the initial call that gets the face. */
3821 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3822
3823 /* If the value of face_id of the iterator is -1, we have to
3824 look in front of IT's position and see whether there is a
3825 face there that's different from new_face_id. */
3826 if (!old_face && IT_CHARPOS (*it) > BEG)
3827 {
3828 int prev_face_id = face_before_it_pos (it);
3829
3830 old_face = FACE_FROM_ID (it->f, prev_face_id);
3831 }
3832
3833 /* If the new face has a box, but the old face does not,
3834 this is the start of a run of characters with box face,
3835 i.e. this character has a shadow on the left side. */
3836 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3837 && (old_face == NULL || !old_face->box));
3838 it->face_box_p = new_face->box != FACE_NO_BOX;
3839 }
3840 }
3841 else
3842 {
3843 int base_face_id;
3844 ptrdiff_t bufpos;
3845 int i;
3846 Lisp_Object from_overlay
3847 = (it->current.overlay_string_index >= 0
3848 ? it->string_overlays[it->current.overlay_string_index
3849 % OVERLAY_STRING_CHUNK_SIZE]
3850 : Qnil);
3851
3852 /* See if we got to this string directly or indirectly from
3853 an overlay property. That includes the before-string or
3854 after-string of an overlay, strings in display properties
3855 provided by an overlay, their text properties, etc.
3856
3857 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3858 if (! NILP (from_overlay))
3859 for (i = it->sp - 1; i >= 0; i--)
3860 {
3861 if (it->stack[i].current.overlay_string_index >= 0)
3862 from_overlay
3863 = it->string_overlays[it->stack[i].current.overlay_string_index
3864 % OVERLAY_STRING_CHUNK_SIZE];
3865 else if (! NILP (it->stack[i].from_overlay))
3866 from_overlay = it->stack[i].from_overlay;
3867
3868 if (!NILP (from_overlay))
3869 break;
3870 }
3871
3872 if (! NILP (from_overlay))
3873 {
3874 bufpos = IT_CHARPOS (*it);
3875 /* For a string from an overlay, the base face depends
3876 only on text properties and ignores overlays. */
3877 base_face_id
3878 = face_for_overlay_string (it->w,
3879 IT_CHARPOS (*it),
3880 it->region_beg_charpos,
3881 it->region_end_charpos,
3882 &next_stop,
3883 (IT_CHARPOS (*it)
3884 + TEXT_PROP_DISTANCE_LIMIT),
3885 0,
3886 from_overlay);
3887 }
3888 else
3889 {
3890 bufpos = 0;
3891
3892 /* For strings from a `display' property, use the face at
3893 IT's current buffer position as the base face to merge
3894 with, so that overlay strings appear in the same face as
3895 surrounding text, unless they specify their own faces.
3896 For strings from wrap-prefix and line-prefix properties,
3897 use the default face, possibly remapped via
3898 Vface_remapping_alist. */
3899 base_face_id = it->string_from_prefix_prop_p
3900 ? (!NILP (Vface_remapping_alist)
3901 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3902 : DEFAULT_FACE_ID)
3903 : underlying_face_id (it);
3904 }
3905
3906 new_face_id = face_at_string_position (it->w,
3907 it->string,
3908 IT_STRING_CHARPOS (*it),
3909 bufpos,
3910 it->region_beg_charpos,
3911 it->region_end_charpos,
3912 &next_stop,
3913 base_face_id, 0);
3914
3915 /* Is this a start of a run of characters with box? Caveat:
3916 this can be called for a freshly allocated iterator; face_id
3917 is -1 is this case. We know that the new face will not
3918 change until the next check pos, i.e. if the new face has a
3919 box, all characters up to that position will have a
3920 box. But, as usual, we don't know whether that position
3921 is really the end. */
3922 if (new_face_id != it->face_id)
3923 {
3924 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3925 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3926
3927 /* If new face has a box but old face hasn't, this is the
3928 start of a run of characters with box, i.e. it has a
3929 shadow on the left side. */
3930 it->start_of_box_run_p
3931 = new_face->box && (old_face == NULL || !old_face->box);
3932 it->face_box_p = new_face->box != FACE_NO_BOX;
3933 }
3934 }
3935
3936 it->face_id = new_face_id;
3937 return HANDLED_NORMALLY;
3938 }
3939
3940
3941 /* Return the ID of the face ``underlying'' IT's current position,
3942 which is in a string. If the iterator is associated with a
3943 buffer, return the face at IT's current buffer position.
3944 Otherwise, use the iterator's base_face_id. */
3945
3946 static int
3947 underlying_face_id (struct it *it)
3948 {
3949 int face_id = it->base_face_id, i;
3950
3951 eassert (STRINGP (it->string));
3952
3953 for (i = it->sp - 1; i >= 0; --i)
3954 if (NILP (it->stack[i].string))
3955 face_id = it->stack[i].face_id;
3956
3957 return face_id;
3958 }
3959
3960
3961 /* Compute the face one character before or after the current position
3962 of IT, in the visual order. BEFORE_P non-zero means get the face
3963 in front (to the left in L2R paragraphs, to the right in R2L
3964 paragraphs) of IT's screen position. Value is the ID of the face. */
3965
3966 static int
3967 face_before_or_after_it_pos (struct it *it, int before_p)
3968 {
3969 int face_id, limit;
3970 ptrdiff_t next_check_charpos;
3971 struct it it_copy;
3972 void *it_copy_data = NULL;
3973
3974 eassert (it->s == NULL);
3975
3976 if (STRINGP (it->string))
3977 {
3978 ptrdiff_t bufpos, charpos;
3979 int base_face_id;
3980
3981 /* No face change past the end of the string (for the case
3982 we are padding with spaces). No face change before the
3983 string start. */
3984 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3985 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3986 return it->face_id;
3987
3988 if (!it->bidi_p)
3989 {
3990 /* Set charpos to the position before or after IT's current
3991 position, in the logical order, which in the non-bidi
3992 case is the same as the visual order. */
3993 if (before_p)
3994 charpos = IT_STRING_CHARPOS (*it) - 1;
3995 else if (it->what == IT_COMPOSITION)
3996 /* For composition, we must check the character after the
3997 composition. */
3998 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3999 else
4000 charpos = IT_STRING_CHARPOS (*it) + 1;
4001 }
4002 else
4003 {
4004 if (before_p)
4005 {
4006 /* With bidi iteration, the character before the current
4007 in the visual order cannot be found by simple
4008 iteration, because "reverse" reordering is not
4009 supported. Instead, we need to use the move_it_*
4010 family of functions. */
4011 /* Ignore face changes before the first visible
4012 character on this display line. */
4013 if (it->current_x <= it->first_visible_x)
4014 return it->face_id;
4015 SAVE_IT (it_copy, *it, it_copy_data);
4016 /* Implementation note: Since move_it_in_display_line
4017 works in the iterator geometry, and thinks the first
4018 character is always the leftmost, even in R2L lines,
4019 we don't need to distinguish between the R2L and L2R
4020 cases here. */
4021 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4022 it_copy.current_x - 1, MOVE_TO_X);
4023 charpos = IT_STRING_CHARPOS (it_copy);
4024 RESTORE_IT (it, it, it_copy_data);
4025 }
4026 else
4027 {
4028 /* Set charpos to the string position of the character
4029 that comes after IT's current position in the visual
4030 order. */
4031 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4032
4033 it_copy = *it;
4034 while (n--)
4035 bidi_move_to_visually_next (&it_copy.bidi_it);
4036
4037 charpos = it_copy.bidi_it.charpos;
4038 }
4039 }
4040 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4041
4042 if (it->current.overlay_string_index >= 0)
4043 bufpos = IT_CHARPOS (*it);
4044 else
4045 bufpos = 0;
4046
4047 base_face_id = underlying_face_id (it);
4048
4049 /* Get the face for ASCII, or unibyte. */
4050 face_id = face_at_string_position (it->w,
4051 it->string,
4052 charpos,
4053 bufpos,
4054 it->region_beg_charpos,
4055 it->region_end_charpos,
4056 &next_check_charpos,
4057 base_face_id, 0);
4058
4059 /* Correct the face for charsets different from ASCII. Do it
4060 for the multibyte case only. The face returned above is
4061 suitable for unibyte text if IT->string is unibyte. */
4062 if (STRING_MULTIBYTE (it->string))
4063 {
4064 struct text_pos pos1 = string_pos (charpos, it->string);
4065 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4066 int c, len;
4067 struct face *face = FACE_FROM_ID (it->f, face_id);
4068
4069 c = string_char_and_length (p, &len);
4070 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4071 }
4072 }
4073 else
4074 {
4075 struct text_pos pos;
4076
4077 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4078 || (IT_CHARPOS (*it) <= BEGV && before_p))
4079 return it->face_id;
4080
4081 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4082 pos = it->current.pos;
4083
4084 if (!it->bidi_p)
4085 {
4086 if (before_p)
4087 DEC_TEXT_POS (pos, it->multibyte_p);
4088 else
4089 {
4090 if (it->what == IT_COMPOSITION)
4091 {
4092 /* For composition, we must check the position after
4093 the composition. */
4094 pos.charpos += it->cmp_it.nchars;
4095 pos.bytepos += it->len;
4096 }
4097 else
4098 INC_TEXT_POS (pos, it->multibyte_p);
4099 }
4100 }
4101 else
4102 {
4103 if (before_p)
4104 {
4105 /* With bidi iteration, the character before the current
4106 in the visual order cannot be found by simple
4107 iteration, because "reverse" reordering is not
4108 supported. Instead, we need to use the move_it_*
4109 family of functions. */
4110 /* Ignore face changes before the first visible
4111 character on this display line. */
4112 if (it->current_x <= it->first_visible_x)
4113 return it->face_id;
4114 SAVE_IT (it_copy, *it, it_copy_data);
4115 /* Implementation note: Since move_it_in_display_line
4116 works in the iterator geometry, and thinks the first
4117 character is always the leftmost, even in R2L lines,
4118 we don't need to distinguish between the R2L and L2R
4119 cases here. */
4120 move_it_in_display_line (&it_copy, ZV,
4121 it_copy.current_x - 1, MOVE_TO_X);
4122 pos = it_copy.current.pos;
4123 RESTORE_IT (it, it, it_copy_data);
4124 }
4125 else
4126 {
4127 /* Set charpos to the buffer position of the character
4128 that comes after IT's current position in the visual
4129 order. */
4130 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4131
4132 it_copy = *it;
4133 while (n--)
4134 bidi_move_to_visually_next (&it_copy.bidi_it);
4135
4136 SET_TEXT_POS (pos,
4137 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4138 }
4139 }
4140 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4141
4142 /* Determine face for CHARSET_ASCII, or unibyte. */
4143 face_id = face_at_buffer_position (it->w,
4144 CHARPOS (pos),
4145 it->region_beg_charpos,
4146 it->region_end_charpos,
4147 &next_check_charpos,
4148 limit, 0, -1);
4149
4150 /* Correct the face for charsets different from ASCII. Do it
4151 for the multibyte case only. The face returned above is
4152 suitable for unibyte text if current_buffer is unibyte. */
4153 if (it->multibyte_p)
4154 {
4155 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4156 struct face *face = FACE_FROM_ID (it->f, face_id);
4157 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4158 }
4159 }
4160
4161 return face_id;
4162 }
4163
4164
4165 \f
4166 /***********************************************************************
4167 Invisible text
4168 ***********************************************************************/
4169
4170 /* Set up iterator IT from invisible properties at its current
4171 position. Called from handle_stop. */
4172
4173 static enum prop_handled
4174 handle_invisible_prop (struct it *it)
4175 {
4176 enum prop_handled handled = HANDLED_NORMALLY;
4177 int invis_p;
4178 Lisp_Object prop;
4179
4180 if (STRINGP (it->string))
4181 {
4182 Lisp_Object end_charpos, limit, charpos;
4183
4184 /* Get the value of the invisible text property at the
4185 current position. Value will be nil if there is no such
4186 property. */
4187 charpos = make_number (IT_STRING_CHARPOS (*it));
4188 prop = Fget_text_property (charpos, Qinvisible, it->string);
4189 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4190
4191 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4192 {
4193 /* Record whether we have to display an ellipsis for the
4194 invisible text. */
4195 int display_ellipsis_p = (invis_p == 2);
4196 ptrdiff_t len, endpos;
4197
4198 handled = HANDLED_RECOMPUTE_PROPS;
4199
4200 /* Get the position at which the next visible text can be
4201 found in IT->string, if any. */
4202 endpos = len = SCHARS (it->string);
4203 XSETINT (limit, len);
4204 do
4205 {
4206 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4207 it->string, limit);
4208 if (INTEGERP (end_charpos))
4209 {
4210 endpos = XFASTINT (end_charpos);
4211 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4212 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4213 if (invis_p == 2)
4214 display_ellipsis_p = 1;
4215 }
4216 }
4217 while (invis_p && endpos < len);
4218
4219 if (display_ellipsis_p)
4220 it->ellipsis_p = 1;
4221
4222 if (endpos < len)
4223 {
4224 /* Text at END_CHARPOS is visible. Move IT there. */
4225 struct text_pos old;
4226 ptrdiff_t oldpos;
4227
4228 old = it->current.string_pos;
4229 oldpos = CHARPOS (old);
4230 if (it->bidi_p)
4231 {
4232 if (it->bidi_it.first_elt
4233 && it->bidi_it.charpos < SCHARS (it->string))
4234 bidi_paragraph_init (it->paragraph_embedding,
4235 &it->bidi_it, 1);
4236 /* Bidi-iterate out of the invisible text. */
4237 do
4238 {
4239 bidi_move_to_visually_next (&it->bidi_it);
4240 }
4241 while (oldpos <= it->bidi_it.charpos
4242 && it->bidi_it.charpos < endpos);
4243
4244 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4245 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4246 if (IT_CHARPOS (*it) >= endpos)
4247 it->prev_stop = endpos;
4248 }
4249 else
4250 {
4251 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4252 compute_string_pos (&it->current.string_pos, old, it->string);
4253 }
4254 }
4255 else
4256 {
4257 /* The rest of the string is invisible. If this is an
4258 overlay string, proceed with the next overlay string
4259 or whatever comes and return a character from there. */
4260 if (it->current.overlay_string_index >= 0
4261 && !display_ellipsis_p)
4262 {
4263 next_overlay_string (it);
4264 /* Don't check for overlay strings when we just
4265 finished processing them. */
4266 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4267 }
4268 else
4269 {
4270 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4271 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4272 }
4273 }
4274 }
4275 }
4276 else
4277 {
4278 ptrdiff_t newpos, next_stop, start_charpos, tem;
4279 Lisp_Object pos, overlay;
4280
4281 /* First of all, is there invisible text at this position? */
4282 tem = start_charpos = IT_CHARPOS (*it);
4283 pos = make_number (tem);
4284 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4285 &overlay);
4286 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4287
4288 /* If we are on invisible text, skip over it. */
4289 if (invis_p && start_charpos < it->end_charpos)
4290 {
4291 /* Record whether we have to display an ellipsis for the
4292 invisible text. */
4293 int display_ellipsis_p = invis_p == 2;
4294
4295 handled = HANDLED_RECOMPUTE_PROPS;
4296
4297 /* Loop skipping over invisible text. The loop is left at
4298 ZV or with IT on the first char being visible again. */
4299 do
4300 {
4301 /* Try to skip some invisible text. Return value is the
4302 position reached which can be equal to where we start
4303 if there is nothing invisible there. This skips both
4304 over invisible text properties and overlays with
4305 invisible property. */
4306 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4307
4308 /* If we skipped nothing at all we weren't at invisible
4309 text in the first place. If everything to the end of
4310 the buffer was skipped, end the loop. */
4311 if (newpos == tem || newpos >= ZV)
4312 invis_p = 0;
4313 else
4314 {
4315 /* We skipped some characters but not necessarily
4316 all there are. Check if we ended up on visible
4317 text. Fget_char_property returns the property of
4318 the char before the given position, i.e. if we
4319 get invis_p = 0, this means that the char at
4320 newpos is visible. */
4321 pos = make_number (newpos);
4322 prop = Fget_char_property (pos, Qinvisible, it->window);
4323 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4324 }
4325
4326 /* If we ended up on invisible text, proceed to
4327 skip starting with next_stop. */
4328 if (invis_p)
4329 tem = next_stop;
4330
4331 /* If there are adjacent invisible texts, don't lose the
4332 second one's ellipsis. */
4333 if (invis_p == 2)
4334 display_ellipsis_p = 1;
4335 }
4336 while (invis_p);
4337
4338 /* The position newpos is now either ZV or on visible text. */
4339 if (it->bidi_p)
4340 {
4341 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4342 int on_newline =
4343 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4344 int after_newline =
4345 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4346
4347 /* If the invisible text ends on a newline or on a
4348 character after a newline, we can avoid the costly,
4349 character by character, bidi iteration to NEWPOS, and
4350 instead simply reseat the iterator there. That's
4351 because all bidi reordering information is tossed at
4352 the newline. This is a big win for modes that hide
4353 complete lines, like Outline, Org, etc. */
4354 if (on_newline || after_newline)
4355 {
4356 struct text_pos tpos;
4357 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4358
4359 SET_TEXT_POS (tpos, newpos, bpos);
4360 reseat_1 (it, tpos, 0);
4361 /* If we reseat on a newline/ZV, we need to prep the
4362 bidi iterator for advancing to the next character
4363 after the newline/EOB, keeping the current paragraph
4364 direction (so that PRODUCE_GLYPHS does TRT wrt
4365 prepending/appending glyphs to a glyph row). */
4366 if (on_newline)
4367 {
4368 it->bidi_it.first_elt = 0;
4369 it->bidi_it.paragraph_dir = pdir;
4370 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4371 it->bidi_it.nchars = 1;
4372 it->bidi_it.ch_len = 1;
4373 }
4374 }
4375 else /* Must use the slow method. */
4376 {
4377 /* With bidi iteration, the region of invisible text
4378 could start and/or end in the middle of a
4379 non-base embedding level. Therefore, we need to
4380 skip invisible text using the bidi iterator,
4381 starting at IT's current position, until we find
4382 ourselves outside of the invisible text.
4383 Skipping invisible text _after_ bidi iteration
4384 avoids affecting the visual order of the
4385 displayed text when invisible properties are
4386 added or removed. */
4387 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4388 {
4389 /* If we were `reseat'ed to a new paragraph,
4390 determine the paragraph base direction. We
4391 need to do it now because
4392 next_element_from_buffer may not have a
4393 chance to do it, if we are going to skip any
4394 text at the beginning, which resets the
4395 FIRST_ELT flag. */
4396 bidi_paragraph_init (it->paragraph_embedding,
4397 &it->bidi_it, 1);
4398 }
4399 do
4400 {
4401 bidi_move_to_visually_next (&it->bidi_it);
4402 }
4403 while (it->stop_charpos <= it->bidi_it.charpos
4404 && it->bidi_it.charpos < newpos);
4405 IT_CHARPOS (*it) = it->bidi_it.charpos;
4406 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4407 /* If we overstepped NEWPOS, record its position in
4408 the iterator, so that we skip invisible text if
4409 later the bidi iteration lands us in the
4410 invisible region again. */
4411 if (IT_CHARPOS (*it) >= newpos)
4412 it->prev_stop = newpos;
4413 }
4414 }
4415 else
4416 {
4417 IT_CHARPOS (*it) = newpos;
4418 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4419 }
4420
4421 /* If there are before-strings at the start of invisible
4422 text, and the text is invisible because of a text
4423 property, arrange to show before-strings because 20.x did
4424 it that way. (If the text is invisible because of an
4425 overlay property instead of a text property, this is
4426 already handled in the overlay code.) */
4427 if (NILP (overlay)
4428 && get_overlay_strings (it, it->stop_charpos))
4429 {
4430 handled = HANDLED_RECOMPUTE_PROPS;
4431 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4432 }
4433 else if (display_ellipsis_p)
4434 {
4435 /* Make sure that the glyphs of the ellipsis will get
4436 correct `charpos' values. If we would not update
4437 it->position here, the glyphs would belong to the
4438 last visible character _before_ the invisible
4439 text, which confuses `set_cursor_from_row'.
4440
4441 We use the last invisible position instead of the
4442 first because this way the cursor is always drawn on
4443 the first "." of the ellipsis, whenever PT is inside
4444 the invisible text. Otherwise the cursor would be
4445 placed _after_ the ellipsis when the point is after the
4446 first invisible character. */
4447 if (!STRINGP (it->object))
4448 {
4449 it->position.charpos = newpos - 1;
4450 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4451 }
4452 it->ellipsis_p = 1;
4453 /* Let the ellipsis display before
4454 considering any properties of the following char.
4455 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4456 handled = HANDLED_RETURN;
4457 }
4458 }
4459 }
4460
4461 return handled;
4462 }
4463
4464
4465 /* Make iterator IT return `...' next.
4466 Replaces LEN characters from buffer. */
4467
4468 static void
4469 setup_for_ellipsis (struct it *it, int len)
4470 {
4471 /* Use the display table definition for `...'. Invalid glyphs
4472 will be handled by the method returning elements from dpvec. */
4473 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4474 {
4475 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4476 it->dpvec = v->u.contents;
4477 it->dpend = v->u.contents + v->header.size;
4478 }
4479 else
4480 {
4481 /* Default `...'. */
4482 it->dpvec = default_invis_vector;
4483 it->dpend = default_invis_vector + 3;
4484 }
4485
4486 it->dpvec_char_len = len;
4487 it->current.dpvec_index = 0;
4488 it->dpvec_face_id = -1;
4489
4490 /* Remember the current face id in case glyphs specify faces.
4491 IT's face is restored in set_iterator_to_next.
4492 saved_face_id was set to preceding char's face in handle_stop. */
4493 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4494 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4495
4496 it->method = GET_FROM_DISPLAY_VECTOR;
4497 it->ellipsis_p = 1;
4498 }
4499
4500
4501 \f
4502 /***********************************************************************
4503 'display' property
4504 ***********************************************************************/
4505
4506 /* Set up iterator IT from `display' property at its current position.
4507 Called from handle_stop.
4508 We return HANDLED_RETURN if some part of the display property
4509 overrides the display of the buffer text itself.
4510 Otherwise we return HANDLED_NORMALLY. */
4511
4512 static enum prop_handled
4513 handle_display_prop (struct it *it)
4514 {
4515 Lisp_Object propval, object, overlay;
4516 struct text_pos *position;
4517 ptrdiff_t bufpos;
4518 /* Nonzero if some property replaces the display of the text itself. */
4519 int display_replaced_p = 0;
4520
4521 if (STRINGP (it->string))
4522 {
4523 object = it->string;
4524 position = &it->current.string_pos;
4525 bufpos = CHARPOS (it->current.pos);
4526 }
4527 else
4528 {
4529 XSETWINDOW (object, it->w);
4530 position = &it->current.pos;
4531 bufpos = CHARPOS (*position);
4532 }
4533
4534 /* Reset those iterator values set from display property values. */
4535 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4536 it->space_width = Qnil;
4537 it->font_height = Qnil;
4538 it->voffset = 0;
4539
4540 /* We don't support recursive `display' properties, i.e. string
4541 values that have a string `display' property, that have a string
4542 `display' property etc. */
4543 if (!it->string_from_display_prop_p)
4544 it->area = TEXT_AREA;
4545
4546 propval = get_char_property_and_overlay (make_number (position->charpos),
4547 Qdisplay, object, &overlay);
4548 if (NILP (propval))
4549 return HANDLED_NORMALLY;
4550 /* Now OVERLAY is the overlay that gave us this property, or nil
4551 if it was a text property. */
4552
4553 if (!STRINGP (it->string))
4554 object = it->w->contents;
4555
4556 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4557 position, bufpos,
4558 FRAME_WINDOW_P (it->f));
4559
4560 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4561 }
4562
4563 /* Subroutine of handle_display_prop. Returns non-zero if the display
4564 specification in SPEC is a replacing specification, i.e. it would
4565 replace the text covered by `display' property with something else,
4566 such as an image or a display string. If SPEC includes any kind or
4567 `(space ...) specification, the value is 2; this is used by
4568 compute_display_string_pos, which see.
4569
4570 See handle_single_display_spec for documentation of arguments.
4571 frame_window_p is non-zero if the window being redisplayed is on a
4572 GUI frame; this argument is used only if IT is NULL, see below.
4573
4574 IT can be NULL, if this is called by the bidi reordering code
4575 through compute_display_string_pos, which see. In that case, this
4576 function only examines SPEC, but does not otherwise "handle" it, in
4577 the sense that it doesn't set up members of IT from the display
4578 spec. */
4579 static int
4580 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4581 Lisp_Object overlay, struct text_pos *position,
4582 ptrdiff_t bufpos, int frame_window_p)
4583 {
4584 int replacing_p = 0;
4585 int rv;
4586
4587 if (CONSP (spec)
4588 /* Simple specifications. */
4589 && !EQ (XCAR (spec), Qimage)
4590 && !EQ (XCAR (spec), Qspace)
4591 && !EQ (XCAR (spec), Qwhen)
4592 && !EQ (XCAR (spec), Qslice)
4593 && !EQ (XCAR (spec), Qspace_width)
4594 && !EQ (XCAR (spec), Qheight)
4595 && !EQ (XCAR (spec), Qraise)
4596 /* Marginal area specifications. */
4597 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4598 && !EQ (XCAR (spec), Qleft_fringe)
4599 && !EQ (XCAR (spec), Qright_fringe)
4600 && !NILP (XCAR (spec)))
4601 {
4602 for (; CONSP (spec); spec = XCDR (spec))
4603 {
4604 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4605 overlay, position, bufpos,
4606 replacing_p, frame_window_p)))
4607 {
4608 replacing_p = rv;
4609 /* If some text in a string is replaced, `position' no
4610 longer points to the position of `object'. */
4611 if (!it || STRINGP (object))
4612 break;
4613 }
4614 }
4615 }
4616 else if (VECTORP (spec))
4617 {
4618 ptrdiff_t i;
4619 for (i = 0; i < ASIZE (spec); ++i)
4620 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4621 overlay, position, bufpos,
4622 replacing_p, frame_window_p)))
4623 {
4624 replacing_p = rv;
4625 /* If some text in a string is replaced, `position' no
4626 longer points to the position of `object'. */
4627 if (!it || STRINGP (object))
4628 break;
4629 }
4630 }
4631 else
4632 {
4633 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4634 position, bufpos, 0,
4635 frame_window_p)))
4636 replacing_p = rv;
4637 }
4638
4639 return replacing_p;
4640 }
4641
4642 /* Value is the position of the end of the `display' property starting
4643 at START_POS in OBJECT. */
4644
4645 static struct text_pos
4646 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4647 {
4648 Lisp_Object end;
4649 struct text_pos end_pos;
4650
4651 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4652 Qdisplay, object, Qnil);
4653 CHARPOS (end_pos) = XFASTINT (end);
4654 if (STRINGP (object))
4655 compute_string_pos (&end_pos, start_pos, it->string);
4656 else
4657 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4658
4659 return end_pos;
4660 }
4661
4662
4663 /* Set up IT from a single `display' property specification SPEC. OBJECT
4664 is the object in which the `display' property was found. *POSITION
4665 is the position in OBJECT at which the `display' property was found.
4666 BUFPOS is the buffer position of OBJECT (different from POSITION if
4667 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4668 previously saw a display specification which already replaced text
4669 display with something else, for example an image; we ignore such
4670 properties after the first one has been processed.
4671
4672 OVERLAY is the overlay this `display' property came from,
4673 or nil if it was a text property.
4674
4675 If SPEC is a `space' or `image' specification, and in some other
4676 cases too, set *POSITION to the position where the `display'
4677 property ends.
4678
4679 If IT is NULL, only examine the property specification in SPEC, but
4680 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4681 is intended to be displayed in a window on a GUI frame.
4682
4683 Value is non-zero if something was found which replaces the display
4684 of buffer or string text. */
4685
4686 static int
4687 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4688 Lisp_Object overlay, struct text_pos *position,
4689 ptrdiff_t bufpos, int display_replaced_p,
4690 int frame_window_p)
4691 {
4692 Lisp_Object form;
4693 Lisp_Object location, value;
4694 struct text_pos start_pos = *position;
4695 int valid_p;
4696
4697 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4698 If the result is non-nil, use VALUE instead of SPEC. */
4699 form = Qt;
4700 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4701 {
4702 spec = XCDR (spec);
4703 if (!CONSP (spec))
4704 return 0;
4705 form = XCAR (spec);
4706 spec = XCDR (spec);
4707 }
4708
4709 if (!NILP (form) && !EQ (form, Qt))
4710 {
4711 ptrdiff_t count = SPECPDL_INDEX ();
4712 struct gcpro gcpro1;
4713
4714 /* Bind `object' to the object having the `display' property, a
4715 buffer or string. Bind `position' to the position in the
4716 object where the property was found, and `buffer-position'
4717 to the current position in the buffer. */
4718
4719 if (NILP (object))
4720 XSETBUFFER (object, current_buffer);
4721 specbind (Qobject, object);
4722 specbind (Qposition, make_number (CHARPOS (*position)));
4723 specbind (Qbuffer_position, make_number (bufpos));
4724 GCPRO1 (form);
4725 form = safe_eval (form);
4726 UNGCPRO;
4727 unbind_to (count, Qnil);
4728 }
4729
4730 if (NILP (form))
4731 return 0;
4732
4733 /* Handle `(height HEIGHT)' specifications. */
4734 if (CONSP (spec)
4735 && EQ (XCAR (spec), Qheight)
4736 && CONSP (XCDR (spec)))
4737 {
4738 if (it)
4739 {
4740 if (!FRAME_WINDOW_P (it->f))
4741 return 0;
4742
4743 it->font_height = XCAR (XCDR (spec));
4744 if (!NILP (it->font_height))
4745 {
4746 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4747 int new_height = -1;
4748
4749 if (CONSP (it->font_height)
4750 && (EQ (XCAR (it->font_height), Qplus)
4751 || EQ (XCAR (it->font_height), Qminus))
4752 && CONSP (XCDR (it->font_height))
4753 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4754 {
4755 /* `(+ N)' or `(- N)' where N is an integer. */
4756 int steps = XINT (XCAR (XCDR (it->font_height)));
4757 if (EQ (XCAR (it->font_height), Qplus))
4758 steps = - steps;
4759 it->face_id = smaller_face (it->f, it->face_id, steps);
4760 }
4761 else if (FUNCTIONP (it->font_height))
4762 {
4763 /* Call function with current height as argument.
4764 Value is the new height. */
4765 Lisp_Object height;
4766 height = safe_call1 (it->font_height,
4767 face->lface[LFACE_HEIGHT_INDEX]);
4768 if (NUMBERP (height))
4769 new_height = XFLOATINT (height);
4770 }
4771 else if (NUMBERP (it->font_height))
4772 {
4773 /* Value is a multiple of the canonical char height. */
4774 struct face *f;
4775
4776 f = FACE_FROM_ID (it->f,
4777 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4778 new_height = (XFLOATINT (it->font_height)
4779 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4780 }
4781 else
4782 {
4783 /* Evaluate IT->font_height with `height' bound to the
4784 current specified height to get the new height. */
4785 ptrdiff_t count = SPECPDL_INDEX ();
4786
4787 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4788 value = safe_eval (it->font_height);
4789 unbind_to (count, Qnil);
4790
4791 if (NUMBERP (value))
4792 new_height = XFLOATINT (value);
4793 }
4794
4795 if (new_height > 0)
4796 it->face_id = face_with_height (it->f, it->face_id, new_height);
4797 }
4798 }
4799
4800 return 0;
4801 }
4802
4803 /* Handle `(space-width WIDTH)'. */
4804 if (CONSP (spec)
4805 && EQ (XCAR (spec), Qspace_width)
4806 && CONSP (XCDR (spec)))
4807 {
4808 if (it)
4809 {
4810 if (!FRAME_WINDOW_P (it->f))
4811 return 0;
4812
4813 value = XCAR (XCDR (spec));
4814 if (NUMBERP (value) && XFLOATINT (value) > 0)
4815 it->space_width = value;
4816 }
4817
4818 return 0;
4819 }
4820
4821 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4822 if (CONSP (spec)
4823 && EQ (XCAR (spec), Qslice))
4824 {
4825 Lisp_Object tem;
4826
4827 if (it)
4828 {
4829 if (!FRAME_WINDOW_P (it->f))
4830 return 0;
4831
4832 if (tem = XCDR (spec), CONSP (tem))
4833 {
4834 it->slice.x = XCAR (tem);
4835 if (tem = XCDR (tem), CONSP (tem))
4836 {
4837 it->slice.y = XCAR (tem);
4838 if (tem = XCDR (tem), CONSP (tem))
4839 {
4840 it->slice.width = XCAR (tem);
4841 if (tem = XCDR (tem), CONSP (tem))
4842 it->slice.height = XCAR (tem);
4843 }
4844 }
4845 }
4846 }
4847
4848 return 0;
4849 }
4850
4851 /* Handle `(raise FACTOR)'. */
4852 if (CONSP (spec)
4853 && EQ (XCAR (spec), Qraise)
4854 && CONSP (XCDR (spec)))
4855 {
4856 if (it)
4857 {
4858 if (!FRAME_WINDOW_P (it->f))
4859 return 0;
4860
4861 #ifdef HAVE_WINDOW_SYSTEM
4862 value = XCAR (XCDR (spec));
4863 if (NUMBERP (value))
4864 {
4865 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4866 it->voffset = - (XFLOATINT (value)
4867 * (FONT_HEIGHT (face->font)));
4868 }
4869 #endif /* HAVE_WINDOW_SYSTEM */
4870 }
4871
4872 return 0;
4873 }
4874
4875 /* Don't handle the other kinds of display specifications
4876 inside a string that we got from a `display' property. */
4877 if (it && it->string_from_display_prop_p)
4878 return 0;
4879
4880 /* Characters having this form of property are not displayed, so
4881 we have to find the end of the property. */
4882 if (it)
4883 {
4884 start_pos = *position;
4885 *position = display_prop_end (it, object, start_pos);
4886 }
4887 value = Qnil;
4888
4889 /* Stop the scan at that end position--we assume that all
4890 text properties change there. */
4891 if (it)
4892 it->stop_charpos = position->charpos;
4893
4894 /* Handle `(left-fringe BITMAP [FACE])'
4895 and `(right-fringe BITMAP [FACE])'. */
4896 if (CONSP (spec)
4897 && (EQ (XCAR (spec), Qleft_fringe)
4898 || EQ (XCAR (spec), Qright_fringe))
4899 && CONSP (XCDR (spec)))
4900 {
4901 int fringe_bitmap;
4902
4903 if (it)
4904 {
4905 if (!FRAME_WINDOW_P (it->f))
4906 /* If we return here, POSITION has been advanced
4907 across the text with this property. */
4908 {
4909 /* Synchronize the bidi iterator with POSITION. This is
4910 needed because we are not going to push the iterator
4911 on behalf of this display property, so there will be
4912 no pop_it call to do this synchronization for us. */
4913 if (it->bidi_p)
4914 {
4915 it->position = *position;
4916 iterate_out_of_display_property (it);
4917 *position = it->position;
4918 }
4919 return 1;
4920 }
4921 }
4922 else if (!frame_window_p)
4923 return 1;
4924
4925 #ifdef HAVE_WINDOW_SYSTEM
4926 value = XCAR (XCDR (spec));
4927 if (!SYMBOLP (value)
4928 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4929 /* If we return here, POSITION has been advanced
4930 across the text with this property. */
4931 {
4932 if (it && it->bidi_p)
4933 {
4934 it->position = *position;
4935 iterate_out_of_display_property (it);
4936 *position = it->position;
4937 }
4938 return 1;
4939 }
4940
4941 if (it)
4942 {
4943 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4944
4945 if (CONSP (XCDR (XCDR (spec))))
4946 {
4947 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4948 int face_id2 = lookup_derived_face (it->f, face_name,
4949 FRINGE_FACE_ID, 0);
4950 if (face_id2 >= 0)
4951 face_id = face_id2;
4952 }
4953
4954 /* Save current settings of IT so that we can restore them
4955 when we are finished with the glyph property value. */
4956 push_it (it, position);
4957
4958 it->area = TEXT_AREA;
4959 it->what = IT_IMAGE;
4960 it->image_id = -1; /* no image */
4961 it->position = start_pos;
4962 it->object = NILP (object) ? it->w->contents : object;
4963 it->method = GET_FROM_IMAGE;
4964 it->from_overlay = Qnil;
4965 it->face_id = face_id;
4966 it->from_disp_prop_p = 1;
4967
4968 /* Say that we haven't consumed the characters with
4969 `display' property yet. The call to pop_it in
4970 set_iterator_to_next will clean this up. */
4971 *position = start_pos;
4972
4973 if (EQ (XCAR (spec), Qleft_fringe))
4974 {
4975 it->left_user_fringe_bitmap = fringe_bitmap;
4976 it->left_user_fringe_face_id = face_id;
4977 }
4978 else
4979 {
4980 it->right_user_fringe_bitmap = fringe_bitmap;
4981 it->right_user_fringe_face_id = face_id;
4982 }
4983 }
4984 #endif /* HAVE_WINDOW_SYSTEM */
4985 return 1;
4986 }
4987
4988 /* Prepare to handle `((margin left-margin) ...)',
4989 `((margin right-margin) ...)' and `((margin nil) ...)'
4990 prefixes for display specifications. */
4991 location = Qunbound;
4992 if (CONSP (spec) && CONSP (XCAR (spec)))
4993 {
4994 Lisp_Object tem;
4995
4996 value = XCDR (spec);
4997 if (CONSP (value))
4998 value = XCAR (value);
4999
5000 tem = XCAR (spec);
5001 if (EQ (XCAR (tem), Qmargin)
5002 && (tem = XCDR (tem),
5003 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5004 (NILP (tem)
5005 || EQ (tem, Qleft_margin)
5006 || EQ (tem, Qright_margin))))
5007 location = tem;
5008 }
5009
5010 if (EQ (location, Qunbound))
5011 {
5012 location = Qnil;
5013 value = spec;
5014 }
5015
5016 /* After this point, VALUE is the property after any
5017 margin prefix has been stripped. It must be a string,
5018 an image specification, or `(space ...)'.
5019
5020 LOCATION specifies where to display: `left-margin',
5021 `right-margin' or nil. */
5022
5023 valid_p = (STRINGP (value)
5024 #ifdef HAVE_WINDOW_SYSTEM
5025 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5026 && valid_image_p (value))
5027 #endif /* not HAVE_WINDOW_SYSTEM */
5028 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5029
5030 if (valid_p && !display_replaced_p)
5031 {
5032 int retval = 1;
5033
5034 if (!it)
5035 {
5036 /* Callers need to know whether the display spec is any kind
5037 of `(space ...)' spec that is about to affect text-area
5038 display. */
5039 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5040 retval = 2;
5041 return retval;
5042 }
5043
5044 /* Save current settings of IT so that we can restore them
5045 when we are finished with the glyph property value. */
5046 push_it (it, position);
5047 it->from_overlay = overlay;
5048 it->from_disp_prop_p = 1;
5049
5050 if (NILP (location))
5051 it->area = TEXT_AREA;
5052 else if (EQ (location, Qleft_margin))
5053 it->area = LEFT_MARGIN_AREA;
5054 else
5055 it->area = RIGHT_MARGIN_AREA;
5056
5057 if (STRINGP (value))
5058 {
5059 it->string = value;
5060 it->multibyte_p = STRING_MULTIBYTE (it->string);
5061 it->current.overlay_string_index = -1;
5062 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5063 it->end_charpos = it->string_nchars = SCHARS (it->string);
5064 it->method = GET_FROM_STRING;
5065 it->stop_charpos = 0;
5066 it->prev_stop = 0;
5067 it->base_level_stop = 0;
5068 it->string_from_display_prop_p = 1;
5069 /* Say that we haven't consumed the characters with
5070 `display' property yet. The call to pop_it in
5071 set_iterator_to_next will clean this up. */
5072 if (BUFFERP (object))
5073 *position = start_pos;
5074
5075 /* Force paragraph direction to be that of the parent
5076 object. If the parent object's paragraph direction is
5077 not yet determined, default to L2R. */
5078 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5079 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5080 else
5081 it->paragraph_embedding = L2R;
5082
5083 /* Set up the bidi iterator for this display string. */
5084 if (it->bidi_p)
5085 {
5086 it->bidi_it.string.lstring = it->string;
5087 it->bidi_it.string.s = NULL;
5088 it->bidi_it.string.schars = it->end_charpos;
5089 it->bidi_it.string.bufpos = bufpos;
5090 it->bidi_it.string.from_disp_str = 1;
5091 it->bidi_it.string.unibyte = !it->multibyte_p;
5092 it->bidi_it.w = it->w;
5093 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5094 }
5095 }
5096 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5097 {
5098 it->method = GET_FROM_STRETCH;
5099 it->object = value;
5100 *position = it->position = start_pos;
5101 retval = 1 + (it->area == TEXT_AREA);
5102 }
5103 #ifdef HAVE_WINDOW_SYSTEM
5104 else
5105 {
5106 it->what = IT_IMAGE;
5107 it->image_id = lookup_image (it->f, value);
5108 it->position = start_pos;
5109 it->object = NILP (object) ? it->w->contents : object;
5110 it->method = GET_FROM_IMAGE;
5111
5112 /* Say that we haven't consumed the characters with
5113 `display' property yet. The call to pop_it in
5114 set_iterator_to_next will clean this up. */
5115 *position = start_pos;
5116 }
5117 #endif /* HAVE_WINDOW_SYSTEM */
5118
5119 return retval;
5120 }
5121
5122 /* Invalid property or property not supported. Restore
5123 POSITION to what it was before. */
5124 *position = start_pos;
5125 return 0;
5126 }
5127
5128 /* Check if PROP is a display property value whose text should be
5129 treated as intangible. OVERLAY is the overlay from which PROP
5130 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5131 specify the buffer position covered by PROP. */
5132
5133 int
5134 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5135 ptrdiff_t charpos, ptrdiff_t bytepos)
5136 {
5137 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5138 struct text_pos position;
5139
5140 SET_TEXT_POS (position, charpos, bytepos);
5141 return handle_display_spec (NULL, prop, Qnil, overlay,
5142 &position, charpos, frame_window_p);
5143 }
5144
5145
5146 /* Return 1 if PROP is a display sub-property value containing STRING.
5147
5148 Implementation note: this and the following function are really
5149 special cases of handle_display_spec and
5150 handle_single_display_spec, and should ideally use the same code.
5151 Until they do, these two pairs must be consistent and must be
5152 modified in sync. */
5153
5154 static int
5155 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5156 {
5157 if (EQ (string, prop))
5158 return 1;
5159
5160 /* Skip over `when FORM'. */
5161 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5162 {
5163 prop = XCDR (prop);
5164 if (!CONSP (prop))
5165 return 0;
5166 /* Actually, the condition following `when' should be eval'ed,
5167 like handle_single_display_spec does, and we should return
5168 zero if it evaluates to nil. However, this function is
5169 called only when the buffer was already displayed and some
5170 glyph in the glyph matrix was found to come from a display
5171 string. Therefore, the condition was already evaluated, and
5172 the result was non-nil, otherwise the display string wouldn't
5173 have been displayed and we would have never been called for
5174 this property. Thus, we can skip the evaluation and assume
5175 its result is non-nil. */
5176 prop = XCDR (prop);
5177 }
5178
5179 if (CONSP (prop))
5180 /* Skip over `margin LOCATION'. */
5181 if (EQ (XCAR (prop), Qmargin))
5182 {
5183 prop = XCDR (prop);
5184 if (!CONSP (prop))
5185 return 0;
5186
5187 prop = XCDR (prop);
5188 if (!CONSP (prop))
5189 return 0;
5190 }
5191
5192 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5193 }
5194
5195
5196 /* Return 1 if STRING appears in the `display' property PROP. */
5197
5198 static int
5199 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5200 {
5201 if (CONSP (prop)
5202 && !EQ (XCAR (prop), Qwhen)
5203 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5204 {
5205 /* A list of sub-properties. */
5206 while (CONSP (prop))
5207 {
5208 if (single_display_spec_string_p (XCAR (prop), string))
5209 return 1;
5210 prop = XCDR (prop);
5211 }
5212 }
5213 else if (VECTORP (prop))
5214 {
5215 /* A vector of sub-properties. */
5216 ptrdiff_t i;
5217 for (i = 0; i < ASIZE (prop); ++i)
5218 if (single_display_spec_string_p (AREF (prop, i), string))
5219 return 1;
5220 }
5221 else
5222 return single_display_spec_string_p (prop, string);
5223
5224 return 0;
5225 }
5226
5227 /* Look for STRING in overlays and text properties in the current
5228 buffer, between character positions FROM and TO (excluding TO).
5229 BACK_P non-zero means look back (in this case, TO is supposed to be
5230 less than FROM).
5231 Value is the first character position where STRING was found, or
5232 zero if it wasn't found before hitting TO.
5233
5234 This function may only use code that doesn't eval because it is
5235 called asynchronously from note_mouse_highlight. */
5236
5237 static ptrdiff_t
5238 string_buffer_position_lim (Lisp_Object string,
5239 ptrdiff_t from, ptrdiff_t to, int back_p)
5240 {
5241 Lisp_Object limit, prop, pos;
5242 int found = 0;
5243
5244 pos = make_number (max (from, BEGV));
5245
5246 if (!back_p) /* looking forward */
5247 {
5248 limit = make_number (min (to, ZV));
5249 while (!found && !EQ (pos, limit))
5250 {
5251 prop = Fget_char_property (pos, Qdisplay, Qnil);
5252 if (!NILP (prop) && display_prop_string_p (prop, string))
5253 found = 1;
5254 else
5255 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5256 limit);
5257 }
5258 }
5259 else /* looking back */
5260 {
5261 limit = make_number (max (to, BEGV));
5262 while (!found && !EQ (pos, limit))
5263 {
5264 prop = Fget_char_property (pos, Qdisplay, Qnil);
5265 if (!NILP (prop) && display_prop_string_p (prop, string))
5266 found = 1;
5267 else
5268 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5269 limit);
5270 }
5271 }
5272
5273 return found ? XINT (pos) : 0;
5274 }
5275
5276 /* Determine which buffer position in current buffer STRING comes from.
5277 AROUND_CHARPOS is an approximate position where it could come from.
5278 Value is the buffer position or 0 if it couldn't be determined.
5279
5280 This function is necessary because we don't record buffer positions
5281 in glyphs generated from strings (to keep struct glyph small).
5282 This function may only use code that doesn't eval because it is
5283 called asynchronously from note_mouse_highlight. */
5284
5285 static ptrdiff_t
5286 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5287 {
5288 const int MAX_DISTANCE = 1000;
5289 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5290 around_charpos + MAX_DISTANCE,
5291 0);
5292
5293 if (!found)
5294 found = string_buffer_position_lim (string, around_charpos,
5295 around_charpos - MAX_DISTANCE, 1);
5296 return found;
5297 }
5298
5299
5300 \f
5301 /***********************************************************************
5302 `composition' property
5303 ***********************************************************************/
5304
5305 /* Set up iterator IT from `composition' property at its current
5306 position. Called from handle_stop. */
5307
5308 static enum prop_handled
5309 handle_composition_prop (struct it *it)
5310 {
5311 Lisp_Object prop, string;
5312 ptrdiff_t pos, pos_byte, start, end;
5313
5314 if (STRINGP (it->string))
5315 {
5316 unsigned char *s;
5317
5318 pos = IT_STRING_CHARPOS (*it);
5319 pos_byte = IT_STRING_BYTEPOS (*it);
5320 string = it->string;
5321 s = SDATA (string) + pos_byte;
5322 it->c = STRING_CHAR (s);
5323 }
5324 else
5325 {
5326 pos = IT_CHARPOS (*it);
5327 pos_byte = IT_BYTEPOS (*it);
5328 string = Qnil;
5329 it->c = FETCH_CHAR (pos_byte);
5330 }
5331
5332 /* If there's a valid composition and point is not inside of the
5333 composition (in the case that the composition is from the current
5334 buffer), draw a glyph composed from the composition components. */
5335 if (find_composition (pos, -1, &start, &end, &prop, string)
5336 && composition_valid_p (start, end, prop)
5337 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5338 {
5339 if (start < pos)
5340 /* As we can't handle this situation (perhaps font-lock added
5341 a new composition), we just return here hoping that next
5342 redisplay will detect this composition much earlier. */
5343 return HANDLED_NORMALLY;
5344 if (start != pos)
5345 {
5346 if (STRINGP (it->string))
5347 pos_byte = string_char_to_byte (it->string, start);
5348 else
5349 pos_byte = CHAR_TO_BYTE (start);
5350 }
5351 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5352 prop, string);
5353
5354 if (it->cmp_it.id >= 0)
5355 {
5356 it->cmp_it.ch = -1;
5357 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5358 it->cmp_it.nglyphs = -1;
5359 }
5360 }
5361
5362 return HANDLED_NORMALLY;
5363 }
5364
5365
5366 \f
5367 /***********************************************************************
5368 Overlay strings
5369 ***********************************************************************/
5370
5371 /* The following structure is used to record overlay strings for
5372 later sorting in load_overlay_strings. */
5373
5374 struct overlay_entry
5375 {
5376 Lisp_Object overlay;
5377 Lisp_Object string;
5378 EMACS_INT priority;
5379 int after_string_p;
5380 };
5381
5382
5383 /* Set up iterator IT from overlay strings at its current position.
5384 Called from handle_stop. */
5385
5386 static enum prop_handled
5387 handle_overlay_change (struct it *it)
5388 {
5389 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5390 return HANDLED_RECOMPUTE_PROPS;
5391 else
5392 return HANDLED_NORMALLY;
5393 }
5394
5395
5396 /* Set up the next overlay string for delivery by IT, if there is an
5397 overlay string to deliver. Called by set_iterator_to_next when the
5398 end of the current overlay string is reached. If there are more
5399 overlay strings to display, IT->string and
5400 IT->current.overlay_string_index are set appropriately here.
5401 Otherwise IT->string is set to nil. */
5402
5403 static void
5404 next_overlay_string (struct it *it)
5405 {
5406 ++it->current.overlay_string_index;
5407 if (it->current.overlay_string_index == it->n_overlay_strings)
5408 {
5409 /* No more overlay strings. Restore IT's settings to what
5410 they were before overlay strings were processed, and
5411 continue to deliver from current_buffer. */
5412
5413 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5414 pop_it (it);
5415 eassert (it->sp > 0
5416 || (NILP (it->string)
5417 && it->method == GET_FROM_BUFFER
5418 && it->stop_charpos >= BEGV
5419 && it->stop_charpos <= it->end_charpos));
5420 it->current.overlay_string_index = -1;
5421 it->n_overlay_strings = 0;
5422 it->overlay_strings_charpos = -1;
5423 /* If there's an empty display string on the stack, pop the
5424 stack, to resync the bidi iterator with IT's position. Such
5425 empty strings are pushed onto the stack in
5426 get_overlay_strings_1. */
5427 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5428 pop_it (it);
5429
5430 /* If we're at the end of the buffer, record that we have
5431 processed the overlay strings there already, so that
5432 next_element_from_buffer doesn't try it again. */
5433 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5434 it->overlay_strings_at_end_processed_p = 1;
5435 }
5436 else
5437 {
5438 /* There are more overlay strings to process. If
5439 IT->current.overlay_string_index has advanced to a position
5440 where we must load IT->overlay_strings with more strings, do
5441 it. We must load at the IT->overlay_strings_charpos where
5442 IT->n_overlay_strings was originally computed; when invisible
5443 text is present, this might not be IT_CHARPOS (Bug#7016). */
5444 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5445
5446 if (it->current.overlay_string_index && i == 0)
5447 load_overlay_strings (it, it->overlay_strings_charpos);
5448
5449 /* Initialize IT to deliver display elements from the overlay
5450 string. */
5451 it->string = it->overlay_strings[i];
5452 it->multibyte_p = STRING_MULTIBYTE (it->string);
5453 SET_TEXT_POS (it->current.string_pos, 0, 0);
5454 it->method = GET_FROM_STRING;
5455 it->stop_charpos = 0;
5456 it->end_charpos = SCHARS (it->string);
5457 if (it->cmp_it.stop_pos >= 0)
5458 it->cmp_it.stop_pos = 0;
5459 it->prev_stop = 0;
5460 it->base_level_stop = 0;
5461
5462 /* Set up the bidi iterator for this overlay string. */
5463 if (it->bidi_p)
5464 {
5465 it->bidi_it.string.lstring = it->string;
5466 it->bidi_it.string.s = NULL;
5467 it->bidi_it.string.schars = SCHARS (it->string);
5468 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5469 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5470 it->bidi_it.string.unibyte = !it->multibyte_p;
5471 it->bidi_it.w = it->w;
5472 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5473 }
5474 }
5475
5476 CHECK_IT (it);
5477 }
5478
5479
5480 /* Compare two overlay_entry structures E1 and E2. Used as a
5481 comparison function for qsort in load_overlay_strings. Overlay
5482 strings for the same position are sorted so that
5483
5484 1. All after-strings come in front of before-strings, except
5485 when they come from the same overlay.
5486
5487 2. Within after-strings, strings are sorted so that overlay strings
5488 from overlays with higher priorities come first.
5489
5490 2. Within before-strings, strings are sorted so that overlay
5491 strings from overlays with higher priorities come last.
5492
5493 Value is analogous to strcmp. */
5494
5495
5496 static int
5497 compare_overlay_entries (const void *e1, const void *e2)
5498 {
5499 struct overlay_entry const *entry1 = e1;
5500 struct overlay_entry const *entry2 = e2;
5501 int result;
5502
5503 if (entry1->after_string_p != entry2->after_string_p)
5504 {
5505 /* Let after-strings appear in front of before-strings if
5506 they come from different overlays. */
5507 if (EQ (entry1->overlay, entry2->overlay))
5508 result = entry1->after_string_p ? 1 : -1;
5509 else
5510 result = entry1->after_string_p ? -1 : 1;
5511 }
5512 else if (entry1->priority != entry2->priority)
5513 {
5514 if (entry1->after_string_p)
5515 /* After-strings sorted in order of decreasing priority. */
5516 result = entry2->priority < entry1->priority ? -1 : 1;
5517 else
5518 /* Before-strings sorted in order of increasing priority. */
5519 result = entry1->priority < entry2->priority ? -1 : 1;
5520 }
5521 else
5522 result = 0;
5523
5524 return result;
5525 }
5526
5527
5528 /* Load the vector IT->overlay_strings with overlay strings from IT's
5529 current buffer position, or from CHARPOS if that is > 0. Set
5530 IT->n_overlays to the total number of overlay strings found.
5531
5532 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5533 a time. On entry into load_overlay_strings,
5534 IT->current.overlay_string_index gives the number of overlay
5535 strings that have already been loaded by previous calls to this
5536 function.
5537
5538 IT->add_overlay_start contains an additional overlay start
5539 position to consider for taking overlay strings from, if non-zero.
5540 This position comes into play when the overlay has an `invisible'
5541 property, and both before and after-strings. When we've skipped to
5542 the end of the overlay, because of its `invisible' property, we
5543 nevertheless want its before-string to appear.
5544 IT->add_overlay_start will contain the overlay start position
5545 in this case.
5546
5547 Overlay strings are sorted so that after-string strings come in
5548 front of before-string strings. Within before and after-strings,
5549 strings are sorted by overlay priority. See also function
5550 compare_overlay_entries. */
5551
5552 static void
5553 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5554 {
5555 Lisp_Object overlay, window, str, invisible;
5556 struct Lisp_Overlay *ov;
5557 ptrdiff_t start, end;
5558 ptrdiff_t size = 20;
5559 ptrdiff_t n = 0, i, j;
5560 int invis_p;
5561 struct overlay_entry *entries = alloca (size * sizeof *entries);
5562 USE_SAFE_ALLOCA;
5563
5564 if (charpos <= 0)
5565 charpos = IT_CHARPOS (*it);
5566
5567 /* Append the overlay string STRING of overlay OVERLAY to vector
5568 `entries' which has size `size' and currently contains `n'
5569 elements. AFTER_P non-zero means STRING is an after-string of
5570 OVERLAY. */
5571 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5572 do \
5573 { \
5574 Lisp_Object priority; \
5575 \
5576 if (n == size) \
5577 { \
5578 struct overlay_entry *old = entries; \
5579 SAFE_NALLOCA (entries, 2, size); \
5580 memcpy (entries, old, size * sizeof *entries); \
5581 size *= 2; \
5582 } \
5583 \
5584 entries[n].string = (STRING); \
5585 entries[n].overlay = (OVERLAY); \
5586 priority = Foverlay_get ((OVERLAY), Qpriority); \
5587 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5588 entries[n].after_string_p = (AFTER_P); \
5589 ++n; \
5590 } \
5591 while (0)
5592
5593 /* Process overlay before the overlay center. */
5594 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5595 {
5596 XSETMISC (overlay, ov);
5597 eassert (OVERLAYP (overlay));
5598 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5599 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5600
5601 if (end < charpos)
5602 break;
5603
5604 /* Skip this overlay if it doesn't start or end at IT's current
5605 position. */
5606 if (end != charpos && start != charpos)
5607 continue;
5608
5609 /* Skip this overlay if it doesn't apply to IT->w. */
5610 window = Foverlay_get (overlay, Qwindow);
5611 if (WINDOWP (window) && XWINDOW (window) != it->w)
5612 continue;
5613
5614 /* If the text ``under'' the overlay is invisible, both before-
5615 and after-strings from this overlay are visible; start and
5616 end position are indistinguishable. */
5617 invisible = Foverlay_get (overlay, Qinvisible);
5618 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5619
5620 /* If overlay has a non-empty before-string, record it. */
5621 if ((start == charpos || (end == charpos && invis_p))
5622 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5623 && SCHARS (str))
5624 RECORD_OVERLAY_STRING (overlay, str, 0);
5625
5626 /* If overlay has a non-empty after-string, record it. */
5627 if ((end == charpos || (start == charpos && invis_p))
5628 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5629 && SCHARS (str))
5630 RECORD_OVERLAY_STRING (overlay, str, 1);
5631 }
5632
5633 /* Process overlays after the overlay center. */
5634 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5635 {
5636 XSETMISC (overlay, ov);
5637 eassert (OVERLAYP (overlay));
5638 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5639 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5640
5641 if (start > charpos)
5642 break;
5643
5644 /* Skip this overlay if it doesn't start or end at IT's current
5645 position. */
5646 if (end != charpos && start != charpos)
5647 continue;
5648
5649 /* Skip this overlay if it doesn't apply to IT->w. */
5650 window = Foverlay_get (overlay, Qwindow);
5651 if (WINDOWP (window) && XWINDOW (window) != it->w)
5652 continue;
5653
5654 /* If the text ``under'' the overlay is invisible, it has a zero
5655 dimension, and both before- and after-strings apply. */
5656 invisible = Foverlay_get (overlay, Qinvisible);
5657 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5658
5659 /* If overlay has a non-empty before-string, record it. */
5660 if ((start == charpos || (end == charpos && invis_p))
5661 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5662 && SCHARS (str))
5663 RECORD_OVERLAY_STRING (overlay, str, 0);
5664
5665 /* If overlay has a non-empty after-string, record it. */
5666 if ((end == charpos || (start == charpos && invis_p))
5667 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5668 && SCHARS (str))
5669 RECORD_OVERLAY_STRING (overlay, str, 1);
5670 }
5671
5672 #undef RECORD_OVERLAY_STRING
5673
5674 /* Sort entries. */
5675 if (n > 1)
5676 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5677
5678 /* Record number of overlay strings, and where we computed it. */
5679 it->n_overlay_strings = n;
5680 it->overlay_strings_charpos = charpos;
5681
5682 /* IT->current.overlay_string_index is the number of overlay strings
5683 that have already been consumed by IT. Copy some of the
5684 remaining overlay strings to IT->overlay_strings. */
5685 i = 0;
5686 j = it->current.overlay_string_index;
5687 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5688 {
5689 it->overlay_strings[i] = entries[j].string;
5690 it->string_overlays[i++] = entries[j++].overlay;
5691 }
5692
5693 CHECK_IT (it);
5694 SAFE_FREE ();
5695 }
5696
5697
5698 /* Get the first chunk of overlay strings at IT's current buffer
5699 position, or at CHARPOS if that is > 0. Value is non-zero if at
5700 least one overlay string was found. */
5701
5702 static int
5703 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5704 {
5705 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5706 process. This fills IT->overlay_strings with strings, and sets
5707 IT->n_overlay_strings to the total number of strings to process.
5708 IT->pos.overlay_string_index has to be set temporarily to zero
5709 because load_overlay_strings needs this; it must be set to -1
5710 when no overlay strings are found because a zero value would
5711 indicate a position in the first overlay string. */
5712 it->current.overlay_string_index = 0;
5713 load_overlay_strings (it, charpos);
5714
5715 /* If we found overlay strings, set up IT to deliver display
5716 elements from the first one. Otherwise set up IT to deliver
5717 from current_buffer. */
5718 if (it->n_overlay_strings)
5719 {
5720 /* Make sure we know settings in current_buffer, so that we can
5721 restore meaningful values when we're done with the overlay
5722 strings. */
5723 if (compute_stop_p)
5724 compute_stop_pos (it);
5725 eassert (it->face_id >= 0);
5726
5727 /* Save IT's settings. They are restored after all overlay
5728 strings have been processed. */
5729 eassert (!compute_stop_p || it->sp == 0);
5730
5731 /* When called from handle_stop, there might be an empty display
5732 string loaded. In that case, don't bother saving it. But
5733 don't use this optimization with the bidi iterator, since we
5734 need the corresponding pop_it call to resync the bidi
5735 iterator's position with IT's position, after we are done
5736 with the overlay strings. (The corresponding call to pop_it
5737 in case of an empty display string is in
5738 next_overlay_string.) */
5739 if (!(!it->bidi_p
5740 && STRINGP (it->string) && !SCHARS (it->string)))
5741 push_it (it, NULL);
5742
5743 /* Set up IT to deliver display elements from the first overlay
5744 string. */
5745 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5746 it->string = it->overlay_strings[0];
5747 it->from_overlay = Qnil;
5748 it->stop_charpos = 0;
5749 eassert (STRINGP (it->string));
5750 it->end_charpos = SCHARS (it->string);
5751 it->prev_stop = 0;
5752 it->base_level_stop = 0;
5753 it->multibyte_p = STRING_MULTIBYTE (it->string);
5754 it->method = GET_FROM_STRING;
5755 it->from_disp_prop_p = 0;
5756
5757 /* Force paragraph direction to be that of the parent
5758 buffer. */
5759 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5760 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5761 else
5762 it->paragraph_embedding = L2R;
5763
5764 /* Set up the bidi iterator for this overlay string. */
5765 if (it->bidi_p)
5766 {
5767 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5768
5769 it->bidi_it.string.lstring = it->string;
5770 it->bidi_it.string.s = NULL;
5771 it->bidi_it.string.schars = SCHARS (it->string);
5772 it->bidi_it.string.bufpos = pos;
5773 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5774 it->bidi_it.string.unibyte = !it->multibyte_p;
5775 it->bidi_it.w = it->w;
5776 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5777 }
5778 return 1;
5779 }
5780
5781 it->current.overlay_string_index = -1;
5782 return 0;
5783 }
5784
5785 static int
5786 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5787 {
5788 it->string = Qnil;
5789 it->method = GET_FROM_BUFFER;
5790
5791 (void) get_overlay_strings_1 (it, charpos, 1);
5792
5793 CHECK_IT (it);
5794
5795 /* Value is non-zero if we found at least one overlay string. */
5796 return STRINGP (it->string);
5797 }
5798
5799
5800 \f
5801 /***********************************************************************
5802 Saving and restoring state
5803 ***********************************************************************/
5804
5805 /* Save current settings of IT on IT->stack. Called, for example,
5806 before setting up IT for an overlay string, to be able to restore
5807 IT's settings to what they were after the overlay string has been
5808 processed. If POSITION is non-NULL, it is the position to save on
5809 the stack instead of IT->position. */
5810
5811 static void
5812 push_it (struct it *it, struct text_pos *position)
5813 {
5814 struct iterator_stack_entry *p;
5815
5816 eassert (it->sp < IT_STACK_SIZE);
5817 p = it->stack + it->sp;
5818
5819 p->stop_charpos = it->stop_charpos;
5820 p->prev_stop = it->prev_stop;
5821 p->base_level_stop = it->base_level_stop;
5822 p->cmp_it = it->cmp_it;
5823 eassert (it->face_id >= 0);
5824 p->face_id = it->face_id;
5825 p->string = it->string;
5826 p->method = it->method;
5827 p->from_overlay = it->from_overlay;
5828 switch (p->method)
5829 {
5830 case GET_FROM_IMAGE:
5831 p->u.image.object = it->object;
5832 p->u.image.image_id = it->image_id;
5833 p->u.image.slice = it->slice;
5834 break;
5835 case GET_FROM_STRETCH:
5836 p->u.stretch.object = it->object;
5837 break;
5838 }
5839 p->position = position ? *position : it->position;
5840 p->current = it->current;
5841 p->end_charpos = it->end_charpos;
5842 p->string_nchars = it->string_nchars;
5843 p->area = it->area;
5844 p->multibyte_p = it->multibyte_p;
5845 p->avoid_cursor_p = it->avoid_cursor_p;
5846 p->space_width = it->space_width;
5847 p->font_height = it->font_height;
5848 p->voffset = it->voffset;
5849 p->string_from_display_prop_p = it->string_from_display_prop_p;
5850 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5851 p->display_ellipsis_p = 0;
5852 p->line_wrap = it->line_wrap;
5853 p->bidi_p = it->bidi_p;
5854 p->paragraph_embedding = it->paragraph_embedding;
5855 p->from_disp_prop_p = it->from_disp_prop_p;
5856 ++it->sp;
5857
5858 /* Save the state of the bidi iterator as well. */
5859 if (it->bidi_p)
5860 bidi_push_it (&it->bidi_it);
5861 }
5862
5863 static void
5864 iterate_out_of_display_property (struct it *it)
5865 {
5866 int buffer_p = !STRINGP (it->string);
5867 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5868 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5869
5870 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5871
5872 /* Maybe initialize paragraph direction. If we are at the beginning
5873 of a new paragraph, next_element_from_buffer may not have a
5874 chance to do that. */
5875 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5876 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5877 /* prev_stop can be zero, so check against BEGV as well. */
5878 while (it->bidi_it.charpos >= bob
5879 && it->prev_stop <= it->bidi_it.charpos
5880 && it->bidi_it.charpos < CHARPOS (it->position)
5881 && it->bidi_it.charpos < eob)
5882 bidi_move_to_visually_next (&it->bidi_it);
5883 /* Record the stop_pos we just crossed, for when we cross it
5884 back, maybe. */
5885 if (it->bidi_it.charpos > CHARPOS (it->position))
5886 it->prev_stop = CHARPOS (it->position);
5887 /* If we ended up not where pop_it put us, resync IT's
5888 positional members with the bidi iterator. */
5889 if (it->bidi_it.charpos != CHARPOS (it->position))
5890 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5891 if (buffer_p)
5892 it->current.pos = it->position;
5893 else
5894 it->current.string_pos = it->position;
5895 }
5896
5897 /* Restore IT's settings from IT->stack. Called, for example, when no
5898 more overlay strings must be processed, and we return to delivering
5899 display elements from a buffer, or when the end of a string from a
5900 `display' property is reached and we return to delivering display
5901 elements from an overlay string, or from a buffer. */
5902
5903 static void
5904 pop_it (struct it *it)
5905 {
5906 struct iterator_stack_entry *p;
5907 int from_display_prop = it->from_disp_prop_p;
5908
5909 eassert (it->sp > 0);
5910 --it->sp;
5911 p = it->stack + it->sp;
5912 it->stop_charpos = p->stop_charpos;
5913 it->prev_stop = p->prev_stop;
5914 it->base_level_stop = p->base_level_stop;
5915 it->cmp_it = p->cmp_it;
5916 it->face_id = p->face_id;
5917 it->current = p->current;
5918 it->position = p->position;
5919 it->string = p->string;
5920 it->from_overlay = p->from_overlay;
5921 if (NILP (it->string))
5922 SET_TEXT_POS (it->current.string_pos, -1, -1);
5923 it->method = p->method;
5924 switch (it->method)
5925 {
5926 case GET_FROM_IMAGE:
5927 it->image_id = p->u.image.image_id;
5928 it->object = p->u.image.object;
5929 it->slice = p->u.image.slice;
5930 break;
5931 case GET_FROM_STRETCH:
5932 it->object = p->u.stretch.object;
5933 break;
5934 case GET_FROM_BUFFER:
5935 it->object = it->w->contents;
5936 break;
5937 case GET_FROM_STRING:
5938 it->object = it->string;
5939 break;
5940 case GET_FROM_DISPLAY_VECTOR:
5941 if (it->s)
5942 it->method = GET_FROM_C_STRING;
5943 else if (STRINGP (it->string))
5944 it->method = GET_FROM_STRING;
5945 else
5946 {
5947 it->method = GET_FROM_BUFFER;
5948 it->object = it->w->contents;
5949 }
5950 }
5951 it->end_charpos = p->end_charpos;
5952 it->string_nchars = p->string_nchars;
5953 it->area = p->area;
5954 it->multibyte_p = p->multibyte_p;
5955 it->avoid_cursor_p = p->avoid_cursor_p;
5956 it->space_width = p->space_width;
5957 it->font_height = p->font_height;
5958 it->voffset = p->voffset;
5959 it->string_from_display_prop_p = p->string_from_display_prop_p;
5960 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5961 it->line_wrap = p->line_wrap;
5962 it->bidi_p = p->bidi_p;
5963 it->paragraph_embedding = p->paragraph_embedding;
5964 it->from_disp_prop_p = p->from_disp_prop_p;
5965 if (it->bidi_p)
5966 {
5967 bidi_pop_it (&it->bidi_it);
5968 /* Bidi-iterate until we get out of the portion of text, if any,
5969 covered by a `display' text property or by an overlay with
5970 `display' property. (We cannot just jump there, because the
5971 internal coherency of the bidi iterator state can not be
5972 preserved across such jumps.) We also must determine the
5973 paragraph base direction if the overlay we just processed is
5974 at the beginning of a new paragraph. */
5975 if (from_display_prop
5976 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5977 iterate_out_of_display_property (it);
5978
5979 eassert ((BUFFERP (it->object)
5980 && IT_CHARPOS (*it) == it->bidi_it.charpos
5981 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5982 || (STRINGP (it->object)
5983 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5984 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5985 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5986 }
5987 }
5988
5989
5990 \f
5991 /***********************************************************************
5992 Moving over lines
5993 ***********************************************************************/
5994
5995 /* Set IT's current position to the previous line start. */
5996
5997 static void
5998 back_to_previous_line_start (struct it *it)
5999 {
6000 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6001
6002 DEC_BOTH (cp, bp);
6003 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6004 }
6005
6006
6007 /* Move IT to the next line start.
6008
6009 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6010 we skipped over part of the text (as opposed to moving the iterator
6011 continuously over the text). Otherwise, don't change the value
6012 of *SKIPPED_P.
6013
6014 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6015 iterator on the newline, if it was found.
6016
6017 Newlines may come from buffer text, overlay strings, or strings
6018 displayed via the `display' property. That's the reason we can't
6019 simply use find_newline_no_quit.
6020
6021 Note that this function may not skip over invisible text that is so
6022 because of text properties and immediately follows a newline. If
6023 it would, function reseat_at_next_visible_line_start, when called
6024 from set_iterator_to_next, would effectively make invisible
6025 characters following a newline part of the wrong glyph row, which
6026 leads to wrong cursor motion. */
6027
6028 static int
6029 forward_to_next_line_start (struct it *it, int *skipped_p,
6030 struct bidi_it *bidi_it_prev)
6031 {
6032 ptrdiff_t old_selective;
6033 int newline_found_p, n;
6034 const int MAX_NEWLINE_DISTANCE = 500;
6035
6036 /* If already on a newline, just consume it to avoid unintended
6037 skipping over invisible text below. */
6038 if (it->what == IT_CHARACTER
6039 && it->c == '\n'
6040 && CHARPOS (it->position) == IT_CHARPOS (*it))
6041 {
6042 if (it->bidi_p && bidi_it_prev)
6043 *bidi_it_prev = it->bidi_it;
6044 set_iterator_to_next (it, 0);
6045 it->c = 0;
6046 return 1;
6047 }
6048
6049 /* Don't handle selective display in the following. It's (a)
6050 unnecessary because it's done by the caller, and (b) leads to an
6051 infinite recursion because next_element_from_ellipsis indirectly
6052 calls this function. */
6053 old_selective = it->selective;
6054 it->selective = 0;
6055
6056 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6057 from buffer text. */
6058 for (n = newline_found_p = 0;
6059 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6060 n += STRINGP (it->string) ? 0 : 1)
6061 {
6062 if (!get_next_display_element (it))
6063 return 0;
6064 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6065 if (newline_found_p && it->bidi_p && bidi_it_prev)
6066 *bidi_it_prev = it->bidi_it;
6067 set_iterator_to_next (it, 0);
6068 }
6069
6070 /* If we didn't find a newline near enough, see if we can use a
6071 short-cut. */
6072 if (!newline_found_p)
6073 {
6074 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6075 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6076 1, &bytepos);
6077 Lisp_Object pos;
6078
6079 eassert (!STRINGP (it->string));
6080
6081 /* If there isn't any `display' property in sight, and no
6082 overlays, we can just use the position of the newline in
6083 buffer text. */
6084 if (it->stop_charpos >= limit
6085 || ((pos = Fnext_single_property_change (make_number (start),
6086 Qdisplay, Qnil,
6087 make_number (limit)),
6088 NILP (pos))
6089 && next_overlay_change (start) == ZV))
6090 {
6091 if (!it->bidi_p)
6092 {
6093 IT_CHARPOS (*it) = limit;
6094 IT_BYTEPOS (*it) = bytepos;
6095 }
6096 else
6097 {
6098 struct bidi_it bprev;
6099
6100 /* Help bidi.c avoid expensive searches for display
6101 properties and overlays, by telling it that there are
6102 none up to `limit'. */
6103 if (it->bidi_it.disp_pos < limit)
6104 {
6105 it->bidi_it.disp_pos = limit;
6106 it->bidi_it.disp_prop = 0;
6107 }
6108 do {
6109 bprev = it->bidi_it;
6110 bidi_move_to_visually_next (&it->bidi_it);
6111 } while (it->bidi_it.charpos != limit);
6112 IT_CHARPOS (*it) = limit;
6113 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6114 if (bidi_it_prev)
6115 *bidi_it_prev = bprev;
6116 }
6117 *skipped_p = newline_found_p = 1;
6118 }
6119 else
6120 {
6121 while (get_next_display_element (it)
6122 && !newline_found_p)
6123 {
6124 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6125 if (newline_found_p && it->bidi_p && bidi_it_prev)
6126 *bidi_it_prev = it->bidi_it;
6127 set_iterator_to_next (it, 0);
6128 }
6129 }
6130 }
6131
6132 it->selective = old_selective;
6133 return newline_found_p;
6134 }
6135
6136
6137 /* Set IT's current position to the previous visible line start. Skip
6138 invisible text that is so either due to text properties or due to
6139 selective display. Caution: this does not change IT->current_x and
6140 IT->hpos. */
6141
6142 static void
6143 back_to_previous_visible_line_start (struct it *it)
6144 {
6145 while (IT_CHARPOS (*it) > BEGV)
6146 {
6147 back_to_previous_line_start (it);
6148
6149 if (IT_CHARPOS (*it) <= BEGV)
6150 break;
6151
6152 /* If selective > 0, then lines indented more than its value are
6153 invisible. */
6154 if (it->selective > 0
6155 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6156 it->selective))
6157 continue;
6158
6159 /* Check the newline before point for invisibility. */
6160 {
6161 Lisp_Object prop;
6162 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6163 Qinvisible, it->window);
6164 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6165 continue;
6166 }
6167
6168 if (IT_CHARPOS (*it) <= BEGV)
6169 break;
6170
6171 {
6172 struct it it2;
6173 void *it2data = NULL;
6174 ptrdiff_t pos;
6175 ptrdiff_t beg, end;
6176 Lisp_Object val, overlay;
6177
6178 SAVE_IT (it2, *it, it2data);
6179
6180 /* If newline is part of a composition, continue from start of composition */
6181 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6182 && beg < IT_CHARPOS (*it))
6183 goto replaced;
6184
6185 /* If newline is replaced by a display property, find start of overlay
6186 or interval and continue search from that point. */
6187 pos = --IT_CHARPOS (it2);
6188 --IT_BYTEPOS (it2);
6189 it2.sp = 0;
6190 bidi_unshelve_cache (NULL, 0);
6191 it2.string_from_display_prop_p = 0;
6192 it2.from_disp_prop_p = 0;
6193 if (handle_display_prop (&it2) == HANDLED_RETURN
6194 && !NILP (val = get_char_property_and_overlay
6195 (make_number (pos), Qdisplay, Qnil, &overlay))
6196 && (OVERLAYP (overlay)
6197 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6198 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6199 {
6200 RESTORE_IT (it, it, it2data);
6201 goto replaced;
6202 }
6203
6204 /* Newline is not replaced by anything -- so we are done. */
6205 RESTORE_IT (it, it, it2data);
6206 break;
6207
6208 replaced:
6209 if (beg < BEGV)
6210 beg = BEGV;
6211 IT_CHARPOS (*it) = beg;
6212 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6213 }
6214 }
6215
6216 it->continuation_lines_width = 0;
6217
6218 eassert (IT_CHARPOS (*it) >= BEGV);
6219 eassert (IT_CHARPOS (*it) == BEGV
6220 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6221 CHECK_IT (it);
6222 }
6223
6224
6225 /* Reseat iterator IT at the previous visible line start. Skip
6226 invisible text that is so either due to text properties or due to
6227 selective display. At the end, update IT's overlay information,
6228 face information etc. */
6229
6230 void
6231 reseat_at_previous_visible_line_start (struct it *it)
6232 {
6233 back_to_previous_visible_line_start (it);
6234 reseat (it, it->current.pos, 1);
6235 CHECK_IT (it);
6236 }
6237
6238
6239 /* Reseat iterator IT on the next visible line start in the current
6240 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6241 preceding the line start. Skip over invisible text that is so
6242 because of selective display. Compute faces, overlays etc at the
6243 new position. Note that this function does not skip over text that
6244 is invisible because of text properties. */
6245
6246 static void
6247 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6248 {
6249 int newline_found_p, skipped_p = 0;
6250 struct bidi_it bidi_it_prev;
6251
6252 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6253
6254 /* Skip over lines that are invisible because they are indented
6255 more than the value of IT->selective. */
6256 if (it->selective > 0)
6257 while (IT_CHARPOS (*it) < ZV
6258 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6259 it->selective))
6260 {
6261 eassert (IT_BYTEPOS (*it) == BEGV
6262 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6263 newline_found_p =
6264 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6265 }
6266
6267 /* Position on the newline if that's what's requested. */
6268 if (on_newline_p && newline_found_p)
6269 {
6270 if (STRINGP (it->string))
6271 {
6272 if (IT_STRING_CHARPOS (*it) > 0)
6273 {
6274 if (!it->bidi_p)
6275 {
6276 --IT_STRING_CHARPOS (*it);
6277 --IT_STRING_BYTEPOS (*it);
6278 }
6279 else
6280 {
6281 /* We need to restore the bidi iterator to the state
6282 it had on the newline, and resync the IT's
6283 position with that. */
6284 it->bidi_it = bidi_it_prev;
6285 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6286 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6287 }
6288 }
6289 }
6290 else if (IT_CHARPOS (*it) > BEGV)
6291 {
6292 if (!it->bidi_p)
6293 {
6294 --IT_CHARPOS (*it);
6295 --IT_BYTEPOS (*it);
6296 }
6297 else
6298 {
6299 /* We need to restore the bidi iterator to the state it
6300 had on the newline and resync IT with that. */
6301 it->bidi_it = bidi_it_prev;
6302 IT_CHARPOS (*it) = it->bidi_it.charpos;
6303 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6304 }
6305 reseat (it, it->current.pos, 0);
6306 }
6307 }
6308 else if (skipped_p)
6309 reseat (it, it->current.pos, 0);
6310
6311 CHECK_IT (it);
6312 }
6313
6314
6315 \f
6316 /***********************************************************************
6317 Changing an iterator's position
6318 ***********************************************************************/
6319
6320 /* Change IT's current position to POS in current_buffer. If FORCE_P
6321 is non-zero, always check for text properties at the new position.
6322 Otherwise, text properties are only looked up if POS >=
6323 IT->check_charpos of a property. */
6324
6325 static void
6326 reseat (struct it *it, struct text_pos pos, int force_p)
6327 {
6328 ptrdiff_t original_pos = IT_CHARPOS (*it);
6329
6330 reseat_1 (it, pos, 0);
6331
6332 /* Determine where to check text properties. Avoid doing it
6333 where possible because text property lookup is very expensive. */
6334 if (force_p
6335 || CHARPOS (pos) > it->stop_charpos
6336 || CHARPOS (pos) < original_pos)
6337 {
6338 if (it->bidi_p)
6339 {
6340 /* For bidi iteration, we need to prime prev_stop and
6341 base_level_stop with our best estimations. */
6342 /* Implementation note: Of course, POS is not necessarily a
6343 stop position, so assigning prev_pos to it is a lie; we
6344 should have called compute_stop_backwards. However, if
6345 the current buffer does not include any R2L characters,
6346 that call would be a waste of cycles, because the
6347 iterator will never move back, and thus never cross this
6348 "fake" stop position. So we delay that backward search
6349 until the time we really need it, in next_element_from_buffer. */
6350 if (CHARPOS (pos) != it->prev_stop)
6351 it->prev_stop = CHARPOS (pos);
6352 if (CHARPOS (pos) < it->base_level_stop)
6353 it->base_level_stop = 0; /* meaning it's unknown */
6354 handle_stop (it);
6355 }
6356 else
6357 {
6358 handle_stop (it);
6359 it->prev_stop = it->base_level_stop = 0;
6360 }
6361
6362 }
6363
6364 CHECK_IT (it);
6365 }
6366
6367
6368 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6369 IT->stop_pos to POS, also. */
6370
6371 static void
6372 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6373 {
6374 /* Don't call this function when scanning a C string. */
6375 eassert (it->s == NULL);
6376
6377 /* POS must be a reasonable value. */
6378 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6379
6380 it->current.pos = it->position = pos;
6381 it->end_charpos = ZV;
6382 it->dpvec = NULL;
6383 it->current.dpvec_index = -1;
6384 it->current.overlay_string_index = -1;
6385 IT_STRING_CHARPOS (*it) = -1;
6386 IT_STRING_BYTEPOS (*it) = -1;
6387 it->string = Qnil;
6388 it->method = GET_FROM_BUFFER;
6389 it->object = it->w->contents;
6390 it->area = TEXT_AREA;
6391 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6392 it->sp = 0;
6393 it->string_from_display_prop_p = 0;
6394 it->string_from_prefix_prop_p = 0;
6395
6396 it->from_disp_prop_p = 0;
6397 it->face_before_selective_p = 0;
6398 if (it->bidi_p)
6399 {
6400 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6401 &it->bidi_it);
6402 bidi_unshelve_cache (NULL, 0);
6403 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6404 it->bidi_it.string.s = NULL;
6405 it->bidi_it.string.lstring = Qnil;
6406 it->bidi_it.string.bufpos = 0;
6407 it->bidi_it.string.unibyte = 0;
6408 it->bidi_it.w = it->w;
6409 }
6410
6411 if (set_stop_p)
6412 {
6413 it->stop_charpos = CHARPOS (pos);
6414 it->base_level_stop = CHARPOS (pos);
6415 }
6416 /* This make the information stored in it->cmp_it invalidate. */
6417 it->cmp_it.id = -1;
6418 }
6419
6420
6421 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6422 If S is non-null, it is a C string to iterate over. Otherwise,
6423 STRING gives a Lisp string to iterate over.
6424
6425 If PRECISION > 0, don't return more then PRECISION number of
6426 characters from the string.
6427
6428 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6429 characters have been returned. FIELD_WIDTH < 0 means an infinite
6430 field width.
6431
6432 MULTIBYTE = 0 means disable processing of multibyte characters,
6433 MULTIBYTE > 0 means enable it,
6434 MULTIBYTE < 0 means use IT->multibyte_p.
6435
6436 IT must be initialized via a prior call to init_iterator before
6437 calling this function. */
6438
6439 static void
6440 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6441 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6442 int multibyte)
6443 {
6444 /* No region in strings. */
6445 it->region_beg_charpos = it->region_end_charpos = -1;
6446
6447 /* No text property checks performed by default, but see below. */
6448 it->stop_charpos = -1;
6449
6450 /* Set iterator position and end position. */
6451 memset (&it->current, 0, sizeof it->current);
6452 it->current.overlay_string_index = -1;
6453 it->current.dpvec_index = -1;
6454 eassert (charpos >= 0);
6455
6456 /* If STRING is specified, use its multibyteness, otherwise use the
6457 setting of MULTIBYTE, if specified. */
6458 if (multibyte >= 0)
6459 it->multibyte_p = multibyte > 0;
6460
6461 /* Bidirectional reordering of strings is controlled by the default
6462 value of bidi-display-reordering. Don't try to reorder while
6463 loading loadup.el, as the necessary character property tables are
6464 not yet available. */
6465 it->bidi_p =
6466 NILP (Vpurify_flag)
6467 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6468
6469 if (s == NULL)
6470 {
6471 eassert (STRINGP (string));
6472 it->string = string;
6473 it->s = NULL;
6474 it->end_charpos = it->string_nchars = SCHARS (string);
6475 it->method = GET_FROM_STRING;
6476 it->current.string_pos = string_pos (charpos, string);
6477
6478 if (it->bidi_p)
6479 {
6480 it->bidi_it.string.lstring = string;
6481 it->bidi_it.string.s = NULL;
6482 it->bidi_it.string.schars = it->end_charpos;
6483 it->bidi_it.string.bufpos = 0;
6484 it->bidi_it.string.from_disp_str = 0;
6485 it->bidi_it.string.unibyte = !it->multibyte_p;
6486 it->bidi_it.w = it->w;
6487 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6488 FRAME_WINDOW_P (it->f), &it->bidi_it);
6489 }
6490 }
6491 else
6492 {
6493 it->s = (const unsigned char *) s;
6494 it->string = Qnil;
6495
6496 /* Note that we use IT->current.pos, not it->current.string_pos,
6497 for displaying C strings. */
6498 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6499 if (it->multibyte_p)
6500 {
6501 it->current.pos = c_string_pos (charpos, s, 1);
6502 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6503 }
6504 else
6505 {
6506 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6507 it->end_charpos = it->string_nchars = strlen (s);
6508 }
6509
6510 if (it->bidi_p)
6511 {
6512 it->bidi_it.string.lstring = Qnil;
6513 it->bidi_it.string.s = (const unsigned char *) s;
6514 it->bidi_it.string.schars = it->end_charpos;
6515 it->bidi_it.string.bufpos = 0;
6516 it->bidi_it.string.from_disp_str = 0;
6517 it->bidi_it.string.unibyte = !it->multibyte_p;
6518 it->bidi_it.w = it->w;
6519 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6520 &it->bidi_it);
6521 }
6522 it->method = GET_FROM_C_STRING;
6523 }
6524
6525 /* PRECISION > 0 means don't return more than PRECISION characters
6526 from the string. */
6527 if (precision > 0 && it->end_charpos - charpos > precision)
6528 {
6529 it->end_charpos = it->string_nchars = charpos + precision;
6530 if (it->bidi_p)
6531 it->bidi_it.string.schars = it->end_charpos;
6532 }
6533
6534 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6535 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6536 FIELD_WIDTH < 0 means infinite field width. This is useful for
6537 padding with `-' at the end of a mode line. */
6538 if (field_width < 0)
6539 field_width = INFINITY;
6540 /* Implementation note: We deliberately don't enlarge
6541 it->bidi_it.string.schars here to fit it->end_charpos, because
6542 the bidi iterator cannot produce characters out of thin air. */
6543 if (field_width > it->end_charpos - charpos)
6544 it->end_charpos = charpos + field_width;
6545
6546 /* Use the standard display table for displaying strings. */
6547 if (DISP_TABLE_P (Vstandard_display_table))
6548 it->dp = XCHAR_TABLE (Vstandard_display_table);
6549
6550 it->stop_charpos = charpos;
6551 it->prev_stop = charpos;
6552 it->base_level_stop = 0;
6553 if (it->bidi_p)
6554 {
6555 it->bidi_it.first_elt = 1;
6556 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6557 it->bidi_it.disp_pos = -1;
6558 }
6559 if (s == NULL && it->multibyte_p)
6560 {
6561 ptrdiff_t endpos = SCHARS (it->string);
6562 if (endpos > it->end_charpos)
6563 endpos = it->end_charpos;
6564 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6565 it->string);
6566 }
6567 CHECK_IT (it);
6568 }
6569
6570
6571 \f
6572 /***********************************************************************
6573 Iteration
6574 ***********************************************************************/
6575
6576 /* Map enum it_method value to corresponding next_element_from_* function. */
6577
6578 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6579 {
6580 next_element_from_buffer,
6581 next_element_from_display_vector,
6582 next_element_from_string,
6583 next_element_from_c_string,
6584 next_element_from_image,
6585 next_element_from_stretch
6586 };
6587
6588 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6589
6590
6591 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6592 (possibly with the following characters). */
6593
6594 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6595 ((IT)->cmp_it.id >= 0 \
6596 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6597 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6598 END_CHARPOS, (IT)->w, \
6599 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6600 (IT)->string)))
6601
6602
6603 /* Lookup the char-table Vglyphless_char_display for character C (-1
6604 if we want information for no-font case), and return the display
6605 method symbol. By side-effect, update it->what and
6606 it->glyphless_method. This function is called from
6607 get_next_display_element for each character element, and from
6608 x_produce_glyphs when no suitable font was found. */
6609
6610 Lisp_Object
6611 lookup_glyphless_char_display (int c, struct it *it)
6612 {
6613 Lisp_Object glyphless_method = Qnil;
6614
6615 if (CHAR_TABLE_P (Vglyphless_char_display)
6616 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6617 {
6618 if (c >= 0)
6619 {
6620 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6621 if (CONSP (glyphless_method))
6622 glyphless_method = FRAME_WINDOW_P (it->f)
6623 ? XCAR (glyphless_method)
6624 : XCDR (glyphless_method);
6625 }
6626 else
6627 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6628 }
6629
6630 retry:
6631 if (NILP (glyphless_method))
6632 {
6633 if (c >= 0)
6634 /* The default is to display the character by a proper font. */
6635 return Qnil;
6636 /* The default for the no-font case is to display an empty box. */
6637 glyphless_method = Qempty_box;
6638 }
6639 if (EQ (glyphless_method, Qzero_width))
6640 {
6641 if (c >= 0)
6642 return glyphless_method;
6643 /* This method can't be used for the no-font case. */
6644 glyphless_method = Qempty_box;
6645 }
6646 if (EQ (glyphless_method, Qthin_space))
6647 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6648 else if (EQ (glyphless_method, Qempty_box))
6649 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6650 else if (EQ (glyphless_method, Qhex_code))
6651 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6652 else if (STRINGP (glyphless_method))
6653 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6654 else
6655 {
6656 /* Invalid value. We use the default method. */
6657 glyphless_method = Qnil;
6658 goto retry;
6659 }
6660 it->what = IT_GLYPHLESS;
6661 return glyphless_method;
6662 }
6663
6664 /* Merge escape glyph face and cache the result. */
6665
6666 static struct frame *last_escape_glyph_frame = NULL;
6667 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6668 static int last_escape_glyph_merged_face_id = 0;
6669
6670 static int
6671 merge_escape_glyph_face (struct it *it)
6672 {
6673 int face_id;
6674
6675 if (it->f == last_escape_glyph_frame
6676 && it->face_id == last_escape_glyph_face_id)
6677 face_id = last_escape_glyph_merged_face_id;
6678 else
6679 {
6680 /* Merge the `escape-glyph' face into the current face. */
6681 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6682 last_escape_glyph_frame = it->f;
6683 last_escape_glyph_face_id = it->face_id;
6684 last_escape_glyph_merged_face_id = face_id;
6685 }
6686 return face_id;
6687 }
6688
6689 /* Likewise for glyphless glyph face. */
6690
6691 static struct frame *last_glyphless_glyph_frame = NULL;
6692 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6693 static int last_glyphless_glyph_merged_face_id = 0;
6694
6695 int
6696 merge_glyphless_glyph_face (struct it *it)
6697 {
6698 int face_id;
6699
6700 if (it->f == last_glyphless_glyph_frame
6701 && it->face_id == last_glyphless_glyph_face_id)
6702 face_id = last_glyphless_glyph_merged_face_id;
6703 else
6704 {
6705 /* Merge the `glyphless-char' face into the current face. */
6706 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6707 last_glyphless_glyph_frame = it->f;
6708 last_glyphless_glyph_face_id = it->face_id;
6709 last_glyphless_glyph_merged_face_id = face_id;
6710 }
6711 return face_id;
6712 }
6713
6714 /* Load IT's display element fields with information about the next
6715 display element from the current position of IT. Value is zero if
6716 end of buffer (or C string) is reached. */
6717
6718 static int
6719 get_next_display_element (struct it *it)
6720 {
6721 /* Non-zero means that we found a display element. Zero means that
6722 we hit the end of what we iterate over. Performance note: the
6723 function pointer `method' used here turns out to be faster than
6724 using a sequence of if-statements. */
6725 int success_p;
6726
6727 get_next:
6728 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6729
6730 if (it->what == IT_CHARACTER)
6731 {
6732 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6733 and only if (a) the resolved directionality of that character
6734 is R..." */
6735 /* FIXME: Do we need an exception for characters from display
6736 tables? */
6737 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6738 it->c = bidi_mirror_char (it->c);
6739 /* Map via display table or translate control characters.
6740 IT->c, IT->len etc. have been set to the next character by
6741 the function call above. If we have a display table, and it
6742 contains an entry for IT->c, translate it. Don't do this if
6743 IT->c itself comes from a display table, otherwise we could
6744 end up in an infinite recursion. (An alternative could be to
6745 count the recursion depth of this function and signal an
6746 error when a certain maximum depth is reached.) Is it worth
6747 it? */
6748 if (success_p && it->dpvec == NULL)
6749 {
6750 Lisp_Object dv;
6751 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6752 int nonascii_space_p = 0;
6753 int nonascii_hyphen_p = 0;
6754 int c = it->c; /* This is the character to display. */
6755
6756 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6757 {
6758 eassert (SINGLE_BYTE_CHAR_P (c));
6759 if (unibyte_display_via_language_environment)
6760 {
6761 c = DECODE_CHAR (unibyte, c);
6762 if (c < 0)
6763 c = BYTE8_TO_CHAR (it->c);
6764 }
6765 else
6766 c = BYTE8_TO_CHAR (it->c);
6767 }
6768
6769 if (it->dp
6770 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6771 VECTORP (dv)))
6772 {
6773 struct Lisp_Vector *v = XVECTOR (dv);
6774
6775 /* Return the first character from the display table
6776 entry, if not empty. If empty, don't display the
6777 current character. */
6778 if (v->header.size)
6779 {
6780 it->dpvec_char_len = it->len;
6781 it->dpvec = v->u.contents;
6782 it->dpend = v->u.contents + v->header.size;
6783 it->current.dpvec_index = 0;
6784 it->dpvec_face_id = -1;
6785 it->saved_face_id = it->face_id;
6786 it->method = GET_FROM_DISPLAY_VECTOR;
6787 it->ellipsis_p = 0;
6788 }
6789 else
6790 {
6791 set_iterator_to_next (it, 0);
6792 }
6793 goto get_next;
6794 }
6795
6796 if (! NILP (lookup_glyphless_char_display (c, it)))
6797 {
6798 if (it->what == IT_GLYPHLESS)
6799 goto done;
6800 /* Don't display this character. */
6801 set_iterator_to_next (it, 0);
6802 goto get_next;
6803 }
6804
6805 /* If `nobreak-char-display' is non-nil, we display
6806 non-ASCII spaces and hyphens specially. */
6807 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6808 {
6809 if (c == 0xA0)
6810 nonascii_space_p = 1;
6811 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6812 nonascii_hyphen_p = 1;
6813 }
6814
6815 /* Translate control characters into `\003' or `^C' form.
6816 Control characters coming from a display table entry are
6817 currently not translated because we use IT->dpvec to hold
6818 the translation. This could easily be changed but I
6819 don't believe that it is worth doing.
6820
6821 The characters handled by `nobreak-char-display' must be
6822 translated too.
6823
6824 Non-printable characters and raw-byte characters are also
6825 translated to octal form. */
6826 if (((c < ' ' || c == 127) /* ASCII control chars */
6827 ? (it->area != TEXT_AREA
6828 /* In mode line, treat \n, \t like other crl chars. */
6829 || (c != '\t'
6830 && it->glyph_row
6831 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6832 || (c != '\n' && c != '\t'))
6833 : (nonascii_space_p
6834 || nonascii_hyphen_p
6835 || CHAR_BYTE8_P (c)
6836 || ! CHAR_PRINTABLE_P (c))))
6837 {
6838 /* C is a control character, non-ASCII space/hyphen,
6839 raw-byte, or a non-printable character which must be
6840 displayed either as '\003' or as `^C' where the '\\'
6841 and '^' can be defined in the display table. Fill
6842 IT->ctl_chars with glyphs for what we have to
6843 display. Then, set IT->dpvec to these glyphs. */
6844 Lisp_Object gc;
6845 int ctl_len;
6846 int face_id;
6847 int lface_id = 0;
6848 int escape_glyph;
6849
6850 /* Handle control characters with ^. */
6851
6852 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6853 {
6854 int g;
6855
6856 g = '^'; /* default glyph for Control */
6857 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6858 if (it->dp
6859 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6860 {
6861 g = GLYPH_CODE_CHAR (gc);
6862 lface_id = GLYPH_CODE_FACE (gc);
6863 }
6864
6865 face_id = (lface_id
6866 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6867 : merge_escape_glyph_face (it));
6868
6869 XSETINT (it->ctl_chars[0], g);
6870 XSETINT (it->ctl_chars[1], c ^ 0100);
6871 ctl_len = 2;
6872 goto display_control;
6873 }
6874
6875 /* Handle non-ascii space in the mode where it only gets
6876 highlighting. */
6877
6878 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6879 {
6880 /* Merge `nobreak-space' into the current face. */
6881 face_id = merge_faces (it->f, Qnobreak_space, 0,
6882 it->face_id);
6883 XSETINT (it->ctl_chars[0], ' ');
6884 ctl_len = 1;
6885 goto display_control;
6886 }
6887
6888 /* Handle sequences that start with the "escape glyph". */
6889
6890 /* the default escape glyph is \. */
6891 escape_glyph = '\\';
6892
6893 if (it->dp
6894 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6895 {
6896 escape_glyph = GLYPH_CODE_CHAR (gc);
6897 lface_id = GLYPH_CODE_FACE (gc);
6898 }
6899
6900 face_id = (lface_id
6901 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6902 : merge_escape_glyph_face (it));
6903
6904 /* Draw non-ASCII hyphen with just highlighting: */
6905
6906 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6907 {
6908 XSETINT (it->ctl_chars[0], '-');
6909 ctl_len = 1;
6910 goto display_control;
6911 }
6912
6913 /* Draw non-ASCII space/hyphen with escape glyph: */
6914
6915 if (nonascii_space_p || nonascii_hyphen_p)
6916 {
6917 XSETINT (it->ctl_chars[0], escape_glyph);
6918 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6919 ctl_len = 2;
6920 goto display_control;
6921 }
6922
6923 {
6924 char str[10];
6925 int len, i;
6926
6927 if (CHAR_BYTE8_P (c))
6928 /* Display \200 instead of \17777600. */
6929 c = CHAR_TO_BYTE8 (c);
6930 len = sprintf (str, "%03o", c);
6931
6932 XSETINT (it->ctl_chars[0], escape_glyph);
6933 for (i = 0; i < len; i++)
6934 XSETINT (it->ctl_chars[i + 1], str[i]);
6935 ctl_len = len + 1;
6936 }
6937
6938 display_control:
6939 /* Set up IT->dpvec and return first character from it. */
6940 it->dpvec_char_len = it->len;
6941 it->dpvec = it->ctl_chars;
6942 it->dpend = it->dpvec + ctl_len;
6943 it->current.dpvec_index = 0;
6944 it->dpvec_face_id = face_id;
6945 it->saved_face_id = it->face_id;
6946 it->method = GET_FROM_DISPLAY_VECTOR;
6947 it->ellipsis_p = 0;
6948 goto get_next;
6949 }
6950 it->char_to_display = c;
6951 }
6952 else if (success_p)
6953 {
6954 it->char_to_display = it->c;
6955 }
6956 }
6957
6958 #ifdef HAVE_WINDOW_SYSTEM
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 #endif /* HAVE_WINDOW_SYSTEM */
7000
7001 done:
7002 /* Is this character the last one of a run of characters with
7003 box? If yes, set IT->end_of_box_run_p to 1. */
7004 if (it->face_box_p
7005 && it->s == NULL)
7006 {
7007 if (it->method == GET_FROM_STRING && it->sp)
7008 {
7009 int face_id = underlying_face_id (it);
7010 struct face *face = FACE_FROM_ID (it->f, face_id);
7011
7012 if (face)
7013 {
7014 if (face->box == FACE_NO_BOX)
7015 {
7016 /* If the box comes from face properties in a
7017 display string, check faces in that string. */
7018 int string_face_id = face_after_it_pos (it);
7019 it->end_of_box_run_p
7020 = (FACE_FROM_ID (it->f, string_face_id)->box
7021 == FACE_NO_BOX);
7022 }
7023 /* Otherwise, the box comes from the underlying face.
7024 If this is the last string character displayed, check
7025 the next buffer location. */
7026 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7027 && (it->current.overlay_string_index
7028 == it->n_overlay_strings - 1))
7029 {
7030 ptrdiff_t ignore;
7031 int next_face_id;
7032 struct text_pos pos = it->current.pos;
7033 INC_TEXT_POS (pos, it->multibyte_p);
7034
7035 next_face_id = face_at_buffer_position
7036 (it->w, CHARPOS (pos), it->region_beg_charpos,
7037 it->region_end_charpos, &ignore,
7038 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7039 -1);
7040 it->end_of_box_run_p
7041 = (FACE_FROM_ID (it->f, next_face_id)->box
7042 == FACE_NO_BOX);
7043 }
7044 }
7045 }
7046 /* next_element_from_display_vector sets this flag according to
7047 faces of the display vector glyphs, see there. */
7048 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7049 {
7050 int face_id = face_after_it_pos (it);
7051 it->end_of_box_run_p
7052 = (face_id != it->face_id
7053 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7054 }
7055 }
7056 /* If we reached the end of the object we've been iterating (e.g., a
7057 display string or an overlay string), and there's something on
7058 IT->stack, proceed with what's on the stack. It doesn't make
7059 sense to return zero if there's unprocessed stuff on the stack,
7060 because otherwise that stuff will never be displayed. */
7061 if (!success_p && it->sp > 0)
7062 {
7063 set_iterator_to_next (it, 0);
7064 success_p = get_next_display_element (it);
7065 }
7066
7067 /* Value is 0 if end of buffer or string reached. */
7068 return success_p;
7069 }
7070
7071
7072 /* Move IT to the next display element.
7073
7074 RESEAT_P non-zero means if called on a newline in buffer text,
7075 skip to the next visible line start.
7076
7077 Functions get_next_display_element and set_iterator_to_next are
7078 separate because I find this arrangement easier to handle than a
7079 get_next_display_element function that also increments IT's
7080 position. The way it is we can first look at an iterator's current
7081 display element, decide whether it fits on a line, and if it does,
7082 increment the iterator position. The other way around we probably
7083 would either need a flag indicating whether the iterator has to be
7084 incremented the next time, or we would have to implement a
7085 decrement position function which would not be easy to write. */
7086
7087 void
7088 set_iterator_to_next (struct it *it, int reseat_p)
7089 {
7090 /* Reset flags indicating start and end of a sequence of characters
7091 with box. Reset them at the start of this function because
7092 moving the iterator to a new position might set them. */
7093 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7094
7095 switch (it->method)
7096 {
7097 case GET_FROM_BUFFER:
7098 /* The current display element of IT is a character from
7099 current_buffer. Advance in the buffer, and maybe skip over
7100 invisible lines that are so because of selective display. */
7101 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7102 reseat_at_next_visible_line_start (it, 0);
7103 else if (it->cmp_it.id >= 0)
7104 {
7105 /* We are currently getting glyphs from a composition. */
7106 int i;
7107
7108 if (! it->bidi_p)
7109 {
7110 IT_CHARPOS (*it) += it->cmp_it.nchars;
7111 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7112 if (it->cmp_it.to < it->cmp_it.nglyphs)
7113 {
7114 it->cmp_it.from = it->cmp_it.to;
7115 }
7116 else
7117 {
7118 it->cmp_it.id = -1;
7119 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7120 IT_BYTEPOS (*it),
7121 it->end_charpos, Qnil);
7122 }
7123 }
7124 else if (! it->cmp_it.reversed_p)
7125 {
7126 /* Composition created while scanning forward. */
7127 /* Update IT's char/byte positions to point to the first
7128 character of the next grapheme cluster, or to the
7129 character visually after the current composition. */
7130 for (i = 0; i < it->cmp_it.nchars; i++)
7131 bidi_move_to_visually_next (&it->bidi_it);
7132 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7133 IT_CHARPOS (*it) = it->bidi_it.charpos;
7134
7135 if (it->cmp_it.to < it->cmp_it.nglyphs)
7136 {
7137 /* Proceed to the next grapheme cluster. */
7138 it->cmp_it.from = it->cmp_it.to;
7139 }
7140 else
7141 {
7142 /* No more grapheme clusters in this composition.
7143 Find the next stop position. */
7144 ptrdiff_t stop = it->end_charpos;
7145 if (it->bidi_it.scan_dir < 0)
7146 /* Now we are scanning backward and don't know
7147 where to stop. */
7148 stop = -1;
7149 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7150 IT_BYTEPOS (*it), stop, Qnil);
7151 }
7152 }
7153 else
7154 {
7155 /* Composition created while scanning backward. */
7156 /* Update IT's char/byte positions to point to the last
7157 character of the previous grapheme cluster, or the
7158 character visually after the current composition. */
7159 for (i = 0; i < it->cmp_it.nchars; i++)
7160 bidi_move_to_visually_next (&it->bidi_it);
7161 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7162 IT_CHARPOS (*it) = it->bidi_it.charpos;
7163 if (it->cmp_it.from > 0)
7164 {
7165 /* Proceed to the previous grapheme cluster. */
7166 it->cmp_it.to = it->cmp_it.from;
7167 }
7168 else
7169 {
7170 /* No more grapheme clusters in this composition.
7171 Find the next stop position. */
7172 ptrdiff_t stop = it->end_charpos;
7173 if (it->bidi_it.scan_dir < 0)
7174 /* Now we are scanning backward and don't know
7175 where to stop. */
7176 stop = -1;
7177 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7178 IT_BYTEPOS (*it), stop, Qnil);
7179 }
7180 }
7181 }
7182 else
7183 {
7184 eassert (it->len != 0);
7185
7186 if (!it->bidi_p)
7187 {
7188 IT_BYTEPOS (*it) += it->len;
7189 IT_CHARPOS (*it) += 1;
7190 }
7191 else
7192 {
7193 int prev_scan_dir = it->bidi_it.scan_dir;
7194 /* If this is a new paragraph, determine its base
7195 direction (a.k.a. its base embedding level). */
7196 if (it->bidi_it.new_paragraph)
7197 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7198 bidi_move_to_visually_next (&it->bidi_it);
7199 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7200 IT_CHARPOS (*it) = it->bidi_it.charpos;
7201 if (prev_scan_dir != it->bidi_it.scan_dir)
7202 {
7203 /* As the scan direction was changed, we must
7204 re-compute the stop position for composition. */
7205 ptrdiff_t stop = it->end_charpos;
7206 if (it->bidi_it.scan_dir < 0)
7207 stop = -1;
7208 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7209 IT_BYTEPOS (*it), stop, Qnil);
7210 }
7211 }
7212 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7213 }
7214 break;
7215
7216 case GET_FROM_C_STRING:
7217 /* Current display element of IT is from a C string. */
7218 if (!it->bidi_p
7219 /* If the string position is beyond string's end, it means
7220 next_element_from_c_string is padding the string with
7221 blanks, in which case we bypass the bidi iterator,
7222 because it cannot deal with such virtual characters. */
7223 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7224 {
7225 IT_BYTEPOS (*it) += it->len;
7226 IT_CHARPOS (*it) += 1;
7227 }
7228 else
7229 {
7230 bidi_move_to_visually_next (&it->bidi_it);
7231 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7232 IT_CHARPOS (*it) = it->bidi_it.charpos;
7233 }
7234 break;
7235
7236 case GET_FROM_DISPLAY_VECTOR:
7237 /* Current display element of IT is from a display table entry.
7238 Advance in the display table definition. Reset it to null if
7239 end reached, and continue with characters from buffers/
7240 strings. */
7241 ++it->current.dpvec_index;
7242
7243 /* Restore face of the iterator to what they were before the
7244 display vector entry (these entries may contain faces). */
7245 it->face_id = it->saved_face_id;
7246
7247 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7248 {
7249 int recheck_faces = it->ellipsis_p;
7250
7251 if (it->s)
7252 it->method = GET_FROM_C_STRING;
7253 else if (STRINGP (it->string))
7254 it->method = GET_FROM_STRING;
7255 else
7256 {
7257 it->method = GET_FROM_BUFFER;
7258 it->object = it->w->contents;
7259 }
7260
7261 it->dpvec = NULL;
7262 it->current.dpvec_index = -1;
7263
7264 /* Skip over characters which were displayed via IT->dpvec. */
7265 if (it->dpvec_char_len < 0)
7266 reseat_at_next_visible_line_start (it, 1);
7267 else if (it->dpvec_char_len > 0)
7268 {
7269 if (it->method == GET_FROM_STRING
7270 && it->current.overlay_string_index >= 0
7271 && it->n_overlay_strings > 0)
7272 it->ignore_overlay_strings_at_pos_p = 1;
7273 it->len = it->dpvec_char_len;
7274 set_iterator_to_next (it, reseat_p);
7275 }
7276
7277 /* Maybe recheck faces after display vector */
7278 if (recheck_faces)
7279 it->stop_charpos = IT_CHARPOS (*it);
7280 }
7281 break;
7282
7283 case GET_FROM_STRING:
7284 /* Current display element is a character from a Lisp string. */
7285 eassert (it->s == NULL && STRINGP (it->string));
7286 /* Don't advance past string end. These conditions are true
7287 when set_iterator_to_next is called at the end of
7288 get_next_display_element, in which case the Lisp string is
7289 already exhausted, and all we want is pop the iterator
7290 stack. */
7291 if (it->current.overlay_string_index >= 0)
7292 {
7293 /* This is an overlay string, so there's no padding with
7294 spaces, and the number of characters in the string is
7295 where the string ends. */
7296 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7297 goto consider_string_end;
7298 }
7299 else
7300 {
7301 /* Not an overlay string. There could be padding, so test
7302 against it->end_charpos . */
7303 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7304 goto consider_string_end;
7305 }
7306 if (it->cmp_it.id >= 0)
7307 {
7308 int i;
7309
7310 if (! it->bidi_p)
7311 {
7312 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7313 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7314 if (it->cmp_it.to < it->cmp_it.nglyphs)
7315 it->cmp_it.from = it->cmp_it.to;
7316 else
7317 {
7318 it->cmp_it.id = -1;
7319 composition_compute_stop_pos (&it->cmp_it,
7320 IT_STRING_CHARPOS (*it),
7321 IT_STRING_BYTEPOS (*it),
7322 it->end_charpos, it->string);
7323 }
7324 }
7325 else if (! it->cmp_it.reversed_p)
7326 {
7327 for (i = 0; i < it->cmp_it.nchars; i++)
7328 bidi_move_to_visually_next (&it->bidi_it);
7329 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7330 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7331
7332 if (it->cmp_it.to < it->cmp_it.nglyphs)
7333 it->cmp_it.from = it->cmp_it.to;
7334 else
7335 {
7336 ptrdiff_t stop = it->end_charpos;
7337 if (it->bidi_it.scan_dir < 0)
7338 stop = -1;
7339 composition_compute_stop_pos (&it->cmp_it,
7340 IT_STRING_CHARPOS (*it),
7341 IT_STRING_BYTEPOS (*it), stop,
7342 it->string);
7343 }
7344 }
7345 else
7346 {
7347 for (i = 0; i < it->cmp_it.nchars; i++)
7348 bidi_move_to_visually_next (&it->bidi_it);
7349 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7350 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7351 if (it->cmp_it.from > 0)
7352 it->cmp_it.to = it->cmp_it.from;
7353 else
7354 {
7355 ptrdiff_t stop = it->end_charpos;
7356 if (it->bidi_it.scan_dir < 0)
7357 stop = -1;
7358 composition_compute_stop_pos (&it->cmp_it,
7359 IT_STRING_CHARPOS (*it),
7360 IT_STRING_BYTEPOS (*it), stop,
7361 it->string);
7362 }
7363 }
7364 }
7365 else
7366 {
7367 if (!it->bidi_p
7368 /* If the string position is beyond string's end, it
7369 means next_element_from_string is padding the string
7370 with blanks, in which case we bypass the bidi
7371 iterator, because it cannot deal with such virtual
7372 characters. */
7373 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7374 {
7375 IT_STRING_BYTEPOS (*it) += it->len;
7376 IT_STRING_CHARPOS (*it) += 1;
7377 }
7378 else
7379 {
7380 int prev_scan_dir = it->bidi_it.scan_dir;
7381
7382 bidi_move_to_visually_next (&it->bidi_it);
7383 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7384 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7385 if (prev_scan_dir != it->bidi_it.scan_dir)
7386 {
7387 ptrdiff_t stop = it->end_charpos;
7388
7389 if (it->bidi_it.scan_dir < 0)
7390 stop = -1;
7391 composition_compute_stop_pos (&it->cmp_it,
7392 IT_STRING_CHARPOS (*it),
7393 IT_STRING_BYTEPOS (*it), stop,
7394 it->string);
7395 }
7396 }
7397 }
7398
7399 consider_string_end:
7400
7401 if (it->current.overlay_string_index >= 0)
7402 {
7403 /* IT->string is an overlay string. Advance to the
7404 next, if there is one. */
7405 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7406 {
7407 it->ellipsis_p = 0;
7408 next_overlay_string (it);
7409 if (it->ellipsis_p)
7410 setup_for_ellipsis (it, 0);
7411 }
7412 }
7413 else
7414 {
7415 /* IT->string is not an overlay string. If we reached
7416 its end, and there is something on IT->stack, proceed
7417 with what is on the stack. This can be either another
7418 string, this time an overlay string, or a buffer. */
7419 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7420 && it->sp > 0)
7421 {
7422 pop_it (it);
7423 if (it->method == GET_FROM_STRING)
7424 goto consider_string_end;
7425 }
7426 }
7427 break;
7428
7429 case GET_FROM_IMAGE:
7430 case GET_FROM_STRETCH:
7431 /* The position etc with which we have to proceed are on
7432 the stack. The position may be at the end of a string,
7433 if the `display' property takes up the whole string. */
7434 eassert (it->sp > 0);
7435 pop_it (it);
7436 if (it->method == GET_FROM_STRING)
7437 goto consider_string_end;
7438 break;
7439
7440 default:
7441 /* There are no other methods defined, so this should be a bug. */
7442 emacs_abort ();
7443 }
7444
7445 eassert (it->method != GET_FROM_STRING
7446 || (STRINGP (it->string)
7447 && IT_STRING_CHARPOS (*it) >= 0));
7448 }
7449
7450 /* Load IT's display element fields with information about the next
7451 display element which comes from a display table entry or from the
7452 result of translating a control character to one of the forms `^C'
7453 or `\003'.
7454
7455 IT->dpvec holds the glyphs to return as characters.
7456 IT->saved_face_id holds the face id before the display vector--it
7457 is restored into IT->face_id in set_iterator_to_next. */
7458
7459 static int
7460 next_element_from_display_vector (struct it *it)
7461 {
7462 Lisp_Object gc;
7463 int prev_face_id = it->face_id;
7464 int next_face_id;
7465
7466 /* Precondition. */
7467 eassert (it->dpvec && it->current.dpvec_index >= 0);
7468
7469 it->face_id = it->saved_face_id;
7470
7471 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7472 That seemed totally bogus - so I changed it... */
7473 gc = it->dpvec[it->current.dpvec_index];
7474
7475 if (GLYPH_CODE_P (gc))
7476 {
7477 struct face *this_face, *prev_face, *next_face;
7478
7479 it->c = GLYPH_CODE_CHAR (gc);
7480 it->len = CHAR_BYTES (it->c);
7481
7482 /* The entry may contain a face id to use. Such a face id is
7483 the id of a Lisp face, not a realized face. A face id of
7484 zero means no face is specified. */
7485 if (it->dpvec_face_id >= 0)
7486 it->face_id = it->dpvec_face_id;
7487 else
7488 {
7489 int lface_id = GLYPH_CODE_FACE (gc);
7490 if (lface_id > 0)
7491 it->face_id = merge_faces (it->f, Qt, lface_id,
7492 it->saved_face_id);
7493 }
7494
7495 /* Glyphs in the display vector could have the box face, so we
7496 need to set the related flags in the iterator, as
7497 appropriate. */
7498 this_face = FACE_FROM_ID (it->f, it->face_id);
7499 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7500
7501 /* Is this character the first character of a box-face run? */
7502 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7503 && (!prev_face
7504 || prev_face->box == FACE_NO_BOX));
7505
7506 /* For the last character of the box-face run, we need to look
7507 either at the next glyph from the display vector, or at the
7508 face we saw before the display vector. */
7509 next_face_id = it->saved_face_id;
7510 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7511 {
7512 if (it->dpvec_face_id >= 0)
7513 next_face_id = it->dpvec_face_id;
7514 else
7515 {
7516 int lface_id =
7517 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7518
7519 if (lface_id > 0)
7520 next_face_id = merge_faces (it->f, Qt, lface_id,
7521 it->saved_face_id);
7522 }
7523 }
7524 next_face = FACE_FROM_ID (it->f, next_face_id);
7525 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7526 && (!next_face
7527 || next_face->box == FACE_NO_BOX));
7528 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7529 }
7530 else
7531 /* Display table entry is invalid. Return a space. */
7532 it->c = ' ', it->len = 1;
7533
7534 /* Don't change position and object of the iterator here. They are
7535 still the values of the character that had this display table
7536 entry or was translated, and that's what we want. */
7537 it->what = IT_CHARACTER;
7538 return 1;
7539 }
7540
7541 /* Get the first element of string/buffer in the visual order, after
7542 being reseated to a new position in a string or a buffer. */
7543 static void
7544 get_visually_first_element (struct it *it)
7545 {
7546 int string_p = STRINGP (it->string) || it->s;
7547 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7548 ptrdiff_t bob = (string_p ? 0 : BEGV);
7549
7550 if (STRINGP (it->string))
7551 {
7552 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7553 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7554 }
7555 else
7556 {
7557 it->bidi_it.charpos = IT_CHARPOS (*it);
7558 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7559 }
7560
7561 if (it->bidi_it.charpos == eob)
7562 {
7563 /* Nothing to do, but reset the FIRST_ELT flag, like
7564 bidi_paragraph_init does, because we are not going to
7565 call it. */
7566 it->bidi_it.first_elt = 0;
7567 }
7568 else if (it->bidi_it.charpos == bob
7569 || (!string_p
7570 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7571 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7572 {
7573 /* If we are at the beginning of a line/string, we can produce
7574 the next element right away. */
7575 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7576 bidi_move_to_visually_next (&it->bidi_it);
7577 }
7578 else
7579 {
7580 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7581
7582 /* We need to prime the bidi iterator starting at the line's or
7583 string's beginning, before we will be able to produce the
7584 next element. */
7585 if (string_p)
7586 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7587 else
7588 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7589 IT_BYTEPOS (*it), -1,
7590 &it->bidi_it.bytepos);
7591 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7592 do
7593 {
7594 /* Now return to buffer/string position where we were asked
7595 to get the next display element, and produce that. */
7596 bidi_move_to_visually_next (&it->bidi_it);
7597 }
7598 while (it->bidi_it.bytepos != orig_bytepos
7599 && it->bidi_it.charpos < eob);
7600 }
7601
7602 /* Adjust IT's position information to where we ended up. */
7603 if (STRINGP (it->string))
7604 {
7605 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7606 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7607 }
7608 else
7609 {
7610 IT_CHARPOS (*it) = it->bidi_it.charpos;
7611 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7612 }
7613
7614 if (STRINGP (it->string) || !it->s)
7615 {
7616 ptrdiff_t stop, charpos, bytepos;
7617
7618 if (STRINGP (it->string))
7619 {
7620 eassert (!it->s);
7621 stop = SCHARS (it->string);
7622 if (stop > it->end_charpos)
7623 stop = it->end_charpos;
7624 charpos = IT_STRING_CHARPOS (*it);
7625 bytepos = IT_STRING_BYTEPOS (*it);
7626 }
7627 else
7628 {
7629 stop = it->end_charpos;
7630 charpos = IT_CHARPOS (*it);
7631 bytepos = IT_BYTEPOS (*it);
7632 }
7633 if (it->bidi_it.scan_dir < 0)
7634 stop = -1;
7635 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7636 it->string);
7637 }
7638 }
7639
7640 /* Load IT with the next display element from Lisp string IT->string.
7641 IT->current.string_pos is the current position within the string.
7642 If IT->current.overlay_string_index >= 0, the Lisp string is an
7643 overlay string. */
7644
7645 static int
7646 next_element_from_string (struct it *it)
7647 {
7648 struct text_pos position;
7649
7650 eassert (STRINGP (it->string));
7651 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7652 eassert (IT_STRING_CHARPOS (*it) >= 0);
7653 position = it->current.string_pos;
7654
7655 /* With bidi reordering, the character to display might not be the
7656 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7657 that we were reseat()ed to a new string, whose paragraph
7658 direction is not known. */
7659 if (it->bidi_p && it->bidi_it.first_elt)
7660 {
7661 get_visually_first_element (it);
7662 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7663 }
7664
7665 /* Time to check for invisible text? */
7666 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7667 {
7668 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7669 {
7670 if (!(!it->bidi_p
7671 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7672 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7673 {
7674 /* With bidi non-linear iteration, we could find
7675 ourselves far beyond the last computed stop_charpos,
7676 with several other stop positions in between that we
7677 missed. Scan them all now, in buffer's logical
7678 order, until we find and handle the last stop_charpos
7679 that precedes our current position. */
7680 handle_stop_backwards (it, it->stop_charpos);
7681 return GET_NEXT_DISPLAY_ELEMENT (it);
7682 }
7683 else
7684 {
7685 if (it->bidi_p)
7686 {
7687 /* Take note of the stop position we just moved
7688 across, for when we will move back across it. */
7689 it->prev_stop = it->stop_charpos;
7690 /* If we are at base paragraph embedding level, take
7691 note of the last stop position seen at this
7692 level. */
7693 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7694 it->base_level_stop = it->stop_charpos;
7695 }
7696 handle_stop (it);
7697
7698 /* Since a handler may have changed IT->method, we must
7699 recurse here. */
7700 return GET_NEXT_DISPLAY_ELEMENT (it);
7701 }
7702 }
7703 else if (it->bidi_p
7704 /* If we are before prev_stop, we may have overstepped
7705 on our way backwards a stop_pos, and if so, we need
7706 to handle that stop_pos. */
7707 && IT_STRING_CHARPOS (*it) < it->prev_stop
7708 /* We can sometimes back up for reasons that have nothing
7709 to do with bidi reordering. E.g., compositions. The
7710 code below is only needed when we are above the base
7711 embedding level, so test for that explicitly. */
7712 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7713 {
7714 /* If we lost track of base_level_stop, we have no better
7715 place for handle_stop_backwards to start from than string
7716 beginning. This happens, e.g., when we were reseated to
7717 the previous screenful of text by vertical-motion. */
7718 if (it->base_level_stop <= 0
7719 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7720 it->base_level_stop = 0;
7721 handle_stop_backwards (it, it->base_level_stop);
7722 return GET_NEXT_DISPLAY_ELEMENT (it);
7723 }
7724 }
7725
7726 if (it->current.overlay_string_index >= 0)
7727 {
7728 /* Get the next character from an overlay string. In overlay
7729 strings, there is no field width or padding with spaces to
7730 do. */
7731 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7732 {
7733 it->what = IT_EOB;
7734 return 0;
7735 }
7736 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7737 IT_STRING_BYTEPOS (*it),
7738 it->bidi_it.scan_dir < 0
7739 ? -1
7740 : SCHARS (it->string))
7741 && next_element_from_composition (it))
7742 {
7743 return 1;
7744 }
7745 else if (STRING_MULTIBYTE (it->string))
7746 {
7747 const unsigned char *s = (SDATA (it->string)
7748 + IT_STRING_BYTEPOS (*it));
7749 it->c = string_char_and_length (s, &it->len);
7750 }
7751 else
7752 {
7753 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7754 it->len = 1;
7755 }
7756 }
7757 else
7758 {
7759 /* Get the next character from a Lisp string that is not an
7760 overlay string. Such strings come from the mode line, for
7761 example. We may have to pad with spaces, or truncate the
7762 string. See also next_element_from_c_string. */
7763 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7764 {
7765 it->what = IT_EOB;
7766 return 0;
7767 }
7768 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7769 {
7770 /* Pad with spaces. */
7771 it->c = ' ', it->len = 1;
7772 CHARPOS (position) = BYTEPOS (position) = -1;
7773 }
7774 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7775 IT_STRING_BYTEPOS (*it),
7776 it->bidi_it.scan_dir < 0
7777 ? -1
7778 : it->string_nchars)
7779 && next_element_from_composition (it))
7780 {
7781 return 1;
7782 }
7783 else if (STRING_MULTIBYTE (it->string))
7784 {
7785 const unsigned char *s = (SDATA (it->string)
7786 + IT_STRING_BYTEPOS (*it));
7787 it->c = string_char_and_length (s, &it->len);
7788 }
7789 else
7790 {
7791 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7792 it->len = 1;
7793 }
7794 }
7795
7796 /* Record what we have and where it came from. */
7797 it->what = IT_CHARACTER;
7798 it->object = it->string;
7799 it->position = position;
7800 return 1;
7801 }
7802
7803
7804 /* Load IT with next display element from C string IT->s.
7805 IT->string_nchars is the maximum number of characters to return
7806 from the string. IT->end_charpos may be greater than
7807 IT->string_nchars when this function is called, in which case we
7808 may have to return padding spaces. Value is zero if end of string
7809 reached, including padding spaces. */
7810
7811 static int
7812 next_element_from_c_string (struct it *it)
7813 {
7814 int success_p = 1;
7815
7816 eassert (it->s);
7817 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7818 it->what = IT_CHARACTER;
7819 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7820 it->object = Qnil;
7821
7822 /* With bidi reordering, the character to display might not be the
7823 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7824 we were reseated to a new string, whose paragraph direction is
7825 not known. */
7826 if (it->bidi_p && it->bidi_it.first_elt)
7827 get_visually_first_element (it);
7828
7829 /* IT's position can be greater than IT->string_nchars in case a
7830 field width or precision has been specified when the iterator was
7831 initialized. */
7832 if (IT_CHARPOS (*it) >= it->end_charpos)
7833 {
7834 /* End of the game. */
7835 it->what = IT_EOB;
7836 success_p = 0;
7837 }
7838 else if (IT_CHARPOS (*it) >= it->string_nchars)
7839 {
7840 /* Pad with spaces. */
7841 it->c = ' ', it->len = 1;
7842 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7843 }
7844 else if (it->multibyte_p)
7845 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7846 else
7847 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7848
7849 return success_p;
7850 }
7851
7852
7853 /* Set up IT to return characters from an ellipsis, if appropriate.
7854 The definition of the ellipsis glyphs may come from a display table
7855 entry. This function fills IT with the first glyph from the
7856 ellipsis if an ellipsis is to be displayed. */
7857
7858 static int
7859 next_element_from_ellipsis (struct it *it)
7860 {
7861 if (it->selective_display_ellipsis_p)
7862 setup_for_ellipsis (it, it->len);
7863 else
7864 {
7865 /* The face at the current position may be different from the
7866 face we find after the invisible text. Remember what it
7867 was in IT->saved_face_id, and signal that it's there by
7868 setting face_before_selective_p. */
7869 it->saved_face_id = it->face_id;
7870 it->method = GET_FROM_BUFFER;
7871 it->object = it->w->contents;
7872 reseat_at_next_visible_line_start (it, 1);
7873 it->face_before_selective_p = 1;
7874 }
7875
7876 return GET_NEXT_DISPLAY_ELEMENT (it);
7877 }
7878
7879
7880 /* Deliver an image display element. The iterator IT is already
7881 filled with image information (done in handle_display_prop). Value
7882 is always 1. */
7883
7884
7885 static int
7886 next_element_from_image (struct it *it)
7887 {
7888 it->what = IT_IMAGE;
7889 it->ignore_overlay_strings_at_pos_p = 0;
7890 return 1;
7891 }
7892
7893
7894 /* Fill iterator IT with next display element from a stretch glyph
7895 property. IT->object is the value of the text property. Value is
7896 always 1. */
7897
7898 static int
7899 next_element_from_stretch (struct it *it)
7900 {
7901 it->what = IT_STRETCH;
7902 return 1;
7903 }
7904
7905 /* Scan backwards from IT's current position until we find a stop
7906 position, or until BEGV. This is called when we find ourself
7907 before both the last known prev_stop and base_level_stop while
7908 reordering bidirectional text. */
7909
7910 static void
7911 compute_stop_pos_backwards (struct it *it)
7912 {
7913 const int SCAN_BACK_LIMIT = 1000;
7914 struct text_pos pos;
7915 struct display_pos save_current = it->current;
7916 struct text_pos save_position = it->position;
7917 ptrdiff_t charpos = IT_CHARPOS (*it);
7918 ptrdiff_t where_we_are = charpos;
7919 ptrdiff_t save_stop_pos = it->stop_charpos;
7920 ptrdiff_t save_end_pos = it->end_charpos;
7921
7922 eassert (NILP (it->string) && !it->s);
7923 eassert (it->bidi_p);
7924 it->bidi_p = 0;
7925 do
7926 {
7927 it->end_charpos = min (charpos + 1, ZV);
7928 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7929 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7930 reseat_1 (it, pos, 0);
7931 compute_stop_pos (it);
7932 /* We must advance forward, right? */
7933 if (it->stop_charpos <= charpos)
7934 emacs_abort ();
7935 }
7936 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7937
7938 if (it->stop_charpos <= where_we_are)
7939 it->prev_stop = it->stop_charpos;
7940 else
7941 it->prev_stop = BEGV;
7942 it->bidi_p = 1;
7943 it->current = save_current;
7944 it->position = save_position;
7945 it->stop_charpos = save_stop_pos;
7946 it->end_charpos = save_end_pos;
7947 }
7948
7949 /* Scan forward from CHARPOS in the current buffer/string, until we
7950 find a stop position > current IT's position. Then handle the stop
7951 position before that. This is called when we bump into a stop
7952 position while reordering bidirectional text. CHARPOS should be
7953 the last previously processed stop_pos (or BEGV/0, if none were
7954 processed yet) whose position is less that IT's current
7955 position. */
7956
7957 static void
7958 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7959 {
7960 int bufp = !STRINGP (it->string);
7961 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7962 struct display_pos save_current = it->current;
7963 struct text_pos save_position = it->position;
7964 struct text_pos pos1;
7965 ptrdiff_t next_stop;
7966
7967 /* Scan in strict logical order. */
7968 eassert (it->bidi_p);
7969 it->bidi_p = 0;
7970 do
7971 {
7972 it->prev_stop = charpos;
7973 if (bufp)
7974 {
7975 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7976 reseat_1 (it, pos1, 0);
7977 }
7978 else
7979 it->current.string_pos = string_pos (charpos, it->string);
7980 compute_stop_pos (it);
7981 /* We must advance forward, right? */
7982 if (it->stop_charpos <= it->prev_stop)
7983 emacs_abort ();
7984 charpos = it->stop_charpos;
7985 }
7986 while (charpos <= where_we_are);
7987
7988 it->bidi_p = 1;
7989 it->current = save_current;
7990 it->position = save_position;
7991 next_stop = it->stop_charpos;
7992 it->stop_charpos = it->prev_stop;
7993 handle_stop (it);
7994 it->stop_charpos = next_stop;
7995 }
7996
7997 /* Load IT with the next display element from current_buffer. Value
7998 is zero if end of buffer reached. IT->stop_charpos is the next
7999 position at which to stop and check for text properties or buffer
8000 end. */
8001
8002 static int
8003 next_element_from_buffer (struct it *it)
8004 {
8005 int success_p = 1;
8006
8007 eassert (IT_CHARPOS (*it) >= BEGV);
8008 eassert (NILP (it->string) && !it->s);
8009 eassert (!it->bidi_p
8010 || (EQ (it->bidi_it.string.lstring, Qnil)
8011 && it->bidi_it.string.s == NULL));
8012
8013 /* With bidi reordering, the character to display might not be the
8014 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8015 we were reseat()ed to a new buffer position, which is potentially
8016 a different paragraph. */
8017 if (it->bidi_p && it->bidi_it.first_elt)
8018 {
8019 get_visually_first_element (it);
8020 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8021 }
8022
8023 if (IT_CHARPOS (*it) >= it->stop_charpos)
8024 {
8025 if (IT_CHARPOS (*it) >= it->end_charpos)
8026 {
8027 int overlay_strings_follow_p;
8028
8029 /* End of the game, except when overlay strings follow that
8030 haven't been returned yet. */
8031 if (it->overlay_strings_at_end_processed_p)
8032 overlay_strings_follow_p = 0;
8033 else
8034 {
8035 it->overlay_strings_at_end_processed_p = 1;
8036 overlay_strings_follow_p = get_overlay_strings (it, 0);
8037 }
8038
8039 if (overlay_strings_follow_p)
8040 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8041 else
8042 {
8043 it->what = IT_EOB;
8044 it->position = it->current.pos;
8045 success_p = 0;
8046 }
8047 }
8048 else if (!(!it->bidi_p
8049 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8050 || IT_CHARPOS (*it) == it->stop_charpos))
8051 {
8052 /* With bidi non-linear iteration, we could find ourselves
8053 far beyond the last computed stop_charpos, with several
8054 other stop positions in between that we missed. Scan
8055 them all now, in buffer's logical order, until we find
8056 and handle the last stop_charpos that precedes our
8057 current position. */
8058 handle_stop_backwards (it, it->stop_charpos);
8059 return GET_NEXT_DISPLAY_ELEMENT (it);
8060 }
8061 else
8062 {
8063 if (it->bidi_p)
8064 {
8065 /* Take note of the stop position we just moved across,
8066 for when we will move back across it. */
8067 it->prev_stop = it->stop_charpos;
8068 /* If we are at base paragraph embedding level, take
8069 note of the last stop position seen at this
8070 level. */
8071 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8072 it->base_level_stop = it->stop_charpos;
8073 }
8074 handle_stop (it);
8075 return GET_NEXT_DISPLAY_ELEMENT (it);
8076 }
8077 }
8078 else if (it->bidi_p
8079 /* If we are before prev_stop, we may have overstepped on
8080 our way backwards a stop_pos, and if so, we need to
8081 handle that stop_pos. */
8082 && IT_CHARPOS (*it) < it->prev_stop
8083 /* We can sometimes back up for reasons that have nothing
8084 to do with bidi reordering. E.g., compositions. The
8085 code below is only needed when we are above the base
8086 embedding level, so test for that explicitly. */
8087 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8088 {
8089 if (it->base_level_stop <= 0
8090 || IT_CHARPOS (*it) < it->base_level_stop)
8091 {
8092 /* If we lost track of base_level_stop, we need to find
8093 prev_stop by looking backwards. This happens, e.g., when
8094 we were reseated to the previous screenful of text by
8095 vertical-motion. */
8096 it->base_level_stop = BEGV;
8097 compute_stop_pos_backwards (it);
8098 handle_stop_backwards (it, it->prev_stop);
8099 }
8100 else
8101 handle_stop_backwards (it, it->base_level_stop);
8102 return GET_NEXT_DISPLAY_ELEMENT (it);
8103 }
8104 else
8105 {
8106 /* No face changes, overlays etc. in sight, so just return a
8107 character from current_buffer. */
8108 unsigned char *p;
8109 ptrdiff_t stop;
8110
8111 /* Maybe run the redisplay end trigger hook. Performance note:
8112 This doesn't seem to cost measurable time. */
8113 if (it->redisplay_end_trigger_charpos
8114 && it->glyph_row
8115 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8116 run_redisplay_end_trigger_hook (it);
8117
8118 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8119 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8120 stop)
8121 && next_element_from_composition (it))
8122 {
8123 return 1;
8124 }
8125
8126 /* Get the next character, maybe multibyte. */
8127 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8128 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8129 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8130 else
8131 it->c = *p, it->len = 1;
8132
8133 /* Record what we have and where it came from. */
8134 it->what = IT_CHARACTER;
8135 it->object = it->w->contents;
8136 it->position = it->current.pos;
8137
8138 /* Normally we return the character found above, except when we
8139 really want to return an ellipsis for selective display. */
8140 if (it->selective)
8141 {
8142 if (it->c == '\n')
8143 {
8144 /* A value of selective > 0 means hide lines indented more
8145 than that number of columns. */
8146 if (it->selective > 0
8147 && IT_CHARPOS (*it) + 1 < ZV
8148 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8149 IT_BYTEPOS (*it) + 1,
8150 it->selective))
8151 {
8152 success_p = next_element_from_ellipsis (it);
8153 it->dpvec_char_len = -1;
8154 }
8155 }
8156 else if (it->c == '\r' && it->selective == -1)
8157 {
8158 /* A value of selective == -1 means that everything from the
8159 CR to the end of the line is invisible, with maybe an
8160 ellipsis displayed for it. */
8161 success_p = next_element_from_ellipsis (it);
8162 it->dpvec_char_len = -1;
8163 }
8164 }
8165 }
8166
8167 /* Value is zero if end of buffer reached. */
8168 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8169 return success_p;
8170 }
8171
8172
8173 /* Run the redisplay end trigger hook for IT. */
8174
8175 static void
8176 run_redisplay_end_trigger_hook (struct it *it)
8177 {
8178 Lisp_Object args[3];
8179
8180 /* IT->glyph_row should be non-null, i.e. we should be actually
8181 displaying something, or otherwise we should not run the hook. */
8182 eassert (it->glyph_row);
8183
8184 /* Set up hook arguments. */
8185 args[0] = Qredisplay_end_trigger_functions;
8186 args[1] = it->window;
8187 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8188 it->redisplay_end_trigger_charpos = 0;
8189
8190 /* Since we are *trying* to run these functions, don't try to run
8191 them again, even if they get an error. */
8192 wset_redisplay_end_trigger (it->w, Qnil);
8193 Frun_hook_with_args (3, args);
8194
8195 /* Notice if it changed the face of the character we are on. */
8196 handle_face_prop (it);
8197 }
8198
8199
8200 /* Deliver a composition display element. Unlike the other
8201 next_element_from_XXX, this function is not registered in the array
8202 get_next_element[]. It is called from next_element_from_buffer and
8203 next_element_from_string when necessary. */
8204
8205 static int
8206 next_element_from_composition (struct it *it)
8207 {
8208 it->what = IT_COMPOSITION;
8209 it->len = it->cmp_it.nbytes;
8210 if (STRINGP (it->string))
8211 {
8212 if (it->c < 0)
8213 {
8214 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8215 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8216 return 0;
8217 }
8218 it->position = it->current.string_pos;
8219 it->object = it->string;
8220 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8221 IT_STRING_BYTEPOS (*it), it->string);
8222 }
8223 else
8224 {
8225 if (it->c < 0)
8226 {
8227 IT_CHARPOS (*it) += it->cmp_it.nchars;
8228 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8229 if (it->bidi_p)
8230 {
8231 if (it->bidi_it.new_paragraph)
8232 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8233 /* Resync the bidi iterator with IT's new position.
8234 FIXME: this doesn't support bidirectional text. */
8235 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8236 bidi_move_to_visually_next (&it->bidi_it);
8237 }
8238 return 0;
8239 }
8240 it->position = it->current.pos;
8241 it->object = it->w->contents;
8242 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8243 IT_BYTEPOS (*it), Qnil);
8244 }
8245 return 1;
8246 }
8247
8248
8249 \f
8250 /***********************************************************************
8251 Moving an iterator without producing glyphs
8252 ***********************************************************************/
8253
8254 /* Check if iterator is at a position corresponding to a valid buffer
8255 position after some move_it_ call. */
8256
8257 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8258 ((it)->method == GET_FROM_STRING \
8259 ? IT_STRING_CHARPOS (*it) == 0 \
8260 : 1)
8261
8262
8263 /* Move iterator IT to a specified buffer or X position within one
8264 line on the display without producing glyphs.
8265
8266 OP should be a bit mask including some or all of these bits:
8267 MOVE_TO_X: Stop upon reaching x-position TO_X.
8268 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8269 Regardless of OP's value, stop upon reaching the end of the display line.
8270
8271 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8272 This means, in particular, that TO_X includes window's horizontal
8273 scroll amount.
8274
8275 The return value has several possible values that
8276 say what condition caused the scan to stop:
8277
8278 MOVE_POS_MATCH_OR_ZV
8279 - when TO_POS or ZV was reached.
8280
8281 MOVE_X_REACHED
8282 -when TO_X was reached before TO_POS or ZV were reached.
8283
8284 MOVE_LINE_CONTINUED
8285 - when we reached the end of the display area and the line must
8286 be continued.
8287
8288 MOVE_LINE_TRUNCATED
8289 - when we reached the end of the display area and the line is
8290 truncated.
8291
8292 MOVE_NEWLINE_OR_CR
8293 - when we stopped at a line end, i.e. a newline or a CR and selective
8294 display is on. */
8295
8296 static enum move_it_result
8297 move_it_in_display_line_to (struct it *it,
8298 ptrdiff_t to_charpos, int to_x,
8299 enum move_operation_enum op)
8300 {
8301 enum move_it_result result = MOVE_UNDEFINED;
8302 struct glyph_row *saved_glyph_row;
8303 struct it wrap_it, atpos_it, atx_it, ppos_it;
8304 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8305 void *ppos_data = NULL;
8306 int may_wrap = 0;
8307 enum it_method prev_method = it->method;
8308 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8309 int saw_smaller_pos = prev_pos < to_charpos;
8310
8311 /* Don't produce glyphs in produce_glyphs. */
8312 saved_glyph_row = it->glyph_row;
8313 it->glyph_row = NULL;
8314
8315 /* Use wrap_it to save a copy of IT wherever a word wrap could
8316 occur. Use atpos_it to save a copy of IT at the desired buffer
8317 position, if found, so that we can scan ahead and check if the
8318 word later overshoots the window edge. Use atx_it similarly, for
8319 pixel positions. */
8320 wrap_it.sp = -1;
8321 atpos_it.sp = -1;
8322 atx_it.sp = -1;
8323
8324 /* Use ppos_it under bidi reordering to save a copy of IT for the
8325 position > CHARPOS that is the closest to CHARPOS. We restore
8326 that position in IT when we have scanned the entire display line
8327 without finding a match for CHARPOS and all the character
8328 positions are greater than CHARPOS. */
8329 if (it->bidi_p)
8330 {
8331 SAVE_IT (ppos_it, *it, ppos_data);
8332 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8333 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8334 SAVE_IT (ppos_it, *it, ppos_data);
8335 }
8336
8337 #define BUFFER_POS_REACHED_P() \
8338 ((op & MOVE_TO_POS) != 0 \
8339 && BUFFERP (it->object) \
8340 && (IT_CHARPOS (*it) == to_charpos \
8341 || ((!it->bidi_p \
8342 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8343 && IT_CHARPOS (*it) > to_charpos) \
8344 || (it->what == IT_COMPOSITION \
8345 && ((IT_CHARPOS (*it) > to_charpos \
8346 && to_charpos >= it->cmp_it.charpos) \
8347 || (IT_CHARPOS (*it) < to_charpos \
8348 && to_charpos <= it->cmp_it.charpos)))) \
8349 && (it->method == GET_FROM_BUFFER \
8350 || (it->method == GET_FROM_DISPLAY_VECTOR \
8351 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8352
8353 /* If there's a line-/wrap-prefix, handle it. */
8354 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8355 && it->current_y < it->last_visible_y)
8356 handle_line_prefix (it);
8357
8358 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8359 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8360
8361 while (1)
8362 {
8363 int x, i, ascent = 0, descent = 0;
8364
8365 /* Utility macro to reset an iterator with x, ascent, and descent. */
8366 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8367 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8368 (IT)->max_descent = descent)
8369
8370 /* Stop if we move beyond TO_CHARPOS (after an image or a
8371 display string or stretch glyph). */
8372 if ((op & MOVE_TO_POS) != 0
8373 && BUFFERP (it->object)
8374 && it->method == GET_FROM_BUFFER
8375 && (((!it->bidi_p
8376 /* When the iterator is at base embedding level, we
8377 are guaranteed that characters are delivered for
8378 display in strictly increasing order of their
8379 buffer positions. */
8380 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8381 && IT_CHARPOS (*it) > to_charpos)
8382 || (it->bidi_p
8383 && (prev_method == GET_FROM_IMAGE
8384 || prev_method == GET_FROM_STRETCH
8385 || prev_method == GET_FROM_STRING)
8386 /* Passed TO_CHARPOS from left to right. */
8387 && ((prev_pos < to_charpos
8388 && IT_CHARPOS (*it) > to_charpos)
8389 /* Passed TO_CHARPOS from right to left. */
8390 || (prev_pos > to_charpos
8391 && IT_CHARPOS (*it) < to_charpos)))))
8392 {
8393 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8394 {
8395 result = MOVE_POS_MATCH_OR_ZV;
8396 break;
8397 }
8398 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8399 /* If wrap_it is valid, the current position might be in a
8400 word that is wrapped. So, save the iterator in
8401 atpos_it and continue to see if wrapping happens. */
8402 SAVE_IT (atpos_it, *it, atpos_data);
8403 }
8404
8405 /* Stop when ZV reached.
8406 We used to stop here when TO_CHARPOS reached as well, but that is
8407 too soon if this glyph does not fit on this line. So we handle it
8408 explicitly below. */
8409 if (!get_next_display_element (it))
8410 {
8411 result = MOVE_POS_MATCH_OR_ZV;
8412 break;
8413 }
8414
8415 if (it->line_wrap == TRUNCATE)
8416 {
8417 if (BUFFER_POS_REACHED_P ())
8418 {
8419 result = MOVE_POS_MATCH_OR_ZV;
8420 break;
8421 }
8422 }
8423 else
8424 {
8425 if (it->line_wrap == WORD_WRAP)
8426 {
8427 if (IT_DISPLAYING_WHITESPACE (it))
8428 may_wrap = 1;
8429 else if (may_wrap)
8430 {
8431 /* We have reached a glyph that follows one or more
8432 whitespace characters. If the position is
8433 already found, we are done. */
8434 if (atpos_it.sp >= 0)
8435 {
8436 RESTORE_IT (it, &atpos_it, atpos_data);
8437 result = MOVE_POS_MATCH_OR_ZV;
8438 goto done;
8439 }
8440 if (atx_it.sp >= 0)
8441 {
8442 RESTORE_IT (it, &atx_it, atx_data);
8443 result = MOVE_X_REACHED;
8444 goto done;
8445 }
8446 /* Otherwise, we can wrap here. */
8447 SAVE_IT (wrap_it, *it, wrap_data);
8448 may_wrap = 0;
8449 }
8450 }
8451 }
8452
8453 /* Remember the line height for the current line, in case
8454 the next element doesn't fit on the line. */
8455 ascent = it->max_ascent;
8456 descent = it->max_descent;
8457
8458 /* The call to produce_glyphs will get the metrics of the
8459 display element IT is loaded with. Record the x-position
8460 before this display element, in case it doesn't fit on the
8461 line. */
8462 x = it->current_x;
8463
8464 PRODUCE_GLYPHS (it);
8465
8466 if (it->area != TEXT_AREA)
8467 {
8468 prev_method = it->method;
8469 if (it->method == GET_FROM_BUFFER)
8470 prev_pos = IT_CHARPOS (*it);
8471 set_iterator_to_next (it, 1);
8472 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8473 SET_TEXT_POS (this_line_min_pos,
8474 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8475 if (it->bidi_p
8476 && (op & MOVE_TO_POS)
8477 && IT_CHARPOS (*it) > to_charpos
8478 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8479 SAVE_IT (ppos_it, *it, ppos_data);
8480 continue;
8481 }
8482
8483 /* The number of glyphs we get back in IT->nglyphs will normally
8484 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8485 character on a terminal frame, or (iii) a line end. For the
8486 second case, IT->nglyphs - 1 padding glyphs will be present.
8487 (On X frames, there is only one glyph produced for a
8488 composite character.)
8489
8490 The behavior implemented below means, for continuation lines,
8491 that as many spaces of a TAB as fit on the current line are
8492 displayed there. For terminal frames, as many glyphs of a
8493 multi-glyph character are displayed in the current line, too.
8494 This is what the old redisplay code did, and we keep it that
8495 way. Under X, the whole shape of a complex character must
8496 fit on the line or it will be completely displayed in the
8497 next line.
8498
8499 Note that both for tabs and padding glyphs, all glyphs have
8500 the same width. */
8501 if (it->nglyphs)
8502 {
8503 /* More than one glyph or glyph doesn't fit on line. All
8504 glyphs have the same width. */
8505 int single_glyph_width = it->pixel_width / it->nglyphs;
8506 int new_x;
8507 int x_before_this_char = x;
8508 int hpos_before_this_char = it->hpos;
8509
8510 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8511 {
8512 new_x = x + single_glyph_width;
8513
8514 /* We want to leave anything reaching TO_X to the caller. */
8515 if ((op & MOVE_TO_X) && new_x > to_x)
8516 {
8517 if (BUFFER_POS_REACHED_P ())
8518 {
8519 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8520 goto buffer_pos_reached;
8521 if (atpos_it.sp < 0)
8522 {
8523 SAVE_IT (atpos_it, *it, atpos_data);
8524 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8525 }
8526 }
8527 else
8528 {
8529 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8530 {
8531 it->current_x = x;
8532 result = MOVE_X_REACHED;
8533 break;
8534 }
8535 if (atx_it.sp < 0)
8536 {
8537 SAVE_IT (atx_it, *it, atx_data);
8538 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8539 }
8540 }
8541 }
8542
8543 if (/* Lines are continued. */
8544 it->line_wrap != TRUNCATE
8545 && (/* And glyph doesn't fit on the line. */
8546 new_x > it->last_visible_x
8547 /* Or it fits exactly and we're on a window
8548 system frame. */
8549 || (new_x == it->last_visible_x
8550 && FRAME_WINDOW_P (it->f)
8551 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8552 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8553 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8554 {
8555 if (/* IT->hpos == 0 means the very first glyph
8556 doesn't fit on the line, e.g. a wide image. */
8557 it->hpos == 0
8558 || (new_x == it->last_visible_x
8559 && FRAME_WINDOW_P (it->f)))
8560 {
8561 ++it->hpos;
8562 it->current_x = new_x;
8563
8564 /* The character's last glyph just barely fits
8565 in this row. */
8566 if (i == it->nglyphs - 1)
8567 {
8568 /* If this is the destination position,
8569 return a position *before* it in this row,
8570 now that we know it fits in this row. */
8571 if (BUFFER_POS_REACHED_P ())
8572 {
8573 if (it->line_wrap != WORD_WRAP
8574 || wrap_it.sp < 0)
8575 {
8576 it->hpos = hpos_before_this_char;
8577 it->current_x = x_before_this_char;
8578 result = MOVE_POS_MATCH_OR_ZV;
8579 break;
8580 }
8581 if (it->line_wrap == WORD_WRAP
8582 && atpos_it.sp < 0)
8583 {
8584 SAVE_IT (atpos_it, *it, atpos_data);
8585 atpos_it.current_x = x_before_this_char;
8586 atpos_it.hpos = hpos_before_this_char;
8587 }
8588 }
8589
8590 prev_method = it->method;
8591 if (it->method == GET_FROM_BUFFER)
8592 prev_pos = IT_CHARPOS (*it);
8593 set_iterator_to_next (it, 1);
8594 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8595 SET_TEXT_POS (this_line_min_pos,
8596 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8597 /* On graphical terminals, newlines may
8598 "overflow" into the fringe if
8599 overflow-newline-into-fringe is non-nil.
8600 On text terminals, and on graphical
8601 terminals with no right margin, newlines
8602 may overflow into the last glyph on the
8603 display line.*/
8604 if (!FRAME_WINDOW_P (it->f)
8605 || ((it->bidi_p
8606 && it->bidi_it.paragraph_dir == R2L)
8607 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8608 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8609 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8610 {
8611 if (!get_next_display_element (it))
8612 {
8613 result = MOVE_POS_MATCH_OR_ZV;
8614 break;
8615 }
8616 if (BUFFER_POS_REACHED_P ())
8617 {
8618 if (ITERATOR_AT_END_OF_LINE_P (it))
8619 result = MOVE_POS_MATCH_OR_ZV;
8620 else
8621 result = MOVE_LINE_CONTINUED;
8622 break;
8623 }
8624 if (ITERATOR_AT_END_OF_LINE_P (it)
8625 && (it->line_wrap != WORD_WRAP
8626 || wrap_it.sp < 0))
8627 {
8628 result = MOVE_NEWLINE_OR_CR;
8629 break;
8630 }
8631 }
8632 }
8633 }
8634 else
8635 IT_RESET_X_ASCENT_DESCENT (it);
8636
8637 if (wrap_it.sp >= 0)
8638 {
8639 RESTORE_IT (it, &wrap_it, wrap_data);
8640 atpos_it.sp = -1;
8641 atx_it.sp = -1;
8642 }
8643
8644 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8645 IT_CHARPOS (*it)));
8646 result = MOVE_LINE_CONTINUED;
8647 break;
8648 }
8649
8650 if (BUFFER_POS_REACHED_P ())
8651 {
8652 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8653 goto buffer_pos_reached;
8654 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8655 {
8656 SAVE_IT (atpos_it, *it, atpos_data);
8657 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8658 }
8659 }
8660
8661 if (new_x > it->first_visible_x)
8662 {
8663 /* Glyph is visible. Increment number of glyphs that
8664 would be displayed. */
8665 ++it->hpos;
8666 }
8667 }
8668
8669 if (result != MOVE_UNDEFINED)
8670 break;
8671 }
8672 else if (BUFFER_POS_REACHED_P ())
8673 {
8674 buffer_pos_reached:
8675 IT_RESET_X_ASCENT_DESCENT (it);
8676 result = MOVE_POS_MATCH_OR_ZV;
8677 break;
8678 }
8679 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8680 {
8681 /* Stop when TO_X specified and reached. This check is
8682 necessary here because of lines consisting of a line end,
8683 only. The line end will not produce any glyphs and we
8684 would never get MOVE_X_REACHED. */
8685 eassert (it->nglyphs == 0);
8686 result = MOVE_X_REACHED;
8687 break;
8688 }
8689
8690 /* Is this a line end? If yes, we're done. */
8691 if (ITERATOR_AT_END_OF_LINE_P (it))
8692 {
8693 /* If we are past TO_CHARPOS, but never saw any character
8694 positions smaller than TO_CHARPOS, return
8695 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8696 did. */
8697 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8698 {
8699 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8700 {
8701 if (IT_CHARPOS (ppos_it) < ZV)
8702 {
8703 RESTORE_IT (it, &ppos_it, ppos_data);
8704 result = MOVE_POS_MATCH_OR_ZV;
8705 }
8706 else
8707 goto buffer_pos_reached;
8708 }
8709 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8710 && IT_CHARPOS (*it) > to_charpos)
8711 goto buffer_pos_reached;
8712 else
8713 result = MOVE_NEWLINE_OR_CR;
8714 }
8715 else
8716 result = MOVE_NEWLINE_OR_CR;
8717 break;
8718 }
8719
8720 prev_method = it->method;
8721 if (it->method == GET_FROM_BUFFER)
8722 prev_pos = IT_CHARPOS (*it);
8723 /* The current display element has been consumed. Advance
8724 to the next. */
8725 set_iterator_to_next (it, 1);
8726 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8727 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8728 if (IT_CHARPOS (*it) < to_charpos)
8729 saw_smaller_pos = 1;
8730 if (it->bidi_p
8731 && (op & MOVE_TO_POS)
8732 && IT_CHARPOS (*it) >= to_charpos
8733 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8734 SAVE_IT (ppos_it, *it, ppos_data);
8735
8736 /* Stop if lines are truncated and IT's current x-position is
8737 past the right edge of the window now. */
8738 if (it->line_wrap == TRUNCATE
8739 && it->current_x >= it->last_visible_x)
8740 {
8741 if (!FRAME_WINDOW_P (it->f)
8742 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8743 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8744 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8745 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8746 {
8747 int at_eob_p = 0;
8748
8749 if ((at_eob_p = !get_next_display_element (it))
8750 || BUFFER_POS_REACHED_P ()
8751 /* If we are past TO_CHARPOS, but never saw any
8752 character positions smaller than TO_CHARPOS,
8753 return MOVE_POS_MATCH_OR_ZV, like the
8754 unidirectional display did. */
8755 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8756 && !saw_smaller_pos
8757 && IT_CHARPOS (*it) > to_charpos))
8758 {
8759 if (it->bidi_p
8760 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8761 RESTORE_IT (it, &ppos_it, ppos_data);
8762 result = MOVE_POS_MATCH_OR_ZV;
8763 break;
8764 }
8765 if (ITERATOR_AT_END_OF_LINE_P (it))
8766 {
8767 result = MOVE_NEWLINE_OR_CR;
8768 break;
8769 }
8770 }
8771 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8772 && !saw_smaller_pos
8773 && IT_CHARPOS (*it) > to_charpos)
8774 {
8775 if (IT_CHARPOS (ppos_it) < ZV)
8776 RESTORE_IT (it, &ppos_it, ppos_data);
8777 result = MOVE_POS_MATCH_OR_ZV;
8778 break;
8779 }
8780 result = MOVE_LINE_TRUNCATED;
8781 break;
8782 }
8783 #undef IT_RESET_X_ASCENT_DESCENT
8784 }
8785
8786 #undef BUFFER_POS_REACHED_P
8787
8788 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8789 restore the saved iterator. */
8790 if (atpos_it.sp >= 0)
8791 RESTORE_IT (it, &atpos_it, atpos_data);
8792 else if (atx_it.sp >= 0)
8793 RESTORE_IT (it, &atx_it, atx_data);
8794
8795 done:
8796
8797 if (atpos_data)
8798 bidi_unshelve_cache (atpos_data, 1);
8799 if (atx_data)
8800 bidi_unshelve_cache (atx_data, 1);
8801 if (wrap_data)
8802 bidi_unshelve_cache (wrap_data, 1);
8803 if (ppos_data)
8804 bidi_unshelve_cache (ppos_data, 1);
8805
8806 /* Restore the iterator settings altered at the beginning of this
8807 function. */
8808 it->glyph_row = saved_glyph_row;
8809 return result;
8810 }
8811
8812 /* For external use. */
8813 void
8814 move_it_in_display_line (struct it *it,
8815 ptrdiff_t to_charpos, int to_x,
8816 enum move_operation_enum op)
8817 {
8818 if (it->line_wrap == WORD_WRAP
8819 && (op & MOVE_TO_X))
8820 {
8821 struct it save_it;
8822 void *save_data = NULL;
8823 int skip;
8824
8825 SAVE_IT (save_it, *it, save_data);
8826 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8827 /* When word-wrap is on, TO_X may lie past the end
8828 of a wrapped line. Then it->current is the
8829 character on the next line, so backtrack to the
8830 space before the wrap point. */
8831 if (skip == MOVE_LINE_CONTINUED)
8832 {
8833 int prev_x = max (it->current_x - 1, 0);
8834 RESTORE_IT (it, &save_it, save_data);
8835 move_it_in_display_line_to
8836 (it, -1, prev_x, MOVE_TO_X);
8837 }
8838 else
8839 bidi_unshelve_cache (save_data, 1);
8840 }
8841 else
8842 move_it_in_display_line_to (it, to_charpos, to_x, op);
8843 }
8844
8845
8846 /* Move IT forward until it satisfies one or more of the criteria in
8847 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8848
8849 OP is a bit-mask that specifies where to stop, and in particular,
8850 which of those four position arguments makes a difference. See the
8851 description of enum move_operation_enum.
8852
8853 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8854 screen line, this function will set IT to the next position that is
8855 displayed to the right of TO_CHARPOS on the screen. */
8856
8857 void
8858 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8859 {
8860 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8861 int line_height, line_start_x = 0, reached = 0;
8862 void *backup_data = NULL;
8863
8864 for (;;)
8865 {
8866 if (op & MOVE_TO_VPOS)
8867 {
8868 /* If no TO_CHARPOS and no TO_X specified, stop at the
8869 start of the line TO_VPOS. */
8870 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8871 {
8872 if (it->vpos == to_vpos)
8873 {
8874 reached = 1;
8875 break;
8876 }
8877 else
8878 skip = move_it_in_display_line_to (it, -1, -1, 0);
8879 }
8880 else
8881 {
8882 /* TO_VPOS >= 0 means stop at TO_X in the line at
8883 TO_VPOS, or at TO_POS, whichever comes first. */
8884 if (it->vpos == to_vpos)
8885 {
8886 reached = 2;
8887 break;
8888 }
8889
8890 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8891
8892 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8893 {
8894 reached = 3;
8895 break;
8896 }
8897 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8898 {
8899 /* We have reached TO_X but not in the line we want. */
8900 skip = move_it_in_display_line_to (it, to_charpos,
8901 -1, MOVE_TO_POS);
8902 if (skip == MOVE_POS_MATCH_OR_ZV)
8903 {
8904 reached = 4;
8905 break;
8906 }
8907 }
8908 }
8909 }
8910 else if (op & MOVE_TO_Y)
8911 {
8912 struct it it_backup;
8913
8914 if (it->line_wrap == WORD_WRAP)
8915 SAVE_IT (it_backup, *it, backup_data);
8916
8917 /* TO_Y specified means stop at TO_X in the line containing
8918 TO_Y---or at TO_CHARPOS if this is reached first. The
8919 problem is that we can't really tell whether the line
8920 contains TO_Y before we have completely scanned it, and
8921 this may skip past TO_X. What we do is to first scan to
8922 TO_X.
8923
8924 If TO_X is not specified, use a TO_X of zero. The reason
8925 is to make the outcome of this function more predictable.
8926 If we didn't use TO_X == 0, we would stop at the end of
8927 the line which is probably not what a caller would expect
8928 to happen. */
8929 skip = move_it_in_display_line_to
8930 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8931 (MOVE_TO_X | (op & MOVE_TO_POS)));
8932
8933 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8934 if (skip == MOVE_POS_MATCH_OR_ZV)
8935 reached = 5;
8936 else if (skip == MOVE_X_REACHED)
8937 {
8938 /* If TO_X was reached, we want to know whether TO_Y is
8939 in the line. We know this is the case if the already
8940 scanned glyphs make the line tall enough. Otherwise,
8941 we must check by scanning the rest of the line. */
8942 line_height = it->max_ascent + it->max_descent;
8943 if (to_y >= it->current_y
8944 && to_y < it->current_y + line_height)
8945 {
8946 reached = 6;
8947 break;
8948 }
8949 SAVE_IT (it_backup, *it, backup_data);
8950 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8951 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8952 op & MOVE_TO_POS);
8953 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8954 line_height = it->max_ascent + it->max_descent;
8955 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8956
8957 if (to_y >= it->current_y
8958 && to_y < it->current_y + line_height)
8959 {
8960 /* If TO_Y is in this line and TO_X was reached
8961 above, we scanned too far. We have to restore
8962 IT's settings to the ones before skipping. But
8963 keep the more accurate values of max_ascent and
8964 max_descent we've found while skipping the rest
8965 of the line, for the sake of callers, such as
8966 pos_visible_p, that need to know the line
8967 height. */
8968 int max_ascent = it->max_ascent;
8969 int max_descent = it->max_descent;
8970
8971 RESTORE_IT (it, &it_backup, backup_data);
8972 it->max_ascent = max_ascent;
8973 it->max_descent = max_descent;
8974 reached = 6;
8975 }
8976 else
8977 {
8978 skip = skip2;
8979 if (skip == MOVE_POS_MATCH_OR_ZV)
8980 reached = 7;
8981 }
8982 }
8983 else
8984 {
8985 /* Check whether TO_Y is in this line. */
8986 line_height = it->max_ascent + it->max_descent;
8987 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8988
8989 if (to_y >= it->current_y
8990 && to_y < it->current_y + line_height)
8991 {
8992 /* When word-wrap is on, TO_X may lie past the end
8993 of a wrapped line. Then it->current is the
8994 character on the next line, so backtrack to the
8995 space before the wrap point. */
8996 if (skip == MOVE_LINE_CONTINUED
8997 && it->line_wrap == WORD_WRAP)
8998 {
8999 int prev_x = max (it->current_x - 1, 0);
9000 RESTORE_IT (it, &it_backup, backup_data);
9001 skip = move_it_in_display_line_to
9002 (it, -1, prev_x, MOVE_TO_X);
9003 }
9004 reached = 6;
9005 }
9006 }
9007
9008 if (reached)
9009 break;
9010 }
9011 else if (BUFFERP (it->object)
9012 && (it->method == GET_FROM_BUFFER
9013 || it->method == GET_FROM_STRETCH)
9014 && IT_CHARPOS (*it) >= to_charpos
9015 /* Under bidi iteration, a call to set_iterator_to_next
9016 can scan far beyond to_charpos if the initial
9017 portion of the next line needs to be reordered. In
9018 that case, give move_it_in_display_line_to another
9019 chance below. */
9020 && !(it->bidi_p
9021 && it->bidi_it.scan_dir == -1))
9022 skip = MOVE_POS_MATCH_OR_ZV;
9023 else
9024 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9025
9026 switch (skip)
9027 {
9028 case MOVE_POS_MATCH_OR_ZV:
9029 reached = 8;
9030 goto out;
9031
9032 case MOVE_NEWLINE_OR_CR:
9033 set_iterator_to_next (it, 1);
9034 it->continuation_lines_width = 0;
9035 break;
9036
9037 case MOVE_LINE_TRUNCATED:
9038 it->continuation_lines_width = 0;
9039 reseat_at_next_visible_line_start (it, 0);
9040 if ((op & MOVE_TO_POS) != 0
9041 && IT_CHARPOS (*it) > to_charpos)
9042 {
9043 reached = 9;
9044 goto out;
9045 }
9046 break;
9047
9048 case MOVE_LINE_CONTINUED:
9049 /* For continued lines ending in a tab, some of the glyphs
9050 associated with the tab are displayed on the current
9051 line. Since it->current_x does not include these glyphs,
9052 we use it->last_visible_x instead. */
9053 if (it->c == '\t')
9054 {
9055 it->continuation_lines_width += it->last_visible_x;
9056 /* When moving by vpos, ensure that the iterator really
9057 advances to the next line (bug#847, bug#969). Fixme:
9058 do we need to do this in other circumstances? */
9059 if (it->current_x != it->last_visible_x
9060 && (op & MOVE_TO_VPOS)
9061 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9062 {
9063 line_start_x = it->current_x + it->pixel_width
9064 - it->last_visible_x;
9065 set_iterator_to_next (it, 0);
9066 }
9067 }
9068 else
9069 it->continuation_lines_width += it->current_x;
9070 break;
9071
9072 default:
9073 emacs_abort ();
9074 }
9075
9076 /* Reset/increment for the next run. */
9077 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9078 it->current_x = line_start_x;
9079 line_start_x = 0;
9080 it->hpos = 0;
9081 it->current_y += it->max_ascent + it->max_descent;
9082 ++it->vpos;
9083 last_height = it->max_ascent + it->max_descent;
9084 it->max_ascent = it->max_descent = 0;
9085 }
9086
9087 out:
9088
9089 /* On text terminals, we may stop at the end of a line in the middle
9090 of a multi-character glyph. If the glyph itself is continued,
9091 i.e. it is actually displayed on the next line, don't treat this
9092 stopping point as valid; move to the next line instead (unless
9093 that brings us offscreen). */
9094 if (!FRAME_WINDOW_P (it->f)
9095 && op & MOVE_TO_POS
9096 && IT_CHARPOS (*it) == to_charpos
9097 && it->what == IT_CHARACTER
9098 && it->nglyphs > 1
9099 && it->line_wrap == WINDOW_WRAP
9100 && it->current_x == it->last_visible_x - 1
9101 && it->c != '\n'
9102 && it->c != '\t'
9103 && it->vpos < it->w->window_end_vpos)
9104 {
9105 it->continuation_lines_width += it->current_x;
9106 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9107 it->current_y += it->max_ascent + it->max_descent;
9108 ++it->vpos;
9109 last_height = it->max_ascent + it->max_descent;
9110 }
9111
9112 if (backup_data)
9113 bidi_unshelve_cache (backup_data, 1);
9114
9115 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9116 }
9117
9118
9119 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9120
9121 If DY > 0, move IT backward at least that many pixels. DY = 0
9122 means move IT backward to the preceding line start or BEGV. This
9123 function may move over more than DY pixels if IT->current_y - DY
9124 ends up in the middle of a line; in this case IT->current_y will be
9125 set to the top of the line moved to. */
9126
9127 void
9128 move_it_vertically_backward (struct it *it, int dy)
9129 {
9130 int nlines, h;
9131 struct it it2, it3;
9132 void *it2data = NULL, *it3data = NULL;
9133 ptrdiff_t start_pos;
9134 int nchars_per_row
9135 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9136 ptrdiff_t pos_limit;
9137
9138 move_further_back:
9139 eassert (dy >= 0);
9140
9141 start_pos = IT_CHARPOS (*it);
9142
9143 /* Estimate how many newlines we must move back. */
9144 nlines = max (1, dy / default_line_pixel_height (it->w));
9145 if (it->line_wrap == TRUNCATE)
9146 pos_limit = BEGV;
9147 else
9148 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9149
9150 /* Set the iterator's position that many lines back. But don't go
9151 back more than NLINES full screen lines -- this wins a day with
9152 buffers which have very long lines. */
9153 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9154 back_to_previous_visible_line_start (it);
9155
9156 /* Reseat the iterator here. When moving backward, we don't want
9157 reseat to skip forward over invisible text, set up the iterator
9158 to deliver from overlay strings at the new position etc. So,
9159 use reseat_1 here. */
9160 reseat_1 (it, it->current.pos, 1);
9161
9162 /* We are now surely at a line start. */
9163 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9164 reordering is in effect. */
9165 it->continuation_lines_width = 0;
9166
9167 /* Move forward and see what y-distance we moved. First move to the
9168 start of the next line so that we get its height. We need this
9169 height to be able to tell whether we reached the specified
9170 y-distance. */
9171 SAVE_IT (it2, *it, it2data);
9172 it2.max_ascent = it2.max_descent = 0;
9173 do
9174 {
9175 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9176 MOVE_TO_POS | MOVE_TO_VPOS);
9177 }
9178 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9179 /* If we are in a display string which starts at START_POS,
9180 and that display string includes a newline, and we are
9181 right after that newline (i.e. at the beginning of a
9182 display line), exit the loop, because otherwise we will
9183 infloop, since move_it_to will see that it is already at
9184 START_POS and will not move. */
9185 || (it2.method == GET_FROM_STRING
9186 && IT_CHARPOS (it2) == start_pos
9187 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9188 eassert (IT_CHARPOS (*it) >= BEGV);
9189 SAVE_IT (it3, it2, it3data);
9190
9191 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9192 eassert (IT_CHARPOS (*it) >= BEGV);
9193 /* H is the actual vertical distance from the position in *IT
9194 and the starting position. */
9195 h = it2.current_y - it->current_y;
9196 /* NLINES is the distance in number of lines. */
9197 nlines = it2.vpos - it->vpos;
9198
9199 /* Correct IT's y and vpos position
9200 so that they are relative to the starting point. */
9201 it->vpos -= nlines;
9202 it->current_y -= h;
9203
9204 if (dy == 0)
9205 {
9206 /* DY == 0 means move to the start of the screen line. The
9207 value of nlines is > 0 if continuation lines were involved,
9208 or if the original IT position was at start of a line. */
9209 RESTORE_IT (it, it, it2data);
9210 if (nlines > 0)
9211 move_it_by_lines (it, nlines);
9212 /* The above code moves us to some position NLINES down,
9213 usually to its first glyph (leftmost in an L2R line), but
9214 that's not necessarily the start of the line, under bidi
9215 reordering. We want to get to the character position
9216 that is immediately after the newline of the previous
9217 line. */
9218 if (it->bidi_p
9219 && !it->continuation_lines_width
9220 && !STRINGP (it->string)
9221 && IT_CHARPOS (*it) > BEGV
9222 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9223 {
9224 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9225
9226 DEC_BOTH (cp, bp);
9227 cp = find_newline_no_quit (cp, bp, -1, NULL);
9228 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9229 }
9230 bidi_unshelve_cache (it3data, 1);
9231 }
9232 else
9233 {
9234 /* The y-position we try to reach, relative to *IT.
9235 Note that H has been subtracted in front of the if-statement. */
9236 int target_y = it->current_y + h - dy;
9237 int y0 = it3.current_y;
9238 int y1;
9239 int line_height;
9240
9241 RESTORE_IT (&it3, &it3, it3data);
9242 y1 = line_bottom_y (&it3);
9243 line_height = y1 - y0;
9244 RESTORE_IT (it, it, it2data);
9245 /* If we did not reach target_y, try to move further backward if
9246 we can. If we moved too far backward, try to move forward. */
9247 if (target_y < it->current_y
9248 /* This is heuristic. In a window that's 3 lines high, with
9249 a line height of 13 pixels each, recentering with point
9250 on the bottom line will try to move -39/2 = 19 pixels
9251 backward. Try to avoid moving into the first line. */
9252 && (it->current_y - target_y
9253 > min (window_box_height (it->w), line_height * 2 / 3))
9254 && IT_CHARPOS (*it) > BEGV)
9255 {
9256 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9257 target_y - it->current_y));
9258 dy = it->current_y - target_y;
9259 goto move_further_back;
9260 }
9261 else if (target_y >= it->current_y + line_height
9262 && IT_CHARPOS (*it) < ZV)
9263 {
9264 /* Should move forward by at least one line, maybe more.
9265
9266 Note: Calling move_it_by_lines can be expensive on
9267 terminal frames, where compute_motion is used (via
9268 vmotion) to do the job, when there are very long lines
9269 and truncate-lines is nil. That's the reason for
9270 treating terminal frames specially here. */
9271
9272 if (!FRAME_WINDOW_P (it->f))
9273 move_it_vertically (it, target_y - (it->current_y + line_height));
9274 else
9275 {
9276 do
9277 {
9278 move_it_by_lines (it, 1);
9279 }
9280 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9281 }
9282 }
9283 }
9284 }
9285
9286
9287 /* Move IT by a specified amount of pixel lines DY. DY negative means
9288 move backwards. DY = 0 means move to start of screen line. At the
9289 end, IT will be on the start of a screen line. */
9290
9291 void
9292 move_it_vertically (struct it *it, int dy)
9293 {
9294 if (dy <= 0)
9295 move_it_vertically_backward (it, -dy);
9296 else
9297 {
9298 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9299 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9300 MOVE_TO_POS | MOVE_TO_Y);
9301 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9302
9303 /* If buffer ends in ZV without a newline, move to the start of
9304 the line to satisfy the post-condition. */
9305 if (IT_CHARPOS (*it) == ZV
9306 && ZV > BEGV
9307 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9308 move_it_by_lines (it, 0);
9309 }
9310 }
9311
9312
9313 /* Move iterator IT past the end of the text line it is in. */
9314
9315 void
9316 move_it_past_eol (struct it *it)
9317 {
9318 enum move_it_result rc;
9319
9320 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9321 if (rc == MOVE_NEWLINE_OR_CR)
9322 set_iterator_to_next (it, 0);
9323 }
9324
9325
9326 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9327 negative means move up. DVPOS == 0 means move to the start of the
9328 screen line.
9329
9330 Optimization idea: If we would know that IT->f doesn't use
9331 a face with proportional font, we could be faster for
9332 truncate-lines nil. */
9333
9334 void
9335 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9336 {
9337
9338 /* The commented-out optimization uses vmotion on terminals. This
9339 gives bad results, because elements like it->what, on which
9340 callers such as pos_visible_p rely, aren't updated. */
9341 /* struct position pos;
9342 if (!FRAME_WINDOW_P (it->f))
9343 {
9344 struct text_pos textpos;
9345
9346 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9347 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9348 reseat (it, textpos, 1);
9349 it->vpos += pos.vpos;
9350 it->current_y += pos.vpos;
9351 }
9352 else */
9353
9354 if (dvpos == 0)
9355 {
9356 /* DVPOS == 0 means move to the start of the screen line. */
9357 move_it_vertically_backward (it, 0);
9358 /* Let next call to line_bottom_y calculate real line height */
9359 last_height = 0;
9360 }
9361 else if (dvpos > 0)
9362 {
9363 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9364 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9365 {
9366 /* Only move to the next buffer position if we ended up in a
9367 string from display property, not in an overlay string
9368 (before-string or after-string). That is because the
9369 latter don't conceal the underlying buffer position, so
9370 we can ask to move the iterator to the exact position we
9371 are interested in. Note that, even if we are already at
9372 IT_CHARPOS (*it), the call below is not a no-op, as it
9373 will detect that we are at the end of the string, pop the
9374 iterator, and compute it->current_x and it->hpos
9375 correctly. */
9376 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9377 -1, -1, -1, MOVE_TO_POS);
9378 }
9379 }
9380 else
9381 {
9382 struct it it2;
9383 void *it2data = NULL;
9384 ptrdiff_t start_charpos, i;
9385 int nchars_per_row
9386 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9387 ptrdiff_t pos_limit;
9388
9389 /* Start at the beginning of the screen line containing IT's
9390 position. This may actually move vertically backwards,
9391 in case of overlays, so adjust dvpos accordingly. */
9392 dvpos += it->vpos;
9393 move_it_vertically_backward (it, 0);
9394 dvpos -= it->vpos;
9395
9396 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9397 screen lines, and reseat the iterator there. */
9398 start_charpos = IT_CHARPOS (*it);
9399 if (it->line_wrap == TRUNCATE)
9400 pos_limit = BEGV;
9401 else
9402 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9403 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9404 back_to_previous_visible_line_start (it);
9405 reseat (it, it->current.pos, 1);
9406
9407 /* Move further back if we end up in a string or an image. */
9408 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9409 {
9410 /* First try to move to start of display line. */
9411 dvpos += it->vpos;
9412 move_it_vertically_backward (it, 0);
9413 dvpos -= it->vpos;
9414 if (IT_POS_VALID_AFTER_MOVE_P (it))
9415 break;
9416 /* If start of line is still in string or image,
9417 move further back. */
9418 back_to_previous_visible_line_start (it);
9419 reseat (it, it->current.pos, 1);
9420 dvpos--;
9421 }
9422
9423 it->current_x = it->hpos = 0;
9424
9425 /* Above call may have moved too far if continuation lines
9426 are involved. Scan forward and see if it did. */
9427 SAVE_IT (it2, *it, it2data);
9428 it2.vpos = it2.current_y = 0;
9429 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9430 it->vpos -= it2.vpos;
9431 it->current_y -= it2.current_y;
9432 it->current_x = it->hpos = 0;
9433
9434 /* If we moved too far back, move IT some lines forward. */
9435 if (it2.vpos > -dvpos)
9436 {
9437 int delta = it2.vpos + dvpos;
9438
9439 RESTORE_IT (&it2, &it2, it2data);
9440 SAVE_IT (it2, *it, it2data);
9441 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9442 /* Move back again if we got too far ahead. */
9443 if (IT_CHARPOS (*it) >= start_charpos)
9444 RESTORE_IT (it, &it2, it2data);
9445 else
9446 bidi_unshelve_cache (it2data, 1);
9447 }
9448 else
9449 RESTORE_IT (it, it, it2data);
9450 }
9451 }
9452
9453 /* Return 1 if IT points into the middle of a display vector. */
9454
9455 int
9456 in_display_vector_p (struct it *it)
9457 {
9458 return (it->method == GET_FROM_DISPLAY_VECTOR
9459 && it->current.dpvec_index > 0
9460 && it->dpvec + it->current.dpvec_index != it->dpend);
9461 }
9462
9463 \f
9464 /***********************************************************************
9465 Messages
9466 ***********************************************************************/
9467
9468
9469 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9470 to *Messages*. */
9471
9472 void
9473 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9474 {
9475 Lisp_Object args[3];
9476 Lisp_Object msg, fmt;
9477 char *buffer;
9478 ptrdiff_t len;
9479 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9480 USE_SAFE_ALLOCA;
9481
9482 fmt = msg = Qnil;
9483 GCPRO4 (fmt, msg, arg1, arg2);
9484
9485 args[0] = fmt = build_string (format);
9486 args[1] = arg1;
9487 args[2] = arg2;
9488 msg = Fformat (3, args);
9489
9490 len = SBYTES (msg) + 1;
9491 buffer = SAFE_ALLOCA (len);
9492 memcpy (buffer, SDATA (msg), len);
9493
9494 message_dolog (buffer, len - 1, 1, 0);
9495 SAFE_FREE ();
9496
9497 UNGCPRO;
9498 }
9499
9500
9501 /* Output a newline in the *Messages* buffer if "needs" one. */
9502
9503 void
9504 message_log_maybe_newline (void)
9505 {
9506 if (message_log_need_newline)
9507 message_dolog ("", 0, 1, 0);
9508 }
9509
9510
9511 /* Add a string M of length NBYTES to the message log, optionally
9512 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9513 true, means interpret the contents of M as multibyte. This
9514 function calls low-level routines in order to bypass text property
9515 hooks, etc. which might not be safe to run.
9516
9517 This may GC (insert may run before/after change hooks),
9518 so the buffer M must NOT point to a Lisp string. */
9519
9520 void
9521 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9522 {
9523 const unsigned char *msg = (const unsigned char *) m;
9524
9525 if (!NILP (Vmemory_full))
9526 return;
9527
9528 if (!NILP (Vmessage_log_max))
9529 {
9530 struct buffer *oldbuf;
9531 Lisp_Object oldpoint, oldbegv, oldzv;
9532 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9533 ptrdiff_t point_at_end = 0;
9534 ptrdiff_t zv_at_end = 0;
9535 Lisp_Object old_deactivate_mark;
9536 bool shown;
9537 struct gcpro gcpro1;
9538
9539 old_deactivate_mark = Vdeactivate_mark;
9540 oldbuf = current_buffer;
9541
9542 /* Ensure the Messages buffer exists, and switch to it.
9543 If we created it, set the major-mode. */
9544 {
9545 int newbuffer = 0;
9546 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9547
9548 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9549
9550 if (newbuffer &&
9551 !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9552 call0 (intern ("messages-buffer-mode"));
9553 }
9554
9555 bset_undo_list (current_buffer, Qt);
9556
9557 oldpoint = message_dolog_marker1;
9558 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9559 oldbegv = message_dolog_marker2;
9560 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9561 oldzv = message_dolog_marker3;
9562 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9563 GCPRO1 (old_deactivate_mark);
9564
9565 if (PT == Z)
9566 point_at_end = 1;
9567 if (ZV == Z)
9568 zv_at_end = 1;
9569
9570 BEGV = BEG;
9571 BEGV_BYTE = BEG_BYTE;
9572 ZV = Z;
9573 ZV_BYTE = Z_BYTE;
9574 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9575
9576 /* Insert the string--maybe converting multibyte to single byte
9577 or vice versa, so that all the text fits the buffer. */
9578 if (multibyte
9579 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9580 {
9581 ptrdiff_t i;
9582 int c, char_bytes;
9583 char work[1];
9584
9585 /* Convert a multibyte string to single-byte
9586 for the *Message* buffer. */
9587 for (i = 0; i < nbytes; i += char_bytes)
9588 {
9589 c = string_char_and_length (msg + i, &char_bytes);
9590 work[0] = (ASCII_CHAR_P (c)
9591 ? c
9592 : multibyte_char_to_unibyte (c));
9593 insert_1_both (work, 1, 1, 1, 0, 0);
9594 }
9595 }
9596 else if (! multibyte
9597 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9598 {
9599 ptrdiff_t i;
9600 int c, char_bytes;
9601 unsigned char str[MAX_MULTIBYTE_LENGTH];
9602 /* Convert a single-byte string to multibyte
9603 for the *Message* buffer. */
9604 for (i = 0; i < nbytes; i++)
9605 {
9606 c = msg[i];
9607 MAKE_CHAR_MULTIBYTE (c);
9608 char_bytes = CHAR_STRING (c, str);
9609 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9610 }
9611 }
9612 else if (nbytes)
9613 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9614
9615 if (nlflag)
9616 {
9617 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9618 printmax_t dups;
9619
9620 insert_1_both ("\n", 1, 1, 1, 0, 0);
9621
9622 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9623 this_bol = PT;
9624 this_bol_byte = PT_BYTE;
9625
9626 /* See if this line duplicates the previous one.
9627 If so, combine duplicates. */
9628 if (this_bol > BEG)
9629 {
9630 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9631 prev_bol = PT;
9632 prev_bol_byte = PT_BYTE;
9633
9634 dups = message_log_check_duplicate (prev_bol_byte,
9635 this_bol_byte);
9636 if (dups)
9637 {
9638 del_range_both (prev_bol, prev_bol_byte,
9639 this_bol, this_bol_byte, 0);
9640 if (dups > 1)
9641 {
9642 char dupstr[sizeof " [ times]"
9643 + INT_STRLEN_BOUND (printmax_t)];
9644
9645 /* If you change this format, don't forget to also
9646 change message_log_check_duplicate. */
9647 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9648 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9649 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9650 }
9651 }
9652 }
9653
9654 /* If we have more than the desired maximum number of lines
9655 in the *Messages* buffer now, delete the oldest ones.
9656 This is safe because we don't have undo in this buffer. */
9657
9658 if (NATNUMP (Vmessage_log_max))
9659 {
9660 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9661 -XFASTINT (Vmessage_log_max) - 1, 0);
9662 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9663 }
9664 }
9665 BEGV = marker_position (oldbegv);
9666 BEGV_BYTE = marker_byte_position (oldbegv);
9667
9668 if (zv_at_end)
9669 {
9670 ZV = Z;
9671 ZV_BYTE = Z_BYTE;
9672 }
9673 else
9674 {
9675 ZV = marker_position (oldzv);
9676 ZV_BYTE = marker_byte_position (oldzv);
9677 }
9678
9679 if (point_at_end)
9680 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9681 else
9682 /* We can't do Fgoto_char (oldpoint) because it will run some
9683 Lisp code. */
9684 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9685 marker_byte_position (oldpoint));
9686
9687 UNGCPRO;
9688 unchain_marker (XMARKER (oldpoint));
9689 unchain_marker (XMARKER (oldbegv));
9690 unchain_marker (XMARKER (oldzv));
9691
9692 shown = buffer_window_count (current_buffer) > 0;
9693 set_buffer_internal (oldbuf);
9694 /* We called insert_1_both above with its 5th argument (PREPARE)
9695 zero, which prevents insert_1_both from calling
9696 prepare_to_modify_buffer, which in turns prevents us from
9697 incrementing windows_or_buffers_changed even if *Messages* is
9698 shown in some window. So we must manually incrementing
9699 windows_or_buffers_changed here to make up for that. */
9700 if (shown)
9701 windows_or_buffers_changed++;
9702 else
9703 windows_or_buffers_changed = old_windows_or_buffers_changed;
9704 message_log_need_newline = !nlflag;
9705 Vdeactivate_mark = old_deactivate_mark;
9706 }
9707 }
9708
9709
9710 /* We are at the end of the buffer after just having inserted a newline.
9711 (Note: We depend on the fact we won't be crossing the gap.)
9712 Check to see if the most recent message looks a lot like the previous one.
9713 Return 0 if different, 1 if the new one should just replace it, or a
9714 value N > 1 if we should also append " [N times]". */
9715
9716 static intmax_t
9717 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9718 {
9719 ptrdiff_t i;
9720 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9721 int seen_dots = 0;
9722 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9723 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9724
9725 for (i = 0; i < len; i++)
9726 {
9727 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9728 seen_dots = 1;
9729 if (p1[i] != p2[i])
9730 return seen_dots;
9731 }
9732 p1 += len;
9733 if (*p1 == '\n')
9734 return 2;
9735 if (*p1++ == ' ' && *p1++ == '[')
9736 {
9737 char *pend;
9738 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9739 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9740 return n + 1;
9741 }
9742 return 0;
9743 }
9744 \f
9745
9746 /* Display an echo area message M with a specified length of NBYTES
9747 bytes. The string may include null characters. If M is not a
9748 string, clear out any existing message, and let the mini-buffer
9749 text show through.
9750
9751 This function cancels echoing. */
9752
9753 void
9754 message3 (Lisp_Object m)
9755 {
9756 struct gcpro gcpro1;
9757
9758 GCPRO1 (m);
9759 clear_message (1,1);
9760 cancel_echoing ();
9761
9762 /* First flush out any partial line written with print. */
9763 message_log_maybe_newline ();
9764 if (STRINGP (m))
9765 {
9766 ptrdiff_t nbytes = SBYTES (m);
9767 bool multibyte = STRING_MULTIBYTE (m);
9768 USE_SAFE_ALLOCA;
9769 char *buffer = SAFE_ALLOCA (nbytes);
9770 memcpy (buffer, SDATA (m), nbytes);
9771 message_dolog (buffer, nbytes, 1, multibyte);
9772 SAFE_FREE ();
9773 }
9774 message3_nolog (m);
9775
9776 UNGCPRO;
9777 }
9778
9779
9780 /* The non-logging version of message3.
9781 This does not cancel echoing, because it is used for echoing.
9782 Perhaps we need to make a separate function for echoing
9783 and make this cancel echoing. */
9784
9785 void
9786 message3_nolog (Lisp_Object m)
9787 {
9788 struct frame *sf = SELECTED_FRAME ();
9789
9790 if (FRAME_INITIAL_P (sf))
9791 {
9792 if (noninteractive_need_newline)
9793 putc ('\n', stderr);
9794 noninteractive_need_newline = 0;
9795 if (STRINGP (m))
9796 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9797 if (cursor_in_echo_area == 0)
9798 fprintf (stderr, "\n");
9799 fflush (stderr);
9800 }
9801 /* Error messages get reported properly by cmd_error, so this must be just an
9802 informative message; if the frame hasn't really been initialized yet, just
9803 toss it. */
9804 else if (INTERACTIVE && sf->glyphs_initialized_p)
9805 {
9806 /* Get the frame containing the mini-buffer
9807 that the selected frame is using. */
9808 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9809 Lisp_Object frame = XWINDOW (mini_window)->frame;
9810 struct frame *f = XFRAME (frame);
9811
9812 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9813 Fmake_frame_visible (frame);
9814
9815 if (STRINGP (m) && SCHARS (m) > 0)
9816 {
9817 set_message (m);
9818 if (minibuffer_auto_raise)
9819 Fraise_frame (frame);
9820 /* Assume we are not echoing.
9821 (If we are, echo_now will override this.) */
9822 echo_message_buffer = Qnil;
9823 }
9824 else
9825 clear_message (1, 1);
9826
9827 do_pending_window_change (0);
9828 echo_area_display (1);
9829 do_pending_window_change (0);
9830 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9831 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9832 }
9833 }
9834
9835
9836 /* Display a null-terminated echo area message M. If M is 0, clear
9837 out any existing message, and let the mini-buffer text show through.
9838
9839 The buffer M must continue to exist until after the echo area gets
9840 cleared or some other message gets displayed there. Do not pass
9841 text that is stored in a Lisp string. Do not pass text in a buffer
9842 that was alloca'd. */
9843
9844 void
9845 message1 (const char *m)
9846 {
9847 message3 (m ? build_unibyte_string (m) : Qnil);
9848 }
9849
9850
9851 /* The non-logging counterpart of message1. */
9852
9853 void
9854 message1_nolog (const char *m)
9855 {
9856 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9857 }
9858
9859 /* Display a message M which contains a single %s
9860 which gets replaced with STRING. */
9861
9862 void
9863 message_with_string (const char *m, Lisp_Object string, int log)
9864 {
9865 CHECK_STRING (string);
9866
9867 if (noninteractive)
9868 {
9869 if (m)
9870 {
9871 if (noninteractive_need_newline)
9872 putc ('\n', stderr);
9873 noninteractive_need_newline = 0;
9874 fprintf (stderr, m, SDATA (string));
9875 if (!cursor_in_echo_area)
9876 fprintf (stderr, "\n");
9877 fflush (stderr);
9878 }
9879 }
9880 else if (INTERACTIVE)
9881 {
9882 /* The frame whose minibuffer we're going to display the message on.
9883 It may be larger than the selected frame, so we need
9884 to use its buffer, not the selected frame's buffer. */
9885 Lisp_Object mini_window;
9886 struct frame *f, *sf = SELECTED_FRAME ();
9887
9888 /* Get the frame containing the minibuffer
9889 that the selected frame is using. */
9890 mini_window = FRAME_MINIBUF_WINDOW (sf);
9891 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9892
9893 /* Error messages get reported properly by cmd_error, so this must be
9894 just an informative message; if the frame hasn't really been
9895 initialized yet, just toss it. */
9896 if (f->glyphs_initialized_p)
9897 {
9898 Lisp_Object args[2], msg;
9899 struct gcpro gcpro1, gcpro2;
9900
9901 args[0] = build_string (m);
9902 args[1] = msg = string;
9903 GCPRO2 (args[0], msg);
9904 gcpro1.nvars = 2;
9905
9906 msg = Fformat (2, args);
9907
9908 if (log)
9909 message3 (msg);
9910 else
9911 message3_nolog (msg);
9912
9913 UNGCPRO;
9914
9915 /* Print should start at the beginning of the message
9916 buffer next time. */
9917 message_buf_print = 0;
9918 }
9919 }
9920 }
9921
9922
9923 /* Dump an informative message to the minibuf. If M is 0, clear out
9924 any existing message, and let the mini-buffer text show through. */
9925
9926 static void
9927 vmessage (const char *m, va_list ap)
9928 {
9929 if (noninteractive)
9930 {
9931 if (m)
9932 {
9933 if (noninteractive_need_newline)
9934 putc ('\n', stderr);
9935 noninteractive_need_newline = 0;
9936 vfprintf (stderr, m, ap);
9937 if (cursor_in_echo_area == 0)
9938 fprintf (stderr, "\n");
9939 fflush (stderr);
9940 }
9941 }
9942 else if (INTERACTIVE)
9943 {
9944 /* The frame whose mini-buffer we're going to display the message
9945 on. It may be larger than the selected frame, so we need to
9946 use its buffer, not the selected frame's buffer. */
9947 Lisp_Object mini_window;
9948 struct frame *f, *sf = SELECTED_FRAME ();
9949
9950 /* Get the frame containing the mini-buffer
9951 that the selected frame is using. */
9952 mini_window = FRAME_MINIBUF_WINDOW (sf);
9953 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9954
9955 /* Error messages get reported properly by cmd_error, so this must be
9956 just an informative message; if the frame hasn't really been
9957 initialized yet, just toss it. */
9958 if (f->glyphs_initialized_p)
9959 {
9960 if (m)
9961 {
9962 ptrdiff_t len;
9963 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9964 char *message_buf = alloca (maxsize + 1);
9965
9966 len = doprnt (message_buf, maxsize, m, 0, ap);
9967
9968 message3 (make_string (message_buf, len));
9969 }
9970 else
9971 message1 (0);
9972
9973 /* Print should start at the beginning of the message
9974 buffer next time. */
9975 message_buf_print = 0;
9976 }
9977 }
9978 }
9979
9980 void
9981 message (const char *m, ...)
9982 {
9983 va_list ap;
9984 va_start (ap, m);
9985 vmessage (m, ap);
9986 va_end (ap);
9987 }
9988
9989
9990 #if 0
9991 /* The non-logging version of message. */
9992
9993 void
9994 message_nolog (const char *m, ...)
9995 {
9996 Lisp_Object old_log_max;
9997 va_list ap;
9998 va_start (ap, m);
9999 old_log_max = Vmessage_log_max;
10000 Vmessage_log_max = Qnil;
10001 vmessage (m, ap);
10002 Vmessage_log_max = old_log_max;
10003 va_end (ap);
10004 }
10005 #endif
10006
10007
10008 /* Display the current message in the current mini-buffer. This is
10009 only called from error handlers in process.c, and is not time
10010 critical. */
10011
10012 void
10013 update_echo_area (void)
10014 {
10015 if (!NILP (echo_area_buffer[0]))
10016 {
10017 Lisp_Object string;
10018 string = Fcurrent_message ();
10019 message3 (string);
10020 }
10021 }
10022
10023
10024 /* Make sure echo area buffers in `echo_buffers' are live.
10025 If they aren't, make new ones. */
10026
10027 static void
10028 ensure_echo_area_buffers (void)
10029 {
10030 int i;
10031
10032 for (i = 0; i < 2; ++i)
10033 if (!BUFFERP (echo_buffer[i])
10034 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10035 {
10036 char name[30];
10037 Lisp_Object old_buffer;
10038 int j;
10039
10040 old_buffer = echo_buffer[i];
10041 echo_buffer[i] = Fget_buffer_create
10042 (make_formatted_string (name, " *Echo Area %d*", i));
10043 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10044 /* to force word wrap in echo area -
10045 it was decided to postpone this*/
10046 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10047
10048 for (j = 0; j < 2; ++j)
10049 if (EQ (old_buffer, echo_area_buffer[j]))
10050 echo_area_buffer[j] = echo_buffer[i];
10051 }
10052 }
10053
10054
10055 /* Call FN with args A1..A2 with either the current or last displayed
10056 echo_area_buffer as current buffer.
10057
10058 WHICH zero means use the current message buffer
10059 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10060 from echo_buffer[] and clear it.
10061
10062 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10063 suitable buffer from echo_buffer[] and clear it.
10064
10065 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10066 that the current message becomes the last displayed one, make
10067 choose a suitable buffer for echo_area_buffer[0], and clear it.
10068
10069 Value is what FN returns. */
10070
10071 static int
10072 with_echo_area_buffer (struct window *w, int which,
10073 int (*fn) (ptrdiff_t, Lisp_Object),
10074 ptrdiff_t a1, Lisp_Object a2)
10075 {
10076 Lisp_Object buffer;
10077 int this_one, the_other, clear_buffer_p, rc;
10078 ptrdiff_t count = SPECPDL_INDEX ();
10079
10080 /* If buffers aren't live, make new ones. */
10081 ensure_echo_area_buffers ();
10082
10083 clear_buffer_p = 0;
10084
10085 if (which == 0)
10086 this_one = 0, the_other = 1;
10087 else if (which > 0)
10088 this_one = 1, the_other = 0;
10089 else
10090 {
10091 this_one = 0, the_other = 1;
10092 clear_buffer_p = 1;
10093
10094 /* We need a fresh one in case the current echo buffer equals
10095 the one containing the last displayed echo area message. */
10096 if (!NILP (echo_area_buffer[this_one])
10097 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10098 echo_area_buffer[this_one] = Qnil;
10099 }
10100
10101 /* Choose a suitable buffer from echo_buffer[] is we don't
10102 have one. */
10103 if (NILP (echo_area_buffer[this_one]))
10104 {
10105 echo_area_buffer[this_one]
10106 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10107 ? echo_buffer[the_other]
10108 : echo_buffer[this_one]);
10109 clear_buffer_p = 1;
10110 }
10111
10112 buffer = echo_area_buffer[this_one];
10113
10114 /* Don't get confused by reusing the buffer used for echoing
10115 for a different purpose. */
10116 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10117 cancel_echoing ();
10118
10119 record_unwind_protect (unwind_with_echo_area_buffer,
10120 with_echo_area_buffer_unwind_data (w));
10121
10122 /* Make the echo area buffer current. Note that for display
10123 purposes, it is not necessary that the displayed window's buffer
10124 == current_buffer, except for text property lookup. So, let's
10125 only set that buffer temporarily here without doing a full
10126 Fset_window_buffer. We must also change w->pointm, though,
10127 because otherwise an assertions in unshow_buffer fails, and Emacs
10128 aborts. */
10129 set_buffer_internal_1 (XBUFFER (buffer));
10130 if (w)
10131 {
10132 wset_buffer (w, buffer);
10133 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10134 }
10135
10136 bset_undo_list (current_buffer, Qt);
10137 bset_read_only (current_buffer, Qnil);
10138 specbind (Qinhibit_read_only, Qt);
10139 specbind (Qinhibit_modification_hooks, Qt);
10140
10141 if (clear_buffer_p && Z > BEG)
10142 del_range (BEG, Z);
10143
10144 eassert (BEGV >= BEG);
10145 eassert (ZV <= Z && ZV >= BEGV);
10146
10147 rc = fn (a1, a2);
10148
10149 eassert (BEGV >= BEG);
10150 eassert (ZV <= Z && ZV >= BEGV);
10151
10152 unbind_to (count, Qnil);
10153 return rc;
10154 }
10155
10156
10157 /* Save state that should be preserved around the call to the function
10158 FN called in with_echo_area_buffer. */
10159
10160 static Lisp_Object
10161 with_echo_area_buffer_unwind_data (struct window *w)
10162 {
10163 int i = 0;
10164 Lisp_Object vector, tmp;
10165
10166 /* Reduce consing by keeping one vector in
10167 Vwith_echo_area_save_vector. */
10168 vector = Vwith_echo_area_save_vector;
10169 Vwith_echo_area_save_vector = Qnil;
10170
10171 if (NILP (vector))
10172 vector = Fmake_vector (make_number (9), Qnil);
10173
10174 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10175 ASET (vector, i, Vdeactivate_mark); ++i;
10176 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10177
10178 if (w)
10179 {
10180 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10181 ASET (vector, i, w->contents); ++i;
10182 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10183 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10184 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10185 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10186 }
10187 else
10188 {
10189 int end = i + 6;
10190 for (; i < end; ++i)
10191 ASET (vector, i, Qnil);
10192 }
10193
10194 eassert (i == ASIZE (vector));
10195 return vector;
10196 }
10197
10198
10199 /* Restore global state from VECTOR which was created by
10200 with_echo_area_buffer_unwind_data. */
10201
10202 static void
10203 unwind_with_echo_area_buffer (Lisp_Object vector)
10204 {
10205 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10206 Vdeactivate_mark = AREF (vector, 1);
10207 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10208
10209 if (WINDOWP (AREF (vector, 3)))
10210 {
10211 struct window *w;
10212 Lisp_Object buffer;
10213
10214 w = XWINDOW (AREF (vector, 3));
10215 buffer = AREF (vector, 4);
10216
10217 wset_buffer (w, buffer);
10218 set_marker_both (w->pointm, buffer,
10219 XFASTINT (AREF (vector, 5)),
10220 XFASTINT (AREF (vector, 6)));
10221 set_marker_both (w->start, buffer,
10222 XFASTINT (AREF (vector, 7)),
10223 XFASTINT (AREF (vector, 8)));
10224 }
10225
10226 Vwith_echo_area_save_vector = vector;
10227 }
10228
10229
10230 /* Set up the echo area for use by print functions. MULTIBYTE_P
10231 non-zero means we will print multibyte. */
10232
10233 void
10234 setup_echo_area_for_printing (int multibyte_p)
10235 {
10236 /* If we can't find an echo area any more, exit. */
10237 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10238 Fkill_emacs (Qnil);
10239
10240 ensure_echo_area_buffers ();
10241
10242 if (!message_buf_print)
10243 {
10244 /* A message has been output since the last time we printed.
10245 Choose a fresh echo area buffer. */
10246 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10247 echo_area_buffer[0] = echo_buffer[1];
10248 else
10249 echo_area_buffer[0] = echo_buffer[0];
10250
10251 /* Switch to that buffer and clear it. */
10252 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10253 bset_truncate_lines (current_buffer, Qnil);
10254
10255 if (Z > BEG)
10256 {
10257 ptrdiff_t count = SPECPDL_INDEX ();
10258 specbind (Qinhibit_read_only, Qt);
10259 /* Note that undo recording is always disabled. */
10260 del_range (BEG, Z);
10261 unbind_to (count, Qnil);
10262 }
10263 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10264
10265 /* Set up the buffer for the multibyteness we need. */
10266 if (multibyte_p
10267 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10268 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10269
10270 /* Raise the frame containing the echo area. */
10271 if (minibuffer_auto_raise)
10272 {
10273 struct frame *sf = SELECTED_FRAME ();
10274 Lisp_Object mini_window;
10275 mini_window = FRAME_MINIBUF_WINDOW (sf);
10276 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10277 }
10278
10279 message_log_maybe_newline ();
10280 message_buf_print = 1;
10281 }
10282 else
10283 {
10284 if (NILP (echo_area_buffer[0]))
10285 {
10286 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10287 echo_area_buffer[0] = echo_buffer[1];
10288 else
10289 echo_area_buffer[0] = echo_buffer[0];
10290 }
10291
10292 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10293 {
10294 /* Someone switched buffers between print requests. */
10295 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10296 bset_truncate_lines (current_buffer, Qnil);
10297 }
10298 }
10299 }
10300
10301
10302 /* Display an echo area message in window W. Value is non-zero if W's
10303 height is changed. If display_last_displayed_message_p is
10304 non-zero, display the message that was last displayed, otherwise
10305 display the current message. */
10306
10307 static int
10308 display_echo_area (struct window *w)
10309 {
10310 int i, no_message_p, window_height_changed_p;
10311
10312 /* Temporarily disable garbage collections while displaying the echo
10313 area. This is done because a GC can print a message itself.
10314 That message would modify the echo area buffer's contents while a
10315 redisplay of the buffer is going on, and seriously confuse
10316 redisplay. */
10317 ptrdiff_t count = inhibit_garbage_collection ();
10318
10319 /* If there is no message, we must call display_echo_area_1
10320 nevertheless because it resizes the window. But we will have to
10321 reset the echo_area_buffer in question to nil at the end because
10322 with_echo_area_buffer will sets it to an empty buffer. */
10323 i = display_last_displayed_message_p ? 1 : 0;
10324 no_message_p = NILP (echo_area_buffer[i]);
10325
10326 window_height_changed_p
10327 = with_echo_area_buffer (w, display_last_displayed_message_p,
10328 display_echo_area_1,
10329 (intptr_t) w, Qnil);
10330
10331 if (no_message_p)
10332 echo_area_buffer[i] = Qnil;
10333
10334 unbind_to (count, Qnil);
10335 return window_height_changed_p;
10336 }
10337
10338
10339 /* Helper for display_echo_area. Display the current buffer which
10340 contains the current echo area message in window W, a mini-window,
10341 a pointer to which is passed in A1. A2..A4 are currently not used.
10342 Change the height of W so that all of the message is displayed.
10343 Value is non-zero if height of W was changed. */
10344
10345 static int
10346 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10347 {
10348 intptr_t i1 = a1;
10349 struct window *w = (struct window *) i1;
10350 Lisp_Object window;
10351 struct text_pos start;
10352 int window_height_changed_p = 0;
10353
10354 /* Do this before displaying, so that we have a large enough glyph
10355 matrix for the display. If we can't get enough space for the
10356 whole text, display the last N lines. That works by setting w->start. */
10357 window_height_changed_p = resize_mini_window (w, 0);
10358
10359 /* Use the starting position chosen by resize_mini_window. */
10360 SET_TEXT_POS_FROM_MARKER (start, w->start);
10361
10362 /* Display. */
10363 clear_glyph_matrix (w->desired_matrix);
10364 XSETWINDOW (window, w);
10365 try_window (window, start, 0);
10366
10367 return window_height_changed_p;
10368 }
10369
10370
10371 /* Resize the echo area window to exactly the size needed for the
10372 currently displayed message, if there is one. If a mini-buffer
10373 is active, don't shrink it. */
10374
10375 void
10376 resize_echo_area_exactly (void)
10377 {
10378 if (BUFFERP (echo_area_buffer[0])
10379 && WINDOWP (echo_area_window))
10380 {
10381 struct window *w = XWINDOW (echo_area_window);
10382 int resized_p;
10383 Lisp_Object resize_exactly;
10384
10385 if (minibuf_level == 0)
10386 resize_exactly = Qt;
10387 else
10388 resize_exactly = Qnil;
10389
10390 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10391 (intptr_t) w, resize_exactly);
10392 if (resized_p)
10393 {
10394 ++windows_or_buffers_changed;
10395 ++update_mode_lines;
10396 redisplay_internal ();
10397 }
10398 }
10399 }
10400
10401
10402 /* Callback function for with_echo_area_buffer, when used from
10403 resize_echo_area_exactly. A1 contains a pointer to the window to
10404 resize, EXACTLY non-nil means resize the mini-window exactly to the
10405 size of the text displayed. A3 and A4 are not used. Value is what
10406 resize_mini_window returns. */
10407
10408 static int
10409 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10410 {
10411 intptr_t i1 = a1;
10412 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10413 }
10414
10415
10416 /* Resize mini-window W to fit the size of its contents. EXACT_P
10417 means size the window exactly to the size needed. Otherwise, it's
10418 only enlarged until W's buffer is empty.
10419
10420 Set W->start to the right place to begin display. If the whole
10421 contents fit, start at the beginning. Otherwise, start so as
10422 to make the end of the contents appear. This is particularly
10423 important for y-or-n-p, but seems desirable generally.
10424
10425 Value is non-zero if the window height has been changed. */
10426
10427 int
10428 resize_mini_window (struct window *w, int exact_p)
10429 {
10430 struct frame *f = XFRAME (w->frame);
10431 int window_height_changed_p = 0;
10432
10433 eassert (MINI_WINDOW_P (w));
10434
10435 /* By default, start display at the beginning. */
10436 set_marker_both (w->start, w->contents,
10437 BUF_BEGV (XBUFFER (w->contents)),
10438 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10439
10440 /* Don't resize windows while redisplaying a window; it would
10441 confuse redisplay functions when the size of the window they are
10442 displaying changes from under them. Such a resizing can happen,
10443 for instance, when which-func prints a long message while
10444 we are running fontification-functions. We're running these
10445 functions with safe_call which binds inhibit-redisplay to t. */
10446 if (!NILP (Vinhibit_redisplay))
10447 return 0;
10448
10449 /* Nil means don't try to resize. */
10450 if (NILP (Vresize_mini_windows)
10451 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10452 return 0;
10453
10454 if (!FRAME_MINIBUF_ONLY_P (f))
10455 {
10456 struct it it;
10457 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10458 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10459 int height;
10460 EMACS_INT max_height;
10461 int unit = FRAME_LINE_HEIGHT (f);
10462 struct text_pos start;
10463 struct buffer *old_current_buffer = NULL;
10464
10465 if (current_buffer != XBUFFER (w->contents))
10466 {
10467 old_current_buffer = current_buffer;
10468 set_buffer_internal (XBUFFER (w->contents));
10469 }
10470
10471 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10472
10473 /* Compute the max. number of lines specified by the user. */
10474 if (FLOATP (Vmax_mini_window_height))
10475 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10476 else if (INTEGERP (Vmax_mini_window_height))
10477 max_height = XINT (Vmax_mini_window_height);
10478 else
10479 max_height = total_height / 4;
10480
10481 /* Correct that max. height if it's bogus. */
10482 max_height = clip_to_bounds (1, max_height, total_height);
10483
10484 /* Find out the height of the text in the window. */
10485 if (it.line_wrap == TRUNCATE)
10486 height = 1;
10487 else
10488 {
10489 last_height = 0;
10490 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10491 if (it.max_ascent == 0 && it.max_descent == 0)
10492 height = it.current_y + last_height;
10493 else
10494 height = it.current_y + it.max_ascent + it.max_descent;
10495 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10496 height = (height + unit - 1) / unit;
10497 }
10498
10499 /* Compute a suitable window start. */
10500 if (height > max_height)
10501 {
10502 height = max_height;
10503 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10504 move_it_vertically_backward (&it, (height - 1) * unit);
10505 start = it.current.pos;
10506 }
10507 else
10508 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10509 SET_MARKER_FROM_TEXT_POS (w->start, start);
10510
10511 if (EQ (Vresize_mini_windows, Qgrow_only))
10512 {
10513 /* Let it grow only, until we display an empty message, in which
10514 case the window shrinks again. */
10515 if (height > WINDOW_TOTAL_LINES (w))
10516 {
10517 int old_height = WINDOW_TOTAL_LINES (w);
10518
10519 FRAME_WINDOWS_FROZEN (f) = 1;
10520 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10521 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10522 }
10523 else if (height < WINDOW_TOTAL_LINES (w)
10524 && (exact_p || BEGV == ZV))
10525 {
10526 int old_height = WINDOW_TOTAL_LINES (w);
10527
10528 FRAME_WINDOWS_FROZEN (f) = 0;
10529 shrink_mini_window (w);
10530 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10531 }
10532 }
10533 else
10534 {
10535 /* Always resize to exact size needed. */
10536 if (height > WINDOW_TOTAL_LINES (w))
10537 {
10538 int old_height = WINDOW_TOTAL_LINES (w);
10539
10540 FRAME_WINDOWS_FROZEN (f) = 1;
10541 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10542 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10543 }
10544 else if (height < WINDOW_TOTAL_LINES (w))
10545 {
10546 int old_height = WINDOW_TOTAL_LINES (w);
10547
10548 FRAME_WINDOWS_FROZEN (f) = 0;
10549 shrink_mini_window (w);
10550
10551 if (height)
10552 {
10553 FRAME_WINDOWS_FROZEN (f) = 1;
10554 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10555 }
10556
10557 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10558 }
10559 }
10560
10561 if (old_current_buffer)
10562 set_buffer_internal (old_current_buffer);
10563 }
10564
10565 return window_height_changed_p;
10566 }
10567
10568
10569 /* Value is the current message, a string, or nil if there is no
10570 current message. */
10571
10572 Lisp_Object
10573 current_message (void)
10574 {
10575 Lisp_Object msg;
10576
10577 if (!BUFFERP (echo_area_buffer[0]))
10578 msg = Qnil;
10579 else
10580 {
10581 with_echo_area_buffer (0, 0, current_message_1,
10582 (intptr_t) &msg, Qnil);
10583 if (NILP (msg))
10584 echo_area_buffer[0] = Qnil;
10585 }
10586
10587 return msg;
10588 }
10589
10590
10591 static int
10592 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10593 {
10594 intptr_t i1 = a1;
10595 Lisp_Object *msg = (Lisp_Object *) i1;
10596
10597 if (Z > BEG)
10598 *msg = make_buffer_string (BEG, Z, 1);
10599 else
10600 *msg = Qnil;
10601 return 0;
10602 }
10603
10604
10605 /* Push the current message on Vmessage_stack for later restoration
10606 by restore_message. Value is non-zero if the current message isn't
10607 empty. This is a relatively infrequent operation, so it's not
10608 worth optimizing. */
10609
10610 bool
10611 push_message (void)
10612 {
10613 Lisp_Object msg = current_message ();
10614 Vmessage_stack = Fcons (msg, Vmessage_stack);
10615 return STRINGP (msg);
10616 }
10617
10618
10619 /* Restore message display from the top of Vmessage_stack. */
10620
10621 void
10622 restore_message (void)
10623 {
10624 eassert (CONSP (Vmessage_stack));
10625 message3_nolog (XCAR (Vmessage_stack));
10626 }
10627
10628
10629 /* Handler for unwind-protect calling pop_message. */
10630
10631 void
10632 pop_message_unwind (void)
10633 {
10634 /* Pop the top-most entry off Vmessage_stack. */
10635 eassert (CONSP (Vmessage_stack));
10636 Vmessage_stack = XCDR (Vmessage_stack);
10637 }
10638
10639
10640 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10641 exits. If the stack is not empty, we have a missing pop_message
10642 somewhere. */
10643
10644 void
10645 check_message_stack (void)
10646 {
10647 if (!NILP (Vmessage_stack))
10648 emacs_abort ();
10649 }
10650
10651
10652 /* Truncate to NCHARS what will be displayed in the echo area the next
10653 time we display it---but don't redisplay it now. */
10654
10655 void
10656 truncate_echo_area (ptrdiff_t nchars)
10657 {
10658 if (nchars == 0)
10659 echo_area_buffer[0] = Qnil;
10660 else if (!noninteractive
10661 && INTERACTIVE
10662 && !NILP (echo_area_buffer[0]))
10663 {
10664 struct frame *sf = SELECTED_FRAME ();
10665 /* Error messages get reported properly by cmd_error, so this must be
10666 just an informative message; if the frame hasn't really been
10667 initialized yet, just toss it. */
10668 if (sf->glyphs_initialized_p)
10669 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10670 }
10671 }
10672
10673
10674 /* Helper function for truncate_echo_area. Truncate the current
10675 message to at most NCHARS characters. */
10676
10677 static int
10678 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10679 {
10680 if (BEG + nchars < Z)
10681 del_range (BEG + nchars, Z);
10682 if (Z == BEG)
10683 echo_area_buffer[0] = Qnil;
10684 return 0;
10685 }
10686
10687 /* Set the current message to STRING. */
10688
10689 static void
10690 set_message (Lisp_Object string)
10691 {
10692 eassert (STRINGP (string));
10693
10694 message_enable_multibyte = STRING_MULTIBYTE (string);
10695
10696 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10697 message_buf_print = 0;
10698 help_echo_showing_p = 0;
10699
10700 if (STRINGP (Vdebug_on_message)
10701 && STRINGP (string)
10702 && fast_string_match (Vdebug_on_message, string) >= 0)
10703 call_debugger (list2 (Qerror, string));
10704 }
10705
10706
10707 /* Helper function for set_message. First argument is ignored and second
10708 argument has the same meaning as for set_message.
10709 This function is called with the echo area buffer being current. */
10710
10711 static int
10712 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10713 {
10714 eassert (STRINGP (string));
10715
10716 /* Change multibyteness of the echo buffer appropriately. */
10717 if (message_enable_multibyte
10718 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10719 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10720
10721 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10722 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10723 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10724
10725 /* Insert new message at BEG. */
10726 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10727
10728 /* This function takes care of single/multibyte conversion.
10729 We just have to ensure that the echo area buffer has the right
10730 setting of enable_multibyte_characters. */
10731 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10732
10733 return 0;
10734 }
10735
10736
10737 /* Clear messages. CURRENT_P non-zero means clear the current
10738 message. LAST_DISPLAYED_P non-zero means clear the message
10739 last displayed. */
10740
10741 void
10742 clear_message (int current_p, int last_displayed_p)
10743 {
10744 if (current_p)
10745 {
10746 echo_area_buffer[0] = Qnil;
10747 message_cleared_p = 1;
10748 }
10749
10750 if (last_displayed_p)
10751 echo_area_buffer[1] = Qnil;
10752
10753 message_buf_print = 0;
10754 }
10755
10756 /* Clear garbaged frames.
10757
10758 This function is used where the old redisplay called
10759 redraw_garbaged_frames which in turn called redraw_frame which in
10760 turn called clear_frame. The call to clear_frame was a source of
10761 flickering. I believe a clear_frame is not necessary. It should
10762 suffice in the new redisplay to invalidate all current matrices,
10763 and ensure a complete redisplay of all windows. */
10764
10765 static void
10766 clear_garbaged_frames (void)
10767 {
10768 if (frame_garbaged)
10769 {
10770 Lisp_Object tail, frame;
10771 int changed_count = 0;
10772
10773 FOR_EACH_FRAME (tail, frame)
10774 {
10775 struct frame *f = XFRAME (frame);
10776
10777 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10778 {
10779 if (f->resized_p)
10780 redraw_frame (f);
10781 else
10782 clear_current_matrices (f);
10783 changed_count++;
10784 f->garbaged = 0;
10785 f->resized_p = 0;
10786 }
10787 }
10788
10789 frame_garbaged = 0;
10790 if (changed_count)
10791 ++windows_or_buffers_changed;
10792 }
10793 }
10794
10795
10796 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10797 is non-zero update selected_frame. Value is non-zero if the
10798 mini-windows height has been changed. */
10799
10800 static int
10801 echo_area_display (int update_frame_p)
10802 {
10803 Lisp_Object mini_window;
10804 struct window *w;
10805 struct frame *f;
10806 int window_height_changed_p = 0;
10807 struct frame *sf = SELECTED_FRAME ();
10808
10809 mini_window = FRAME_MINIBUF_WINDOW (sf);
10810 w = XWINDOW (mini_window);
10811 f = XFRAME (WINDOW_FRAME (w));
10812
10813 /* Don't display if frame is invisible or not yet initialized. */
10814 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10815 return 0;
10816
10817 #ifdef HAVE_WINDOW_SYSTEM
10818 /* When Emacs starts, selected_frame may be the initial terminal
10819 frame. If we let this through, a message would be displayed on
10820 the terminal. */
10821 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10822 return 0;
10823 #endif /* HAVE_WINDOW_SYSTEM */
10824
10825 /* Redraw garbaged frames. */
10826 clear_garbaged_frames ();
10827
10828 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10829 {
10830 echo_area_window = mini_window;
10831 window_height_changed_p = display_echo_area (w);
10832 w->must_be_updated_p = 1;
10833
10834 /* Update the display, unless called from redisplay_internal.
10835 Also don't update the screen during redisplay itself. The
10836 update will happen at the end of redisplay, and an update
10837 here could cause confusion. */
10838 if (update_frame_p && !redisplaying_p)
10839 {
10840 int n = 0;
10841
10842 /* If the display update has been interrupted by pending
10843 input, update mode lines in the frame. Due to the
10844 pending input, it might have been that redisplay hasn't
10845 been called, so that mode lines above the echo area are
10846 garbaged. This looks odd, so we prevent it here. */
10847 if (!display_completed)
10848 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10849
10850 if (window_height_changed_p
10851 /* Don't do this if Emacs is shutting down. Redisplay
10852 needs to run hooks. */
10853 && !NILP (Vrun_hooks))
10854 {
10855 /* Must update other windows. Likewise as in other
10856 cases, don't let this update be interrupted by
10857 pending input. */
10858 ptrdiff_t count = SPECPDL_INDEX ();
10859 specbind (Qredisplay_dont_pause, Qt);
10860 windows_or_buffers_changed = 1;
10861 redisplay_internal ();
10862 unbind_to (count, Qnil);
10863 }
10864 else if (FRAME_WINDOW_P (f) && n == 0)
10865 {
10866 /* Window configuration is the same as before.
10867 Can do with a display update of the echo area,
10868 unless we displayed some mode lines. */
10869 update_single_window (w, 1);
10870 flush_frame (f);
10871 }
10872 else
10873 update_frame (f, 1, 1);
10874
10875 /* If cursor is in the echo area, make sure that the next
10876 redisplay displays the minibuffer, so that the cursor will
10877 be replaced with what the minibuffer wants. */
10878 if (cursor_in_echo_area)
10879 ++windows_or_buffers_changed;
10880 }
10881 }
10882 else if (!EQ (mini_window, selected_window))
10883 windows_or_buffers_changed++;
10884
10885 /* Last displayed message is now the current message. */
10886 echo_area_buffer[1] = echo_area_buffer[0];
10887 /* Inform read_char that we're not echoing. */
10888 echo_message_buffer = Qnil;
10889
10890 /* Prevent redisplay optimization in redisplay_internal by resetting
10891 this_line_start_pos. This is done because the mini-buffer now
10892 displays the message instead of its buffer text. */
10893 if (EQ (mini_window, selected_window))
10894 CHARPOS (this_line_start_pos) = 0;
10895
10896 return window_height_changed_p;
10897 }
10898
10899 /* Nonzero if the current window's buffer is shown in more than one
10900 window and was modified since last redisplay. */
10901
10902 static int
10903 buffer_shared_and_changed (void)
10904 {
10905 return (buffer_window_count (current_buffer) > 1
10906 && UNCHANGED_MODIFIED < MODIFF);
10907 }
10908
10909 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10910 is enabled and mark of W's buffer was changed since last W's update. */
10911
10912 static int
10913 window_buffer_changed (struct window *w)
10914 {
10915 struct buffer *b = XBUFFER (w->contents);
10916
10917 eassert (BUFFER_LIVE_P (b));
10918
10919 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10920 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10921 != (w->region_showing != 0)));
10922 }
10923
10924 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10925
10926 static int
10927 mode_line_update_needed (struct window *w)
10928 {
10929 return (w->column_number_displayed != -1
10930 && !(PT == w->last_point && !window_outdated (w))
10931 && (w->column_number_displayed != current_column ()));
10932 }
10933
10934 /* Nonzero if window start of W is frozen and may not be changed during
10935 redisplay. */
10936
10937 static bool
10938 window_frozen_p (struct window *w)
10939 {
10940 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10941 {
10942 Lisp_Object window;
10943
10944 XSETWINDOW (window, w);
10945 if (MINI_WINDOW_P (w))
10946 return 0;
10947 else if (EQ (window, selected_window))
10948 return 0;
10949 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10950 && EQ (window, Vminibuf_scroll_window))
10951 /* This special window can't be frozen too. */
10952 return 0;
10953 else
10954 return 1;
10955 }
10956 return 0;
10957 }
10958
10959 /***********************************************************************
10960 Mode Lines and Frame Titles
10961 ***********************************************************************/
10962
10963 /* A buffer for constructing non-propertized mode-line strings and
10964 frame titles in it; allocated from the heap in init_xdisp and
10965 resized as needed in store_mode_line_noprop_char. */
10966
10967 static char *mode_line_noprop_buf;
10968
10969 /* The buffer's end, and a current output position in it. */
10970
10971 static char *mode_line_noprop_buf_end;
10972 static char *mode_line_noprop_ptr;
10973
10974 #define MODE_LINE_NOPROP_LEN(start) \
10975 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10976
10977 static enum {
10978 MODE_LINE_DISPLAY = 0,
10979 MODE_LINE_TITLE,
10980 MODE_LINE_NOPROP,
10981 MODE_LINE_STRING
10982 } mode_line_target;
10983
10984 /* Alist that caches the results of :propertize.
10985 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10986 static Lisp_Object mode_line_proptrans_alist;
10987
10988 /* List of strings making up the mode-line. */
10989 static Lisp_Object mode_line_string_list;
10990
10991 /* Base face property when building propertized mode line string. */
10992 static Lisp_Object mode_line_string_face;
10993 static Lisp_Object mode_line_string_face_prop;
10994
10995
10996 /* Unwind data for mode line strings */
10997
10998 static Lisp_Object Vmode_line_unwind_vector;
10999
11000 static Lisp_Object
11001 format_mode_line_unwind_data (struct frame *target_frame,
11002 struct buffer *obuf,
11003 Lisp_Object owin,
11004 int save_proptrans)
11005 {
11006 Lisp_Object vector, tmp;
11007
11008 /* Reduce consing by keeping one vector in
11009 Vwith_echo_area_save_vector. */
11010 vector = Vmode_line_unwind_vector;
11011 Vmode_line_unwind_vector = Qnil;
11012
11013 if (NILP (vector))
11014 vector = Fmake_vector (make_number (10), Qnil);
11015
11016 ASET (vector, 0, make_number (mode_line_target));
11017 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11018 ASET (vector, 2, mode_line_string_list);
11019 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11020 ASET (vector, 4, mode_line_string_face);
11021 ASET (vector, 5, mode_line_string_face_prop);
11022
11023 if (obuf)
11024 XSETBUFFER (tmp, obuf);
11025 else
11026 tmp = Qnil;
11027 ASET (vector, 6, tmp);
11028 ASET (vector, 7, owin);
11029 if (target_frame)
11030 {
11031 /* Similarly to `with-selected-window', if the operation selects
11032 a window on another frame, we must restore that frame's
11033 selected window, and (for a tty) the top-frame. */
11034 ASET (vector, 8, target_frame->selected_window);
11035 if (FRAME_TERMCAP_P (target_frame))
11036 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11037 }
11038
11039 return vector;
11040 }
11041
11042 static void
11043 unwind_format_mode_line (Lisp_Object vector)
11044 {
11045 Lisp_Object old_window = AREF (vector, 7);
11046 Lisp_Object target_frame_window = AREF (vector, 8);
11047 Lisp_Object old_top_frame = AREF (vector, 9);
11048
11049 mode_line_target = XINT (AREF (vector, 0));
11050 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11051 mode_line_string_list = AREF (vector, 2);
11052 if (! EQ (AREF (vector, 3), Qt))
11053 mode_line_proptrans_alist = AREF (vector, 3);
11054 mode_line_string_face = AREF (vector, 4);
11055 mode_line_string_face_prop = AREF (vector, 5);
11056
11057 /* Select window before buffer, since it may change the buffer. */
11058 if (!NILP (old_window))
11059 {
11060 /* If the operation that we are unwinding had selected a window
11061 on a different frame, reset its frame-selected-window. For a
11062 text terminal, reset its top-frame if necessary. */
11063 if (!NILP (target_frame_window))
11064 {
11065 Lisp_Object frame
11066 = WINDOW_FRAME (XWINDOW (target_frame_window));
11067
11068 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11069 Fselect_window (target_frame_window, Qt);
11070
11071 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11072 Fselect_frame (old_top_frame, Qt);
11073 }
11074
11075 Fselect_window (old_window, Qt);
11076 }
11077
11078 if (!NILP (AREF (vector, 6)))
11079 {
11080 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11081 ASET (vector, 6, Qnil);
11082 }
11083
11084 Vmode_line_unwind_vector = vector;
11085 }
11086
11087
11088 /* Store a single character C for the frame title in mode_line_noprop_buf.
11089 Re-allocate mode_line_noprop_buf if necessary. */
11090
11091 static void
11092 store_mode_line_noprop_char (char c)
11093 {
11094 /* If output position has reached the end of the allocated buffer,
11095 increase the buffer's size. */
11096 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11097 {
11098 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11099 ptrdiff_t size = len;
11100 mode_line_noprop_buf =
11101 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11102 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11103 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11104 }
11105
11106 *mode_line_noprop_ptr++ = c;
11107 }
11108
11109
11110 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11111 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11112 characters that yield more columns than PRECISION; PRECISION <= 0
11113 means copy the whole string. Pad with spaces until FIELD_WIDTH
11114 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11115 pad. Called from display_mode_element when it is used to build a
11116 frame title. */
11117
11118 static int
11119 store_mode_line_noprop (const char *string, int field_width, int precision)
11120 {
11121 const unsigned char *str = (const unsigned char *) string;
11122 int n = 0;
11123 ptrdiff_t dummy, nbytes;
11124
11125 /* Copy at most PRECISION chars from STR. */
11126 nbytes = strlen (string);
11127 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11128 while (nbytes--)
11129 store_mode_line_noprop_char (*str++);
11130
11131 /* Fill up with spaces until FIELD_WIDTH reached. */
11132 while (field_width > 0
11133 && n < field_width)
11134 {
11135 store_mode_line_noprop_char (' ');
11136 ++n;
11137 }
11138
11139 return n;
11140 }
11141
11142 /***********************************************************************
11143 Frame Titles
11144 ***********************************************************************/
11145
11146 #ifdef HAVE_WINDOW_SYSTEM
11147
11148 /* Set the title of FRAME, if it has changed. The title format is
11149 Vicon_title_format if FRAME is iconified, otherwise it is
11150 frame_title_format. */
11151
11152 static void
11153 x_consider_frame_title (Lisp_Object frame)
11154 {
11155 struct frame *f = XFRAME (frame);
11156
11157 if (FRAME_WINDOW_P (f)
11158 || FRAME_MINIBUF_ONLY_P (f)
11159 || f->explicit_name)
11160 {
11161 /* Do we have more than one visible frame on this X display? */
11162 Lisp_Object tail, other_frame, fmt;
11163 ptrdiff_t title_start;
11164 char *title;
11165 ptrdiff_t len;
11166 struct it it;
11167 ptrdiff_t count = SPECPDL_INDEX ();
11168
11169 FOR_EACH_FRAME (tail, other_frame)
11170 {
11171 struct frame *tf = XFRAME (other_frame);
11172
11173 if (tf != f
11174 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11175 && !FRAME_MINIBUF_ONLY_P (tf)
11176 && !EQ (other_frame, tip_frame)
11177 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11178 break;
11179 }
11180
11181 /* Set global variable indicating that multiple frames exist. */
11182 multiple_frames = CONSP (tail);
11183
11184 /* Switch to the buffer of selected window of the frame. Set up
11185 mode_line_target so that display_mode_element will output into
11186 mode_line_noprop_buf; then display the title. */
11187 record_unwind_protect (unwind_format_mode_line,
11188 format_mode_line_unwind_data
11189 (f, current_buffer, selected_window, 0));
11190
11191 Fselect_window (f->selected_window, Qt);
11192 set_buffer_internal_1
11193 (XBUFFER (XWINDOW (f->selected_window)->contents));
11194 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11195
11196 mode_line_target = MODE_LINE_TITLE;
11197 title_start = MODE_LINE_NOPROP_LEN (0);
11198 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11199 NULL, DEFAULT_FACE_ID);
11200 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11201 len = MODE_LINE_NOPROP_LEN (title_start);
11202 title = mode_line_noprop_buf + title_start;
11203 unbind_to (count, Qnil);
11204
11205 /* Set the title only if it's changed. This avoids consing in
11206 the common case where it hasn't. (If it turns out that we've
11207 already wasted too much time by walking through the list with
11208 display_mode_element, then we might need to optimize at a
11209 higher level than this.) */
11210 if (! STRINGP (f->name)
11211 || SBYTES (f->name) != len
11212 || memcmp (title, SDATA (f->name), len) != 0)
11213 x_implicitly_set_name (f, make_string (title, len), Qnil);
11214 }
11215 }
11216
11217 #endif /* not HAVE_WINDOW_SYSTEM */
11218
11219 \f
11220 /***********************************************************************
11221 Menu Bars
11222 ***********************************************************************/
11223
11224
11225 /* Prepare for redisplay by updating menu-bar item lists when
11226 appropriate. This can call eval. */
11227
11228 void
11229 prepare_menu_bars (void)
11230 {
11231 int all_windows;
11232 struct gcpro gcpro1, gcpro2;
11233 struct frame *f;
11234 Lisp_Object tooltip_frame;
11235
11236 #ifdef HAVE_WINDOW_SYSTEM
11237 tooltip_frame = tip_frame;
11238 #else
11239 tooltip_frame = Qnil;
11240 #endif
11241
11242 /* Update all frame titles based on their buffer names, etc. We do
11243 this before the menu bars so that the buffer-menu will show the
11244 up-to-date frame titles. */
11245 #ifdef HAVE_WINDOW_SYSTEM
11246 if (windows_or_buffers_changed || update_mode_lines)
11247 {
11248 Lisp_Object tail, frame;
11249
11250 FOR_EACH_FRAME (tail, frame)
11251 {
11252 f = XFRAME (frame);
11253 if (!EQ (frame, tooltip_frame)
11254 && (FRAME_ICONIFIED_P (f)
11255 || FRAME_VISIBLE_P (f) == 1
11256 /* Exclude TTY frames that are obscured because they
11257 are not the top frame on their console. This is
11258 because x_consider_frame_title actually switches
11259 to the frame, which for TTY frames means it is
11260 marked as garbaged, and will be completely
11261 redrawn on the next redisplay cycle. This causes
11262 TTY frames to be completely redrawn, when there
11263 are more than one of them, even though nothing
11264 should be changed on display. */
11265 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11266 x_consider_frame_title (frame);
11267 }
11268 }
11269 #endif /* HAVE_WINDOW_SYSTEM */
11270
11271 /* Update the menu bar item lists, if appropriate. This has to be
11272 done before any actual redisplay or generation of display lines. */
11273 all_windows = (update_mode_lines
11274 || buffer_shared_and_changed ()
11275 || windows_or_buffers_changed);
11276 if (all_windows)
11277 {
11278 Lisp_Object tail, frame;
11279 ptrdiff_t count = SPECPDL_INDEX ();
11280 /* 1 means that update_menu_bar has run its hooks
11281 so any further calls to update_menu_bar shouldn't do so again. */
11282 int menu_bar_hooks_run = 0;
11283
11284 record_unwind_save_match_data ();
11285
11286 FOR_EACH_FRAME (tail, frame)
11287 {
11288 f = XFRAME (frame);
11289
11290 /* Ignore tooltip frame. */
11291 if (EQ (frame, tooltip_frame))
11292 continue;
11293
11294 /* If a window on this frame changed size, report that to
11295 the user and clear the size-change flag. */
11296 if (FRAME_WINDOW_SIZES_CHANGED (f))
11297 {
11298 Lisp_Object functions;
11299
11300 /* Clear flag first in case we get an error below. */
11301 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11302 functions = Vwindow_size_change_functions;
11303 GCPRO2 (tail, functions);
11304
11305 while (CONSP (functions))
11306 {
11307 if (!EQ (XCAR (functions), Qt))
11308 call1 (XCAR (functions), frame);
11309 functions = XCDR (functions);
11310 }
11311 UNGCPRO;
11312 }
11313
11314 GCPRO1 (tail);
11315 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11316 #ifdef HAVE_WINDOW_SYSTEM
11317 update_tool_bar (f, 0);
11318 #endif
11319 #ifdef HAVE_NS
11320 if (windows_or_buffers_changed
11321 && FRAME_NS_P (f))
11322 ns_set_doc_edited
11323 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11324 #endif
11325 UNGCPRO;
11326 }
11327
11328 unbind_to (count, Qnil);
11329 }
11330 else
11331 {
11332 struct frame *sf = SELECTED_FRAME ();
11333 update_menu_bar (sf, 1, 0);
11334 #ifdef HAVE_WINDOW_SYSTEM
11335 update_tool_bar (sf, 1);
11336 #endif
11337 }
11338 }
11339
11340
11341 /* Update the menu bar item list for frame F. This has to be done
11342 before we start to fill in any display lines, because it can call
11343 eval.
11344
11345 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11346
11347 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11348 already ran the menu bar hooks for this redisplay, so there
11349 is no need to run them again. The return value is the
11350 updated value of this flag, to pass to the next call. */
11351
11352 static int
11353 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11354 {
11355 Lisp_Object window;
11356 register struct window *w;
11357
11358 /* If called recursively during a menu update, do nothing. This can
11359 happen when, for instance, an activate-menubar-hook causes a
11360 redisplay. */
11361 if (inhibit_menubar_update)
11362 return hooks_run;
11363
11364 window = FRAME_SELECTED_WINDOW (f);
11365 w = XWINDOW (window);
11366
11367 if (FRAME_WINDOW_P (f)
11368 ?
11369 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11370 || defined (HAVE_NS) || defined (USE_GTK)
11371 FRAME_EXTERNAL_MENU_BAR (f)
11372 #else
11373 FRAME_MENU_BAR_LINES (f) > 0
11374 #endif
11375 : FRAME_MENU_BAR_LINES (f) > 0)
11376 {
11377 /* If the user has switched buffers or windows, we need to
11378 recompute to reflect the new bindings. But we'll
11379 recompute when update_mode_lines is set too; that means
11380 that people can use force-mode-line-update to request
11381 that the menu bar be recomputed. The adverse effect on
11382 the rest of the redisplay algorithm is about the same as
11383 windows_or_buffers_changed anyway. */
11384 if (windows_or_buffers_changed
11385 /* This used to test w->update_mode_line, but we believe
11386 there is no need to recompute the menu in that case. */
11387 || update_mode_lines
11388 || window_buffer_changed (w))
11389 {
11390 struct buffer *prev = current_buffer;
11391 ptrdiff_t count = SPECPDL_INDEX ();
11392
11393 specbind (Qinhibit_menubar_update, Qt);
11394
11395 set_buffer_internal_1 (XBUFFER (w->contents));
11396 if (save_match_data)
11397 record_unwind_save_match_data ();
11398 if (NILP (Voverriding_local_map_menu_flag))
11399 {
11400 specbind (Qoverriding_terminal_local_map, Qnil);
11401 specbind (Qoverriding_local_map, Qnil);
11402 }
11403
11404 if (!hooks_run)
11405 {
11406 /* Run the Lucid hook. */
11407 safe_run_hooks (Qactivate_menubar_hook);
11408
11409 /* If it has changed current-menubar from previous value,
11410 really recompute the menu-bar from the value. */
11411 if (! NILP (Vlucid_menu_bar_dirty_flag))
11412 call0 (Qrecompute_lucid_menubar);
11413
11414 safe_run_hooks (Qmenu_bar_update_hook);
11415
11416 hooks_run = 1;
11417 }
11418
11419 XSETFRAME (Vmenu_updating_frame, f);
11420 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11421
11422 /* Redisplay the menu bar in case we changed it. */
11423 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11424 || defined (HAVE_NS) || defined (USE_GTK)
11425 if (FRAME_WINDOW_P (f))
11426 {
11427 #if defined (HAVE_NS)
11428 /* All frames on Mac OS share the same menubar. So only
11429 the selected frame should be allowed to set it. */
11430 if (f == SELECTED_FRAME ())
11431 #endif
11432 set_frame_menubar (f, 0, 0);
11433 }
11434 else
11435 /* On a terminal screen, the menu bar is an ordinary screen
11436 line, and this makes it get updated. */
11437 w->update_mode_line = 1;
11438 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11439 /* In the non-toolkit version, the menu bar is an ordinary screen
11440 line, and this makes it get updated. */
11441 w->update_mode_line = 1;
11442 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11443
11444 unbind_to (count, Qnil);
11445 set_buffer_internal_1 (prev);
11446 }
11447 }
11448
11449 return hooks_run;
11450 }
11451
11452 /***********************************************************************
11453 Tool-bars
11454 ***********************************************************************/
11455
11456 #ifdef HAVE_WINDOW_SYSTEM
11457
11458 /* Tool-bar item index of the item on which a mouse button was pressed
11459 or -1. */
11460
11461 int last_tool_bar_item;
11462
11463 /* Select `frame' temporarily without running all the code in
11464 do_switch_frame.
11465 FIXME: Maybe do_switch_frame should be trimmed down similarly
11466 when `norecord' is set. */
11467 static void
11468 fast_set_selected_frame (Lisp_Object frame)
11469 {
11470 if (!EQ (selected_frame, frame))
11471 {
11472 selected_frame = frame;
11473 selected_window = XFRAME (frame)->selected_window;
11474 }
11475 }
11476
11477 /* Update the tool-bar item list for frame F. This has to be done
11478 before we start to fill in any display lines. Called from
11479 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11480 and restore it here. */
11481
11482 static void
11483 update_tool_bar (struct frame *f, int save_match_data)
11484 {
11485 #if defined (USE_GTK) || defined (HAVE_NS)
11486 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11487 #else
11488 int do_update = WINDOWP (f->tool_bar_window)
11489 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11490 #endif
11491
11492 if (do_update)
11493 {
11494 Lisp_Object window;
11495 struct window *w;
11496
11497 window = FRAME_SELECTED_WINDOW (f);
11498 w = XWINDOW (window);
11499
11500 /* If the user has switched buffers or windows, we need to
11501 recompute to reflect the new bindings. But we'll
11502 recompute when update_mode_lines is set too; that means
11503 that people can use force-mode-line-update to request
11504 that the menu bar be recomputed. The adverse effect on
11505 the rest of the redisplay algorithm is about the same as
11506 windows_or_buffers_changed anyway. */
11507 if (windows_or_buffers_changed
11508 || w->update_mode_line
11509 || update_mode_lines
11510 || window_buffer_changed (w))
11511 {
11512 struct buffer *prev = current_buffer;
11513 ptrdiff_t count = SPECPDL_INDEX ();
11514 Lisp_Object frame, new_tool_bar;
11515 int new_n_tool_bar;
11516 struct gcpro gcpro1;
11517
11518 /* Set current_buffer to the buffer of the selected
11519 window of the frame, so that we get the right local
11520 keymaps. */
11521 set_buffer_internal_1 (XBUFFER (w->contents));
11522
11523 /* Save match data, if we must. */
11524 if (save_match_data)
11525 record_unwind_save_match_data ();
11526
11527 /* Make sure that we don't accidentally use bogus keymaps. */
11528 if (NILP (Voverriding_local_map_menu_flag))
11529 {
11530 specbind (Qoverriding_terminal_local_map, Qnil);
11531 specbind (Qoverriding_local_map, Qnil);
11532 }
11533
11534 GCPRO1 (new_tool_bar);
11535
11536 /* We must temporarily set the selected frame to this frame
11537 before calling tool_bar_items, because the calculation of
11538 the tool-bar keymap uses the selected frame (see
11539 `tool-bar-make-keymap' in tool-bar.el). */
11540 eassert (EQ (selected_window,
11541 /* Since we only explicitly preserve selected_frame,
11542 check that selected_window would be redundant. */
11543 XFRAME (selected_frame)->selected_window));
11544 record_unwind_protect (fast_set_selected_frame, selected_frame);
11545 XSETFRAME (frame, f);
11546 fast_set_selected_frame (frame);
11547
11548 /* Build desired tool-bar items from keymaps. */
11549 new_tool_bar
11550 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11551 &new_n_tool_bar);
11552
11553 /* Redisplay the tool-bar if we changed it. */
11554 if (new_n_tool_bar != f->n_tool_bar_items
11555 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11556 {
11557 /* Redisplay that happens asynchronously due to an expose event
11558 may access f->tool_bar_items. Make sure we update both
11559 variables within BLOCK_INPUT so no such event interrupts. */
11560 block_input ();
11561 fset_tool_bar_items (f, new_tool_bar);
11562 f->n_tool_bar_items = new_n_tool_bar;
11563 w->update_mode_line = 1;
11564 unblock_input ();
11565 }
11566
11567 UNGCPRO;
11568
11569 unbind_to (count, Qnil);
11570 set_buffer_internal_1 (prev);
11571 }
11572 }
11573 }
11574
11575 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11576
11577 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11578 F's desired tool-bar contents. F->tool_bar_items must have
11579 been set up previously by calling prepare_menu_bars. */
11580
11581 static void
11582 build_desired_tool_bar_string (struct frame *f)
11583 {
11584 int i, size, size_needed;
11585 struct gcpro gcpro1, gcpro2, gcpro3;
11586 Lisp_Object image, plist, props;
11587
11588 image = plist = props = Qnil;
11589 GCPRO3 (image, plist, props);
11590
11591 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11592 Otherwise, make a new string. */
11593
11594 /* The size of the string we might be able to reuse. */
11595 size = (STRINGP (f->desired_tool_bar_string)
11596 ? SCHARS (f->desired_tool_bar_string)
11597 : 0);
11598
11599 /* We need one space in the string for each image. */
11600 size_needed = f->n_tool_bar_items;
11601
11602 /* Reuse f->desired_tool_bar_string, if possible. */
11603 if (size < size_needed || NILP (f->desired_tool_bar_string))
11604 fset_desired_tool_bar_string
11605 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11606 else
11607 {
11608 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11609 Fremove_text_properties (make_number (0), make_number (size),
11610 props, f->desired_tool_bar_string);
11611 }
11612
11613 /* Put a `display' property on the string for the images to display,
11614 put a `menu_item' property on tool-bar items with a value that
11615 is the index of the item in F's tool-bar item vector. */
11616 for (i = 0; i < f->n_tool_bar_items; ++i)
11617 {
11618 #define PROP(IDX) \
11619 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11620
11621 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11622 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11623 int hmargin, vmargin, relief, idx, end;
11624
11625 /* If image is a vector, choose the image according to the
11626 button state. */
11627 image = PROP (TOOL_BAR_ITEM_IMAGES);
11628 if (VECTORP (image))
11629 {
11630 if (enabled_p)
11631 idx = (selected_p
11632 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11633 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11634 else
11635 idx = (selected_p
11636 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11637 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11638
11639 eassert (ASIZE (image) >= idx);
11640 image = AREF (image, idx);
11641 }
11642 else
11643 idx = -1;
11644
11645 /* Ignore invalid image specifications. */
11646 if (!valid_image_p (image))
11647 continue;
11648
11649 /* Display the tool-bar button pressed, or depressed. */
11650 plist = Fcopy_sequence (XCDR (image));
11651
11652 /* Compute margin and relief to draw. */
11653 relief = (tool_bar_button_relief >= 0
11654 ? tool_bar_button_relief
11655 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11656 hmargin = vmargin = relief;
11657
11658 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11659 INT_MAX - max (hmargin, vmargin)))
11660 {
11661 hmargin += XFASTINT (Vtool_bar_button_margin);
11662 vmargin += XFASTINT (Vtool_bar_button_margin);
11663 }
11664 else if (CONSP (Vtool_bar_button_margin))
11665 {
11666 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11667 INT_MAX - hmargin))
11668 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11669
11670 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11671 INT_MAX - vmargin))
11672 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11673 }
11674
11675 if (auto_raise_tool_bar_buttons_p)
11676 {
11677 /* Add a `:relief' property to the image spec if the item is
11678 selected. */
11679 if (selected_p)
11680 {
11681 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11682 hmargin -= relief;
11683 vmargin -= relief;
11684 }
11685 }
11686 else
11687 {
11688 /* If image is selected, display it pressed, i.e. with a
11689 negative relief. If it's not selected, display it with a
11690 raised relief. */
11691 plist = Fplist_put (plist, QCrelief,
11692 (selected_p
11693 ? make_number (-relief)
11694 : make_number (relief)));
11695 hmargin -= relief;
11696 vmargin -= relief;
11697 }
11698
11699 /* Put a margin around the image. */
11700 if (hmargin || vmargin)
11701 {
11702 if (hmargin == vmargin)
11703 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11704 else
11705 plist = Fplist_put (plist, QCmargin,
11706 Fcons (make_number (hmargin),
11707 make_number (vmargin)));
11708 }
11709
11710 /* If button is not enabled, and we don't have special images
11711 for the disabled state, make the image appear disabled by
11712 applying an appropriate algorithm to it. */
11713 if (!enabled_p && idx < 0)
11714 plist = Fplist_put (plist, QCconversion, Qdisabled);
11715
11716 /* Put a `display' text property on the string for the image to
11717 display. Put a `menu-item' property on the string that gives
11718 the start of this item's properties in the tool-bar items
11719 vector. */
11720 image = Fcons (Qimage, plist);
11721 props = list4 (Qdisplay, image,
11722 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11723
11724 /* Let the last image hide all remaining spaces in the tool bar
11725 string. The string can be longer than needed when we reuse a
11726 previous string. */
11727 if (i + 1 == f->n_tool_bar_items)
11728 end = SCHARS (f->desired_tool_bar_string);
11729 else
11730 end = i + 1;
11731 Fadd_text_properties (make_number (i), make_number (end),
11732 props, f->desired_tool_bar_string);
11733 #undef PROP
11734 }
11735
11736 UNGCPRO;
11737 }
11738
11739
11740 /* Display one line of the tool-bar of frame IT->f.
11741
11742 HEIGHT specifies the desired height of the tool-bar line.
11743 If the actual height of the glyph row is less than HEIGHT, the
11744 row's height is increased to HEIGHT, and the icons are centered
11745 vertically in the new height.
11746
11747 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11748 count a final empty row in case the tool-bar width exactly matches
11749 the window width.
11750 */
11751
11752 static void
11753 display_tool_bar_line (struct it *it, int height)
11754 {
11755 struct glyph_row *row = it->glyph_row;
11756 int max_x = it->last_visible_x;
11757 struct glyph *last;
11758
11759 prepare_desired_row (row);
11760 row->y = it->current_y;
11761
11762 /* Note that this isn't made use of if the face hasn't a box,
11763 so there's no need to check the face here. */
11764 it->start_of_box_run_p = 1;
11765
11766 while (it->current_x < max_x)
11767 {
11768 int x, n_glyphs_before, i, nglyphs;
11769 struct it it_before;
11770
11771 /* Get the next display element. */
11772 if (!get_next_display_element (it))
11773 {
11774 /* Don't count empty row if we are counting needed tool-bar lines. */
11775 if (height < 0 && !it->hpos)
11776 return;
11777 break;
11778 }
11779
11780 /* Produce glyphs. */
11781 n_glyphs_before = row->used[TEXT_AREA];
11782 it_before = *it;
11783
11784 PRODUCE_GLYPHS (it);
11785
11786 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11787 i = 0;
11788 x = it_before.current_x;
11789 while (i < nglyphs)
11790 {
11791 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11792
11793 if (x + glyph->pixel_width > max_x)
11794 {
11795 /* Glyph doesn't fit on line. Backtrack. */
11796 row->used[TEXT_AREA] = n_glyphs_before;
11797 *it = it_before;
11798 /* If this is the only glyph on this line, it will never fit on the
11799 tool-bar, so skip it. But ensure there is at least one glyph,
11800 so we don't accidentally disable the tool-bar. */
11801 if (n_glyphs_before == 0
11802 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11803 break;
11804 goto out;
11805 }
11806
11807 ++it->hpos;
11808 x += glyph->pixel_width;
11809 ++i;
11810 }
11811
11812 /* Stop at line end. */
11813 if (ITERATOR_AT_END_OF_LINE_P (it))
11814 break;
11815
11816 set_iterator_to_next (it, 1);
11817 }
11818
11819 out:;
11820
11821 row->displays_text_p = row->used[TEXT_AREA] != 0;
11822
11823 /* Use default face for the border below the tool bar.
11824
11825 FIXME: When auto-resize-tool-bars is grow-only, there is
11826 no additional border below the possibly empty tool-bar lines.
11827 So to make the extra empty lines look "normal", we have to
11828 use the tool-bar face for the border too. */
11829 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11830 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11831 it->face_id = DEFAULT_FACE_ID;
11832
11833 extend_face_to_end_of_line (it);
11834 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11835 last->right_box_line_p = 1;
11836 if (last == row->glyphs[TEXT_AREA])
11837 last->left_box_line_p = 1;
11838
11839 /* Make line the desired height and center it vertically. */
11840 if ((height -= it->max_ascent + it->max_descent) > 0)
11841 {
11842 /* Don't add more than one line height. */
11843 height %= FRAME_LINE_HEIGHT (it->f);
11844 it->max_ascent += height / 2;
11845 it->max_descent += (height + 1) / 2;
11846 }
11847
11848 compute_line_metrics (it);
11849
11850 /* If line is empty, make it occupy the rest of the tool-bar. */
11851 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11852 {
11853 row->height = row->phys_height = it->last_visible_y - row->y;
11854 row->visible_height = row->height;
11855 row->ascent = row->phys_ascent = 0;
11856 row->extra_line_spacing = 0;
11857 }
11858
11859 row->full_width_p = 1;
11860 row->continued_p = 0;
11861 row->truncated_on_left_p = 0;
11862 row->truncated_on_right_p = 0;
11863
11864 it->current_x = it->hpos = 0;
11865 it->current_y += row->height;
11866 ++it->vpos;
11867 ++it->glyph_row;
11868 }
11869
11870
11871 /* Max tool-bar height. */
11872
11873 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11874 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11875
11876 /* Value is the number of screen lines needed to make all tool-bar
11877 items of frame F visible. The number of actual rows needed is
11878 returned in *N_ROWS if non-NULL. */
11879
11880 static int
11881 tool_bar_lines_needed (struct frame *f, int *n_rows)
11882 {
11883 struct window *w = XWINDOW (f->tool_bar_window);
11884 struct it it;
11885 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11886 the desired matrix, so use (unused) mode-line row as temporary row to
11887 avoid destroying the first tool-bar row. */
11888 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11889
11890 /* Initialize an iterator for iteration over
11891 F->desired_tool_bar_string in the tool-bar window of frame F. */
11892 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11893 it.first_visible_x = 0;
11894 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11895 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11896 it.paragraph_embedding = L2R;
11897
11898 while (!ITERATOR_AT_END_P (&it))
11899 {
11900 clear_glyph_row (temp_row);
11901 it.glyph_row = temp_row;
11902 display_tool_bar_line (&it, -1);
11903 }
11904 clear_glyph_row (temp_row);
11905
11906 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11907 if (n_rows)
11908 *n_rows = it.vpos > 0 ? it.vpos : -1;
11909
11910 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11911 }
11912
11913 #endif /* !USE_GTK && !HAVE_NS */
11914
11915 #if defined USE_GTK || defined HAVE_NS
11916 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
11917 #endif
11918
11919 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11920 0, 1, 0,
11921 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11922 If FRAME is nil or omitted, use the selected frame. */)
11923 (Lisp_Object frame)
11924 {
11925 int nlines = 0;
11926 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11927 struct frame *f = decode_any_frame (frame);
11928 struct window *w;
11929
11930 if (WINDOWP (f->tool_bar_window)
11931 && (w = XWINDOW (f->tool_bar_window),
11932 WINDOW_TOTAL_LINES (w) > 0))
11933 {
11934 update_tool_bar (f, 1);
11935 if (f->n_tool_bar_items)
11936 {
11937 build_desired_tool_bar_string (f);
11938 nlines = tool_bar_lines_needed (f, NULL);
11939 }
11940 }
11941 #endif
11942 return make_number (nlines);
11943 }
11944
11945
11946 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11947 height should be changed. */
11948
11949 static int
11950 redisplay_tool_bar (struct frame *f)
11951 {
11952 #if defined (USE_GTK) || defined (HAVE_NS)
11953
11954 if (FRAME_EXTERNAL_TOOL_BAR (f))
11955 update_frame_tool_bar (f);
11956 return 0;
11957
11958 #else /* !USE_GTK && !HAVE_NS */
11959
11960 struct window *w;
11961 struct it it;
11962 struct glyph_row *row;
11963
11964 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11965 do anything. This means you must start with tool-bar-lines
11966 non-zero to get the auto-sizing effect. Or in other words, you
11967 can turn off tool-bars by specifying tool-bar-lines zero. */
11968 if (!WINDOWP (f->tool_bar_window)
11969 || (w = XWINDOW (f->tool_bar_window),
11970 WINDOW_TOTAL_LINES (w) == 0))
11971 return 0;
11972
11973 /* Set up an iterator for the tool-bar window. */
11974 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11975 it.first_visible_x = 0;
11976 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11977 row = it.glyph_row;
11978
11979 /* Build a string that represents the contents of the tool-bar. */
11980 build_desired_tool_bar_string (f);
11981 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11982 /* FIXME: This should be controlled by a user option. But it
11983 doesn't make sense to have an R2L tool bar if the menu bar cannot
11984 be drawn also R2L, and making the menu bar R2L is tricky due
11985 toolkit-specific code that implements it. If an R2L tool bar is
11986 ever supported, display_tool_bar_line should also be augmented to
11987 call unproduce_glyphs like display_line and display_string
11988 do. */
11989 it.paragraph_embedding = L2R;
11990
11991 if (f->n_tool_bar_rows == 0)
11992 {
11993 int nlines;
11994
11995 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11996 nlines != WINDOW_TOTAL_LINES (w)))
11997 {
11998 Lisp_Object frame;
11999 int old_height = WINDOW_TOTAL_LINES (w);
12000
12001 XSETFRAME (frame, f);
12002 Fmodify_frame_parameters (frame,
12003 list1 (Fcons (Qtool_bar_lines,
12004 make_number (nlines))));
12005 if (WINDOW_TOTAL_LINES (w) != old_height)
12006 {
12007 clear_glyph_matrix (w->desired_matrix);
12008 f->fonts_changed = 1;
12009 return 1;
12010 }
12011 }
12012 }
12013
12014 /* Display as many lines as needed to display all tool-bar items. */
12015
12016 if (f->n_tool_bar_rows > 0)
12017 {
12018 int border, rows, height, extra;
12019
12020 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12021 border = XINT (Vtool_bar_border);
12022 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12023 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12024 else if (EQ (Vtool_bar_border, Qborder_width))
12025 border = f->border_width;
12026 else
12027 border = 0;
12028 if (border < 0)
12029 border = 0;
12030
12031 rows = f->n_tool_bar_rows;
12032 height = max (1, (it.last_visible_y - border) / rows);
12033 extra = it.last_visible_y - border - height * rows;
12034
12035 while (it.current_y < it.last_visible_y)
12036 {
12037 int h = 0;
12038 if (extra > 0 && rows-- > 0)
12039 {
12040 h = (extra + rows - 1) / rows;
12041 extra -= h;
12042 }
12043 display_tool_bar_line (&it, height + h);
12044 }
12045 }
12046 else
12047 {
12048 while (it.current_y < it.last_visible_y)
12049 display_tool_bar_line (&it, 0);
12050 }
12051
12052 /* It doesn't make much sense to try scrolling in the tool-bar
12053 window, so don't do it. */
12054 w->desired_matrix->no_scrolling_p = 1;
12055 w->must_be_updated_p = 1;
12056
12057 if (!NILP (Vauto_resize_tool_bars))
12058 {
12059 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12060 int change_height_p = 0;
12061
12062 /* If we couldn't display everything, change the tool-bar's
12063 height if there is room for more. */
12064 if (IT_STRING_CHARPOS (it) < it.end_charpos
12065 && it.current_y < max_tool_bar_height)
12066 change_height_p = 1;
12067
12068 row = it.glyph_row - 1;
12069
12070 /* If there are blank lines at the end, except for a partially
12071 visible blank line at the end that is smaller than
12072 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12073 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12074 && row->height >= FRAME_LINE_HEIGHT (f))
12075 change_height_p = 1;
12076
12077 /* If row displays tool-bar items, but is partially visible,
12078 change the tool-bar's height. */
12079 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12080 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12081 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12082 change_height_p = 1;
12083
12084 /* Resize windows as needed by changing the `tool-bar-lines'
12085 frame parameter. */
12086 if (change_height_p)
12087 {
12088 Lisp_Object frame;
12089 int old_height = WINDOW_TOTAL_LINES (w);
12090 int nrows;
12091 int nlines = tool_bar_lines_needed (f, &nrows);
12092
12093 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12094 && !f->minimize_tool_bar_window_p)
12095 ? (nlines > old_height)
12096 : (nlines != old_height));
12097 f->minimize_tool_bar_window_p = 0;
12098
12099 if (change_height_p)
12100 {
12101 XSETFRAME (frame, f);
12102 Fmodify_frame_parameters (frame,
12103 list1 (Fcons (Qtool_bar_lines,
12104 make_number (nlines))));
12105 if (WINDOW_TOTAL_LINES (w) != old_height)
12106 {
12107 clear_glyph_matrix (w->desired_matrix);
12108 f->n_tool_bar_rows = nrows;
12109 f->fonts_changed = 1;
12110 return 1;
12111 }
12112 }
12113 }
12114 }
12115
12116 f->minimize_tool_bar_window_p = 0;
12117 return 0;
12118
12119 #endif /* USE_GTK || HAVE_NS */
12120 }
12121
12122 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12123
12124 /* Get information about the tool-bar item which is displayed in GLYPH
12125 on frame F. Return in *PROP_IDX the index where tool-bar item
12126 properties start in F->tool_bar_items. Value is zero if
12127 GLYPH doesn't display a tool-bar item. */
12128
12129 static int
12130 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12131 {
12132 Lisp_Object prop;
12133 int success_p;
12134 int charpos;
12135
12136 /* This function can be called asynchronously, which means we must
12137 exclude any possibility that Fget_text_property signals an
12138 error. */
12139 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12140 charpos = max (0, charpos);
12141
12142 /* Get the text property `menu-item' at pos. The value of that
12143 property is the start index of this item's properties in
12144 F->tool_bar_items. */
12145 prop = Fget_text_property (make_number (charpos),
12146 Qmenu_item, f->current_tool_bar_string);
12147 if (INTEGERP (prop))
12148 {
12149 *prop_idx = XINT (prop);
12150 success_p = 1;
12151 }
12152 else
12153 success_p = 0;
12154
12155 return success_p;
12156 }
12157
12158 \f
12159 /* Get information about the tool-bar item at position X/Y on frame F.
12160 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12161 the current matrix of the tool-bar window of F, or NULL if not
12162 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12163 item in F->tool_bar_items. Value is
12164
12165 -1 if X/Y is not on a tool-bar item
12166 0 if X/Y is on the same item that was highlighted before.
12167 1 otherwise. */
12168
12169 static int
12170 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12171 int *hpos, int *vpos, int *prop_idx)
12172 {
12173 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12174 struct window *w = XWINDOW (f->tool_bar_window);
12175 int area;
12176
12177 /* Find the glyph under X/Y. */
12178 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12179 if (*glyph == NULL)
12180 return -1;
12181
12182 /* Get the start of this tool-bar item's properties in
12183 f->tool_bar_items. */
12184 if (!tool_bar_item_info (f, *glyph, prop_idx))
12185 return -1;
12186
12187 /* Is mouse on the highlighted item? */
12188 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12189 && *vpos >= hlinfo->mouse_face_beg_row
12190 && *vpos <= hlinfo->mouse_face_end_row
12191 && (*vpos > hlinfo->mouse_face_beg_row
12192 || *hpos >= hlinfo->mouse_face_beg_col)
12193 && (*vpos < hlinfo->mouse_face_end_row
12194 || *hpos < hlinfo->mouse_face_end_col
12195 || hlinfo->mouse_face_past_end))
12196 return 0;
12197
12198 return 1;
12199 }
12200
12201
12202 /* EXPORT:
12203 Handle mouse button event on the tool-bar of frame F, at
12204 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12205 0 for button release. MODIFIERS is event modifiers for button
12206 release. */
12207
12208 void
12209 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12210 int modifiers)
12211 {
12212 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12213 struct window *w = XWINDOW (f->tool_bar_window);
12214 int hpos, vpos, prop_idx;
12215 struct glyph *glyph;
12216 Lisp_Object enabled_p;
12217 int ts;
12218
12219 /* If not on the highlighted tool-bar item, and mouse-highlight is
12220 non-nil, return. This is so we generate the tool-bar button
12221 click only when the mouse button is released on the same item as
12222 where it was pressed. However, when mouse-highlight is disabled,
12223 generate the click when the button is released regardless of the
12224 highlight, since tool-bar items are not highlighted in that
12225 case. */
12226 frame_to_window_pixel_xy (w, &x, &y);
12227 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12228 if (ts == -1
12229 || (ts != 0 && !NILP (Vmouse_highlight)))
12230 return;
12231
12232 /* When mouse-highlight is off, generate the click for the item
12233 where the button was pressed, disregarding where it was
12234 released. */
12235 if (NILP (Vmouse_highlight) && !down_p)
12236 prop_idx = last_tool_bar_item;
12237
12238 /* If item is disabled, do nothing. */
12239 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12240 if (NILP (enabled_p))
12241 return;
12242
12243 if (down_p)
12244 {
12245 /* Show item in pressed state. */
12246 if (!NILP (Vmouse_highlight))
12247 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12248 last_tool_bar_item = prop_idx;
12249 }
12250 else
12251 {
12252 Lisp_Object key, frame;
12253 struct input_event event;
12254 EVENT_INIT (event);
12255
12256 /* Show item in released state. */
12257 if (!NILP (Vmouse_highlight))
12258 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12259
12260 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12261
12262 XSETFRAME (frame, f);
12263 event.kind = TOOL_BAR_EVENT;
12264 event.frame_or_window = frame;
12265 event.arg = frame;
12266 kbd_buffer_store_event (&event);
12267
12268 event.kind = TOOL_BAR_EVENT;
12269 event.frame_or_window = frame;
12270 event.arg = key;
12271 event.modifiers = modifiers;
12272 kbd_buffer_store_event (&event);
12273 last_tool_bar_item = -1;
12274 }
12275 }
12276
12277
12278 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12279 tool-bar window-relative coordinates X/Y. Called from
12280 note_mouse_highlight. */
12281
12282 static void
12283 note_tool_bar_highlight (struct frame *f, int x, int y)
12284 {
12285 Lisp_Object window = f->tool_bar_window;
12286 struct window *w = XWINDOW (window);
12287 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12288 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12289 int hpos, vpos;
12290 struct glyph *glyph;
12291 struct glyph_row *row;
12292 int i;
12293 Lisp_Object enabled_p;
12294 int prop_idx;
12295 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12296 int mouse_down_p, rc;
12297
12298 /* Function note_mouse_highlight is called with negative X/Y
12299 values when mouse moves outside of the frame. */
12300 if (x <= 0 || y <= 0)
12301 {
12302 clear_mouse_face (hlinfo);
12303 return;
12304 }
12305
12306 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12307 if (rc < 0)
12308 {
12309 /* Not on tool-bar item. */
12310 clear_mouse_face (hlinfo);
12311 return;
12312 }
12313 else if (rc == 0)
12314 /* On same tool-bar item as before. */
12315 goto set_help_echo;
12316
12317 clear_mouse_face (hlinfo);
12318
12319 /* Mouse is down, but on different tool-bar item? */
12320 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12321 && f == dpyinfo->last_mouse_frame);
12322
12323 if (mouse_down_p
12324 && last_tool_bar_item != prop_idx)
12325 return;
12326
12327 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12328
12329 /* If tool-bar item is not enabled, don't highlight it. */
12330 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12331 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12332 {
12333 /* Compute the x-position of the glyph. In front and past the
12334 image is a space. We include this in the highlighted area. */
12335 row = MATRIX_ROW (w->current_matrix, vpos);
12336 for (i = x = 0; i < hpos; ++i)
12337 x += row->glyphs[TEXT_AREA][i].pixel_width;
12338
12339 /* Record this as the current active region. */
12340 hlinfo->mouse_face_beg_col = hpos;
12341 hlinfo->mouse_face_beg_row = vpos;
12342 hlinfo->mouse_face_beg_x = x;
12343 hlinfo->mouse_face_past_end = 0;
12344
12345 hlinfo->mouse_face_end_col = hpos + 1;
12346 hlinfo->mouse_face_end_row = vpos;
12347 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12348 hlinfo->mouse_face_window = window;
12349 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12350
12351 /* Display it as active. */
12352 show_mouse_face (hlinfo, draw);
12353 }
12354
12355 set_help_echo:
12356
12357 /* Set help_echo_string to a help string to display for this tool-bar item.
12358 XTread_socket does the rest. */
12359 help_echo_object = help_echo_window = Qnil;
12360 help_echo_pos = -1;
12361 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12362 if (NILP (help_echo_string))
12363 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12364 }
12365
12366 #endif /* !USE_GTK && !HAVE_NS */
12367
12368 #endif /* HAVE_WINDOW_SYSTEM */
12369
12370
12371 \f
12372 /************************************************************************
12373 Horizontal scrolling
12374 ************************************************************************/
12375
12376 static int hscroll_window_tree (Lisp_Object);
12377 static int hscroll_windows (Lisp_Object);
12378
12379 /* For all leaf windows in the window tree rooted at WINDOW, set their
12380 hscroll value so that PT is (i) visible in the window, and (ii) so
12381 that it is not within a certain margin at the window's left and
12382 right border. Value is non-zero if any window's hscroll has been
12383 changed. */
12384
12385 static int
12386 hscroll_window_tree (Lisp_Object window)
12387 {
12388 int hscrolled_p = 0;
12389 int hscroll_relative_p = FLOATP (Vhscroll_step);
12390 int hscroll_step_abs = 0;
12391 double hscroll_step_rel = 0;
12392
12393 if (hscroll_relative_p)
12394 {
12395 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12396 if (hscroll_step_rel < 0)
12397 {
12398 hscroll_relative_p = 0;
12399 hscroll_step_abs = 0;
12400 }
12401 }
12402 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12403 {
12404 hscroll_step_abs = XINT (Vhscroll_step);
12405 if (hscroll_step_abs < 0)
12406 hscroll_step_abs = 0;
12407 }
12408 else
12409 hscroll_step_abs = 0;
12410
12411 while (WINDOWP (window))
12412 {
12413 struct window *w = XWINDOW (window);
12414
12415 if (WINDOWP (w->contents))
12416 hscrolled_p |= hscroll_window_tree (w->contents);
12417 else if (w->cursor.vpos >= 0)
12418 {
12419 int h_margin;
12420 int text_area_width;
12421 struct glyph_row *current_cursor_row
12422 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12423 struct glyph_row *desired_cursor_row
12424 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12425 struct glyph_row *cursor_row
12426 = (desired_cursor_row->enabled_p
12427 ? desired_cursor_row
12428 : current_cursor_row);
12429 int row_r2l_p = cursor_row->reversed_p;
12430
12431 text_area_width = window_box_width (w, TEXT_AREA);
12432
12433 /* Scroll when cursor is inside this scroll margin. */
12434 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12435
12436 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12437 /* For left-to-right rows, hscroll when cursor is either
12438 (i) inside the right hscroll margin, or (ii) if it is
12439 inside the left margin and the window is already
12440 hscrolled. */
12441 && ((!row_r2l_p
12442 && ((w->hscroll
12443 && w->cursor.x <= h_margin)
12444 || (cursor_row->enabled_p
12445 && cursor_row->truncated_on_right_p
12446 && (w->cursor.x >= text_area_width - h_margin))))
12447 /* For right-to-left rows, the logic is similar,
12448 except that rules for scrolling to left and right
12449 are reversed. E.g., if cursor.x <= h_margin, we
12450 need to hscroll "to the right" unconditionally,
12451 and that will scroll the screen to the left so as
12452 to reveal the next portion of the row. */
12453 || (row_r2l_p
12454 && ((cursor_row->enabled_p
12455 /* FIXME: It is confusing to set the
12456 truncated_on_right_p flag when R2L rows
12457 are actually truncated on the left. */
12458 && cursor_row->truncated_on_right_p
12459 && w->cursor.x <= h_margin)
12460 || (w->hscroll
12461 && (w->cursor.x >= text_area_width - h_margin))))))
12462 {
12463 struct it it;
12464 ptrdiff_t hscroll;
12465 struct buffer *saved_current_buffer;
12466 ptrdiff_t pt;
12467 int wanted_x;
12468
12469 /* Find point in a display of infinite width. */
12470 saved_current_buffer = current_buffer;
12471 current_buffer = XBUFFER (w->contents);
12472
12473 if (w == XWINDOW (selected_window))
12474 pt = PT;
12475 else
12476 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12477
12478 /* Move iterator to pt starting at cursor_row->start in
12479 a line with infinite width. */
12480 init_to_row_start (&it, w, cursor_row);
12481 it.last_visible_x = INFINITY;
12482 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12483 current_buffer = saved_current_buffer;
12484
12485 /* Position cursor in window. */
12486 if (!hscroll_relative_p && hscroll_step_abs == 0)
12487 hscroll = max (0, (it.current_x
12488 - (ITERATOR_AT_END_OF_LINE_P (&it)
12489 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12490 : (text_area_width / 2))))
12491 / FRAME_COLUMN_WIDTH (it.f);
12492 else if ((!row_r2l_p
12493 && w->cursor.x >= text_area_width - h_margin)
12494 || (row_r2l_p && w->cursor.x <= h_margin))
12495 {
12496 if (hscroll_relative_p)
12497 wanted_x = text_area_width * (1 - hscroll_step_rel)
12498 - h_margin;
12499 else
12500 wanted_x = text_area_width
12501 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12502 - h_margin;
12503 hscroll
12504 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12505 }
12506 else
12507 {
12508 if (hscroll_relative_p)
12509 wanted_x = text_area_width * hscroll_step_rel
12510 + h_margin;
12511 else
12512 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12513 + h_margin;
12514 hscroll
12515 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12516 }
12517 hscroll = max (hscroll, w->min_hscroll);
12518
12519 /* Don't prevent redisplay optimizations if hscroll
12520 hasn't changed, as it will unnecessarily slow down
12521 redisplay. */
12522 if (w->hscroll != hscroll)
12523 {
12524 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12525 w->hscroll = hscroll;
12526 hscrolled_p = 1;
12527 }
12528 }
12529 }
12530
12531 window = w->next;
12532 }
12533
12534 /* Value is non-zero if hscroll of any leaf window has been changed. */
12535 return hscrolled_p;
12536 }
12537
12538
12539 /* Set hscroll so that cursor is visible and not inside horizontal
12540 scroll margins for all windows in the tree rooted at WINDOW. See
12541 also hscroll_window_tree above. Value is non-zero if any window's
12542 hscroll has been changed. If it has, desired matrices on the frame
12543 of WINDOW are cleared. */
12544
12545 static int
12546 hscroll_windows (Lisp_Object window)
12547 {
12548 int hscrolled_p = hscroll_window_tree (window);
12549 if (hscrolled_p)
12550 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12551 return hscrolled_p;
12552 }
12553
12554
12555 \f
12556 /************************************************************************
12557 Redisplay
12558 ************************************************************************/
12559
12560 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12561 to a non-zero value. This is sometimes handy to have in a debugger
12562 session. */
12563
12564 #ifdef GLYPH_DEBUG
12565
12566 /* First and last unchanged row for try_window_id. */
12567
12568 static int debug_first_unchanged_at_end_vpos;
12569 static int debug_last_unchanged_at_beg_vpos;
12570
12571 /* Delta vpos and y. */
12572
12573 static int debug_dvpos, debug_dy;
12574
12575 /* Delta in characters and bytes for try_window_id. */
12576
12577 static ptrdiff_t debug_delta, debug_delta_bytes;
12578
12579 /* Values of window_end_pos and window_end_vpos at the end of
12580 try_window_id. */
12581
12582 static ptrdiff_t debug_end_vpos;
12583
12584 /* Append a string to W->desired_matrix->method. FMT is a printf
12585 format string. If trace_redisplay_p is non-zero also printf the
12586 resulting string to stderr. */
12587
12588 static void debug_method_add (struct window *, char const *, ...)
12589 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12590
12591 static void
12592 debug_method_add (struct window *w, char const *fmt, ...)
12593 {
12594 void *ptr = w;
12595 char *method = w->desired_matrix->method;
12596 int len = strlen (method);
12597 int size = sizeof w->desired_matrix->method;
12598 int remaining = size - len - 1;
12599 va_list ap;
12600
12601 if (len && remaining)
12602 {
12603 method[len] = '|';
12604 --remaining, ++len;
12605 }
12606
12607 va_start (ap, fmt);
12608 vsnprintf (method + len, remaining + 1, fmt, ap);
12609 va_end (ap);
12610
12611 if (trace_redisplay_p)
12612 fprintf (stderr, "%p (%s): %s\n",
12613 ptr,
12614 ((BUFFERP (w->contents)
12615 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12616 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12617 : "no buffer"),
12618 method + len);
12619 }
12620
12621 #endif /* GLYPH_DEBUG */
12622
12623
12624 /* Value is non-zero if all changes in window W, which displays
12625 current_buffer, are in the text between START and END. START is a
12626 buffer position, END is given as a distance from Z. Used in
12627 redisplay_internal for display optimization. */
12628
12629 static int
12630 text_outside_line_unchanged_p (struct window *w,
12631 ptrdiff_t start, ptrdiff_t end)
12632 {
12633 int unchanged_p = 1;
12634
12635 /* If text or overlays have changed, see where. */
12636 if (window_outdated (w))
12637 {
12638 /* Gap in the line? */
12639 if (GPT < start || Z - GPT < end)
12640 unchanged_p = 0;
12641
12642 /* Changes start in front of the line, or end after it? */
12643 if (unchanged_p
12644 && (BEG_UNCHANGED < start - 1
12645 || END_UNCHANGED < end))
12646 unchanged_p = 0;
12647
12648 /* If selective display, can't optimize if changes start at the
12649 beginning of the line. */
12650 if (unchanged_p
12651 && INTEGERP (BVAR (current_buffer, selective_display))
12652 && XINT (BVAR (current_buffer, selective_display)) > 0
12653 && (BEG_UNCHANGED < start || GPT <= start))
12654 unchanged_p = 0;
12655
12656 /* If there are overlays at the start or end of the line, these
12657 may have overlay strings with newlines in them. A change at
12658 START, for instance, may actually concern the display of such
12659 overlay strings as well, and they are displayed on different
12660 lines. So, quickly rule out this case. (For the future, it
12661 might be desirable to implement something more telling than
12662 just BEG/END_UNCHANGED.) */
12663 if (unchanged_p)
12664 {
12665 if (BEG + BEG_UNCHANGED == start
12666 && overlay_touches_p (start))
12667 unchanged_p = 0;
12668 if (END_UNCHANGED == end
12669 && overlay_touches_p (Z - end))
12670 unchanged_p = 0;
12671 }
12672
12673 /* Under bidi reordering, adding or deleting a character in the
12674 beginning of a paragraph, before the first strong directional
12675 character, can change the base direction of the paragraph (unless
12676 the buffer specifies a fixed paragraph direction), which will
12677 require to redisplay the whole paragraph. It might be worthwhile
12678 to find the paragraph limits and widen the range of redisplayed
12679 lines to that, but for now just give up this optimization. */
12680 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12681 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12682 unchanged_p = 0;
12683 }
12684
12685 return unchanged_p;
12686 }
12687
12688
12689 /* Do a frame update, taking possible shortcuts into account. This is
12690 the main external entry point for redisplay.
12691
12692 If the last redisplay displayed an echo area message and that message
12693 is no longer requested, we clear the echo area or bring back the
12694 mini-buffer if that is in use. */
12695
12696 void
12697 redisplay (void)
12698 {
12699 redisplay_internal ();
12700 }
12701
12702
12703 static Lisp_Object
12704 overlay_arrow_string_or_property (Lisp_Object var)
12705 {
12706 Lisp_Object val;
12707
12708 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12709 return val;
12710
12711 return Voverlay_arrow_string;
12712 }
12713
12714 /* Return 1 if there are any overlay-arrows in current_buffer. */
12715 static int
12716 overlay_arrow_in_current_buffer_p (void)
12717 {
12718 Lisp_Object vlist;
12719
12720 for (vlist = Voverlay_arrow_variable_list;
12721 CONSP (vlist);
12722 vlist = XCDR (vlist))
12723 {
12724 Lisp_Object var = XCAR (vlist);
12725 Lisp_Object val;
12726
12727 if (!SYMBOLP (var))
12728 continue;
12729 val = find_symbol_value (var);
12730 if (MARKERP (val)
12731 && current_buffer == XMARKER (val)->buffer)
12732 return 1;
12733 }
12734 return 0;
12735 }
12736
12737
12738 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12739 has changed. */
12740
12741 static int
12742 overlay_arrows_changed_p (void)
12743 {
12744 Lisp_Object vlist;
12745
12746 for (vlist = Voverlay_arrow_variable_list;
12747 CONSP (vlist);
12748 vlist = XCDR (vlist))
12749 {
12750 Lisp_Object var = XCAR (vlist);
12751 Lisp_Object val, pstr;
12752
12753 if (!SYMBOLP (var))
12754 continue;
12755 val = find_symbol_value (var);
12756 if (!MARKERP (val))
12757 continue;
12758 if (! EQ (COERCE_MARKER (val),
12759 Fget (var, Qlast_arrow_position))
12760 || ! (pstr = overlay_arrow_string_or_property (var),
12761 EQ (pstr, Fget (var, Qlast_arrow_string))))
12762 return 1;
12763 }
12764 return 0;
12765 }
12766
12767 /* Mark overlay arrows to be updated on next redisplay. */
12768
12769 static void
12770 update_overlay_arrows (int up_to_date)
12771 {
12772 Lisp_Object vlist;
12773
12774 for (vlist = Voverlay_arrow_variable_list;
12775 CONSP (vlist);
12776 vlist = XCDR (vlist))
12777 {
12778 Lisp_Object var = XCAR (vlist);
12779
12780 if (!SYMBOLP (var))
12781 continue;
12782
12783 if (up_to_date > 0)
12784 {
12785 Lisp_Object val = find_symbol_value (var);
12786 Fput (var, Qlast_arrow_position,
12787 COERCE_MARKER (val));
12788 Fput (var, Qlast_arrow_string,
12789 overlay_arrow_string_or_property (var));
12790 }
12791 else if (up_to_date < 0
12792 || !NILP (Fget (var, Qlast_arrow_position)))
12793 {
12794 Fput (var, Qlast_arrow_position, Qt);
12795 Fput (var, Qlast_arrow_string, Qt);
12796 }
12797 }
12798 }
12799
12800
12801 /* Return overlay arrow string to display at row.
12802 Return integer (bitmap number) for arrow bitmap in left fringe.
12803 Return nil if no overlay arrow. */
12804
12805 static Lisp_Object
12806 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12807 {
12808 Lisp_Object vlist;
12809
12810 for (vlist = Voverlay_arrow_variable_list;
12811 CONSP (vlist);
12812 vlist = XCDR (vlist))
12813 {
12814 Lisp_Object var = XCAR (vlist);
12815 Lisp_Object val;
12816
12817 if (!SYMBOLP (var))
12818 continue;
12819
12820 val = find_symbol_value (var);
12821
12822 if (MARKERP (val)
12823 && current_buffer == XMARKER (val)->buffer
12824 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12825 {
12826 if (FRAME_WINDOW_P (it->f)
12827 /* FIXME: if ROW->reversed_p is set, this should test
12828 the right fringe, not the left one. */
12829 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12830 {
12831 #ifdef HAVE_WINDOW_SYSTEM
12832 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12833 {
12834 int fringe_bitmap;
12835 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12836 return make_number (fringe_bitmap);
12837 }
12838 #endif
12839 return make_number (-1); /* Use default arrow bitmap. */
12840 }
12841 return overlay_arrow_string_or_property (var);
12842 }
12843 }
12844
12845 return Qnil;
12846 }
12847
12848 /* Return 1 if point moved out of or into a composition. Otherwise
12849 return 0. PREV_BUF and PREV_PT are the last point buffer and
12850 position. BUF and PT are the current point buffer and position. */
12851
12852 static int
12853 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12854 struct buffer *buf, ptrdiff_t pt)
12855 {
12856 ptrdiff_t start, end;
12857 Lisp_Object prop;
12858 Lisp_Object buffer;
12859
12860 XSETBUFFER (buffer, buf);
12861 /* Check a composition at the last point if point moved within the
12862 same buffer. */
12863 if (prev_buf == buf)
12864 {
12865 if (prev_pt == pt)
12866 /* Point didn't move. */
12867 return 0;
12868
12869 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12870 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12871 && composition_valid_p (start, end, prop)
12872 && start < prev_pt && end > prev_pt)
12873 /* The last point was within the composition. Return 1 iff
12874 point moved out of the composition. */
12875 return (pt <= start || pt >= end);
12876 }
12877
12878 /* Check a composition at the current point. */
12879 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12880 && find_composition (pt, -1, &start, &end, &prop, buffer)
12881 && composition_valid_p (start, end, prop)
12882 && start < pt && end > pt);
12883 }
12884
12885 /* Reconsider the clip changes of buffer which is displayed in W. */
12886
12887 static void
12888 reconsider_clip_changes (struct window *w)
12889 {
12890 struct buffer *b = XBUFFER (w->contents);
12891
12892 if (b->clip_changed
12893 && w->window_end_valid
12894 && w->current_matrix->buffer == b
12895 && w->current_matrix->zv == BUF_ZV (b)
12896 && w->current_matrix->begv == BUF_BEGV (b))
12897 b->clip_changed = 0;
12898
12899 /* If display wasn't paused, and W is not a tool bar window, see if
12900 point has been moved into or out of a composition. In that case,
12901 we set b->clip_changed to 1 to force updating the screen. If
12902 b->clip_changed has already been set to 1, we can skip this
12903 check. */
12904 if (!b->clip_changed && w->window_end_valid)
12905 {
12906 ptrdiff_t pt = (w == XWINDOW (selected_window)
12907 ? PT : marker_position (w->pointm));
12908
12909 if ((w->current_matrix->buffer != b || pt != w->last_point)
12910 && check_point_in_composition (w->current_matrix->buffer,
12911 w->last_point, b, pt))
12912 b->clip_changed = 1;
12913 }
12914 }
12915
12916 #define STOP_POLLING \
12917 do { if (! polling_stopped_here) stop_polling (); \
12918 polling_stopped_here = 1; } while (0)
12919
12920 #define RESUME_POLLING \
12921 do { if (polling_stopped_here) start_polling (); \
12922 polling_stopped_here = 0; } while (0)
12923
12924
12925 /* Perhaps in the future avoid recentering windows if it
12926 is not necessary; currently that causes some problems. */
12927
12928 static void
12929 redisplay_internal (void)
12930 {
12931 struct window *w = XWINDOW (selected_window);
12932 struct window *sw;
12933 struct frame *fr;
12934 int pending;
12935 bool must_finish = 0, match_p;
12936 struct text_pos tlbufpos, tlendpos;
12937 int number_of_visible_frames;
12938 ptrdiff_t count;
12939 struct frame *sf;
12940 int polling_stopped_here = 0;
12941 Lisp_Object tail, frame;
12942
12943 /* Non-zero means redisplay has to consider all windows on all
12944 frames. Zero means, only selected_window is considered. */
12945 int consider_all_windows_p;
12946
12947 /* Non-zero means redisplay has to redisplay the miniwindow. */
12948 int update_miniwindow_p = 0;
12949
12950 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12951
12952 /* No redisplay if running in batch mode or frame is not yet fully
12953 initialized, or redisplay is explicitly turned off by setting
12954 Vinhibit_redisplay. */
12955 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12956 || !NILP (Vinhibit_redisplay))
12957 return;
12958
12959 /* Don't examine these until after testing Vinhibit_redisplay.
12960 When Emacs is shutting down, perhaps because its connection to
12961 X has dropped, we should not look at them at all. */
12962 fr = XFRAME (w->frame);
12963 sf = SELECTED_FRAME ();
12964
12965 if (!fr->glyphs_initialized_p)
12966 return;
12967
12968 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12969 if (popup_activated ())
12970 return;
12971 #endif
12972
12973 /* I don't think this happens but let's be paranoid. */
12974 if (redisplaying_p)
12975 return;
12976
12977 /* Record a function that clears redisplaying_p
12978 when we leave this function. */
12979 count = SPECPDL_INDEX ();
12980 record_unwind_protect_void (unwind_redisplay);
12981 redisplaying_p = 1;
12982 specbind (Qinhibit_free_realized_faces, Qnil);
12983
12984 /* Record this function, so it appears on the profiler's backtraces. */
12985 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12986
12987 FOR_EACH_FRAME (tail, frame)
12988 XFRAME (frame)->already_hscrolled_p = 0;
12989
12990 retry:
12991 /* Remember the currently selected window. */
12992 sw = w;
12993
12994 pending = 0;
12995 last_escape_glyph_frame = NULL;
12996 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12997 last_glyphless_glyph_frame = NULL;
12998 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12999
13000 /* If face_change_count is non-zero, init_iterator will free all
13001 realized faces, which includes the faces referenced from current
13002 matrices. So, we can't reuse current matrices in this case. */
13003 if (face_change_count)
13004 ++windows_or_buffers_changed;
13005
13006 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13007 && FRAME_TTY (sf)->previous_frame != sf)
13008 {
13009 /* Since frames on a single ASCII terminal share the same
13010 display area, displaying a different frame means redisplay
13011 the whole thing. */
13012 windows_or_buffers_changed++;
13013 SET_FRAME_GARBAGED (sf);
13014 #ifndef DOS_NT
13015 set_tty_color_mode (FRAME_TTY (sf), sf);
13016 #endif
13017 FRAME_TTY (sf)->previous_frame = sf;
13018 }
13019
13020 /* Set the visible flags for all frames. Do this before checking for
13021 resized or garbaged frames; they want to know if their frames are
13022 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13023 number_of_visible_frames = 0;
13024
13025 FOR_EACH_FRAME (tail, frame)
13026 {
13027 struct frame *f = XFRAME (frame);
13028
13029 if (FRAME_VISIBLE_P (f))
13030 {
13031 ++number_of_visible_frames;
13032 /* Adjust matrices for visible frames only. */
13033 if (f->fonts_changed)
13034 {
13035 adjust_frame_glyphs (f);
13036 f->fonts_changed = 0;
13037 }
13038 /* If cursor type has been changed on the frame
13039 other than selected, consider all frames. */
13040 if (f != sf && f->cursor_type_changed)
13041 update_mode_lines++;
13042 }
13043 clear_desired_matrices (f);
13044 }
13045
13046 /* Notice any pending interrupt request to change frame size. */
13047 do_pending_window_change (1);
13048
13049 /* do_pending_window_change could change the selected_window due to
13050 frame resizing which makes the selected window too small. */
13051 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13052 sw = w;
13053
13054 /* Clear frames marked as garbaged. */
13055 clear_garbaged_frames ();
13056
13057 /* Build menubar and tool-bar items. */
13058 if (NILP (Vmemory_full))
13059 prepare_menu_bars ();
13060
13061 if (windows_or_buffers_changed)
13062 update_mode_lines++;
13063
13064 reconsider_clip_changes (w);
13065
13066 /* In most cases selected window displays current buffer. */
13067 match_p = XBUFFER (w->contents) == current_buffer;
13068 if (match_p)
13069 {
13070 /* Detect case that we need to write or remove a star in the mode line. */
13071 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13072 {
13073 w->update_mode_line = 1;
13074 if (buffer_shared_and_changed ())
13075 update_mode_lines++;
13076 }
13077
13078 if (mode_line_update_needed (w))
13079 w->update_mode_line = 1;
13080 }
13081
13082 consider_all_windows_p = (update_mode_lines
13083 || buffer_shared_and_changed ());
13084
13085 /* If specs for an arrow have changed, do thorough redisplay
13086 to ensure we remove any arrow that should no longer exist. */
13087 if (overlay_arrows_changed_p ())
13088 consider_all_windows_p = windows_or_buffers_changed = 1;
13089
13090 /* Normally the message* functions will have already displayed and
13091 updated the echo area, but the frame may have been trashed, or
13092 the update may have been preempted, so display the echo area
13093 again here. Checking message_cleared_p captures the case that
13094 the echo area should be cleared. */
13095 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13096 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13097 || (message_cleared_p
13098 && minibuf_level == 0
13099 /* If the mini-window is currently selected, this means the
13100 echo-area doesn't show through. */
13101 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13102 {
13103 int window_height_changed_p = echo_area_display (0);
13104
13105 if (message_cleared_p)
13106 update_miniwindow_p = 1;
13107
13108 must_finish = 1;
13109
13110 /* If we don't display the current message, don't clear the
13111 message_cleared_p flag, because, if we did, we wouldn't clear
13112 the echo area in the next redisplay which doesn't preserve
13113 the echo area. */
13114 if (!display_last_displayed_message_p)
13115 message_cleared_p = 0;
13116
13117 if (window_height_changed_p)
13118 {
13119 consider_all_windows_p = 1;
13120 ++update_mode_lines;
13121 ++windows_or_buffers_changed;
13122
13123 /* If window configuration was changed, frames may have been
13124 marked garbaged. Clear them or we will experience
13125 surprises wrt scrolling. */
13126 clear_garbaged_frames ();
13127 }
13128 }
13129 else if (EQ (selected_window, minibuf_window)
13130 && (current_buffer->clip_changed || window_outdated (w))
13131 && resize_mini_window (w, 0))
13132 {
13133 /* Resized active mini-window to fit the size of what it is
13134 showing if its contents might have changed. */
13135 must_finish = 1;
13136 /* FIXME: this causes all frames to be updated, which seems unnecessary
13137 since only the current frame needs to be considered. This function
13138 needs to be rewritten with two variables, consider_all_windows and
13139 consider_all_frames. */
13140 consider_all_windows_p = 1;
13141 ++windows_or_buffers_changed;
13142 ++update_mode_lines;
13143
13144 /* If window configuration was changed, frames may have been
13145 marked garbaged. Clear them or we will experience
13146 surprises wrt scrolling. */
13147 clear_garbaged_frames ();
13148 }
13149
13150 /* If showing the region, and mark has changed, we must redisplay
13151 the whole window. The assignment to this_line_start_pos prevents
13152 the optimization directly below this if-statement. */
13153 if (((!NILP (Vtransient_mark_mode)
13154 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13155 != (w->region_showing > 0))
13156 || (w->region_showing
13157 && w->region_showing
13158 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13159 CHARPOS (this_line_start_pos) = 0;
13160
13161 /* Optimize the case that only the line containing the cursor in the
13162 selected window has changed. Variables starting with this_ are
13163 set in display_line and record information about the line
13164 containing the cursor. */
13165 tlbufpos = this_line_start_pos;
13166 tlendpos = this_line_end_pos;
13167 if (!consider_all_windows_p
13168 && CHARPOS (tlbufpos) > 0
13169 && !w->update_mode_line
13170 && !current_buffer->clip_changed
13171 && !current_buffer->prevent_redisplay_optimizations_p
13172 && FRAME_VISIBLE_P (XFRAME (w->frame))
13173 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13174 && !XFRAME (w->frame)->cursor_type_changed
13175 /* Make sure recorded data applies to current buffer, etc. */
13176 && this_line_buffer == current_buffer
13177 && match_p
13178 && !w->force_start
13179 && !w->optional_new_start
13180 /* Point must be on the line that we have info recorded about. */
13181 && PT >= CHARPOS (tlbufpos)
13182 && PT <= Z - CHARPOS (tlendpos)
13183 /* All text outside that line, including its final newline,
13184 must be unchanged. */
13185 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13186 CHARPOS (tlendpos)))
13187 {
13188 if (CHARPOS (tlbufpos) > BEGV
13189 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13190 && (CHARPOS (tlbufpos) == ZV
13191 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13192 /* Former continuation line has disappeared by becoming empty. */
13193 goto cancel;
13194 else if (window_outdated (w) || MINI_WINDOW_P (w))
13195 {
13196 /* We have to handle the case of continuation around a
13197 wide-column character (see the comment in indent.c around
13198 line 1340).
13199
13200 For instance, in the following case:
13201
13202 -------- Insert --------
13203 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13204 J_I_ ==> J_I_ `^^' are cursors.
13205 ^^ ^^
13206 -------- --------
13207
13208 As we have to redraw the line above, we cannot use this
13209 optimization. */
13210
13211 struct it it;
13212 int line_height_before = this_line_pixel_height;
13213
13214 /* Note that start_display will handle the case that the
13215 line starting at tlbufpos is a continuation line. */
13216 start_display (&it, w, tlbufpos);
13217
13218 /* Implementation note: It this still necessary? */
13219 if (it.current_x != this_line_start_x)
13220 goto cancel;
13221
13222 TRACE ((stderr, "trying display optimization 1\n"));
13223 w->cursor.vpos = -1;
13224 overlay_arrow_seen = 0;
13225 it.vpos = this_line_vpos;
13226 it.current_y = this_line_y;
13227 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13228 display_line (&it);
13229
13230 /* If line contains point, is not continued,
13231 and ends at same distance from eob as before, we win. */
13232 if (w->cursor.vpos >= 0
13233 /* Line is not continued, otherwise this_line_start_pos
13234 would have been set to 0 in display_line. */
13235 && CHARPOS (this_line_start_pos)
13236 /* Line ends as before. */
13237 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13238 /* Line has same height as before. Otherwise other lines
13239 would have to be shifted up or down. */
13240 && this_line_pixel_height == line_height_before)
13241 {
13242 /* If this is not the window's last line, we must adjust
13243 the charstarts of the lines below. */
13244 if (it.current_y < it.last_visible_y)
13245 {
13246 struct glyph_row *row
13247 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13248 ptrdiff_t delta, delta_bytes;
13249
13250 /* We used to distinguish between two cases here,
13251 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13252 when the line ends in a newline or the end of the
13253 buffer's accessible portion. But both cases did
13254 the same, so they were collapsed. */
13255 delta = (Z
13256 - CHARPOS (tlendpos)
13257 - MATRIX_ROW_START_CHARPOS (row));
13258 delta_bytes = (Z_BYTE
13259 - BYTEPOS (tlendpos)
13260 - MATRIX_ROW_START_BYTEPOS (row));
13261
13262 increment_matrix_positions (w->current_matrix,
13263 this_line_vpos + 1,
13264 w->current_matrix->nrows,
13265 delta, delta_bytes);
13266 }
13267
13268 /* If this row displays text now but previously didn't,
13269 or vice versa, w->window_end_vpos may have to be
13270 adjusted. */
13271 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13272 {
13273 if (w->window_end_vpos < this_line_vpos)
13274 w->window_end_vpos = this_line_vpos;
13275 }
13276 else if (w->window_end_vpos == this_line_vpos
13277 && this_line_vpos > 0)
13278 w->window_end_vpos = this_line_vpos - 1;
13279 w->window_end_valid = 0;
13280
13281 /* Update hint: No need to try to scroll in update_window. */
13282 w->desired_matrix->no_scrolling_p = 1;
13283
13284 #ifdef GLYPH_DEBUG
13285 *w->desired_matrix->method = 0;
13286 debug_method_add (w, "optimization 1");
13287 #endif
13288 #ifdef HAVE_WINDOW_SYSTEM
13289 update_window_fringes (w, 0);
13290 #endif
13291 goto update;
13292 }
13293 else
13294 goto cancel;
13295 }
13296 else if (/* Cursor position hasn't changed. */
13297 PT == w->last_point
13298 /* Make sure the cursor was last displayed
13299 in this window. Otherwise we have to reposition it. */
13300 && 0 <= w->cursor.vpos
13301 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13302 {
13303 if (!must_finish)
13304 {
13305 do_pending_window_change (1);
13306 /* If selected_window changed, redisplay again. */
13307 if (WINDOWP (selected_window)
13308 && (w = XWINDOW (selected_window)) != sw)
13309 goto retry;
13310
13311 /* We used to always goto end_of_redisplay here, but this
13312 isn't enough if we have a blinking cursor. */
13313 if (w->cursor_off_p == w->last_cursor_off_p)
13314 goto end_of_redisplay;
13315 }
13316 goto update;
13317 }
13318 /* If highlighting the region, or if the cursor is in the echo area,
13319 then we can't just move the cursor. */
13320 else if (! (!NILP (Vtransient_mark_mode)
13321 && !NILP (BVAR (current_buffer, mark_active)))
13322 && (EQ (selected_window,
13323 BVAR (current_buffer, last_selected_window))
13324 || highlight_nonselected_windows)
13325 && !w->region_showing
13326 && NILP (Vshow_trailing_whitespace)
13327 && !cursor_in_echo_area)
13328 {
13329 struct it it;
13330 struct glyph_row *row;
13331
13332 /* Skip from tlbufpos to PT and see where it is. Note that
13333 PT may be in invisible text. If so, we will end at the
13334 next visible position. */
13335 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13336 NULL, DEFAULT_FACE_ID);
13337 it.current_x = this_line_start_x;
13338 it.current_y = this_line_y;
13339 it.vpos = this_line_vpos;
13340
13341 /* The call to move_it_to stops in front of PT, but
13342 moves over before-strings. */
13343 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13344
13345 if (it.vpos == this_line_vpos
13346 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13347 row->enabled_p))
13348 {
13349 eassert (this_line_vpos == it.vpos);
13350 eassert (this_line_y == it.current_y);
13351 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13352 #ifdef GLYPH_DEBUG
13353 *w->desired_matrix->method = 0;
13354 debug_method_add (w, "optimization 3");
13355 #endif
13356 goto update;
13357 }
13358 else
13359 goto cancel;
13360 }
13361
13362 cancel:
13363 /* Text changed drastically or point moved off of line. */
13364 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13365 }
13366
13367 CHARPOS (this_line_start_pos) = 0;
13368 consider_all_windows_p |= buffer_shared_and_changed ();
13369 ++clear_face_cache_count;
13370 #ifdef HAVE_WINDOW_SYSTEM
13371 ++clear_image_cache_count;
13372 #endif
13373
13374 /* Build desired matrices, and update the display. If
13375 consider_all_windows_p is non-zero, do it for all windows on all
13376 frames. Otherwise do it for selected_window, only. */
13377
13378 if (consider_all_windows_p)
13379 {
13380 FOR_EACH_FRAME (tail, frame)
13381 XFRAME (frame)->updated_p = 0;
13382
13383 FOR_EACH_FRAME (tail, frame)
13384 {
13385 struct frame *f = XFRAME (frame);
13386
13387 /* We don't have to do anything for unselected terminal
13388 frames. */
13389 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13390 && !EQ (FRAME_TTY (f)->top_frame, frame))
13391 continue;
13392
13393 retry_frame:
13394
13395 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13396 {
13397 /* Mark all the scroll bars to be removed; we'll redeem
13398 the ones we want when we redisplay their windows. */
13399 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13400 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13401
13402 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13403 redisplay_windows (FRAME_ROOT_WINDOW (f));
13404
13405 /* The X error handler may have deleted that frame. */
13406 if (!FRAME_LIVE_P (f))
13407 continue;
13408
13409 /* Any scroll bars which redisplay_windows should have
13410 nuked should now go away. */
13411 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13412 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13413
13414 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13415 {
13416 /* If fonts changed on visible frame, display again. */
13417 if (f->fonts_changed)
13418 {
13419 adjust_frame_glyphs (f);
13420 f->fonts_changed = 0;
13421 goto retry_frame;
13422 }
13423
13424 /* See if we have to hscroll. */
13425 if (!f->already_hscrolled_p)
13426 {
13427 f->already_hscrolled_p = 1;
13428 if (hscroll_windows (f->root_window))
13429 goto retry_frame;
13430 }
13431
13432 /* Prevent various kinds of signals during display
13433 update. stdio is not robust about handling
13434 signals, which can cause an apparent I/O
13435 error. */
13436 if (interrupt_input)
13437 unrequest_sigio ();
13438 STOP_POLLING;
13439
13440 /* Mark windows on frame F to update. If we decide to
13441 update all frames but windows_or_buffers_changed is
13442 zero, we assume that only the windows that shows
13443 current buffer should be really updated. */
13444 set_window_update_flags
13445 (XWINDOW (f->root_window),
13446 (windows_or_buffers_changed ? NULL : current_buffer), 1);
13447 pending |= update_frame (f, 0, 0);
13448 f->cursor_type_changed = 0;
13449 f->updated_p = 1;
13450 }
13451 }
13452 }
13453
13454 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13455
13456 if (!pending)
13457 {
13458 /* Do the mark_window_display_accurate after all windows have
13459 been redisplayed because this call resets flags in buffers
13460 which are needed for proper redisplay. */
13461 FOR_EACH_FRAME (tail, frame)
13462 {
13463 struct frame *f = XFRAME (frame);
13464 if (f->updated_p)
13465 {
13466 mark_window_display_accurate (f->root_window, 1);
13467 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13468 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13469 }
13470 }
13471 }
13472 }
13473 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13474 {
13475 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13476 struct frame *mini_frame;
13477
13478 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13479 /* Use list_of_error, not Qerror, so that
13480 we catch only errors and don't run the debugger. */
13481 internal_condition_case_1 (redisplay_window_1, selected_window,
13482 list_of_error,
13483 redisplay_window_error);
13484 if (update_miniwindow_p)
13485 internal_condition_case_1 (redisplay_window_1, mini_window,
13486 list_of_error,
13487 redisplay_window_error);
13488
13489 /* Compare desired and current matrices, perform output. */
13490
13491 update:
13492 /* If fonts changed, display again. */
13493 if (sf->fonts_changed)
13494 goto retry;
13495
13496 /* Prevent various kinds of signals during display update.
13497 stdio is not robust about handling signals,
13498 which can cause an apparent I/O error. */
13499 if (interrupt_input)
13500 unrequest_sigio ();
13501 STOP_POLLING;
13502
13503 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13504 {
13505 if (hscroll_windows (selected_window))
13506 goto retry;
13507
13508 XWINDOW (selected_window)->must_be_updated_p = 1;
13509 pending = update_frame (sf, 0, 0);
13510 sf->cursor_type_changed = 0;
13511 }
13512
13513 /* We may have called echo_area_display at the top of this
13514 function. If the echo area is on another frame, that may
13515 have put text on a frame other than the selected one, so the
13516 above call to update_frame would not have caught it. Catch
13517 it here. */
13518 mini_window = FRAME_MINIBUF_WINDOW (sf);
13519 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13520
13521 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13522 {
13523 XWINDOW (mini_window)->must_be_updated_p = 1;
13524 pending |= update_frame (mini_frame, 0, 0);
13525 mini_frame->cursor_type_changed = 0;
13526 if (!pending && hscroll_windows (mini_window))
13527 goto retry;
13528 }
13529 }
13530
13531 /* If display was paused because of pending input, make sure we do a
13532 thorough update the next time. */
13533 if (pending)
13534 {
13535 /* Prevent the optimization at the beginning of
13536 redisplay_internal that tries a single-line update of the
13537 line containing the cursor in the selected window. */
13538 CHARPOS (this_line_start_pos) = 0;
13539
13540 /* Let the overlay arrow be updated the next time. */
13541 update_overlay_arrows (0);
13542
13543 /* If we pause after scrolling, some rows in the current
13544 matrices of some windows are not valid. */
13545 if (!WINDOW_FULL_WIDTH_P (w)
13546 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13547 update_mode_lines = 1;
13548 }
13549 else
13550 {
13551 if (!consider_all_windows_p)
13552 {
13553 /* This has already been done above if
13554 consider_all_windows_p is set. */
13555 mark_window_display_accurate_1 (w, 1);
13556
13557 /* Say overlay arrows are up to date. */
13558 update_overlay_arrows (1);
13559
13560 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13561 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13562 }
13563
13564 update_mode_lines = 0;
13565 windows_or_buffers_changed = 0;
13566 }
13567
13568 /* Start SIGIO interrupts coming again. Having them off during the
13569 code above makes it less likely one will discard output, but not
13570 impossible, since there might be stuff in the system buffer here.
13571 But it is much hairier to try to do anything about that. */
13572 if (interrupt_input)
13573 request_sigio ();
13574 RESUME_POLLING;
13575
13576 /* If a frame has become visible which was not before, redisplay
13577 again, so that we display it. Expose events for such a frame
13578 (which it gets when becoming visible) don't call the parts of
13579 redisplay constructing glyphs, so simply exposing a frame won't
13580 display anything in this case. So, we have to display these
13581 frames here explicitly. */
13582 if (!pending)
13583 {
13584 int new_count = 0;
13585
13586 FOR_EACH_FRAME (tail, frame)
13587 {
13588 int this_is_visible = 0;
13589
13590 if (XFRAME (frame)->visible)
13591 this_is_visible = 1;
13592
13593 if (this_is_visible)
13594 new_count++;
13595 }
13596
13597 if (new_count != number_of_visible_frames)
13598 windows_or_buffers_changed++;
13599 }
13600
13601 /* Change frame size now if a change is pending. */
13602 do_pending_window_change (1);
13603
13604 /* If we just did a pending size change, or have additional
13605 visible frames, or selected_window changed, redisplay again. */
13606 if ((windows_or_buffers_changed && !pending)
13607 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13608 goto retry;
13609
13610 /* Clear the face and image caches.
13611
13612 We used to do this only if consider_all_windows_p. But the cache
13613 needs to be cleared if a timer creates images in the current
13614 buffer (e.g. the test case in Bug#6230). */
13615
13616 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13617 {
13618 clear_face_cache (0);
13619 clear_face_cache_count = 0;
13620 }
13621
13622 #ifdef HAVE_WINDOW_SYSTEM
13623 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13624 {
13625 clear_image_caches (Qnil);
13626 clear_image_cache_count = 0;
13627 }
13628 #endif /* HAVE_WINDOW_SYSTEM */
13629
13630 end_of_redisplay:
13631 unbind_to (count, Qnil);
13632 RESUME_POLLING;
13633 }
13634
13635
13636 /* Redisplay, but leave alone any recent echo area message unless
13637 another message has been requested in its place.
13638
13639 This is useful in situations where you need to redisplay but no
13640 user action has occurred, making it inappropriate for the message
13641 area to be cleared. See tracking_off and
13642 wait_reading_process_output for examples of these situations.
13643
13644 FROM_WHERE is an integer saying from where this function was
13645 called. This is useful for debugging. */
13646
13647 void
13648 redisplay_preserve_echo_area (int from_where)
13649 {
13650 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13651
13652 if (!NILP (echo_area_buffer[1]))
13653 {
13654 /* We have a previously displayed message, but no current
13655 message. Redisplay the previous message. */
13656 display_last_displayed_message_p = 1;
13657 redisplay_internal ();
13658 display_last_displayed_message_p = 0;
13659 }
13660 else
13661 redisplay_internal ();
13662
13663 flush_frame (SELECTED_FRAME ());
13664 }
13665
13666
13667 /* Function registered with record_unwind_protect in redisplay_internal. */
13668
13669 static void
13670 unwind_redisplay (void)
13671 {
13672 redisplaying_p = 0;
13673 }
13674
13675
13676 /* Mark the display of leaf window W as accurate or inaccurate.
13677 If ACCURATE_P is non-zero mark display of W as accurate. If
13678 ACCURATE_P is zero, arrange for W to be redisplayed the next
13679 time redisplay_internal is called. */
13680
13681 static void
13682 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13683 {
13684 struct buffer *b = XBUFFER (w->contents);
13685
13686 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13687 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13688 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13689
13690 if (accurate_p)
13691 {
13692 b->clip_changed = 0;
13693 b->prevent_redisplay_optimizations_p = 0;
13694
13695 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13696 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13697 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13698 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13699
13700 w->current_matrix->buffer = b;
13701 w->current_matrix->begv = BUF_BEGV (b);
13702 w->current_matrix->zv = BUF_ZV (b);
13703
13704 w->last_cursor_vpos = w->cursor.vpos;
13705 w->last_cursor_off_p = w->cursor_off_p;
13706
13707 if (w == XWINDOW (selected_window))
13708 w->last_point = BUF_PT (b);
13709 else
13710 w->last_point = marker_position (w->pointm);
13711
13712 w->window_end_valid = 1;
13713 w->update_mode_line = 0;
13714 }
13715 }
13716
13717
13718 /* Mark the display of windows in the window tree rooted at WINDOW as
13719 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13720 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13721 be redisplayed the next time redisplay_internal is called. */
13722
13723 void
13724 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13725 {
13726 struct window *w;
13727
13728 for (; !NILP (window); window = w->next)
13729 {
13730 w = XWINDOW (window);
13731 if (WINDOWP (w->contents))
13732 mark_window_display_accurate (w->contents, accurate_p);
13733 else
13734 mark_window_display_accurate_1 (w, accurate_p);
13735 }
13736
13737 if (accurate_p)
13738 update_overlay_arrows (1);
13739 else
13740 /* Force a thorough redisplay the next time by setting
13741 last_arrow_position and last_arrow_string to t, which is
13742 unequal to any useful value of Voverlay_arrow_... */
13743 update_overlay_arrows (-1);
13744 }
13745
13746
13747 /* Return value in display table DP (Lisp_Char_Table *) for character
13748 C. Since a display table doesn't have any parent, we don't have to
13749 follow parent. Do not call this function directly but use the
13750 macro DISP_CHAR_VECTOR. */
13751
13752 Lisp_Object
13753 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13754 {
13755 Lisp_Object val;
13756
13757 if (ASCII_CHAR_P (c))
13758 {
13759 val = dp->ascii;
13760 if (SUB_CHAR_TABLE_P (val))
13761 val = XSUB_CHAR_TABLE (val)->contents[c];
13762 }
13763 else
13764 {
13765 Lisp_Object table;
13766
13767 XSETCHAR_TABLE (table, dp);
13768 val = char_table_ref (table, c);
13769 }
13770 if (NILP (val))
13771 val = dp->defalt;
13772 return val;
13773 }
13774
13775
13776 \f
13777 /***********************************************************************
13778 Window Redisplay
13779 ***********************************************************************/
13780
13781 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13782
13783 static void
13784 redisplay_windows (Lisp_Object window)
13785 {
13786 while (!NILP (window))
13787 {
13788 struct window *w = XWINDOW (window);
13789
13790 if (WINDOWP (w->contents))
13791 redisplay_windows (w->contents);
13792 else if (BUFFERP (w->contents))
13793 {
13794 displayed_buffer = XBUFFER (w->contents);
13795 /* Use list_of_error, not Qerror, so that
13796 we catch only errors and don't run the debugger. */
13797 internal_condition_case_1 (redisplay_window_0, window,
13798 list_of_error,
13799 redisplay_window_error);
13800 }
13801
13802 window = w->next;
13803 }
13804 }
13805
13806 static Lisp_Object
13807 redisplay_window_error (Lisp_Object ignore)
13808 {
13809 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13810 return Qnil;
13811 }
13812
13813 static Lisp_Object
13814 redisplay_window_0 (Lisp_Object window)
13815 {
13816 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13817 redisplay_window (window, 0);
13818 return Qnil;
13819 }
13820
13821 static Lisp_Object
13822 redisplay_window_1 (Lisp_Object window)
13823 {
13824 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13825 redisplay_window (window, 1);
13826 return Qnil;
13827 }
13828 \f
13829
13830 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13831 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13832 which positions recorded in ROW differ from current buffer
13833 positions.
13834
13835 Return 0 if cursor is not on this row, 1 otherwise. */
13836
13837 static int
13838 set_cursor_from_row (struct window *w, struct glyph_row *row,
13839 struct glyph_matrix *matrix,
13840 ptrdiff_t delta, ptrdiff_t delta_bytes,
13841 int dy, int dvpos)
13842 {
13843 struct glyph *glyph = row->glyphs[TEXT_AREA];
13844 struct glyph *end = glyph + row->used[TEXT_AREA];
13845 struct glyph *cursor = NULL;
13846 /* The last known character position in row. */
13847 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13848 int x = row->x;
13849 ptrdiff_t pt_old = PT - delta;
13850 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13851 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13852 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13853 /* A glyph beyond the edge of TEXT_AREA which we should never
13854 touch. */
13855 struct glyph *glyphs_end = end;
13856 /* Non-zero means we've found a match for cursor position, but that
13857 glyph has the avoid_cursor_p flag set. */
13858 int match_with_avoid_cursor = 0;
13859 /* Non-zero means we've seen at least one glyph that came from a
13860 display string. */
13861 int string_seen = 0;
13862 /* Largest and smallest buffer positions seen so far during scan of
13863 glyph row. */
13864 ptrdiff_t bpos_max = pos_before;
13865 ptrdiff_t bpos_min = pos_after;
13866 /* Last buffer position covered by an overlay string with an integer
13867 `cursor' property. */
13868 ptrdiff_t bpos_covered = 0;
13869 /* Non-zero means the display string on which to display the cursor
13870 comes from a text property, not from an overlay. */
13871 int string_from_text_prop = 0;
13872
13873 /* Don't even try doing anything if called for a mode-line or
13874 header-line row, since the rest of the code isn't prepared to
13875 deal with such calamities. */
13876 eassert (!row->mode_line_p);
13877 if (row->mode_line_p)
13878 return 0;
13879
13880 /* Skip over glyphs not having an object at the start and the end of
13881 the row. These are special glyphs like truncation marks on
13882 terminal frames. */
13883 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13884 {
13885 if (!row->reversed_p)
13886 {
13887 while (glyph < end
13888 && INTEGERP (glyph->object)
13889 && glyph->charpos < 0)
13890 {
13891 x += glyph->pixel_width;
13892 ++glyph;
13893 }
13894 while (end > glyph
13895 && INTEGERP ((end - 1)->object)
13896 /* CHARPOS is zero for blanks and stretch glyphs
13897 inserted by extend_face_to_end_of_line. */
13898 && (end - 1)->charpos <= 0)
13899 --end;
13900 glyph_before = glyph - 1;
13901 glyph_after = end;
13902 }
13903 else
13904 {
13905 struct glyph *g;
13906
13907 /* If the glyph row is reversed, we need to process it from back
13908 to front, so swap the edge pointers. */
13909 glyphs_end = end = glyph - 1;
13910 glyph += row->used[TEXT_AREA] - 1;
13911
13912 while (glyph > end + 1
13913 && INTEGERP (glyph->object)
13914 && glyph->charpos < 0)
13915 {
13916 --glyph;
13917 x -= glyph->pixel_width;
13918 }
13919 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13920 --glyph;
13921 /* By default, in reversed rows we put the cursor on the
13922 rightmost (first in the reading order) glyph. */
13923 for (g = end + 1; g < glyph; g++)
13924 x += g->pixel_width;
13925 while (end < glyph
13926 && INTEGERP ((end + 1)->object)
13927 && (end + 1)->charpos <= 0)
13928 ++end;
13929 glyph_before = glyph + 1;
13930 glyph_after = end;
13931 }
13932 }
13933 else if (row->reversed_p)
13934 {
13935 /* In R2L rows that don't display text, put the cursor on the
13936 rightmost glyph. Case in point: an empty last line that is
13937 part of an R2L paragraph. */
13938 cursor = end - 1;
13939 /* Avoid placing the cursor on the last glyph of the row, where
13940 on terminal frames we hold the vertical border between
13941 adjacent windows. */
13942 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13943 && !WINDOW_RIGHTMOST_P (w)
13944 && cursor == row->glyphs[LAST_AREA] - 1)
13945 cursor--;
13946 x = -1; /* will be computed below, at label compute_x */
13947 }
13948
13949 /* Step 1: Try to find the glyph whose character position
13950 corresponds to point. If that's not possible, find 2 glyphs
13951 whose character positions are the closest to point, one before
13952 point, the other after it. */
13953 if (!row->reversed_p)
13954 while (/* not marched to end of glyph row */
13955 glyph < end
13956 /* glyph was not inserted by redisplay for internal purposes */
13957 && !INTEGERP (glyph->object))
13958 {
13959 if (BUFFERP (glyph->object))
13960 {
13961 ptrdiff_t dpos = glyph->charpos - pt_old;
13962
13963 if (glyph->charpos > bpos_max)
13964 bpos_max = glyph->charpos;
13965 if (glyph->charpos < bpos_min)
13966 bpos_min = glyph->charpos;
13967 if (!glyph->avoid_cursor_p)
13968 {
13969 /* If we hit point, we've found the glyph on which to
13970 display the cursor. */
13971 if (dpos == 0)
13972 {
13973 match_with_avoid_cursor = 0;
13974 break;
13975 }
13976 /* See if we've found a better approximation to
13977 POS_BEFORE or to POS_AFTER. */
13978 if (0 > dpos && dpos > pos_before - pt_old)
13979 {
13980 pos_before = glyph->charpos;
13981 glyph_before = glyph;
13982 }
13983 else if (0 < dpos && dpos < pos_after - pt_old)
13984 {
13985 pos_after = glyph->charpos;
13986 glyph_after = glyph;
13987 }
13988 }
13989 else if (dpos == 0)
13990 match_with_avoid_cursor = 1;
13991 }
13992 else if (STRINGP (glyph->object))
13993 {
13994 Lisp_Object chprop;
13995 ptrdiff_t glyph_pos = glyph->charpos;
13996
13997 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13998 glyph->object);
13999 if (!NILP (chprop))
14000 {
14001 /* If the string came from a `display' text property,
14002 look up the buffer position of that property and
14003 use that position to update bpos_max, as if we
14004 actually saw such a position in one of the row's
14005 glyphs. This helps with supporting integer values
14006 of `cursor' property on the display string in
14007 situations where most or all of the row's buffer
14008 text is completely covered by display properties,
14009 so that no glyph with valid buffer positions is
14010 ever seen in the row. */
14011 ptrdiff_t prop_pos =
14012 string_buffer_position_lim (glyph->object, pos_before,
14013 pos_after, 0);
14014
14015 if (prop_pos >= pos_before)
14016 bpos_max = prop_pos - 1;
14017 }
14018 if (INTEGERP (chprop))
14019 {
14020 bpos_covered = bpos_max + XINT (chprop);
14021 /* If the `cursor' property covers buffer positions up
14022 to and including point, we should display cursor on
14023 this glyph. Note that, if a `cursor' property on one
14024 of the string's characters has an integer value, we
14025 will break out of the loop below _before_ we get to
14026 the position match above. IOW, integer values of
14027 the `cursor' property override the "exact match for
14028 point" strategy of positioning the cursor. */
14029 /* Implementation note: bpos_max == pt_old when, e.g.,
14030 we are in an empty line, where bpos_max is set to
14031 MATRIX_ROW_START_CHARPOS, see above. */
14032 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14033 {
14034 cursor = glyph;
14035 break;
14036 }
14037 }
14038
14039 string_seen = 1;
14040 }
14041 x += glyph->pixel_width;
14042 ++glyph;
14043 }
14044 else if (glyph > end) /* row is reversed */
14045 while (!INTEGERP (glyph->object))
14046 {
14047 if (BUFFERP (glyph->object))
14048 {
14049 ptrdiff_t dpos = glyph->charpos - pt_old;
14050
14051 if (glyph->charpos > bpos_max)
14052 bpos_max = glyph->charpos;
14053 if (glyph->charpos < bpos_min)
14054 bpos_min = glyph->charpos;
14055 if (!glyph->avoid_cursor_p)
14056 {
14057 if (dpos == 0)
14058 {
14059 match_with_avoid_cursor = 0;
14060 break;
14061 }
14062 if (0 > dpos && dpos > pos_before - pt_old)
14063 {
14064 pos_before = glyph->charpos;
14065 glyph_before = glyph;
14066 }
14067 else if (0 < dpos && dpos < pos_after - pt_old)
14068 {
14069 pos_after = glyph->charpos;
14070 glyph_after = glyph;
14071 }
14072 }
14073 else if (dpos == 0)
14074 match_with_avoid_cursor = 1;
14075 }
14076 else if (STRINGP (glyph->object))
14077 {
14078 Lisp_Object chprop;
14079 ptrdiff_t glyph_pos = glyph->charpos;
14080
14081 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14082 glyph->object);
14083 if (!NILP (chprop))
14084 {
14085 ptrdiff_t prop_pos =
14086 string_buffer_position_lim (glyph->object, pos_before,
14087 pos_after, 0);
14088
14089 if (prop_pos >= pos_before)
14090 bpos_max = prop_pos - 1;
14091 }
14092 if (INTEGERP (chprop))
14093 {
14094 bpos_covered = bpos_max + XINT (chprop);
14095 /* If the `cursor' property covers buffer positions up
14096 to and including point, we should display cursor on
14097 this glyph. */
14098 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14099 {
14100 cursor = glyph;
14101 break;
14102 }
14103 }
14104 string_seen = 1;
14105 }
14106 --glyph;
14107 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14108 {
14109 x--; /* can't use any pixel_width */
14110 break;
14111 }
14112 x -= glyph->pixel_width;
14113 }
14114
14115 /* Step 2: If we didn't find an exact match for point, we need to
14116 look for a proper place to put the cursor among glyphs between
14117 GLYPH_BEFORE and GLYPH_AFTER. */
14118 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14119 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14120 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14121 {
14122 /* An empty line has a single glyph whose OBJECT is zero and
14123 whose CHARPOS is the position of a newline on that line.
14124 Note that on a TTY, there are more glyphs after that, which
14125 were produced by extend_face_to_end_of_line, but their
14126 CHARPOS is zero or negative. */
14127 int empty_line_p =
14128 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14129 && INTEGERP (glyph->object) && glyph->charpos > 0
14130 /* On a TTY, continued and truncated rows also have a glyph at
14131 their end whose OBJECT is zero and whose CHARPOS is
14132 positive (the continuation and truncation glyphs), but such
14133 rows are obviously not "empty". */
14134 && !(row->continued_p || row->truncated_on_right_p);
14135
14136 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14137 {
14138 ptrdiff_t ellipsis_pos;
14139
14140 /* Scan back over the ellipsis glyphs. */
14141 if (!row->reversed_p)
14142 {
14143 ellipsis_pos = (glyph - 1)->charpos;
14144 while (glyph > row->glyphs[TEXT_AREA]
14145 && (glyph - 1)->charpos == ellipsis_pos)
14146 glyph--, x -= glyph->pixel_width;
14147 /* That loop always goes one position too far, including
14148 the glyph before the ellipsis. So scan forward over
14149 that one. */
14150 x += glyph->pixel_width;
14151 glyph++;
14152 }
14153 else /* row is reversed */
14154 {
14155 ellipsis_pos = (glyph + 1)->charpos;
14156 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14157 && (glyph + 1)->charpos == ellipsis_pos)
14158 glyph++, x += glyph->pixel_width;
14159 x -= glyph->pixel_width;
14160 glyph--;
14161 }
14162 }
14163 else if (match_with_avoid_cursor)
14164 {
14165 cursor = glyph_after;
14166 x = -1;
14167 }
14168 else if (string_seen)
14169 {
14170 int incr = row->reversed_p ? -1 : +1;
14171
14172 /* Need to find the glyph that came out of a string which is
14173 present at point. That glyph is somewhere between
14174 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14175 positioned between POS_BEFORE and POS_AFTER in the
14176 buffer. */
14177 struct glyph *start, *stop;
14178 ptrdiff_t pos = pos_before;
14179
14180 x = -1;
14181
14182 /* If the row ends in a newline from a display string,
14183 reordering could have moved the glyphs belonging to the
14184 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14185 in this case we extend the search to the last glyph in
14186 the row that was not inserted by redisplay. */
14187 if (row->ends_in_newline_from_string_p)
14188 {
14189 glyph_after = end;
14190 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14191 }
14192
14193 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14194 correspond to POS_BEFORE and POS_AFTER, respectively. We
14195 need START and STOP in the order that corresponds to the
14196 row's direction as given by its reversed_p flag. If the
14197 directionality of characters between POS_BEFORE and
14198 POS_AFTER is the opposite of the row's base direction,
14199 these characters will have been reordered for display,
14200 and we need to reverse START and STOP. */
14201 if (!row->reversed_p)
14202 {
14203 start = min (glyph_before, glyph_after);
14204 stop = max (glyph_before, glyph_after);
14205 }
14206 else
14207 {
14208 start = max (glyph_before, glyph_after);
14209 stop = min (glyph_before, glyph_after);
14210 }
14211 for (glyph = start + incr;
14212 row->reversed_p ? glyph > stop : glyph < stop; )
14213 {
14214
14215 /* Any glyphs that come from the buffer are here because
14216 of bidi reordering. Skip them, and only pay
14217 attention to glyphs that came from some string. */
14218 if (STRINGP (glyph->object))
14219 {
14220 Lisp_Object str;
14221 ptrdiff_t tem;
14222 /* If the display property covers the newline, we
14223 need to search for it one position farther. */
14224 ptrdiff_t lim = pos_after
14225 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14226
14227 string_from_text_prop = 0;
14228 str = glyph->object;
14229 tem = string_buffer_position_lim (str, pos, lim, 0);
14230 if (tem == 0 /* from overlay */
14231 || pos <= tem)
14232 {
14233 /* If the string from which this glyph came is
14234 found in the buffer at point, or at position
14235 that is closer to point than pos_after, then
14236 we've found the glyph we've been looking for.
14237 If it comes from an overlay (tem == 0), and
14238 it has the `cursor' property on one of its
14239 glyphs, record that glyph as a candidate for
14240 displaying the cursor. (As in the
14241 unidirectional version, we will display the
14242 cursor on the last candidate we find.) */
14243 if (tem == 0
14244 || tem == pt_old
14245 || (tem - pt_old > 0 && tem < pos_after))
14246 {
14247 /* The glyphs from this string could have
14248 been reordered. Find the one with the
14249 smallest string position. Or there could
14250 be a character in the string with the
14251 `cursor' property, which means display
14252 cursor on that character's glyph. */
14253 ptrdiff_t strpos = glyph->charpos;
14254
14255 if (tem)
14256 {
14257 cursor = glyph;
14258 string_from_text_prop = 1;
14259 }
14260 for ( ;
14261 (row->reversed_p ? glyph > stop : glyph < stop)
14262 && EQ (glyph->object, str);
14263 glyph += incr)
14264 {
14265 Lisp_Object cprop;
14266 ptrdiff_t gpos = glyph->charpos;
14267
14268 cprop = Fget_char_property (make_number (gpos),
14269 Qcursor,
14270 glyph->object);
14271 if (!NILP (cprop))
14272 {
14273 cursor = glyph;
14274 break;
14275 }
14276 if (tem && glyph->charpos < strpos)
14277 {
14278 strpos = glyph->charpos;
14279 cursor = glyph;
14280 }
14281 }
14282
14283 if (tem == pt_old
14284 || (tem - pt_old > 0 && tem < pos_after))
14285 goto compute_x;
14286 }
14287 if (tem)
14288 pos = tem + 1; /* don't find previous instances */
14289 }
14290 /* This string is not what we want; skip all of the
14291 glyphs that came from it. */
14292 while ((row->reversed_p ? glyph > stop : glyph < stop)
14293 && EQ (glyph->object, str))
14294 glyph += incr;
14295 }
14296 else
14297 glyph += incr;
14298 }
14299
14300 /* If we reached the end of the line, and END was from a string,
14301 the cursor is not on this line. */
14302 if (cursor == NULL
14303 && (row->reversed_p ? glyph <= end : glyph >= end)
14304 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14305 && STRINGP (end->object)
14306 && row->continued_p)
14307 return 0;
14308 }
14309 /* A truncated row may not include PT among its character positions.
14310 Setting the cursor inside the scroll margin will trigger
14311 recalculation of hscroll in hscroll_window_tree. But if a
14312 display string covers point, defer to the string-handling
14313 code below to figure this out. */
14314 else if (row->truncated_on_left_p && pt_old < bpos_min)
14315 {
14316 cursor = glyph_before;
14317 x = -1;
14318 }
14319 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14320 /* Zero-width characters produce no glyphs. */
14321 || (!empty_line_p
14322 && (row->reversed_p
14323 ? glyph_after > glyphs_end
14324 : glyph_after < glyphs_end)))
14325 {
14326 cursor = glyph_after;
14327 x = -1;
14328 }
14329 }
14330
14331 compute_x:
14332 if (cursor != NULL)
14333 glyph = cursor;
14334 else if (glyph == glyphs_end
14335 && pos_before == pos_after
14336 && STRINGP ((row->reversed_p
14337 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14338 : row->glyphs[TEXT_AREA])->object))
14339 {
14340 /* If all the glyphs of this row came from strings, put the
14341 cursor on the first glyph of the row. This avoids having the
14342 cursor outside of the text area in this very rare and hard
14343 use case. */
14344 glyph =
14345 row->reversed_p
14346 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14347 : row->glyphs[TEXT_AREA];
14348 }
14349 if (x < 0)
14350 {
14351 struct glyph *g;
14352
14353 /* Need to compute x that corresponds to GLYPH. */
14354 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14355 {
14356 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14357 emacs_abort ();
14358 x += g->pixel_width;
14359 }
14360 }
14361
14362 /* ROW could be part of a continued line, which, under bidi
14363 reordering, might have other rows whose start and end charpos
14364 occlude point. Only set w->cursor if we found a better
14365 approximation to the cursor position than we have from previously
14366 examined candidate rows belonging to the same continued line. */
14367 if (/* we already have a candidate row */
14368 w->cursor.vpos >= 0
14369 /* that candidate is not the row we are processing */
14370 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14371 /* Make sure cursor.vpos specifies a row whose start and end
14372 charpos occlude point, and it is valid candidate for being a
14373 cursor-row. This is because some callers of this function
14374 leave cursor.vpos at the row where the cursor was displayed
14375 during the last redisplay cycle. */
14376 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14377 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14378 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14379 {
14380 struct glyph *g1 =
14381 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14382
14383 /* Don't consider glyphs that are outside TEXT_AREA. */
14384 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14385 return 0;
14386 /* Keep the candidate whose buffer position is the closest to
14387 point or has the `cursor' property. */
14388 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14389 w->cursor.hpos >= 0
14390 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14391 && ((BUFFERP (g1->object)
14392 && (g1->charpos == pt_old /* an exact match always wins */
14393 || (BUFFERP (glyph->object)
14394 && eabs (g1->charpos - pt_old)
14395 < eabs (glyph->charpos - pt_old))))
14396 /* previous candidate is a glyph from a string that has
14397 a non-nil `cursor' property */
14398 || (STRINGP (g1->object)
14399 && (!NILP (Fget_char_property (make_number (g1->charpos),
14400 Qcursor, g1->object))
14401 /* previous candidate is from the same display
14402 string as this one, and the display string
14403 came from a text property */
14404 || (EQ (g1->object, glyph->object)
14405 && string_from_text_prop)
14406 /* this candidate is from newline and its
14407 position is not an exact match */
14408 || (INTEGERP (glyph->object)
14409 && glyph->charpos != pt_old)))))
14410 return 0;
14411 /* If this candidate gives an exact match, use that. */
14412 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14413 /* If this candidate is a glyph created for the
14414 terminating newline of a line, and point is on that
14415 newline, it wins because it's an exact match. */
14416 || (!row->continued_p
14417 && INTEGERP (glyph->object)
14418 && glyph->charpos == 0
14419 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14420 /* Otherwise, keep the candidate that comes from a row
14421 spanning less buffer positions. This may win when one or
14422 both candidate positions are on glyphs that came from
14423 display strings, for which we cannot compare buffer
14424 positions. */
14425 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14426 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14427 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14428 return 0;
14429 }
14430 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14431 w->cursor.x = x;
14432 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14433 w->cursor.y = row->y + dy;
14434
14435 if (w == XWINDOW (selected_window))
14436 {
14437 if (!row->continued_p
14438 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14439 && row->x == 0)
14440 {
14441 this_line_buffer = XBUFFER (w->contents);
14442
14443 CHARPOS (this_line_start_pos)
14444 = MATRIX_ROW_START_CHARPOS (row) + delta;
14445 BYTEPOS (this_line_start_pos)
14446 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14447
14448 CHARPOS (this_line_end_pos)
14449 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14450 BYTEPOS (this_line_end_pos)
14451 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14452
14453 this_line_y = w->cursor.y;
14454 this_line_pixel_height = row->height;
14455 this_line_vpos = w->cursor.vpos;
14456 this_line_start_x = row->x;
14457 }
14458 else
14459 CHARPOS (this_line_start_pos) = 0;
14460 }
14461
14462 return 1;
14463 }
14464
14465
14466 /* Run window scroll functions, if any, for WINDOW with new window
14467 start STARTP. Sets the window start of WINDOW to that position.
14468
14469 We assume that the window's buffer is really current. */
14470
14471 static struct text_pos
14472 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14473 {
14474 struct window *w = XWINDOW (window);
14475 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14476
14477 eassert (current_buffer == XBUFFER (w->contents));
14478
14479 if (!NILP (Vwindow_scroll_functions))
14480 {
14481 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14482 make_number (CHARPOS (startp)));
14483 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14484 /* In case the hook functions switch buffers. */
14485 set_buffer_internal (XBUFFER (w->contents));
14486 }
14487
14488 return startp;
14489 }
14490
14491
14492 /* Make sure the line containing the cursor is fully visible.
14493 A value of 1 means there is nothing to be done.
14494 (Either the line is fully visible, or it cannot be made so,
14495 or we cannot tell.)
14496
14497 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14498 is higher than window.
14499
14500 A value of 0 means the caller should do scrolling
14501 as if point had gone off the screen. */
14502
14503 static int
14504 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14505 {
14506 struct glyph_matrix *matrix;
14507 struct glyph_row *row;
14508 int window_height;
14509
14510 if (!make_cursor_line_fully_visible_p)
14511 return 1;
14512
14513 /* It's not always possible to find the cursor, e.g, when a window
14514 is full of overlay strings. Don't do anything in that case. */
14515 if (w->cursor.vpos < 0)
14516 return 1;
14517
14518 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14519 row = MATRIX_ROW (matrix, w->cursor.vpos);
14520
14521 /* If the cursor row is not partially visible, there's nothing to do. */
14522 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14523 return 1;
14524
14525 /* If the row the cursor is in is taller than the window's height,
14526 it's not clear what to do, so do nothing. */
14527 window_height = window_box_height (w);
14528 if (row->height >= window_height)
14529 {
14530 if (!force_p || MINI_WINDOW_P (w)
14531 || w->vscroll || w->cursor.vpos == 0)
14532 return 1;
14533 }
14534 return 0;
14535 }
14536
14537
14538 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14539 non-zero means only WINDOW is redisplayed in redisplay_internal.
14540 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14541 in redisplay_window to bring a partially visible line into view in
14542 the case that only the cursor has moved.
14543
14544 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14545 last screen line's vertical height extends past the end of the screen.
14546
14547 Value is
14548
14549 1 if scrolling succeeded
14550
14551 0 if scrolling didn't find point.
14552
14553 -1 if new fonts have been loaded so that we must interrupt
14554 redisplay, adjust glyph matrices, and try again. */
14555
14556 enum
14557 {
14558 SCROLLING_SUCCESS,
14559 SCROLLING_FAILED,
14560 SCROLLING_NEED_LARGER_MATRICES
14561 };
14562
14563 /* If scroll-conservatively is more than this, never recenter.
14564
14565 If you change this, don't forget to update the doc string of
14566 `scroll-conservatively' and the Emacs manual. */
14567 #define SCROLL_LIMIT 100
14568
14569 static int
14570 try_scrolling (Lisp_Object window, int just_this_one_p,
14571 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14572 int temp_scroll_step, int last_line_misfit)
14573 {
14574 struct window *w = XWINDOW (window);
14575 struct frame *f = XFRAME (w->frame);
14576 struct text_pos pos, startp;
14577 struct it it;
14578 int this_scroll_margin, scroll_max, rc, height;
14579 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14580 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14581 Lisp_Object aggressive;
14582 /* We will never try scrolling more than this number of lines. */
14583 int scroll_limit = SCROLL_LIMIT;
14584 int frame_line_height = default_line_pixel_height (w);
14585 int window_total_lines
14586 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14587
14588 #ifdef GLYPH_DEBUG
14589 debug_method_add (w, "try_scrolling");
14590 #endif
14591
14592 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14593
14594 /* Compute scroll margin height in pixels. We scroll when point is
14595 within this distance from the top or bottom of the window. */
14596 if (scroll_margin > 0)
14597 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14598 * frame_line_height;
14599 else
14600 this_scroll_margin = 0;
14601
14602 /* Force arg_scroll_conservatively to have a reasonable value, to
14603 avoid scrolling too far away with slow move_it_* functions. Note
14604 that the user can supply scroll-conservatively equal to
14605 `most-positive-fixnum', which can be larger than INT_MAX. */
14606 if (arg_scroll_conservatively > scroll_limit)
14607 {
14608 arg_scroll_conservatively = scroll_limit + 1;
14609 scroll_max = scroll_limit * frame_line_height;
14610 }
14611 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14612 /* Compute how much we should try to scroll maximally to bring
14613 point into view. */
14614 scroll_max = (max (scroll_step,
14615 max (arg_scroll_conservatively, temp_scroll_step))
14616 * frame_line_height);
14617 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14618 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14619 /* We're trying to scroll because of aggressive scrolling but no
14620 scroll_step is set. Choose an arbitrary one. */
14621 scroll_max = 10 * frame_line_height;
14622 else
14623 scroll_max = 0;
14624
14625 too_near_end:
14626
14627 /* Decide whether to scroll down. */
14628 if (PT > CHARPOS (startp))
14629 {
14630 int scroll_margin_y;
14631
14632 /* Compute the pixel ypos of the scroll margin, then move IT to
14633 either that ypos or PT, whichever comes first. */
14634 start_display (&it, w, startp);
14635 scroll_margin_y = it.last_visible_y - this_scroll_margin
14636 - frame_line_height * extra_scroll_margin_lines;
14637 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14638 (MOVE_TO_POS | MOVE_TO_Y));
14639
14640 if (PT > CHARPOS (it.current.pos))
14641 {
14642 int y0 = line_bottom_y (&it);
14643 /* Compute how many pixels below window bottom to stop searching
14644 for PT. This avoids costly search for PT that is far away if
14645 the user limited scrolling by a small number of lines, but
14646 always finds PT if scroll_conservatively is set to a large
14647 number, such as most-positive-fixnum. */
14648 int slack = max (scroll_max, 10 * frame_line_height);
14649 int y_to_move = it.last_visible_y + slack;
14650
14651 /* Compute the distance from the scroll margin to PT or to
14652 the scroll limit, whichever comes first. This should
14653 include the height of the cursor line, to make that line
14654 fully visible. */
14655 move_it_to (&it, PT, -1, y_to_move,
14656 -1, MOVE_TO_POS | MOVE_TO_Y);
14657 dy = line_bottom_y (&it) - y0;
14658
14659 if (dy > scroll_max)
14660 return SCROLLING_FAILED;
14661
14662 if (dy > 0)
14663 scroll_down_p = 1;
14664 }
14665 }
14666
14667 if (scroll_down_p)
14668 {
14669 /* Point is in or below the bottom scroll margin, so move the
14670 window start down. If scrolling conservatively, move it just
14671 enough down to make point visible. If scroll_step is set,
14672 move it down by scroll_step. */
14673 if (arg_scroll_conservatively)
14674 amount_to_scroll
14675 = min (max (dy, frame_line_height),
14676 frame_line_height * arg_scroll_conservatively);
14677 else if (scroll_step || temp_scroll_step)
14678 amount_to_scroll = scroll_max;
14679 else
14680 {
14681 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14682 height = WINDOW_BOX_TEXT_HEIGHT (w);
14683 if (NUMBERP (aggressive))
14684 {
14685 double float_amount = XFLOATINT (aggressive) * height;
14686 int aggressive_scroll = float_amount;
14687 if (aggressive_scroll == 0 && float_amount > 0)
14688 aggressive_scroll = 1;
14689 /* Don't let point enter the scroll margin near top of
14690 the window. This could happen if the value of
14691 scroll_up_aggressively is too large and there are
14692 non-zero margins, because scroll_up_aggressively
14693 means put point that fraction of window height
14694 _from_the_bottom_margin_. */
14695 if (aggressive_scroll + 2*this_scroll_margin > height)
14696 aggressive_scroll = height - 2*this_scroll_margin;
14697 amount_to_scroll = dy + aggressive_scroll;
14698 }
14699 }
14700
14701 if (amount_to_scroll <= 0)
14702 return SCROLLING_FAILED;
14703
14704 start_display (&it, w, startp);
14705 if (arg_scroll_conservatively <= scroll_limit)
14706 move_it_vertically (&it, amount_to_scroll);
14707 else
14708 {
14709 /* Extra precision for users who set scroll-conservatively
14710 to a large number: make sure the amount we scroll
14711 the window start is never less than amount_to_scroll,
14712 which was computed as distance from window bottom to
14713 point. This matters when lines at window top and lines
14714 below window bottom have different height. */
14715 struct it it1;
14716 void *it1data = NULL;
14717 /* We use a temporary it1 because line_bottom_y can modify
14718 its argument, if it moves one line down; see there. */
14719 int start_y;
14720
14721 SAVE_IT (it1, it, it1data);
14722 start_y = line_bottom_y (&it1);
14723 do {
14724 RESTORE_IT (&it, &it, it1data);
14725 move_it_by_lines (&it, 1);
14726 SAVE_IT (it1, it, it1data);
14727 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14728 }
14729
14730 /* If STARTP is unchanged, move it down another screen line. */
14731 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14732 move_it_by_lines (&it, 1);
14733 startp = it.current.pos;
14734 }
14735 else
14736 {
14737 struct text_pos scroll_margin_pos = startp;
14738 int y_offset = 0;
14739
14740 /* See if point is inside the scroll margin at the top of the
14741 window. */
14742 if (this_scroll_margin)
14743 {
14744 int y_start;
14745
14746 start_display (&it, w, startp);
14747 y_start = it.current_y;
14748 move_it_vertically (&it, this_scroll_margin);
14749 scroll_margin_pos = it.current.pos;
14750 /* If we didn't move enough before hitting ZV, request
14751 additional amount of scroll, to move point out of the
14752 scroll margin. */
14753 if (IT_CHARPOS (it) == ZV
14754 && it.current_y - y_start < this_scroll_margin)
14755 y_offset = this_scroll_margin - (it.current_y - y_start);
14756 }
14757
14758 if (PT < CHARPOS (scroll_margin_pos))
14759 {
14760 /* Point is in the scroll margin at the top of the window or
14761 above what is displayed in the window. */
14762 int y0, y_to_move;
14763
14764 /* Compute the vertical distance from PT to the scroll
14765 margin position. Move as far as scroll_max allows, or
14766 one screenful, or 10 screen lines, whichever is largest.
14767 Give up if distance is greater than scroll_max or if we
14768 didn't reach the scroll margin position. */
14769 SET_TEXT_POS (pos, PT, PT_BYTE);
14770 start_display (&it, w, pos);
14771 y0 = it.current_y;
14772 y_to_move = max (it.last_visible_y,
14773 max (scroll_max, 10 * frame_line_height));
14774 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14775 y_to_move, -1,
14776 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14777 dy = it.current_y - y0;
14778 if (dy > scroll_max
14779 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14780 return SCROLLING_FAILED;
14781
14782 /* Additional scroll for when ZV was too close to point. */
14783 dy += y_offset;
14784
14785 /* Compute new window start. */
14786 start_display (&it, w, startp);
14787
14788 if (arg_scroll_conservatively)
14789 amount_to_scroll = max (dy, frame_line_height *
14790 max (scroll_step, temp_scroll_step));
14791 else if (scroll_step || temp_scroll_step)
14792 amount_to_scroll = scroll_max;
14793 else
14794 {
14795 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14796 height = WINDOW_BOX_TEXT_HEIGHT (w);
14797 if (NUMBERP (aggressive))
14798 {
14799 double float_amount = XFLOATINT (aggressive) * height;
14800 int aggressive_scroll = float_amount;
14801 if (aggressive_scroll == 0 && float_amount > 0)
14802 aggressive_scroll = 1;
14803 /* Don't let point enter the scroll margin near
14804 bottom of the window, if the value of
14805 scroll_down_aggressively happens to be too
14806 large. */
14807 if (aggressive_scroll + 2*this_scroll_margin > height)
14808 aggressive_scroll = height - 2*this_scroll_margin;
14809 amount_to_scroll = dy + aggressive_scroll;
14810 }
14811 }
14812
14813 if (amount_to_scroll <= 0)
14814 return SCROLLING_FAILED;
14815
14816 move_it_vertically_backward (&it, amount_to_scroll);
14817 startp = it.current.pos;
14818 }
14819 }
14820
14821 /* Run window scroll functions. */
14822 startp = run_window_scroll_functions (window, startp);
14823
14824 /* Display the window. Give up if new fonts are loaded, or if point
14825 doesn't appear. */
14826 if (!try_window (window, startp, 0))
14827 rc = SCROLLING_NEED_LARGER_MATRICES;
14828 else if (w->cursor.vpos < 0)
14829 {
14830 clear_glyph_matrix (w->desired_matrix);
14831 rc = SCROLLING_FAILED;
14832 }
14833 else
14834 {
14835 /* Maybe forget recorded base line for line number display. */
14836 if (!just_this_one_p
14837 || current_buffer->clip_changed
14838 || BEG_UNCHANGED < CHARPOS (startp))
14839 w->base_line_number = 0;
14840
14841 /* If cursor ends up on a partially visible line,
14842 treat that as being off the bottom of the screen. */
14843 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14844 /* It's possible that the cursor is on the first line of the
14845 buffer, which is partially obscured due to a vscroll
14846 (Bug#7537). In that case, avoid looping forever . */
14847 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14848 {
14849 clear_glyph_matrix (w->desired_matrix);
14850 ++extra_scroll_margin_lines;
14851 goto too_near_end;
14852 }
14853 rc = SCROLLING_SUCCESS;
14854 }
14855
14856 return rc;
14857 }
14858
14859
14860 /* Compute a suitable window start for window W if display of W starts
14861 on a continuation line. Value is non-zero if a new window start
14862 was computed.
14863
14864 The new window start will be computed, based on W's width, starting
14865 from the start of the continued line. It is the start of the
14866 screen line with the minimum distance from the old start W->start. */
14867
14868 static int
14869 compute_window_start_on_continuation_line (struct window *w)
14870 {
14871 struct text_pos pos, start_pos;
14872 int window_start_changed_p = 0;
14873
14874 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14875
14876 /* If window start is on a continuation line... Window start may be
14877 < BEGV in case there's invisible text at the start of the
14878 buffer (M-x rmail, for example). */
14879 if (CHARPOS (start_pos) > BEGV
14880 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14881 {
14882 struct it it;
14883 struct glyph_row *row;
14884
14885 /* Handle the case that the window start is out of range. */
14886 if (CHARPOS (start_pos) < BEGV)
14887 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14888 else if (CHARPOS (start_pos) > ZV)
14889 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14890
14891 /* Find the start of the continued line. This should be fast
14892 because find_newline is fast (newline cache). */
14893 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14894 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14895 row, DEFAULT_FACE_ID);
14896 reseat_at_previous_visible_line_start (&it);
14897
14898 /* If the line start is "too far" away from the window start,
14899 say it takes too much time to compute a new window start. */
14900 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14901 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14902 {
14903 int min_distance, distance;
14904
14905 /* Move forward by display lines to find the new window
14906 start. If window width was enlarged, the new start can
14907 be expected to be > the old start. If window width was
14908 decreased, the new window start will be < the old start.
14909 So, we're looking for the display line start with the
14910 minimum distance from the old window start. */
14911 pos = it.current.pos;
14912 min_distance = INFINITY;
14913 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14914 distance < min_distance)
14915 {
14916 min_distance = distance;
14917 pos = it.current.pos;
14918 if (it.line_wrap == WORD_WRAP)
14919 {
14920 /* Under WORD_WRAP, move_it_by_lines is likely to
14921 overshoot and stop not at the first, but the
14922 second character from the left margin. So in
14923 that case, we need a more tight control on the X
14924 coordinate of the iterator than move_it_by_lines
14925 promises in its contract. The method is to first
14926 go to the last (rightmost) visible character of a
14927 line, then move to the leftmost character on the
14928 next line in a separate call. */
14929 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14930 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14931 move_it_to (&it, ZV, 0,
14932 it.current_y + it.max_ascent + it.max_descent, -1,
14933 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14934 }
14935 else
14936 move_it_by_lines (&it, 1);
14937 }
14938
14939 /* Set the window start there. */
14940 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14941 window_start_changed_p = 1;
14942 }
14943 }
14944
14945 return window_start_changed_p;
14946 }
14947
14948
14949 /* Try cursor movement in case text has not changed in window WINDOW,
14950 with window start STARTP. Value is
14951
14952 CURSOR_MOVEMENT_SUCCESS if successful
14953
14954 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14955
14956 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14957 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14958 we want to scroll as if scroll-step were set to 1. See the code.
14959
14960 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14961 which case we have to abort this redisplay, and adjust matrices
14962 first. */
14963
14964 enum
14965 {
14966 CURSOR_MOVEMENT_SUCCESS,
14967 CURSOR_MOVEMENT_CANNOT_BE_USED,
14968 CURSOR_MOVEMENT_MUST_SCROLL,
14969 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14970 };
14971
14972 static int
14973 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14974 {
14975 struct window *w = XWINDOW (window);
14976 struct frame *f = XFRAME (w->frame);
14977 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14978
14979 #ifdef GLYPH_DEBUG
14980 if (inhibit_try_cursor_movement)
14981 return rc;
14982 #endif
14983
14984 /* Previously, there was a check for Lisp integer in the
14985 if-statement below. Now, this field is converted to
14986 ptrdiff_t, thus zero means invalid position in a buffer. */
14987 eassert (w->last_point > 0);
14988 /* Likewise there was a check whether window_end_vpos is nil or larger
14989 than the window. Now window_end_vpos is int and so never nil, but
14990 let's leave eassert to check whether it fits in the window. */
14991 eassert (w->window_end_vpos < w->current_matrix->nrows);
14992
14993 /* Handle case where text has not changed, only point, and it has
14994 not moved off the frame. */
14995 if (/* Point may be in this window. */
14996 PT >= CHARPOS (startp)
14997 /* Selective display hasn't changed. */
14998 && !current_buffer->clip_changed
14999 /* Function force-mode-line-update is used to force a thorough
15000 redisplay. It sets either windows_or_buffers_changed or
15001 update_mode_lines. So don't take a shortcut here for these
15002 cases. */
15003 && !update_mode_lines
15004 && !windows_or_buffers_changed
15005 && !f->cursor_type_changed
15006 /* Can't use this case if highlighting a region. When a
15007 region exists, cursor movement has to do more than just
15008 set the cursor. */
15009 && markpos_of_region () < 0
15010 && !w->region_showing
15011 && NILP (Vshow_trailing_whitespace)
15012 /* This code is not used for mini-buffer for the sake of the case
15013 of redisplaying to replace an echo area message; since in
15014 that case the mini-buffer contents per se are usually
15015 unchanged. This code is of no real use in the mini-buffer
15016 since the handling of this_line_start_pos, etc., in redisplay
15017 handles the same cases. */
15018 && !EQ (window, minibuf_window)
15019 && (FRAME_WINDOW_P (f)
15020 || !overlay_arrow_in_current_buffer_p ()))
15021 {
15022 int this_scroll_margin, top_scroll_margin;
15023 struct glyph_row *row = NULL;
15024 int frame_line_height = default_line_pixel_height (w);
15025 int window_total_lines
15026 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15027
15028 #ifdef GLYPH_DEBUG
15029 debug_method_add (w, "cursor movement");
15030 #endif
15031
15032 /* Scroll if point within this distance from the top or bottom
15033 of the window. This is a pixel value. */
15034 if (scroll_margin > 0)
15035 {
15036 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15037 this_scroll_margin *= frame_line_height;
15038 }
15039 else
15040 this_scroll_margin = 0;
15041
15042 top_scroll_margin = this_scroll_margin;
15043 if (WINDOW_WANTS_HEADER_LINE_P (w))
15044 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15045
15046 /* Start with the row the cursor was displayed during the last
15047 not paused redisplay. Give up if that row is not valid. */
15048 if (w->last_cursor_vpos < 0
15049 || w->last_cursor_vpos >= w->current_matrix->nrows)
15050 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15051 else
15052 {
15053 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15054 if (row->mode_line_p)
15055 ++row;
15056 if (!row->enabled_p)
15057 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15058 }
15059
15060 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15061 {
15062 int scroll_p = 0, must_scroll = 0;
15063 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15064
15065 if (PT > w->last_point)
15066 {
15067 /* Point has moved forward. */
15068 while (MATRIX_ROW_END_CHARPOS (row) < PT
15069 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15070 {
15071 eassert (row->enabled_p);
15072 ++row;
15073 }
15074
15075 /* If the end position of a row equals the start
15076 position of the next row, and PT is at that position,
15077 we would rather display cursor in the next line. */
15078 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15079 && MATRIX_ROW_END_CHARPOS (row) == PT
15080 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15081 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15082 && !cursor_row_p (row))
15083 ++row;
15084
15085 /* If within the scroll margin, scroll. Note that
15086 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15087 the next line would be drawn, and that
15088 this_scroll_margin can be zero. */
15089 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15090 || PT > MATRIX_ROW_END_CHARPOS (row)
15091 /* Line is completely visible last line in window
15092 and PT is to be set in the next line. */
15093 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15094 && PT == MATRIX_ROW_END_CHARPOS (row)
15095 && !row->ends_at_zv_p
15096 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15097 scroll_p = 1;
15098 }
15099 else if (PT < w->last_point)
15100 {
15101 /* Cursor has to be moved backward. Note that PT >=
15102 CHARPOS (startp) because of the outer if-statement. */
15103 while (!row->mode_line_p
15104 && (MATRIX_ROW_START_CHARPOS (row) > PT
15105 || (MATRIX_ROW_START_CHARPOS (row) == PT
15106 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15107 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15108 row > w->current_matrix->rows
15109 && (row-1)->ends_in_newline_from_string_p))))
15110 && (row->y > top_scroll_margin
15111 || CHARPOS (startp) == BEGV))
15112 {
15113 eassert (row->enabled_p);
15114 --row;
15115 }
15116
15117 /* Consider the following case: Window starts at BEGV,
15118 there is invisible, intangible text at BEGV, so that
15119 display starts at some point START > BEGV. It can
15120 happen that we are called with PT somewhere between
15121 BEGV and START. Try to handle that case. */
15122 if (row < w->current_matrix->rows
15123 || row->mode_line_p)
15124 {
15125 row = w->current_matrix->rows;
15126 if (row->mode_line_p)
15127 ++row;
15128 }
15129
15130 /* Due to newlines in overlay strings, we may have to
15131 skip forward over overlay strings. */
15132 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15133 && MATRIX_ROW_END_CHARPOS (row) == PT
15134 && !cursor_row_p (row))
15135 ++row;
15136
15137 /* If within the scroll margin, scroll. */
15138 if (row->y < top_scroll_margin
15139 && CHARPOS (startp) != BEGV)
15140 scroll_p = 1;
15141 }
15142 else
15143 {
15144 /* Cursor did not move. So don't scroll even if cursor line
15145 is partially visible, as it was so before. */
15146 rc = CURSOR_MOVEMENT_SUCCESS;
15147 }
15148
15149 if (PT < MATRIX_ROW_START_CHARPOS (row)
15150 || PT > MATRIX_ROW_END_CHARPOS (row))
15151 {
15152 /* if PT is not in the glyph row, give up. */
15153 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15154 must_scroll = 1;
15155 }
15156 else if (rc != CURSOR_MOVEMENT_SUCCESS
15157 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15158 {
15159 struct glyph_row *row1;
15160
15161 /* If rows are bidi-reordered and point moved, back up
15162 until we find a row that does not belong to a
15163 continuation line. This is because we must consider
15164 all rows of a continued line as candidates for the
15165 new cursor positioning, since row start and end
15166 positions change non-linearly with vertical position
15167 in such rows. */
15168 /* FIXME: Revisit this when glyph ``spilling'' in
15169 continuation lines' rows is implemented for
15170 bidi-reordered rows. */
15171 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15172 MATRIX_ROW_CONTINUATION_LINE_P (row);
15173 --row)
15174 {
15175 /* If we hit the beginning of the displayed portion
15176 without finding the first row of a continued
15177 line, give up. */
15178 if (row <= row1)
15179 {
15180 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15181 break;
15182 }
15183 eassert (row->enabled_p);
15184 }
15185 }
15186 if (must_scroll)
15187 ;
15188 else if (rc != CURSOR_MOVEMENT_SUCCESS
15189 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15190 /* Make sure this isn't a header line by any chance, since
15191 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15192 && !row->mode_line_p
15193 && make_cursor_line_fully_visible_p)
15194 {
15195 if (PT == MATRIX_ROW_END_CHARPOS (row)
15196 && !row->ends_at_zv_p
15197 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15198 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15199 else if (row->height > window_box_height (w))
15200 {
15201 /* If we end up in a partially visible line, let's
15202 make it fully visible, except when it's taller
15203 than the window, in which case we can't do much
15204 about it. */
15205 *scroll_step = 1;
15206 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15207 }
15208 else
15209 {
15210 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15211 if (!cursor_row_fully_visible_p (w, 0, 1))
15212 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15213 else
15214 rc = CURSOR_MOVEMENT_SUCCESS;
15215 }
15216 }
15217 else if (scroll_p)
15218 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15219 else if (rc != CURSOR_MOVEMENT_SUCCESS
15220 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15221 {
15222 /* With bidi-reordered rows, there could be more than
15223 one candidate row whose start and end positions
15224 occlude point. We need to let set_cursor_from_row
15225 find the best candidate. */
15226 /* FIXME: Revisit this when glyph ``spilling'' in
15227 continuation lines' rows is implemented for
15228 bidi-reordered rows. */
15229 int rv = 0;
15230
15231 do
15232 {
15233 int at_zv_p = 0, exact_match_p = 0;
15234
15235 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15236 && PT <= MATRIX_ROW_END_CHARPOS (row)
15237 && cursor_row_p (row))
15238 rv |= set_cursor_from_row (w, row, w->current_matrix,
15239 0, 0, 0, 0);
15240 /* As soon as we've found the exact match for point,
15241 or the first suitable row whose ends_at_zv_p flag
15242 is set, we are done. */
15243 at_zv_p =
15244 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15245 if (rv && !at_zv_p
15246 && w->cursor.hpos >= 0
15247 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15248 w->cursor.vpos))
15249 {
15250 struct glyph_row *candidate =
15251 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15252 struct glyph *g =
15253 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15254 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15255
15256 exact_match_p =
15257 (BUFFERP (g->object) && g->charpos == PT)
15258 || (INTEGERP (g->object)
15259 && (g->charpos == PT
15260 || (g->charpos == 0 && endpos - 1 == PT)));
15261 }
15262 if (rv && (at_zv_p || exact_match_p))
15263 {
15264 rc = CURSOR_MOVEMENT_SUCCESS;
15265 break;
15266 }
15267 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15268 break;
15269 ++row;
15270 }
15271 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15272 || row->continued_p)
15273 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15274 || (MATRIX_ROW_START_CHARPOS (row) == PT
15275 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15276 /* If we didn't find any candidate rows, or exited the
15277 loop before all the candidates were examined, signal
15278 to the caller that this method failed. */
15279 if (rc != CURSOR_MOVEMENT_SUCCESS
15280 && !(rv
15281 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15282 && !row->continued_p))
15283 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15284 else if (rv)
15285 rc = CURSOR_MOVEMENT_SUCCESS;
15286 }
15287 else
15288 {
15289 do
15290 {
15291 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15292 {
15293 rc = CURSOR_MOVEMENT_SUCCESS;
15294 break;
15295 }
15296 ++row;
15297 }
15298 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15299 && MATRIX_ROW_START_CHARPOS (row) == PT
15300 && cursor_row_p (row));
15301 }
15302 }
15303 }
15304
15305 return rc;
15306 }
15307
15308 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15309 static
15310 #endif
15311 void
15312 set_vertical_scroll_bar (struct window *w)
15313 {
15314 ptrdiff_t start, end, whole;
15315
15316 /* Calculate the start and end positions for the current window.
15317 At some point, it would be nice to choose between scrollbars
15318 which reflect the whole buffer size, with special markers
15319 indicating narrowing, and scrollbars which reflect only the
15320 visible region.
15321
15322 Note that mini-buffers sometimes aren't displaying any text. */
15323 if (!MINI_WINDOW_P (w)
15324 || (w == XWINDOW (minibuf_window)
15325 && NILP (echo_area_buffer[0])))
15326 {
15327 struct buffer *buf = XBUFFER (w->contents);
15328 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15329 start = marker_position (w->start) - BUF_BEGV (buf);
15330 /* I don't think this is guaranteed to be right. For the
15331 moment, we'll pretend it is. */
15332 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15333
15334 if (end < start)
15335 end = start;
15336 if (whole < (end - start))
15337 whole = end - start;
15338 }
15339 else
15340 start = end = whole = 0;
15341
15342 /* Indicate what this scroll bar ought to be displaying now. */
15343 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15344 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15345 (w, end - start, whole, start);
15346 }
15347
15348
15349 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15350 selected_window is redisplayed.
15351
15352 We can return without actually redisplaying the window if fonts has been
15353 changed on window's frame. In that case, redisplay_internal will retry. */
15354
15355 static void
15356 redisplay_window (Lisp_Object window, int just_this_one_p)
15357 {
15358 struct window *w = XWINDOW (window);
15359 struct frame *f = XFRAME (w->frame);
15360 struct buffer *buffer = XBUFFER (w->contents);
15361 struct buffer *old = current_buffer;
15362 struct text_pos lpoint, opoint, startp;
15363 int update_mode_line;
15364 int tem;
15365 struct it it;
15366 /* Record it now because it's overwritten. */
15367 int current_matrix_up_to_date_p = 0;
15368 int used_current_matrix_p = 0;
15369 /* This is less strict than current_matrix_up_to_date_p.
15370 It indicates that the buffer contents and narrowing are unchanged. */
15371 int buffer_unchanged_p = 0;
15372 int temp_scroll_step = 0;
15373 ptrdiff_t count = SPECPDL_INDEX ();
15374 int rc;
15375 int centering_position = -1;
15376 int last_line_misfit = 0;
15377 ptrdiff_t beg_unchanged, end_unchanged;
15378 int frame_line_height;
15379
15380 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15381 opoint = lpoint;
15382
15383 #ifdef GLYPH_DEBUG
15384 *w->desired_matrix->method = 0;
15385 #endif
15386
15387 /* Make sure that both W's markers are valid. */
15388 eassert (XMARKER (w->start)->buffer == buffer);
15389 eassert (XMARKER (w->pointm)->buffer == buffer);
15390
15391 restart:
15392 reconsider_clip_changes (w);
15393 frame_line_height = default_line_pixel_height (w);
15394
15395 /* Has the mode line to be updated? */
15396 update_mode_line = (w->update_mode_line
15397 || update_mode_lines
15398 || buffer->clip_changed
15399 || buffer->prevent_redisplay_optimizations_p);
15400
15401 if (MINI_WINDOW_P (w))
15402 {
15403 if (w == XWINDOW (echo_area_window)
15404 && !NILP (echo_area_buffer[0]))
15405 {
15406 if (update_mode_line)
15407 /* We may have to update a tty frame's menu bar or a
15408 tool-bar. Example `M-x C-h C-h C-g'. */
15409 goto finish_menu_bars;
15410 else
15411 /* We've already displayed the echo area glyphs in this window. */
15412 goto finish_scroll_bars;
15413 }
15414 else if ((w != XWINDOW (minibuf_window)
15415 || minibuf_level == 0)
15416 /* When buffer is nonempty, redisplay window normally. */
15417 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15418 /* Quail displays non-mini buffers in minibuffer window.
15419 In that case, redisplay the window normally. */
15420 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15421 {
15422 /* W is a mini-buffer window, but it's not active, so clear
15423 it. */
15424 int yb = window_text_bottom_y (w);
15425 struct glyph_row *row;
15426 int y;
15427
15428 for (y = 0, row = w->desired_matrix->rows;
15429 y < yb;
15430 y += row->height, ++row)
15431 blank_row (w, row, y);
15432 goto finish_scroll_bars;
15433 }
15434
15435 clear_glyph_matrix (w->desired_matrix);
15436 }
15437
15438 /* Otherwise set up data on this window; select its buffer and point
15439 value. */
15440 /* Really select the buffer, for the sake of buffer-local
15441 variables. */
15442 set_buffer_internal_1 (XBUFFER (w->contents));
15443
15444 current_matrix_up_to_date_p
15445 = (w->window_end_valid
15446 && !current_buffer->clip_changed
15447 && !current_buffer->prevent_redisplay_optimizations_p
15448 && !window_outdated (w));
15449
15450 /* Run the window-bottom-change-functions
15451 if it is possible that the text on the screen has changed
15452 (either due to modification of the text, or any other reason). */
15453 if (!current_matrix_up_to_date_p
15454 && !NILP (Vwindow_text_change_functions))
15455 {
15456 safe_run_hooks (Qwindow_text_change_functions);
15457 goto restart;
15458 }
15459
15460 beg_unchanged = BEG_UNCHANGED;
15461 end_unchanged = END_UNCHANGED;
15462
15463 SET_TEXT_POS (opoint, PT, PT_BYTE);
15464
15465 specbind (Qinhibit_point_motion_hooks, Qt);
15466
15467 buffer_unchanged_p
15468 = (w->window_end_valid
15469 && !current_buffer->clip_changed
15470 && !window_outdated (w));
15471
15472 /* When windows_or_buffers_changed is non-zero, we can't rely
15473 on the window end being valid, so set it to zero there. */
15474 if (windows_or_buffers_changed)
15475 {
15476 /* If window starts on a continuation line, maybe adjust the
15477 window start in case the window's width changed. */
15478 if (XMARKER (w->start)->buffer == current_buffer)
15479 compute_window_start_on_continuation_line (w);
15480
15481 w->window_end_valid = 0;
15482 /* If so, we also can't rely on current matrix
15483 and should not fool try_cursor_movement below. */
15484 current_matrix_up_to_date_p = 0;
15485 }
15486
15487 /* Some sanity checks. */
15488 CHECK_WINDOW_END (w);
15489 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15490 emacs_abort ();
15491 if (BYTEPOS (opoint) < CHARPOS (opoint))
15492 emacs_abort ();
15493
15494 if (mode_line_update_needed (w))
15495 update_mode_line = 1;
15496
15497 /* Point refers normally to the selected window. For any other
15498 window, set up appropriate value. */
15499 if (!EQ (window, selected_window))
15500 {
15501 ptrdiff_t new_pt = marker_position (w->pointm);
15502 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15503 if (new_pt < BEGV)
15504 {
15505 new_pt = BEGV;
15506 new_pt_byte = BEGV_BYTE;
15507 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15508 }
15509 else if (new_pt > (ZV - 1))
15510 {
15511 new_pt = ZV;
15512 new_pt_byte = ZV_BYTE;
15513 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15514 }
15515
15516 /* We don't use SET_PT so that the point-motion hooks don't run. */
15517 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15518 }
15519
15520 /* If any of the character widths specified in the display table
15521 have changed, invalidate the width run cache. It's true that
15522 this may be a bit late to catch such changes, but the rest of
15523 redisplay goes (non-fatally) haywire when the display table is
15524 changed, so why should we worry about doing any better? */
15525 if (current_buffer->width_run_cache)
15526 {
15527 struct Lisp_Char_Table *disptab = buffer_display_table ();
15528
15529 if (! disptab_matches_widthtab
15530 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15531 {
15532 invalidate_region_cache (current_buffer,
15533 current_buffer->width_run_cache,
15534 BEG, Z);
15535 recompute_width_table (current_buffer, disptab);
15536 }
15537 }
15538
15539 /* If window-start is screwed up, choose a new one. */
15540 if (XMARKER (w->start)->buffer != current_buffer)
15541 goto recenter;
15542
15543 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15544
15545 /* If someone specified a new starting point but did not insist,
15546 check whether it can be used. */
15547 if (w->optional_new_start
15548 && CHARPOS (startp) >= BEGV
15549 && CHARPOS (startp) <= ZV)
15550 {
15551 w->optional_new_start = 0;
15552 start_display (&it, w, startp);
15553 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15554 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15555 if (IT_CHARPOS (it) == PT)
15556 w->force_start = 1;
15557 /* IT may overshoot PT if text at PT is invisible. */
15558 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15559 w->force_start = 1;
15560 }
15561
15562 force_start:
15563
15564 /* Handle case where place to start displaying has been specified,
15565 unless the specified location is outside the accessible range. */
15566 if (w->force_start || window_frozen_p (w))
15567 {
15568 /* We set this later on if we have to adjust point. */
15569 int new_vpos = -1;
15570
15571 w->force_start = 0;
15572 w->vscroll = 0;
15573 w->window_end_valid = 0;
15574
15575 /* Forget any recorded base line for line number display. */
15576 if (!buffer_unchanged_p)
15577 w->base_line_number = 0;
15578
15579 /* Redisplay the mode line. Select the buffer properly for that.
15580 Also, run the hook window-scroll-functions
15581 because we have scrolled. */
15582 /* Note, we do this after clearing force_start because
15583 if there's an error, it is better to forget about force_start
15584 than to get into an infinite loop calling the hook functions
15585 and having them get more errors. */
15586 if (!update_mode_line
15587 || ! NILP (Vwindow_scroll_functions))
15588 {
15589 update_mode_line = 1;
15590 w->update_mode_line = 1;
15591 startp = run_window_scroll_functions (window, startp);
15592 }
15593
15594 if (CHARPOS (startp) < BEGV)
15595 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15596 else if (CHARPOS (startp) > ZV)
15597 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15598
15599 /* Redisplay, then check if cursor has been set during the
15600 redisplay. Give up if new fonts were loaded. */
15601 /* We used to issue a CHECK_MARGINS argument to try_window here,
15602 but this causes scrolling to fail when point begins inside
15603 the scroll margin (bug#148) -- cyd */
15604 if (!try_window (window, startp, 0))
15605 {
15606 w->force_start = 1;
15607 clear_glyph_matrix (w->desired_matrix);
15608 goto need_larger_matrices;
15609 }
15610
15611 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15612 {
15613 /* If point does not appear, try to move point so it does
15614 appear. The desired matrix has been built above, so we
15615 can use it here. */
15616 new_vpos = window_box_height (w) / 2;
15617 }
15618
15619 if (!cursor_row_fully_visible_p (w, 0, 0))
15620 {
15621 /* Point does appear, but on a line partly visible at end of window.
15622 Move it back to a fully-visible line. */
15623 new_vpos = window_box_height (w);
15624 }
15625 else if (w->cursor.vpos >=0)
15626 {
15627 /* Some people insist on not letting point enter the scroll
15628 margin, even though this part handles windows that didn't
15629 scroll at all. */
15630 int window_total_lines
15631 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15632 int margin = min (scroll_margin, window_total_lines / 4);
15633 int pixel_margin = margin * frame_line_height;
15634 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15635
15636 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15637 below, which finds the row to move point to, advances by
15638 the Y coordinate of the _next_ row, see the definition of
15639 MATRIX_ROW_BOTTOM_Y. */
15640 if (w->cursor.vpos < margin + header_line)
15641 {
15642 w->cursor.vpos = -1;
15643 clear_glyph_matrix (w->desired_matrix);
15644 goto try_to_scroll;
15645 }
15646 else
15647 {
15648 int window_height = window_box_height (w);
15649
15650 if (header_line)
15651 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15652 if (w->cursor.y >= window_height - pixel_margin)
15653 {
15654 w->cursor.vpos = -1;
15655 clear_glyph_matrix (w->desired_matrix);
15656 goto try_to_scroll;
15657 }
15658 }
15659 }
15660
15661 /* If we need to move point for either of the above reasons,
15662 now actually do it. */
15663 if (new_vpos >= 0)
15664 {
15665 struct glyph_row *row;
15666
15667 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15668 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15669 ++row;
15670
15671 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15672 MATRIX_ROW_START_BYTEPOS (row));
15673
15674 if (w != XWINDOW (selected_window))
15675 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15676 else if (current_buffer == old)
15677 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15678
15679 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15680
15681 /* If we are highlighting the region, then we just changed
15682 the region, so redisplay to show it. */
15683 if (markpos_of_region () >= 0)
15684 {
15685 clear_glyph_matrix (w->desired_matrix);
15686 if (!try_window (window, startp, 0))
15687 goto need_larger_matrices;
15688 }
15689 }
15690
15691 #ifdef GLYPH_DEBUG
15692 debug_method_add (w, "forced window start");
15693 #endif
15694 goto done;
15695 }
15696
15697 /* Handle case where text has not changed, only point, and it has
15698 not moved off the frame, and we are not retrying after hscroll.
15699 (current_matrix_up_to_date_p is nonzero when retrying.) */
15700 if (current_matrix_up_to_date_p
15701 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15702 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15703 {
15704 switch (rc)
15705 {
15706 case CURSOR_MOVEMENT_SUCCESS:
15707 used_current_matrix_p = 1;
15708 goto done;
15709
15710 case CURSOR_MOVEMENT_MUST_SCROLL:
15711 goto try_to_scroll;
15712
15713 default:
15714 emacs_abort ();
15715 }
15716 }
15717 /* If current starting point was originally the beginning of a line
15718 but no longer is, find a new starting point. */
15719 else if (w->start_at_line_beg
15720 && !(CHARPOS (startp) <= BEGV
15721 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15722 {
15723 #ifdef GLYPH_DEBUG
15724 debug_method_add (w, "recenter 1");
15725 #endif
15726 goto recenter;
15727 }
15728
15729 /* Try scrolling with try_window_id. Value is > 0 if update has
15730 been done, it is -1 if we know that the same window start will
15731 not work. It is 0 if unsuccessful for some other reason. */
15732 else if ((tem = try_window_id (w)) != 0)
15733 {
15734 #ifdef GLYPH_DEBUG
15735 debug_method_add (w, "try_window_id %d", tem);
15736 #endif
15737
15738 if (f->fonts_changed)
15739 goto need_larger_matrices;
15740 if (tem > 0)
15741 goto done;
15742
15743 /* Otherwise try_window_id has returned -1 which means that we
15744 don't want the alternative below this comment to execute. */
15745 }
15746 else if (CHARPOS (startp) >= BEGV
15747 && CHARPOS (startp) <= ZV
15748 && PT >= CHARPOS (startp)
15749 && (CHARPOS (startp) < ZV
15750 /* Avoid starting at end of buffer. */
15751 || CHARPOS (startp) == BEGV
15752 || !window_outdated (w)))
15753 {
15754 int d1, d2, d3, d4, d5, d6;
15755
15756 /* If first window line is a continuation line, and window start
15757 is inside the modified region, but the first change is before
15758 current window start, we must select a new window start.
15759
15760 However, if this is the result of a down-mouse event (e.g. by
15761 extending the mouse-drag-overlay), we don't want to select a
15762 new window start, since that would change the position under
15763 the mouse, resulting in an unwanted mouse-movement rather
15764 than a simple mouse-click. */
15765 if (!w->start_at_line_beg
15766 && NILP (do_mouse_tracking)
15767 && CHARPOS (startp) > BEGV
15768 && CHARPOS (startp) > BEG + beg_unchanged
15769 && CHARPOS (startp) <= Z - end_unchanged
15770 /* Even if w->start_at_line_beg is nil, a new window may
15771 start at a line_beg, since that's how set_buffer_window
15772 sets it. So, we need to check the return value of
15773 compute_window_start_on_continuation_line. (See also
15774 bug#197). */
15775 && XMARKER (w->start)->buffer == current_buffer
15776 && compute_window_start_on_continuation_line (w)
15777 /* It doesn't make sense to force the window start like we
15778 do at label force_start if it is already known that point
15779 will not be visible in the resulting window, because
15780 doing so will move point from its correct position
15781 instead of scrolling the window to bring point into view.
15782 See bug#9324. */
15783 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15784 {
15785 w->force_start = 1;
15786 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15787 goto force_start;
15788 }
15789
15790 #ifdef GLYPH_DEBUG
15791 debug_method_add (w, "same window start");
15792 #endif
15793
15794 /* Try to redisplay starting at same place as before.
15795 If point has not moved off frame, accept the results. */
15796 if (!current_matrix_up_to_date_p
15797 /* Don't use try_window_reusing_current_matrix in this case
15798 because a window scroll function can have changed the
15799 buffer. */
15800 || !NILP (Vwindow_scroll_functions)
15801 || MINI_WINDOW_P (w)
15802 || !(used_current_matrix_p
15803 = try_window_reusing_current_matrix (w)))
15804 {
15805 IF_DEBUG (debug_method_add (w, "1"));
15806 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15807 /* -1 means we need to scroll.
15808 0 means we need new matrices, but fonts_changed
15809 is set in that case, so we will detect it below. */
15810 goto try_to_scroll;
15811 }
15812
15813 if (f->fonts_changed)
15814 goto need_larger_matrices;
15815
15816 if (w->cursor.vpos >= 0)
15817 {
15818 if (!just_this_one_p
15819 || current_buffer->clip_changed
15820 || BEG_UNCHANGED < CHARPOS (startp))
15821 /* Forget any recorded base line for line number display. */
15822 w->base_line_number = 0;
15823
15824 if (!cursor_row_fully_visible_p (w, 1, 0))
15825 {
15826 clear_glyph_matrix (w->desired_matrix);
15827 last_line_misfit = 1;
15828 }
15829 /* Drop through and scroll. */
15830 else
15831 goto done;
15832 }
15833 else
15834 clear_glyph_matrix (w->desired_matrix);
15835 }
15836
15837 try_to_scroll:
15838
15839 /* Redisplay the mode line. Select the buffer properly for that. */
15840 if (!update_mode_line)
15841 {
15842 update_mode_line = 1;
15843 w->update_mode_line = 1;
15844 }
15845
15846 /* Try to scroll by specified few lines. */
15847 if ((scroll_conservatively
15848 || emacs_scroll_step
15849 || temp_scroll_step
15850 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15851 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15852 && CHARPOS (startp) >= BEGV
15853 && CHARPOS (startp) <= ZV)
15854 {
15855 /* The function returns -1 if new fonts were loaded, 1 if
15856 successful, 0 if not successful. */
15857 int ss = try_scrolling (window, just_this_one_p,
15858 scroll_conservatively,
15859 emacs_scroll_step,
15860 temp_scroll_step, last_line_misfit);
15861 switch (ss)
15862 {
15863 case SCROLLING_SUCCESS:
15864 goto done;
15865
15866 case SCROLLING_NEED_LARGER_MATRICES:
15867 goto need_larger_matrices;
15868
15869 case SCROLLING_FAILED:
15870 break;
15871
15872 default:
15873 emacs_abort ();
15874 }
15875 }
15876
15877 /* Finally, just choose a place to start which positions point
15878 according to user preferences. */
15879
15880 recenter:
15881
15882 #ifdef GLYPH_DEBUG
15883 debug_method_add (w, "recenter");
15884 #endif
15885
15886 /* Forget any previously recorded base line for line number display. */
15887 if (!buffer_unchanged_p)
15888 w->base_line_number = 0;
15889
15890 /* Determine the window start relative to point. */
15891 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15892 it.current_y = it.last_visible_y;
15893 if (centering_position < 0)
15894 {
15895 int window_total_lines
15896 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15897 int margin =
15898 scroll_margin > 0
15899 ? min (scroll_margin, window_total_lines / 4)
15900 : 0;
15901 ptrdiff_t margin_pos = CHARPOS (startp);
15902 Lisp_Object aggressive;
15903 int scrolling_up;
15904
15905 /* If there is a scroll margin at the top of the window, find
15906 its character position. */
15907 if (margin
15908 /* Cannot call start_display if startp is not in the
15909 accessible region of the buffer. This can happen when we
15910 have just switched to a different buffer and/or changed
15911 its restriction. In that case, startp is initialized to
15912 the character position 1 (BEGV) because we did not yet
15913 have chance to display the buffer even once. */
15914 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15915 {
15916 struct it it1;
15917 void *it1data = NULL;
15918
15919 SAVE_IT (it1, it, it1data);
15920 start_display (&it1, w, startp);
15921 move_it_vertically (&it1, margin * frame_line_height);
15922 margin_pos = IT_CHARPOS (it1);
15923 RESTORE_IT (&it, &it, it1data);
15924 }
15925 scrolling_up = PT > margin_pos;
15926 aggressive =
15927 scrolling_up
15928 ? BVAR (current_buffer, scroll_up_aggressively)
15929 : BVAR (current_buffer, scroll_down_aggressively);
15930
15931 if (!MINI_WINDOW_P (w)
15932 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15933 {
15934 int pt_offset = 0;
15935
15936 /* Setting scroll-conservatively overrides
15937 scroll-*-aggressively. */
15938 if (!scroll_conservatively && NUMBERP (aggressive))
15939 {
15940 double float_amount = XFLOATINT (aggressive);
15941
15942 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15943 if (pt_offset == 0 && float_amount > 0)
15944 pt_offset = 1;
15945 if (pt_offset && margin > 0)
15946 margin -= 1;
15947 }
15948 /* Compute how much to move the window start backward from
15949 point so that point will be displayed where the user
15950 wants it. */
15951 if (scrolling_up)
15952 {
15953 centering_position = it.last_visible_y;
15954 if (pt_offset)
15955 centering_position -= pt_offset;
15956 centering_position -=
15957 frame_line_height * (1 + margin + (last_line_misfit != 0))
15958 + WINDOW_HEADER_LINE_HEIGHT (w);
15959 /* Don't let point enter the scroll margin near top of
15960 the window. */
15961 if (centering_position < margin * frame_line_height)
15962 centering_position = margin * frame_line_height;
15963 }
15964 else
15965 centering_position = margin * frame_line_height + pt_offset;
15966 }
15967 else
15968 /* Set the window start half the height of the window backward
15969 from point. */
15970 centering_position = window_box_height (w) / 2;
15971 }
15972 move_it_vertically_backward (&it, centering_position);
15973
15974 eassert (IT_CHARPOS (it) >= BEGV);
15975
15976 /* The function move_it_vertically_backward may move over more
15977 than the specified y-distance. If it->w is small, e.g. a
15978 mini-buffer window, we may end up in front of the window's
15979 display area. Start displaying at the start of the line
15980 containing PT in this case. */
15981 if (it.current_y <= 0)
15982 {
15983 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15984 move_it_vertically_backward (&it, 0);
15985 it.current_y = 0;
15986 }
15987
15988 it.current_x = it.hpos = 0;
15989
15990 /* Set the window start position here explicitly, to avoid an
15991 infinite loop in case the functions in window-scroll-functions
15992 get errors. */
15993 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15994
15995 /* Run scroll hooks. */
15996 startp = run_window_scroll_functions (window, it.current.pos);
15997
15998 /* Redisplay the window. */
15999 if (!current_matrix_up_to_date_p
16000 || windows_or_buffers_changed
16001 || f->cursor_type_changed
16002 /* Don't use try_window_reusing_current_matrix in this case
16003 because it can have changed the buffer. */
16004 || !NILP (Vwindow_scroll_functions)
16005 || !just_this_one_p
16006 || MINI_WINDOW_P (w)
16007 || !(used_current_matrix_p
16008 = try_window_reusing_current_matrix (w)))
16009 try_window (window, startp, 0);
16010
16011 /* If new fonts have been loaded (due to fontsets), give up. We
16012 have to start a new redisplay since we need to re-adjust glyph
16013 matrices. */
16014 if (f->fonts_changed)
16015 goto need_larger_matrices;
16016
16017 /* If cursor did not appear assume that the middle of the window is
16018 in the first line of the window. Do it again with the next line.
16019 (Imagine a window of height 100, displaying two lines of height
16020 60. Moving back 50 from it->last_visible_y will end in the first
16021 line.) */
16022 if (w->cursor.vpos < 0)
16023 {
16024 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16025 {
16026 clear_glyph_matrix (w->desired_matrix);
16027 move_it_by_lines (&it, 1);
16028 try_window (window, it.current.pos, 0);
16029 }
16030 else if (PT < IT_CHARPOS (it))
16031 {
16032 clear_glyph_matrix (w->desired_matrix);
16033 move_it_by_lines (&it, -1);
16034 try_window (window, it.current.pos, 0);
16035 }
16036 else
16037 {
16038 /* Not much we can do about it. */
16039 }
16040 }
16041
16042 /* Consider the following case: Window starts at BEGV, there is
16043 invisible, intangible text at BEGV, so that display starts at
16044 some point START > BEGV. It can happen that we are called with
16045 PT somewhere between BEGV and START. Try to handle that case. */
16046 if (w->cursor.vpos < 0)
16047 {
16048 struct glyph_row *row = w->current_matrix->rows;
16049 if (row->mode_line_p)
16050 ++row;
16051 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16052 }
16053
16054 if (!cursor_row_fully_visible_p (w, 0, 0))
16055 {
16056 /* If vscroll is enabled, disable it and try again. */
16057 if (w->vscroll)
16058 {
16059 w->vscroll = 0;
16060 clear_glyph_matrix (w->desired_matrix);
16061 goto recenter;
16062 }
16063
16064 /* Users who set scroll-conservatively to a large number want
16065 point just above/below the scroll margin. If we ended up
16066 with point's row partially visible, move the window start to
16067 make that row fully visible and out of the margin. */
16068 if (scroll_conservatively > SCROLL_LIMIT)
16069 {
16070 int window_total_lines
16071 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16072 int margin =
16073 scroll_margin > 0
16074 ? min (scroll_margin, window_total_lines / 4)
16075 : 0;
16076 int move_down = w->cursor.vpos >= window_total_lines / 2;
16077
16078 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16079 clear_glyph_matrix (w->desired_matrix);
16080 if (1 == try_window (window, it.current.pos,
16081 TRY_WINDOW_CHECK_MARGINS))
16082 goto done;
16083 }
16084
16085 /* If centering point failed to make the whole line visible,
16086 put point at the top instead. That has to make the whole line
16087 visible, if it can be done. */
16088 if (centering_position == 0)
16089 goto done;
16090
16091 clear_glyph_matrix (w->desired_matrix);
16092 centering_position = 0;
16093 goto recenter;
16094 }
16095
16096 done:
16097
16098 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16099 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16100 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16101
16102 /* Display the mode line, if we must. */
16103 if ((update_mode_line
16104 /* If window not full width, must redo its mode line
16105 if (a) the window to its side is being redone and
16106 (b) we do a frame-based redisplay. This is a consequence
16107 of how inverted lines are drawn in frame-based redisplay. */
16108 || (!just_this_one_p
16109 && !FRAME_WINDOW_P (f)
16110 && !WINDOW_FULL_WIDTH_P (w))
16111 /* Line number to display. */
16112 || w->base_line_pos > 0
16113 /* Column number is displayed and different from the one displayed. */
16114 || (w->column_number_displayed != -1
16115 && (w->column_number_displayed != current_column ())))
16116 /* This means that the window has a mode line. */
16117 && (WINDOW_WANTS_MODELINE_P (w)
16118 || WINDOW_WANTS_HEADER_LINE_P (w)))
16119 {
16120 display_mode_lines (w);
16121
16122 /* If mode line height has changed, arrange for a thorough
16123 immediate redisplay using the correct mode line height. */
16124 if (WINDOW_WANTS_MODELINE_P (w)
16125 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16126 {
16127 f->fonts_changed = 1;
16128 w->mode_line_height = -1;
16129 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16130 = DESIRED_MODE_LINE_HEIGHT (w);
16131 }
16132
16133 /* If header line height has changed, arrange for a thorough
16134 immediate redisplay using the correct header line height. */
16135 if (WINDOW_WANTS_HEADER_LINE_P (w)
16136 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16137 {
16138 f->fonts_changed = 1;
16139 w->header_line_height = -1;
16140 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16141 = DESIRED_HEADER_LINE_HEIGHT (w);
16142 }
16143
16144 if (f->fonts_changed)
16145 goto need_larger_matrices;
16146 }
16147
16148 if (!line_number_displayed && w->base_line_pos != -1)
16149 {
16150 w->base_line_pos = 0;
16151 w->base_line_number = 0;
16152 }
16153
16154 finish_menu_bars:
16155
16156 /* When we reach a frame's selected window, redo the frame's menu bar. */
16157 if (update_mode_line
16158 && EQ (FRAME_SELECTED_WINDOW (f), window))
16159 {
16160 int redisplay_menu_p = 0;
16161
16162 if (FRAME_WINDOW_P (f))
16163 {
16164 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16165 || defined (HAVE_NS) || defined (USE_GTK)
16166 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16167 #else
16168 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16169 #endif
16170 }
16171 else
16172 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16173
16174 if (redisplay_menu_p)
16175 display_menu_bar (w);
16176
16177 #ifdef HAVE_WINDOW_SYSTEM
16178 if (FRAME_WINDOW_P (f))
16179 {
16180 #if defined (USE_GTK) || defined (HAVE_NS)
16181 if (FRAME_EXTERNAL_TOOL_BAR (f))
16182 redisplay_tool_bar (f);
16183 #else
16184 if (WINDOWP (f->tool_bar_window)
16185 && (FRAME_TOOL_BAR_LINES (f) > 0
16186 || !NILP (Vauto_resize_tool_bars))
16187 && redisplay_tool_bar (f))
16188 ignore_mouse_drag_p = 1;
16189 #endif
16190 }
16191 #endif
16192 }
16193
16194 #ifdef HAVE_WINDOW_SYSTEM
16195 if (FRAME_WINDOW_P (f)
16196 && update_window_fringes (w, (just_this_one_p
16197 || (!used_current_matrix_p && !overlay_arrow_seen)
16198 || w->pseudo_window_p)))
16199 {
16200 update_begin (f);
16201 block_input ();
16202 if (draw_window_fringes (w, 1))
16203 x_draw_vertical_border (w);
16204 unblock_input ();
16205 update_end (f);
16206 }
16207 #endif /* HAVE_WINDOW_SYSTEM */
16208
16209 /* We go to this label, with fonts_changed set, if it is
16210 necessary to try again using larger glyph matrices.
16211 We have to redeem the scroll bar even in this case,
16212 because the loop in redisplay_internal expects that. */
16213 need_larger_matrices:
16214 ;
16215 finish_scroll_bars:
16216
16217 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16218 {
16219 /* Set the thumb's position and size. */
16220 set_vertical_scroll_bar (w);
16221
16222 /* Note that we actually used the scroll bar attached to this
16223 window, so it shouldn't be deleted at the end of redisplay. */
16224 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16225 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16226 }
16227
16228 /* Restore current_buffer and value of point in it. The window
16229 update may have changed the buffer, so first make sure `opoint'
16230 is still valid (Bug#6177). */
16231 if (CHARPOS (opoint) < BEGV)
16232 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16233 else if (CHARPOS (opoint) > ZV)
16234 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16235 else
16236 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16237
16238 set_buffer_internal_1 (old);
16239 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16240 shorter. This can be caused by log truncation in *Messages*. */
16241 if (CHARPOS (lpoint) <= ZV)
16242 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16243
16244 unbind_to (count, Qnil);
16245 }
16246
16247
16248 /* Build the complete desired matrix of WINDOW with a window start
16249 buffer position POS.
16250
16251 Value is 1 if successful. It is zero if fonts were loaded during
16252 redisplay which makes re-adjusting glyph matrices necessary, and -1
16253 if point would appear in the scroll margins.
16254 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16255 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16256 set in FLAGS.) */
16257
16258 int
16259 try_window (Lisp_Object window, struct text_pos pos, int flags)
16260 {
16261 struct window *w = XWINDOW (window);
16262 struct it it;
16263 struct glyph_row *last_text_row = NULL;
16264 struct frame *f = XFRAME (w->frame);
16265 int frame_line_height = default_line_pixel_height (w);
16266
16267 /* Make POS the new window start. */
16268 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16269
16270 /* Mark cursor position as unknown. No overlay arrow seen. */
16271 w->cursor.vpos = -1;
16272 overlay_arrow_seen = 0;
16273
16274 /* Initialize iterator and info to start at POS. */
16275 start_display (&it, w, pos);
16276
16277 /* Display all lines of W. */
16278 while (it.current_y < it.last_visible_y)
16279 {
16280 if (display_line (&it))
16281 last_text_row = it.glyph_row - 1;
16282 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16283 return 0;
16284 }
16285
16286 /* Don't let the cursor end in the scroll margins. */
16287 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16288 && !MINI_WINDOW_P (w))
16289 {
16290 int this_scroll_margin;
16291 int window_total_lines
16292 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16293
16294 if (scroll_margin > 0)
16295 {
16296 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16297 this_scroll_margin *= frame_line_height;
16298 }
16299 else
16300 this_scroll_margin = 0;
16301
16302 if ((w->cursor.y >= 0 /* not vscrolled */
16303 && w->cursor.y < this_scroll_margin
16304 && CHARPOS (pos) > BEGV
16305 && IT_CHARPOS (it) < ZV)
16306 /* rms: considering make_cursor_line_fully_visible_p here
16307 seems to give wrong results. We don't want to recenter
16308 when the last line is partly visible, we want to allow
16309 that case to be handled in the usual way. */
16310 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16311 {
16312 w->cursor.vpos = -1;
16313 clear_glyph_matrix (w->desired_matrix);
16314 return -1;
16315 }
16316 }
16317
16318 /* If bottom moved off end of frame, change mode line percentage. */
16319 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16320 w->update_mode_line = 1;
16321
16322 /* Set window_end_pos to the offset of the last character displayed
16323 on the window from the end of current_buffer. Set
16324 window_end_vpos to its row number. */
16325 if (last_text_row)
16326 {
16327 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16328 adjust_window_ends (w, last_text_row, 0);
16329 eassert
16330 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16331 w->window_end_vpos)));
16332 }
16333 else
16334 {
16335 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16336 w->window_end_pos = Z - ZV;
16337 w->window_end_vpos = 0;
16338 }
16339
16340 /* But that is not valid info until redisplay finishes. */
16341 w->window_end_valid = 0;
16342 return 1;
16343 }
16344
16345
16346 \f
16347 /************************************************************************
16348 Window redisplay reusing current matrix when buffer has not changed
16349 ************************************************************************/
16350
16351 /* Try redisplay of window W showing an unchanged buffer with a
16352 different window start than the last time it was displayed by
16353 reusing its current matrix. Value is non-zero if successful.
16354 W->start is the new window start. */
16355
16356 static int
16357 try_window_reusing_current_matrix (struct window *w)
16358 {
16359 struct frame *f = XFRAME (w->frame);
16360 struct glyph_row *bottom_row;
16361 struct it it;
16362 struct run run;
16363 struct text_pos start, new_start;
16364 int nrows_scrolled, i;
16365 struct glyph_row *last_text_row;
16366 struct glyph_row *last_reused_text_row;
16367 struct glyph_row *start_row;
16368 int start_vpos, min_y, max_y;
16369
16370 #ifdef GLYPH_DEBUG
16371 if (inhibit_try_window_reusing)
16372 return 0;
16373 #endif
16374
16375 if (/* This function doesn't handle terminal frames. */
16376 !FRAME_WINDOW_P (f)
16377 /* Don't try to reuse the display if windows have been split
16378 or such. */
16379 || windows_or_buffers_changed
16380 || f->cursor_type_changed)
16381 return 0;
16382
16383 /* Can't do this if region may have changed. */
16384 if (markpos_of_region () >= 0
16385 || w->region_showing
16386 || !NILP (Vshow_trailing_whitespace))
16387 return 0;
16388
16389 /* If top-line visibility has changed, give up. */
16390 if (WINDOW_WANTS_HEADER_LINE_P (w)
16391 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16392 return 0;
16393
16394 /* Give up if old or new display is scrolled vertically. We could
16395 make this function handle this, but right now it doesn't. */
16396 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16397 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16398 return 0;
16399
16400 /* The variable new_start now holds the new window start. The old
16401 start `start' can be determined from the current matrix. */
16402 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16403 start = start_row->minpos;
16404 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16405
16406 /* Clear the desired matrix for the display below. */
16407 clear_glyph_matrix (w->desired_matrix);
16408
16409 if (CHARPOS (new_start) <= CHARPOS (start))
16410 {
16411 /* Don't use this method if the display starts with an ellipsis
16412 displayed for invisible text. It's not easy to handle that case
16413 below, and it's certainly not worth the effort since this is
16414 not a frequent case. */
16415 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16416 return 0;
16417
16418 IF_DEBUG (debug_method_add (w, "twu1"));
16419
16420 /* Display up to a row that can be reused. The variable
16421 last_text_row is set to the last row displayed that displays
16422 text. Note that it.vpos == 0 if or if not there is a
16423 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16424 start_display (&it, w, new_start);
16425 w->cursor.vpos = -1;
16426 last_text_row = last_reused_text_row = NULL;
16427
16428 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16429 {
16430 /* If we have reached into the characters in the START row,
16431 that means the line boundaries have changed. So we
16432 can't start copying with the row START. Maybe it will
16433 work to start copying with the following row. */
16434 while (IT_CHARPOS (it) > CHARPOS (start))
16435 {
16436 /* Advance to the next row as the "start". */
16437 start_row++;
16438 start = start_row->minpos;
16439 /* If there are no more rows to try, or just one, give up. */
16440 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16441 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16442 || CHARPOS (start) == ZV)
16443 {
16444 clear_glyph_matrix (w->desired_matrix);
16445 return 0;
16446 }
16447
16448 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16449 }
16450 /* If we have reached alignment, we can copy the rest of the
16451 rows. */
16452 if (IT_CHARPOS (it) == CHARPOS (start)
16453 /* Don't accept "alignment" inside a display vector,
16454 since start_row could have started in the middle of
16455 that same display vector (thus their character
16456 positions match), and we have no way of telling if
16457 that is the case. */
16458 && it.current.dpvec_index < 0)
16459 break;
16460
16461 if (display_line (&it))
16462 last_text_row = it.glyph_row - 1;
16463
16464 }
16465
16466 /* A value of current_y < last_visible_y means that we stopped
16467 at the previous window start, which in turn means that we
16468 have at least one reusable row. */
16469 if (it.current_y < it.last_visible_y)
16470 {
16471 struct glyph_row *row;
16472
16473 /* IT.vpos always starts from 0; it counts text lines. */
16474 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16475
16476 /* Find PT if not already found in the lines displayed. */
16477 if (w->cursor.vpos < 0)
16478 {
16479 int dy = it.current_y - start_row->y;
16480
16481 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16482 row = row_containing_pos (w, PT, row, NULL, dy);
16483 if (row)
16484 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16485 dy, nrows_scrolled);
16486 else
16487 {
16488 clear_glyph_matrix (w->desired_matrix);
16489 return 0;
16490 }
16491 }
16492
16493 /* Scroll the display. Do it before the current matrix is
16494 changed. The problem here is that update has not yet
16495 run, i.e. part of the current matrix is not up to date.
16496 scroll_run_hook will clear the cursor, and use the
16497 current matrix to get the height of the row the cursor is
16498 in. */
16499 run.current_y = start_row->y;
16500 run.desired_y = it.current_y;
16501 run.height = it.last_visible_y - it.current_y;
16502
16503 if (run.height > 0 && run.current_y != run.desired_y)
16504 {
16505 update_begin (f);
16506 FRAME_RIF (f)->update_window_begin_hook (w);
16507 FRAME_RIF (f)->clear_window_mouse_face (w);
16508 FRAME_RIF (f)->scroll_run_hook (w, &run);
16509 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16510 update_end (f);
16511 }
16512
16513 /* Shift current matrix down by nrows_scrolled lines. */
16514 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16515 rotate_matrix (w->current_matrix,
16516 start_vpos,
16517 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16518 nrows_scrolled);
16519
16520 /* Disable lines that must be updated. */
16521 for (i = 0; i < nrows_scrolled; ++i)
16522 (start_row + i)->enabled_p = 0;
16523
16524 /* Re-compute Y positions. */
16525 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16526 max_y = it.last_visible_y;
16527 for (row = start_row + nrows_scrolled;
16528 row < bottom_row;
16529 ++row)
16530 {
16531 row->y = it.current_y;
16532 row->visible_height = row->height;
16533
16534 if (row->y < min_y)
16535 row->visible_height -= min_y - row->y;
16536 if (row->y + row->height > max_y)
16537 row->visible_height -= row->y + row->height - max_y;
16538 if (row->fringe_bitmap_periodic_p)
16539 row->redraw_fringe_bitmaps_p = 1;
16540
16541 it.current_y += row->height;
16542
16543 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16544 last_reused_text_row = row;
16545 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16546 break;
16547 }
16548
16549 /* Disable lines in the current matrix which are now
16550 below the window. */
16551 for (++row; row < bottom_row; ++row)
16552 row->enabled_p = row->mode_line_p = 0;
16553 }
16554
16555 /* Update window_end_pos etc.; last_reused_text_row is the last
16556 reused row from the current matrix containing text, if any.
16557 The value of last_text_row is the last displayed line
16558 containing text. */
16559 if (last_reused_text_row)
16560 adjust_window_ends (w, last_reused_text_row, 1);
16561 else if (last_text_row)
16562 adjust_window_ends (w, last_text_row, 0);
16563 else
16564 {
16565 /* This window must be completely empty. */
16566 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16567 w->window_end_pos = Z - ZV;
16568 w->window_end_vpos = 0;
16569 }
16570 w->window_end_valid = 0;
16571
16572 /* Update hint: don't try scrolling again in update_window. */
16573 w->desired_matrix->no_scrolling_p = 1;
16574
16575 #ifdef GLYPH_DEBUG
16576 debug_method_add (w, "try_window_reusing_current_matrix 1");
16577 #endif
16578 return 1;
16579 }
16580 else if (CHARPOS (new_start) > CHARPOS (start))
16581 {
16582 struct glyph_row *pt_row, *row;
16583 struct glyph_row *first_reusable_row;
16584 struct glyph_row *first_row_to_display;
16585 int dy;
16586 int yb = window_text_bottom_y (w);
16587
16588 /* Find the row starting at new_start, if there is one. Don't
16589 reuse a partially visible line at the end. */
16590 first_reusable_row = start_row;
16591 while (first_reusable_row->enabled_p
16592 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16593 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16594 < CHARPOS (new_start)))
16595 ++first_reusable_row;
16596
16597 /* Give up if there is no row to reuse. */
16598 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16599 || !first_reusable_row->enabled_p
16600 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16601 != CHARPOS (new_start)))
16602 return 0;
16603
16604 /* We can reuse fully visible rows beginning with
16605 first_reusable_row to the end of the window. Set
16606 first_row_to_display to the first row that cannot be reused.
16607 Set pt_row to the row containing point, if there is any. */
16608 pt_row = NULL;
16609 for (first_row_to_display = first_reusable_row;
16610 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16611 ++first_row_to_display)
16612 {
16613 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16614 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16615 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16616 && first_row_to_display->ends_at_zv_p
16617 && pt_row == NULL)))
16618 pt_row = first_row_to_display;
16619 }
16620
16621 /* Start displaying at the start of first_row_to_display. */
16622 eassert (first_row_to_display->y < yb);
16623 init_to_row_start (&it, w, first_row_to_display);
16624
16625 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16626 - start_vpos);
16627 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16628 - nrows_scrolled);
16629 it.current_y = (first_row_to_display->y - first_reusable_row->y
16630 + WINDOW_HEADER_LINE_HEIGHT (w));
16631
16632 /* Display lines beginning with first_row_to_display in the
16633 desired matrix. Set last_text_row to the last row displayed
16634 that displays text. */
16635 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16636 if (pt_row == NULL)
16637 w->cursor.vpos = -1;
16638 last_text_row = NULL;
16639 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16640 if (display_line (&it))
16641 last_text_row = it.glyph_row - 1;
16642
16643 /* If point is in a reused row, adjust y and vpos of the cursor
16644 position. */
16645 if (pt_row)
16646 {
16647 w->cursor.vpos -= nrows_scrolled;
16648 w->cursor.y -= first_reusable_row->y - start_row->y;
16649 }
16650
16651 /* Give up if point isn't in a row displayed or reused. (This
16652 also handles the case where w->cursor.vpos < nrows_scrolled
16653 after the calls to display_line, which can happen with scroll
16654 margins. See bug#1295.) */
16655 if (w->cursor.vpos < 0)
16656 {
16657 clear_glyph_matrix (w->desired_matrix);
16658 return 0;
16659 }
16660
16661 /* Scroll the display. */
16662 run.current_y = first_reusable_row->y;
16663 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16664 run.height = it.last_visible_y - run.current_y;
16665 dy = run.current_y - run.desired_y;
16666
16667 if (run.height)
16668 {
16669 update_begin (f);
16670 FRAME_RIF (f)->update_window_begin_hook (w);
16671 FRAME_RIF (f)->clear_window_mouse_face (w);
16672 FRAME_RIF (f)->scroll_run_hook (w, &run);
16673 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16674 update_end (f);
16675 }
16676
16677 /* Adjust Y positions of reused rows. */
16678 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16679 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16680 max_y = it.last_visible_y;
16681 for (row = first_reusable_row; row < first_row_to_display; ++row)
16682 {
16683 row->y -= dy;
16684 row->visible_height = row->height;
16685 if (row->y < min_y)
16686 row->visible_height -= min_y - row->y;
16687 if (row->y + row->height > max_y)
16688 row->visible_height -= row->y + row->height - max_y;
16689 if (row->fringe_bitmap_periodic_p)
16690 row->redraw_fringe_bitmaps_p = 1;
16691 }
16692
16693 /* Scroll the current matrix. */
16694 eassert (nrows_scrolled > 0);
16695 rotate_matrix (w->current_matrix,
16696 start_vpos,
16697 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16698 -nrows_scrolled);
16699
16700 /* Disable rows not reused. */
16701 for (row -= nrows_scrolled; row < bottom_row; ++row)
16702 row->enabled_p = 0;
16703
16704 /* Point may have moved to a different line, so we cannot assume that
16705 the previous cursor position is valid; locate the correct row. */
16706 if (pt_row)
16707 {
16708 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16709 row < bottom_row
16710 && PT >= MATRIX_ROW_END_CHARPOS (row)
16711 && !row->ends_at_zv_p;
16712 row++)
16713 {
16714 w->cursor.vpos++;
16715 w->cursor.y = row->y;
16716 }
16717 if (row < bottom_row)
16718 {
16719 /* Can't simply scan the row for point with
16720 bidi-reordered glyph rows. Let set_cursor_from_row
16721 figure out where to put the cursor, and if it fails,
16722 give up. */
16723 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16724 {
16725 if (!set_cursor_from_row (w, row, w->current_matrix,
16726 0, 0, 0, 0))
16727 {
16728 clear_glyph_matrix (w->desired_matrix);
16729 return 0;
16730 }
16731 }
16732 else
16733 {
16734 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16735 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16736
16737 for (; glyph < end
16738 && (!BUFFERP (glyph->object)
16739 || glyph->charpos < PT);
16740 glyph++)
16741 {
16742 w->cursor.hpos++;
16743 w->cursor.x += glyph->pixel_width;
16744 }
16745 }
16746 }
16747 }
16748
16749 /* Adjust window end. A null value of last_text_row means that
16750 the window end is in reused rows which in turn means that
16751 only its vpos can have changed. */
16752 if (last_text_row)
16753 adjust_window_ends (w, last_text_row, 0);
16754 else
16755 w->window_end_vpos -= nrows_scrolled;
16756
16757 w->window_end_valid = 0;
16758 w->desired_matrix->no_scrolling_p = 1;
16759
16760 #ifdef GLYPH_DEBUG
16761 debug_method_add (w, "try_window_reusing_current_matrix 2");
16762 #endif
16763 return 1;
16764 }
16765
16766 return 0;
16767 }
16768
16769
16770 \f
16771 /************************************************************************
16772 Window redisplay reusing current matrix when buffer has changed
16773 ************************************************************************/
16774
16775 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16776 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16777 ptrdiff_t *, ptrdiff_t *);
16778 static struct glyph_row *
16779 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16780 struct glyph_row *);
16781
16782
16783 /* Return the last row in MATRIX displaying text. If row START is
16784 non-null, start searching with that row. IT gives the dimensions
16785 of the display. Value is null if matrix is empty; otherwise it is
16786 a pointer to the row found. */
16787
16788 static struct glyph_row *
16789 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16790 struct glyph_row *start)
16791 {
16792 struct glyph_row *row, *row_found;
16793
16794 /* Set row_found to the last row in IT->w's current matrix
16795 displaying text. The loop looks funny but think of partially
16796 visible lines. */
16797 row_found = NULL;
16798 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16799 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16800 {
16801 eassert (row->enabled_p);
16802 row_found = row;
16803 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16804 break;
16805 ++row;
16806 }
16807
16808 return row_found;
16809 }
16810
16811
16812 /* Return the last row in the current matrix of W that is not affected
16813 by changes at the start of current_buffer that occurred since W's
16814 current matrix was built. Value is null if no such row exists.
16815
16816 BEG_UNCHANGED us the number of characters unchanged at the start of
16817 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16818 first changed character in current_buffer. Characters at positions <
16819 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16820 when the current matrix was built. */
16821
16822 static struct glyph_row *
16823 find_last_unchanged_at_beg_row (struct window *w)
16824 {
16825 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16826 struct glyph_row *row;
16827 struct glyph_row *row_found = NULL;
16828 int yb = window_text_bottom_y (w);
16829
16830 /* Find the last row displaying unchanged text. */
16831 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16832 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16833 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16834 ++row)
16835 {
16836 if (/* If row ends before first_changed_pos, it is unchanged,
16837 except in some case. */
16838 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16839 /* When row ends in ZV and we write at ZV it is not
16840 unchanged. */
16841 && !row->ends_at_zv_p
16842 /* When first_changed_pos is the end of a continued line,
16843 row is not unchanged because it may be no longer
16844 continued. */
16845 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16846 && (row->continued_p
16847 || row->exact_window_width_line_p))
16848 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16849 needs to be recomputed, so don't consider this row as
16850 unchanged. This happens when the last line was
16851 bidi-reordered and was killed immediately before this
16852 redisplay cycle. In that case, ROW->end stores the
16853 buffer position of the first visual-order character of
16854 the killed text, which is now beyond ZV. */
16855 && CHARPOS (row->end.pos) <= ZV)
16856 row_found = row;
16857
16858 /* Stop if last visible row. */
16859 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16860 break;
16861 }
16862
16863 return row_found;
16864 }
16865
16866
16867 /* Find the first glyph row in the current matrix of W that is not
16868 affected by changes at the end of current_buffer since the
16869 time W's current matrix was built.
16870
16871 Return in *DELTA the number of chars by which buffer positions in
16872 unchanged text at the end of current_buffer must be adjusted.
16873
16874 Return in *DELTA_BYTES the corresponding number of bytes.
16875
16876 Value is null if no such row exists, i.e. all rows are affected by
16877 changes. */
16878
16879 static struct glyph_row *
16880 find_first_unchanged_at_end_row (struct window *w,
16881 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16882 {
16883 struct glyph_row *row;
16884 struct glyph_row *row_found = NULL;
16885
16886 *delta = *delta_bytes = 0;
16887
16888 /* Display must not have been paused, otherwise the current matrix
16889 is not up to date. */
16890 eassert (w->window_end_valid);
16891
16892 /* A value of window_end_pos >= END_UNCHANGED means that the window
16893 end is in the range of changed text. If so, there is no
16894 unchanged row at the end of W's current matrix. */
16895 if (w->window_end_pos >= END_UNCHANGED)
16896 return NULL;
16897
16898 /* Set row to the last row in W's current matrix displaying text. */
16899 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16900
16901 /* If matrix is entirely empty, no unchanged row exists. */
16902 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16903 {
16904 /* The value of row is the last glyph row in the matrix having a
16905 meaningful buffer position in it. The end position of row
16906 corresponds to window_end_pos. This allows us to translate
16907 buffer positions in the current matrix to current buffer
16908 positions for characters not in changed text. */
16909 ptrdiff_t Z_old =
16910 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16911 ptrdiff_t Z_BYTE_old =
16912 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16913 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16914 struct glyph_row *first_text_row
16915 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16916
16917 *delta = Z - Z_old;
16918 *delta_bytes = Z_BYTE - Z_BYTE_old;
16919
16920 /* Set last_unchanged_pos to the buffer position of the last
16921 character in the buffer that has not been changed. Z is the
16922 index + 1 of the last character in current_buffer, i.e. by
16923 subtracting END_UNCHANGED we get the index of the last
16924 unchanged character, and we have to add BEG to get its buffer
16925 position. */
16926 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16927 last_unchanged_pos_old = last_unchanged_pos - *delta;
16928
16929 /* Search backward from ROW for a row displaying a line that
16930 starts at a minimum position >= last_unchanged_pos_old. */
16931 for (; row > first_text_row; --row)
16932 {
16933 /* This used to abort, but it can happen.
16934 It is ok to just stop the search instead here. KFS. */
16935 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16936 break;
16937
16938 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16939 row_found = row;
16940 }
16941 }
16942
16943 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16944
16945 return row_found;
16946 }
16947
16948
16949 /* Make sure that glyph rows in the current matrix of window W
16950 reference the same glyph memory as corresponding rows in the
16951 frame's frame matrix. This function is called after scrolling W's
16952 current matrix on a terminal frame in try_window_id and
16953 try_window_reusing_current_matrix. */
16954
16955 static void
16956 sync_frame_with_window_matrix_rows (struct window *w)
16957 {
16958 struct frame *f = XFRAME (w->frame);
16959 struct glyph_row *window_row, *window_row_end, *frame_row;
16960
16961 /* Preconditions: W must be a leaf window and full-width. Its frame
16962 must have a frame matrix. */
16963 eassert (BUFFERP (w->contents));
16964 eassert (WINDOW_FULL_WIDTH_P (w));
16965 eassert (!FRAME_WINDOW_P (f));
16966
16967 /* If W is a full-width window, glyph pointers in W's current matrix
16968 have, by definition, to be the same as glyph pointers in the
16969 corresponding frame matrix. Note that frame matrices have no
16970 marginal areas (see build_frame_matrix). */
16971 window_row = w->current_matrix->rows;
16972 window_row_end = window_row + w->current_matrix->nrows;
16973 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16974 while (window_row < window_row_end)
16975 {
16976 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16977 struct glyph *end = window_row->glyphs[LAST_AREA];
16978
16979 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16980 frame_row->glyphs[TEXT_AREA] = start;
16981 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16982 frame_row->glyphs[LAST_AREA] = end;
16983
16984 /* Disable frame rows whose corresponding window rows have
16985 been disabled in try_window_id. */
16986 if (!window_row->enabled_p)
16987 frame_row->enabled_p = 0;
16988
16989 ++window_row, ++frame_row;
16990 }
16991 }
16992
16993
16994 /* Find the glyph row in window W containing CHARPOS. Consider all
16995 rows between START and END (not inclusive). END null means search
16996 all rows to the end of the display area of W. Value is the row
16997 containing CHARPOS or null. */
16998
16999 struct glyph_row *
17000 row_containing_pos (struct window *w, ptrdiff_t charpos,
17001 struct glyph_row *start, struct glyph_row *end, int dy)
17002 {
17003 struct glyph_row *row = start;
17004 struct glyph_row *best_row = NULL;
17005 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17006 int last_y;
17007
17008 /* If we happen to start on a header-line, skip that. */
17009 if (row->mode_line_p)
17010 ++row;
17011
17012 if ((end && row >= end) || !row->enabled_p)
17013 return NULL;
17014
17015 last_y = window_text_bottom_y (w) - dy;
17016
17017 while (1)
17018 {
17019 /* Give up if we have gone too far. */
17020 if (end && row >= end)
17021 return NULL;
17022 /* This formerly returned if they were equal.
17023 I think that both quantities are of a "last plus one" type;
17024 if so, when they are equal, the row is within the screen. -- rms. */
17025 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17026 return NULL;
17027
17028 /* If it is in this row, return this row. */
17029 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17030 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17031 /* The end position of a row equals the start
17032 position of the next row. If CHARPOS is there, we
17033 would rather consider it displayed in the next
17034 line, except when this line ends in ZV. */
17035 && !row_for_charpos_p (row, charpos)))
17036 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17037 {
17038 struct glyph *g;
17039
17040 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17041 || (!best_row && !row->continued_p))
17042 return row;
17043 /* In bidi-reordered rows, there could be several rows whose
17044 edges surround CHARPOS, all of these rows belonging to
17045 the same continued line. We need to find the row which
17046 fits CHARPOS the best. */
17047 for (g = row->glyphs[TEXT_AREA];
17048 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17049 g++)
17050 {
17051 if (!STRINGP (g->object))
17052 {
17053 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17054 {
17055 mindif = eabs (g->charpos - charpos);
17056 best_row = row;
17057 /* Exact match always wins. */
17058 if (mindif == 0)
17059 return best_row;
17060 }
17061 }
17062 }
17063 }
17064 else if (best_row && !row->continued_p)
17065 return best_row;
17066 ++row;
17067 }
17068 }
17069
17070
17071 /* Try to redisplay window W by reusing its existing display. W's
17072 current matrix must be up to date when this function is called,
17073 i.e. window_end_valid must be nonzero.
17074
17075 Value is
17076
17077 1 if display has been updated
17078 0 if otherwise unsuccessful
17079 -1 if redisplay with same window start is known not to succeed
17080
17081 The following steps are performed:
17082
17083 1. Find the last row in the current matrix of W that is not
17084 affected by changes at the start of current_buffer. If no such row
17085 is found, give up.
17086
17087 2. Find the first row in W's current matrix that is not affected by
17088 changes at the end of current_buffer. Maybe there is no such row.
17089
17090 3. Display lines beginning with the row + 1 found in step 1 to the
17091 row found in step 2 or, if step 2 didn't find a row, to the end of
17092 the window.
17093
17094 4. If cursor is not known to appear on the window, give up.
17095
17096 5. If display stopped at the row found in step 2, scroll the
17097 display and current matrix as needed.
17098
17099 6. Maybe display some lines at the end of W, if we must. This can
17100 happen under various circumstances, like a partially visible line
17101 becoming fully visible, or because newly displayed lines are displayed
17102 in smaller font sizes.
17103
17104 7. Update W's window end information. */
17105
17106 static int
17107 try_window_id (struct window *w)
17108 {
17109 struct frame *f = XFRAME (w->frame);
17110 struct glyph_matrix *current_matrix = w->current_matrix;
17111 struct glyph_matrix *desired_matrix = w->desired_matrix;
17112 struct glyph_row *last_unchanged_at_beg_row;
17113 struct glyph_row *first_unchanged_at_end_row;
17114 struct glyph_row *row;
17115 struct glyph_row *bottom_row;
17116 int bottom_vpos;
17117 struct it it;
17118 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17119 int dvpos, dy;
17120 struct text_pos start_pos;
17121 struct run run;
17122 int first_unchanged_at_end_vpos = 0;
17123 struct glyph_row *last_text_row, *last_text_row_at_end;
17124 struct text_pos start;
17125 ptrdiff_t first_changed_charpos, last_changed_charpos;
17126
17127 #ifdef GLYPH_DEBUG
17128 if (inhibit_try_window_id)
17129 return 0;
17130 #endif
17131
17132 /* This is handy for debugging. */
17133 #if 0
17134 #define GIVE_UP(X) \
17135 do { \
17136 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17137 return 0; \
17138 } while (0)
17139 #else
17140 #define GIVE_UP(X) return 0
17141 #endif
17142
17143 SET_TEXT_POS_FROM_MARKER (start, w->start);
17144
17145 /* Don't use this for mini-windows because these can show
17146 messages and mini-buffers, and we don't handle that here. */
17147 if (MINI_WINDOW_P (w))
17148 GIVE_UP (1);
17149
17150 /* This flag is used to prevent redisplay optimizations. */
17151 if (windows_or_buffers_changed || f->cursor_type_changed)
17152 GIVE_UP (2);
17153
17154 /* Verify that narrowing has not changed.
17155 Also verify that we were not told to prevent redisplay optimizations.
17156 It would be nice to further
17157 reduce the number of cases where this prevents try_window_id. */
17158 if (current_buffer->clip_changed
17159 || current_buffer->prevent_redisplay_optimizations_p)
17160 GIVE_UP (3);
17161
17162 /* Window must either use window-based redisplay or be full width. */
17163 if (!FRAME_WINDOW_P (f)
17164 && (!FRAME_LINE_INS_DEL_OK (f)
17165 || !WINDOW_FULL_WIDTH_P (w)))
17166 GIVE_UP (4);
17167
17168 /* Give up if point is known NOT to appear in W. */
17169 if (PT < CHARPOS (start))
17170 GIVE_UP (5);
17171
17172 /* Another way to prevent redisplay optimizations. */
17173 if (w->last_modified == 0)
17174 GIVE_UP (6);
17175
17176 /* Verify that window is not hscrolled. */
17177 if (w->hscroll != 0)
17178 GIVE_UP (7);
17179
17180 /* Verify that display wasn't paused. */
17181 if (!w->window_end_valid)
17182 GIVE_UP (8);
17183
17184 /* Can't use this if highlighting a region because a cursor movement
17185 will do more than just set the cursor. */
17186 if (markpos_of_region () >= 0)
17187 GIVE_UP (9);
17188
17189 /* Likewise if highlighting trailing whitespace. */
17190 if (!NILP (Vshow_trailing_whitespace))
17191 GIVE_UP (11);
17192
17193 /* Likewise if showing a region. */
17194 if (w->region_showing)
17195 GIVE_UP (10);
17196
17197 /* Can't use this if overlay arrow position and/or string have
17198 changed. */
17199 if (overlay_arrows_changed_p ())
17200 GIVE_UP (12);
17201
17202 /* When word-wrap is on, adding a space to the first word of a
17203 wrapped line can change the wrap position, altering the line
17204 above it. It might be worthwhile to handle this more
17205 intelligently, but for now just redisplay from scratch. */
17206 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17207 GIVE_UP (21);
17208
17209 /* Under bidi reordering, adding or deleting a character in the
17210 beginning of a paragraph, before the first strong directional
17211 character, can change the base direction of the paragraph (unless
17212 the buffer specifies a fixed paragraph direction), which will
17213 require to redisplay the whole paragraph. It might be worthwhile
17214 to find the paragraph limits and widen the range of redisplayed
17215 lines to that, but for now just give up this optimization and
17216 redisplay from scratch. */
17217 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17218 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17219 GIVE_UP (22);
17220
17221 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17222 only if buffer has really changed. The reason is that the gap is
17223 initially at Z for freshly visited files. The code below would
17224 set end_unchanged to 0 in that case. */
17225 if (MODIFF > SAVE_MODIFF
17226 /* This seems to happen sometimes after saving a buffer. */
17227 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17228 {
17229 if (GPT - BEG < BEG_UNCHANGED)
17230 BEG_UNCHANGED = GPT - BEG;
17231 if (Z - GPT < END_UNCHANGED)
17232 END_UNCHANGED = Z - GPT;
17233 }
17234
17235 /* The position of the first and last character that has been changed. */
17236 first_changed_charpos = BEG + BEG_UNCHANGED;
17237 last_changed_charpos = Z - END_UNCHANGED;
17238
17239 /* If window starts after a line end, and the last change is in
17240 front of that newline, then changes don't affect the display.
17241 This case happens with stealth-fontification. Note that although
17242 the display is unchanged, glyph positions in the matrix have to
17243 be adjusted, of course. */
17244 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17245 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17246 && ((last_changed_charpos < CHARPOS (start)
17247 && CHARPOS (start) == BEGV)
17248 || (last_changed_charpos < CHARPOS (start) - 1
17249 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17250 {
17251 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17252 struct glyph_row *r0;
17253
17254 /* Compute how many chars/bytes have been added to or removed
17255 from the buffer. */
17256 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17257 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17258 Z_delta = Z - Z_old;
17259 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17260
17261 /* Give up if PT is not in the window. Note that it already has
17262 been checked at the start of try_window_id that PT is not in
17263 front of the window start. */
17264 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17265 GIVE_UP (13);
17266
17267 /* If window start is unchanged, we can reuse the whole matrix
17268 as is, after adjusting glyph positions. No need to compute
17269 the window end again, since its offset from Z hasn't changed. */
17270 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17271 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17272 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17273 /* PT must not be in a partially visible line. */
17274 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17275 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17276 {
17277 /* Adjust positions in the glyph matrix. */
17278 if (Z_delta || Z_delta_bytes)
17279 {
17280 struct glyph_row *r1
17281 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17282 increment_matrix_positions (w->current_matrix,
17283 MATRIX_ROW_VPOS (r0, current_matrix),
17284 MATRIX_ROW_VPOS (r1, current_matrix),
17285 Z_delta, Z_delta_bytes);
17286 }
17287
17288 /* Set the cursor. */
17289 row = row_containing_pos (w, PT, r0, NULL, 0);
17290 if (row)
17291 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17292 return 1;
17293 }
17294 }
17295
17296 /* Handle the case that changes are all below what is displayed in
17297 the window, and that PT is in the window. This shortcut cannot
17298 be taken if ZV is visible in the window, and text has been added
17299 there that is visible in the window. */
17300 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17301 /* ZV is not visible in the window, or there are no
17302 changes at ZV, actually. */
17303 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17304 || first_changed_charpos == last_changed_charpos))
17305 {
17306 struct glyph_row *r0;
17307
17308 /* Give up if PT is not in the window. Note that it already has
17309 been checked at the start of try_window_id that PT is not in
17310 front of the window start. */
17311 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17312 GIVE_UP (14);
17313
17314 /* If window start is unchanged, we can reuse the whole matrix
17315 as is, without changing glyph positions since no text has
17316 been added/removed in front of the window end. */
17317 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17318 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17319 /* PT must not be in a partially visible line. */
17320 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17321 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17322 {
17323 /* We have to compute the window end anew since text
17324 could have been added/removed after it. */
17325 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17326 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17327
17328 /* Set the cursor. */
17329 row = row_containing_pos (w, PT, r0, NULL, 0);
17330 if (row)
17331 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17332 return 2;
17333 }
17334 }
17335
17336 /* Give up if window start is in the changed area.
17337
17338 The condition used to read
17339
17340 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17341
17342 but why that was tested escapes me at the moment. */
17343 if (CHARPOS (start) >= first_changed_charpos
17344 && CHARPOS (start) <= last_changed_charpos)
17345 GIVE_UP (15);
17346
17347 /* Check that window start agrees with the start of the first glyph
17348 row in its current matrix. Check this after we know the window
17349 start is not in changed text, otherwise positions would not be
17350 comparable. */
17351 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17352 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17353 GIVE_UP (16);
17354
17355 /* Give up if the window ends in strings. Overlay strings
17356 at the end are difficult to handle, so don't try. */
17357 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17358 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17359 GIVE_UP (20);
17360
17361 /* Compute the position at which we have to start displaying new
17362 lines. Some of the lines at the top of the window might be
17363 reusable because they are not displaying changed text. Find the
17364 last row in W's current matrix not affected by changes at the
17365 start of current_buffer. Value is null if changes start in the
17366 first line of window. */
17367 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17368 if (last_unchanged_at_beg_row)
17369 {
17370 /* Avoid starting to display in the middle of a character, a TAB
17371 for instance. This is easier than to set up the iterator
17372 exactly, and it's not a frequent case, so the additional
17373 effort wouldn't really pay off. */
17374 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17375 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17376 && last_unchanged_at_beg_row > w->current_matrix->rows)
17377 --last_unchanged_at_beg_row;
17378
17379 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17380 GIVE_UP (17);
17381
17382 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17383 GIVE_UP (18);
17384 start_pos = it.current.pos;
17385
17386 /* Start displaying new lines in the desired matrix at the same
17387 vpos we would use in the current matrix, i.e. below
17388 last_unchanged_at_beg_row. */
17389 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17390 current_matrix);
17391 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17392 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17393
17394 eassert (it.hpos == 0 && it.current_x == 0);
17395 }
17396 else
17397 {
17398 /* There are no reusable lines at the start of the window.
17399 Start displaying in the first text line. */
17400 start_display (&it, w, start);
17401 it.vpos = it.first_vpos;
17402 start_pos = it.current.pos;
17403 }
17404
17405 /* Find the first row that is not affected by changes at the end of
17406 the buffer. Value will be null if there is no unchanged row, in
17407 which case we must redisplay to the end of the window. delta
17408 will be set to the value by which buffer positions beginning with
17409 first_unchanged_at_end_row have to be adjusted due to text
17410 changes. */
17411 first_unchanged_at_end_row
17412 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17413 IF_DEBUG (debug_delta = delta);
17414 IF_DEBUG (debug_delta_bytes = delta_bytes);
17415
17416 /* Set stop_pos to the buffer position up to which we will have to
17417 display new lines. If first_unchanged_at_end_row != NULL, this
17418 is the buffer position of the start of the line displayed in that
17419 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17420 that we don't stop at a buffer position. */
17421 stop_pos = 0;
17422 if (first_unchanged_at_end_row)
17423 {
17424 eassert (last_unchanged_at_beg_row == NULL
17425 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17426
17427 /* If this is a continuation line, move forward to the next one
17428 that isn't. Changes in lines above affect this line.
17429 Caution: this may move first_unchanged_at_end_row to a row
17430 not displaying text. */
17431 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17432 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17433 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17434 < it.last_visible_y))
17435 ++first_unchanged_at_end_row;
17436
17437 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17438 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17439 >= it.last_visible_y))
17440 first_unchanged_at_end_row = NULL;
17441 else
17442 {
17443 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17444 + delta);
17445 first_unchanged_at_end_vpos
17446 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17447 eassert (stop_pos >= Z - END_UNCHANGED);
17448 }
17449 }
17450 else if (last_unchanged_at_beg_row == NULL)
17451 GIVE_UP (19);
17452
17453
17454 #ifdef GLYPH_DEBUG
17455
17456 /* Either there is no unchanged row at the end, or the one we have
17457 now displays text. This is a necessary condition for the window
17458 end pos calculation at the end of this function. */
17459 eassert (first_unchanged_at_end_row == NULL
17460 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17461
17462 debug_last_unchanged_at_beg_vpos
17463 = (last_unchanged_at_beg_row
17464 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17465 : -1);
17466 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17467
17468 #endif /* GLYPH_DEBUG */
17469
17470
17471 /* Display new lines. Set last_text_row to the last new line
17472 displayed which has text on it, i.e. might end up as being the
17473 line where the window_end_vpos is. */
17474 w->cursor.vpos = -1;
17475 last_text_row = NULL;
17476 overlay_arrow_seen = 0;
17477 while (it.current_y < it.last_visible_y
17478 && !f->fonts_changed
17479 && (first_unchanged_at_end_row == NULL
17480 || IT_CHARPOS (it) < stop_pos))
17481 {
17482 if (display_line (&it))
17483 last_text_row = it.glyph_row - 1;
17484 }
17485
17486 if (f->fonts_changed)
17487 return -1;
17488
17489
17490 /* Compute differences in buffer positions, y-positions etc. for
17491 lines reused at the bottom of the window. Compute what we can
17492 scroll. */
17493 if (first_unchanged_at_end_row
17494 /* No lines reused because we displayed everything up to the
17495 bottom of the window. */
17496 && it.current_y < it.last_visible_y)
17497 {
17498 dvpos = (it.vpos
17499 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17500 current_matrix));
17501 dy = it.current_y - first_unchanged_at_end_row->y;
17502 run.current_y = first_unchanged_at_end_row->y;
17503 run.desired_y = run.current_y + dy;
17504 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17505 }
17506 else
17507 {
17508 delta = delta_bytes = dvpos = dy
17509 = run.current_y = run.desired_y = run.height = 0;
17510 first_unchanged_at_end_row = NULL;
17511 }
17512 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17513
17514
17515 /* Find the cursor if not already found. We have to decide whether
17516 PT will appear on this window (it sometimes doesn't, but this is
17517 not a very frequent case.) This decision has to be made before
17518 the current matrix is altered. A value of cursor.vpos < 0 means
17519 that PT is either in one of the lines beginning at
17520 first_unchanged_at_end_row or below the window. Don't care for
17521 lines that might be displayed later at the window end; as
17522 mentioned, this is not a frequent case. */
17523 if (w->cursor.vpos < 0)
17524 {
17525 /* Cursor in unchanged rows at the top? */
17526 if (PT < CHARPOS (start_pos)
17527 && last_unchanged_at_beg_row)
17528 {
17529 row = row_containing_pos (w, PT,
17530 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17531 last_unchanged_at_beg_row + 1, 0);
17532 if (row)
17533 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17534 }
17535
17536 /* Start from first_unchanged_at_end_row looking for PT. */
17537 else if (first_unchanged_at_end_row)
17538 {
17539 row = row_containing_pos (w, PT - delta,
17540 first_unchanged_at_end_row, NULL, 0);
17541 if (row)
17542 set_cursor_from_row (w, row, w->current_matrix, delta,
17543 delta_bytes, dy, dvpos);
17544 }
17545
17546 /* Give up if cursor was not found. */
17547 if (w->cursor.vpos < 0)
17548 {
17549 clear_glyph_matrix (w->desired_matrix);
17550 return -1;
17551 }
17552 }
17553
17554 /* Don't let the cursor end in the scroll margins. */
17555 {
17556 int this_scroll_margin, cursor_height;
17557 int frame_line_height = default_line_pixel_height (w);
17558 int window_total_lines
17559 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17560
17561 this_scroll_margin =
17562 max (0, min (scroll_margin, window_total_lines / 4));
17563 this_scroll_margin *= frame_line_height;
17564 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17565
17566 if ((w->cursor.y < this_scroll_margin
17567 && CHARPOS (start) > BEGV)
17568 /* Old redisplay didn't take scroll margin into account at the bottom,
17569 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17570 || (w->cursor.y + (make_cursor_line_fully_visible_p
17571 ? cursor_height + this_scroll_margin
17572 : 1)) > it.last_visible_y)
17573 {
17574 w->cursor.vpos = -1;
17575 clear_glyph_matrix (w->desired_matrix);
17576 return -1;
17577 }
17578 }
17579
17580 /* Scroll the display. Do it before changing the current matrix so
17581 that xterm.c doesn't get confused about where the cursor glyph is
17582 found. */
17583 if (dy && run.height)
17584 {
17585 update_begin (f);
17586
17587 if (FRAME_WINDOW_P (f))
17588 {
17589 FRAME_RIF (f)->update_window_begin_hook (w);
17590 FRAME_RIF (f)->clear_window_mouse_face (w);
17591 FRAME_RIF (f)->scroll_run_hook (w, &run);
17592 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17593 }
17594 else
17595 {
17596 /* Terminal frame. In this case, dvpos gives the number of
17597 lines to scroll by; dvpos < 0 means scroll up. */
17598 int from_vpos
17599 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17600 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17601 int end = (WINDOW_TOP_EDGE_LINE (w)
17602 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17603 + window_internal_height (w));
17604
17605 #if defined (HAVE_GPM) || defined (MSDOS)
17606 x_clear_window_mouse_face (w);
17607 #endif
17608 /* Perform the operation on the screen. */
17609 if (dvpos > 0)
17610 {
17611 /* Scroll last_unchanged_at_beg_row to the end of the
17612 window down dvpos lines. */
17613 set_terminal_window (f, end);
17614
17615 /* On dumb terminals delete dvpos lines at the end
17616 before inserting dvpos empty lines. */
17617 if (!FRAME_SCROLL_REGION_OK (f))
17618 ins_del_lines (f, end - dvpos, -dvpos);
17619
17620 /* Insert dvpos empty lines in front of
17621 last_unchanged_at_beg_row. */
17622 ins_del_lines (f, from, dvpos);
17623 }
17624 else if (dvpos < 0)
17625 {
17626 /* Scroll up last_unchanged_at_beg_vpos to the end of
17627 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17628 set_terminal_window (f, end);
17629
17630 /* Delete dvpos lines in front of
17631 last_unchanged_at_beg_vpos. ins_del_lines will set
17632 the cursor to the given vpos and emit |dvpos| delete
17633 line sequences. */
17634 ins_del_lines (f, from + dvpos, dvpos);
17635
17636 /* On a dumb terminal insert dvpos empty lines at the
17637 end. */
17638 if (!FRAME_SCROLL_REGION_OK (f))
17639 ins_del_lines (f, end + dvpos, -dvpos);
17640 }
17641
17642 set_terminal_window (f, 0);
17643 }
17644
17645 update_end (f);
17646 }
17647
17648 /* Shift reused rows of the current matrix to the right position.
17649 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17650 text. */
17651 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17652 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17653 if (dvpos < 0)
17654 {
17655 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17656 bottom_vpos, dvpos);
17657 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17658 bottom_vpos);
17659 }
17660 else if (dvpos > 0)
17661 {
17662 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17663 bottom_vpos, dvpos);
17664 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17665 first_unchanged_at_end_vpos + dvpos);
17666 }
17667
17668 /* For frame-based redisplay, make sure that current frame and window
17669 matrix are in sync with respect to glyph memory. */
17670 if (!FRAME_WINDOW_P (f))
17671 sync_frame_with_window_matrix_rows (w);
17672
17673 /* Adjust buffer positions in reused rows. */
17674 if (delta || delta_bytes)
17675 increment_matrix_positions (current_matrix,
17676 first_unchanged_at_end_vpos + dvpos,
17677 bottom_vpos, delta, delta_bytes);
17678
17679 /* Adjust Y positions. */
17680 if (dy)
17681 shift_glyph_matrix (w, current_matrix,
17682 first_unchanged_at_end_vpos + dvpos,
17683 bottom_vpos, dy);
17684
17685 if (first_unchanged_at_end_row)
17686 {
17687 first_unchanged_at_end_row += dvpos;
17688 if (first_unchanged_at_end_row->y >= it.last_visible_y
17689 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17690 first_unchanged_at_end_row = NULL;
17691 }
17692
17693 /* If scrolling up, there may be some lines to display at the end of
17694 the window. */
17695 last_text_row_at_end = NULL;
17696 if (dy < 0)
17697 {
17698 /* Scrolling up can leave for example a partially visible line
17699 at the end of the window to be redisplayed. */
17700 /* Set last_row to the glyph row in the current matrix where the
17701 window end line is found. It has been moved up or down in
17702 the matrix by dvpos. */
17703 int last_vpos = w->window_end_vpos + dvpos;
17704 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17705
17706 /* If last_row is the window end line, it should display text. */
17707 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17708
17709 /* If window end line was partially visible before, begin
17710 displaying at that line. Otherwise begin displaying with the
17711 line following it. */
17712 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17713 {
17714 init_to_row_start (&it, w, last_row);
17715 it.vpos = last_vpos;
17716 it.current_y = last_row->y;
17717 }
17718 else
17719 {
17720 init_to_row_end (&it, w, last_row);
17721 it.vpos = 1 + last_vpos;
17722 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17723 ++last_row;
17724 }
17725
17726 /* We may start in a continuation line. If so, we have to
17727 get the right continuation_lines_width and current_x. */
17728 it.continuation_lines_width = last_row->continuation_lines_width;
17729 it.hpos = it.current_x = 0;
17730
17731 /* Display the rest of the lines at the window end. */
17732 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17733 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17734 {
17735 /* Is it always sure that the display agrees with lines in
17736 the current matrix? I don't think so, so we mark rows
17737 displayed invalid in the current matrix by setting their
17738 enabled_p flag to zero. */
17739 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17740 if (display_line (&it))
17741 last_text_row_at_end = it.glyph_row - 1;
17742 }
17743 }
17744
17745 /* Update window_end_pos and window_end_vpos. */
17746 if (first_unchanged_at_end_row && !last_text_row_at_end)
17747 {
17748 /* Window end line if one of the preserved rows from the current
17749 matrix. Set row to the last row displaying text in current
17750 matrix starting at first_unchanged_at_end_row, after
17751 scrolling. */
17752 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17753 row = find_last_row_displaying_text (w->current_matrix, &it,
17754 first_unchanged_at_end_row);
17755 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17756 adjust_window_ends (w, row, 1);
17757 eassert (w->window_end_bytepos >= 0);
17758 IF_DEBUG (debug_method_add (w, "A"));
17759 }
17760 else if (last_text_row_at_end)
17761 {
17762 adjust_window_ends (w, last_text_row_at_end, 0);
17763 eassert (w->window_end_bytepos >= 0);
17764 IF_DEBUG (debug_method_add (w, "B"));
17765 }
17766 else if (last_text_row)
17767 {
17768 /* We have displayed either to the end of the window or at the
17769 end of the window, i.e. the last row with text is to be found
17770 in the desired matrix. */
17771 adjust_window_ends (w, last_text_row, 0);
17772 eassert (w->window_end_bytepos >= 0);
17773 }
17774 else if (first_unchanged_at_end_row == NULL
17775 && last_text_row == NULL
17776 && last_text_row_at_end == NULL)
17777 {
17778 /* Displayed to end of window, but no line containing text was
17779 displayed. Lines were deleted at the end of the window. */
17780 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17781 int vpos = w->window_end_vpos;
17782 struct glyph_row *current_row = current_matrix->rows + vpos;
17783 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17784
17785 for (row = NULL;
17786 row == NULL && vpos >= first_vpos;
17787 --vpos, --current_row, --desired_row)
17788 {
17789 if (desired_row->enabled_p)
17790 {
17791 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17792 row = desired_row;
17793 }
17794 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17795 row = current_row;
17796 }
17797
17798 eassert (row != NULL);
17799 w->window_end_vpos = vpos + 1;
17800 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17801 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17802 eassert (w->window_end_bytepos >= 0);
17803 IF_DEBUG (debug_method_add (w, "C"));
17804 }
17805 else
17806 emacs_abort ();
17807
17808 IF_DEBUG (debug_end_pos = w->window_end_pos;
17809 debug_end_vpos = w->window_end_vpos);
17810
17811 /* Record that display has not been completed. */
17812 w->window_end_valid = 0;
17813 w->desired_matrix->no_scrolling_p = 1;
17814 return 3;
17815
17816 #undef GIVE_UP
17817 }
17818
17819
17820 \f
17821 /***********************************************************************
17822 More debugging support
17823 ***********************************************************************/
17824
17825 #ifdef GLYPH_DEBUG
17826
17827 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17828 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17829 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17830
17831
17832 /* Dump the contents of glyph matrix MATRIX on stderr.
17833
17834 GLYPHS 0 means don't show glyph contents.
17835 GLYPHS 1 means show glyphs in short form
17836 GLYPHS > 1 means show glyphs in long form. */
17837
17838 void
17839 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17840 {
17841 int i;
17842 for (i = 0; i < matrix->nrows; ++i)
17843 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17844 }
17845
17846
17847 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17848 the glyph row and area where the glyph comes from. */
17849
17850 void
17851 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17852 {
17853 if (glyph->type == CHAR_GLYPH
17854 || glyph->type == GLYPHLESS_GLYPH)
17855 {
17856 fprintf (stderr,
17857 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17858 glyph - row->glyphs[TEXT_AREA],
17859 (glyph->type == CHAR_GLYPH
17860 ? 'C'
17861 : 'G'),
17862 glyph->charpos,
17863 (BUFFERP (glyph->object)
17864 ? 'B'
17865 : (STRINGP (glyph->object)
17866 ? 'S'
17867 : (INTEGERP (glyph->object)
17868 ? '0'
17869 : '-'))),
17870 glyph->pixel_width,
17871 glyph->u.ch,
17872 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17873 ? glyph->u.ch
17874 : '.'),
17875 glyph->face_id,
17876 glyph->left_box_line_p,
17877 glyph->right_box_line_p);
17878 }
17879 else if (glyph->type == STRETCH_GLYPH)
17880 {
17881 fprintf (stderr,
17882 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17883 glyph - row->glyphs[TEXT_AREA],
17884 'S',
17885 glyph->charpos,
17886 (BUFFERP (glyph->object)
17887 ? 'B'
17888 : (STRINGP (glyph->object)
17889 ? 'S'
17890 : (INTEGERP (glyph->object)
17891 ? '0'
17892 : '-'))),
17893 glyph->pixel_width,
17894 0,
17895 ' ',
17896 glyph->face_id,
17897 glyph->left_box_line_p,
17898 glyph->right_box_line_p);
17899 }
17900 else if (glyph->type == IMAGE_GLYPH)
17901 {
17902 fprintf (stderr,
17903 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17904 glyph - row->glyphs[TEXT_AREA],
17905 'I',
17906 glyph->charpos,
17907 (BUFFERP (glyph->object)
17908 ? 'B'
17909 : (STRINGP (glyph->object)
17910 ? 'S'
17911 : (INTEGERP (glyph->object)
17912 ? '0'
17913 : '-'))),
17914 glyph->pixel_width,
17915 glyph->u.img_id,
17916 '.',
17917 glyph->face_id,
17918 glyph->left_box_line_p,
17919 glyph->right_box_line_p);
17920 }
17921 else if (glyph->type == COMPOSITE_GLYPH)
17922 {
17923 fprintf (stderr,
17924 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17925 glyph - row->glyphs[TEXT_AREA],
17926 '+',
17927 glyph->charpos,
17928 (BUFFERP (glyph->object)
17929 ? 'B'
17930 : (STRINGP (glyph->object)
17931 ? 'S'
17932 : (INTEGERP (glyph->object)
17933 ? '0'
17934 : '-'))),
17935 glyph->pixel_width,
17936 glyph->u.cmp.id);
17937 if (glyph->u.cmp.automatic)
17938 fprintf (stderr,
17939 "[%d-%d]",
17940 glyph->slice.cmp.from, glyph->slice.cmp.to);
17941 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17942 glyph->face_id,
17943 glyph->left_box_line_p,
17944 glyph->right_box_line_p);
17945 }
17946 }
17947
17948
17949 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17950 GLYPHS 0 means don't show glyph contents.
17951 GLYPHS 1 means show glyphs in short form
17952 GLYPHS > 1 means show glyphs in long form. */
17953
17954 void
17955 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17956 {
17957 if (glyphs != 1)
17958 {
17959 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17960 fprintf (stderr, "==============================================================================\n");
17961
17962 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17963 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17964 vpos,
17965 MATRIX_ROW_START_CHARPOS (row),
17966 MATRIX_ROW_END_CHARPOS (row),
17967 row->used[TEXT_AREA],
17968 row->contains_overlapping_glyphs_p,
17969 row->enabled_p,
17970 row->truncated_on_left_p,
17971 row->truncated_on_right_p,
17972 row->continued_p,
17973 MATRIX_ROW_CONTINUATION_LINE_P (row),
17974 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17975 row->ends_at_zv_p,
17976 row->fill_line_p,
17977 row->ends_in_middle_of_char_p,
17978 row->starts_in_middle_of_char_p,
17979 row->mouse_face_p,
17980 row->x,
17981 row->y,
17982 row->pixel_width,
17983 row->height,
17984 row->visible_height,
17985 row->ascent,
17986 row->phys_ascent);
17987 /* The next 3 lines should align to "Start" in the header. */
17988 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17989 row->end.overlay_string_index,
17990 row->continuation_lines_width);
17991 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17992 CHARPOS (row->start.string_pos),
17993 CHARPOS (row->end.string_pos));
17994 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17995 row->end.dpvec_index);
17996 }
17997
17998 if (glyphs > 1)
17999 {
18000 int area;
18001
18002 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18003 {
18004 struct glyph *glyph = row->glyphs[area];
18005 struct glyph *glyph_end = glyph + row->used[area];
18006
18007 /* Glyph for a line end in text. */
18008 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18009 ++glyph_end;
18010
18011 if (glyph < glyph_end)
18012 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18013
18014 for (; glyph < glyph_end; ++glyph)
18015 dump_glyph (row, glyph, area);
18016 }
18017 }
18018 else if (glyphs == 1)
18019 {
18020 int area;
18021
18022 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18023 {
18024 char *s = alloca (row->used[area] + 4);
18025 int i;
18026
18027 for (i = 0; i < row->used[area]; ++i)
18028 {
18029 struct glyph *glyph = row->glyphs[area] + i;
18030 if (i == row->used[area] - 1
18031 && area == TEXT_AREA
18032 && INTEGERP (glyph->object)
18033 && glyph->type == CHAR_GLYPH
18034 && glyph->u.ch == ' ')
18035 {
18036 strcpy (&s[i], "[\\n]");
18037 i += 4;
18038 }
18039 else if (glyph->type == CHAR_GLYPH
18040 && glyph->u.ch < 0x80
18041 && glyph->u.ch >= ' ')
18042 s[i] = glyph->u.ch;
18043 else
18044 s[i] = '.';
18045 }
18046
18047 s[i] = '\0';
18048 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18049 }
18050 }
18051 }
18052
18053
18054 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18055 Sdump_glyph_matrix, 0, 1, "p",
18056 doc: /* Dump the current matrix of the selected window to stderr.
18057 Shows contents of glyph row structures. With non-nil
18058 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18059 glyphs in short form, otherwise show glyphs in long form. */)
18060 (Lisp_Object glyphs)
18061 {
18062 struct window *w = XWINDOW (selected_window);
18063 struct buffer *buffer = XBUFFER (w->contents);
18064
18065 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18066 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18067 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18068 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18069 fprintf (stderr, "=============================================\n");
18070 dump_glyph_matrix (w->current_matrix,
18071 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18072 return Qnil;
18073 }
18074
18075
18076 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18077 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18078 (void)
18079 {
18080 struct frame *f = XFRAME (selected_frame);
18081 dump_glyph_matrix (f->current_matrix, 1);
18082 return Qnil;
18083 }
18084
18085
18086 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18087 doc: /* Dump glyph row ROW to stderr.
18088 GLYPH 0 means don't dump glyphs.
18089 GLYPH 1 means dump glyphs in short form.
18090 GLYPH > 1 or omitted means dump glyphs in long form. */)
18091 (Lisp_Object row, Lisp_Object glyphs)
18092 {
18093 struct glyph_matrix *matrix;
18094 EMACS_INT vpos;
18095
18096 CHECK_NUMBER (row);
18097 matrix = XWINDOW (selected_window)->current_matrix;
18098 vpos = XINT (row);
18099 if (vpos >= 0 && vpos < matrix->nrows)
18100 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18101 vpos,
18102 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18103 return Qnil;
18104 }
18105
18106
18107 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18108 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18109 GLYPH 0 means don't dump glyphs.
18110 GLYPH 1 means dump glyphs in short form.
18111 GLYPH > 1 or omitted means dump glyphs in long form.
18112
18113 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18114 do nothing. */)
18115 (Lisp_Object row, Lisp_Object glyphs)
18116 {
18117 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18118 struct frame *sf = SELECTED_FRAME ();
18119 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18120 EMACS_INT vpos;
18121
18122 CHECK_NUMBER (row);
18123 vpos = XINT (row);
18124 if (vpos >= 0 && vpos < m->nrows)
18125 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18126 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18127 #endif
18128 return Qnil;
18129 }
18130
18131
18132 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18133 doc: /* Toggle tracing of redisplay.
18134 With ARG, turn tracing on if and only if ARG is positive. */)
18135 (Lisp_Object arg)
18136 {
18137 if (NILP (arg))
18138 trace_redisplay_p = !trace_redisplay_p;
18139 else
18140 {
18141 arg = Fprefix_numeric_value (arg);
18142 trace_redisplay_p = XINT (arg) > 0;
18143 }
18144
18145 return Qnil;
18146 }
18147
18148
18149 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18150 doc: /* Like `format', but print result to stderr.
18151 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18152 (ptrdiff_t nargs, Lisp_Object *args)
18153 {
18154 Lisp_Object s = Fformat (nargs, args);
18155 fprintf (stderr, "%s", SDATA (s));
18156 return Qnil;
18157 }
18158
18159 #endif /* GLYPH_DEBUG */
18160
18161
18162 \f
18163 /***********************************************************************
18164 Building Desired Matrix Rows
18165 ***********************************************************************/
18166
18167 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18168 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18169
18170 static struct glyph_row *
18171 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18172 {
18173 struct frame *f = XFRAME (WINDOW_FRAME (w));
18174 struct buffer *buffer = XBUFFER (w->contents);
18175 struct buffer *old = current_buffer;
18176 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18177 int arrow_len = SCHARS (overlay_arrow_string);
18178 const unsigned char *arrow_end = arrow_string + arrow_len;
18179 const unsigned char *p;
18180 struct it it;
18181 bool multibyte_p;
18182 int n_glyphs_before;
18183
18184 set_buffer_temp (buffer);
18185 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18186 it.glyph_row->used[TEXT_AREA] = 0;
18187 SET_TEXT_POS (it.position, 0, 0);
18188
18189 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18190 p = arrow_string;
18191 while (p < arrow_end)
18192 {
18193 Lisp_Object face, ilisp;
18194
18195 /* Get the next character. */
18196 if (multibyte_p)
18197 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18198 else
18199 {
18200 it.c = it.char_to_display = *p, it.len = 1;
18201 if (! ASCII_CHAR_P (it.c))
18202 it.char_to_display = BYTE8_TO_CHAR (it.c);
18203 }
18204 p += it.len;
18205
18206 /* Get its face. */
18207 ilisp = make_number (p - arrow_string);
18208 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18209 it.face_id = compute_char_face (f, it.char_to_display, face);
18210
18211 /* Compute its width, get its glyphs. */
18212 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18213 SET_TEXT_POS (it.position, -1, -1);
18214 PRODUCE_GLYPHS (&it);
18215
18216 /* If this character doesn't fit any more in the line, we have
18217 to remove some glyphs. */
18218 if (it.current_x > it.last_visible_x)
18219 {
18220 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18221 break;
18222 }
18223 }
18224
18225 set_buffer_temp (old);
18226 return it.glyph_row;
18227 }
18228
18229
18230 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18231 glyphs to insert is determined by produce_special_glyphs. */
18232
18233 static void
18234 insert_left_trunc_glyphs (struct it *it)
18235 {
18236 struct it truncate_it;
18237 struct glyph *from, *end, *to, *toend;
18238
18239 eassert (!FRAME_WINDOW_P (it->f)
18240 || (!it->glyph_row->reversed_p
18241 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18242 || (it->glyph_row->reversed_p
18243 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18244
18245 /* Get the truncation glyphs. */
18246 truncate_it = *it;
18247 truncate_it.current_x = 0;
18248 truncate_it.face_id = DEFAULT_FACE_ID;
18249 truncate_it.glyph_row = &scratch_glyph_row;
18250 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18251 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18252 truncate_it.object = make_number (0);
18253 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18254
18255 /* Overwrite glyphs from IT with truncation glyphs. */
18256 if (!it->glyph_row->reversed_p)
18257 {
18258 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18259
18260 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18261 end = from + tused;
18262 to = it->glyph_row->glyphs[TEXT_AREA];
18263 toend = to + it->glyph_row->used[TEXT_AREA];
18264 if (FRAME_WINDOW_P (it->f))
18265 {
18266 /* On GUI frames, when variable-size fonts are displayed,
18267 the truncation glyphs may need more pixels than the row's
18268 glyphs they overwrite. We overwrite more glyphs to free
18269 enough screen real estate, and enlarge the stretch glyph
18270 on the right (see display_line), if there is one, to
18271 preserve the screen position of the truncation glyphs on
18272 the right. */
18273 int w = 0;
18274 struct glyph *g = to;
18275 short used;
18276
18277 /* The first glyph could be partially visible, in which case
18278 it->glyph_row->x will be negative. But we want the left
18279 truncation glyphs to be aligned at the left margin of the
18280 window, so we override the x coordinate at which the row
18281 will begin. */
18282 it->glyph_row->x = 0;
18283 while (g < toend && w < it->truncation_pixel_width)
18284 {
18285 w += g->pixel_width;
18286 ++g;
18287 }
18288 if (g - to - tused > 0)
18289 {
18290 memmove (to + tused, g, (toend - g) * sizeof(*g));
18291 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18292 }
18293 used = it->glyph_row->used[TEXT_AREA];
18294 if (it->glyph_row->truncated_on_right_p
18295 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18296 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18297 == STRETCH_GLYPH)
18298 {
18299 int extra = w - it->truncation_pixel_width;
18300
18301 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18302 }
18303 }
18304
18305 while (from < end)
18306 *to++ = *from++;
18307
18308 /* There may be padding glyphs left over. Overwrite them too. */
18309 if (!FRAME_WINDOW_P (it->f))
18310 {
18311 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18312 {
18313 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18314 while (from < end)
18315 *to++ = *from++;
18316 }
18317 }
18318
18319 if (to > toend)
18320 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18321 }
18322 else
18323 {
18324 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18325
18326 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18327 that back to front. */
18328 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18329 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18330 toend = it->glyph_row->glyphs[TEXT_AREA];
18331 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18332 if (FRAME_WINDOW_P (it->f))
18333 {
18334 int w = 0;
18335 struct glyph *g = to;
18336
18337 while (g >= toend && w < it->truncation_pixel_width)
18338 {
18339 w += g->pixel_width;
18340 --g;
18341 }
18342 if (to - g - tused > 0)
18343 to = g + tused;
18344 if (it->glyph_row->truncated_on_right_p
18345 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18346 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18347 {
18348 int extra = w - it->truncation_pixel_width;
18349
18350 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18351 }
18352 }
18353
18354 while (from >= end && to >= toend)
18355 *to-- = *from--;
18356 if (!FRAME_WINDOW_P (it->f))
18357 {
18358 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18359 {
18360 from =
18361 truncate_it.glyph_row->glyphs[TEXT_AREA]
18362 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18363 while (from >= end && to >= toend)
18364 *to-- = *from--;
18365 }
18366 }
18367 if (from >= end)
18368 {
18369 /* Need to free some room before prepending additional
18370 glyphs. */
18371 int move_by = from - end + 1;
18372 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18373 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18374
18375 for ( ; g >= g0; g--)
18376 g[move_by] = *g;
18377 while (from >= end)
18378 *to-- = *from--;
18379 it->glyph_row->used[TEXT_AREA] += move_by;
18380 }
18381 }
18382 }
18383
18384 /* Compute the hash code for ROW. */
18385 unsigned
18386 row_hash (struct glyph_row *row)
18387 {
18388 int area, k;
18389 unsigned hashval = 0;
18390
18391 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18392 for (k = 0; k < row->used[area]; ++k)
18393 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18394 + row->glyphs[area][k].u.val
18395 + row->glyphs[area][k].face_id
18396 + row->glyphs[area][k].padding_p
18397 + (row->glyphs[area][k].type << 2));
18398
18399 return hashval;
18400 }
18401
18402 /* Compute the pixel height and width of IT->glyph_row.
18403
18404 Most of the time, ascent and height of a display line will be equal
18405 to the max_ascent and max_height values of the display iterator
18406 structure. This is not the case if
18407
18408 1. We hit ZV without displaying anything. In this case, max_ascent
18409 and max_height will be zero.
18410
18411 2. We have some glyphs that don't contribute to the line height.
18412 (The glyph row flag contributes_to_line_height_p is for future
18413 pixmap extensions).
18414
18415 The first case is easily covered by using default values because in
18416 these cases, the line height does not really matter, except that it
18417 must not be zero. */
18418
18419 static void
18420 compute_line_metrics (struct it *it)
18421 {
18422 struct glyph_row *row = it->glyph_row;
18423
18424 if (FRAME_WINDOW_P (it->f))
18425 {
18426 int i, min_y, max_y;
18427
18428 /* The line may consist of one space only, that was added to
18429 place the cursor on it. If so, the row's height hasn't been
18430 computed yet. */
18431 if (row->height == 0)
18432 {
18433 if (it->max_ascent + it->max_descent == 0)
18434 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18435 row->ascent = it->max_ascent;
18436 row->height = it->max_ascent + it->max_descent;
18437 row->phys_ascent = it->max_phys_ascent;
18438 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18439 row->extra_line_spacing = it->max_extra_line_spacing;
18440 }
18441
18442 /* Compute the width of this line. */
18443 row->pixel_width = row->x;
18444 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18445 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18446
18447 eassert (row->pixel_width >= 0);
18448 eassert (row->ascent >= 0 && row->height > 0);
18449
18450 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18451 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18452
18453 /* If first line's physical ascent is larger than its logical
18454 ascent, use the physical ascent, and make the row taller.
18455 This makes accented characters fully visible. */
18456 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18457 && row->phys_ascent > row->ascent)
18458 {
18459 row->height += row->phys_ascent - row->ascent;
18460 row->ascent = row->phys_ascent;
18461 }
18462
18463 /* Compute how much of the line is visible. */
18464 row->visible_height = row->height;
18465
18466 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18467 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18468
18469 if (row->y < min_y)
18470 row->visible_height -= min_y - row->y;
18471 if (row->y + row->height > max_y)
18472 row->visible_height -= row->y + row->height - max_y;
18473 }
18474 else
18475 {
18476 row->pixel_width = row->used[TEXT_AREA];
18477 if (row->continued_p)
18478 row->pixel_width -= it->continuation_pixel_width;
18479 else if (row->truncated_on_right_p)
18480 row->pixel_width -= it->truncation_pixel_width;
18481 row->ascent = row->phys_ascent = 0;
18482 row->height = row->phys_height = row->visible_height = 1;
18483 row->extra_line_spacing = 0;
18484 }
18485
18486 /* Compute a hash code for this row. */
18487 row->hash = row_hash (row);
18488
18489 it->max_ascent = it->max_descent = 0;
18490 it->max_phys_ascent = it->max_phys_descent = 0;
18491 }
18492
18493
18494 /* Append one space to the glyph row of iterator IT if doing a
18495 window-based redisplay. The space has the same face as
18496 IT->face_id. Value is non-zero if a space was added.
18497
18498 This function is called to make sure that there is always one glyph
18499 at the end of a glyph row that the cursor can be set on under
18500 window-systems. (If there weren't such a glyph we would not know
18501 how wide and tall a box cursor should be displayed).
18502
18503 At the same time this space let's a nicely handle clearing to the
18504 end of the line if the row ends in italic text. */
18505
18506 static int
18507 append_space_for_newline (struct it *it, int default_face_p)
18508 {
18509 if (FRAME_WINDOW_P (it->f))
18510 {
18511 int n = it->glyph_row->used[TEXT_AREA];
18512
18513 if (it->glyph_row->glyphs[TEXT_AREA] + n
18514 < it->glyph_row->glyphs[1 + TEXT_AREA])
18515 {
18516 /* Save some values that must not be changed.
18517 Must save IT->c and IT->len because otherwise
18518 ITERATOR_AT_END_P wouldn't work anymore after
18519 append_space_for_newline has been called. */
18520 enum display_element_type saved_what = it->what;
18521 int saved_c = it->c, saved_len = it->len;
18522 int saved_char_to_display = it->char_to_display;
18523 int saved_x = it->current_x;
18524 int saved_face_id = it->face_id;
18525 int saved_box_end = it->end_of_box_run_p;
18526 struct text_pos saved_pos;
18527 Lisp_Object saved_object;
18528 struct face *face;
18529
18530 saved_object = it->object;
18531 saved_pos = it->position;
18532
18533 it->what = IT_CHARACTER;
18534 memset (&it->position, 0, sizeof it->position);
18535 it->object = make_number (0);
18536 it->c = it->char_to_display = ' ';
18537 it->len = 1;
18538
18539 /* If the default face was remapped, be sure to use the
18540 remapped face for the appended newline. */
18541 if (default_face_p)
18542 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18543 else if (it->face_before_selective_p)
18544 it->face_id = it->saved_face_id;
18545 face = FACE_FROM_ID (it->f, it->face_id);
18546 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18547 /* In R2L rows, we will prepend a stretch glyph that will
18548 have the end_of_box_run_p flag set for it, so there's no
18549 need for the appended newline glyph to have that flag
18550 set. */
18551 if (it->glyph_row->reversed_p
18552 /* But if the appended newline glyph goes all the way to
18553 the end of the row, there will be no stretch glyph,
18554 so leave the box flag set. */
18555 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18556 it->end_of_box_run_p = 0;
18557
18558 PRODUCE_GLYPHS (it);
18559
18560 it->override_ascent = -1;
18561 it->constrain_row_ascent_descent_p = 0;
18562 it->current_x = saved_x;
18563 it->object = saved_object;
18564 it->position = saved_pos;
18565 it->what = saved_what;
18566 it->face_id = saved_face_id;
18567 it->len = saved_len;
18568 it->c = saved_c;
18569 it->char_to_display = saved_char_to_display;
18570 it->end_of_box_run_p = saved_box_end;
18571 return 1;
18572 }
18573 }
18574
18575 return 0;
18576 }
18577
18578
18579 /* Extend the face of the last glyph in the text area of IT->glyph_row
18580 to the end of the display line. Called from display_line. If the
18581 glyph row is empty, add a space glyph to it so that we know the
18582 face to draw. Set the glyph row flag fill_line_p. If the glyph
18583 row is R2L, prepend a stretch glyph to cover the empty space to the
18584 left of the leftmost glyph. */
18585
18586 static void
18587 extend_face_to_end_of_line (struct it *it)
18588 {
18589 struct face *face, *default_face;
18590 struct frame *f = it->f;
18591
18592 /* If line is already filled, do nothing. Non window-system frames
18593 get a grace of one more ``pixel'' because their characters are
18594 1-``pixel'' wide, so they hit the equality too early. This grace
18595 is needed only for R2L rows that are not continued, to produce
18596 one extra blank where we could display the cursor. */
18597 if (it->current_x >= it->last_visible_x
18598 + (!FRAME_WINDOW_P (f)
18599 && it->glyph_row->reversed_p
18600 && !it->glyph_row->continued_p))
18601 return;
18602
18603 /* The default face, possibly remapped. */
18604 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18605
18606 /* Face extension extends the background and box of IT->face_id
18607 to the end of the line. If the background equals the background
18608 of the frame, we don't have to do anything. */
18609 if (it->face_before_selective_p)
18610 face = FACE_FROM_ID (f, it->saved_face_id);
18611 else
18612 face = FACE_FROM_ID (f, it->face_id);
18613
18614 if (FRAME_WINDOW_P (f)
18615 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18616 && face->box == FACE_NO_BOX
18617 && face->background == FRAME_BACKGROUND_PIXEL (f)
18618 && !face->stipple
18619 && !it->glyph_row->reversed_p)
18620 return;
18621
18622 /* Set the glyph row flag indicating that the face of the last glyph
18623 in the text area has to be drawn to the end of the text area. */
18624 it->glyph_row->fill_line_p = 1;
18625
18626 /* If current character of IT is not ASCII, make sure we have the
18627 ASCII face. This will be automatically undone the next time
18628 get_next_display_element returns a multibyte character. Note
18629 that the character will always be single byte in unibyte
18630 text. */
18631 if (!ASCII_CHAR_P (it->c))
18632 {
18633 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18634 }
18635
18636 if (FRAME_WINDOW_P (f))
18637 {
18638 /* If the row is empty, add a space with the current face of IT,
18639 so that we know which face to draw. */
18640 if (it->glyph_row->used[TEXT_AREA] == 0)
18641 {
18642 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18643 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18644 it->glyph_row->used[TEXT_AREA] = 1;
18645 }
18646 #ifdef HAVE_WINDOW_SYSTEM
18647 if (it->glyph_row->reversed_p)
18648 {
18649 /* Prepend a stretch glyph to the row, such that the
18650 rightmost glyph will be drawn flushed all the way to the
18651 right margin of the window. The stretch glyph that will
18652 occupy the empty space, if any, to the left of the
18653 glyphs. */
18654 struct font *font = face->font ? face->font : FRAME_FONT (f);
18655 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18656 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18657 struct glyph *g;
18658 int row_width, stretch_ascent, stretch_width;
18659 struct text_pos saved_pos;
18660 int saved_face_id, saved_avoid_cursor, saved_box_start;
18661
18662 for (row_width = 0, g = row_start; g < row_end; g++)
18663 row_width += g->pixel_width;
18664 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18665 if (stretch_width > 0)
18666 {
18667 stretch_ascent =
18668 (((it->ascent + it->descent)
18669 * FONT_BASE (font)) / FONT_HEIGHT (font));
18670 saved_pos = it->position;
18671 memset (&it->position, 0, sizeof it->position);
18672 saved_avoid_cursor = it->avoid_cursor_p;
18673 it->avoid_cursor_p = 1;
18674 saved_face_id = it->face_id;
18675 saved_box_start = it->start_of_box_run_p;
18676 /* The last row's stretch glyph should get the default
18677 face, to avoid painting the rest of the window with
18678 the region face, if the region ends at ZV. */
18679 if (it->glyph_row->ends_at_zv_p)
18680 it->face_id = default_face->id;
18681 else
18682 it->face_id = face->id;
18683 it->start_of_box_run_p = 0;
18684 append_stretch_glyph (it, make_number (0), stretch_width,
18685 it->ascent + it->descent, stretch_ascent);
18686 it->position = saved_pos;
18687 it->avoid_cursor_p = saved_avoid_cursor;
18688 it->face_id = saved_face_id;
18689 it->start_of_box_run_p = saved_box_start;
18690 }
18691 }
18692 #endif /* HAVE_WINDOW_SYSTEM */
18693 }
18694 else
18695 {
18696 /* Save some values that must not be changed. */
18697 int saved_x = it->current_x;
18698 struct text_pos saved_pos;
18699 Lisp_Object saved_object;
18700 enum display_element_type saved_what = it->what;
18701 int saved_face_id = it->face_id;
18702
18703 saved_object = it->object;
18704 saved_pos = it->position;
18705
18706 it->what = IT_CHARACTER;
18707 memset (&it->position, 0, sizeof it->position);
18708 it->object = make_number (0);
18709 it->c = it->char_to_display = ' ';
18710 it->len = 1;
18711 /* The last row's blank glyphs should get the default face, to
18712 avoid painting the rest of the window with the region face,
18713 if the region ends at ZV. */
18714 if (it->glyph_row->ends_at_zv_p)
18715 it->face_id = default_face->id;
18716 else
18717 it->face_id = face->id;
18718
18719 PRODUCE_GLYPHS (it);
18720
18721 while (it->current_x <= it->last_visible_x)
18722 PRODUCE_GLYPHS (it);
18723
18724 /* Don't count these blanks really. It would let us insert a left
18725 truncation glyph below and make us set the cursor on them, maybe. */
18726 it->current_x = saved_x;
18727 it->object = saved_object;
18728 it->position = saved_pos;
18729 it->what = saved_what;
18730 it->face_id = saved_face_id;
18731 }
18732 }
18733
18734
18735 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18736 trailing whitespace. */
18737
18738 static int
18739 trailing_whitespace_p (ptrdiff_t charpos)
18740 {
18741 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18742 int c = 0;
18743
18744 while (bytepos < ZV_BYTE
18745 && (c = FETCH_CHAR (bytepos),
18746 c == ' ' || c == '\t'))
18747 ++bytepos;
18748
18749 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18750 {
18751 if (bytepos != PT_BYTE)
18752 return 1;
18753 }
18754 return 0;
18755 }
18756
18757
18758 /* Highlight trailing whitespace, if any, in ROW. */
18759
18760 static void
18761 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18762 {
18763 int used = row->used[TEXT_AREA];
18764
18765 if (used)
18766 {
18767 struct glyph *start = row->glyphs[TEXT_AREA];
18768 struct glyph *glyph = start + used - 1;
18769
18770 if (row->reversed_p)
18771 {
18772 /* Right-to-left rows need to be processed in the opposite
18773 direction, so swap the edge pointers. */
18774 glyph = start;
18775 start = row->glyphs[TEXT_AREA] + used - 1;
18776 }
18777
18778 /* Skip over glyphs inserted to display the cursor at the
18779 end of a line, for extending the face of the last glyph
18780 to the end of the line on terminals, and for truncation
18781 and continuation glyphs. */
18782 if (!row->reversed_p)
18783 {
18784 while (glyph >= start
18785 && glyph->type == CHAR_GLYPH
18786 && INTEGERP (glyph->object))
18787 --glyph;
18788 }
18789 else
18790 {
18791 while (glyph <= start
18792 && glyph->type == CHAR_GLYPH
18793 && INTEGERP (glyph->object))
18794 ++glyph;
18795 }
18796
18797 /* If last glyph is a space or stretch, and it's trailing
18798 whitespace, set the face of all trailing whitespace glyphs in
18799 IT->glyph_row to `trailing-whitespace'. */
18800 if ((row->reversed_p ? glyph <= start : glyph >= start)
18801 && BUFFERP (glyph->object)
18802 && (glyph->type == STRETCH_GLYPH
18803 || (glyph->type == CHAR_GLYPH
18804 && glyph->u.ch == ' '))
18805 && trailing_whitespace_p (glyph->charpos))
18806 {
18807 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18808 if (face_id < 0)
18809 return;
18810
18811 if (!row->reversed_p)
18812 {
18813 while (glyph >= start
18814 && BUFFERP (glyph->object)
18815 && (glyph->type == STRETCH_GLYPH
18816 || (glyph->type == CHAR_GLYPH
18817 && glyph->u.ch == ' ')))
18818 (glyph--)->face_id = face_id;
18819 }
18820 else
18821 {
18822 while (glyph <= start
18823 && BUFFERP (glyph->object)
18824 && (glyph->type == STRETCH_GLYPH
18825 || (glyph->type == CHAR_GLYPH
18826 && glyph->u.ch == ' ')))
18827 (glyph++)->face_id = face_id;
18828 }
18829 }
18830 }
18831 }
18832
18833
18834 /* Value is non-zero if glyph row ROW should be
18835 considered to hold the buffer position CHARPOS. */
18836
18837 static int
18838 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18839 {
18840 int result = 1;
18841
18842 if (charpos == CHARPOS (row->end.pos)
18843 || charpos == MATRIX_ROW_END_CHARPOS (row))
18844 {
18845 /* Suppose the row ends on a string.
18846 Unless the row is continued, that means it ends on a newline
18847 in the string. If it's anything other than a display string
18848 (e.g., a before-string from an overlay), we don't want the
18849 cursor there. (This heuristic seems to give the optimal
18850 behavior for the various types of multi-line strings.)
18851 One exception: if the string has `cursor' property on one of
18852 its characters, we _do_ want the cursor there. */
18853 if (CHARPOS (row->end.string_pos) >= 0)
18854 {
18855 if (row->continued_p)
18856 result = 1;
18857 else
18858 {
18859 /* Check for `display' property. */
18860 struct glyph *beg = row->glyphs[TEXT_AREA];
18861 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18862 struct glyph *glyph;
18863
18864 result = 0;
18865 for (glyph = end; glyph >= beg; --glyph)
18866 if (STRINGP (glyph->object))
18867 {
18868 Lisp_Object prop
18869 = Fget_char_property (make_number (charpos),
18870 Qdisplay, Qnil);
18871 result =
18872 (!NILP (prop)
18873 && display_prop_string_p (prop, glyph->object));
18874 /* If there's a `cursor' property on one of the
18875 string's characters, this row is a cursor row,
18876 even though this is not a display string. */
18877 if (!result)
18878 {
18879 Lisp_Object s = glyph->object;
18880
18881 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18882 {
18883 ptrdiff_t gpos = glyph->charpos;
18884
18885 if (!NILP (Fget_char_property (make_number (gpos),
18886 Qcursor, s)))
18887 {
18888 result = 1;
18889 break;
18890 }
18891 }
18892 }
18893 break;
18894 }
18895 }
18896 }
18897 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18898 {
18899 /* If the row ends in middle of a real character,
18900 and the line is continued, we want the cursor here.
18901 That's because CHARPOS (ROW->end.pos) would equal
18902 PT if PT is before the character. */
18903 if (!row->ends_in_ellipsis_p)
18904 result = row->continued_p;
18905 else
18906 /* If the row ends in an ellipsis, then
18907 CHARPOS (ROW->end.pos) will equal point after the
18908 invisible text. We want that position to be displayed
18909 after the ellipsis. */
18910 result = 0;
18911 }
18912 /* If the row ends at ZV, display the cursor at the end of that
18913 row instead of at the start of the row below. */
18914 else if (row->ends_at_zv_p)
18915 result = 1;
18916 else
18917 result = 0;
18918 }
18919
18920 return result;
18921 }
18922
18923 /* Value is non-zero if glyph row ROW should be
18924 used to hold the cursor. */
18925
18926 static int
18927 cursor_row_p (struct glyph_row *row)
18928 {
18929 return row_for_charpos_p (row, PT);
18930 }
18931
18932 \f
18933
18934 /* Push the property PROP so that it will be rendered at the current
18935 position in IT. Return 1 if PROP was successfully pushed, 0
18936 otherwise. Called from handle_line_prefix to handle the
18937 `line-prefix' and `wrap-prefix' properties. */
18938
18939 static int
18940 push_prefix_prop (struct it *it, Lisp_Object prop)
18941 {
18942 struct text_pos pos =
18943 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18944
18945 eassert (it->method == GET_FROM_BUFFER
18946 || it->method == GET_FROM_DISPLAY_VECTOR
18947 || it->method == GET_FROM_STRING);
18948
18949 /* We need to save the current buffer/string position, so it will be
18950 restored by pop_it, because iterate_out_of_display_property
18951 depends on that being set correctly, but some situations leave
18952 it->position not yet set when this function is called. */
18953 push_it (it, &pos);
18954
18955 if (STRINGP (prop))
18956 {
18957 if (SCHARS (prop) == 0)
18958 {
18959 pop_it (it);
18960 return 0;
18961 }
18962
18963 it->string = prop;
18964 it->string_from_prefix_prop_p = 1;
18965 it->multibyte_p = STRING_MULTIBYTE (it->string);
18966 it->current.overlay_string_index = -1;
18967 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18968 it->end_charpos = it->string_nchars = SCHARS (it->string);
18969 it->method = GET_FROM_STRING;
18970 it->stop_charpos = 0;
18971 it->prev_stop = 0;
18972 it->base_level_stop = 0;
18973
18974 /* Force paragraph direction to be that of the parent
18975 buffer/string. */
18976 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18977 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18978 else
18979 it->paragraph_embedding = L2R;
18980
18981 /* Set up the bidi iterator for this display string. */
18982 if (it->bidi_p)
18983 {
18984 it->bidi_it.string.lstring = it->string;
18985 it->bidi_it.string.s = NULL;
18986 it->bidi_it.string.schars = it->end_charpos;
18987 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18988 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18989 it->bidi_it.string.unibyte = !it->multibyte_p;
18990 it->bidi_it.w = it->w;
18991 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18992 }
18993 }
18994 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18995 {
18996 it->method = GET_FROM_STRETCH;
18997 it->object = prop;
18998 }
18999 #ifdef HAVE_WINDOW_SYSTEM
19000 else if (IMAGEP (prop))
19001 {
19002 it->what = IT_IMAGE;
19003 it->image_id = lookup_image (it->f, prop);
19004 it->method = GET_FROM_IMAGE;
19005 }
19006 #endif /* HAVE_WINDOW_SYSTEM */
19007 else
19008 {
19009 pop_it (it); /* bogus display property, give up */
19010 return 0;
19011 }
19012
19013 return 1;
19014 }
19015
19016 /* Return the character-property PROP at the current position in IT. */
19017
19018 static Lisp_Object
19019 get_it_property (struct it *it, Lisp_Object prop)
19020 {
19021 Lisp_Object position, object = it->object;
19022
19023 if (STRINGP (object))
19024 position = make_number (IT_STRING_CHARPOS (*it));
19025 else if (BUFFERP (object))
19026 {
19027 position = make_number (IT_CHARPOS (*it));
19028 object = it->window;
19029 }
19030 else
19031 return Qnil;
19032
19033 return Fget_char_property (position, prop, object);
19034 }
19035
19036 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19037
19038 static void
19039 handle_line_prefix (struct it *it)
19040 {
19041 Lisp_Object prefix;
19042
19043 if (it->continuation_lines_width > 0)
19044 {
19045 prefix = get_it_property (it, Qwrap_prefix);
19046 if (NILP (prefix))
19047 prefix = Vwrap_prefix;
19048 }
19049 else
19050 {
19051 prefix = get_it_property (it, Qline_prefix);
19052 if (NILP (prefix))
19053 prefix = Vline_prefix;
19054 }
19055 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19056 {
19057 /* If the prefix is wider than the window, and we try to wrap
19058 it, it would acquire its own wrap prefix, and so on till the
19059 iterator stack overflows. So, don't wrap the prefix. */
19060 it->line_wrap = TRUNCATE;
19061 it->avoid_cursor_p = 1;
19062 }
19063 }
19064
19065 \f
19066
19067 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19068 only for R2L lines from display_line and display_string, when they
19069 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19070 the line/string needs to be continued on the next glyph row. */
19071 static void
19072 unproduce_glyphs (struct it *it, int n)
19073 {
19074 struct glyph *glyph, *end;
19075
19076 eassert (it->glyph_row);
19077 eassert (it->glyph_row->reversed_p);
19078 eassert (it->area == TEXT_AREA);
19079 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19080
19081 if (n > it->glyph_row->used[TEXT_AREA])
19082 n = it->glyph_row->used[TEXT_AREA];
19083 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19084 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19085 for ( ; glyph < end; glyph++)
19086 glyph[-n] = *glyph;
19087 }
19088
19089 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19090 and ROW->maxpos. */
19091 static void
19092 find_row_edges (struct it *it, struct glyph_row *row,
19093 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19094 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19095 {
19096 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19097 lines' rows is implemented for bidi-reordered rows. */
19098
19099 /* ROW->minpos is the value of min_pos, the minimal buffer position
19100 we have in ROW, or ROW->start.pos if that is smaller. */
19101 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19102 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19103 else
19104 /* We didn't find buffer positions smaller than ROW->start, or
19105 didn't find _any_ valid buffer positions in any of the glyphs,
19106 so we must trust the iterator's computed positions. */
19107 row->minpos = row->start.pos;
19108 if (max_pos <= 0)
19109 {
19110 max_pos = CHARPOS (it->current.pos);
19111 max_bpos = BYTEPOS (it->current.pos);
19112 }
19113
19114 /* Here are the various use-cases for ending the row, and the
19115 corresponding values for ROW->maxpos:
19116
19117 Line ends in a newline from buffer eol_pos + 1
19118 Line is continued from buffer max_pos + 1
19119 Line is truncated on right it->current.pos
19120 Line ends in a newline from string max_pos + 1(*)
19121 (*) + 1 only when line ends in a forward scan
19122 Line is continued from string max_pos
19123 Line is continued from display vector max_pos
19124 Line is entirely from a string min_pos == max_pos
19125 Line is entirely from a display vector min_pos == max_pos
19126 Line that ends at ZV ZV
19127
19128 If you discover other use-cases, please add them here as
19129 appropriate. */
19130 if (row->ends_at_zv_p)
19131 row->maxpos = it->current.pos;
19132 else if (row->used[TEXT_AREA])
19133 {
19134 int seen_this_string = 0;
19135 struct glyph_row *r1 = row - 1;
19136
19137 /* Did we see the same display string on the previous row? */
19138 if (STRINGP (it->object)
19139 /* this is not the first row */
19140 && row > it->w->desired_matrix->rows
19141 /* previous row is not the header line */
19142 && !r1->mode_line_p
19143 /* previous row also ends in a newline from a string */
19144 && r1->ends_in_newline_from_string_p)
19145 {
19146 struct glyph *start, *end;
19147
19148 /* Search for the last glyph of the previous row that came
19149 from buffer or string. Depending on whether the row is
19150 L2R or R2L, we need to process it front to back or the
19151 other way round. */
19152 if (!r1->reversed_p)
19153 {
19154 start = r1->glyphs[TEXT_AREA];
19155 end = start + r1->used[TEXT_AREA];
19156 /* Glyphs inserted by redisplay have an integer (zero)
19157 as their object. */
19158 while (end > start
19159 && INTEGERP ((end - 1)->object)
19160 && (end - 1)->charpos <= 0)
19161 --end;
19162 if (end > start)
19163 {
19164 if (EQ ((end - 1)->object, it->object))
19165 seen_this_string = 1;
19166 }
19167 else
19168 /* If all the glyphs of the previous row were inserted
19169 by redisplay, it means the previous row was
19170 produced from a single newline, which is only
19171 possible if that newline came from the same string
19172 as the one which produced this ROW. */
19173 seen_this_string = 1;
19174 }
19175 else
19176 {
19177 end = r1->glyphs[TEXT_AREA] - 1;
19178 start = end + r1->used[TEXT_AREA];
19179 while (end < start
19180 && INTEGERP ((end + 1)->object)
19181 && (end + 1)->charpos <= 0)
19182 ++end;
19183 if (end < start)
19184 {
19185 if (EQ ((end + 1)->object, it->object))
19186 seen_this_string = 1;
19187 }
19188 else
19189 seen_this_string = 1;
19190 }
19191 }
19192 /* Take note of each display string that covers a newline only
19193 once, the first time we see it. This is for when a display
19194 string includes more than one newline in it. */
19195 if (row->ends_in_newline_from_string_p && !seen_this_string)
19196 {
19197 /* If we were scanning the buffer forward when we displayed
19198 the string, we want to account for at least one buffer
19199 position that belongs to this row (position covered by
19200 the display string), so that cursor positioning will
19201 consider this row as a candidate when point is at the end
19202 of the visual line represented by this row. This is not
19203 required when scanning back, because max_pos will already
19204 have a much larger value. */
19205 if (CHARPOS (row->end.pos) > max_pos)
19206 INC_BOTH (max_pos, max_bpos);
19207 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19208 }
19209 else if (CHARPOS (it->eol_pos) > 0)
19210 SET_TEXT_POS (row->maxpos,
19211 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19212 else if (row->continued_p)
19213 {
19214 /* If max_pos is different from IT's current position, it
19215 means IT->method does not belong to the display element
19216 at max_pos. However, it also means that the display
19217 element at max_pos was displayed in its entirety on this
19218 line, which is equivalent to saying that the next line
19219 starts at the next buffer position. */
19220 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19221 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19222 else
19223 {
19224 INC_BOTH (max_pos, max_bpos);
19225 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19226 }
19227 }
19228 else if (row->truncated_on_right_p)
19229 /* display_line already called reseat_at_next_visible_line_start,
19230 which puts the iterator at the beginning of the next line, in
19231 the logical order. */
19232 row->maxpos = it->current.pos;
19233 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19234 /* A line that is entirely from a string/image/stretch... */
19235 row->maxpos = row->minpos;
19236 else
19237 emacs_abort ();
19238 }
19239 else
19240 row->maxpos = it->current.pos;
19241 }
19242
19243 /* Construct the glyph row IT->glyph_row in the desired matrix of
19244 IT->w from text at the current position of IT. See dispextern.h
19245 for an overview of struct it. Value is non-zero if
19246 IT->glyph_row displays text, as opposed to a line displaying ZV
19247 only. */
19248
19249 static int
19250 display_line (struct it *it)
19251 {
19252 struct glyph_row *row = it->glyph_row;
19253 Lisp_Object overlay_arrow_string;
19254 struct it wrap_it;
19255 void *wrap_data = NULL;
19256 int may_wrap = 0, wrap_x IF_LINT (= 0);
19257 int wrap_row_used = -1;
19258 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19259 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19260 int wrap_row_extra_line_spacing IF_LINT (= 0);
19261 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19262 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19263 int cvpos;
19264 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19265 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19266
19267 /* We always start displaying at hpos zero even if hscrolled. */
19268 eassert (it->hpos == 0 && it->current_x == 0);
19269
19270 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19271 >= it->w->desired_matrix->nrows)
19272 {
19273 it->w->nrows_scale_factor++;
19274 it->f->fonts_changed = 1;
19275 return 0;
19276 }
19277
19278 /* Is IT->w showing the region? */
19279 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19280
19281 /* Clear the result glyph row and enable it. */
19282 prepare_desired_row (row);
19283
19284 row->y = it->current_y;
19285 row->start = it->start;
19286 row->continuation_lines_width = it->continuation_lines_width;
19287 row->displays_text_p = 1;
19288 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19289 it->starts_in_middle_of_char_p = 0;
19290
19291 /* Arrange the overlays nicely for our purposes. Usually, we call
19292 display_line on only one line at a time, in which case this
19293 can't really hurt too much, or we call it on lines which appear
19294 one after another in the buffer, in which case all calls to
19295 recenter_overlay_lists but the first will be pretty cheap. */
19296 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19297
19298 /* Move over display elements that are not visible because we are
19299 hscrolled. This may stop at an x-position < IT->first_visible_x
19300 if the first glyph is partially visible or if we hit a line end. */
19301 if (it->current_x < it->first_visible_x)
19302 {
19303 enum move_it_result move_result;
19304
19305 this_line_min_pos = row->start.pos;
19306 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19307 MOVE_TO_POS | MOVE_TO_X);
19308 /* If we are under a large hscroll, move_it_in_display_line_to
19309 could hit the end of the line without reaching
19310 it->first_visible_x. Pretend that we did reach it. This is
19311 especially important on a TTY, where we will call
19312 extend_face_to_end_of_line, which needs to know how many
19313 blank glyphs to produce. */
19314 if (it->current_x < it->first_visible_x
19315 && (move_result == MOVE_NEWLINE_OR_CR
19316 || move_result == MOVE_POS_MATCH_OR_ZV))
19317 it->current_x = it->first_visible_x;
19318
19319 /* Record the smallest positions seen while we moved over
19320 display elements that are not visible. This is needed by
19321 redisplay_internal for optimizing the case where the cursor
19322 stays inside the same line. The rest of this function only
19323 considers positions that are actually displayed, so
19324 RECORD_MAX_MIN_POS will not otherwise record positions that
19325 are hscrolled to the left of the left edge of the window. */
19326 min_pos = CHARPOS (this_line_min_pos);
19327 min_bpos = BYTEPOS (this_line_min_pos);
19328 }
19329 else
19330 {
19331 /* We only do this when not calling `move_it_in_display_line_to'
19332 above, because move_it_in_display_line_to calls
19333 handle_line_prefix itself. */
19334 handle_line_prefix (it);
19335 }
19336
19337 /* Get the initial row height. This is either the height of the
19338 text hscrolled, if there is any, or zero. */
19339 row->ascent = it->max_ascent;
19340 row->height = it->max_ascent + it->max_descent;
19341 row->phys_ascent = it->max_phys_ascent;
19342 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19343 row->extra_line_spacing = it->max_extra_line_spacing;
19344
19345 /* Utility macro to record max and min buffer positions seen until now. */
19346 #define RECORD_MAX_MIN_POS(IT) \
19347 do \
19348 { \
19349 int composition_p = !STRINGP ((IT)->string) \
19350 && ((IT)->what == IT_COMPOSITION); \
19351 ptrdiff_t current_pos = \
19352 composition_p ? (IT)->cmp_it.charpos \
19353 : IT_CHARPOS (*(IT)); \
19354 ptrdiff_t current_bpos = \
19355 composition_p ? CHAR_TO_BYTE (current_pos) \
19356 : IT_BYTEPOS (*(IT)); \
19357 if (current_pos < min_pos) \
19358 { \
19359 min_pos = current_pos; \
19360 min_bpos = current_bpos; \
19361 } \
19362 if (IT_CHARPOS (*it) > max_pos) \
19363 { \
19364 max_pos = IT_CHARPOS (*it); \
19365 max_bpos = IT_BYTEPOS (*it); \
19366 } \
19367 } \
19368 while (0)
19369
19370 /* Loop generating characters. The loop is left with IT on the next
19371 character to display. */
19372 while (1)
19373 {
19374 int n_glyphs_before, hpos_before, x_before;
19375 int x, nglyphs;
19376 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19377
19378 /* Retrieve the next thing to display. Value is zero if end of
19379 buffer reached. */
19380 if (!get_next_display_element (it))
19381 {
19382 /* Maybe add a space at the end of this line that is used to
19383 display the cursor there under X. Set the charpos of the
19384 first glyph of blank lines not corresponding to any text
19385 to -1. */
19386 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19387 row->exact_window_width_line_p = 1;
19388 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19389 || row->used[TEXT_AREA] == 0)
19390 {
19391 row->glyphs[TEXT_AREA]->charpos = -1;
19392 row->displays_text_p = 0;
19393
19394 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19395 && (!MINI_WINDOW_P (it->w)
19396 || (minibuf_level && EQ (it->window, minibuf_window))))
19397 row->indicate_empty_line_p = 1;
19398 }
19399
19400 it->continuation_lines_width = 0;
19401 row->ends_at_zv_p = 1;
19402 /* A row that displays right-to-left text must always have
19403 its last face extended all the way to the end of line,
19404 even if this row ends in ZV, because we still write to
19405 the screen left to right. We also need to extend the
19406 last face if the default face is remapped to some
19407 different face, otherwise the functions that clear
19408 portions of the screen will clear with the default face's
19409 background color. */
19410 if (row->reversed_p
19411 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19412 extend_face_to_end_of_line (it);
19413 break;
19414 }
19415
19416 /* Now, get the metrics of what we want to display. This also
19417 generates glyphs in `row' (which is IT->glyph_row). */
19418 n_glyphs_before = row->used[TEXT_AREA];
19419 x = it->current_x;
19420
19421 /* Remember the line height so far in case the next element doesn't
19422 fit on the line. */
19423 if (it->line_wrap != TRUNCATE)
19424 {
19425 ascent = it->max_ascent;
19426 descent = it->max_descent;
19427 phys_ascent = it->max_phys_ascent;
19428 phys_descent = it->max_phys_descent;
19429
19430 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19431 {
19432 if (IT_DISPLAYING_WHITESPACE (it))
19433 may_wrap = 1;
19434 else if (may_wrap)
19435 {
19436 SAVE_IT (wrap_it, *it, wrap_data);
19437 wrap_x = x;
19438 wrap_row_used = row->used[TEXT_AREA];
19439 wrap_row_ascent = row->ascent;
19440 wrap_row_height = row->height;
19441 wrap_row_phys_ascent = row->phys_ascent;
19442 wrap_row_phys_height = row->phys_height;
19443 wrap_row_extra_line_spacing = row->extra_line_spacing;
19444 wrap_row_min_pos = min_pos;
19445 wrap_row_min_bpos = min_bpos;
19446 wrap_row_max_pos = max_pos;
19447 wrap_row_max_bpos = max_bpos;
19448 may_wrap = 0;
19449 }
19450 }
19451 }
19452
19453 PRODUCE_GLYPHS (it);
19454
19455 /* If this display element was in marginal areas, continue with
19456 the next one. */
19457 if (it->area != TEXT_AREA)
19458 {
19459 row->ascent = max (row->ascent, it->max_ascent);
19460 row->height = max (row->height, it->max_ascent + it->max_descent);
19461 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19462 row->phys_height = max (row->phys_height,
19463 it->max_phys_ascent + it->max_phys_descent);
19464 row->extra_line_spacing = max (row->extra_line_spacing,
19465 it->max_extra_line_spacing);
19466 set_iterator_to_next (it, 1);
19467 continue;
19468 }
19469
19470 /* Does the display element fit on the line? If we truncate
19471 lines, we should draw past the right edge of the window. If
19472 we don't truncate, we want to stop so that we can display the
19473 continuation glyph before the right margin. If lines are
19474 continued, there are two possible strategies for characters
19475 resulting in more than 1 glyph (e.g. tabs): Display as many
19476 glyphs as possible in this line and leave the rest for the
19477 continuation line, or display the whole element in the next
19478 line. Original redisplay did the former, so we do it also. */
19479 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19480 hpos_before = it->hpos;
19481 x_before = x;
19482
19483 if (/* Not a newline. */
19484 nglyphs > 0
19485 /* Glyphs produced fit entirely in the line. */
19486 && it->current_x < it->last_visible_x)
19487 {
19488 it->hpos += nglyphs;
19489 row->ascent = max (row->ascent, it->max_ascent);
19490 row->height = max (row->height, it->max_ascent + it->max_descent);
19491 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19492 row->phys_height = max (row->phys_height,
19493 it->max_phys_ascent + it->max_phys_descent);
19494 row->extra_line_spacing = max (row->extra_line_spacing,
19495 it->max_extra_line_spacing);
19496 if (it->current_x - it->pixel_width < it->first_visible_x)
19497 row->x = x - it->first_visible_x;
19498 /* Record the maximum and minimum buffer positions seen so
19499 far in glyphs that will be displayed by this row. */
19500 if (it->bidi_p)
19501 RECORD_MAX_MIN_POS (it);
19502 }
19503 else
19504 {
19505 int i, new_x;
19506 struct glyph *glyph;
19507
19508 for (i = 0; i < nglyphs; ++i, x = new_x)
19509 {
19510 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19511 new_x = x + glyph->pixel_width;
19512
19513 if (/* Lines are continued. */
19514 it->line_wrap != TRUNCATE
19515 && (/* Glyph doesn't fit on the line. */
19516 new_x > it->last_visible_x
19517 /* Or it fits exactly on a window system frame. */
19518 || (new_x == it->last_visible_x
19519 && FRAME_WINDOW_P (it->f)
19520 && (row->reversed_p
19521 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19522 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19523 {
19524 /* End of a continued line. */
19525
19526 if (it->hpos == 0
19527 || (new_x == it->last_visible_x
19528 && FRAME_WINDOW_P (it->f)
19529 && (row->reversed_p
19530 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19531 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19532 {
19533 /* Current glyph is the only one on the line or
19534 fits exactly on the line. We must continue
19535 the line because we can't draw the cursor
19536 after the glyph. */
19537 row->continued_p = 1;
19538 it->current_x = new_x;
19539 it->continuation_lines_width += new_x;
19540 ++it->hpos;
19541 if (i == nglyphs - 1)
19542 {
19543 /* If line-wrap is on, check if a previous
19544 wrap point was found. */
19545 if (wrap_row_used > 0
19546 /* Even if there is a previous wrap
19547 point, continue the line here as
19548 usual, if (i) the previous character
19549 was a space or tab AND (ii) the
19550 current character is not. */
19551 && (!may_wrap
19552 || IT_DISPLAYING_WHITESPACE (it)))
19553 goto back_to_wrap;
19554
19555 /* Record the maximum and minimum buffer
19556 positions seen so far in glyphs that will be
19557 displayed by this row. */
19558 if (it->bidi_p)
19559 RECORD_MAX_MIN_POS (it);
19560 set_iterator_to_next (it, 1);
19561 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19562 {
19563 if (!get_next_display_element (it))
19564 {
19565 row->exact_window_width_line_p = 1;
19566 it->continuation_lines_width = 0;
19567 row->continued_p = 0;
19568 row->ends_at_zv_p = 1;
19569 }
19570 else if (ITERATOR_AT_END_OF_LINE_P (it))
19571 {
19572 row->continued_p = 0;
19573 row->exact_window_width_line_p = 1;
19574 }
19575 }
19576 }
19577 else if (it->bidi_p)
19578 RECORD_MAX_MIN_POS (it);
19579 }
19580 else if (CHAR_GLYPH_PADDING_P (*glyph)
19581 && !FRAME_WINDOW_P (it->f))
19582 {
19583 /* A padding glyph that doesn't fit on this line.
19584 This means the whole character doesn't fit
19585 on the line. */
19586 if (row->reversed_p)
19587 unproduce_glyphs (it, row->used[TEXT_AREA]
19588 - n_glyphs_before);
19589 row->used[TEXT_AREA] = n_glyphs_before;
19590
19591 /* Fill the rest of the row with continuation
19592 glyphs like in 20.x. */
19593 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19594 < row->glyphs[1 + TEXT_AREA])
19595 produce_special_glyphs (it, IT_CONTINUATION);
19596
19597 row->continued_p = 1;
19598 it->current_x = x_before;
19599 it->continuation_lines_width += x_before;
19600
19601 /* Restore the height to what it was before the
19602 element not fitting on the line. */
19603 it->max_ascent = ascent;
19604 it->max_descent = descent;
19605 it->max_phys_ascent = phys_ascent;
19606 it->max_phys_descent = phys_descent;
19607 }
19608 else if (wrap_row_used > 0)
19609 {
19610 back_to_wrap:
19611 if (row->reversed_p)
19612 unproduce_glyphs (it,
19613 row->used[TEXT_AREA] - wrap_row_used);
19614 RESTORE_IT (it, &wrap_it, wrap_data);
19615 it->continuation_lines_width += wrap_x;
19616 row->used[TEXT_AREA] = wrap_row_used;
19617 row->ascent = wrap_row_ascent;
19618 row->height = wrap_row_height;
19619 row->phys_ascent = wrap_row_phys_ascent;
19620 row->phys_height = wrap_row_phys_height;
19621 row->extra_line_spacing = wrap_row_extra_line_spacing;
19622 min_pos = wrap_row_min_pos;
19623 min_bpos = wrap_row_min_bpos;
19624 max_pos = wrap_row_max_pos;
19625 max_bpos = wrap_row_max_bpos;
19626 row->continued_p = 1;
19627 row->ends_at_zv_p = 0;
19628 row->exact_window_width_line_p = 0;
19629 it->continuation_lines_width += x;
19630
19631 /* Make sure that a non-default face is extended
19632 up to the right margin of the window. */
19633 extend_face_to_end_of_line (it);
19634 }
19635 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19636 {
19637 /* A TAB that extends past the right edge of the
19638 window. This produces a single glyph on
19639 window system frames. We leave the glyph in
19640 this row and let it fill the row, but don't
19641 consume the TAB. */
19642 if ((row->reversed_p
19643 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19644 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19645 produce_special_glyphs (it, IT_CONTINUATION);
19646 it->continuation_lines_width += it->last_visible_x;
19647 row->ends_in_middle_of_char_p = 1;
19648 row->continued_p = 1;
19649 glyph->pixel_width = it->last_visible_x - x;
19650 it->starts_in_middle_of_char_p = 1;
19651 }
19652 else
19653 {
19654 /* Something other than a TAB that draws past
19655 the right edge of the window. Restore
19656 positions to values before the element. */
19657 if (row->reversed_p)
19658 unproduce_glyphs (it, row->used[TEXT_AREA]
19659 - (n_glyphs_before + i));
19660 row->used[TEXT_AREA] = n_glyphs_before + i;
19661
19662 /* Display continuation glyphs. */
19663 it->current_x = x_before;
19664 it->continuation_lines_width += x;
19665 if (!FRAME_WINDOW_P (it->f)
19666 || (row->reversed_p
19667 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19668 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19669 produce_special_glyphs (it, IT_CONTINUATION);
19670 row->continued_p = 1;
19671
19672 extend_face_to_end_of_line (it);
19673
19674 if (nglyphs > 1 && i > 0)
19675 {
19676 row->ends_in_middle_of_char_p = 1;
19677 it->starts_in_middle_of_char_p = 1;
19678 }
19679
19680 /* Restore the height to what it was before the
19681 element not fitting on the line. */
19682 it->max_ascent = ascent;
19683 it->max_descent = descent;
19684 it->max_phys_ascent = phys_ascent;
19685 it->max_phys_descent = phys_descent;
19686 }
19687
19688 break;
19689 }
19690 else if (new_x > it->first_visible_x)
19691 {
19692 /* Increment number of glyphs actually displayed. */
19693 ++it->hpos;
19694
19695 /* Record the maximum and minimum buffer positions
19696 seen so far in glyphs that will be displayed by
19697 this row. */
19698 if (it->bidi_p)
19699 RECORD_MAX_MIN_POS (it);
19700
19701 if (x < it->first_visible_x)
19702 /* Glyph is partially visible, i.e. row starts at
19703 negative X position. */
19704 row->x = x - it->first_visible_x;
19705 }
19706 else
19707 {
19708 /* Glyph is completely off the left margin of the
19709 window. This should not happen because of the
19710 move_it_in_display_line at the start of this
19711 function, unless the text display area of the
19712 window is empty. */
19713 eassert (it->first_visible_x <= it->last_visible_x);
19714 }
19715 }
19716 /* Even if this display element produced no glyphs at all,
19717 we want to record its position. */
19718 if (it->bidi_p && nglyphs == 0)
19719 RECORD_MAX_MIN_POS (it);
19720
19721 row->ascent = max (row->ascent, it->max_ascent);
19722 row->height = max (row->height, it->max_ascent + it->max_descent);
19723 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19724 row->phys_height = max (row->phys_height,
19725 it->max_phys_ascent + it->max_phys_descent);
19726 row->extra_line_spacing = max (row->extra_line_spacing,
19727 it->max_extra_line_spacing);
19728
19729 /* End of this display line if row is continued. */
19730 if (row->continued_p || row->ends_at_zv_p)
19731 break;
19732 }
19733
19734 at_end_of_line:
19735 /* Is this a line end? If yes, we're also done, after making
19736 sure that a non-default face is extended up to the right
19737 margin of the window. */
19738 if (ITERATOR_AT_END_OF_LINE_P (it))
19739 {
19740 int used_before = row->used[TEXT_AREA];
19741
19742 row->ends_in_newline_from_string_p = STRINGP (it->object);
19743
19744 /* Add a space at the end of the line that is used to
19745 display the cursor there. */
19746 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19747 append_space_for_newline (it, 0);
19748
19749 /* Extend the face to the end of the line. */
19750 extend_face_to_end_of_line (it);
19751
19752 /* Make sure we have the position. */
19753 if (used_before == 0)
19754 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19755
19756 /* Record the position of the newline, for use in
19757 find_row_edges. */
19758 it->eol_pos = it->current.pos;
19759
19760 /* Consume the line end. This skips over invisible lines. */
19761 set_iterator_to_next (it, 1);
19762 it->continuation_lines_width = 0;
19763 break;
19764 }
19765
19766 /* Proceed with next display element. Note that this skips
19767 over lines invisible because of selective display. */
19768 set_iterator_to_next (it, 1);
19769
19770 /* If we truncate lines, we are done when the last displayed
19771 glyphs reach past the right margin of the window. */
19772 if (it->line_wrap == TRUNCATE
19773 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19774 ? (it->current_x >= it->last_visible_x)
19775 : (it->current_x > it->last_visible_x)))
19776 {
19777 /* Maybe add truncation glyphs. */
19778 if (!FRAME_WINDOW_P (it->f)
19779 || (row->reversed_p
19780 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19781 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19782 {
19783 int i, n;
19784
19785 if (!row->reversed_p)
19786 {
19787 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19788 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19789 break;
19790 }
19791 else
19792 {
19793 for (i = 0; i < row->used[TEXT_AREA]; i++)
19794 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19795 break;
19796 /* Remove any padding glyphs at the front of ROW, to
19797 make room for the truncation glyphs we will be
19798 adding below. The loop below always inserts at
19799 least one truncation glyph, so also remove the
19800 last glyph added to ROW. */
19801 unproduce_glyphs (it, i + 1);
19802 /* Adjust i for the loop below. */
19803 i = row->used[TEXT_AREA] - (i + 1);
19804 }
19805
19806 it->current_x = x_before;
19807 if (!FRAME_WINDOW_P (it->f))
19808 {
19809 for (n = row->used[TEXT_AREA]; i < n; ++i)
19810 {
19811 row->used[TEXT_AREA] = i;
19812 produce_special_glyphs (it, IT_TRUNCATION);
19813 }
19814 }
19815 else
19816 {
19817 row->used[TEXT_AREA] = i;
19818 produce_special_glyphs (it, IT_TRUNCATION);
19819 }
19820 }
19821 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19822 {
19823 /* Don't truncate if we can overflow newline into fringe. */
19824 if (!get_next_display_element (it))
19825 {
19826 it->continuation_lines_width = 0;
19827 row->ends_at_zv_p = 1;
19828 row->exact_window_width_line_p = 1;
19829 break;
19830 }
19831 if (ITERATOR_AT_END_OF_LINE_P (it))
19832 {
19833 row->exact_window_width_line_p = 1;
19834 goto at_end_of_line;
19835 }
19836 it->current_x = x_before;
19837 }
19838
19839 row->truncated_on_right_p = 1;
19840 it->continuation_lines_width = 0;
19841 reseat_at_next_visible_line_start (it, 0);
19842 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19843 it->hpos = hpos_before;
19844 break;
19845 }
19846 }
19847
19848 if (wrap_data)
19849 bidi_unshelve_cache (wrap_data, 1);
19850
19851 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19852 at the left window margin. */
19853 if (it->first_visible_x
19854 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19855 {
19856 if (!FRAME_WINDOW_P (it->f)
19857 || (row->reversed_p
19858 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19859 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19860 insert_left_trunc_glyphs (it);
19861 row->truncated_on_left_p = 1;
19862 }
19863
19864 /* Remember the position at which this line ends.
19865
19866 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19867 cannot be before the call to find_row_edges below, since that is
19868 where these positions are determined. */
19869 row->end = it->current;
19870 if (!it->bidi_p)
19871 {
19872 row->minpos = row->start.pos;
19873 row->maxpos = row->end.pos;
19874 }
19875 else
19876 {
19877 /* ROW->minpos and ROW->maxpos must be the smallest and
19878 `1 + the largest' buffer positions in ROW. But if ROW was
19879 bidi-reordered, these two positions can be anywhere in the
19880 row, so we must determine them now. */
19881 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19882 }
19883
19884 /* If the start of this line is the overlay arrow-position, then
19885 mark this glyph row as the one containing the overlay arrow.
19886 This is clearly a mess with variable size fonts. It would be
19887 better to let it be displayed like cursors under X. */
19888 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19889 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19890 !NILP (overlay_arrow_string)))
19891 {
19892 /* Overlay arrow in window redisplay is a fringe bitmap. */
19893 if (STRINGP (overlay_arrow_string))
19894 {
19895 struct glyph_row *arrow_row
19896 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19897 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19898 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19899 struct glyph *p = row->glyphs[TEXT_AREA];
19900 struct glyph *p2, *end;
19901
19902 /* Copy the arrow glyphs. */
19903 while (glyph < arrow_end)
19904 *p++ = *glyph++;
19905
19906 /* Throw away padding glyphs. */
19907 p2 = p;
19908 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19909 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19910 ++p2;
19911 if (p2 > p)
19912 {
19913 while (p2 < end)
19914 *p++ = *p2++;
19915 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19916 }
19917 }
19918 else
19919 {
19920 eassert (INTEGERP (overlay_arrow_string));
19921 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19922 }
19923 overlay_arrow_seen = 1;
19924 }
19925
19926 /* Highlight trailing whitespace. */
19927 if (!NILP (Vshow_trailing_whitespace))
19928 highlight_trailing_whitespace (it->f, it->glyph_row);
19929
19930 /* Compute pixel dimensions of this line. */
19931 compute_line_metrics (it);
19932
19933 /* Implementation note: No changes in the glyphs of ROW or in their
19934 faces can be done past this point, because compute_line_metrics
19935 computes ROW's hash value and stores it within the glyph_row
19936 structure. */
19937
19938 /* Record whether this row ends inside an ellipsis. */
19939 row->ends_in_ellipsis_p
19940 = (it->method == GET_FROM_DISPLAY_VECTOR
19941 && it->ellipsis_p);
19942
19943 /* Save fringe bitmaps in this row. */
19944 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19945 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19946 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19947 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19948
19949 it->left_user_fringe_bitmap = 0;
19950 it->left_user_fringe_face_id = 0;
19951 it->right_user_fringe_bitmap = 0;
19952 it->right_user_fringe_face_id = 0;
19953
19954 /* Maybe set the cursor. */
19955 cvpos = it->w->cursor.vpos;
19956 if ((cvpos < 0
19957 /* In bidi-reordered rows, keep checking for proper cursor
19958 position even if one has been found already, because buffer
19959 positions in such rows change non-linearly with ROW->VPOS,
19960 when a line is continued. One exception: when we are at ZV,
19961 display cursor on the first suitable glyph row, since all
19962 the empty rows after that also have their position set to ZV. */
19963 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19964 lines' rows is implemented for bidi-reordered rows. */
19965 || (it->bidi_p
19966 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19967 && PT >= MATRIX_ROW_START_CHARPOS (row)
19968 && PT <= MATRIX_ROW_END_CHARPOS (row)
19969 && cursor_row_p (row))
19970 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19971
19972 /* Prepare for the next line. This line starts horizontally at (X
19973 HPOS) = (0 0). Vertical positions are incremented. As a
19974 convenience for the caller, IT->glyph_row is set to the next
19975 row to be used. */
19976 it->current_x = it->hpos = 0;
19977 it->current_y += row->height;
19978 SET_TEXT_POS (it->eol_pos, 0, 0);
19979 ++it->vpos;
19980 ++it->glyph_row;
19981 /* The next row should by default use the same value of the
19982 reversed_p flag as this one. set_iterator_to_next decides when
19983 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19984 the flag accordingly. */
19985 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19986 it->glyph_row->reversed_p = row->reversed_p;
19987 it->start = row->end;
19988 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19989
19990 #undef RECORD_MAX_MIN_POS
19991 }
19992
19993 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19994 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19995 doc: /* Return paragraph direction at point in BUFFER.
19996 Value is either `left-to-right' or `right-to-left'.
19997 If BUFFER is omitted or nil, it defaults to the current buffer.
19998
19999 Paragraph direction determines how the text in the paragraph is displayed.
20000 In left-to-right paragraphs, text begins at the left margin of the window
20001 and the reading direction is generally left to right. In right-to-left
20002 paragraphs, text begins at the right margin and is read from right to left.
20003
20004 See also `bidi-paragraph-direction'. */)
20005 (Lisp_Object buffer)
20006 {
20007 struct buffer *buf = current_buffer;
20008 struct buffer *old = buf;
20009
20010 if (! NILP (buffer))
20011 {
20012 CHECK_BUFFER (buffer);
20013 buf = XBUFFER (buffer);
20014 }
20015
20016 if (NILP (BVAR (buf, bidi_display_reordering))
20017 || NILP (BVAR (buf, enable_multibyte_characters))
20018 /* When we are loading loadup.el, the character property tables
20019 needed for bidi iteration are not yet available. */
20020 || !NILP (Vpurify_flag))
20021 return Qleft_to_right;
20022 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20023 return BVAR (buf, bidi_paragraph_direction);
20024 else
20025 {
20026 /* Determine the direction from buffer text. We could try to
20027 use current_matrix if it is up to date, but this seems fast
20028 enough as it is. */
20029 struct bidi_it itb;
20030 ptrdiff_t pos = BUF_PT (buf);
20031 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20032 int c;
20033 void *itb_data = bidi_shelve_cache ();
20034
20035 set_buffer_temp (buf);
20036 /* bidi_paragraph_init finds the base direction of the paragraph
20037 by searching forward from paragraph start. We need the base
20038 direction of the current or _previous_ paragraph, so we need
20039 to make sure we are within that paragraph. To that end, find
20040 the previous non-empty line. */
20041 if (pos >= ZV && pos > BEGV)
20042 DEC_BOTH (pos, bytepos);
20043 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20044 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20045 {
20046 while ((c = FETCH_BYTE (bytepos)) == '\n'
20047 || c == ' ' || c == '\t' || c == '\f')
20048 {
20049 if (bytepos <= BEGV_BYTE)
20050 break;
20051 bytepos--;
20052 pos--;
20053 }
20054 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20055 bytepos--;
20056 }
20057 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20058 itb.paragraph_dir = NEUTRAL_DIR;
20059 itb.string.s = NULL;
20060 itb.string.lstring = Qnil;
20061 itb.string.bufpos = 0;
20062 itb.string.unibyte = 0;
20063 /* We have no window to use here for ignoring window-specific
20064 overlays. Using NULL for window pointer will cause
20065 compute_display_string_pos to use the current buffer. */
20066 itb.w = NULL;
20067 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20068 bidi_unshelve_cache (itb_data, 0);
20069 set_buffer_temp (old);
20070 switch (itb.paragraph_dir)
20071 {
20072 case L2R:
20073 return Qleft_to_right;
20074 break;
20075 case R2L:
20076 return Qright_to_left;
20077 break;
20078 default:
20079 emacs_abort ();
20080 }
20081 }
20082 }
20083
20084 DEFUN ("move-point-visually", Fmove_point_visually,
20085 Smove_point_visually, 1, 1, 0,
20086 doc: /* Move point in the visual order in the specified DIRECTION.
20087 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20088 left.
20089
20090 Value is the new character position of point. */)
20091 (Lisp_Object direction)
20092 {
20093 struct window *w = XWINDOW (selected_window);
20094 struct buffer *b = XBUFFER (w->contents);
20095 struct glyph_row *row;
20096 int dir;
20097 Lisp_Object paragraph_dir;
20098
20099 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20100 (!(ROW)->continued_p \
20101 && INTEGERP ((GLYPH)->object) \
20102 && (GLYPH)->type == CHAR_GLYPH \
20103 && (GLYPH)->u.ch == ' ' \
20104 && (GLYPH)->charpos >= 0 \
20105 && !(GLYPH)->avoid_cursor_p)
20106
20107 CHECK_NUMBER (direction);
20108 dir = XINT (direction);
20109 if (dir > 0)
20110 dir = 1;
20111 else
20112 dir = -1;
20113
20114 /* If current matrix is up-to-date, we can use the information
20115 recorded in the glyphs, at least as long as the goal is on the
20116 screen. */
20117 if (w->window_end_valid
20118 && !windows_or_buffers_changed
20119 && b
20120 && !b->clip_changed
20121 && !b->prevent_redisplay_optimizations_p
20122 && !window_outdated (w)
20123 && w->cursor.vpos >= 0
20124 && w->cursor.vpos < w->current_matrix->nrows
20125 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20126 {
20127 struct glyph *g = row->glyphs[TEXT_AREA];
20128 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20129 struct glyph *gpt = g + w->cursor.hpos;
20130
20131 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20132 {
20133 if (BUFFERP (g->object) && g->charpos != PT)
20134 {
20135 SET_PT (g->charpos);
20136 w->cursor.vpos = -1;
20137 return make_number (PT);
20138 }
20139 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20140 {
20141 ptrdiff_t new_pos;
20142
20143 if (BUFFERP (gpt->object))
20144 {
20145 new_pos = PT;
20146 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20147 new_pos += (row->reversed_p ? -dir : dir);
20148 else
20149 new_pos -= (row->reversed_p ? -dir : dir);;
20150 }
20151 else if (BUFFERP (g->object))
20152 new_pos = g->charpos;
20153 else
20154 break;
20155 SET_PT (new_pos);
20156 w->cursor.vpos = -1;
20157 return make_number (PT);
20158 }
20159 else if (ROW_GLYPH_NEWLINE_P (row, g))
20160 {
20161 /* Glyphs inserted at the end of a non-empty line for
20162 positioning the cursor have zero charpos, so we must
20163 deduce the value of point by other means. */
20164 if (g->charpos > 0)
20165 SET_PT (g->charpos);
20166 else if (row->ends_at_zv_p && PT != ZV)
20167 SET_PT (ZV);
20168 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20169 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20170 else
20171 break;
20172 w->cursor.vpos = -1;
20173 return make_number (PT);
20174 }
20175 }
20176 if (g == e || INTEGERP (g->object))
20177 {
20178 if (row->truncated_on_left_p || row->truncated_on_right_p)
20179 goto simulate_display;
20180 if (!row->reversed_p)
20181 row += dir;
20182 else
20183 row -= dir;
20184 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20185 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20186 goto simulate_display;
20187
20188 if (dir > 0)
20189 {
20190 if (row->reversed_p && !row->continued_p)
20191 {
20192 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20193 w->cursor.vpos = -1;
20194 return make_number (PT);
20195 }
20196 g = row->glyphs[TEXT_AREA];
20197 e = g + row->used[TEXT_AREA];
20198 for ( ; g < e; g++)
20199 {
20200 if (BUFFERP (g->object)
20201 /* Empty lines have only one glyph, which stands
20202 for the newline, and whose charpos is the
20203 buffer position of the newline. */
20204 || ROW_GLYPH_NEWLINE_P (row, g)
20205 /* When the buffer ends in a newline, the line at
20206 EOB also has one glyph, but its charpos is -1. */
20207 || (row->ends_at_zv_p
20208 && !row->reversed_p
20209 && INTEGERP (g->object)
20210 && g->type == CHAR_GLYPH
20211 && g->u.ch == ' '))
20212 {
20213 if (g->charpos > 0)
20214 SET_PT (g->charpos);
20215 else if (!row->reversed_p
20216 && row->ends_at_zv_p
20217 && PT != ZV)
20218 SET_PT (ZV);
20219 else
20220 continue;
20221 w->cursor.vpos = -1;
20222 return make_number (PT);
20223 }
20224 }
20225 }
20226 else
20227 {
20228 if (!row->reversed_p && !row->continued_p)
20229 {
20230 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20231 w->cursor.vpos = -1;
20232 return make_number (PT);
20233 }
20234 e = row->glyphs[TEXT_AREA];
20235 g = e + row->used[TEXT_AREA] - 1;
20236 for ( ; g >= e; g--)
20237 {
20238 if (BUFFERP (g->object)
20239 || (ROW_GLYPH_NEWLINE_P (row, g)
20240 && g->charpos > 0)
20241 /* Empty R2L lines on GUI frames have the buffer
20242 position of the newline stored in the stretch
20243 glyph. */
20244 || g->type == STRETCH_GLYPH
20245 || (row->ends_at_zv_p
20246 && row->reversed_p
20247 && INTEGERP (g->object)
20248 && g->type == CHAR_GLYPH
20249 && g->u.ch == ' '))
20250 {
20251 if (g->charpos > 0)
20252 SET_PT (g->charpos);
20253 else if (row->reversed_p
20254 && row->ends_at_zv_p
20255 && PT != ZV)
20256 SET_PT (ZV);
20257 else
20258 continue;
20259 w->cursor.vpos = -1;
20260 return make_number (PT);
20261 }
20262 }
20263 }
20264 }
20265 }
20266
20267 simulate_display:
20268
20269 /* If we wind up here, we failed to move by using the glyphs, so we
20270 need to simulate display instead. */
20271
20272 if (b)
20273 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20274 else
20275 paragraph_dir = Qleft_to_right;
20276 if (EQ (paragraph_dir, Qright_to_left))
20277 dir = -dir;
20278 if (PT <= BEGV && dir < 0)
20279 xsignal0 (Qbeginning_of_buffer);
20280 else if (PT >= ZV && dir > 0)
20281 xsignal0 (Qend_of_buffer);
20282 else
20283 {
20284 struct text_pos pt;
20285 struct it it;
20286 int pt_x, target_x, pixel_width, pt_vpos;
20287 bool at_eol_p;
20288 bool overshoot_expected = false;
20289 bool target_is_eol_p = false;
20290
20291 /* Setup the arena. */
20292 SET_TEXT_POS (pt, PT, PT_BYTE);
20293 start_display (&it, w, pt);
20294
20295 if (it.cmp_it.id < 0
20296 && it.method == GET_FROM_STRING
20297 && it.area == TEXT_AREA
20298 && it.string_from_display_prop_p
20299 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20300 overshoot_expected = true;
20301
20302 /* Find the X coordinate of point. We start from the beginning
20303 of this or previous line to make sure we are before point in
20304 the logical order (since the move_it_* functions can only
20305 move forward). */
20306 reseat_at_previous_visible_line_start (&it);
20307 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20308 if (IT_CHARPOS (it) != PT)
20309 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20310 -1, -1, -1, MOVE_TO_POS);
20311 pt_x = it.current_x;
20312 pt_vpos = it.vpos;
20313 if (dir > 0 || overshoot_expected)
20314 {
20315 struct glyph_row *row = it.glyph_row;
20316
20317 /* When point is at beginning of line, we don't have
20318 information about the glyph there loaded into struct
20319 it. Calling get_next_display_element fixes that. */
20320 if (pt_x == 0)
20321 get_next_display_element (&it);
20322 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20323 it.glyph_row = NULL;
20324 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20325 it.glyph_row = row;
20326 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20327 it, lest it will become out of sync with it's buffer
20328 position. */
20329 it.current_x = pt_x;
20330 }
20331 else
20332 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20333 pixel_width = it.pixel_width;
20334 if (overshoot_expected && at_eol_p)
20335 pixel_width = 0;
20336 else if (pixel_width <= 0)
20337 pixel_width = 1;
20338
20339 /* If there's a display string at point, we are actually at the
20340 glyph to the left of point, so we need to correct the X
20341 coordinate. */
20342 if (overshoot_expected)
20343 pt_x += pixel_width;
20344
20345 /* Compute target X coordinate, either to the left or to the
20346 right of point. On TTY frames, all characters have the same
20347 pixel width of 1, so we can use that. On GUI frames we don't
20348 have an easy way of getting at the pixel width of the
20349 character to the left of point, so we use a different method
20350 of getting to that place. */
20351 if (dir > 0)
20352 target_x = pt_x + pixel_width;
20353 else
20354 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20355
20356 /* Target X coordinate could be one line above or below the line
20357 of point, in which case we need to adjust the target X
20358 coordinate. Also, if moving to the left, we need to begin at
20359 the left edge of the point's screen line. */
20360 if (dir < 0)
20361 {
20362 if (pt_x > 0)
20363 {
20364 start_display (&it, w, pt);
20365 reseat_at_previous_visible_line_start (&it);
20366 it.current_x = it.current_y = it.hpos = 0;
20367 if (pt_vpos != 0)
20368 move_it_by_lines (&it, pt_vpos);
20369 }
20370 else
20371 {
20372 move_it_by_lines (&it, -1);
20373 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20374 target_is_eol_p = true;
20375 }
20376 }
20377 else
20378 {
20379 if (at_eol_p
20380 || (target_x >= it.last_visible_x
20381 && it.line_wrap != TRUNCATE))
20382 {
20383 if (pt_x > 0)
20384 move_it_by_lines (&it, 0);
20385 move_it_by_lines (&it, 1);
20386 target_x = 0;
20387 }
20388 }
20389
20390 /* Move to the target X coordinate. */
20391 #ifdef HAVE_WINDOW_SYSTEM
20392 /* On GUI frames, as we don't know the X coordinate of the
20393 character to the left of point, moving point to the left
20394 requires walking, one grapheme cluster at a time, until we
20395 find ourself at a place immediately to the left of the
20396 character at point. */
20397 if (FRAME_WINDOW_P (it.f) && dir < 0)
20398 {
20399 struct text_pos new_pos = it.current.pos;
20400 enum move_it_result rc = MOVE_X_REACHED;
20401
20402 while (it.current_x + it.pixel_width <= target_x
20403 && rc == MOVE_X_REACHED)
20404 {
20405 int new_x = it.current_x + it.pixel_width;
20406
20407 new_pos = it.current.pos;
20408 if (new_x == it.current_x)
20409 new_x++;
20410 rc = move_it_in_display_line_to (&it, ZV, new_x,
20411 MOVE_TO_POS | MOVE_TO_X);
20412 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20413 break;
20414 }
20415 /* If we ended up on a composed character inside
20416 bidi-reordered text (e.g., Hebrew text with diacritics),
20417 the iterator gives us the buffer position of the last (in
20418 logical order) character of the composed grapheme cluster,
20419 which is not what we want. So we cheat: we compute the
20420 character position of the character that follows (in the
20421 logical order) the one where the above loop stopped. That
20422 character will appear on display to the left of point. */
20423 if (it.bidi_p
20424 && it.bidi_it.scan_dir == -1
20425 && new_pos.charpos - IT_CHARPOS (it) > 1)
20426 {
20427 new_pos.charpos = IT_CHARPOS (it) + 1;
20428 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20429 }
20430 it.current.pos = new_pos;
20431 }
20432 else
20433 #endif
20434 if (it.current_x != target_x)
20435 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20436
20437 /* When lines are truncated, the above loop will stop at the
20438 window edge. But we want to get to the end of line, even if
20439 it is beyond the window edge; automatic hscroll will then
20440 scroll the window to show point as appropriate. */
20441 if (target_is_eol_p && it.line_wrap == TRUNCATE
20442 && get_next_display_element (&it))
20443 {
20444 struct text_pos new_pos = it.current.pos;
20445
20446 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20447 {
20448 set_iterator_to_next (&it, 0);
20449 if (it.method == GET_FROM_BUFFER)
20450 new_pos = it.current.pos;
20451 if (!get_next_display_element (&it))
20452 break;
20453 }
20454
20455 it.current.pos = new_pos;
20456 }
20457
20458 /* If we ended up in a display string that covers point, move to
20459 buffer position to the right in the visual order. */
20460 if (dir > 0)
20461 {
20462 while (IT_CHARPOS (it) == PT)
20463 {
20464 set_iterator_to_next (&it, 0);
20465 if (!get_next_display_element (&it))
20466 break;
20467 }
20468 }
20469
20470 /* Move point to that position. */
20471 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20472 }
20473
20474 return make_number (PT);
20475
20476 #undef ROW_GLYPH_NEWLINE_P
20477 }
20478
20479 \f
20480 /***********************************************************************
20481 Menu Bar
20482 ***********************************************************************/
20483
20484 /* Redisplay the menu bar in the frame for window W.
20485
20486 The menu bar of X frames that don't have X toolkit support is
20487 displayed in a special window W->frame->menu_bar_window.
20488
20489 The menu bar of terminal frames is treated specially as far as
20490 glyph matrices are concerned. Menu bar lines are not part of
20491 windows, so the update is done directly on the frame matrix rows
20492 for the menu bar. */
20493
20494 static void
20495 display_menu_bar (struct window *w)
20496 {
20497 struct frame *f = XFRAME (WINDOW_FRAME (w));
20498 struct it it;
20499 Lisp_Object items;
20500 int i;
20501
20502 /* Don't do all this for graphical frames. */
20503 #ifdef HAVE_NTGUI
20504 if (FRAME_W32_P (f))
20505 return;
20506 #endif
20507 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20508 if (FRAME_X_P (f))
20509 return;
20510 #endif
20511
20512 #ifdef HAVE_NS
20513 if (FRAME_NS_P (f))
20514 return;
20515 #endif /* HAVE_NS */
20516
20517 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20518 eassert (!FRAME_WINDOW_P (f));
20519 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20520 it.first_visible_x = 0;
20521 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20522 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20523 if (FRAME_WINDOW_P (f))
20524 {
20525 /* Menu bar lines are displayed in the desired matrix of the
20526 dummy window menu_bar_window. */
20527 struct window *menu_w;
20528 menu_w = XWINDOW (f->menu_bar_window);
20529 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20530 MENU_FACE_ID);
20531 it.first_visible_x = 0;
20532 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20533 }
20534 else
20535 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20536 {
20537 /* This is a TTY frame, i.e. character hpos/vpos are used as
20538 pixel x/y. */
20539 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20540 MENU_FACE_ID);
20541 it.first_visible_x = 0;
20542 it.last_visible_x = FRAME_COLS (f);
20543 }
20544
20545 /* FIXME: This should be controlled by a user option. See the
20546 comments in redisplay_tool_bar and display_mode_line about
20547 this. */
20548 it.paragraph_embedding = L2R;
20549
20550 /* Clear all rows of the menu bar. */
20551 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20552 {
20553 struct glyph_row *row = it.glyph_row + i;
20554 clear_glyph_row (row);
20555 row->enabled_p = 1;
20556 row->full_width_p = 1;
20557 }
20558
20559 /* Display all items of the menu bar. */
20560 items = FRAME_MENU_BAR_ITEMS (it.f);
20561 for (i = 0; i < ASIZE (items); i += 4)
20562 {
20563 Lisp_Object string;
20564
20565 /* Stop at nil string. */
20566 string = AREF (items, i + 1);
20567 if (NILP (string))
20568 break;
20569
20570 /* Remember where item was displayed. */
20571 ASET (items, i + 3, make_number (it.hpos));
20572
20573 /* Display the item, pad with one space. */
20574 if (it.current_x < it.last_visible_x)
20575 display_string (NULL, string, Qnil, 0, 0, &it,
20576 SCHARS (string) + 1, 0, 0, -1);
20577 }
20578
20579 /* Fill out the line with spaces. */
20580 if (it.current_x < it.last_visible_x)
20581 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20582
20583 /* Compute the total height of the lines. */
20584 compute_line_metrics (&it);
20585 }
20586
20587 #ifdef HAVE_MENUS
20588 /* Deep copy of a glyph row, including the glyphs. */
20589 static void
20590 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20591 {
20592 int area, i, sum_used = 0;
20593 struct glyph *pointers[1 + LAST_AREA];
20594
20595 /* Save glyph pointers of TO. */
20596 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20597
20598 /* Do a structure assignment. */
20599 *to = *from;
20600
20601 /* Restore original pointers of TO. */
20602 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20603
20604 /* Count how many glyphs to copy and update glyph pointers. */
20605 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
20606 {
20607 if (area > LEFT_MARGIN_AREA)
20608 {
20609 eassert (from->glyphs[area] - from->glyphs[area - 1]
20610 == from->used[area - 1]);
20611 to->glyphs[area] = to->glyphs[area - 1] + to->used[area - 1];
20612 }
20613 sum_used += from->used[area];
20614 }
20615
20616 /* Copy the glyphs. */
20617 eassert (sum_used <= to->glyphs[LAST_AREA] - to->glyphs[LEFT_MARGIN_AREA]);
20618 for (i = 0; i < sum_used; i++)
20619 to->glyphs[LEFT_MARGIN_AREA][i] = from->glyphs[LEFT_MARGIN_AREA][i];
20620 }
20621
20622 /* Display one menu item on a TTY, by overwriting the glyphs in the
20623 frame F's desired glyph matrix with glyphs produced from the menu
20624 item text. Called from term.c to display TTY drop-down menus one
20625 item at a time.
20626
20627 ITEM_TEXT is the menu item text as a C string.
20628
20629 FACE_ID is the face ID to be used for this menu item. FACE_ID
20630 could specify one of 3 faces: a face for an enabled item, a face
20631 for a disabled item, or a face for a selected item.
20632
20633 X and Y are coordinates of the first glyph in the frame's desired
20634 matrix to be overwritten by the menu item. Since this is a TTY, Y
20635 is the zero-based number of the glyph row and X is the zero-based
20636 glyph number in the row, starting from left, where to start
20637 displaying the item.
20638
20639 SUBMENU non-zero means this menu item drops down a submenu, which
20640 should be indicated by displaying a proper visual cue after the
20641 item text. */
20642
20643 void
20644 display_tty_menu_item (const char *item_text, int width, int face_id,
20645 int x, int y, int submenu)
20646 {
20647 struct it it;
20648 struct frame *f = SELECTED_FRAME ();
20649 struct window *w = XWINDOW (f->selected_window);
20650 int saved_used, saved_truncated, saved_width, saved_reversed;
20651 struct glyph_row *row;
20652 size_t item_len = strlen (item_text);
20653
20654 eassert (FRAME_TERMCAP_P (f));
20655
20656 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
20657 it.first_visible_x = 0;
20658 it.last_visible_x = FRAME_COLS (f) - 1;
20659 row = it.glyph_row;
20660 /* Start with the row contents from the current matrix. */
20661 deep_copy_glyph_row (row, f->current_matrix->rows + y);
20662 saved_width = row->full_width_p;
20663 row->full_width_p = 1;
20664 saved_reversed = row->reversed_p;
20665 row->reversed_p = 0;
20666 row->enabled_p = 1;
20667
20668 /* Arrange for the menu item glyphs to start at (X,Y) and have the
20669 desired face. */
20670 it.current_x = it.hpos = x;
20671 it.current_y = it.vpos = y;
20672 saved_used = row->used[TEXT_AREA];
20673 saved_truncated = row->truncated_on_right_p;
20674 row->used[TEXT_AREA] = x;
20675 it.face_id = face_id;
20676 it.line_wrap = TRUNCATE;
20677
20678 /* FIXME: This should be controlled by a user option. See the
20679 comments in redisplay_tool_bar and display_mode_line about this.
20680 Also, if paragraph_embedding could ever be R2L, changes will be
20681 needed to avoid shifting to the right the row characters in
20682 term.c:append_glyph. */
20683 it.paragraph_embedding = L2R;
20684
20685 /* Pad with a space on the left. */
20686 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
20687 width--;
20688 /* Display the menu item, pad with spaces to WIDTH. */
20689 if (submenu)
20690 {
20691 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20692 item_len, 0, FRAME_COLS (f) - 1, -1);
20693 width -= item_len;
20694 /* Indicate with " >" that there's a submenu. */
20695 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
20696 FRAME_COLS (f) - 1, -1);
20697 }
20698 else
20699 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20700 width, 0, FRAME_COLS (f) - 1, -1);
20701
20702 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
20703 row->truncated_on_right_p = saved_truncated;
20704 row->hash = row_hash (row);
20705 row->full_width_p = saved_width;
20706 row->reversed_p = saved_reversed;
20707 }
20708 #endif /* HAVE_MENUS */
20709 \f
20710 /***********************************************************************
20711 Mode Line
20712 ***********************************************************************/
20713
20714 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20715 FORCE is non-zero, redisplay mode lines unconditionally.
20716 Otherwise, redisplay only mode lines that are garbaged. Value is
20717 the number of windows whose mode lines were redisplayed. */
20718
20719 static int
20720 redisplay_mode_lines (Lisp_Object window, int force)
20721 {
20722 int nwindows = 0;
20723
20724 while (!NILP (window))
20725 {
20726 struct window *w = XWINDOW (window);
20727
20728 if (WINDOWP (w->contents))
20729 nwindows += redisplay_mode_lines (w->contents, force);
20730 else if (force
20731 || FRAME_GARBAGED_P (XFRAME (w->frame))
20732 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20733 {
20734 struct text_pos lpoint;
20735 struct buffer *old = current_buffer;
20736
20737 /* Set the window's buffer for the mode line display. */
20738 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20739 set_buffer_internal_1 (XBUFFER (w->contents));
20740
20741 /* Point refers normally to the selected window. For any
20742 other window, set up appropriate value. */
20743 if (!EQ (window, selected_window))
20744 {
20745 struct text_pos pt;
20746
20747 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20748 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20749 }
20750
20751 /* Display mode lines. */
20752 clear_glyph_matrix (w->desired_matrix);
20753 if (display_mode_lines (w))
20754 {
20755 ++nwindows;
20756 w->must_be_updated_p = 1;
20757 }
20758
20759 /* Restore old settings. */
20760 set_buffer_internal_1 (old);
20761 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20762 }
20763
20764 window = w->next;
20765 }
20766
20767 return nwindows;
20768 }
20769
20770
20771 /* Display the mode and/or header line of window W. Value is the
20772 sum number of mode lines and header lines displayed. */
20773
20774 static int
20775 display_mode_lines (struct window *w)
20776 {
20777 Lisp_Object old_selected_window = selected_window;
20778 Lisp_Object old_selected_frame = selected_frame;
20779 Lisp_Object new_frame = w->frame;
20780 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20781 int n = 0;
20782
20783 selected_frame = new_frame;
20784 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20785 or window's point, then we'd need select_window_1 here as well. */
20786 XSETWINDOW (selected_window, w);
20787 XFRAME (new_frame)->selected_window = selected_window;
20788
20789 /* These will be set while the mode line specs are processed. */
20790 line_number_displayed = 0;
20791 w->column_number_displayed = -1;
20792
20793 if (WINDOW_WANTS_MODELINE_P (w))
20794 {
20795 struct window *sel_w = XWINDOW (old_selected_window);
20796
20797 /* Select mode line face based on the real selected window. */
20798 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20799 BVAR (current_buffer, mode_line_format));
20800 ++n;
20801 }
20802
20803 if (WINDOW_WANTS_HEADER_LINE_P (w))
20804 {
20805 display_mode_line (w, HEADER_LINE_FACE_ID,
20806 BVAR (current_buffer, header_line_format));
20807 ++n;
20808 }
20809
20810 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20811 selected_frame = old_selected_frame;
20812 selected_window = old_selected_window;
20813 return n;
20814 }
20815
20816
20817 /* Display mode or header line of window W. FACE_ID specifies which
20818 line to display; it is either MODE_LINE_FACE_ID or
20819 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20820 display. Value is the pixel height of the mode/header line
20821 displayed. */
20822
20823 static int
20824 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20825 {
20826 struct it it;
20827 struct face *face;
20828 ptrdiff_t count = SPECPDL_INDEX ();
20829
20830 init_iterator (&it, w, -1, -1, NULL, face_id);
20831 /* Don't extend on a previously drawn mode-line.
20832 This may happen if called from pos_visible_p. */
20833 it.glyph_row->enabled_p = 0;
20834 prepare_desired_row (it.glyph_row);
20835
20836 it.glyph_row->mode_line_p = 1;
20837
20838 /* FIXME: This should be controlled by a user option. But
20839 supporting such an option is not trivial, since the mode line is
20840 made up of many separate strings. */
20841 it.paragraph_embedding = L2R;
20842
20843 record_unwind_protect (unwind_format_mode_line,
20844 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20845
20846 mode_line_target = MODE_LINE_DISPLAY;
20847
20848 /* Temporarily make frame's keyboard the current kboard so that
20849 kboard-local variables in the mode_line_format will get the right
20850 values. */
20851 push_kboard (FRAME_KBOARD (it.f));
20852 record_unwind_save_match_data ();
20853 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20854 pop_kboard ();
20855
20856 unbind_to (count, Qnil);
20857
20858 /* Fill up with spaces. */
20859 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20860
20861 compute_line_metrics (&it);
20862 it.glyph_row->full_width_p = 1;
20863 it.glyph_row->continued_p = 0;
20864 it.glyph_row->truncated_on_left_p = 0;
20865 it.glyph_row->truncated_on_right_p = 0;
20866
20867 /* Make a 3D mode-line have a shadow at its right end. */
20868 face = FACE_FROM_ID (it.f, face_id);
20869 extend_face_to_end_of_line (&it);
20870 if (face->box != FACE_NO_BOX)
20871 {
20872 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20873 + it.glyph_row->used[TEXT_AREA] - 1);
20874 last->right_box_line_p = 1;
20875 }
20876
20877 return it.glyph_row->height;
20878 }
20879
20880 /* Move element ELT in LIST to the front of LIST.
20881 Return the updated list. */
20882
20883 static Lisp_Object
20884 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20885 {
20886 register Lisp_Object tail, prev;
20887 register Lisp_Object tem;
20888
20889 tail = list;
20890 prev = Qnil;
20891 while (CONSP (tail))
20892 {
20893 tem = XCAR (tail);
20894
20895 if (EQ (elt, tem))
20896 {
20897 /* Splice out the link TAIL. */
20898 if (NILP (prev))
20899 list = XCDR (tail);
20900 else
20901 Fsetcdr (prev, XCDR (tail));
20902
20903 /* Now make it the first. */
20904 Fsetcdr (tail, list);
20905 return tail;
20906 }
20907 else
20908 prev = tail;
20909 tail = XCDR (tail);
20910 QUIT;
20911 }
20912
20913 /* Not found--return unchanged LIST. */
20914 return list;
20915 }
20916
20917 /* Contribute ELT to the mode line for window IT->w. How it
20918 translates into text depends on its data type.
20919
20920 IT describes the display environment in which we display, as usual.
20921
20922 DEPTH is the depth in recursion. It is used to prevent
20923 infinite recursion here.
20924
20925 FIELD_WIDTH is the number of characters the display of ELT should
20926 occupy in the mode line, and PRECISION is the maximum number of
20927 characters to display from ELT's representation. See
20928 display_string for details.
20929
20930 Returns the hpos of the end of the text generated by ELT.
20931
20932 PROPS is a property list to add to any string we encounter.
20933
20934 If RISKY is nonzero, remove (disregard) any properties in any string
20935 we encounter, and ignore :eval and :propertize.
20936
20937 The global variable `mode_line_target' determines whether the
20938 output is passed to `store_mode_line_noprop',
20939 `store_mode_line_string', or `display_string'. */
20940
20941 static int
20942 display_mode_element (struct it *it, int depth, int field_width, int precision,
20943 Lisp_Object elt, Lisp_Object props, int risky)
20944 {
20945 int n = 0, field, prec;
20946 int literal = 0;
20947
20948 tail_recurse:
20949 if (depth > 100)
20950 elt = build_string ("*too-deep*");
20951
20952 depth++;
20953
20954 switch (XTYPE (elt))
20955 {
20956 case Lisp_String:
20957 {
20958 /* A string: output it and check for %-constructs within it. */
20959 unsigned char c;
20960 ptrdiff_t offset = 0;
20961
20962 if (SCHARS (elt) > 0
20963 && (!NILP (props) || risky))
20964 {
20965 Lisp_Object oprops, aelt;
20966 oprops = Ftext_properties_at (make_number (0), elt);
20967
20968 /* If the starting string's properties are not what
20969 we want, translate the string. Also, if the string
20970 is risky, do that anyway. */
20971
20972 if (NILP (Fequal (props, oprops)) || risky)
20973 {
20974 /* If the starting string has properties,
20975 merge the specified ones onto the existing ones. */
20976 if (! NILP (oprops) && !risky)
20977 {
20978 Lisp_Object tem;
20979
20980 oprops = Fcopy_sequence (oprops);
20981 tem = props;
20982 while (CONSP (tem))
20983 {
20984 oprops = Fplist_put (oprops, XCAR (tem),
20985 XCAR (XCDR (tem)));
20986 tem = XCDR (XCDR (tem));
20987 }
20988 props = oprops;
20989 }
20990
20991 aelt = Fassoc (elt, mode_line_proptrans_alist);
20992 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20993 {
20994 /* AELT is what we want. Move it to the front
20995 without consing. */
20996 elt = XCAR (aelt);
20997 mode_line_proptrans_alist
20998 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20999 }
21000 else
21001 {
21002 Lisp_Object tem;
21003
21004 /* If AELT has the wrong props, it is useless.
21005 so get rid of it. */
21006 if (! NILP (aelt))
21007 mode_line_proptrans_alist
21008 = Fdelq (aelt, mode_line_proptrans_alist);
21009
21010 elt = Fcopy_sequence (elt);
21011 Fset_text_properties (make_number (0), Flength (elt),
21012 props, elt);
21013 /* Add this item to mode_line_proptrans_alist. */
21014 mode_line_proptrans_alist
21015 = Fcons (Fcons (elt, props),
21016 mode_line_proptrans_alist);
21017 /* Truncate mode_line_proptrans_alist
21018 to at most 50 elements. */
21019 tem = Fnthcdr (make_number (50),
21020 mode_line_proptrans_alist);
21021 if (! NILP (tem))
21022 XSETCDR (tem, Qnil);
21023 }
21024 }
21025 }
21026
21027 offset = 0;
21028
21029 if (literal)
21030 {
21031 prec = precision - n;
21032 switch (mode_line_target)
21033 {
21034 case MODE_LINE_NOPROP:
21035 case MODE_LINE_TITLE:
21036 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21037 break;
21038 case MODE_LINE_STRING:
21039 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21040 break;
21041 case MODE_LINE_DISPLAY:
21042 n += display_string (NULL, elt, Qnil, 0, 0, it,
21043 0, prec, 0, STRING_MULTIBYTE (elt));
21044 break;
21045 }
21046
21047 break;
21048 }
21049
21050 /* Handle the non-literal case. */
21051
21052 while ((precision <= 0 || n < precision)
21053 && SREF (elt, offset) != 0
21054 && (mode_line_target != MODE_LINE_DISPLAY
21055 || it->current_x < it->last_visible_x))
21056 {
21057 ptrdiff_t last_offset = offset;
21058
21059 /* Advance to end of string or next format specifier. */
21060 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21061 ;
21062
21063 if (offset - 1 != last_offset)
21064 {
21065 ptrdiff_t nchars, nbytes;
21066
21067 /* Output to end of string or up to '%'. Field width
21068 is length of string. Don't output more than
21069 PRECISION allows us. */
21070 offset--;
21071
21072 prec = c_string_width (SDATA (elt) + last_offset,
21073 offset - last_offset, precision - n,
21074 &nchars, &nbytes);
21075
21076 switch (mode_line_target)
21077 {
21078 case MODE_LINE_NOPROP:
21079 case MODE_LINE_TITLE:
21080 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21081 break;
21082 case MODE_LINE_STRING:
21083 {
21084 ptrdiff_t bytepos = last_offset;
21085 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21086 ptrdiff_t endpos = (precision <= 0
21087 ? string_byte_to_char (elt, offset)
21088 : charpos + nchars);
21089
21090 n += store_mode_line_string (NULL,
21091 Fsubstring (elt, make_number (charpos),
21092 make_number (endpos)),
21093 0, 0, 0, Qnil);
21094 }
21095 break;
21096 case MODE_LINE_DISPLAY:
21097 {
21098 ptrdiff_t bytepos = last_offset;
21099 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21100
21101 if (precision <= 0)
21102 nchars = string_byte_to_char (elt, offset) - charpos;
21103 n += display_string (NULL, elt, Qnil, 0, charpos,
21104 it, 0, nchars, 0,
21105 STRING_MULTIBYTE (elt));
21106 }
21107 break;
21108 }
21109 }
21110 else /* c == '%' */
21111 {
21112 ptrdiff_t percent_position = offset;
21113
21114 /* Get the specified minimum width. Zero means
21115 don't pad. */
21116 field = 0;
21117 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21118 field = field * 10 + c - '0';
21119
21120 /* Don't pad beyond the total padding allowed. */
21121 if (field_width - n > 0 && field > field_width - n)
21122 field = field_width - n;
21123
21124 /* Note that either PRECISION <= 0 or N < PRECISION. */
21125 prec = precision - n;
21126
21127 if (c == 'M')
21128 n += display_mode_element (it, depth, field, prec,
21129 Vglobal_mode_string, props,
21130 risky);
21131 else if (c != 0)
21132 {
21133 bool multibyte;
21134 ptrdiff_t bytepos, charpos;
21135 const char *spec;
21136 Lisp_Object string;
21137
21138 bytepos = percent_position;
21139 charpos = (STRING_MULTIBYTE (elt)
21140 ? string_byte_to_char (elt, bytepos)
21141 : bytepos);
21142 spec = decode_mode_spec (it->w, c, field, &string);
21143 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21144
21145 switch (mode_line_target)
21146 {
21147 case MODE_LINE_NOPROP:
21148 case MODE_LINE_TITLE:
21149 n += store_mode_line_noprop (spec, field, prec);
21150 break;
21151 case MODE_LINE_STRING:
21152 {
21153 Lisp_Object tem = build_string (spec);
21154 props = Ftext_properties_at (make_number (charpos), elt);
21155 /* Should only keep face property in props */
21156 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21157 }
21158 break;
21159 case MODE_LINE_DISPLAY:
21160 {
21161 int nglyphs_before, nwritten;
21162
21163 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21164 nwritten = display_string (spec, string, elt,
21165 charpos, 0, it,
21166 field, prec, 0,
21167 multibyte);
21168
21169 /* Assign to the glyphs written above the
21170 string where the `%x' came from, position
21171 of the `%'. */
21172 if (nwritten > 0)
21173 {
21174 struct glyph *glyph
21175 = (it->glyph_row->glyphs[TEXT_AREA]
21176 + nglyphs_before);
21177 int i;
21178
21179 for (i = 0; i < nwritten; ++i)
21180 {
21181 glyph[i].object = elt;
21182 glyph[i].charpos = charpos;
21183 }
21184
21185 n += nwritten;
21186 }
21187 }
21188 break;
21189 }
21190 }
21191 else /* c == 0 */
21192 break;
21193 }
21194 }
21195 }
21196 break;
21197
21198 case Lisp_Symbol:
21199 /* A symbol: process the value of the symbol recursively
21200 as if it appeared here directly. Avoid error if symbol void.
21201 Special case: if value of symbol is a string, output the string
21202 literally. */
21203 {
21204 register Lisp_Object tem;
21205
21206 /* If the variable is not marked as risky to set
21207 then its contents are risky to use. */
21208 if (NILP (Fget (elt, Qrisky_local_variable)))
21209 risky = 1;
21210
21211 tem = Fboundp (elt);
21212 if (!NILP (tem))
21213 {
21214 tem = Fsymbol_value (elt);
21215 /* If value is a string, output that string literally:
21216 don't check for % within it. */
21217 if (STRINGP (tem))
21218 literal = 1;
21219
21220 if (!EQ (tem, elt))
21221 {
21222 /* Give up right away for nil or t. */
21223 elt = tem;
21224 goto tail_recurse;
21225 }
21226 }
21227 }
21228 break;
21229
21230 case Lisp_Cons:
21231 {
21232 register Lisp_Object car, tem;
21233
21234 /* A cons cell: five distinct cases.
21235 If first element is :eval or :propertize, do something special.
21236 If first element is a string or a cons, process all the elements
21237 and effectively concatenate them.
21238 If first element is a negative number, truncate displaying cdr to
21239 at most that many characters. If positive, pad (with spaces)
21240 to at least that many characters.
21241 If first element is a symbol, process the cadr or caddr recursively
21242 according to whether the symbol's value is non-nil or nil. */
21243 car = XCAR (elt);
21244 if (EQ (car, QCeval))
21245 {
21246 /* An element of the form (:eval FORM) means evaluate FORM
21247 and use the result as mode line elements. */
21248
21249 if (risky)
21250 break;
21251
21252 if (CONSP (XCDR (elt)))
21253 {
21254 Lisp_Object spec;
21255 spec = safe_eval (XCAR (XCDR (elt)));
21256 n += display_mode_element (it, depth, field_width - n,
21257 precision - n, spec, props,
21258 risky);
21259 }
21260 }
21261 else if (EQ (car, QCpropertize))
21262 {
21263 /* An element of the form (:propertize ELT PROPS...)
21264 means display ELT but applying properties PROPS. */
21265
21266 if (risky)
21267 break;
21268
21269 if (CONSP (XCDR (elt)))
21270 n += display_mode_element (it, depth, field_width - n,
21271 precision - n, XCAR (XCDR (elt)),
21272 XCDR (XCDR (elt)), risky);
21273 }
21274 else if (SYMBOLP (car))
21275 {
21276 tem = Fboundp (car);
21277 elt = XCDR (elt);
21278 if (!CONSP (elt))
21279 goto invalid;
21280 /* elt is now the cdr, and we know it is a cons cell.
21281 Use its car if CAR has a non-nil value. */
21282 if (!NILP (tem))
21283 {
21284 tem = Fsymbol_value (car);
21285 if (!NILP (tem))
21286 {
21287 elt = XCAR (elt);
21288 goto tail_recurse;
21289 }
21290 }
21291 /* Symbol's value is nil (or symbol is unbound)
21292 Get the cddr of the original list
21293 and if possible find the caddr and use that. */
21294 elt = XCDR (elt);
21295 if (NILP (elt))
21296 break;
21297 else if (!CONSP (elt))
21298 goto invalid;
21299 elt = XCAR (elt);
21300 goto tail_recurse;
21301 }
21302 else if (INTEGERP (car))
21303 {
21304 register int lim = XINT (car);
21305 elt = XCDR (elt);
21306 if (lim < 0)
21307 {
21308 /* Negative int means reduce maximum width. */
21309 if (precision <= 0)
21310 precision = -lim;
21311 else
21312 precision = min (precision, -lim);
21313 }
21314 else if (lim > 0)
21315 {
21316 /* Padding specified. Don't let it be more than
21317 current maximum. */
21318 if (precision > 0)
21319 lim = min (precision, lim);
21320
21321 /* If that's more padding than already wanted, queue it.
21322 But don't reduce padding already specified even if
21323 that is beyond the current truncation point. */
21324 field_width = max (lim, field_width);
21325 }
21326 goto tail_recurse;
21327 }
21328 else if (STRINGP (car) || CONSP (car))
21329 {
21330 Lisp_Object halftail = elt;
21331 int len = 0;
21332
21333 while (CONSP (elt)
21334 && (precision <= 0 || n < precision))
21335 {
21336 n += display_mode_element (it, depth,
21337 /* Do padding only after the last
21338 element in the list. */
21339 (! CONSP (XCDR (elt))
21340 ? field_width - n
21341 : 0),
21342 precision - n, XCAR (elt),
21343 props, risky);
21344 elt = XCDR (elt);
21345 len++;
21346 if ((len & 1) == 0)
21347 halftail = XCDR (halftail);
21348 /* Check for cycle. */
21349 if (EQ (halftail, elt))
21350 break;
21351 }
21352 }
21353 }
21354 break;
21355
21356 default:
21357 invalid:
21358 elt = build_string ("*invalid*");
21359 goto tail_recurse;
21360 }
21361
21362 /* Pad to FIELD_WIDTH. */
21363 if (field_width > 0 && n < field_width)
21364 {
21365 switch (mode_line_target)
21366 {
21367 case MODE_LINE_NOPROP:
21368 case MODE_LINE_TITLE:
21369 n += store_mode_line_noprop ("", field_width - n, 0);
21370 break;
21371 case MODE_LINE_STRING:
21372 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21373 break;
21374 case MODE_LINE_DISPLAY:
21375 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21376 0, 0, 0);
21377 break;
21378 }
21379 }
21380
21381 return n;
21382 }
21383
21384 /* Store a mode-line string element in mode_line_string_list.
21385
21386 If STRING is non-null, display that C string. Otherwise, the Lisp
21387 string LISP_STRING is displayed.
21388
21389 FIELD_WIDTH is the minimum number of output glyphs to produce.
21390 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21391 with spaces. FIELD_WIDTH <= 0 means don't pad.
21392
21393 PRECISION is the maximum number of characters to output from
21394 STRING. PRECISION <= 0 means don't truncate the string.
21395
21396 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21397 properties to the string.
21398
21399 PROPS are the properties to add to the string.
21400 The mode_line_string_face face property is always added to the string.
21401 */
21402
21403 static int
21404 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21405 int field_width, int precision, Lisp_Object props)
21406 {
21407 ptrdiff_t len;
21408 int n = 0;
21409
21410 if (string != NULL)
21411 {
21412 len = strlen (string);
21413 if (precision > 0 && len > precision)
21414 len = precision;
21415 lisp_string = make_string (string, len);
21416 if (NILP (props))
21417 props = mode_line_string_face_prop;
21418 else if (!NILP (mode_line_string_face))
21419 {
21420 Lisp_Object face = Fplist_get (props, Qface);
21421 props = Fcopy_sequence (props);
21422 if (NILP (face))
21423 face = mode_line_string_face;
21424 else
21425 face = list2 (face, mode_line_string_face);
21426 props = Fplist_put (props, Qface, face);
21427 }
21428 Fadd_text_properties (make_number (0), make_number (len),
21429 props, lisp_string);
21430 }
21431 else
21432 {
21433 len = XFASTINT (Flength (lisp_string));
21434 if (precision > 0 && len > precision)
21435 {
21436 len = precision;
21437 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21438 precision = -1;
21439 }
21440 if (!NILP (mode_line_string_face))
21441 {
21442 Lisp_Object face;
21443 if (NILP (props))
21444 props = Ftext_properties_at (make_number (0), lisp_string);
21445 face = Fplist_get (props, Qface);
21446 if (NILP (face))
21447 face = mode_line_string_face;
21448 else
21449 face = list2 (face, mode_line_string_face);
21450 props = list2 (Qface, face);
21451 if (copy_string)
21452 lisp_string = Fcopy_sequence (lisp_string);
21453 }
21454 if (!NILP (props))
21455 Fadd_text_properties (make_number (0), make_number (len),
21456 props, lisp_string);
21457 }
21458
21459 if (len > 0)
21460 {
21461 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21462 n += len;
21463 }
21464
21465 if (field_width > len)
21466 {
21467 field_width -= len;
21468 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21469 if (!NILP (props))
21470 Fadd_text_properties (make_number (0), make_number (field_width),
21471 props, lisp_string);
21472 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21473 n += field_width;
21474 }
21475
21476 return n;
21477 }
21478
21479
21480 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21481 1, 4, 0,
21482 doc: /* Format a string out of a mode line format specification.
21483 First arg FORMAT specifies the mode line format (see `mode-line-format'
21484 for details) to use.
21485
21486 By default, the format is evaluated for the currently selected window.
21487
21488 Optional second arg FACE specifies the face property to put on all
21489 characters for which no face is specified. The value nil means the
21490 default face. The value t means whatever face the window's mode line
21491 currently uses (either `mode-line' or `mode-line-inactive',
21492 depending on whether the window is the selected window or not).
21493 An integer value means the value string has no text
21494 properties.
21495
21496 Optional third and fourth args WINDOW and BUFFER specify the window
21497 and buffer to use as the context for the formatting (defaults
21498 are the selected window and the WINDOW's buffer). */)
21499 (Lisp_Object format, Lisp_Object face,
21500 Lisp_Object window, Lisp_Object buffer)
21501 {
21502 struct it it;
21503 int len;
21504 struct window *w;
21505 struct buffer *old_buffer = NULL;
21506 int face_id;
21507 int no_props = INTEGERP (face);
21508 ptrdiff_t count = SPECPDL_INDEX ();
21509 Lisp_Object str;
21510 int string_start = 0;
21511
21512 w = decode_any_window (window);
21513 XSETWINDOW (window, w);
21514
21515 if (NILP (buffer))
21516 buffer = w->contents;
21517 CHECK_BUFFER (buffer);
21518
21519 /* Make formatting the modeline a non-op when noninteractive, otherwise
21520 there will be problems later caused by a partially initialized frame. */
21521 if (NILP (format) || noninteractive)
21522 return empty_unibyte_string;
21523
21524 if (no_props)
21525 face = Qnil;
21526
21527 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21528 : EQ (face, Qt) ? (EQ (window, selected_window)
21529 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21530 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21531 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21532 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21533 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21534 : DEFAULT_FACE_ID;
21535
21536 old_buffer = current_buffer;
21537
21538 /* Save things including mode_line_proptrans_alist,
21539 and set that to nil so that we don't alter the outer value. */
21540 record_unwind_protect (unwind_format_mode_line,
21541 format_mode_line_unwind_data
21542 (XFRAME (WINDOW_FRAME (w)),
21543 old_buffer, selected_window, 1));
21544 mode_line_proptrans_alist = Qnil;
21545
21546 Fselect_window (window, Qt);
21547 set_buffer_internal_1 (XBUFFER (buffer));
21548
21549 init_iterator (&it, w, -1, -1, NULL, face_id);
21550
21551 if (no_props)
21552 {
21553 mode_line_target = MODE_LINE_NOPROP;
21554 mode_line_string_face_prop = Qnil;
21555 mode_line_string_list = Qnil;
21556 string_start = MODE_LINE_NOPROP_LEN (0);
21557 }
21558 else
21559 {
21560 mode_line_target = MODE_LINE_STRING;
21561 mode_line_string_list = Qnil;
21562 mode_line_string_face = face;
21563 mode_line_string_face_prop
21564 = NILP (face) ? Qnil : list2 (Qface, face);
21565 }
21566
21567 push_kboard (FRAME_KBOARD (it.f));
21568 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21569 pop_kboard ();
21570
21571 if (no_props)
21572 {
21573 len = MODE_LINE_NOPROP_LEN (string_start);
21574 str = make_string (mode_line_noprop_buf + string_start, len);
21575 }
21576 else
21577 {
21578 mode_line_string_list = Fnreverse (mode_line_string_list);
21579 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21580 empty_unibyte_string);
21581 }
21582
21583 unbind_to (count, Qnil);
21584 return str;
21585 }
21586
21587 /* Write a null-terminated, right justified decimal representation of
21588 the positive integer D to BUF using a minimal field width WIDTH. */
21589
21590 static void
21591 pint2str (register char *buf, register int width, register ptrdiff_t d)
21592 {
21593 register char *p = buf;
21594
21595 if (d <= 0)
21596 *p++ = '0';
21597 else
21598 {
21599 while (d > 0)
21600 {
21601 *p++ = d % 10 + '0';
21602 d /= 10;
21603 }
21604 }
21605
21606 for (width -= (int) (p - buf); width > 0; --width)
21607 *p++ = ' ';
21608 *p-- = '\0';
21609 while (p > buf)
21610 {
21611 d = *buf;
21612 *buf++ = *p;
21613 *p-- = d;
21614 }
21615 }
21616
21617 /* Write a null-terminated, right justified decimal and "human
21618 readable" representation of the nonnegative integer D to BUF using
21619 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21620
21621 static const char power_letter[] =
21622 {
21623 0, /* no letter */
21624 'k', /* kilo */
21625 'M', /* mega */
21626 'G', /* giga */
21627 'T', /* tera */
21628 'P', /* peta */
21629 'E', /* exa */
21630 'Z', /* zetta */
21631 'Y' /* yotta */
21632 };
21633
21634 static void
21635 pint2hrstr (char *buf, int width, ptrdiff_t d)
21636 {
21637 /* We aim to represent the nonnegative integer D as
21638 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21639 ptrdiff_t quotient = d;
21640 int remainder = 0;
21641 /* -1 means: do not use TENTHS. */
21642 int tenths = -1;
21643 int exponent = 0;
21644
21645 /* Length of QUOTIENT.TENTHS as a string. */
21646 int length;
21647
21648 char * psuffix;
21649 char * p;
21650
21651 if (quotient >= 1000)
21652 {
21653 /* Scale to the appropriate EXPONENT. */
21654 do
21655 {
21656 remainder = quotient % 1000;
21657 quotient /= 1000;
21658 exponent++;
21659 }
21660 while (quotient >= 1000);
21661
21662 /* Round to nearest and decide whether to use TENTHS or not. */
21663 if (quotient <= 9)
21664 {
21665 tenths = remainder / 100;
21666 if (remainder % 100 >= 50)
21667 {
21668 if (tenths < 9)
21669 tenths++;
21670 else
21671 {
21672 quotient++;
21673 if (quotient == 10)
21674 tenths = -1;
21675 else
21676 tenths = 0;
21677 }
21678 }
21679 }
21680 else
21681 if (remainder >= 500)
21682 {
21683 if (quotient < 999)
21684 quotient++;
21685 else
21686 {
21687 quotient = 1;
21688 exponent++;
21689 tenths = 0;
21690 }
21691 }
21692 }
21693
21694 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21695 if (tenths == -1 && quotient <= 99)
21696 if (quotient <= 9)
21697 length = 1;
21698 else
21699 length = 2;
21700 else
21701 length = 3;
21702 p = psuffix = buf + max (width, length);
21703
21704 /* Print EXPONENT. */
21705 *psuffix++ = power_letter[exponent];
21706 *psuffix = '\0';
21707
21708 /* Print TENTHS. */
21709 if (tenths >= 0)
21710 {
21711 *--p = '0' + tenths;
21712 *--p = '.';
21713 }
21714
21715 /* Print QUOTIENT. */
21716 do
21717 {
21718 int digit = quotient % 10;
21719 *--p = '0' + digit;
21720 }
21721 while ((quotient /= 10) != 0);
21722
21723 /* Print leading spaces. */
21724 while (buf < p)
21725 *--p = ' ';
21726 }
21727
21728 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21729 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21730 type of CODING_SYSTEM. Return updated pointer into BUF. */
21731
21732 static unsigned char invalid_eol_type[] = "(*invalid*)";
21733
21734 static char *
21735 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21736 {
21737 Lisp_Object val;
21738 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21739 const unsigned char *eol_str;
21740 int eol_str_len;
21741 /* The EOL conversion we are using. */
21742 Lisp_Object eoltype;
21743
21744 val = CODING_SYSTEM_SPEC (coding_system);
21745 eoltype = Qnil;
21746
21747 if (!VECTORP (val)) /* Not yet decided. */
21748 {
21749 *buf++ = multibyte ? '-' : ' ';
21750 if (eol_flag)
21751 eoltype = eol_mnemonic_undecided;
21752 /* Don't mention EOL conversion if it isn't decided. */
21753 }
21754 else
21755 {
21756 Lisp_Object attrs;
21757 Lisp_Object eolvalue;
21758
21759 attrs = AREF (val, 0);
21760 eolvalue = AREF (val, 2);
21761
21762 *buf++ = multibyte
21763 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21764 : ' ';
21765
21766 if (eol_flag)
21767 {
21768 /* The EOL conversion that is normal on this system. */
21769
21770 if (NILP (eolvalue)) /* Not yet decided. */
21771 eoltype = eol_mnemonic_undecided;
21772 else if (VECTORP (eolvalue)) /* Not yet decided. */
21773 eoltype = eol_mnemonic_undecided;
21774 else /* eolvalue is Qunix, Qdos, or Qmac. */
21775 eoltype = (EQ (eolvalue, Qunix)
21776 ? eol_mnemonic_unix
21777 : (EQ (eolvalue, Qdos) == 1
21778 ? eol_mnemonic_dos : eol_mnemonic_mac));
21779 }
21780 }
21781
21782 if (eol_flag)
21783 {
21784 /* Mention the EOL conversion if it is not the usual one. */
21785 if (STRINGP (eoltype))
21786 {
21787 eol_str = SDATA (eoltype);
21788 eol_str_len = SBYTES (eoltype);
21789 }
21790 else if (CHARACTERP (eoltype))
21791 {
21792 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21793 int c = XFASTINT (eoltype);
21794 eol_str_len = CHAR_STRING (c, tmp);
21795 eol_str = tmp;
21796 }
21797 else
21798 {
21799 eol_str = invalid_eol_type;
21800 eol_str_len = sizeof (invalid_eol_type) - 1;
21801 }
21802 memcpy (buf, eol_str, eol_str_len);
21803 buf += eol_str_len;
21804 }
21805
21806 return buf;
21807 }
21808
21809 /* Return a string for the output of a mode line %-spec for window W,
21810 generated by character C. FIELD_WIDTH > 0 means pad the string
21811 returned with spaces to that value. Return a Lisp string in
21812 *STRING if the resulting string is taken from that Lisp string.
21813
21814 Note we operate on the current buffer for most purposes. */
21815
21816 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21817
21818 static const char *
21819 decode_mode_spec (struct window *w, register int c, int field_width,
21820 Lisp_Object *string)
21821 {
21822 Lisp_Object obj;
21823 struct frame *f = XFRAME (WINDOW_FRAME (w));
21824 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21825 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21826 produce strings from numerical values, so limit preposterously
21827 large values of FIELD_WIDTH to avoid overrunning the buffer's
21828 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21829 bytes plus the terminating null. */
21830 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21831 struct buffer *b = current_buffer;
21832
21833 obj = Qnil;
21834 *string = Qnil;
21835
21836 switch (c)
21837 {
21838 case '*':
21839 if (!NILP (BVAR (b, read_only)))
21840 return "%";
21841 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21842 return "*";
21843 return "-";
21844
21845 case '+':
21846 /* This differs from %* only for a modified read-only buffer. */
21847 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21848 return "*";
21849 if (!NILP (BVAR (b, read_only)))
21850 return "%";
21851 return "-";
21852
21853 case '&':
21854 /* This differs from %* in ignoring read-only-ness. */
21855 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21856 return "*";
21857 return "-";
21858
21859 case '%':
21860 return "%";
21861
21862 case '[':
21863 {
21864 int i;
21865 char *p;
21866
21867 if (command_loop_level > 5)
21868 return "[[[... ";
21869 p = decode_mode_spec_buf;
21870 for (i = 0; i < command_loop_level; i++)
21871 *p++ = '[';
21872 *p = 0;
21873 return decode_mode_spec_buf;
21874 }
21875
21876 case ']':
21877 {
21878 int i;
21879 char *p;
21880
21881 if (command_loop_level > 5)
21882 return " ...]]]";
21883 p = decode_mode_spec_buf;
21884 for (i = 0; i < command_loop_level; i++)
21885 *p++ = ']';
21886 *p = 0;
21887 return decode_mode_spec_buf;
21888 }
21889
21890 case '-':
21891 {
21892 register int i;
21893
21894 /* Let lots_of_dashes be a string of infinite length. */
21895 if (mode_line_target == MODE_LINE_NOPROP
21896 || mode_line_target == MODE_LINE_STRING)
21897 return "--";
21898 if (field_width <= 0
21899 || field_width > sizeof (lots_of_dashes))
21900 {
21901 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21902 decode_mode_spec_buf[i] = '-';
21903 decode_mode_spec_buf[i] = '\0';
21904 return decode_mode_spec_buf;
21905 }
21906 else
21907 return lots_of_dashes;
21908 }
21909
21910 case 'b':
21911 obj = BVAR (b, name);
21912 break;
21913
21914 case 'c':
21915 /* %c and %l are ignored in `frame-title-format'.
21916 (In redisplay_internal, the frame title is drawn _before_ the
21917 windows are updated, so the stuff which depends on actual
21918 window contents (such as %l) may fail to render properly, or
21919 even crash emacs.) */
21920 if (mode_line_target == MODE_LINE_TITLE)
21921 return "";
21922 else
21923 {
21924 ptrdiff_t col = current_column ();
21925 w->column_number_displayed = col;
21926 pint2str (decode_mode_spec_buf, width, col);
21927 return decode_mode_spec_buf;
21928 }
21929
21930 case 'e':
21931 #ifndef SYSTEM_MALLOC
21932 {
21933 if (NILP (Vmemory_full))
21934 return "";
21935 else
21936 return "!MEM FULL! ";
21937 }
21938 #else
21939 return "";
21940 #endif
21941
21942 case 'F':
21943 /* %F displays the frame name. */
21944 if (!NILP (f->title))
21945 return SSDATA (f->title);
21946 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21947 return SSDATA (f->name);
21948 return "Emacs";
21949
21950 case 'f':
21951 obj = BVAR (b, filename);
21952 break;
21953
21954 case 'i':
21955 {
21956 ptrdiff_t size = ZV - BEGV;
21957 pint2str (decode_mode_spec_buf, width, size);
21958 return decode_mode_spec_buf;
21959 }
21960
21961 case 'I':
21962 {
21963 ptrdiff_t size = ZV - BEGV;
21964 pint2hrstr (decode_mode_spec_buf, width, size);
21965 return decode_mode_spec_buf;
21966 }
21967
21968 case 'l':
21969 {
21970 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21971 ptrdiff_t topline, nlines, height;
21972 ptrdiff_t junk;
21973
21974 /* %c and %l are ignored in `frame-title-format'. */
21975 if (mode_line_target == MODE_LINE_TITLE)
21976 return "";
21977
21978 startpos = marker_position (w->start);
21979 startpos_byte = marker_byte_position (w->start);
21980 height = WINDOW_TOTAL_LINES (w);
21981
21982 /* If we decided that this buffer isn't suitable for line numbers,
21983 don't forget that too fast. */
21984 if (w->base_line_pos == -1)
21985 goto no_value;
21986
21987 /* If the buffer is very big, don't waste time. */
21988 if (INTEGERP (Vline_number_display_limit)
21989 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21990 {
21991 w->base_line_pos = 0;
21992 w->base_line_number = 0;
21993 goto no_value;
21994 }
21995
21996 if (w->base_line_number > 0
21997 && w->base_line_pos > 0
21998 && w->base_line_pos <= startpos)
21999 {
22000 line = w->base_line_number;
22001 linepos = w->base_line_pos;
22002 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22003 }
22004 else
22005 {
22006 line = 1;
22007 linepos = BUF_BEGV (b);
22008 linepos_byte = BUF_BEGV_BYTE (b);
22009 }
22010
22011 /* Count lines from base line to window start position. */
22012 nlines = display_count_lines (linepos_byte,
22013 startpos_byte,
22014 startpos, &junk);
22015
22016 topline = nlines + line;
22017
22018 /* Determine a new base line, if the old one is too close
22019 or too far away, or if we did not have one.
22020 "Too close" means it's plausible a scroll-down would
22021 go back past it. */
22022 if (startpos == BUF_BEGV (b))
22023 {
22024 w->base_line_number = topline;
22025 w->base_line_pos = BUF_BEGV (b);
22026 }
22027 else if (nlines < height + 25 || nlines > height * 3 + 50
22028 || linepos == BUF_BEGV (b))
22029 {
22030 ptrdiff_t limit = BUF_BEGV (b);
22031 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22032 ptrdiff_t position;
22033 ptrdiff_t distance =
22034 (height * 2 + 30) * line_number_display_limit_width;
22035
22036 if (startpos - distance > limit)
22037 {
22038 limit = startpos - distance;
22039 limit_byte = CHAR_TO_BYTE (limit);
22040 }
22041
22042 nlines = display_count_lines (startpos_byte,
22043 limit_byte,
22044 - (height * 2 + 30),
22045 &position);
22046 /* If we couldn't find the lines we wanted within
22047 line_number_display_limit_width chars per line,
22048 give up on line numbers for this window. */
22049 if (position == limit_byte && limit == startpos - distance)
22050 {
22051 w->base_line_pos = -1;
22052 w->base_line_number = 0;
22053 goto no_value;
22054 }
22055
22056 w->base_line_number = topline - nlines;
22057 w->base_line_pos = BYTE_TO_CHAR (position);
22058 }
22059
22060 /* Now count lines from the start pos to point. */
22061 nlines = display_count_lines (startpos_byte,
22062 PT_BYTE, PT, &junk);
22063
22064 /* Record that we did display the line number. */
22065 line_number_displayed = 1;
22066
22067 /* Make the string to show. */
22068 pint2str (decode_mode_spec_buf, width, topline + nlines);
22069 return decode_mode_spec_buf;
22070 no_value:
22071 {
22072 char* p = decode_mode_spec_buf;
22073 int pad = width - 2;
22074 while (pad-- > 0)
22075 *p++ = ' ';
22076 *p++ = '?';
22077 *p++ = '?';
22078 *p = '\0';
22079 return decode_mode_spec_buf;
22080 }
22081 }
22082 break;
22083
22084 case 'm':
22085 obj = BVAR (b, mode_name);
22086 break;
22087
22088 case 'n':
22089 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22090 return " Narrow";
22091 break;
22092
22093 case 'p':
22094 {
22095 ptrdiff_t pos = marker_position (w->start);
22096 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22097
22098 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22099 {
22100 if (pos <= BUF_BEGV (b))
22101 return "All";
22102 else
22103 return "Bottom";
22104 }
22105 else if (pos <= BUF_BEGV (b))
22106 return "Top";
22107 else
22108 {
22109 if (total > 1000000)
22110 /* Do it differently for a large value, to avoid overflow. */
22111 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22112 else
22113 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22114 /* We can't normally display a 3-digit number,
22115 so get us a 2-digit number that is close. */
22116 if (total == 100)
22117 total = 99;
22118 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22119 return decode_mode_spec_buf;
22120 }
22121 }
22122
22123 /* Display percentage of size above the bottom of the screen. */
22124 case 'P':
22125 {
22126 ptrdiff_t toppos = marker_position (w->start);
22127 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22128 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22129
22130 if (botpos >= BUF_ZV (b))
22131 {
22132 if (toppos <= BUF_BEGV (b))
22133 return "All";
22134 else
22135 return "Bottom";
22136 }
22137 else
22138 {
22139 if (total > 1000000)
22140 /* Do it differently for a large value, to avoid overflow. */
22141 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22142 else
22143 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22144 /* We can't normally display a 3-digit number,
22145 so get us a 2-digit number that is close. */
22146 if (total == 100)
22147 total = 99;
22148 if (toppos <= BUF_BEGV (b))
22149 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22150 else
22151 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22152 return decode_mode_spec_buf;
22153 }
22154 }
22155
22156 case 's':
22157 /* status of process */
22158 obj = Fget_buffer_process (Fcurrent_buffer ());
22159 if (NILP (obj))
22160 return "no process";
22161 #ifndef MSDOS
22162 obj = Fsymbol_name (Fprocess_status (obj));
22163 #endif
22164 break;
22165
22166 case '@':
22167 {
22168 ptrdiff_t count = inhibit_garbage_collection ();
22169 Lisp_Object val = call1 (intern ("file-remote-p"),
22170 BVAR (current_buffer, directory));
22171 unbind_to (count, Qnil);
22172
22173 if (NILP (val))
22174 return "-";
22175 else
22176 return "@";
22177 }
22178
22179 case 'z':
22180 /* coding-system (not including end-of-line format) */
22181 case 'Z':
22182 /* coding-system (including end-of-line type) */
22183 {
22184 int eol_flag = (c == 'Z');
22185 char *p = decode_mode_spec_buf;
22186
22187 if (! FRAME_WINDOW_P (f))
22188 {
22189 /* No need to mention EOL here--the terminal never needs
22190 to do EOL conversion. */
22191 p = decode_mode_spec_coding (CODING_ID_NAME
22192 (FRAME_KEYBOARD_CODING (f)->id),
22193 p, 0);
22194 p = decode_mode_spec_coding (CODING_ID_NAME
22195 (FRAME_TERMINAL_CODING (f)->id),
22196 p, 0);
22197 }
22198 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22199 p, eol_flag);
22200
22201 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22202 #ifdef subprocesses
22203 obj = Fget_buffer_process (Fcurrent_buffer ());
22204 if (PROCESSP (obj))
22205 {
22206 p = decode_mode_spec_coding
22207 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22208 p = decode_mode_spec_coding
22209 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22210 }
22211 #endif /* subprocesses */
22212 #endif /* 0 */
22213 *p = 0;
22214 return decode_mode_spec_buf;
22215 }
22216 }
22217
22218 if (STRINGP (obj))
22219 {
22220 *string = obj;
22221 return SSDATA (obj);
22222 }
22223 else
22224 return "";
22225 }
22226
22227
22228 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22229 means count lines back from START_BYTE. But don't go beyond
22230 LIMIT_BYTE. Return the number of lines thus found (always
22231 nonnegative).
22232
22233 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22234 either the position COUNT lines after/before START_BYTE, if we
22235 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22236 COUNT lines. */
22237
22238 static ptrdiff_t
22239 display_count_lines (ptrdiff_t start_byte,
22240 ptrdiff_t limit_byte, ptrdiff_t count,
22241 ptrdiff_t *byte_pos_ptr)
22242 {
22243 register unsigned char *cursor;
22244 unsigned char *base;
22245
22246 register ptrdiff_t ceiling;
22247 register unsigned char *ceiling_addr;
22248 ptrdiff_t orig_count = count;
22249
22250 /* If we are not in selective display mode,
22251 check only for newlines. */
22252 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22253 && !INTEGERP (BVAR (current_buffer, selective_display)));
22254
22255 if (count > 0)
22256 {
22257 while (start_byte < limit_byte)
22258 {
22259 ceiling = BUFFER_CEILING_OF (start_byte);
22260 ceiling = min (limit_byte - 1, ceiling);
22261 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22262 base = (cursor = BYTE_POS_ADDR (start_byte));
22263
22264 do
22265 {
22266 if (selective_display)
22267 {
22268 while (*cursor != '\n' && *cursor != 015
22269 && ++cursor != ceiling_addr)
22270 continue;
22271 if (cursor == ceiling_addr)
22272 break;
22273 }
22274 else
22275 {
22276 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22277 if (! cursor)
22278 break;
22279 }
22280
22281 cursor++;
22282
22283 if (--count == 0)
22284 {
22285 start_byte += cursor - base;
22286 *byte_pos_ptr = start_byte;
22287 return orig_count;
22288 }
22289 }
22290 while (cursor < ceiling_addr);
22291
22292 start_byte += ceiling_addr - base;
22293 }
22294 }
22295 else
22296 {
22297 while (start_byte > limit_byte)
22298 {
22299 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22300 ceiling = max (limit_byte, ceiling);
22301 ceiling_addr = BYTE_POS_ADDR (ceiling);
22302 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22303 while (1)
22304 {
22305 if (selective_display)
22306 {
22307 while (--cursor >= ceiling_addr
22308 && *cursor != '\n' && *cursor != 015)
22309 continue;
22310 if (cursor < ceiling_addr)
22311 break;
22312 }
22313 else
22314 {
22315 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22316 if (! cursor)
22317 break;
22318 }
22319
22320 if (++count == 0)
22321 {
22322 start_byte += cursor - base + 1;
22323 *byte_pos_ptr = start_byte;
22324 /* When scanning backwards, we should
22325 not count the newline posterior to which we stop. */
22326 return - orig_count - 1;
22327 }
22328 }
22329 start_byte += ceiling_addr - base;
22330 }
22331 }
22332
22333 *byte_pos_ptr = limit_byte;
22334
22335 if (count < 0)
22336 return - orig_count + count;
22337 return orig_count - count;
22338
22339 }
22340
22341
22342 \f
22343 /***********************************************************************
22344 Displaying strings
22345 ***********************************************************************/
22346
22347 /* Display a NUL-terminated string, starting with index START.
22348
22349 If STRING is non-null, display that C string. Otherwise, the Lisp
22350 string LISP_STRING is displayed. There's a case that STRING is
22351 non-null and LISP_STRING is not nil. It means STRING is a string
22352 data of LISP_STRING. In that case, we display LISP_STRING while
22353 ignoring its text properties.
22354
22355 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22356 FACE_STRING. Display STRING or LISP_STRING with the face at
22357 FACE_STRING_POS in FACE_STRING:
22358
22359 Display the string in the environment given by IT, but use the
22360 standard display table, temporarily.
22361
22362 FIELD_WIDTH is the minimum number of output glyphs to produce.
22363 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22364 with spaces. If STRING has more characters, more than FIELD_WIDTH
22365 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22366
22367 PRECISION is the maximum number of characters to output from
22368 STRING. PRECISION < 0 means don't truncate the string.
22369
22370 This is roughly equivalent to printf format specifiers:
22371
22372 FIELD_WIDTH PRECISION PRINTF
22373 ----------------------------------------
22374 -1 -1 %s
22375 -1 10 %.10s
22376 10 -1 %10s
22377 20 10 %20.10s
22378
22379 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22380 display them, and < 0 means obey the current buffer's value of
22381 enable_multibyte_characters.
22382
22383 Value is the number of columns displayed. */
22384
22385 static int
22386 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22387 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22388 int field_width, int precision, int max_x, int multibyte)
22389 {
22390 int hpos_at_start = it->hpos;
22391 int saved_face_id = it->face_id;
22392 struct glyph_row *row = it->glyph_row;
22393 ptrdiff_t it_charpos;
22394
22395 /* Initialize the iterator IT for iteration over STRING beginning
22396 with index START. */
22397 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22398 precision, field_width, multibyte);
22399 if (string && STRINGP (lisp_string))
22400 /* LISP_STRING is the one returned by decode_mode_spec. We should
22401 ignore its text properties. */
22402 it->stop_charpos = it->end_charpos;
22403
22404 /* If displaying STRING, set up the face of the iterator from
22405 FACE_STRING, if that's given. */
22406 if (STRINGP (face_string))
22407 {
22408 ptrdiff_t endptr;
22409 struct face *face;
22410
22411 it->face_id
22412 = face_at_string_position (it->w, face_string, face_string_pos,
22413 0, it->region_beg_charpos,
22414 it->region_end_charpos,
22415 &endptr, it->base_face_id, 0);
22416 face = FACE_FROM_ID (it->f, it->face_id);
22417 it->face_box_p = face->box != FACE_NO_BOX;
22418 }
22419
22420 /* Set max_x to the maximum allowed X position. Don't let it go
22421 beyond the right edge of the window. */
22422 if (max_x <= 0)
22423 max_x = it->last_visible_x;
22424 else
22425 max_x = min (max_x, it->last_visible_x);
22426
22427 /* Skip over display elements that are not visible. because IT->w is
22428 hscrolled. */
22429 if (it->current_x < it->first_visible_x)
22430 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22431 MOVE_TO_POS | MOVE_TO_X);
22432
22433 row->ascent = it->max_ascent;
22434 row->height = it->max_ascent + it->max_descent;
22435 row->phys_ascent = it->max_phys_ascent;
22436 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22437 row->extra_line_spacing = it->max_extra_line_spacing;
22438
22439 if (STRINGP (it->string))
22440 it_charpos = IT_STRING_CHARPOS (*it);
22441 else
22442 it_charpos = IT_CHARPOS (*it);
22443
22444 /* This condition is for the case that we are called with current_x
22445 past last_visible_x. */
22446 while (it->current_x < max_x)
22447 {
22448 int x_before, x, n_glyphs_before, i, nglyphs;
22449
22450 /* Get the next display element. */
22451 if (!get_next_display_element (it))
22452 break;
22453
22454 /* Produce glyphs. */
22455 x_before = it->current_x;
22456 n_glyphs_before = row->used[TEXT_AREA];
22457 PRODUCE_GLYPHS (it);
22458
22459 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22460 i = 0;
22461 x = x_before;
22462 while (i < nglyphs)
22463 {
22464 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22465
22466 if (it->line_wrap != TRUNCATE
22467 && x + glyph->pixel_width > max_x)
22468 {
22469 /* End of continued line or max_x reached. */
22470 if (CHAR_GLYPH_PADDING_P (*glyph))
22471 {
22472 /* A wide character is unbreakable. */
22473 if (row->reversed_p)
22474 unproduce_glyphs (it, row->used[TEXT_AREA]
22475 - n_glyphs_before);
22476 row->used[TEXT_AREA] = n_glyphs_before;
22477 it->current_x = x_before;
22478 }
22479 else
22480 {
22481 if (row->reversed_p)
22482 unproduce_glyphs (it, row->used[TEXT_AREA]
22483 - (n_glyphs_before + i));
22484 row->used[TEXT_AREA] = n_glyphs_before + i;
22485 it->current_x = x;
22486 }
22487 break;
22488 }
22489 else if (x + glyph->pixel_width >= it->first_visible_x)
22490 {
22491 /* Glyph is at least partially visible. */
22492 ++it->hpos;
22493 if (x < it->first_visible_x)
22494 row->x = x - it->first_visible_x;
22495 }
22496 else
22497 {
22498 /* Glyph is off the left margin of the display area.
22499 Should not happen. */
22500 emacs_abort ();
22501 }
22502
22503 row->ascent = max (row->ascent, it->max_ascent);
22504 row->height = max (row->height, it->max_ascent + it->max_descent);
22505 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22506 row->phys_height = max (row->phys_height,
22507 it->max_phys_ascent + it->max_phys_descent);
22508 row->extra_line_spacing = max (row->extra_line_spacing,
22509 it->max_extra_line_spacing);
22510 x += glyph->pixel_width;
22511 ++i;
22512 }
22513
22514 /* Stop if max_x reached. */
22515 if (i < nglyphs)
22516 break;
22517
22518 /* Stop at line ends. */
22519 if (ITERATOR_AT_END_OF_LINE_P (it))
22520 {
22521 it->continuation_lines_width = 0;
22522 break;
22523 }
22524
22525 set_iterator_to_next (it, 1);
22526 if (STRINGP (it->string))
22527 it_charpos = IT_STRING_CHARPOS (*it);
22528 else
22529 it_charpos = IT_CHARPOS (*it);
22530
22531 /* Stop if truncating at the right edge. */
22532 if (it->line_wrap == TRUNCATE
22533 && it->current_x >= it->last_visible_x)
22534 {
22535 /* Add truncation mark, but don't do it if the line is
22536 truncated at a padding space. */
22537 if (it_charpos < it->string_nchars)
22538 {
22539 if (!FRAME_WINDOW_P (it->f))
22540 {
22541 int ii, n;
22542
22543 if (it->current_x > it->last_visible_x)
22544 {
22545 if (!row->reversed_p)
22546 {
22547 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22548 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22549 break;
22550 }
22551 else
22552 {
22553 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22554 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22555 break;
22556 unproduce_glyphs (it, ii + 1);
22557 ii = row->used[TEXT_AREA] - (ii + 1);
22558 }
22559 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22560 {
22561 row->used[TEXT_AREA] = ii;
22562 produce_special_glyphs (it, IT_TRUNCATION);
22563 }
22564 }
22565 produce_special_glyphs (it, IT_TRUNCATION);
22566 }
22567 row->truncated_on_right_p = 1;
22568 }
22569 break;
22570 }
22571 }
22572
22573 /* Maybe insert a truncation at the left. */
22574 if (it->first_visible_x
22575 && it_charpos > 0)
22576 {
22577 if (!FRAME_WINDOW_P (it->f)
22578 || (row->reversed_p
22579 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22580 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22581 insert_left_trunc_glyphs (it);
22582 row->truncated_on_left_p = 1;
22583 }
22584
22585 it->face_id = saved_face_id;
22586
22587 /* Value is number of columns displayed. */
22588 return it->hpos - hpos_at_start;
22589 }
22590
22591
22592 \f
22593 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22594 appears as an element of LIST or as the car of an element of LIST.
22595 If PROPVAL is a list, compare each element against LIST in that
22596 way, and return 1/2 if any element of PROPVAL is found in LIST.
22597 Otherwise return 0. This function cannot quit.
22598 The return value is 2 if the text is invisible but with an ellipsis
22599 and 1 if it's invisible and without an ellipsis. */
22600
22601 int
22602 invisible_p (register Lisp_Object propval, Lisp_Object list)
22603 {
22604 register Lisp_Object tail, proptail;
22605
22606 for (tail = list; CONSP (tail); tail = XCDR (tail))
22607 {
22608 register Lisp_Object tem;
22609 tem = XCAR (tail);
22610 if (EQ (propval, tem))
22611 return 1;
22612 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22613 return NILP (XCDR (tem)) ? 1 : 2;
22614 }
22615
22616 if (CONSP (propval))
22617 {
22618 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22619 {
22620 Lisp_Object propelt;
22621 propelt = XCAR (proptail);
22622 for (tail = list; CONSP (tail); tail = XCDR (tail))
22623 {
22624 register Lisp_Object tem;
22625 tem = XCAR (tail);
22626 if (EQ (propelt, tem))
22627 return 1;
22628 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22629 return NILP (XCDR (tem)) ? 1 : 2;
22630 }
22631 }
22632 }
22633
22634 return 0;
22635 }
22636
22637 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22638 doc: /* Non-nil if the property makes the text invisible.
22639 POS-OR-PROP can be a marker or number, in which case it is taken to be
22640 a position in the current buffer and the value of the `invisible' property
22641 is checked; or it can be some other value, which is then presumed to be the
22642 value of the `invisible' property of the text of interest.
22643 The non-nil value returned can be t for truly invisible text or something
22644 else if the text is replaced by an ellipsis. */)
22645 (Lisp_Object pos_or_prop)
22646 {
22647 Lisp_Object prop
22648 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22649 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22650 : pos_or_prop);
22651 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22652 return (invis == 0 ? Qnil
22653 : invis == 1 ? Qt
22654 : make_number (invis));
22655 }
22656
22657 /* Calculate a width or height in pixels from a specification using
22658 the following elements:
22659
22660 SPEC ::=
22661 NUM - a (fractional) multiple of the default font width/height
22662 (NUM) - specifies exactly NUM pixels
22663 UNIT - a fixed number of pixels, see below.
22664 ELEMENT - size of a display element in pixels, see below.
22665 (NUM . SPEC) - equals NUM * SPEC
22666 (+ SPEC SPEC ...) - add pixel values
22667 (- SPEC SPEC ...) - subtract pixel values
22668 (- SPEC) - negate pixel value
22669
22670 NUM ::=
22671 INT or FLOAT - a number constant
22672 SYMBOL - use symbol's (buffer local) variable binding.
22673
22674 UNIT ::=
22675 in - pixels per inch *)
22676 mm - pixels per 1/1000 meter *)
22677 cm - pixels per 1/100 meter *)
22678 width - width of current font in pixels.
22679 height - height of current font in pixels.
22680
22681 *) using the ratio(s) defined in display-pixels-per-inch.
22682
22683 ELEMENT ::=
22684
22685 left-fringe - left fringe width in pixels
22686 right-fringe - right fringe width in pixels
22687
22688 left-margin - left margin width in pixels
22689 right-margin - right margin width in pixels
22690
22691 scroll-bar - scroll-bar area width in pixels
22692
22693 Examples:
22694
22695 Pixels corresponding to 5 inches:
22696 (5 . in)
22697
22698 Total width of non-text areas on left side of window (if scroll-bar is on left):
22699 '(space :width (+ left-fringe left-margin scroll-bar))
22700
22701 Align to first text column (in header line):
22702 '(space :align-to 0)
22703
22704 Align to middle of text area minus half the width of variable `my-image'
22705 containing a loaded image:
22706 '(space :align-to (0.5 . (- text my-image)))
22707
22708 Width of left margin minus width of 1 character in the default font:
22709 '(space :width (- left-margin 1))
22710
22711 Width of left margin minus width of 2 characters in the current font:
22712 '(space :width (- left-margin (2 . width)))
22713
22714 Center 1 character over left-margin (in header line):
22715 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22716
22717 Different ways to express width of left fringe plus left margin minus one pixel:
22718 '(space :width (- (+ left-fringe left-margin) (1)))
22719 '(space :width (+ left-fringe left-margin (- (1))))
22720 '(space :width (+ left-fringe left-margin (-1)))
22721
22722 */
22723
22724 static int
22725 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22726 struct font *font, int width_p, int *align_to)
22727 {
22728 double pixels;
22729
22730 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22731 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22732
22733 if (NILP (prop))
22734 return OK_PIXELS (0);
22735
22736 eassert (FRAME_LIVE_P (it->f));
22737
22738 if (SYMBOLP (prop))
22739 {
22740 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22741 {
22742 char *unit = SSDATA (SYMBOL_NAME (prop));
22743
22744 if (unit[0] == 'i' && unit[1] == 'n')
22745 pixels = 1.0;
22746 else if (unit[0] == 'm' && unit[1] == 'm')
22747 pixels = 25.4;
22748 else if (unit[0] == 'c' && unit[1] == 'm')
22749 pixels = 2.54;
22750 else
22751 pixels = 0;
22752 if (pixels > 0)
22753 {
22754 double ppi = (width_p ? FRAME_RES_X (it->f)
22755 : FRAME_RES_Y (it->f));
22756
22757 if (ppi > 0)
22758 return OK_PIXELS (ppi / pixels);
22759 return 0;
22760 }
22761 }
22762
22763 #ifdef HAVE_WINDOW_SYSTEM
22764 if (EQ (prop, Qheight))
22765 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22766 if (EQ (prop, Qwidth))
22767 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22768 #else
22769 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22770 return OK_PIXELS (1);
22771 #endif
22772
22773 if (EQ (prop, Qtext))
22774 return OK_PIXELS (width_p
22775 ? window_box_width (it->w, TEXT_AREA)
22776 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22777
22778 if (align_to && *align_to < 0)
22779 {
22780 *res = 0;
22781 if (EQ (prop, Qleft))
22782 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22783 if (EQ (prop, Qright))
22784 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22785 if (EQ (prop, Qcenter))
22786 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22787 + window_box_width (it->w, TEXT_AREA) / 2);
22788 if (EQ (prop, Qleft_fringe))
22789 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22790 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22791 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22792 if (EQ (prop, Qright_fringe))
22793 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22794 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22795 : window_box_right_offset (it->w, TEXT_AREA));
22796 if (EQ (prop, Qleft_margin))
22797 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22798 if (EQ (prop, Qright_margin))
22799 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22800 if (EQ (prop, Qscroll_bar))
22801 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22802 ? 0
22803 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22804 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22805 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22806 : 0)));
22807 }
22808 else
22809 {
22810 if (EQ (prop, Qleft_fringe))
22811 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22812 if (EQ (prop, Qright_fringe))
22813 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22814 if (EQ (prop, Qleft_margin))
22815 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22816 if (EQ (prop, Qright_margin))
22817 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22818 if (EQ (prop, Qscroll_bar))
22819 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22820 }
22821
22822 prop = buffer_local_value_1 (prop, it->w->contents);
22823 if (EQ (prop, Qunbound))
22824 prop = Qnil;
22825 }
22826
22827 if (INTEGERP (prop) || FLOATP (prop))
22828 {
22829 int base_unit = (width_p
22830 ? FRAME_COLUMN_WIDTH (it->f)
22831 : FRAME_LINE_HEIGHT (it->f));
22832 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22833 }
22834
22835 if (CONSP (prop))
22836 {
22837 Lisp_Object car = XCAR (prop);
22838 Lisp_Object cdr = XCDR (prop);
22839
22840 if (SYMBOLP (car))
22841 {
22842 #ifdef HAVE_WINDOW_SYSTEM
22843 if (FRAME_WINDOW_P (it->f)
22844 && valid_image_p (prop))
22845 {
22846 ptrdiff_t id = lookup_image (it->f, prop);
22847 struct image *img = IMAGE_FROM_ID (it->f, id);
22848
22849 return OK_PIXELS (width_p ? img->width : img->height);
22850 }
22851 #endif
22852 if (EQ (car, Qplus) || EQ (car, Qminus))
22853 {
22854 int first = 1;
22855 double px;
22856
22857 pixels = 0;
22858 while (CONSP (cdr))
22859 {
22860 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22861 font, width_p, align_to))
22862 return 0;
22863 if (first)
22864 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22865 else
22866 pixels += px;
22867 cdr = XCDR (cdr);
22868 }
22869 if (EQ (car, Qminus))
22870 pixels = -pixels;
22871 return OK_PIXELS (pixels);
22872 }
22873
22874 car = buffer_local_value_1 (car, it->w->contents);
22875 if (EQ (car, Qunbound))
22876 car = Qnil;
22877 }
22878
22879 if (INTEGERP (car) || FLOATP (car))
22880 {
22881 double fact;
22882 pixels = XFLOATINT (car);
22883 if (NILP (cdr))
22884 return OK_PIXELS (pixels);
22885 if (calc_pixel_width_or_height (&fact, it, cdr,
22886 font, width_p, align_to))
22887 return OK_PIXELS (pixels * fact);
22888 return 0;
22889 }
22890
22891 return 0;
22892 }
22893
22894 return 0;
22895 }
22896
22897 \f
22898 /***********************************************************************
22899 Glyph Display
22900 ***********************************************************************/
22901
22902 #ifdef HAVE_WINDOW_SYSTEM
22903
22904 #ifdef GLYPH_DEBUG
22905
22906 void
22907 dump_glyph_string (struct glyph_string *s)
22908 {
22909 fprintf (stderr, "glyph string\n");
22910 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22911 s->x, s->y, s->width, s->height);
22912 fprintf (stderr, " ybase = %d\n", s->ybase);
22913 fprintf (stderr, " hl = %d\n", s->hl);
22914 fprintf (stderr, " left overhang = %d, right = %d\n",
22915 s->left_overhang, s->right_overhang);
22916 fprintf (stderr, " nchars = %d\n", s->nchars);
22917 fprintf (stderr, " extends to end of line = %d\n",
22918 s->extends_to_end_of_line_p);
22919 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22920 fprintf (stderr, " bg width = %d\n", s->background_width);
22921 }
22922
22923 #endif /* GLYPH_DEBUG */
22924
22925 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22926 of XChar2b structures for S; it can't be allocated in
22927 init_glyph_string because it must be allocated via `alloca'. W
22928 is the window on which S is drawn. ROW and AREA are the glyph row
22929 and area within the row from which S is constructed. START is the
22930 index of the first glyph structure covered by S. HL is a
22931 face-override for drawing S. */
22932
22933 #ifdef HAVE_NTGUI
22934 #define OPTIONAL_HDC(hdc) HDC hdc,
22935 #define DECLARE_HDC(hdc) HDC hdc;
22936 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22937 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22938 #endif
22939
22940 #ifndef OPTIONAL_HDC
22941 #define OPTIONAL_HDC(hdc)
22942 #define DECLARE_HDC(hdc)
22943 #define ALLOCATE_HDC(hdc, f)
22944 #define RELEASE_HDC(hdc, f)
22945 #endif
22946
22947 static void
22948 init_glyph_string (struct glyph_string *s,
22949 OPTIONAL_HDC (hdc)
22950 XChar2b *char2b, struct window *w, struct glyph_row *row,
22951 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22952 {
22953 memset (s, 0, sizeof *s);
22954 s->w = w;
22955 s->f = XFRAME (w->frame);
22956 #ifdef HAVE_NTGUI
22957 s->hdc = hdc;
22958 #endif
22959 s->display = FRAME_X_DISPLAY (s->f);
22960 s->window = FRAME_X_WINDOW (s->f);
22961 s->char2b = char2b;
22962 s->hl = hl;
22963 s->row = row;
22964 s->area = area;
22965 s->first_glyph = row->glyphs[area] + start;
22966 s->height = row->height;
22967 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22968 s->ybase = s->y + row->ascent;
22969 }
22970
22971
22972 /* Append the list of glyph strings with head H and tail T to the list
22973 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22974
22975 static void
22976 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22977 struct glyph_string *h, struct glyph_string *t)
22978 {
22979 if (h)
22980 {
22981 if (*head)
22982 (*tail)->next = h;
22983 else
22984 *head = h;
22985 h->prev = *tail;
22986 *tail = t;
22987 }
22988 }
22989
22990
22991 /* Prepend the list of glyph strings with head H and tail T to the
22992 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22993 result. */
22994
22995 static void
22996 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22997 struct glyph_string *h, struct glyph_string *t)
22998 {
22999 if (h)
23000 {
23001 if (*head)
23002 (*head)->prev = t;
23003 else
23004 *tail = t;
23005 t->next = *head;
23006 *head = h;
23007 }
23008 }
23009
23010
23011 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23012 Set *HEAD and *TAIL to the resulting list. */
23013
23014 static void
23015 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23016 struct glyph_string *s)
23017 {
23018 s->next = s->prev = NULL;
23019 append_glyph_string_lists (head, tail, s, s);
23020 }
23021
23022
23023 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23024 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23025 make sure that X resources for the face returned are allocated.
23026 Value is a pointer to a realized face that is ready for display if
23027 DISPLAY_P is non-zero. */
23028
23029 static struct face *
23030 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23031 XChar2b *char2b, int display_p)
23032 {
23033 struct face *face = FACE_FROM_ID (f, face_id);
23034 unsigned code = 0;
23035
23036 if (face->font)
23037 {
23038 code = face->font->driver->encode_char (face->font, c);
23039
23040 if (code == FONT_INVALID_CODE)
23041 code = 0;
23042 }
23043 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23044
23045 /* Make sure X resources of the face are allocated. */
23046 #ifdef HAVE_X_WINDOWS
23047 if (display_p)
23048 #endif
23049 {
23050 eassert (face != NULL);
23051 PREPARE_FACE_FOR_DISPLAY (f, face);
23052 }
23053
23054 return face;
23055 }
23056
23057
23058 /* Get face and two-byte form of character glyph GLYPH on frame F.
23059 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23060 a pointer to a realized face that is ready for display. */
23061
23062 static struct face *
23063 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23064 XChar2b *char2b, int *two_byte_p)
23065 {
23066 struct face *face;
23067 unsigned code = 0;
23068
23069 eassert (glyph->type == CHAR_GLYPH);
23070 face = FACE_FROM_ID (f, glyph->face_id);
23071
23072 /* Make sure X resources of the face are allocated. */
23073 eassert (face != NULL);
23074 PREPARE_FACE_FOR_DISPLAY (f, face);
23075
23076 if (two_byte_p)
23077 *two_byte_p = 0;
23078
23079 if (face->font)
23080 {
23081 if (CHAR_BYTE8_P (glyph->u.ch))
23082 code = CHAR_TO_BYTE8 (glyph->u.ch);
23083 else
23084 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23085
23086 if (code == FONT_INVALID_CODE)
23087 code = 0;
23088 }
23089
23090 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23091 return face;
23092 }
23093
23094
23095 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23096 Return 1 if FONT has a glyph for C, otherwise return 0. */
23097
23098 static int
23099 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23100 {
23101 unsigned code;
23102
23103 if (CHAR_BYTE8_P (c))
23104 code = CHAR_TO_BYTE8 (c);
23105 else
23106 code = font->driver->encode_char (font, c);
23107
23108 if (code == FONT_INVALID_CODE)
23109 return 0;
23110 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23111 return 1;
23112 }
23113
23114
23115 /* Fill glyph string S with composition components specified by S->cmp.
23116
23117 BASE_FACE is the base face of the composition.
23118 S->cmp_from is the index of the first component for S.
23119
23120 OVERLAPS non-zero means S should draw the foreground only, and use
23121 its physical height for clipping. See also draw_glyphs.
23122
23123 Value is the index of a component not in S. */
23124
23125 static int
23126 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23127 int overlaps)
23128 {
23129 int i;
23130 /* For all glyphs of this composition, starting at the offset
23131 S->cmp_from, until we reach the end of the definition or encounter a
23132 glyph that requires the different face, add it to S. */
23133 struct face *face;
23134
23135 eassert (s);
23136
23137 s->for_overlaps = overlaps;
23138 s->face = NULL;
23139 s->font = NULL;
23140 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23141 {
23142 int c = COMPOSITION_GLYPH (s->cmp, i);
23143
23144 /* TAB in a composition means display glyphs with padding space
23145 on the left or right. */
23146 if (c != '\t')
23147 {
23148 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23149 -1, Qnil);
23150
23151 face = get_char_face_and_encoding (s->f, c, face_id,
23152 s->char2b + i, 1);
23153 if (face)
23154 {
23155 if (! s->face)
23156 {
23157 s->face = face;
23158 s->font = s->face->font;
23159 }
23160 else if (s->face != face)
23161 break;
23162 }
23163 }
23164 ++s->nchars;
23165 }
23166 s->cmp_to = i;
23167
23168 if (s->face == NULL)
23169 {
23170 s->face = base_face->ascii_face;
23171 s->font = s->face->font;
23172 }
23173
23174 /* All glyph strings for the same composition has the same width,
23175 i.e. the width set for the first component of the composition. */
23176 s->width = s->first_glyph->pixel_width;
23177
23178 /* If the specified font could not be loaded, use the frame's
23179 default font, but record the fact that we couldn't load it in
23180 the glyph string so that we can draw rectangles for the
23181 characters of the glyph string. */
23182 if (s->font == NULL)
23183 {
23184 s->font_not_found_p = 1;
23185 s->font = FRAME_FONT (s->f);
23186 }
23187
23188 /* Adjust base line for subscript/superscript text. */
23189 s->ybase += s->first_glyph->voffset;
23190
23191 /* This glyph string must always be drawn with 16-bit functions. */
23192 s->two_byte_p = 1;
23193
23194 return s->cmp_to;
23195 }
23196
23197 static int
23198 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23199 int start, int end, int overlaps)
23200 {
23201 struct glyph *glyph, *last;
23202 Lisp_Object lgstring;
23203 int i;
23204
23205 s->for_overlaps = overlaps;
23206 glyph = s->row->glyphs[s->area] + start;
23207 last = s->row->glyphs[s->area] + end;
23208 s->cmp_id = glyph->u.cmp.id;
23209 s->cmp_from = glyph->slice.cmp.from;
23210 s->cmp_to = glyph->slice.cmp.to + 1;
23211 s->face = FACE_FROM_ID (s->f, face_id);
23212 lgstring = composition_gstring_from_id (s->cmp_id);
23213 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23214 glyph++;
23215 while (glyph < last
23216 && glyph->u.cmp.automatic
23217 && glyph->u.cmp.id == s->cmp_id
23218 && s->cmp_to == glyph->slice.cmp.from)
23219 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23220
23221 for (i = s->cmp_from; i < s->cmp_to; i++)
23222 {
23223 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23224 unsigned code = LGLYPH_CODE (lglyph);
23225
23226 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23227 }
23228 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23229 return glyph - s->row->glyphs[s->area];
23230 }
23231
23232
23233 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23234 See the comment of fill_glyph_string for arguments.
23235 Value is the index of the first glyph not in S. */
23236
23237
23238 static int
23239 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23240 int start, int end, int overlaps)
23241 {
23242 struct glyph *glyph, *last;
23243 int voffset;
23244
23245 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23246 s->for_overlaps = overlaps;
23247 glyph = s->row->glyphs[s->area] + start;
23248 last = s->row->glyphs[s->area] + end;
23249 voffset = glyph->voffset;
23250 s->face = FACE_FROM_ID (s->f, face_id);
23251 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23252 s->nchars = 1;
23253 s->width = glyph->pixel_width;
23254 glyph++;
23255 while (glyph < last
23256 && glyph->type == GLYPHLESS_GLYPH
23257 && glyph->voffset == voffset
23258 && glyph->face_id == face_id)
23259 {
23260 s->nchars++;
23261 s->width += glyph->pixel_width;
23262 glyph++;
23263 }
23264 s->ybase += voffset;
23265 return glyph - s->row->glyphs[s->area];
23266 }
23267
23268
23269 /* Fill glyph string S from a sequence of character glyphs.
23270
23271 FACE_ID is the face id of the string. START is the index of the
23272 first glyph to consider, END is the index of the last + 1.
23273 OVERLAPS non-zero means S should draw the foreground only, and use
23274 its physical height for clipping. See also draw_glyphs.
23275
23276 Value is the index of the first glyph not in S. */
23277
23278 static int
23279 fill_glyph_string (struct glyph_string *s, int face_id,
23280 int start, int end, int overlaps)
23281 {
23282 struct glyph *glyph, *last;
23283 int voffset;
23284 int glyph_not_available_p;
23285
23286 eassert (s->f == XFRAME (s->w->frame));
23287 eassert (s->nchars == 0);
23288 eassert (start >= 0 && end > start);
23289
23290 s->for_overlaps = overlaps;
23291 glyph = s->row->glyphs[s->area] + start;
23292 last = s->row->glyphs[s->area] + end;
23293 voffset = glyph->voffset;
23294 s->padding_p = glyph->padding_p;
23295 glyph_not_available_p = glyph->glyph_not_available_p;
23296
23297 while (glyph < last
23298 && glyph->type == CHAR_GLYPH
23299 && glyph->voffset == voffset
23300 /* Same face id implies same font, nowadays. */
23301 && glyph->face_id == face_id
23302 && glyph->glyph_not_available_p == glyph_not_available_p)
23303 {
23304 int two_byte_p;
23305
23306 s->face = get_glyph_face_and_encoding (s->f, glyph,
23307 s->char2b + s->nchars,
23308 &two_byte_p);
23309 s->two_byte_p = two_byte_p;
23310 ++s->nchars;
23311 eassert (s->nchars <= end - start);
23312 s->width += glyph->pixel_width;
23313 if (glyph++->padding_p != s->padding_p)
23314 break;
23315 }
23316
23317 s->font = s->face->font;
23318
23319 /* If the specified font could not be loaded, use the frame's font,
23320 but record the fact that we couldn't load it in
23321 S->font_not_found_p so that we can draw rectangles for the
23322 characters of the glyph string. */
23323 if (s->font == NULL || glyph_not_available_p)
23324 {
23325 s->font_not_found_p = 1;
23326 s->font = FRAME_FONT (s->f);
23327 }
23328
23329 /* Adjust base line for subscript/superscript text. */
23330 s->ybase += voffset;
23331
23332 eassert (s->face && s->face->gc);
23333 return glyph - s->row->glyphs[s->area];
23334 }
23335
23336
23337 /* Fill glyph string S from image glyph S->first_glyph. */
23338
23339 static void
23340 fill_image_glyph_string (struct glyph_string *s)
23341 {
23342 eassert (s->first_glyph->type == IMAGE_GLYPH);
23343 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23344 eassert (s->img);
23345 s->slice = s->first_glyph->slice.img;
23346 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23347 s->font = s->face->font;
23348 s->width = s->first_glyph->pixel_width;
23349
23350 /* Adjust base line for subscript/superscript text. */
23351 s->ybase += s->first_glyph->voffset;
23352 }
23353
23354
23355 /* Fill glyph string S from a sequence of stretch glyphs.
23356
23357 START is the index of the first glyph to consider,
23358 END is the index of the last + 1.
23359
23360 Value is the index of the first glyph not in S. */
23361
23362 static int
23363 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23364 {
23365 struct glyph *glyph, *last;
23366 int voffset, face_id;
23367
23368 eassert (s->first_glyph->type == STRETCH_GLYPH);
23369
23370 glyph = s->row->glyphs[s->area] + start;
23371 last = s->row->glyphs[s->area] + end;
23372 face_id = glyph->face_id;
23373 s->face = FACE_FROM_ID (s->f, face_id);
23374 s->font = s->face->font;
23375 s->width = glyph->pixel_width;
23376 s->nchars = 1;
23377 voffset = glyph->voffset;
23378
23379 for (++glyph;
23380 (glyph < last
23381 && glyph->type == STRETCH_GLYPH
23382 && glyph->voffset == voffset
23383 && glyph->face_id == face_id);
23384 ++glyph)
23385 s->width += glyph->pixel_width;
23386
23387 /* Adjust base line for subscript/superscript text. */
23388 s->ybase += voffset;
23389
23390 /* The case that face->gc == 0 is handled when drawing the glyph
23391 string by calling PREPARE_FACE_FOR_DISPLAY. */
23392 eassert (s->face);
23393 return glyph - s->row->glyphs[s->area];
23394 }
23395
23396 static struct font_metrics *
23397 get_per_char_metric (struct font *font, XChar2b *char2b)
23398 {
23399 static struct font_metrics metrics;
23400 unsigned code;
23401
23402 if (! font)
23403 return NULL;
23404 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23405 if (code == FONT_INVALID_CODE)
23406 return NULL;
23407 font->driver->text_extents (font, &code, 1, &metrics);
23408 return &metrics;
23409 }
23410
23411 /* EXPORT for RIF:
23412 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23413 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23414 assumed to be zero. */
23415
23416 void
23417 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23418 {
23419 *left = *right = 0;
23420
23421 if (glyph->type == CHAR_GLYPH)
23422 {
23423 struct face *face;
23424 XChar2b char2b;
23425 struct font_metrics *pcm;
23426
23427 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23428 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23429 {
23430 if (pcm->rbearing > pcm->width)
23431 *right = pcm->rbearing - pcm->width;
23432 if (pcm->lbearing < 0)
23433 *left = -pcm->lbearing;
23434 }
23435 }
23436 else if (glyph->type == COMPOSITE_GLYPH)
23437 {
23438 if (! glyph->u.cmp.automatic)
23439 {
23440 struct composition *cmp = composition_table[glyph->u.cmp.id];
23441
23442 if (cmp->rbearing > cmp->pixel_width)
23443 *right = cmp->rbearing - cmp->pixel_width;
23444 if (cmp->lbearing < 0)
23445 *left = - cmp->lbearing;
23446 }
23447 else
23448 {
23449 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23450 struct font_metrics metrics;
23451
23452 composition_gstring_width (gstring, glyph->slice.cmp.from,
23453 glyph->slice.cmp.to + 1, &metrics);
23454 if (metrics.rbearing > metrics.width)
23455 *right = metrics.rbearing - metrics.width;
23456 if (metrics.lbearing < 0)
23457 *left = - metrics.lbearing;
23458 }
23459 }
23460 }
23461
23462
23463 /* Return the index of the first glyph preceding glyph string S that
23464 is overwritten by S because of S's left overhang. Value is -1
23465 if no glyphs are overwritten. */
23466
23467 static int
23468 left_overwritten (struct glyph_string *s)
23469 {
23470 int k;
23471
23472 if (s->left_overhang)
23473 {
23474 int x = 0, i;
23475 struct glyph *glyphs = s->row->glyphs[s->area];
23476 int first = s->first_glyph - glyphs;
23477
23478 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23479 x -= glyphs[i].pixel_width;
23480
23481 k = i + 1;
23482 }
23483 else
23484 k = -1;
23485
23486 return k;
23487 }
23488
23489
23490 /* Return the index of the first glyph preceding glyph string S that
23491 is overwriting S because of its right overhang. Value is -1 if no
23492 glyph in front of S overwrites S. */
23493
23494 static int
23495 left_overwriting (struct glyph_string *s)
23496 {
23497 int i, k, x;
23498 struct glyph *glyphs = s->row->glyphs[s->area];
23499 int first = s->first_glyph - glyphs;
23500
23501 k = -1;
23502 x = 0;
23503 for (i = first - 1; i >= 0; --i)
23504 {
23505 int left, right;
23506 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23507 if (x + right > 0)
23508 k = i;
23509 x -= glyphs[i].pixel_width;
23510 }
23511
23512 return k;
23513 }
23514
23515
23516 /* Return the index of the last glyph following glyph string S that is
23517 overwritten by S because of S's right overhang. Value is -1 if
23518 no such glyph is found. */
23519
23520 static int
23521 right_overwritten (struct glyph_string *s)
23522 {
23523 int k = -1;
23524
23525 if (s->right_overhang)
23526 {
23527 int x = 0, i;
23528 struct glyph *glyphs = s->row->glyphs[s->area];
23529 int first = (s->first_glyph - glyphs
23530 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23531 int end = s->row->used[s->area];
23532
23533 for (i = first; i < end && s->right_overhang > x; ++i)
23534 x += glyphs[i].pixel_width;
23535
23536 k = i;
23537 }
23538
23539 return k;
23540 }
23541
23542
23543 /* Return the index of the last glyph following glyph string S that
23544 overwrites S because of its left overhang. Value is negative
23545 if no such glyph is found. */
23546
23547 static int
23548 right_overwriting (struct glyph_string *s)
23549 {
23550 int i, k, x;
23551 int end = s->row->used[s->area];
23552 struct glyph *glyphs = s->row->glyphs[s->area];
23553 int first = (s->first_glyph - glyphs
23554 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23555
23556 k = -1;
23557 x = 0;
23558 for (i = first; i < end; ++i)
23559 {
23560 int left, right;
23561 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23562 if (x - left < 0)
23563 k = i;
23564 x += glyphs[i].pixel_width;
23565 }
23566
23567 return k;
23568 }
23569
23570
23571 /* Set background width of glyph string S. START is the index of the
23572 first glyph following S. LAST_X is the right-most x-position + 1
23573 in the drawing area. */
23574
23575 static void
23576 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23577 {
23578 /* If the face of this glyph string has to be drawn to the end of
23579 the drawing area, set S->extends_to_end_of_line_p. */
23580
23581 if (start == s->row->used[s->area]
23582 && s->area == TEXT_AREA
23583 && ((s->row->fill_line_p
23584 && (s->hl == DRAW_NORMAL_TEXT
23585 || s->hl == DRAW_IMAGE_RAISED
23586 || s->hl == DRAW_IMAGE_SUNKEN))
23587 || s->hl == DRAW_MOUSE_FACE))
23588 s->extends_to_end_of_line_p = 1;
23589
23590 /* If S extends its face to the end of the line, set its
23591 background_width to the distance to the right edge of the drawing
23592 area. */
23593 if (s->extends_to_end_of_line_p)
23594 s->background_width = last_x - s->x + 1;
23595 else
23596 s->background_width = s->width;
23597 }
23598
23599
23600 /* Compute overhangs and x-positions for glyph string S and its
23601 predecessors, or successors. X is the starting x-position for S.
23602 BACKWARD_P non-zero means process predecessors. */
23603
23604 static void
23605 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23606 {
23607 if (backward_p)
23608 {
23609 while (s)
23610 {
23611 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23612 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23613 x -= s->width;
23614 s->x = x;
23615 s = s->prev;
23616 }
23617 }
23618 else
23619 {
23620 while (s)
23621 {
23622 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23623 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23624 s->x = x;
23625 x += s->width;
23626 s = s->next;
23627 }
23628 }
23629 }
23630
23631
23632
23633 /* The following macros are only called from draw_glyphs below.
23634 They reference the following parameters of that function directly:
23635 `w', `row', `area', and `overlap_p'
23636 as well as the following local variables:
23637 `s', `f', and `hdc' (in W32) */
23638
23639 #ifdef HAVE_NTGUI
23640 /* On W32, silently add local `hdc' variable to argument list of
23641 init_glyph_string. */
23642 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23643 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23644 #else
23645 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23646 init_glyph_string (s, char2b, w, row, area, start, hl)
23647 #endif
23648
23649 /* Add a glyph string for a stretch glyph to the list of strings
23650 between HEAD and TAIL. START is the index of the stretch glyph in
23651 row area AREA of glyph row ROW. END is the index of the last glyph
23652 in that glyph row area. X is the current output position assigned
23653 to the new glyph string constructed. HL overrides that face of the
23654 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23655 is the right-most x-position of the drawing area. */
23656
23657 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23658 and below -- keep them on one line. */
23659 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23660 do \
23661 { \
23662 s = alloca (sizeof *s); \
23663 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23664 START = fill_stretch_glyph_string (s, START, END); \
23665 append_glyph_string (&HEAD, &TAIL, s); \
23666 s->x = (X); \
23667 } \
23668 while (0)
23669
23670
23671 /* Add a glyph string for an image glyph to the list of strings
23672 between HEAD and TAIL. START is the index of the image glyph in
23673 row area AREA of glyph row ROW. END is the index of the last glyph
23674 in that glyph row area. X is the current output position assigned
23675 to the new glyph string constructed. HL overrides that face of the
23676 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23677 is the right-most x-position of the drawing area. */
23678
23679 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23680 do \
23681 { \
23682 s = alloca (sizeof *s); \
23683 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23684 fill_image_glyph_string (s); \
23685 append_glyph_string (&HEAD, &TAIL, s); \
23686 ++START; \
23687 s->x = (X); \
23688 } \
23689 while (0)
23690
23691
23692 /* Add a glyph string for a sequence of character glyphs to the list
23693 of strings between HEAD and TAIL. START is the index of the first
23694 glyph in row area AREA of glyph row ROW that is part of the new
23695 glyph string. END is the index of the last glyph in that glyph row
23696 area. X is the current output position assigned to the new glyph
23697 string constructed. HL overrides that face of the glyph; e.g. it
23698 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23699 right-most x-position of the drawing area. */
23700
23701 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23702 do \
23703 { \
23704 int face_id; \
23705 XChar2b *char2b; \
23706 \
23707 face_id = (row)->glyphs[area][START].face_id; \
23708 \
23709 s = alloca (sizeof *s); \
23710 char2b = alloca ((END - START) * sizeof *char2b); \
23711 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23712 append_glyph_string (&HEAD, &TAIL, s); \
23713 s->x = (X); \
23714 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23715 } \
23716 while (0)
23717
23718
23719 /* Add a glyph string for a composite sequence to the list of strings
23720 between HEAD and TAIL. START is the index of the first glyph in
23721 row area AREA of glyph row ROW that is part of the new glyph
23722 string. END is the index of the last glyph in that glyph row area.
23723 X is the current output position assigned to the new glyph string
23724 constructed. HL overrides that face of the glyph; e.g. it is
23725 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23726 x-position of the drawing area. */
23727
23728 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23729 do { \
23730 int face_id = (row)->glyphs[area][START].face_id; \
23731 struct face *base_face = FACE_FROM_ID (f, face_id); \
23732 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23733 struct composition *cmp = composition_table[cmp_id]; \
23734 XChar2b *char2b; \
23735 struct glyph_string *first_s = NULL; \
23736 int n; \
23737 \
23738 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23739 \
23740 /* Make glyph_strings for each glyph sequence that is drawable by \
23741 the same face, and append them to HEAD/TAIL. */ \
23742 for (n = 0; n < cmp->glyph_len;) \
23743 { \
23744 s = alloca (sizeof *s); \
23745 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23746 append_glyph_string (&(HEAD), &(TAIL), s); \
23747 s->cmp = cmp; \
23748 s->cmp_from = n; \
23749 s->x = (X); \
23750 if (n == 0) \
23751 first_s = s; \
23752 n = fill_composite_glyph_string (s, base_face, overlaps); \
23753 } \
23754 \
23755 ++START; \
23756 s = first_s; \
23757 } while (0)
23758
23759
23760 /* Add a glyph string for a glyph-string sequence to the list of strings
23761 between HEAD and TAIL. */
23762
23763 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23764 do { \
23765 int face_id; \
23766 XChar2b *char2b; \
23767 Lisp_Object gstring; \
23768 \
23769 face_id = (row)->glyphs[area][START].face_id; \
23770 gstring = (composition_gstring_from_id \
23771 ((row)->glyphs[area][START].u.cmp.id)); \
23772 s = alloca (sizeof *s); \
23773 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23774 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23775 append_glyph_string (&(HEAD), &(TAIL), s); \
23776 s->x = (X); \
23777 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23778 } while (0)
23779
23780
23781 /* Add a glyph string for a sequence of glyphless character's glyphs
23782 to the list of strings between HEAD and TAIL. The meanings of
23783 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23784
23785 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23786 do \
23787 { \
23788 int face_id; \
23789 \
23790 face_id = (row)->glyphs[area][START].face_id; \
23791 \
23792 s = alloca (sizeof *s); \
23793 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23794 append_glyph_string (&HEAD, &TAIL, s); \
23795 s->x = (X); \
23796 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23797 overlaps); \
23798 } \
23799 while (0)
23800
23801
23802 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23803 of AREA of glyph row ROW on window W between indices START and END.
23804 HL overrides the face for drawing glyph strings, e.g. it is
23805 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23806 x-positions of the drawing area.
23807
23808 This is an ugly monster macro construct because we must use alloca
23809 to allocate glyph strings (because draw_glyphs can be called
23810 asynchronously). */
23811
23812 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23813 do \
23814 { \
23815 HEAD = TAIL = NULL; \
23816 while (START < END) \
23817 { \
23818 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23819 switch (first_glyph->type) \
23820 { \
23821 case CHAR_GLYPH: \
23822 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23823 HL, X, LAST_X); \
23824 break; \
23825 \
23826 case COMPOSITE_GLYPH: \
23827 if (first_glyph->u.cmp.automatic) \
23828 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23829 HL, X, LAST_X); \
23830 else \
23831 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23832 HL, X, LAST_X); \
23833 break; \
23834 \
23835 case STRETCH_GLYPH: \
23836 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23837 HL, X, LAST_X); \
23838 break; \
23839 \
23840 case IMAGE_GLYPH: \
23841 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23842 HL, X, LAST_X); \
23843 break; \
23844 \
23845 case GLYPHLESS_GLYPH: \
23846 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23847 HL, X, LAST_X); \
23848 break; \
23849 \
23850 default: \
23851 emacs_abort (); \
23852 } \
23853 \
23854 if (s) \
23855 { \
23856 set_glyph_string_background_width (s, START, LAST_X); \
23857 (X) += s->width; \
23858 } \
23859 } \
23860 } while (0)
23861
23862
23863 /* Draw glyphs between START and END in AREA of ROW on window W,
23864 starting at x-position X. X is relative to AREA in W. HL is a
23865 face-override with the following meaning:
23866
23867 DRAW_NORMAL_TEXT draw normally
23868 DRAW_CURSOR draw in cursor face
23869 DRAW_MOUSE_FACE draw in mouse face.
23870 DRAW_INVERSE_VIDEO draw in mode line face
23871 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23872 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23873
23874 If OVERLAPS is non-zero, draw only the foreground of characters and
23875 clip to the physical height of ROW. Non-zero value also defines
23876 the overlapping part to be drawn:
23877
23878 OVERLAPS_PRED overlap with preceding rows
23879 OVERLAPS_SUCC overlap with succeeding rows
23880 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23881 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23882
23883 Value is the x-position reached, relative to AREA of W. */
23884
23885 static int
23886 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23887 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23888 enum draw_glyphs_face hl, int overlaps)
23889 {
23890 struct glyph_string *head, *tail;
23891 struct glyph_string *s;
23892 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23893 int i, j, x_reached, last_x, area_left = 0;
23894 struct frame *f = XFRAME (WINDOW_FRAME (w));
23895 DECLARE_HDC (hdc);
23896
23897 ALLOCATE_HDC (hdc, f);
23898
23899 /* Let's rather be paranoid than getting a SEGV. */
23900 end = min (end, row->used[area]);
23901 start = clip_to_bounds (0, start, end);
23902
23903 /* Translate X to frame coordinates. Set last_x to the right
23904 end of the drawing area. */
23905 if (row->full_width_p)
23906 {
23907 /* X is relative to the left edge of W, without scroll bars
23908 or fringes. */
23909 area_left = WINDOW_LEFT_EDGE_X (w);
23910 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23911 }
23912 else
23913 {
23914 area_left = window_box_left (w, area);
23915 last_x = area_left + window_box_width (w, area);
23916 }
23917 x += area_left;
23918
23919 /* Build a doubly-linked list of glyph_string structures between
23920 head and tail from what we have to draw. Note that the macro
23921 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23922 the reason we use a separate variable `i'. */
23923 i = start;
23924 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23925 if (tail)
23926 x_reached = tail->x + tail->background_width;
23927 else
23928 x_reached = x;
23929
23930 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23931 the row, redraw some glyphs in front or following the glyph
23932 strings built above. */
23933 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23934 {
23935 struct glyph_string *h, *t;
23936 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23937 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23938 int check_mouse_face = 0;
23939 int dummy_x = 0;
23940
23941 /* If mouse highlighting is on, we may need to draw adjacent
23942 glyphs using mouse-face highlighting. */
23943 if (area == TEXT_AREA && row->mouse_face_p
23944 && hlinfo->mouse_face_beg_row >= 0
23945 && hlinfo->mouse_face_end_row >= 0)
23946 {
23947 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23948
23949 if (row_vpos >= hlinfo->mouse_face_beg_row
23950 && row_vpos <= hlinfo->mouse_face_end_row)
23951 {
23952 check_mouse_face = 1;
23953 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23954 ? hlinfo->mouse_face_beg_col : 0;
23955 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23956 ? hlinfo->mouse_face_end_col
23957 : row->used[TEXT_AREA];
23958 }
23959 }
23960
23961 /* Compute overhangs for all glyph strings. */
23962 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23963 for (s = head; s; s = s->next)
23964 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23965
23966 /* Prepend glyph strings for glyphs in front of the first glyph
23967 string that are overwritten because of the first glyph
23968 string's left overhang. The background of all strings
23969 prepended must be drawn because the first glyph string
23970 draws over it. */
23971 i = left_overwritten (head);
23972 if (i >= 0)
23973 {
23974 enum draw_glyphs_face overlap_hl;
23975
23976 /* If this row contains mouse highlighting, attempt to draw
23977 the overlapped glyphs with the correct highlight. This
23978 code fails if the overlap encompasses more than one glyph
23979 and mouse-highlight spans only some of these glyphs.
23980 However, making it work perfectly involves a lot more
23981 code, and I don't know if the pathological case occurs in
23982 practice, so we'll stick to this for now. --- cyd */
23983 if (check_mouse_face
23984 && mouse_beg_col < start && mouse_end_col > i)
23985 overlap_hl = DRAW_MOUSE_FACE;
23986 else
23987 overlap_hl = DRAW_NORMAL_TEXT;
23988
23989 j = i;
23990 BUILD_GLYPH_STRINGS (j, start, h, t,
23991 overlap_hl, dummy_x, last_x);
23992 start = i;
23993 compute_overhangs_and_x (t, head->x, 1);
23994 prepend_glyph_string_lists (&head, &tail, h, t);
23995 clip_head = head;
23996 }
23997
23998 /* Prepend glyph strings for glyphs in front of the first glyph
23999 string that overwrite that glyph string because of their
24000 right overhang. For these strings, only the foreground must
24001 be drawn, because it draws over the glyph string at `head'.
24002 The background must not be drawn because this would overwrite
24003 right overhangs of preceding glyphs for which no glyph
24004 strings exist. */
24005 i = left_overwriting (head);
24006 if (i >= 0)
24007 {
24008 enum draw_glyphs_face overlap_hl;
24009
24010 if (check_mouse_face
24011 && mouse_beg_col < start && mouse_end_col > i)
24012 overlap_hl = DRAW_MOUSE_FACE;
24013 else
24014 overlap_hl = DRAW_NORMAL_TEXT;
24015
24016 clip_head = head;
24017 BUILD_GLYPH_STRINGS (i, start, h, t,
24018 overlap_hl, dummy_x, last_x);
24019 for (s = h; s; s = s->next)
24020 s->background_filled_p = 1;
24021 compute_overhangs_and_x (t, head->x, 1);
24022 prepend_glyph_string_lists (&head, &tail, h, t);
24023 }
24024
24025 /* Append glyphs strings for glyphs following the last glyph
24026 string tail that are overwritten by tail. The background of
24027 these strings has to be drawn because tail's foreground draws
24028 over it. */
24029 i = right_overwritten (tail);
24030 if (i >= 0)
24031 {
24032 enum draw_glyphs_face overlap_hl;
24033
24034 if (check_mouse_face
24035 && mouse_beg_col < i && mouse_end_col > end)
24036 overlap_hl = DRAW_MOUSE_FACE;
24037 else
24038 overlap_hl = DRAW_NORMAL_TEXT;
24039
24040 BUILD_GLYPH_STRINGS (end, i, h, t,
24041 overlap_hl, x, last_x);
24042 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24043 we don't have `end = i;' here. */
24044 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24045 append_glyph_string_lists (&head, &tail, h, t);
24046 clip_tail = tail;
24047 }
24048
24049 /* Append glyph strings for glyphs following the last glyph
24050 string tail that overwrite tail. The foreground of such
24051 glyphs has to be drawn because it writes into the background
24052 of tail. The background must not be drawn because it could
24053 paint over the foreground of following glyphs. */
24054 i = right_overwriting (tail);
24055 if (i >= 0)
24056 {
24057 enum draw_glyphs_face overlap_hl;
24058 if (check_mouse_face
24059 && mouse_beg_col < i && mouse_end_col > end)
24060 overlap_hl = DRAW_MOUSE_FACE;
24061 else
24062 overlap_hl = DRAW_NORMAL_TEXT;
24063
24064 clip_tail = tail;
24065 i++; /* We must include the Ith glyph. */
24066 BUILD_GLYPH_STRINGS (end, i, h, t,
24067 overlap_hl, x, last_x);
24068 for (s = h; s; s = s->next)
24069 s->background_filled_p = 1;
24070 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24071 append_glyph_string_lists (&head, &tail, h, t);
24072 }
24073 if (clip_head || clip_tail)
24074 for (s = head; s; s = s->next)
24075 {
24076 s->clip_head = clip_head;
24077 s->clip_tail = clip_tail;
24078 }
24079 }
24080
24081 /* Draw all strings. */
24082 for (s = head; s; s = s->next)
24083 FRAME_RIF (f)->draw_glyph_string (s);
24084
24085 #ifndef HAVE_NS
24086 /* When focus a sole frame and move horizontally, this sets on_p to 0
24087 causing a failure to erase prev cursor position. */
24088 if (area == TEXT_AREA
24089 && !row->full_width_p
24090 /* When drawing overlapping rows, only the glyph strings'
24091 foreground is drawn, which doesn't erase a cursor
24092 completely. */
24093 && !overlaps)
24094 {
24095 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24096 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24097 : (tail ? tail->x + tail->background_width : x));
24098 x0 -= area_left;
24099 x1 -= area_left;
24100
24101 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24102 row->y, MATRIX_ROW_BOTTOM_Y (row));
24103 }
24104 #endif
24105
24106 /* Value is the x-position up to which drawn, relative to AREA of W.
24107 This doesn't include parts drawn because of overhangs. */
24108 if (row->full_width_p)
24109 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24110 else
24111 x_reached -= area_left;
24112
24113 RELEASE_HDC (hdc, f);
24114
24115 return x_reached;
24116 }
24117
24118 /* Expand row matrix if too narrow. Don't expand if area
24119 is not present. */
24120
24121 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24122 { \
24123 if (!it->f->fonts_changed \
24124 && (it->glyph_row->glyphs[area] \
24125 < it->glyph_row->glyphs[area + 1])) \
24126 { \
24127 it->w->ncols_scale_factor++; \
24128 it->f->fonts_changed = 1; \
24129 } \
24130 }
24131
24132 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24133 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24134
24135 static void
24136 append_glyph (struct it *it)
24137 {
24138 struct glyph *glyph;
24139 enum glyph_row_area area = it->area;
24140
24141 eassert (it->glyph_row);
24142 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24143
24144 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24145 if (glyph < it->glyph_row->glyphs[area + 1])
24146 {
24147 /* If the glyph row is reversed, we need to prepend the glyph
24148 rather than append it. */
24149 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24150 {
24151 struct glyph *g;
24152
24153 /* Make room for the additional glyph. */
24154 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24155 g[1] = *g;
24156 glyph = it->glyph_row->glyphs[area];
24157 }
24158 glyph->charpos = CHARPOS (it->position);
24159 glyph->object = it->object;
24160 if (it->pixel_width > 0)
24161 {
24162 glyph->pixel_width = it->pixel_width;
24163 glyph->padding_p = 0;
24164 }
24165 else
24166 {
24167 /* Assure at least 1-pixel width. Otherwise, cursor can't
24168 be displayed correctly. */
24169 glyph->pixel_width = 1;
24170 glyph->padding_p = 1;
24171 }
24172 glyph->ascent = it->ascent;
24173 glyph->descent = it->descent;
24174 glyph->voffset = it->voffset;
24175 glyph->type = CHAR_GLYPH;
24176 glyph->avoid_cursor_p = it->avoid_cursor_p;
24177 glyph->multibyte_p = it->multibyte_p;
24178 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24179 {
24180 /* In R2L rows, the left and the right box edges need to be
24181 drawn in reverse direction. */
24182 glyph->right_box_line_p = it->start_of_box_run_p;
24183 glyph->left_box_line_p = it->end_of_box_run_p;
24184 }
24185 else
24186 {
24187 glyph->left_box_line_p = it->start_of_box_run_p;
24188 glyph->right_box_line_p = it->end_of_box_run_p;
24189 }
24190 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24191 || it->phys_descent > it->descent);
24192 glyph->glyph_not_available_p = it->glyph_not_available_p;
24193 glyph->face_id = it->face_id;
24194 glyph->u.ch = it->char_to_display;
24195 glyph->slice.img = null_glyph_slice;
24196 glyph->font_type = FONT_TYPE_UNKNOWN;
24197 if (it->bidi_p)
24198 {
24199 glyph->resolved_level = it->bidi_it.resolved_level;
24200 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24201 emacs_abort ();
24202 glyph->bidi_type = it->bidi_it.type;
24203 }
24204 else
24205 {
24206 glyph->resolved_level = 0;
24207 glyph->bidi_type = UNKNOWN_BT;
24208 }
24209 ++it->glyph_row->used[area];
24210 }
24211 else
24212 IT_EXPAND_MATRIX_WIDTH (it, area);
24213 }
24214
24215 /* Store one glyph for the composition IT->cmp_it.id in
24216 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24217 non-null. */
24218
24219 static void
24220 append_composite_glyph (struct it *it)
24221 {
24222 struct glyph *glyph;
24223 enum glyph_row_area area = it->area;
24224
24225 eassert (it->glyph_row);
24226
24227 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24228 if (glyph < it->glyph_row->glyphs[area + 1])
24229 {
24230 /* If the glyph row is reversed, we need to prepend the glyph
24231 rather than append it. */
24232 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24233 {
24234 struct glyph *g;
24235
24236 /* Make room for the new glyph. */
24237 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24238 g[1] = *g;
24239 glyph = it->glyph_row->glyphs[it->area];
24240 }
24241 glyph->charpos = it->cmp_it.charpos;
24242 glyph->object = it->object;
24243 glyph->pixel_width = it->pixel_width;
24244 glyph->ascent = it->ascent;
24245 glyph->descent = it->descent;
24246 glyph->voffset = it->voffset;
24247 glyph->type = COMPOSITE_GLYPH;
24248 if (it->cmp_it.ch < 0)
24249 {
24250 glyph->u.cmp.automatic = 0;
24251 glyph->u.cmp.id = it->cmp_it.id;
24252 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24253 }
24254 else
24255 {
24256 glyph->u.cmp.automatic = 1;
24257 glyph->u.cmp.id = it->cmp_it.id;
24258 glyph->slice.cmp.from = it->cmp_it.from;
24259 glyph->slice.cmp.to = it->cmp_it.to - 1;
24260 }
24261 glyph->avoid_cursor_p = it->avoid_cursor_p;
24262 glyph->multibyte_p = it->multibyte_p;
24263 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24264 {
24265 /* In R2L rows, the left and the right box edges need to be
24266 drawn in reverse direction. */
24267 glyph->right_box_line_p = it->start_of_box_run_p;
24268 glyph->left_box_line_p = it->end_of_box_run_p;
24269 }
24270 else
24271 {
24272 glyph->left_box_line_p = it->start_of_box_run_p;
24273 glyph->right_box_line_p = it->end_of_box_run_p;
24274 }
24275 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24276 || it->phys_descent > it->descent);
24277 glyph->padding_p = 0;
24278 glyph->glyph_not_available_p = 0;
24279 glyph->face_id = it->face_id;
24280 glyph->font_type = FONT_TYPE_UNKNOWN;
24281 if (it->bidi_p)
24282 {
24283 glyph->resolved_level = it->bidi_it.resolved_level;
24284 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24285 emacs_abort ();
24286 glyph->bidi_type = it->bidi_it.type;
24287 }
24288 ++it->glyph_row->used[area];
24289 }
24290 else
24291 IT_EXPAND_MATRIX_WIDTH (it, area);
24292 }
24293
24294
24295 /* Change IT->ascent and IT->height according to the setting of
24296 IT->voffset. */
24297
24298 static void
24299 take_vertical_position_into_account (struct it *it)
24300 {
24301 if (it->voffset)
24302 {
24303 if (it->voffset < 0)
24304 /* Increase the ascent so that we can display the text higher
24305 in the line. */
24306 it->ascent -= it->voffset;
24307 else
24308 /* Increase the descent so that we can display the text lower
24309 in the line. */
24310 it->descent += it->voffset;
24311 }
24312 }
24313
24314
24315 /* Produce glyphs/get display metrics for the image IT is loaded with.
24316 See the description of struct display_iterator in dispextern.h for
24317 an overview of struct display_iterator. */
24318
24319 static void
24320 produce_image_glyph (struct it *it)
24321 {
24322 struct image *img;
24323 struct face *face;
24324 int glyph_ascent, crop;
24325 struct glyph_slice slice;
24326
24327 eassert (it->what == IT_IMAGE);
24328
24329 face = FACE_FROM_ID (it->f, it->face_id);
24330 eassert (face);
24331 /* Make sure X resources of the face is loaded. */
24332 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24333
24334 if (it->image_id < 0)
24335 {
24336 /* Fringe bitmap. */
24337 it->ascent = it->phys_ascent = 0;
24338 it->descent = it->phys_descent = 0;
24339 it->pixel_width = 0;
24340 it->nglyphs = 0;
24341 return;
24342 }
24343
24344 img = IMAGE_FROM_ID (it->f, it->image_id);
24345 eassert (img);
24346 /* Make sure X resources of the image is loaded. */
24347 prepare_image_for_display (it->f, img);
24348
24349 slice.x = slice.y = 0;
24350 slice.width = img->width;
24351 slice.height = img->height;
24352
24353 if (INTEGERP (it->slice.x))
24354 slice.x = XINT (it->slice.x);
24355 else if (FLOATP (it->slice.x))
24356 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24357
24358 if (INTEGERP (it->slice.y))
24359 slice.y = XINT (it->slice.y);
24360 else if (FLOATP (it->slice.y))
24361 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24362
24363 if (INTEGERP (it->slice.width))
24364 slice.width = XINT (it->slice.width);
24365 else if (FLOATP (it->slice.width))
24366 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24367
24368 if (INTEGERP (it->slice.height))
24369 slice.height = XINT (it->slice.height);
24370 else if (FLOATP (it->slice.height))
24371 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24372
24373 if (slice.x >= img->width)
24374 slice.x = img->width;
24375 if (slice.y >= img->height)
24376 slice.y = img->height;
24377 if (slice.x + slice.width >= img->width)
24378 slice.width = img->width - slice.x;
24379 if (slice.y + slice.height > img->height)
24380 slice.height = img->height - slice.y;
24381
24382 if (slice.width == 0 || slice.height == 0)
24383 return;
24384
24385 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24386
24387 it->descent = slice.height - glyph_ascent;
24388 if (slice.y == 0)
24389 it->descent += img->vmargin;
24390 if (slice.y + slice.height == img->height)
24391 it->descent += img->vmargin;
24392 it->phys_descent = it->descent;
24393
24394 it->pixel_width = slice.width;
24395 if (slice.x == 0)
24396 it->pixel_width += img->hmargin;
24397 if (slice.x + slice.width == img->width)
24398 it->pixel_width += img->hmargin;
24399
24400 /* It's quite possible for images to have an ascent greater than
24401 their height, so don't get confused in that case. */
24402 if (it->descent < 0)
24403 it->descent = 0;
24404
24405 it->nglyphs = 1;
24406
24407 if (face->box != FACE_NO_BOX)
24408 {
24409 if (face->box_line_width > 0)
24410 {
24411 if (slice.y == 0)
24412 it->ascent += face->box_line_width;
24413 if (slice.y + slice.height == img->height)
24414 it->descent += face->box_line_width;
24415 }
24416
24417 if (it->start_of_box_run_p && slice.x == 0)
24418 it->pixel_width += eabs (face->box_line_width);
24419 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24420 it->pixel_width += eabs (face->box_line_width);
24421 }
24422
24423 take_vertical_position_into_account (it);
24424
24425 /* Automatically crop wide image glyphs at right edge so we can
24426 draw the cursor on same display row. */
24427 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24428 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24429 {
24430 it->pixel_width -= crop;
24431 slice.width -= crop;
24432 }
24433
24434 if (it->glyph_row)
24435 {
24436 struct glyph *glyph;
24437 enum glyph_row_area area = it->area;
24438
24439 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24440 if (glyph < it->glyph_row->glyphs[area + 1])
24441 {
24442 glyph->charpos = CHARPOS (it->position);
24443 glyph->object = it->object;
24444 glyph->pixel_width = it->pixel_width;
24445 glyph->ascent = glyph_ascent;
24446 glyph->descent = it->descent;
24447 glyph->voffset = it->voffset;
24448 glyph->type = IMAGE_GLYPH;
24449 glyph->avoid_cursor_p = it->avoid_cursor_p;
24450 glyph->multibyte_p = it->multibyte_p;
24451 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24452 {
24453 /* In R2L rows, the left and the right box edges need to be
24454 drawn in reverse direction. */
24455 glyph->right_box_line_p = it->start_of_box_run_p;
24456 glyph->left_box_line_p = it->end_of_box_run_p;
24457 }
24458 else
24459 {
24460 glyph->left_box_line_p = it->start_of_box_run_p;
24461 glyph->right_box_line_p = it->end_of_box_run_p;
24462 }
24463 glyph->overlaps_vertically_p = 0;
24464 glyph->padding_p = 0;
24465 glyph->glyph_not_available_p = 0;
24466 glyph->face_id = it->face_id;
24467 glyph->u.img_id = img->id;
24468 glyph->slice.img = slice;
24469 glyph->font_type = FONT_TYPE_UNKNOWN;
24470 if (it->bidi_p)
24471 {
24472 glyph->resolved_level = it->bidi_it.resolved_level;
24473 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24474 emacs_abort ();
24475 glyph->bidi_type = it->bidi_it.type;
24476 }
24477 ++it->glyph_row->used[area];
24478 }
24479 else
24480 IT_EXPAND_MATRIX_WIDTH (it, area);
24481 }
24482 }
24483
24484
24485 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24486 of the glyph, WIDTH and HEIGHT are the width and height of the
24487 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24488
24489 static void
24490 append_stretch_glyph (struct it *it, Lisp_Object object,
24491 int width, int height, int ascent)
24492 {
24493 struct glyph *glyph;
24494 enum glyph_row_area area = it->area;
24495
24496 eassert (ascent >= 0 && ascent <= height);
24497
24498 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24499 if (glyph < it->glyph_row->glyphs[area + 1])
24500 {
24501 /* If the glyph row is reversed, we need to prepend the glyph
24502 rather than append it. */
24503 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24504 {
24505 struct glyph *g;
24506
24507 /* Make room for the additional glyph. */
24508 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24509 g[1] = *g;
24510 glyph = it->glyph_row->glyphs[area];
24511 }
24512 glyph->charpos = CHARPOS (it->position);
24513 glyph->object = object;
24514 glyph->pixel_width = width;
24515 glyph->ascent = ascent;
24516 glyph->descent = height - ascent;
24517 glyph->voffset = it->voffset;
24518 glyph->type = STRETCH_GLYPH;
24519 glyph->avoid_cursor_p = it->avoid_cursor_p;
24520 glyph->multibyte_p = it->multibyte_p;
24521 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24522 {
24523 /* In R2L rows, the left and the right box edges need to be
24524 drawn in reverse direction. */
24525 glyph->right_box_line_p = it->start_of_box_run_p;
24526 glyph->left_box_line_p = it->end_of_box_run_p;
24527 }
24528 else
24529 {
24530 glyph->left_box_line_p = it->start_of_box_run_p;
24531 glyph->right_box_line_p = it->end_of_box_run_p;
24532 }
24533 glyph->overlaps_vertically_p = 0;
24534 glyph->padding_p = 0;
24535 glyph->glyph_not_available_p = 0;
24536 glyph->face_id = it->face_id;
24537 glyph->u.stretch.ascent = ascent;
24538 glyph->u.stretch.height = height;
24539 glyph->slice.img = null_glyph_slice;
24540 glyph->font_type = FONT_TYPE_UNKNOWN;
24541 if (it->bidi_p)
24542 {
24543 glyph->resolved_level = it->bidi_it.resolved_level;
24544 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24545 emacs_abort ();
24546 glyph->bidi_type = it->bidi_it.type;
24547 }
24548 else
24549 {
24550 glyph->resolved_level = 0;
24551 glyph->bidi_type = UNKNOWN_BT;
24552 }
24553 ++it->glyph_row->used[area];
24554 }
24555 else
24556 IT_EXPAND_MATRIX_WIDTH (it, area);
24557 }
24558
24559 #endif /* HAVE_WINDOW_SYSTEM */
24560
24561 /* Produce a stretch glyph for iterator IT. IT->object is the value
24562 of the glyph property displayed. The value must be a list
24563 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24564 being recognized:
24565
24566 1. `:width WIDTH' specifies that the space should be WIDTH *
24567 canonical char width wide. WIDTH may be an integer or floating
24568 point number.
24569
24570 2. `:relative-width FACTOR' specifies that the width of the stretch
24571 should be computed from the width of the first character having the
24572 `glyph' property, and should be FACTOR times that width.
24573
24574 3. `:align-to HPOS' specifies that the space should be wide enough
24575 to reach HPOS, a value in canonical character units.
24576
24577 Exactly one of the above pairs must be present.
24578
24579 4. `:height HEIGHT' specifies that the height of the stretch produced
24580 should be HEIGHT, measured in canonical character units.
24581
24582 5. `:relative-height FACTOR' specifies that the height of the
24583 stretch should be FACTOR times the height of the characters having
24584 the glyph property.
24585
24586 Either none or exactly one of 4 or 5 must be present.
24587
24588 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24589 of the stretch should be used for the ascent of the stretch.
24590 ASCENT must be in the range 0 <= ASCENT <= 100. */
24591
24592 void
24593 produce_stretch_glyph (struct it *it)
24594 {
24595 /* (space :width WIDTH :height HEIGHT ...) */
24596 Lisp_Object prop, plist;
24597 int width = 0, height = 0, align_to = -1;
24598 int zero_width_ok_p = 0;
24599 double tem;
24600 struct font *font = NULL;
24601
24602 #ifdef HAVE_WINDOW_SYSTEM
24603 int ascent = 0;
24604 int zero_height_ok_p = 0;
24605
24606 if (FRAME_WINDOW_P (it->f))
24607 {
24608 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24609 font = face->font ? face->font : FRAME_FONT (it->f);
24610 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24611 }
24612 #endif
24613
24614 /* List should start with `space'. */
24615 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24616 plist = XCDR (it->object);
24617
24618 /* Compute the width of the stretch. */
24619 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24620 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24621 {
24622 /* Absolute width `:width WIDTH' specified and valid. */
24623 zero_width_ok_p = 1;
24624 width = (int)tem;
24625 }
24626 #ifdef HAVE_WINDOW_SYSTEM
24627 else if (FRAME_WINDOW_P (it->f)
24628 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24629 {
24630 /* Relative width `:relative-width FACTOR' specified and valid.
24631 Compute the width of the characters having the `glyph'
24632 property. */
24633 struct it it2;
24634 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24635
24636 it2 = *it;
24637 if (it->multibyte_p)
24638 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24639 else
24640 {
24641 it2.c = it2.char_to_display = *p, it2.len = 1;
24642 if (! ASCII_CHAR_P (it2.c))
24643 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24644 }
24645
24646 it2.glyph_row = NULL;
24647 it2.what = IT_CHARACTER;
24648 x_produce_glyphs (&it2);
24649 width = NUMVAL (prop) * it2.pixel_width;
24650 }
24651 #endif /* HAVE_WINDOW_SYSTEM */
24652 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24653 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24654 {
24655 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24656 align_to = (align_to < 0
24657 ? 0
24658 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24659 else if (align_to < 0)
24660 align_to = window_box_left_offset (it->w, TEXT_AREA);
24661 width = max (0, (int)tem + align_to - it->current_x);
24662 zero_width_ok_p = 1;
24663 }
24664 else
24665 /* Nothing specified -> width defaults to canonical char width. */
24666 width = FRAME_COLUMN_WIDTH (it->f);
24667
24668 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24669 width = 1;
24670
24671 #ifdef HAVE_WINDOW_SYSTEM
24672 /* Compute height. */
24673 if (FRAME_WINDOW_P (it->f))
24674 {
24675 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24676 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24677 {
24678 height = (int)tem;
24679 zero_height_ok_p = 1;
24680 }
24681 else if (prop = Fplist_get (plist, QCrelative_height),
24682 NUMVAL (prop) > 0)
24683 height = FONT_HEIGHT (font) * NUMVAL (prop);
24684 else
24685 height = FONT_HEIGHT (font);
24686
24687 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24688 height = 1;
24689
24690 /* Compute percentage of height used for ascent. If
24691 `:ascent ASCENT' is present and valid, use that. Otherwise,
24692 derive the ascent from the font in use. */
24693 if (prop = Fplist_get (plist, QCascent),
24694 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24695 ascent = height * NUMVAL (prop) / 100.0;
24696 else if (!NILP (prop)
24697 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24698 ascent = min (max (0, (int)tem), height);
24699 else
24700 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24701 }
24702 else
24703 #endif /* HAVE_WINDOW_SYSTEM */
24704 height = 1;
24705
24706 if (width > 0 && it->line_wrap != TRUNCATE
24707 && it->current_x + width > it->last_visible_x)
24708 {
24709 width = it->last_visible_x - it->current_x;
24710 #ifdef HAVE_WINDOW_SYSTEM
24711 /* Subtract one more pixel from the stretch width, but only on
24712 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24713 width -= FRAME_WINDOW_P (it->f);
24714 #endif
24715 }
24716
24717 if (width > 0 && height > 0 && it->glyph_row)
24718 {
24719 Lisp_Object o_object = it->object;
24720 Lisp_Object object = it->stack[it->sp - 1].string;
24721 int n = width;
24722
24723 if (!STRINGP (object))
24724 object = it->w->contents;
24725 #ifdef HAVE_WINDOW_SYSTEM
24726 if (FRAME_WINDOW_P (it->f))
24727 append_stretch_glyph (it, object, width, height, ascent);
24728 else
24729 #endif
24730 {
24731 it->object = object;
24732 it->char_to_display = ' ';
24733 it->pixel_width = it->len = 1;
24734 while (n--)
24735 tty_append_glyph (it);
24736 it->object = o_object;
24737 }
24738 }
24739
24740 it->pixel_width = width;
24741 #ifdef HAVE_WINDOW_SYSTEM
24742 if (FRAME_WINDOW_P (it->f))
24743 {
24744 it->ascent = it->phys_ascent = ascent;
24745 it->descent = it->phys_descent = height - it->ascent;
24746 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24747 take_vertical_position_into_account (it);
24748 }
24749 else
24750 #endif
24751 it->nglyphs = width;
24752 }
24753
24754 /* Get information about special display element WHAT in an
24755 environment described by IT. WHAT is one of IT_TRUNCATION or
24756 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24757 non-null glyph_row member. This function ensures that fields like
24758 face_id, c, len of IT are left untouched. */
24759
24760 static void
24761 produce_special_glyphs (struct it *it, enum display_element_type what)
24762 {
24763 struct it temp_it;
24764 Lisp_Object gc;
24765 GLYPH glyph;
24766
24767 temp_it = *it;
24768 temp_it.object = make_number (0);
24769 memset (&temp_it.current, 0, sizeof temp_it.current);
24770
24771 if (what == IT_CONTINUATION)
24772 {
24773 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24774 if (it->bidi_it.paragraph_dir == R2L)
24775 SET_GLYPH_FROM_CHAR (glyph, '/');
24776 else
24777 SET_GLYPH_FROM_CHAR (glyph, '\\');
24778 if (it->dp
24779 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24780 {
24781 /* FIXME: Should we mirror GC for R2L lines? */
24782 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24783 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24784 }
24785 }
24786 else if (what == IT_TRUNCATION)
24787 {
24788 /* Truncation glyph. */
24789 SET_GLYPH_FROM_CHAR (glyph, '$');
24790 if (it->dp
24791 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24792 {
24793 /* FIXME: Should we mirror GC for R2L lines? */
24794 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24795 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24796 }
24797 }
24798 else
24799 emacs_abort ();
24800
24801 #ifdef HAVE_WINDOW_SYSTEM
24802 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24803 is turned off, we precede the truncation/continuation glyphs by a
24804 stretch glyph whose width is computed such that these special
24805 glyphs are aligned at the window margin, even when very different
24806 fonts are used in different glyph rows. */
24807 if (FRAME_WINDOW_P (temp_it.f)
24808 /* init_iterator calls this with it->glyph_row == NULL, and it
24809 wants only the pixel width of the truncation/continuation
24810 glyphs. */
24811 && temp_it.glyph_row
24812 /* insert_left_trunc_glyphs calls us at the beginning of the
24813 row, and it has its own calculation of the stretch glyph
24814 width. */
24815 && temp_it.glyph_row->used[TEXT_AREA] > 0
24816 && (temp_it.glyph_row->reversed_p
24817 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24818 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24819 {
24820 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24821
24822 if (stretch_width > 0)
24823 {
24824 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24825 struct font *font =
24826 face->font ? face->font : FRAME_FONT (temp_it.f);
24827 int stretch_ascent =
24828 (((temp_it.ascent + temp_it.descent)
24829 * FONT_BASE (font)) / FONT_HEIGHT (font));
24830
24831 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24832 temp_it.ascent + temp_it.descent,
24833 stretch_ascent);
24834 }
24835 }
24836 #endif
24837
24838 temp_it.dp = NULL;
24839 temp_it.what = IT_CHARACTER;
24840 temp_it.len = 1;
24841 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24842 temp_it.face_id = GLYPH_FACE (glyph);
24843 temp_it.len = CHAR_BYTES (temp_it.c);
24844
24845 PRODUCE_GLYPHS (&temp_it);
24846 it->pixel_width = temp_it.pixel_width;
24847 it->nglyphs = temp_it.pixel_width;
24848 }
24849
24850 #ifdef HAVE_WINDOW_SYSTEM
24851
24852 /* Calculate line-height and line-spacing properties.
24853 An integer value specifies explicit pixel value.
24854 A float value specifies relative value to current face height.
24855 A cons (float . face-name) specifies relative value to
24856 height of specified face font.
24857
24858 Returns height in pixels, or nil. */
24859
24860
24861 static Lisp_Object
24862 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24863 int boff, int override)
24864 {
24865 Lisp_Object face_name = Qnil;
24866 int ascent, descent, height;
24867
24868 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24869 return val;
24870
24871 if (CONSP (val))
24872 {
24873 face_name = XCAR (val);
24874 val = XCDR (val);
24875 if (!NUMBERP (val))
24876 val = make_number (1);
24877 if (NILP (face_name))
24878 {
24879 height = it->ascent + it->descent;
24880 goto scale;
24881 }
24882 }
24883
24884 if (NILP (face_name))
24885 {
24886 font = FRAME_FONT (it->f);
24887 boff = FRAME_BASELINE_OFFSET (it->f);
24888 }
24889 else if (EQ (face_name, Qt))
24890 {
24891 override = 0;
24892 }
24893 else
24894 {
24895 int face_id;
24896 struct face *face;
24897
24898 face_id = lookup_named_face (it->f, face_name, 0);
24899 if (face_id < 0)
24900 return make_number (-1);
24901
24902 face = FACE_FROM_ID (it->f, face_id);
24903 font = face->font;
24904 if (font == NULL)
24905 return make_number (-1);
24906 boff = font->baseline_offset;
24907 if (font->vertical_centering)
24908 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24909 }
24910
24911 ascent = FONT_BASE (font) + boff;
24912 descent = FONT_DESCENT (font) - boff;
24913
24914 if (override)
24915 {
24916 it->override_ascent = ascent;
24917 it->override_descent = descent;
24918 it->override_boff = boff;
24919 }
24920
24921 height = ascent + descent;
24922
24923 scale:
24924 if (FLOATP (val))
24925 height = (int)(XFLOAT_DATA (val) * height);
24926 else if (INTEGERP (val))
24927 height *= XINT (val);
24928
24929 return make_number (height);
24930 }
24931
24932
24933 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24934 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24935 and only if this is for a character for which no font was found.
24936
24937 If the display method (it->glyphless_method) is
24938 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24939 length of the acronym or the hexadecimal string, UPPER_XOFF and
24940 UPPER_YOFF are pixel offsets for the upper part of the string,
24941 LOWER_XOFF and LOWER_YOFF are for the lower part.
24942
24943 For the other display methods, LEN through LOWER_YOFF are zero. */
24944
24945 static void
24946 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24947 short upper_xoff, short upper_yoff,
24948 short lower_xoff, short lower_yoff)
24949 {
24950 struct glyph *glyph;
24951 enum glyph_row_area area = it->area;
24952
24953 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24954 if (glyph < it->glyph_row->glyphs[area + 1])
24955 {
24956 /* If the glyph row is reversed, we need to prepend the glyph
24957 rather than append it. */
24958 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24959 {
24960 struct glyph *g;
24961
24962 /* Make room for the additional glyph. */
24963 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24964 g[1] = *g;
24965 glyph = it->glyph_row->glyphs[area];
24966 }
24967 glyph->charpos = CHARPOS (it->position);
24968 glyph->object = it->object;
24969 glyph->pixel_width = it->pixel_width;
24970 glyph->ascent = it->ascent;
24971 glyph->descent = it->descent;
24972 glyph->voffset = it->voffset;
24973 glyph->type = GLYPHLESS_GLYPH;
24974 glyph->u.glyphless.method = it->glyphless_method;
24975 glyph->u.glyphless.for_no_font = for_no_font;
24976 glyph->u.glyphless.len = len;
24977 glyph->u.glyphless.ch = it->c;
24978 glyph->slice.glyphless.upper_xoff = upper_xoff;
24979 glyph->slice.glyphless.upper_yoff = upper_yoff;
24980 glyph->slice.glyphless.lower_xoff = lower_xoff;
24981 glyph->slice.glyphless.lower_yoff = lower_yoff;
24982 glyph->avoid_cursor_p = it->avoid_cursor_p;
24983 glyph->multibyte_p = it->multibyte_p;
24984 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24985 {
24986 /* In R2L rows, the left and the right box edges need to be
24987 drawn in reverse direction. */
24988 glyph->right_box_line_p = it->start_of_box_run_p;
24989 glyph->left_box_line_p = it->end_of_box_run_p;
24990 }
24991 else
24992 {
24993 glyph->left_box_line_p = it->start_of_box_run_p;
24994 glyph->right_box_line_p = it->end_of_box_run_p;
24995 }
24996 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24997 || it->phys_descent > it->descent);
24998 glyph->padding_p = 0;
24999 glyph->glyph_not_available_p = 0;
25000 glyph->face_id = face_id;
25001 glyph->font_type = FONT_TYPE_UNKNOWN;
25002 if (it->bidi_p)
25003 {
25004 glyph->resolved_level = it->bidi_it.resolved_level;
25005 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25006 emacs_abort ();
25007 glyph->bidi_type = it->bidi_it.type;
25008 }
25009 ++it->glyph_row->used[area];
25010 }
25011 else
25012 IT_EXPAND_MATRIX_WIDTH (it, area);
25013 }
25014
25015
25016 /* Produce a glyph for a glyphless character for iterator IT.
25017 IT->glyphless_method specifies which method to use for displaying
25018 the character. See the description of enum
25019 glyphless_display_method in dispextern.h for the detail.
25020
25021 FOR_NO_FONT is nonzero if and only if this is for a character for
25022 which no font was found. ACRONYM, if non-nil, is an acronym string
25023 for the character. */
25024
25025 static void
25026 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25027 {
25028 int face_id;
25029 struct face *face;
25030 struct font *font;
25031 int base_width, base_height, width, height;
25032 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25033 int len;
25034
25035 /* Get the metrics of the base font. We always refer to the current
25036 ASCII face. */
25037 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25038 font = face->font ? face->font : FRAME_FONT (it->f);
25039 it->ascent = FONT_BASE (font) + font->baseline_offset;
25040 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25041 base_height = it->ascent + it->descent;
25042 base_width = font->average_width;
25043
25044 face_id = merge_glyphless_glyph_face (it);
25045
25046 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25047 {
25048 it->pixel_width = THIN_SPACE_WIDTH;
25049 len = 0;
25050 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25051 }
25052 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25053 {
25054 width = CHAR_WIDTH (it->c);
25055 if (width == 0)
25056 width = 1;
25057 else if (width > 4)
25058 width = 4;
25059 it->pixel_width = base_width * width;
25060 len = 0;
25061 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25062 }
25063 else
25064 {
25065 char buf[7];
25066 const char *str;
25067 unsigned int code[6];
25068 int upper_len;
25069 int ascent, descent;
25070 struct font_metrics metrics_upper, metrics_lower;
25071
25072 face = FACE_FROM_ID (it->f, face_id);
25073 font = face->font ? face->font : FRAME_FONT (it->f);
25074 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25075
25076 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25077 {
25078 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25079 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25080 if (CONSP (acronym))
25081 acronym = XCAR (acronym);
25082 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25083 }
25084 else
25085 {
25086 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25087 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25088 str = buf;
25089 }
25090 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25091 code[len] = font->driver->encode_char (font, str[len]);
25092 upper_len = (len + 1) / 2;
25093 font->driver->text_extents (font, code, upper_len,
25094 &metrics_upper);
25095 font->driver->text_extents (font, code + upper_len, len - upper_len,
25096 &metrics_lower);
25097
25098
25099
25100 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25101 width = max (metrics_upper.width, metrics_lower.width) + 4;
25102 upper_xoff = upper_yoff = 2; /* the typical case */
25103 if (base_width >= width)
25104 {
25105 /* Align the upper to the left, the lower to the right. */
25106 it->pixel_width = base_width;
25107 lower_xoff = base_width - 2 - metrics_lower.width;
25108 }
25109 else
25110 {
25111 /* Center the shorter one. */
25112 it->pixel_width = width;
25113 if (metrics_upper.width >= metrics_lower.width)
25114 lower_xoff = (width - metrics_lower.width) / 2;
25115 else
25116 {
25117 /* FIXME: This code doesn't look right. It formerly was
25118 missing the "lower_xoff = 0;", which couldn't have
25119 been right since it left lower_xoff uninitialized. */
25120 lower_xoff = 0;
25121 upper_xoff = (width - metrics_upper.width) / 2;
25122 }
25123 }
25124
25125 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25126 top, bottom, and between upper and lower strings. */
25127 height = (metrics_upper.ascent + metrics_upper.descent
25128 + metrics_lower.ascent + metrics_lower.descent) + 5;
25129 /* Center vertically.
25130 H:base_height, D:base_descent
25131 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25132
25133 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25134 descent = D - H/2 + h/2;
25135 lower_yoff = descent - 2 - ld;
25136 upper_yoff = lower_yoff - la - 1 - ud; */
25137 ascent = - (it->descent - (base_height + height + 1) / 2);
25138 descent = it->descent - (base_height - height) / 2;
25139 lower_yoff = descent - 2 - metrics_lower.descent;
25140 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25141 - metrics_upper.descent);
25142 /* Don't make the height shorter than the base height. */
25143 if (height > base_height)
25144 {
25145 it->ascent = ascent;
25146 it->descent = descent;
25147 }
25148 }
25149
25150 it->phys_ascent = it->ascent;
25151 it->phys_descent = it->descent;
25152 if (it->glyph_row)
25153 append_glyphless_glyph (it, face_id, for_no_font, len,
25154 upper_xoff, upper_yoff,
25155 lower_xoff, lower_yoff);
25156 it->nglyphs = 1;
25157 take_vertical_position_into_account (it);
25158 }
25159
25160
25161 /* RIF:
25162 Produce glyphs/get display metrics for the display element IT is
25163 loaded with. See the description of struct it in dispextern.h
25164 for an overview of struct it. */
25165
25166 void
25167 x_produce_glyphs (struct it *it)
25168 {
25169 int extra_line_spacing = it->extra_line_spacing;
25170
25171 it->glyph_not_available_p = 0;
25172
25173 if (it->what == IT_CHARACTER)
25174 {
25175 XChar2b char2b;
25176 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25177 struct font *font = face->font;
25178 struct font_metrics *pcm = NULL;
25179 int boff; /* baseline offset */
25180
25181 if (font == NULL)
25182 {
25183 /* When no suitable font is found, display this character by
25184 the method specified in the first extra slot of
25185 Vglyphless_char_display. */
25186 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25187
25188 eassert (it->what == IT_GLYPHLESS);
25189 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25190 goto done;
25191 }
25192
25193 boff = font->baseline_offset;
25194 if (font->vertical_centering)
25195 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25196
25197 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25198 {
25199 int stretched_p;
25200
25201 it->nglyphs = 1;
25202
25203 if (it->override_ascent >= 0)
25204 {
25205 it->ascent = it->override_ascent;
25206 it->descent = it->override_descent;
25207 boff = it->override_boff;
25208 }
25209 else
25210 {
25211 it->ascent = FONT_BASE (font) + boff;
25212 it->descent = FONT_DESCENT (font) - boff;
25213 }
25214
25215 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25216 {
25217 pcm = get_per_char_metric (font, &char2b);
25218 if (pcm->width == 0
25219 && pcm->rbearing == 0 && pcm->lbearing == 0)
25220 pcm = NULL;
25221 }
25222
25223 if (pcm)
25224 {
25225 it->phys_ascent = pcm->ascent + boff;
25226 it->phys_descent = pcm->descent - boff;
25227 it->pixel_width = pcm->width;
25228 }
25229 else
25230 {
25231 it->glyph_not_available_p = 1;
25232 it->phys_ascent = it->ascent;
25233 it->phys_descent = it->descent;
25234 it->pixel_width = font->space_width;
25235 }
25236
25237 if (it->constrain_row_ascent_descent_p)
25238 {
25239 if (it->descent > it->max_descent)
25240 {
25241 it->ascent += it->descent - it->max_descent;
25242 it->descent = it->max_descent;
25243 }
25244 if (it->ascent > it->max_ascent)
25245 {
25246 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25247 it->ascent = it->max_ascent;
25248 }
25249 it->phys_ascent = min (it->phys_ascent, it->ascent);
25250 it->phys_descent = min (it->phys_descent, it->descent);
25251 extra_line_spacing = 0;
25252 }
25253
25254 /* If this is a space inside a region of text with
25255 `space-width' property, change its width. */
25256 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25257 if (stretched_p)
25258 it->pixel_width *= XFLOATINT (it->space_width);
25259
25260 /* If face has a box, add the box thickness to the character
25261 height. If character has a box line to the left and/or
25262 right, add the box line width to the character's width. */
25263 if (face->box != FACE_NO_BOX)
25264 {
25265 int thick = face->box_line_width;
25266
25267 if (thick > 0)
25268 {
25269 it->ascent += thick;
25270 it->descent += thick;
25271 }
25272 else
25273 thick = -thick;
25274
25275 if (it->start_of_box_run_p)
25276 it->pixel_width += thick;
25277 if (it->end_of_box_run_p)
25278 it->pixel_width += thick;
25279 }
25280
25281 /* If face has an overline, add the height of the overline
25282 (1 pixel) and a 1 pixel margin to the character height. */
25283 if (face->overline_p)
25284 it->ascent += overline_margin;
25285
25286 if (it->constrain_row_ascent_descent_p)
25287 {
25288 if (it->ascent > it->max_ascent)
25289 it->ascent = it->max_ascent;
25290 if (it->descent > it->max_descent)
25291 it->descent = it->max_descent;
25292 }
25293
25294 take_vertical_position_into_account (it);
25295
25296 /* If we have to actually produce glyphs, do it. */
25297 if (it->glyph_row)
25298 {
25299 if (stretched_p)
25300 {
25301 /* Translate a space with a `space-width' property
25302 into a stretch glyph. */
25303 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25304 / FONT_HEIGHT (font));
25305 append_stretch_glyph (it, it->object, it->pixel_width,
25306 it->ascent + it->descent, ascent);
25307 }
25308 else
25309 append_glyph (it);
25310
25311 /* If characters with lbearing or rbearing are displayed
25312 in this line, record that fact in a flag of the
25313 glyph row. This is used to optimize X output code. */
25314 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25315 it->glyph_row->contains_overlapping_glyphs_p = 1;
25316 }
25317 if (! stretched_p && it->pixel_width == 0)
25318 /* We assure that all visible glyphs have at least 1-pixel
25319 width. */
25320 it->pixel_width = 1;
25321 }
25322 else if (it->char_to_display == '\n')
25323 {
25324 /* A newline has no width, but we need the height of the
25325 line. But if previous part of the line sets a height,
25326 don't increase that height */
25327
25328 Lisp_Object height;
25329 Lisp_Object total_height = Qnil;
25330
25331 it->override_ascent = -1;
25332 it->pixel_width = 0;
25333 it->nglyphs = 0;
25334
25335 height = get_it_property (it, Qline_height);
25336 /* Split (line-height total-height) list */
25337 if (CONSP (height)
25338 && CONSP (XCDR (height))
25339 && NILP (XCDR (XCDR (height))))
25340 {
25341 total_height = XCAR (XCDR (height));
25342 height = XCAR (height);
25343 }
25344 height = calc_line_height_property (it, height, font, boff, 1);
25345
25346 if (it->override_ascent >= 0)
25347 {
25348 it->ascent = it->override_ascent;
25349 it->descent = it->override_descent;
25350 boff = it->override_boff;
25351 }
25352 else
25353 {
25354 it->ascent = FONT_BASE (font) + boff;
25355 it->descent = FONT_DESCENT (font) - boff;
25356 }
25357
25358 if (EQ (height, Qt))
25359 {
25360 if (it->descent > it->max_descent)
25361 {
25362 it->ascent += it->descent - it->max_descent;
25363 it->descent = it->max_descent;
25364 }
25365 if (it->ascent > it->max_ascent)
25366 {
25367 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25368 it->ascent = it->max_ascent;
25369 }
25370 it->phys_ascent = min (it->phys_ascent, it->ascent);
25371 it->phys_descent = min (it->phys_descent, it->descent);
25372 it->constrain_row_ascent_descent_p = 1;
25373 extra_line_spacing = 0;
25374 }
25375 else
25376 {
25377 Lisp_Object spacing;
25378
25379 it->phys_ascent = it->ascent;
25380 it->phys_descent = it->descent;
25381
25382 if ((it->max_ascent > 0 || it->max_descent > 0)
25383 && face->box != FACE_NO_BOX
25384 && face->box_line_width > 0)
25385 {
25386 it->ascent += face->box_line_width;
25387 it->descent += face->box_line_width;
25388 }
25389 if (!NILP (height)
25390 && XINT (height) > it->ascent + it->descent)
25391 it->ascent = XINT (height) - it->descent;
25392
25393 if (!NILP (total_height))
25394 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25395 else
25396 {
25397 spacing = get_it_property (it, Qline_spacing);
25398 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25399 }
25400 if (INTEGERP (spacing))
25401 {
25402 extra_line_spacing = XINT (spacing);
25403 if (!NILP (total_height))
25404 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25405 }
25406 }
25407 }
25408 else /* i.e. (it->char_to_display == '\t') */
25409 {
25410 if (font->space_width > 0)
25411 {
25412 int tab_width = it->tab_width * font->space_width;
25413 int x = it->current_x + it->continuation_lines_width;
25414 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25415
25416 /* If the distance from the current position to the next tab
25417 stop is less than a space character width, use the
25418 tab stop after that. */
25419 if (next_tab_x - x < font->space_width)
25420 next_tab_x += tab_width;
25421
25422 it->pixel_width = next_tab_x - x;
25423 it->nglyphs = 1;
25424 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25425 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25426
25427 if (it->glyph_row)
25428 {
25429 append_stretch_glyph (it, it->object, it->pixel_width,
25430 it->ascent + it->descent, it->ascent);
25431 }
25432 }
25433 else
25434 {
25435 it->pixel_width = 0;
25436 it->nglyphs = 1;
25437 }
25438 }
25439 }
25440 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25441 {
25442 /* A static composition.
25443
25444 Note: A composition is represented as one glyph in the
25445 glyph matrix. There are no padding glyphs.
25446
25447 Important note: pixel_width, ascent, and descent are the
25448 values of what is drawn by draw_glyphs (i.e. the values of
25449 the overall glyphs composed). */
25450 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25451 int boff; /* baseline offset */
25452 struct composition *cmp = composition_table[it->cmp_it.id];
25453 int glyph_len = cmp->glyph_len;
25454 struct font *font = face->font;
25455
25456 it->nglyphs = 1;
25457
25458 /* If we have not yet calculated pixel size data of glyphs of
25459 the composition for the current face font, calculate them
25460 now. Theoretically, we have to check all fonts for the
25461 glyphs, but that requires much time and memory space. So,
25462 here we check only the font of the first glyph. This may
25463 lead to incorrect display, but it's very rare, and C-l
25464 (recenter-top-bottom) can correct the display anyway. */
25465 if (! cmp->font || cmp->font != font)
25466 {
25467 /* Ascent and descent of the font of the first character
25468 of this composition (adjusted by baseline offset).
25469 Ascent and descent of overall glyphs should not be less
25470 than these, respectively. */
25471 int font_ascent, font_descent, font_height;
25472 /* Bounding box of the overall glyphs. */
25473 int leftmost, rightmost, lowest, highest;
25474 int lbearing, rbearing;
25475 int i, width, ascent, descent;
25476 int left_padded = 0, right_padded = 0;
25477 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25478 XChar2b char2b;
25479 struct font_metrics *pcm;
25480 int font_not_found_p;
25481 ptrdiff_t pos;
25482
25483 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25484 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25485 break;
25486 if (glyph_len < cmp->glyph_len)
25487 right_padded = 1;
25488 for (i = 0; i < glyph_len; i++)
25489 {
25490 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25491 break;
25492 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25493 }
25494 if (i > 0)
25495 left_padded = 1;
25496
25497 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25498 : IT_CHARPOS (*it));
25499 /* If no suitable font is found, use the default font. */
25500 font_not_found_p = font == NULL;
25501 if (font_not_found_p)
25502 {
25503 face = face->ascii_face;
25504 font = face->font;
25505 }
25506 boff = font->baseline_offset;
25507 if (font->vertical_centering)
25508 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25509 font_ascent = FONT_BASE (font) + boff;
25510 font_descent = FONT_DESCENT (font) - boff;
25511 font_height = FONT_HEIGHT (font);
25512
25513 cmp->font = font;
25514
25515 pcm = NULL;
25516 if (! font_not_found_p)
25517 {
25518 get_char_face_and_encoding (it->f, c, it->face_id,
25519 &char2b, 0);
25520 pcm = get_per_char_metric (font, &char2b);
25521 }
25522
25523 /* Initialize the bounding box. */
25524 if (pcm)
25525 {
25526 width = cmp->glyph_len > 0 ? pcm->width : 0;
25527 ascent = pcm->ascent;
25528 descent = pcm->descent;
25529 lbearing = pcm->lbearing;
25530 rbearing = pcm->rbearing;
25531 }
25532 else
25533 {
25534 width = cmp->glyph_len > 0 ? font->space_width : 0;
25535 ascent = FONT_BASE (font);
25536 descent = FONT_DESCENT (font);
25537 lbearing = 0;
25538 rbearing = width;
25539 }
25540
25541 rightmost = width;
25542 leftmost = 0;
25543 lowest = - descent + boff;
25544 highest = ascent + boff;
25545
25546 if (! font_not_found_p
25547 && font->default_ascent
25548 && CHAR_TABLE_P (Vuse_default_ascent)
25549 && !NILP (Faref (Vuse_default_ascent,
25550 make_number (it->char_to_display))))
25551 highest = font->default_ascent + boff;
25552
25553 /* Draw the first glyph at the normal position. It may be
25554 shifted to right later if some other glyphs are drawn
25555 at the left. */
25556 cmp->offsets[i * 2] = 0;
25557 cmp->offsets[i * 2 + 1] = boff;
25558 cmp->lbearing = lbearing;
25559 cmp->rbearing = rbearing;
25560
25561 /* Set cmp->offsets for the remaining glyphs. */
25562 for (i++; i < glyph_len; i++)
25563 {
25564 int left, right, btm, top;
25565 int ch = COMPOSITION_GLYPH (cmp, i);
25566 int face_id;
25567 struct face *this_face;
25568
25569 if (ch == '\t')
25570 ch = ' ';
25571 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25572 this_face = FACE_FROM_ID (it->f, face_id);
25573 font = this_face->font;
25574
25575 if (font == NULL)
25576 pcm = NULL;
25577 else
25578 {
25579 get_char_face_and_encoding (it->f, ch, face_id,
25580 &char2b, 0);
25581 pcm = get_per_char_metric (font, &char2b);
25582 }
25583 if (! pcm)
25584 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25585 else
25586 {
25587 width = pcm->width;
25588 ascent = pcm->ascent;
25589 descent = pcm->descent;
25590 lbearing = pcm->lbearing;
25591 rbearing = pcm->rbearing;
25592 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25593 {
25594 /* Relative composition with or without
25595 alternate chars. */
25596 left = (leftmost + rightmost - width) / 2;
25597 btm = - descent + boff;
25598 if (font->relative_compose
25599 && (! CHAR_TABLE_P (Vignore_relative_composition)
25600 || NILP (Faref (Vignore_relative_composition,
25601 make_number (ch)))))
25602 {
25603
25604 if (- descent >= font->relative_compose)
25605 /* One extra pixel between two glyphs. */
25606 btm = highest + 1;
25607 else if (ascent <= 0)
25608 /* One extra pixel between two glyphs. */
25609 btm = lowest - 1 - ascent - descent;
25610 }
25611 }
25612 else
25613 {
25614 /* A composition rule is specified by an integer
25615 value that encodes global and new reference
25616 points (GREF and NREF). GREF and NREF are
25617 specified by numbers as below:
25618
25619 0---1---2 -- ascent
25620 | |
25621 | |
25622 | |
25623 9--10--11 -- center
25624 | |
25625 ---3---4---5--- baseline
25626 | |
25627 6---7---8 -- descent
25628 */
25629 int rule = COMPOSITION_RULE (cmp, i);
25630 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25631
25632 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25633 grefx = gref % 3, nrefx = nref % 3;
25634 grefy = gref / 3, nrefy = nref / 3;
25635 if (xoff)
25636 xoff = font_height * (xoff - 128) / 256;
25637 if (yoff)
25638 yoff = font_height * (yoff - 128) / 256;
25639
25640 left = (leftmost
25641 + grefx * (rightmost - leftmost) / 2
25642 - nrefx * width / 2
25643 + xoff);
25644
25645 btm = ((grefy == 0 ? highest
25646 : grefy == 1 ? 0
25647 : grefy == 2 ? lowest
25648 : (highest + lowest) / 2)
25649 - (nrefy == 0 ? ascent + descent
25650 : nrefy == 1 ? descent - boff
25651 : nrefy == 2 ? 0
25652 : (ascent + descent) / 2)
25653 + yoff);
25654 }
25655
25656 cmp->offsets[i * 2] = left;
25657 cmp->offsets[i * 2 + 1] = btm + descent;
25658
25659 /* Update the bounding box of the overall glyphs. */
25660 if (width > 0)
25661 {
25662 right = left + width;
25663 if (left < leftmost)
25664 leftmost = left;
25665 if (right > rightmost)
25666 rightmost = right;
25667 }
25668 top = btm + descent + ascent;
25669 if (top > highest)
25670 highest = top;
25671 if (btm < lowest)
25672 lowest = btm;
25673
25674 if (cmp->lbearing > left + lbearing)
25675 cmp->lbearing = left + lbearing;
25676 if (cmp->rbearing < left + rbearing)
25677 cmp->rbearing = left + rbearing;
25678 }
25679 }
25680
25681 /* If there are glyphs whose x-offsets are negative,
25682 shift all glyphs to the right and make all x-offsets
25683 non-negative. */
25684 if (leftmost < 0)
25685 {
25686 for (i = 0; i < cmp->glyph_len; i++)
25687 cmp->offsets[i * 2] -= leftmost;
25688 rightmost -= leftmost;
25689 cmp->lbearing -= leftmost;
25690 cmp->rbearing -= leftmost;
25691 }
25692
25693 if (left_padded && cmp->lbearing < 0)
25694 {
25695 for (i = 0; i < cmp->glyph_len; i++)
25696 cmp->offsets[i * 2] -= cmp->lbearing;
25697 rightmost -= cmp->lbearing;
25698 cmp->rbearing -= cmp->lbearing;
25699 cmp->lbearing = 0;
25700 }
25701 if (right_padded && rightmost < cmp->rbearing)
25702 {
25703 rightmost = cmp->rbearing;
25704 }
25705
25706 cmp->pixel_width = rightmost;
25707 cmp->ascent = highest;
25708 cmp->descent = - lowest;
25709 if (cmp->ascent < font_ascent)
25710 cmp->ascent = font_ascent;
25711 if (cmp->descent < font_descent)
25712 cmp->descent = font_descent;
25713 }
25714
25715 if (it->glyph_row
25716 && (cmp->lbearing < 0
25717 || cmp->rbearing > cmp->pixel_width))
25718 it->glyph_row->contains_overlapping_glyphs_p = 1;
25719
25720 it->pixel_width = cmp->pixel_width;
25721 it->ascent = it->phys_ascent = cmp->ascent;
25722 it->descent = it->phys_descent = cmp->descent;
25723 if (face->box != FACE_NO_BOX)
25724 {
25725 int thick = face->box_line_width;
25726
25727 if (thick > 0)
25728 {
25729 it->ascent += thick;
25730 it->descent += thick;
25731 }
25732 else
25733 thick = - thick;
25734
25735 if (it->start_of_box_run_p)
25736 it->pixel_width += thick;
25737 if (it->end_of_box_run_p)
25738 it->pixel_width += thick;
25739 }
25740
25741 /* If face has an overline, add the height of the overline
25742 (1 pixel) and a 1 pixel margin to the character height. */
25743 if (face->overline_p)
25744 it->ascent += overline_margin;
25745
25746 take_vertical_position_into_account (it);
25747 if (it->ascent < 0)
25748 it->ascent = 0;
25749 if (it->descent < 0)
25750 it->descent = 0;
25751
25752 if (it->glyph_row && cmp->glyph_len > 0)
25753 append_composite_glyph (it);
25754 }
25755 else if (it->what == IT_COMPOSITION)
25756 {
25757 /* A dynamic (automatic) composition. */
25758 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25759 Lisp_Object gstring;
25760 struct font_metrics metrics;
25761
25762 it->nglyphs = 1;
25763
25764 gstring = composition_gstring_from_id (it->cmp_it.id);
25765 it->pixel_width
25766 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25767 &metrics);
25768 if (it->glyph_row
25769 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25770 it->glyph_row->contains_overlapping_glyphs_p = 1;
25771 it->ascent = it->phys_ascent = metrics.ascent;
25772 it->descent = it->phys_descent = metrics.descent;
25773 if (face->box != FACE_NO_BOX)
25774 {
25775 int thick = face->box_line_width;
25776
25777 if (thick > 0)
25778 {
25779 it->ascent += thick;
25780 it->descent += thick;
25781 }
25782 else
25783 thick = - thick;
25784
25785 if (it->start_of_box_run_p)
25786 it->pixel_width += thick;
25787 if (it->end_of_box_run_p)
25788 it->pixel_width += thick;
25789 }
25790 /* If face has an overline, add the height of the overline
25791 (1 pixel) and a 1 pixel margin to the character height. */
25792 if (face->overline_p)
25793 it->ascent += overline_margin;
25794 take_vertical_position_into_account (it);
25795 if (it->ascent < 0)
25796 it->ascent = 0;
25797 if (it->descent < 0)
25798 it->descent = 0;
25799
25800 if (it->glyph_row)
25801 append_composite_glyph (it);
25802 }
25803 else if (it->what == IT_GLYPHLESS)
25804 produce_glyphless_glyph (it, 0, Qnil);
25805 else if (it->what == IT_IMAGE)
25806 produce_image_glyph (it);
25807 else if (it->what == IT_STRETCH)
25808 produce_stretch_glyph (it);
25809
25810 done:
25811 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25812 because this isn't true for images with `:ascent 100'. */
25813 eassert (it->ascent >= 0 && it->descent >= 0);
25814 if (it->area == TEXT_AREA)
25815 it->current_x += it->pixel_width;
25816
25817 if (extra_line_spacing > 0)
25818 {
25819 it->descent += extra_line_spacing;
25820 if (extra_line_spacing > it->max_extra_line_spacing)
25821 it->max_extra_line_spacing = extra_line_spacing;
25822 }
25823
25824 it->max_ascent = max (it->max_ascent, it->ascent);
25825 it->max_descent = max (it->max_descent, it->descent);
25826 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25827 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25828 }
25829
25830 /* EXPORT for RIF:
25831 Output LEN glyphs starting at START at the nominal cursor position.
25832 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25833 being updated, and UPDATED_AREA is the area of that row being updated. */
25834
25835 void
25836 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25837 struct glyph *start, enum glyph_row_area updated_area, int len)
25838 {
25839 int x, hpos, chpos = w->phys_cursor.hpos;
25840
25841 eassert (updated_row);
25842 /* When the window is hscrolled, cursor hpos can legitimately be out
25843 of bounds, but we draw the cursor at the corresponding window
25844 margin in that case. */
25845 if (!updated_row->reversed_p && chpos < 0)
25846 chpos = 0;
25847 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25848 chpos = updated_row->used[TEXT_AREA] - 1;
25849
25850 block_input ();
25851
25852 /* Write glyphs. */
25853
25854 hpos = start - updated_row->glyphs[updated_area];
25855 x = draw_glyphs (w, w->output_cursor.x,
25856 updated_row, updated_area,
25857 hpos, hpos + len,
25858 DRAW_NORMAL_TEXT, 0);
25859
25860 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25861 if (updated_area == TEXT_AREA
25862 && w->phys_cursor_on_p
25863 && w->phys_cursor.vpos == w->output_cursor.vpos
25864 && chpos >= hpos
25865 && chpos < hpos + len)
25866 w->phys_cursor_on_p = 0;
25867
25868 unblock_input ();
25869
25870 /* Advance the output cursor. */
25871 w->output_cursor.hpos += len;
25872 w->output_cursor.x = x;
25873 }
25874
25875
25876 /* EXPORT for RIF:
25877 Insert LEN glyphs from START at the nominal cursor position. */
25878
25879 void
25880 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25881 struct glyph *start, enum glyph_row_area updated_area, int len)
25882 {
25883 struct frame *f;
25884 int line_height, shift_by_width, shifted_region_width;
25885 struct glyph_row *row;
25886 struct glyph *glyph;
25887 int frame_x, frame_y;
25888 ptrdiff_t hpos;
25889
25890 eassert (updated_row);
25891 block_input ();
25892 f = XFRAME (WINDOW_FRAME (w));
25893
25894 /* Get the height of the line we are in. */
25895 row = updated_row;
25896 line_height = row->height;
25897
25898 /* Get the width of the glyphs to insert. */
25899 shift_by_width = 0;
25900 for (glyph = start; glyph < start + len; ++glyph)
25901 shift_by_width += glyph->pixel_width;
25902
25903 /* Get the width of the region to shift right. */
25904 shifted_region_width = (window_box_width (w, updated_area)
25905 - w->output_cursor.x
25906 - shift_by_width);
25907
25908 /* Shift right. */
25909 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25910 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25911
25912 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25913 line_height, shift_by_width);
25914
25915 /* Write the glyphs. */
25916 hpos = start - row->glyphs[updated_area];
25917 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25918 hpos, hpos + len,
25919 DRAW_NORMAL_TEXT, 0);
25920
25921 /* Advance the output cursor. */
25922 w->output_cursor.hpos += len;
25923 w->output_cursor.x += shift_by_width;
25924 unblock_input ();
25925 }
25926
25927
25928 /* EXPORT for RIF:
25929 Erase the current text line from the nominal cursor position
25930 (inclusive) to pixel column TO_X (exclusive). The idea is that
25931 everything from TO_X onward is already erased.
25932
25933 TO_X is a pixel position relative to UPDATED_AREA of currently
25934 updated window W. TO_X == -1 means clear to the end of this area. */
25935
25936 void
25937 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25938 enum glyph_row_area updated_area, int to_x)
25939 {
25940 struct frame *f;
25941 int max_x, min_y, max_y;
25942 int from_x, from_y, to_y;
25943
25944 eassert (updated_row);
25945 f = XFRAME (w->frame);
25946
25947 if (updated_row->full_width_p)
25948 max_x = WINDOW_TOTAL_WIDTH (w);
25949 else
25950 max_x = window_box_width (w, updated_area);
25951 max_y = window_text_bottom_y (w);
25952
25953 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25954 of window. For TO_X > 0, truncate to end of drawing area. */
25955 if (to_x == 0)
25956 return;
25957 else if (to_x < 0)
25958 to_x = max_x;
25959 else
25960 to_x = min (to_x, max_x);
25961
25962 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25963
25964 /* Notice if the cursor will be cleared by this operation. */
25965 if (!updated_row->full_width_p)
25966 notice_overwritten_cursor (w, updated_area,
25967 w->output_cursor.x, -1,
25968 updated_row->y,
25969 MATRIX_ROW_BOTTOM_Y (updated_row));
25970
25971 from_x = w->output_cursor.x;
25972
25973 /* Translate to frame coordinates. */
25974 if (updated_row->full_width_p)
25975 {
25976 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25977 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25978 }
25979 else
25980 {
25981 int area_left = window_box_left (w, updated_area);
25982 from_x += area_left;
25983 to_x += area_left;
25984 }
25985
25986 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25987 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
25988 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25989
25990 /* Prevent inadvertently clearing to end of the X window. */
25991 if (to_x > from_x && to_y > from_y)
25992 {
25993 block_input ();
25994 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25995 to_x - from_x, to_y - from_y);
25996 unblock_input ();
25997 }
25998 }
25999
26000 #endif /* HAVE_WINDOW_SYSTEM */
26001
26002
26003 \f
26004 /***********************************************************************
26005 Cursor types
26006 ***********************************************************************/
26007
26008 /* Value is the internal representation of the specified cursor type
26009 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26010 of the bar cursor. */
26011
26012 static enum text_cursor_kinds
26013 get_specified_cursor_type (Lisp_Object arg, int *width)
26014 {
26015 enum text_cursor_kinds type;
26016
26017 if (NILP (arg))
26018 return NO_CURSOR;
26019
26020 if (EQ (arg, Qbox))
26021 return FILLED_BOX_CURSOR;
26022
26023 if (EQ (arg, Qhollow))
26024 return HOLLOW_BOX_CURSOR;
26025
26026 if (EQ (arg, Qbar))
26027 {
26028 *width = 2;
26029 return BAR_CURSOR;
26030 }
26031
26032 if (CONSP (arg)
26033 && EQ (XCAR (arg), Qbar)
26034 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26035 {
26036 *width = XINT (XCDR (arg));
26037 return BAR_CURSOR;
26038 }
26039
26040 if (EQ (arg, Qhbar))
26041 {
26042 *width = 2;
26043 return HBAR_CURSOR;
26044 }
26045
26046 if (CONSP (arg)
26047 && EQ (XCAR (arg), Qhbar)
26048 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26049 {
26050 *width = XINT (XCDR (arg));
26051 return HBAR_CURSOR;
26052 }
26053
26054 /* Treat anything unknown as "hollow box cursor".
26055 It was bad to signal an error; people have trouble fixing
26056 .Xdefaults with Emacs, when it has something bad in it. */
26057 type = HOLLOW_BOX_CURSOR;
26058
26059 return type;
26060 }
26061
26062 /* Set the default cursor types for specified frame. */
26063 void
26064 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26065 {
26066 int width = 1;
26067 Lisp_Object tem;
26068
26069 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26070 FRAME_CURSOR_WIDTH (f) = width;
26071
26072 /* By default, set up the blink-off state depending on the on-state. */
26073
26074 tem = Fassoc (arg, Vblink_cursor_alist);
26075 if (!NILP (tem))
26076 {
26077 FRAME_BLINK_OFF_CURSOR (f)
26078 = get_specified_cursor_type (XCDR (tem), &width);
26079 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26080 }
26081 else
26082 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26083
26084 /* Make sure the cursor gets redrawn. */
26085 f->cursor_type_changed = 1;
26086 }
26087
26088
26089 #ifdef HAVE_WINDOW_SYSTEM
26090
26091 /* Return the cursor we want to be displayed in window W. Return
26092 width of bar/hbar cursor through WIDTH arg. Return with
26093 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26094 (i.e. if the `system caret' should track this cursor).
26095
26096 In a mini-buffer window, we want the cursor only to appear if we
26097 are reading input from this window. For the selected window, we
26098 want the cursor type given by the frame parameter or buffer local
26099 setting of cursor-type. If explicitly marked off, draw no cursor.
26100 In all other cases, we want a hollow box cursor. */
26101
26102 static enum text_cursor_kinds
26103 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26104 int *active_cursor)
26105 {
26106 struct frame *f = XFRAME (w->frame);
26107 struct buffer *b = XBUFFER (w->contents);
26108 int cursor_type = DEFAULT_CURSOR;
26109 Lisp_Object alt_cursor;
26110 int non_selected = 0;
26111
26112 *active_cursor = 1;
26113
26114 /* Echo area */
26115 if (cursor_in_echo_area
26116 && FRAME_HAS_MINIBUF_P (f)
26117 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26118 {
26119 if (w == XWINDOW (echo_area_window))
26120 {
26121 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26122 {
26123 *width = FRAME_CURSOR_WIDTH (f);
26124 return FRAME_DESIRED_CURSOR (f);
26125 }
26126 else
26127 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26128 }
26129
26130 *active_cursor = 0;
26131 non_selected = 1;
26132 }
26133
26134 /* Detect a nonselected window or nonselected frame. */
26135 else if (w != XWINDOW (f->selected_window)
26136 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26137 {
26138 *active_cursor = 0;
26139
26140 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26141 return NO_CURSOR;
26142
26143 non_selected = 1;
26144 }
26145
26146 /* Never display a cursor in a window in which cursor-type is nil. */
26147 if (NILP (BVAR (b, cursor_type)))
26148 return NO_CURSOR;
26149
26150 /* Get the normal cursor type for this window. */
26151 if (EQ (BVAR (b, cursor_type), Qt))
26152 {
26153 cursor_type = FRAME_DESIRED_CURSOR (f);
26154 *width = FRAME_CURSOR_WIDTH (f);
26155 }
26156 else
26157 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26158
26159 /* Use cursor-in-non-selected-windows instead
26160 for non-selected window or frame. */
26161 if (non_selected)
26162 {
26163 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26164 if (!EQ (Qt, alt_cursor))
26165 return get_specified_cursor_type (alt_cursor, width);
26166 /* t means modify the normal cursor type. */
26167 if (cursor_type == FILLED_BOX_CURSOR)
26168 cursor_type = HOLLOW_BOX_CURSOR;
26169 else if (cursor_type == BAR_CURSOR && *width > 1)
26170 --*width;
26171 return cursor_type;
26172 }
26173
26174 /* Use normal cursor if not blinked off. */
26175 if (!w->cursor_off_p)
26176 {
26177 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26178 {
26179 if (cursor_type == FILLED_BOX_CURSOR)
26180 {
26181 /* Using a block cursor on large images can be very annoying.
26182 So use a hollow cursor for "large" images.
26183 If image is not transparent (no mask), also use hollow cursor. */
26184 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26185 if (img != NULL && IMAGEP (img->spec))
26186 {
26187 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26188 where N = size of default frame font size.
26189 This should cover most of the "tiny" icons people may use. */
26190 if (!img->mask
26191 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26192 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26193 cursor_type = HOLLOW_BOX_CURSOR;
26194 }
26195 }
26196 else if (cursor_type != NO_CURSOR)
26197 {
26198 /* Display current only supports BOX and HOLLOW cursors for images.
26199 So for now, unconditionally use a HOLLOW cursor when cursor is
26200 not a solid box cursor. */
26201 cursor_type = HOLLOW_BOX_CURSOR;
26202 }
26203 }
26204 return cursor_type;
26205 }
26206
26207 /* Cursor is blinked off, so determine how to "toggle" it. */
26208
26209 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26210 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26211 return get_specified_cursor_type (XCDR (alt_cursor), width);
26212
26213 /* Then see if frame has specified a specific blink off cursor type. */
26214 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26215 {
26216 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26217 return FRAME_BLINK_OFF_CURSOR (f);
26218 }
26219
26220 #if 0
26221 /* Some people liked having a permanently visible blinking cursor,
26222 while others had very strong opinions against it. So it was
26223 decided to remove it. KFS 2003-09-03 */
26224
26225 /* Finally perform built-in cursor blinking:
26226 filled box <-> hollow box
26227 wide [h]bar <-> narrow [h]bar
26228 narrow [h]bar <-> no cursor
26229 other type <-> no cursor */
26230
26231 if (cursor_type == FILLED_BOX_CURSOR)
26232 return HOLLOW_BOX_CURSOR;
26233
26234 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26235 {
26236 *width = 1;
26237 return cursor_type;
26238 }
26239 #endif
26240
26241 return NO_CURSOR;
26242 }
26243
26244
26245 /* Notice when the text cursor of window W has been completely
26246 overwritten by a drawing operation that outputs glyphs in AREA
26247 starting at X0 and ending at X1 in the line starting at Y0 and
26248 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26249 the rest of the line after X0 has been written. Y coordinates
26250 are window-relative. */
26251
26252 static void
26253 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26254 int x0, int x1, int y0, int y1)
26255 {
26256 int cx0, cx1, cy0, cy1;
26257 struct glyph_row *row;
26258
26259 if (!w->phys_cursor_on_p)
26260 return;
26261 if (area != TEXT_AREA)
26262 return;
26263
26264 if (w->phys_cursor.vpos < 0
26265 || w->phys_cursor.vpos >= w->current_matrix->nrows
26266 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26267 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26268 return;
26269
26270 if (row->cursor_in_fringe_p)
26271 {
26272 row->cursor_in_fringe_p = 0;
26273 draw_fringe_bitmap (w, row, row->reversed_p);
26274 w->phys_cursor_on_p = 0;
26275 return;
26276 }
26277
26278 cx0 = w->phys_cursor.x;
26279 cx1 = cx0 + w->phys_cursor_width;
26280 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26281 return;
26282
26283 /* The cursor image will be completely removed from the
26284 screen if the output area intersects the cursor area in
26285 y-direction. When we draw in [y0 y1[, and some part of
26286 the cursor is at y < y0, that part must have been drawn
26287 before. When scrolling, the cursor is erased before
26288 actually scrolling, so we don't come here. When not
26289 scrolling, the rows above the old cursor row must have
26290 changed, and in this case these rows must have written
26291 over the cursor image.
26292
26293 Likewise if part of the cursor is below y1, with the
26294 exception of the cursor being in the first blank row at
26295 the buffer and window end because update_text_area
26296 doesn't draw that row. (Except when it does, but
26297 that's handled in update_text_area.) */
26298
26299 cy0 = w->phys_cursor.y;
26300 cy1 = cy0 + w->phys_cursor_height;
26301 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26302 return;
26303
26304 w->phys_cursor_on_p = 0;
26305 }
26306
26307 #endif /* HAVE_WINDOW_SYSTEM */
26308
26309 \f
26310 /************************************************************************
26311 Mouse Face
26312 ************************************************************************/
26313
26314 #ifdef HAVE_WINDOW_SYSTEM
26315
26316 /* EXPORT for RIF:
26317 Fix the display of area AREA of overlapping row ROW in window W
26318 with respect to the overlapping part OVERLAPS. */
26319
26320 void
26321 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26322 enum glyph_row_area area, int overlaps)
26323 {
26324 int i, x;
26325
26326 block_input ();
26327
26328 x = 0;
26329 for (i = 0; i < row->used[area];)
26330 {
26331 if (row->glyphs[area][i].overlaps_vertically_p)
26332 {
26333 int start = i, start_x = x;
26334
26335 do
26336 {
26337 x += row->glyphs[area][i].pixel_width;
26338 ++i;
26339 }
26340 while (i < row->used[area]
26341 && row->glyphs[area][i].overlaps_vertically_p);
26342
26343 draw_glyphs (w, start_x, row, area,
26344 start, i,
26345 DRAW_NORMAL_TEXT, overlaps);
26346 }
26347 else
26348 {
26349 x += row->glyphs[area][i].pixel_width;
26350 ++i;
26351 }
26352 }
26353
26354 unblock_input ();
26355 }
26356
26357
26358 /* EXPORT:
26359 Draw the cursor glyph of window W in glyph row ROW. See the
26360 comment of draw_glyphs for the meaning of HL. */
26361
26362 void
26363 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26364 enum draw_glyphs_face hl)
26365 {
26366 /* If cursor hpos is out of bounds, don't draw garbage. This can
26367 happen in mini-buffer windows when switching between echo area
26368 glyphs and mini-buffer. */
26369 if ((row->reversed_p
26370 ? (w->phys_cursor.hpos >= 0)
26371 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26372 {
26373 int on_p = w->phys_cursor_on_p;
26374 int x1;
26375 int hpos = w->phys_cursor.hpos;
26376
26377 /* When the window is hscrolled, cursor hpos can legitimately be
26378 out of bounds, but we draw the cursor at the corresponding
26379 window margin in that case. */
26380 if (!row->reversed_p && hpos < 0)
26381 hpos = 0;
26382 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26383 hpos = row->used[TEXT_AREA] - 1;
26384
26385 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26386 hl, 0);
26387 w->phys_cursor_on_p = on_p;
26388
26389 if (hl == DRAW_CURSOR)
26390 w->phys_cursor_width = x1 - w->phys_cursor.x;
26391 /* When we erase the cursor, and ROW is overlapped by other
26392 rows, make sure that these overlapping parts of other rows
26393 are redrawn. */
26394 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26395 {
26396 w->phys_cursor_width = x1 - w->phys_cursor.x;
26397
26398 if (row > w->current_matrix->rows
26399 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26400 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26401 OVERLAPS_ERASED_CURSOR);
26402
26403 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26404 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26405 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26406 OVERLAPS_ERASED_CURSOR);
26407 }
26408 }
26409 }
26410
26411
26412 /* EXPORT:
26413 Erase the image of a cursor of window W from the screen. */
26414
26415 void
26416 erase_phys_cursor (struct window *w)
26417 {
26418 struct frame *f = XFRAME (w->frame);
26419 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26420 int hpos = w->phys_cursor.hpos;
26421 int vpos = w->phys_cursor.vpos;
26422 int mouse_face_here_p = 0;
26423 struct glyph_matrix *active_glyphs = w->current_matrix;
26424 struct glyph_row *cursor_row;
26425 struct glyph *cursor_glyph;
26426 enum draw_glyphs_face hl;
26427
26428 /* No cursor displayed or row invalidated => nothing to do on the
26429 screen. */
26430 if (w->phys_cursor_type == NO_CURSOR)
26431 goto mark_cursor_off;
26432
26433 /* VPOS >= active_glyphs->nrows means that window has been resized.
26434 Don't bother to erase the cursor. */
26435 if (vpos >= active_glyphs->nrows)
26436 goto mark_cursor_off;
26437
26438 /* If row containing cursor is marked invalid, there is nothing we
26439 can do. */
26440 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26441 if (!cursor_row->enabled_p)
26442 goto mark_cursor_off;
26443
26444 /* If line spacing is > 0, old cursor may only be partially visible in
26445 window after split-window. So adjust visible height. */
26446 cursor_row->visible_height = min (cursor_row->visible_height,
26447 window_text_bottom_y (w) - cursor_row->y);
26448
26449 /* If row is completely invisible, don't attempt to delete a cursor which
26450 isn't there. This can happen if cursor is at top of a window, and
26451 we switch to a buffer with a header line in that window. */
26452 if (cursor_row->visible_height <= 0)
26453 goto mark_cursor_off;
26454
26455 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26456 if (cursor_row->cursor_in_fringe_p)
26457 {
26458 cursor_row->cursor_in_fringe_p = 0;
26459 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26460 goto mark_cursor_off;
26461 }
26462
26463 /* This can happen when the new row is shorter than the old one.
26464 In this case, either draw_glyphs or clear_end_of_line
26465 should have cleared the cursor. Note that we wouldn't be
26466 able to erase the cursor in this case because we don't have a
26467 cursor glyph at hand. */
26468 if ((cursor_row->reversed_p
26469 ? (w->phys_cursor.hpos < 0)
26470 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26471 goto mark_cursor_off;
26472
26473 /* When the window is hscrolled, cursor hpos can legitimately be out
26474 of bounds, but we draw the cursor at the corresponding window
26475 margin in that case. */
26476 if (!cursor_row->reversed_p && hpos < 0)
26477 hpos = 0;
26478 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26479 hpos = cursor_row->used[TEXT_AREA] - 1;
26480
26481 /* If the cursor is in the mouse face area, redisplay that when
26482 we clear the cursor. */
26483 if (! NILP (hlinfo->mouse_face_window)
26484 && coords_in_mouse_face_p (w, hpos, vpos)
26485 /* Don't redraw the cursor's spot in mouse face if it is at the
26486 end of a line (on a newline). The cursor appears there, but
26487 mouse highlighting does not. */
26488 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26489 mouse_face_here_p = 1;
26490
26491 /* Maybe clear the display under the cursor. */
26492 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26493 {
26494 int x, y, left_x;
26495 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26496 int width;
26497
26498 cursor_glyph = get_phys_cursor_glyph (w);
26499 if (cursor_glyph == NULL)
26500 goto mark_cursor_off;
26501
26502 width = cursor_glyph->pixel_width;
26503 left_x = window_box_left_offset (w, TEXT_AREA);
26504 x = w->phys_cursor.x;
26505 if (x < left_x)
26506 width -= left_x - x;
26507 width = min (width, window_box_width (w, TEXT_AREA) - x);
26508 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26509 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26510
26511 if (width > 0)
26512 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26513 }
26514
26515 /* Erase the cursor by redrawing the character underneath it. */
26516 if (mouse_face_here_p)
26517 hl = DRAW_MOUSE_FACE;
26518 else
26519 hl = DRAW_NORMAL_TEXT;
26520 draw_phys_cursor_glyph (w, cursor_row, hl);
26521
26522 mark_cursor_off:
26523 w->phys_cursor_on_p = 0;
26524 w->phys_cursor_type = NO_CURSOR;
26525 }
26526
26527
26528 /* EXPORT:
26529 Display or clear cursor of window W. If ON is zero, clear the
26530 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26531 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26532
26533 void
26534 display_and_set_cursor (struct window *w, bool on,
26535 int hpos, int vpos, int x, int y)
26536 {
26537 struct frame *f = XFRAME (w->frame);
26538 int new_cursor_type;
26539 int new_cursor_width;
26540 int active_cursor;
26541 struct glyph_row *glyph_row;
26542 struct glyph *glyph;
26543
26544 /* This is pointless on invisible frames, and dangerous on garbaged
26545 windows and frames; in the latter case, the frame or window may
26546 be in the midst of changing its size, and x and y may be off the
26547 window. */
26548 if (! FRAME_VISIBLE_P (f)
26549 || FRAME_GARBAGED_P (f)
26550 || vpos >= w->current_matrix->nrows
26551 || hpos >= w->current_matrix->matrix_w)
26552 return;
26553
26554 /* If cursor is off and we want it off, return quickly. */
26555 if (!on && !w->phys_cursor_on_p)
26556 return;
26557
26558 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26559 /* If cursor row is not enabled, we don't really know where to
26560 display the cursor. */
26561 if (!glyph_row->enabled_p)
26562 {
26563 w->phys_cursor_on_p = 0;
26564 return;
26565 }
26566
26567 glyph = NULL;
26568 if (!glyph_row->exact_window_width_line_p
26569 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26570 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26571
26572 eassert (input_blocked_p ());
26573
26574 /* Set new_cursor_type to the cursor we want to be displayed. */
26575 new_cursor_type = get_window_cursor_type (w, glyph,
26576 &new_cursor_width, &active_cursor);
26577
26578 /* If cursor is currently being shown and we don't want it to be or
26579 it is in the wrong place, or the cursor type is not what we want,
26580 erase it. */
26581 if (w->phys_cursor_on_p
26582 && (!on
26583 || w->phys_cursor.x != x
26584 || w->phys_cursor.y != y
26585 || new_cursor_type != w->phys_cursor_type
26586 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26587 && new_cursor_width != w->phys_cursor_width)))
26588 erase_phys_cursor (w);
26589
26590 /* Don't check phys_cursor_on_p here because that flag is only set
26591 to zero in some cases where we know that the cursor has been
26592 completely erased, to avoid the extra work of erasing the cursor
26593 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26594 still not be visible, or it has only been partly erased. */
26595 if (on)
26596 {
26597 w->phys_cursor_ascent = glyph_row->ascent;
26598 w->phys_cursor_height = glyph_row->height;
26599
26600 /* Set phys_cursor_.* before x_draw_.* is called because some
26601 of them may need the information. */
26602 w->phys_cursor.x = x;
26603 w->phys_cursor.y = glyph_row->y;
26604 w->phys_cursor.hpos = hpos;
26605 w->phys_cursor.vpos = vpos;
26606 }
26607
26608 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26609 new_cursor_type, new_cursor_width,
26610 on, active_cursor);
26611 }
26612
26613
26614 /* Switch the display of W's cursor on or off, according to the value
26615 of ON. */
26616
26617 static void
26618 update_window_cursor (struct window *w, bool on)
26619 {
26620 /* Don't update cursor in windows whose frame is in the process
26621 of being deleted. */
26622 if (w->current_matrix)
26623 {
26624 int hpos = w->phys_cursor.hpos;
26625 int vpos = w->phys_cursor.vpos;
26626 struct glyph_row *row;
26627
26628 if (vpos >= w->current_matrix->nrows
26629 || hpos >= w->current_matrix->matrix_w)
26630 return;
26631
26632 row = MATRIX_ROW (w->current_matrix, vpos);
26633
26634 /* When the window is hscrolled, cursor hpos can legitimately be
26635 out of bounds, but we draw the cursor at the corresponding
26636 window margin in that case. */
26637 if (!row->reversed_p && hpos < 0)
26638 hpos = 0;
26639 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26640 hpos = row->used[TEXT_AREA] - 1;
26641
26642 block_input ();
26643 display_and_set_cursor (w, on, hpos, vpos,
26644 w->phys_cursor.x, w->phys_cursor.y);
26645 unblock_input ();
26646 }
26647 }
26648
26649
26650 /* Call update_window_cursor with parameter ON_P on all leaf windows
26651 in the window tree rooted at W. */
26652
26653 static void
26654 update_cursor_in_window_tree (struct window *w, bool on_p)
26655 {
26656 while (w)
26657 {
26658 if (WINDOWP (w->contents))
26659 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26660 else
26661 update_window_cursor (w, on_p);
26662
26663 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26664 }
26665 }
26666
26667
26668 /* EXPORT:
26669 Display the cursor on window W, or clear it, according to ON_P.
26670 Don't change the cursor's position. */
26671
26672 void
26673 x_update_cursor (struct frame *f, bool on_p)
26674 {
26675 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26676 }
26677
26678
26679 /* EXPORT:
26680 Clear the cursor of window W to background color, and mark the
26681 cursor as not shown. This is used when the text where the cursor
26682 is about to be rewritten. */
26683
26684 void
26685 x_clear_cursor (struct window *w)
26686 {
26687 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26688 update_window_cursor (w, 0);
26689 }
26690
26691 #endif /* HAVE_WINDOW_SYSTEM */
26692
26693 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26694 and MSDOS. */
26695 static void
26696 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26697 int start_hpos, int end_hpos,
26698 enum draw_glyphs_face draw)
26699 {
26700 #ifdef HAVE_WINDOW_SYSTEM
26701 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26702 {
26703 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26704 return;
26705 }
26706 #endif
26707 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26708 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26709 #endif
26710 }
26711
26712 /* Display the active region described by mouse_face_* according to DRAW. */
26713
26714 static void
26715 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26716 {
26717 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26718 struct frame *f = XFRAME (WINDOW_FRAME (w));
26719
26720 if (/* If window is in the process of being destroyed, don't bother
26721 to do anything. */
26722 w->current_matrix != NULL
26723 /* Don't update mouse highlight if hidden */
26724 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26725 /* Recognize when we are called to operate on rows that don't exist
26726 anymore. This can happen when a window is split. */
26727 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26728 {
26729 int phys_cursor_on_p = w->phys_cursor_on_p;
26730 struct glyph_row *row, *first, *last;
26731
26732 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26733 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26734
26735 for (row = first; row <= last && row->enabled_p; ++row)
26736 {
26737 int start_hpos, end_hpos, start_x;
26738
26739 /* For all but the first row, the highlight starts at column 0. */
26740 if (row == first)
26741 {
26742 /* R2L rows have BEG and END in reversed order, but the
26743 screen drawing geometry is always left to right. So
26744 we need to mirror the beginning and end of the
26745 highlighted area in R2L rows. */
26746 if (!row->reversed_p)
26747 {
26748 start_hpos = hlinfo->mouse_face_beg_col;
26749 start_x = hlinfo->mouse_face_beg_x;
26750 }
26751 else if (row == last)
26752 {
26753 start_hpos = hlinfo->mouse_face_end_col;
26754 start_x = hlinfo->mouse_face_end_x;
26755 }
26756 else
26757 {
26758 start_hpos = 0;
26759 start_x = 0;
26760 }
26761 }
26762 else if (row->reversed_p && row == last)
26763 {
26764 start_hpos = hlinfo->mouse_face_end_col;
26765 start_x = hlinfo->mouse_face_end_x;
26766 }
26767 else
26768 {
26769 start_hpos = 0;
26770 start_x = 0;
26771 }
26772
26773 if (row == last)
26774 {
26775 if (!row->reversed_p)
26776 end_hpos = hlinfo->mouse_face_end_col;
26777 else if (row == first)
26778 end_hpos = hlinfo->mouse_face_beg_col;
26779 else
26780 {
26781 end_hpos = row->used[TEXT_AREA];
26782 if (draw == DRAW_NORMAL_TEXT)
26783 row->fill_line_p = 1; /* Clear to end of line */
26784 }
26785 }
26786 else if (row->reversed_p && row == first)
26787 end_hpos = hlinfo->mouse_face_beg_col;
26788 else
26789 {
26790 end_hpos = row->used[TEXT_AREA];
26791 if (draw == DRAW_NORMAL_TEXT)
26792 row->fill_line_p = 1; /* Clear to end of line */
26793 }
26794
26795 if (end_hpos > start_hpos)
26796 {
26797 draw_row_with_mouse_face (w, start_x, row,
26798 start_hpos, end_hpos, draw);
26799
26800 row->mouse_face_p
26801 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26802 }
26803 }
26804
26805 #ifdef HAVE_WINDOW_SYSTEM
26806 /* When we've written over the cursor, arrange for it to
26807 be displayed again. */
26808 if (FRAME_WINDOW_P (f)
26809 && phys_cursor_on_p && !w->phys_cursor_on_p)
26810 {
26811 int hpos = w->phys_cursor.hpos;
26812
26813 /* When the window is hscrolled, cursor hpos can legitimately be
26814 out of bounds, but we draw the cursor at the corresponding
26815 window margin in that case. */
26816 if (!row->reversed_p && hpos < 0)
26817 hpos = 0;
26818 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26819 hpos = row->used[TEXT_AREA] - 1;
26820
26821 block_input ();
26822 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26823 w->phys_cursor.x, w->phys_cursor.y);
26824 unblock_input ();
26825 }
26826 #endif /* HAVE_WINDOW_SYSTEM */
26827 }
26828
26829 #ifdef HAVE_WINDOW_SYSTEM
26830 /* Change the mouse cursor. */
26831 if (FRAME_WINDOW_P (f))
26832 {
26833 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
26834 if (draw == DRAW_NORMAL_TEXT
26835 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26836 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26837 else
26838 #endif
26839 if (draw == DRAW_MOUSE_FACE)
26840 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26841 else
26842 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26843 }
26844 #endif /* HAVE_WINDOW_SYSTEM */
26845 }
26846
26847 /* EXPORT:
26848 Clear out the mouse-highlighted active region.
26849 Redraw it un-highlighted first. Value is non-zero if mouse
26850 face was actually drawn unhighlighted. */
26851
26852 int
26853 clear_mouse_face (Mouse_HLInfo *hlinfo)
26854 {
26855 int cleared = 0;
26856
26857 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26858 {
26859 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26860 cleared = 1;
26861 }
26862
26863 reset_mouse_highlight (hlinfo);
26864 return cleared;
26865 }
26866
26867 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26868 within the mouse face on that window. */
26869 static int
26870 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26871 {
26872 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26873
26874 /* Quickly resolve the easy cases. */
26875 if (!(WINDOWP (hlinfo->mouse_face_window)
26876 && XWINDOW (hlinfo->mouse_face_window) == w))
26877 return 0;
26878 if (vpos < hlinfo->mouse_face_beg_row
26879 || vpos > hlinfo->mouse_face_end_row)
26880 return 0;
26881 if (vpos > hlinfo->mouse_face_beg_row
26882 && vpos < hlinfo->mouse_face_end_row)
26883 return 1;
26884
26885 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26886 {
26887 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26888 {
26889 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26890 return 1;
26891 }
26892 else if ((vpos == hlinfo->mouse_face_beg_row
26893 && hpos >= hlinfo->mouse_face_beg_col)
26894 || (vpos == hlinfo->mouse_face_end_row
26895 && hpos < hlinfo->mouse_face_end_col))
26896 return 1;
26897 }
26898 else
26899 {
26900 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26901 {
26902 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26903 return 1;
26904 }
26905 else if ((vpos == hlinfo->mouse_face_beg_row
26906 && hpos <= hlinfo->mouse_face_beg_col)
26907 || (vpos == hlinfo->mouse_face_end_row
26908 && hpos > hlinfo->mouse_face_end_col))
26909 return 1;
26910 }
26911 return 0;
26912 }
26913
26914
26915 /* EXPORT:
26916 Non-zero if physical cursor of window W is within mouse face. */
26917
26918 int
26919 cursor_in_mouse_face_p (struct window *w)
26920 {
26921 int hpos = w->phys_cursor.hpos;
26922 int vpos = w->phys_cursor.vpos;
26923 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26924
26925 /* When the window is hscrolled, cursor hpos can legitimately be out
26926 of bounds, but we draw the cursor at the corresponding window
26927 margin in that case. */
26928 if (!row->reversed_p && hpos < 0)
26929 hpos = 0;
26930 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26931 hpos = row->used[TEXT_AREA] - 1;
26932
26933 return coords_in_mouse_face_p (w, hpos, vpos);
26934 }
26935
26936
26937 \f
26938 /* Find the glyph rows START_ROW and END_ROW of window W that display
26939 characters between buffer positions START_CHARPOS and END_CHARPOS
26940 (excluding END_CHARPOS). DISP_STRING is a display string that
26941 covers these buffer positions. This is similar to
26942 row_containing_pos, but is more accurate when bidi reordering makes
26943 buffer positions change non-linearly with glyph rows. */
26944 static void
26945 rows_from_pos_range (struct window *w,
26946 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26947 Lisp_Object disp_string,
26948 struct glyph_row **start, struct glyph_row **end)
26949 {
26950 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26951 int last_y = window_text_bottom_y (w);
26952 struct glyph_row *row;
26953
26954 *start = NULL;
26955 *end = NULL;
26956
26957 while (!first->enabled_p
26958 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26959 first++;
26960
26961 /* Find the START row. */
26962 for (row = first;
26963 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26964 row++)
26965 {
26966 /* A row can potentially be the START row if the range of the
26967 characters it displays intersects the range
26968 [START_CHARPOS..END_CHARPOS). */
26969 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26970 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26971 /* See the commentary in row_containing_pos, for the
26972 explanation of the complicated way to check whether
26973 some position is beyond the end of the characters
26974 displayed by a row. */
26975 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26976 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26977 && !row->ends_at_zv_p
26978 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26979 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26980 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26981 && !row->ends_at_zv_p
26982 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26983 {
26984 /* Found a candidate row. Now make sure at least one of the
26985 glyphs it displays has a charpos from the range
26986 [START_CHARPOS..END_CHARPOS).
26987
26988 This is not obvious because bidi reordering could make
26989 buffer positions of a row be 1,2,3,102,101,100, and if we
26990 want to highlight characters in [50..60), we don't want
26991 this row, even though [50..60) does intersect [1..103),
26992 the range of character positions given by the row's start
26993 and end positions. */
26994 struct glyph *g = row->glyphs[TEXT_AREA];
26995 struct glyph *e = g + row->used[TEXT_AREA];
26996
26997 while (g < e)
26998 {
26999 if (((BUFFERP (g->object) || INTEGERP (g->object))
27000 && start_charpos <= g->charpos && g->charpos < end_charpos)
27001 /* A glyph that comes from DISP_STRING is by
27002 definition to be highlighted. */
27003 || EQ (g->object, disp_string))
27004 *start = row;
27005 g++;
27006 }
27007 if (*start)
27008 break;
27009 }
27010 }
27011
27012 /* Find the END row. */
27013 if (!*start
27014 /* If the last row is partially visible, start looking for END
27015 from that row, instead of starting from FIRST. */
27016 && !(row->enabled_p
27017 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27018 row = first;
27019 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27020 {
27021 struct glyph_row *next = row + 1;
27022 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27023
27024 if (!next->enabled_p
27025 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27026 /* The first row >= START whose range of displayed characters
27027 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27028 is the row END + 1. */
27029 || (start_charpos < next_start
27030 && end_charpos < next_start)
27031 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27032 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27033 && !next->ends_at_zv_p
27034 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27035 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27036 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27037 && !next->ends_at_zv_p
27038 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27039 {
27040 *end = row;
27041 break;
27042 }
27043 else
27044 {
27045 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27046 but none of the characters it displays are in the range, it is
27047 also END + 1. */
27048 struct glyph *g = next->glyphs[TEXT_AREA];
27049 struct glyph *s = g;
27050 struct glyph *e = g + next->used[TEXT_AREA];
27051
27052 while (g < e)
27053 {
27054 if (((BUFFERP (g->object) || INTEGERP (g->object))
27055 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27056 /* If the buffer position of the first glyph in
27057 the row is equal to END_CHARPOS, it means
27058 the last character to be highlighted is the
27059 newline of ROW, and we must consider NEXT as
27060 END, not END+1. */
27061 || (((!next->reversed_p && g == s)
27062 || (next->reversed_p && g == e - 1))
27063 && (g->charpos == end_charpos
27064 /* Special case for when NEXT is an
27065 empty line at ZV. */
27066 || (g->charpos == -1
27067 && !row->ends_at_zv_p
27068 && next_start == end_charpos)))))
27069 /* A glyph that comes from DISP_STRING is by
27070 definition to be highlighted. */
27071 || EQ (g->object, disp_string))
27072 break;
27073 g++;
27074 }
27075 if (g == e)
27076 {
27077 *end = row;
27078 break;
27079 }
27080 /* The first row that ends at ZV must be the last to be
27081 highlighted. */
27082 else if (next->ends_at_zv_p)
27083 {
27084 *end = next;
27085 break;
27086 }
27087 }
27088 }
27089 }
27090
27091 /* This function sets the mouse_face_* elements of HLINFO, assuming
27092 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27093 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27094 for the overlay or run of text properties specifying the mouse
27095 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27096 before-string and after-string that must also be highlighted.
27097 DISP_STRING, if non-nil, is a display string that may cover some
27098 or all of the highlighted text. */
27099
27100 static void
27101 mouse_face_from_buffer_pos (Lisp_Object window,
27102 Mouse_HLInfo *hlinfo,
27103 ptrdiff_t mouse_charpos,
27104 ptrdiff_t start_charpos,
27105 ptrdiff_t end_charpos,
27106 Lisp_Object before_string,
27107 Lisp_Object after_string,
27108 Lisp_Object disp_string)
27109 {
27110 struct window *w = XWINDOW (window);
27111 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27112 struct glyph_row *r1, *r2;
27113 struct glyph *glyph, *end;
27114 ptrdiff_t ignore, pos;
27115 int x;
27116
27117 eassert (NILP (disp_string) || STRINGP (disp_string));
27118 eassert (NILP (before_string) || STRINGP (before_string));
27119 eassert (NILP (after_string) || STRINGP (after_string));
27120
27121 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27122 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27123 if (r1 == NULL)
27124 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27125 /* If the before-string or display-string contains newlines,
27126 rows_from_pos_range skips to its last row. Move back. */
27127 if (!NILP (before_string) || !NILP (disp_string))
27128 {
27129 struct glyph_row *prev;
27130 while ((prev = r1 - 1, prev >= first)
27131 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27132 && prev->used[TEXT_AREA] > 0)
27133 {
27134 struct glyph *beg = prev->glyphs[TEXT_AREA];
27135 glyph = beg + prev->used[TEXT_AREA];
27136 while (--glyph >= beg && INTEGERP (glyph->object));
27137 if (glyph < beg
27138 || !(EQ (glyph->object, before_string)
27139 || EQ (glyph->object, disp_string)))
27140 break;
27141 r1 = prev;
27142 }
27143 }
27144 if (r2 == NULL)
27145 {
27146 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27147 hlinfo->mouse_face_past_end = 1;
27148 }
27149 else if (!NILP (after_string))
27150 {
27151 /* If the after-string has newlines, advance to its last row. */
27152 struct glyph_row *next;
27153 struct glyph_row *last
27154 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27155
27156 for (next = r2 + 1;
27157 next <= last
27158 && next->used[TEXT_AREA] > 0
27159 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27160 ++next)
27161 r2 = next;
27162 }
27163 /* The rest of the display engine assumes that mouse_face_beg_row is
27164 either above mouse_face_end_row or identical to it. But with
27165 bidi-reordered continued lines, the row for START_CHARPOS could
27166 be below the row for END_CHARPOS. If so, swap the rows and store
27167 them in correct order. */
27168 if (r1->y > r2->y)
27169 {
27170 struct glyph_row *tem = r2;
27171
27172 r2 = r1;
27173 r1 = tem;
27174 }
27175
27176 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27177 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27178
27179 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27180 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27181 could be anywhere in the row and in any order. The strategy
27182 below is to find the leftmost and the rightmost glyph that
27183 belongs to either of these 3 strings, or whose position is
27184 between START_CHARPOS and END_CHARPOS, and highlight all the
27185 glyphs between those two. This may cover more than just the text
27186 between START_CHARPOS and END_CHARPOS if the range of characters
27187 strides the bidi level boundary, e.g. if the beginning is in R2L
27188 text while the end is in L2R text or vice versa. */
27189 if (!r1->reversed_p)
27190 {
27191 /* This row is in a left to right paragraph. Scan it left to
27192 right. */
27193 glyph = r1->glyphs[TEXT_AREA];
27194 end = glyph + r1->used[TEXT_AREA];
27195 x = r1->x;
27196
27197 /* Skip truncation glyphs at the start of the glyph row. */
27198 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27199 for (; glyph < end
27200 && INTEGERP (glyph->object)
27201 && glyph->charpos < 0;
27202 ++glyph)
27203 x += glyph->pixel_width;
27204
27205 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27206 or DISP_STRING, and the first glyph from buffer whose
27207 position is between START_CHARPOS and END_CHARPOS. */
27208 for (; glyph < end
27209 && !INTEGERP (glyph->object)
27210 && !EQ (glyph->object, disp_string)
27211 && !(BUFFERP (glyph->object)
27212 && (glyph->charpos >= start_charpos
27213 && glyph->charpos < end_charpos));
27214 ++glyph)
27215 {
27216 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27217 are present at buffer positions between START_CHARPOS and
27218 END_CHARPOS, or if they come from an overlay. */
27219 if (EQ (glyph->object, before_string))
27220 {
27221 pos = string_buffer_position (before_string,
27222 start_charpos);
27223 /* If pos == 0, it means before_string came from an
27224 overlay, not from a buffer position. */
27225 if (!pos || (pos >= start_charpos && pos < end_charpos))
27226 break;
27227 }
27228 else if (EQ (glyph->object, after_string))
27229 {
27230 pos = string_buffer_position (after_string, end_charpos);
27231 if (!pos || (pos >= start_charpos && pos < end_charpos))
27232 break;
27233 }
27234 x += glyph->pixel_width;
27235 }
27236 hlinfo->mouse_face_beg_x = x;
27237 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27238 }
27239 else
27240 {
27241 /* This row is in a right to left paragraph. Scan it right to
27242 left. */
27243 struct glyph *g;
27244
27245 end = r1->glyphs[TEXT_AREA] - 1;
27246 glyph = end + r1->used[TEXT_AREA];
27247
27248 /* Skip truncation glyphs at the start of the glyph row. */
27249 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27250 for (; glyph > end
27251 && INTEGERP (glyph->object)
27252 && glyph->charpos < 0;
27253 --glyph)
27254 ;
27255
27256 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27257 or DISP_STRING, and the first glyph from buffer whose
27258 position is between START_CHARPOS and END_CHARPOS. */
27259 for (; glyph > end
27260 && !INTEGERP (glyph->object)
27261 && !EQ (glyph->object, disp_string)
27262 && !(BUFFERP (glyph->object)
27263 && (glyph->charpos >= start_charpos
27264 && glyph->charpos < end_charpos));
27265 --glyph)
27266 {
27267 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27268 are present at buffer positions between START_CHARPOS and
27269 END_CHARPOS, or if they come from an overlay. */
27270 if (EQ (glyph->object, before_string))
27271 {
27272 pos = string_buffer_position (before_string, start_charpos);
27273 /* If pos == 0, it means before_string came from an
27274 overlay, not from a buffer position. */
27275 if (!pos || (pos >= start_charpos && pos < end_charpos))
27276 break;
27277 }
27278 else if (EQ (glyph->object, after_string))
27279 {
27280 pos = string_buffer_position (after_string, end_charpos);
27281 if (!pos || (pos >= start_charpos && pos < end_charpos))
27282 break;
27283 }
27284 }
27285
27286 glyph++; /* first glyph to the right of the highlighted area */
27287 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27288 x += g->pixel_width;
27289 hlinfo->mouse_face_beg_x = x;
27290 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27291 }
27292
27293 /* If the highlight ends in a different row, compute GLYPH and END
27294 for the end row. Otherwise, reuse the values computed above for
27295 the row where the highlight begins. */
27296 if (r2 != r1)
27297 {
27298 if (!r2->reversed_p)
27299 {
27300 glyph = r2->glyphs[TEXT_AREA];
27301 end = glyph + r2->used[TEXT_AREA];
27302 x = r2->x;
27303 }
27304 else
27305 {
27306 end = r2->glyphs[TEXT_AREA] - 1;
27307 glyph = end + r2->used[TEXT_AREA];
27308 }
27309 }
27310
27311 if (!r2->reversed_p)
27312 {
27313 /* Skip truncation and continuation glyphs near the end of the
27314 row, and also blanks and stretch glyphs inserted by
27315 extend_face_to_end_of_line. */
27316 while (end > glyph
27317 && INTEGERP ((end - 1)->object))
27318 --end;
27319 /* Scan the rest of the glyph row from the end, looking for the
27320 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27321 DISP_STRING, or whose position is between START_CHARPOS
27322 and END_CHARPOS */
27323 for (--end;
27324 end > glyph
27325 && !INTEGERP (end->object)
27326 && !EQ (end->object, disp_string)
27327 && !(BUFFERP (end->object)
27328 && (end->charpos >= start_charpos
27329 && end->charpos < end_charpos));
27330 --end)
27331 {
27332 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27333 are present at buffer positions between START_CHARPOS and
27334 END_CHARPOS, or if they come from an overlay. */
27335 if (EQ (end->object, before_string))
27336 {
27337 pos = string_buffer_position (before_string, start_charpos);
27338 if (!pos || (pos >= start_charpos && pos < end_charpos))
27339 break;
27340 }
27341 else if (EQ (end->object, after_string))
27342 {
27343 pos = string_buffer_position (after_string, end_charpos);
27344 if (!pos || (pos >= start_charpos && pos < end_charpos))
27345 break;
27346 }
27347 }
27348 /* Find the X coordinate of the last glyph to be highlighted. */
27349 for (; glyph <= end; ++glyph)
27350 x += glyph->pixel_width;
27351
27352 hlinfo->mouse_face_end_x = x;
27353 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27354 }
27355 else
27356 {
27357 /* Skip truncation and continuation glyphs near the end of the
27358 row, and also blanks and stretch glyphs inserted by
27359 extend_face_to_end_of_line. */
27360 x = r2->x;
27361 end++;
27362 while (end < glyph
27363 && INTEGERP (end->object))
27364 {
27365 x += end->pixel_width;
27366 ++end;
27367 }
27368 /* Scan the rest of the glyph row from the end, looking for the
27369 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27370 DISP_STRING, or whose position is between START_CHARPOS
27371 and END_CHARPOS */
27372 for ( ;
27373 end < glyph
27374 && !INTEGERP (end->object)
27375 && !EQ (end->object, disp_string)
27376 && !(BUFFERP (end->object)
27377 && (end->charpos >= start_charpos
27378 && end->charpos < end_charpos));
27379 ++end)
27380 {
27381 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27382 are present at buffer positions between START_CHARPOS and
27383 END_CHARPOS, or if they come from an overlay. */
27384 if (EQ (end->object, before_string))
27385 {
27386 pos = string_buffer_position (before_string, start_charpos);
27387 if (!pos || (pos >= start_charpos && pos < end_charpos))
27388 break;
27389 }
27390 else if (EQ (end->object, after_string))
27391 {
27392 pos = string_buffer_position (after_string, end_charpos);
27393 if (!pos || (pos >= start_charpos && pos < end_charpos))
27394 break;
27395 }
27396 x += end->pixel_width;
27397 }
27398 /* If we exited the above loop because we arrived at the last
27399 glyph of the row, and its buffer position is still not in
27400 range, it means the last character in range is the preceding
27401 newline. Bump the end column and x values to get past the
27402 last glyph. */
27403 if (end == glyph
27404 && BUFFERP (end->object)
27405 && (end->charpos < start_charpos
27406 || end->charpos >= end_charpos))
27407 {
27408 x += end->pixel_width;
27409 ++end;
27410 }
27411 hlinfo->mouse_face_end_x = x;
27412 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27413 }
27414
27415 hlinfo->mouse_face_window = window;
27416 hlinfo->mouse_face_face_id
27417 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27418 mouse_charpos + 1,
27419 !hlinfo->mouse_face_hidden, -1);
27420 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27421 }
27422
27423 /* The following function is not used anymore (replaced with
27424 mouse_face_from_string_pos), but I leave it here for the time
27425 being, in case someone would. */
27426
27427 #if 0 /* not used */
27428
27429 /* Find the position of the glyph for position POS in OBJECT in
27430 window W's current matrix, and return in *X, *Y the pixel
27431 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27432
27433 RIGHT_P non-zero means return the position of the right edge of the
27434 glyph, RIGHT_P zero means return the left edge position.
27435
27436 If no glyph for POS exists in the matrix, return the position of
27437 the glyph with the next smaller position that is in the matrix, if
27438 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27439 exists in the matrix, return the position of the glyph with the
27440 next larger position in OBJECT.
27441
27442 Value is non-zero if a glyph was found. */
27443
27444 static int
27445 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27446 int *hpos, int *vpos, int *x, int *y, int right_p)
27447 {
27448 int yb = window_text_bottom_y (w);
27449 struct glyph_row *r;
27450 struct glyph *best_glyph = NULL;
27451 struct glyph_row *best_row = NULL;
27452 int best_x = 0;
27453
27454 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27455 r->enabled_p && r->y < yb;
27456 ++r)
27457 {
27458 struct glyph *g = r->glyphs[TEXT_AREA];
27459 struct glyph *e = g + r->used[TEXT_AREA];
27460 int gx;
27461
27462 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27463 if (EQ (g->object, object))
27464 {
27465 if (g->charpos == pos)
27466 {
27467 best_glyph = g;
27468 best_x = gx;
27469 best_row = r;
27470 goto found;
27471 }
27472 else if (best_glyph == NULL
27473 || ((eabs (g->charpos - pos)
27474 < eabs (best_glyph->charpos - pos))
27475 && (right_p
27476 ? g->charpos < pos
27477 : g->charpos > pos)))
27478 {
27479 best_glyph = g;
27480 best_x = gx;
27481 best_row = r;
27482 }
27483 }
27484 }
27485
27486 found:
27487
27488 if (best_glyph)
27489 {
27490 *x = best_x;
27491 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27492
27493 if (right_p)
27494 {
27495 *x += best_glyph->pixel_width;
27496 ++*hpos;
27497 }
27498
27499 *y = best_row->y;
27500 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27501 }
27502
27503 return best_glyph != NULL;
27504 }
27505 #endif /* not used */
27506
27507 /* Find the positions of the first and the last glyphs in window W's
27508 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27509 (assumed to be a string), and return in HLINFO's mouse_face_*
27510 members the pixel and column/row coordinates of those glyphs. */
27511
27512 static void
27513 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27514 Lisp_Object object,
27515 ptrdiff_t startpos, ptrdiff_t endpos)
27516 {
27517 int yb = window_text_bottom_y (w);
27518 struct glyph_row *r;
27519 struct glyph *g, *e;
27520 int gx;
27521 int found = 0;
27522
27523 /* Find the glyph row with at least one position in the range
27524 [STARTPOS..ENDPOS), and the first glyph in that row whose
27525 position belongs to that range. */
27526 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27527 r->enabled_p && r->y < yb;
27528 ++r)
27529 {
27530 if (!r->reversed_p)
27531 {
27532 g = r->glyphs[TEXT_AREA];
27533 e = g + r->used[TEXT_AREA];
27534 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27535 if (EQ (g->object, object)
27536 && startpos <= g->charpos && g->charpos < endpos)
27537 {
27538 hlinfo->mouse_face_beg_row
27539 = MATRIX_ROW_VPOS (r, w->current_matrix);
27540 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27541 hlinfo->mouse_face_beg_x = gx;
27542 found = 1;
27543 break;
27544 }
27545 }
27546 else
27547 {
27548 struct glyph *g1;
27549
27550 e = r->glyphs[TEXT_AREA];
27551 g = e + r->used[TEXT_AREA];
27552 for ( ; g > e; --g)
27553 if (EQ ((g-1)->object, object)
27554 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27555 {
27556 hlinfo->mouse_face_beg_row
27557 = MATRIX_ROW_VPOS (r, w->current_matrix);
27558 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27559 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27560 gx += g1->pixel_width;
27561 hlinfo->mouse_face_beg_x = gx;
27562 found = 1;
27563 break;
27564 }
27565 }
27566 if (found)
27567 break;
27568 }
27569
27570 if (!found)
27571 return;
27572
27573 /* Starting with the next row, look for the first row which does NOT
27574 include any glyphs whose positions are in the range. */
27575 for (++r; r->enabled_p && r->y < yb; ++r)
27576 {
27577 g = r->glyphs[TEXT_AREA];
27578 e = g + r->used[TEXT_AREA];
27579 found = 0;
27580 for ( ; g < e; ++g)
27581 if (EQ (g->object, object)
27582 && startpos <= g->charpos && g->charpos < endpos)
27583 {
27584 found = 1;
27585 break;
27586 }
27587 if (!found)
27588 break;
27589 }
27590
27591 /* The highlighted region ends on the previous row. */
27592 r--;
27593
27594 /* Set the end row. */
27595 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27596
27597 /* Compute and set the end column and the end column's horizontal
27598 pixel coordinate. */
27599 if (!r->reversed_p)
27600 {
27601 g = r->glyphs[TEXT_AREA];
27602 e = g + r->used[TEXT_AREA];
27603 for ( ; e > g; --e)
27604 if (EQ ((e-1)->object, object)
27605 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27606 break;
27607 hlinfo->mouse_face_end_col = e - g;
27608
27609 for (gx = r->x; g < e; ++g)
27610 gx += g->pixel_width;
27611 hlinfo->mouse_face_end_x = gx;
27612 }
27613 else
27614 {
27615 e = r->glyphs[TEXT_AREA];
27616 g = e + r->used[TEXT_AREA];
27617 for (gx = r->x ; e < g; ++e)
27618 {
27619 if (EQ (e->object, object)
27620 && startpos <= e->charpos && e->charpos < endpos)
27621 break;
27622 gx += e->pixel_width;
27623 }
27624 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27625 hlinfo->mouse_face_end_x = gx;
27626 }
27627 }
27628
27629 #ifdef HAVE_WINDOW_SYSTEM
27630
27631 /* See if position X, Y is within a hot-spot of an image. */
27632
27633 static int
27634 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27635 {
27636 if (!CONSP (hot_spot))
27637 return 0;
27638
27639 if (EQ (XCAR (hot_spot), Qrect))
27640 {
27641 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27642 Lisp_Object rect = XCDR (hot_spot);
27643 Lisp_Object tem;
27644 if (!CONSP (rect))
27645 return 0;
27646 if (!CONSP (XCAR (rect)))
27647 return 0;
27648 if (!CONSP (XCDR (rect)))
27649 return 0;
27650 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27651 return 0;
27652 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27653 return 0;
27654 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27655 return 0;
27656 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27657 return 0;
27658 return 1;
27659 }
27660 else if (EQ (XCAR (hot_spot), Qcircle))
27661 {
27662 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27663 Lisp_Object circ = XCDR (hot_spot);
27664 Lisp_Object lr, lx0, ly0;
27665 if (CONSP (circ)
27666 && CONSP (XCAR (circ))
27667 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27668 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27669 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27670 {
27671 double r = XFLOATINT (lr);
27672 double dx = XINT (lx0) - x;
27673 double dy = XINT (ly0) - y;
27674 return (dx * dx + dy * dy <= r * r);
27675 }
27676 }
27677 else if (EQ (XCAR (hot_spot), Qpoly))
27678 {
27679 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27680 if (VECTORP (XCDR (hot_spot)))
27681 {
27682 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27683 Lisp_Object *poly = v->u.contents;
27684 ptrdiff_t n = v->header.size;
27685 ptrdiff_t i;
27686 int inside = 0;
27687 Lisp_Object lx, ly;
27688 int x0, y0;
27689
27690 /* Need an even number of coordinates, and at least 3 edges. */
27691 if (n < 6 || n & 1)
27692 return 0;
27693
27694 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27695 If count is odd, we are inside polygon. Pixels on edges
27696 may or may not be included depending on actual geometry of the
27697 polygon. */
27698 if ((lx = poly[n-2], !INTEGERP (lx))
27699 || (ly = poly[n-1], !INTEGERP (lx)))
27700 return 0;
27701 x0 = XINT (lx), y0 = XINT (ly);
27702 for (i = 0; i < n; i += 2)
27703 {
27704 int x1 = x0, y1 = y0;
27705 if ((lx = poly[i], !INTEGERP (lx))
27706 || (ly = poly[i+1], !INTEGERP (ly)))
27707 return 0;
27708 x0 = XINT (lx), y0 = XINT (ly);
27709
27710 /* Does this segment cross the X line? */
27711 if (x0 >= x)
27712 {
27713 if (x1 >= x)
27714 continue;
27715 }
27716 else if (x1 < x)
27717 continue;
27718 if (y > y0 && y > y1)
27719 continue;
27720 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27721 inside = !inside;
27722 }
27723 return inside;
27724 }
27725 }
27726 return 0;
27727 }
27728
27729 Lisp_Object
27730 find_hot_spot (Lisp_Object map, int x, int y)
27731 {
27732 while (CONSP (map))
27733 {
27734 if (CONSP (XCAR (map))
27735 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27736 return XCAR (map);
27737 map = XCDR (map);
27738 }
27739
27740 return Qnil;
27741 }
27742
27743 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27744 3, 3, 0,
27745 doc: /* Lookup in image map MAP coordinates X and Y.
27746 An image map is an alist where each element has the format (AREA ID PLIST).
27747 An AREA is specified as either a rectangle, a circle, or a polygon:
27748 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27749 pixel coordinates of the upper left and bottom right corners.
27750 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27751 and the radius of the circle; r may be a float or integer.
27752 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27753 vector describes one corner in the polygon.
27754 Returns the alist element for the first matching AREA in MAP. */)
27755 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27756 {
27757 if (NILP (map))
27758 return Qnil;
27759
27760 CHECK_NUMBER (x);
27761 CHECK_NUMBER (y);
27762
27763 return find_hot_spot (map,
27764 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27765 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27766 }
27767
27768
27769 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27770 static void
27771 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27772 {
27773 /* Do not change cursor shape while dragging mouse. */
27774 if (!NILP (do_mouse_tracking))
27775 return;
27776
27777 if (!NILP (pointer))
27778 {
27779 if (EQ (pointer, Qarrow))
27780 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27781 else if (EQ (pointer, Qhand))
27782 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27783 else if (EQ (pointer, Qtext))
27784 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27785 else if (EQ (pointer, intern ("hdrag")))
27786 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27787 #ifdef HAVE_X_WINDOWS
27788 else if (EQ (pointer, intern ("vdrag")))
27789 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27790 #endif
27791 else if (EQ (pointer, intern ("hourglass")))
27792 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27793 else if (EQ (pointer, Qmodeline))
27794 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27795 else
27796 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27797 }
27798
27799 if (cursor != No_Cursor)
27800 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27801 }
27802
27803 #endif /* HAVE_WINDOW_SYSTEM */
27804
27805 /* Take proper action when mouse has moved to the mode or header line
27806 or marginal area AREA of window W, x-position X and y-position Y.
27807 X is relative to the start of the text display area of W, so the
27808 width of bitmap areas and scroll bars must be subtracted to get a
27809 position relative to the start of the mode line. */
27810
27811 static void
27812 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27813 enum window_part area)
27814 {
27815 struct window *w = XWINDOW (window);
27816 struct frame *f = XFRAME (w->frame);
27817 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27818 #ifdef HAVE_WINDOW_SYSTEM
27819 Display_Info *dpyinfo;
27820 #endif
27821 Cursor cursor = No_Cursor;
27822 Lisp_Object pointer = Qnil;
27823 int dx, dy, width, height;
27824 ptrdiff_t charpos;
27825 Lisp_Object string, object = Qnil;
27826 Lisp_Object pos IF_LINT (= Qnil), help;
27827
27828 Lisp_Object mouse_face;
27829 int original_x_pixel = x;
27830 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27831 struct glyph_row *row IF_LINT (= 0);
27832
27833 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27834 {
27835 int x0;
27836 struct glyph *end;
27837
27838 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27839 returns them in row/column units! */
27840 string = mode_line_string (w, area, &x, &y, &charpos,
27841 &object, &dx, &dy, &width, &height);
27842
27843 row = (area == ON_MODE_LINE
27844 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27845 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27846
27847 /* Find the glyph under the mouse pointer. */
27848 if (row->mode_line_p && row->enabled_p)
27849 {
27850 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27851 end = glyph + row->used[TEXT_AREA];
27852
27853 for (x0 = original_x_pixel;
27854 glyph < end && x0 >= glyph->pixel_width;
27855 ++glyph)
27856 x0 -= glyph->pixel_width;
27857
27858 if (glyph >= end)
27859 glyph = NULL;
27860 }
27861 }
27862 else
27863 {
27864 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27865 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27866 returns them in row/column units! */
27867 string = marginal_area_string (w, area, &x, &y, &charpos,
27868 &object, &dx, &dy, &width, &height);
27869 }
27870
27871 help = Qnil;
27872
27873 #ifdef HAVE_WINDOW_SYSTEM
27874 if (IMAGEP (object))
27875 {
27876 Lisp_Object image_map, hotspot;
27877 if ((image_map = Fplist_get (XCDR (object), QCmap),
27878 !NILP (image_map))
27879 && (hotspot = find_hot_spot (image_map, dx, dy),
27880 CONSP (hotspot))
27881 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27882 {
27883 Lisp_Object plist;
27884
27885 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27886 If so, we could look for mouse-enter, mouse-leave
27887 properties in PLIST (and do something...). */
27888 hotspot = XCDR (hotspot);
27889 if (CONSP (hotspot)
27890 && (plist = XCAR (hotspot), CONSP (plist)))
27891 {
27892 pointer = Fplist_get (plist, Qpointer);
27893 if (NILP (pointer))
27894 pointer = Qhand;
27895 help = Fplist_get (plist, Qhelp_echo);
27896 if (!NILP (help))
27897 {
27898 help_echo_string = help;
27899 XSETWINDOW (help_echo_window, w);
27900 help_echo_object = w->contents;
27901 help_echo_pos = charpos;
27902 }
27903 }
27904 }
27905 if (NILP (pointer))
27906 pointer = Fplist_get (XCDR (object), QCpointer);
27907 }
27908 #endif /* HAVE_WINDOW_SYSTEM */
27909
27910 if (STRINGP (string))
27911 pos = make_number (charpos);
27912
27913 /* Set the help text and mouse pointer. If the mouse is on a part
27914 of the mode line without any text (e.g. past the right edge of
27915 the mode line text), use the default help text and pointer. */
27916 if (STRINGP (string) || area == ON_MODE_LINE)
27917 {
27918 /* Arrange to display the help by setting the global variables
27919 help_echo_string, help_echo_object, and help_echo_pos. */
27920 if (NILP (help))
27921 {
27922 if (STRINGP (string))
27923 help = Fget_text_property (pos, Qhelp_echo, string);
27924
27925 if (!NILP (help))
27926 {
27927 help_echo_string = help;
27928 XSETWINDOW (help_echo_window, w);
27929 help_echo_object = string;
27930 help_echo_pos = charpos;
27931 }
27932 else if (area == ON_MODE_LINE)
27933 {
27934 Lisp_Object default_help
27935 = buffer_local_value_1 (Qmode_line_default_help_echo,
27936 w->contents);
27937
27938 if (STRINGP (default_help))
27939 {
27940 help_echo_string = default_help;
27941 XSETWINDOW (help_echo_window, w);
27942 help_echo_object = Qnil;
27943 help_echo_pos = -1;
27944 }
27945 }
27946 }
27947
27948 #ifdef HAVE_WINDOW_SYSTEM
27949 /* Change the mouse pointer according to what is under it. */
27950 if (FRAME_WINDOW_P (f))
27951 {
27952 dpyinfo = FRAME_DISPLAY_INFO (f);
27953 if (STRINGP (string))
27954 {
27955 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27956
27957 if (NILP (pointer))
27958 pointer = Fget_text_property (pos, Qpointer, string);
27959
27960 /* Change the mouse pointer according to what is under X/Y. */
27961 if (NILP (pointer)
27962 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27963 {
27964 Lisp_Object map;
27965 map = Fget_text_property (pos, Qlocal_map, string);
27966 if (!KEYMAPP (map))
27967 map = Fget_text_property (pos, Qkeymap, string);
27968 if (!KEYMAPP (map))
27969 cursor = dpyinfo->vertical_scroll_bar_cursor;
27970 }
27971 }
27972 else
27973 /* Default mode-line pointer. */
27974 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27975 }
27976 #endif
27977 }
27978
27979 /* Change the mouse face according to what is under X/Y. */
27980 if (STRINGP (string))
27981 {
27982 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27983 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27984 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27985 && glyph)
27986 {
27987 Lisp_Object b, e;
27988
27989 struct glyph * tmp_glyph;
27990
27991 int gpos;
27992 int gseq_length;
27993 int total_pixel_width;
27994 ptrdiff_t begpos, endpos, ignore;
27995
27996 int vpos, hpos;
27997
27998 b = Fprevious_single_property_change (make_number (charpos + 1),
27999 Qmouse_face, string, Qnil);
28000 if (NILP (b))
28001 begpos = 0;
28002 else
28003 begpos = XINT (b);
28004
28005 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28006 if (NILP (e))
28007 endpos = SCHARS (string);
28008 else
28009 endpos = XINT (e);
28010
28011 /* Calculate the glyph position GPOS of GLYPH in the
28012 displayed string, relative to the beginning of the
28013 highlighted part of the string.
28014
28015 Note: GPOS is different from CHARPOS. CHARPOS is the
28016 position of GLYPH in the internal string object. A mode
28017 line string format has structures which are converted to
28018 a flattened string by the Emacs Lisp interpreter. The
28019 internal string is an element of those structures. The
28020 displayed string is the flattened string. */
28021 tmp_glyph = row_start_glyph;
28022 while (tmp_glyph < glyph
28023 && (!(EQ (tmp_glyph->object, glyph->object)
28024 && begpos <= tmp_glyph->charpos
28025 && tmp_glyph->charpos < endpos)))
28026 tmp_glyph++;
28027 gpos = glyph - tmp_glyph;
28028
28029 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28030 the highlighted part of the displayed string to which
28031 GLYPH belongs. Note: GSEQ_LENGTH is different from
28032 SCHARS (STRING), because the latter returns the length of
28033 the internal string. */
28034 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28035 tmp_glyph > glyph
28036 && (!(EQ (tmp_glyph->object, glyph->object)
28037 && begpos <= tmp_glyph->charpos
28038 && tmp_glyph->charpos < endpos));
28039 tmp_glyph--)
28040 ;
28041 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28042
28043 /* Calculate the total pixel width of all the glyphs between
28044 the beginning of the highlighted area and GLYPH. */
28045 total_pixel_width = 0;
28046 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28047 total_pixel_width += tmp_glyph->pixel_width;
28048
28049 /* Pre calculation of re-rendering position. Note: X is in
28050 column units here, after the call to mode_line_string or
28051 marginal_area_string. */
28052 hpos = x - gpos;
28053 vpos = (area == ON_MODE_LINE
28054 ? (w->current_matrix)->nrows - 1
28055 : 0);
28056
28057 /* If GLYPH's position is included in the region that is
28058 already drawn in mouse face, we have nothing to do. */
28059 if ( EQ (window, hlinfo->mouse_face_window)
28060 && (!row->reversed_p
28061 ? (hlinfo->mouse_face_beg_col <= hpos
28062 && hpos < hlinfo->mouse_face_end_col)
28063 /* In R2L rows we swap BEG and END, see below. */
28064 : (hlinfo->mouse_face_end_col <= hpos
28065 && hpos < hlinfo->mouse_face_beg_col))
28066 && hlinfo->mouse_face_beg_row == vpos )
28067 return;
28068
28069 if (clear_mouse_face (hlinfo))
28070 cursor = No_Cursor;
28071
28072 if (!row->reversed_p)
28073 {
28074 hlinfo->mouse_face_beg_col = hpos;
28075 hlinfo->mouse_face_beg_x = original_x_pixel
28076 - (total_pixel_width + dx);
28077 hlinfo->mouse_face_end_col = hpos + gseq_length;
28078 hlinfo->mouse_face_end_x = 0;
28079 }
28080 else
28081 {
28082 /* In R2L rows, show_mouse_face expects BEG and END
28083 coordinates to be swapped. */
28084 hlinfo->mouse_face_end_col = hpos;
28085 hlinfo->mouse_face_end_x = original_x_pixel
28086 - (total_pixel_width + dx);
28087 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28088 hlinfo->mouse_face_beg_x = 0;
28089 }
28090
28091 hlinfo->mouse_face_beg_row = vpos;
28092 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28093 hlinfo->mouse_face_past_end = 0;
28094 hlinfo->mouse_face_window = window;
28095
28096 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28097 charpos,
28098 0, 0, 0,
28099 &ignore,
28100 glyph->face_id,
28101 1);
28102 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28103
28104 if (NILP (pointer))
28105 pointer = Qhand;
28106 }
28107 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28108 clear_mouse_face (hlinfo);
28109 }
28110 #ifdef HAVE_WINDOW_SYSTEM
28111 if (FRAME_WINDOW_P (f))
28112 define_frame_cursor1 (f, cursor, pointer);
28113 #endif
28114 }
28115
28116
28117 /* EXPORT:
28118 Take proper action when the mouse has moved to position X, Y on
28119 frame F with regards to highlighting portions of display that have
28120 mouse-face properties. Also de-highlight portions of display where
28121 the mouse was before, set the mouse pointer shape as appropriate
28122 for the mouse coordinates, and activate help echo (tooltips).
28123 X and Y can be negative or out of range. */
28124
28125 void
28126 note_mouse_highlight (struct frame *f, int x, int y)
28127 {
28128 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28129 enum window_part part = ON_NOTHING;
28130 Lisp_Object window;
28131 struct window *w;
28132 Cursor cursor = No_Cursor;
28133 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28134 struct buffer *b;
28135
28136 /* When a menu is active, don't highlight because this looks odd. */
28137 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28138 if (popup_activated ())
28139 return;
28140 #endif
28141
28142 if (!f->glyphs_initialized_p
28143 || f->pointer_invisible)
28144 return;
28145
28146 hlinfo->mouse_face_mouse_x = x;
28147 hlinfo->mouse_face_mouse_y = y;
28148 hlinfo->mouse_face_mouse_frame = f;
28149
28150 if (hlinfo->mouse_face_defer)
28151 return;
28152
28153 /* Which window is that in? */
28154 window = window_from_coordinates (f, x, y, &part, 1);
28155
28156 /* If displaying active text in another window, clear that. */
28157 if (! EQ (window, hlinfo->mouse_face_window)
28158 /* Also clear if we move out of text area in same window. */
28159 || (!NILP (hlinfo->mouse_face_window)
28160 && !NILP (window)
28161 && part != ON_TEXT
28162 && part != ON_MODE_LINE
28163 && part != ON_HEADER_LINE))
28164 clear_mouse_face (hlinfo);
28165
28166 /* Not on a window -> return. */
28167 if (!WINDOWP (window))
28168 return;
28169
28170 /* Reset help_echo_string. It will get recomputed below. */
28171 help_echo_string = Qnil;
28172
28173 /* Convert to window-relative pixel coordinates. */
28174 w = XWINDOW (window);
28175 frame_to_window_pixel_xy (w, &x, &y);
28176
28177 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28178 /* Handle tool-bar window differently since it doesn't display a
28179 buffer. */
28180 if (EQ (window, f->tool_bar_window))
28181 {
28182 note_tool_bar_highlight (f, x, y);
28183 return;
28184 }
28185 #endif
28186
28187 /* Mouse is on the mode, header line or margin? */
28188 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28189 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28190 {
28191 note_mode_line_or_margin_highlight (window, x, y, part);
28192 return;
28193 }
28194
28195 #ifdef HAVE_WINDOW_SYSTEM
28196 if (part == ON_VERTICAL_BORDER)
28197 {
28198 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28199 help_echo_string = build_string ("drag-mouse-1: resize");
28200 }
28201 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28202 || part == ON_SCROLL_BAR)
28203 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28204 else
28205 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28206 #endif
28207
28208 /* Are we in a window whose display is up to date?
28209 And verify the buffer's text has not changed. */
28210 b = XBUFFER (w->contents);
28211 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28212 {
28213 int hpos, vpos, dx, dy, area = LAST_AREA;
28214 ptrdiff_t pos;
28215 struct glyph *glyph;
28216 Lisp_Object object;
28217 Lisp_Object mouse_face = Qnil, position;
28218 Lisp_Object *overlay_vec = NULL;
28219 ptrdiff_t i, noverlays;
28220 struct buffer *obuf;
28221 ptrdiff_t obegv, ozv;
28222 int same_region;
28223
28224 /* Find the glyph under X/Y. */
28225 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28226
28227 #ifdef HAVE_WINDOW_SYSTEM
28228 /* Look for :pointer property on image. */
28229 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28230 {
28231 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28232 if (img != NULL && IMAGEP (img->spec))
28233 {
28234 Lisp_Object image_map, hotspot;
28235 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28236 !NILP (image_map))
28237 && (hotspot = find_hot_spot (image_map,
28238 glyph->slice.img.x + dx,
28239 glyph->slice.img.y + dy),
28240 CONSP (hotspot))
28241 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28242 {
28243 Lisp_Object plist;
28244
28245 /* Could check XCAR (hotspot) to see if we enter/leave
28246 this hot-spot.
28247 If so, we could look for mouse-enter, mouse-leave
28248 properties in PLIST (and do something...). */
28249 hotspot = XCDR (hotspot);
28250 if (CONSP (hotspot)
28251 && (plist = XCAR (hotspot), CONSP (plist)))
28252 {
28253 pointer = Fplist_get (plist, Qpointer);
28254 if (NILP (pointer))
28255 pointer = Qhand;
28256 help_echo_string = Fplist_get (plist, Qhelp_echo);
28257 if (!NILP (help_echo_string))
28258 {
28259 help_echo_window = window;
28260 help_echo_object = glyph->object;
28261 help_echo_pos = glyph->charpos;
28262 }
28263 }
28264 }
28265 if (NILP (pointer))
28266 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28267 }
28268 }
28269 #endif /* HAVE_WINDOW_SYSTEM */
28270
28271 /* Clear mouse face if X/Y not over text. */
28272 if (glyph == NULL
28273 || area != TEXT_AREA
28274 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28275 /* Glyph's OBJECT is an integer for glyphs inserted by the
28276 display engine for its internal purposes, like truncation
28277 and continuation glyphs and blanks beyond the end of
28278 line's text on text terminals. If we are over such a
28279 glyph, we are not over any text. */
28280 || INTEGERP (glyph->object)
28281 /* R2L rows have a stretch glyph at their front, which
28282 stands for no text, whereas L2R rows have no glyphs at
28283 all beyond the end of text. Treat such stretch glyphs
28284 like we do with NULL glyphs in L2R rows. */
28285 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28286 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28287 && glyph->type == STRETCH_GLYPH
28288 && glyph->avoid_cursor_p))
28289 {
28290 if (clear_mouse_face (hlinfo))
28291 cursor = No_Cursor;
28292 #ifdef HAVE_WINDOW_SYSTEM
28293 if (FRAME_WINDOW_P (f) && NILP (pointer))
28294 {
28295 if (area != TEXT_AREA)
28296 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28297 else
28298 pointer = Vvoid_text_area_pointer;
28299 }
28300 #endif
28301 goto set_cursor;
28302 }
28303
28304 pos = glyph->charpos;
28305 object = glyph->object;
28306 if (!STRINGP (object) && !BUFFERP (object))
28307 goto set_cursor;
28308
28309 /* If we get an out-of-range value, return now; avoid an error. */
28310 if (BUFFERP (object) && pos > BUF_Z (b))
28311 goto set_cursor;
28312
28313 /* Make the window's buffer temporarily current for
28314 overlays_at and compute_char_face. */
28315 obuf = current_buffer;
28316 current_buffer = b;
28317 obegv = BEGV;
28318 ozv = ZV;
28319 BEGV = BEG;
28320 ZV = Z;
28321
28322 /* Is this char mouse-active or does it have help-echo? */
28323 position = make_number (pos);
28324
28325 if (BUFFERP (object))
28326 {
28327 /* Put all the overlays we want in a vector in overlay_vec. */
28328 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28329 /* Sort overlays into increasing priority order. */
28330 noverlays = sort_overlays (overlay_vec, noverlays, w);
28331 }
28332 else
28333 noverlays = 0;
28334
28335 if (NILP (Vmouse_highlight))
28336 {
28337 clear_mouse_face (hlinfo);
28338 goto check_help_echo;
28339 }
28340
28341 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28342
28343 if (same_region)
28344 cursor = No_Cursor;
28345
28346 /* Check mouse-face highlighting. */
28347 if (! same_region
28348 /* If there exists an overlay with mouse-face overlapping
28349 the one we are currently highlighting, we have to
28350 check if we enter the overlapping overlay, and then
28351 highlight only that. */
28352 || (OVERLAYP (hlinfo->mouse_face_overlay)
28353 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28354 {
28355 /* Find the highest priority overlay with a mouse-face. */
28356 Lisp_Object overlay = Qnil;
28357 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28358 {
28359 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28360 if (!NILP (mouse_face))
28361 overlay = overlay_vec[i];
28362 }
28363
28364 /* If we're highlighting the same overlay as before, there's
28365 no need to do that again. */
28366 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28367 goto check_help_echo;
28368 hlinfo->mouse_face_overlay = overlay;
28369
28370 /* Clear the display of the old active region, if any. */
28371 if (clear_mouse_face (hlinfo))
28372 cursor = No_Cursor;
28373
28374 /* If no overlay applies, get a text property. */
28375 if (NILP (overlay))
28376 mouse_face = Fget_text_property (position, Qmouse_face, object);
28377
28378 /* Next, compute the bounds of the mouse highlighting and
28379 display it. */
28380 if (!NILP (mouse_face) && STRINGP (object))
28381 {
28382 /* The mouse-highlighting comes from a display string
28383 with a mouse-face. */
28384 Lisp_Object s, e;
28385 ptrdiff_t ignore;
28386
28387 s = Fprevious_single_property_change
28388 (make_number (pos + 1), Qmouse_face, object, Qnil);
28389 e = Fnext_single_property_change
28390 (position, Qmouse_face, object, Qnil);
28391 if (NILP (s))
28392 s = make_number (0);
28393 if (NILP (e))
28394 e = make_number (SCHARS (object));
28395 mouse_face_from_string_pos (w, hlinfo, object,
28396 XINT (s), XINT (e));
28397 hlinfo->mouse_face_past_end = 0;
28398 hlinfo->mouse_face_window = window;
28399 hlinfo->mouse_face_face_id
28400 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28401 glyph->face_id, 1);
28402 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28403 cursor = No_Cursor;
28404 }
28405 else
28406 {
28407 /* The mouse-highlighting, if any, comes from an overlay
28408 or text property in the buffer. */
28409 Lisp_Object buffer IF_LINT (= Qnil);
28410 Lisp_Object disp_string IF_LINT (= Qnil);
28411
28412 if (STRINGP (object))
28413 {
28414 /* If we are on a display string with no mouse-face,
28415 check if the text under it has one. */
28416 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28417 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28418 pos = string_buffer_position (object, start);
28419 if (pos > 0)
28420 {
28421 mouse_face = get_char_property_and_overlay
28422 (make_number (pos), Qmouse_face, w->contents, &overlay);
28423 buffer = w->contents;
28424 disp_string = object;
28425 }
28426 }
28427 else
28428 {
28429 buffer = object;
28430 disp_string = Qnil;
28431 }
28432
28433 if (!NILP (mouse_face))
28434 {
28435 Lisp_Object before, after;
28436 Lisp_Object before_string, after_string;
28437 /* To correctly find the limits of mouse highlight
28438 in a bidi-reordered buffer, we must not use the
28439 optimization of limiting the search in
28440 previous-single-property-change and
28441 next-single-property-change, because
28442 rows_from_pos_range needs the real start and end
28443 positions to DTRT in this case. That's because
28444 the first row visible in a window does not
28445 necessarily display the character whose position
28446 is the smallest. */
28447 Lisp_Object lim1 =
28448 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28449 ? Fmarker_position (w->start)
28450 : Qnil;
28451 Lisp_Object lim2 =
28452 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28453 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28454 : Qnil;
28455
28456 if (NILP (overlay))
28457 {
28458 /* Handle the text property case. */
28459 before = Fprevious_single_property_change
28460 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28461 after = Fnext_single_property_change
28462 (make_number (pos), Qmouse_face, buffer, lim2);
28463 before_string = after_string = Qnil;
28464 }
28465 else
28466 {
28467 /* Handle the overlay case. */
28468 before = Foverlay_start (overlay);
28469 after = Foverlay_end (overlay);
28470 before_string = Foverlay_get (overlay, Qbefore_string);
28471 after_string = Foverlay_get (overlay, Qafter_string);
28472
28473 if (!STRINGP (before_string)) before_string = Qnil;
28474 if (!STRINGP (after_string)) after_string = Qnil;
28475 }
28476
28477 mouse_face_from_buffer_pos (window, hlinfo, pos,
28478 NILP (before)
28479 ? 1
28480 : XFASTINT (before),
28481 NILP (after)
28482 ? BUF_Z (XBUFFER (buffer))
28483 : XFASTINT (after),
28484 before_string, after_string,
28485 disp_string);
28486 cursor = No_Cursor;
28487 }
28488 }
28489 }
28490
28491 check_help_echo:
28492
28493 /* Look for a `help-echo' property. */
28494 if (NILP (help_echo_string)) {
28495 Lisp_Object help, overlay;
28496
28497 /* Check overlays first. */
28498 help = overlay = Qnil;
28499 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28500 {
28501 overlay = overlay_vec[i];
28502 help = Foverlay_get (overlay, Qhelp_echo);
28503 }
28504
28505 if (!NILP (help))
28506 {
28507 help_echo_string = help;
28508 help_echo_window = window;
28509 help_echo_object = overlay;
28510 help_echo_pos = pos;
28511 }
28512 else
28513 {
28514 Lisp_Object obj = glyph->object;
28515 ptrdiff_t charpos = glyph->charpos;
28516
28517 /* Try text properties. */
28518 if (STRINGP (obj)
28519 && charpos >= 0
28520 && charpos < SCHARS (obj))
28521 {
28522 help = Fget_text_property (make_number (charpos),
28523 Qhelp_echo, obj);
28524 if (NILP (help))
28525 {
28526 /* If the string itself doesn't specify a help-echo,
28527 see if the buffer text ``under'' it does. */
28528 struct glyph_row *r
28529 = MATRIX_ROW (w->current_matrix, vpos);
28530 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28531 ptrdiff_t p = string_buffer_position (obj, start);
28532 if (p > 0)
28533 {
28534 help = Fget_char_property (make_number (p),
28535 Qhelp_echo, w->contents);
28536 if (!NILP (help))
28537 {
28538 charpos = p;
28539 obj = w->contents;
28540 }
28541 }
28542 }
28543 }
28544 else if (BUFFERP (obj)
28545 && charpos >= BEGV
28546 && charpos < ZV)
28547 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28548 obj);
28549
28550 if (!NILP (help))
28551 {
28552 help_echo_string = help;
28553 help_echo_window = window;
28554 help_echo_object = obj;
28555 help_echo_pos = charpos;
28556 }
28557 }
28558 }
28559
28560 #ifdef HAVE_WINDOW_SYSTEM
28561 /* Look for a `pointer' property. */
28562 if (FRAME_WINDOW_P (f) && NILP (pointer))
28563 {
28564 /* Check overlays first. */
28565 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28566 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28567
28568 if (NILP (pointer))
28569 {
28570 Lisp_Object obj = glyph->object;
28571 ptrdiff_t charpos = glyph->charpos;
28572
28573 /* Try text properties. */
28574 if (STRINGP (obj)
28575 && charpos >= 0
28576 && charpos < SCHARS (obj))
28577 {
28578 pointer = Fget_text_property (make_number (charpos),
28579 Qpointer, obj);
28580 if (NILP (pointer))
28581 {
28582 /* If the string itself doesn't specify a pointer,
28583 see if the buffer text ``under'' it does. */
28584 struct glyph_row *r
28585 = MATRIX_ROW (w->current_matrix, vpos);
28586 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28587 ptrdiff_t p = string_buffer_position (obj, start);
28588 if (p > 0)
28589 pointer = Fget_char_property (make_number (p),
28590 Qpointer, w->contents);
28591 }
28592 }
28593 else if (BUFFERP (obj)
28594 && charpos >= BEGV
28595 && charpos < ZV)
28596 pointer = Fget_text_property (make_number (charpos),
28597 Qpointer, obj);
28598 }
28599 }
28600 #endif /* HAVE_WINDOW_SYSTEM */
28601
28602 BEGV = obegv;
28603 ZV = ozv;
28604 current_buffer = obuf;
28605 }
28606
28607 set_cursor:
28608
28609 #ifdef HAVE_WINDOW_SYSTEM
28610 if (FRAME_WINDOW_P (f))
28611 define_frame_cursor1 (f, cursor, pointer);
28612 #else
28613 /* This is here to prevent a compiler error, about "label at end of
28614 compound statement". */
28615 return;
28616 #endif
28617 }
28618
28619
28620 /* EXPORT for RIF:
28621 Clear any mouse-face on window W. This function is part of the
28622 redisplay interface, and is called from try_window_id and similar
28623 functions to ensure the mouse-highlight is off. */
28624
28625 void
28626 x_clear_window_mouse_face (struct window *w)
28627 {
28628 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28629 Lisp_Object window;
28630
28631 block_input ();
28632 XSETWINDOW (window, w);
28633 if (EQ (window, hlinfo->mouse_face_window))
28634 clear_mouse_face (hlinfo);
28635 unblock_input ();
28636 }
28637
28638
28639 /* EXPORT:
28640 Just discard the mouse face information for frame F, if any.
28641 This is used when the size of F is changed. */
28642
28643 void
28644 cancel_mouse_face (struct frame *f)
28645 {
28646 Lisp_Object window;
28647 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28648
28649 window = hlinfo->mouse_face_window;
28650 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28651 reset_mouse_highlight (hlinfo);
28652 }
28653
28654
28655 \f
28656 /***********************************************************************
28657 Exposure Events
28658 ***********************************************************************/
28659
28660 #ifdef HAVE_WINDOW_SYSTEM
28661
28662 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28663 which intersects rectangle R. R is in window-relative coordinates. */
28664
28665 static void
28666 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28667 enum glyph_row_area area)
28668 {
28669 struct glyph *first = row->glyphs[area];
28670 struct glyph *end = row->glyphs[area] + row->used[area];
28671 struct glyph *last;
28672 int first_x, start_x, x;
28673
28674 if (area == TEXT_AREA && row->fill_line_p)
28675 /* If row extends face to end of line write the whole line. */
28676 draw_glyphs (w, 0, row, area,
28677 0, row->used[area],
28678 DRAW_NORMAL_TEXT, 0);
28679 else
28680 {
28681 /* Set START_X to the window-relative start position for drawing glyphs of
28682 AREA. The first glyph of the text area can be partially visible.
28683 The first glyphs of other areas cannot. */
28684 start_x = window_box_left_offset (w, area);
28685 x = start_x;
28686 if (area == TEXT_AREA)
28687 x += row->x;
28688
28689 /* Find the first glyph that must be redrawn. */
28690 while (first < end
28691 && x + first->pixel_width < r->x)
28692 {
28693 x += first->pixel_width;
28694 ++first;
28695 }
28696
28697 /* Find the last one. */
28698 last = first;
28699 first_x = x;
28700 while (last < end
28701 && x < r->x + r->width)
28702 {
28703 x += last->pixel_width;
28704 ++last;
28705 }
28706
28707 /* Repaint. */
28708 if (last > first)
28709 draw_glyphs (w, first_x - start_x, row, area,
28710 first - row->glyphs[area], last - row->glyphs[area],
28711 DRAW_NORMAL_TEXT, 0);
28712 }
28713 }
28714
28715
28716 /* Redraw the parts of the glyph row ROW on window W intersecting
28717 rectangle R. R is in window-relative coordinates. Value is
28718 non-zero if mouse-face was overwritten. */
28719
28720 static int
28721 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28722 {
28723 eassert (row->enabled_p);
28724
28725 if (row->mode_line_p || w->pseudo_window_p)
28726 draw_glyphs (w, 0, row, TEXT_AREA,
28727 0, row->used[TEXT_AREA],
28728 DRAW_NORMAL_TEXT, 0);
28729 else
28730 {
28731 if (row->used[LEFT_MARGIN_AREA])
28732 expose_area (w, row, r, LEFT_MARGIN_AREA);
28733 if (row->used[TEXT_AREA])
28734 expose_area (w, row, r, TEXT_AREA);
28735 if (row->used[RIGHT_MARGIN_AREA])
28736 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28737 draw_row_fringe_bitmaps (w, row);
28738 }
28739
28740 return row->mouse_face_p;
28741 }
28742
28743
28744 /* Redraw those parts of glyphs rows during expose event handling that
28745 overlap other rows. Redrawing of an exposed line writes over parts
28746 of lines overlapping that exposed line; this function fixes that.
28747
28748 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28749 row in W's current matrix that is exposed and overlaps other rows.
28750 LAST_OVERLAPPING_ROW is the last such row. */
28751
28752 static void
28753 expose_overlaps (struct window *w,
28754 struct glyph_row *first_overlapping_row,
28755 struct glyph_row *last_overlapping_row,
28756 XRectangle *r)
28757 {
28758 struct glyph_row *row;
28759
28760 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28761 if (row->overlapping_p)
28762 {
28763 eassert (row->enabled_p && !row->mode_line_p);
28764
28765 row->clip = r;
28766 if (row->used[LEFT_MARGIN_AREA])
28767 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28768
28769 if (row->used[TEXT_AREA])
28770 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28771
28772 if (row->used[RIGHT_MARGIN_AREA])
28773 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28774 row->clip = NULL;
28775 }
28776 }
28777
28778
28779 /* Return non-zero if W's cursor intersects rectangle R. */
28780
28781 static int
28782 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28783 {
28784 XRectangle cr, result;
28785 struct glyph *cursor_glyph;
28786 struct glyph_row *row;
28787
28788 if (w->phys_cursor.vpos >= 0
28789 && w->phys_cursor.vpos < w->current_matrix->nrows
28790 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28791 row->enabled_p)
28792 && row->cursor_in_fringe_p)
28793 {
28794 /* Cursor is in the fringe. */
28795 cr.x = window_box_right_offset (w,
28796 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28797 ? RIGHT_MARGIN_AREA
28798 : TEXT_AREA));
28799 cr.y = row->y;
28800 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28801 cr.height = row->height;
28802 return x_intersect_rectangles (&cr, r, &result);
28803 }
28804
28805 cursor_glyph = get_phys_cursor_glyph (w);
28806 if (cursor_glyph)
28807 {
28808 /* r is relative to W's box, but w->phys_cursor.x is relative
28809 to left edge of W's TEXT area. Adjust it. */
28810 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28811 cr.y = w->phys_cursor.y;
28812 cr.width = cursor_glyph->pixel_width;
28813 cr.height = w->phys_cursor_height;
28814 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28815 I assume the effect is the same -- and this is portable. */
28816 return x_intersect_rectangles (&cr, r, &result);
28817 }
28818 /* If we don't understand the format, pretend we're not in the hot-spot. */
28819 return 0;
28820 }
28821
28822
28823 /* EXPORT:
28824 Draw a vertical window border to the right of window W if W doesn't
28825 have vertical scroll bars. */
28826
28827 void
28828 x_draw_vertical_border (struct window *w)
28829 {
28830 struct frame *f = XFRAME (WINDOW_FRAME (w));
28831
28832 /* We could do better, if we knew what type of scroll-bar the adjacent
28833 windows (on either side) have... But we don't :-(
28834 However, I think this works ok. ++KFS 2003-04-25 */
28835
28836 /* Redraw borders between horizontally adjacent windows. Don't
28837 do it for frames with vertical scroll bars because either the
28838 right scroll bar of a window, or the left scroll bar of its
28839 neighbor will suffice as a border. */
28840 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28841 return;
28842
28843 /* Note: It is necessary to redraw both the left and the right
28844 borders, for when only this single window W is being
28845 redisplayed. */
28846 if (!WINDOW_RIGHTMOST_P (w)
28847 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28848 {
28849 int x0, x1, y0, y1;
28850
28851 window_box_edges (w, &x0, &y0, &x1, &y1);
28852 y1 -= 1;
28853
28854 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28855 x1 -= 1;
28856
28857 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28858 }
28859 if (!WINDOW_LEFTMOST_P (w)
28860 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28861 {
28862 int x0, x1, y0, y1;
28863
28864 window_box_edges (w, &x0, &y0, &x1, &y1);
28865 y1 -= 1;
28866
28867 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28868 x0 -= 1;
28869
28870 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28871 }
28872 }
28873
28874
28875 /* Redraw the part of window W intersection rectangle FR. Pixel
28876 coordinates in FR are frame-relative. Call this function with
28877 input blocked. Value is non-zero if the exposure overwrites
28878 mouse-face. */
28879
28880 static int
28881 expose_window (struct window *w, XRectangle *fr)
28882 {
28883 struct frame *f = XFRAME (w->frame);
28884 XRectangle wr, r;
28885 int mouse_face_overwritten_p = 0;
28886
28887 /* If window is not yet fully initialized, do nothing. This can
28888 happen when toolkit scroll bars are used and a window is split.
28889 Reconfiguring the scroll bar will generate an expose for a newly
28890 created window. */
28891 if (w->current_matrix == NULL)
28892 return 0;
28893
28894 /* When we're currently updating the window, display and current
28895 matrix usually don't agree. Arrange for a thorough display
28896 later. */
28897 if (w->must_be_updated_p)
28898 {
28899 SET_FRAME_GARBAGED (f);
28900 return 0;
28901 }
28902
28903 /* Frame-relative pixel rectangle of W. */
28904 wr.x = WINDOW_LEFT_EDGE_X (w);
28905 wr.y = WINDOW_TOP_EDGE_Y (w);
28906 wr.width = WINDOW_TOTAL_WIDTH (w);
28907 wr.height = WINDOW_TOTAL_HEIGHT (w);
28908
28909 if (x_intersect_rectangles (fr, &wr, &r))
28910 {
28911 int yb = window_text_bottom_y (w);
28912 struct glyph_row *row;
28913 int cursor_cleared_p, phys_cursor_on_p;
28914 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28915
28916 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28917 r.x, r.y, r.width, r.height));
28918
28919 /* Convert to window coordinates. */
28920 r.x -= WINDOW_LEFT_EDGE_X (w);
28921 r.y -= WINDOW_TOP_EDGE_Y (w);
28922
28923 /* Turn off the cursor. */
28924 if (!w->pseudo_window_p
28925 && phys_cursor_in_rect_p (w, &r))
28926 {
28927 x_clear_cursor (w);
28928 cursor_cleared_p = 1;
28929 }
28930 else
28931 cursor_cleared_p = 0;
28932
28933 /* If the row containing the cursor extends face to end of line,
28934 then expose_area might overwrite the cursor outside the
28935 rectangle and thus notice_overwritten_cursor might clear
28936 w->phys_cursor_on_p. We remember the original value and
28937 check later if it is changed. */
28938 phys_cursor_on_p = w->phys_cursor_on_p;
28939
28940 /* Update lines intersecting rectangle R. */
28941 first_overlapping_row = last_overlapping_row = NULL;
28942 for (row = w->current_matrix->rows;
28943 row->enabled_p;
28944 ++row)
28945 {
28946 int y0 = row->y;
28947 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28948
28949 if ((y0 >= r.y && y0 < r.y + r.height)
28950 || (y1 > r.y && y1 < r.y + r.height)
28951 || (r.y >= y0 && r.y < y1)
28952 || (r.y + r.height > y0 && r.y + r.height < y1))
28953 {
28954 /* A header line may be overlapping, but there is no need
28955 to fix overlapping areas for them. KFS 2005-02-12 */
28956 if (row->overlapping_p && !row->mode_line_p)
28957 {
28958 if (first_overlapping_row == NULL)
28959 first_overlapping_row = row;
28960 last_overlapping_row = row;
28961 }
28962
28963 row->clip = fr;
28964 if (expose_line (w, row, &r))
28965 mouse_face_overwritten_p = 1;
28966 row->clip = NULL;
28967 }
28968 else if (row->overlapping_p)
28969 {
28970 /* We must redraw a row overlapping the exposed area. */
28971 if (y0 < r.y
28972 ? y0 + row->phys_height > r.y
28973 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28974 {
28975 if (first_overlapping_row == NULL)
28976 first_overlapping_row = row;
28977 last_overlapping_row = row;
28978 }
28979 }
28980
28981 if (y1 >= yb)
28982 break;
28983 }
28984
28985 /* Display the mode line if there is one. */
28986 if (WINDOW_WANTS_MODELINE_P (w)
28987 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28988 row->enabled_p)
28989 && row->y < r.y + r.height)
28990 {
28991 if (expose_line (w, row, &r))
28992 mouse_face_overwritten_p = 1;
28993 }
28994
28995 if (!w->pseudo_window_p)
28996 {
28997 /* Fix the display of overlapping rows. */
28998 if (first_overlapping_row)
28999 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29000 fr);
29001
29002 /* Draw border between windows. */
29003 x_draw_vertical_border (w);
29004
29005 /* Turn the cursor on again. */
29006 if (cursor_cleared_p
29007 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29008 update_window_cursor (w, 1);
29009 }
29010 }
29011
29012 return mouse_face_overwritten_p;
29013 }
29014
29015
29016
29017 /* Redraw (parts) of all windows in the window tree rooted at W that
29018 intersect R. R contains frame pixel coordinates. Value is
29019 non-zero if the exposure overwrites mouse-face. */
29020
29021 static int
29022 expose_window_tree (struct window *w, XRectangle *r)
29023 {
29024 struct frame *f = XFRAME (w->frame);
29025 int mouse_face_overwritten_p = 0;
29026
29027 while (w && !FRAME_GARBAGED_P (f))
29028 {
29029 if (WINDOWP (w->contents))
29030 mouse_face_overwritten_p
29031 |= expose_window_tree (XWINDOW (w->contents), r);
29032 else
29033 mouse_face_overwritten_p |= expose_window (w, r);
29034
29035 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29036 }
29037
29038 return mouse_face_overwritten_p;
29039 }
29040
29041
29042 /* EXPORT:
29043 Redisplay an exposed area of frame F. X and Y are the upper-left
29044 corner of the exposed rectangle. W and H are width and height of
29045 the exposed area. All are pixel values. W or H zero means redraw
29046 the entire frame. */
29047
29048 void
29049 expose_frame (struct frame *f, int x, int y, int w, int h)
29050 {
29051 XRectangle r;
29052 int mouse_face_overwritten_p = 0;
29053
29054 TRACE ((stderr, "expose_frame "));
29055
29056 /* No need to redraw if frame will be redrawn soon. */
29057 if (FRAME_GARBAGED_P (f))
29058 {
29059 TRACE ((stderr, " garbaged\n"));
29060 return;
29061 }
29062
29063 /* If basic faces haven't been realized yet, there is no point in
29064 trying to redraw anything. This can happen when we get an expose
29065 event while Emacs is starting, e.g. by moving another window. */
29066 if (FRAME_FACE_CACHE (f) == NULL
29067 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29068 {
29069 TRACE ((stderr, " no faces\n"));
29070 return;
29071 }
29072
29073 if (w == 0 || h == 0)
29074 {
29075 r.x = r.y = 0;
29076 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29077 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29078 }
29079 else
29080 {
29081 r.x = x;
29082 r.y = y;
29083 r.width = w;
29084 r.height = h;
29085 }
29086
29087 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29088 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29089
29090 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29091 if (WINDOWP (f->tool_bar_window))
29092 mouse_face_overwritten_p
29093 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29094 #endif
29095
29096 #ifdef HAVE_X_WINDOWS
29097 #ifndef MSDOS
29098 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29099 if (WINDOWP (f->menu_bar_window))
29100 mouse_face_overwritten_p
29101 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29102 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29103 #endif
29104 #endif
29105
29106 /* Some window managers support a focus-follows-mouse style with
29107 delayed raising of frames. Imagine a partially obscured frame,
29108 and moving the mouse into partially obscured mouse-face on that
29109 frame. The visible part of the mouse-face will be highlighted,
29110 then the WM raises the obscured frame. With at least one WM, KDE
29111 2.1, Emacs is not getting any event for the raising of the frame
29112 (even tried with SubstructureRedirectMask), only Expose events.
29113 These expose events will draw text normally, i.e. not
29114 highlighted. Which means we must redo the highlight here.
29115 Subsume it under ``we love X''. --gerd 2001-08-15 */
29116 /* Included in Windows version because Windows most likely does not
29117 do the right thing if any third party tool offers
29118 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29119 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29120 {
29121 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29122 if (f == hlinfo->mouse_face_mouse_frame)
29123 {
29124 int mouse_x = hlinfo->mouse_face_mouse_x;
29125 int mouse_y = hlinfo->mouse_face_mouse_y;
29126 clear_mouse_face (hlinfo);
29127 note_mouse_highlight (f, mouse_x, mouse_y);
29128 }
29129 }
29130 }
29131
29132
29133 /* EXPORT:
29134 Determine the intersection of two rectangles R1 and R2. Return
29135 the intersection in *RESULT. Value is non-zero if RESULT is not
29136 empty. */
29137
29138 int
29139 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29140 {
29141 XRectangle *left, *right;
29142 XRectangle *upper, *lower;
29143 int intersection_p = 0;
29144
29145 /* Rearrange so that R1 is the left-most rectangle. */
29146 if (r1->x < r2->x)
29147 left = r1, right = r2;
29148 else
29149 left = r2, right = r1;
29150
29151 /* X0 of the intersection is right.x0, if this is inside R1,
29152 otherwise there is no intersection. */
29153 if (right->x <= left->x + left->width)
29154 {
29155 result->x = right->x;
29156
29157 /* The right end of the intersection is the minimum of
29158 the right ends of left and right. */
29159 result->width = (min (left->x + left->width, right->x + right->width)
29160 - result->x);
29161
29162 /* Same game for Y. */
29163 if (r1->y < r2->y)
29164 upper = r1, lower = r2;
29165 else
29166 upper = r2, lower = r1;
29167
29168 /* The upper end of the intersection is lower.y0, if this is inside
29169 of upper. Otherwise, there is no intersection. */
29170 if (lower->y <= upper->y + upper->height)
29171 {
29172 result->y = lower->y;
29173
29174 /* The lower end of the intersection is the minimum of the lower
29175 ends of upper and lower. */
29176 result->height = (min (lower->y + lower->height,
29177 upper->y + upper->height)
29178 - result->y);
29179 intersection_p = 1;
29180 }
29181 }
29182
29183 return intersection_p;
29184 }
29185
29186 #endif /* HAVE_WINDOW_SYSTEM */
29187
29188 \f
29189 /***********************************************************************
29190 Initialization
29191 ***********************************************************************/
29192
29193 void
29194 syms_of_xdisp (void)
29195 {
29196 Vwith_echo_area_save_vector = Qnil;
29197 staticpro (&Vwith_echo_area_save_vector);
29198
29199 Vmessage_stack = Qnil;
29200 staticpro (&Vmessage_stack);
29201
29202 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29203 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29204
29205 message_dolog_marker1 = Fmake_marker ();
29206 staticpro (&message_dolog_marker1);
29207 message_dolog_marker2 = Fmake_marker ();
29208 staticpro (&message_dolog_marker2);
29209 message_dolog_marker3 = Fmake_marker ();
29210 staticpro (&message_dolog_marker3);
29211
29212 #ifdef GLYPH_DEBUG
29213 defsubr (&Sdump_frame_glyph_matrix);
29214 defsubr (&Sdump_glyph_matrix);
29215 defsubr (&Sdump_glyph_row);
29216 defsubr (&Sdump_tool_bar_row);
29217 defsubr (&Strace_redisplay);
29218 defsubr (&Strace_to_stderr);
29219 #endif
29220 #ifdef HAVE_WINDOW_SYSTEM
29221 defsubr (&Stool_bar_lines_needed);
29222 defsubr (&Slookup_image_map);
29223 #endif
29224 defsubr (&Sline_pixel_height);
29225 defsubr (&Sformat_mode_line);
29226 defsubr (&Sinvisible_p);
29227 defsubr (&Scurrent_bidi_paragraph_direction);
29228 defsubr (&Smove_point_visually);
29229
29230 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29231 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29232 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29233 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29234 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29235 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29236 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29237 DEFSYM (Qeval, "eval");
29238 DEFSYM (QCdata, ":data");
29239 DEFSYM (Qdisplay, "display");
29240 DEFSYM (Qspace_width, "space-width");
29241 DEFSYM (Qraise, "raise");
29242 DEFSYM (Qslice, "slice");
29243 DEFSYM (Qspace, "space");
29244 DEFSYM (Qmargin, "margin");
29245 DEFSYM (Qpointer, "pointer");
29246 DEFSYM (Qleft_margin, "left-margin");
29247 DEFSYM (Qright_margin, "right-margin");
29248 DEFSYM (Qcenter, "center");
29249 DEFSYM (Qline_height, "line-height");
29250 DEFSYM (QCalign_to, ":align-to");
29251 DEFSYM (QCrelative_width, ":relative-width");
29252 DEFSYM (QCrelative_height, ":relative-height");
29253 DEFSYM (QCeval, ":eval");
29254 DEFSYM (QCpropertize, ":propertize");
29255 DEFSYM (QCfile, ":file");
29256 DEFSYM (Qfontified, "fontified");
29257 DEFSYM (Qfontification_functions, "fontification-functions");
29258 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29259 DEFSYM (Qescape_glyph, "escape-glyph");
29260 DEFSYM (Qnobreak_space, "nobreak-space");
29261 DEFSYM (Qimage, "image");
29262 DEFSYM (Qtext, "text");
29263 DEFSYM (Qboth, "both");
29264 DEFSYM (Qboth_horiz, "both-horiz");
29265 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29266 DEFSYM (QCmap, ":map");
29267 DEFSYM (QCpointer, ":pointer");
29268 DEFSYM (Qrect, "rect");
29269 DEFSYM (Qcircle, "circle");
29270 DEFSYM (Qpoly, "poly");
29271 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29272 DEFSYM (Qgrow_only, "grow-only");
29273 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29274 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29275 DEFSYM (Qposition, "position");
29276 DEFSYM (Qbuffer_position, "buffer-position");
29277 DEFSYM (Qobject, "object");
29278 DEFSYM (Qbar, "bar");
29279 DEFSYM (Qhbar, "hbar");
29280 DEFSYM (Qbox, "box");
29281 DEFSYM (Qhollow, "hollow");
29282 DEFSYM (Qhand, "hand");
29283 DEFSYM (Qarrow, "arrow");
29284 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29285
29286 list_of_error = list1 (list2 (intern_c_string ("error"),
29287 intern_c_string ("void-variable")));
29288 staticpro (&list_of_error);
29289
29290 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29291 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29292 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29293 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29294
29295 echo_buffer[0] = echo_buffer[1] = Qnil;
29296 staticpro (&echo_buffer[0]);
29297 staticpro (&echo_buffer[1]);
29298
29299 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29300 staticpro (&echo_area_buffer[0]);
29301 staticpro (&echo_area_buffer[1]);
29302
29303 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29304 staticpro (&Vmessages_buffer_name);
29305
29306 mode_line_proptrans_alist = Qnil;
29307 staticpro (&mode_line_proptrans_alist);
29308 mode_line_string_list = Qnil;
29309 staticpro (&mode_line_string_list);
29310 mode_line_string_face = Qnil;
29311 staticpro (&mode_line_string_face);
29312 mode_line_string_face_prop = Qnil;
29313 staticpro (&mode_line_string_face_prop);
29314 Vmode_line_unwind_vector = Qnil;
29315 staticpro (&Vmode_line_unwind_vector);
29316
29317 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29318
29319 help_echo_string = Qnil;
29320 staticpro (&help_echo_string);
29321 help_echo_object = Qnil;
29322 staticpro (&help_echo_object);
29323 help_echo_window = Qnil;
29324 staticpro (&help_echo_window);
29325 previous_help_echo_string = Qnil;
29326 staticpro (&previous_help_echo_string);
29327 help_echo_pos = -1;
29328
29329 DEFSYM (Qright_to_left, "right-to-left");
29330 DEFSYM (Qleft_to_right, "left-to-right");
29331
29332 #ifdef HAVE_WINDOW_SYSTEM
29333 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29334 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29335 For example, if a block cursor is over a tab, it will be drawn as
29336 wide as that tab on the display. */);
29337 x_stretch_cursor_p = 0;
29338 #endif
29339
29340 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29341 doc: /* Non-nil means highlight trailing whitespace.
29342 The face used for trailing whitespace is `trailing-whitespace'. */);
29343 Vshow_trailing_whitespace = Qnil;
29344
29345 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29346 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29347 If the value is t, Emacs highlights non-ASCII chars which have the
29348 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29349 or `escape-glyph' face respectively.
29350
29351 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29352 U+2011 (non-breaking hyphen) are affected.
29353
29354 Any other non-nil value means to display these characters as a escape
29355 glyph followed by an ordinary space or hyphen.
29356
29357 A value of nil means no special handling of these characters. */);
29358 Vnobreak_char_display = Qt;
29359
29360 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29361 doc: /* The pointer shape to show in void text areas.
29362 A value of nil means to show the text pointer. Other options are `arrow',
29363 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29364 Vvoid_text_area_pointer = Qarrow;
29365
29366 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29367 doc: /* Non-nil means don't actually do any redisplay.
29368 This is used for internal purposes. */);
29369 Vinhibit_redisplay = Qnil;
29370
29371 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29372 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29373 Vglobal_mode_string = Qnil;
29374
29375 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29376 doc: /* Marker for where to display an arrow on top of the buffer text.
29377 This must be the beginning of a line in order to work.
29378 See also `overlay-arrow-string'. */);
29379 Voverlay_arrow_position = Qnil;
29380
29381 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29382 doc: /* String to display as an arrow in non-window frames.
29383 See also `overlay-arrow-position'. */);
29384 Voverlay_arrow_string = build_pure_c_string ("=>");
29385
29386 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29387 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29388 The symbols on this list are examined during redisplay to determine
29389 where to display overlay arrows. */);
29390 Voverlay_arrow_variable_list
29391 = list1 (intern_c_string ("overlay-arrow-position"));
29392
29393 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29394 doc: /* The number of lines to try scrolling a window by when point moves out.
29395 If that fails to bring point back on frame, point is centered instead.
29396 If this is zero, point is always centered after it moves off frame.
29397 If you want scrolling to always be a line at a time, you should set
29398 `scroll-conservatively' to a large value rather than set this to 1. */);
29399
29400 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29401 doc: /* Scroll up to this many lines, to bring point back on screen.
29402 If point moves off-screen, redisplay will scroll by up to
29403 `scroll-conservatively' lines in order to bring point just barely
29404 onto the screen again. If that cannot be done, then redisplay
29405 recenters point as usual.
29406
29407 If the value is greater than 100, redisplay will never recenter point,
29408 but will always scroll just enough text to bring point into view, even
29409 if you move far away.
29410
29411 A value of zero means always recenter point if it moves off screen. */);
29412 scroll_conservatively = 0;
29413
29414 DEFVAR_INT ("scroll-margin", scroll_margin,
29415 doc: /* Number of lines of margin at the top and bottom of a window.
29416 Recenter the window whenever point gets within this many lines
29417 of the top or bottom of the window. */);
29418 scroll_margin = 0;
29419
29420 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29421 doc: /* Pixels per inch value for non-window system displays.
29422 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29423 Vdisplay_pixels_per_inch = make_float (72.0);
29424
29425 #ifdef GLYPH_DEBUG
29426 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29427 #endif
29428
29429 DEFVAR_LISP ("truncate-partial-width-windows",
29430 Vtruncate_partial_width_windows,
29431 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29432 For an integer value, truncate lines in each window narrower than the
29433 full frame width, provided the window width is less than that integer;
29434 otherwise, respect the value of `truncate-lines'.
29435
29436 For any other non-nil value, truncate lines in all windows that do
29437 not span the full frame width.
29438
29439 A value of nil means to respect the value of `truncate-lines'.
29440
29441 If `word-wrap' is enabled, you might want to reduce this. */);
29442 Vtruncate_partial_width_windows = make_number (50);
29443
29444 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29445 doc: /* Maximum buffer size for which line number should be displayed.
29446 If the buffer is bigger than this, the line number does not appear
29447 in the mode line. A value of nil means no limit. */);
29448 Vline_number_display_limit = Qnil;
29449
29450 DEFVAR_INT ("line-number-display-limit-width",
29451 line_number_display_limit_width,
29452 doc: /* Maximum line width (in characters) for line number display.
29453 If the average length of the lines near point is bigger than this, then the
29454 line number may be omitted from the mode line. */);
29455 line_number_display_limit_width = 200;
29456
29457 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29458 doc: /* Non-nil means highlight region even in nonselected windows. */);
29459 highlight_nonselected_windows = 0;
29460
29461 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29462 doc: /* Non-nil if more than one frame is visible on this display.
29463 Minibuffer-only frames don't count, but iconified frames do.
29464 This variable is not guaranteed to be accurate except while processing
29465 `frame-title-format' and `icon-title-format'. */);
29466
29467 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29468 doc: /* Template for displaying the title bar of visible frames.
29469 \(Assuming the window manager supports this feature.)
29470
29471 This variable has the same structure as `mode-line-format', except that
29472 the %c and %l constructs are ignored. It is used only on frames for
29473 which no explicit name has been set \(see `modify-frame-parameters'). */);
29474
29475 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29476 doc: /* Template for displaying the title bar of an iconified frame.
29477 \(Assuming the window manager supports this feature.)
29478 This variable has the same structure as `mode-line-format' (which see),
29479 and is used only on frames for which no explicit name has been set
29480 \(see `modify-frame-parameters'). */);
29481 Vicon_title_format
29482 = Vframe_title_format
29483 = listn (CONSTYPE_PURE, 3,
29484 intern_c_string ("multiple-frames"),
29485 build_pure_c_string ("%b"),
29486 listn (CONSTYPE_PURE, 4,
29487 empty_unibyte_string,
29488 intern_c_string ("invocation-name"),
29489 build_pure_c_string ("@"),
29490 intern_c_string ("system-name")));
29491
29492 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29493 doc: /* Maximum number of lines to keep in the message log buffer.
29494 If nil, disable message logging. If t, log messages but don't truncate
29495 the buffer when it becomes large. */);
29496 Vmessage_log_max = make_number (1000);
29497
29498 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29499 doc: /* Functions called before redisplay, if window sizes have changed.
29500 The value should be a list of functions that take one argument.
29501 Just before redisplay, for each frame, if any of its windows have changed
29502 size since the last redisplay, or have been split or deleted,
29503 all the functions in the list are called, with the frame as argument. */);
29504 Vwindow_size_change_functions = Qnil;
29505
29506 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29507 doc: /* List of functions to call before redisplaying a window with scrolling.
29508 Each function is called with two arguments, the window and its new
29509 display-start position. Note that these functions are also called by
29510 `set-window-buffer'. Also note that the value of `window-end' is not
29511 valid when these functions are called.
29512
29513 Warning: Do not use this feature to alter the way the window
29514 is scrolled. It is not designed for that, and such use probably won't
29515 work. */);
29516 Vwindow_scroll_functions = Qnil;
29517
29518 DEFVAR_LISP ("window-text-change-functions",
29519 Vwindow_text_change_functions,
29520 doc: /* Functions to call in redisplay when text in the window might change. */);
29521 Vwindow_text_change_functions = Qnil;
29522
29523 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29524 doc: /* Functions called when redisplay of a window reaches the end trigger.
29525 Each function is called with two arguments, the window and the end trigger value.
29526 See `set-window-redisplay-end-trigger'. */);
29527 Vredisplay_end_trigger_functions = Qnil;
29528
29529 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29530 doc: /* Non-nil means autoselect window with mouse pointer.
29531 If nil, do not autoselect windows.
29532 A positive number means delay autoselection by that many seconds: a
29533 window is autoselected only after the mouse has remained in that
29534 window for the duration of the delay.
29535 A negative number has a similar effect, but causes windows to be
29536 autoselected only after the mouse has stopped moving. \(Because of
29537 the way Emacs compares mouse events, you will occasionally wait twice
29538 that time before the window gets selected.\)
29539 Any other value means to autoselect window instantaneously when the
29540 mouse pointer enters it.
29541
29542 Autoselection selects the minibuffer only if it is active, and never
29543 unselects the minibuffer if it is active.
29544
29545 When customizing this variable make sure that the actual value of
29546 `focus-follows-mouse' matches the behavior of your window manager. */);
29547 Vmouse_autoselect_window = Qnil;
29548
29549 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29550 doc: /* Non-nil means automatically resize tool-bars.
29551 This dynamically changes the tool-bar's height to the minimum height
29552 that is needed to make all tool-bar items visible.
29553 If value is `grow-only', the tool-bar's height is only increased
29554 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29555 Vauto_resize_tool_bars = Qt;
29556
29557 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29558 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29559 auto_raise_tool_bar_buttons_p = 1;
29560
29561 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29562 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29563 make_cursor_line_fully_visible_p = 1;
29564
29565 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29566 doc: /* Border below tool-bar in pixels.
29567 If an integer, use it as the height of the border.
29568 If it is one of `internal-border-width' or `border-width', use the
29569 value of the corresponding frame parameter.
29570 Otherwise, no border is added below the tool-bar. */);
29571 Vtool_bar_border = Qinternal_border_width;
29572
29573 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29574 doc: /* Margin around tool-bar buttons in pixels.
29575 If an integer, use that for both horizontal and vertical margins.
29576 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29577 HORZ specifying the horizontal margin, and VERT specifying the
29578 vertical margin. */);
29579 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29580
29581 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29582 doc: /* Relief thickness of tool-bar buttons. */);
29583 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29584
29585 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29586 doc: /* Tool bar style to use.
29587 It can be one of
29588 image - show images only
29589 text - show text only
29590 both - show both, text below image
29591 both-horiz - show text to the right of the image
29592 text-image-horiz - show text to the left of the image
29593 any other - use system default or image if no system default.
29594
29595 This variable only affects the GTK+ toolkit version of Emacs. */);
29596 Vtool_bar_style = Qnil;
29597
29598 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29599 doc: /* Maximum number of characters a label can have to be shown.
29600 The tool bar style must also show labels for this to have any effect, see
29601 `tool-bar-style'. */);
29602 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29603
29604 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29605 doc: /* List of functions to call to fontify regions of text.
29606 Each function is called with one argument POS. Functions must
29607 fontify a region starting at POS in the current buffer, and give
29608 fontified regions the property `fontified'. */);
29609 Vfontification_functions = Qnil;
29610 Fmake_variable_buffer_local (Qfontification_functions);
29611
29612 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29613 unibyte_display_via_language_environment,
29614 doc: /* Non-nil means display unibyte text according to language environment.
29615 Specifically, this means that raw bytes in the range 160-255 decimal
29616 are displayed by converting them to the equivalent multibyte characters
29617 according to the current language environment. As a result, they are
29618 displayed according to the current fontset.
29619
29620 Note that this variable affects only how these bytes are displayed,
29621 but does not change the fact they are interpreted as raw bytes. */);
29622 unibyte_display_via_language_environment = 0;
29623
29624 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29625 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29626 If a float, it specifies a fraction of the mini-window frame's height.
29627 If an integer, it specifies a number of lines. */);
29628 Vmax_mini_window_height = make_float (0.25);
29629
29630 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29631 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29632 A value of nil means don't automatically resize mini-windows.
29633 A value of t means resize them to fit the text displayed in them.
29634 A value of `grow-only', the default, means let mini-windows grow only;
29635 they return to their normal size when the minibuffer is closed, or the
29636 echo area becomes empty. */);
29637 Vresize_mini_windows = Qgrow_only;
29638
29639 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29640 doc: /* Alist specifying how to blink the cursor off.
29641 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29642 `cursor-type' frame-parameter or variable equals ON-STATE,
29643 comparing using `equal', Emacs uses OFF-STATE to specify
29644 how to blink it off. ON-STATE and OFF-STATE are values for
29645 the `cursor-type' frame parameter.
29646
29647 If a frame's ON-STATE has no entry in this list,
29648 the frame's other specifications determine how to blink the cursor off. */);
29649 Vblink_cursor_alist = Qnil;
29650
29651 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29652 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29653 If non-nil, windows are automatically scrolled horizontally to make
29654 point visible. */);
29655 automatic_hscrolling_p = 1;
29656 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29657
29658 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29659 doc: /* How many columns away from the window edge point is allowed to get
29660 before automatic hscrolling will horizontally scroll the window. */);
29661 hscroll_margin = 5;
29662
29663 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29664 doc: /* How many columns to scroll the window when point gets too close to the edge.
29665 When point is less than `hscroll-margin' columns from the window
29666 edge, automatic hscrolling will scroll the window by the amount of columns
29667 determined by this variable. If its value is a positive integer, scroll that
29668 many columns. If it's a positive floating-point number, it specifies the
29669 fraction of the window's width to scroll. If it's nil or zero, point will be
29670 centered horizontally after the scroll. Any other value, including negative
29671 numbers, are treated as if the value were zero.
29672
29673 Automatic hscrolling always moves point outside the scroll margin, so if
29674 point was more than scroll step columns inside the margin, the window will
29675 scroll more than the value given by the scroll step.
29676
29677 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29678 and `scroll-right' overrides this variable's effect. */);
29679 Vhscroll_step = make_number (0);
29680
29681 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29682 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29683 Bind this around calls to `message' to let it take effect. */);
29684 message_truncate_lines = 0;
29685
29686 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29687 doc: /* Normal hook run to update the menu bar definitions.
29688 Redisplay runs this hook before it redisplays the menu bar.
29689 This is used to update submenus such as Buffers,
29690 whose contents depend on various data. */);
29691 Vmenu_bar_update_hook = Qnil;
29692
29693 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29694 doc: /* Frame for which we are updating a menu.
29695 The enable predicate for a menu binding should check this variable. */);
29696 Vmenu_updating_frame = Qnil;
29697
29698 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29699 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29700 inhibit_menubar_update = 0;
29701
29702 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29703 doc: /* Prefix prepended to all continuation lines at display time.
29704 The value may be a string, an image, or a stretch-glyph; it is
29705 interpreted in the same way as the value of a `display' text property.
29706
29707 This variable is overridden by any `wrap-prefix' text or overlay
29708 property.
29709
29710 To add a prefix to non-continuation lines, use `line-prefix'. */);
29711 Vwrap_prefix = Qnil;
29712 DEFSYM (Qwrap_prefix, "wrap-prefix");
29713 Fmake_variable_buffer_local (Qwrap_prefix);
29714
29715 DEFVAR_LISP ("line-prefix", Vline_prefix,
29716 doc: /* Prefix prepended to all non-continuation lines at display time.
29717 The value may be a string, an image, or a stretch-glyph; it is
29718 interpreted in the same way as the value of a `display' text property.
29719
29720 This variable is overridden by any `line-prefix' text or overlay
29721 property.
29722
29723 To add a prefix to continuation lines, use `wrap-prefix'. */);
29724 Vline_prefix = Qnil;
29725 DEFSYM (Qline_prefix, "line-prefix");
29726 Fmake_variable_buffer_local (Qline_prefix);
29727
29728 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29729 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29730 inhibit_eval_during_redisplay = 0;
29731
29732 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29733 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29734 inhibit_free_realized_faces = 0;
29735
29736 #ifdef GLYPH_DEBUG
29737 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29738 doc: /* Inhibit try_window_id display optimization. */);
29739 inhibit_try_window_id = 0;
29740
29741 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29742 doc: /* Inhibit try_window_reusing display optimization. */);
29743 inhibit_try_window_reusing = 0;
29744
29745 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29746 doc: /* Inhibit try_cursor_movement display optimization. */);
29747 inhibit_try_cursor_movement = 0;
29748 #endif /* GLYPH_DEBUG */
29749
29750 DEFVAR_INT ("overline-margin", overline_margin,
29751 doc: /* Space between overline and text, in pixels.
29752 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29753 margin to the character height. */);
29754 overline_margin = 2;
29755
29756 DEFVAR_INT ("underline-minimum-offset",
29757 underline_minimum_offset,
29758 doc: /* Minimum distance between baseline and underline.
29759 This can improve legibility of underlined text at small font sizes,
29760 particularly when using variable `x-use-underline-position-properties'
29761 with fonts that specify an UNDERLINE_POSITION relatively close to the
29762 baseline. The default value is 1. */);
29763 underline_minimum_offset = 1;
29764
29765 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29766 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29767 This feature only works when on a window system that can change
29768 cursor shapes. */);
29769 display_hourglass_p = 1;
29770
29771 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29772 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29773 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29774
29775 #ifdef HAVE_WINDOW_SYSTEM
29776 hourglass_atimer = NULL;
29777 hourglass_shown_p = 0;
29778 #endif /* HAVE_WINDOW_SYSTEM */
29779
29780 DEFSYM (Qglyphless_char, "glyphless-char");
29781 DEFSYM (Qhex_code, "hex-code");
29782 DEFSYM (Qempty_box, "empty-box");
29783 DEFSYM (Qthin_space, "thin-space");
29784 DEFSYM (Qzero_width, "zero-width");
29785
29786 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29787 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29788
29789 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29790 doc: /* Char-table defining glyphless characters.
29791 Each element, if non-nil, should be one of the following:
29792 an ASCII acronym string: display this string in a box
29793 `hex-code': display the hexadecimal code of a character in a box
29794 `empty-box': display as an empty box
29795 `thin-space': display as 1-pixel width space
29796 `zero-width': don't display
29797 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29798 display method for graphical terminals and text terminals respectively.
29799 GRAPHICAL and TEXT should each have one of the values listed above.
29800
29801 The char-table has one extra slot to control the display of a character for
29802 which no font is found. This slot only takes effect on graphical terminals.
29803 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29804 `thin-space'. The default is `empty-box'. */);
29805 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29806 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29807 Qempty_box);
29808
29809 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29810 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29811 Vdebug_on_message = Qnil;
29812 }
29813
29814
29815 /* Initialize this module when Emacs starts. */
29816
29817 void
29818 init_xdisp (void)
29819 {
29820 CHARPOS (this_line_start_pos) = 0;
29821
29822 if (!noninteractive)
29823 {
29824 struct window *m = XWINDOW (minibuf_window);
29825 Lisp_Object frame = m->frame;
29826 struct frame *f = XFRAME (frame);
29827 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29828 struct window *r = XWINDOW (root);
29829 int i;
29830
29831 echo_area_window = minibuf_window;
29832
29833 r->top_line = FRAME_TOP_MARGIN (f);
29834 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29835 r->total_cols = FRAME_COLS (f);
29836
29837 m->top_line = FRAME_LINES (f) - 1;
29838 m->total_lines = 1;
29839 m->total_cols = FRAME_COLS (f);
29840
29841 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29842 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29843 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29844
29845 /* The default ellipsis glyphs `...'. */
29846 for (i = 0; i < 3; ++i)
29847 default_invis_vector[i] = make_number ('.');
29848 }
29849
29850 {
29851 /* Allocate the buffer for frame titles.
29852 Also used for `format-mode-line'. */
29853 int size = 100;
29854 mode_line_noprop_buf = xmalloc (size);
29855 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29856 mode_line_noprop_ptr = mode_line_noprop_buf;
29857 mode_line_target = MODE_LINE_DISPLAY;
29858 }
29859
29860 help_echo_showing_p = 0;
29861 }
29862
29863 #ifdef HAVE_WINDOW_SYSTEM
29864
29865 /* Platform-independent portion of hourglass implementation. */
29866
29867 /* Cancel a currently active hourglass timer, and start a new one. */
29868 void
29869 start_hourglass (void)
29870 {
29871 struct timespec delay;
29872
29873 cancel_hourglass ();
29874
29875 if (INTEGERP (Vhourglass_delay)
29876 && XINT (Vhourglass_delay) > 0)
29877 delay = make_timespec (min (XINT (Vhourglass_delay),
29878 TYPE_MAXIMUM (time_t)),
29879 0);
29880 else if (FLOATP (Vhourglass_delay)
29881 && XFLOAT_DATA (Vhourglass_delay) > 0)
29882 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29883 else
29884 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29885
29886 #ifdef HAVE_NTGUI
29887 {
29888 extern void w32_note_current_window (void);
29889 w32_note_current_window ();
29890 }
29891 #endif /* HAVE_NTGUI */
29892
29893 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29894 show_hourglass, NULL);
29895 }
29896
29897
29898 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29899 shown. */
29900 void
29901 cancel_hourglass (void)
29902 {
29903 if (hourglass_atimer)
29904 {
29905 cancel_atimer (hourglass_atimer);
29906 hourglass_atimer = NULL;
29907 }
29908
29909 if (hourglass_shown_p)
29910 hide_hourglass ();
29911 }
29912
29913 #endif /* HAVE_WINDOW_SYSTEM */