* src/xdisp.c (redisplay_internal): Fix typo in last change.
[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 /***********************************************************************
2605 Iterator initialization
2606 ***********************************************************************/
2607
2608 /* Initialize IT for displaying current_buffer in window W, starting
2609 at character position CHARPOS. CHARPOS < 0 means that no buffer
2610 position is specified which is useful when the iterator is assigned
2611 a position later. BYTEPOS is the byte position corresponding to
2612 CHARPOS.
2613
2614 If ROW is not null, calls to produce_glyphs with IT as parameter
2615 will produce glyphs in that row.
2616
2617 BASE_FACE_ID is the id of a base face to use. It must be one of
2618 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2619 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2620 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2621
2622 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2623 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2624 will be initialized to use the corresponding mode line glyph row of
2625 the desired matrix of W. */
2626
2627 void
2628 init_iterator (struct it *it, struct window *w,
2629 ptrdiff_t charpos, ptrdiff_t bytepos,
2630 struct glyph_row *row, enum face_id base_face_id)
2631 {
2632 enum face_id remapped_base_face_id = base_face_id;
2633
2634 /* Some precondition checks. */
2635 eassert (w != NULL && it != NULL);
2636 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2637 && charpos <= ZV));
2638
2639 /* If face attributes have been changed since the last redisplay,
2640 free realized faces now because they depend on face definitions
2641 that might have changed. Don't free faces while there might be
2642 desired matrices pending which reference these faces. */
2643 if (face_change_count && !inhibit_free_realized_faces)
2644 {
2645 face_change_count = 0;
2646 free_all_realized_faces (Qnil);
2647 }
2648
2649 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2650 if (! NILP (Vface_remapping_alist))
2651 remapped_base_face_id
2652 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2653
2654 /* Use one of the mode line rows of W's desired matrix if
2655 appropriate. */
2656 if (row == NULL)
2657 {
2658 if (base_face_id == MODE_LINE_FACE_ID
2659 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2660 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2661 else if (base_face_id == HEADER_LINE_FACE_ID)
2662 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2663 }
2664
2665 /* Clear IT. */
2666 memset (it, 0, sizeof *it);
2667 it->current.overlay_string_index = -1;
2668 it->current.dpvec_index = -1;
2669 it->base_face_id = remapped_base_face_id;
2670 it->string = Qnil;
2671 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2672 it->paragraph_embedding = L2R;
2673 it->bidi_it.string.lstring = Qnil;
2674 it->bidi_it.string.s = NULL;
2675 it->bidi_it.string.bufpos = 0;
2676 it->bidi_it.w = w;
2677
2678 /* The window in which we iterate over current_buffer: */
2679 XSETWINDOW (it->window, w);
2680 it->w = w;
2681 it->f = XFRAME (w->frame);
2682
2683 it->cmp_it.id = -1;
2684
2685 /* Extra space between lines (on window systems only). */
2686 if (base_face_id == DEFAULT_FACE_ID
2687 && FRAME_WINDOW_P (it->f))
2688 {
2689 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2690 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2691 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2692 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2693 * FRAME_LINE_HEIGHT (it->f));
2694 else if (it->f->extra_line_spacing > 0)
2695 it->extra_line_spacing = it->f->extra_line_spacing;
2696 it->max_extra_line_spacing = 0;
2697 }
2698
2699 /* If realized faces have been removed, e.g. because of face
2700 attribute changes of named faces, recompute them. When running
2701 in batch mode, the face cache of the initial frame is null. If
2702 we happen to get called, make a dummy face cache. */
2703 if (FRAME_FACE_CACHE (it->f) == NULL)
2704 init_frame_faces (it->f);
2705 if (FRAME_FACE_CACHE (it->f)->used == 0)
2706 recompute_basic_faces (it->f);
2707
2708 /* Current value of the `slice', `space-width', and 'height' properties. */
2709 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2710 it->space_width = Qnil;
2711 it->font_height = Qnil;
2712 it->override_ascent = -1;
2713
2714 /* Are control characters displayed as `^C'? */
2715 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2716
2717 /* -1 means everything between a CR and the following line end
2718 is invisible. >0 means lines indented more than this value are
2719 invisible. */
2720 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2721 ? (clip_to_bounds
2722 (-1, XINT (BVAR (current_buffer, selective_display)),
2723 PTRDIFF_MAX))
2724 : (!NILP (BVAR (current_buffer, selective_display))
2725 ? -1 : 0));
2726 it->selective_display_ellipsis_p
2727 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2728
2729 /* Display table to use. */
2730 it->dp = window_display_table (w);
2731
2732 /* Are multibyte characters enabled in current_buffer? */
2733 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2734
2735 /* Get the position at which the redisplay_end_trigger hook should
2736 be run, if it is to be run at all. */
2737 if (MARKERP (w->redisplay_end_trigger)
2738 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2739 it->redisplay_end_trigger_charpos
2740 = marker_position (w->redisplay_end_trigger);
2741 else if (INTEGERP (w->redisplay_end_trigger))
2742 it->redisplay_end_trigger_charpos =
2743 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2744
2745 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2746
2747 /* Are lines in the display truncated? */
2748 if (base_face_id != DEFAULT_FACE_ID
2749 || it->w->hscroll
2750 || (! WINDOW_FULL_WIDTH_P (it->w)
2751 && ((!NILP (Vtruncate_partial_width_windows)
2752 && !INTEGERP (Vtruncate_partial_width_windows))
2753 || (INTEGERP (Vtruncate_partial_width_windows)
2754 && (WINDOW_TOTAL_COLS (it->w)
2755 < XINT (Vtruncate_partial_width_windows))))))
2756 it->line_wrap = TRUNCATE;
2757 else if (NILP (BVAR (current_buffer, truncate_lines)))
2758 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2759 ? WINDOW_WRAP : WORD_WRAP;
2760 else
2761 it->line_wrap = TRUNCATE;
2762
2763 /* Get dimensions of truncation and continuation glyphs. These are
2764 displayed as fringe bitmaps under X, but we need them for such
2765 frames when the fringes are turned off. But leave the dimensions
2766 zero for tooltip frames, as these glyphs look ugly there and also
2767 sabotage calculations of tooltip dimensions in x-show-tip. */
2768 #ifdef HAVE_WINDOW_SYSTEM
2769 if (!(FRAME_WINDOW_P (it->f)
2770 && FRAMEP (tip_frame)
2771 && it->f == XFRAME (tip_frame)))
2772 #endif
2773 {
2774 if (it->line_wrap == TRUNCATE)
2775 {
2776 /* We will need the truncation glyph. */
2777 eassert (it->glyph_row == NULL);
2778 produce_special_glyphs (it, IT_TRUNCATION);
2779 it->truncation_pixel_width = it->pixel_width;
2780 }
2781 else
2782 {
2783 /* We will need the continuation glyph. */
2784 eassert (it->glyph_row == NULL);
2785 produce_special_glyphs (it, IT_CONTINUATION);
2786 it->continuation_pixel_width = it->pixel_width;
2787 }
2788 }
2789
2790 /* Reset these values to zero because the produce_special_glyphs
2791 above has changed them. */
2792 it->pixel_width = it->ascent = it->descent = 0;
2793 it->phys_ascent = it->phys_descent = 0;
2794
2795 /* Set this after getting the dimensions of truncation and
2796 continuation glyphs, so that we don't produce glyphs when calling
2797 produce_special_glyphs, above. */
2798 it->glyph_row = row;
2799 it->area = TEXT_AREA;
2800
2801 /* Forget any previous info about this row being reversed. */
2802 if (it->glyph_row)
2803 it->glyph_row->reversed_p = 0;
2804
2805 /* Get the dimensions of the display area. The display area
2806 consists of the visible window area plus a horizontally scrolled
2807 part to the left of the window. All x-values are relative to the
2808 start of this total display area. */
2809 if (base_face_id != DEFAULT_FACE_ID)
2810 {
2811 /* Mode lines, menu bar in terminal frames. */
2812 it->first_visible_x = 0;
2813 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2814 }
2815 else
2816 {
2817 it->first_visible_x =
2818 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2819 it->last_visible_x = (it->first_visible_x
2820 + window_box_width (w, TEXT_AREA));
2821
2822 /* If we truncate lines, leave room for the truncation glyph(s) at
2823 the right margin. Otherwise, leave room for the continuation
2824 glyph(s). Done only if the window has no fringes. Since we
2825 don't know at this point whether there will be any R2L lines in
2826 the window, we reserve space for truncation/continuation glyphs
2827 even if only one of the fringes is absent. */
2828 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2829 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2830 {
2831 if (it->line_wrap == TRUNCATE)
2832 it->last_visible_x -= it->truncation_pixel_width;
2833 else
2834 it->last_visible_x -= it->continuation_pixel_width;
2835 }
2836
2837 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2838 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2839 }
2840
2841 /* Leave room for a border glyph. */
2842 if (!FRAME_WINDOW_P (it->f)
2843 && !WINDOW_RIGHTMOST_P (it->w))
2844 it->last_visible_x -= 1;
2845
2846 it->last_visible_y = window_text_bottom_y (w);
2847
2848 /* For mode lines and alike, arrange for the first glyph having a
2849 left box line if the face specifies a box. */
2850 if (base_face_id != DEFAULT_FACE_ID)
2851 {
2852 struct face *face;
2853
2854 it->face_id = remapped_base_face_id;
2855
2856 /* If we have a boxed mode line, make the first character appear
2857 with a left box line. */
2858 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2859 if (face->box != FACE_NO_BOX)
2860 it->start_of_box_run_p = 1;
2861 }
2862
2863 /* If a buffer position was specified, set the iterator there,
2864 getting overlays and face properties from that position. */
2865 if (charpos >= BUF_BEG (current_buffer))
2866 {
2867 it->end_charpos = ZV;
2868 eassert (charpos == BYTE_TO_CHAR (bytepos));
2869 IT_CHARPOS (*it) = charpos;
2870 IT_BYTEPOS (*it) = bytepos;
2871
2872 /* We will rely on `reseat' to set this up properly, via
2873 handle_face_prop. */
2874 it->face_id = it->base_face_id;
2875
2876 it->start = it->current;
2877 /* Do we need to reorder bidirectional text? Not if this is a
2878 unibyte buffer: by definition, none of the single-byte
2879 characters are strong R2L, so no reordering is needed. And
2880 bidi.c doesn't support unibyte buffers anyway. Also, don't
2881 reorder while we are loading loadup.el, since the tables of
2882 character properties needed for reordering are not yet
2883 available. */
2884 it->bidi_p =
2885 NILP (Vpurify_flag)
2886 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2887 && it->multibyte_p;
2888
2889 /* If we are to reorder bidirectional text, init the bidi
2890 iterator. */
2891 if (it->bidi_p)
2892 {
2893 /* Note the paragraph direction that this buffer wants to
2894 use. */
2895 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2896 Qleft_to_right))
2897 it->paragraph_embedding = L2R;
2898 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2899 Qright_to_left))
2900 it->paragraph_embedding = R2L;
2901 else
2902 it->paragraph_embedding = NEUTRAL_DIR;
2903 bidi_unshelve_cache (NULL, 0);
2904 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2905 &it->bidi_it);
2906 }
2907
2908 /* Compute faces etc. */
2909 reseat (it, it->current.pos, 1);
2910 }
2911
2912 CHECK_IT (it);
2913 }
2914
2915
2916 /* Initialize IT for the display of window W with window start POS. */
2917
2918 void
2919 start_display (struct it *it, struct window *w, struct text_pos pos)
2920 {
2921 struct glyph_row *row;
2922 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2923
2924 row = w->desired_matrix->rows + first_vpos;
2925 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2926 it->first_vpos = first_vpos;
2927
2928 /* Don't reseat to previous visible line start if current start
2929 position is in a string or image. */
2930 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2931 {
2932 int start_at_line_beg_p;
2933 int first_y = it->current_y;
2934
2935 /* If window start is not at a line start, skip forward to POS to
2936 get the correct continuation lines width. */
2937 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2938 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2939 if (!start_at_line_beg_p)
2940 {
2941 int new_x;
2942
2943 reseat_at_previous_visible_line_start (it);
2944 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2945
2946 new_x = it->current_x + it->pixel_width;
2947
2948 /* If lines are continued, this line may end in the middle
2949 of a multi-glyph character (e.g. a control character
2950 displayed as \003, or in the middle of an overlay
2951 string). In this case move_it_to above will not have
2952 taken us to the start of the continuation line but to the
2953 end of the continued line. */
2954 if (it->current_x > 0
2955 && it->line_wrap != TRUNCATE /* Lines are continued. */
2956 && (/* And glyph doesn't fit on the line. */
2957 new_x > it->last_visible_x
2958 /* Or it fits exactly and we're on a window
2959 system frame. */
2960 || (new_x == it->last_visible_x
2961 && FRAME_WINDOW_P (it->f)
2962 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2963 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2964 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2965 {
2966 if ((it->current.dpvec_index >= 0
2967 || it->current.overlay_string_index >= 0)
2968 /* If we are on a newline from a display vector or
2969 overlay string, then we are already at the end of
2970 a screen line; no need to go to the next line in
2971 that case, as this line is not really continued.
2972 (If we do go to the next line, C-e will not DTRT.) */
2973 && it->c != '\n')
2974 {
2975 set_iterator_to_next (it, 1);
2976 move_it_in_display_line_to (it, -1, -1, 0);
2977 }
2978
2979 it->continuation_lines_width += it->current_x;
2980 }
2981 /* If the character at POS is displayed via a display
2982 vector, move_it_to above stops at the final glyph of
2983 IT->dpvec. To make the caller redisplay that character
2984 again (a.k.a. start at POS), we need to reset the
2985 dpvec_index to the beginning of IT->dpvec. */
2986 else if (it->current.dpvec_index >= 0)
2987 it->current.dpvec_index = 0;
2988
2989 /* We're starting a new display line, not affected by the
2990 height of the continued line, so clear the appropriate
2991 fields in the iterator structure. */
2992 it->max_ascent = it->max_descent = 0;
2993 it->max_phys_ascent = it->max_phys_descent = 0;
2994
2995 it->current_y = first_y;
2996 it->vpos = 0;
2997 it->current_x = it->hpos = 0;
2998 }
2999 }
3000 }
3001
3002
3003 /* Return 1 if POS is a position in ellipses displayed for invisible
3004 text. W is the window we display, for text property lookup. */
3005
3006 static int
3007 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3008 {
3009 Lisp_Object prop, window;
3010 int ellipses_p = 0;
3011 ptrdiff_t charpos = CHARPOS (pos->pos);
3012
3013 /* If POS specifies a position in a display vector, this might
3014 be for an ellipsis displayed for invisible text. We won't
3015 get the iterator set up for delivering that ellipsis unless
3016 we make sure that it gets aware of the invisible text. */
3017 if (pos->dpvec_index >= 0
3018 && pos->overlay_string_index < 0
3019 && CHARPOS (pos->string_pos) < 0
3020 && charpos > BEGV
3021 && (XSETWINDOW (window, w),
3022 prop = Fget_char_property (make_number (charpos),
3023 Qinvisible, window),
3024 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3025 {
3026 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3027 window);
3028 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3029 }
3030
3031 return ellipses_p;
3032 }
3033
3034
3035 /* Initialize IT for stepping through current_buffer in window W,
3036 starting at position POS that includes overlay string and display
3037 vector/ control character translation position information. Value
3038 is zero if there are overlay strings with newlines at POS. */
3039
3040 static int
3041 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3042 {
3043 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3044 int i, overlay_strings_with_newlines = 0;
3045
3046 /* If POS specifies a position in a display vector, this might
3047 be for an ellipsis displayed for invisible text. We won't
3048 get the iterator set up for delivering that ellipsis unless
3049 we make sure that it gets aware of the invisible text. */
3050 if (in_ellipses_for_invisible_text_p (pos, w))
3051 {
3052 --charpos;
3053 bytepos = 0;
3054 }
3055
3056 /* Keep in mind: the call to reseat in init_iterator skips invisible
3057 text, so we might end up at a position different from POS. This
3058 is only a problem when POS is a row start after a newline and an
3059 overlay starts there with an after-string, and the overlay has an
3060 invisible property. Since we don't skip invisible text in
3061 display_line and elsewhere immediately after consuming the
3062 newline before the row start, such a POS will not be in a string,
3063 but the call to init_iterator below will move us to the
3064 after-string. */
3065 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3066
3067 /* This only scans the current chunk -- it should scan all chunks.
3068 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3069 to 16 in 22.1 to make this a lesser problem. */
3070 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3071 {
3072 const char *s = SSDATA (it->overlay_strings[i]);
3073 const char *e = s + SBYTES (it->overlay_strings[i]);
3074
3075 while (s < e && *s != '\n')
3076 ++s;
3077
3078 if (s < e)
3079 {
3080 overlay_strings_with_newlines = 1;
3081 break;
3082 }
3083 }
3084
3085 /* If position is within an overlay string, set up IT to the right
3086 overlay string. */
3087 if (pos->overlay_string_index >= 0)
3088 {
3089 int relative_index;
3090
3091 /* If the first overlay string happens to have a `display'
3092 property for an image, the iterator will be set up for that
3093 image, and we have to undo that setup first before we can
3094 correct the overlay string index. */
3095 if (it->method == GET_FROM_IMAGE)
3096 pop_it (it);
3097
3098 /* We already have the first chunk of overlay strings in
3099 IT->overlay_strings. Load more until the one for
3100 pos->overlay_string_index is in IT->overlay_strings. */
3101 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3102 {
3103 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3104 it->current.overlay_string_index = 0;
3105 while (n--)
3106 {
3107 load_overlay_strings (it, 0);
3108 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3109 }
3110 }
3111
3112 it->current.overlay_string_index = pos->overlay_string_index;
3113 relative_index = (it->current.overlay_string_index
3114 % OVERLAY_STRING_CHUNK_SIZE);
3115 it->string = it->overlay_strings[relative_index];
3116 eassert (STRINGP (it->string));
3117 it->current.string_pos = pos->string_pos;
3118 it->method = GET_FROM_STRING;
3119 it->end_charpos = SCHARS (it->string);
3120 /* Set up the bidi iterator for this overlay string. */
3121 if (it->bidi_p)
3122 {
3123 it->bidi_it.string.lstring = it->string;
3124 it->bidi_it.string.s = NULL;
3125 it->bidi_it.string.schars = SCHARS (it->string);
3126 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3127 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3128 it->bidi_it.string.unibyte = !it->multibyte_p;
3129 it->bidi_it.w = it->w;
3130 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3131 FRAME_WINDOW_P (it->f), &it->bidi_it);
3132
3133 /* Synchronize the state of the bidi iterator with
3134 pos->string_pos. For any string position other than
3135 zero, this will be done automagically when we resume
3136 iteration over the string and get_visually_first_element
3137 is called. But if string_pos is zero, and the string is
3138 to be reordered for display, we need to resync manually,
3139 since it could be that the iteration state recorded in
3140 pos ended at string_pos of 0 moving backwards in string. */
3141 if (CHARPOS (pos->string_pos) == 0)
3142 {
3143 get_visually_first_element (it);
3144 if (IT_STRING_CHARPOS (*it) != 0)
3145 do {
3146 /* Paranoia. */
3147 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3148 bidi_move_to_visually_next (&it->bidi_it);
3149 } while (it->bidi_it.charpos != 0);
3150 }
3151 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3152 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3153 }
3154 }
3155
3156 if (CHARPOS (pos->string_pos) >= 0)
3157 {
3158 /* Recorded position is not in an overlay string, but in another
3159 string. This can only be a string from a `display' property.
3160 IT should already be filled with that string. */
3161 it->current.string_pos = pos->string_pos;
3162 eassert (STRINGP (it->string));
3163 if (it->bidi_p)
3164 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3165 FRAME_WINDOW_P (it->f), &it->bidi_it);
3166 }
3167
3168 /* Restore position in display vector translations, control
3169 character translations or ellipses. */
3170 if (pos->dpvec_index >= 0)
3171 {
3172 if (it->dpvec == NULL)
3173 get_next_display_element (it);
3174 eassert (it->dpvec && it->current.dpvec_index == 0);
3175 it->current.dpvec_index = pos->dpvec_index;
3176 }
3177
3178 CHECK_IT (it);
3179 return !overlay_strings_with_newlines;
3180 }
3181
3182
3183 /* Initialize IT for stepping through current_buffer in window W
3184 starting at ROW->start. */
3185
3186 static void
3187 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3188 {
3189 init_from_display_pos (it, w, &row->start);
3190 it->start = row->start;
3191 it->continuation_lines_width = row->continuation_lines_width;
3192 CHECK_IT (it);
3193 }
3194
3195
3196 /* Initialize IT for stepping through current_buffer in window W
3197 starting in the line following ROW, i.e. starting at ROW->end.
3198 Value is zero if there are overlay strings with newlines at ROW's
3199 end position. */
3200
3201 static int
3202 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3203 {
3204 int success = 0;
3205
3206 if (init_from_display_pos (it, w, &row->end))
3207 {
3208 if (row->continued_p)
3209 it->continuation_lines_width
3210 = row->continuation_lines_width + row->pixel_width;
3211 CHECK_IT (it);
3212 success = 1;
3213 }
3214
3215 return success;
3216 }
3217
3218
3219
3220 \f
3221 /***********************************************************************
3222 Text properties
3223 ***********************************************************************/
3224
3225 /* Called when IT reaches IT->stop_charpos. Handle text property and
3226 overlay changes. Set IT->stop_charpos to the next position where
3227 to stop. */
3228
3229 static void
3230 handle_stop (struct it *it)
3231 {
3232 enum prop_handled handled;
3233 int handle_overlay_change_p;
3234 struct props *p;
3235
3236 it->dpvec = NULL;
3237 it->current.dpvec_index = -1;
3238 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3239 it->ignore_overlay_strings_at_pos_p = 0;
3240 it->ellipsis_p = 0;
3241
3242 /* Use face of preceding text for ellipsis (if invisible) */
3243 if (it->selective_display_ellipsis_p)
3244 it->saved_face_id = it->face_id;
3245
3246 do
3247 {
3248 handled = HANDLED_NORMALLY;
3249
3250 /* Call text property handlers. */
3251 for (p = it_props; p->handler; ++p)
3252 {
3253 handled = p->handler (it);
3254
3255 if (handled == HANDLED_RECOMPUTE_PROPS)
3256 break;
3257 else if (handled == HANDLED_RETURN)
3258 {
3259 /* We still want to show before and after strings from
3260 overlays even if the actual buffer text is replaced. */
3261 if (!handle_overlay_change_p
3262 || it->sp > 1
3263 /* Don't call get_overlay_strings_1 if we already
3264 have overlay strings loaded, because doing so
3265 will load them again and push the iterator state
3266 onto the stack one more time, which is not
3267 expected by the rest of the code that processes
3268 overlay strings. */
3269 || (it->current.overlay_string_index < 0
3270 ? !get_overlay_strings_1 (it, 0, 0)
3271 : 0))
3272 {
3273 if (it->ellipsis_p)
3274 setup_for_ellipsis (it, 0);
3275 /* When handling a display spec, we might load an
3276 empty string. In that case, discard it here. We
3277 used to discard it in handle_single_display_spec,
3278 but that causes get_overlay_strings_1, above, to
3279 ignore overlay strings that we must check. */
3280 if (STRINGP (it->string) && !SCHARS (it->string))
3281 pop_it (it);
3282 return;
3283 }
3284 else if (STRINGP (it->string) && !SCHARS (it->string))
3285 pop_it (it);
3286 else
3287 {
3288 it->ignore_overlay_strings_at_pos_p = 1;
3289 it->string_from_display_prop_p = 0;
3290 it->from_disp_prop_p = 0;
3291 handle_overlay_change_p = 0;
3292 }
3293 handled = HANDLED_RECOMPUTE_PROPS;
3294 break;
3295 }
3296 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3297 handle_overlay_change_p = 0;
3298 }
3299
3300 if (handled != HANDLED_RECOMPUTE_PROPS)
3301 {
3302 /* Don't check for overlay strings below when set to deliver
3303 characters from a display vector. */
3304 if (it->method == GET_FROM_DISPLAY_VECTOR)
3305 handle_overlay_change_p = 0;
3306
3307 /* Handle overlay changes.
3308 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3309 if it finds overlays. */
3310 if (handle_overlay_change_p)
3311 handled = handle_overlay_change (it);
3312 }
3313
3314 if (it->ellipsis_p)
3315 {
3316 setup_for_ellipsis (it, 0);
3317 break;
3318 }
3319 }
3320 while (handled == HANDLED_RECOMPUTE_PROPS);
3321
3322 /* Determine where to stop next. */
3323 if (handled == HANDLED_NORMALLY)
3324 compute_stop_pos (it);
3325 }
3326
3327
3328 /* Compute IT->stop_charpos from text property and overlay change
3329 information for IT's current position. */
3330
3331 static void
3332 compute_stop_pos (struct it *it)
3333 {
3334 register INTERVAL iv, next_iv;
3335 Lisp_Object object, limit, position;
3336 ptrdiff_t charpos, bytepos;
3337
3338 if (STRINGP (it->string))
3339 {
3340 /* Strings are usually short, so don't limit the search for
3341 properties. */
3342 it->stop_charpos = it->end_charpos;
3343 object = it->string;
3344 limit = Qnil;
3345 charpos = IT_STRING_CHARPOS (*it);
3346 bytepos = IT_STRING_BYTEPOS (*it);
3347 }
3348 else
3349 {
3350 ptrdiff_t pos;
3351
3352 /* If end_charpos is out of range for some reason, such as a
3353 misbehaving display function, rationalize it (Bug#5984). */
3354 if (it->end_charpos > ZV)
3355 it->end_charpos = ZV;
3356 it->stop_charpos = it->end_charpos;
3357
3358 /* If next overlay change is in front of the current stop pos
3359 (which is IT->end_charpos), stop there. Note: value of
3360 next_overlay_change is point-max if no overlay change
3361 follows. */
3362 charpos = IT_CHARPOS (*it);
3363 bytepos = IT_BYTEPOS (*it);
3364 pos = next_overlay_change (charpos);
3365 if (pos < it->stop_charpos)
3366 it->stop_charpos = pos;
3367
3368 /* Set up variables for computing the stop position from text
3369 property changes. */
3370 XSETBUFFER (object, current_buffer);
3371 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3372 }
3373
3374 /* Get the interval containing IT's position. Value is a null
3375 interval if there isn't such an interval. */
3376 position = make_number (charpos);
3377 iv = validate_interval_range (object, &position, &position, 0);
3378 if (iv)
3379 {
3380 Lisp_Object values_here[LAST_PROP_IDX];
3381 struct props *p;
3382
3383 /* Get properties here. */
3384 for (p = it_props; p->handler; ++p)
3385 values_here[p->idx] = textget (iv->plist, *p->name);
3386
3387 /* Look for an interval following iv that has different
3388 properties. */
3389 for (next_iv = next_interval (iv);
3390 (next_iv
3391 && (NILP (limit)
3392 || XFASTINT (limit) > next_iv->position));
3393 next_iv = next_interval (next_iv))
3394 {
3395 for (p = it_props; p->handler; ++p)
3396 {
3397 Lisp_Object new_value;
3398
3399 new_value = textget (next_iv->plist, *p->name);
3400 if (!EQ (values_here[p->idx], new_value))
3401 break;
3402 }
3403
3404 if (p->handler)
3405 break;
3406 }
3407
3408 if (next_iv)
3409 {
3410 if (INTEGERP (limit)
3411 && next_iv->position >= XFASTINT (limit))
3412 /* No text property change up to limit. */
3413 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3414 else
3415 /* Text properties change in next_iv. */
3416 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3417 }
3418 }
3419
3420 if (it->cmp_it.id < 0)
3421 {
3422 ptrdiff_t stoppos = it->end_charpos;
3423
3424 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3425 stoppos = -1;
3426 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3427 stoppos, it->string);
3428 }
3429
3430 eassert (STRINGP (it->string)
3431 || (it->stop_charpos >= BEGV
3432 && it->stop_charpos >= IT_CHARPOS (*it)));
3433 }
3434
3435
3436 /* Return the position of the next overlay change after POS in
3437 current_buffer. Value is point-max if no overlay change
3438 follows. This is like `next-overlay-change' but doesn't use
3439 xmalloc. */
3440
3441 static ptrdiff_t
3442 next_overlay_change (ptrdiff_t pos)
3443 {
3444 ptrdiff_t i, noverlays;
3445 ptrdiff_t endpos;
3446 Lisp_Object *overlays;
3447
3448 /* Get all overlays at the given position. */
3449 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3450
3451 /* If any of these overlays ends before endpos,
3452 use its ending point instead. */
3453 for (i = 0; i < noverlays; ++i)
3454 {
3455 Lisp_Object oend;
3456 ptrdiff_t oendpos;
3457
3458 oend = OVERLAY_END (overlays[i]);
3459 oendpos = OVERLAY_POSITION (oend);
3460 endpos = min (endpos, oendpos);
3461 }
3462
3463 return endpos;
3464 }
3465
3466 /* How many characters forward to search for a display property or
3467 display string. Searching too far forward makes the bidi display
3468 sluggish, especially in small windows. */
3469 #define MAX_DISP_SCAN 250
3470
3471 /* Return the character position of a display string at or after
3472 position specified by POSITION. If no display string exists at or
3473 after POSITION, return ZV. A display string is either an overlay
3474 with `display' property whose value is a string, or a `display'
3475 text property whose value is a string. STRING is data about the
3476 string to iterate; if STRING->lstring is nil, we are iterating a
3477 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3478 on a GUI frame. DISP_PROP is set to zero if we searched
3479 MAX_DISP_SCAN characters forward without finding any display
3480 strings, non-zero otherwise. It is set to 2 if the display string
3481 uses any kind of `(space ...)' spec that will produce a stretch of
3482 white space in the text area. */
3483 ptrdiff_t
3484 compute_display_string_pos (struct text_pos *position,
3485 struct bidi_string_data *string,
3486 struct window *w,
3487 int frame_window_p, int *disp_prop)
3488 {
3489 /* OBJECT = nil means current buffer. */
3490 Lisp_Object object, object1;
3491 Lisp_Object pos, spec, limpos;
3492 int string_p = (string && (STRINGP (string->lstring) || string->s));
3493 ptrdiff_t eob = string_p ? string->schars : ZV;
3494 ptrdiff_t begb = string_p ? 0 : BEGV;
3495 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3496 ptrdiff_t lim =
3497 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3498 struct text_pos tpos;
3499 int rv = 0;
3500
3501 if (string && STRINGP (string->lstring))
3502 object1 = object = string->lstring;
3503 else if (w && !string_p)
3504 {
3505 XSETWINDOW (object, w);
3506 object1 = Qnil;
3507 }
3508 else
3509 object1 = object = Qnil;
3510
3511 *disp_prop = 1;
3512
3513 if (charpos >= eob
3514 /* We don't support display properties whose values are strings
3515 that have display string properties. */
3516 || string->from_disp_str
3517 /* C strings cannot have display properties. */
3518 || (string->s && !STRINGP (object)))
3519 {
3520 *disp_prop = 0;
3521 return eob;
3522 }
3523
3524 /* If the character at CHARPOS is where the display string begins,
3525 return CHARPOS. */
3526 pos = make_number (charpos);
3527 if (STRINGP (object))
3528 bufpos = string->bufpos;
3529 else
3530 bufpos = charpos;
3531 tpos = *position;
3532 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3533 && (charpos <= begb
3534 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3535 object),
3536 spec))
3537 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3538 frame_window_p)))
3539 {
3540 if (rv == 2)
3541 *disp_prop = 2;
3542 return charpos;
3543 }
3544
3545 /* Look forward for the first character with a `display' property
3546 that will replace the underlying text when displayed. */
3547 limpos = make_number (lim);
3548 do {
3549 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3550 CHARPOS (tpos) = XFASTINT (pos);
3551 if (CHARPOS (tpos) >= lim)
3552 {
3553 *disp_prop = 0;
3554 break;
3555 }
3556 if (STRINGP (object))
3557 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3558 else
3559 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3560 spec = Fget_char_property (pos, Qdisplay, object);
3561 if (!STRINGP (object))
3562 bufpos = CHARPOS (tpos);
3563 } while (NILP (spec)
3564 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3565 bufpos, frame_window_p)));
3566 if (rv == 2)
3567 *disp_prop = 2;
3568
3569 return CHARPOS (tpos);
3570 }
3571
3572 /* Return the character position of the end of the display string that
3573 started at CHARPOS. If there's no display string at CHARPOS,
3574 return -1. A display string is either an overlay with `display'
3575 property whose value is a string or a `display' text property whose
3576 value is a string. */
3577 ptrdiff_t
3578 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3579 {
3580 /* OBJECT = nil means current buffer. */
3581 Lisp_Object object =
3582 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3583 Lisp_Object pos = make_number (charpos);
3584 ptrdiff_t eob =
3585 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3586
3587 if (charpos >= eob || (string->s && !STRINGP (object)))
3588 return eob;
3589
3590 /* It could happen that the display property or overlay was removed
3591 since we found it in compute_display_string_pos above. One way
3592 this can happen is if JIT font-lock was called (through
3593 handle_fontified_prop), and jit-lock-functions remove text
3594 properties or overlays from the portion of buffer that includes
3595 CHARPOS. Muse mode is known to do that, for example. In this
3596 case, we return -1 to the caller, to signal that no display
3597 string is actually present at CHARPOS. See bidi_fetch_char for
3598 how this is handled.
3599
3600 An alternative would be to never look for display properties past
3601 it->stop_charpos. But neither compute_display_string_pos nor
3602 bidi_fetch_char that calls it know or care where the next
3603 stop_charpos is. */
3604 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3605 return -1;
3606
3607 /* Look forward for the first character where the `display' property
3608 changes. */
3609 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3610
3611 return XFASTINT (pos);
3612 }
3613
3614
3615 \f
3616 /***********************************************************************
3617 Fontification
3618 ***********************************************************************/
3619
3620 /* Handle changes in the `fontified' property of the current buffer by
3621 calling hook functions from Qfontification_functions to fontify
3622 regions of text. */
3623
3624 static enum prop_handled
3625 handle_fontified_prop (struct it *it)
3626 {
3627 Lisp_Object prop, pos;
3628 enum prop_handled handled = HANDLED_NORMALLY;
3629
3630 if (!NILP (Vmemory_full))
3631 return handled;
3632
3633 /* Get the value of the `fontified' property at IT's current buffer
3634 position. (The `fontified' property doesn't have a special
3635 meaning in strings.) If the value is nil, call functions from
3636 Qfontification_functions. */
3637 if (!STRINGP (it->string)
3638 && it->s == NULL
3639 && !NILP (Vfontification_functions)
3640 && !NILP (Vrun_hooks)
3641 && (pos = make_number (IT_CHARPOS (*it)),
3642 prop = Fget_char_property (pos, Qfontified, Qnil),
3643 /* Ignore the special cased nil value always present at EOB since
3644 no amount of fontifying will be able to change it. */
3645 NILP (prop) && IT_CHARPOS (*it) < Z))
3646 {
3647 ptrdiff_t count = SPECPDL_INDEX ();
3648 Lisp_Object val;
3649 struct buffer *obuf = current_buffer;
3650 ptrdiff_t begv = BEGV, zv = ZV;
3651 bool old_clip_changed = current_buffer->clip_changed;
3652
3653 val = Vfontification_functions;
3654 specbind (Qfontification_functions, Qnil);
3655
3656 eassert (it->end_charpos == ZV);
3657
3658 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3659 safe_call1 (val, pos);
3660 else
3661 {
3662 Lisp_Object fns, fn;
3663 struct gcpro gcpro1, gcpro2;
3664
3665 fns = Qnil;
3666 GCPRO2 (val, fns);
3667
3668 for (; CONSP (val); val = XCDR (val))
3669 {
3670 fn = XCAR (val);
3671
3672 if (EQ (fn, Qt))
3673 {
3674 /* A value of t indicates this hook has a local
3675 binding; it means to run the global binding too.
3676 In a global value, t should not occur. If it
3677 does, we must ignore it to avoid an endless
3678 loop. */
3679 for (fns = Fdefault_value (Qfontification_functions);
3680 CONSP (fns);
3681 fns = XCDR (fns))
3682 {
3683 fn = XCAR (fns);
3684 if (!EQ (fn, Qt))
3685 safe_call1 (fn, pos);
3686 }
3687 }
3688 else
3689 safe_call1 (fn, pos);
3690 }
3691
3692 UNGCPRO;
3693 }
3694
3695 unbind_to (count, Qnil);
3696
3697 /* Fontification functions routinely call `save-restriction'.
3698 Normally, this tags clip_changed, which can confuse redisplay
3699 (see discussion in Bug#6671). Since we don't perform any
3700 special handling of fontification changes in the case where
3701 `save-restriction' isn't called, there's no point doing so in
3702 this case either. So, if the buffer's restrictions are
3703 actually left unchanged, reset clip_changed. */
3704 if (obuf == current_buffer)
3705 {
3706 if (begv == BEGV && zv == ZV)
3707 current_buffer->clip_changed = old_clip_changed;
3708 }
3709 /* There isn't much we can reasonably do to protect against
3710 misbehaving fontification, but here's a fig leaf. */
3711 else if (BUFFER_LIVE_P (obuf))
3712 set_buffer_internal_1 (obuf);
3713
3714 /* The fontification code may have added/removed text.
3715 It could do even a lot worse, but let's at least protect against
3716 the most obvious case where only the text past `pos' gets changed',
3717 as is/was done in grep.el where some escapes sequences are turned
3718 into face properties (bug#7876). */
3719 it->end_charpos = ZV;
3720
3721 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3722 something. This avoids an endless loop if they failed to
3723 fontify the text for which reason ever. */
3724 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3725 handled = HANDLED_RECOMPUTE_PROPS;
3726 }
3727
3728 return handled;
3729 }
3730
3731
3732 \f
3733 /***********************************************************************
3734 Faces
3735 ***********************************************************************/
3736
3737 /* Set up iterator IT from face properties at its current position.
3738 Called from handle_stop. */
3739
3740 static enum prop_handled
3741 handle_face_prop (struct it *it)
3742 {
3743 int new_face_id;
3744 ptrdiff_t next_stop;
3745
3746 if (!STRINGP (it->string))
3747 {
3748 new_face_id
3749 = face_at_buffer_position (it->w,
3750 IT_CHARPOS (*it),
3751 &next_stop,
3752 (IT_CHARPOS (*it)
3753 + TEXT_PROP_DISTANCE_LIMIT),
3754 0, it->base_face_id);
3755
3756 /* Is this a start of a run of characters with box face?
3757 Caveat: this can be called for a freshly initialized
3758 iterator; face_id is -1 in this case. We know that the new
3759 face will not change until limit, i.e. if the new face has a
3760 box, all characters up to limit will have one. But, as
3761 usual, we don't know whether limit is really the end. */
3762 if (new_face_id != it->face_id)
3763 {
3764 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3765 /* If it->face_id is -1, old_face below will be NULL, see
3766 the definition of FACE_FROM_ID. This will happen if this
3767 is the initial call that gets the face. */
3768 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3769
3770 /* If the value of face_id of the iterator is -1, we have to
3771 look in front of IT's position and see whether there is a
3772 face there that's different from new_face_id. */
3773 if (!old_face && IT_CHARPOS (*it) > BEG)
3774 {
3775 int prev_face_id = face_before_it_pos (it);
3776
3777 old_face = FACE_FROM_ID (it->f, prev_face_id);
3778 }
3779
3780 /* If the new face has a box, but the old face does not,
3781 this is the start of a run of characters with box face,
3782 i.e. this character has a shadow on the left side. */
3783 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3784 && (old_face == NULL || !old_face->box));
3785 it->face_box_p = new_face->box != FACE_NO_BOX;
3786 }
3787 }
3788 else
3789 {
3790 int base_face_id;
3791 ptrdiff_t bufpos;
3792 int i;
3793 Lisp_Object from_overlay
3794 = (it->current.overlay_string_index >= 0
3795 ? it->string_overlays[it->current.overlay_string_index
3796 % OVERLAY_STRING_CHUNK_SIZE]
3797 : Qnil);
3798
3799 /* See if we got to this string directly or indirectly from
3800 an overlay property. That includes the before-string or
3801 after-string of an overlay, strings in display properties
3802 provided by an overlay, their text properties, etc.
3803
3804 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3805 if (! NILP (from_overlay))
3806 for (i = it->sp - 1; i >= 0; i--)
3807 {
3808 if (it->stack[i].current.overlay_string_index >= 0)
3809 from_overlay
3810 = it->string_overlays[it->stack[i].current.overlay_string_index
3811 % OVERLAY_STRING_CHUNK_SIZE];
3812 else if (! NILP (it->stack[i].from_overlay))
3813 from_overlay = it->stack[i].from_overlay;
3814
3815 if (!NILP (from_overlay))
3816 break;
3817 }
3818
3819 if (! NILP (from_overlay))
3820 {
3821 bufpos = IT_CHARPOS (*it);
3822 /* For a string from an overlay, the base face depends
3823 only on text properties and ignores overlays. */
3824 base_face_id
3825 = face_for_overlay_string (it->w,
3826 IT_CHARPOS (*it),
3827 &next_stop,
3828 (IT_CHARPOS (*it)
3829 + TEXT_PROP_DISTANCE_LIMIT),
3830 0,
3831 from_overlay);
3832 }
3833 else
3834 {
3835 bufpos = 0;
3836
3837 /* For strings from a `display' property, use the face at
3838 IT's current buffer position as the base face to merge
3839 with, so that overlay strings appear in the same face as
3840 surrounding text, unless they specify their own faces.
3841 For strings from wrap-prefix and line-prefix properties,
3842 use the default face, possibly remapped via
3843 Vface_remapping_alist. */
3844 base_face_id = it->string_from_prefix_prop_p
3845 ? (!NILP (Vface_remapping_alist)
3846 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3847 : DEFAULT_FACE_ID)
3848 : underlying_face_id (it);
3849 }
3850
3851 new_face_id = face_at_string_position (it->w,
3852 it->string,
3853 IT_STRING_CHARPOS (*it),
3854 bufpos,
3855 &next_stop,
3856 base_face_id, 0);
3857
3858 /* Is this a start of a run of characters with box? Caveat:
3859 this can be called for a freshly allocated iterator; face_id
3860 is -1 is this case. We know that the new face will not
3861 change until the next check pos, i.e. if the new face has a
3862 box, all characters up to that position will have a
3863 box. But, as usual, we don't know whether that position
3864 is really the end. */
3865 if (new_face_id != it->face_id)
3866 {
3867 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3868 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3869
3870 /* If new face has a box but old face hasn't, this is the
3871 start of a run of characters with box, i.e. it has a
3872 shadow on the left side. */
3873 it->start_of_box_run_p
3874 = new_face->box && (old_face == NULL || !old_face->box);
3875 it->face_box_p = new_face->box != FACE_NO_BOX;
3876 }
3877 }
3878
3879 it->face_id = new_face_id;
3880 return HANDLED_NORMALLY;
3881 }
3882
3883
3884 /* Return the ID of the face ``underlying'' IT's current position,
3885 which is in a string. If the iterator is associated with a
3886 buffer, return the face at IT's current buffer position.
3887 Otherwise, use the iterator's base_face_id. */
3888
3889 static int
3890 underlying_face_id (struct it *it)
3891 {
3892 int face_id = it->base_face_id, i;
3893
3894 eassert (STRINGP (it->string));
3895
3896 for (i = it->sp - 1; i >= 0; --i)
3897 if (NILP (it->stack[i].string))
3898 face_id = it->stack[i].face_id;
3899
3900 return face_id;
3901 }
3902
3903
3904 /* Compute the face one character before or after the current position
3905 of IT, in the visual order. BEFORE_P non-zero means get the face
3906 in front (to the left in L2R paragraphs, to the right in R2L
3907 paragraphs) of IT's screen position. Value is the ID of the face. */
3908
3909 static int
3910 face_before_or_after_it_pos (struct it *it, int before_p)
3911 {
3912 int face_id, limit;
3913 ptrdiff_t next_check_charpos;
3914 struct it it_copy;
3915 void *it_copy_data = NULL;
3916
3917 eassert (it->s == NULL);
3918
3919 if (STRINGP (it->string))
3920 {
3921 ptrdiff_t bufpos, charpos;
3922 int base_face_id;
3923
3924 /* No face change past the end of the string (for the case
3925 we are padding with spaces). No face change before the
3926 string start. */
3927 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3928 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3929 return it->face_id;
3930
3931 if (!it->bidi_p)
3932 {
3933 /* Set charpos to the position before or after IT's current
3934 position, in the logical order, which in the non-bidi
3935 case is the same as the visual order. */
3936 if (before_p)
3937 charpos = IT_STRING_CHARPOS (*it) - 1;
3938 else if (it->what == IT_COMPOSITION)
3939 /* For composition, we must check the character after the
3940 composition. */
3941 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3942 else
3943 charpos = IT_STRING_CHARPOS (*it) + 1;
3944 }
3945 else
3946 {
3947 if (before_p)
3948 {
3949 /* With bidi iteration, the character before the current
3950 in the visual order cannot be found by simple
3951 iteration, because "reverse" reordering is not
3952 supported. Instead, we need to use the move_it_*
3953 family of functions. */
3954 /* Ignore face changes before the first visible
3955 character on this display line. */
3956 if (it->current_x <= it->first_visible_x)
3957 return it->face_id;
3958 SAVE_IT (it_copy, *it, it_copy_data);
3959 /* Implementation note: Since move_it_in_display_line
3960 works in the iterator geometry, and thinks the first
3961 character is always the leftmost, even in R2L lines,
3962 we don't need to distinguish between the R2L and L2R
3963 cases here. */
3964 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3965 it_copy.current_x - 1, MOVE_TO_X);
3966 charpos = IT_STRING_CHARPOS (it_copy);
3967 RESTORE_IT (it, it, it_copy_data);
3968 }
3969 else
3970 {
3971 /* Set charpos to the string position of the character
3972 that comes after IT's current position in the visual
3973 order. */
3974 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3975
3976 it_copy = *it;
3977 while (n--)
3978 bidi_move_to_visually_next (&it_copy.bidi_it);
3979
3980 charpos = it_copy.bidi_it.charpos;
3981 }
3982 }
3983 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3984
3985 if (it->current.overlay_string_index >= 0)
3986 bufpos = IT_CHARPOS (*it);
3987 else
3988 bufpos = 0;
3989
3990 base_face_id = underlying_face_id (it);
3991
3992 /* Get the face for ASCII, or unibyte. */
3993 face_id = face_at_string_position (it->w,
3994 it->string,
3995 charpos,
3996 bufpos,
3997 &next_check_charpos,
3998 base_face_id, 0);
3999
4000 /* Correct the face for charsets different from ASCII. Do it
4001 for the multibyte case only. The face returned above is
4002 suitable for unibyte text if IT->string is unibyte. */
4003 if (STRING_MULTIBYTE (it->string))
4004 {
4005 struct text_pos pos1 = string_pos (charpos, it->string);
4006 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4007 int c, len;
4008 struct face *face = FACE_FROM_ID (it->f, face_id);
4009
4010 c = string_char_and_length (p, &len);
4011 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4012 }
4013 }
4014 else
4015 {
4016 struct text_pos pos;
4017
4018 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4019 || (IT_CHARPOS (*it) <= BEGV && before_p))
4020 return it->face_id;
4021
4022 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4023 pos = it->current.pos;
4024
4025 if (!it->bidi_p)
4026 {
4027 if (before_p)
4028 DEC_TEXT_POS (pos, it->multibyte_p);
4029 else
4030 {
4031 if (it->what == IT_COMPOSITION)
4032 {
4033 /* For composition, we must check the position after
4034 the composition. */
4035 pos.charpos += it->cmp_it.nchars;
4036 pos.bytepos += it->len;
4037 }
4038 else
4039 INC_TEXT_POS (pos, it->multibyte_p);
4040 }
4041 }
4042 else
4043 {
4044 if (before_p)
4045 {
4046 /* With bidi iteration, the character before the current
4047 in the visual order cannot be found by simple
4048 iteration, because "reverse" reordering is not
4049 supported. Instead, we need to use the move_it_*
4050 family of functions. */
4051 /* Ignore face changes before the first visible
4052 character on this display line. */
4053 if (it->current_x <= it->first_visible_x)
4054 return it->face_id;
4055 SAVE_IT (it_copy, *it, it_copy_data);
4056 /* Implementation note: Since move_it_in_display_line
4057 works in the iterator geometry, and thinks the first
4058 character is always the leftmost, even in R2L lines,
4059 we don't need to distinguish between the R2L and L2R
4060 cases here. */
4061 move_it_in_display_line (&it_copy, ZV,
4062 it_copy.current_x - 1, MOVE_TO_X);
4063 pos = it_copy.current.pos;
4064 RESTORE_IT (it, it, it_copy_data);
4065 }
4066 else
4067 {
4068 /* Set charpos to the buffer position of the character
4069 that comes after IT's current position in the visual
4070 order. */
4071 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4072
4073 it_copy = *it;
4074 while (n--)
4075 bidi_move_to_visually_next (&it_copy.bidi_it);
4076
4077 SET_TEXT_POS (pos,
4078 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4079 }
4080 }
4081 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4082
4083 /* Determine face for CHARSET_ASCII, or unibyte. */
4084 face_id = face_at_buffer_position (it->w,
4085 CHARPOS (pos),
4086 &next_check_charpos,
4087 limit, 0, -1);
4088
4089 /* Correct the face for charsets different from ASCII. Do it
4090 for the multibyte case only. The face returned above is
4091 suitable for unibyte text if current_buffer is unibyte. */
4092 if (it->multibyte_p)
4093 {
4094 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4095 struct face *face = FACE_FROM_ID (it->f, face_id);
4096 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4097 }
4098 }
4099
4100 return face_id;
4101 }
4102
4103
4104 \f
4105 /***********************************************************************
4106 Invisible text
4107 ***********************************************************************/
4108
4109 /* Set up iterator IT from invisible properties at its current
4110 position. Called from handle_stop. */
4111
4112 static enum prop_handled
4113 handle_invisible_prop (struct it *it)
4114 {
4115 enum prop_handled handled = HANDLED_NORMALLY;
4116 int invis_p;
4117 Lisp_Object prop;
4118
4119 if (STRINGP (it->string))
4120 {
4121 Lisp_Object end_charpos, limit, charpos;
4122
4123 /* Get the value of the invisible text property at the
4124 current position. Value will be nil if there is no such
4125 property. */
4126 charpos = make_number (IT_STRING_CHARPOS (*it));
4127 prop = Fget_text_property (charpos, Qinvisible, it->string);
4128 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4129
4130 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4131 {
4132 /* Record whether we have to display an ellipsis for the
4133 invisible text. */
4134 int display_ellipsis_p = (invis_p == 2);
4135 ptrdiff_t len, endpos;
4136
4137 handled = HANDLED_RECOMPUTE_PROPS;
4138
4139 /* Get the position at which the next visible text can be
4140 found in IT->string, if any. */
4141 endpos = len = SCHARS (it->string);
4142 XSETINT (limit, len);
4143 do
4144 {
4145 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4146 it->string, limit);
4147 if (INTEGERP (end_charpos))
4148 {
4149 endpos = XFASTINT (end_charpos);
4150 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4151 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4152 if (invis_p == 2)
4153 display_ellipsis_p = 1;
4154 }
4155 }
4156 while (invis_p && endpos < len);
4157
4158 if (display_ellipsis_p)
4159 it->ellipsis_p = 1;
4160
4161 if (endpos < len)
4162 {
4163 /* Text at END_CHARPOS is visible. Move IT there. */
4164 struct text_pos old;
4165 ptrdiff_t oldpos;
4166
4167 old = it->current.string_pos;
4168 oldpos = CHARPOS (old);
4169 if (it->bidi_p)
4170 {
4171 if (it->bidi_it.first_elt
4172 && it->bidi_it.charpos < SCHARS (it->string))
4173 bidi_paragraph_init (it->paragraph_embedding,
4174 &it->bidi_it, 1);
4175 /* Bidi-iterate out of the invisible text. */
4176 do
4177 {
4178 bidi_move_to_visually_next (&it->bidi_it);
4179 }
4180 while (oldpos <= it->bidi_it.charpos
4181 && it->bidi_it.charpos < endpos);
4182
4183 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4184 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4185 if (IT_CHARPOS (*it) >= endpos)
4186 it->prev_stop = endpos;
4187 }
4188 else
4189 {
4190 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4191 compute_string_pos (&it->current.string_pos, old, it->string);
4192 }
4193 }
4194 else
4195 {
4196 /* The rest of the string is invisible. If this is an
4197 overlay string, proceed with the next overlay string
4198 or whatever comes and return a character from there. */
4199 if (it->current.overlay_string_index >= 0
4200 && !display_ellipsis_p)
4201 {
4202 next_overlay_string (it);
4203 /* Don't check for overlay strings when we just
4204 finished processing them. */
4205 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4206 }
4207 else
4208 {
4209 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4210 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4211 }
4212 }
4213 }
4214 }
4215 else
4216 {
4217 ptrdiff_t newpos, next_stop, start_charpos, tem;
4218 Lisp_Object pos, overlay;
4219
4220 /* First of all, is there invisible text at this position? */
4221 tem = start_charpos = IT_CHARPOS (*it);
4222 pos = make_number (tem);
4223 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4224 &overlay);
4225 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4226
4227 /* If we are on invisible text, skip over it. */
4228 if (invis_p && start_charpos < it->end_charpos)
4229 {
4230 /* Record whether we have to display an ellipsis for the
4231 invisible text. */
4232 int display_ellipsis_p = invis_p == 2;
4233
4234 handled = HANDLED_RECOMPUTE_PROPS;
4235
4236 /* Loop skipping over invisible text. The loop is left at
4237 ZV or with IT on the first char being visible again. */
4238 do
4239 {
4240 /* Try to skip some invisible text. Return value is the
4241 position reached which can be equal to where we start
4242 if there is nothing invisible there. This skips both
4243 over invisible text properties and overlays with
4244 invisible property. */
4245 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4246
4247 /* If we skipped nothing at all we weren't at invisible
4248 text in the first place. If everything to the end of
4249 the buffer was skipped, end the loop. */
4250 if (newpos == tem || newpos >= ZV)
4251 invis_p = 0;
4252 else
4253 {
4254 /* We skipped some characters but not necessarily
4255 all there are. Check if we ended up on visible
4256 text. Fget_char_property returns the property of
4257 the char before the given position, i.e. if we
4258 get invis_p = 0, this means that the char at
4259 newpos is visible. */
4260 pos = make_number (newpos);
4261 prop = Fget_char_property (pos, Qinvisible, it->window);
4262 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4263 }
4264
4265 /* If we ended up on invisible text, proceed to
4266 skip starting with next_stop. */
4267 if (invis_p)
4268 tem = next_stop;
4269
4270 /* If there are adjacent invisible texts, don't lose the
4271 second one's ellipsis. */
4272 if (invis_p == 2)
4273 display_ellipsis_p = 1;
4274 }
4275 while (invis_p);
4276
4277 /* The position newpos is now either ZV or on visible text. */
4278 if (it->bidi_p)
4279 {
4280 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4281 int on_newline =
4282 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4283 int after_newline =
4284 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4285
4286 /* If the invisible text ends on a newline or on a
4287 character after a newline, we can avoid the costly,
4288 character by character, bidi iteration to NEWPOS, and
4289 instead simply reseat the iterator there. That's
4290 because all bidi reordering information is tossed at
4291 the newline. This is a big win for modes that hide
4292 complete lines, like Outline, Org, etc. */
4293 if (on_newline || after_newline)
4294 {
4295 struct text_pos tpos;
4296 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4297
4298 SET_TEXT_POS (tpos, newpos, bpos);
4299 reseat_1 (it, tpos, 0);
4300 /* If we reseat on a newline/ZV, we need to prep the
4301 bidi iterator for advancing to the next character
4302 after the newline/EOB, keeping the current paragraph
4303 direction (so that PRODUCE_GLYPHS does TRT wrt
4304 prepending/appending glyphs to a glyph row). */
4305 if (on_newline)
4306 {
4307 it->bidi_it.first_elt = 0;
4308 it->bidi_it.paragraph_dir = pdir;
4309 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4310 it->bidi_it.nchars = 1;
4311 it->bidi_it.ch_len = 1;
4312 }
4313 }
4314 else /* Must use the slow method. */
4315 {
4316 /* With bidi iteration, the region of invisible text
4317 could start and/or end in the middle of a
4318 non-base embedding level. Therefore, we need to
4319 skip invisible text using the bidi iterator,
4320 starting at IT's current position, until we find
4321 ourselves outside of the invisible text.
4322 Skipping invisible text _after_ bidi iteration
4323 avoids affecting the visual order of the
4324 displayed text when invisible properties are
4325 added or removed. */
4326 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4327 {
4328 /* If we were `reseat'ed to a new paragraph,
4329 determine the paragraph base direction. We
4330 need to do it now because
4331 next_element_from_buffer may not have a
4332 chance to do it, if we are going to skip any
4333 text at the beginning, which resets the
4334 FIRST_ELT flag. */
4335 bidi_paragraph_init (it->paragraph_embedding,
4336 &it->bidi_it, 1);
4337 }
4338 do
4339 {
4340 bidi_move_to_visually_next (&it->bidi_it);
4341 }
4342 while (it->stop_charpos <= it->bidi_it.charpos
4343 && it->bidi_it.charpos < newpos);
4344 IT_CHARPOS (*it) = it->bidi_it.charpos;
4345 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4346 /* If we overstepped NEWPOS, record its position in
4347 the iterator, so that we skip invisible text if
4348 later the bidi iteration lands us in the
4349 invisible region again. */
4350 if (IT_CHARPOS (*it) >= newpos)
4351 it->prev_stop = newpos;
4352 }
4353 }
4354 else
4355 {
4356 IT_CHARPOS (*it) = newpos;
4357 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4358 }
4359
4360 /* If there are before-strings at the start of invisible
4361 text, and the text is invisible because of a text
4362 property, arrange to show before-strings because 20.x did
4363 it that way. (If the text is invisible because of an
4364 overlay property instead of a text property, this is
4365 already handled in the overlay code.) */
4366 if (NILP (overlay)
4367 && get_overlay_strings (it, it->stop_charpos))
4368 {
4369 handled = HANDLED_RECOMPUTE_PROPS;
4370 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4371 }
4372 else if (display_ellipsis_p)
4373 {
4374 /* Make sure that the glyphs of the ellipsis will get
4375 correct `charpos' values. If we would not update
4376 it->position here, the glyphs would belong to the
4377 last visible character _before_ the invisible
4378 text, which confuses `set_cursor_from_row'.
4379
4380 We use the last invisible position instead of the
4381 first because this way the cursor is always drawn on
4382 the first "." of the ellipsis, whenever PT is inside
4383 the invisible text. Otherwise the cursor would be
4384 placed _after_ the ellipsis when the point is after the
4385 first invisible character. */
4386 if (!STRINGP (it->object))
4387 {
4388 it->position.charpos = newpos - 1;
4389 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4390 }
4391 it->ellipsis_p = 1;
4392 /* Let the ellipsis display before
4393 considering any properties of the following char.
4394 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4395 handled = HANDLED_RETURN;
4396 }
4397 }
4398 }
4399
4400 return handled;
4401 }
4402
4403
4404 /* Make iterator IT return `...' next.
4405 Replaces LEN characters from buffer. */
4406
4407 static void
4408 setup_for_ellipsis (struct it *it, int len)
4409 {
4410 /* Use the display table definition for `...'. Invalid glyphs
4411 will be handled by the method returning elements from dpvec. */
4412 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4413 {
4414 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4415 it->dpvec = v->contents;
4416 it->dpend = v->contents + v->header.size;
4417 }
4418 else
4419 {
4420 /* Default `...'. */
4421 it->dpvec = default_invis_vector;
4422 it->dpend = default_invis_vector + 3;
4423 }
4424
4425 it->dpvec_char_len = len;
4426 it->current.dpvec_index = 0;
4427 it->dpvec_face_id = -1;
4428
4429 /* Remember the current face id in case glyphs specify faces.
4430 IT's face is restored in set_iterator_to_next.
4431 saved_face_id was set to preceding char's face in handle_stop. */
4432 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4433 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4434
4435 it->method = GET_FROM_DISPLAY_VECTOR;
4436 it->ellipsis_p = 1;
4437 }
4438
4439
4440 \f
4441 /***********************************************************************
4442 'display' property
4443 ***********************************************************************/
4444
4445 /* Set up iterator IT from `display' property at its current position.
4446 Called from handle_stop.
4447 We return HANDLED_RETURN if some part of the display property
4448 overrides the display of the buffer text itself.
4449 Otherwise we return HANDLED_NORMALLY. */
4450
4451 static enum prop_handled
4452 handle_display_prop (struct it *it)
4453 {
4454 Lisp_Object propval, object, overlay;
4455 struct text_pos *position;
4456 ptrdiff_t bufpos;
4457 /* Nonzero if some property replaces the display of the text itself. */
4458 int display_replaced_p = 0;
4459
4460 if (STRINGP (it->string))
4461 {
4462 object = it->string;
4463 position = &it->current.string_pos;
4464 bufpos = CHARPOS (it->current.pos);
4465 }
4466 else
4467 {
4468 XSETWINDOW (object, it->w);
4469 position = &it->current.pos;
4470 bufpos = CHARPOS (*position);
4471 }
4472
4473 /* Reset those iterator values set from display property values. */
4474 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4475 it->space_width = Qnil;
4476 it->font_height = Qnil;
4477 it->voffset = 0;
4478
4479 /* We don't support recursive `display' properties, i.e. string
4480 values that have a string `display' property, that have a string
4481 `display' property etc. */
4482 if (!it->string_from_display_prop_p)
4483 it->area = TEXT_AREA;
4484
4485 propval = get_char_property_and_overlay (make_number (position->charpos),
4486 Qdisplay, object, &overlay);
4487 if (NILP (propval))
4488 return HANDLED_NORMALLY;
4489 /* Now OVERLAY is the overlay that gave us this property, or nil
4490 if it was a text property. */
4491
4492 if (!STRINGP (it->string))
4493 object = it->w->contents;
4494
4495 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4496 position, bufpos,
4497 FRAME_WINDOW_P (it->f));
4498
4499 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4500 }
4501
4502 /* Subroutine of handle_display_prop. Returns non-zero if the display
4503 specification in SPEC is a replacing specification, i.e. it would
4504 replace the text covered by `display' property with something else,
4505 such as an image or a display string. If SPEC includes any kind or
4506 `(space ...) specification, the value is 2; this is used by
4507 compute_display_string_pos, which see.
4508
4509 See handle_single_display_spec for documentation of arguments.
4510 frame_window_p is non-zero if the window being redisplayed is on a
4511 GUI frame; this argument is used only if IT is NULL, see below.
4512
4513 IT can be NULL, if this is called by the bidi reordering code
4514 through compute_display_string_pos, which see. In that case, this
4515 function only examines SPEC, but does not otherwise "handle" it, in
4516 the sense that it doesn't set up members of IT from the display
4517 spec. */
4518 static int
4519 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4520 Lisp_Object overlay, struct text_pos *position,
4521 ptrdiff_t bufpos, int frame_window_p)
4522 {
4523 int replacing_p = 0;
4524 int rv;
4525
4526 if (CONSP (spec)
4527 /* Simple specifications. */
4528 && !EQ (XCAR (spec), Qimage)
4529 && !EQ (XCAR (spec), Qspace)
4530 && !EQ (XCAR (spec), Qwhen)
4531 && !EQ (XCAR (spec), Qslice)
4532 && !EQ (XCAR (spec), Qspace_width)
4533 && !EQ (XCAR (spec), Qheight)
4534 && !EQ (XCAR (spec), Qraise)
4535 /* Marginal area specifications. */
4536 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4537 && !EQ (XCAR (spec), Qleft_fringe)
4538 && !EQ (XCAR (spec), Qright_fringe)
4539 && !NILP (XCAR (spec)))
4540 {
4541 for (; CONSP (spec); spec = XCDR (spec))
4542 {
4543 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4544 overlay, position, bufpos,
4545 replacing_p, frame_window_p)))
4546 {
4547 replacing_p = rv;
4548 /* If some text in a string is replaced, `position' no
4549 longer points to the position of `object'. */
4550 if (!it || STRINGP (object))
4551 break;
4552 }
4553 }
4554 }
4555 else if (VECTORP (spec))
4556 {
4557 ptrdiff_t i;
4558 for (i = 0; i < ASIZE (spec); ++i)
4559 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4560 overlay, position, bufpos,
4561 replacing_p, frame_window_p)))
4562 {
4563 replacing_p = rv;
4564 /* If some text in a string is replaced, `position' no
4565 longer points to the position of `object'. */
4566 if (!it || STRINGP (object))
4567 break;
4568 }
4569 }
4570 else
4571 {
4572 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4573 position, bufpos, 0,
4574 frame_window_p)))
4575 replacing_p = rv;
4576 }
4577
4578 return replacing_p;
4579 }
4580
4581 /* Value is the position of the end of the `display' property starting
4582 at START_POS in OBJECT. */
4583
4584 static struct text_pos
4585 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4586 {
4587 Lisp_Object end;
4588 struct text_pos end_pos;
4589
4590 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4591 Qdisplay, object, Qnil);
4592 CHARPOS (end_pos) = XFASTINT (end);
4593 if (STRINGP (object))
4594 compute_string_pos (&end_pos, start_pos, it->string);
4595 else
4596 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4597
4598 return end_pos;
4599 }
4600
4601
4602 /* Set up IT from a single `display' property specification SPEC. OBJECT
4603 is the object in which the `display' property was found. *POSITION
4604 is the position in OBJECT at which the `display' property was found.
4605 BUFPOS is the buffer position of OBJECT (different from POSITION if
4606 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4607 previously saw a display specification which already replaced text
4608 display with something else, for example an image; we ignore such
4609 properties after the first one has been processed.
4610
4611 OVERLAY is the overlay this `display' property came from,
4612 or nil if it was a text property.
4613
4614 If SPEC is a `space' or `image' specification, and in some other
4615 cases too, set *POSITION to the position where the `display'
4616 property ends.
4617
4618 If IT is NULL, only examine the property specification in SPEC, but
4619 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4620 is intended to be displayed in a window on a GUI frame.
4621
4622 Value is non-zero if something was found which replaces the display
4623 of buffer or string text. */
4624
4625 static int
4626 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4627 Lisp_Object overlay, struct text_pos *position,
4628 ptrdiff_t bufpos, int display_replaced_p,
4629 int frame_window_p)
4630 {
4631 Lisp_Object form;
4632 Lisp_Object location, value;
4633 struct text_pos start_pos = *position;
4634 int valid_p;
4635
4636 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4637 If the result is non-nil, use VALUE instead of SPEC. */
4638 form = Qt;
4639 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4640 {
4641 spec = XCDR (spec);
4642 if (!CONSP (spec))
4643 return 0;
4644 form = XCAR (spec);
4645 spec = XCDR (spec);
4646 }
4647
4648 if (!NILP (form) && !EQ (form, Qt))
4649 {
4650 ptrdiff_t count = SPECPDL_INDEX ();
4651 struct gcpro gcpro1;
4652
4653 /* Bind `object' to the object having the `display' property, a
4654 buffer or string. Bind `position' to the position in the
4655 object where the property was found, and `buffer-position'
4656 to the current position in the buffer. */
4657
4658 if (NILP (object))
4659 XSETBUFFER (object, current_buffer);
4660 specbind (Qobject, object);
4661 specbind (Qposition, make_number (CHARPOS (*position)));
4662 specbind (Qbuffer_position, make_number (bufpos));
4663 GCPRO1 (form);
4664 form = safe_eval (form);
4665 UNGCPRO;
4666 unbind_to (count, Qnil);
4667 }
4668
4669 if (NILP (form))
4670 return 0;
4671
4672 /* Handle `(height HEIGHT)' specifications. */
4673 if (CONSP (spec)
4674 && EQ (XCAR (spec), Qheight)
4675 && CONSP (XCDR (spec)))
4676 {
4677 if (it)
4678 {
4679 if (!FRAME_WINDOW_P (it->f))
4680 return 0;
4681
4682 it->font_height = XCAR (XCDR (spec));
4683 if (!NILP (it->font_height))
4684 {
4685 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4686 int new_height = -1;
4687
4688 if (CONSP (it->font_height)
4689 && (EQ (XCAR (it->font_height), Qplus)
4690 || EQ (XCAR (it->font_height), Qminus))
4691 && CONSP (XCDR (it->font_height))
4692 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4693 {
4694 /* `(+ N)' or `(- N)' where N is an integer. */
4695 int steps = XINT (XCAR (XCDR (it->font_height)));
4696 if (EQ (XCAR (it->font_height), Qplus))
4697 steps = - steps;
4698 it->face_id = smaller_face (it->f, it->face_id, steps);
4699 }
4700 else if (FUNCTIONP (it->font_height))
4701 {
4702 /* Call function with current height as argument.
4703 Value is the new height. */
4704 Lisp_Object height;
4705 height = safe_call1 (it->font_height,
4706 face->lface[LFACE_HEIGHT_INDEX]);
4707 if (NUMBERP (height))
4708 new_height = XFLOATINT (height);
4709 }
4710 else if (NUMBERP (it->font_height))
4711 {
4712 /* Value is a multiple of the canonical char height. */
4713 struct face *f;
4714
4715 f = FACE_FROM_ID (it->f,
4716 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4717 new_height = (XFLOATINT (it->font_height)
4718 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4719 }
4720 else
4721 {
4722 /* Evaluate IT->font_height with `height' bound to the
4723 current specified height to get the new height. */
4724 ptrdiff_t count = SPECPDL_INDEX ();
4725
4726 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4727 value = safe_eval (it->font_height);
4728 unbind_to (count, Qnil);
4729
4730 if (NUMBERP (value))
4731 new_height = XFLOATINT (value);
4732 }
4733
4734 if (new_height > 0)
4735 it->face_id = face_with_height (it->f, it->face_id, new_height);
4736 }
4737 }
4738
4739 return 0;
4740 }
4741
4742 /* Handle `(space-width WIDTH)'. */
4743 if (CONSP (spec)
4744 && EQ (XCAR (spec), Qspace_width)
4745 && CONSP (XCDR (spec)))
4746 {
4747 if (it)
4748 {
4749 if (!FRAME_WINDOW_P (it->f))
4750 return 0;
4751
4752 value = XCAR (XCDR (spec));
4753 if (NUMBERP (value) && XFLOATINT (value) > 0)
4754 it->space_width = value;
4755 }
4756
4757 return 0;
4758 }
4759
4760 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4761 if (CONSP (spec)
4762 && EQ (XCAR (spec), Qslice))
4763 {
4764 Lisp_Object tem;
4765
4766 if (it)
4767 {
4768 if (!FRAME_WINDOW_P (it->f))
4769 return 0;
4770
4771 if (tem = XCDR (spec), CONSP (tem))
4772 {
4773 it->slice.x = XCAR (tem);
4774 if (tem = XCDR (tem), CONSP (tem))
4775 {
4776 it->slice.y = XCAR (tem);
4777 if (tem = XCDR (tem), CONSP (tem))
4778 {
4779 it->slice.width = XCAR (tem);
4780 if (tem = XCDR (tem), CONSP (tem))
4781 it->slice.height = XCAR (tem);
4782 }
4783 }
4784 }
4785 }
4786
4787 return 0;
4788 }
4789
4790 /* Handle `(raise FACTOR)'. */
4791 if (CONSP (spec)
4792 && EQ (XCAR (spec), Qraise)
4793 && CONSP (XCDR (spec)))
4794 {
4795 if (it)
4796 {
4797 if (!FRAME_WINDOW_P (it->f))
4798 return 0;
4799
4800 #ifdef HAVE_WINDOW_SYSTEM
4801 value = XCAR (XCDR (spec));
4802 if (NUMBERP (value))
4803 {
4804 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4805 it->voffset = - (XFLOATINT (value)
4806 * (FONT_HEIGHT (face->font)));
4807 }
4808 #endif /* HAVE_WINDOW_SYSTEM */
4809 }
4810
4811 return 0;
4812 }
4813
4814 /* Don't handle the other kinds of display specifications
4815 inside a string that we got from a `display' property. */
4816 if (it && it->string_from_display_prop_p)
4817 return 0;
4818
4819 /* Characters having this form of property are not displayed, so
4820 we have to find the end of the property. */
4821 if (it)
4822 {
4823 start_pos = *position;
4824 *position = display_prop_end (it, object, start_pos);
4825 }
4826 value = Qnil;
4827
4828 /* Stop the scan at that end position--we assume that all
4829 text properties change there. */
4830 if (it)
4831 it->stop_charpos = position->charpos;
4832
4833 /* Handle `(left-fringe BITMAP [FACE])'
4834 and `(right-fringe BITMAP [FACE])'. */
4835 if (CONSP (spec)
4836 && (EQ (XCAR (spec), Qleft_fringe)
4837 || EQ (XCAR (spec), Qright_fringe))
4838 && CONSP (XCDR (spec)))
4839 {
4840 int fringe_bitmap;
4841
4842 if (it)
4843 {
4844 if (!FRAME_WINDOW_P (it->f))
4845 /* If we return here, POSITION has been advanced
4846 across the text with this property. */
4847 {
4848 /* Synchronize the bidi iterator with POSITION. This is
4849 needed because we are not going to push the iterator
4850 on behalf of this display property, so there will be
4851 no pop_it call to do this synchronization for us. */
4852 if (it->bidi_p)
4853 {
4854 it->position = *position;
4855 iterate_out_of_display_property (it);
4856 *position = it->position;
4857 }
4858 return 1;
4859 }
4860 }
4861 else if (!frame_window_p)
4862 return 1;
4863
4864 #ifdef HAVE_WINDOW_SYSTEM
4865 value = XCAR (XCDR (spec));
4866 if (!SYMBOLP (value)
4867 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4868 /* If we return here, POSITION has been advanced
4869 across the text with this property. */
4870 {
4871 if (it && it->bidi_p)
4872 {
4873 it->position = *position;
4874 iterate_out_of_display_property (it);
4875 *position = it->position;
4876 }
4877 return 1;
4878 }
4879
4880 if (it)
4881 {
4882 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4883
4884 if (CONSP (XCDR (XCDR (spec))))
4885 {
4886 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4887 int face_id2 = lookup_derived_face (it->f, face_name,
4888 FRINGE_FACE_ID, 0);
4889 if (face_id2 >= 0)
4890 face_id = face_id2;
4891 }
4892
4893 /* Save current settings of IT so that we can restore them
4894 when we are finished with the glyph property value. */
4895 push_it (it, position);
4896
4897 it->area = TEXT_AREA;
4898 it->what = IT_IMAGE;
4899 it->image_id = -1; /* no image */
4900 it->position = start_pos;
4901 it->object = NILP (object) ? it->w->contents : object;
4902 it->method = GET_FROM_IMAGE;
4903 it->from_overlay = Qnil;
4904 it->face_id = face_id;
4905 it->from_disp_prop_p = 1;
4906
4907 /* Say that we haven't consumed the characters with
4908 `display' property yet. The call to pop_it in
4909 set_iterator_to_next will clean this up. */
4910 *position = start_pos;
4911
4912 if (EQ (XCAR (spec), Qleft_fringe))
4913 {
4914 it->left_user_fringe_bitmap = fringe_bitmap;
4915 it->left_user_fringe_face_id = face_id;
4916 }
4917 else
4918 {
4919 it->right_user_fringe_bitmap = fringe_bitmap;
4920 it->right_user_fringe_face_id = face_id;
4921 }
4922 }
4923 #endif /* HAVE_WINDOW_SYSTEM */
4924 return 1;
4925 }
4926
4927 /* Prepare to handle `((margin left-margin) ...)',
4928 `((margin right-margin) ...)' and `((margin nil) ...)'
4929 prefixes for display specifications. */
4930 location = Qunbound;
4931 if (CONSP (spec) && CONSP (XCAR (spec)))
4932 {
4933 Lisp_Object tem;
4934
4935 value = XCDR (spec);
4936 if (CONSP (value))
4937 value = XCAR (value);
4938
4939 tem = XCAR (spec);
4940 if (EQ (XCAR (tem), Qmargin)
4941 && (tem = XCDR (tem),
4942 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4943 (NILP (tem)
4944 || EQ (tem, Qleft_margin)
4945 || EQ (tem, Qright_margin))))
4946 location = tem;
4947 }
4948
4949 if (EQ (location, Qunbound))
4950 {
4951 location = Qnil;
4952 value = spec;
4953 }
4954
4955 /* After this point, VALUE is the property after any
4956 margin prefix has been stripped. It must be a string,
4957 an image specification, or `(space ...)'.
4958
4959 LOCATION specifies where to display: `left-margin',
4960 `right-margin' or nil. */
4961
4962 valid_p = (STRINGP (value)
4963 #ifdef HAVE_WINDOW_SYSTEM
4964 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4965 && valid_image_p (value))
4966 #endif /* not HAVE_WINDOW_SYSTEM */
4967 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4968
4969 if (valid_p && !display_replaced_p)
4970 {
4971 int retval = 1;
4972
4973 if (!it)
4974 {
4975 /* Callers need to know whether the display spec is any kind
4976 of `(space ...)' spec that is about to affect text-area
4977 display. */
4978 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4979 retval = 2;
4980 return retval;
4981 }
4982
4983 /* Save current settings of IT so that we can restore them
4984 when we are finished with the glyph property value. */
4985 push_it (it, position);
4986 it->from_overlay = overlay;
4987 it->from_disp_prop_p = 1;
4988
4989 if (NILP (location))
4990 it->area = TEXT_AREA;
4991 else if (EQ (location, Qleft_margin))
4992 it->area = LEFT_MARGIN_AREA;
4993 else
4994 it->area = RIGHT_MARGIN_AREA;
4995
4996 if (STRINGP (value))
4997 {
4998 it->string = value;
4999 it->multibyte_p = STRING_MULTIBYTE (it->string);
5000 it->current.overlay_string_index = -1;
5001 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5002 it->end_charpos = it->string_nchars = SCHARS (it->string);
5003 it->method = GET_FROM_STRING;
5004 it->stop_charpos = 0;
5005 it->prev_stop = 0;
5006 it->base_level_stop = 0;
5007 it->string_from_display_prop_p = 1;
5008 /* Say that we haven't consumed the characters with
5009 `display' property yet. The call to pop_it in
5010 set_iterator_to_next will clean this up. */
5011 if (BUFFERP (object))
5012 *position = start_pos;
5013
5014 /* Force paragraph direction to be that of the parent
5015 object. If the parent object's paragraph direction is
5016 not yet determined, default to L2R. */
5017 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5018 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5019 else
5020 it->paragraph_embedding = L2R;
5021
5022 /* Set up the bidi iterator for this display string. */
5023 if (it->bidi_p)
5024 {
5025 it->bidi_it.string.lstring = it->string;
5026 it->bidi_it.string.s = NULL;
5027 it->bidi_it.string.schars = it->end_charpos;
5028 it->bidi_it.string.bufpos = bufpos;
5029 it->bidi_it.string.from_disp_str = 1;
5030 it->bidi_it.string.unibyte = !it->multibyte_p;
5031 it->bidi_it.w = it->w;
5032 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5033 }
5034 }
5035 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5036 {
5037 it->method = GET_FROM_STRETCH;
5038 it->object = value;
5039 *position = it->position = start_pos;
5040 retval = 1 + (it->area == TEXT_AREA);
5041 }
5042 #ifdef HAVE_WINDOW_SYSTEM
5043 else
5044 {
5045 it->what = IT_IMAGE;
5046 it->image_id = lookup_image (it->f, value);
5047 it->position = start_pos;
5048 it->object = NILP (object) ? it->w->contents : object;
5049 it->method = GET_FROM_IMAGE;
5050
5051 /* Say that we haven't consumed the characters with
5052 `display' property yet. The call to pop_it in
5053 set_iterator_to_next will clean this up. */
5054 *position = start_pos;
5055 }
5056 #endif /* HAVE_WINDOW_SYSTEM */
5057
5058 return retval;
5059 }
5060
5061 /* Invalid property or property not supported. Restore
5062 POSITION to what it was before. */
5063 *position = start_pos;
5064 return 0;
5065 }
5066
5067 /* Check if PROP is a display property value whose text should be
5068 treated as intangible. OVERLAY is the overlay from which PROP
5069 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5070 specify the buffer position covered by PROP. */
5071
5072 int
5073 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5074 ptrdiff_t charpos, ptrdiff_t bytepos)
5075 {
5076 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5077 struct text_pos position;
5078
5079 SET_TEXT_POS (position, charpos, bytepos);
5080 return handle_display_spec (NULL, prop, Qnil, overlay,
5081 &position, charpos, frame_window_p);
5082 }
5083
5084
5085 /* Return 1 if PROP is a display sub-property value containing STRING.
5086
5087 Implementation note: this and the following function are really
5088 special cases of handle_display_spec and
5089 handle_single_display_spec, and should ideally use the same code.
5090 Until they do, these two pairs must be consistent and must be
5091 modified in sync. */
5092
5093 static int
5094 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5095 {
5096 if (EQ (string, prop))
5097 return 1;
5098
5099 /* Skip over `when FORM'. */
5100 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5101 {
5102 prop = XCDR (prop);
5103 if (!CONSP (prop))
5104 return 0;
5105 /* Actually, the condition following `when' should be eval'ed,
5106 like handle_single_display_spec does, and we should return
5107 zero if it evaluates to nil. However, this function is
5108 called only when the buffer was already displayed and some
5109 glyph in the glyph matrix was found to come from a display
5110 string. Therefore, the condition was already evaluated, and
5111 the result was non-nil, otherwise the display string wouldn't
5112 have been displayed and we would have never been called for
5113 this property. Thus, we can skip the evaluation and assume
5114 its result is non-nil. */
5115 prop = XCDR (prop);
5116 }
5117
5118 if (CONSP (prop))
5119 /* Skip over `margin LOCATION'. */
5120 if (EQ (XCAR (prop), Qmargin))
5121 {
5122 prop = XCDR (prop);
5123 if (!CONSP (prop))
5124 return 0;
5125
5126 prop = XCDR (prop);
5127 if (!CONSP (prop))
5128 return 0;
5129 }
5130
5131 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5132 }
5133
5134
5135 /* Return 1 if STRING appears in the `display' property PROP. */
5136
5137 static int
5138 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5139 {
5140 if (CONSP (prop)
5141 && !EQ (XCAR (prop), Qwhen)
5142 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5143 {
5144 /* A list of sub-properties. */
5145 while (CONSP (prop))
5146 {
5147 if (single_display_spec_string_p (XCAR (prop), string))
5148 return 1;
5149 prop = XCDR (prop);
5150 }
5151 }
5152 else if (VECTORP (prop))
5153 {
5154 /* A vector of sub-properties. */
5155 ptrdiff_t i;
5156 for (i = 0; i < ASIZE (prop); ++i)
5157 if (single_display_spec_string_p (AREF (prop, i), string))
5158 return 1;
5159 }
5160 else
5161 return single_display_spec_string_p (prop, string);
5162
5163 return 0;
5164 }
5165
5166 /* Look for STRING in overlays and text properties in the current
5167 buffer, between character positions FROM and TO (excluding TO).
5168 BACK_P non-zero means look back (in this case, TO is supposed to be
5169 less than FROM).
5170 Value is the first character position where STRING was found, or
5171 zero if it wasn't found before hitting TO.
5172
5173 This function may only use code that doesn't eval because it is
5174 called asynchronously from note_mouse_highlight. */
5175
5176 static ptrdiff_t
5177 string_buffer_position_lim (Lisp_Object string,
5178 ptrdiff_t from, ptrdiff_t to, int back_p)
5179 {
5180 Lisp_Object limit, prop, pos;
5181 int found = 0;
5182
5183 pos = make_number (max (from, BEGV));
5184
5185 if (!back_p) /* looking forward */
5186 {
5187 limit = make_number (min (to, ZV));
5188 while (!found && !EQ (pos, limit))
5189 {
5190 prop = Fget_char_property (pos, Qdisplay, Qnil);
5191 if (!NILP (prop) && display_prop_string_p (prop, string))
5192 found = 1;
5193 else
5194 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5195 limit);
5196 }
5197 }
5198 else /* looking back */
5199 {
5200 limit = make_number (max (to, BEGV));
5201 while (!found && !EQ (pos, limit))
5202 {
5203 prop = Fget_char_property (pos, Qdisplay, Qnil);
5204 if (!NILP (prop) && display_prop_string_p (prop, string))
5205 found = 1;
5206 else
5207 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5208 limit);
5209 }
5210 }
5211
5212 return found ? XINT (pos) : 0;
5213 }
5214
5215 /* Determine which buffer position in current buffer STRING comes from.
5216 AROUND_CHARPOS is an approximate position where it could come from.
5217 Value is the buffer position or 0 if it couldn't be determined.
5218
5219 This function is necessary because we don't record buffer positions
5220 in glyphs generated from strings (to keep struct glyph small).
5221 This function may only use code that doesn't eval because it is
5222 called asynchronously from note_mouse_highlight. */
5223
5224 static ptrdiff_t
5225 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5226 {
5227 const int MAX_DISTANCE = 1000;
5228 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5229 around_charpos + MAX_DISTANCE,
5230 0);
5231
5232 if (!found)
5233 found = string_buffer_position_lim (string, around_charpos,
5234 around_charpos - MAX_DISTANCE, 1);
5235 return found;
5236 }
5237
5238
5239 \f
5240 /***********************************************************************
5241 `composition' property
5242 ***********************************************************************/
5243
5244 /* Set up iterator IT from `composition' property at its current
5245 position. Called from handle_stop. */
5246
5247 static enum prop_handled
5248 handle_composition_prop (struct it *it)
5249 {
5250 Lisp_Object prop, string;
5251 ptrdiff_t pos, pos_byte, start, end;
5252
5253 if (STRINGP (it->string))
5254 {
5255 unsigned char *s;
5256
5257 pos = IT_STRING_CHARPOS (*it);
5258 pos_byte = IT_STRING_BYTEPOS (*it);
5259 string = it->string;
5260 s = SDATA (string) + pos_byte;
5261 it->c = STRING_CHAR (s);
5262 }
5263 else
5264 {
5265 pos = IT_CHARPOS (*it);
5266 pos_byte = IT_BYTEPOS (*it);
5267 string = Qnil;
5268 it->c = FETCH_CHAR (pos_byte);
5269 }
5270
5271 /* If there's a valid composition and point is not inside of the
5272 composition (in the case that the composition is from the current
5273 buffer), draw a glyph composed from the composition components. */
5274 if (find_composition (pos, -1, &start, &end, &prop, string)
5275 && composition_valid_p (start, end, prop)
5276 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5277 {
5278 if (start < pos)
5279 /* As we can't handle this situation (perhaps font-lock added
5280 a new composition), we just return here hoping that next
5281 redisplay will detect this composition much earlier. */
5282 return HANDLED_NORMALLY;
5283 if (start != pos)
5284 {
5285 if (STRINGP (it->string))
5286 pos_byte = string_char_to_byte (it->string, start);
5287 else
5288 pos_byte = CHAR_TO_BYTE (start);
5289 }
5290 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5291 prop, string);
5292
5293 if (it->cmp_it.id >= 0)
5294 {
5295 it->cmp_it.ch = -1;
5296 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5297 it->cmp_it.nglyphs = -1;
5298 }
5299 }
5300
5301 return HANDLED_NORMALLY;
5302 }
5303
5304
5305 \f
5306 /***********************************************************************
5307 Overlay strings
5308 ***********************************************************************/
5309
5310 /* The following structure is used to record overlay strings for
5311 later sorting in load_overlay_strings. */
5312
5313 struct overlay_entry
5314 {
5315 Lisp_Object overlay;
5316 Lisp_Object string;
5317 EMACS_INT priority;
5318 int after_string_p;
5319 };
5320
5321
5322 /* Set up iterator IT from overlay strings at its current position.
5323 Called from handle_stop. */
5324
5325 static enum prop_handled
5326 handle_overlay_change (struct it *it)
5327 {
5328 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5329 return HANDLED_RECOMPUTE_PROPS;
5330 else
5331 return HANDLED_NORMALLY;
5332 }
5333
5334
5335 /* Set up the next overlay string for delivery by IT, if there is an
5336 overlay string to deliver. Called by set_iterator_to_next when the
5337 end of the current overlay string is reached. If there are more
5338 overlay strings to display, IT->string and
5339 IT->current.overlay_string_index are set appropriately here.
5340 Otherwise IT->string is set to nil. */
5341
5342 static void
5343 next_overlay_string (struct it *it)
5344 {
5345 ++it->current.overlay_string_index;
5346 if (it->current.overlay_string_index == it->n_overlay_strings)
5347 {
5348 /* No more overlay strings. Restore IT's settings to what
5349 they were before overlay strings were processed, and
5350 continue to deliver from current_buffer. */
5351
5352 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5353 pop_it (it);
5354 eassert (it->sp > 0
5355 || (NILP (it->string)
5356 && it->method == GET_FROM_BUFFER
5357 && it->stop_charpos >= BEGV
5358 && it->stop_charpos <= it->end_charpos));
5359 it->current.overlay_string_index = -1;
5360 it->n_overlay_strings = 0;
5361 it->overlay_strings_charpos = -1;
5362 /* If there's an empty display string on the stack, pop the
5363 stack, to resync the bidi iterator with IT's position. Such
5364 empty strings are pushed onto the stack in
5365 get_overlay_strings_1. */
5366 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5367 pop_it (it);
5368
5369 /* If we're at the end of the buffer, record that we have
5370 processed the overlay strings there already, so that
5371 next_element_from_buffer doesn't try it again. */
5372 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5373 it->overlay_strings_at_end_processed_p = 1;
5374 }
5375 else
5376 {
5377 /* There are more overlay strings to process. If
5378 IT->current.overlay_string_index has advanced to a position
5379 where we must load IT->overlay_strings with more strings, do
5380 it. We must load at the IT->overlay_strings_charpos where
5381 IT->n_overlay_strings was originally computed; when invisible
5382 text is present, this might not be IT_CHARPOS (Bug#7016). */
5383 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5384
5385 if (it->current.overlay_string_index && i == 0)
5386 load_overlay_strings (it, it->overlay_strings_charpos);
5387
5388 /* Initialize IT to deliver display elements from the overlay
5389 string. */
5390 it->string = it->overlay_strings[i];
5391 it->multibyte_p = STRING_MULTIBYTE (it->string);
5392 SET_TEXT_POS (it->current.string_pos, 0, 0);
5393 it->method = GET_FROM_STRING;
5394 it->stop_charpos = 0;
5395 it->end_charpos = SCHARS (it->string);
5396 if (it->cmp_it.stop_pos >= 0)
5397 it->cmp_it.stop_pos = 0;
5398 it->prev_stop = 0;
5399 it->base_level_stop = 0;
5400
5401 /* Set up the bidi iterator for this overlay string. */
5402 if (it->bidi_p)
5403 {
5404 it->bidi_it.string.lstring = it->string;
5405 it->bidi_it.string.s = NULL;
5406 it->bidi_it.string.schars = SCHARS (it->string);
5407 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5408 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5409 it->bidi_it.string.unibyte = !it->multibyte_p;
5410 it->bidi_it.w = it->w;
5411 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5412 }
5413 }
5414
5415 CHECK_IT (it);
5416 }
5417
5418
5419 /* Compare two overlay_entry structures E1 and E2. Used as a
5420 comparison function for qsort in load_overlay_strings. Overlay
5421 strings for the same position are sorted so that
5422
5423 1. All after-strings come in front of before-strings, except
5424 when they come from the same overlay.
5425
5426 2. Within after-strings, strings are sorted so that overlay strings
5427 from overlays with higher priorities come first.
5428
5429 2. Within before-strings, strings are sorted so that overlay
5430 strings from overlays with higher priorities come last.
5431
5432 Value is analogous to strcmp. */
5433
5434
5435 static int
5436 compare_overlay_entries (const void *e1, const void *e2)
5437 {
5438 struct overlay_entry const *entry1 = e1;
5439 struct overlay_entry const *entry2 = e2;
5440 int result;
5441
5442 if (entry1->after_string_p != entry2->after_string_p)
5443 {
5444 /* Let after-strings appear in front of before-strings if
5445 they come from different overlays. */
5446 if (EQ (entry1->overlay, entry2->overlay))
5447 result = entry1->after_string_p ? 1 : -1;
5448 else
5449 result = entry1->after_string_p ? -1 : 1;
5450 }
5451 else if (entry1->priority != entry2->priority)
5452 {
5453 if (entry1->after_string_p)
5454 /* After-strings sorted in order of decreasing priority. */
5455 result = entry2->priority < entry1->priority ? -1 : 1;
5456 else
5457 /* Before-strings sorted in order of increasing priority. */
5458 result = entry1->priority < entry2->priority ? -1 : 1;
5459 }
5460 else
5461 result = 0;
5462
5463 return result;
5464 }
5465
5466
5467 /* Load the vector IT->overlay_strings with overlay strings from IT's
5468 current buffer position, or from CHARPOS if that is > 0. Set
5469 IT->n_overlays to the total number of overlay strings found.
5470
5471 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5472 a time. On entry into load_overlay_strings,
5473 IT->current.overlay_string_index gives the number of overlay
5474 strings that have already been loaded by previous calls to this
5475 function.
5476
5477 IT->add_overlay_start contains an additional overlay start
5478 position to consider for taking overlay strings from, if non-zero.
5479 This position comes into play when the overlay has an `invisible'
5480 property, and both before and after-strings. When we've skipped to
5481 the end of the overlay, because of its `invisible' property, we
5482 nevertheless want its before-string to appear.
5483 IT->add_overlay_start will contain the overlay start position
5484 in this case.
5485
5486 Overlay strings are sorted so that after-string strings come in
5487 front of before-string strings. Within before and after-strings,
5488 strings are sorted by overlay priority. See also function
5489 compare_overlay_entries. */
5490
5491 static void
5492 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5493 {
5494 Lisp_Object overlay, window, str, invisible;
5495 struct Lisp_Overlay *ov;
5496 ptrdiff_t start, end;
5497 ptrdiff_t size = 20;
5498 ptrdiff_t n = 0, i, j;
5499 int invis_p;
5500 struct overlay_entry *entries = alloca (size * sizeof *entries);
5501 USE_SAFE_ALLOCA;
5502
5503 if (charpos <= 0)
5504 charpos = IT_CHARPOS (*it);
5505
5506 /* Append the overlay string STRING of overlay OVERLAY to vector
5507 `entries' which has size `size' and currently contains `n'
5508 elements. AFTER_P non-zero means STRING is an after-string of
5509 OVERLAY. */
5510 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5511 do \
5512 { \
5513 Lisp_Object priority; \
5514 \
5515 if (n == size) \
5516 { \
5517 struct overlay_entry *old = entries; \
5518 SAFE_NALLOCA (entries, 2, size); \
5519 memcpy (entries, old, size * sizeof *entries); \
5520 size *= 2; \
5521 } \
5522 \
5523 entries[n].string = (STRING); \
5524 entries[n].overlay = (OVERLAY); \
5525 priority = Foverlay_get ((OVERLAY), Qpriority); \
5526 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5527 entries[n].after_string_p = (AFTER_P); \
5528 ++n; \
5529 } \
5530 while (0)
5531
5532 /* Process overlay before the overlay center. */
5533 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5534 {
5535 XSETMISC (overlay, ov);
5536 eassert (OVERLAYP (overlay));
5537 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5538 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5539
5540 if (end < charpos)
5541 break;
5542
5543 /* Skip this overlay if it doesn't start or end at IT's current
5544 position. */
5545 if (end != charpos && start != charpos)
5546 continue;
5547
5548 /* Skip this overlay if it doesn't apply to IT->w. */
5549 window = Foverlay_get (overlay, Qwindow);
5550 if (WINDOWP (window) && XWINDOW (window) != it->w)
5551 continue;
5552
5553 /* If the text ``under'' the overlay is invisible, both before-
5554 and after-strings from this overlay are visible; start and
5555 end position are indistinguishable. */
5556 invisible = Foverlay_get (overlay, Qinvisible);
5557 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5558
5559 /* If overlay has a non-empty before-string, record it. */
5560 if ((start == charpos || (end == charpos && invis_p))
5561 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5562 && SCHARS (str))
5563 RECORD_OVERLAY_STRING (overlay, str, 0);
5564
5565 /* If overlay has a non-empty after-string, record it. */
5566 if ((end == charpos || (start == charpos && invis_p))
5567 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5568 && SCHARS (str))
5569 RECORD_OVERLAY_STRING (overlay, str, 1);
5570 }
5571
5572 /* Process overlays after the overlay center. */
5573 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5574 {
5575 XSETMISC (overlay, ov);
5576 eassert (OVERLAYP (overlay));
5577 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5578 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5579
5580 if (start > charpos)
5581 break;
5582
5583 /* Skip this overlay if it doesn't start or end at IT's current
5584 position. */
5585 if (end != charpos && start != charpos)
5586 continue;
5587
5588 /* Skip this overlay if it doesn't apply to IT->w. */
5589 window = Foverlay_get (overlay, Qwindow);
5590 if (WINDOWP (window) && XWINDOW (window) != it->w)
5591 continue;
5592
5593 /* If the text ``under'' the overlay is invisible, it has a zero
5594 dimension, and both before- and after-strings apply. */
5595 invisible = Foverlay_get (overlay, Qinvisible);
5596 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5597
5598 /* If overlay has a non-empty before-string, record it. */
5599 if ((start == charpos || (end == charpos && invis_p))
5600 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5601 && SCHARS (str))
5602 RECORD_OVERLAY_STRING (overlay, str, 0);
5603
5604 /* If overlay has a non-empty after-string, record it. */
5605 if ((end == charpos || (start == charpos && invis_p))
5606 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5607 && SCHARS (str))
5608 RECORD_OVERLAY_STRING (overlay, str, 1);
5609 }
5610
5611 #undef RECORD_OVERLAY_STRING
5612
5613 /* Sort entries. */
5614 if (n > 1)
5615 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5616
5617 /* Record number of overlay strings, and where we computed it. */
5618 it->n_overlay_strings = n;
5619 it->overlay_strings_charpos = charpos;
5620
5621 /* IT->current.overlay_string_index is the number of overlay strings
5622 that have already been consumed by IT. Copy some of the
5623 remaining overlay strings to IT->overlay_strings. */
5624 i = 0;
5625 j = it->current.overlay_string_index;
5626 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5627 {
5628 it->overlay_strings[i] = entries[j].string;
5629 it->string_overlays[i++] = entries[j++].overlay;
5630 }
5631
5632 CHECK_IT (it);
5633 SAFE_FREE ();
5634 }
5635
5636
5637 /* Get the first chunk of overlay strings at IT's current buffer
5638 position, or at CHARPOS if that is > 0. Value is non-zero if at
5639 least one overlay string was found. */
5640
5641 static int
5642 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5643 {
5644 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5645 process. This fills IT->overlay_strings with strings, and sets
5646 IT->n_overlay_strings to the total number of strings to process.
5647 IT->pos.overlay_string_index has to be set temporarily to zero
5648 because load_overlay_strings needs this; it must be set to -1
5649 when no overlay strings are found because a zero value would
5650 indicate a position in the first overlay string. */
5651 it->current.overlay_string_index = 0;
5652 load_overlay_strings (it, charpos);
5653
5654 /* If we found overlay strings, set up IT to deliver display
5655 elements from the first one. Otherwise set up IT to deliver
5656 from current_buffer. */
5657 if (it->n_overlay_strings)
5658 {
5659 /* Make sure we know settings in current_buffer, so that we can
5660 restore meaningful values when we're done with the overlay
5661 strings. */
5662 if (compute_stop_p)
5663 compute_stop_pos (it);
5664 eassert (it->face_id >= 0);
5665
5666 /* Save IT's settings. They are restored after all overlay
5667 strings have been processed. */
5668 eassert (!compute_stop_p || it->sp == 0);
5669
5670 /* When called from handle_stop, there might be an empty display
5671 string loaded. In that case, don't bother saving it. But
5672 don't use this optimization with the bidi iterator, since we
5673 need the corresponding pop_it call to resync the bidi
5674 iterator's position with IT's position, after we are done
5675 with the overlay strings. (The corresponding call to pop_it
5676 in case of an empty display string is in
5677 next_overlay_string.) */
5678 if (!(!it->bidi_p
5679 && STRINGP (it->string) && !SCHARS (it->string)))
5680 push_it (it, NULL);
5681
5682 /* Set up IT to deliver display elements from the first overlay
5683 string. */
5684 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5685 it->string = it->overlay_strings[0];
5686 it->from_overlay = Qnil;
5687 it->stop_charpos = 0;
5688 eassert (STRINGP (it->string));
5689 it->end_charpos = SCHARS (it->string);
5690 it->prev_stop = 0;
5691 it->base_level_stop = 0;
5692 it->multibyte_p = STRING_MULTIBYTE (it->string);
5693 it->method = GET_FROM_STRING;
5694 it->from_disp_prop_p = 0;
5695
5696 /* Force paragraph direction to be that of the parent
5697 buffer. */
5698 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5699 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5700 else
5701 it->paragraph_embedding = L2R;
5702
5703 /* Set up the bidi iterator for this overlay string. */
5704 if (it->bidi_p)
5705 {
5706 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5707
5708 it->bidi_it.string.lstring = it->string;
5709 it->bidi_it.string.s = NULL;
5710 it->bidi_it.string.schars = SCHARS (it->string);
5711 it->bidi_it.string.bufpos = pos;
5712 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5713 it->bidi_it.string.unibyte = !it->multibyte_p;
5714 it->bidi_it.w = it->w;
5715 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5716 }
5717 return 1;
5718 }
5719
5720 it->current.overlay_string_index = -1;
5721 return 0;
5722 }
5723
5724 static int
5725 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5726 {
5727 it->string = Qnil;
5728 it->method = GET_FROM_BUFFER;
5729
5730 (void) get_overlay_strings_1 (it, charpos, 1);
5731
5732 CHECK_IT (it);
5733
5734 /* Value is non-zero if we found at least one overlay string. */
5735 return STRINGP (it->string);
5736 }
5737
5738
5739 \f
5740 /***********************************************************************
5741 Saving and restoring state
5742 ***********************************************************************/
5743
5744 /* Save current settings of IT on IT->stack. Called, for example,
5745 before setting up IT for an overlay string, to be able to restore
5746 IT's settings to what they were after the overlay string has been
5747 processed. If POSITION is non-NULL, it is the position to save on
5748 the stack instead of IT->position. */
5749
5750 static void
5751 push_it (struct it *it, struct text_pos *position)
5752 {
5753 struct iterator_stack_entry *p;
5754
5755 eassert (it->sp < IT_STACK_SIZE);
5756 p = it->stack + it->sp;
5757
5758 p->stop_charpos = it->stop_charpos;
5759 p->prev_stop = it->prev_stop;
5760 p->base_level_stop = it->base_level_stop;
5761 p->cmp_it = it->cmp_it;
5762 eassert (it->face_id >= 0);
5763 p->face_id = it->face_id;
5764 p->string = it->string;
5765 p->method = it->method;
5766 p->from_overlay = it->from_overlay;
5767 switch (p->method)
5768 {
5769 case GET_FROM_IMAGE:
5770 p->u.image.object = it->object;
5771 p->u.image.image_id = it->image_id;
5772 p->u.image.slice = it->slice;
5773 break;
5774 case GET_FROM_STRETCH:
5775 p->u.stretch.object = it->object;
5776 break;
5777 }
5778 p->position = position ? *position : it->position;
5779 p->current = it->current;
5780 p->end_charpos = it->end_charpos;
5781 p->string_nchars = it->string_nchars;
5782 p->area = it->area;
5783 p->multibyte_p = it->multibyte_p;
5784 p->avoid_cursor_p = it->avoid_cursor_p;
5785 p->space_width = it->space_width;
5786 p->font_height = it->font_height;
5787 p->voffset = it->voffset;
5788 p->string_from_display_prop_p = it->string_from_display_prop_p;
5789 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5790 p->display_ellipsis_p = 0;
5791 p->line_wrap = it->line_wrap;
5792 p->bidi_p = it->bidi_p;
5793 p->paragraph_embedding = it->paragraph_embedding;
5794 p->from_disp_prop_p = it->from_disp_prop_p;
5795 ++it->sp;
5796
5797 /* Save the state of the bidi iterator as well. */
5798 if (it->bidi_p)
5799 bidi_push_it (&it->bidi_it);
5800 }
5801
5802 static void
5803 iterate_out_of_display_property (struct it *it)
5804 {
5805 int buffer_p = !STRINGP (it->string);
5806 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5807 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5808
5809 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5810
5811 /* Maybe initialize paragraph direction. If we are at the beginning
5812 of a new paragraph, next_element_from_buffer may not have a
5813 chance to do that. */
5814 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5815 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5816 /* prev_stop can be zero, so check against BEGV as well. */
5817 while (it->bidi_it.charpos >= bob
5818 && it->prev_stop <= it->bidi_it.charpos
5819 && it->bidi_it.charpos < CHARPOS (it->position)
5820 && it->bidi_it.charpos < eob)
5821 bidi_move_to_visually_next (&it->bidi_it);
5822 /* Record the stop_pos we just crossed, for when we cross it
5823 back, maybe. */
5824 if (it->bidi_it.charpos > CHARPOS (it->position))
5825 it->prev_stop = CHARPOS (it->position);
5826 /* If we ended up not where pop_it put us, resync IT's
5827 positional members with the bidi iterator. */
5828 if (it->bidi_it.charpos != CHARPOS (it->position))
5829 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5830 if (buffer_p)
5831 it->current.pos = it->position;
5832 else
5833 it->current.string_pos = it->position;
5834 }
5835
5836 /* Restore IT's settings from IT->stack. Called, for example, when no
5837 more overlay strings must be processed, and we return to delivering
5838 display elements from a buffer, or when the end of a string from a
5839 `display' property is reached and we return to delivering display
5840 elements from an overlay string, or from a buffer. */
5841
5842 static void
5843 pop_it (struct it *it)
5844 {
5845 struct iterator_stack_entry *p;
5846 int from_display_prop = it->from_disp_prop_p;
5847
5848 eassert (it->sp > 0);
5849 --it->sp;
5850 p = it->stack + it->sp;
5851 it->stop_charpos = p->stop_charpos;
5852 it->prev_stop = p->prev_stop;
5853 it->base_level_stop = p->base_level_stop;
5854 it->cmp_it = p->cmp_it;
5855 it->face_id = p->face_id;
5856 it->current = p->current;
5857 it->position = p->position;
5858 it->string = p->string;
5859 it->from_overlay = p->from_overlay;
5860 if (NILP (it->string))
5861 SET_TEXT_POS (it->current.string_pos, -1, -1);
5862 it->method = p->method;
5863 switch (it->method)
5864 {
5865 case GET_FROM_IMAGE:
5866 it->image_id = p->u.image.image_id;
5867 it->object = p->u.image.object;
5868 it->slice = p->u.image.slice;
5869 break;
5870 case GET_FROM_STRETCH:
5871 it->object = p->u.stretch.object;
5872 break;
5873 case GET_FROM_BUFFER:
5874 it->object = it->w->contents;
5875 break;
5876 case GET_FROM_STRING:
5877 it->object = it->string;
5878 break;
5879 case GET_FROM_DISPLAY_VECTOR:
5880 if (it->s)
5881 it->method = GET_FROM_C_STRING;
5882 else if (STRINGP (it->string))
5883 it->method = GET_FROM_STRING;
5884 else
5885 {
5886 it->method = GET_FROM_BUFFER;
5887 it->object = it->w->contents;
5888 }
5889 }
5890 it->end_charpos = p->end_charpos;
5891 it->string_nchars = p->string_nchars;
5892 it->area = p->area;
5893 it->multibyte_p = p->multibyte_p;
5894 it->avoid_cursor_p = p->avoid_cursor_p;
5895 it->space_width = p->space_width;
5896 it->font_height = p->font_height;
5897 it->voffset = p->voffset;
5898 it->string_from_display_prop_p = p->string_from_display_prop_p;
5899 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5900 it->line_wrap = p->line_wrap;
5901 it->bidi_p = p->bidi_p;
5902 it->paragraph_embedding = p->paragraph_embedding;
5903 it->from_disp_prop_p = p->from_disp_prop_p;
5904 if (it->bidi_p)
5905 {
5906 bidi_pop_it (&it->bidi_it);
5907 /* Bidi-iterate until we get out of the portion of text, if any,
5908 covered by a `display' text property or by an overlay with
5909 `display' property. (We cannot just jump there, because the
5910 internal coherency of the bidi iterator state can not be
5911 preserved across such jumps.) We also must determine the
5912 paragraph base direction if the overlay we just processed is
5913 at the beginning of a new paragraph. */
5914 if (from_display_prop
5915 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5916 iterate_out_of_display_property (it);
5917
5918 eassert ((BUFFERP (it->object)
5919 && IT_CHARPOS (*it) == it->bidi_it.charpos
5920 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5921 || (STRINGP (it->object)
5922 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5923 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5924 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5925 }
5926 }
5927
5928
5929 \f
5930 /***********************************************************************
5931 Moving over lines
5932 ***********************************************************************/
5933
5934 /* Set IT's current position to the previous line start. */
5935
5936 static void
5937 back_to_previous_line_start (struct it *it)
5938 {
5939 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
5940
5941 DEC_BOTH (cp, bp);
5942 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
5943 }
5944
5945
5946 /* Move IT to the next line start.
5947
5948 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5949 we skipped over part of the text (as opposed to moving the iterator
5950 continuously over the text). Otherwise, don't change the value
5951 of *SKIPPED_P.
5952
5953 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5954 iterator on the newline, if it was found.
5955
5956 Newlines may come from buffer text, overlay strings, or strings
5957 displayed via the `display' property. That's the reason we can't
5958 simply use find_newline_no_quit.
5959
5960 Note that this function may not skip over invisible text that is so
5961 because of text properties and immediately follows a newline. If
5962 it would, function reseat_at_next_visible_line_start, when called
5963 from set_iterator_to_next, would effectively make invisible
5964 characters following a newline part of the wrong glyph row, which
5965 leads to wrong cursor motion. */
5966
5967 static int
5968 forward_to_next_line_start (struct it *it, int *skipped_p,
5969 struct bidi_it *bidi_it_prev)
5970 {
5971 ptrdiff_t old_selective;
5972 int newline_found_p, n;
5973 const int MAX_NEWLINE_DISTANCE = 500;
5974
5975 /* If already on a newline, just consume it to avoid unintended
5976 skipping over invisible text below. */
5977 if (it->what == IT_CHARACTER
5978 && it->c == '\n'
5979 && CHARPOS (it->position) == IT_CHARPOS (*it))
5980 {
5981 if (it->bidi_p && bidi_it_prev)
5982 *bidi_it_prev = it->bidi_it;
5983 set_iterator_to_next (it, 0);
5984 it->c = 0;
5985 return 1;
5986 }
5987
5988 /* Don't handle selective display in the following. It's (a)
5989 unnecessary because it's done by the caller, and (b) leads to an
5990 infinite recursion because next_element_from_ellipsis indirectly
5991 calls this function. */
5992 old_selective = it->selective;
5993 it->selective = 0;
5994
5995 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5996 from buffer text. */
5997 for (n = newline_found_p = 0;
5998 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5999 n += STRINGP (it->string) ? 0 : 1)
6000 {
6001 if (!get_next_display_element (it))
6002 return 0;
6003 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6004 if (newline_found_p && it->bidi_p && bidi_it_prev)
6005 *bidi_it_prev = it->bidi_it;
6006 set_iterator_to_next (it, 0);
6007 }
6008
6009 /* If we didn't find a newline near enough, see if we can use a
6010 short-cut. */
6011 if (!newline_found_p)
6012 {
6013 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6014 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6015 1, &bytepos);
6016 Lisp_Object pos;
6017
6018 eassert (!STRINGP (it->string));
6019
6020 /* If there isn't any `display' property in sight, and no
6021 overlays, we can just use the position of the newline in
6022 buffer text. */
6023 if (it->stop_charpos >= limit
6024 || ((pos = Fnext_single_property_change (make_number (start),
6025 Qdisplay, Qnil,
6026 make_number (limit)),
6027 NILP (pos))
6028 && next_overlay_change (start) == ZV))
6029 {
6030 if (!it->bidi_p)
6031 {
6032 IT_CHARPOS (*it) = limit;
6033 IT_BYTEPOS (*it) = bytepos;
6034 }
6035 else
6036 {
6037 struct bidi_it bprev;
6038
6039 /* Help bidi.c avoid expensive searches for display
6040 properties and overlays, by telling it that there are
6041 none up to `limit'. */
6042 if (it->bidi_it.disp_pos < limit)
6043 {
6044 it->bidi_it.disp_pos = limit;
6045 it->bidi_it.disp_prop = 0;
6046 }
6047 do {
6048 bprev = it->bidi_it;
6049 bidi_move_to_visually_next (&it->bidi_it);
6050 } while (it->bidi_it.charpos != limit);
6051 IT_CHARPOS (*it) = limit;
6052 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6053 if (bidi_it_prev)
6054 *bidi_it_prev = bprev;
6055 }
6056 *skipped_p = newline_found_p = 1;
6057 }
6058 else
6059 {
6060 while (get_next_display_element (it)
6061 && !newline_found_p)
6062 {
6063 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6064 if (newline_found_p && it->bidi_p && bidi_it_prev)
6065 *bidi_it_prev = it->bidi_it;
6066 set_iterator_to_next (it, 0);
6067 }
6068 }
6069 }
6070
6071 it->selective = old_selective;
6072 return newline_found_p;
6073 }
6074
6075
6076 /* Set IT's current position to the previous visible line start. Skip
6077 invisible text that is so either due to text properties or due to
6078 selective display. Caution: this does not change IT->current_x and
6079 IT->hpos. */
6080
6081 static void
6082 back_to_previous_visible_line_start (struct it *it)
6083 {
6084 while (IT_CHARPOS (*it) > BEGV)
6085 {
6086 back_to_previous_line_start (it);
6087
6088 if (IT_CHARPOS (*it) <= BEGV)
6089 break;
6090
6091 /* If selective > 0, then lines indented more than its value are
6092 invisible. */
6093 if (it->selective > 0
6094 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6095 it->selective))
6096 continue;
6097
6098 /* Check the newline before point for invisibility. */
6099 {
6100 Lisp_Object prop;
6101 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6102 Qinvisible, it->window);
6103 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6104 continue;
6105 }
6106
6107 if (IT_CHARPOS (*it) <= BEGV)
6108 break;
6109
6110 {
6111 struct it it2;
6112 void *it2data = NULL;
6113 ptrdiff_t pos;
6114 ptrdiff_t beg, end;
6115 Lisp_Object val, overlay;
6116
6117 SAVE_IT (it2, *it, it2data);
6118
6119 /* If newline is part of a composition, continue from start of composition */
6120 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6121 && beg < IT_CHARPOS (*it))
6122 goto replaced;
6123
6124 /* If newline is replaced by a display property, find start of overlay
6125 or interval and continue search from that point. */
6126 pos = --IT_CHARPOS (it2);
6127 --IT_BYTEPOS (it2);
6128 it2.sp = 0;
6129 bidi_unshelve_cache (NULL, 0);
6130 it2.string_from_display_prop_p = 0;
6131 it2.from_disp_prop_p = 0;
6132 if (handle_display_prop (&it2) == HANDLED_RETURN
6133 && !NILP (val = get_char_property_and_overlay
6134 (make_number (pos), Qdisplay, Qnil, &overlay))
6135 && (OVERLAYP (overlay)
6136 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6137 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6138 {
6139 RESTORE_IT (it, it, it2data);
6140 goto replaced;
6141 }
6142
6143 /* Newline is not replaced by anything -- so we are done. */
6144 RESTORE_IT (it, it, it2data);
6145 break;
6146
6147 replaced:
6148 if (beg < BEGV)
6149 beg = BEGV;
6150 IT_CHARPOS (*it) = beg;
6151 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6152 }
6153 }
6154
6155 it->continuation_lines_width = 0;
6156
6157 eassert (IT_CHARPOS (*it) >= BEGV);
6158 eassert (IT_CHARPOS (*it) == BEGV
6159 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6160 CHECK_IT (it);
6161 }
6162
6163
6164 /* Reseat iterator IT at the previous visible line start. Skip
6165 invisible text that is so either due to text properties or due to
6166 selective display. At the end, update IT's overlay information,
6167 face information etc. */
6168
6169 void
6170 reseat_at_previous_visible_line_start (struct it *it)
6171 {
6172 back_to_previous_visible_line_start (it);
6173 reseat (it, it->current.pos, 1);
6174 CHECK_IT (it);
6175 }
6176
6177
6178 /* Reseat iterator IT on the next visible line start in the current
6179 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6180 preceding the line start. Skip over invisible text that is so
6181 because of selective display. Compute faces, overlays etc at the
6182 new position. Note that this function does not skip over text that
6183 is invisible because of text properties. */
6184
6185 static void
6186 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6187 {
6188 int newline_found_p, skipped_p = 0;
6189 struct bidi_it bidi_it_prev;
6190
6191 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6192
6193 /* Skip over lines that are invisible because they are indented
6194 more than the value of IT->selective. */
6195 if (it->selective > 0)
6196 while (IT_CHARPOS (*it) < ZV
6197 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6198 it->selective))
6199 {
6200 eassert (IT_BYTEPOS (*it) == BEGV
6201 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6202 newline_found_p =
6203 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6204 }
6205
6206 /* Position on the newline if that's what's requested. */
6207 if (on_newline_p && newline_found_p)
6208 {
6209 if (STRINGP (it->string))
6210 {
6211 if (IT_STRING_CHARPOS (*it) > 0)
6212 {
6213 if (!it->bidi_p)
6214 {
6215 --IT_STRING_CHARPOS (*it);
6216 --IT_STRING_BYTEPOS (*it);
6217 }
6218 else
6219 {
6220 /* We need to restore the bidi iterator to the state
6221 it had on the newline, and resync the IT's
6222 position with that. */
6223 it->bidi_it = bidi_it_prev;
6224 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6225 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6226 }
6227 }
6228 }
6229 else if (IT_CHARPOS (*it) > BEGV)
6230 {
6231 if (!it->bidi_p)
6232 {
6233 --IT_CHARPOS (*it);
6234 --IT_BYTEPOS (*it);
6235 }
6236 else
6237 {
6238 /* We need to restore the bidi iterator to the state it
6239 had on the newline and resync IT with that. */
6240 it->bidi_it = bidi_it_prev;
6241 IT_CHARPOS (*it) = it->bidi_it.charpos;
6242 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6243 }
6244 reseat (it, it->current.pos, 0);
6245 }
6246 }
6247 else if (skipped_p)
6248 reseat (it, it->current.pos, 0);
6249
6250 CHECK_IT (it);
6251 }
6252
6253
6254 \f
6255 /***********************************************************************
6256 Changing an iterator's position
6257 ***********************************************************************/
6258
6259 /* Change IT's current position to POS in current_buffer. If FORCE_P
6260 is non-zero, always check for text properties at the new position.
6261 Otherwise, text properties are only looked up if POS >=
6262 IT->check_charpos of a property. */
6263
6264 static void
6265 reseat (struct it *it, struct text_pos pos, int force_p)
6266 {
6267 ptrdiff_t original_pos = IT_CHARPOS (*it);
6268
6269 reseat_1 (it, pos, 0);
6270
6271 /* Determine where to check text properties. Avoid doing it
6272 where possible because text property lookup is very expensive. */
6273 if (force_p
6274 || CHARPOS (pos) > it->stop_charpos
6275 || CHARPOS (pos) < original_pos)
6276 {
6277 if (it->bidi_p)
6278 {
6279 /* For bidi iteration, we need to prime prev_stop and
6280 base_level_stop with our best estimations. */
6281 /* Implementation note: Of course, POS is not necessarily a
6282 stop position, so assigning prev_pos to it is a lie; we
6283 should have called compute_stop_backwards. However, if
6284 the current buffer does not include any R2L characters,
6285 that call would be a waste of cycles, because the
6286 iterator will never move back, and thus never cross this
6287 "fake" stop position. So we delay that backward search
6288 until the time we really need it, in next_element_from_buffer. */
6289 if (CHARPOS (pos) != it->prev_stop)
6290 it->prev_stop = CHARPOS (pos);
6291 if (CHARPOS (pos) < it->base_level_stop)
6292 it->base_level_stop = 0; /* meaning it's unknown */
6293 handle_stop (it);
6294 }
6295 else
6296 {
6297 handle_stop (it);
6298 it->prev_stop = it->base_level_stop = 0;
6299 }
6300
6301 }
6302
6303 CHECK_IT (it);
6304 }
6305
6306
6307 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6308 IT->stop_pos to POS, also. */
6309
6310 static void
6311 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6312 {
6313 /* Don't call this function when scanning a C string. */
6314 eassert (it->s == NULL);
6315
6316 /* POS must be a reasonable value. */
6317 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6318
6319 it->current.pos = it->position = pos;
6320 it->end_charpos = ZV;
6321 it->dpvec = NULL;
6322 it->current.dpvec_index = -1;
6323 it->current.overlay_string_index = -1;
6324 IT_STRING_CHARPOS (*it) = -1;
6325 IT_STRING_BYTEPOS (*it) = -1;
6326 it->string = Qnil;
6327 it->method = GET_FROM_BUFFER;
6328 it->object = it->w->contents;
6329 it->area = TEXT_AREA;
6330 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6331 it->sp = 0;
6332 it->string_from_display_prop_p = 0;
6333 it->string_from_prefix_prop_p = 0;
6334
6335 it->from_disp_prop_p = 0;
6336 it->face_before_selective_p = 0;
6337 if (it->bidi_p)
6338 {
6339 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6340 &it->bidi_it);
6341 bidi_unshelve_cache (NULL, 0);
6342 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6343 it->bidi_it.string.s = NULL;
6344 it->bidi_it.string.lstring = Qnil;
6345 it->bidi_it.string.bufpos = 0;
6346 it->bidi_it.string.unibyte = 0;
6347 it->bidi_it.w = it->w;
6348 }
6349
6350 if (set_stop_p)
6351 {
6352 it->stop_charpos = CHARPOS (pos);
6353 it->base_level_stop = CHARPOS (pos);
6354 }
6355 /* This make the information stored in it->cmp_it invalidate. */
6356 it->cmp_it.id = -1;
6357 }
6358
6359
6360 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6361 If S is non-null, it is a C string to iterate over. Otherwise,
6362 STRING gives a Lisp string to iterate over.
6363
6364 If PRECISION > 0, don't return more then PRECISION number of
6365 characters from the string.
6366
6367 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6368 characters have been returned. FIELD_WIDTH < 0 means an infinite
6369 field width.
6370
6371 MULTIBYTE = 0 means disable processing of multibyte characters,
6372 MULTIBYTE > 0 means enable it,
6373 MULTIBYTE < 0 means use IT->multibyte_p.
6374
6375 IT must be initialized via a prior call to init_iterator before
6376 calling this function. */
6377
6378 static void
6379 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6380 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6381 int multibyte)
6382 {
6383 /* No text property checks performed by default, but see below. */
6384 it->stop_charpos = -1;
6385
6386 /* Set iterator position and end position. */
6387 memset (&it->current, 0, sizeof it->current);
6388 it->current.overlay_string_index = -1;
6389 it->current.dpvec_index = -1;
6390 eassert (charpos >= 0);
6391
6392 /* If STRING is specified, use its multibyteness, otherwise use the
6393 setting of MULTIBYTE, if specified. */
6394 if (multibyte >= 0)
6395 it->multibyte_p = multibyte > 0;
6396
6397 /* Bidirectional reordering of strings is controlled by the default
6398 value of bidi-display-reordering. Don't try to reorder while
6399 loading loadup.el, as the necessary character property tables are
6400 not yet available. */
6401 it->bidi_p =
6402 NILP (Vpurify_flag)
6403 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6404
6405 if (s == NULL)
6406 {
6407 eassert (STRINGP (string));
6408 it->string = string;
6409 it->s = NULL;
6410 it->end_charpos = it->string_nchars = SCHARS (string);
6411 it->method = GET_FROM_STRING;
6412 it->current.string_pos = string_pos (charpos, string);
6413
6414 if (it->bidi_p)
6415 {
6416 it->bidi_it.string.lstring = string;
6417 it->bidi_it.string.s = NULL;
6418 it->bidi_it.string.schars = it->end_charpos;
6419 it->bidi_it.string.bufpos = 0;
6420 it->bidi_it.string.from_disp_str = 0;
6421 it->bidi_it.string.unibyte = !it->multibyte_p;
6422 it->bidi_it.w = it->w;
6423 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6424 FRAME_WINDOW_P (it->f), &it->bidi_it);
6425 }
6426 }
6427 else
6428 {
6429 it->s = (const unsigned char *) s;
6430 it->string = Qnil;
6431
6432 /* Note that we use IT->current.pos, not it->current.string_pos,
6433 for displaying C strings. */
6434 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6435 if (it->multibyte_p)
6436 {
6437 it->current.pos = c_string_pos (charpos, s, 1);
6438 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6439 }
6440 else
6441 {
6442 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6443 it->end_charpos = it->string_nchars = strlen (s);
6444 }
6445
6446 if (it->bidi_p)
6447 {
6448 it->bidi_it.string.lstring = Qnil;
6449 it->bidi_it.string.s = (const unsigned char *) s;
6450 it->bidi_it.string.schars = it->end_charpos;
6451 it->bidi_it.string.bufpos = 0;
6452 it->bidi_it.string.from_disp_str = 0;
6453 it->bidi_it.string.unibyte = !it->multibyte_p;
6454 it->bidi_it.w = it->w;
6455 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6456 &it->bidi_it);
6457 }
6458 it->method = GET_FROM_C_STRING;
6459 }
6460
6461 /* PRECISION > 0 means don't return more than PRECISION characters
6462 from the string. */
6463 if (precision > 0 && it->end_charpos - charpos > precision)
6464 {
6465 it->end_charpos = it->string_nchars = charpos + precision;
6466 if (it->bidi_p)
6467 it->bidi_it.string.schars = it->end_charpos;
6468 }
6469
6470 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6471 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6472 FIELD_WIDTH < 0 means infinite field width. This is useful for
6473 padding with `-' at the end of a mode line. */
6474 if (field_width < 0)
6475 field_width = INFINITY;
6476 /* Implementation note: We deliberately don't enlarge
6477 it->bidi_it.string.schars here to fit it->end_charpos, because
6478 the bidi iterator cannot produce characters out of thin air. */
6479 if (field_width > it->end_charpos - charpos)
6480 it->end_charpos = charpos + field_width;
6481
6482 /* Use the standard display table for displaying strings. */
6483 if (DISP_TABLE_P (Vstandard_display_table))
6484 it->dp = XCHAR_TABLE (Vstandard_display_table);
6485
6486 it->stop_charpos = charpos;
6487 it->prev_stop = charpos;
6488 it->base_level_stop = 0;
6489 if (it->bidi_p)
6490 {
6491 it->bidi_it.first_elt = 1;
6492 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6493 it->bidi_it.disp_pos = -1;
6494 }
6495 if (s == NULL && it->multibyte_p)
6496 {
6497 ptrdiff_t endpos = SCHARS (it->string);
6498 if (endpos > it->end_charpos)
6499 endpos = it->end_charpos;
6500 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6501 it->string);
6502 }
6503 CHECK_IT (it);
6504 }
6505
6506
6507 \f
6508 /***********************************************************************
6509 Iteration
6510 ***********************************************************************/
6511
6512 /* Map enum it_method value to corresponding next_element_from_* function. */
6513
6514 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6515 {
6516 next_element_from_buffer,
6517 next_element_from_display_vector,
6518 next_element_from_string,
6519 next_element_from_c_string,
6520 next_element_from_image,
6521 next_element_from_stretch
6522 };
6523
6524 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6525
6526
6527 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6528 (possibly with the following characters). */
6529
6530 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6531 ((IT)->cmp_it.id >= 0 \
6532 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6533 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6534 END_CHARPOS, (IT)->w, \
6535 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6536 (IT)->string)))
6537
6538
6539 /* Lookup the char-table Vglyphless_char_display for character C (-1
6540 if we want information for no-font case), and return the display
6541 method symbol. By side-effect, update it->what and
6542 it->glyphless_method. This function is called from
6543 get_next_display_element for each character element, and from
6544 x_produce_glyphs when no suitable font was found. */
6545
6546 Lisp_Object
6547 lookup_glyphless_char_display (int c, struct it *it)
6548 {
6549 Lisp_Object glyphless_method = Qnil;
6550
6551 if (CHAR_TABLE_P (Vglyphless_char_display)
6552 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6553 {
6554 if (c >= 0)
6555 {
6556 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6557 if (CONSP (glyphless_method))
6558 glyphless_method = FRAME_WINDOW_P (it->f)
6559 ? XCAR (glyphless_method)
6560 : XCDR (glyphless_method);
6561 }
6562 else
6563 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6564 }
6565
6566 retry:
6567 if (NILP (glyphless_method))
6568 {
6569 if (c >= 0)
6570 /* The default is to display the character by a proper font. */
6571 return Qnil;
6572 /* The default for the no-font case is to display an empty box. */
6573 glyphless_method = Qempty_box;
6574 }
6575 if (EQ (glyphless_method, Qzero_width))
6576 {
6577 if (c >= 0)
6578 return glyphless_method;
6579 /* This method can't be used for the no-font case. */
6580 glyphless_method = Qempty_box;
6581 }
6582 if (EQ (glyphless_method, Qthin_space))
6583 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6584 else if (EQ (glyphless_method, Qempty_box))
6585 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6586 else if (EQ (glyphless_method, Qhex_code))
6587 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6588 else if (STRINGP (glyphless_method))
6589 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6590 else
6591 {
6592 /* Invalid value. We use the default method. */
6593 glyphless_method = Qnil;
6594 goto retry;
6595 }
6596 it->what = IT_GLYPHLESS;
6597 return glyphless_method;
6598 }
6599
6600 /* Merge escape glyph face and cache the result. */
6601
6602 static struct frame *last_escape_glyph_frame = NULL;
6603 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6604 static int last_escape_glyph_merged_face_id = 0;
6605
6606 static int
6607 merge_escape_glyph_face (struct it *it)
6608 {
6609 int face_id;
6610
6611 if (it->f == last_escape_glyph_frame
6612 && it->face_id == last_escape_glyph_face_id)
6613 face_id = last_escape_glyph_merged_face_id;
6614 else
6615 {
6616 /* Merge the `escape-glyph' face into the current face. */
6617 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6618 last_escape_glyph_frame = it->f;
6619 last_escape_glyph_face_id = it->face_id;
6620 last_escape_glyph_merged_face_id = face_id;
6621 }
6622 return face_id;
6623 }
6624
6625 /* Likewise for glyphless glyph face. */
6626
6627 static struct frame *last_glyphless_glyph_frame = NULL;
6628 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6629 static int last_glyphless_glyph_merged_face_id = 0;
6630
6631 int
6632 merge_glyphless_glyph_face (struct it *it)
6633 {
6634 int face_id;
6635
6636 if (it->f == last_glyphless_glyph_frame
6637 && it->face_id == last_glyphless_glyph_face_id)
6638 face_id = last_glyphless_glyph_merged_face_id;
6639 else
6640 {
6641 /* Merge the `glyphless-char' face into the current face. */
6642 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6643 last_glyphless_glyph_frame = it->f;
6644 last_glyphless_glyph_face_id = it->face_id;
6645 last_glyphless_glyph_merged_face_id = face_id;
6646 }
6647 return face_id;
6648 }
6649
6650 /* Load IT's display element fields with information about the next
6651 display element from the current position of IT. Value is zero if
6652 end of buffer (or C string) is reached. */
6653
6654 static int
6655 get_next_display_element (struct it *it)
6656 {
6657 /* Non-zero means that we found a display element. Zero means that
6658 we hit the end of what we iterate over. Performance note: the
6659 function pointer `method' used here turns out to be faster than
6660 using a sequence of if-statements. */
6661 int success_p;
6662
6663 get_next:
6664 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6665
6666 if (it->what == IT_CHARACTER)
6667 {
6668 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6669 and only if (a) the resolved directionality of that character
6670 is R..." */
6671 /* FIXME: Do we need an exception for characters from display
6672 tables? */
6673 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6674 it->c = bidi_mirror_char (it->c);
6675 /* Map via display table or translate control characters.
6676 IT->c, IT->len etc. have been set to the next character by
6677 the function call above. If we have a display table, and it
6678 contains an entry for IT->c, translate it. Don't do this if
6679 IT->c itself comes from a display table, otherwise we could
6680 end up in an infinite recursion. (An alternative could be to
6681 count the recursion depth of this function and signal an
6682 error when a certain maximum depth is reached.) Is it worth
6683 it? */
6684 if (success_p && it->dpvec == NULL)
6685 {
6686 Lisp_Object dv;
6687 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6688 int nonascii_space_p = 0;
6689 int nonascii_hyphen_p = 0;
6690 int c = it->c; /* This is the character to display. */
6691
6692 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6693 {
6694 eassert (SINGLE_BYTE_CHAR_P (c));
6695 if (unibyte_display_via_language_environment)
6696 {
6697 c = DECODE_CHAR (unibyte, c);
6698 if (c < 0)
6699 c = BYTE8_TO_CHAR (it->c);
6700 }
6701 else
6702 c = BYTE8_TO_CHAR (it->c);
6703 }
6704
6705 if (it->dp
6706 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6707 VECTORP (dv)))
6708 {
6709 struct Lisp_Vector *v = XVECTOR (dv);
6710
6711 /* Return the first character from the display table
6712 entry, if not empty. If empty, don't display the
6713 current character. */
6714 if (v->header.size)
6715 {
6716 it->dpvec_char_len = it->len;
6717 it->dpvec = v->contents;
6718 it->dpend = v->contents + v->header.size;
6719 it->current.dpvec_index = 0;
6720 it->dpvec_face_id = -1;
6721 it->saved_face_id = it->face_id;
6722 it->method = GET_FROM_DISPLAY_VECTOR;
6723 it->ellipsis_p = 0;
6724 }
6725 else
6726 {
6727 set_iterator_to_next (it, 0);
6728 }
6729 goto get_next;
6730 }
6731
6732 if (! NILP (lookup_glyphless_char_display (c, it)))
6733 {
6734 if (it->what == IT_GLYPHLESS)
6735 goto done;
6736 /* Don't display this character. */
6737 set_iterator_to_next (it, 0);
6738 goto get_next;
6739 }
6740
6741 /* If `nobreak-char-display' is non-nil, we display
6742 non-ASCII spaces and hyphens specially. */
6743 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6744 {
6745 if (c == 0xA0)
6746 nonascii_space_p = 1;
6747 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6748 nonascii_hyphen_p = 1;
6749 }
6750
6751 /* Translate control characters into `\003' or `^C' form.
6752 Control characters coming from a display table entry are
6753 currently not translated because we use IT->dpvec to hold
6754 the translation. This could easily be changed but I
6755 don't believe that it is worth doing.
6756
6757 The characters handled by `nobreak-char-display' must be
6758 translated too.
6759
6760 Non-printable characters and raw-byte characters are also
6761 translated to octal form. */
6762 if (((c < ' ' || c == 127) /* ASCII control chars. */
6763 ? (it->area != TEXT_AREA
6764 /* In mode line, treat \n, \t like other crl chars. */
6765 || (c != '\t'
6766 && it->glyph_row
6767 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6768 || (c != '\n' && c != '\t'))
6769 : (nonascii_space_p
6770 || nonascii_hyphen_p
6771 || CHAR_BYTE8_P (c)
6772 || ! CHAR_PRINTABLE_P (c))))
6773 {
6774 /* C is a control character, non-ASCII space/hyphen,
6775 raw-byte, or a non-printable character which must be
6776 displayed either as '\003' or as `^C' where the '\\'
6777 and '^' can be defined in the display table. Fill
6778 IT->ctl_chars with glyphs for what we have to
6779 display. Then, set IT->dpvec to these glyphs. */
6780 Lisp_Object gc;
6781 int ctl_len;
6782 int face_id;
6783 int lface_id = 0;
6784 int escape_glyph;
6785
6786 /* Handle control characters with ^. */
6787
6788 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6789 {
6790 int g;
6791
6792 g = '^'; /* default glyph for Control */
6793 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6794 if (it->dp
6795 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6796 {
6797 g = GLYPH_CODE_CHAR (gc);
6798 lface_id = GLYPH_CODE_FACE (gc);
6799 }
6800
6801 face_id = (lface_id
6802 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6803 : merge_escape_glyph_face (it));
6804
6805 XSETINT (it->ctl_chars[0], g);
6806 XSETINT (it->ctl_chars[1], c ^ 0100);
6807 ctl_len = 2;
6808 goto display_control;
6809 }
6810
6811 /* Handle non-ascii space in the mode where it only gets
6812 highlighting. */
6813
6814 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6815 {
6816 /* Merge `nobreak-space' into the current face. */
6817 face_id = merge_faces (it->f, Qnobreak_space, 0,
6818 it->face_id);
6819 XSETINT (it->ctl_chars[0], ' ');
6820 ctl_len = 1;
6821 goto display_control;
6822 }
6823
6824 /* Handle sequences that start with the "escape glyph". */
6825
6826 /* the default escape glyph is \. */
6827 escape_glyph = '\\';
6828
6829 if (it->dp
6830 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6831 {
6832 escape_glyph = GLYPH_CODE_CHAR (gc);
6833 lface_id = GLYPH_CODE_FACE (gc);
6834 }
6835
6836 face_id = (lface_id
6837 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6838 : merge_escape_glyph_face (it));
6839
6840 /* Draw non-ASCII hyphen with just highlighting: */
6841
6842 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6843 {
6844 XSETINT (it->ctl_chars[0], '-');
6845 ctl_len = 1;
6846 goto display_control;
6847 }
6848
6849 /* Draw non-ASCII space/hyphen with escape glyph: */
6850
6851 if (nonascii_space_p || nonascii_hyphen_p)
6852 {
6853 XSETINT (it->ctl_chars[0], escape_glyph);
6854 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6855 ctl_len = 2;
6856 goto display_control;
6857 }
6858
6859 {
6860 char str[10];
6861 int len, i;
6862
6863 if (CHAR_BYTE8_P (c))
6864 /* Display \200 instead of \17777600. */
6865 c = CHAR_TO_BYTE8 (c);
6866 len = sprintf (str, "%03o", c);
6867
6868 XSETINT (it->ctl_chars[0], escape_glyph);
6869 for (i = 0; i < len; i++)
6870 XSETINT (it->ctl_chars[i + 1], str[i]);
6871 ctl_len = len + 1;
6872 }
6873
6874 display_control:
6875 /* Set up IT->dpvec and return first character from it. */
6876 it->dpvec_char_len = it->len;
6877 it->dpvec = it->ctl_chars;
6878 it->dpend = it->dpvec + ctl_len;
6879 it->current.dpvec_index = 0;
6880 it->dpvec_face_id = face_id;
6881 it->saved_face_id = it->face_id;
6882 it->method = GET_FROM_DISPLAY_VECTOR;
6883 it->ellipsis_p = 0;
6884 goto get_next;
6885 }
6886 it->char_to_display = c;
6887 }
6888 else if (success_p)
6889 {
6890 it->char_to_display = it->c;
6891 }
6892 }
6893
6894 #ifdef HAVE_WINDOW_SYSTEM
6895 /* Adjust face id for a multibyte character. There are no multibyte
6896 character in unibyte text. */
6897 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6898 && it->multibyte_p
6899 && success_p
6900 && FRAME_WINDOW_P (it->f))
6901 {
6902 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6903
6904 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6905 {
6906 /* Automatic composition with glyph-string. */
6907 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6908
6909 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6910 }
6911 else
6912 {
6913 ptrdiff_t pos = (it->s ? -1
6914 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6915 : IT_CHARPOS (*it));
6916 int c;
6917
6918 if (it->what == IT_CHARACTER)
6919 c = it->char_to_display;
6920 else
6921 {
6922 struct composition *cmp = composition_table[it->cmp_it.id];
6923 int i;
6924
6925 c = ' ';
6926 for (i = 0; i < cmp->glyph_len; i++)
6927 /* TAB in a composition means display glyphs with
6928 padding space on the left or right. */
6929 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6930 break;
6931 }
6932 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6933 }
6934 }
6935 #endif /* HAVE_WINDOW_SYSTEM */
6936
6937 done:
6938 /* Is this character the last one of a run of characters with
6939 box? If yes, set IT->end_of_box_run_p to 1. */
6940 if (it->face_box_p
6941 && it->s == NULL)
6942 {
6943 if (it->method == GET_FROM_STRING && it->sp)
6944 {
6945 int face_id = underlying_face_id (it);
6946 struct face *face = FACE_FROM_ID (it->f, face_id);
6947
6948 if (face)
6949 {
6950 if (face->box == FACE_NO_BOX)
6951 {
6952 /* If the box comes from face properties in a
6953 display string, check faces in that string. */
6954 int string_face_id = face_after_it_pos (it);
6955 it->end_of_box_run_p
6956 = (FACE_FROM_ID (it->f, string_face_id)->box
6957 == FACE_NO_BOX);
6958 }
6959 /* Otherwise, the box comes from the underlying face.
6960 If this is the last string character displayed, check
6961 the next buffer location. */
6962 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6963 && (it->current.overlay_string_index
6964 == it->n_overlay_strings - 1))
6965 {
6966 ptrdiff_t ignore;
6967 int next_face_id;
6968 struct text_pos pos = it->current.pos;
6969 INC_TEXT_POS (pos, it->multibyte_p);
6970
6971 next_face_id = face_at_buffer_position
6972 (it->w, CHARPOS (pos), &ignore,
6973 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6974 -1);
6975 it->end_of_box_run_p
6976 = (FACE_FROM_ID (it->f, next_face_id)->box
6977 == FACE_NO_BOX);
6978 }
6979 }
6980 }
6981 /* next_element_from_display_vector sets this flag according to
6982 faces of the display vector glyphs, see there. */
6983 else if (it->method != GET_FROM_DISPLAY_VECTOR)
6984 {
6985 int face_id = face_after_it_pos (it);
6986 it->end_of_box_run_p
6987 = (face_id != it->face_id
6988 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6989 }
6990 }
6991 /* If we reached the end of the object we've been iterating (e.g., a
6992 display string or an overlay string), and there's something on
6993 IT->stack, proceed with what's on the stack. It doesn't make
6994 sense to return zero if there's unprocessed stuff on the stack,
6995 because otherwise that stuff will never be displayed. */
6996 if (!success_p && it->sp > 0)
6997 {
6998 set_iterator_to_next (it, 0);
6999 success_p = get_next_display_element (it);
7000 }
7001
7002 /* Value is 0 if end of buffer or string reached. */
7003 return success_p;
7004 }
7005
7006
7007 /* Move IT to the next display element.
7008
7009 RESEAT_P non-zero means if called on a newline in buffer text,
7010 skip to the next visible line start.
7011
7012 Functions get_next_display_element and set_iterator_to_next are
7013 separate because I find this arrangement easier to handle than a
7014 get_next_display_element function that also increments IT's
7015 position. The way it is we can first look at an iterator's current
7016 display element, decide whether it fits on a line, and if it does,
7017 increment the iterator position. The other way around we probably
7018 would either need a flag indicating whether the iterator has to be
7019 incremented the next time, or we would have to implement a
7020 decrement position function which would not be easy to write. */
7021
7022 void
7023 set_iterator_to_next (struct it *it, int reseat_p)
7024 {
7025 /* Reset flags indicating start and end of a sequence of characters
7026 with box. Reset them at the start of this function because
7027 moving the iterator to a new position might set them. */
7028 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7029
7030 switch (it->method)
7031 {
7032 case GET_FROM_BUFFER:
7033 /* The current display element of IT is a character from
7034 current_buffer. Advance in the buffer, and maybe skip over
7035 invisible lines that are so because of selective display. */
7036 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7037 reseat_at_next_visible_line_start (it, 0);
7038 else if (it->cmp_it.id >= 0)
7039 {
7040 /* We are currently getting glyphs from a composition. */
7041 int i;
7042
7043 if (! it->bidi_p)
7044 {
7045 IT_CHARPOS (*it) += it->cmp_it.nchars;
7046 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7047 if (it->cmp_it.to < it->cmp_it.nglyphs)
7048 {
7049 it->cmp_it.from = it->cmp_it.to;
7050 }
7051 else
7052 {
7053 it->cmp_it.id = -1;
7054 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7055 IT_BYTEPOS (*it),
7056 it->end_charpos, Qnil);
7057 }
7058 }
7059 else if (! it->cmp_it.reversed_p)
7060 {
7061 /* Composition created while scanning forward. */
7062 /* Update IT's char/byte positions to point to the first
7063 character of the next grapheme cluster, or to the
7064 character visually after the current composition. */
7065 for (i = 0; i < it->cmp_it.nchars; i++)
7066 bidi_move_to_visually_next (&it->bidi_it);
7067 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7068 IT_CHARPOS (*it) = it->bidi_it.charpos;
7069
7070 if (it->cmp_it.to < it->cmp_it.nglyphs)
7071 {
7072 /* Proceed to the next grapheme cluster. */
7073 it->cmp_it.from = it->cmp_it.to;
7074 }
7075 else
7076 {
7077 /* No more grapheme clusters in this composition.
7078 Find the next stop position. */
7079 ptrdiff_t stop = it->end_charpos;
7080 if (it->bidi_it.scan_dir < 0)
7081 /* Now we are scanning backward and don't know
7082 where to stop. */
7083 stop = -1;
7084 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7085 IT_BYTEPOS (*it), stop, Qnil);
7086 }
7087 }
7088 else
7089 {
7090 /* Composition created while scanning backward. */
7091 /* Update IT's char/byte positions to point to the last
7092 character of the previous grapheme cluster, or the
7093 character visually after the current composition. */
7094 for (i = 0; i < it->cmp_it.nchars; i++)
7095 bidi_move_to_visually_next (&it->bidi_it);
7096 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7097 IT_CHARPOS (*it) = it->bidi_it.charpos;
7098 if (it->cmp_it.from > 0)
7099 {
7100 /* Proceed to the previous grapheme cluster. */
7101 it->cmp_it.to = it->cmp_it.from;
7102 }
7103 else
7104 {
7105 /* No more grapheme clusters in this composition.
7106 Find the next stop position. */
7107 ptrdiff_t stop = it->end_charpos;
7108 if (it->bidi_it.scan_dir < 0)
7109 /* Now we are scanning backward and don't know
7110 where to stop. */
7111 stop = -1;
7112 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7113 IT_BYTEPOS (*it), stop, Qnil);
7114 }
7115 }
7116 }
7117 else
7118 {
7119 eassert (it->len != 0);
7120
7121 if (!it->bidi_p)
7122 {
7123 IT_BYTEPOS (*it) += it->len;
7124 IT_CHARPOS (*it) += 1;
7125 }
7126 else
7127 {
7128 int prev_scan_dir = it->bidi_it.scan_dir;
7129 /* If this is a new paragraph, determine its base
7130 direction (a.k.a. its base embedding level). */
7131 if (it->bidi_it.new_paragraph)
7132 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7133 bidi_move_to_visually_next (&it->bidi_it);
7134 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7135 IT_CHARPOS (*it) = it->bidi_it.charpos;
7136 if (prev_scan_dir != it->bidi_it.scan_dir)
7137 {
7138 /* As the scan direction was changed, we must
7139 re-compute the stop position for composition. */
7140 ptrdiff_t stop = it->end_charpos;
7141 if (it->bidi_it.scan_dir < 0)
7142 stop = -1;
7143 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7144 IT_BYTEPOS (*it), stop, Qnil);
7145 }
7146 }
7147 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7148 }
7149 break;
7150
7151 case GET_FROM_C_STRING:
7152 /* Current display element of IT is from a C string. */
7153 if (!it->bidi_p
7154 /* If the string position is beyond string's end, it means
7155 next_element_from_c_string is padding the string with
7156 blanks, in which case we bypass the bidi iterator,
7157 because it cannot deal with such virtual characters. */
7158 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7159 {
7160 IT_BYTEPOS (*it) += it->len;
7161 IT_CHARPOS (*it) += 1;
7162 }
7163 else
7164 {
7165 bidi_move_to_visually_next (&it->bidi_it);
7166 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7167 IT_CHARPOS (*it) = it->bidi_it.charpos;
7168 }
7169 break;
7170
7171 case GET_FROM_DISPLAY_VECTOR:
7172 /* Current display element of IT is from a display table entry.
7173 Advance in the display table definition. Reset it to null if
7174 end reached, and continue with characters from buffers/
7175 strings. */
7176 ++it->current.dpvec_index;
7177
7178 /* Restore face of the iterator to what they were before the
7179 display vector entry (these entries may contain faces). */
7180 it->face_id = it->saved_face_id;
7181
7182 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7183 {
7184 int recheck_faces = it->ellipsis_p;
7185
7186 if (it->s)
7187 it->method = GET_FROM_C_STRING;
7188 else if (STRINGP (it->string))
7189 it->method = GET_FROM_STRING;
7190 else
7191 {
7192 it->method = GET_FROM_BUFFER;
7193 it->object = it->w->contents;
7194 }
7195
7196 it->dpvec = NULL;
7197 it->current.dpvec_index = -1;
7198
7199 /* Skip over characters which were displayed via IT->dpvec. */
7200 if (it->dpvec_char_len < 0)
7201 reseat_at_next_visible_line_start (it, 1);
7202 else if (it->dpvec_char_len > 0)
7203 {
7204 if (it->method == GET_FROM_STRING
7205 && it->current.overlay_string_index >= 0
7206 && it->n_overlay_strings > 0)
7207 it->ignore_overlay_strings_at_pos_p = 1;
7208 it->len = it->dpvec_char_len;
7209 set_iterator_to_next (it, reseat_p);
7210 }
7211
7212 /* Maybe recheck faces after display vector */
7213 if (recheck_faces)
7214 it->stop_charpos = IT_CHARPOS (*it);
7215 }
7216 break;
7217
7218 case GET_FROM_STRING:
7219 /* Current display element is a character from a Lisp string. */
7220 eassert (it->s == NULL && STRINGP (it->string));
7221 /* Don't advance past string end. These conditions are true
7222 when set_iterator_to_next is called at the end of
7223 get_next_display_element, in which case the Lisp string is
7224 already exhausted, and all we want is pop the iterator
7225 stack. */
7226 if (it->current.overlay_string_index >= 0)
7227 {
7228 /* This is an overlay string, so there's no padding with
7229 spaces, and the number of characters in the string is
7230 where the string ends. */
7231 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7232 goto consider_string_end;
7233 }
7234 else
7235 {
7236 /* Not an overlay string. There could be padding, so test
7237 against it->end_charpos . */
7238 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7239 goto consider_string_end;
7240 }
7241 if (it->cmp_it.id >= 0)
7242 {
7243 int i;
7244
7245 if (! it->bidi_p)
7246 {
7247 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7248 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7249 if (it->cmp_it.to < it->cmp_it.nglyphs)
7250 it->cmp_it.from = it->cmp_it.to;
7251 else
7252 {
7253 it->cmp_it.id = -1;
7254 composition_compute_stop_pos (&it->cmp_it,
7255 IT_STRING_CHARPOS (*it),
7256 IT_STRING_BYTEPOS (*it),
7257 it->end_charpos, it->string);
7258 }
7259 }
7260 else if (! it->cmp_it.reversed_p)
7261 {
7262 for (i = 0; i < it->cmp_it.nchars; i++)
7263 bidi_move_to_visually_next (&it->bidi_it);
7264 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7265 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7266
7267 if (it->cmp_it.to < it->cmp_it.nglyphs)
7268 it->cmp_it.from = it->cmp_it.to;
7269 else
7270 {
7271 ptrdiff_t stop = it->end_charpos;
7272 if (it->bidi_it.scan_dir < 0)
7273 stop = -1;
7274 composition_compute_stop_pos (&it->cmp_it,
7275 IT_STRING_CHARPOS (*it),
7276 IT_STRING_BYTEPOS (*it), stop,
7277 it->string);
7278 }
7279 }
7280 else
7281 {
7282 for (i = 0; i < it->cmp_it.nchars; i++)
7283 bidi_move_to_visually_next (&it->bidi_it);
7284 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7285 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7286 if (it->cmp_it.from > 0)
7287 it->cmp_it.to = it->cmp_it.from;
7288 else
7289 {
7290 ptrdiff_t stop = it->end_charpos;
7291 if (it->bidi_it.scan_dir < 0)
7292 stop = -1;
7293 composition_compute_stop_pos (&it->cmp_it,
7294 IT_STRING_CHARPOS (*it),
7295 IT_STRING_BYTEPOS (*it), stop,
7296 it->string);
7297 }
7298 }
7299 }
7300 else
7301 {
7302 if (!it->bidi_p
7303 /* If the string position is beyond string's end, it
7304 means next_element_from_string is padding the string
7305 with blanks, in which case we bypass the bidi
7306 iterator, because it cannot deal with such virtual
7307 characters. */
7308 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7309 {
7310 IT_STRING_BYTEPOS (*it) += it->len;
7311 IT_STRING_CHARPOS (*it) += 1;
7312 }
7313 else
7314 {
7315 int prev_scan_dir = it->bidi_it.scan_dir;
7316
7317 bidi_move_to_visually_next (&it->bidi_it);
7318 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7319 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7320 if (prev_scan_dir != it->bidi_it.scan_dir)
7321 {
7322 ptrdiff_t stop = it->end_charpos;
7323
7324 if (it->bidi_it.scan_dir < 0)
7325 stop = -1;
7326 composition_compute_stop_pos (&it->cmp_it,
7327 IT_STRING_CHARPOS (*it),
7328 IT_STRING_BYTEPOS (*it), stop,
7329 it->string);
7330 }
7331 }
7332 }
7333
7334 consider_string_end:
7335
7336 if (it->current.overlay_string_index >= 0)
7337 {
7338 /* IT->string is an overlay string. Advance to the
7339 next, if there is one. */
7340 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7341 {
7342 it->ellipsis_p = 0;
7343 next_overlay_string (it);
7344 if (it->ellipsis_p)
7345 setup_for_ellipsis (it, 0);
7346 }
7347 }
7348 else
7349 {
7350 /* IT->string is not an overlay string. If we reached
7351 its end, and there is something on IT->stack, proceed
7352 with what is on the stack. This can be either another
7353 string, this time an overlay string, or a buffer. */
7354 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7355 && it->sp > 0)
7356 {
7357 pop_it (it);
7358 if (it->method == GET_FROM_STRING)
7359 goto consider_string_end;
7360 }
7361 }
7362 break;
7363
7364 case GET_FROM_IMAGE:
7365 case GET_FROM_STRETCH:
7366 /* The position etc with which we have to proceed are on
7367 the stack. The position may be at the end of a string,
7368 if the `display' property takes up the whole string. */
7369 eassert (it->sp > 0);
7370 pop_it (it);
7371 if (it->method == GET_FROM_STRING)
7372 goto consider_string_end;
7373 break;
7374
7375 default:
7376 /* There are no other methods defined, so this should be a bug. */
7377 emacs_abort ();
7378 }
7379
7380 eassert (it->method != GET_FROM_STRING
7381 || (STRINGP (it->string)
7382 && IT_STRING_CHARPOS (*it) >= 0));
7383 }
7384
7385 /* Load IT's display element fields with information about the next
7386 display element which comes from a display table entry or from the
7387 result of translating a control character to one of the forms `^C'
7388 or `\003'.
7389
7390 IT->dpvec holds the glyphs to return as characters.
7391 IT->saved_face_id holds the face id before the display vector--it
7392 is restored into IT->face_id in set_iterator_to_next. */
7393
7394 static int
7395 next_element_from_display_vector (struct it *it)
7396 {
7397 Lisp_Object gc;
7398 int prev_face_id = it->face_id;
7399 int next_face_id;
7400
7401 /* Precondition. */
7402 eassert (it->dpvec && it->current.dpvec_index >= 0);
7403
7404 it->face_id = it->saved_face_id;
7405
7406 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7407 That seemed totally bogus - so I changed it... */
7408 gc = it->dpvec[it->current.dpvec_index];
7409
7410 if (GLYPH_CODE_P (gc))
7411 {
7412 struct face *this_face, *prev_face, *next_face;
7413
7414 it->c = GLYPH_CODE_CHAR (gc);
7415 it->len = CHAR_BYTES (it->c);
7416
7417 /* The entry may contain a face id to use. Such a face id is
7418 the id of a Lisp face, not a realized face. A face id of
7419 zero means no face is specified. */
7420 if (it->dpvec_face_id >= 0)
7421 it->face_id = it->dpvec_face_id;
7422 else
7423 {
7424 int lface_id = GLYPH_CODE_FACE (gc);
7425 if (lface_id > 0)
7426 it->face_id = merge_faces (it->f, Qt, lface_id,
7427 it->saved_face_id);
7428 }
7429
7430 /* Glyphs in the display vector could have the box face, so we
7431 need to set the related flags in the iterator, as
7432 appropriate. */
7433 this_face = FACE_FROM_ID (it->f, it->face_id);
7434 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7435
7436 /* Is this character the first character of a box-face run? */
7437 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7438 && (!prev_face
7439 || prev_face->box == FACE_NO_BOX));
7440
7441 /* For the last character of the box-face run, we need to look
7442 either at the next glyph from the display vector, or at the
7443 face we saw before the display vector. */
7444 next_face_id = it->saved_face_id;
7445 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7446 {
7447 if (it->dpvec_face_id >= 0)
7448 next_face_id = it->dpvec_face_id;
7449 else
7450 {
7451 int lface_id =
7452 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7453
7454 if (lface_id > 0)
7455 next_face_id = merge_faces (it->f, Qt, lface_id,
7456 it->saved_face_id);
7457 }
7458 }
7459 next_face = FACE_FROM_ID (it->f, next_face_id);
7460 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7461 && (!next_face
7462 || next_face->box == FACE_NO_BOX));
7463 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7464 }
7465 else
7466 /* Display table entry is invalid. Return a space. */
7467 it->c = ' ', it->len = 1;
7468
7469 /* Don't change position and object of the iterator here. They are
7470 still the values of the character that had this display table
7471 entry or was translated, and that's what we want. */
7472 it->what = IT_CHARACTER;
7473 return 1;
7474 }
7475
7476 /* Get the first element of string/buffer in the visual order, after
7477 being reseated to a new position in a string or a buffer. */
7478 static void
7479 get_visually_first_element (struct it *it)
7480 {
7481 int string_p = STRINGP (it->string) || it->s;
7482 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7483 ptrdiff_t bob = (string_p ? 0 : BEGV);
7484
7485 if (STRINGP (it->string))
7486 {
7487 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7488 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7489 }
7490 else
7491 {
7492 it->bidi_it.charpos = IT_CHARPOS (*it);
7493 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7494 }
7495
7496 if (it->bidi_it.charpos == eob)
7497 {
7498 /* Nothing to do, but reset the FIRST_ELT flag, like
7499 bidi_paragraph_init does, because we are not going to
7500 call it. */
7501 it->bidi_it.first_elt = 0;
7502 }
7503 else if (it->bidi_it.charpos == bob
7504 || (!string_p
7505 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7506 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7507 {
7508 /* If we are at the beginning of a line/string, we can produce
7509 the next element right away. */
7510 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7511 bidi_move_to_visually_next (&it->bidi_it);
7512 }
7513 else
7514 {
7515 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7516
7517 /* We need to prime the bidi iterator starting at the line's or
7518 string's beginning, before we will be able to produce the
7519 next element. */
7520 if (string_p)
7521 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7522 else
7523 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7524 IT_BYTEPOS (*it), -1,
7525 &it->bidi_it.bytepos);
7526 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7527 do
7528 {
7529 /* Now return to buffer/string position where we were asked
7530 to get the next display element, and produce that. */
7531 bidi_move_to_visually_next (&it->bidi_it);
7532 }
7533 while (it->bidi_it.bytepos != orig_bytepos
7534 && it->bidi_it.charpos < eob);
7535 }
7536
7537 /* Adjust IT's position information to where we ended up. */
7538 if (STRINGP (it->string))
7539 {
7540 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7541 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7542 }
7543 else
7544 {
7545 IT_CHARPOS (*it) = it->bidi_it.charpos;
7546 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7547 }
7548
7549 if (STRINGP (it->string) || !it->s)
7550 {
7551 ptrdiff_t stop, charpos, bytepos;
7552
7553 if (STRINGP (it->string))
7554 {
7555 eassert (!it->s);
7556 stop = SCHARS (it->string);
7557 if (stop > it->end_charpos)
7558 stop = it->end_charpos;
7559 charpos = IT_STRING_CHARPOS (*it);
7560 bytepos = IT_STRING_BYTEPOS (*it);
7561 }
7562 else
7563 {
7564 stop = it->end_charpos;
7565 charpos = IT_CHARPOS (*it);
7566 bytepos = IT_BYTEPOS (*it);
7567 }
7568 if (it->bidi_it.scan_dir < 0)
7569 stop = -1;
7570 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7571 it->string);
7572 }
7573 }
7574
7575 /* Load IT with the next display element from Lisp string IT->string.
7576 IT->current.string_pos is the current position within the string.
7577 If IT->current.overlay_string_index >= 0, the Lisp string is an
7578 overlay string. */
7579
7580 static int
7581 next_element_from_string (struct it *it)
7582 {
7583 struct text_pos position;
7584
7585 eassert (STRINGP (it->string));
7586 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7587 eassert (IT_STRING_CHARPOS (*it) >= 0);
7588 position = it->current.string_pos;
7589
7590 /* With bidi reordering, the character to display might not be the
7591 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7592 that we were reseat()ed to a new string, whose paragraph
7593 direction is not known. */
7594 if (it->bidi_p && it->bidi_it.first_elt)
7595 {
7596 get_visually_first_element (it);
7597 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7598 }
7599
7600 /* Time to check for invisible text? */
7601 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7602 {
7603 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7604 {
7605 if (!(!it->bidi_p
7606 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7607 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7608 {
7609 /* With bidi non-linear iteration, we could find
7610 ourselves far beyond the last computed stop_charpos,
7611 with several other stop positions in between that we
7612 missed. Scan them all now, in buffer's logical
7613 order, until we find and handle the last stop_charpos
7614 that precedes our current position. */
7615 handle_stop_backwards (it, it->stop_charpos);
7616 return GET_NEXT_DISPLAY_ELEMENT (it);
7617 }
7618 else
7619 {
7620 if (it->bidi_p)
7621 {
7622 /* Take note of the stop position we just moved
7623 across, for when we will move back across it. */
7624 it->prev_stop = it->stop_charpos;
7625 /* If we are at base paragraph embedding level, take
7626 note of the last stop position seen at this
7627 level. */
7628 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7629 it->base_level_stop = it->stop_charpos;
7630 }
7631 handle_stop (it);
7632
7633 /* Since a handler may have changed IT->method, we must
7634 recurse here. */
7635 return GET_NEXT_DISPLAY_ELEMENT (it);
7636 }
7637 }
7638 else if (it->bidi_p
7639 /* If we are before prev_stop, we may have overstepped
7640 on our way backwards a stop_pos, and if so, we need
7641 to handle that stop_pos. */
7642 && IT_STRING_CHARPOS (*it) < it->prev_stop
7643 /* We can sometimes back up for reasons that have nothing
7644 to do with bidi reordering. E.g., compositions. The
7645 code below is only needed when we are above the base
7646 embedding level, so test for that explicitly. */
7647 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7648 {
7649 /* If we lost track of base_level_stop, we have no better
7650 place for handle_stop_backwards to start from than string
7651 beginning. This happens, e.g., when we were reseated to
7652 the previous screenful of text by vertical-motion. */
7653 if (it->base_level_stop <= 0
7654 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7655 it->base_level_stop = 0;
7656 handle_stop_backwards (it, it->base_level_stop);
7657 return GET_NEXT_DISPLAY_ELEMENT (it);
7658 }
7659 }
7660
7661 if (it->current.overlay_string_index >= 0)
7662 {
7663 /* Get the next character from an overlay string. In overlay
7664 strings, there is no field width or padding with spaces to
7665 do. */
7666 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7667 {
7668 it->what = IT_EOB;
7669 return 0;
7670 }
7671 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7672 IT_STRING_BYTEPOS (*it),
7673 it->bidi_it.scan_dir < 0
7674 ? -1
7675 : SCHARS (it->string))
7676 && next_element_from_composition (it))
7677 {
7678 return 1;
7679 }
7680 else if (STRING_MULTIBYTE (it->string))
7681 {
7682 const unsigned char *s = (SDATA (it->string)
7683 + IT_STRING_BYTEPOS (*it));
7684 it->c = string_char_and_length (s, &it->len);
7685 }
7686 else
7687 {
7688 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7689 it->len = 1;
7690 }
7691 }
7692 else
7693 {
7694 /* Get the next character from a Lisp string that is not an
7695 overlay string. Such strings come from the mode line, for
7696 example. We may have to pad with spaces, or truncate the
7697 string. See also next_element_from_c_string. */
7698 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7699 {
7700 it->what = IT_EOB;
7701 return 0;
7702 }
7703 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7704 {
7705 /* Pad with spaces. */
7706 it->c = ' ', it->len = 1;
7707 CHARPOS (position) = BYTEPOS (position) = -1;
7708 }
7709 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7710 IT_STRING_BYTEPOS (*it),
7711 it->bidi_it.scan_dir < 0
7712 ? -1
7713 : it->string_nchars)
7714 && next_element_from_composition (it))
7715 {
7716 return 1;
7717 }
7718 else if (STRING_MULTIBYTE (it->string))
7719 {
7720 const unsigned char *s = (SDATA (it->string)
7721 + IT_STRING_BYTEPOS (*it));
7722 it->c = string_char_and_length (s, &it->len);
7723 }
7724 else
7725 {
7726 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7727 it->len = 1;
7728 }
7729 }
7730
7731 /* Record what we have and where it came from. */
7732 it->what = IT_CHARACTER;
7733 it->object = it->string;
7734 it->position = position;
7735 return 1;
7736 }
7737
7738
7739 /* Load IT with next display element from C string IT->s.
7740 IT->string_nchars is the maximum number of characters to return
7741 from the string. IT->end_charpos may be greater than
7742 IT->string_nchars when this function is called, in which case we
7743 may have to return padding spaces. Value is zero if end of string
7744 reached, including padding spaces. */
7745
7746 static int
7747 next_element_from_c_string (struct it *it)
7748 {
7749 int success_p = 1;
7750
7751 eassert (it->s);
7752 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7753 it->what = IT_CHARACTER;
7754 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7755 it->object = Qnil;
7756
7757 /* With bidi reordering, the character to display might not be the
7758 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7759 we were reseated to a new string, whose paragraph direction is
7760 not known. */
7761 if (it->bidi_p && it->bidi_it.first_elt)
7762 get_visually_first_element (it);
7763
7764 /* IT's position can be greater than IT->string_nchars in case a
7765 field width or precision has been specified when the iterator was
7766 initialized. */
7767 if (IT_CHARPOS (*it) >= it->end_charpos)
7768 {
7769 /* End of the game. */
7770 it->what = IT_EOB;
7771 success_p = 0;
7772 }
7773 else if (IT_CHARPOS (*it) >= it->string_nchars)
7774 {
7775 /* Pad with spaces. */
7776 it->c = ' ', it->len = 1;
7777 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7778 }
7779 else if (it->multibyte_p)
7780 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7781 else
7782 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7783
7784 return success_p;
7785 }
7786
7787
7788 /* Set up IT to return characters from an ellipsis, if appropriate.
7789 The definition of the ellipsis glyphs may come from a display table
7790 entry. This function fills IT with the first glyph from the
7791 ellipsis if an ellipsis is to be displayed. */
7792
7793 static int
7794 next_element_from_ellipsis (struct it *it)
7795 {
7796 if (it->selective_display_ellipsis_p)
7797 setup_for_ellipsis (it, it->len);
7798 else
7799 {
7800 /* The face at the current position may be different from the
7801 face we find after the invisible text. Remember what it
7802 was in IT->saved_face_id, and signal that it's there by
7803 setting face_before_selective_p. */
7804 it->saved_face_id = it->face_id;
7805 it->method = GET_FROM_BUFFER;
7806 it->object = it->w->contents;
7807 reseat_at_next_visible_line_start (it, 1);
7808 it->face_before_selective_p = 1;
7809 }
7810
7811 return GET_NEXT_DISPLAY_ELEMENT (it);
7812 }
7813
7814
7815 /* Deliver an image display element. The iterator IT is already
7816 filled with image information (done in handle_display_prop). Value
7817 is always 1. */
7818
7819
7820 static int
7821 next_element_from_image (struct it *it)
7822 {
7823 it->what = IT_IMAGE;
7824 it->ignore_overlay_strings_at_pos_p = 0;
7825 return 1;
7826 }
7827
7828
7829 /* Fill iterator IT with next display element from a stretch glyph
7830 property. IT->object is the value of the text property. Value is
7831 always 1. */
7832
7833 static int
7834 next_element_from_stretch (struct it *it)
7835 {
7836 it->what = IT_STRETCH;
7837 return 1;
7838 }
7839
7840 /* Scan backwards from IT's current position until we find a stop
7841 position, or until BEGV. This is called when we find ourself
7842 before both the last known prev_stop and base_level_stop while
7843 reordering bidirectional text. */
7844
7845 static void
7846 compute_stop_pos_backwards (struct it *it)
7847 {
7848 const int SCAN_BACK_LIMIT = 1000;
7849 struct text_pos pos;
7850 struct display_pos save_current = it->current;
7851 struct text_pos save_position = it->position;
7852 ptrdiff_t charpos = IT_CHARPOS (*it);
7853 ptrdiff_t where_we_are = charpos;
7854 ptrdiff_t save_stop_pos = it->stop_charpos;
7855 ptrdiff_t save_end_pos = it->end_charpos;
7856
7857 eassert (NILP (it->string) && !it->s);
7858 eassert (it->bidi_p);
7859 it->bidi_p = 0;
7860 do
7861 {
7862 it->end_charpos = min (charpos + 1, ZV);
7863 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7864 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7865 reseat_1 (it, pos, 0);
7866 compute_stop_pos (it);
7867 /* We must advance forward, right? */
7868 if (it->stop_charpos <= charpos)
7869 emacs_abort ();
7870 }
7871 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7872
7873 if (it->stop_charpos <= where_we_are)
7874 it->prev_stop = it->stop_charpos;
7875 else
7876 it->prev_stop = BEGV;
7877 it->bidi_p = 1;
7878 it->current = save_current;
7879 it->position = save_position;
7880 it->stop_charpos = save_stop_pos;
7881 it->end_charpos = save_end_pos;
7882 }
7883
7884 /* Scan forward from CHARPOS in the current buffer/string, until we
7885 find a stop position > current IT's position. Then handle the stop
7886 position before that. This is called when we bump into a stop
7887 position while reordering bidirectional text. CHARPOS should be
7888 the last previously processed stop_pos (or BEGV/0, if none were
7889 processed yet) whose position is less that IT's current
7890 position. */
7891
7892 static void
7893 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7894 {
7895 int bufp = !STRINGP (it->string);
7896 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7897 struct display_pos save_current = it->current;
7898 struct text_pos save_position = it->position;
7899 struct text_pos pos1;
7900 ptrdiff_t next_stop;
7901
7902 /* Scan in strict logical order. */
7903 eassert (it->bidi_p);
7904 it->bidi_p = 0;
7905 do
7906 {
7907 it->prev_stop = charpos;
7908 if (bufp)
7909 {
7910 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7911 reseat_1 (it, pos1, 0);
7912 }
7913 else
7914 it->current.string_pos = string_pos (charpos, it->string);
7915 compute_stop_pos (it);
7916 /* We must advance forward, right? */
7917 if (it->stop_charpos <= it->prev_stop)
7918 emacs_abort ();
7919 charpos = it->stop_charpos;
7920 }
7921 while (charpos <= where_we_are);
7922
7923 it->bidi_p = 1;
7924 it->current = save_current;
7925 it->position = save_position;
7926 next_stop = it->stop_charpos;
7927 it->stop_charpos = it->prev_stop;
7928 handle_stop (it);
7929 it->stop_charpos = next_stop;
7930 }
7931
7932 /* Load IT with the next display element from current_buffer. Value
7933 is zero if end of buffer reached. IT->stop_charpos is the next
7934 position at which to stop and check for text properties or buffer
7935 end. */
7936
7937 static int
7938 next_element_from_buffer (struct it *it)
7939 {
7940 int success_p = 1;
7941
7942 eassert (IT_CHARPOS (*it) >= BEGV);
7943 eassert (NILP (it->string) && !it->s);
7944 eassert (!it->bidi_p
7945 || (EQ (it->bidi_it.string.lstring, Qnil)
7946 && it->bidi_it.string.s == NULL));
7947
7948 /* With bidi reordering, the character to display might not be the
7949 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7950 we were reseat()ed to a new buffer position, which is potentially
7951 a different paragraph. */
7952 if (it->bidi_p && it->bidi_it.first_elt)
7953 {
7954 get_visually_first_element (it);
7955 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7956 }
7957
7958 if (IT_CHARPOS (*it) >= it->stop_charpos)
7959 {
7960 if (IT_CHARPOS (*it) >= it->end_charpos)
7961 {
7962 int overlay_strings_follow_p;
7963
7964 /* End of the game, except when overlay strings follow that
7965 haven't been returned yet. */
7966 if (it->overlay_strings_at_end_processed_p)
7967 overlay_strings_follow_p = 0;
7968 else
7969 {
7970 it->overlay_strings_at_end_processed_p = 1;
7971 overlay_strings_follow_p = get_overlay_strings (it, 0);
7972 }
7973
7974 if (overlay_strings_follow_p)
7975 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7976 else
7977 {
7978 it->what = IT_EOB;
7979 it->position = it->current.pos;
7980 success_p = 0;
7981 }
7982 }
7983 else if (!(!it->bidi_p
7984 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7985 || IT_CHARPOS (*it) == it->stop_charpos))
7986 {
7987 /* With bidi non-linear iteration, we could find ourselves
7988 far beyond the last computed stop_charpos, with several
7989 other stop positions in between that we missed. Scan
7990 them all now, in buffer's logical order, until we find
7991 and handle the last stop_charpos that precedes our
7992 current position. */
7993 handle_stop_backwards (it, it->stop_charpos);
7994 return GET_NEXT_DISPLAY_ELEMENT (it);
7995 }
7996 else
7997 {
7998 if (it->bidi_p)
7999 {
8000 /* Take note of the stop position we just moved across,
8001 for when we will move back across it. */
8002 it->prev_stop = it->stop_charpos;
8003 /* If we are at base paragraph embedding level, take
8004 note of the last stop position seen at this
8005 level. */
8006 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8007 it->base_level_stop = it->stop_charpos;
8008 }
8009 handle_stop (it);
8010 return GET_NEXT_DISPLAY_ELEMENT (it);
8011 }
8012 }
8013 else if (it->bidi_p
8014 /* If we are before prev_stop, we may have overstepped on
8015 our way backwards a stop_pos, and if so, we need to
8016 handle that stop_pos. */
8017 && IT_CHARPOS (*it) < it->prev_stop
8018 /* We can sometimes back up for reasons that have nothing
8019 to do with bidi reordering. E.g., compositions. The
8020 code below is only needed when we are above the base
8021 embedding level, so test for that explicitly. */
8022 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8023 {
8024 if (it->base_level_stop <= 0
8025 || IT_CHARPOS (*it) < it->base_level_stop)
8026 {
8027 /* If we lost track of base_level_stop, we need to find
8028 prev_stop by looking backwards. This happens, e.g., when
8029 we were reseated to the previous screenful of text by
8030 vertical-motion. */
8031 it->base_level_stop = BEGV;
8032 compute_stop_pos_backwards (it);
8033 handle_stop_backwards (it, it->prev_stop);
8034 }
8035 else
8036 handle_stop_backwards (it, it->base_level_stop);
8037 return GET_NEXT_DISPLAY_ELEMENT (it);
8038 }
8039 else
8040 {
8041 /* No face changes, overlays etc. in sight, so just return a
8042 character from current_buffer. */
8043 unsigned char *p;
8044 ptrdiff_t stop;
8045
8046 /* Maybe run the redisplay end trigger hook. Performance note:
8047 This doesn't seem to cost measurable time. */
8048 if (it->redisplay_end_trigger_charpos
8049 && it->glyph_row
8050 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8051 run_redisplay_end_trigger_hook (it);
8052
8053 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8054 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8055 stop)
8056 && next_element_from_composition (it))
8057 {
8058 return 1;
8059 }
8060
8061 /* Get the next character, maybe multibyte. */
8062 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8063 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8064 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8065 else
8066 it->c = *p, it->len = 1;
8067
8068 /* Record what we have and where it came from. */
8069 it->what = IT_CHARACTER;
8070 it->object = it->w->contents;
8071 it->position = it->current.pos;
8072
8073 /* Normally we return the character found above, except when we
8074 really want to return an ellipsis for selective display. */
8075 if (it->selective)
8076 {
8077 if (it->c == '\n')
8078 {
8079 /* A value of selective > 0 means hide lines indented more
8080 than that number of columns. */
8081 if (it->selective > 0
8082 && IT_CHARPOS (*it) + 1 < ZV
8083 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8084 IT_BYTEPOS (*it) + 1,
8085 it->selective))
8086 {
8087 success_p = next_element_from_ellipsis (it);
8088 it->dpvec_char_len = -1;
8089 }
8090 }
8091 else if (it->c == '\r' && it->selective == -1)
8092 {
8093 /* A value of selective == -1 means that everything from the
8094 CR to the end of the line is invisible, with maybe an
8095 ellipsis displayed for it. */
8096 success_p = next_element_from_ellipsis (it);
8097 it->dpvec_char_len = -1;
8098 }
8099 }
8100 }
8101
8102 /* Value is zero if end of buffer reached. */
8103 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8104 return success_p;
8105 }
8106
8107
8108 /* Run the redisplay end trigger hook for IT. */
8109
8110 static void
8111 run_redisplay_end_trigger_hook (struct it *it)
8112 {
8113 Lisp_Object args[3];
8114
8115 /* IT->glyph_row should be non-null, i.e. we should be actually
8116 displaying something, or otherwise we should not run the hook. */
8117 eassert (it->glyph_row);
8118
8119 /* Set up hook arguments. */
8120 args[0] = Qredisplay_end_trigger_functions;
8121 args[1] = it->window;
8122 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8123 it->redisplay_end_trigger_charpos = 0;
8124
8125 /* Since we are *trying* to run these functions, don't try to run
8126 them again, even if they get an error. */
8127 wset_redisplay_end_trigger (it->w, Qnil);
8128 Frun_hook_with_args (3, args);
8129
8130 /* Notice if it changed the face of the character we are on. */
8131 handle_face_prop (it);
8132 }
8133
8134
8135 /* Deliver a composition display element. Unlike the other
8136 next_element_from_XXX, this function is not registered in the array
8137 get_next_element[]. It is called from next_element_from_buffer and
8138 next_element_from_string when necessary. */
8139
8140 static int
8141 next_element_from_composition (struct it *it)
8142 {
8143 it->what = IT_COMPOSITION;
8144 it->len = it->cmp_it.nbytes;
8145 if (STRINGP (it->string))
8146 {
8147 if (it->c < 0)
8148 {
8149 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8150 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8151 return 0;
8152 }
8153 it->position = it->current.string_pos;
8154 it->object = it->string;
8155 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8156 IT_STRING_BYTEPOS (*it), it->string);
8157 }
8158 else
8159 {
8160 if (it->c < 0)
8161 {
8162 IT_CHARPOS (*it) += it->cmp_it.nchars;
8163 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8164 if (it->bidi_p)
8165 {
8166 if (it->bidi_it.new_paragraph)
8167 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8168 /* Resync the bidi iterator with IT's new position.
8169 FIXME: this doesn't support bidirectional text. */
8170 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8171 bidi_move_to_visually_next (&it->bidi_it);
8172 }
8173 return 0;
8174 }
8175 it->position = it->current.pos;
8176 it->object = it->w->contents;
8177 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8178 IT_BYTEPOS (*it), Qnil);
8179 }
8180 return 1;
8181 }
8182
8183
8184 \f
8185 /***********************************************************************
8186 Moving an iterator without producing glyphs
8187 ***********************************************************************/
8188
8189 /* Check if iterator is at a position corresponding to a valid buffer
8190 position after some move_it_ call. */
8191
8192 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8193 ((it)->method == GET_FROM_STRING \
8194 ? IT_STRING_CHARPOS (*it) == 0 \
8195 : 1)
8196
8197
8198 /* Move iterator IT to a specified buffer or X position within one
8199 line on the display without producing glyphs.
8200
8201 OP should be a bit mask including some or all of these bits:
8202 MOVE_TO_X: Stop upon reaching x-position TO_X.
8203 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8204 Regardless of OP's value, stop upon reaching the end of the display line.
8205
8206 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8207 This means, in particular, that TO_X includes window's horizontal
8208 scroll amount.
8209
8210 The return value has several possible values that
8211 say what condition caused the scan to stop:
8212
8213 MOVE_POS_MATCH_OR_ZV
8214 - when TO_POS or ZV was reached.
8215
8216 MOVE_X_REACHED
8217 -when TO_X was reached before TO_POS or ZV were reached.
8218
8219 MOVE_LINE_CONTINUED
8220 - when we reached the end of the display area and the line must
8221 be continued.
8222
8223 MOVE_LINE_TRUNCATED
8224 - when we reached the end of the display area and the line is
8225 truncated.
8226
8227 MOVE_NEWLINE_OR_CR
8228 - when we stopped at a line end, i.e. a newline or a CR and selective
8229 display is on. */
8230
8231 static enum move_it_result
8232 move_it_in_display_line_to (struct it *it,
8233 ptrdiff_t to_charpos, int to_x,
8234 enum move_operation_enum op)
8235 {
8236 enum move_it_result result = MOVE_UNDEFINED;
8237 struct glyph_row *saved_glyph_row;
8238 struct it wrap_it, atpos_it, atx_it, ppos_it;
8239 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8240 void *ppos_data = NULL;
8241 int may_wrap = 0;
8242 enum it_method prev_method = it->method;
8243 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8244 int saw_smaller_pos = prev_pos < to_charpos;
8245
8246 /* Don't produce glyphs in produce_glyphs. */
8247 saved_glyph_row = it->glyph_row;
8248 it->glyph_row = NULL;
8249
8250 /* Use wrap_it to save a copy of IT wherever a word wrap could
8251 occur. Use atpos_it to save a copy of IT at the desired buffer
8252 position, if found, so that we can scan ahead and check if the
8253 word later overshoots the window edge. Use atx_it similarly, for
8254 pixel positions. */
8255 wrap_it.sp = -1;
8256 atpos_it.sp = -1;
8257 atx_it.sp = -1;
8258
8259 /* Use ppos_it under bidi reordering to save a copy of IT for the
8260 position > CHARPOS that is the closest to CHARPOS. We restore
8261 that position in IT when we have scanned the entire display line
8262 without finding a match for CHARPOS and all the character
8263 positions are greater than CHARPOS. */
8264 if (it->bidi_p)
8265 {
8266 SAVE_IT (ppos_it, *it, ppos_data);
8267 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8268 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8269 SAVE_IT (ppos_it, *it, ppos_data);
8270 }
8271
8272 #define BUFFER_POS_REACHED_P() \
8273 ((op & MOVE_TO_POS) != 0 \
8274 && BUFFERP (it->object) \
8275 && (IT_CHARPOS (*it) == to_charpos \
8276 || ((!it->bidi_p \
8277 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8278 && IT_CHARPOS (*it) > to_charpos) \
8279 || (it->what == IT_COMPOSITION \
8280 && ((IT_CHARPOS (*it) > to_charpos \
8281 && to_charpos >= it->cmp_it.charpos) \
8282 || (IT_CHARPOS (*it) < to_charpos \
8283 && to_charpos <= it->cmp_it.charpos)))) \
8284 && (it->method == GET_FROM_BUFFER \
8285 || (it->method == GET_FROM_DISPLAY_VECTOR \
8286 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8287
8288 /* If there's a line-/wrap-prefix, handle it. */
8289 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8290 && it->current_y < it->last_visible_y)
8291 handle_line_prefix (it);
8292
8293 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8294 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8295
8296 while (1)
8297 {
8298 int x, i, ascent = 0, descent = 0;
8299
8300 /* Utility macro to reset an iterator with x, ascent, and descent. */
8301 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8302 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8303 (IT)->max_descent = descent)
8304
8305 /* Stop if we move beyond TO_CHARPOS (after an image or a
8306 display string or stretch glyph). */
8307 if ((op & MOVE_TO_POS) != 0
8308 && BUFFERP (it->object)
8309 && it->method == GET_FROM_BUFFER
8310 && (((!it->bidi_p
8311 /* When the iterator is at base embedding level, we
8312 are guaranteed that characters are delivered for
8313 display in strictly increasing order of their
8314 buffer positions. */
8315 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8316 && IT_CHARPOS (*it) > to_charpos)
8317 || (it->bidi_p
8318 && (prev_method == GET_FROM_IMAGE
8319 || prev_method == GET_FROM_STRETCH
8320 || prev_method == GET_FROM_STRING)
8321 /* Passed TO_CHARPOS from left to right. */
8322 && ((prev_pos < to_charpos
8323 && IT_CHARPOS (*it) > to_charpos)
8324 /* Passed TO_CHARPOS from right to left. */
8325 || (prev_pos > to_charpos
8326 && IT_CHARPOS (*it) < to_charpos)))))
8327 {
8328 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8329 {
8330 result = MOVE_POS_MATCH_OR_ZV;
8331 break;
8332 }
8333 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8334 /* If wrap_it is valid, the current position might be in a
8335 word that is wrapped. So, save the iterator in
8336 atpos_it and continue to see if wrapping happens. */
8337 SAVE_IT (atpos_it, *it, atpos_data);
8338 }
8339
8340 /* Stop when ZV reached.
8341 We used to stop here when TO_CHARPOS reached as well, but that is
8342 too soon if this glyph does not fit on this line. So we handle it
8343 explicitly below. */
8344 if (!get_next_display_element (it))
8345 {
8346 result = MOVE_POS_MATCH_OR_ZV;
8347 break;
8348 }
8349
8350 if (it->line_wrap == TRUNCATE)
8351 {
8352 if (BUFFER_POS_REACHED_P ())
8353 {
8354 result = MOVE_POS_MATCH_OR_ZV;
8355 break;
8356 }
8357 }
8358 else
8359 {
8360 if (it->line_wrap == WORD_WRAP)
8361 {
8362 if (IT_DISPLAYING_WHITESPACE (it))
8363 may_wrap = 1;
8364 else if (may_wrap)
8365 {
8366 /* We have reached a glyph that follows one or more
8367 whitespace characters. If the position is
8368 already found, we are done. */
8369 if (atpos_it.sp >= 0)
8370 {
8371 RESTORE_IT (it, &atpos_it, atpos_data);
8372 result = MOVE_POS_MATCH_OR_ZV;
8373 goto done;
8374 }
8375 if (atx_it.sp >= 0)
8376 {
8377 RESTORE_IT (it, &atx_it, atx_data);
8378 result = MOVE_X_REACHED;
8379 goto done;
8380 }
8381 /* Otherwise, we can wrap here. */
8382 SAVE_IT (wrap_it, *it, wrap_data);
8383 may_wrap = 0;
8384 }
8385 }
8386 }
8387
8388 /* Remember the line height for the current line, in case
8389 the next element doesn't fit on the line. */
8390 ascent = it->max_ascent;
8391 descent = it->max_descent;
8392
8393 /* The call to produce_glyphs will get the metrics of the
8394 display element IT is loaded with. Record the x-position
8395 before this display element, in case it doesn't fit on the
8396 line. */
8397 x = it->current_x;
8398
8399 PRODUCE_GLYPHS (it);
8400
8401 if (it->area != TEXT_AREA)
8402 {
8403 prev_method = it->method;
8404 if (it->method == GET_FROM_BUFFER)
8405 prev_pos = IT_CHARPOS (*it);
8406 set_iterator_to_next (it, 1);
8407 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8408 SET_TEXT_POS (this_line_min_pos,
8409 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8410 if (it->bidi_p
8411 && (op & MOVE_TO_POS)
8412 && IT_CHARPOS (*it) > to_charpos
8413 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8414 SAVE_IT (ppos_it, *it, ppos_data);
8415 continue;
8416 }
8417
8418 /* The number of glyphs we get back in IT->nglyphs will normally
8419 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8420 character on a terminal frame, or (iii) a line end. For the
8421 second case, IT->nglyphs - 1 padding glyphs will be present.
8422 (On X frames, there is only one glyph produced for a
8423 composite character.)
8424
8425 The behavior implemented below means, for continuation lines,
8426 that as many spaces of a TAB as fit on the current line are
8427 displayed there. For terminal frames, as many glyphs of a
8428 multi-glyph character are displayed in the current line, too.
8429 This is what the old redisplay code did, and we keep it that
8430 way. Under X, the whole shape of a complex character must
8431 fit on the line or it will be completely displayed in the
8432 next line.
8433
8434 Note that both for tabs and padding glyphs, all glyphs have
8435 the same width. */
8436 if (it->nglyphs)
8437 {
8438 /* More than one glyph or glyph doesn't fit on line. All
8439 glyphs have the same width. */
8440 int single_glyph_width = it->pixel_width / it->nglyphs;
8441 int new_x;
8442 int x_before_this_char = x;
8443 int hpos_before_this_char = it->hpos;
8444
8445 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8446 {
8447 new_x = x + single_glyph_width;
8448
8449 /* We want to leave anything reaching TO_X to the caller. */
8450 if ((op & MOVE_TO_X) && new_x > to_x)
8451 {
8452 if (BUFFER_POS_REACHED_P ())
8453 {
8454 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8455 goto buffer_pos_reached;
8456 if (atpos_it.sp < 0)
8457 {
8458 SAVE_IT (atpos_it, *it, atpos_data);
8459 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8460 }
8461 }
8462 else
8463 {
8464 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8465 {
8466 it->current_x = x;
8467 result = MOVE_X_REACHED;
8468 break;
8469 }
8470 if (atx_it.sp < 0)
8471 {
8472 SAVE_IT (atx_it, *it, atx_data);
8473 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8474 }
8475 }
8476 }
8477
8478 if (/* Lines are continued. */
8479 it->line_wrap != TRUNCATE
8480 && (/* And glyph doesn't fit on the line. */
8481 new_x > it->last_visible_x
8482 /* Or it fits exactly and we're on a window
8483 system frame. */
8484 || (new_x == it->last_visible_x
8485 && FRAME_WINDOW_P (it->f)
8486 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8487 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8488 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8489 {
8490 if (/* IT->hpos == 0 means the very first glyph
8491 doesn't fit on the line, e.g. a wide image. */
8492 it->hpos == 0
8493 || (new_x == it->last_visible_x
8494 && FRAME_WINDOW_P (it->f)))
8495 {
8496 ++it->hpos;
8497 it->current_x = new_x;
8498
8499 /* The character's last glyph just barely fits
8500 in this row. */
8501 if (i == it->nglyphs - 1)
8502 {
8503 /* If this is the destination position,
8504 return a position *before* it in this row,
8505 now that we know it fits in this row. */
8506 if (BUFFER_POS_REACHED_P ())
8507 {
8508 if (it->line_wrap != WORD_WRAP
8509 || wrap_it.sp < 0)
8510 {
8511 it->hpos = hpos_before_this_char;
8512 it->current_x = x_before_this_char;
8513 result = MOVE_POS_MATCH_OR_ZV;
8514 break;
8515 }
8516 if (it->line_wrap == WORD_WRAP
8517 && atpos_it.sp < 0)
8518 {
8519 SAVE_IT (atpos_it, *it, atpos_data);
8520 atpos_it.current_x = x_before_this_char;
8521 atpos_it.hpos = hpos_before_this_char;
8522 }
8523 }
8524
8525 prev_method = it->method;
8526 if (it->method == GET_FROM_BUFFER)
8527 prev_pos = IT_CHARPOS (*it);
8528 set_iterator_to_next (it, 1);
8529 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8530 SET_TEXT_POS (this_line_min_pos,
8531 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8532 /* On graphical terminals, newlines may
8533 "overflow" into the fringe if
8534 overflow-newline-into-fringe is non-nil.
8535 On text terminals, and on graphical
8536 terminals with no right margin, newlines
8537 may overflow into the last glyph on the
8538 display line.*/
8539 if (!FRAME_WINDOW_P (it->f)
8540 || ((it->bidi_p
8541 && it->bidi_it.paragraph_dir == R2L)
8542 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8543 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8544 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8545 {
8546 if (!get_next_display_element (it))
8547 {
8548 result = MOVE_POS_MATCH_OR_ZV;
8549 break;
8550 }
8551 if (BUFFER_POS_REACHED_P ())
8552 {
8553 if (ITERATOR_AT_END_OF_LINE_P (it))
8554 result = MOVE_POS_MATCH_OR_ZV;
8555 else
8556 result = MOVE_LINE_CONTINUED;
8557 break;
8558 }
8559 if (ITERATOR_AT_END_OF_LINE_P (it)
8560 && (it->line_wrap != WORD_WRAP
8561 || wrap_it.sp < 0))
8562 {
8563 result = MOVE_NEWLINE_OR_CR;
8564 break;
8565 }
8566 }
8567 }
8568 }
8569 else
8570 IT_RESET_X_ASCENT_DESCENT (it);
8571
8572 if (wrap_it.sp >= 0)
8573 {
8574 RESTORE_IT (it, &wrap_it, wrap_data);
8575 atpos_it.sp = -1;
8576 atx_it.sp = -1;
8577 }
8578
8579 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8580 IT_CHARPOS (*it)));
8581 result = MOVE_LINE_CONTINUED;
8582 break;
8583 }
8584
8585 if (BUFFER_POS_REACHED_P ())
8586 {
8587 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8588 goto buffer_pos_reached;
8589 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8590 {
8591 SAVE_IT (atpos_it, *it, atpos_data);
8592 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8593 }
8594 }
8595
8596 if (new_x > it->first_visible_x)
8597 {
8598 /* Glyph is visible. Increment number of glyphs that
8599 would be displayed. */
8600 ++it->hpos;
8601 }
8602 }
8603
8604 if (result != MOVE_UNDEFINED)
8605 break;
8606 }
8607 else if (BUFFER_POS_REACHED_P ())
8608 {
8609 buffer_pos_reached:
8610 IT_RESET_X_ASCENT_DESCENT (it);
8611 result = MOVE_POS_MATCH_OR_ZV;
8612 break;
8613 }
8614 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8615 {
8616 /* Stop when TO_X specified and reached. This check is
8617 necessary here because of lines consisting of a line end,
8618 only. The line end will not produce any glyphs and we
8619 would never get MOVE_X_REACHED. */
8620 eassert (it->nglyphs == 0);
8621 result = MOVE_X_REACHED;
8622 break;
8623 }
8624
8625 /* Is this a line end? If yes, we're done. */
8626 if (ITERATOR_AT_END_OF_LINE_P (it))
8627 {
8628 /* If we are past TO_CHARPOS, but never saw any character
8629 positions smaller than TO_CHARPOS, return
8630 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8631 did. */
8632 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8633 {
8634 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8635 {
8636 if (IT_CHARPOS (ppos_it) < ZV)
8637 {
8638 RESTORE_IT (it, &ppos_it, ppos_data);
8639 result = MOVE_POS_MATCH_OR_ZV;
8640 }
8641 else
8642 goto buffer_pos_reached;
8643 }
8644 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8645 && IT_CHARPOS (*it) > to_charpos)
8646 goto buffer_pos_reached;
8647 else
8648 result = MOVE_NEWLINE_OR_CR;
8649 }
8650 else
8651 result = MOVE_NEWLINE_OR_CR;
8652 break;
8653 }
8654
8655 prev_method = it->method;
8656 if (it->method == GET_FROM_BUFFER)
8657 prev_pos = IT_CHARPOS (*it);
8658 /* The current display element has been consumed. Advance
8659 to the next. */
8660 set_iterator_to_next (it, 1);
8661 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8662 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8663 if (IT_CHARPOS (*it) < to_charpos)
8664 saw_smaller_pos = 1;
8665 if (it->bidi_p
8666 && (op & MOVE_TO_POS)
8667 && IT_CHARPOS (*it) >= to_charpos
8668 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8669 SAVE_IT (ppos_it, *it, ppos_data);
8670
8671 /* Stop if lines are truncated and IT's current x-position is
8672 past the right edge of the window now. */
8673 if (it->line_wrap == TRUNCATE
8674 && it->current_x >= it->last_visible_x)
8675 {
8676 if (!FRAME_WINDOW_P (it->f)
8677 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8678 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8679 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8680 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8681 {
8682 int at_eob_p = 0;
8683
8684 if ((at_eob_p = !get_next_display_element (it))
8685 || BUFFER_POS_REACHED_P ()
8686 /* If we are past TO_CHARPOS, but never saw any
8687 character positions smaller than TO_CHARPOS,
8688 return MOVE_POS_MATCH_OR_ZV, like the
8689 unidirectional display did. */
8690 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8691 && !saw_smaller_pos
8692 && IT_CHARPOS (*it) > to_charpos))
8693 {
8694 if (it->bidi_p
8695 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8696 RESTORE_IT (it, &ppos_it, ppos_data);
8697 result = MOVE_POS_MATCH_OR_ZV;
8698 break;
8699 }
8700 if (ITERATOR_AT_END_OF_LINE_P (it))
8701 {
8702 result = MOVE_NEWLINE_OR_CR;
8703 break;
8704 }
8705 }
8706 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8707 && !saw_smaller_pos
8708 && IT_CHARPOS (*it) > to_charpos)
8709 {
8710 if (IT_CHARPOS (ppos_it) < ZV)
8711 RESTORE_IT (it, &ppos_it, ppos_data);
8712 result = MOVE_POS_MATCH_OR_ZV;
8713 break;
8714 }
8715 result = MOVE_LINE_TRUNCATED;
8716 break;
8717 }
8718 #undef IT_RESET_X_ASCENT_DESCENT
8719 }
8720
8721 #undef BUFFER_POS_REACHED_P
8722
8723 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8724 restore the saved iterator. */
8725 if (atpos_it.sp >= 0)
8726 RESTORE_IT (it, &atpos_it, atpos_data);
8727 else if (atx_it.sp >= 0)
8728 RESTORE_IT (it, &atx_it, atx_data);
8729
8730 done:
8731
8732 if (atpos_data)
8733 bidi_unshelve_cache (atpos_data, 1);
8734 if (atx_data)
8735 bidi_unshelve_cache (atx_data, 1);
8736 if (wrap_data)
8737 bidi_unshelve_cache (wrap_data, 1);
8738 if (ppos_data)
8739 bidi_unshelve_cache (ppos_data, 1);
8740
8741 /* Restore the iterator settings altered at the beginning of this
8742 function. */
8743 it->glyph_row = saved_glyph_row;
8744 return result;
8745 }
8746
8747 /* For external use. */
8748 void
8749 move_it_in_display_line (struct it *it,
8750 ptrdiff_t to_charpos, int to_x,
8751 enum move_operation_enum op)
8752 {
8753 if (it->line_wrap == WORD_WRAP
8754 && (op & MOVE_TO_X))
8755 {
8756 struct it save_it;
8757 void *save_data = NULL;
8758 int skip;
8759
8760 SAVE_IT (save_it, *it, save_data);
8761 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8762 /* When word-wrap is on, TO_X may lie past the end
8763 of a wrapped line. Then it->current is the
8764 character on the next line, so backtrack to the
8765 space before the wrap point. */
8766 if (skip == MOVE_LINE_CONTINUED)
8767 {
8768 int prev_x = max (it->current_x - 1, 0);
8769 RESTORE_IT (it, &save_it, save_data);
8770 move_it_in_display_line_to
8771 (it, -1, prev_x, MOVE_TO_X);
8772 }
8773 else
8774 bidi_unshelve_cache (save_data, 1);
8775 }
8776 else
8777 move_it_in_display_line_to (it, to_charpos, to_x, op);
8778 }
8779
8780
8781 /* Move IT forward until it satisfies one or more of the criteria in
8782 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8783
8784 OP is a bit-mask that specifies where to stop, and in particular,
8785 which of those four position arguments makes a difference. See the
8786 description of enum move_operation_enum.
8787
8788 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8789 screen line, this function will set IT to the next position that is
8790 displayed to the right of TO_CHARPOS on the screen. */
8791
8792 void
8793 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8794 {
8795 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8796 int line_height, line_start_x = 0, reached = 0;
8797 void *backup_data = NULL;
8798
8799 for (;;)
8800 {
8801 if (op & MOVE_TO_VPOS)
8802 {
8803 /* If no TO_CHARPOS and no TO_X specified, stop at the
8804 start of the line TO_VPOS. */
8805 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8806 {
8807 if (it->vpos == to_vpos)
8808 {
8809 reached = 1;
8810 break;
8811 }
8812 else
8813 skip = move_it_in_display_line_to (it, -1, -1, 0);
8814 }
8815 else
8816 {
8817 /* TO_VPOS >= 0 means stop at TO_X in the line at
8818 TO_VPOS, or at TO_POS, whichever comes first. */
8819 if (it->vpos == to_vpos)
8820 {
8821 reached = 2;
8822 break;
8823 }
8824
8825 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8826
8827 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8828 {
8829 reached = 3;
8830 break;
8831 }
8832 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8833 {
8834 /* We have reached TO_X but not in the line we want. */
8835 skip = move_it_in_display_line_to (it, to_charpos,
8836 -1, MOVE_TO_POS);
8837 if (skip == MOVE_POS_MATCH_OR_ZV)
8838 {
8839 reached = 4;
8840 break;
8841 }
8842 }
8843 }
8844 }
8845 else if (op & MOVE_TO_Y)
8846 {
8847 struct it it_backup;
8848
8849 if (it->line_wrap == WORD_WRAP)
8850 SAVE_IT (it_backup, *it, backup_data);
8851
8852 /* TO_Y specified means stop at TO_X in the line containing
8853 TO_Y---or at TO_CHARPOS if this is reached first. The
8854 problem is that we can't really tell whether the line
8855 contains TO_Y before we have completely scanned it, and
8856 this may skip past TO_X. What we do is to first scan to
8857 TO_X.
8858
8859 If TO_X is not specified, use a TO_X of zero. The reason
8860 is to make the outcome of this function more predictable.
8861 If we didn't use TO_X == 0, we would stop at the end of
8862 the line which is probably not what a caller would expect
8863 to happen. */
8864 skip = move_it_in_display_line_to
8865 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8866 (MOVE_TO_X | (op & MOVE_TO_POS)));
8867
8868 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8869 if (skip == MOVE_POS_MATCH_OR_ZV)
8870 reached = 5;
8871 else if (skip == MOVE_X_REACHED)
8872 {
8873 /* If TO_X was reached, we want to know whether TO_Y is
8874 in the line. We know this is the case if the already
8875 scanned glyphs make the line tall enough. Otherwise,
8876 we must check by scanning the rest of the line. */
8877 line_height = it->max_ascent + it->max_descent;
8878 if (to_y >= it->current_y
8879 && to_y < it->current_y + line_height)
8880 {
8881 reached = 6;
8882 break;
8883 }
8884 SAVE_IT (it_backup, *it, backup_data);
8885 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8886 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8887 op & MOVE_TO_POS);
8888 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8889 line_height = it->max_ascent + it->max_descent;
8890 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8891
8892 if (to_y >= it->current_y
8893 && to_y < it->current_y + line_height)
8894 {
8895 /* If TO_Y is in this line and TO_X was reached
8896 above, we scanned too far. We have to restore
8897 IT's settings to the ones before skipping. But
8898 keep the more accurate values of max_ascent and
8899 max_descent we've found while skipping the rest
8900 of the line, for the sake of callers, such as
8901 pos_visible_p, that need to know the line
8902 height. */
8903 int max_ascent = it->max_ascent;
8904 int max_descent = it->max_descent;
8905
8906 RESTORE_IT (it, &it_backup, backup_data);
8907 it->max_ascent = max_ascent;
8908 it->max_descent = max_descent;
8909 reached = 6;
8910 }
8911 else
8912 {
8913 skip = skip2;
8914 if (skip == MOVE_POS_MATCH_OR_ZV)
8915 reached = 7;
8916 }
8917 }
8918 else
8919 {
8920 /* Check whether TO_Y is in this line. */
8921 line_height = it->max_ascent + it->max_descent;
8922 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8923
8924 if (to_y >= it->current_y
8925 && to_y < it->current_y + line_height)
8926 {
8927 /* When word-wrap is on, TO_X may lie past the end
8928 of a wrapped line. Then it->current is the
8929 character on the next line, so backtrack to the
8930 space before the wrap point. */
8931 if (skip == MOVE_LINE_CONTINUED
8932 && it->line_wrap == WORD_WRAP)
8933 {
8934 int prev_x = max (it->current_x - 1, 0);
8935 RESTORE_IT (it, &it_backup, backup_data);
8936 skip = move_it_in_display_line_to
8937 (it, -1, prev_x, MOVE_TO_X);
8938 }
8939 reached = 6;
8940 }
8941 }
8942
8943 if (reached)
8944 break;
8945 }
8946 else if (BUFFERP (it->object)
8947 && (it->method == GET_FROM_BUFFER
8948 || it->method == GET_FROM_STRETCH)
8949 && IT_CHARPOS (*it) >= to_charpos
8950 /* Under bidi iteration, a call to set_iterator_to_next
8951 can scan far beyond to_charpos if the initial
8952 portion of the next line needs to be reordered. In
8953 that case, give move_it_in_display_line_to another
8954 chance below. */
8955 && !(it->bidi_p
8956 && it->bidi_it.scan_dir == -1))
8957 skip = MOVE_POS_MATCH_OR_ZV;
8958 else
8959 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8960
8961 switch (skip)
8962 {
8963 case MOVE_POS_MATCH_OR_ZV:
8964 reached = 8;
8965 goto out;
8966
8967 case MOVE_NEWLINE_OR_CR:
8968 set_iterator_to_next (it, 1);
8969 it->continuation_lines_width = 0;
8970 break;
8971
8972 case MOVE_LINE_TRUNCATED:
8973 it->continuation_lines_width = 0;
8974 reseat_at_next_visible_line_start (it, 0);
8975 if ((op & MOVE_TO_POS) != 0
8976 && IT_CHARPOS (*it) > to_charpos)
8977 {
8978 reached = 9;
8979 goto out;
8980 }
8981 break;
8982
8983 case MOVE_LINE_CONTINUED:
8984 /* For continued lines ending in a tab, some of the glyphs
8985 associated with the tab are displayed on the current
8986 line. Since it->current_x does not include these glyphs,
8987 we use it->last_visible_x instead. */
8988 if (it->c == '\t')
8989 {
8990 it->continuation_lines_width += it->last_visible_x;
8991 /* When moving by vpos, ensure that the iterator really
8992 advances to the next line (bug#847, bug#969). Fixme:
8993 do we need to do this in other circumstances? */
8994 if (it->current_x != it->last_visible_x
8995 && (op & MOVE_TO_VPOS)
8996 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8997 {
8998 line_start_x = it->current_x + it->pixel_width
8999 - it->last_visible_x;
9000 set_iterator_to_next (it, 0);
9001 }
9002 }
9003 else
9004 it->continuation_lines_width += it->current_x;
9005 break;
9006
9007 default:
9008 emacs_abort ();
9009 }
9010
9011 /* Reset/increment for the next run. */
9012 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9013 it->current_x = line_start_x;
9014 line_start_x = 0;
9015 it->hpos = 0;
9016 it->current_y += it->max_ascent + it->max_descent;
9017 ++it->vpos;
9018 last_height = it->max_ascent + it->max_descent;
9019 it->max_ascent = it->max_descent = 0;
9020 }
9021
9022 out:
9023
9024 /* On text terminals, we may stop at the end of a line in the middle
9025 of a multi-character glyph. If the glyph itself is continued,
9026 i.e. it is actually displayed on the next line, don't treat this
9027 stopping point as valid; move to the next line instead (unless
9028 that brings us offscreen). */
9029 if (!FRAME_WINDOW_P (it->f)
9030 && op & MOVE_TO_POS
9031 && IT_CHARPOS (*it) == to_charpos
9032 && it->what == IT_CHARACTER
9033 && it->nglyphs > 1
9034 && it->line_wrap == WINDOW_WRAP
9035 && it->current_x == it->last_visible_x - 1
9036 && it->c != '\n'
9037 && it->c != '\t'
9038 && it->vpos < it->w->window_end_vpos)
9039 {
9040 it->continuation_lines_width += it->current_x;
9041 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9042 it->current_y += it->max_ascent + it->max_descent;
9043 ++it->vpos;
9044 last_height = it->max_ascent + it->max_descent;
9045 }
9046
9047 if (backup_data)
9048 bidi_unshelve_cache (backup_data, 1);
9049
9050 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9051 }
9052
9053
9054 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9055
9056 If DY > 0, move IT backward at least that many pixels. DY = 0
9057 means move IT backward to the preceding line start or BEGV. This
9058 function may move over more than DY pixels if IT->current_y - DY
9059 ends up in the middle of a line; in this case IT->current_y will be
9060 set to the top of the line moved to. */
9061
9062 void
9063 move_it_vertically_backward (struct it *it, int dy)
9064 {
9065 int nlines, h;
9066 struct it it2, it3;
9067 void *it2data = NULL, *it3data = NULL;
9068 ptrdiff_t start_pos;
9069 int nchars_per_row
9070 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9071 ptrdiff_t pos_limit;
9072
9073 move_further_back:
9074 eassert (dy >= 0);
9075
9076 start_pos = IT_CHARPOS (*it);
9077
9078 /* Estimate how many newlines we must move back. */
9079 nlines = max (1, dy / default_line_pixel_height (it->w));
9080 if (it->line_wrap == TRUNCATE)
9081 pos_limit = BEGV;
9082 else
9083 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9084
9085 /* Set the iterator's position that many lines back. But don't go
9086 back more than NLINES full screen lines -- this wins a day with
9087 buffers which have very long lines. */
9088 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9089 back_to_previous_visible_line_start (it);
9090
9091 /* Reseat the iterator here. When moving backward, we don't want
9092 reseat to skip forward over invisible text, set up the iterator
9093 to deliver from overlay strings at the new position etc. So,
9094 use reseat_1 here. */
9095 reseat_1 (it, it->current.pos, 1);
9096
9097 /* We are now surely at a line start. */
9098 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9099 reordering is in effect. */
9100 it->continuation_lines_width = 0;
9101
9102 /* Move forward and see what y-distance we moved. First move to the
9103 start of the next line so that we get its height. We need this
9104 height to be able to tell whether we reached the specified
9105 y-distance. */
9106 SAVE_IT (it2, *it, it2data);
9107 it2.max_ascent = it2.max_descent = 0;
9108 do
9109 {
9110 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9111 MOVE_TO_POS | MOVE_TO_VPOS);
9112 }
9113 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9114 /* If we are in a display string which starts at START_POS,
9115 and that display string includes a newline, and we are
9116 right after that newline (i.e. at the beginning of a
9117 display line), exit the loop, because otherwise we will
9118 infloop, since move_it_to will see that it is already at
9119 START_POS and will not move. */
9120 || (it2.method == GET_FROM_STRING
9121 && IT_CHARPOS (it2) == start_pos
9122 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9123 eassert (IT_CHARPOS (*it) >= BEGV);
9124 SAVE_IT (it3, it2, it3data);
9125
9126 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9127 eassert (IT_CHARPOS (*it) >= BEGV);
9128 /* H is the actual vertical distance from the position in *IT
9129 and the starting position. */
9130 h = it2.current_y - it->current_y;
9131 /* NLINES is the distance in number of lines. */
9132 nlines = it2.vpos - it->vpos;
9133
9134 /* Correct IT's y and vpos position
9135 so that they are relative to the starting point. */
9136 it->vpos -= nlines;
9137 it->current_y -= h;
9138
9139 if (dy == 0)
9140 {
9141 /* DY == 0 means move to the start of the screen line. The
9142 value of nlines is > 0 if continuation lines were involved,
9143 or if the original IT position was at start of a line. */
9144 RESTORE_IT (it, it, it2data);
9145 if (nlines > 0)
9146 move_it_by_lines (it, nlines);
9147 /* The above code moves us to some position NLINES down,
9148 usually to its first glyph (leftmost in an L2R line), but
9149 that's not necessarily the start of the line, under bidi
9150 reordering. We want to get to the character position
9151 that is immediately after the newline of the previous
9152 line. */
9153 if (it->bidi_p
9154 && !it->continuation_lines_width
9155 && !STRINGP (it->string)
9156 && IT_CHARPOS (*it) > BEGV
9157 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9158 {
9159 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9160
9161 DEC_BOTH (cp, bp);
9162 cp = find_newline_no_quit (cp, bp, -1, NULL);
9163 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9164 }
9165 bidi_unshelve_cache (it3data, 1);
9166 }
9167 else
9168 {
9169 /* The y-position we try to reach, relative to *IT.
9170 Note that H has been subtracted in front of the if-statement. */
9171 int target_y = it->current_y + h - dy;
9172 int y0 = it3.current_y;
9173 int y1;
9174 int line_height;
9175
9176 RESTORE_IT (&it3, &it3, it3data);
9177 y1 = line_bottom_y (&it3);
9178 line_height = y1 - y0;
9179 RESTORE_IT (it, it, it2data);
9180 /* If we did not reach target_y, try to move further backward if
9181 we can. If we moved too far backward, try to move forward. */
9182 if (target_y < it->current_y
9183 /* This is heuristic. In a window that's 3 lines high, with
9184 a line height of 13 pixels each, recentering with point
9185 on the bottom line will try to move -39/2 = 19 pixels
9186 backward. Try to avoid moving into the first line. */
9187 && (it->current_y - target_y
9188 > min (window_box_height (it->w), line_height * 2 / 3))
9189 && IT_CHARPOS (*it) > BEGV)
9190 {
9191 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9192 target_y - it->current_y));
9193 dy = it->current_y - target_y;
9194 goto move_further_back;
9195 }
9196 else if (target_y >= it->current_y + line_height
9197 && IT_CHARPOS (*it) < ZV)
9198 {
9199 /* Should move forward by at least one line, maybe more.
9200
9201 Note: Calling move_it_by_lines can be expensive on
9202 terminal frames, where compute_motion is used (via
9203 vmotion) to do the job, when there are very long lines
9204 and truncate-lines is nil. That's the reason for
9205 treating terminal frames specially here. */
9206
9207 if (!FRAME_WINDOW_P (it->f))
9208 move_it_vertically (it, target_y - (it->current_y + line_height));
9209 else
9210 {
9211 do
9212 {
9213 move_it_by_lines (it, 1);
9214 }
9215 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9216 }
9217 }
9218 }
9219 }
9220
9221
9222 /* Move IT by a specified amount of pixel lines DY. DY negative means
9223 move backwards. DY = 0 means move to start of screen line. At the
9224 end, IT will be on the start of a screen line. */
9225
9226 void
9227 move_it_vertically (struct it *it, int dy)
9228 {
9229 if (dy <= 0)
9230 move_it_vertically_backward (it, -dy);
9231 else
9232 {
9233 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9234 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9235 MOVE_TO_POS | MOVE_TO_Y);
9236 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9237
9238 /* If buffer ends in ZV without a newline, move to the start of
9239 the line to satisfy the post-condition. */
9240 if (IT_CHARPOS (*it) == ZV
9241 && ZV > BEGV
9242 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9243 move_it_by_lines (it, 0);
9244 }
9245 }
9246
9247
9248 /* Move iterator IT past the end of the text line it is in. */
9249
9250 void
9251 move_it_past_eol (struct it *it)
9252 {
9253 enum move_it_result rc;
9254
9255 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9256 if (rc == MOVE_NEWLINE_OR_CR)
9257 set_iterator_to_next (it, 0);
9258 }
9259
9260
9261 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9262 negative means move up. DVPOS == 0 means move to the start of the
9263 screen line.
9264
9265 Optimization idea: If we would know that IT->f doesn't use
9266 a face with proportional font, we could be faster for
9267 truncate-lines nil. */
9268
9269 void
9270 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9271 {
9272
9273 /* The commented-out optimization uses vmotion on terminals. This
9274 gives bad results, because elements like it->what, on which
9275 callers such as pos_visible_p rely, aren't updated. */
9276 /* struct position pos;
9277 if (!FRAME_WINDOW_P (it->f))
9278 {
9279 struct text_pos textpos;
9280
9281 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9282 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9283 reseat (it, textpos, 1);
9284 it->vpos += pos.vpos;
9285 it->current_y += pos.vpos;
9286 }
9287 else */
9288
9289 if (dvpos == 0)
9290 {
9291 /* DVPOS == 0 means move to the start of the screen line. */
9292 move_it_vertically_backward (it, 0);
9293 /* Let next call to line_bottom_y calculate real line height. */
9294 last_height = 0;
9295 }
9296 else if (dvpos > 0)
9297 {
9298 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9299 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9300 {
9301 /* Only move to the next buffer position if we ended up in a
9302 string from display property, not in an overlay string
9303 (before-string or after-string). That is because the
9304 latter don't conceal the underlying buffer position, so
9305 we can ask to move the iterator to the exact position we
9306 are interested in. Note that, even if we are already at
9307 IT_CHARPOS (*it), the call below is not a no-op, as it
9308 will detect that we are at the end of the string, pop the
9309 iterator, and compute it->current_x and it->hpos
9310 correctly. */
9311 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9312 -1, -1, -1, MOVE_TO_POS);
9313 }
9314 }
9315 else
9316 {
9317 struct it it2;
9318 void *it2data = NULL;
9319 ptrdiff_t start_charpos, i;
9320 int nchars_per_row
9321 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9322 ptrdiff_t pos_limit;
9323
9324 /* Start at the beginning of the screen line containing IT's
9325 position. This may actually move vertically backwards,
9326 in case of overlays, so adjust dvpos accordingly. */
9327 dvpos += it->vpos;
9328 move_it_vertically_backward (it, 0);
9329 dvpos -= it->vpos;
9330
9331 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9332 screen lines, and reseat the iterator there. */
9333 start_charpos = IT_CHARPOS (*it);
9334 if (it->line_wrap == TRUNCATE)
9335 pos_limit = BEGV;
9336 else
9337 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9338 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9339 back_to_previous_visible_line_start (it);
9340 reseat (it, it->current.pos, 1);
9341
9342 /* Move further back if we end up in a string or an image. */
9343 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9344 {
9345 /* First try to move to start of display line. */
9346 dvpos += it->vpos;
9347 move_it_vertically_backward (it, 0);
9348 dvpos -= it->vpos;
9349 if (IT_POS_VALID_AFTER_MOVE_P (it))
9350 break;
9351 /* If start of line is still in string or image,
9352 move further back. */
9353 back_to_previous_visible_line_start (it);
9354 reseat (it, it->current.pos, 1);
9355 dvpos--;
9356 }
9357
9358 it->current_x = it->hpos = 0;
9359
9360 /* Above call may have moved too far if continuation lines
9361 are involved. Scan forward and see if it did. */
9362 SAVE_IT (it2, *it, it2data);
9363 it2.vpos = it2.current_y = 0;
9364 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9365 it->vpos -= it2.vpos;
9366 it->current_y -= it2.current_y;
9367 it->current_x = it->hpos = 0;
9368
9369 /* If we moved too far back, move IT some lines forward. */
9370 if (it2.vpos > -dvpos)
9371 {
9372 int delta = it2.vpos + dvpos;
9373
9374 RESTORE_IT (&it2, &it2, it2data);
9375 SAVE_IT (it2, *it, it2data);
9376 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9377 /* Move back again if we got too far ahead. */
9378 if (IT_CHARPOS (*it) >= start_charpos)
9379 RESTORE_IT (it, &it2, it2data);
9380 else
9381 bidi_unshelve_cache (it2data, 1);
9382 }
9383 else
9384 RESTORE_IT (it, it, it2data);
9385 }
9386 }
9387
9388 /* Return 1 if IT points into the middle of a display vector. */
9389
9390 int
9391 in_display_vector_p (struct it *it)
9392 {
9393 return (it->method == GET_FROM_DISPLAY_VECTOR
9394 && it->current.dpvec_index > 0
9395 && it->dpvec + it->current.dpvec_index != it->dpend);
9396 }
9397
9398 \f
9399 /***********************************************************************
9400 Messages
9401 ***********************************************************************/
9402
9403
9404 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9405 to *Messages*. */
9406
9407 void
9408 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9409 {
9410 Lisp_Object args[3];
9411 Lisp_Object msg, fmt;
9412 char *buffer;
9413 ptrdiff_t len;
9414 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9415 USE_SAFE_ALLOCA;
9416
9417 fmt = msg = Qnil;
9418 GCPRO4 (fmt, msg, arg1, arg2);
9419
9420 args[0] = fmt = build_string (format);
9421 args[1] = arg1;
9422 args[2] = arg2;
9423 msg = Fformat (3, args);
9424
9425 len = SBYTES (msg) + 1;
9426 buffer = SAFE_ALLOCA (len);
9427 memcpy (buffer, SDATA (msg), len);
9428
9429 message_dolog (buffer, len - 1, 1, 0);
9430 SAFE_FREE ();
9431
9432 UNGCPRO;
9433 }
9434
9435
9436 /* Output a newline in the *Messages* buffer if "needs" one. */
9437
9438 void
9439 message_log_maybe_newline (void)
9440 {
9441 if (message_log_need_newline)
9442 message_dolog ("", 0, 1, 0);
9443 }
9444
9445
9446 /* Add a string M of length NBYTES to the message log, optionally
9447 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9448 true, means interpret the contents of M as multibyte. This
9449 function calls low-level routines in order to bypass text property
9450 hooks, etc. which might not be safe to run.
9451
9452 This may GC (insert may run before/after change hooks),
9453 so the buffer M must NOT point to a Lisp string. */
9454
9455 void
9456 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9457 {
9458 const unsigned char *msg = (const unsigned char *) m;
9459
9460 if (!NILP (Vmemory_full))
9461 return;
9462
9463 if (!NILP (Vmessage_log_max))
9464 {
9465 struct buffer *oldbuf;
9466 Lisp_Object oldpoint, oldbegv, oldzv;
9467 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9468 ptrdiff_t point_at_end = 0;
9469 ptrdiff_t zv_at_end = 0;
9470 Lisp_Object old_deactivate_mark;
9471 bool shown;
9472 struct gcpro gcpro1;
9473
9474 old_deactivate_mark = Vdeactivate_mark;
9475 oldbuf = current_buffer;
9476
9477 /* Ensure the Messages buffer exists, and switch to it.
9478 If we created it, set the major-mode. */
9479 {
9480 int newbuffer = 0;
9481 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9482
9483 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9484
9485 if (newbuffer &&
9486 !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9487 call0 (intern ("messages-buffer-mode"));
9488 }
9489
9490 bset_undo_list (current_buffer, Qt);
9491
9492 oldpoint = message_dolog_marker1;
9493 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9494 oldbegv = message_dolog_marker2;
9495 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9496 oldzv = message_dolog_marker3;
9497 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9498 GCPRO1 (old_deactivate_mark);
9499
9500 if (PT == Z)
9501 point_at_end = 1;
9502 if (ZV == Z)
9503 zv_at_end = 1;
9504
9505 BEGV = BEG;
9506 BEGV_BYTE = BEG_BYTE;
9507 ZV = Z;
9508 ZV_BYTE = Z_BYTE;
9509 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9510
9511 /* Insert the string--maybe converting multibyte to single byte
9512 or vice versa, so that all the text fits the buffer. */
9513 if (multibyte
9514 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9515 {
9516 ptrdiff_t i;
9517 int c, char_bytes;
9518 char work[1];
9519
9520 /* Convert a multibyte string to single-byte
9521 for the *Message* buffer. */
9522 for (i = 0; i < nbytes; i += char_bytes)
9523 {
9524 c = string_char_and_length (msg + i, &char_bytes);
9525 work[0] = (ASCII_CHAR_P (c)
9526 ? c
9527 : multibyte_char_to_unibyte (c));
9528 insert_1_both (work, 1, 1, 1, 0, 0);
9529 }
9530 }
9531 else if (! multibyte
9532 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9533 {
9534 ptrdiff_t i;
9535 int c, char_bytes;
9536 unsigned char str[MAX_MULTIBYTE_LENGTH];
9537 /* Convert a single-byte string to multibyte
9538 for the *Message* buffer. */
9539 for (i = 0; i < nbytes; i++)
9540 {
9541 c = msg[i];
9542 MAKE_CHAR_MULTIBYTE (c);
9543 char_bytes = CHAR_STRING (c, str);
9544 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9545 }
9546 }
9547 else if (nbytes)
9548 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9549
9550 if (nlflag)
9551 {
9552 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9553 printmax_t dups;
9554
9555 insert_1_both ("\n", 1, 1, 1, 0, 0);
9556
9557 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9558 this_bol = PT;
9559 this_bol_byte = PT_BYTE;
9560
9561 /* See if this line duplicates the previous one.
9562 If so, combine duplicates. */
9563 if (this_bol > BEG)
9564 {
9565 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9566 prev_bol = PT;
9567 prev_bol_byte = PT_BYTE;
9568
9569 dups = message_log_check_duplicate (prev_bol_byte,
9570 this_bol_byte);
9571 if (dups)
9572 {
9573 del_range_both (prev_bol, prev_bol_byte,
9574 this_bol, this_bol_byte, 0);
9575 if (dups > 1)
9576 {
9577 char dupstr[sizeof " [ times]"
9578 + INT_STRLEN_BOUND (printmax_t)];
9579
9580 /* If you change this format, don't forget to also
9581 change message_log_check_duplicate. */
9582 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9583 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9584 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9585 }
9586 }
9587 }
9588
9589 /* If we have more than the desired maximum number of lines
9590 in the *Messages* buffer now, delete the oldest ones.
9591 This is safe because we don't have undo in this buffer. */
9592
9593 if (NATNUMP (Vmessage_log_max))
9594 {
9595 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9596 -XFASTINT (Vmessage_log_max) - 1, 0);
9597 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9598 }
9599 }
9600 BEGV = marker_position (oldbegv);
9601 BEGV_BYTE = marker_byte_position (oldbegv);
9602
9603 if (zv_at_end)
9604 {
9605 ZV = Z;
9606 ZV_BYTE = Z_BYTE;
9607 }
9608 else
9609 {
9610 ZV = marker_position (oldzv);
9611 ZV_BYTE = marker_byte_position (oldzv);
9612 }
9613
9614 if (point_at_end)
9615 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9616 else
9617 /* We can't do Fgoto_char (oldpoint) because it will run some
9618 Lisp code. */
9619 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9620 marker_byte_position (oldpoint));
9621
9622 UNGCPRO;
9623 unchain_marker (XMARKER (oldpoint));
9624 unchain_marker (XMARKER (oldbegv));
9625 unchain_marker (XMARKER (oldzv));
9626
9627 shown = buffer_window_count (current_buffer) > 0;
9628 set_buffer_internal (oldbuf);
9629 /* We called insert_1_both above with its 5th argument (PREPARE)
9630 zero, which prevents insert_1_both from calling
9631 prepare_to_modify_buffer, which in turns prevents us from
9632 incrementing windows_or_buffers_changed even if *Messages* is
9633 shown in some window. So we must manually incrementing
9634 windows_or_buffers_changed here to make up for that. */
9635 if (shown)
9636 windows_or_buffers_changed = 41;
9637 else
9638 windows_or_buffers_changed = old_windows_or_buffers_changed;
9639 message_log_need_newline = !nlflag;
9640 Vdeactivate_mark = old_deactivate_mark;
9641 }
9642 }
9643
9644
9645 /* We are at the end of the buffer after just having inserted a newline.
9646 (Note: We depend on the fact we won't be crossing the gap.)
9647 Check to see if the most recent message looks a lot like the previous one.
9648 Return 0 if different, 1 if the new one should just replace it, or a
9649 value N > 1 if we should also append " [N times]". */
9650
9651 static intmax_t
9652 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9653 {
9654 ptrdiff_t i;
9655 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9656 int seen_dots = 0;
9657 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9658 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9659
9660 for (i = 0; i < len; i++)
9661 {
9662 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9663 seen_dots = 1;
9664 if (p1[i] != p2[i])
9665 return seen_dots;
9666 }
9667 p1 += len;
9668 if (*p1 == '\n')
9669 return 2;
9670 if (*p1++ == ' ' && *p1++ == '[')
9671 {
9672 char *pend;
9673 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9674 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9675 return n + 1;
9676 }
9677 return 0;
9678 }
9679 \f
9680
9681 /* Display an echo area message M with a specified length of NBYTES
9682 bytes. The string may include null characters. If M is not a
9683 string, clear out any existing message, and let the mini-buffer
9684 text show through.
9685
9686 This function cancels echoing. */
9687
9688 void
9689 message3 (Lisp_Object m)
9690 {
9691 struct gcpro gcpro1;
9692
9693 GCPRO1 (m);
9694 clear_message (1,1);
9695 cancel_echoing ();
9696
9697 /* First flush out any partial line written with print. */
9698 message_log_maybe_newline ();
9699 if (STRINGP (m))
9700 {
9701 ptrdiff_t nbytes = SBYTES (m);
9702 bool multibyte = STRING_MULTIBYTE (m);
9703 USE_SAFE_ALLOCA;
9704 char *buffer = SAFE_ALLOCA (nbytes);
9705 memcpy (buffer, SDATA (m), nbytes);
9706 message_dolog (buffer, nbytes, 1, multibyte);
9707 SAFE_FREE ();
9708 }
9709 message3_nolog (m);
9710
9711 UNGCPRO;
9712 }
9713
9714
9715 /* The non-logging version of message3.
9716 This does not cancel echoing, because it is used for echoing.
9717 Perhaps we need to make a separate function for echoing
9718 and make this cancel echoing. */
9719
9720 void
9721 message3_nolog (Lisp_Object m)
9722 {
9723 struct frame *sf = SELECTED_FRAME ();
9724
9725 if (FRAME_INITIAL_P (sf))
9726 {
9727 if (noninteractive_need_newline)
9728 putc ('\n', stderr);
9729 noninteractive_need_newline = 0;
9730 if (STRINGP (m))
9731 {
9732 Lisp_Object s = ENCODE_SYSTEM (m);
9733
9734 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9735 }
9736 if (cursor_in_echo_area == 0)
9737 fprintf (stderr, "\n");
9738 fflush (stderr);
9739 }
9740 /* Error messages get reported properly by cmd_error, so this must be just an
9741 informative message; if the frame hasn't really been initialized yet, just
9742 toss it. */
9743 else if (INTERACTIVE && sf->glyphs_initialized_p)
9744 {
9745 /* Get the frame containing the mini-buffer
9746 that the selected frame is using. */
9747 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9748 Lisp_Object frame = XWINDOW (mini_window)->frame;
9749 struct frame *f = XFRAME (frame);
9750
9751 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9752 Fmake_frame_visible (frame);
9753
9754 if (STRINGP (m) && SCHARS (m) > 0)
9755 {
9756 set_message (m);
9757 if (minibuffer_auto_raise)
9758 Fraise_frame (frame);
9759 /* Assume we are not echoing.
9760 (If we are, echo_now will override this.) */
9761 echo_message_buffer = Qnil;
9762 }
9763 else
9764 clear_message (1, 1);
9765
9766 do_pending_window_change (0);
9767 echo_area_display (1);
9768 do_pending_window_change (0);
9769 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9770 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9771 }
9772 }
9773
9774
9775 /* Display a null-terminated echo area message M. If M is 0, clear
9776 out any existing message, and let the mini-buffer text show through.
9777
9778 The buffer M must continue to exist until after the echo area gets
9779 cleared or some other message gets displayed there. Do not pass
9780 text that is stored in a Lisp string. Do not pass text in a buffer
9781 that was alloca'd. */
9782
9783 void
9784 message1 (const char *m)
9785 {
9786 message3 (m ? build_unibyte_string (m) : Qnil);
9787 }
9788
9789
9790 /* The non-logging counterpart of message1. */
9791
9792 void
9793 message1_nolog (const char *m)
9794 {
9795 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9796 }
9797
9798 /* Display a message M which contains a single %s
9799 which gets replaced with STRING. */
9800
9801 void
9802 message_with_string (const char *m, Lisp_Object string, int log)
9803 {
9804 CHECK_STRING (string);
9805
9806 if (noninteractive)
9807 {
9808 if (m)
9809 {
9810 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
9811 String whose data pointer might be passed to us in M. So
9812 we use a local copy. */
9813 char *fmt = xstrdup (m);
9814
9815 if (noninteractive_need_newline)
9816 putc ('\n', stderr);
9817 noninteractive_need_newline = 0;
9818 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
9819 if (!cursor_in_echo_area)
9820 fprintf (stderr, "\n");
9821 fflush (stderr);
9822 xfree (fmt);
9823 }
9824 }
9825 else if (INTERACTIVE)
9826 {
9827 /* The frame whose minibuffer we're going to display the message on.
9828 It may be larger than the selected frame, so we need
9829 to use its buffer, not the selected frame's buffer. */
9830 Lisp_Object mini_window;
9831 struct frame *f, *sf = SELECTED_FRAME ();
9832
9833 /* Get the frame containing the minibuffer
9834 that the selected frame is using. */
9835 mini_window = FRAME_MINIBUF_WINDOW (sf);
9836 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9837
9838 /* Error messages get reported properly by cmd_error, so this must be
9839 just an informative message; if the frame hasn't really been
9840 initialized yet, just toss it. */
9841 if (f->glyphs_initialized_p)
9842 {
9843 Lisp_Object args[2], msg;
9844 struct gcpro gcpro1, gcpro2;
9845
9846 args[0] = build_string (m);
9847 args[1] = msg = string;
9848 GCPRO2 (args[0], msg);
9849 gcpro1.nvars = 2;
9850
9851 msg = Fformat (2, args);
9852
9853 if (log)
9854 message3 (msg);
9855 else
9856 message3_nolog (msg);
9857
9858 UNGCPRO;
9859
9860 /* Print should start at the beginning of the message
9861 buffer next time. */
9862 message_buf_print = 0;
9863 }
9864 }
9865 }
9866
9867
9868 /* Dump an informative message to the minibuf. If M is 0, clear out
9869 any existing message, and let the mini-buffer text show through. */
9870
9871 static void
9872 vmessage (const char *m, va_list ap)
9873 {
9874 if (noninteractive)
9875 {
9876 if (m)
9877 {
9878 if (noninteractive_need_newline)
9879 putc ('\n', stderr);
9880 noninteractive_need_newline = 0;
9881 vfprintf (stderr, m, ap);
9882 if (cursor_in_echo_area == 0)
9883 fprintf (stderr, "\n");
9884 fflush (stderr);
9885 }
9886 }
9887 else if (INTERACTIVE)
9888 {
9889 /* The frame whose mini-buffer we're going to display the message
9890 on. It may be larger than the selected frame, so we need to
9891 use its buffer, not the selected frame's buffer. */
9892 Lisp_Object mini_window;
9893 struct frame *f, *sf = SELECTED_FRAME ();
9894
9895 /* Get the frame containing the mini-buffer
9896 that the selected frame is using. */
9897 mini_window = FRAME_MINIBUF_WINDOW (sf);
9898 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9899
9900 /* Error messages get reported properly by cmd_error, so this must be
9901 just an informative message; if the frame hasn't really been
9902 initialized yet, just toss it. */
9903 if (f->glyphs_initialized_p)
9904 {
9905 if (m)
9906 {
9907 ptrdiff_t len;
9908 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9909 char *message_buf = alloca (maxsize + 1);
9910
9911 len = doprnt (message_buf, maxsize, m, 0, ap);
9912
9913 message3 (make_string (message_buf, len));
9914 }
9915 else
9916 message1 (0);
9917
9918 /* Print should start at the beginning of the message
9919 buffer next time. */
9920 message_buf_print = 0;
9921 }
9922 }
9923 }
9924
9925 void
9926 message (const char *m, ...)
9927 {
9928 va_list ap;
9929 va_start (ap, m);
9930 vmessage (m, ap);
9931 va_end (ap);
9932 }
9933
9934
9935 #if 0
9936 /* The non-logging version of message. */
9937
9938 void
9939 message_nolog (const char *m, ...)
9940 {
9941 Lisp_Object old_log_max;
9942 va_list ap;
9943 va_start (ap, m);
9944 old_log_max = Vmessage_log_max;
9945 Vmessage_log_max = Qnil;
9946 vmessage (m, ap);
9947 Vmessage_log_max = old_log_max;
9948 va_end (ap);
9949 }
9950 #endif
9951
9952
9953 /* Display the current message in the current mini-buffer. This is
9954 only called from error handlers in process.c, and is not time
9955 critical. */
9956
9957 void
9958 update_echo_area (void)
9959 {
9960 if (!NILP (echo_area_buffer[0]))
9961 {
9962 Lisp_Object string;
9963 string = Fcurrent_message ();
9964 message3 (string);
9965 }
9966 }
9967
9968
9969 /* Make sure echo area buffers in `echo_buffers' are live.
9970 If they aren't, make new ones. */
9971
9972 static void
9973 ensure_echo_area_buffers (void)
9974 {
9975 int i;
9976
9977 for (i = 0; i < 2; ++i)
9978 if (!BUFFERP (echo_buffer[i])
9979 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9980 {
9981 char name[30];
9982 Lisp_Object old_buffer;
9983 int j;
9984
9985 old_buffer = echo_buffer[i];
9986 echo_buffer[i] = Fget_buffer_create
9987 (make_formatted_string (name, " *Echo Area %d*", i));
9988 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9989 /* to force word wrap in echo area -
9990 it was decided to postpone this*/
9991 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9992
9993 for (j = 0; j < 2; ++j)
9994 if (EQ (old_buffer, echo_area_buffer[j]))
9995 echo_area_buffer[j] = echo_buffer[i];
9996 }
9997 }
9998
9999
10000 /* Call FN with args A1..A2 with either the current or last displayed
10001 echo_area_buffer as current buffer.
10002
10003 WHICH zero means use the current message buffer
10004 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10005 from echo_buffer[] and clear it.
10006
10007 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10008 suitable buffer from echo_buffer[] and clear it.
10009
10010 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10011 that the current message becomes the last displayed one, make
10012 choose a suitable buffer for echo_area_buffer[0], and clear it.
10013
10014 Value is what FN returns. */
10015
10016 static int
10017 with_echo_area_buffer (struct window *w, int which,
10018 int (*fn) (ptrdiff_t, Lisp_Object),
10019 ptrdiff_t a1, Lisp_Object a2)
10020 {
10021 Lisp_Object buffer;
10022 int this_one, the_other, clear_buffer_p, rc;
10023 ptrdiff_t count = SPECPDL_INDEX ();
10024
10025 /* If buffers aren't live, make new ones. */
10026 ensure_echo_area_buffers ();
10027
10028 clear_buffer_p = 0;
10029
10030 if (which == 0)
10031 this_one = 0, the_other = 1;
10032 else if (which > 0)
10033 this_one = 1, the_other = 0;
10034 else
10035 {
10036 this_one = 0, the_other = 1;
10037 clear_buffer_p = 1;
10038
10039 /* We need a fresh one in case the current echo buffer equals
10040 the one containing the last displayed echo area message. */
10041 if (!NILP (echo_area_buffer[this_one])
10042 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10043 echo_area_buffer[this_one] = Qnil;
10044 }
10045
10046 /* Choose a suitable buffer from echo_buffer[] is we don't
10047 have one. */
10048 if (NILP (echo_area_buffer[this_one]))
10049 {
10050 echo_area_buffer[this_one]
10051 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10052 ? echo_buffer[the_other]
10053 : echo_buffer[this_one]);
10054 clear_buffer_p = 1;
10055 }
10056
10057 buffer = echo_area_buffer[this_one];
10058
10059 /* Don't get confused by reusing the buffer used for echoing
10060 for a different purpose. */
10061 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10062 cancel_echoing ();
10063
10064 record_unwind_protect (unwind_with_echo_area_buffer,
10065 with_echo_area_buffer_unwind_data (w));
10066
10067 /* Make the echo area buffer current. Note that for display
10068 purposes, it is not necessary that the displayed window's buffer
10069 == current_buffer, except for text property lookup. So, let's
10070 only set that buffer temporarily here without doing a full
10071 Fset_window_buffer. We must also change w->pointm, though,
10072 because otherwise an assertions in unshow_buffer fails, and Emacs
10073 aborts. */
10074 set_buffer_internal_1 (XBUFFER (buffer));
10075 if (w)
10076 {
10077 wset_buffer (w, buffer);
10078 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10079 }
10080
10081 bset_undo_list (current_buffer, Qt);
10082 bset_read_only (current_buffer, Qnil);
10083 specbind (Qinhibit_read_only, Qt);
10084 specbind (Qinhibit_modification_hooks, Qt);
10085
10086 if (clear_buffer_p && Z > BEG)
10087 del_range (BEG, Z);
10088
10089 eassert (BEGV >= BEG);
10090 eassert (ZV <= Z && ZV >= BEGV);
10091
10092 rc = fn (a1, a2);
10093
10094 eassert (BEGV >= BEG);
10095 eassert (ZV <= Z && ZV >= BEGV);
10096
10097 unbind_to (count, Qnil);
10098 return rc;
10099 }
10100
10101
10102 /* Save state that should be preserved around the call to the function
10103 FN called in with_echo_area_buffer. */
10104
10105 static Lisp_Object
10106 with_echo_area_buffer_unwind_data (struct window *w)
10107 {
10108 int i = 0;
10109 Lisp_Object vector, tmp;
10110
10111 /* Reduce consing by keeping one vector in
10112 Vwith_echo_area_save_vector. */
10113 vector = Vwith_echo_area_save_vector;
10114 Vwith_echo_area_save_vector = Qnil;
10115
10116 if (NILP (vector))
10117 vector = Fmake_vector (make_number (9), Qnil);
10118
10119 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10120 ASET (vector, i, Vdeactivate_mark); ++i;
10121 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10122
10123 if (w)
10124 {
10125 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10126 ASET (vector, i, w->contents); ++i;
10127 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10128 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10129 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10130 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10131 }
10132 else
10133 {
10134 int end = i + 6;
10135 for (; i < end; ++i)
10136 ASET (vector, i, Qnil);
10137 }
10138
10139 eassert (i == ASIZE (vector));
10140 return vector;
10141 }
10142
10143
10144 /* Restore global state from VECTOR which was created by
10145 with_echo_area_buffer_unwind_data. */
10146
10147 static void
10148 unwind_with_echo_area_buffer (Lisp_Object vector)
10149 {
10150 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10151 Vdeactivate_mark = AREF (vector, 1);
10152 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10153
10154 if (WINDOWP (AREF (vector, 3)))
10155 {
10156 struct window *w;
10157 Lisp_Object buffer;
10158
10159 w = XWINDOW (AREF (vector, 3));
10160 buffer = AREF (vector, 4);
10161
10162 wset_buffer (w, buffer);
10163 set_marker_both (w->pointm, buffer,
10164 XFASTINT (AREF (vector, 5)),
10165 XFASTINT (AREF (vector, 6)));
10166 set_marker_both (w->start, buffer,
10167 XFASTINT (AREF (vector, 7)),
10168 XFASTINT (AREF (vector, 8)));
10169 }
10170
10171 Vwith_echo_area_save_vector = vector;
10172 }
10173
10174
10175 /* Set up the echo area for use by print functions. MULTIBYTE_P
10176 non-zero means we will print multibyte. */
10177
10178 void
10179 setup_echo_area_for_printing (int multibyte_p)
10180 {
10181 /* If we can't find an echo area any more, exit. */
10182 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10183 Fkill_emacs (Qnil);
10184
10185 ensure_echo_area_buffers ();
10186
10187 if (!message_buf_print)
10188 {
10189 /* A message has been output since the last time we printed.
10190 Choose a fresh echo area buffer. */
10191 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10192 echo_area_buffer[0] = echo_buffer[1];
10193 else
10194 echo_area_buffer[0] = echo_buffer[0];
10195
10196 /* Switch to that buffer and clear it. */
10197 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10198 bset_truncate_lines (current_buffer, Qnil);
10199
10200 if (Z > BEG)
10201 {
10202 ptrdiff_t count = SPECPDL_INDEX ();
10203 specbind (Qinhibit_read_only, Qt);
10204 /* Note that undo recording is always disabled. */
10205 del_range (BEG, Z);
10206 unbind_to (count, Qnil);
10207 }
10208 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10209
10210 /* Set up the buffer for the multibyteness we need. */
10211 if (multibyte_p
10212 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10213 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10214
10215 /* Raise the frame containing the echo area. */
10216 if (minibuffer_auto_raise)
10217 {
10218 struct frame *sf = SELECTED_FRAME ();
10219 Lisp_Object mini_window;
10220 mini_window = FRAME_MINIBUF_WINDOW (sf);
10221 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10222 }
10223
10224 message_log_maybe_newline ();
10225 message_buf_print = 1;
10226 }
10227 else
10228 {
10229 if (NILP (echo_area_buffer[0]))
10230 {
10231 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10232 echo_area_buffer[0] = echo_buffer[1];
10233 else
10234 echo_area_buffer[0] = echo_buffer[0];
10235 }
10236
10237 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10238 {
10239 /* Someone switched buffers between print requests. */
10240 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10241 bset_truncate_lines (current_buffer, Qnil);
10242 }
10243 }
10244 }
10245
10246
10247 /* Display an echo area message in window W. Value is non-zero if W's
10248 height is changed. If display_last_displayed_message_p is
10249 non-zero, display the message that was last displayed, otherwise
10250 display the current message. */
10251
10252 static int
10253 display_echo_area (struct window *w)
10254 {
10255 int i, no_message_p, window_height_changed_p;
10256
10257 /* Temporarily disable garbage collections while displaying the echo
10258 area. This is done because a GC can print a message itself.
10259 That message would modify the echo area buffer's contents while a
10260 redisplay of the buffer is going on, and seriously confuse
10261 redisplay. */
10262 ptrdiff_t count = inhibit_garbage_collection ();
10263
10264 /* If there is no message, we must call display_echo_area_1
10265 nevertheless because it resizes the window. But we will have to
10266 reset the echo_area_buffer in question to nil at the end because
10267 with_echo_area_buffer will sets it to an empty buffer. */
10268 i = display_last_displayed_message_p ? 1 : 0;
10269 no_message_p = NILP (echo_area_buffer[i]);
10270
10271 window_height_changed_p
10272 = with_echo_area_buffer (w, display_last_displayed_message_p,
10273 display_echo_area_1,
10274 (intptr_t) w, Qnil);
10275
10276 if (no_message_p)
10277 echo_area_buffer[i] = Qnil;
10278
10279 unbind_to (count, Qnil);
10280 return window_height_changed_p;
10281 }
10282
10283
10284 /* Helper for display_echo_area. Display the current buffer which
10285 contains the current echo area message in window W, a mini-window,
10286 a pointer to which is passed in A1. A2..A4 are currently not used.
10287 Change the height of W so that all of the message is displayed.
10288 Value is non-zero if height of W was changed. */
10289
10290 static int
10291 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10292 {
10293 intptr_t i1 = a1;
10294 struct window *w = (struct window *) i1;
10295 Lisp_Object window;
10296 struct text_pos start;
10297 int window_height_changed_p = 0;
10298
10299 /* Do this before displaying, so that we have a large enough glyph
10300 matrix for the display. If we can't get enough space for the
10301 whole text, display the last N lines. That works by setting w->start. */
10302 window_height_changed_p = resize_mini_window (w, 0);
10303
10304 /* Use the starting position chosen by resize_mini_window. */
10305 SET_TEXT_POS_FROM_MARKER (start, w->start);
10306
10307 /* Display. */
10308 clear_glyph_matrix (w->desired_matrix);
10309 XSETWINDOW (window, w);
10310 try_window (window, start, 0);
10311
10312 return window_height_changed_p;
10313 }
10314
10315
10316 /* Resize the echo area window to exactly the size needed for the
10317 currently displayed message, if there is one. If a mini-buffer
10318 is active, don't shrink it. */
10319
10320 void
10321 resize_echo_area_exactly (void)
10322 {
10323 if (BUFFERP (echo_area_buffer[0])
10324 && WINDOWP (echo_area_window))
10325 {
10326 struct window *w = XWINDOW (echo_area_window);
10327 int resized_p;
10328 Lisp_Object resize_exactly;
10329
10330 if (minibuf_level == 0)
10331 resize_exactly = Qt;
10332 else
10333 resize_exactly = Qnil;
10334
10335 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10336 (intptr_t) w, resize_exactly);
10337 if (resized_p)
10338 {
10339 windows_or_buffers_changed = 42;
10340 update_mode_lines = 30;
10341 redisplay_internal ();
10342 }
10343 }
10344 }
10345
10346
10347 /* Callback function for with_echo_area_buffer, when used from
10348 resize_echo_area_exactly. A1 contains a pointer to the window to
10349 resize, EXACTLY non-nil means resize the mini-window exactly to the
10350 size of the text displayed. A3 and A4 are not used. Value is what
10351 resize_mini_window returns. */
10352
10353 static int
10354 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10355 {
10356 intptr_t i1 = a1;
10357 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10358 }
10359
10360
10361 /* Resize mini-window W to fit the size of its contents. EXACT_P
10362 means size the window exactly to the size needed. Otherwise, it's
10363 only enlarged until W's buffer is empty.
10364
10365 Set W->start to the right place to begin display. If the whole
10366 contents fit, start at the beginning. Otherwise, start so as
10367 to make the end of the contents appear. This is particularly
10368 important for y-or-n-p, but seems desirable generally.
10369
10370 Value is non-zero if the window height has been changed. */
10371
10372 int
10373 resize_mini_window (struct window *w, int exact_p)
10374 {
10375 struct frame *f = XFRAME (w->frame);
10376 int window_height_changed_p = 0;
10377
10378 eassert (MINI_WINDOW_P (w));
10379
10380 /* By default, start display at the beginning. */
10381 set_marker_both (w->start, w->contents,
10382 BUF_BEGV (XBUFFER (w->contents)),
10383 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10384
10385 /* Don't resize windows while redisplaying a window; it would
10386 confuse redisplay functions when the size of the window they are
10387 displaying changes from under them. Such a resizing can happen,
10388 for instance, when which-func prints a long message while
10389 we are running fontification-functions. We're running these
10390 functions with safe_call which binds inhibit-redisplay to t. */
10391 if (!NILP (Vinhibit_redisplay))
10392 return 0;
10393
10394 /* Nil means don't try to resize. */
10395 if (NILP (Vresize_mini_windows)
10396 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10397 return 0;
10398
10399 if (!FRAME_MINIBUF_ONLY_P (f))
10400 {
10401 struct it it;
10402 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10403 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10404 int height;
10405 EMACS_INT max_height;
10406 int unit = FRAME_LINE_HEIGHT (f);
10407 struct text_pos start;
10408 struct buffer *old_current_buffer = NULL;
10409
10410 if (current_buffer != XBUFFER (w->contents))
10411 {
10412 old_current_buffer = current_buffer;
10413 set_buffer_internal (XBUFFER (w->contents));
10414 }
10415
10416 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10417
10418 /* Compute the max. number of lines specified by the user. */
10419 if (FLOATP (Vmax_mini_window_height))
10420 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10421 else if (INTEGERP (Vmax_mini_window_height))
10422 max_height = XINT (Vmax_mini_window_height);
10423 else
10424 max_height = total_height / 4;
10425
10426 /* Correct that max. height if it's bogus. */
10427 max_height = clip_to_bounds (1, max_height, total_height);
10428
10429 /* Find out the height of the text in the window. */
10430 if (it.line_wrap == TRUNCATE)
10431 height = 1;
10432 else
10433 {
10434 last_height = 0;
10435 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10436 if (it.max_ascent == 0 && it.max_descent == 0)
10437 height = it.current_y + last_height;
10438 else
10439 height = it.current_y + it.max_ascent + it.max_descent;
10440 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10441 height = (height + unit - 1) / unit;
10442 }
10443
10444 /* Compute a suitable window start. */
10445 if (height > max_height)
10446 {
10447 height = max_height;
10448 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10449 move_it_vertically_backward (&it, (height - 1) * unit);
10450 start = it.current.pos;
10451 }
10452 else
10453 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10454 SET_MARKER_FROM_TEXT_POS (w->start, start);
10455
10456 if (EQ (Vresize_mini_windows, Qgrow_only))
10457 {
10458 /* Let it grow only, until we display an empty message, in which
10459 case the window shrinks again. */
10460 if (height > WINDOW_TOTAL_LINES (w))
10461 {
10462 int old_height = WINDOW_TOTAL_LINES (w);
10463
10464 FRAME_WINDOWS_FROZEN (f) = 1;
10465 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10466 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10467 }
10468 else if (height < WINDOW_TOTAL_LINES (w)
10469 && (exact_p || BEGV == ZV))
10470 {
10471 int old_height = WINDOW_TOTAL_LINES (w);
10472
10473 FRAME_WINDOWS_FROZEN (f) = 0;
10474 shrink_mini_window (w);
10475 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10476 }
10477 }
10478 else
10479 {
10480 /* Always resize to exact size needed. */
10481 if (height > WINDOW_TOTAL_LINES (w))
10482 {
10483 int old_height = WINDOW_TOTAL_LINES (w);
10484
10485 FRAME_WINDOWS_FROZEN (f) = 1;
10486 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10487 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10488 }
10489 else if (height < WINDOW_TOTAL_LINES (w))
10490 {
10491 int old_height = WINDOW_TOTAL_LINES (w);
10492
10493 FRAME_WINDOWS_FROZEN (f) = 0;
10494 shrink_mini_window (w);
10495
10496 if (height)
10497 {
10498 FRAME_WINDOWS_FROZEN (f) = 1;
10499 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10500 }
10501
10502 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10503 }
10504 }
10505
10506 if (old_current_buffer)
10507 set_buffer_internal (old_current_buffer);
10508 }
10509
10510 return window_height_changed_p;
10511 }
10512
10513
10514 /* Value is the current message, a string, or nil if there is no
10515 current message. */
10516
10517 Lisp_Object
10518 current_message (void)
10519 {
10520 Lisp_Object msg;
10521
10522 if (!BUFFERP (echo_area_buffer[0]))
10523 msg = Qnil;
10524 else
10525 {
10526 with_echo_area_buffer (0, 0, current_message_1,
10527 (intptr_t) &msg, Qnil);
10528 if (NILP (msg))
10529 echo_area_buffer[0] = Qnil;
10530 }
10531
10532 return msg;
10533 }
10534
10535
10536 static int
10537 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10538 {
10539 intptr_t i1 = a1;
10540 Lisp_Object *msg = (Lisp_Object *) i1;
10541
10542 if (Z > BEG)
10543 *msg = make_buffer_string (BEG, Z, 1);
10544 else
10545 *msg = Qnil;
10546 return 0;
10547 }
10548
10549
10550 /* Push the current message on Vmessage_stack for later restoration
10551 by restore_message. Value is non-zero if the current message isn't
10552 empty. This is a relatively infrequent operation, so it's not
10553 worth optimizing. */
10554
10555 bool
10556 push_message (void)
10557 {
10558 Lisp_Object msg = current_message ();
10559 Vmessage_stack = Fcons (msg, Vmessage_stack);
10560 return STRINGP (msg);
10561 }
10562
10563
10564 /* Restore message display from the top of Vmessage_stack. */
10565
10566 void
10567 restore_message (void)
10568 {
10569 eassert (CONSP (Vmessage_stack));
10570 message3_nolog (XCAR (Vmessage_stack));
10571 }
10572
10573
10574 /* Handler for unwind-protect calling pop_message. */
10575
10576 void
10577 pop_message_unwind (void)
10578 {
10579 /* Pop the top-most entry off Vmessage_stack. */
10580 eassert (CONSP (Vmessage_stack));
10581 Vmessage_stack = XCDR (Vmessage_stack);
10582 }
10583
10584
10585 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10586 exits. If the stack is not empty, we have a missing pop_message
10587 somewhere. */
10588
10589 void
10590 check_message_stack (void)
10591 {
10592 if (!NILP (Vmessage_stack))
10593 emacs_abort ();
10594 }
10595
10596
10597 /* Truncate to NCHARS what will be displayed in the echo area the next
10598 time we display it---but don't redisplay it now. */
10599
10600 void
10601 truncate_echo_area (ptrdiff_t nchars)
10602 {
10603 if (nchars == 0)
10604 echo_area_buffer[0] = Qnil;
10605 else if (!noninteractive
10606 && INTERACTIVE
10607 && !NILP (echo_area_buffer[0]))
10608 {
10609 struct frame *sf = SELECTED_FRAME ();
10610 /* Error messages get reported properly by cmd_error, so this must be
10611 just an informative message; if the frame hasn't really been
10612 initialized yet, just toss it. */
10613 if (sf->glyphs_initialized_p)
10614 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10615 }
10616 }
10617
10618
10619 /* Helper function for truncate_echo_area. Truncate the current
10620 message to at most NCHARS characters. */
10621
10622 static int
10623 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10624 {
10625 if (BEG + nchars < Z)
10626 del_range (BEG + nchars, Z);
10627 if (Z == BEG)
10628 echo_area_buffer[0] = Qnil;
10629 return 0;
10630 }
10631
10632 /* Set the current message to STRING. */
10633
10634 static void
10635 set_message (Lisp_Object string)
10636 {
10637 eassert (STRINGP (string));
10638
10639 message_enable_multibyte = STRING_MULTIBYTE (string);
10640
10641 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10642 message_buf_print = 0;
10643 help_echo_showing_p = 0;
10644
10645 if (STRINGP (Vdebug_on_message)
10646 && STRINGP (string)
10647 && fast_string_match (Vdebug_on_message, string) >= 0)
10648 call_debugger (list2 (Qerror, string));
10649 }
10650
10651
10652 /* Helper function for set_message. First argument is ignored and second
10653 argument has the same meaning as for set_message.
10654 This function is called with the echo area buffer being current. */
10655
10656 static int
10657 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10658 {
10659 eassert (STRINGP (string));
10660
10661 /* Change multibyteness of the echo buffer appropriately. */
10662 if (message_enable_multibyte
10663 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10664 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10665
10666 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10667 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10668 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10669
10670 /* Insert new message at BEG. */
10671 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10672
10673 /* This function takes care of single/multibyte conversion.
10674 We just have to ensure that the echo area buffer has the right
10675 setting of enable_multibyte_characters. */
10676 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10677
10678 return 0;
10679 }
10680
10681
10682 /* Clear messages. CURRENT_P non-zero means clear the current
10683 message. LAST_DISPLAYED_P non-zero means clear the message
10684 last displayed. */
10685
10686 void
10687 clear_message (int current_p, int last_displayed_p)
10688 {
10689 if (current_p)
10690 {
10691 echo_area_buffer[0] = Qnil;
10692 message_cleared_p = 1;
10693 }
10694
10695 if (last_displayed_p)
10696 echo_area_buffer[1] = Qnil;
10697
10698 message_buf_print = 0;
10699 }
10700
10701 /* Clear garbaged frames.
10702
10703 This function is used where the old redisplay called
10704 redraw_garbaged_frames which in turn called redraw_frame which in
10705 turn called clear_frame. The call to clear_frame was a source of
10706 flickering. I believe a clear_frame is not necessary. It should
10707 suffice in the new redisplay to invalidate all current matrices,
10708 and ensure a complete redisplay of all windows. */
10709
10710 static void
10711 clear_garbaged_frames (void)
10712 {
10713 if (frame_garbaged)
10714 {
10715 Lisp_Object tail, frame;
10716 int changed_count = 0;
10717
10718 FOR_EACH_FRAME (tail, frame)
10719 {
10720 struct frame *f = XFRAME (frame);
10721
10722 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10723 {
10724 if (f->resized_p)
10725 redraw_frame (f);
10726 else
10727 clear_current_matrices (f);
10728 changed_count++;
10729 f->garbaged = 0;
10730 f->resized_p = 0;
10731 }
10732 }
10733
10734 frame_garbaged = 0;
10735 if (changed_count)
10736 windows_or_buffers_changed = 43;
10737 }
10738 }
10739
10740
10741 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10742 is non-zero update selected_frame. Value is non-zero if the
10743 mini-windows height has been changed. */
10744
10745 static int
10746 echo_area_display (int update_frame_p)
10747 {
10748 Lisp_Object mini_window;
10749 struct window *w;
10750 struct frame *f;
10751 int window_height_changed_p = 0;
10752 struct frame *sf = SELECTED_FRAME ();
10753
10754 mini_window = FRAME_MINIBUF_WINDOW (sf);
10755 w = XWINDOW (mini_window);
10756 f = XFRAME (WINDOW_FRAME (w));
10757
10758 /* Don't display if frame is invisible or not yet initialized. */
10759 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10760 return 0;
10761
10762 #ifdef HAVE_WINDOW_SYSTEM
10763 /* When Emacs starts, selected_frame may be the initial terminal
10764 frame. If we let this through, a message would be displayed on
10765 the terminal. */
10766 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10767 return 0;
10768 #endif /* HAVE_WINDOW_SYSTEM */
10769
10770 /* Redraw garbaged frames. */
10771 clear_garbaged_frames ();
10772
10773 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10774 {
10775 echo_area_window = mini_window;
10776 window_height_changed_p = display_echo_area (w);
10777 w->must_be_updated_p = 1;
10778
10779 /* Update the display, unless called from redisplay_internal.
10780 Also don't update the screen during redisplay itself. The
10781 update will happen at the end of redisplay, and an update
10782 here could cause confusion. */
10783 if (update_frame_p && !redisplaying_p)
10784 {
10785 int n = 0;
10786
10787 /* If the display update has been interrupted by pending
10788 input, update mode lines in the frame. Due to the
10789 pending input, it might have been that redisplay hasn't
10790 been called, so that mode lines above the echo area are
10791 garbaged. This looks odd, so we prevent it here. */
10792 if (!display_completed)
10793 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10794
10795 if (window_height_changed_p
10796 /* Don't do this if Emacs is shutting down. Redisplay
10797 needs to run hooks. */
10798 && !NILP (Vrun_hooks))
10799 {
10800 /* Must update other windows. Likewise as in other
10801 cases, don't let this update be interrupted by
10802 pending input. */
10803 ptrdiff_t count = SPECPDL_INDEX ();
10804 specbind (Qredisplay_dont_pause, Qt);
10805 windows_or_buffers_changed = 44;
10806 redisplay_internal ();
10807 unbind_to (count, Qnil);
10808 }
10809 else if (FRAME_WINDOW_P (f) && n == 0)
10810 {
10811 /* Window configuration is the same as before.
10812 Can do with a display update of the echo area,
10813 unless we displayed some mode lines. */
10814 update_single_window (w, 1);
10815 flush_frame (f);
10816 }
10817 else
10818 update_frame (f, 1, 1);
10819
10820 /* If cursor is in the echo area, make sure that the next
10821 redisplay displays the minibuffer, so that the cursor will
10822 be replaced with what the minibuffer wants. */
10823 if (cursor_in_echo_area)
10824 windows_or_buffers_changed = 45;
10825 }
10826 }
10827 else if (!EQ (mini_window, selected_window))
10828 windows_or_buffers_changed = 46;
10829
10830 /* Last displayed message is now the current message. */
10831 echo_area_buffer[1] = echo_area_buffer[0];
10832 /* Inform read_char that we're not echoing. */
10833 echo_message_buffer = Qnil;
10834
10835 /* Prevent redisplay optimization in redisplay_internal by resetting
10836 this_line_start_pos. This is done because the mini-buffer now
10837 displays the message instead of its buffer text. */
10838 if (EQ (mini_window, selected_window))
10839 CHARPOS (this_line_start_pos) = 0;
10840
10841 return window_height_changed_p;
10842 }
10843
10844 /* Nonzero if the current window's buffer is shown in more than one
10845 window and was modified since last redisplay. */
10846
10847 static int
10848 buffer_shared_and_changed (void)
10849 {
10850 return (buffer_window_count (current_buffer) > 1
10851 && UNCHANGED_MODIFIED < MODIFF);
10852 }
10853
10854 /* Nonzero if W's buffer was changed but not saved. */
10855
10856 static int
10857 window_buffer_changed (struct window *w)
10858 {
10859 struct buffer *b = XBUFFER (w->contents);
10860
10861 eassert (BUFFER_LIVE_P (b));
10862
10863 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
10864 }
10865
10866 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10867
10868 static int
10869 mode_line_update_needed (struct window *w)
10870 {
10871 return (w->column_number_displayed != -1
10872 && !(PT == w->last_point && !window_outdated (w))
10873 && (w->column_number_displayed != current_column ()));
10874 }
10875
10876 /* Nonzero if window start of W is frozen and may not be changed during
10877 redisplay. */
10878
10879 static bool
10880 window_frozen_p (struct window *w)
10881 {
10882 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10883 {
10884 Lisp_Object window;
10885
10886 XSETWINDOW (window, w);
10887 if (MINI_WINDOW_P (w))
10888 return 0;
10889 else if (EQ (window, selected_window))
10890 return 0;
10891 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10892 && EQ (window, Vminibuf_scroll_window))
10893 /* This special window can't be frozen too. */
10894 return 0;
10895 else
10896 return 1;
10897 }
10898 return 0;
10899 }
10900
10901 /***********************************************************************
10902 Mode Lines and Frame Titles
10903 ***********************************************************************/
10904
10905 /* A buffer for constructing non-propertized mode-line strings and
10906 frame titles in it; allocated from the heap in init_xdisp and
10907 resized as needed in store_mode_line_noprop_char. */
10908
10909 static char *mode_line_noprop_buf;
10910
10911 /* The buffer's end, and a current output position in it. */
10912
10913 static char *mode_line_noprop_buf_end;
10914 static char *mode_line_noprop_ptr;
10915
10916 #define MODE_LINE_NOPROP_LEN(start) \
10917 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10918
10919 static enum {
10920 MODE_LINE_DISPLAY = 0,
10921 MODE_LINE_TITLE,
10922 MODE_LINE_NOPROP,
10923 MODE_LINE_STRING
10924 } mode_line_target;
10925
10926 /* Alist that caches the results of :propertize.
10927 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10928 static Lisp_Object mode_line_proptrans_alist;
10929
10930 /* List of strings making up the mode-line. */
10931 static Lisp_Object mode_line_string_list;
10932
10933 /* Base face property when building propertized mode line string. */
10934 static Lisp_Object mode_line_string_face;
10935 static Lisp_Object mode_line_string_face_prop;
10936
10937
10938 /* Unwind data for mode line strings */
10939
10940 static Lisp_Object Vmode_line_unwind_vector;
10941
10942 static Lisp_Object
10943 format_mode_line_unwind_data (struct frame *target_frame,
10944 struct buffer *obuf,
10945 Lisp_Object owin,
10946 int save_proptrans)
10947 {
10948 Lisp_Object vector, tmp;
10949
10950 /* Reduce consing by keeping one vector in
10951 Vwith_echo_area_save_vector. */
10952 vector = Vmode_line_unwind_vector;
10953 Vmode_line_unwind_vector = Qnil;
10954
10955 if (NILP (vector))
10956 vector = Fmake_vector (make_number (10), Qnil);
10957
10958 ASET (vector, 0, make_number (mode_line_target));
10959 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10960 ASET (vector, 2, mode_line_string_list);
10961 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10962 ASET (vector, 4, mode_line_string_face);
10963 ASET (vector, 5, mode_line_string_face_prop);
10964
10965 if (obuf)
10966 XSETBUFFER (tmp, obuf);
10967 else
10968 tmp = Qnil;
10969 ASET (vector, 6, tmp);
10970 ASET (vector, 7, owin);
10971 if (target_frame)
10972 {
10973 /* Similarly to `with-selected-window', if the operation selects
10974 a window on another frame, we must restore that frame's
10975 selected window, and (for a tty) the top-frame. */
10976 ASET (vector, 8, target_frame->selected_window);
10977 if (FRAME_TERMCAP_P (target_frame))
10978 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10979 }
10980
10981 return vector;
10982 }
10983
10984 static void
10985 unwind_format_mode_line (Lisp_Object vector)
10986 {
10987 Lisp_Object old_window = AREF (vector, 7);
10988 Lisp_Object target_frame_window = AREF (vector, 8);
10989 Lisp_Object old_top_frame = AREF (vector, 9);
10990
10991 mode_line_target = XINT (AREF (vector, 0));
10992 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10993 mode_line_string_list = AREF (vector, 2);
10994 if (! EQ (AREF (vector, 3), Qt))
10995 mode_line_proptrans_alist = AREF (vector, 3);
10996 mode_line_string_face = AREF (vector, 4);
10997 mode_line_string_face_prop = AREF (vector, 5);
10998
10999 /* Select window before buffer, since it may change the buffer. */
11000 if (!NILP (old_window))
11001 {
11002 /* If the operation that we are unwinding had selected a window
11003 on a different frame, reset its frame-selected-window. For a
11004 text terminal, reset its top-frame if necessary. */
11005 if (!NILP (target_frame_window))
11006 {
11007 Lisp_Object frame
11008 = WINDOW_FRAME (XWINDOW (target_frame_window));
11009
11010 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11011 Fselect_window (target_frame_window, Qt);
11012
11013 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11014 Fselect_frame (old_top_frame, Qt);
11015 }
11016
11017 Fselect_window (old_window, Qt);
11018 }
11019
11020 if (!NILP (AREF (vector, 6)))
11021 {
11022 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11023 ASET (vector, 6, Qnil);
11024 }
11025
11026 Vmode_line_unwind_vector = vector;
11027 }
11028
11029
11030 /* Store a single character C for the frame title in mode_line_noprop_buf.
11031 Re-allocate mode_line_noprop_buf if necessary. */
11032
11033 static void
11034 store_mode_line_noprop_char (char c)
11035 {
11036 /* If output position has reached the end of the allocated buffer,
11037 increase the buffer's size. */
11038 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11039 {
11040 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11041 ptrdiff_t size = len;
11042 mode_line_noprop_buf =
11043 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11044 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11045 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11046 }
11047
11048 *mode_line_noprop_ptr++ = c;
11049 }
11050
11051
11052 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11053 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11054 characters that yield more columns than PRECISION; PRECISION <= 0
11055 means copy the whole string. Pad with spaces until FIELD_WIDTH
11056 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11057 pad. Called from display_mode_element when it is used to build a
11058 frame title. */
11059
11060 static int
11061 store_mode_line_noprop (const char *string, int field_width, int precision)
11062 {
11063 const unsigned char *str = (const unsigned char *) string;
11064 int n = 0;
11065 ptrdiff_t dummy, nbytes;
11066
11067 /* Copy at most PRECISION chars from STR. */
11068 nbytes = strlen (string);
11069 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11070 while (nbytes--)
11071 store_mode_line_noprop_char (*str++);
11072
11073 /* Fill up with spaces until FIELD_WIDTH reached. */
11074 while (field_width > 0
11075 && n < field_width)
11076 {
11077 store_mode_line_noprop_char (' ');
11078 ++n;
11079 }
11080
11081 return n;
11082 }
11083
11084 /***********************************************************************
11085 Frame Titles
11086 ***********************************************************************/
11087
11088 #ifdef HAVE_WINDOW_SYSTEM
11089
11090 /* Set the title of FRAME, if it has changed. The title format is
11091 Vicon_title_format if FRAME is iconified, otherwise it is
11092 frame_title_format. */
11093
11094 static void
11095 x_consider_frame_title (Lisp_Object frame)
11096 {
11097 struct frame *f = XFRAME (frame);
11098
11099 if (FRAME_WINDOW_P (f)
11100 || FRAME_MINIBUF_ONLY_P (f)
11101 || f->explicit_name)
11102 {
11103 /* Do we have more than one visible frame on this X display? */
11104 Lisp_Object tail, other_frame, fmt;
11105 ptrdiff_t title_start;
11106 char *title;
11107 ptrdiff_t len;
11108 struct it it;
11109 ptrdiff_t count = SPECPDL_INDEX ();
11110
11111 FOR_EACH_FRAME (tail, other_frame)
11112 {
11113 struct frame *tf = XFRAME (other_frame);
11114
11115 if (tf != f
11116 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11117 && !FRAME_MINIBUF_ONLY_P (tf)
11118 && !EQ (other_frame, tip_frame)
11119 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11120 break;
11121 }
11122
11123 /* Set global variable indicating that multiple frames exist. */
11124 multiple_frames = CONSP (tail);
11125
11126 /* Switch to the buffer of selected window of the frame. Set up
11127 mode_line_target so that display_mode_element will output into
11128 mode_line_noprop_buf; then display the title. */
11129 record_unwind_protect (unwind_format_mode_line,
11130 format_mode_line_unwind_data
11131 (f, current_buffer, selected_window, 0));
11132
11133 Fselect_window (f->selected_window, Qt);
11134 set_buffer_internal_1
11135 (XBUFFER (XWINDOW (f->selected_window)->contents));
11136 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11137
11138 mode_line_target = MODE_LINE_TITLE;
11139 title_start = MODE_LINE_NOPROP_LEN (0);
11140 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11141 NULL, DEFAULT_FACE_ID);
11142 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11143 len = MODE_LINE_NOPROP_LEN (title_start);
11144 title = mode_line_noprop_buf + title_start;
11145 unbind_to (count, Qnil);
11146
11147 /* Set the title only if it's changed. This avoids consing in
11148 the common case where it hasn't. (If it turns out that we've
11149 already wasted too much time by walking through the list with
11150 display_mode_element, then we might need to optimize at a
11151 higher level than this.) */
11152 if (! STRINGP (f->name)
11153 || SBYTES (f->name) != len
11154 || memcmp (title, SDATA (f->name), len) != 0)
11155 x_implicitly_set_name (f, make_string (title, len), Qnil);
11156 }
11157 }
11158
11159 #endif /* not HAVE_WINDOW_SYSTEM */
11160
11161 \f
11162 /***********************************************************************
11163 Menu Bars
11164 ***********************************************************************/
11165
11166
11167 /* Prepare for redisplay by updating menu-bar item lists when
11168 appropriate. This can call eval. */
11169
11170 static void
11171 prepare_menu_bars (void)
11172 {
11173 int all_windows;
11174 struct gcpro gcpro1, gcpro2;
11175 struct frame *f;
11176 Lisp_Object tooltip_frame;
11177
11178 #ifdef HAVE_WINDOW_SYSTEM
11179 tooltip_frame = tip_frame;
11180 #else
11181 tooltip_frame = Qnil;
11182 #endif
11183
11184 /* Update all frame titles based on their buffer names, etc. We do
11185 this before the menu bars so that the buffer-menu will show the
11186 up-to-date frame titles. */
11187 #ifdef HAVE_WINDOW_SYSTEM
11188 if (windows_or_buffers_changed || update_mode_lines)
11189 {
11190 Lisp_Object tail, frame;
11191
11192 FOR_EACH_FRAME (tail, frame)
11193 {
11194 f = XFRAME (frame);
11195 if (!EQ (frame, tooltip_frame)
11196 && (FRAME_ICONIFIED_P (f)
11197 || FRAME_VISIBLE_P (f) == 1
11198 /* Exclude TTY frames that are obscured because they
11199 are not the top frame on their console. This is
11200 because x_consider_frame_title actually switches
11201 to the frame, which for TTY frames means it is
11202 marked as garbaged, and will be completely
11203 redrawn on the next redisplay cycle. This causes
11204 TTY frames to be completely redrawn, when there
11205 are more than one of them, even though nothing
11206 should be changed on display. */
11207 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11208 x_consider_frame_title (frame);
11209 }
11210 }
11211 #endif /* HAVE_WINDOW_SYSTEM */
11212
11213 /* Update the menu bar item lists, if appropriate. This has to be
11214 done before any actual redisplay or generation of display lines. */
11215 all_windows = (update_mode_lines
11216 || buffer_shared_and_changed ()
11217 || windows_or_buffers_changed);
11218
11219 if (FUNCTIONP (Vpre_redisplay_function))
11220 safe_call1 (Vpre_redisplay_function, all_windows ? Qt : Qnil);
11221
11222 if (all_windows)
11223 {
11224 Lisp_Object tail, frame;
11225 ptrdiff_t count = SPECPDL_INDEX ();
11226 /* 1 means that update_menu_bar has run its hooks
11227 so any further calls to update_menu_bar shouldn't do so again. */
11228 int menu_bar_hooks_run = 0;
11229
11230 record_unwind_save_match_data ();
11231
11232 FOR_EACH_FRAME (tail, frame)
11233 {
11234 f = XFRAME (frame);
11235
11236 /* Ignore tooltip frame. */
11237 if (EQ (frame, tooltip_frame))
11238 continue;
11239
11240 /* If a window on this frame changed size, report that to
11241 the user and clear the size-change flag. */
11242 if (FRAME_WINDOW_SIZES_CHANGED (f))
11243 {
11244 Lisp_Object functions;
11245
11246 /* Clear flag first in case we get an error below. */
11247 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11248 functions = Vwindow_size_change_functions;
11249 GCPRO2 (tail, functions);
11250
11251 while (CONSP (functions))
11252 {
11253 if (!EQ (XCAR (functions), Qt))
11254 call1 (XCAR (functions), frame);
11255 functions = XCDR (functions);
11256 }
11257 UNGCPRO;
11258 }
11259
11260 GCPRO1 (tail);
11261 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11262 #ifdef HAVE_WINDOW_SYSTEM
11263 update_tool_bar (f, 0);
11264 #endif
11265 #ifdef HAVE_NS
11266 if (windows_or_buffers_changed
11267 && FRAME_NS_P (f))
11268 ns_set_doc_edited
11269 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11270 #endif
11271 UNGCPRO;
11272 }
11273
11274 unbind_to (count, Qnil);
11275 }
11276 else
11277 {
11278 struct frame *sf = SELECTED_FRAME ();
11279 update_menu_bar (sf, 1, 0);
11280 #ifdef HAVE_WINDOW_SYSTEM
11281 update_tool_bar (sf, 1);
11282 #endif
11283 }
11284 }
11285
11286
11287 /* Update the menu bar item list for frame F. This has to be done
11288 before we start to fill in any display lines, because it can call
11289 eval.
11290
11291 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11292
11293 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11294 already ran the menu bar hooks for this redisplay, so there
11295 is no need to run them again. The return value is the
11296 updated value of this flag, to pass to the next call. */
11297
11298 static int
11299 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11300 {
11301 Lisp_Object window;
11302 register struct window *w;
11303
11304 /* If called recursively during a menu update, do nothing. This can
11305 happen when, for instance, an activate-menubar-hook causes a
11306 redisplay. */
11307 if (inhibit_menubar_update)
11308 return hooks_run;
11309
11310 window = FRAME_SELECTED_WINDOW (f);
11311 w = XWINDOW (window);
11312
11313 if (FRAME_WINDOW_P (f)
11314 ?
11315 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11316 || defined (HAVE_NS) || defined (USE_GTK)
11317 FRAME_EXTERNAL_MENU_BAR (f)
11318 #else
11319 FRAME_MENU_BAR_LINES (f) > 0
11320 #endif
11321 : FRAME_MENU_BAR_LINES (f) > 0)
11322 {
11323 /* If the user has switched buffers or windows, we need to
11324 recompute to reflect the new bindings. But we'll
11325 recompute when update_mode_lines is set too; that means
11326 that people can use force-mode-line-update to request
11327 that the menu bar be recomputed. The adverse effect on
11328 the rest of the redisplay algorithm is about the same as
11329 windows_or_buffers_changed anyway. */
11330 if (windows_or_buffers_changed
11331 /* This used to test w->update_mode_line, but we believe
11332 there is no need to recompute the menu in that case. */
11333 || update_mode_lines
11334 || window_buffer_changed (w))
11335 {
11336 struct buffer *prev = current_buffer;
11337 ptrdiff_t count = SPECPDL_INDEX ();
11338
11339 specbind (Qinhibit_menubar_update, Qt);
11340
11341 set_buffer_internal_1 (XBUFFER (w->contents));
11342 if (save_match_data)
11343 record_unwind_save_match_data ();
11344 if (NILP (Voverriding_local_map_menu_flag))
11345 {
11346 specbind (Qoverriding_terminal_local_map, Qnil);
11347 specbind (Qoverriding_local_map, Qnil);
11348 }
11349
11350 if (!hooks_run)
11351 {
11352 /* Run the Lucid hook. */
11353 safe_run_hooks (Qactivate_menubar_hook);
11354
11355 /* If it has changed current-menubar from previous value,
11356 really recompute the menu-bar from the value. */
11357 if (! NILP (Vlucid_menu_bar_dirty_flag))
11358 call0 (Qrecompute_lucid_menubar);
11359
11360 safe_run_hooks (Qmenu_bar_update_hook);
11361
11362 hooks_run = 1;
11363 }
11364
11365 XSETFRAME (Vmenu_updating_frame, f);
11366 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11367
11368 /* Redisplay the menu bar in case we changed it. */
11369 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11370 || defined (HAVE_NS) || defined (USE_GTK)
11371 if (FRAME_WINDOW_P (f))
11372 {
11373 #if defined (HAVE_NS)
11374 /* All frames on Mac OS share the same menubar. So only
11375 the selected frame should be allowed to set it. */
11376 if (f == SELECTED_FRAME ())
11377 #endif
11378 set_frame_menubar (f, 0, 0);
11379 }
11380 else
11381 /* On a terminal screen, the menu bar is an ordinary screen
11382 line, and this makes it get updated. */
11383 w->update_mode_line = 1;
11384 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11385 /* In the non-toolkit version, the menu bar is an ordinary screen
11386 line, and this makes it get updated. */
11387 w->update_mode_line = 1;
11388 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11389
11390 unbind_to (count, Qnil);
11391 set_buffer_internal_1 (prev);
11392 }
11393 }
11394
11395 return hooks_run;
11396 }
11397
11398 /***********************************************************************
11399 Tool-bars
11400 ***********************************************************************/
11401
11402 #ifdef HAVE_WINDOW_SYSTEM
11403
11404 /* Tool-bar item index of the item on which a mouse button was pressed
11405 or -1. */
11406
11407 int last_tool_bar_item;
11408
11409 /* Select `frame' temporarily without running all the code in
11410 do_switch_frame.
11411 FIXME: Maybe do_switch_frame should be trimmed down similarly
11412 when `norecord' is set. */
11413 static void
11414 fast_set_selected_frame (Lisp_Object frame)
11415 {
11416 if (!EQ (selected_frame, frame))
11417 {
11418 selected_frame = frame;
11419 selected_window = XFRAME (frame)->selected_window;
11420 }
11421 }
11422
11423 /* Update the tool-bar item list for frame F. This has to be done
11424 before we start to fill in any display lines. Called from
11425 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11426 and restore it here. */
11427
11428 static void
11429 update_tool_bar (struct frame *f, int save_match_data)
11430 {
11431 #if defined (USE_GTK) || defined (HAVE_NS)
11432 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11433 #else
11434 int do_update = WINDOWP (f->tool_bar_window)
11435 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11436 #endif
11437
11438 if (do_update)
11439 {
11440 Lisp_Object window;
11441 struct window *w;
11442
11443 window = FRAME_SELECTED_WINDOW (f);
11444 w = XWINDOW (window);
11445
11446 /* If the user has switched buffers or windows, we need to
11447 recompute to reflect the new bindings. But we'll
11448 recompute when update_mode_lines is set too; that means
11449 that people can use force-mode-line-update to request
11450 that the menu bar be recomputed. The adverse effect on
11451 the rest of the redisplay algorithm is about the same as
11452 windows_or_buffers_changed anyway. */
11453 if (windows_or_buffers_changed
11454 || w->update_mode_line
11455 || update_mode_lines
11456 || window_buffer_changed (w))
11457 {
11458 struct buffer *prev = current_buffer;
11459 ptrdiff_t count = SPECPDL_INDEX ();
11460 Lisp_Object frame, new_tool_bar;
11461 int new_n_tool_bar;
11462 struct gcpro gcpro1;
11463
11464 /* Set current_buffer to the buffer of the selected
11465 window of the frame, so that we get the right local
11466 keymaps. */
11467 set_buffer_internal_1 (XBUFFER (w->contents));
11468
11469 /* Save match data, if we must. */
11470 if (save_match_data)
11471 record_unwind_save_match_data ();
11472
11473 /* Make sure that we don't accidentally use bogus keymaps. */
11474 if (NILP (Voverriding_local_map_menu_flag))
11475 {
11476 specbind (Qoverriding_terminal_local_map, Qnil);
11477 specbind (Qoverriding_local_map, Qnil);
11478 }
11479
11480 GCPRO1 (new_tool_bar);
11481
11482 /* We must temporarily set the selected frame to this frame
11483 before calling tool_bar_items, because the calculation of
11484 the tool-bar keymap uses the selected frame (see
11485 `tool-bar-make-keymap' in tool-bar.el). */
11486 eassert (EQ (selected_window,
11487 /* Since we only explicitly preserve selected_frame,
11488 check that selected_window would be redundant. */
11489 XFRAME (selected_frame)->selected_window));
11490 record_unwind_protect (fast_set_selected_frame, selected_frame);
11491 XSETFRAME (frame, f);
11492 fast_set_selected_frame (frame);
11493
11494 /* Build desired tool-bar items from keymaps. */
11495 new_tool_bar
11496 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11497 &new_n_tool_bar);
11498
11499 /* Redisplay the tool-bar if we changed it. */
11500 if (new_n_tool_bar != f->n_tool_bar_items
11501 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11502 {
11503 /* Redisplay that happens asynchronously due to an expose event
11504 may access f->tool_bar_items. Make sure we update both
11505 variables within BLOCK_INPUT so no such event interrupts. */
11506 block_input ();
11507 fset_tool_bar_items (f, new_tool_bar);
11508 f->n_tool_bar_items = new_n_tool_bar;
11509 w->update_mode_line = 1;
11510 unblock_input ();
11511 }
11512
11513 UNGCPRO;
11514
11515 unbind_to (count, Qnil);
11516 set_buffer_internal_1 (prev);
11517 }
11518 }
11519 }
11520
11521 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11522
11523 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11524 F's desired tool-bar contents. F->tool_bar_items must have
11525 been set up previously by calling prepare_menu_bars. */
11526
11527 static void
11528 build_desired_tool_bar_string (struct frame *f)
11529 {
11530 int i, size, size_needed;
11531 struct gcpro gcpro1, gcpro2, gcpro3;
11532 Lisp_Object image, plist, props;
11533
11534 image = plist = props = Qnil;
11535 GCPRO3 (image, plist, props);
11536
11537 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11538 Otherwise, make a new string. */
11539
11540 /* The size of the string we might be able to reuse. */
11541 size = (STRINGP (f->desired_tool_bar_string)
11542 ? SCHARS (f->desired_tool_bar_string)
11543 : 0);
11544
11545 /* We need one space in the string for each image. */
11546 size_needed = f->n_tool_bar_items;
11547
11548 /* Reuse f->desired_tool_bar_string, if possible. */
11549 if (size < size_needed || NILP (f->desired_tool_bar_string))
11550 fset_desired_tool_bar_string
11551 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11552 else
11553 {
11554 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11555 Fremove_text_properties (make_number (0), make_number (size),
11556 props, f->desired_tool_bar_string);
11557 }
11558
11559 /* Put a `display' property on the string for the images to display,
11560 put a `menu_item' property on tool-bar items with a value that
11561 is the index of the item in F's tool-bar item vector. */
11562 for (i = 0; i < f->n_tool_bar_items; ++i)
11563 {
11564 #define PROP(IDX) \
11565 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11566
11567 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11568 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11569 int hmargin, vmargin, relief, idx, end;
11570
11571 /* If image is a vector, choose the image according to the
11572 button state. */
11573 image = PROP (TOOL_BAR_ITEM_IMAGES);
11574 if (VECTORP (image))
11575 {
11576 if (enabled_p)
11577 idx = (selected_p
11578 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11579 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11580 else
11581 idx = (selected_p
11582 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11583 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11584
11585 eassert (ASIZE (image) >= idx);
11586 image = AREF (image, idx);
11587 }
11588 else
11589 idx = -1;
11590
11591 /* Ignore invalid image specifications. */
11592 if (!valid_image_p (image))
11593 continue;
11594
11595 /* Display the tool-bar button pressed, or depressed. */
11596 plist = Fcopy_sequence (XCDR (image));
11597
11598 /* Compute margin and relief to draw. */
11599 relief = (tool_bar_button_relief >= 0
11600 ? tool_bar_button_relief
11601 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11602 hmargin = vmargin = relief;
11603
11604 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11605 INT_MAX - max (hmargin, vmargin)))
11606 {
11607 hmargin += XFASTINT (Vtool_bar_button_margin);
11608 vmargin += XFASTINT (Vtool_bar_button_margin);
11609 }
11610 else if (CONSP (Vtool_bar_button_margin))
11611 {
11612 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11613 INT_MAX - hmargin))
11614 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11615
11616 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11617 INT_MAX - vmargin))
11618 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11619 }
11620
11621 if (auto_raise_tool_bar_buttons_p)
11622 {
11623 /* Add a `:relief' property to the image spec if the item is
11624 selected. */
11625 if (selected_p)
11626 {
11627 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11628 hmargin -= relief;
11629 vmargin -= relief;
11630 }
11631 }
11632 else
11633 {
11634 /* If image is selected, display it pressed, i.e. with a
11635 negative relief. If it's not selected, display it with a
11636 raised relief. */
11637 plist = Fplist_put (plist, QCrelief,
11638 (selected_p
11639 ? make_number (-relief)
11640 : make_number (relief)));
11641 hmargin -= relief;
11642 vmargin -= relief;
11643 }
11644
11645 /* Put a margin around the image. */
11646 if (hmargin || vmargin)
11647 {
11648 if (hmargin == vmargin)
11649 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11650 else
11651 plist = Fplist_put (plist, QCmargin,
11652 Fcons (make_number (hmargin),
11653 make_number (vmargin)));
11654 }
11655
11656 /* If button is not enabled, and we don't have special images
11657 for the disabled state, make the image appear disabled by
11658 applying an appropriate algorithm to it. */
11659 if (!enabled_p && idx < 0)
11660 plist = Fplist_put (plist, QCconversion, Qdisabled);
11661
11662 /* Put a `display' text property on the string for the image to
11663 display. Put a `menu-item' property on the string that gives
11664 the start of this item's properties in the tool-bar items
11665 vector. */
11666 image = Fcons (Qimage, plist);
11667 props = list4 (Qdisplay, image,
11668 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11669
11670 /* Let the last image hide all remaining spaces in the tool bar
11671 string. The string can be longer than needed when we reuse a
11672 previous string. */
11673 if (i + 1 == f->n_tool_bar_items)
11674 end = SCHARS (f->desired_tool_bar_string);
11675 else
11676 end = i + 1;
11677 Fadd_text_properties (make_number (i), make_number (end),
11678 props, f->desired_tool_bar_string);
11679 #undef PROP
11680 }
11681
11682 UNGCPRO;
11683 }
11684
11685
11686 /* Display one line of the tool-bar of frame IT->f.
11687
11688 HEIGHT specifies the desired height of the tool-bar line.
11689 If the actual height of the glyph row is less than HEIGHT, the
11690 row's height is increased to HEIGHT, and the icons are centered
11691 vertically in the new height.
11692
11693 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11694 count a final empty row in case the tool-bar width exactly matches
11695 the window width.
11696 */
11697
11698 static void
11699 display_tool_bar_line (struct it *it, int height)
11700 {
11701 struct glyph_row *row = it->glyph_row;
11702 int max_x = it->last_visible_x;
11703 struct glyph *last;
11704
11705 prepare_desired_row (row);
11706 row->y = it->current_y;
11707
11708 /* Note that this isn't made use of if the face hasn't a box,
11709 so there's no need to check the face here. */
11710 it->start_of_box_run_p = 1;
11711
11712 while (it->current_x < max_x)
11713 {
11714 int x, n_glyphs_before, i, nglyphs;
11715 struct it it_before;
11716
11717 /* Get the next display element. */
11718 if (!get_next_display_element (it))
11719 {
11720 /* Don't count empty row if we are counting needed tool-bar lines. */
11721 if (height < 0 && !it->hpos)
11722 return;
11723 break;
11724 }
11725
11726 /* Produce glyphs. */
11727 n_glyphs_before = row->used[TEXT_AREA];
11728 it_before = *it;
11729
11730 PRODUCE_GLYPHS (it);
11731
11732 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11733 i = 0;
11734 x = it_before.current_x;
11735 while (i < nglyphs)
11736 {
11737 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11738
11739 if (x + glyph->pixel_width > max_x)
11740 {
11741 /* Glyph doesn't fit on line. Backtrack. */
11742 row->used[TEXT_AREA] = n_glyphs_before;
11743 *it = it_before;
11744 /* If this is the only glyph on this line, it will never fit on the
11745 tool-bar, so skip it. But ensure there is at least one glyph,
11746 so we don't accidentally disable the tool-bar. */
11747 if (n_glyphs_before == 0
11748 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11749 break;
11750 goto out;
11751 }
11752
11753 ++it->hpos;
11754 x += glyph->pixel_width;
11755 ++i;
11756 }
11757
11758 /* Stop at line end. */
11759 if (ITERATOR_AT_END_OF_LINE_P (it))
11760 break;
11761
11762 set_iterator_to_next (it, 1);
11763 }
11764
11765 out:;
11766
11767 row->displays_text_p = row->used[TEXT_AREA] != 0;
11768
11769 /* Use default face for the border below the tool bar.
11770
11771 FIXME: When auto-resize-tool-bars is grow-only, there is
11772 no additional border below the possibly empty tool-bar lines.
11773 So to make the extra empty lines look "normal", we have to
11774 use the tool-bar face for the border too. */
11775 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11776 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11777 it->face_id = DEFAULT_FACE_ID;
11778
11779 extend_face_to_end_of_line (it);
11780 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11781 last->right_box_line_p = 1;
11782 if (last == row->glyphs[TEXT_AREA])
11783 last->left_box_line_p = 1;
11784
11785 /* Make line the desired height and center it vertically. */
11786 if ((height -= it->max_ascent + it->max_descent) > 0)
11787 {
11788 /* Don't add more than one line height. */
11789 height %= FRAME_LINE_HEIGHT (it->f);
11790 it->max_ascent += height / 2;
11791 it->max_descent += (height + 1) / 2;
11792 }
11793
11794 compute_line_metrics (it);
11795
11796 /* If line is empty, make it occupy the rest of the tool-bar. */
11797 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11798 {
11799 row->height = row->phys_height = it->last_visible_y - row->y;
11800 row->visible_height = row->height;
11801 row->ascent = row->phys_ascent = 0;
11802 row->extra_line_spacing = 0;
11803 }
11804
11805 row->full_width_p = 1;
11806 row->continued_p = 0;
11807 row->truncated_on_left_p = 0;
11808 row->truncated_on_right_p = 0;
11809
11810 it->current_x = it->hpos = 0;
11811 it->current_y += row->height;
11812 ++it->vpos;
11813 ++it->glyph_row;
11814 }
11815
11816
11817 /* Max tool-bar height. */
11818
11819 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11820 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11821
11822 /* Value is the number of screen lines needed to make all tool-bar
11823 items of frame F visible. The number of actual rows needed is
11824 returned in *N_ROWS if non-NULL. */
11825
11826 static int
11827 tool_bar_lines_needed (struct frame *f, int *n_rows)
11828 {
11829 struct window *w = XWINDOW (f->tool_bar_window);
11830 struct it it;
11831 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11832 the desired matrix, so use (unused) mode-line row as temporary row to
11833 avoid destroying the first tool-bar row. */
11834 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11835
11836 /* Initialize an iterator for iteration over
11837 F->desired_tool_bar_string in the tool-bar window of frame F. */
11838 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11839 it.first_visible_x = 0;
11840 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11841 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11842 it.paragraph_embedding = L2R;
11843
11844 while (!ITERATOR_AT_END_P (&it))
11845 {
11846 clear_glyph_row (temp_row);
11847 it.glyph_row = temp_row;
11848 display_tool_bar_line (&it, -1);
11849 }
11850 clear_glyph_row (temp_row);
11851
11852 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11853 if (n_rows)
11854 *n_rows = it.vpos > 0 ? it.vpos : -1;
11855
11856 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11857 }
11858
11859 #endif /* !USE_GTK && !HAVE_NS */
11860
11861 #if defined USE_GTK || defined HAVE_NS
11862 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
11863 #endif
11864
11865 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11866 0, 1, 0,
11867 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11868 If FRAME is nil or omitted, use the selected frame. */)
11869 (Lisp_Object frame)
11870 {
11871 int nlines = 0;
11872 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11873 struct frame *f = decode_any_frame (frame);
11874 struct window *w;
11875
11876 if (WINDOWP (f->tool_bar_window)
11877 && (w = XWINDOW (f->tool_bar_window),
11878 WINDOW_TOTAL_LINES (w) > 0))
11879 {
11880 update_tool_bar (f, 1);
11881 if (f->n_tool_bar_items)
11882 {
11883 build_desired_tool_bar_string (f);
11884 nlines = tool_bar_lines_needed (f, NULL);
11885 }
11886 }
11887 #endif
11888 return make_number (nlines);
11889 }
11890
11891
11892 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11893 height should be changed. */
11894
11895 static int
11896 redisplay_tool_bar (struct frame *f)
11897 {
11898 #if defined (USE_GTK) || defined (HAVE_NS)
11899
11900 if (FRAME_EXTERNAL_TOOL_BAR (f))
11901 update_frame_tool_bar (f);
11902 return 0;
11903
11904 #else /* !USE_GTK && !HAVE_NS */
11905
11906 struct window *w;
11907 struct it it;
11908 struct glyph_row *row;
11909
11910 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11911 do anything. This means you must start with tool-bar-lines
11912 non-zero to get the auto-sizing effect. Or in other words, you
11913 can turn off tool-bars by specifying tool-bar-lines zero. */
11914 if (!WINDOWP (f->tool_bar_window)
11915 || (w = XWINDOW (f->tool_bar_window),
11916 WINDOW_TOTAL_LINES (w) == 0))
11917 return 0;
11918
11919 /* Set up an iterator for the tool-bar window. */
11920 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11921 it.first_visible_x = 0;
11922 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11923 row = it.glyph_row;
11924
11925 /* Build a string that represents the contents of the tool-bar. */
11926 build_desired_tool_bar_string (f);
11927 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11928 /* FIXME: This should be controlled by a user option. But it
11929 doesn't make sense to have an R2L tool bar if the menu bar cannot
11930 be drawn also R2L, and making the menu bar R2L is tricky due
11931 toolkit-specific code that implements it. If an R2L tool bar is
11932 ever supported, display_tool_bar_line should also be augmented to
11933 call unproduce_glyphs like display_line and display_string
11934 do. */
11935 it.paragraph_embedding = L2R;
11936
11937 if (f->n_tool_bar_rows == 0)
11938 {
11939 int nlines;
11940
11941 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11942 nlines != WINDOW_TOTAL_LINES (w)))
11943 {
11944 Lisp_Object frame;
11945 int old_height = WINDOW_TOTAL_LINES (w);
11946
11947 XSETFRAME (frame, f);
11948 Fmodify_frame_parameters (frame,
11949 list1 (Fcons (Qtool_bar_lines,
11950 make_number (nlines))));
11951 if (WINDOW_TOTAL_LINES (w) != old_height)
11952 {
11953 clear_glyph_matrix (w->desired_matrix);
11954 f->fonts_changed = 1;
11955 return 1;
11956 }
11957 }
11958 }
11959
11960 /* Display as many lines as needed to display all tool-bar items. */
11961
11962 if (f->n_tool_bar_rows > 0)
11963 {
11964 int border, rows, height, extra;
11965
11966 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11967 border = XINT (Vtool_bar_border);
11968 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11969 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11970 else if (EQ (Vtool_bar_border, Qborder_width))
11971 border = f->border_width;
11972 else
11973 border = 0;
11974 if (border < 0)
11975 border = 0;
11976
11977 rows = f->n_tool_bar_rows;
11978 height = max (1, (it.last_visible_y - border) / rows);
11979 extra = it.last_visible_y - border - height * rows;
11980
11981 while (it.current_y < it.last_visible_y)
11982 {
11983 int h = 0;
11984 if (extra > 0 && rows-- > 0)
11985 {
11986 h = (extra + rows - 1) / rows;
11987 extra -= h;
11988 }
11989 display_tool_bar_line (&it, height + h);
11990 }
11991 }
11992 else
11993 {
11994 while (it.current_y < it.last_visible_y)
11995 display_tool_bar_line (&it, 0);
11996 }
11997
11998 /* It doesn't make much sense to try scrolling in the tool-bar
11999 window, so don't do it. */
12000 w->desired_matrix->no_scrolling_p = 1;
12001 w->must_be_updated_p = 1;
12002
12003 if (!NILP (Vauto_resize_tool_bars))
12004 {
12005 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12006 int change_height_p = 0;
12007
12008 /* If we couldn't display everything, change the tool-bar's
12009 height if there is room for more. */
12010 if (IT_STRING_CHARPOS (it) < it.end_charpos
12011 && it.current_y < max_tool_bar_height)
12012 change_height_p = 1;
12013
12014 row = it.glyph_row - 1;
12015
12016 /* If there are blank lines at the end, except for a partially
12017 visible blank line at the end that is smaller than
12018 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12019 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12020 && row->height >= FRAME_LINE_HEIGHT (f))
12021 change_height_p = 1;
12022
12023 /* If row displays tool-bar items, but is partially visible,
12024 change the tool-bar's height. */
12025 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12026 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12027 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12028 change_height_p = 1;
12029
12030 /* Resize windows as needed by changing the `tool-bar-lines'
12031 frame parameter. */
12032 if (change_height_p)
12033 {
12034 Lisp_Object frame;
12035 int old_height = WINDOW_TOTAL_LINES (w);
12036 int nrows;
12037 int nlines = tool_bar_lines_needed (f, &nrows);
12038
12039 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12040 && !f->minimize_tool_bar_window_p)
12041 ? (nlines > old_height)
12042 : (nlines != old_height));
12043 f->minimize_tool_bar_window_p = 0;
12044
12045 if (change_height_p)
12046 {
12047 XSETFRAME (frame, f);
12048 Fmodify_frame_parameters (frame,
12049 list1 (Fcons (Qtool_bar_lines,
12050 make_number (nlines))));
12051 if (WINDOW_TOTAL_LINES (w) != old_height)
12052 {
12053 clear_glyph_matrix (w->desired_matrix);
12054 f->n_tool_bar_rows = nrows;
12055 f->fonts_changed = 1;
12056 return 1;
12057 }
12058 }
12059 }
12060 }
12061
12062 f->minimize_tool_bar_window_p = 0;
12063 return 0;
12064
12065 #endif /* USE_GTK || HAVE_NS */
12066 }
12067
12068 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12069
12070 /* Get information about the tool-bar item which is displayed in GLYPH
12071 on frame F. Return in *PROP_IDX the index where tool-bar item
12072 properties start in F->tool_bar_items. Value is zero if
12073 GLYPH doesn't display a tool-bar item. */
12074
12075 static int
12076 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12077 {
12078 Lisp_Object prop;
12079 int success_p;
12080 int charpos;
12081
12082 /* This function can be called asynchronously, which means we must
12083 exclude any possibility that Fget_text_property signals an
12084 error. */
12085 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12086 charpos = max (0, charpos);
12087
12088 /* Get the text property `menu-item' at pos. The value of that
12089 property is the start index of this item's properties in
12090 F->tool_bar_items. */
12091 prop = Fget_text_property (make_number (charpos),
12092 Qmenu_item, f->current_tool_bar_string);
12093 if (INTEGERP (prop))
12094 {
12095 *prop_idx = XINT (prop);
12096 success_p = 1;
12097 }
12098 else
12099 success_p = 0;
12100
12101 return success_p;
12102 }
12103
12104 \f
12105 /* Get information about the tool-bar item at position X/Y on frame F.
12106 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12107 the current matrix of the tool-bar window of F, or NULL if not
12108 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12109 item in F->tool_bar_items. Value is
12110
12111 -1 if X/Y is not on a tool-bar item
12112 0 if X/Y is on the same item that was highlighted before.
12113 1 otherwise. */
12114
12115 static int
12116 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12117 int *hpos, int *vpos, int *prop_idx)
12118 {
12119 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12120 struct window *w = XWINDOW (f->tool_bar_window);
12121 int area;
12122
12123 /* Find the glyph under X/Y. */
12124 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12125 if (*glyph == NULL)
12126 return -1;
12127
12128 /* Get the start of this tool-bar item's properties in
12129 f->tool_bar_items. */
12130 if (!tool_bar_item_info (f, *glyph, prop_idx))
12131 return -1;
12132
12133 /* Is mouse on the highlighted item? */
12134 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12135 && *vpos >= hlinfo->mouse_face_beg_row
12136 && *vpos <= hlinfo->mouse_face_end_row
12137 && (*vpos > hlinfo->mouse_face_beg_row
12138 || *hpos >= hlinfo->mouse_face_beg_col)
12139 && (*vpos < hlinfo->mouse_face_end_row
12140 || *hpos < hlinfo->mouse_face_end_col
12141 || hlinfo->mouse_face_past_end))
12142 return 0;
12143
12144 return 1;
12145 }
12146
12147
12148 /* EXPORT:
12149 Handle mouse button event on the tool-bar of frame F, at
12150 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12151 0 for button release. MODIFIERS is event modifiers for button
12152 release. */
12153
12154 void
12155 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12156 int modifiers)
12157 {
12158 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12159 struct window *w = XWINDOW (f->tool_bar_window);
12160 int hpos, vpos, prop_idx;
12161 struct glyph *glyph;
12162 Lisp_Object enabled_p;
12163 int ts;
12164
12165 /* If not on the highlighted tool-bar item, and mouse-highlight is
12166 non-nil, return. This is so we generate the tool-bar button
12167 click only when the mouse button is released on the same item as
12168 where it was pressed. However, when mouse-highlight is disabled,
12169 generate the click when the button is released regardless of the
12170 highlight, since tool-bar items are not highlighted in that
12171 case. */
12172 frame_to_window_pixel_xy (w, &x, &y);
12173 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12174 if (ts == -1
12175 || (ts != 0 && !NILP (Vmouse_highlight)))
12176 return;
12177
12178 /* When mouse-highlight is off, generate the click for the item
12179 where the button was pressed, disregarding where it was
12180 released. */
12181 if (NILP (Vmouse_highlight) && !down_p)
12182 prop_idx = last_tool_bar_item;
12183
12184 /* If item is disabled, do nothing. */
12185 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12186 if (NILP (enabled_p))
12187 return;
12188
12189 if (down_p)
12190 {
12191 /* Show item in pressed state. */
12192 if (!NILP (Vmouse_highlight))
12193 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12194 last_tool_bar_item = prop_idx;
12195 }
12196 else
12197 {
12198 Lisp_Object key, frame;
12199 struct input_event event;
12200 EVENT_INIT (event);
12201
12202 /* Show item in released state. */
12203 if (!NILP (Vmouse_highlight))
12204 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12205
12206 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12207
12208 XSETFRAME (frame, f);
12209 event.kind = TOOL_BAR_EVENT;
12210 event.frame_or_window = frame;
12211 event.arg = frame;
12212 kbd_buffer_store_event (&event);
12213
12214 event.kind = TOOL_BAR_EVENT;
12215 event.frame_or_window = frame;
12216 event.arg = key;
12217 event.modifiers = modifiers;
12218 kbd_buffer_store_event (&event);
12219 last_tool_bar_item = -1;
12220 }
12221 }
12222
12223
12224 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12225 tool-bar window-relative coordinates X/Y. Called from
12226 note_mouse_highlight. */
12227
12228 static void
12229 note_tool_bar_highlight (struct frame *f, int x, int y)
12230 {
12231 Lisp_Object window = f->tool_bar_window;
12232 struct window *w = XWINDOW (window);
12233 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12234 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12235 int hpos, vpos;
12236 struct glyph *glyph;
12237 struct glyph_row *row;
12238 int i;
12239 Lisp_Object enabled_p;
12240 int prop_idx;
12241 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12242 int mouse_down_p, rc;
12243
12244 /* Function note_mouse_highlight is called with negative X/Y
12245 values when mouse moves outside of the frame. */
12246 if (x <= 0 || y <= 0)
12247 {
12248 clear_mouse_face (hlinfo);
12249 return;
12250 }
12251
12252 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12253 if (rc < 0)
12254 {
12255 /* Not on tool-bar item. */
12256 clear_mouse_face (hlinfo);
12257 return;
12258 }
12259 else if (rc == 0)
12260 /* On same tool-bar item as before. */
12261 goto set_help_echo;
12262
12263 clear_mouse_face (hlinfo);
12264
12265 /* Mouse is down, but on different tool-bar item? */
12266 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12267 && f == dpyinfo->last_mouse_frame);
12268
12269 if (mouse_down_p
12270 && last_tool_bar_item != prop_idx)
12271 return;
12272
12273 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12274
12275 /* If tool-bar item is not enabled, don't highlight it. */
12276 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12277 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12278 {
12279 /* Compute the x-position of the glyph. In front and past the
12280 image is a space. We include this in the highlighted area. */
12281 row = MATRIX_ROW (w->current_matrix, vpos);
12282 for (i = x = 0; i < hpos; ++i)
12283 x += row->glyphs[TEXT_AREA][i].pixel_width;
12284
12285 /* Record this as the current active region. */
12286 hlinfo->mouse_face_beg_col = hpos;
12287 hlinfo->mouse_face_beg_row = vpos;
12288 hlinfo->mouse_face_beg_x = x;
12289 hlinfo->mouse_face_past_end = 0;
12290
12291 hlinfo->mouse_face_end_col = hpos + 1;
12292 hlinfo->mouse_face_end_row = vpos;
12293 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12294 hlinfo->mouse_face_window = window;
12295 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12296
12297 /* Display it as active. */
12298 show_mouse_face (hlinfo, draw);
12299 }
12300
12301 set_help_echo:
12302
12303 /* Set help_echo_string to a help string to display for this tool-bar item.
12304 XTread_socket does the rest. */
12305 help_echo_object = help_echo_window = Qnil;
12306 help_echo_pos = -1;
12307 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12308 if (NILP (help_echo_string))
12309 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12310 }
12311
12312 #endif /* !USE_GTK && !HAVE_NS */
12313
12314 #endif /* HAVE_WINDOW_SYSTEM */
12315
12316
12317 \f
12318 /************************************************************************
12319 Horizontal scrolling
12320 ************************************************************************/
12321
12322 static int hscroll_window_tree (Lisp_Object);
12323 static int hscroll_windows (Lisp_Object);
12324
12325 /* For all leaf windows in the window tree rooted at WINDOW, set their
12326 hscroll value so that PT is (i) visible in the window, and (ii) so
12327 that it is not within a certain margin at the window's left and
12328 right border. Value is non-zero if any window's hscroll has been
12329 changed. */
12330
12331 static int
12332 hscroll_window_tree (Lisp_Object window)
12333 {
12334 int hscrolled_p = 0;
12335 int hscroll_relative_p = FLOATP (Vhscroll_step);
12336 int hscroll_step_abs = 0;
12337 double hscroll_step_rel = 0;
12338
12339 if (hscroll_relative_p)
12340 {
12341 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12342 if (hscroll_step_rel < 0)
12343 {
12344 hscroll_relative_p = 0;
12345 hscroll_step_abs = 0;
12346 }
12347 }
12348 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12349 {
12350 hscroll_step_abs = XINT (Vhscroll_step);
12351 if (hscroll_step_abs < 0)
12352 hscroll_step_abs = 0;
12353 }
12354 else
12355 hscroll_step_abs = 0;
12356
12357 while (WINDOWP (window))
12358 {
12359 struct window *w = XWINDOW (window);
12360
12361 if (WINDOWP (w->contents))
12362 hscrolled_p |= hscroll_window_tree (w->contents);
12363 else if (w->cursor.vpos >= 0)
12364 {
12365 int h_margin;
12366 int text_area_width;
12367 struct glyph_row *current_cursor_row
12368 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12369 struct glyph_row *desired_cursor_row
12370 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12371 struct glyph_row *cursor_row
12372 = (desired_cursor_row->enabled_p
12373 ? desired_cursor_row
12374 : current_cursor_row);
12375 int row_r2l_p = cursor_row->reversed_p;
12376
12377 text_area_width = window_box_width (w, TEXT_AREA);
12378
12379 /* Scroll when cursor is inside this scroll margin. */
12380 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12381
12382 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12383 /* For left-to-right rows, hscroll when cursor is either
12384 (i) inside the right hscroll margin, or (ii) if it is
12385 inside the left margin and the window is already
12386 hscrolled. */
12387 && ((!row_r2l_p
12388 && ((w->hscroll
12389 && w->cursor.x <= h_margin)
12390 || (cursor_row->enabled_p
12391 && cursor_row->truncated_on_right_p
12392 && (w->cursor.x >= text_area_width - h_margin))))
12393 /* For right-to-left rows, the logic is similar,
12394 except that rules for scrolling to left and right
12395 are reversed. E.g., if cursor.x <= h_margin, we
12396 need to hscroll "to the right" unconditionally,
12397 and that will scroll the screen to the left so as
12398 to reveal the next portion of the row. */
12399 || (row_r2l_p
12400 && ((cursor_row->enabled_p
12401 /* FIXME: It is confusing to set the
12402 truncated_on_right_p flag when R2L rows
12403 are actually truncated on the left. */
12404 && cursor_row->truncated_on_right_p
12405 && w->cursor.x <= h_margin)
12406 || (w->hscroll
12407 && (w->cursor.x >= text_area_width - h_margin))))))
12408 {
12409 struct it it;
12410 ptrdiff_t hscroll;
12411 struct buffer *saved_current_buffer;
12412 ptrdiff_t pt;
12413 int wanted_x;
12414
12415 /* Find point in a display of infinite width. */
12416 saved_current_buffer = current_buffer;
12417 current_buffer = XBUFFER (w->contents);
12418
12419 if (w == XWINDOW (selected_window))
12420 pt = PT;
12421 else
12422 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12423
12424 /* Move iterator to pt starting at cursor_row->start in
12425 a line with infinite width. */
12426 init_to_row_start (&it, w, cursor_row);
12427 it.last_visible_x = INFINITY;
12428 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12429 current_buffer = saved_current_buffer;
12430
12431 /* Position cursor in window. */
12432 if (!hscroll_relative_p && hscroll_step_abs == 0)
12433 hscroll = max (0, (it.current_x
12434 - (ITERATOR_AT_END_OF_LINE_P (&it)
12435 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12436 : (text_area_width / 2))))
12437 / FRAME_COLUMN_WIDTH (it.f);
12438 else if ((!row_r2l_p
12439 && w->cursor.x >= text_area_width - h_margin)
12440 || (row_r2l_p && w->cursor.x <= h_margin))
12441 {
12442 if (hscroll_relative_p)
12443 wanted_x = text_area_width * (1 - hscroll_step_rel)
12444 - h_margin;
12445 else
12446 wanted_x = text_area_width
12447 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12448 - h_margin;
12449 hscroll
12450 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12451 }
12452 else
12453 {
12454 if (hscroll_relative_p)
12455 wanted_x = text_area_width * hscroll_step_rel
12456 + h_margin;
12457 else
12458 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12459 + h_margin;
12460 hscroll
12461 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12462 }
12463 hscroll = max (hscroll, w->min_hscroll);
12464
12465 /* Don't prevent redisplay optimizations if hscroll
12466 hasn't changed, as it will unnecessarily slow down
12467 redisplay. */
12468 if (w->hscroll != hscroll)
12469 {
12470 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12471 w->hscroll = hscroll;
12472 hscrolled_p = 1;
12473 }
12474 }
12475 }
12476
12477 window = w->next;
12478 }
12479
12480 /* Value is non-zero if hscroll of any leaf window has been changed. */
12481 return hscrolled_p;
12482 }
12483
12484
12485 /* Set hscroll so that cursor is visible and not inside horizontal
12486 scroll margins for all windows in the tree rooted at WINDOW. See
12487 also hscroll_window_tree above. Value is non-zero if any window's
12488 hscroll has been changed. If it has, desired matrices on the frame
12489 of WINDOW are cleared. */
12490
12491 static int
12492 hscroll_windows (Lisp_Object window)
12493 {
12494 int hscrolled_p = hscroll_window_tree (window);
12495 if (hscrolled_p)
12496 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12497 return hscrolled_p;
12498 }
12499
12500
12501 \f
12502 /************************************************************************
12503 Redisplay
12504 ************************************************************************/
12505
12506 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12507 to a non-zero value. This is sometimes handy to have in a debugger
12508 session. */
12509
12510 #ifdef GLYPH_DEBUG
12511
12512 /* First and last unchanged row for try_window_id. */
12513
12514 static int debug_first_unchanged_at_end_vpos;
12515 static int debug_last_unchanged_at_beg_vpos;
12516
12517 /* Delta vpos and y. */
12518
12519 static int debug_dvpos, debug_dy;
12520
12521 /* Delta in characters and bytes for try_window_id. */
12522
12523 static ptrdiff_t debug_delta, debug_delta_bytes;
12524
12525 /* Values of window_end_pos and window_end_vpos at the end of
12526 try_window_id. */
12527
12528 static ptrdiff_t debug_end_vpos;
12529
12530 /* Append a string to W->desired_matrix->method. FMT is a printf
12531 format string. If trace_redisplay_p is non-zero also printf the
12532 resulting string to stderr. */
12533
12534 static void debug_method_add (struct window *, char const *, ...)
12535 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12536
12537 static void
12538 debug_method_add (struct window *w, char const *fmt, ...)
12539 {
12540 void *ptr = w;
12541 char *method = w->desired_matrix->method;
12542 int len = strlen (method);
12543 int size = sizeof w->desired_matrix->method;
12544 int remaining = size - len - 1;
12545 va_list ap;
12546
12547 if (len && remaining)
12548 {
12549 method[len] = '|';
12550 --remaining, ++len;
12551 }
12552
12553 va_start (ap, fmt);
12554 vsnprintf (method + len, remaining + 1, fmt, ap);
12555 va_end (ap);
12556
12557 if (trace_redisplay_p)
12558 fprintf (stderr, "%p (%s): %s\n",
12559 ptr,
12560 ((BUFFERP (w->contents)
12561 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12562 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12563 : "no buffer"),
12564 method + len);
12565 }
12566
12567 #endif /* GLYPH_DEBUG */
12568
12569
12570 /* Value is non-zero if all changes in window W, which displays
12571 current_buffer, are in the text between START and END. START is a
12572 buffer position, END is given as a distance from Z. Used in
12573 redisplay_internal for display optimization. */
12574
12575 static int
12576 text_outside_line_unchanged_p (struct window *w,
12577 ptrdiff_t start, ptrdiff_t end)
12578 {
12579 int unchanged_p = 1;
12580
12581 /* If text or overlays have changed, see where. */
12582 if (window_outdated (w))
12583 {
12584 /* Gap in the line? */
12585 if (GPT < start || Z - GPT < end)
12586 unchanged_p = 0;
12587
12588 /* Changes start in front of the line, or end after it? */
12589 if (unchanged_p
12590 && (BEG_UNCHANGED < start - 1
12591 || END_UNCHANGED < end))
12592 unchanged_p = 0;
12593
12594 /* If selective display, can't optimize if changes start at the
12595 beginning of the line. */
12596 if (unchanged_p
12597 && INTEGERP (BVAR (current_buffer, selective_display))
12598 && XINT (BVAR (current_buffer, selective_display)) > 0
12599 && (BEG_UNCHANGED < start || GPT <= start))
12600 unchanged_p = 0;
12601
12602 /* If there are overlays at the start or end of the line, these
12603 may have overlay strings with newlines in them. A change at
12604 START, for instance, may actually concern the display of such
12605 overlay strings as well, and they are displayed on different
12606 lines. So, quickly rule out this case. (For the future, it
12607 might be desirable to implement something more telling than
12608 just BEG/END_UNCHANGED.) */
12609 if (unchanged_p)
12610 {
12611 if (BEG + BEG_UNCHANGED == start
12612 && overlay_touches_p (start))
12613 unchanged_p = 0;
12614 if (END_UNCHANGED == end
12615 && overlay_touches_p (Z - end))
12616 unchanged_p = 0;
12617 }
12618
12619 /* Under bidi reordering, adding or deleting a character in the
12620 beginning of a paragraph, before the first strong directional
12621 character, can change the base direction of the paragraph (unless
12622 the buffer specifies a fixed paragraph direction), which will
12623 require to redisplay the whole paragraph. It might be worthwhile
12624 to find the paragraph limits and widen the range of redisplayed
12625 lines to that, but for now just give up this optimization. */
12626 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12627 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12628 unchanged_p = 0;
12629 }
12630
12631 return unchanged_p;
12632 }
12633
12634
12635 /* Do a frame update, taking possible shortcuts into account. This is
12636 the main external entry point for redisplay.
12637
12638 If the last redisplay displayed an echo area message and that message
12639 is no longer requested, we clear the echo area or bring back the
12640 mini-buffer if that is in use. */
12641
12642 void
12643 redisplay (void)
12644 {
12645 redisplay_internal ();
12646 }
12647
12648
12649 static Lisp_Object
12650 overlay_arrow_string_or_property (Lisp_Object var)
12651 {
12652 Lisp_Object val;
12653
12654 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12655 return val;
12656
12657 return Voverlay_arrow_string;
12658 }
12659
12660 /* Return 1 if there are any overlay-arrows in current_buffer. */
12661 static int
12662 overlay_arrow_in_current_buffer_p (void)
12663 {
12664 Lisp_Object vlist;
12665
12666 for (vlist = Voverlay_arrow_variable_list;
12667 CONSP (vlist);
12668 vlist = XCDR (vlist))
12669 {
12670 Lisp_Object var = XCAR (vlist);
12671 Lisp_Object val;
12672
12673 if (!SYMBOLP (var))
12674 continue;
12675 val = find_symbol_value (var);
12676 if (MARKERP (val)
12677 && current_buffer == XMARKER (val)->buffer)
12678 return 1;
12679 }
12680 return 0;
12681 }
12682
12683
12684 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12685 has changed. */
12686
12687 static int
12688 overlay_arrows_changed_p (void)
12689 {
12690 Lisp_Object vlist;
12691
12692 for (vlist = Voverlay_arrow_variable_list;
12693 CONSP (vlist);
12694 vlist = XCDR (vlist))
12695 {
12696 Lisp_Object var = XCAR (vlist);
12697 Lisp_Object val, pstr;
12698
12699 if (!SYMBOLP (var))
12700 continue;
12701 val = find_symbol_value (var);
12702 if (!MARKERP (val))
12703 continue;
12704 if (! EQ (COERCE_MARKER (val),
12705 Fget (var, Qlast_arrow_position))
12706 || ! (pstr = overlay_arrow_string_or_property (var),
12707 EQ (pstr, Fget (var, Qlast_arrow_string))))
12708 return 1;
12709 }
12710 return 0;
12711 }
12712
12713 /* Mark overlay arrows to be updated on next redisplay. */
12714
12715 static void
12716 update_overlay_arrows (int up_to_date)
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
12726 if (!SYMBOLP (var))
12727 continue;
12728
12729 if (up_to_date > 0)
12730 {
12731 Lisp_Object val = find_symbol_value (var);
12732 Fput (var, Qlast_arrow_position,
12733 COERCE_MARKER (val));
12734 Fput (var, Qlast_arrow_string,
12735 overlay_arrow_string_or_property (var));
12736 }
12737 else if (up_to_date < 0
12738 || !NILP (Fget (var, Qlast_arrow_position)))
12739 {
12740 Fput (var, Qlast_arrow_position, Qt);
12741 Fput (var, Qlast_arrow_string, Qt);
12742 }
12743 }
12744 }
12745
12746
12747 /* Return overlay arrow string to display at row.
12748 Return integer (bitmap number) for arrow bitmap in left fringe.
12749 Return nil if no overlay arrow. */
12750
12751 static Lisp_Object
12752 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12753 {
12754 Lisp_Object vlist;
12755
12756 for (vlist = Voverlay_arrow_variable_list;
12757 CONSP (vlist);
12758 vlist = XCDR (vlist))
12759 {
12760 Lisp_Object var = XCAR (vlist);
12761 Lisp_Object val;
12762
12763 if (!SYMBOLP (var))
12764 continue;
12765
12766 val = find_symbol_value (var);
12767
12768 if (MARKERP (val)
12769 && current_buffer == XMARKER (val)->buffer
12770 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12771 {
12772 if (FRAME_WINDOW_P (it->f)
12773 /* FIXME: if ROW->reversed_p is set, this should test
12774 the right fringe, not the left one. */
12775 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12776 {
12777 #ifdef HAVE_WINDOW_SYSTEM
12778 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12779 {
12780 int fringe_bitmap;
12781 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12782 return make_number (fringe_bitmap);
12783 }
12784 #endif
12785 return make_number (-1); /* Use default arrow bitmap. */
12786 }
12787 return overlay_arrow_string_or_property (var);
12788 }
12789 }
12790
12791 return Qnil;
12792 }
12793
12794 /* Return 1 if point moved out of or into a composition. Otherwise
12795 return 0. PREV_BUF and PREV_PT are the last point buffer and
12796 position. BUF and PT are the current point buffer and position. */
12797
12798 static int
12799 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12800 struct buffer *buf, ptrdiff_t pt)
12801 {
12802 ptrdiff_t start, end;
12803 Lisp_Object prop;
12804 Lisp_Object buffer;
12805
12806 XSETBUFFER (buffer, buf);
12807 /* Check a composition at the last point if point moved within the
12808 same buffer. */
12809 if (prev_buf == buf)
12810 {
12811 if (prev_pt == pt)
12812 /* Point didn't move. */
12813 return 0;
12814
12815 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12816 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12817 && composition_valid_p (start, end, prop)
12818 && start < prev_pt && end > prev_pt)
12819 /* The last point was within the composition. Return 1 iff
12820 point moved out of the composition. */
12821 return (pt <= start || pt >= end);
12822 }
12823
12824 /* Check a composition at the current point. */
12825 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12826 && find_composition (pt, -1, &start, &end, &prop, buffer)
12827 && composition_valid_p (start, end, prop)
12828 && start < pt && end > pt);
12829 }
12830
12831 /* Reconsider the clip changes of buffer which is displayed in W. */
12832
12833 static void
12834 reconsider_clip_changes (struct window *w)
12835 {
12836 struct buffer *b = XBUFFER (w->contents);
12837
12838 if (b->clip_changed
12839 && w->window_end_valid
12840 && w->current_matrix->buffer == b
12841 && w->current_matrix->zv == BUF_ZV (b)
12842 && w->current_matrix->begv == BUF_BEGV (b))
12843 b->clip_changed = 0;
12844
12845 /* If display wasn't paused, and W is not a tool bar window, see if
12846 point has been moved into or out of a composition. In that case,
12847 we set b->clip_changed to 1 to force updating the screen. If
12848 b->clip_changed has already been set to 1, we can skip this
12849 check. */
12850 if (!b->clip_changed && w->window_end_valid)
12851 {
12852 ptrdiff_t pt = (w == XWINDOW (selected_window)
12853 ? PT : marker_position (w->pointm));
12854
12855 if ((w->current_matrix->buffer != b || pt != w->last_point)
12856 && check_point_in_composition (w->current_matrix->buffer,
12857 w->last_point, b, pt))
12858 b->clip_changed = 1;
12859 }
12860 }
12861
12862 #define STOP_POLLING \
12863 do { if (! polling_stopped_here) stop_polling (); \
12864 polling_stopped_here = 1; } while (0)
12865
12866 #define RESUME_POLLING \
12867 do { if (polling_stopped_here) start_polling (); \
12868 polling_stopped_here = 0; } while (0)
12869
12870
12871 /* Perhaps in the future avoid recentering windows if it
12872 is not necessary; currently that causes some problems. */
12873
12874 static void
12875 redisplay_internal (void)
12876 {
12877 struct window *w = XWINDOW (selected_window);
12878 struct window *sw;
12879 struct frame *fr;
12880 int pending;
12881 bool must_finish = 0, match_p;
12882 struct text_pos tlbufpos, tlendpos;
12883 int number_of_visible_frames;
12884 ptrdiff_t count;
12885 struct frame *sf;
12886 int polling_stopped_here = 0;
12887 Lisp_Object tail, frame;
12888
12889 /* True means redisplay has to consider all windows on all
12890 frames. False, only selected_window is considered. */
12891 bool consider_all_windows_p;
12892
12893 /* True means redisplay has to redisplay the miniwindow. */
12894 bool update_miniwindow_p = false;
12895
12896 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12897
12898 /* No redisplay if running in batch mode or frame is not yet fully
12899 initialized, or redisplay is explicitly turned off by setting
12900 Vinhibit_redisplay. */
12901 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12902 || !NILP (Vinhibit_redisplay))
12903 return;
12904
12905 /* Don't examine these until after testing Vinhibit_redisplay.
12906 When Emacs is shutting down, perhaps because its connection to
12907 X has dropped, we should not look at them at all. */
12908 fr = XFRAME (w->frame);
12909 sf = SELECTED_FRAME ();
12910
12911 if (!fr->glyphs_initialized_p)
12912 return;
12913
12914 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12915 if (popup_activated ())
12916 return;
12917 #endif
12918
12919 /* I don't think this happens but let's be paranoid. */
12920 if (redisplaying_p)
12921 return;
12922
12923 /* Record a function that clears redisplaying_p
12924 when we leave this function. */
12925 count = SPECPDL_INDEX ();
12926 record_unwind_protect_void (unwind_redisplay);
12927 redisplaying_p = 1;
12928 specbind (Qinhibit_free_realized_faces, Qnil);
12929
12930 /* Record this function, so it appears on the profiler's backtraces. */
12931 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12932
12933 FOR_EACH_FRAME (tail, frame)
12934 XFRAME (frame)->already_hscrolled_p = 0;
12935
12936 retry:
12937 /* Remember the currently selected window. */
12938 sw = w;
12939
12940 pending = 0;
12941 last_escape_glyph_frame = NULL;
12942 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12943 last_glyphless_glyph_frame = NULL;
12944 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12945
12946 /* If face_change_count is non-zero, init_iterator will free all
12947 realized faces, which includes the faces referenced from current
12948 matrices. So, we can't reuse current matrices in this case. */
12949 if (face_change_count)
12950 windows_or_buffers_changed = 47;
12951
12952 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12953 && FRAME_TTY (sf)->previous_frame != sf)
12954 {
12955 /* Since frames on a single ASCII terminal share the same
12956 display area, displaying a different frame means redisplay
12957 the whole thing. */
12958 windows_or_buffers_changed = 48;
12959 SET_FRAME_GARBAGED (sf);
12960 #ifndef DOS_NT
12961 set_tty_color_mode (FRAME_TTY (sf), sf);
12962 #endif
12963 FRAME_TTY (sf)->previous_frame = sf;
12964 }
12965
12966 /* Set the visible flags for all frames. Do this before checking for
12967 resized or garbaged frames; they want to know if their frames are
12968 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
12969 number_of_visible_frames = 0;
12970
12971 FOR_EACH_FRAME (tail, frame)
12972 {
12973 struct frame *f = XFRAME (frame);
12974
12975 if (FRAME_VISIBLE_P (f))
12976 {
12977 ++number_of_visible_frames;
12978 /* Adjust matrices for visible frames only. */
12979 if (f->fonts_changed)
12980 {
12981 adjust_frame_glyphs (f);
12982 f->fonts_changed = 0;
12983 }
12984 /* If cursor type has been changed on the frame
12985 other than selected, consider all frames. */
12986 if (f != sf && f->cursor_type_changed)
12987 update_mode_lines = 31;
12988 }
12989 clear_desired_matrices (f);
12990 }
12991
12992 /* Notice any pending interrupt request to change frame size. */
12993 do_pending_window_change (1);
12994
12995 /* do_pending_window_change could change the selected_window due to
12996 frame resizing which makes the selected window too small. */
12997 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12998 sw = w;
12999
13000 /* Clear frames marked as garbaged. */
13001 clear_garbaged_frames ();
13002
13003 /* Build menubar and tool-bar items. */
13004 if (NILP (Vmemory_full))
13005 prepare_menu_bars ();
13006
13007 if (windows_or_buffers_changed && !update_mode_lines)
13008 update_mode_lines = 32;
13009
13010 reconsider_clip_changes (w);
13011
13012 /* In most cases selected window displays current buffer. */
13013 match_p = XBUFFER (w->contents) == current_buffer;
13014 if (match_p)
13015 {
13016 /* Detect case that we need to write or remove a star in the mode line. */
13017 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13018 {
13019 w->update_mode_line = 1;
13020 if (buffer_shared_and_changed ())
13021 update_mode_lines = 33;
13022 }
13023
13024 if (mode_line_update_needed (w))
13025 w->update_mode_line = 1;
13026 }
13027
13028 consider_all_windows_p = (update_mode_lines
13029 || buffer_shared_and_changed ());
13030
13031 /* If specs for an arrow have changed, do thorough redisplay
13032 to ensure we remove any arrow that should no longer exist. */
13033 if (overlay_arrows_changed_p ())
13034 {
13035 consider_all_windows_p = true;
13036 windows_or_buffers_changed = 49;
13037 }
13038
13039 /* Normally the message* functions will have already displayed and
13040 updated the echo area, but the frame may have been trashed, or
13041 the update may have been preempted, so display the echo area
13042 again here. Checking message_cleared_p captures the case that
13043 the echo area should be cleared. */
13044 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13045 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13046 || (message_cleared_p
13047 && minibuf_level == 0
13048 /* If the mini-window is currently selected, this means the
13049 echo-area doesn't show through. */
13050 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13051 {
13052 int window_height_changed_p = echo_area_display (0);
13053
13054 if (message_cleared_p)
13055 update_miniwindow_p = true;
13056
13057 must_finish = 1;
13058
13059 /* If we don't display the current message, don't clear the
13060 message_cleared_p flag, because, if we did, we wouldn't clear
13061 the echo area in the next redisplay which doesn't preserve
13062 the echo area. */
13063 if (!display_last_displayed_message_p)
13064 message_cleared_p = 0;
13065
13066 if (window_height_changed_p)
13067 {
13068 consider_all_windows_p = true;
13069 update_mode_lines = 34;
13070 windows_or_buffers_changed = 50;
13071
13072 /* If window configuration was changed, frames may have been
13073 marked garbaged. Clear them or we will experience
13074 surprises wrt scrolling. */
13075 clear_garbaged_frames ();
13076 }
13077 }
13078 else if (EQ (selected_window, minibuf_window)
13079 && (current_buffer->clip_changed || window_outdated (w))
13080 && resize_mini_window (w, 0))
13081 {
13082 /* Resized active mini-window to fit the size of what it is
13083 showing if its contents might have changed. */
13084 must_finish = 1;
13085 /* FIXME: this causes all frames to be updated, which seems unnecessary
13086 since only the current frame needs to be considered. This function
13087 needs to be rewritten with two variables, consider_all_windows and
13088 consider_all_frames. */
13089 consider_all_windows_p = true;
13090 windows_or_buffers_changed = 51;
13091 update_mode_lines = 35;
13092
13093 /* If window configuration was changed, frames may have been
13094 marked garbaged. Clear them or we will experience
13095 surprises wrt scrolling. */
13096 clear_garbaged_frames ();
13097 }
13098
13099 if (VECTORP (Vredisplay__all_windows_cause)
13100 && windows_or_buffers_changed >= 0
13101 && windows_or_buffers_changed < ASIZE (Vredisplay__all_windows_cause)
13102 && INTEGERP (AREF (Vredisplay__all_windows_cause,
13103 windows_or_buffers_changed)))
13104 ASET (Vredisplay__all_windows_cause, windows_or_buffers_changed,
13105 make_number (1 + XINT (AREF (Vredisplay__all_windows_cause,
13106 windows_or_buffers_changed))));
13107
13108 if (VECTORP (Vredisplay__mode_lines_cause)
13109 && update_mode_lines >= 0
13110 && update_mode_lines < ASIZE (Vredisplay__mode_lines_cause)
13111 && INTEGERP (AREF (Vredisplay__mode_lines_cause,
13112 update_mode_lines)))
13113 ASET (Vredisplay__mode_lines_cause, update_mode_lines,
13114 make_number (1 + XINT (AREF (Vredisplay__mode_lines_cause,
13115 update_mode_lines))));
13116
13117 /* Optimize the case that only the line containing the cursor in the
13118 selected window has changed. Variables starting with this_ are
13119 set in display_line and record information about the line
13120 containing the cursor. */
13121 tlbufpos = this_line_start_pos;
13122 tlendpos = this_line_end_pos;
13123 if (!consider_all_windows_p
13124 && CHARPOS (tlbufpos) > 0
13125 && !w->update_mode_line
13126 && !current_buffer->clip_changed
13127 && !current_buffer->prevent_redisplay_optimizations_p
13128 && FRAME_VISIBLE_P (XFRAME (w->frame))
13129 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13130 && !XFRAME (w->frame)->cursor_type_changed
13131 /* Make sure recorded data applies to current buffer, etc. */
13132 && this_line_buffer == current_buffer
13133 && match_p
13134 && !w->force_start
13135 && !w->optional_new_start
13136 /* Point must be on the line that we have info recorded about. */
13137 && PT >= CHARPOS (tlbufpos)
13138 && PT <= Z - CHARPOS (tlendpos)
13139 /* All text outside that line, including its final newline,
13140 must be unchanged. */
13141 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13142 CHARPOS (tlendpos)))
13143 {
13144 if (CHARPOS (tlbufpos) > BEGV
13145 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13146 && (CHARPOS (tlbufpos) == ZV
13147 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13148 /* Former continuation line has disappeared by becoming empty. */
13149 goto cancel;
13150 else if (window_outdated (w) || MINI_WINDOW_P (w))
13151 {
13152 /* We have to handle the case of continuation around a
13153 wide-column character (see the comment in indent.c around
13154 line 1340).
13155
13156 For instance, in the following case:
13157
13158 -------- Insert --------
13159 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13160 J_I_ ==> J_I_ `^^' are cursors.
13161 ^^ ^^
13162 -------- --------
13163
13164 As we have to redraw the line above, we cannot use this
13165 optimization. */
13166
13167 struct it it;
13168 int line_height_before = this_line_pixel_height;
13169
13170 /* Note that start_display will handle the case that the
13171 line starting at tlbufpos is a continuation line. */
13172 start_display (&it, w, tlbufpos);
13173
13174 /* Implementation note: It this still necessary? */
13175 if (it.current_x != this_line_start_x)
13176 goto cancel;
13177
13178 TRACE ((stderr, "trying display optimization 1\n"));
13179 w->cursor.vpos = -1;
13180 overlay_arrow_seen = 0;
13181 it.vpos = this_line_vpos;
13182 it.current_y = this_line_y;
13183 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13184 display_line (&it);
13185
13186 /* If line contains point, is not continued,
13187 and ends at same distance from eob as before, we win. */
13188 if (w->cursor.vpos >= 0
13189 /* Line is not continued, otherwise this_line_start_pos
13190 would have been set to 0 in display_line. */
13191 && CHARPOS (this_line_start_pos)
13192 /* Line ends as before. */
13193 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13194 /* Line has same height as before. Otherwise other lines
13195 would have to be shifted up or down. */
13196 && this_line_pixel_height == line_height_before)
13197 {
13198 /* If this is not the window's last line, we must adjust
13199 the charstarts of the lines below. */
13200 if (it.current_y < it.last_visible_y)
13201 {
13202 struct glyph_row *row
13203 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13204 ptrdiff_t delta, delta_bytes;
13205
13206 /* We used to distinguish between two cases here,
13207 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13208 when the line ends in a newline or the end of the
13209 buffer's accessible portion. But both cases did
13210 the same, so they were collapsed. */
13211 delta = (Z
13212 - CHARPOS (tlendpos)
13213 - MATRIX_ROW_START_CHARPOS (row));
13214 delta_bytes = (Z_BYTE
13215 - BYTEPOS (tlendpos)
13216 - MATRIX_ROW_START_BYTEPOS (row));
13217
13218 increment_matrix_positions (w->current_matrix,
13219 this_line_vpos + 1,
13220 w->current_matrix->nrows,
13221 delta, delta_bytes);
13222 }
13223
13224 /* If this row displays text now but previously didn't,
13225 or vice versa, w->window_end_vpos may have to be
13226 adjusted. */
13227 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13228 {
13229 if (w->window_end_vpos < this_line_vpos)
13230 w->window_end_vpos = this_line_vpos;
13231 }
13232 else if (w->window_end_vpos == this_line_vpos
13233 && this_line_vpos > 0)
13234 w->window_end_vpos = this_line_vpos - 1;
13235 w->window_end_valid = 0;
13236
13237 /* Update hint: No need to try to scroll in update_window. */
13238 w->desired_matrix->no_scrolling_p = 1;
13239
13240 #ifdef GLYPH_DEBUG
13241 *w->desired_matrix->method = 0;
13242 debug_method_add (w, "optimization 1");
13243 #endif
13244 #ifdef HAVE_WINDOW_SYSTEM
13245 update_window_fringes (w, 0);
13246 #endif
13247 goto update;
13248 }
13249 else
13250 goto cancel;
13251 }
13252 else if (/* Cursor position hasn't changed. */
13253 PT == w->last_point
13254 /* Make sure the cursor was last displayed
13255 in this window. Otherwise we have to reposition it. */
13256 && 0 <= w->cursor.vpos
13257 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13258 {
13259 if (!must_finish)
13260 {
13261 do_pending_window_change (1);
13262 /* If selected_window changed, redisplay again. */
13263 if (WINDOWP (selected_window)
13264 && (w = XWINDOW (selected_window)) != sw)
13265 goto retry;
13266
13267 /* We used to always goto end_of_redisplay here, but this
13268 isn't enough if we have a blinking cursor. */
13269 if (w->cursor_off_p == w->last_cursor_off_p)
13270 goto end_of_redisplay;
13271 }
13272 goto update;
13273 }
13274 /* If highlighting the region, or if the cursor is in the echo area,
13275 then we can't just move the cursor. */
13276 else if (NILP (Vshow_trailing_whitespace)
13277 && !cursor_in_echo_area)
13278 {
13279 struct it it;
13280 struct glyph_row *row;
13281
13282 /* Skip from tlbufpos to PT and see where it is. Note that
13283 PT may be in invisible text. If so, we will end at the
13284 next visible position. */
13285 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13286 NULL, DEFAULT_FACE_ID);
13287 it.current_x = this_line_start_x;
13288 it.current_y = this_line_y;
13289 it.vpos = this_line_vpos;
13290
13291 /* The call to move_it_to stops in front of PT, but
13292 moves over before-strings. */
13293 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13294
13295 if (it.vpos == this_line_vpos
13296 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13297 row->enabled_p))
13298 {
13299 eassert (this_line_vpos == it.vpos);
13300 eassert (this_line_y == it.current_y);
13301 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13302 #ifdef GLYPH_DEBUG
13303 *w->desired_matrix->method = 0;
13304 debug_method_add (w, "optimization 3");
13305 #endif
13306 goto update;
13307 }
13308 else
13309 goto cancel;
13310 }
13311
13312 cancel:
13313 /* Text changed drastically or point moved off of line. */
13314 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13315 }
13316
13317 CHARPOS (this_line_start_pos) = 0;
13318 ++clear_face_cache_count;
13319 #ifdef HAVE_WINDOW_SYSTEM
13320 ++clear_image_cache_count;
13321 #endif
13322
13323 /* Build desired matrices, and update the display. If
13324 consider_all_windows_p is non-zero, do it for all windows on all
13325 frames. Otherwise do it for selected_window, only. */
13326
13327 if (consider_all_windows_p)
13328 {
13329 FOR_EACH_FRAME (tail, frame)
13330 XFRAME (frame)->updated_p = 0;
13331
13332 FOR_EACH_FRAME (tail, frame)
13333 {
13334 struct frame *f = XFRAME (frame);
13335
13336 /* We don't have to do anything for unselected terminal
13337 frames. */
13338 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13339 && !EQ (FRAME_TTY (f)->top_frame, frame))
13340 continue;
13341
13342 retry_frame:
13343
13344 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13345 {
13346 /* Mark all the scroll bars to be removed; we'll redeem
13347 the ones we want when we redisplay their windows. */
13348 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13349 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13350
13351 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13352 redisplay_windows (FRAME_ROOT_WINDOW (f));
13353
13354 /* The X error handler may have deleted that frame. */
13355 if (!FRAME_LIVE_P (f))
13356 continue;
13357
13358 /* Any scroll bars which redisplay_windows should have
13359 nuked should now go away. */
13360 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13361 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13362
13363 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13364 {
13365 /* If fonts changed on visible frame, display again. */
13366 if (f->fonts_changed)
13367 {
13368 adjust_frame_glyphs (f);
13369 f->fonts_changed = 0;
13370 goto retry_frame;
13371 }
13372
13373 /* See if we have to hscroll. */
13374 if (!f->already_hscrolled_p)
13375 {
13376 f->already_hscrolled_p = 1;
13377 if (hscroll_windows (f->root_window))
13378 goto retry_frame;
13379 }
13380
13381 /* Prevent various kinds of signals during display
13382 update. stdio is not robust about handling
13383 signals, which can cause an apparent I/O
13384 error. */
13385 if (interrupt_input)
13386 unrequest_sigio ();
13387 STOP_POLLING;
13388
13389 /* Mark windows on frame F to update. If we decide to
13390 update all frames but windows_or_buffers_changed is
13391 zero, we assume that only the windows that shows
13392 current buffer should be really updated. */
13393 set_window_update_flags
13394 (XWINDOW (f->root_window),
13395 (windows_or_buffers_changed ? NULL : current_buffer), 1);
13396 pending |= update_frame (f, 0, 0);
13397 f->cursor_type_changed = 0;
13398 f->updated_p = 1;
13399 }
13400 }
13401 }
13402
13403 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13404
13405 if (!pending)
13406 {
13407 /* Do the mark_window_display_accurate after all windows have
13408 been redisplayed because this call resets flags in buffers
13409 which are needed for proper redisplay. */
13410 FOR_EACH_FRAME (tail, frame)
13411 {
13412 struct frame *f = XFRAME (frame);
13413 if (f->updated_p)
13414 {
13415 mark_window_display_accurate (f->root_window, 1);
13416 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13417 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13418 }
13419 }
13420 }
13421 }
13422 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13423 {
13424 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13425 struct frame *mini_frame;
13426
13427 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13428 /* Use list_of_error, not Qerror, so that
13429 we catch only errors and don't run the debugger. */
13430 internal_condition_case_1 (redisplay_window_1, selected_window,
13431 list_of_error,
13432 redisplay_window_error);
13433 if (update_miniwindow_p)
13434 internal_condition_case_1 (redisplay_window_1, mini_window,
13435 list_of_error,
13436 redisplay_window_error);
13437
13438 /* Compare desired and current matrices, perform output. */
13439
13440 update:
13441 /* If fonts changed, display again. */
13442 if (sf->fonts_changed)
13443 goto retry;
13444
13445 /* Prevent various kinds of signals during display update.
13446 stdio is not robust about handling signals,
13447 which can cause an apparent I/O error. */
13448 if (interrupt_input)
13449 unrequest_sigio ();
13450 STOP_POLLING;
13451
13452 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13453 {
13454 if (hscroll_windows (selected_window))
13455 goto retry;
13456
13457 XWINDOW (selected_window)->must_be_updated_p = 1;
13458 pending = update_frame (sf, 0, 0);
13459 sf->cursor_type_changed = 0;
13460 }
13461
13462 /* We may have called echo_area_display at the top of this
13463 function. If the echo area is on another frame, that may
13464 have put text on a frame other than the selected one, so the
13465 above call to update_frame would not have caught it. Catch
13466 it here. */
13467 mini_window = FRAME_MINIBUF_WINDOW (sf);
13468 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13469
13470 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13471 {
13472 XWINDOW (mini_window)->must_be_updated_p = 1;
13473 pending |= update_frame (mini_frame, 0, 0);
13474 mini_frame->cursor_type_changed = 0;
13475 if (!pending && hscroll_windows (mini_window))
13476 goto retry;
13477 }
13478 }
13479
13480 /* If display was paused because of pending input, make sure we do a
13481 thorough update the next time. */
13482 if (pending)
13483 {
13484 /* Prevent the optimization at the beginning of
13485 redisplay_internal that tries a single-line update of the
13486 line containing the cursor in the selected window. */
13487 CHARPOS (this_line_start_pos) = 0;
13488
13489 /* Let the overlay arrow be updated the next time. */
13490 update_overlay_arrows (0);
13491
13492 /* If we pause after scrolling, some rows in the current
13493 matrices of some windows are not valid. */
13494 if (!WINDOW_FULL_WIDTH_P (w)
13495 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13496 update_mode_lines = 36;
13497 }
13498 else
13499 {
13500 if (!consider_all_windows_p)
13501 {
13502 /* This has already been done above if
13503 consider_all_windows_p is set. */
13504 mark_window_display_accurate_1 (w, 1);
13505
13506 /* Say overlay arrows are up to date. */
13507 update_overlay_arrows (1);
13508
13509 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13510 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13511 }
13512
13513 update_mode_lines = 0;
13514 windows_or_buffers_changed = 0;
13515 }
13516
13517 /* Start SIGIO interrupts coming again. Having them off during the
13518 code above makes it less likely one will discard output, but not
13519 impossible, since there might be stuff in the system buffer here.
13520 But it is much hairier to try to do anything about that. */
13521 if (interrupt_input)
13522 request_sigio ();
13523 RESUME_POLLING;
13524
13525 /* If a frame has become visible which was not before, redisplay
13526 again, so that we display it. Expose events for such a frame
13527 (which it gets when becoming visible) don't call the parts of
13528 redisplay constructing glyphs, so simply exposing a frame won't
13529 display anything in this case. So, we have to display these
13530 frames here explicitly. */
13531 if (!pending)
13532 {
13533 int new_count = 0;
13534
13535 FOR_EACH_FRAME (tail, frame)
13536 {
13537 int this_is_visible = 0;
13538
13539 if (XFRAME (frame)->visible)
13540 this_is_visible = 1;
13541
13542 if (this_is_visible)
13543 new_count++;
13544 }
13545
13546 if (new_count != number_of_visible_frames)
13547 windows_or_buffers_changed = 52;
13548 }
13549
13550 /* Change frame size now if a change is pending. */
13551 do_pending_window_change (1);
13552
13553 /* If we just did a pending size change, or have additional
13554 visible frames, or selected_window changed, redisplay again. */
13555 if ((windows_or_buffers_changed && !pending)
13556 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13557 goto retry;
13558
13559 /* Clear the face and image caches.
13560
13561 We used to do this only if consider_all_windows_p. But the cache
13562 needs to be cleared if a timer creates images in the current
13563 buffer (e.g. the test case in Bug#6230). */
13564
13565 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13566 {
13567 clear_face_cache (0);
13568 clear_face_cache_count = 0;
13569 }
13570
13571 #ifdef HAVE_WINDOW_SYSTEM
13572 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13573 {
13574 clear_image_caches (Qnil);
13575 clear_image_cache_count = 0;
13576 }
13577 #endif /* HAVE_WINDOW_SYSTEM */
13578
13579 end_of_redisplay:
13580 unbind_to (count, Qnil);
13581 RESUME_POLLING;
13582 }
13583
13584
13585 /* Redisplay, but leave alone any recent echo area message unless
13586 another message has been requested in its place.
13587
13588 This is useful in situations where you need to redisplay but no
13589 user action has occurred, making it inappropriate for the message
13590 area to be cleared. See tracking_off and
13591 wait_reading_process_output for examples of these situations.
13592
13593 FROM_WHERE is an integer saying from where this function was
13594 called. This is useful for debugging. */
13595
13596 void
13597 redisplay_preserve_echo_area (int from_where)
13598 {
13599 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13600
13601 if (!NILP (echo_area_buffer[1]))
13602 {
13603 /* We have a previously displayed message, but no current
13604 message. Redisplay the previous message. */
13605 display_last_displayed_message_p = 1;
13606 redisplay_internal ();
13607 display_last_displayed_message_p = 0;
13608 }
13609 else
13610 redisplay_internal ();
13611
13612 flush_frame (SELECTED_FRAME ());
13613 }
13614
13615
13616 /* Function registered with record_unwind_protect in redisplay_internal. */
13617
13618 static void
13619 unwind_redisplay (void)
13620 {
13621 redisplaying_p = 0;
13622 }
13623
13624
13625 /* Mark the display of leaf window W as accurate or inaccurate.
13626 If ACCURATE_P is non-zero mark display of W as accurate. If
13627 ACCURATE_P is zero, arrange for W to be redisplayed the next
13628 time redisplay_internal is called. */
13629
13630 static void
13631 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13632 {
13633 struct buffer *b = XBUFFER (w->contents);
13634
13635 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13636 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13637 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13638
13639 if (accurate_p)
13640 {
13641 b->clip_changed = 0;
13642 b->prevent_redisplay_optimizations_p = 0;
13643
13644 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13645 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13646 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13647 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13648
13649 w->current_matrix->buffer = b;
13650 w->current_matrix->begv = BUF_BEGV (b);
13651 w->current_matrix->zv = BUF_ZV (b);
13652
13653 w->last_cursor_vpos = w->cursor.vpos;
13654 w->last_cursor_off_p = w->cursor_off_p;
13655
13656 if (w == XWINDOW (selected_window))
13657 w->last_point = BUF_PT (b);
13658 else
13659 w->last_point = marker_position (w->pointm);
13660
13661 w->window_end_valid = 1;
13662 w->update_mode_line = 0;
13663 }
13664 }
13665
13666
13667 /* Mark the display of windows in the window tree rooted at WINDOW as
13668 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13669 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13670 be redisplayed the next time redisplay_internal is called. */
13671
13672 void
13673 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13674 {
13675 struct window *w;
13676
13677 for (; !NILP (window); window = w->next)
13678 {
13679 w = XWINDOW (window);
13680 if (WINDOWP (w->contents))
13681 mark_window_display_accurate (w->contents, accurate_p);
13682 else
13683 mark_window_display_accurate_1 (w, accurate_p);
13684 }
13685
13686 if (accurate_p)
13687 update_overlay_arrows (1);
13688 else
13689 /* Force a thorough redisplay the next time by setting
13690 last_arrow_position and last_arrow_string to t, which is
13691 unequal to any useful value of Voverlay_arrow_... */
13692 update_overlay_arrows (-1);
13693 }
13694
13695
13696 /* Return value in display table DP (Lisp_Char_Table *) for character
13697 C. Since a display table doesn't have any parent, we don't have to
13698 follow parent. Do not call this function directly but use the
13699 macro DISP_CHAR_VECTOR. */
13700
13701 Lisp_Object
13702 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13703 {
13704 Lisp_Object val;
13705
13706 if (ASCII_CHAR_P (c))
13707 {
13708 val = dp->ascii;
13709 if (SUB_CHAR_TABLE_P (val))
13710 val = XSUB_CHAR_TABLE (val)->contents[c];
13711 }
13712 else
13713 {
13714 Lisp_Object table;
13715
13716 XSETCHAR_TABLE (table, dp);
13717 val = char_table_ref (table, c);
13718 }
13719 if (NILP (val))
13720 val = dp->defalt;
13721 return val;
13722 }
13723
13724
13725 \f
13726 /***********************************************************************
13727 Window Redisplay
13728 ***********************************************************************/
13729
13730 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13731
13732 static void
13733 redisplay_windows (Lisp_Object window)
13734 {
13735 while (!NILP (window))
13736 {
13737 struct window *w = XWINDOW (window);
13738
13739 if (WINDOWP (w->contents))
13740 redisplay_windows (w->contents);
13741 else if (BUFFERP (w->contents))
13742 {
13743 displayed_buffer = XBUFFER (w->contents);
13744 /* Use list_of_error, not Qerror, so that
13745 we catch only errors and don't run the debugger. */
13746 internal_condition_case_1 (redisplay_window_0, window,
13747 list_of_error,
13748 redisplay_window_error);
13749 }
13750
13751 window = w->next;
13752 }
13753 }
13754
13755 static Lisp_Object
13756 redisplay_window_error (Lisp_Object ignore)
13757 {
13758 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13759 return Qnil;
13760 }
13761
13762 static Lisp_Object
13763 redisplay_window_0 (Lisp_Object window)
13764 {
13765 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13766 redisplay_window (window, 0);
13767 return Qnil;
13768 }
13769
13770 static Lisp_Object
13771 redisplay_window_1 (Lisp_Object window)
13772 {
13773 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13774 redisplay_window (window, 1);
13775 return Qnil;
13776 }
13777 \f
13778
13779 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13780 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13781 which positions recorded in ROW differ from current buffer
13782 positions.
13783
13784 Return 0 if cursor is not on this row, 1 otherwise. */
13785
13786 static int
13787 set_cursor_from_row (struct window *w, struct glyph_row *row,
13788 struct glyph_matrix *matrix,
13789 ptrdiff_t delta, ptrdiff_t delta_bytes,
13790 int dy, int dvpos)
13791 {
13792 struct glyph *glyph = row->glyphs[TEXT_AREA];
13793 struct glyph *end = glyph + row->used[TEXT_AREA];
13794 struct glyph *cursor = NULL;
13795 /* The last known character position in row. */
13796 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13797 int x = row->x;
13798 ptrdiff_t pt_old = PT - delta;
13799 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13800 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13801 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13802 /* A glyph beyond the edge of TEXT_AREA which we should never
13803 touch. */
13804 struct glyph *glyphs_end = end;
13805 /* Non-zero means we've found a match for cursor position, but that
13806 glyph has the avoid_cursor_p flag set. */
13807 int match_with_avoid_cursor = 0;
13808 /* Non-zero means we've seen at least one glyph that came from a
13809 display string. */
13810 int string_seen = 0;
13811 /* Largest and smallest buffer positions seen so far during scan of
13812 glyph row. */
13813 ptrdiff_t bpos_max = pos_before;
13814 ptrdiff_t bpos_min = pos_after;
13815 /* Last buffer position covered by an overlay string with an integer
13816 `cursor' property. */
13817 ptrdiff_t bpos_covered = 0;
13818 /* Non-zero means the display string on which to display the cursor
13819 comes from a text property, not from an overlay. */
13820 int string_from_text_prop = 0;
13821
13822 /* Don't even try doing anything if called for a mode-line or
13823 header-line row, since the rest of the code isn't prepared to
13824 deal with such calamities. */
13825 eassert (!row->mode_line_p);
13826 if (row->mode_line_p)
13827 return 0;
13828
13829 /* Skip over glyphs not having an object at the start and the end of
13830 the row. These are special glyphs like truncation marks on
13831 terminal frames. */
13832 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13833 {
13834 if (!row->reversed_p)
13835 {
13836 while (glyph < end
13837 && INTEGERP (glyph->object)
13838 && glyph->charpos < 0)
13839 {
13840 x += glyph->pixel_width;
13841 ++glyph;
13842 }
13843 while (end > glyph
13844 && INTEGERP ((end - 1)->object)
13845 /* CHARPOS is zero for blanks and stretch glyphs
13846 inserted by extend_face_to_end_of_line. */
13847 && (end - 1)->charpos <= 0)
13848 --end;
13849 glyph_before = glyph - 1;
13850 glyph_after = end;
13851 }
13852 else
13853 {
13854 struct glyph *g;
13855
13856 /* If the glyph row is reversed, we need to process it from back
13857 to front, so swap the edge pointers. */
13858 glyphs_end = end = glyph - 1;
13859 glyph += row->used[TEXT_AREA] - 1;
13860
13861 while (glyph > end + 1
13862 && INTEGERP (glyph->object)
13863 && glyph->charpos < 0)
13864 {
13865 --glyph;
13866 x -= glyph->pixel_width;
13867 }
13868 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13869 --glyph;
13870 /* By default, in reversed rows we put the cursor on the
13871 rightmost (first in the reading order) glyph. */
13872 for (g = end + 1; g < glyph; g++)
13873 x += g->pixel_width;
13874 while (end < glyph
13875 && INTEGERP ((end + 1)->object)
13876 && (end + 1)->charpos <= 0)
13877 ++end;
13878 glyph_before = glyph + 1;
13879 glyph_after = end;
13880 }
13881 }
13882 else if (row->reversed_p)
13883 {
13884 /* In R2L rows that don't display text, put the cursor on the
13885 rightmost glyph. Case in point: an empty last line that is
13886 part of an R2L paragraph. */
13887 cursor = end - 1;
13888 /* Avoid placing the cursor on the last glyph of the row, where
13889 on terminal frames we hold the vertical border between
13890 adjacent windows. */
13891 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13892 && !WINDOW_RIGHTMOST_P (w)
13893 && cursor == row->glyphs[LAST_AREA] - 1)
13894 cursor--;
13895 x = -1; /* will be computed below, at label compute_x */
13896 }
13897
13898 /* Step 1: Try to find the glyph whose character position
13899 corresponds to point. If that's not possible, find 2 glyphs
13900 whose character positions are the closest to point, one before
13901 point, the other after it. */
13902 if (!row->reversed_p)
13903 while (/* not marched to end of glyph row */
13904 glyph < end
13905 /* glyph was not inserted by redisplay for internal purposes */
13906 && !INTEGERP (glyph->object))
13907 {
13908 if (BUFFERP (glyph->object))
13909 {
13910 ptrdiff_t dpos = glyph->charpos - pt_old;
13911
13912 if (glyph->charpos > bpos_max)
13913 bpos_max = glyph->charpos;
13914 if (glyph->charpos < bpos_min)
13915 bpos_min = glyph->charpos;
13916 if (!glyph->avoid_cursor_p)
13917 {
13918 /* If we hit point, we've found the glyph on which to
13919 display the cursor. */
13920 if (dpos == 0)
13921 {
13922 match_with_avoid_cursor = 0;
13923 break;
13924 }
13925 /* See if we've found a better approximation to
13926 POS_BEFORE or to POS_AFTER. */
13927 if (0 > dpos && dpos > pos_before - pt_old)
13928 {
13929 pos_before = glyph->charpos;
13930 glyph_before = glyph;
13931 }
13932 else if (0 < dpos && dpos < pos_after - pt_old)
13933 {
13934 pos_after = glyph->charpos;
13935 glyph_after = glyph;
13936 }
13937 }
13938 else if (dpos == 0)
13939 match_with_avoid_cursor = 1;
13940 }
13941 else if (STRINGP (glyph->object))
13942 {
13943 Lisp_Object chprop;
13944 ptrdiff_t glyph_pos = glyph->charpos;
13945
13946 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13947 glyph->object);
13948 if (!NILP (chprop))
13949 {
13950 /* If the string came from a `display' text property,
13951 look up the buffer position of that property and
13952 use that position to update bpos_max, as if we
13953 actually saw such a position in one of the row's
13954 glyphs. This helps with supporting integer values
13955 of `cursor' property on the display string in
13956 situations where most or all of the row's buffer
13957 text is completely covered by display properties,
13958 so that no glyph with valid buffer positions is
13959 ever seen in the row. */
13960 ptrdiff_t prop_pos =
13961 string_buffer_position_lim (glyph->object, pos_before,
13962 pos_after, 0);
13963
13964 if (prop_pos >= pos_before)
13965 bpos_max = prop_pos - 1;
13966 }
13967 if (INTEGERP (chprop))
13968 {
13969 bpos_covered = bpos_max + XINT (chprop);
13970 /* If the `cursor' property covers buffer positions up
13971 to and including point, we should display cursor on
13972 this glyph. Note that, if a `cursor' property on one
13973 of the string's characters has an integer value, we
13974 will break out of the loop below _before_ we get to
13975 the position match above. IOW, integer values of
13976 the `cursor' property override the "exact match for
13977 point" strategy of positioning the cursor. */
13978 /* Implementation note: bpos_max == pt_old when, e.g.,
13979 we are in an empty line, where bpos_max is set to
13980 MATRIX_ROW_START_CHARPOS, see above. */
13981 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13982 {
13983 cursor = glyph;
13984 break;
13985 }
13986 }
13987
13988 string_seen = 1;
13989 }
13990 x += glyph->pixel_width;
13991 ++glyph;
13992 }
13993 else if (glyph > end) /* row is reversed */
13994 while (!INTEGERP (glyph->object))
13995 {
13996 if (BUFFERP (glyph->object))
13997 {
13998 ptrdiff_t dpos = glyph->charpos - pt_old;
13999
14000 if (glyph->charpos > bpos_max)
14001 bpos_max = glyph->charpos;
14002 if (glyph->charpos < bpos_min)
14003 bpos_min = glyph->charpos;
14004 if (!glyph->avoid_cursor_p)
14005 {
14006 if (dpos == 0)
14007 {
14008 match_with_avoid_cursor = 0;
14009 break;
14010 }
14011 if (0 > dpos && dpos > pos_before - pt_old)
14012 {
14013 pos_before = glyph->charpos;
14014 glyph_before = glyph;
14015 }
14016 else if (0 < dpos && dpos < pos_after - pt_old)
14017 {
14018 pos_after = glyph->charpos;
14019 glyph_after = glyph;
14020 }
14021 }
14022 else if (dpos == 0)
14023 match_with_avoid_cursor = 1;
14024 }
14025 else if (STRINGP (glyph->object))
14026 {
14027 Lisp_Object chprop;
14028 ptrdiff_t glyph_pos = glyph->charpos;
14029
14030 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14031 glyph->object);
14032 if (!NILP (chprop))
14033 {
14034 ptrdiff_t prop_pos =
14035 string_buffer_position_lim (glyph->object, pos_before,
14036 pos_after, 0);
14037
14038 if (prop_pos >= pos_before)
14039 bpos_max = prop_pos - 1;
14040 }
14041 if (INTEGERP (chprop))
14042 {
14043 bpos_covered = bpos_max + XINT (chprop);
14044 /* If the `cursor' property covers buffer positions up
14045 to and including point, we should display cursor on
14046 this glyph. */
14047 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14048 {
14049 cursor = glyph;
14050 break;
14051 }
14052 }
14053 string_seen = 1;
14054 }
14055 --glyph;
14056 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14057 {
14058 x--; /* can't use any pixel_width */
14059 break;
14060 }
14061 x -= glyph->pixel_width;
14062 }
14063
14064 /* Step 2: If we didn't find an exact match for point, we need to
14065 look for a proper place to put the cursor among glyphs between
14066 GLYPH_BEFORE and GLYPH_AFTER. */
14067 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14068 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14069 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14070 {
14071 /* An empty line has a single glyph whose OBJECT is zero and
14072 whose CHARPOS is the position of a newline on that line.
14073 Note that on a TTY, there are more glyphs after that, which
14074 were produced by extend_face_to_end_of_line, but their
14075 CHARPOS is zero or negative. */
14076 int empty_line_p =
14077 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14078 && INTEGERP (glyph->object) && glyph->charpos > 0
14079 /* On a TTY, continued and truncated rows also have a glyph at
14080 their end whose OBJECT is zero and whose CHARPOS is
14081 positive (the continuation and truncation glyphs), but such
14082 rows are obviously not "empty". */
14083 && !(row->continued_p || row->truncated_on_right_p);
14084
14085 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14086 {
14087 ptrdiff_t ellipsis_pos;
14088
14089 /* Scan back over the ellipsis glyphs. */
14090 if (!row->reversed_p)
14091 {
14092 ellipsis_pos = (glyph - 1)->charpos;
14093 while (glyph > row->glyphs[TEXT_AREA]
14094 && (glyph - 1)->charpos == ellipsis_pos)
14095 glyph--, x -= glyph->pixel_width;
14096 /* That loop always goes one position too far, including
14097 the glyph before the ellipsis. So scan forward over
14098 that one. */
14099 x += glyph->pixel_width;
14100 glyph++;
14101 }
14102 else /* row is reversed */
14103 {
14104 ellipsis_pos = (glyph + 1)->charpos;
14105 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14106 && (glyph + 1)->charpos == ellipsis_pos)
14107 glyph++, x += glyph->pixel_width;
14108 x -= glyph->pixel_width;
14109 glyph--;
14110 }
14111 }
14112 else if (match_with_avoid_cursor)
14113 {
14114 cursor = glyph_after;
14115 x = -1;
14116 }
14117 else if (string_seen)
14118 {
14119 int incr = row->reversed_p ? -1 : +1;
14120
14121 /* Need to find the glyph that came out of a string which is
14122 present at point. That glyph is somewhere between
14123 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14124 positioned between POS_BEFORE and POS_AFTER in the
14125 buffer. */
14126 struct glyph *start, *stop;
14127 ptrdiff_t pos = pos_before;
14128
14129 x = -1;
14130
14131 /* If the row ends in a newline from a display string,
14132 reordering could have moved the glyphs belonging to the
14133 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14134 in this case we extend the search to the last glyph in
14135 the row that was not inserted by redisplay. */
14136 if (row->ends_in_newline_from_string_p)
14137 {
14138 glyph_after = end;
14139 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14140 }
14141
14142 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14143 correspond to POS_BEFORE and POS_AFTER, respectively. We
14144 need START and STOP in the order that corresponds to the
14145 row's direction as given by its reversed_p flag. If the
14146 directionality of characters between POS_BEFORE and
14147 POS_AFTER is the opposite of the row's base direction,
14148 these characters will have been reordered for display,
14149 and we need to reverse START and STOP. */
14150 if (!row->reversed_p)
14151 {
14152 start = min (glyph_before, glyph_after);
14153 stop = max (glyph_before, glyph_after);
14154 }
14155 else
14156 {
14157 start = max (glyph_before, glyph_after);
14158 stop = min (glyph_before, glyph_after);
14159 }
14160 for (glyph = start + incr;
14161 row->reversed_p ? glyph > stop : glyph < stop; )
14162 {
14163
14164 /* Any glyphs that come from the buffer are here because
14165 of bidi reordering. Skip them, and only pay
14166 attention to glyphs that came from some string. */
14167 if (STRINGP (glyph->object))
14168 {
14169 Lisp_Object str;
14170 ptrdiff_t tem;
14171 /* If the display property covers the newline, we
14172 need to search for it one position farther. */
14173 ptrdiff_t lim = pos_after
14174 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14175
14176 string_from_text_prop = 0;
14177 str = glyph->object;
14178 tem = string_buffer_position_lim (str, pos, lim, 0);
14179 if (tem == 0 /* from overlay */
14180 || pos <= tem)
14181 {
14182 /* If the string from which this glyph came is
14183 found in the buffer at point, or at position
14184 that is closer to point than pos_after, then
14185 we've found the glyph we've been looking for.
14186 If it comes from an overlay (tem == 0), and
14187 it has the `cursor' property on one of its
14188 glyphs, record that glyph as a candidate for
14189 displaying the cursor. (As in the
14190 unidirectional version, we will display the
14191 cursor on the last candidate we find.) */
14192 if (tem == 0
14193 || tem == pt_old
14194 || (tem - pt_old > 0 && tem < pos_after))
14195 {
14196 /* The glyphs from this string could have
14197 been reordered. Find the one with the
14198 smallest string position. Or there could
14199 be a character in the string with the
14200 `cursor' property, which means display
14201 cursor on that character's glyph. */
14202 ptrdiff_t strpos = glyph->charpos;
14203
14204 if (tem)
14205 {
14206 cursor = glyph;
14207 string_from_text_prop = 1;
14208 }
14209 for ( ;
14210 (row->reversed_p ? glyph > stop : glyph < stop)
14211 && EQ (glyph->object, str);
14212 glyph += incr)
14213 {
14214 Lisp_Object cprop;
14215 ptrdiff_t gpos = glyph->charpos;
14216
14217 cprop = Fget_char_property (make_number (gpos),
14218 Qcursor,
14219 glyph->object);
14220 if (!NILP (cprop))
14221 {
14222 cursor = glyph;
14223 break;
14224 }
14225 if (tem && glyph->charpos < strpos)
14226 {
14227 strpos = glyph->charpos;
14228 cursor = glyph;
14229 }
14230 }
14231
14232 if (tem == pt_old
14233 || (tem - pt_old > 0 && tem < pos_after))
14234 goto compute_x;
14235 }
14236 if (tem)
14237 pos = tem + 1; /* don't find previous instances */
14238 }
14239 /* This string is not what we want; skip all of the
14240 glyphs that came from it. */
14241 while ((row->reversed_p ? glyph > stop : glyph < stop)
14242 && EQ (glyph->object, str))
14243 glyph += incr;
14244 }
14245 else
14246 glyph += incr;
14247 }
14248
14249 /* If we reached the end of the line, and END was from a string,
14250 the cursor is not on this line. */
14251 if (cursor == NULL
14252 && (row->reversed_p ? glyph <= end : glyph >= end)
14253 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14254 && STRINGP (end->object)
14255 && row->continued_p)
14256 return 0;
14257 }
14258 /* A truncated row may not include PT among its character positions.
14259 Setting the cursor inside the scroll margin will trigger
14260 recalculation of hscroll in hscroll_window_tree. But if a
14261 display string covers point, defer to the string-handling
14262 code below to figure this out. */
14263 else if (row->truncated_on_left_p && pt_old < bpos_min)
14264 {
14265 cursor = glyph_before;
14266 x = -1;
14267 }
14268 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14269 /* Zero-width characters produce no glyphs. */
14270 || (!empty_line_p
14271 && (row->reversed_p
14272 ? glyph_after > glyphs_end
14273 : glyph_after < glyphs_end)))
14274 {
14275 cursor = glyph_after;
14276 x = -1;
14277 }
14278 }
14279
14280 compute_x:
14281 if (cursor != NULL)
14282 glyph = cursor;
14283 else if (glyph == glyphs_end
14284 && pos_before == pos_after
14285 && STRINGP ((row->reversed_p
14286 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14287 : row->glyphs[TEXT_AREA])->object))
14288 {
14289 /* If all the glyphs of this row came from strings, put the
14290 cursor on the first glyph of the row. This avoids having the
14291 cursor outside of the text area in this very rare and hard
14292 use case. */
14293 glyph =
14294 row->reversed_p
14295 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14296 : row->glyphs[TEXT_AREA];
14297 }
14298 if (x < 0)
14299 {
14300 struct glyph *g;
14301
14302 /* Need to compute x that corresponds to GLYPH. */
14303 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14304 {
14305 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14306 emacs_abort ();
14307 x += g->pixel_width;
14308 }
14309 }
14310
14311 /* ROW could be part of a continued line, which, under bidi
14312 reordering, might have other rows whose start and end charpos
14313 occlude point. Only set w->cursor if we found a better
14314 approximation to the cursor position than we have from previously
14315 examined candidate rows belonging to the same continued line. */
14316 if (/* we already have a candidate row */
14317 w->cursor.vpos >= 0
14318 /* that candidate is not the row we are processing */
14319 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14320 /* Make sure cursor.vpos specifies a row whose start and end
14321 charpos occlude point, and it is valid candidate for being a
14322 cursor-row. This is because some callers of this function
14323 leave cursor.vpos at the row where the cursor was displayed
14324 during the last redisplay cycle. */
14325 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14326 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14327 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14328 {
14329 struct glyph *g1 =
14330 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14331
14332 /* Don't consider glyphs that are outside TEXT_AREA. */
14333 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14334 return 0;
14335 /* Keep the candidate whose buffer position is the closest to
14336 point or has the `cursor' property. */
14337 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14338 w->cursor.hpos >= 0
14339 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14340 && ((BUFFERP (g1->object)
14341 && (g1->charpos == pt_old /* an exact match always wins */
14342 || (BUFFERP (glyph->object)
14343 && eabs (g1->charpos - pt_old)
14344 < eabs (glyph->charpos - pt_old))))
14345 /* previous candidate is a glyph from a string that has
14346 a non-nil `cursor' property */
14347 || (STRINGP (g1->object)
14348 && (!NILP (Fget_char_property (make_number (g1->charpos),
14349 Qcursor, g1->object))
14350 /* previous candidate is from the same display
14351 string as this one, and the display string
14352 came from a text property */
14353 || (EQ (g1->object, glyph->object)
14354 && string_from_text_prop)
14355 /* this candidate is from newline and its
14356 position is not an exact match */
14357 || (INTEGERP (glyph->object)
14358 && glyph->charpos != pt_old)))))
14359 return 0;
14360 /* If this candidate gives an exact match, use that. */
14361 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14362 /* If this candidate is a glyph created for the
14363 terminating newline of a line, and point is on that
14364 newline, it wins because it's an exact match. */
14365 || (!row->continued_p
14366 && INTEGERP (glyph->object)
14367 && glyph->charpos == 0
14368 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14369 /* Otherwise, keep the candidate that comes from a row
14370 spanning less buffer positions. This may win when one or
14371 both candidate positions are on glyphs that came from
14372 display strings, for which we cannot compare buffer
14373 positions. */
14374 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14375 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14376 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14377 return 0;
14378 }
14379 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14380 w->cursor.x = x;
14381 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14382 w->cursor.y = row->y + dy;
14383
14384 if (w == XWINDOW (selected_window))
14385 {
14386 if (!row->continued_p
14387 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14388 && row->x == 0)
14389 {
14390 this_line_buffer = XBUFFER (w->contents);
14391
14392 CHARPOS (this_line_start_pos)
14393 = MATRIX_ROW_START_CHARPOS (row) + delta;
14394 BYTEPOS (this_line_start_pos)
14395 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14396
14397 CHARPOS (this_line_end_pos)
14398 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14399 BYTEPOS (this_line_end_pos)
14400 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14401
14402 this_line_y = w->cursor.y;
14403 this_line_pixel_height = row->height;
14404 this_line_vpos = w->cursor.vpos;
14405 this_line_start_x = row->x;
14406 }
14407 else
14408 CHARPOS (this_line_start_pos) = 0;
14409 }
14410
14411 return 1;
14412 }
14413
14414
14415 /* Run window scroll functions, if any, for WINDOW with new window
14416 start STARTP. Sets the window start of WINDOW to that position.
14417
14418 We assume that the window's buffer is really current. */
14419
14420 static struct text_pos
14421 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14422 {
14423 struct window *w = XWINDOW (window);
14424 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14425
14426 eassert (current_buffer == XBUFFER (w->contents));
14427
14428 if (!NILP (Vwindow_scroll_functions))
14429 {
14430 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14431 make_number (CHARPOS (startp)));
14432 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14433 /* In case the hook functions switch buffers. */
14434 set_buffer_internal (XBUFFER (w->contents));
14435 }
14436
14437 return startp;
14438 }
14439
14440
14441 /* Make sure the line containing the cursor is fully visible.
14442 A value of 1 means there is nothing to be done.
14443 (Either the line is fully visible, or it cannot be made so,
14444 or we cannot tell.)
14445
14446 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14447 is higher than window.
14448
14449 A value of 0 means the caller should do scrolling
14450 as if point had gone off the screen. */
14451
14452 static int
14453 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14454 {
14455 struct glyph_matrix *matrix;
14456 struct glyph_row *row;
14457 int window_height;
14458
14459 if (!make_cursor_line_fully_visible_p)
14460 return 1;
14461
14462 /* It's not always possible to find the cursor, e.g, when a window
14463 is full of overlay strings. Don't do anything in that case. */
14464 if (w->cursor.vpos < 0)
14465 return 1;
14466
14467 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14468 row = MATRIX_ROW (matrix, w->cursor.vpos);
14469
14470 /* If the cursor row is not partially visible, there's nothing to do. */
14471 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14472 return 1;
14473
14474 /* If the row the cursor is in is taller than the window's height,
14475 it's not clear what to do, so do nothing. */
14476 window_height = window_box_height (w);
14477 if (row->height >= window_height)
14478 {
14479 if (!force_p || MINI_WINDOW_P (w)
14480 || w->vscroll || w->cursor.vpos == 0)
14481 return 1;
14482 }
14483 return 0;
14484 }
14485
14486
14487 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14488 non-zero means only WINDOW is redisplayed in redisplay_internal.
14489 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14490 in redisplay_window to bring a partially visible line into view in
14491 the case that only the cursor has moved.
14492
14493 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14494 last screen line's vertical height extends past the end of the screen.
14495
14496 Value is
14497
14498 1 if scrolling succeeded
14499
14500 0 if scrolling didn't find point.
14501
14502 -1 if new fonts have been loaded so that we must interrupt
14503 redisplay, adjust glyph matrices, and try again. */
14504
14505 enum
14506 {
14507 SCROLLING_SUCCESS,
14508 SCROLLING_FAILED,
14509 SCROLLING_NEED_LARGER_MATRICES
14510 };
14511
14512 /* If scroll-conservatively is more than this, never recenter.
14513
14514 If you change this, don't forget to update the doc string of
14515 `scroll-conservatively' and the Emacs manual. */
14516 #define SCROLL_LIMIT 100
14517
14518 static int
14519 try_scrolling (Lisp_Object window, int just_this_one_p,
14520 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14521 int temp_scroll_step, int last_line_misfit)
14522 {
14523 struct window *w = XWINDOW (window);
14524 struct frame *f = XFRAME (w->frame);
14525 struct text_pos pos, startp;
14526 struct it it;
14527 int this_scroll_margin, scroll_max, rc, height;
14528 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14529 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14530 Lisp_Object aggressive;
14531 /* We will never try scrolling more than this number of lines. */
14532 int scroll_limit = SCROLL_LIMIT;
14533 int frame_line_height = default_line_pixel_height (w);
14534 int window_total_lines
14535 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14536
14537 #ifdef GLYPH_DEBUG
14538 debug_method_add (w, "try_scrolling");
14539 #endif
14540
14541 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14542
14543 /* Compute scroll margin height in pixels. We scroll when point is
14544 within this distance from the top or bottom of the window. */
14545 if (scroll_margin > 0)
14546 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14547 * frame_line_height;
14548 else
14549 this_scroll_margin = 0;
14550
14551 /* Force arg_scroll_conservatively to have a reasonable value, to
14552 avoid scrolling too far away with slow move_it_* functions. Note
14553 that the user can supply scroll-conservatively equal to
14554 `most-positive-fixnum', which can be larger than INT_MAX. */
14555 if (arg_scroll_conservatively > scroll_limit)
14556 {
14557 arg_scroll_conservatively = scroll_limit + 1;
14558 scroll_max = scroll_limit * frame_line_height;
14559 }
14560 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14561 /* Compute how much we should try to scroll maximally to bring
14562 point into view. */
14563 scroll_max = (max (scroll_step,
14564 max (arg_scroll_conservatively, temp_scroll_step))
14565 * frame_line_height);
14566 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14567 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14568 /* We're trying to scroll because of aggressive scrolling but no
14569 scroll_step is set. Choose an arbitrary one. */
14570 scroll_max = 10 * frame_line_height;
14571 else
14572 scroll_max = 0;
14573
14574 too_near_end:
14575
14576 /* Decide whether to scroll down. */
14577 if (PT > CHARPOS (startp))
14578 {
14579 int scroll_margin_y;
14580
14581 /* Compute the pixel ypos of the scroll margin, then move IT to
14582 either that ypos or PT, whichever comes first. */
14583 start_display (&it, w, startp);
14584 scroll_margin_y = it.last_visible_y - this_scroll_margin
14585 - frame_line_height * extra_scroll_margin_lines;
14586 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14587 (MOVE_TO_POS | MOVE_TO_Y));
14588
14589 if (PT > CHARPOS (it.current.pos))
14590 {
14591 int y0 = line_bottom_y (&it);
14592 /* Compute how many pixels below window bottom to stop searching
14593 for PT. This avoids costly search for PT that is far away if
14594 the user limited scrolling by a small number of lines, but
14595 always finds PT if scroll_conservatively is set to a large
14596 number, such as most-positive-fixnum. */
14597 int slack = max (scroll_max, 10 * frame_line_height);
14598 int y_to_move = it.last_visible_y + slack;
14599
14600 /* Compute the distance from the scroll margin to PT or to
14601 the scroll limit, whichever comes first. This should
14602 include the height of the cursor line, to make that line
14603 fully visible. */
14604 move_it_to (&it, PT, -1, y_to_move,
14605 -1, MOVE_TO_POS | MOVE_TO_Y);
14606 dy = line_bottom_y (&it) - y0;
14607
14608 if (dy > scroll_max)
14609 return SCROLLING_FAILED;
14610
14611 if (dy > 0)
14612 scroll_down_p = 1;
14613 }
14614 }
14615
14616 if (scroll_down_p)
14617 {
14618 /* Point is in or below the bottom scroll margin, so move the
14619 window start down. If scrolling conservatively, move it just
14620 enough down to make point visible. If scroll_step is set,
14621 move it down by scroll_step. */
14622 if (arg_scroll_conservatively)
14623 amount_to_scroll
14624 = min (max (dy, frame_line_height),
14625 frame_line_height * arg_scroll_conservatively);
14626 else if (scroll_step || temp_scroll_step)
14627 amount_to_scroll = scroll_max;
14628 else
14629 {
14630 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14631 height = WINDOW_BOX_TEXT_HEIGHT (w);
14632 if (NUMBERP (aggressive))
14633 {
14634 double float_amount = XFLOATINT (aggressive) * height;
14635 int aggressive_scroll = float_amount;
14636 if (aggressive_scroll == 0 && float_amount > 0)
14637 aggressive_scroll = 1;
14638 /* Don't let point enter the scroll margin near top of
14639 the window. This could happen if the value of
14640 scroll_up_aggressively is too large and there are
14641 non-zero margins, because scroll_up_aggressively
14642 means put point that fraction of window height
14643 _from_the_bottom_margin_. */
14644 if (aggressive_scroll + 2*this_scroll_margin > height)
14645 aggressive_scroll = height - 2*this_scroll_margin;
14646 amount_to_scroll = dy + aggressive_scroll;
14647 }
14648 }
14649
14650 if (amount_to_scroll <= 0)
14651 return SCROLLING_FAILED;
14652
14653 start_display (&it, w, startp);
14654 if (arg_scroll_conservatively <= scroll_limit)
14655 move_it_vertically (&it, amount_to_scroll);
14656 else
14657 {
14658 /* Extra precision for users who set scroll-conservatively
14659 to a large number: make sure the amount we scroll
14660 the window start is never less than amount_to_scroll,
14661 which was computed as distance from window bottom to
14662 point. This matters when lines at window top and lines
14663 below window bottom have different height. */
14664 struct it it1;
14665 void *it1data = NULL;
14666 /* We use a temporary it1 because line_bottom_y can modify
14667 its argument, if it moves one line down; see there. */
14668 int start_y;
14669
14670 SAVE_IT (it1, it, it1data);
14671 start_y = line_bottom_y (&it1);
14672 do {
14673 RESTORE_IT (&it, &it, it1data);
14674 move_it_by_lines (&it, 1);
14675 SAVE_IT (it1, it, it1data);
14676 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14677 }
14678
14679 /* If STARTP is unchanged, move it down another screen line. */
14680 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14681 move_it_by_lines (&it, 1);
14682 startp = it.current.pos;
14683 }
14684 else
14685 {
14686 struct text_pos scroll_margin_pos = startp;
14687 int y_offset = 0;
14688
14689 /* See if point is inside the scroll margin at the top of the
14690 window. */
14691 if (this_scroll_margin)
14692 {
14693 int y_start;
14694
14695 start_display (&it, w, startp);
14696 y_start = it.current_y;
14697 move_it_vertically (&it, this_scroll_margin);
14698 scroll_margin_pos = it.current.pos;
14699 /* If we didn't move enough before hitting ZV, request
14700 additional amount of scroll, to move point out of the
14701 scroll margin. */
14702 if (IT_CHARPOS (it) == ZV
14703 && it.current_y - y_start < this_scroll_margin)
14704 y_offset = this_scroll_margin - (it.current_y - y_start);
14705 }
14706
14707 if (PT < CHARPOS (scroll_margin_pos))
14708 {
14709 /* Point is in the scroll margin at the top of the window or
14710 above what is displayed in the window. */
14711 int y0, y_to_move;
14712
14713 /* Compute the vertical distance from PT to the scroll
14714 margin position. Move as far as scroll_max allows, or
14715 one screenful, or 10 screen lines, whichever is largest.
14716 Give up if distance is greater than scroll_max or if we
14717 didn't reach the scroll margin position. */
14718 SET_TEXT_POS (pos, PT, PT_BYTE);
14719 start_display (&it, w, pos);
14720 y0 = it.current_y;
14721 y_to_move = max (it.last_visible_y,
14722 max (scroll_max, 10 * frame_line_height));
14723 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14724 y_to_move, -1,
14725 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14726 dy = it.current_y - y0;
14727 if (dy > scroll_max
14728 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14729 return SCROLLING_FAILED;
14730
14731 /* Additional scroll for when ZV was too close to point. */
14732 dy += y_offset;
14733
14734 /* Compute new window start. */
14735 start_display (&it, w, startp);
14736
14737 if (arg_scroll_conservatively)
14738 amount_to_scroll = max (dy, frame_line_height *
14739 max (scroll_step, temp_scroll_step));
14740 else if (scroll_step || temp_scroll_step)
14741 amount_to_scroll = scroll_max;
14742 else
14743 {
14744 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14745 height = WINDOW_BOX_TEXT_HEIGHT (w);
14746 if (NUMBERP (aggressive))
14747 {
14748 double float_amount = XFLOATINT (aggressive) * height;
14749 int aggressive_scroll = float_amount;
14750 if (aggressive_scroll == 0 && float_amount > 0)
14751 aggressive_scroll = 1;
14752 /* Don't let point enter the scroll margin near
14753 bottom of the window, if the value of
14754 scroll_down_aggressively happens to be too
14755 large. */
14756 if (aggressive_scroll + 2*this_scroll_margin > height)
14757 aggressive_scroll = height - 2*this_scroll_margin;
14758 amount_to_scroll = dy + aggressive_scroll;
14759 }
14760 }
14761
14762 if (amount_to_scroll <= 0)
14763 return SCROLLING_FAILED;
14764
14765 move_it_vertically_backward (&it, amount_to_scroll);
14766 startp = it.current.pos;
14767 }
14768 }
14769
14770 /* Run window scroll functions. */
14771 startp = run_window_scroll_functions (window, startp);
14772
14773 /* Display the window. Give up if new fonts are loaded, or if point
14774 doesn't appear. */
14775 if (!try_window (window, startp, 0))
14776 rc = SCROLLING_NEED_LARGER_MATRICES;
14777 else if (w->cursor.vpos < 0)
14778 {
14779 clear_glyph_matrix (w->desired_matrix);
14780 rc = SCROLLING_FAILED;
14781 }
14782 else
14783 {
14784 /* Maybe forget recorded base line for line number display. */
14785 if (!just_this_one_p
14786 || current_buffer->clip_changed
14787 || BEG_UNCHANGED < CHARPOS (startp))
14788 w->base_line_number = 0;
14789
14790 /* If cursor ends up on a partially visible line,
14791 treat that as being off the bottom of the screen. */
14792 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14793 /* It's possible that the cursor is on the first line of the
14794 buffer, which is partially obscured due to a vscroll
14795 (Bug#7537). In that case, avoid looping forever . */
14796 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14797 {
14798 clear_glyph_matrix (w->desired_matrix);
14799 ++extra_scroll_margin_lines;
14800 goto too_near_end;
14801 }
14802 rc = SCROLLING_SUCCESS;
14803 }
14804
14805 return rc;
14806 }
14807
14808
14809 /* Compute a suitable window start for window W if display of W starts
14810 on a continuation line. Value is non-zero if a new window start
14811 was computed.
14812
14813 The new window start will be computed, based on W's width, starting
14814 from the start of the continued line. It is the start of the
14815 screen line with the minimum distance from the old start W->start. */
14816
14817 static int
14818 compute_window_start_on_continuation_line (struct window *w)
14819 {
14820 struct text_pos pos, start_pos;
14821 int window_start_changed_p = 0;
14822
14823 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14824
14825 /* If window start is on a continuation line... Window start may be
14826 < BEGV in case there's invisible text at the start of the
14827 buffer (M-x rmail, for example). */
14828 if (CHARPOS (start_pos) > BEGV
14829 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14830 {
14831 struct it it;
14832 struct glyph_row *row;
14833
14834 /* Handle the case that the window start is out of range. */
14835 if (CHARPOS (start_pos) < BEGV)
14836 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14837 else if (CHARPOS (start_pos) > ZV)
14838 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14839
14840 /* Find the start of the continued line. This should be fast
14841 because find_newline is fast (newline cache). */
14842 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14843 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14844 row, DEFAULT_FACE_ID);
14845 reseat_at_previous_visible_line_start (&it);
14846
14847 /* If the line start is "too far" away from the window start,
14848 say it takes too much time to compute a new window start. */
14849 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14850 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14851 {
14852 int min_distance, distance;
14853
14854 /* Move forward by display lines to find the new window
14855 start. If window width was enlarged, the new start can
14856 be expected to be > the old start. If window width was
14857 decreased, the new window start will be < the old start.
14858 So, we're looking for the display line start with the
14859 minimum distance from the old window start. */
14860 pos = it.current.pos;
14861 min_distance = INFINITY;
14862 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14863 distance < min_distance)
14864 {
14865 min_distance = distance;
14866 pos = it.current.pos;
14867 if (it.line_wrap == WORD_WRAP)
14868 {
14869 /* Under WORD_WRAP, move_it_by_lines is likely to
14870 overshoot and stop not at the first, but the
14871 second character from the left margin. So in
14872 that case, we need a more tight control on the X
14873 coordinate of the iterator than move_it_by_lines
14874 promises in its contract. The method is to first
14875 go to the last (rightmost) visible character of a
14876 line, then move to the leftmost character on the
14877 next line in a separate call. */
14878 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14879 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14880 move_it_to (&it, ZV, 0,
14881 it.current_y + it.max_ascent + it.max_descent, -1,
14882 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14883 }
14884 else
14885 move_it_by_lines (&it, 1);
14886 }
14887
14888 /* Set the window start there. */
14889 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14890 window_start_changed_p = 1;
14891 }
14892 }
14893
14894 return window_start_changed_p;
14895 }
14896
14897
14898 /* Try cursor movement in case text has not changed in window WINDOW,
14899 with window start STARTP. Value is
14900
14901 CURSOR_MOVEMENT_SUCCESS if successful
14902
14903 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14904
14905 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14906 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14907 we want to scroll as if scroll-step were set to 1. See the code.
14908
14909 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14910 which case we have to abort this redisplay, and adjust matrices
14911 first. */
14912
14913 enum
14914 {
14915 CURSOR_MOVEMENT_SUCCESS,
14916 CURSOR_MOVEMENT_CANNOT_BE_USED,
14917 CURSOR_MOVEMENT_MUST_SCROLL,
14918 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14919 };
14920
14921 static int
14922 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14923 {
14924 struct window *w = XWINDOW (window);
14925 struct frame *f = XFRAME (w->frame);
14926 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14927
14928 #ifdef GLYPH_DEBUG
14929 if (inhibit_try_cursor_movement)
14930 return rc;
14931 #endif
14932
14933 /* Previously, there was a check for Lisp integer in the
14934 if-statement below. Now, this field is converted to
14935 ptrdiff_t, thus zero means invalid position in a buffer. */
14936 eassert (w->last_point > 0);
14937 /* Likewise there was a check whether window_end_vpos is nil or larger
14938 than the window. Now window_end_vpos is int and so never nil, but
14939 let's leave eassert to check whether it fits in the window. */
14940 eassert (w->window_end_vpos < w->current_matrix->nrows);
14941
14942 /* Handle case where text has not changed, only point, and it has
14943 not moved off the frame. */
14944 if (/* Point may be in this window. */
14945 PT >= CHARPOS (startp)
14946 /* Selective display hasn't changed. */
14947 && !current_buffer->clip_changed
14948 /* Function force-mode-line-update is used to force a thorough
14949 redisplay. It sets either windows_or_buffers_changed or
14950 update_mode_lines. So don't take a shortcut here for these
14951 cases. */
14952 && !update_mode_lines
14953 && !windows_or_buffers_changed
14954 && !f->cursor_type_changed
14955 && NILP (Vshow_trailing_whitespace)
14956 /* This code is not used for mini-buffer for the sake of the case
14957 of redisplaying to replace an echo area message; since in
14958 that case the mini-buffer contents per se are usually
14959 unchanged. This code is of no real use in the mini-buffer
14960 since the handling of this_line_start_pos, etc., in redisplay
14961 handles the same cases. */
14962 && !EQ (window, minibuf_window)
14963 && (FRAME_WINDOW_P (f)
14964 || !overlay_arrow_in_current_buffer_p ()))
14965 {
14966 int this_scroll_margin, top_scroll_margin;
14967 struct glyph_row *row = NULL;
14968 int frame_line_height = default_line_pixel_height (w);
14969 int window_total_lines
14970 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14971
14972 #ifdef GLYPH_DEBUG
14973 debug_method_add (w, "cursor movement");
14974 #endif
14975
14976 /* Scroll if point within this distance from the top or bottom
14977 of the window. This is a pixel value. */
14978 if (scroll_margin > 0)
14979 {
14980 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
14981 this_scroll_margin *= frame_line_height;
14982 }
14983 else
14984 this_scroll_margin = 0;
14985
14986 top_scroll_margin = this_scroll_margin;
14987 if (WINDOW_WANTS_HEADER_LINE_P (w))
14988 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14989
14990 /* Start with the row the cursor was displayed during the last
14991 not paused redisplay. Give up if that row is not valid. */
14992 if (w->last_cursor_vpos < 0
14993 || w->last_cursor_vpos >= w->current_matrix->nrows)
14994 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14995 else
14996 {
14997 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
14998 if (row->mode_line_p)
14999 ++row;
15000 if (!row->enabled_p)
15001 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15002 }
15003
15004 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15005 {
15006 int scroll_p = 0, must_scroll = 0;
15007 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15008
15009 if (PT > w->last_point)
15010 {
15011 /* Point has moved forward. */
15012 while (MATRIX_ROW_END_CHARPOS (row) < PT
15013 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15014 {
15015 eassert (row->enabled_p);
15016 ++row;
15017 }
15018
15019 /* If the end position of a row equals the start
15020 position of the next row, and PT is at that position,
15021 we would rather display cursor in the next line. */
15022 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15023 && MATRIX_ROW_END_CHARPOS (row) == PT
15024 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15025 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15026 && !cursor_row_p (row))
15027 ++row;
15028
15029 /* If within the scroll margin, scroll. Note that
15030 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15031 the next line would be drawn, and that
15032 this_scroll_margin can be zero. */
15033 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15034 || PT > MATRIX_ROW_END_CHARPOS (row)
15035 /* Line is completely visible last line in window
15036 and PT is to be set in the next line. */
15037 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15038 && PT == MATRIX_ROW_END_CHARPOS (row)
15039 && !row->ends_at_zv_p
15040 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15041 scroll_p = 1;
15042 }
15043 else if (PT < w->last_point)
15044 {
15045 /* Cursor has to be moved backward. Note that PT >=
15046 CHARPOS (startp) because of the outer if-statement. */
15047 while (!row->mode_line_p
15048 && (MATRIX_ROW_START_CHARPOS (row) > PT
15049 || (MATRIX_ROW_START_CHARPOS (row) == PT
15050 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15051 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15052 row > w->current_matrix->rows
15053 && (row-1)->ends_in_newline_from_string_p))))
15054 && (row->y > top_scroll_margin
15055 || CHARPOS (startp) == BEGV))
15056 {
15057 eassert (row->enabled_p);
15058 --row;
15059 }
15060
15061 /* Consider the following case: Window starts at BEGV,
15062 there is invisible, intangible text at BEGV, so that
15063 display starts at some point START > BEGV. It can
15064 happen that we are called with PT somewhere between
15065 BEGV and START. Try to handle that case. */
15066 if (row < w->current_matrix->rows
15067 || row->mode_line_p)
15068 {
15069 row = w->current_matrix->rows;
15070 if (row->mode_line_p)
15071 ++row;
15072 }
15073
15074 /* Due to newlines in overlay strings, we may have to
15075 skip forward over overlay strings. */
15076 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15077 && MATRIX_ROW_END_CHARPOS (row) == PT
15078 && !cursor_row_p (row))
15079 ++row;
15080
15081 /* If within the scroll margin, scroll. */
15082 if (row->y < top_scroll_margin
15083 && CHARPOS (startp) != BEGV)
15084 scroll_p = 1;
15085 }
15086 else
15087 {
15088 /* Cursor did not move. So don't scroll even if cursor line
15089 is partially visible, as it was so before. */
15090 rc = CURSOR_MOVEMENT_SUCCESS;
15091 }
15092
15093 if (PT < MATRIX_ROW_START_CHARPOS (row)
15094 || PT > MATRIX_ROW_END_CHARPOS (row))
15095 {
15096 /* if PT is not in the glyph row, give up. */
15097 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15098 must_scroll = 1;
15099 }
15100 else if (rc != CURSOR_MOVEMENT_SUCCESS
15101 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15102 {
15103 struct glyph_row *row1;
15104
15105 /* If rows are bidi-reordered and point moved, back up
15106 until we find a row that does not belong to a
15107 continuation line. This is because we must consider
15108 all rows of a continued line as candidates for the
15109 new cursor positioning, since row start and end
15110 positions change non-linearly with vertical position
15111 in such rows. */
15112 /* FIXME: Revisit this when glyph ``spilling'' in
15113 continuation lines' rows is implemented for
15114 bidi-reordered rows. */
15115 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15116 MATRIX_ROW_CONTINUATION_LINE_P (row);
15117 --row)
15118 {
15119 /* If we hit the beginning of the displayed portion
15120 without finding the first row of a continued
15121 line, give up. */
15122 if (row <= row1)
15123 {
15124 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15125 break;
15126 }
15127 eassert (row->enabled_p);
15128 }
15129 }
15130 if (must_scroll)
15131 ;
15132 else if (rc != CURSOR_MOVEMENT_SUCCESS
15133 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15134 /* Make sure this isn't a header line by any chance, since
15135 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15136 && !row->mode_line_p
15137 && make_cursor_line_fully_visible_p)
15138 {
15139 if (PT == MATRIX_ROW_END_CHARPOS (row)
15140 && !row->ends_at_zv_p
15141 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15142 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15143 else if (row->height > window_box_height (w))
15144 {
15145 /* If we end up in a partially visible line, let's
15146 make it fully visible, except when it's taller
15147 than the window, in which case we can't do much
15148 about it. */
15149 *scroll_step = 1;
15150 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15151 }
15152 else
15153 {
15154 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15155 if (!cursor_row_fully_visible_p (w, 0, 1))
15156 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15157 else
15158 rc = CURSOR_MOVEMENT_SUCCESS;
15159 }
15160 }
15161 else if (scroll_p)
15162 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15163 else if (rc != CURSOR_MOVEMENT_SUCCESS
15164 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15165 {
15166 /* With bidi-reordered rows, there could be more than
15167 one candidate row whose start and end positions
15168 occlude point. We need to let set_cursor_from_row
15169 find the best candidate. */
15170 /* FIXME: Revisit this when glyph ``spilling'' in
15171 continuation lines' rows is implemented for
15172 bidi-reordered rows. */
15173 int rv = 0;
15174
15175 do
15176 {
15177 int at_zv_p = 0, exact_match_p = 0;
15178
15179 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15180 && PT <= MATRIX_ROW_END_CHARPOS (row)
15181 && cursor_row_p (row))
15182 rv |= set_cursor_from_row (w, row, w->current_matrix,
15183 0, 0, 0, 0);
15184 /* As soon as we've found the exact match for point,
15185 or the first suitable row whose ends_at_zv_p flag
15186 is set, we are done. */
15187 at_zv_p =
15188 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15189 if (rv && !at_zv_p
15190 && w->cursor.hpos >= 0
15191 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15192 w->cursor.vpos))
15193 {
15194 struct glyph_row *candidate =
15195 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15196 struct glyph *g =
15197 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15198 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15199
15200 exact_match_p =
15201 (BUFFERP (g->object) && g->charpos == PT)
15202 || (INTEGERP (g->object)
15203 && (g->charpos == PT
15204 || (g->charpos == 0 && endpos - 1 == PT)));
15205 }
15206 if (rv && (at_zv_p || exact_match_p))
15207 {
15208 rc = CURSOR_MOVEMENT_SUCCESS;
15209 break;
15210 }
15211 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15212 break;
15213 ++row;
15214 }
15215 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15216 || row->continued_p)
15217 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15218 || (MATRIX_ROW_START_CHARPOS (row) == PT
15219 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15220 /* If we didn't find any candidate rows, or exited the
15221 loop before all the candidates were examined, signal
15222 to the caller that this method failed. */
15223 if (rc != CURSOR_MOVEMENT_SUCCESS
15224 && !(rv
15225 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15226 && !row->continued_p))
15227 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15228 else if (rv)
15229 rc = CURSOR_MOVEMENT_SUCCESS;
15230 }
15231 else
15232 {
15233 do
15234 {
15235 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15236 {
15237 rc = CURSOR_MOVEMENT_SUCCESS;
15238 break;
15239 }
15240 ++row;
15241 }
15242 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15243 && MATRIX_ROW_START_CHARPOS (row) == PT
15244 && cursor_row_p (row));
15245 }
15246 }
15247 }
15248
15249 return rc;
15250 }
15251
15252 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15253 static
15254 #endif
15255 void
15256 set_vertical_scroll_bar (struct window *w)
15257 {
15258 ptrdiff_t start, end, whole;
15259
15260 /* Calculate the start and end positions for the current window.
15261 At some point, it would be nice to choose between scrollbars
15262 which reflect the whole buffer size, with special markers
15263 indicating narrowing, and scrollbars which reflect only the
15264 visible region.
15265
15266 Note that mini-buffers sometimes aren't displaying any text. */
15267 if (!MINI_WINDOW_P (w)
15268 || (w == XWINDOW (minibuf_window)
15269 && NILP (echo_area_buffer[0])))
15270 {
15271 struct buffer *buf = XBUFFER (w->contents);
15272 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15273 start = marker_position (w->start) - BUF_BEGV (buf);
15274 /* I don't think this is guaranteed to be right. For the
15275 moment, we'll pretend it is. */
15276 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15277
15278 if (end < start)
15279 end = start;
15280 if (whole < (end - start))
15281 whole = end - start;
15282 }
15283 else
15284 start = end = whole = 0;
15285
15286 /* Indicate what this scroll bar ought to be displaying now. */
15287 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15288 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15289 (w, end - start, whole, start);
15290 }
15291
15292
15293 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15294 selected_window is redisplayed.
15295
15296 We can return without actually redisplaying the window if fonts has been
15297 changed on window's frame. In that case, redisplay_internal will retry. */
15298
15299 static void
15300 redisplay_window (Lisp_Object window, int just_this_one_p)
15301 {
15302 struct window *w = XWINDOW (window);
15303 struct frame *f = XFRAME (w->frame);
15304 struct buffer *buffer = XBUFFER (w->contents);
15305 struct buffer *old = current_buffer;
15306 struct text_pos lpoint, opoint, startp;
15307 int update_mode_line;
15308 int tem;
15309 struct it it;
15310 /* Record it now because it's overwritten. */
15311 int current_matrix_up_to_date_p = 0;
15312 int used_current_matrix_p = 0;
15313 /* This is less strict than current_matrix_up_to_date_p.
15314 It indicates that the buffer contents and narrowing are unchanged. */
15315 int buffer_unchanged_p = 0;
15316 int temp_scroll_step = 0;
15317 ptrdiff_t count = SPECPDL_INDEX ();
15318 int rc;
15319 int centering_position = -1;
15320 int last_line_misfit = 0;
15321 ptrdiff_t beg_unchanged, end_unchanged;
15322 int frame_line_height;
15323
15324 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15325 opoint = lpoint;
15326
15327 #ifdef GLYPH_DEBUG
15328 *w->desired_matrix->method = 0;
15329 #endif
15330
15331 /* Make sure that both W's markers are valid. */
15332 eassert (XMARKER (w->start)->buffer == buffer);
15333 eassert (XMARKER (w->pointm)->buffer == buffer);
15334
15335 restart:
15336 reconsider_clip_changes (w);
15337 frame_line_height = default_line_pixel_height (w);
15338
15339 /* Has the mode line to be updated? */
15340 update_mode_line = (w->update_mode_line
15341 || update_mode_lines
15342 || buffer->clip_changed
15343 || buffer->prevent_redisplay_optimizations_p);
15344
15345 if (MINI_WINDOW_P (w))
15346 {
15347 if (w == XWINDOW (echo_area_window)
15348 && !NILP (echo_area_buffer[0]))
15349 {
15350 if (update_mode_line)
15351 /* We may have to update a tty frame's menu bar or a
15352 tool-bar. Example `M-x C-h C-h C-g'. */
15353 goto finish_menu_bars;
15354 else
15355 /* We've already displayed the echo area glyphs in this window. */
15356 goto finish_scroll_bars;
15357 }
15358 else if ((w != XWINDOW (minibuf_window)
15359 || minibuf_level == 0)
15360 /* When buffer is nonempty, redisplay window normally. */
15361 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15362 /* Quail displays non-mini buffers in minibuffer window.
15363 In that case, redisplay the window normally. */
15364 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15365 {
15366 /* W is a mini-buffer window, but it's not active, so clear
15367 it. */
15368 int yb = window_text_bottom_y (w);
15369 struct glyph_row *row;
15370 int y;
15371
15372 for (y = 0, row = w->desired_matrix->rows;
15373 y < yb;
15374 y += row->height, ++row)
15375 blank_row (w, row, y);
15376 goto finish_scroll_bars;
15377 }
15378
15379 clear_glyph_matrix (w->desired_matrix);
15380 }
15381
15382 /* Otherwise set up data on this window; select its buffer and point
15383 value. */
15384 /* Really select the buffer, for the sake of buffer-local
15385 variables. */
15386 set_buffer_internal_1 (XBUFFER (w->contents));
15387
15388 current_matrix_up_to_date_p
15389 = (w->window_end_valid
15390 && !current_buffer->clip_changed
15391 && !current_buffer->prevent_redisplay_optimizations_p
15392 && !window_outdated (w));
15393
15394 /* Run the window-bottom-change-functions
15395 if it is possible that the text on the screen has changed
15396 (either due to modification of the text, or any other reason). */
15397 if (!current_matrix_up_to_date_p
15398 && !NILP (Vwindow_text_change_functions))
15399 {
15400 safe_run_hooks (Qwindow_text_change_functions);
15401 goto restart;
15402 }
15403
15404 beg_unchanged = BEG_UNCHANGED;
15405 end_unchanged = END_UNCHANGED;
15406
15407 SET_TEXT_POS (opoint, PT, PT_BYTE);
15408
15409 specbind (Qinhibit_point_motion_hooks, Qt);
15410
15411 buffer_unchanged_p
15412 = (w->window_end_valid
15413 && !current_buffer->clip_changed
15414 && !window_outdated (w));
15415
15416 /* When windows_or_buffers_changed is non-zero, we can't rely
15417 on the window end being valid, so set it to zero there. */
15418 if (windows_or_buffers_changed)
15419 {
15420 /* If window starts on a continuation line, maybe adjust the
15421 window start in case the window's width changed. */
15422 if (XMARKER (w->start)->buffer == current_buffer)
15423 compute_window_start_on_continuation_line (w);
15424
15425 w->window_end_valid = 0;
15426 /* If so, we also can't rely on current matrix
15427 and should not fool try_cursor_movement below. */
15428 current_matrix_up_to_date_p = 0;
15429 }
15430
15431 /* Some sanity checks. */
15432 CHECK_WINDOW_END (w);
15433 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15434 emacs_abort ();
15435 if (BYTEPOS (opoint) < CHARPOS (opoint))
15436 emacs_abort ();
15437
15438 if (mode_line_update_needed (w))
15439 update_mode_line = 1;
15440
15441 /* Point refers normally to the selected window. For any other
15442 window, set up appropriate value. */
15443 if (!EQ (window, selected_window))
15444 {
15445 ptrdiff_t new_pt = marker_position (w->pointm);
15446 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15447 if (new_pt < BEGV)
15448 {
15449 new_pt = BEGV;
15450 new_pt_byte = BEGV_BYTE;
15451 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15452 }
15453 else if (new_pt > (ZV - 1))
15454 {
15455 new_pt = ZV;
15456 new_pt_byte = ZV_BYTE;
15457 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15458 }
15459
15460 /* We don't use SET_PT so that the point-motion hooks don't run. */
15461 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15462 }
15463
15464 /* If any of the character widths specified in the display table
15465 have changed, invalidate the width run cache. It's true that
15466 this may be a bit late to catch such changes, but the rest of
15467 redisplay goes (non-fatally) haywire when the display table is
15468 changed, so why should we worry about doing any better? */
15469 if (current_buffer->width_run_cache)
15470 {
15471 struct Lisp_Char_Table *disptab = buffer_display_table ();
15472
15473 if (! disptab_matches_widthtab
15474 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15475 {
15476 invalidate_region_cache (current_buffer,
15477 current_buffer->width_run_cache,
15478 BEG, Z);
15479 recompute_width_table (current_buffer, disptab);
15480 }
15481 }
15482
15483 /* If window-start is screwed up, choose a new one. */
15484 if (XMARKER (w->start)->buffer != current_buffer)
15485 goto recenter;
15486
15487 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15488
15489 /* If someone specified a new starting point but did not insist,
15490 check whether it can be used. */
15491 if (w->optional_new_start
15492 && CHARPOS (startp) >= BEGV
15493 && CHARPOS (startp) <= ZV)
15494 {
15495 w->optional_new_start = 0;
15496 start_display (&it, w, startp);
15497 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15498 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15499 if (IT_CHARPOS (it) == PT)
15500 w->force_start = 1;
15501 /* IT may overshoot PT if text at PT is invisible. */
15502 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15503 w->force_start = 1;
15504 }
15505
15506 force_start:
15507
15508 /* Handle case where place to start displaying has been specified,
15509 unless the specified location is outside the accessible range. */
15510 if (w->force_start || window_frozen_p (w))
15511 {
15512 /* We set this later on if we have to adjust point. */
15513 int new_vpos = -1;
15514
15515 w->force_start = 0;
15516 w->vscroll = 0;
15517 w->window_end_valid = 0;
15518
15519 /* Forget any recorded base line for line number display. */
15520 if (!buffer_unchanged_p)
15521 w->base_line_number = 0;
15522
15523 /* Redisplay the mode line. Select the buffer properly for that.
15524 Also, run the hook window-scroll-functions
15525 because we have scrolled. */
15526 /* Note, we do this after clearing force_start because
15527 if there's an error, it is better to forget about force_start
15528 than to get into an infinite loop calling the hook functions
15529 and having them get more errors. */
15530 if (!update_mode_line
15531 || ! NILP (Vwindow_scroll_functions))
15532 {
15533 update_mode_line = 1;
15534 w->update_mode_line = 1;
15535 startp = run_window_scroll_functions (window, startp);
15536 }
15537
15538 if (CHARPOS (startp) < BEGV)
15539 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15540 else if (CHARPOS (startp) > ZV)
15541 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15542
15543 /* Redisplay, then check if cursor has been set during the
15544 redisplay. Give up if new fonts were loaded. */
15545 /* We used to issue a CHECK_MARGINS argument to try_window here,
15546 but this causes scrolling to fail when point begins inside
15547 the scroll margin (bug#148) -- cyd */
15548 if (!try_window (window, startp, 0))
15549 {
15550 w->force_start = 1;
15551 clear_glyph_matrix (w->desired_matrix);
15552 goto need_larger_matrices;
15553 }
15554
15555 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15556 {
15557 /* If point does not appear, try to move point so it does
15558 appear. The desired matrix has been built above, so we
15559 can use it here. */
15560 new_vpos = window_box_height (w) / 2;
15561 }
15562
15563 if (!cursor_row_fully_visible_p (w, 0, 0))
15564 {
15565 /* Point does appear, but on a line partly visible at end of window.
15566 Move it back to a fully-visible line. */
15567 new_vpos = window_box_height (w);
15568 }
15569 else if (w->cursor.vpos >= 0)
15570 {
15571 /* Some people insist on not letting point enter the scroll
15572 margin, even though this part handles windows that didn't
15573 scroll at all. */
15574 int window_total_lines
15575 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15576 int margin = min (scroll_margin, window_total_lines / 4);
15577 int pixel_margin = margin * frame_line_height;
15578 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15579
15580 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15581 below, which finds the row to move point to, advances by
15582 the Y coordinate of the _next_ row, see the definition of
15583 MATRIX_ROW_BOTTOM_Y. */
15584 if (w->cursor.vpos < margin + header_line)
15585 {
15586 w->cursor.vpos = -1;
15587 clear_glyph_matrix (w->desired_matrix);
15588 goto try_to_scroll;
15589 }
15590 else
15591 {
15592 int window_height = window_box_height (w);
15593
15594 if (header_line)
15595 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15596 if (w->cursor.y >= window_height - pixel_margin)
15597 {
15598 w->cursor.vpos = -1;
15599 clear_glyph_matrix (w->desired_matrix);
15600 goto try_to_scroll;
15601 }
15602 }
15603 }
15604
15605 /* If we need to move point for either of the above reasons,
15606 now actually do it. */
15607 if (new_vpos >= 0)
15608 {
15609 struct glyph_row *row;
15610
15611 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15612 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15613 ++row;
15614
15615 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15616 MATRIX_ROW_START_BYTEPOS (row));
15617
15618 if (w != XWINDOW (selected_window))
15619 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15620 else if (current_buffer == old)
15621 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15622
15623 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15624
15625 /* If we are highlighting the region, then we just changed
15626 the region, so redisplay to show it. */
15627 /* FIXME: We need to (re)run pre-redisplay-function! */
15628 /* if (markpos_of_region () >= 0)
15629 {
15630 clear_glyph_matrix (w->desired_matrix);
15631 if (!try_window (window, startp, 0))
15632 goto need_larger_matrices;
15633 }
15634 */
15635 }
15636
15637 #ifdef GLYPH_DEBUG
15638 debug_method_add (w, "forced window start");
15639 #endif
15640 goto done;
15641 }
15642
15643 /* Handle case where text has not changed, only point, and it has
15644 not moved off the frame, and we are not retrying after hscroll.
15645 (current_matrix_up_to_date_p is nonzero when retrying.) */
15646 if (current_matrix_up_to_date_p
15647 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15648 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15649 {
15650 switch (rc)
15651 {
15652 case CURSOR_MOVEMENT_SUCCESS:
15653 used_current_matrix_p = 1;
15654 goto done;
15655
15656 case CURSOR_MOVEMENT_MUST_SCROLL:
15657 goto try_to_scroll;
15658
15659 default:
15660 emacs_abort ();
15661 }
15662 }
15663 /* If current starting point was originally the beginning of a line
15664 but no longer is, find a new starting point. */
15665 else if (w->start_at_line_beg
15666 && !(CHARPOS (startp) <= BEGV
15667 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15668 {
15669 #ifdef GLYPH_DEBUG
15670 debug_method_add (w, "recenter 1");
15671 #endif
15672 goto recenter;
15673 }
15674
15675 /* Try scrolling with try_window_id. Value is > 0 if update has
15676 been done, it is -1 if we know that the same window start will
15677 not work. It is 0 if unsuccessful for some other reason. */
15678 else if ((tem = try_window_id (w)) != 0)
15679 {
15680 #ifdef GLYPH_DEBUG
15681 debug_method_add (w, "try_window_id %d", tem);
15682 #endif
15683
15684 if (f->fonts_changed)
15685 goto need_larger_matrices;
15686 if (tem > 0)
15687 goto done;
15688
15689 /* Otherwise try_window_id has returned -1 which means that we
15690 don't want the alternative below this comment to execute. */
15691 }
15692 else if (CHARPOS (startp) >= BEGV
15693 && CHARPOS (startp) <= ZV
15694 && PT >= CHARPOS (startp)
15695 && (CHARPOS (startp) < ZV
15696 /* Avoid starting at end of buffer. */
15697 || CHARPOS (startp) == BEGV
15698 || !window_outdated (w)))
15699 {
15700 int d1, d2, d3, d4, d5, d6;
15701
15702 /* If first window line is a continuation line, and window start
15703 is inside the modified region, but the first change is before
15704 current window start, we must select a new window start.
15705
15706 However, if this is the result of a down-mouse event (e.g. by
15707 extending the mouse-drag-overlay), we don't want to select a
15708 new window start, since that would change the position under
15709 the mouse, resulting in an unwanted mouse-movement rather
15710 than a simple mouse-click. */
15711 if (!w->start_at_line_beg
15712 && NILP (do_mouse_tracking)
15713 && CHARPOS (startp) > BEGV
15714 && CHARPOS (startp) > BEG + beg_unchanged
15715 && CHARPOS (startp) <= Z - end_unchanged
15716 /* Even if w->start_at_line_beg is nil, a new window may
15717 start at a line_beg, since that's how set_buffer_window
15718 sets it. So, we need to check the return value of
15719 compute_window_start_on_continuation_line. (See also
15720 bug#197). */
15721 && XMARKER (w->start)->buffer == current_buffer
15722 && compute_window_start_on_continuation_line (w)
15723 /* It doesn't make sense to force the window start like we
15724 do at label force_start if it is already known that point
15725 will not be visible in the resulting window, because
15726 doing so will move point from its correct position
15727 instead of scrolling the window to bring point into view.
15728 See bug#9324. */
15729 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15730 {
15731 w->force_start = 1;
15732 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15733 goto force_start;
15734 }
15735
15736 #ifdef GLYPH_DEBUG
15737 debug_method_add (w, "same window start");
15738 #endif
15739
15740 /* Try to redisplay starting at same place as before.
15741 If point has not moved off frame, accept the results. */
15742 if (!current_matrix_up_to_date_p
15743 /* Don't use try_window_reusing_current_matrix in this case
15744 because a window scroll function can have changed the
15745 buffer. */
15746 || !NILP (Vwindow_scroll_functions)
15747 || MINI_WINDOW_P (w)
15748 || !(used_current_matrix_p
15749 = try_window_reusing_current_matrix (w)))
15750 {
15751 IF_DEBUG (debug_method_add (w, "1"));
15752 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15753 /* -1 means we need to scroll.
15754 0 means we need new matrices, but fonts_changed
15755 is set in that case, so we will detect it below. */
15756 goto try_to_scroll;
15757 }
15758
15759 if (f->fonts_changed)
15760 goto need_larger_matrices;
15761
15762 if (w->cursor.vpos >= 0)
15763 {
15764 if (!just_this_one_p
15765 || current_buffer->clip_changed
15766 || BEG_UNCHANGED < CHARPOS (startp))
15767 /* Forget any recorded base line for line number display. */
15768 w->base_line_number = 0;
15769
15770 if (!cursor_row_fully_visible_p (w, 1, 0))
15771 {
15772 clear_glyph_matrix (w->desired_matrix);
15773 last_line_misfit = 1;
15774 }
15775 /* Drop through and scroll. */
15776 else
15777 goto done;
15778 }
15779 else
15780 clear_glyph_matrix (w->desired_matrix);
15781 }
15782
15783 try_to_scroll:
15784
15785 /* Redisplay the mode line. Select the buffer properly for that. */
15786 if (!update_mode_line)
15787 {
15788 update_mode_line = 1;
15789 w->update_mode_line = 1;
15790 }
15791
15792 /* Try to scroll by specified few lines. */
15793 if ((scroll_conservatively
15794 || emacs_scroll_step
15795 || temp_scroll_step
15796 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15797 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15798 && CHARPOS (startp) >= BEGV
15799 && CHARPOS (startp) <= ZV)
15800 {
15801 /* The function returns -1 if new fonts were loaded, 1 if
15802 successful, 0 if not successful. */
15803 int ss = try_scrolling (window, just_this_one_p,
15804 scroll_conservatively,
15805 emacs_scroll_step,
15806 temp_scroll_step, last_line_misfit);
15807 switch (ss)
15808 {
15809 case SCROLLING_SUCCESS:
15810 goto done;
15811
15812 case SCROLLING_NEED_LARGER_MATRICES:
15813 goto need_larger_matrices;
15814
15815 case SCROLLING_FAILED:
15816 break;
15817
15818 default:
15819 emacs_abort ();
15820 }
15821 }
15822
15823 /* Finally, just choose a place to start which positions point
15824 according to user preferences. */
15825
15826 recenter:
15827
15828 #ifdef GLYPH_DEBUG
15829 debug_method_add (w, "recenter");
15830 #endif
15831
15832 /* Forget any previously recorded base line for line number display. */
15833 if (!buffer_unchanged_p)
15834 w->base_line_number = 0;
15835
15836 /* Determine the window start relative to point. */
15837 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15838 it.current_y = it.last_visible_y;
15839 if (centering_position < 0)
15840 {
15841 int window_total_lines
15842 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15843 int margin =
15844 scroll_margin > 0
15845 ? min (scroll_margin, window_total_lines / 4)
15846 : 0;
15847 ptrdiff_t margin_pos = CHARPOS (startp);
15848 Lisp_Object aggressive;
15849 int scrolling_up;
15850
15851 /* If there is a scroll margin at the top of the window, find
15852 its character position. */
15853 if (margin
15854 /* Cannot call start_display if startp is not in the
15855 accessible region of the buffer. This can happen when we
15856 have just switched to a different buffer and/or changed
15857 its restriction. In that case, startp is initialized to
15858 the character position 1 (BEGV) because we did not yet
15859 have chance to display the buffer even once. */
15860 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15861 {
15862 struct it it1;
15863 void *it1data = NULL;
15864
15865 SAVE_IT (it1, it, it1data);
15866 start_display (&it1, w, startp);
15867 move_it_vertically (&it1, margin * frame_line_height);
15868 margin_pos = IT_CHARPOS (it1);
15869 RESTORE_IT (&it, &it, it1data);
15870 }
15871 scrolling_up = PT > margin_pos;
15872 aggressive =
15873 scrolling_up
15874 ? BVAR (current_buffer, scroll_up_aggressively)
15875 : BVAR (current_buffer, scroll_down_aggressively);
15876
15877 if (!MINI_WINDOW_P (w)
15878 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15879 {
15880 int pt_offset = 0;
15881
15882 /* Setting scroll-conservatively overrides
15883 scroll-*-aggressively. */
15884 if (!scroll_conservatively && NUMBERP (aggressive))
15885 {
15886 double float_amount = XFLOATINT (aggressive);
15887
15888 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15889 if (pt_offset == 0 && float_amount > 0)
15890 pt_offset = 1;
15891 if (pt_offset && margin > 0)
15892 margin -= 1;
15893 }
15894 /* Compute how much to move the window start backward from
15895 point so that point will be displayed where the user
15896 wants it. */
15897 if (scrolling_up)
15898 {
15899 centering_position = it.last_visible_y;
15900 if (pt_offset)
15901 centering_position -= pt_offset;
15902 centering_position -=
15903 frame_line_height * (1 + margin + (last_line_misfit != 0))
15904 + WINDOW_HEADER_LINE_HEIGHT (w);
15905 /* Don't let point enter the scroll margin near top of
15906 the window. */
15907 if (centering_position < margin * frame_line_height)
15908 centering_position = margin * frame_line_height;
15909 }
15910 else
15911 centering_position = margin * frame_line_height + pt_offset;
15912 }
15913 else
15914 /* Set the window start half the height of the window backward
15915 from point. */
15916 centering_position = window_box_height (w) / 2;
15917 }
15918 move_it_vertically_backward (&it, centering_position);
15919
15920 eassert (IT_CHARPOS (it) >= BEGV);
15921
15922 /* The function move_it_vertically_backward may move over more
15923 than the specified y-distance. If it->w is small, e.g. a
15924 mini-buffer window, we may end up in front of the window's
15925 display area. Start displaying at the start of the line
15926 containing PT in this case. */
15927 if (it.current_y <= 0)
15928 {
15929 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15930 move_it_vertically_backward (&it, 0);
15931 it.current_y = 0;
15932 }
15933
15934 it.current_x = it.hpos = 0;
15935
15936 /* Set the window start position here explicitly, to avoid an
15937 infinite loop in case the functions in window-scroll-functions
15938 get errors. */
15939 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15940
15941 /* Run scroll hooks. */
15942 startp = run_window_scroll_functions (window, it.current.pos);
15943
15944 /* Redisplay the window. */
15945 if (!current_matrix_up_to_date_p
15946 || windows_or_buffers_changed
15947 || f->cursor_type_changed
15948 /* Don't use try_window_reusing_current_matrix in this case
15949 because it can have changed the buffer. */
15950 || !NILP (Vwindow_scroll_functions)
15951 || !just_this_one_p
15952 || MINI_WINDOW_P (w)
15953 || !(used_current_matrix_p
15954 = try_window_reusing_current_matrix (w)))
15955 try_window (window, startp, 0);
15956
15957 /* If new fonts have been loaded (due to fontsets), give up. We
15958 have to start a new redisplay since we need to re-adjust glyph
15959 matrices. */
15960 if (f->fonts_changed)
15961 goto need_larger_matrices;
15962
15963 /* If cursor did not appear assume that the middle of the window is
15964 in the first line of the window. Do it again with the next line.
15965 (Imagine a window of height 100, displaying two lines of height
15966 60. Moving back 50 from it->last_visible_y will end in the first
15967 line.) */
15968 if (w->cursor.vpos < 0)
15969 {
15970 if (w->window_end_valid && PT >= Z - w->window_end_pos)
15971 {
15972 clear_glyph_matrix (w->desired_matrix);
15973 move_it_by_lines (&it, 1);
15974 try_window (window, it.current.pos, 0);
15975 }
15976 else if (PT < IT_CHARPOS (it))
15977 {
15978 clear_glyph_matrix (w->desired_matrix);
15979 move_it_by_lines (&it, -1);
15980 try_window (window, it.current.pos, 0);
15981 }
15982 else
15983 {
15984 /* Not much we can do about it. */
15985 }
15986 }
15987
15988 /* Consider the following case: Window starts at BEGV, there is
15989 invisible, intangible text at BEGV, so that display starts at
15990 some point START > BEGV. It can happen that we are called with
15991 PT somewhere between BEGV and START. Try to handle that case. */
15992 if (w->cursor.vpos < 0)
15993 {
15994 struct glyph_row *row = w->current_matrix->rows;
15995 if (row->mode_line_p)
15996 ++row;
15997 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15998 }
15999
16000 if (!cursor_row_fully_visible_p (w, 0, 0))
16001 {
16002 /* If vscroll is enabled, disable it and try again. */
16003 if (w->vscroll)
16004 {
16005 w->vscroll = 0;
16006 clear_glyph_matrix (w->desired_matrix);
16007 goto recenter;
16008 }
16009
16010 /* Users who set scroll-conservatively to a large number want
16011 point just above/below the scroll margin. If we ended up
16012 with point's row partially visible, move the window start to
16013 make that row fully visible and out of the margin. */
16014 if (scroll_conservatively > SCROLL_LIMIT)
16015 {
16016 int window_total_lines
16017 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16018 int margin =
16019 scroll_margin > 0
16020 ? min (scroll_margin, window_total_lines / 4)
16021 : 0;
16022 int move_down = w->cursor.vpos >= window_total_lines / 2;
16023
16024 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16025 clear_glyph_matrix (w->desired_matrix);
16026 if (1 == try_window (window, it.current.pos,
16027 TRY_WINDOW_CHECK_MARGINS))
16028 goto done;
16029 }
16030
16031 /* If centering point failed to make the whole line visible,
16032 put point at the top instead. That has to make the whole line
16033 visible, if it can be done. */
16034 if (centering_position == 0)
16035 goto done;
16036
16037 clear_glyph_matrix (w->desired_matrix);
16038 centering_position = 0;
16039 goto recenter;
16040 }
16041
16042 done:
16043
16044 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16045 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16046 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16047
16048 /* Display the mode line, if we must. */
16049 if ((update_mode_line
16050 /* If window not full width, must redo its mode line
16051 if (a) the window to its side is being redone and
16052 (b) we do a frame-based redisplay. This is a consequence
16053 of how inverted lines are drawn in frame-based redisplay. */
16054 || (!just_this_one_p
16055 && !FRAME_WINDOW_P (f)
16056 && !WINDOW_FULL_WIDTH_P (w))
16057 /* Line number to display. */
16058 || w->base_line_pos > 0
16059 /* Column number is displayed and different from the one displayed. */
16060 || (w->column_number_displayed != -1
16061 && (w->column_number_displayed != current_column ())))
16062 /* This means that the window has a mode line. */
16063 && (WINDOW_WANTS_MODELINE_P (w)
16064 || WINDOW_WANTS_HEADER_LINE_P (w)))
16065 {
16066 display_mode_lines (w);
16067
16068 /* If mode line height has changed, arrange for a thorough
16069 immediate redisplay using the correct mode line height. */
16070 if (WINDOW_WANTS_MODELINE_P (w)
16071 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16072 {
16073 f->fonts_changed = 1;
16074 w->mode_line_height = -1;
16075 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16076 = DESIRED_MODE_LINE_HEIGHT (w);
16077 }
16078
16079 /* If header line height has changed, arrange for a thorough
16080 immediate redisplay using the correct header line height. */
16081 if (WINDOW_WANTS_HEADER_LINE_P (w)
16082 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16083 {
16084 f->fonts_changed = 1;
16085 w->header_line_height = -1;
16086 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16087 = DESIRED_HEADER_LINE_HEIGHT (w);
16088 }
16089
16090 if (f->fonts_changed)
16091 goto need_larger_matrices;
16092 }
16093
16094 if (!line_number_displayed && w->base_line_pos != -1)
16095 {
16096 w->base_line_pos = 0;
16097 w->base_line_number = 0;
16098 }
16099
16100 finish_menu_bars:
16101
16102 /* When we reach a frame's selected window, redo the frame's menu bar. */
16103 if (update_mode_line
16104 && EQ (FRAME_SELECTED_WINDOW (f), window))
16105 {
16106 int redisplay_menu_p = 0;
16107
16108 if (FRAME_WINDOW_P (f))
16109 {
16110 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16111 || defined (HAVE_NS) || defined (USE_GTK)
16112 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16113 #else
16114 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16115 #endif
16116 }
16117 else
16118 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16119
16120 if (redisplay_menu_p)
16121 display_menu_bar (w);
16122
16123 #ifdef HAVE_WINDOW_SYSTEM
16124 if (FRAME_WINDOW_P (f))
16125 {
16126 #if defined (USE_GTK) || defined (HAVE_NS)
16127 if (FRAME_EXTERNAL_TOOL_BAR (f))
16128 redisplay_tool_bar (f);
16129 #else
16130 if (WINDOWP (f->tool_bar_window)
16131 && (FRAME_TOOL_BAR_LINES (f) > 0
16132 || !NILP (Vauto_resize_tool_bars))
16133 && redisplay_tool_bar (f))
16134 ignore_mouse_drag_p = 1;
16135 #endif
16136 }
16137 #endif
16138 }
16139
16140 #ifdef HAVE_WINDOW_SYSTEM
16141 if (FRAME_WINDOW_P (f)
16142 && update_window_fringes (w, (just_this_one_p
16143 || (!used_current_matrix_p && !overlay_arrow_seen)
16144 || w->pseudo_window_p)))
16145 {
16146 update_begin (f);
16147 block_input ();
16148 if (draw_window_fringes (w, 1))
16149 x_draw_vertical_border (w);
16150 unblock_input ();
16151 update_end (f);
16152 }
16153 #endif /* HAVE_WINDOW_SYSTEM */
16154
16155 /* We go to this label, with fonts_changed set, if it is
16156 necessary to try again using larger glyph matrices.
16157 We have to redeem the scroll bar even in this case,
16158 because the loop in redisplay_internal expects that. */
16159 need_larger_matrices:
16160 ;
16161 finish_scroll_bars:
16162
16163 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16164 {
16165 /* Set the thumb's position and size. */
16166 set_vertical_scroll_bar (w);
16167
16168 /* Note that we actually used the scroll bar attached to this
16169 window, so it shouldn't be deleted at the end of redisplay. */
16170 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16171 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16172 }
16173
16174 /* Restore current_buffer and value of point in it. The window
16175 update may have changed the buffer, so first make sure `opoint'
16176 is still valid (Bug#6177). */
16177 if (CHARPOS (opoint) < BEGV)
16178 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16179 else if (CHARPOS (opoint) > ZV)
16180 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16181 else
16182 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16183
16184 set_buffer_internal_1 (old);
16185 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16186 shorter. This can be caused by log truncation in *Messages*. */
16187 if (CHARPOS (lpoint) <= ZV)
16188 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16189
16190 unbind_to (count, Qnil);
16191 }
16192
16193
16194 /* Build the complete desired matrix of WINDOW with a window start
16195 buffer position POS.
16196
16197 Value is 1 if successful. It is zero if fonts were loaded during
16198 redisplay which makes re-adjusting glyph matrices necessary, and -1
16199 if point would appear in the scroll margins.
16200 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16201 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16202 set in FLAGS.) */
16203
16204 int
16205 try_window (Lisp_Object window, struct text_pos pos, int flags)
16206 {
16207 struct window *w = XWINDOW (window);
16208 struct it it;
16209 struct glyph_row *last_text_row = NULL;
16210 struct frame *f = XFRAME (w->frame);
16211 int frame_line_height = default_line_pixel_height (w);
16212
16213 /* Make POS the new window start. */
16214 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16215
16216 /* Mark cursor position as unknown. No overlay arrow seen. */
16217 w->cursor.vpos = -1;
16218 overlay_arrow_seen = 0;
16219
16220 /* Initialize iterator and info to start at POS. */
16221 start_display (&it, w, pos);
16222
16223 /* Display all lines of W. */
16224 while (it.current_y < it.last_visible_y)
16225 {
16226 if (display_line (&it))
16227 last_text_row = it.glyph_row - 1;
16228 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16229 return 0;
16230 }
16231
16232 /* Don't let the cursor end in the scroll margins. */
16233 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16234 && !MINI_WINDOW_P (w))
16235 {
16236 int this_scroll_margin;
16237 int window_total_lines
16238 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16239
16240 if (scroll_margin > 0)
16241 {
16242 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16243 this_scroll_margin *= frame_line_height;
16244 }
16245 else
16246 this_scroll_margin = 0;
16247
16248 if ((w->cursor.y >= 0 /* not vscrolled */
16249 && w->cursor.y < this_scroll_margin
16250 && CHARPOS (pos) > BEGV
16251 && IT_CHARPOS (it) < ZV)
16252 /* rms: considering make_cursor_line_fully_visible_p here
16253 seems to give wrong results. We don't want to recenter
16254 when the last line is partly visible, we want to allow
16255 that case to be handled in the usual way. */
16256 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16257 {
16258 w->cursor.vpos = -1;
16259 clear_glyph_matrix (w->desired_matrix);
16260 return -1;
16261 }
16262 }
16263
16264 /* If bottom moved off end of frame, change mode line percentage. */
16265 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16266 w->update_mode_line = 1;
16267
16268 /* Set window_end_pos to the offset of the last character displayed
16269 on the window from the end of current_buffer. Set
16270 window_end_vpos to its row number. */
16271 if (last_text_row)
16272 {
16273 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16274 adjust_window_ends (w, last_text_row, 0);
16275 eassert
16276 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16277 w->window_end_vpos)));
16278 }
16279 else
16280 {
16281 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16282 w->window_end_pos = Z - ZV;
16283 w->window_end_vpos = 0;
16284 }
16285
16286 /* But that is not valid info until redisplay finishes. */
16287 w->window_end_valid = 0;
16288 return 1;
16289 }
16290
16291
16292 \f
16293 /************************************************************************
16294 Window redisplay reusing current matrix when buffer has not changed
16295 ************************************************************************/
16296
16297 /* Try redisplay of window W showing an unchanged buffer with a
16298 different window start than the last time it was displayed by
16299 reusing its current matrix. Value is non-zero if successful.
16300 W->start is the new window start. */
16301
16302 static int
16303 try_window_reusing_current_matrix (struct window *w)
16304 {
16305 struct frame *f = XFRAME (w->frame);
16306 struct glyph_row *bottom_row;
16307 struct it it;
16308 struct run run;
16309 struct text_pos start, new_start;
16310 int nrows_scrolled, i;
16311 struct glyph_row *last_text_row;
16312 struct glyph_row *last_reused_text_row;
16313 struct glyph_row *start_row;
16314 int start_vpos, min_y, max_y;
16315
16316 #ifdef GLYPH_DEBUG
16317 if (inhibit_try_window_reusing)
16318 return 0;
16319 #endif
16320
16321 if (/* This function doesn't handle terminal frames. */
16322 !FRAME_WINDOW_P (f)
16323 /* Don't try to reuse the display if windows have been split
16324 or such. */
16325 || windows_or_buffers_changed
16326 || f->cursor_type_changed)
16327 return 0;
16328
16329 /* Can't do this if showing trailing whitespace. */
16330 if (!NILP (Vshow_trailing_whitespace))
16331 return 0;
16332
16333 /* If top-line visibility has changed, give up. */
16334 if (WINDOW_WANTS_HEADER_LINE_P (w)
16335 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16336 return 0;
16337
16338 /* Give up if old or new display is scrolled vertically. We could
16339 make this function handle this, but right now it doesn't. */
16340 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16341 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16342 return 0;
16343
16344 /* The variable new_start now holds the new window start. The old
16345 start `start' can be determined from the current matrix. */
16346 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16347 start = start_row->minpos;
16348 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16349
16350 /* Clear the desired matrix for the display below. */
16351 clear_glyph_matrix (w->desired_matrix);
16352
16353 if (CHARPOS (new_start) <= CHARPOS (start))
16354 {
16355 /* Don't use this method if the display starts with an ellipsis
16356 displayed for invisible text. It's not easy to handle that case
16357 below, and it's certainly not worth the effort since this is
16358 not a frequent case. */
16359 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16360 return 0;
16361
16362 IF_DEBUG (debug_method_add (w, "twu1"));
16363
16364 /* Display up to a row that can be reused. The variable
16365 last_text_row is set to the last row displayed that displays
16366 text. Note that it.vpos == 0 if or if not there is a
16367 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16368 start_display (&it, w, new_start);
16369 w->cursor.vpos = -1;
16370 last_text_row = last_reused_text_row = NULL;
16371
16372 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16373 {
16374 /* If we have reached into the characters in the START row,
16375 that means the line boundaries have changed. So we
16376 can't start copying with the row START. Maybe it will
16377 work to start copying with the following row. */
16378 while (IT_CHARPOS (it) > CHARPOS (start))
16379 {
16380 /* Advance to the next row as the "start". */
16381 start_row++;
16382 start = start_row->minpos;
16383 /* If there are no more rows to try, or just one, give up. */
16384 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16385 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16386 || CHARPOS (start) == ZV)
16387 {
16388 clear_glyph_matrix (w->desired_matrix);
16389 return 0;
16390 }
16391
16392 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16393 }
16394 /* If we have reached alignment, we can copy the rest of the
16395 rows. */
16396 if (IT_CHARPOS (it) == CHARPOS (start)
16397 /* Don't accept "alignment" inside a display vector,
16398 since start_row could have started in the middle of
16399 that same display vector (thus their character
16400 positions match), and we have no way of telling if
16401 that is the case. */
16402 && it.current.dpvec_index < 0)
16403 break;
16404
16405 if (display_line (&it))
16406 last_text_row = it.glyph_row - 1;
16407
16408 }
16409
16410 /* A value of current_y < last_visible_y means that we stopped
16411 at the previous window start, which in turn means that we
16412 have at least one reusable row. */
16413 if (it.current_y < it.last_visible_y)
16414 {
16415 struct glyph_row *row;
16416
16417 /* IT.vpos always starts from 0; it counts text lines. */
16418 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16419
16420 /* Find PT if not already found in the lines displayed. */
16421 if (w->cursor.vpos < 0)
16422 {
16423 int dy = it.current_y - start_row->y;
16424
16425 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16426 row = row_containing_pos (w, PT, row, NULL, dy);
16427 if (row)
16428 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16429 dy, nrows_scrolled);
16430 else
16431 {
16432 clear_glyph_matrix (w->desired_matrix);
16433 return 0;
16434 }
16435 }
16436
16437 /* Scroll the display. Do it before the current matrix is
16438 changed. The problem here is that update has not yet
16439 run, i.e. part of the current matrix is not up to date.
16440 scroll_run_hook will clear the cursor, and use the
16441 current matrix to get the height of the row the cursor is
16442 in. */
16443 run.current_y = start_row->y;
16444 run.desired_y = it.current_y;
16445 run.height = it.last_visible_y - it.current_y;
16446
16447 if (run.height > 0 && run.current_y != run.desired_y)
16448 {
16449 update_begin (f);
16450 FRAME_RIF (f)->update_window_begin_hook (w);
16451 FRAME_RIF (f)->clear_window_mouse_face (w);
16452 FRAME_RIF (f)->scroll_run_hook (w, &run);
16453 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16454 update_end (f);
16455 }
16456
16457 /* Shift current matrix down by nrows_scrolled lines. */
16458 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16459 rotate_matrix (w->current_matrix,
16460 start_vpos,
16461 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16462 nrows_scrolled);
16463
16464 /* Disable lines that must be updated. */
16465 for (i = 0; i < nrows_scrolled; ++i)
16466 (start_row + i)->enabled_p = 0;
16467
16468 /* Re-compute Y positions. */
16469 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16470 max_y = it.last_visible_y;
16471 for (row = start_row + nrows_scrolled;
16472 row < bottom_row;
16473 ++row)
16474 {
16475 row->y = it.current_y;
16476 row->visible_height = row->height;
16477
16478 if (row->y < min_y)
16479 row->visible_height -= min_y - row->y;
16480 if (row->y + row->height > max_y)
16481 row->visible_height -= row->y + row->height - max_y;
16482 if (row->fringe_bitmap_periodic_p)
16483 row->redraw_fringe_bitmaps_p = 1;
16484
16485 it.current_y += row->height;
16486
16487 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16488 last_reused_text_row = row;
16489 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16490 break;
16491 }
16492
16493 /* Disable lines in the current matrix which are now
16494 below the window. */
16495 for (++row; row < bottom_row; ++row)
16496 row->enabled_p = row->mode_line_p = 0;
16497 }
16498
16499 /* Update window_end_pos etc.; last_reused_text_row is the last
16500 reused row from the current matrix containing text, if any.
16501 The value of last_text_row is the last displayed line
16502 containing text. */
16503 if (last_reused_text_row)
16504 adjust_window_ends (w, last_reused_text_row, 1);
16505 else if (last_text_row)
16506 adjust_window_ends (w, last_text_row, 0);
16507 else
16508 {
16509 /* This window must be completely empty. */
16510 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16511 w->window_end_pos = Z - ZV;
16512 w->window_end_vpos = 0;
16513 }
16514 w->window_end_valid = 0;
16515
16516 /* Update hint: don't try scrolling again in update_window. */
16517 w->desired_matrix->no_scrolling_p = 1;
16518
16519 #ifdef GLYPH_DEBUG
16520 debug_method_add (w, "try_window_reusing_current_matrix 1");
16521 #endif
16522 return 1;
16523 }
16524 else if (CHARPOS (new_start) > CHARPOS (start))
16525 {
16526 struct glyph_row *pt_row, *row;
16527 struct glyph_row *first_reusable_row;
16528 struct glyph_row *first_row_to_display;
16529 int dy;
16530 int yb = window_text_bottom_y (w);
16531
16532 /* Find the row starting at new_start, if there is one. Don't
16533 reuse a partially visible line at the end. */
16534 first_reusable_row = start_row;
16535 while (first_reusable_row->enabled_p
16536 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16537 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16538 < CHARPOS (new_start)))
16539 ++first_reusable_row;
16540
16541 /* Give up if there is no row to reuse. */
16542 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16543 || !first_reusable_row->enabled_p
16544 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16545 != CHARPOS (new_start)))
16546 return 0;
16547
16548 /* We can reuse fully visible rows beginning with
16549 first_reusable_row to the end of the window. Set
16550 first_row_to_display to the first row that cannot be reused.
16551 Set pt_row to the row containing point, if there is any. */
16552 pt_row = NULL;
16553 for (first_row_to_display = first_reusable_row;
16554 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16555 ++first_row_to_display)
16556 {
16557 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16558 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16559 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16560 && first_row_to_display->ends_at_zv_p
16561 && pt_row == NULL)))
16562 pt_row = first_row_to_display;
16563 }
16564
16565 /* Start displaying at the start of first_row_to_display. */
16566 eassert (first_row_to_display->y < yb);
16567 init_to_row_start (&it, w, first_row_to_display);
16568
16569 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16570 - start_vpos);
16571 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16572 - nrows_scrolled);
16573 it.current_y = (first_row_to_display->y - first_reusable_row->y
16574 + WINDOW_HEADER_LINE_HEIGHT (w));
16575
16576 /* Display lines beginning with first_row_to_display in the
16577 desired matrix. Set last_text_row to the last row displayed
16578 that displays text. */
16579 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16580 if (pt_row == NULL)
16581 w->cursor.vpos = -1;
16582 last_text_row = NULL;
16583 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16584 if (display_line (&it))
16585 last_text_row = it.glyph_row - 1;
16586
16587 /* If point is in a reused row, adjust y and vpos of the cursor
16588 position. */
16589 if (pt_row)
16590 {
16591 w->cursor.vpos -= nrows_scrolled;
16592 w->cursor.y -= first_reusable_row->y - start_row->y;
16593 }
16594
16595 /* Give up if point isn't in a row displayed or reused. (This
16596 also handles the case where w->cursor.vpos < nrows_scrolled
16597 after the calls to display_line, which can happen with scroll
16598 margins. See bug#1295.) */
16599 if (w->cursor.vpos < 0)
16600 {
16601 clear_glyph_matrix (w->desired_matrix);
16602 return 0;
16603 }
16604
16605 /* Scroll the display. */
16606 run.current_y = first_reusable_row->y;
16607 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16608 run.height = it.last_visible_y - run.current_y;
16609 dy = run.current_y - run.desired_y;
16610
16611 if (run.height)
16612 {
16613 update_begin (f);
16614 FRAME_RIF (f)->update_window_begin_hook (w);
16615 FRAME_RIF (f)->clear_window_mouse_face (w);
16616 FRAME_RIF (f)->scroll_run_hook (w, &run);
16617 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16618 update_end (f);
16619 }
16620
16621 /* Adjust Y positions of reused rows. */
16622 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16623 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16624 max_y = it.last_visible_y;
16625 for (row = first_reusable_row; row < first_row_to_display; ++row)
16626 {
16627 row->y -= dy;
16628 row->visible_height = row->height;
16629 if (row->y < min_y)
16630 row->visible_height -= min_y - row->y;
16631 if (row->y + row->height > max_y)
16632 row->visible_height -= row->y + row->height - max_y;
16633 if (row->fringe_bitmap_periodic_p)
16634 row->redraw_fringe_bitmaps_p = 1;
16635 }
16636
16637 /* Scroll the current matrix. */
16638 eassert (nrows_scrolled > 0);
16639 rotate_matrix (w->current_matrix,
16640 start_vpos,
16641 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16642 -nrows_scrolled);
16643
16644 /* Disable rows not reused. */
16645 for (row -= nrows_scrolled; row < bottom_row; ++row)
16646 row->enabled_p = 0;
16647
16648 /* Point may have moved to a different line, so we cannot assume that
16649 the previous cursor position is valid; locate the correct row. */
16650 if (pt_row)
16651 {
16652 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16653 row < bottom_row
16654 && PT >= MATRIX_ROW_END_CHARPOS (row)
16655 && !row->ends_at_zv_p;
16656 row++)
16657 {
16658 w->cursor.vpos++;
16659 w->cursor.y = row->y;
16660 }
16661 if (row < bottom_row)
16662 {
16663 /* Can't simply scan the row for point with
16664 bidi-reordered glyph rows. Let set_cursor_from_row
16665 figure out where to put the cursor, and if it fails,
16666 give up. */
16667 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16668 {
16669 if (!set_cursor_from_row (w, row, w->current_matrix,
16670 0, 0, 0, 0))
16671 {
16672 clear_glyph_matrix (w->desired_matrix);
16673 return 0;
16674 }
16675 }
16676 else
16677 {
16678 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16679 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16680
16681 for (; glyph < end
16682 && (!BUFFERP (glyph->object)
16683 || glyph->charpos < PT);
16684 glyph++)
16685 {
16686 w->cursor.hpos++;
16687 w->cursor.x += glyph->pixel_width;
16688 }
16689 }
16690 }
16691 }
16692
16693 /* Adjust window end. A null value of last_text_row means that
16694 the window end is in reused rows which in turn means that
16695 only its vpos can have changed. */
16696 if (last_text_row)
16697 adjust_window_ends (w, last_text_row, 0);
16698 else
16699 w->window_end_vpos -= nrows_scrolled;
16700
16701 w->window_end_valid = 0;
16702 w->desired_matrix->no_scrolling_p = 1;
16703
16704 #ifdef GLYPH_DEBUG
16705 debug_method_add (w, "try_window_reusing_current_matrix 2");
16706 #endif
16707 return 1;
16708 }
16709
16710 return 0;
16711 }
16712
16713
16714 \f
16715 /************************************************************************
16716 Window redisplay reusing current matrix when buffer has changed
16717 ************************************************************************/
16718
16719 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16720 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16721 ptrdiff_t *, ptrdiff_t *);
16722 static struct glyph_row *
16723 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16724 struct glyph_row *);
16725
16726
16727 /* Return the last row in MATRIX displaying text. If row START is
16728 non-null, start searching with that row. IT gives the dimensions
16729 of the display. Value is null if matrix is empty; otherwise it is
16730 a pointer to the row found. */
16731
16732 static struct glyph_row *
16733 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16734 struct glyph_row *start)
16735 {
16736 struct glyph_row *row, *row_found;
16737
16738 /* Set row_found to the last row in IT->w's current matrix
16739 displaying text. The loop looks funny but think of partially
16740 visible lines. */
16741 row_found = NULL;
16742 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16743 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16744 {
16745 eassert (row->enabled_p);
16746 row_found = row;
16747 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16748 break;
16749 ++row;
16750 }
16751
16752 return row_found;
16753 }
16754
16755
16756 /* Return the last row in the current matrix of W that is not affected
16757 by changes at the start of current_buffer that occurred since W's
16758 current matrix was built. Value is null if no such row exists.
16759
16760 BEG_UNCHANGED us the number of characters unchanged at the start of
16761 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16762 first changed character in current_buffer. Characters at positions <
16763 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16764 when the current matrix was built. */
16765
16766 static struct glyph_row *
16767 find_last_unchanged_at_beg_row (struct window *w)
16768 {
16769 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16770 struct glyph_row *row;
16771 struct glyph_row *row_found = NULL;
16772 int yb = window_text_bottom_y (w);
16773
16774 /* Find the last row displaying unchanged text. */
16775 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16776 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16777 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16778 ++row)
16779 {
16780 if (/* If row ends before first_changed_pos, it is unchanged,
16781 except in some case. */
16782 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16783 /* When row ends in ZV and we write at ZV it is not
16784 unchanged. */
16785 && !row->ends_at_zv_p
16786 /* When first_changed_pos is the end of a continued line,
16787 row is not unchanged because it may be no longer
16788 continued. */
16789 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16790 && (row->continued_p
16791 || row->exact_window_width_line_p))
16792 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16793 needs to be recomputed, so don't consider this row as
16794 unchanged. This happens when the last line was
16795 bidi-reordered and was killed immediately before this
16796 redisplay cycle. In that case, ROW->end stores the
16797 buffer position of the first visual-order character of
16798 the killed text, which is now beyond ZV. */
16799 && CHARPOS (row->end.pos) <= ZV)
16800 row_found = row;
16801
16802 /* Stop if last visible row. */
16803 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16804 break;
16805 }
16806
16807 return row_found;
16808 }
16809
16810
16811 /* Find the first glyph row in the current matrix of W that is not
16812 affected by changes at the end of current_buffer since the
16813 time W's current matrix was built.
16814
16815 Return in *DELTA the number of chars by which buffer positions in
16816 unchanged text at the end of current_buffer must be adjusted.
16817
16818 Return in *DELTA_BYTES the corresponding number of bytes.
16819
16820 Value is null if no such row exists, i.e. all rows are affected by
16821 changes. */
16822
16823 static struct glyph_row *
16824 find_first_unchanged_at_end_row (struct window *w,
16825 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16826 {
16827 struct glyph_row *row;
16828 struct glyph_row *row_found = NULL;
16829
16830 *delta = *delta_bytes = 0;
16831
16832 /* Display must not have been paused, otherwise the current matrix
16833 is not up to date. */
16834 eassert (w->window_end_valid);
16835
16836 /* A value of window_end_pos >= END_UNCHANGED means that the window
16837 end is in the range of changed text. If so, there is no
16838 unchanged row at the end of W's current matrix. */
16839 if (w->window_end_pos >= END_UNCHANGED)
16840 return NULL;
16841
16842 /* Set row to the last row in W's current matrix displaying text. */
16843 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16844
16845 /* If matrix is entirely empty, no unchanged row exists. */
16846 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16847 {
16848 /* The value of row is the last glyph row in the matrix having a
16849 meaningful buffer position in it. The end position of row
16850 corresponds to window_end_pos. This allows us to translate
16851 buffer positions in the current matrix to current buffer
16852 positions for characters not in changed text. */
16853 ptrdiff_t Z_old =
16854 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16855 ptrdiff_t Z_BYTE_old =
16856 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16857 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16858 struct glyph_row *first_text_row
16859 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16860
16861 *delta = Z - Z_old;
16862 *delta_bytes = Z_BYTE - Z_BYTE_old;
16863
16864 /* Set last_unchanged_pos to the buffer position of the last
16865 character in the buffer that has not been changed. Z is the
16866 index + 1 of the last character in current_buffer, i.e. by
16867 subtracting END_UNCHANGED we get the index of the last
16868 unchanged character, and we have to add BEG to get its buffer
16869 position. */
16870 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16871 last_unchanged_pos_old = last_unchanged_pos - *delta;
16872
16873 /* Search backward from ROW for a row displaying a line that
16874 starts at a minimum position >= last_unchanged_pos_old. */
16875 for (; row > first_text_row; --row)
16876 {
16877 /* This used to abort, but it can happen.
16878 It is ok to just stop the search instead here. KFS. */
16879 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16880 break;
16881
16882 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16883 row_found = row;
16884 }
16885 }
16886
16887 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16888
16889 return row_found;
16890 }
16891
16892
16893 /* Make sure that glyph rows in the current matrix of window W
16894 reference the same glyph memory as corresponding rows in the
16895 frame's frame matrix. This function is called after scrolling W's
16896 current matrix on a terminal frame in try_window_id and
16897 try_window_reusing_current_matrix. */
16898
16899 static void
16900 sync_frame_with_window_matrix_rows (struct window *w)
16901 {
16902 struct frame *f = XFRAME (w->frame);
16903 struct glyph_row *window_row, *window_row_end, *frame_row;
16904
16905 /* Preconditions: W must be a leaf window and full-width. Its frame
16906 must have a frame matrix. */
16907 eassert (BUFFERP (w->contents));
16908 eassert (WINDOW_FULL_WIDTH_P (w));
16909 eassert (!FRAME_WINDOW_P (f));
16910
16911 /* If W is a full-width window, glyph pointers in W's current matrix
16912 have, by definition, to be the same as glyph pointers in the
16913 corresponding frame matrix. Note that frame matrices have no
16914 marginal areas (see build_frame_matrix). */
16915 window_row = w->current_matrix->rows;
16916 window_row_end = window_row + w->current_matrix->nrows;
16917 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16918 while (window_row < window_row_end)
16919 {
16920 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16921 struct glyph *end = window_row->glyphs[LAST_AREA];
16922
16923 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16924 frame_row->glyphs[TEXT_AREA] = start;
16925 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16926 frame_row->glyphs[LAST_AREA] = end;
16927
16928 /* Disable frame rows whose corresponding window rows have
16929 been disabled in try_window_id. */
16930 if (!window_row->enabled_p)
16931 frame_row->enabled_p = 0;
16932
16933 ++window_row, ++frame_row;
16934 }
16935 }
16936
16937
16938 /* Find the glyph row in window W containing CHARPOS. Consider all
16939 rows between START and END (not inclusive). END null means search
16940 all rows to the end of the display area of W. Value is the row
16941 containing CHARPOS or null. */
16942
16943 struct glyph_row *
16944 row_containing_pos (struct window *w, ptrdiff_t charpos,
16945 struct glyph_row *start, struct glyph_row *end, int dy)
16946 {
16947 struct glyph_row *row = start;
16948 struct glyph_row *best_row = NULL;
16949 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16950 int last_y;
16951
16952 /* If we happen to start on a header-line, skip that. */
16953 if (row->mode_line_p)
16954 ++row;
16955
16956 if ((end && row >= end) || !row->enabled_p)
16957 return NULL;
16958
16959 last_y = window_text_bottom_y (w) - dy;
16960
16961 while (1)
16962 {
16963 /* Give up if we have gone too far. */
16964 if (end && row >= end)
16965 return NULL;
16966 /* This formerly returned if they were equal.
16967 I think that both quantities are of a "last plus one" type;
16968 if so, when they are equal, the row is within the screen. -- rms. */
16969 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16970 return NULL;
16971
16972 /* If it is in this row, return this row. */
16973 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16974 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16975 /* The end position of a row equals the start
16976 position of the next row. If CHARPOS is there, we
16977 would rather consider it displayed in the next
16978 line, except when this line ends in ZV. */
16979 && !row_for_charpos_p (row, charpos)))
16980 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16981 {
16982 struct glyph *g;
16983
16984 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
16985 || (!best_row && !row->continued_p))
16986 return row;
16987 /* In bidi-reordered rows, there could be several rows whose
16988 edges surround CHARPOS, all of these rows belonging to
16989 the same continued line. We need to find the row which
16990 fits CHARPOS the best. */
16991 for (g = row->glyphs[TEXT_AREA];
16992 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16993 g++)
16994 {
16995 if (!STRINGP (g->object))
16996 {
16997 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16998 {
16999 mindif = eabs (g->charpos - charpos);
17000 best_row = row;
17001 /* Exact match always wins. */
17002 if (mindif == 0)
17003 return best_row;
17004 }
17005 }
17006 }
17007 }
17008 else if (best_row && !row->continued_p)
17009 return best_row;
17010 ++row;
17011 }
17012 }
17013
17014
17015 /* Try to redisplay window W by reusing its existing display. W's
17016 current matrix must be up to date when this function is called,
17017 i.e. window_end_valid must be nonzero.
17018
17019 Value is
17020
17021 1 if display has been updated
17022 0 if otherwise unsuccessful
17023 -1 if redisplay with same window start is known not to succeed
17024
17025 The following steps are performed:
17026
17027 1. Find the last row in the current matrix of W that is not
17028 affected by changes at the start of current_buffer. If no such row
17029 is found, give up.
17030
17031 2. Find the first row in W's current matrix that is not affected by
17032 changes at the end of current_buffer. Maybe there is no such row.
17033
17034 3. Display lines beginning with the row + 1 found in step 1 to the
17035 row found in step 2 or, if step 2 didn't find a row, to the end of
17036 the window.
17037
17038 4. If cursor is not known to appear on the window, give up.
17039
17040 5. If display stopped at the row found in step 2, scroll the
17041 display and current matrix as needed.
17042
17043 6. Maybe display some lines at the end of W, if we must. This can
17044 happen under various circumstances, like a partially visible line
17045 becoming fully visible, or because newly displayed lines are displayed
17046 in smaller font sizes.
17047
17048 7. Update W's window end information. */
17049
17050 static int
17051 try_window_id (struct window *w)
17052 {
17053 struct frame *f = XFRAME (w->frame);
17054 struct glyph_matrix *current_matrix = w->current_matrix;
17055 struct glyph_matrix *desired_matrix = w->desired_matrix;
17056 struct glyph_row *last_unchanged_at_beg_row;
17057 struct glyph_row *first_unchanged_at_end_row;
17058 struct glyph_row *row;
17059 struct glyph_row *bottom_row;
17060 int bottom_vpos;
17061 struct it it;
17062 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17063 int dvpos, dy;
17064 struct text_pos start_pos;
17065 struct run run;
17066 int first_unchanged_at_end_vpos = 0;
17067 struct glyph_row *last_text_row, *last_text_row_at_end;
17068 struct text_pos start;
17069 ptrdiff_t first_changed_charpos, last_changed_charpos;
17070
17071 #ifdef GLYPH_DEBUG
17072 if (inhibit_try_window_id)
17073 return 0;
17074 #endif
17075
17076 /* This is handy for debugging. */
17077 #if 0
17078 #define GIVE_UP(X) \
17079 do { \
17080 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17081 return 0; \
17082 } while (0)
17083 #else
17084 #define GIVE_UP(X) return 0
17085 #endif
17086
17087 SET_TEXT_POS_FROM_MARKER (start, w->start);
17088
17089 /* Don't use this for mini-windows because these can show
17090 messages and mini-buffers, and we don't handle that here. */
17091 if (MINI_WINDOW_P (w))
17092 GIVE_UP (1);
17093
17094 /* This flag is used to prevent redisplay optimizations. */
17095 if (windows_or_buffers_changed || f->cursor_type_changed)
17096 GIVE_UP (2);
17097
17098 /* Verify that narrowing has not changed.
17099 Also verify that we were not told to prevent redisplay optimizations.
17100 It would be nice to further
17101 reduce the number of cases where this prevents try_window_id. */
17102 if (current_buffer->clip_changed
17103 || current_buffer->prevent_redisplay_optimizations_p)
17104 GIVE_UP (3);
17105
17106 /* Window must either use window-based redisplay or be full width. */
17107 if (!FRAME_WINDOW_P (f)
17108 && (!FRAME_LINE_INS_DEL_OK (f)
17109 || !WINDOW_FULL_WIDTH_P (w)))
17110 GIVE_UP (4);
17111
17112 /* Give up if point is known NOT to appear in W. */
17113 if (PT < CHARPOS (start))
17114 GIVE_UP (5);
17115
17116 /* Another way to prevent redisplay optimizations. */
17117 if (w->last_modified == 0)
17118 GIVE_UP (6);
17119
17120 /* Verify that window is not hscrolled. */
17121 if (w->hscroll != 0)
17122 GIVE_UP (7);
17123
17124 /* Verify that display wasn't paused. */
17125 if (!w->window_end_valid)
17126 GIVE_UP (8);
17127
17128 /* Likewise if highlighting trailing whitespace. */
17129 if (!NILP (Vshow_trailing_whitespace))
17130 GIVE_UP (11);
17131
17132 /* Can't use this if overlay arrow position and/or string have
17133 changed. */
17134 if (overlay_arrows_changed_p ())
17135 GIVE_UP (12);
17136
17137 /* When word-wrap is on, adding a space to the first word of a
17138 wrapped line can change the wrap position, altering the line
17139 above it. It might be worthwhile to handle this more
17140 intelligently, but for now just redisplay from scratch. */
17141 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17142 GIVE_UP (21);
17143
17144 /* Under bidi reordering, adding or deleting a character in the
17145 beginning of a paragraph, before the first strong directional
17146 character, can change the base direction of the paragraph (unless
17147 the buffer specifies a fixed paragraph direction), which will
17148 require to redisplay the whole paragraph. It might be worthwhile
17149 to find the paragraph limits and widen the range of redisplayed
17150 lines to that, but for now just give up this optimization and
17151 redisplay from scratch. */
17152 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17153 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17154 GIVE_UP (22);
17155
17156 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17157 only if buffer has really changed. The reason is that the gap is
17158 initially at Z for freshly visited files. The code below would
17159 set end_unchanged to 0 in that case. */
17160 if (MODIFF > SAVE_MODIFF
17161 /* This seems to happen sometimes after saving a buffer. */
17162 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17163 {
17164 if (GPT - BEG < BEG_UNCHANGED)
17165 BEG_UNCHANGED = GPT - BEG;
17166 if (Z - GPT < END_UNCHANGED)
17167 END_UNCHANGED = Z - GPT;
17168 }
17169
17170 /* The position of the first and last character that has been changed. */
17171 first_changed_charpos = BEG + BEG_UNCHANGED;
17172 last_changed_charpos = Z - END_UNCHANGED;
17173
17174 /* If window starts after a line end, and the last change is in
17175 front of that newline, then changes don't affect the display.
17176 This case happens with stealth-fontification. Note that although
17177 the display is unchanged, glyph positions in the matrix have to
17178 be adjusted, of course. */
17179 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17180 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17181 && ((last_changed_charpos < CHARPOS (start)
17182 && CHARPOS (start) == BEGV)
17183 || (last_changed_charpos < CHARPOS (start) - 1
17184 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17185 {
17186 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17187 struct glyph_row *r0;
17188
17189 /* Compute how many chars/bytes have been added to or removed
17190 from the buffer. */
17191 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17192 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17193 Z_delta = Z - Z_old;
17194 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17195
17196 /* Give up if PT is not in the window. Note that it already has
17197 been checked at the start of try_window_id that PT is not in
17198 front of the window start. */
17199 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17200 GIVE_UP (13);
17201
17202 /* If window start is unchanged, we can reuse the whole matrix
17203 as is, after adjusting glyph positions. No need to compute
17204 the window end again, since its offset from Z hasn't changed. */
17205 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17206 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17207 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17208 /* PT must not be in a partially visible line. */
17209 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17210 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17211 {
17212 /* Adjust positions in the glyph matrix. */
17213 if (Z_delta || Z_delta_bytes)
17214 {
17215 struct glyph_row *r1
17216 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17217 increment_matrix_positions (w->current_matrix,
17218 MATRIX_ROW_VPOS (r0, current_matrix),
17219 MATRIX_ROW_VPOS (r1, current_matrix),
17220 Z_delta, Z_delta_bytes);
17221 }
17222
17223 /* Set the cursor. */
17224 row = row_containing_pos (w, PT, r0, NULL, 0);
17225 if (row)
17226 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17227 return 1;
17228 }
17229 }
17230
17231 /* Handle the case that changes are all below what is displayed in
17232 the window, and that PT is in the window. This shortcut cannot
17233 be taken if ZV is visible in the window, and text has been added
17234 there that is visible in the window. */
17235 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17236 /* ZV is not visible in the window, or there are no
17237 changes at ZV, actually. */
17238 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17239 || first_changed_charpos == last_changed_charpos))
17240 {
17241 struct glyph_row *r0;
17242
17243 /* Give up if PT is not in the window. Note that it already has
17244 been checked at the start of try_window_id that PT is not in
17245 front of the window start. */
17246 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17247 GIVE_UP (14);
17248
17249 /* If window start is unchanged, we can reuse the whole matrix
17250 as is, without changing glyph positions since no text has
17251 been added/removed in front of the window end. */
17252 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17253 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17254 /* PT must not be in a partially visible line. */
17255 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17256 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17257 {
17258 /* We have to compute the window end anew since text
17259 could have been added/removed after it. */
17260 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17261 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17262
17263 /* Set the cursor. */
17264 row = row_containing_pos (w, PT, r0, NULL, 0);
17265 if (row)
17266 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17267 return 2;
17268 }
17269 }
17270
17271 /* Give up if window start is in the changed area.
17272
17273 The condition used to read
17274
17275 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17276
17277 but why that was tested escapes me at the moment. */
17278 if (CHARPOS (start) >= first_changed_charpos
17279 && CHARPOS (start) <= last_changed_charpos)
17280 GIVE_UP (15);
17281
17282 /* Check that window start agrees with the start of the first glyph
17283 row in its current matrix. Check this after we know the window
17284 start is not in changed text, otherwise positions would not be
17285 comparable. */
17286 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17287 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17288 GIVE_UP (16);
17289
17290 /* Give up if the window ends in strings. Overlay strings
17291 at the end are difficult to handle, so don't try. */
17292 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17293 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17294 GIVE_UP (20);
17295
17296 /* Compute the position at which we have to start displaying new
17297 lines. Some of the lines at the top of the window might be
17298 reusable because they are not displaying changed text. Find the
17299 last row in W's current matrix not affected by changes at the
17300 start of current_buffer. Value is null if changes start in the
17301 first line of window. */
17302 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17303 if (last_unchanged_at_beg_row)
17304 {
17305 /* Avoid starting to display in the middle of a character, a TAB
17306 for instance. This is easier than to set up the iterator
17307 exactly, and it's not a frequent case, so the additional
17308 effort wouldn't really pay off. */
17309 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17310 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17311 && last_unchanged_at_beg_row > w->current_matrix->rows)
17312 --last_unchanged_at_beg_row;
17313
17314 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17315 GIVE_UP (17);
17316
17317 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17318 GIVE_UP (18);
17319 start_pos = it.current.pos;
17320
17321 /* Start displaying new lines in the desired matrix at the same
17322 vpos we would use in the current matrix, i.e. below
17323 last_unchanged_at_beg_row. */
17324 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17325 current_matrix);
17326 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17327 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17328
17329 eassert (it.hpos == 0 && it.current_x == 0);
17330 }
17331 else
17332 {
17333 /* There are no reusable lines at the start of the window.
17334 Start displaying in the first text line. */
17335 start_display (&it, w, start);
17336 it.vpos = it.first_vpos;
17337 start_pos = it.current.pos;
17338 }
17339
17340 /* Find the first row that is not affected by changes at the end of
17341 the buffer. Value will be null if there is no unchanged row, in
17342 which case we must redisplay to the end of the window. delta
17343 will be set to the value by which buffer positions beginning with
17344 first_unchanged_at_end_row have to be adjusted due to text
17345 changes. */
17346 first_unchanged_at_end_row
17347 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17348 IF_DEBUG (debug_delta = delta);
17349 IF_DEBUG (debug_delta_bytes = delta_bytes);
17350
17351 /* Set stop_pos to the buffer position up to which we will have to
17352 display new lines. If first_unchanged_at_end_row != NULL, this
17353 is the buffer position of the start of the line displayed in that
17354 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17355 that we don't stop at a buffer position. */
17356 stop_pos = 0;
17357 if (first_unchanged_at_end_row)
17358 {
17359 eassert (last_unchanged_at_beg_row == NULL
17360 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17361
17362 /* If this is a continuation line, move forward to the next one
17363 that isn't. Changes in lines above affect this line.
17364 Caution: this may move first_unchanged_at_end_row to a row
17365 not displaying text. */
17366 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17367 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17368 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17369 < it.last_visible_y))
17370 ++first_unchanged_at_end_row;
17371
17372 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17373 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17374 >= it.last_visible_y))
17375 first_unchanged_at_end_row = NULL;
17376 else
17377 {
17378 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17379 + delta);
17380 first_unchanged_at_end_vpos
17381 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17382 eassert (stop_pos >= Z - END_UNCHANGED);
17383 }
17384 }
17385 else if (last_unchanged_at_beg_row == NULL)
17386 GIVE_UP (19);
17387
17388
17389 #ifdef GLYPH_DEBUG
17390
17391 /* Either there is no unchanged row at the end, or the one we have
17392 now displays text. This is a necessary condition for the window
17393 end pos calculation at the end of this function. */
17394 eassert (first_unchanged_at_end_row == NULL
17395 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17396
17397 debug_last_unchanged_at_beg_vpos
17398 = (last_unchanged_at_beg_row
17399 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17400 : -1);
17401 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17402
17403 #endif /* GLYPH_DEBUG */
17404
17405
17406 /* Display new lines. Set last_text_row to the last new line
17407 displayed which has text on it, i.e. might end up as being the
17408 line where the window_end_vpos is. */
17409 w->cursor.vpos = -1;
17410 last_text_row = NULL;
17411 overlay_arrow_seen = 0;
17412 while (it.current_y < it.last_visible_y
17413 && !f->fonts_changed
17414 && (first_unchanged_at_end_row == NULL
17415 || IT_CHARPOS (it) < stop_pos))
17416 {
17417 if (display_line (&it))
17418 last_text_row = it.glyph_row - 1;
17419 }
17420
17421 if (f->fonts_changed)
17422 return -1;
17423
17424
17425 /* Compute differences in buffer positions, y-positions etc. for
17426 lines reused at the bottom of the window. Compute what we can
17427 scroll. */
17428 if (first_unchanged_at_end_row
17429 /* No lines reused because we displayed everything up to the
17430 bottom of the window. */
17431 && it.current_y < it.last_visible_y)
17432 {
17433 dvpos = (it.vpos
17434 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17435 current_matrix));
17436 dy = it.current_y - first_unchanged_at_end_row->y;
17437 run.current_y = first_unchanged_at_end_row->y;
17438 run.desired_y = run.current_y + dy;
17439 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17440 }
17441 else
17442 {
17443 delta = delta_bytes = dvpos = dy
17444 = run.current_y = run.desired_y = run.height = 0;
17445 first_unchanged_at_end_row = NULL;
17446 }
17447 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17448
17449
17450 /* Find the cursor if not already found. We have to decide whether
17451 PT will appear on this window (it sometimes doesn't, but this is
17452 not a very frequent case.) This decision has to be made before
17453 the current matrix is altered. A value of cursor.vpos < 0 means
17454 that PT is either in one of the lines beginning at
17455 first_unchanged_at_end_row or below the window. Don't care for
17456 lines that might be displayed later at the window end; as
17457 mentioned, this is not a frequent case. */
17458 if (w->cursor.vpos < 0)
17459 {
17460 /* Cursor in unchanged rows at the top? */
17461 if (PT < CHARPOS (start_pos)
17462 && last_unchanged_at_beg_row)
17463 {
17464 row = row_containing_pos (w, PT,
17465 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17466 last_unchanged_at_beg_row + 1, 0);
17467 if (row)
17468 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17469 }
17470
17471 /* Start from first_unchanged_at_end_row looking for PT. */
17472 else if (first_unchanged_at_end_row)
17473 {
17474 row = row_containing_pos (w, PT - delta,
17475 first_unchanged_at_end_row, NULL, 0);
17476 if (row)
17477 set_cursor_from_row (w, row, w->current_matrix, delta,
17478 delta_bytes, dy, dvpos);
17479 }
17480
17481 /* Give up if cursor was not found. */
17482 if (w->cursor.vpos < 0)
17483 {
17484 clear_glyph_matrix (w->desired_matrix);
17485 return -1;
17486 }
17487 }
17488
17489 /* Don't let the cursor end in the scroll margins. */
17490 {
17491 int this_scroll_margin, cursor_height;
17492 int frame_line_height = default_line_pixel_height (w);
17493 int window_total_lines
17494 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17495
17496 this_scroll_margin =
17497 max (0, min (scroll_margin, window_total_lines / 4));
17498 this_scroll_margin *= frame_line_height;
17499 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17500
17501 if ((w->cursor.y < this_scroll_margin
17502 && CHARPOS (start) > BEGV)
17503 /* Old redisplay didn't take scroll margin into account at the bottom,
17504 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17505 || (w->cursor.y + (make_cursor_line_fully_visible_p
17506 ? cursor_height + this_scroll_margin
17507 : 1)) > it.last_visible_y)
17508 {
17509 w->cursor.vpos = -1;
17510 clear_glyph_matrix (w->desired_matrix);
17511 return -1;
17512 }
17513 }
17514
17515 /* Scroll the display. Do it before changing the current matrix so
17516 that xterm.c doesn't get confused about where the cursor glyph is
17517 found. */
17518 if (dy && run.height)
17519 {
17520 update_begin (f);
17521
17522 if (FRAME_WINDOW_P (f))
17523 {
17524 FRAME_RIF (f)->update_window_begin_hook (w);
17525 FRAME_RIF (f)->clear_window_mouse_face (w);
17526 FRAME_RIF (f)->scroll_run_hook (w, &run);
17527 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17528 }
17529 else
17530 {
17531 /* Terminal frame. In this case, dvpos gives the number of
17532 lines to scroll by; dvpos < 0 means scroll up. */
17533 int from_vpos
17534 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17535 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17536 int end = (WINDOW_TOP_EDGE_LINE (w)
17537 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17538 + window_internal_height (w));
17539
17540 #if defined (HAVE_GPM) || defined (MSDOS)
17541 x_clear_window_mouse_face (w);
17542 #endif
17543 /* Perform the operation on the screen. */
17544 if (dvpos > 0)
17545 {
17546 /* Scroll last_unchanged_at_beg_row to the end of the
17547 window down dvpos lines. */
17548 set_terminal_window (f, end);
17549
17550 /* On dumb terminals delete dvpos lines at the end
17551 before inserting dvpos empty lines. */
17552 if (!FRAME_SCROLL_REGION_OK (f))
17553 ins_del_lines (f, end - dvpos, -dvpos);
17554
17555 /* Insert dvpos empty lines in front of
17556 last_unchanged_at_beg_row. */
17557 ins_del_lines (f, from, dvpos);
17558 }
17559 else if (dvpos < 0)
17560 {
17561 /* Scroll up last_unchanged_at_beg_vpos to the end of
17562 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17563 set_terminal_window (f, end);
17564
17565 /* Delete dvpos lines in front of
17566 last_unchanged_at_beg_vpos. ins_del_lines will set
17567 the cursor to the given vpos and emit |dvpos| delete
17568 line sequences. */
17569 ins_del_lines (f, from + dvpos, dvpos);
17570
17571 /* On a dumb terminal insert dvpos empty lines at the
17572 end. */
17573 if (!FRAME_SCROLL_REGION_OK (f))
17574 ins_del_lines (f, end + dvpos, -dvpos);
17575 }
17576
17577 set_terminal_window (f, 0);
17578 }
17579
17580 update_end (f);
17581 }
17582
17583 /* Shift reused rows of the current matrix to the right position.
17584 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17585 text. */
17586 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17587 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17588 if (dvpos < 0)
17589 {
17590 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17591 bottom_vpos, dvpos);
17592 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17593 bottom_vpos);
17594 }
17595 else if (dvpos > 0)
17596 {
17597 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17598 bottom_vpos, dvpos);
17599 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17600 first_unchanged_at_end_vpos + dvpos);
17601 }
17602
17603 /* For frame-based redisplay, make sure that current frame and window
17604 matrix are in sync with respect to glyph memory. */
17605 if (!FRAME_WINDOW_P (f))
17606 sync_frame_with_window_matrix_rows (w);
17607
17608 /* Adjust buffer positions in reused rows. */
17609 if (delta || delta_bytes)
17610 increment_matrix_positions (current_matrix,
17611 first_unchanged_at_end_vpos + dvpos,
17612 bottom_vpos, delta, delta_bytes);
17613
17614 /* Adjust Y positions. */
17615 if (dy)
17616 shift_glyph_matrix (w, current_matrix,
17617 first_unchanged_at_end_vpos + dvpos,
17618 bottom_vpos, dy);
17619
17620 if (first_unchanged_at_end_row)
17621 {
17622 first_unchanged_at_end_row += dvpos;
17623 if (first_unchanged_at_end_row->y >= it.last_visible_y
17624 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17625 first_unchanged_at_end_row = NULL;
17626 }
17627
17628 /* If scrolling up, there may be some lines to display at the end of
17629 the window. */
17630 last_text_row_at_end = NULL;
17631 if (dy < 0)
17632 {
17633 /* Scrolling up can leave for example a partially visible line
17634 at the end of the window to be redisplayed. */
17635 /* Set last_row to the glyph row in the current matrix where the
17636 window end line is found. It has been moved up or down in
17637 the matrix by dvpos. */
17638 int last_vpos = w->window_end_vpos + dvpos;
17639 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17640
17641 /* If last_row is the window end line, it should display text. */
17642 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17643
17644 /* If window end line was partially visible before, begin
17645 displaying at that line. Otherwise begin displaying with the
17646 line following it. */
17647 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17648 {
17649 init_to_row_start (&it, w, last_row);
17650 it.vpos = last_vpos;
17651 it.current_y = last_row->y;
17652 }
17653 else
17654 {
17655 init_to_row_end (&it, w, last_row);
17656 it.vpos = 1 + last_vpos;
17657 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17658 ++last_row;
17659 }
17660
17661 /* We may start in a continuation line. If so, we have to
17662 get the right continuation_lines_width and current_x. */
17663 it.continuation_lines_width = last_row->continuation_lines_width;
17664 it.hpos = it.current_x = 0;
17665
17666 /* Display the rest of the lines at the window end. */
17667 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17668 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17669 {
17670 /* Is it always sure that the display agrees with lines in
17671 the current matrix? I don't think so, so we mark rows
17672 displayed invalid in the current matrix by setting their
17673 enabled_p flag to zero. */
17674 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17675 if (display_line (&it))
17676 last_text_row_at_end = it.glyph_row - 1;
17677 }
17678 }
17679
17680 /* Update window_end_pos and window_end_vpos. */
17681 if (first_unchanged_at_end_row && !last_text_row_at_end)
17682 {
17683 /* Window end line if one of the preserved rows from the current
17684 matrix. Set row to the last row displaying text in current
17685 matrix starting at first_unchanged_at_end_row, after
17686 scrolling. */
17687 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17688 row = find_last_row_displaying_text (w->current_matrix, &it,
17689 first_unchanged_at_end_row);
17690 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17691 adjust_window_ends (w, row, 1);
17692 eassert (w->window_end_bytepos >= 0);
17693 IF_DEBUG (debug_method_add (w, "A"));
17694 }
17695 else if (last_text_row_at_end)
17696 {
17697 adjust_window_ends (w, last_text_row_at_end, 0);
17698 eassert (w->window_end_bytepos >= 0);
17699 IF_DEBUG (debug_method_add (w, "B"));
17700 }
17701 else if (last_text_row)
17702 {
17703 /* We have displayed either to the end of the window or at the
17704 end of the window, i.e. the last row with text is to be found
17705 in the desired matrix. */
17706 adjust_window_ends (w, last_text_row, 0);
17707 eassert (w->window_end_bytepos >= 0);
17708 }
17709 else if (first_unchanged_at_end_row == NULL
17710 && last_text_row == NULL
17711 && last_text_row_at_end == NULL)
17712 {
17713 /* Displayed to end of window, but no line containing text was
17714 displayed. Lines were deleted at the end of the window. */
17715 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17716 int vpos = w->window_end_vpos;
17717 struct glyph_row *current_row = current_matrix->rows + vpos;
17718 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17719
17720 for (row = NULL;
17721 row == NULL && vpos >= first_vpos;
17722 --vpos, --current_row, --desired_row)
17723 {
17724 if (desired_row->enabled_p)
17725 {
17726 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17727 row = desired_row;
17728 }
17729 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17730 row = current_row;
17731 }
17732
17733 eassert (row != NULL);
17734 w->window_end_vpos = vpos + 1;
17735 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17736 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17737 eassert (w->window_end_bytepos >= 0);
17738 IF_DEBUG (debug_method_add (w, "C"));
17739 }
17740 else
17741 emacs_abort ();
17742
17743 IF_DEBUG (debug_end_pos = w->window_end_pos;
17744 debug_end_vpos = w->window_end_vpos);
17745
17746 /* Record that display has not been completed. */
17747 w->window_end_valid = 0;
17748 w->desired_matrix->no_scrolling_p = 1;
17749 return 3;
17750
17751 #undef GIVE_UP
17752 }
17753
17754
17755 \f
17756 /***********************************************************************
17757 More debugging support
17758 ***********************************************************************/
17759
17760 #ifdef GLYPH_DEBUG
17761
17762 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17763 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17764 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17765
17766
17767 /* Dump the contents of glyph matrix MATRIX on stderr.
17768
17769 GLYPHS 0 means don't show glyph contents.
17770 GLYPHS 1 means show glyphs in short form
17771 GLYPHS > 1 means show glyphs in long form. */
17772
17773 void
17774 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17775 {
17776 int i;
17777 for (i = 0; i < matrix->nrows; ++i)
17778 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17779 }
17780
17781
17782 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17783 the glyph row and area where the glyph comes from. */
17784
17785 void
17786 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17787 {
17788 if (glyph->type == CHAR_GLYPH
17789 || glyph->type == GLYPHLESS_GLYPH)
17790 {
17791 fprintf (stderr,
17792 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17793 glyph - row->glyphs[TEXT_AREA],
17794 (glyph->type == CHAR_GLYPH
17795 ? 'C'
17796 : 'G'),
17797 glyph->charpos,
17798 (BUFFERP (glyph->object)
17799 ? 'B'
17800 : (STRINGP (glyph->object)
17801 ? 'S'
17802 : (INTEGERP (glyph->object)
17803 ? '0'
17804 : '-'))),
17805 glyph->pixel_width,
17806 glyph->u.ch,
17807 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17808 ? glyph->u.ch
17809 : '.'),
17810 glyph->face_id,
17811 glyph->left_box_line_p,
17812 glyph->right_box_line_p);
17813 }
17814 else if (glyph->type == STRETCH_GLYPH)
17815 {
17816 fprintf (stderr,
17817 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17818 glyph - row->glyphs[TEXT_AREA],
17819 'S',
17820 glyph->charpos,
17821 (BUFFERP (glyph->object)
17822 ? 'B'
17823 : (STRINGP (glyph->object)
17824 ? 'S'
17825 : (INTEGERP (glyph->object)
17826 ? '0'
17827 : '-'))),
17828 glyph->pixel_width,
17829 0,
17830 ' ',
17831 glyph->face_id,
17832 glyph->left_box_line_p,
17833 glyph->right_box_line_p);
17834 }
17835 else if (glyph->type == IMAGE_GLYPH)
17836 {
17837 fprintf (stderr,
17838 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17839 glyph - row->glyphs[TEXT_AREA],
17840 'I',
17841 glyph->charpos,
17842 (BUFFERP (glyph->object)
17843 ? 'B'
17844 : (STRINGP (glyph->object)
17845 ? 'S'
17846 : (INTEGERP (glyph->object)
17847 ? '0'
17848 : '-'))),
17849 glyph->pixel_width,
17850 glyph->u.img_id,
17851 '.',
17852 glyph->face_id,
17853 glyph->left_box_line_p,
17854 glyph->right_box_line_p);
17855 }
17856 else if (glyph->type == COMPOSITE_GLYPH)
17857 {
17858 fprintf (stderr,
17859 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17860 glyph - row->glyphs[TEXT_AREA],
17861 '+',
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.cmp.id);
17872 if (glyph->u.cmp.automatic)
17873 fprintf (stderr,
17874 "[%d-%d]",
17875 glyph->slice.cmp.from, glyph->slice.cmp.to);
17876 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17877 glyph->face_id,
17878 glyph->left_box_line_p,
17879 glyph->right_box_line_p);
17880 }
17881 }
17882
17883
17884 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17885 GLYPHS 0 means don't show glyph contents.
17886 GLYPHS 1 means show glyphs in short form
17887 GLYPHS > 1 means show glyphs in long form. */
17888
17889 void
17890 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17891 {
17892 if (glyphs != 1)
17893 {
17894 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17895 fprintf (stderr, "==============================================================================\n");
17896
17897 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17898 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17899 vpos,
17900 MATRIX_ROW_START_CHARPOS (row),
17901 MATRIX_ROW_END_CHARPOS (row),
17902 row->used[TEXT_AREA],
17903 row->contains_overlapping_glyphs_p,
17904 row->enabled_p,
17905 row->truncated_on_left_p,
17906 row->truncated_on_right_p,
17907 row->continued_p,
17908 MATRIX_ROW_CONTINUATION_LINE_P (row),
17909 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17910 row->ends_at_zv_p,
17911 row->fill_line_p,
17912 row->ends_in_middle_of_char_p,
17913 row->starts_in_middle_of_char_p,
17914 row->mouse_face_p,
17915 row->x,
17916 row->y,
17917 row->pixel_width,
17918 row->height,
17919 row->visible_height,
17920 row->ascent,
17921 row->phys_ascent);
17922 /* The next 3 lines should align to "Start" in the header. */
17923 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17924 row->end.overlay_string_index,
17925 row->continuation_lines_width);
17926 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17927 CHARPOS (row->start.string_pos),
17928 CHARPOS (row->end.string_pos));
17929 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17930 row->end.dpvec_index);
17931 }
17932
17933 if (glyphs > 1)
17934 {
17935 int area;
17936
17937 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17938 {
17939 struct glyph *glyph = row->glyphs[area];
17940 struct glyph *glyph_end = glyph + row->used[area];
17941
17942 /* Glyph for a line end in text. */
17943 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17944 ++glyph_end;
17945
17946 if (glyph < glyph_end)
17947 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
17948
17949 for (; glyph < glyph_end; ++glyph)
17950 dump_glyph (row, glyph, area);
17951 }
17952 }
17953 else if (glyphs == 1)
17954 {
17955 int area;
17956
17957 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17958 {
17959 char *s = alloca (row->used[area] + 4);
17960 int i;
17961
17962 for (i = 0; i < row->used[area]; ++i)
17963 {
17964 struct glyph *glyph = row->glyphs[area] + i;
17965 if (i == row->used[area] - 1
17966 && area == TEXT_AREA
17967 && INTEGERP (glyph->object)
17968 && glyph->type == CHAR_GLYPH
17969 && glyph->u.ch == ' ')
17970 {
17971 strcpy (&s[i], "[\\n]");
17972 i += 4;
17973 }
17974 else if (glyph->type == CHAR_GLYPH
17975 && glyph->u.ch < 0x80
17976 && glyph->u.ch >= ' ')
17977 s[i] = glyph->u.ch;
17978 else
17979 s[i] = '.';
17980 }
17981
17982 s[i] = '\0';
17983 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17984 }
17985 }
17986 }
17987
17988
17989 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17990 Sdump_glyph_matrix, 0, 1, "p",
17991 doc: /* Dump the current matrix of the selected window to stderr.
17992 Shows contents of glyph row structures. With non-nil
17993 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17994 glyphs in short form, otherwise show glyphs in long form. */)
17995 (Lisp_Object glyphs)
17996 {
17997 struct window *w = XWINDOW (selected_window);
17998 struct buffer *buffer = XBUFFER (w->contents);
17999
18000 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18001 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18002 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18003 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18004 fprintf (stderr, "=============================================\n");
18005 dump_glyph_matrix (w->current_matrix,
18006 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18007 return Qnil;
18008 }
18009
18010
18011 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18012 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18013 (void)
18014 {
18015 struct frame *f = XFRAME (selected_frame);
18016 dump_glyph_matrix (f->current_matrix, 1);
18017 return Qnil;
18018 }
18019
18020
18021 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18022 doc: /* Dump glyph row ROW to stderr.
18023 GLYPH 0 means don't dump glyphs.
18024 GLYPH 1 means dump glyphs in short form.
18025 GLYPH > 1 or omitted means dump glyphs in long form. */)
18026 (Lisp_Object row, Lisp_Object glyphs)
18027 {
18028 struct glyph_matrix *matrix;
18029 EMACS_INT vpos;
18030
18031 CHECK_NUMBER (row);
18032 matrix = XWINDOW (selected_window)->current_matrix;
18033 vpos = XINT (row);
18034 if (vpos >= 0 && vpos < matrix->nrows)
18035 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18036 vpos,
18037 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18038 return Qnil;
18039 }
18040
18041
18042 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18043 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18044 GLYPH 0 means don't dump glyphs.
18045 GLYPH 1 means dump glyphs in short form.
18046 GLYPH > 1 or omitted means dump glyphs in long form.
18047
18048 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18049 do nothing. */)
18050 (Lisp_Object row, Lisp_Object glyphs)
18051 {
18052 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18053 struct frame *sf = SELECTED_FRAME ();
18054 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18055 EMACS_INT vpos;
18056
18057 CHECK_NUMBER (row);
18058 vpos = XINT (row);
18059 if (vpos >= 0 && vpos < m->nrows)
18060 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18061 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18062 #endif
18063 return Qnil;
18064 }
18065
18066
18067 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18068 doc: /* Toggle tracing of redisplay.
18069 With ARG, turn tracing on if and only if ARG is positive. */)
18070 (Lisp_Object arg)
18071 {
18072 if (NILP (arg))
18073 trace_redisplay_p = !trace_redisplay_p;
18074 else
18075 {
18076 arg = Fprefix_numeric_value (arg);
18077 trace_redisplay_p = XINT (arg) > 0;
18078 }
18079
18080 return Qnil;
18081 }
18082
18083
18084 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18085 doc: /* Like `format', but print result to stderr.
18086 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18087 (ptrdiff_t nargs, Lisp_Object *args)
18088 {
18089 Lisp_Object s = Fformat (nargs, args);
18090 fprintf (stderr, "%s", SDATA (s));
18091 return Qnil;
18092 }
18093
18094 #endif /* GLYPH_DEBUG */
18095
18096
18097 \f
18098 /***********************************************************************
18099 Building Desired Matrix Rows
18100 ***********************************************************************/
18101
18102 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18103 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18104
18105 static struct glyph_row *
18106 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18107 {
18108 struct frame *f = XFRAME (WINDOW_FRAME (w));
18109 struct buffer *buffer = XBUFFER (w->contents);
18110 struct buffer *old = current_buffer;
18111 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18112 int arrow_len = SCHARS (overlay_arrow_string);
18113 const unsigned char *arrow_end = arrow_string + arrow_len;
18114 const unsigned char *p;
18115 struct it it;
18116 bool multibyte_p;
18117 int n_glyphs_before;
18118
18119 set_buffer_temp (buffer);
18120 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18121 it.glyph_row->used[TEXT_AREA] = 0;
18122 SET_TEXT_POS (it.position, 0, 0);
18123
18124 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18125 p = arrow_string;
18126 while (p < arrow_end)
18127 {
18128 Lisp_Object face, ilisp;
18129
18130 /* Get the next character. */
18131 if (multibyte_p)
18132 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18133 else
18134 {
18135 it.c = it.char_to_display = *p, it.len = 1;
18136 if (! ASCII_CHAR_P (it.c))
18137 it.char_to_display = BYTE8_TO_CHAR (it.c);
18138 }
18139 p += it.len;
18140
18141 /* Get its face. */
18142 ilisp = make_number (p - arrow_string);
18143 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18144 it.face_id = compute_char_face (f, it.char_to_display, face);
18145
18146 /* Compute its width, get its glyphs. */
18147 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18148 SET_TEXT_POS (it.position, -1, -1);
18149 PRODUCE_GLYPHS (&it);
18150
18151 /* If this character doesn't fit any more in the line, we have
18152 to remove some glyphs. */
18153 if (it.current_x > it.last_visible_x)
18154 {
18155 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18156 break;
18157 }
18158 }
18159
18160 set_buffer_temp (old);
18161 return it.glyph_row;
18162 }
18163
18164
18165 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18166 glyphs to insert is determined by produce_special_glyphs. */
18167
18168 static void
18169 insert_left_trunc_glyphs (struct it *it)
18170 {
18171 struct it truncate_it;
18172 struct glyph *from, *end, *to, *toend;
18173
18174 eassert (!FRAME_WINDOW_P (it->f)
18175 || (!it->glyph_row->reversed_p
18176 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18177 || (it->glyph_row->reversed_p
18178 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18179
18180 /* Get the truncation glyphs. */
18181 truncate_it = *it;
18182 truncate_it.current_x = 0;
18183 truncate_it.face_id = DEFAULT_FACE_ID;
18184 truncate_it.glyph_row = &scratch_glyph_row;
18185 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18186 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18187 truncate_it.object = make_number (0);
18188 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18189
18190 /* Overwrite glyphs from IT with truncation glyphs. */
18191 if (!it->glyph_row->reversed_p)
18192 {
18193 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18194
18195 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18196 end = from + tused;
18197 to = it->glyph_row->glyphs[TEXT_AREA];
18198 toend = to + it->glyph_row->used[TEXT_AREA];
18199 if (FRAME_WINDOW_P (it->f))
18200 {
18201 /* On GUI frames, when variable-size fonts are displayed,
18202 the truncation glyphs may need more pixels than the row's
18203 glyphs they overwrite. We overwrite more glyphs to free
18204 enough screen real estate, and enlarge the stretch glyph
18205 on the right (see display_line), if there is one, to
18206 preserve the screen position of the truncation glyphs on
18207 the right. */
18208 int w = 0;
18209 struct glyph *g = to;
18210 short used;
18211
18212 /* The first glyph could be partially visible, in which case
18213 it->glyph_row->x will be negative. But we want the left
18214 truncation glyphs to be aligned at the left margin of the
18215 window, so we override the x coordinate at which the row
18216 will begin. */
18217 it->glyph_row->x = 0;
18218 while (g < toend && w < it->truncation_pixel_width)
18219 {
18220 w += g->pixel_width;
18221 ++g;
18222 }
18223 if (g - to - tused > 0)
18224 {
18225 memmove (to + tused, g, (toend - g) * sizeof(*g));
18226 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18227 }
18228 used = it->glyph_row->used[TEXT_AREA];
18229 if (it->glyph_row->truncated_on_right_p
18230 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18231 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18232 == STRETCH_GLYPH)
18233 {
18234 int extra = w - it->truncation_pixel_width;
18235
18236 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18237 }
18238 }
18239
18240 while (from < end)
18241 *to++ = *from++;
18242
18243 /* There may be padding glyphs left over. Overwrite them too. */
18244 if (!FRAME_WINDOW_P (it->f))
18245 {
18246 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18247 {
18248 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18249 while (from < end)
18250 *to++ = *from++;
18251 }
18252 }
18253
18254 if (to > toend)
18255 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18256 }
18257 else
18258 {
18259 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18260
18261 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18262 that back to front. */
18263 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18264 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18265 toend = it->glyph_row->glyphs[TEXT_AREA];
18266 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18267 if (FRAME_WINDOW_P (it->f))
18268 {
18269 int w = 0;
18270 struct glyph *g = to;
18271
18272 while (g >= toend && w < it->truncation_pixel_width)
18273 {
18274 w += g->pixel_width;
18275 --g;
18276 }
18277 if (to - g - tused > 0)
18278 to = g + tused;
18279 if (it->glyph_row->truncated_on_right_p
18280 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18281 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18282 {
18283 int extra = w - it->truncation_pixel_width;
18284
18285 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18286 }
18287 }
18288
18289 while (from >= end && to >= toend)
18290 *to-- = *from--;
18291 if (!FRAME_WINDOW_P (it->f))
18292 {
18293 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18294 {
18295 from =
18296 truncate_it.glyph_row->glyphs[TEXT_AREA]
18297 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18298 while (from >= end && to >= toend)
18299 *to-- = *from--;
18300 }
18301 }
18302 if (from >= end)
18303 {
18304 /* Need to free some room before prepending additional
18305 glyphs. */
18306 int move_by = from - end + 1;
18307 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18308 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18309
18310 for ( ; g >= g0; g--)
18311 g[move_by] = *g;
18312 while (from >= end)
18313 *to-- = *from--;
18314 it->glyph_row->used[TEXT_AREA] += move_by;
18315 }
18316 }
18317 }
18318
18319 /* Compute the hash code for ROW. */
18320 unsigned
18321 row_hash (struct glyph_row *row)
18322 {
18323 int area, k;
18324 unsigned hashval = 0;
18325
18326 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18327 for (k = 0; k < row->used[area]; ++k)
18328 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18329 + row->glyphs[area][k].u.val
18330 + row->glyphs[area][k].face_id
18331 + row->glyphs[area][k].padding_p
18332 + (row->glyphs[area][k].type << 2));
18333
18334 return hashval;
18335 }
18336
18337 /* Compute the pixel height and width of IT->glyph_row.
18338
18339 Most of the time, ascent and height of a display line will be equal
18340 to the max_ascent and max_height values of the display iterator
18341 structure. This is not the case if
18342
18343 1. We hit ZV without displaying anything. In this case, max_ascent
18344 and max_height will be zero.
18345
18346 2. We have some glyphs that don't contribute to the line height.
18347 (The glyph row flag contributes_to_line_height_p is for future
18348 pixmap extensions).
18349
18350 The first case is easily covered by using default values because in
18351 these cases, the line height does not really matter, except that it
18352 must not be zero. */
18353
18354 static void
18355 compute_line_metrics (struct it *it)
18356 {
18357 struct glyph_row *row = it->glyph_row;
18358
18359 if (FRAME_WINDOW_P (it->f))
18360 {
18361 int i, min_y, max_y;
18362
18363 /* The line may consist of one space only, that was added to
18364 place the cursor on it. If so, the row's height hasn't been
18365 computed yet. */
18366 if (row->height == 0)
18367 {
18368 if (it->max_ascent + it->max_descent == 0)
18369 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18370 row->ascent = it->max_ascent;
18371 row->height = it->max_ascent + it->max_descent;
18372 row->phys_ascent = it->max_phys_ascent;
18373 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18374 row->extra_line_spacing = it->max_extra_line_spacing;
18375 }
18376
18377 /* Compute the width of this line. */
18378 row->pixel_width = row->x;
18379 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18380 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18381
18382 eassert (row->pixel_width >= 0);
18383 eassert (row->ascent >= 0 && row->height > 0);
18384
18385 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18386 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18387
18388 /* If first line's physical ascent is larger than its logical
18389 ascent, use the physical ascent, and make the row taller.
18390 This makes accented characters fully visible. */
18391 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18392 && row->phys_ascent > row->ascent)
18393 {
18394 row->height += row->phys_ascent - row->ascent;
18395 row->ascent = row->phys_ascent;
18396 }
18397
18398 /* Compute how much of the line is visible. */
18399 row->visible_height = row->height;
18400
18401 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18402 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18403
18404 if (row->y < min_y)
18405 row->visible_height -= min_y - row->y;
18406 if (row->y + row->height > max_y)
18407 row->visible_height -= row->y + row->height - max_y;
18408 }
18409 else
18410 {
18411 row->pixel_width = row->used[TEXT_AREA];
18412 if (row->continued_p)
18413 row->pixel_width -= it->continuation_pixel_width;
18414 else if (row->truncated_on_right_p)
18415 row->pixel_width -= it->truncation_pixel_width;
18416 row->ascent = row->phys_ascent = 0;
18417 row->height = row->phys_height = row->visible_height = 1;
18418 row->extra_line_spacing = 0;
18419 }
18420
18421 /* Compute a hash code for this row. */
18422 row->hash = row_hash (row);
18423
18424 it->max_ascent = it->max_descent = 0;
18425 it->max_phys_ascent = it->max_phys_descent = 0;
18426 }
18427
18428
18429 /* Append one space to the glyph row of iterator IT if doing a
18430 window-based redisplay. The space has the same face as
18431 IT->face_id. Value is non-zero if a space was added.
18432
18433 This function is called to make sure that there is always one glyph
18434 at the end of a glyph row that the cursor can be set on under
18435 window-systems. (If there weren't such a glyph we would not know
18436 how wide and tall a box cursor should be displayed).
18437
18438 At the same time this space let's a nicely handle clearing to the
18439 end of the line if the row ends in italic text. */
18440
18441 static int
18442 append_space_for_newline (struct it *it, int default_face_p)
18443 {
18444 if (FRAME_WINDOW_P (it->f))
18445 {
18446 int n = it->glyph_row->used[TEXT_AREA];
18447
18448 if (it->glyph_row->glyphs[TEXT_AREA] + n
18449 < it->glyph_row->glyphs[1 + TEXT_AREA])
18450 {
18451 /* Save some values that must not be changed.
18452 Must save IT->c and IT->len because otherwise
18453 ITERATOR_AT_END_P wouldn't work anymore after
18454 append_space_for_newline has been called. */
18455 enum display_element_type saved_what = it->what;
18456 int saved_c = it->c, saved_len = it->len;
18457 int saved_char_to_display = it->char_to_display;
18458 int saved_x = it->current_x;
18459 int saved_face_id = it->face_id;
18460 int saved_box_end = it->end_of_box_run_p;
18461 struct text_pos saved_pos;
18462 Lisp_Object saved_object;
18463 struct face *face;
18464
18465 saved_object = it->object;
18466 saved_pos = it->position;
18467
18468 it->what = IT_CHARACTER;
18469 memset (&it->position, 0, sizeof it->position);
18470 it->object = make_number (0);
18471 it->c = it->char_to_display = ' ';
18472 it->len = 1;
18473
18474 /* If the default face was remapped, be sure to use the
18475 remapped face for the appended newline. */
18476 if (default_face_p)
18477 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18478 else if (it->face_before_selective_p)
18479 it->face_id = it->saved_face_id;
18480 face = FACE_FROM_ID (it->f, it->face_id);
18481 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18482 /* In R2L rows, we will prepend a stretch glyph that will
18483 have the end_of_box_run_p flag set for it, so there's no
18484 need for the appended newline glyph to have that flag
18485 set. */
18486 if (it->glyph_row->reversed_p
18487 /* But if the appended newline glyph goes all the way to
18488 the end of the row, there will be no stretch glyph,
18489 so leave the box flag set. */
18490 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18491 it->end_of_box_run_p = 0;
18492
18493 PRODUCE_GLYPHS (it);
18494
18495 it->override_ascent = -1;
18496 it->constrain_row_ascent_descent_p = 0;
18497 it->current_x = saved_x;
18498 it->object = saved_object;
18499 it->position = saved_pos;
18500 it->what = saved_what;
18501 it->face_id = saved_face_id;
18502 it->len = saved_len;
18503 it->c = saved_c;
18504 it->char_to_display = saved_char_to_display;
18505 it->end_of_box_run_p = saved_box_end;
18506 return 1;
18507 }
18508 }
18509
18510 return 0;
18511 }
18512
18513
18514 /* Extend the face of the last glyph in the text area of IT->glyph_row
18515 to the end of the display line. Called from display_line. If the
18516 glyph row is empty, add a space glyph to it so that we know the
18517 face to draw. Set the glyph row flag fill_line_p. If the glyph
18518 row is R2L, prepend a stretch glyph to cover the empty space to the
18519 left of the leftmost glyph. */
18520
18521 static void
18522 extend_face_to_end_of_line (struct it *it)
18523 {
18524 struct face *face, *default_face;
18525 struct frame *f = it->f;
18526
18527 /* If line is already filled, do nothing. Non window-system frames
18528 get a grace of one more ``pixel'' because their characters are
18529 1-``pixel'' wide, so they hit the equality too early. This grace
18530 is needed only for R2L rows that are not continued, to produce
18531 one extra blank where we could display the cursor. */
18532 if (it->current_x >= it->last_visible_x
18533 + (!FRAME_WINDOW_P (f)
18534 && it->glyph_row->reversed_p
18535 && !it->glyph_row->continued_p))
18536 return;
18537
18538 /* The default face, possibly remapped. */
18539 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18540
18541 /* Face extension extends the background and box of IT->face_id
18542 to the end of the line. If the background equals the background
18543 of the frame, we don't have to do anything. */
18544 if (it->face_before_selective_p)
18545 face = FACE_FROM_ID (f, it->saved_face_id);
18546 else
18547 face = FACE_FROM_ID (f, it->face_id);
18548
18549 if (FRAME_WINDOW_P (f)
18550 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18551 && face->box == FACE_NO_BOX
18552 && face->background == FRAME_BACKGROUND_PIXEL (f)
18553 #ifdef HAVE_WINDOW_SYSTEM
18554 && !face->stipple
18555 #endif
18556 && !it->glyph_row->reversed_p)
18557 return;
18558
18559 /* Set the glyph row flag indicating that the face of the last glyph
18560 in the text area has to be drawn to the end of the text area. */
18561 it->glyph_row->fill_line_p = 1;
18562
18563 /* If current character of IT is not ASCII, make sure we have the
18564 ASCII face. This will be automatically undone the next time
18565 get_next_display_element returns a multibyte character. Note
18566 that the character will always be single byte in unibyte
18567 text. */
18568 if (!ASCII_CHAR_P (it->c))
18569 {
18570 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18571 }
18572
18573 if (FRAME_WINDOW_P (f))
18574 {
18575 /* If the row is empty, add a space with the current face of IT,
18576 so that we know which face to draw. */
18577 if (it->glyph_row->used[TEXT_AREA] == 0)
18578 {
18579 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18580 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18581 it->glyph_row->used[TEXT_AREA] = 1;
18582 }
18583 #ifdef HAVE_WINDOW_SYSTEM
18584 if (it->glyph_row->reversed_p)
18585 {
18586 /* Prepend a stretch glyph to the row, such that the
18587 rightmost glyph will be drawn flushed all the way to the
18588 right margin of the window. The stretch glyph that will
18589 occupy the empty space, if any, to the left of the
18590 glyphs. */
18591 struct font *font = face->font ? face->font : FRAME_FONT (f);
18592 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18593 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18594 struct glyph *g;
18595 int row_width, stretch_ascent, stretch_width;
18596 struct text_pos saved_pos;
18597 int saved_face_id, saved_avoid_cursor, saved_box_start;
18598
18599 for (row_width = 0, g = row_start; g < row_end; g++)
18600 row_width += g->pixel_width;
18601 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18602 if (stretch_width > 0)
18603 {
18604 stretch_ascent =
18605 (((it->ascent + it->descent)
18606 * FONT_BASE (font)) / FONT_HEIGHT (font));
18607 saved_pos = it->position;
18608 memset (&it->position, 0, sizeof it->position);
18609 saved_avoid_cursor = it->avoid_cursor_p;
18610 it->avoid_cursor_p = 1;
18611 saved_face_id = it->face_id;
18612 saved_box_start = it->start_of_box_run_p;
18613 /* The last row's stretch glyph should get the default
18614 face, to avoid painting the rest of the window with
18615 the region face, if the region ends at ZV. */
18616 if (it->glyph_row->ends_at_zv_p)
18617 it->face_id = default_face->id;
18618 else
18619 it->face_id = face->id;
18620 it->start_of_box_run_p = 0;
18621 append_stretch_glyph (it, make_number (0), stretch_width,
18622 it->ascent + it->descent, stretch_ascent);
18623 it->position = saved_pos;
18624 it->avoid_cursor_p = saved_avoid_cursor;
18625 it->face_id = saved_face_id;
18626 it->start_of_box_run_p = saved_box_start;
18627 }
18628 }
18629 #endif /* HAVE_WINDOW_SYSTEM */
18630 }
18631 else
18632 {
18633 /* Save some values that must not be changed. */
18634 int saved_x = it->current_x;
18635 struct text_pos saved_pos;
18636 Lisp_Object saved_object;
18637 enum display_element_type saved_what = it->what;
18638 int saved_face_id = it->face_id;
18639
18640 saved_object = it->object;
18641 saved_pos = it->position;
18642
18643 it->what = IT_CHARACTER;
18644 memset (&it->position, 0, sizeof it->position);
18645 it->object = make_number (0);
18646 it->c = it->char_to_display = ' ';
18647 it->len = 1;
18648 /* The last row's blank glyphs should get the default face, to
18649 avoid painting the rest of the window with the region face,
18650 if the region ends at ZV. */
18651 if (it->glyph_row->ends_at_zv_p)
18652 it->face_id = default_face->id;
18653 else
18654 it->face_id = face->id;
18655
18656 PRODUCE_GLYPHS (it);
18657
18658 while (it->current_x <= it->last_visible_x)
18659 PRODUCE_GLYPHS (it);
18660
18661 /* Don't count these blanks really. It would let us insert a left
18662 truncation glyph below and make us set the cursor on them, maybe. */
18663 it->current_x = saved_x;
18664 it->object = saved_object;
18665 it->position = saved_pos;
18666 it->what = saved_what;
18667 it->face_id = saved_face_id;
18668 }
18669 }
18670
18671
18672 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18673 trailing whitespace. */
18674
18675 static int
18676 trailing_whitespace_p (ptrdiff_t charpos)
18677 {
18678 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18679 int c = 0;
18680
18681 while (bytepos < ZV_BYTE
18682 && (c = FETCH_CHAR (bytepos),
18683 c == ' ' || c == '\t'))
18684 ++bytepos;
18685
18686 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18687 {
18688 if (bytepos != PT_BYTE)
18689 return 1;
18690 }
18691 return 0;
18692 }
18693
18694
18695 /* Highlight trailing whitespace, if any, in ROW. */
18696
18697 static void
18698 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18699 {
18700 int used = row->used[TEXT_AREA];
18701
18702 if (used)
18703 {
18704 struct glyph *start = row->glyphs[TEXT_AREA];
18705 struct glyph *glyph = start + used - 1;
18706
18707 if (row->reversed_p)
18708 {
18709 /* Right-to-left rows need to be processed in the opposite
18710 direction, so swap the edge pointers. */
18711 glyph = start;
18712 start = row->glyphs[TEXT_AREA] + used - 1;
18713 }
18714
18715 /* Skip over glyphs inserted to display the cursor at the
18716 end of a line, for extending the face of the last glyph
18717 to the end of the line on terminals, and for truncation
18718 and continuation glyphs. */
18719 if (!row->reversed_p)
18720 {
18721 while (glyph >= start
18722 && glyph->type == CHAR_GLYPH
18723 && INTEGERP (glyph->object))
18724 --glyph;
18725 }
18726 else
18727 {
18728 while (glyph <= start
18729 && glyph->type == CHAR_GLYPH
18730 && INTEGERP (glyph->object))
18731 ++glyph;
18732 }
18733
18734 /* If last glyph is a space or stretch, and it's trailing
18735 whitespace, set the face of all trailing whitespace glyphs in
18736 IT->glyph_row to `trailing-whitespace'. */
18737 if ((row->reversed_p ? glyph <= start : glyph >= start)
18738 && BUFFERP (glyph->object)
18739 && (glyph->type == STRETCH_GLYPH
18740 || (glyph->type == CHAR_GLYPH
18741 && glyph->u.ch == ' '))
18742 && trailing_whitespace_p (glyph->charpos))
18743 {
18744 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18745 if (face_id < 0)
18746 return;
18747
18748 if (!row->reversed_p)
18749 {
18750 while (glyph >= start
18751 && BUFFERP (glyph->object)
18752 && (glyph->type == STRETCH_GLYPH
18753 || (glyph->type == CHAR_GLYPH
18754 && glyph->u.ch == ' ')))
18755 (glyph--)->face_id = face_id;
18756 }
18757 else
18758 {
18759 while (glyph <= start
18760 && BUFFERP (glyph->object)
18761 && (glyph->type == STRETCH_GLYPH
18762 || (glyph->type == CHAR_GLYPH
18763 && glyph->u.ch == ' ')))
18764 (glyph++)->face_id = face_id;
18765 }
18766 }
18767 }
18768 }
18769
18770
18771 /* Value is non-zero if glyph row ROW should be
18772 considered to hold the buffer position CHARPOS. */
18773
18774 static int
18775 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18776 {
18777 int result = 1;
18778
18779 if (charpos == CHARPOS (row->end.pos)
18780 || charpos == MATRIX_ROW_END_CHARPOS (row))
18781 {
18782 /* Suppose the row ends on a string.
18783 Unless the row is continued, that means it ends on a newline
18784 in the string. If it's anything other than a display string
18785 (e.g., a before-string from an overlay), we don't want the
18786 cursor there. (This heuristic seems to give the optimal
18787 behavior for the various types of multi-line strings.)
18788 One exception: if the string has `cursor' property on one of
18789 its characters, we _do_ want the cursor there. */
18790 if (CHARPOS (row->end.string_pos) >= 0)
18791 {
18792 if (row->continued_p)
18793 result = 1;
18794 else
18795 {
18796 /* Check for `display' property. */
18797 struct glyph *beg = row->glyphs[TEXT_AREA];
18798 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18799 struct glyph *glyph;
18800
18801 result = 0;
18802 for (glyph = end; glyph >= beg; --glyph)
18803 if (STRINGP (glyph->object))
18804 {
18805 Lisp_Object prop
18806 = Fget_char_property (make_number (charpos),
18807 Qdisplay, Qnil);
18808 result =
18809 (!NILP (prop)
18810 && display_prop_string_p (prop, glyph->object));
18811 /* If there's a `cursor' property on one of the
18812 string's characters, this row is a cursor row,
18813 even though this is not a display string. */
18814 if (!result)
18815 {
18816 Lisp_Object s = glyph->object;
18817
18818 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18819 {
18820 ptrdiff_t gpos = glyph->charpos;
18821
18822 if (!NILP (Fget_char_property (make_number (gpos),
18823 Qcursor, s)))
18824 {
18825 result = 1;
18826 break;
18827 }
18828 }
18829 }
18830 break;
18831 }
18832 }
18833 }
18834 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18835 {
18836 /* If the row ends in middle of a real character,
18837 and the line is continued, we want the cursor here.
18838 That's because CHARPOS (ROW->end.pos) would equal
18839 PT if PT is before the character. */
18840 if (!row->ends_in_ellipsis_p)
18841 result = row->continued_p;
18842 else
18843 /* If the row ends in an ellipsis, then
18844 CHARPOS (ROW->end.pos) will equal point after the
18845 invisible text. We want that position to be displayed
18846 after the ellipsis. */
18847 result = 0;
18848 }
18849 /* If the row ends at ZV, display the cursor at the end of that
18850 row instead of at the start of the row below. */
18851 else if (row->ends_at_zv_p)
18852 result = 1;
18853 else
18854 result = 0;
18855 }
18856
18857 return result;
18858 }
18859
18860 /* Value is non-zero if glyph row ROW should be
18861 used to hold the cursor. */
18862
18863 static int
18864 cursor_row_p (struct glyph_row *row)
18865 {
18866 return row_for_charpos_p (row, PT);
18867 }
18868
18869 \f
18870
18871 /* Push the property PROP so that it will be rendered at the current
18872 position in IT. Return 1 if PROP was successfully pushed, 0
18873 otherwise. Called from handle_line_prefix to handle the
18874 `line-prefix' and `wrap-prefix' properties. */
18875
18876 static int
18877 push_prefix_prop (struct it *it, Lisp_Object prop)
18878 {
18879 struct text_pos pos =
18880 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18881
18882 eassert (it->method == GET_FROM_BUFFER
18883 || it->method == GET_FROM_DISPLAY_VECTOR
18884 || it->method == GET_FROM_STRING);
18885
18886 /* We need to save the current buffer/string position, so it will be
18887 restored by pop_it, because iterate_out_of_display_property
18888 depends on that being set correctly, but some situations leave
18889 it->position not yet set when this function is called. */
18890 push_it (it, &pos);
18891
18892 if (STRINGP (prop))
18893 {
18894 if (SCHARS (prop) == 0)
18895 {
18896 pop_it (it);
18897 return 0;
18898 }
18899
18900 it->string = prop;
18901 it->string_from_prefix_prop_p = 1;
18902 it->multibyte_p = STRING_MULTIBYTE (it->string);
18903 it->current.overlay_string_index = -1;
18904 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18905 it->end_charpos = it->string_nchars = SCHARS (it->string);
18906 it->method = GET_FROM_STRING;
18907 it->stop_charpos = 0;
18908 it->prev_stop = 0;
18909 it->base_level_stop = 0;
18910
18911 /* Force paragraph direction to be that of the parent
18912 buffer/string. */
18913 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18914 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18915 else
18916 it->paragraph_embedding = L2R;
18917
18918 /* Set up the bidi iterator for this display string. */
18919 if (it->bidi_p)
18920 {
18921 it->bidi_it.string.lstring = it->string;
18922 it->bidi_it.string.s = NULL;
18923 it->bidi_it.string.schars = it->end_charpos;
18924 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18925 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18926 it->bidi_it.string.unibyte = !it->multibyte_p;
18927 it->bidi_it.w = it->w;
18928 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18929 }
18930 }
18931 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18932 {
18933 it->method = GET_FROM_STRETCH;
18934 it->object = prop;
18935 }
18936 #ifdef HAVE_WINDOW_SYSTEM
18937 else if (IMAGEP (prop))
18938 {
18939 it->what = IT_IMAGE;
18940 it->image_id = lookup_image (it->f, prop);
18941 it->method = GET_FROM_IMAGE;
18942 }
18943 #endif /* HAVE_WINDOW_SYSTEM */
18944 else
18945 {
18946 pop_it (it); /* bogus display property, give up */
18947 return 0;
18948 }
18949
18950 return 1;
18951 }
18952
18953 /* Return the character-property PROP at the current position in IT. */
18954
18955 static Lisp_Object
18956 get_it_property (struct it *it, Lisp_Object prop)
18957 {
18958 Lisp_Object position, object = it->object;
18959
18960 if (STRINGP (object))
18961 position = make_number (IT_STRING_CHARPOS (*it));
18962 else if (BUFFERP (object))
18963 {
18964 position = make_number (IT_CHARPOS (*it));
18965 object = it->window;
18966 }
18967 else
18968 return Qnil;
18969
18970 return Fget_char_property (position, prop, object);
18971 }
18972
18973 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18974
18975 static void
18976 handle_line_prefix (struct it *it)
18977 {
18978 Lisp_Object prefix;
18979
18980 if (it->continuation_lines_width > 0)
18981 {
18982 prefix = get_it_property (it, Qwrap_prefix);
18983 if (NILP (prefix))
18984 prefix = Vwrap_prefix;
18985 }
18986 else
18987 {
18988 prefix = get_it_property (it, Qline_prefix);
18989 if (NILP (prefix))
18990 prefix = Vline_prefix;
18991 }
18992 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18993 {
18994 /* If the prefix is wider than the window, and we try to wrap
18995 it, it would acquire its own wrap prefix, and so on till the
18996 iterator stack overflows. So, don't wrap the prefix. */
18997 it->line_wrap = TRUNCATE;
18998 it->avoid_cursor_p = 1;
18999 }
19000 }
19001
19002 \f
19003
19004 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19005 only for R2L lines from display_line and display_string, when they
19006 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19007 the line/string needs to be continued on the next glyph row. */
19008 static void
19009 unproduce_glyphs (struct it *it, int n)
19010 {
19011 struct glyph *glyph, *end;
19012
19013 eassert (it->glyph_row);
19014 eassert (it->glyph_row->reversed_p);
19015 eassert (it->area == TEXT_AREA);
19016 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19017
19018 if (n > it->glyph_row->used[TEXT_AREA])
19019 n = it->glyph_row->used[TEXT_AREA];
19020 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19021 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19022 for ( ; glyph < end; glyph++)
19023 glyph[-n] = *glyph;
19024 }
19025
19026 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19027 and ROW->maxpos. */
19028 static void
19029 find_row_edges (struct it *it, struct glyph_row *row,
19030 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19031 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19032 {
19033 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19034 lines' rows is implemented for bidi-reordered rows. */
19035
19036 /* ROW->minpos is the value of min_pos, the minimal buffer position
19037 we have in ROW, or ROW->start.pos if that is smaller. */
19038 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19039 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19040 else
19041 /* We didn't find buffer positions smaller than ROW->start, or
19042 didn't find _any_ valid buffer positions in any of the glyphs,
19043 so we must trust the iterator's computed positions. */
19044 row->minpos = row->start.pos;
19045 if (max_pos <= 0)
19046 {
19047 max_pos = CHARPOS (it->current.pos);
19048 max_bpos = BYTEPOS (it->current.pos);
19049 }
19050
19051 /* Here are the various use-cases for ending the row, and the
19052 corresponding values for ROW->maxpos:
19053
19054 Line ends in a newline from buffer eol_pos + 1
19055 Line is continued from buffer max_pos + 1
19056 Line is truncated on right it->current.pos
19057 Line ends in a newline from string max_pos + 1(*)
19058 (*) + 1 only when line ends in a forward scan
19059 Line is continued from string max_pos
19060 Line is continued from display vector max_pos
19061 Line is entirely from a string min_pos == max_pos
19062 Line is entirely from a display vector min_pos == max_pos
19063 Line that ends at ZV ZV
19064
19065 If you discover other use-cases, please add them here as
19066 appropriate. */
19067 if (row->ends_at_zv_p)
19068 row->maxpos = it->current.pos;
19069 else if (row->used[TEXT_AREA])
19070 {
19071 int seen_this_string = 0;
19072 struct glyph_row *r1 = row - 1;
19073
19074 /* Did we see the same display string on the previous row? */
19075 if (STRINGP (it->object)
19076 /* this is not the first row */
19077 && row > it->w->desired_matrix->rows
19078 /* previous row is not the header line */
19079 && !r1->mode_line_p
19080 /* previous row also ends in a newline from a string */
19081 && r1->ends_in_newline_from_string_p)
19082 {
19083 struct glyph *start, *end;
19084
19085 /* Search for the last glyph of the previous row that came
19086 from buffer or string. Depending on whether the row is
19087 L2R or R2L, we need to process it front to back or the
19088 other way round. */
19089 if (!r1->reversed_p)
19090 {
19091 start = r1->glyphs[TEXT_AREA];
19092 end = start + r1->used[TEXT_AREA];
19093 /* Glyphs inserted by redisplay have an integer (zero)
19094 as their object. */
19095 while (end > start
19096 && INTEGERP ((end - 1)->object)
19097 && (end - 1)->charpos <= 0)
19098 --end;
19099 if (end > start)
19100 {
19101 if (EQ ((end - 1)->object, it->object))
19102 seen_this_string = 1;
19103 }
19104 else
19105 /* If all the glyphs of the previous row were inserted
19106 by redisplay, it means the previous row was
19107 produced from a single newline, which is only
19108 possible if that newline came from the same string
19109 as the one which produced this ROW. */
19110 seen_this_string = 1;
19111 }
19112 else
19113 {
19114 end = r1->glyphs[TEXT_AREA] - 1;
19115 start = end + r1->used[TEXT_AREA];
19116 while (end < start
19117 && INTEGERP ((end + 1)->object)
19118 && (end + 1)->charpos <= 0)
19119 ++end;
19120 if (end < start)
19121 {
19122 if (EQ ((end + 1)->object, it->object))
19123 seen_this_string = 1;
19124 }
19125 else
19126 seen_this_string = 1;
19127 }
19128 }
19129 /* Take note of each display string that covers a newline only
19130 once, the first time we see it. This is for when a display
19131 string includes more than one newline in it. */
19132 if (row->ends_in_newline_from_string_p && !seen_this_string)
19133 {
19134 /* If we were scanning the buffer forward when we displayed
19135 the string, we want to account for at least one buffer
19136 position that belongs to this row (position covered by
19137 the display string), so that cursor positioning will
19138 consider this row as a candidate when point is at the end
19139 of the visual line represented by this row. This is not
19140 required when scanning back, because max_pos will already
19141 have a much larger value. */
19142 if (CHARPOS (row->end.pos) > max_pos)
19143 INC_BOTH (max_pos, max_bpos);
19144 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19145 }
19146 else if (CHARPOS (it->eol_pos) > 0)
19147 SET_TEXT_POS (row->maxpos,
19148 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19149 else if (row->continued_p)
19150 {
19151 /* If max_pos is different from IT's current position, it
19152 means IT->method does not belong to the display element
19153 at max_pos. However, it also means that the display
19154 element at max_pos was displayed in its entirety on this
19155 line, which is equivalent to saying that the next line
19156 starts at the next buffer position. */
19157 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19158 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19159 else
19160 {
19161 INC_BOTH (max_pos, max_bpos);
19162 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19163 }
19164 }
19165 else if (row->truncated_on_right_p)
19166 /* display_line already called reseat_at_next_visible_line_start,
19167 which puts the iterator at the beginning of the next line, in
19168 the logical order. */
19169 row->maxpos = it->current.pos;
19170 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19171 /* A line that is entirely from a string/image/stretch... */
19172 row->maxpos = row->minpos;
19173 else
19174 emacs_abort ();
19175 }
19176 else
19177 row->maxpos = it->current.pos;
19178 }
19179
19180 /* Construct the glyph row IT->glyph_row in the desired matrix of
19181 IT->w from text at the current position of IT. See dispextern.h
19182 for an overview of struct it. Value is non-zero if
19183 IT->glyph_row displays text, as opposed to a line displaying ZV
19184 only. */
19185
19186 static int
19187 display_line (struct it *it)
19188 {
19189 struct glyph_row *row = it->glyph_row;
19190 Lisp_Object overlay_arrow_string;
19191 struct it wrap_it;
19192 void *wrap_data = NULL;
19193 int may_wrap = 0, wrap_x IF_LINT (= 0);
19194 int wrap_row_used = -1;
19195 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19196 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19197 int wrap_row_extra_line_spacing IF_LINT (= 0);
19198 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19199 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19200 int cvpos;
19201 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19202 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19203
19204 /* We always start displaying at hpos zero even if hscrolled. */
19205 eassert (it->hpos == 0 && it->current_x == 0);
19206
19207 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19208 >= it->w->desired_matrix->nrows)
19209 {
19210 it->w->nrows_scale_factor++;
19211 it->f->fonts_changed = 1;
19212 return 0;
19213 }
19214
19215 /* Clear the result glyph row and enable it. */
19216 prepare_desired_row (row);
19217
19218 row->y = it->current_y;
19219 row->start = it->start;
19220 row->continuation_lines_width = it->continuation_lines_width;
19221 row->displays_text_p = 1;
19222 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19223 it->starts_in_middle_of_char_p = 0;
19224
19225 /* Arrange the overlays nicely for our purposes. Usually, we call
19226 display_line on only one line at a time, in which case this
19227 can't really hurt too much, or we call it on lines which appear
19228 one after another in the buffer, in which case all calls to
19229 recenter_overlay_lists but the first will be pretty cheap. */
19230 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19231
19232 /* Move over display elements that are not visible because we are
19233 hscrolled. This may stop at an x-position < IT->first_visible_x
19234 if the first glyph is partially visible or if we hit a line end. */
19235 if (it->current_x < it->first_visible_x)
19236 {
19237 enum move_it_result move_result;
19238
19239 this_line_min_pos = row->start.pos;
19240 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19241 MOVE_TO_POS | MOVE_TO_X);
19242 /* If we are under a large hscroll, move_it_in_display_line_to
19243 could hit the end of the line without reaching
19244 it->first_visible_x. Pretend that we did reach it. This is
19245 especially important on a TTY, where we will call
19246 extend_face_to_end_of_line, which needs to know how many
19247 blank glyphs to produce. */
19248 if (it->current_x < it->first_visible_x
19249 && (move_result == MOVE_NEWLINE_OR_CR
19250 || move_result == MOVE_POS_MATCH_OR_ZV))
19251 it->current_x = it->first_visible_x;
19252
19253 /* Record the smallest positions seen while we moved over
19254 display elements that are not visible. This is needed by
19255 redisplay_internal for optimizing the case where the cursor
19256 stays inside the same line. The rest of this function only
19257 considers positions that are actually displayed, so
19258 RECORD_MAX_MIN_POS will not otherwise record positions that
19259 are hscrolled to the left of the left edge of the window. */
19260 min_pos = CHARPOS (this_line_min_pos);
19261 min_bpos = BYTEPOS (this_line_min_pos);
19262 }
19263 else
19264 {
19265 /* We only do this when not calling `move_it_in_display_line_to'
19266 above, because move_it_in_display_line_to calls
19267 handle_line_prefix itself. */
19268 handle_line_prefix (it);
19269 }
19270
19271 /* Get the initial row height. This is either the height of the
19272 text hscrolled, if there is any, or zero. */
19273 row->ascent = it->max_ascent;
19274 row->height = it->max_ascent + it->max_descent;
19275 row->phys_ascent = it->max_phys_ascent;
19276 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19277 row->extra_line_spacing = it->max_extra_line_spacing;
19278
19279 /* Utility macro to record max and min buffer positions seen until now. */
19280 #define RECORD_MAX_MIN_POS(IT) \
19281 do \
19282 { \
19283 int composition_p = !STRINGP ((IT)->string) \
19284 && ((IT)->what == IT_COMPOSITION); \
19285 ptrdiff_t current_pos = \
19286 composition_p ? (IT)->cmp_it.charpos \
19287 : IT_CHARPOS (*(IT)); \
19288 ptrdiff_t current_bpos = \
19289 composition_p ? CHAR_TO_BYTE (current_pos) \
19290 : IT_BYTEPOS (*(IT)); \
19291 if (current_pos < min_pos) \
19292 { \
19293 min_pos = current_pos; \
19294 min_bpos = current_bpos; \
19295 } \
19296 if (IT_CHARPOS (*it) > max_pos) \
19297 { \
19298 max_pos = IT_CHARPOS (*it); \
19299 max_bpos = IT_BYTEPOS (*it); \
19300 } \
19301 } \
19302 while (0)
19303
19304 /* Loop generating characters. The loop is left with IT on the next
19305 character to display. */
19306 while (1)
19307 {
19308 int n_glyphs_before, hpos_before, x_before;
19309 int x, nglyphs;
19310 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19311
19312 /* Retrieve the next thing to display. Value is zero if end of
19313 buffer reached. */
19314 if (!get_next_display_element (it))
19315 {
19316 /* Maybe add a space at the end of this line that is used to
19317 display the cursor there under X. Set the charpos of the
19318 first glyph of blank lines not corresponding to any text
19319 to -1. */
19320 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19321 row->exact_window_width_line_p = 1;
19322 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19323 || row->used[TEXT_AREA] == 0)
19324 {
19325 row->glyphs[TEXT_AREA]->charpos = -1;
19326 row->displays_text_p = 0;
19327
19328 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19329 && (!MINI_WINDOW_P (it->w)
19330 || (minibuf_level && EQ (it->window, minibuf_window))))
19331 row->indicate_empty_line_p = 1;
19332 }
19333
19334 it->continuation_lines_width = 0;
19335 row->ends_at_zv_p = 1;
19336 /* A row that displays right-to-left text must always have
19337 its last face extended all the way to the end of line,
19338 even if this row ends in ZV, because we still write to
19339 the screen left to right. We also need to extend the
19340 last face if the default face is remapped to some
19341 different face, otherwise the functions that clear
19342 portions of the screen will clear with the default face's
19343 background color. */
19344 if (row->reversed_p
19345 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19346 extend_face_to_end_of_line (it);
19347 break;
19348 }
19349
19350 /* Now, get the metrics of what we want to display. This also
19351 generates glyphs in `row' (which is IT->glyph_row). */
19352 n_glyphs_before = row->used[TEXT_AREA];
19353 x = it->current_x;
19354
19355 /* Remember the line height so far in case the next element doesn't
19356 fit on the line. */
19357 if (it->line_wrap != TRUNCATE)
19358 {
19359 ascent = it->max_ascent;
19360 descent = it->max_descent;
19361 phys_ascent = it->max_phys_ascent;
19362 phys_descent = it->max_phys_descent;
19363
19364 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19365 {
19366 if (IT_DISPLAYING_WHITESPACE (it))
19367 may_wrap = 1;
19368 else if (may_wrap)
19369 {
19370 SAVE_IT (wrap_it, *it, wrap_data);
19371 wrap_x = x;
19372 wrap_row_used = row->used[TEXT_AREA];
19373 wrap_row_ascent = row->ascent;
19374 wrap_row_height = row->height;
19375 wrap_row_phys_ascent = row->phys_ascent;
19376 wrap_row_phys_height = row->phys_height;
19377 wrap_row_extra_line_spacing = row->extra_line_spacing;
19378 wrap_row_min_pos = min_pos;
19379 wrap_row_min_bpos = min_bpos;
19380 wrap_row_max_pos = max_pos;
19381 wrap_row_max_bpos = max_bpos;
19382 may_wrap = 0;
19383 }
19384 }
19385 }
19386
19387 PRODUCE_GLYPHS (it);
19388
19389 /* If this display element was in marginal areas, continue with
19390 the next one. */
19391 if (it->area != TEXT_AREA)
19392 {
19393 row->ascent = max (row->ascent, it->max_ascent);
19394 row->height = max (row->height, it->max_ascent + it->max_descent);
19395 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19396 row->phys_height = max (row->phys_height,
19397 it->max_phys_ascent + it->max_phys_descent);
19398 row->extra_line_spacing = max (row->extra_line_spacing,
19399 it->max_extra_line_spacing);
19400 set_iterator_to_next (it, 1);
19401 continue;
19402 }
19403
19404 /* Does the display element fit on the line? If we truncate
19405 lines, we should draw past the right edge of the window. If
19406 we don't truncate, we want to stop so that we can display the
19407 continuation glyph before the right margin. If lines are
19408 continued, there are two possible strategies for characters
19409 resulting in more than 1 glyph (e.g. tabs): Display as many
19410 glyphs as possible in this line and leave the rest for the
19411 continuation line, or display the whole element in the next
19412 line. Original redisplay did the former, so we do it also. */
19413 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19414 hpos_before = it->hpos;
19415 x_before = x;
19416
19417 if (/* Not a newline. */
19418 nglyphs > 0
19419 /* Glyphs produced fit entirely in the line. */
19420 && it->current_x < it->last_visible_x)
19421 {
19422 it->hpos += nglyphs;
19423 row->ascent = max (row->ascent, it->max_ascent);
19424 row->height = max (row->height, it->max_ascent + it->max_descent);
19425 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19426 row->phys_height = max (row->phys_height,
19427 it->max_phys_ascent + it->max_phys_descent);
19428 row->extra_line_spacing = max (row->extra_line_spacing,
19429 it->max_extra_line_spacing);
19430 if (it->current_x - it->pixel_width < it->first_visible_x)
19431 row->x = x - it->first_visible_x;
19432 /* Record the maximum and minimum buffer positions seen so
19433 far in glyphs that will be displayed by this row. */
19434 if (it->bidi_p)
19435 RECORD_MAX_MIN_POS (it);
19436 }
19437 else
19438 {
19439 int i, new_x;
19440 struct glyph *glyph;
19441
19442 for (i = 0; i < nglyphs; ++i, x = new_x)
19443 {
19444 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19445 new_x = x + glyph->pixel_width;
19446
19447 if (/* Lines are continued. */
19448 it->line_wrap != TRUNCATE
19449 && (/* Glyph doesn't fit on the line. */
19450 new_x > it->last_visible_x
19451 /* Or it fits exactly on a window system frame. */
19452 || (new_x == it->last_visible_x
19453 && FRAME_WINDOW_P (it->f)
19454 && (row->reversed_p
19455 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19456 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19457 {
19458 /* End of a continued line. */
19459
19460 if (it->hpos == 0
19461 || (new_x == it->last_visible_x
19462 && FRAME_WINDOW_P (it->f)
19463 && (row->reversed_p
19464 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19465 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19466 {
19467 /* Current glyph is the only one on the line or
19468 fits exactly on the line. We must continue
19469 the line because we can't draw the cursor
19470 after the glyph. */
19471 row->continued_p = 1;
19472 it->current_x = new_x;
19473 it->continuation_lines_width += new_x;
19474 ++it->hpos;
19475 if (i == nglyphs - 1)
19476 {
19477 /* If line-wrap is on, check if a previous
19478 wrap point was found. */
19479 if (wrap_row_used > 0
19480 /* Even if there is a previous wrap
19481 point, continue the line here as
19482 usual, if (i) the previous character
19483 was a space or tab AND (ii) the
19484 current character is not. */
19485 && (!may_wrap
19486 || IT_DISPLAYING_WHITESPACE (it)))
19487 goto back_to_wrap;
19488
19489 /* Record the maximum and minimum buffer
19490 positions seen so far in glyphs that will be
19491 displayed by this row. */
19492 if (it->bidi_p)
19493 RECORD_MAX_MIN_POS (it);
19494 set_iterator_to_next (it, 1);
19495 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19496 {
19497 if (!get_next_display_element (it))
19498 {
19499 row->exact_window_width_line_p = 1;
19500 it->continuation_lines_width = 0;
19501 row->continued_p = 0;
19502 row->ends_at_zv_p = 1;
19503 }
19504 else if (ITERATOR_AT_END_OF_LINE_P (it))
19505 {
19506 row->continued_p = 0;
19507 row->exact_window_width_line_p = 1;
19508 }
19509 }
19510 }
19511 else if (it->bidi_p)
19512 RECORD_MAX_MIN_POS (it);
19513 }
19514 else if (CHAR_GLYPH_PADDING_P (*glyph)
19515 && !FRAME_WINDOW_P (it->f))
19516 {
19517 /* A padding glyph that doesn't fit on this line.
19518 This means the whole character doesn't fit
19519 on the line. */
19520 if (row->reversed_p)
19521 unproduce_glyphs (it, row->used[TEXT_AREA]
19522 - n_glyphs_before);
19523 row->used[TEXT_AREA] = n_glyphs_before;
19524
19525 /* Fill the rest of the row with continuation
19526 glyphs like in 20.x. */
19527 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19528 < row->glyphs[1 + TEXT_AREA])
19529 produce_special_glyphs (it, IT_CONTINUATION);
19530
19531 row->continued_p = 1;
19532 it->current_x = x_before;
19533 it->continuation_lines_width += x_before;
19534
19535 /* Restore the height to what it was before the
19536 element not fitting on the line. */
19537 it->max_ascent = ascent;
19538 it->max_descent = descent;
19539 it->max_phys_ascent = phys_ascent;
19540 it->max_phys_descent = phys_descent;
19541 }
19542 else if (wrap_row_used > 0)
19543 {
19544 back_to_wrap:
19545 if (row->reversed_p)
19546 unproduce_glyphs (it,
19547 row->used[TEXT_AREA] - wrap_row_used);
19548 RESTORE_IT (it, &wrap_it, wrap_data);
19549 it->continuation_lines_width += wrap_x;
19550 row->used[TEXT_AREA] = wrap_row_used;
19551 row->ascent = wrap_row_ascent;
19552 row->height = wrap_row_height;
19553 row->phys_ascent = wrap_row_phys_ascent;
19554 row->phys_height = wrap_row_phys_height;
19555 row->extra_line_spacing = wrap_row_extra_line_spacing;
19556 min_pos = wrap_row_min_pos;
19557 min_bpos = wrap_row_min_bpos;
19558 max_pos = wrap_row_max_pos;
19559 max_bpos = wrap_row_max_bpos;
19560 row->continued_p = 1;
19561 row->ends_at_zv_p = 0;
19562 row->exact_window_width_line_p = 0;
19563 it->continuation_lines_width += x;
19564
19565 /* Make sure that a non-default face is extended
19566 up to the right margin of the window. */
19567 extend_face_to_end_of_line (it);
19568 }
19569 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19570 {
19571 /* A TAB that extends past the right edge of the
19572 window. This produces a single glyph on
19573 window system frames. We leave the glyph in
19574 this row and let it fill the row, but don't
19575 consume the TAB. */
19576 if ((row->reversed_p
19577 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19578 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19579 produce_special_glyphs (it, IT_CONTINUATION);
19580 it->continuation_lines_width += it->last_visible_x;
19581 row->ends_in_middle_of_char_p = 1;
19582 row->continued_p = 1;
19583 glyph->pixel_width = it->last_visible_x - x;
19584 it->starts_in_middle_of_char_p = 1;
19585 }
19586 else
19587 {
19588 /* Something other than a TAB that draws past
19589 the right edge of the window. Restore
19590 positions to values before the element. */
19591 if (row->reversed_p)
19592 unproduce_glyphs (it, row->used[TEXT_AREA]
19593 - (n_glyphs_before + i));
19594 row->used[TEXT_AREA] = n_glyphs_before + i;
19595
19596 /* Display continuation glyphs. */
19597 it->current_x = x_before;
19598 it->continuation_lines_width += x;
19599 if (!FRAME_WINDOW_P (it->f)
19600 || (row->reversed_p
19601 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19602 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19603 produce_special_glyphs (it, IT_CONTINUATION);
19604 row->continued_p = 1;
19605
19606 extend_face_to_end_of_line (it);
19607
19608 if (nglyphs > 1 && i > 0)
19609 {
19610 row->ends_in_middle_of_char_p = 1;
19611 it->starts_in_middle_of_char_p = 1;
19612 }
19613
19614 /* Restore the height to what it was before the
19615 element not fitting on the line. */
19616 it->max_ascent = ascent;
19617 it->max_descent = descent;
19618 it->max_phys_ascent = phys_ascent;
19619 it->max_phys_descent = phys_descent;
19620 }
19621
19622 break;
19623 }
19624 else if (new_x > it->first_visible_x)
19625 {
19626 /* Increment number of glyphs actually displayed. */
19627 ++it->hpos;
19628
19629 /* Record the maximum and minimum buffer positions
19630 seen so far in glyphs that will be displayed by
19631 this row. */
19632 if (it->bidi_p)
19633 RECORD_MAX_MIN_POS (it);
19634
19635 if (x < it->first_visible_x)
19636 /* Glyph is partially visible, i.e. row starts at
19637 negative X position. */
19638 row->x = x - it->first_visible_x;
19639 }
19640 else
19641 {
19642 /* Glyph is completely off the left margin of the
19643 window. This should not happen because of the
19644 move_it_in_display_line at the start of this
19645 function, unless the text display area of the
19646 window is empty. */
19647 eassert (it->first_visible_x <= it->last_visible_x);
19648 }
19649 }
19650 /* Even if this display element produced no glyphs at all,
19651 we want to record its position. */
19652 if (it->bidi_p && nglyphs == 0)
19653 RECORD_MAX_MIN_POS (it);
19654
19655 row->ascent = max (row->ascent, it->max_ascent);
19656 row->height = max (row->height, it->max_ascent + it->max_descent);
19657 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19658 row->phys_height = max (row->phys_height,
19659 it->max_phys_ascent + it->max_phys_descent);
19660 row->extra_line_spacing = max (row->extra_line_spacing,
19661 it->max_extra_line_spacing);
19662
19663 /* End of this display line if row is continued. */
19664 if (row->continued_p || row->ends_at_zv_p)
19665 break;
19666 }
19667
19668 at_end_of_line:
19669 /* Is this a line end? If yes, we're also done, after making
19670 sure that a non-default face is extended up to the right
19671 margin of the window. */
19672 if (ITERATOR_AT_END_OF_LINE_P (it))
19673 {
19674 int used_before = row->used[TEXT_AREA];
19675
19676 row->ends_in_newline_from_string_p = STRINGP (it->object);
19677
19678 /* Add a space at the end of the line that is used to
19679 display the cursor there. */
19680 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19681 append_space_for_newline (it, 0);
19682
19683 /* Extend the face to the end of the line. */
19684 extend_face_to_end_of_line (it);
19685
19686 /* Make sure we have the position. */
19687 if (used_before == 0)
19688 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19689
19690 /* Record the position of the newline, for use in
19691 find_row_edges. */
19692 it->eol_pos = it->current.pos;
19693
19694 /* Consume the line end. This skips over invisible lines. */
19695 set_iterator_to_next (it, 1);
19696 it->continuation_lines_width = 0;
19697 break;
19698 }
19699
19700 /* Proceed with next display element. Note that this skips
19701 over lines invisible because of selective display. */
19702 set_iterator_to_next (it, 1);
19703
19704 /* If we truncate lines, we are done when the last displayed
19705 glyphs reach past the right margin of the window. */
19706 if (it->line_wrap == TRUNCATE
19707 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19708 ? (it->current_x >= it->last_visible_x)
19709 : (it->current_x > it->last_visible_x)))
19710 {
19711 /* Maybe add truncation glyphs. */
19712 if (!FRAME_WINDOW_P (it->f)
19713 || (row->reversed_p
19714 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19715 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19716 {
19717 int i, n;
19718
19719 if (!row->reversed_p)
19720 {
19721 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19722 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19723 break;
19724 }
19725 else
19726 {
19727 for (i = 0; i < row->used[TEXT_AREA]; i++)
19728 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19729 break;
19730 /* Remove any padding glyphs at the front of ROW, to
19731 make room for the truncation glyphs we will be
19732 adding below. The loop below always inserts at
19733 least one truncation glyph, so also remove the
19734 last glyph added to ROW. */
19735 unproduce_glyphs (it, i + 1);
19736 /* Adjust i for the loop below. */
19737 i = row->used[TEXT_AREA] - (i + 1);
19738 }
19739
19740 it->current_x = x_before;
19741 if (!FRAME_WINDOW_P (it->f))
19742 {
19743 for (n = row->used[TEXT_AREA]; i < n; ++i)
19744 {
19745 row->used[TEXT_AREA] = i;
19746 produce_special_glyphs (it, IT_TRUNCATION);
19747 }
19748 }
19749 else
19750 {
19751 row->used[TEXT_AREA] = i;
19752 produce_special_glyphs (it, IT_TRUNCATION);
19753 }
19754 }
19755 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19756 {
19757 /* Don't truncate if we can overflow newline into fringe. */
19758 if (!get_next_display_element (it))
19759 {
19760 it->continuation_lines_width = 0;
19761 row->ends_at_zv_p = 1;
19762 row->exact_window_width_line_p = 1;
19763 break;
19764 }
19765 if (ITERATOR_AT_END_OF_LINE_P (it))
19766 {
19767 row->exact_window_width_line_p = 1;
19768 goto at_end_of_line;
19769 }
19770 it->current_x = x_before;
19771 }
19772
19773 row->truncated_on_right_p = 1;
19774 it->continuation_lines_width = 0;
19775 reseat_at_next_visible_line_start (it, 0);
19776 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19777 it->hpos = hpos_before;
19778 break;
19779 }
19780 }
19781
19782 if (wrap_data)
19783 bidi_unshelve_cache (wrap_data, 1);
19784
19785 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19786 at the left window margin. */
19787 if (it->first_visible_x
19788 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19789 {
19790 if (!FRAME_WINDOW_P (it->f)
19791 || (row->reversed_p
19792 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19793 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19794 insert_left_trunc_glyphs (it);
19795 row->truncated_on_left_p = 1;
19796 }
19797
19798 /* Remember the position at which this line ends.
19799
19800 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19801 cannot be before the call to find_row_edges below, since that is
19802 where these positions are determined. */
19803 row->end = it->current;
19804 if (!it->bidi_p)
19805 {
19806 row->minpos = row->start.pos;
19807 row->maxpos = row->end.pos;
19808 }
19809 else
19810 {
19811 /* ROW->minpos and ROW->maxpos must be the smallest and
19812 `1 + the largest' buffer positions in ROW. But if ROW was
19813 bidi-reordered, these two positions can be anywhere in the
19814 row, so we must determine them now. */
19815 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19816 }
19817
19818 /* If the start of this line is the overlay arrow-position, then
19819 mark this glyph row as the one containing the overlay arrow.
19820 This is clearly a mess with variable size fonts. It would be
19821 better to let it be displayed like cursors under X. */
19822 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19823 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19824 !NILP (overlay_arrow_string)))
19825 {
19826 /* Overlay arrow in window redisplay is a fringe bitmap. */
19827 if (STRINGP (overlay_arrow_string))
19828 {
19829 struct glyph_row *arrow_row
19830 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19831 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19832 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19833 struct glyph *p = row->glyphs[TEXT_AREA];
19834 struct glyph *p2, *end;
19835
19836 /* Copy the arrow glyphs. */
19837 while (glyph < arrow_end)
19838 *p++ = *glyph++;
19839
19840 /* Throw away padding glyphs. */
19841 p2 = p;
19842 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19843 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19844 ++p2;
19845 if (p2 > p)
19846 {
19847 while (p2 < end)
19848 *p++ = *p2++;
19849 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19850 }
19851 }
19852 else
19853 {
19854 eassert (INTEGERP (overlay_arrow_string));
19855 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19856 }
19857 overlay_arrow_seen = 1;
19858 }
19859
19860 /* Highlight trailing whitespace. */
19861 if (!NILP (Vshow_trailing_whitespace))
19862 highlight_trailing_whitespace (it->f, it->glyph_row);
19863
19864 /* Compute pixel dimensions of this line. */
19865 compute_line_metrics (it);
19866
19867 /* Implementation note: No changes in the glyphs of ROW or in their
19868 faces can be done past this point, because compute_line_metrics
19869 computes ROW's hash value and stores it within the glyph_row
19870 structure. */
19871
19872 /* Record whether this row ends inside an ellipsis. */
19873 row->ends_in_ellipsis_p
19874 = (it->method == GET_FROM_DISPLAY_VECTOR
19875 && it->ellipsis_p);
19876
19877 /* Save fringe bitmaps in this row. */
19878 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19879 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19880 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19881 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19882
19883 it->left_user_fringe_bitmap = 0;
19884 it->left_user_fringe_face_id = 0;
19885 it->right_user_fringe_bitmap = 0;
19886 it->right_user_fringe_face_id = 0;
19887
19888 /* Maybe set the cursor. */
19889 cvpos = it->w->cursor.vpos;
19890 if ((cvpos < 0
19891 /* In bidi-reordered rows, keep checking for proper cursor
19892 position even if one has been found already, because buffer
19893 positions in such rows change non-linearly with ROW->VPOS,
19894 when a line is continued. One exception: when we are at ZV,
19895 display cursor on the first suitable glyph row, since all
19896 the empty rows after that also have their position set to ZV. */
19897 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19898 lines' rows is implemented for bidi-reordered rows. */
19899 || (it->bidi_p
19900 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19901 && PT >= MATRIX_ROW_START_CHARPOS (row)
19902 && PT <= MATRIX_ROW_END_CHARPOS (row)
19903 && cursor_row_p (row))
19904 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19905
19906 /* Prepare for the next line. This line starts horizontally at (X
19907 HPOS) = (0 0). Vertical positions are incremented. As a
19908 convenience for the caller, IT->glyph_row is set to the next
19909 row to be used. */
19910 it->current_x = it->hpos = 0;
19911 it->current_y += row->height;
19912 SET_TEXT_POS (it->eol_pos, 0, 0);
19913 ++it->vpos;
19914 ++it->glyph_row;
19915 /* The next row should by default use the same value of the
19916 reversed_p flag as this one. set_iterator_to_next decides when
19917 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19918 the flag accordingly. */
19919 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19920 it->glyph_row->reversed_p = row->reversed_p;
19921 it->start = row->end;
19922 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19923
19924 #undef RECORD_MAX_MIN_POS
19925 }
19926
19927 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19928 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19929 doc: /* Return paragraph direction at point in BUFFER.
19930 Value is either `left-to-right' or `right-to-left'.
19931 If BUFFER is omitted or nil, it defaults to the current buffer.
19932
19933 Paragraph direction determines how the text in the paragraph is displayed.
19934 In left-to-right paragraphs, text begins at the left margin of the window
19935 and the reading direction is generally left to right. In right-to-left
19936 paragraphs, text begins at the right margin and is read from right to left.
19937
19938 See also `bidi-paragraph-direction'. */)
19939 (Lisp_Object buffer)
19940 {
19941 struct buffer *buf = current_buffer;
19942 struct buffer *old = buf;
19943
19944 if (! NILP (buffer))
19945 {
19946 CHECK_BUFFER (buffer);
19947 buf = XBUFFER (buffer);
19948 }
19949
19950 if (NILP (BVAR (buf, bidi_display_reordering))
19951 || NILP (BVAR (buf, enable_multibyte_characters))
19952 /* When we are loading loadup.el, the character property tables
19953 needed for bidi iteration are not yet available. */
19954 || !NILP (Vpurify_flag))
19955 return Qleft_to_right;
19956 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19957 return BVAR (buf, bidi_paragraph_direction);
19958 else
19959 {
19960 /* Determine the direction from buffer text. We could try to
19961 use current_matrix if it is up to date, but this seems fast
19962 enough as it is. */
19963 struct bidi_it itb;
19964 ptrdiff_t pos = BUF_PT (buf);
19965 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19966 int c;
19967 void *itb_data = bidi_shelve_cache ();
19968
19969 set_buffer_temp (buf);
19970 /* bidi_paragraph_init finds the base direction of the paragraph
19971 by searching forward from paragraph start. We need the base
19972 direction of the current or _previous_ paragraph, so we need
19973 to make sure we are within that paragraph. To that end, find
19974 the previous non-empty line. */
19975 if (pos >= ZV && pos > BEGV)
19976 DEC_BOTH (pos, bytepos);
19977 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19978 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19979 {
19980 while ((c = FETCH_BYTE (bytepos)) == '\n'
19981 || c == ' ' || c == '\t' || c == '\f')
19982 {
19983 if (bytepos <= BEGV_BYTE)
19984 break;
19985 bytepos--;
19986 pos--;
19987 }
19988 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19989 bytepos--;
19990 }
19991 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19992 itb.paragraph_dir = NEUTRAL_DIR;
19993 itb.string.s = NULL;
19994 itb.string.lstring = Qnil;
19995 itb.string.bufpos = 0;
19996 itb.string.unibyte = 0;
19997 /* We have no window to use here for ignoring window-specific
19998 overlays. Using NULL for window pointer will cause
19999 compute_display_string_pos to use the current buffer. */
20000 itb.w = NULL;
20001 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20002 bidi_unshelve_cache (itb_data, 0);
20003 set_buffer_temp (old);
20004 switch (itb.paragraph_dir)
20005 {
20006 case L2R:
20007 return Qleft_to_right;
20008 break;
20009 case R2L:
20010 return Qright_to_left;
20011 break;
20012 default:
20013 emacs_abort ();
20014 }
20015 }
20016 }
20017
20018 DEFUN ("move-point-visually", Fmove_point_visually,
20019 Smove_point_visually, 1, 1, 0,
20020 doc: /* Move point in the visual order in the specified DIRECTION.
20021 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20022 left.
20023
20024 Value is the new character position of point. */)
20025 (Lisp_Object direction)
20026 {
20027 struct window *w = XWINDOW (selected_window);
20028 struct buffer *b = XBUFFER (w->contents);
20029 struct glyph_row *row;
20030 int dir;
20031 Lisp_Object paragraph_dir;
20032
20033 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20034 (!(ROW)->continued_p \
20035 && INTEGERP ((GLYPH)->object) \
20036 && (GLYPH)->type == CHAR_GLYPH \
20037 && (GLYPH)->u.ch == ' ' \
20038 && (GLYPH)->charpos >= 0 \
20039 && !(GLYPH)->avoid_cursor_p)
20040
20041 CHECK_NUMBER (direction);
20042 dir = XINT (direction);
20043 if (dir > 0)
20044 dir = 1;
20045 else
20046 dir = -1;
20047
20048 /* If current matrix is up-to-date, we can use the information
20049 recorded in the glyphs, at least as long as the goal is on the
20050 screen. */
20051 if (w->window_end_valid
20052 && !windows_or_buffers_changed
20053 && b
20054 && !b->clip_changed
20055 && !b->prevent_redisplay_optimizations_p
20056 && !window_outdated (w)
20057 && w->cursor.vpos >= 0
20058 && w->cursor.vpos < w->current_matrix->nrows
20059 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20060 {
20061 struct glyph *g = row->glyphs[TEXT_AREA];
20062 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20063 struct glyph *gpt = g + w->cursor.hpos;
20064
20065 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20066 {
20067 if (BUFFERP (g->object) && g->charpos != PT)
20068 {
20069 SET_PT (g->charpos);
20070 w->cursor.vpos = -1;
20071 return make_number (PT);
20072 }
20073 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20074 {
20075 ptrdiff_t new_pos;
20076
20077 if (BUFFERP (gpt->object))
20078 {
20079 new_pos = PT;
20080 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20081 new_pos += (row->reversed_p ? -dir : dir);
20082 else
20083 new_pos -= (row->reversed_p ? -dir : dir);;
20084 }
20085 else if (BUFFERP (g->object))
20086 new_pos = g->charpos;
20087 else
20088 break;
20089 SET_PT (new_pos);
20090 w->cursor.vpos = -1;
20091 return make_number (PT);
20092 }
20093 else if (ROW_GLYPH_NEWLINE_P (row, g))
20094 {
20095 /* Glyphs inserted at the end of a non-empty line for
20096 positioning the cursor have zero charpos, so we must
20097 deduce the value of point by other means. */
20098 if (g->charpos > 0)
20099 SET_PT (g->charpos);
20100 else if (row->ends_at_zv_p && PT != ZV)
20101 SET_PT (ZV);
20102 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20103 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20104 else
20105 break;
20106 w->cursor.vpos = -1;
20107 return make_number (PT);
20108 }
20109 }
20110 if (g == e || INTEGERP (g->object))
20111 {
20112 if (row->truncated_on_left_p || row->truncated_on_right_p)
20113 goto simulate_display;
20114 if (!row->reversed_p)
20115 row += dir;
20116 else
20117 row -= dir;
20118 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20119 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20120 goto simulate_display;
20121
20122 if (dir > 0)
20123 {
20124 if (row->reversed_p && !row->continued_p)
20125 {
20126 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20127 w->cursor.vpos = -1;
20128 return make_number (PT);
20129 }
20130 g = row->glyphs[TEXT_AREA];
20131 e = g + row->used[TEXT_AREA];
20132 for ( ; g < e; g++)
20133 {
20134 if (BUFFERP (g->object)
20135 /* Empty lines have only one glyph, which stands
20136 for the newline, and whose charpos is the
20137 buffer position of the newline. */
20138 || ROW_GLYPH_NEWLINE_P (row, g)
20139 /* When the buffer ends in a newline, the line at
20140 EOB also has one glyph, but its charpos is -1. */
20141 || (row->ends_at_zv_p
20142 && !row->reversed_p
20143 && INTEGERP (g->object)
20144 && g->type == CHAR_GLYPH
20145 && g->u.ch == ' '))
20146 {
20147 if (g->charpos > 0)
20148 SET_PT (g->charpos);
20149 else if (!row->reversed_p
20150 && row->ends_at_zv_p
20151 && PT != ZV)
20152 SET_PT (ZV);
20153 else
20154 continue;
20155 w->cursor.vpos = -1;
20156 return make_number (PT);
20157 }
20158 }
20159 }
20160 else
20161 {
20162 if (!row->reversed_p && !row->continued_p)
20163 {
20164 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20165 w->cursor.vpos = -1;
20166 return make_number (PT);
20167 }
20168 e = row->glyphs[TEXT_AREA];
20169 g = e + row->used[TEXT_AREA] - 1;
20170 for ( ; g >= e; g--)
20171 {
20172 if (BUFFERP (g->object)
20173 || (ROW_GLYPH_NEWLINE_P (row, g)
20174 && g->charpos > 0)
20175 /* Empty R2L lines on GUI frames have the buffer
20176 position of the newline stored in the stretch
20177 glyph. */
20178 || g->type == STRETCH_GLYPH
20179 || (row->ends_at_zv_p
20180 && row->reversed_p
20181 && INTEGERP (g->object)
20182 && g->type == CHAR_GLYPH
20183 && g->u.ch == ' '))
20184 {
20185 if (g->charpos > 0)
20186 SET_PT (g->charpos);
20187 else if (row->reversed_p
20188 && row->ends_at_zv_p
20189 && PT != ZV)
20190 SET_PT (ZV);
20191 else
20192 continue;
20193 w->cursor.vpos = -1;
20194 return make_number (PT);
20195 }
20196 }
20197 }
20198 }
20199 }
20200
20201 simulate_display:
20202
20203 /* If we wind up here, we failed to move by using the glyphs, so we
20204 need to simulate display instead. */
20205
20206 if (b)
20207 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20208 else
20209 paragraph_dir = Qleft_to_right;
20210 if (EQ (paragraph_dir, Qright_to_left))
20211 dir = -dir;
20212 if (PT <= BEGV && dir < 0)
20213 xsignal0 (Qbeginning_of_buffer);
20214 else if (PT >= ZV && dir > 0)
20215 xsignal0 (Qend_of_buffer);
20216 else
20217 {
20218 struct text_pos pt;
20219 struct it it;
20220 int pt_x, target_x, pixel_width, pt_vpos;
20221 bool at_eol_p;
20222 bool overshoot_expected = false;
20223 bool target_is_eol_p = false;
20224
20225 /* Setup the arena. */
20226 SET_TEXT_POS (pt, PT, PT_BYTE);
20227 start_display (&it, w, pt);
20228
20229 if (it.cmp_it.id < 0
20230 && it.method == GET_FROM_STRING
20231 && it.area == TEXT_AREA
20232 && it.string_from_display_prop_p
20233 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20234 overshoot_expected = true;
20235
20236 /* Find the X coordinate of point. We start from the beginning
20237 of this or previous line to make sure we are before point in
20238 the logical order (since the move_it_* functions can only
20239 move forward). */
20240 reseat_at_previous_visible_line_start (&it);
20241 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20242 if (IT_CHARPOS (it) != PT)
20243 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20244 -1, -1, -1, MOVE_TO_POS);
20245 pt_x = it.current_x;
20246 pt_vpos = it.vpos;
20247 if (dir > 0 || overshoot_expected)
20248 {
20249 struct glyph_row *row = it.glyph_row;
20250
20251 /* When point is at beginning of line, we don't have
20252 information about the glyph there loaded into struct
20253 it. Calling get_next_display_element fixes that. */
20254 if (pt_x == 0)
20255 get_next_display_element (&it);
20256 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20257 it.glyph_row = NULL;
20258 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20259 it.glyph_row = row;
20260 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20261 it, lest it will become out of sync with it's buffer
20262 position. */
20263 it.current_x = pt_x;
20264 }
20265 else
20266 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20267 pixel_width = it.pixel_width;
20268 if (overshoot_expected && at_eol_p)
20269 pixel_width = 0;
20270 else if (pixel_width <= 0)
20271 pixel_width = 1;
20272
20273 /* If there's a display string at point, we are actually at the
20274 glyph to the left of point, so we need to correct the X
20275 coordinate. */
20276 if (overshoot_expected)
20277 pt_x += pixel_width;
20278
20279 /* Compute target X coordinate, either to the left or to the
20280 right of point. On TTY frames, all characters have the same
20281 pixel width of 1, so we can use that. On GUI frames we don't
20282 have an easy way of getting at the pixel width of the
20283 character to the left of point, so we use a different method
20284 of getting to that place. */
20285 if (dir > 0)
20286 target_x = pt_x + pixel_width;
20287 else
20288 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20289
20290 /* Target X coordinate could be one line above or below the line
20291 of point, in which case we need to adjust the target X
20292 coordinate. Also, if moving to the left, we need to begin at
20293 the left edge of the point's screen line. */
20294 if (dir < 0)
20295 {
20296 if (pt_x > 0)
20297 {
20298 start_display (&it, w, pt);
20299 reseat_at_previous_visible_line_start (&it);
20300 it.current_x = it.current_y = it.hpos = 0;
20301 if (pt_vpos != 0)
20302 move_it_by_lines (&it, pt_vpos);
20303 }
20304 else
20305 {
20306 move_it_by_lines (&it, -1);
20307 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20308 target_is_eol_p = true;
20309 }
20310 }
20311 else
20312 {
20313 if (at_eol_p
20314 || (target_x >= it.last_visible_x
20315 && it.line_wrap != TRUNCATE))
20316 {
20317 if (pt_x > 0)
20318 move_it_by_lines (&it, 0);
20319 move_it_by_lines (&it, 1);
20320 target_x = 0;
20321 }
20322 }
20323
20324 /* Move to the target X coordinate. */
20325 #ifdef HAVE_WINDOW_SYSTEM
20326 /* On GUI frames, as we don't know the X coordinate of the
20327 character to the left of point, moving point to the left
20328 requires walking, one grapheme cluster at a time, until we
20329 find ourself at a place immediately to the left of the
20330 character at point. */
20331 if (FRAME_WINDOW_P (it.f) && dir < 0)
20332 {
20333 struct text_pos new_pos = it.current.pos;
20334 enum move_it_result rc = MOVE_X_REACHED;
20335
20336 while (it.current_x + it.pixel_width <= target_x
20337 && rc == MOVE_X_REACHED)
20338 {
20339 int new_x = it.current_x + it.pixel_width;
20340
20341 new_pos = it.current.pos;
20342 if (new_x == it.current_x)
20343 new_x++;
20344 rc = move_it_in_display_line_to (&it, ZV, new_x,
20345 MOVE_TO_POS | MOVE_TO_X);
20346 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20347 break;
20348 }
20349 /* If we ended up on a composed character inside
20350 bidi-reordered text (e.g., Hebrew text with diacritics),
20351 the iterator gives us the buffer position of the last (in
20352 logical order) character of the composed grapheme cluster,
20353 which is not what we want. So we cheat: we compute the
20354 character position of the character that follows (in the
20355 logical order) the one where the above loop stopped. That
20356 character will appear on display to the left of point. */
20357 if (it.bidi_p
20358 && it.bidi_it.scan_dir == -1
20359 && new_pos.charpos - IT_CHARPOS (it) > 1)
20360 {
20361 new_pos.charpos = IT_CHARPOS (it) + 1;
20362 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20363 }
20364 it.current.pos = new_pos;
20365 }
20366 else
20367 #endif
20368 if (it.current_x != target_x)
20369 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20370
20371 /* When lines are truncated, the above loop will stop at the
20372 window edge. But we want to get to the end of line, even if
20373 it is beyond the window edge; automatic hscroll will then
20374 scroll the window to show point as appropriate. */
20375 if (target_is_eol_p && it.line_wrap == TRUNCATE
20376 && get_next_display_element (&it))
20377 {
20378 struct text_pos new_pos = it.current.pos;
20379
20380 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20381 {
20382 set_iterator_to_next (&it, 0);
20383 if (it.method == GET_FROM_BUFFER)
20384 new_pos = it.current.pos;
20385 if (!get_next_display_element (&it))
20386 break;
20387 }
20388
20389 it.current.pos = new_pos;
20390 }
20391
20392 /* If we ended up in a display string that covers point, move to
20393 buffer position to the right in the visual order. */
20394 if (dir > 0)
20395 {
20396 while (IT_CHARPOS (it) == PT)
20397 {
20398 set_iterator_to_next (&it, 0);
20399 if (!get_next_display_element (&it))
20400 break;
20401 }
20402 }
20403
20404 /* Move point to that position. */
20405 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20406 }
20407
20408 return make_number (PT);
20409
20410 #undef ROW_GLYPH_NEWLINE_P
20411 }
20412
20413 \f
20414 /***********************************************************************
20415 Menu Bar
20416 ***********************************************************************/
20417
20418 /* Redisplay the menu bar in the frame for window W.
20419
20420 The menu bar of X frames that don't have X toolkit support is
20421 displayed in a special window W->frame->menu_bar_window.
20422
20423 The menu bar of terminal frames is treated specially as far as
20424 glyph matrices are concerned. Menu bar lines are not part of
20425 windows, so the update is done directly on the frame matrix rows
20426 for the menu bar. */
20427
20428 static void
20429 display_menu_bar (struct window *w)
20430 {
20431 struct frame *f = XFRAME (WINDOW_FRAME (w));
20432 struct it it;
20433 Lisp_Object items;
20434 int i;
20435
20436 /* Don't do all this for graphical frames. */
20437 #ifdef HAVE_NTGUI
20438 if (FRAME_W32_P (f))
20439 return;
20440 #endif
20441 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20442 if (FRAME_X_P (f))
20443 return;
20444 #endif
20445
20446 #ifdef HAVE_NS
20447 if (FRAME_NS_P (f))
20448 return;
20449 #endif /* HAVE_NS */
20450
20451 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20452 eassert (!FRAME_WINDOW_P (f));
20453 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20454 it.first_visible_x = 0;
20455 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20456 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20457 if (FRAME_WINDOW_P (f))
20458 {
20459 /* Menu bar lines are displayed in the desired matrix of the
20460 dummy window menu_bar_window. */
20461 struct window *menu_w;
20462 menu_w = XWINDOW (f->menu_bar_window);
20463 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20464 MENU_FACE_ID);
20465 it.first_visible_x = 0;
20466 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20467 }
20468 else
20469 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20470 {
20471 /* This is a TTY frame, i.e. character hpos/vpos are used as
20472 pixel x/y. */
20473 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20474 MENU_FACE_ID);
20475 it.first_visible_x = 0;
20476 it.last_visible_x = FRAME_COLS (f);
20477 }
20478
20479 /* FIXME: This should be controlled by a user option. See the
20480 comments in redisplay_tool_bar and display_mode_line about
20481 this. */
20482 it.paragraph_embedding = L2R;
20483
20484 /* Clear all rows of the menu bar. */
20485 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20486 {
20487 struct glyph_row *row = it.glyph_row + i;
20488 clear_glyph_row (row);
20489 row->enabled_p = 1;
20490 row->full_width_p = 1;
20491 }
20492
20493 /* Display all items of the menu bar. */
20494 items = FRAME_MENU_BAR_ITEMS (it.f);
20495 for (i = 0; i < ASIZE (items); i += 4)
20496 {
20497 Lisp_Object string;
20498
20499 /* Stop at nil string. */
20500 string = AREF (items, i + 1);
20501 if (NILP (string))
20502 break;
20503
20504 /* Remember where item was displayed. */
20505 ASET (items, i + 3, make_number (it.hpos));
20506
20507 /* Display the item, pad with one space. */
20508 if (it.current_x < it.last_visible_x)
20509 display_string (NULL, string, Qnil, 0, 0, &it,
20510 SCHARS (string) + 1, 0, 0, -1);
20511 }
20512
20513 /* Fill out the line with spaces. */
20514 if (it.current_x < it.last_visible_x)
20515 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20516
20517 /* Compute the total height of the lines. */
20518 compute_line_metrics (&it);
20519 }
20520
20521 #ifdef HAVE_MENUS
20522 /* Deep copy of a glyph row, including the glyphs. */
20523 static void
20524 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20525 {
20526 struct glyph *pointers[1 + LAST_AREA];
20527 int to_used = to->used[TEXT_AREA];
20528
20529 /* Save glyph pointers of TO. */
20530 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20531
20532 /* Do a structure assignment. */
20533 *to = *from;
20534
20535 /* Restore original glyph pointers of TO. */
20536 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20537
20538 /* Copy the glyphs. */
20539 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
20540 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
20541
20542 /* If we filled only part of the TO row, fill the rest with
20543 space_glyph (which will display as empty space). */
20544 if (to_used > from->used[TEXT_AREA])
20545 fill_up_frame_row_with_spaces (to, to_used);
20546 }
20547
20548 /* Display one menu item on a TTY, by overwriting the glyphs in the
20549 frame F's desired glyph matrix with glyphs produced from the menu
20550 item text. Called from term.c to display TTY drop-down menus one
20551 item at a time.
20552
20553 ITEM_TEXT is the menu item text as a C string.
20554
20555 FACE_ID is the face ID to be used for this menu item. FACE_ID
20556 could specify one of 3 faces: a face for an enabled item, a face
20557 for a disabled item, or a face for a selected item.
20558
20559 X and Y are coordinates of the first glyph in the frame's desired
20560 matrix to be overwritten by the menu item. Since this is a TTY, Y
20561 is the zero-based number of the glyph row and X is the zero-based
20562 glyph number in the row, starting from left, where to start
20563 displaying the item.
20564
20565 SUBMENU non-zero means this menu item drops down a submenu, which
20566 should be indicated by displaying a proper visual cue after the
20567 item text. */
20568
20569 void
20570 display_tty_menu_item (const char *item_text, int width, int face_id,
20571 int x, int y, int submenu)
20572 {
20573 struct it it;
20574 struct frame *f = SELECTED_FRAME ();
20575 struct window *w = XWINDOW (f->selected_window);
20576 int saved_used, saved_truncated, saved_width, saved_reversed;
20577 struct glyph_row *row;
20578 size_t item_len = strlen (item_text);
20579
20580 eassert (FRAME_TERMCAP_P (f));
20581
20582 /* Don't write beyond the matrix's last row. This can happen for
20583 TTY screens that are not high enough to show the entire menu.
20584 (This is actually a bit of defensive programming, as
20585 tty_menu_display already limits the number of menu items to one
20586 less than the number of screen lines.) */
20587 if (y >= f->desired_matrix->nrows)
20588 return;
20589
20590 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
20591 it.first_visible_x = 0;
20592 it.last_visible_x = FRAME_COLS (f) - 1;
20593 row = it.glyph_row;
20594 /* Start with the row contents from the current matrix. */
20595 deep_copy_glyph_row (row, f->current_matrix->rows + y);
20596 saved_width = row->full_width_p;
20597 row->full_width_p = 1;
20598 saved_reversed = row->reversed_p;
20599 row->reversed_p = 0;
20600 row->enabled_p = 1;
20601
20602 /* Arrange for the menu item glyphs to start at (X,Y) and have the
20603 desired face. */
20604 eassert (x < f->desired_matrix->matrix_w);
20605 it.current_x = it.hpos = x;
20606 it.current_y = it.vpos = y;
20607 saved_used = row->used[TEXT_AREA];
20608 saved_truncated = row->truncated_on_right_p;
20609 row->used[TEXT_AREA] = x;
20610 it.face_id = face_id;
20611 it.line_wrap = TRUNCATE;
20612
20613 /* FIXME: This should be controlled by a user option. See the
20614 comments in redisplay_tool_bar and display_mode_line about this.
20615 Also, if paragraph_embedding could ever be R2L, changes will be
20616 needed to avoid shifting to the right the row characters in
20617 term.c:append_glyph. */
20618 it.paragraph_embedding = L2R;
20619
20620 /* Pad with a space on the left. */
20621 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
20622 width--;
20623 /* Display the menu item, pad with spaces to WIDTH. */
20624 if (submenu)
20625 {
20626 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20627 item_len, 0, FRAME_COLS (f) - 1, -1);
20628 width -= item_len;
20629 /* Indicate with " >" that there's a submenu. */
20630 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
20631 FRAME_COLS (f) - 1, -1);
20632 }
20633 else
20634 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20635 width, 0, FRAME_COLS (f) - 1, -1);
20636
20637 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
20638 row->truncated_on_right_p = saved_truncated;
20639 row->hash = row_hash (row);
20640 row->full_width_p = saved_width;
20641 row->reversed_p = saved_reversed;
20642 }
20643 #endif /* HAVE_MENUS */
20644 \f
20645 /***********************************************************************
20646 Mode Line
20647 ***********************************************************************/
20648
20649 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20650 FORCE is non-zero, redisplay mode lines unconditionally.
20651 Otherwise, redisplay only mode lines that are garbaged. Value is
20652 the number of windows whose mode lines were redisplayed. */
20653
20654 static int
20655 redisplay_mode_lines (Lisp_Object window, int force)
20656 {
20657 int nwindows = 0;
20658
20659 while (!NILP (window))
20660 {
20661 struct window *w = XWINDOW (window);
20662
20663 if (WINDOWP (w->contents))
20664 nwindows += redisplay_mode_lines (w->contents, force);
20665 else if (force
20666 || FRAME_GARBAGED_P (XFRAME (w->frame))
20667 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20668 {
20669 struct text_pos lpoint;
20670 struct buffer *old = current_buffer;
20671
20672 /* Set the window's buffer for the mode line display. */
20673 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20674 set_buffer_internal_1 (XBUFFER (w->contents));
20675
20676 /* Point refers normally to the selected window. For any
20677 other window, set up appropriate value. */
20678 if (!EQ (window, selected_window))
20679 {
20680 struct text_pos pt;
20681
20682 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20683 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20684 }
20685
20686 /* Display mode lines. */
20687 clear_glyph_matrix (w->desired_matrix);
20688 if (display_mode_lines (w))
20689 {
20690 ++nwindows;
20691 w->must_be_updated_p = 1;
20692 }
20693
20694 /* Restore old settings. */
20695 set_buffer_internal_1 (old);
20696 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20697 }
20698
20699 window = w->next;
20700 }
20701
20702 return nwindows;
20703 }
20704
20705
20706 /* Display the mode and/or header line of window W. Value is the
20707 sum number of mode lines and header lines displayed. */
20708
20709 static int
20710 display_mode_lines (struct window *w)
20711 {
20712 Lisp_Object old_selected_window = selected_window;
20713 Lisp_Object old_selected_frame = selected_frame;
20714 Lisp_Object new_frame = w->frame;
20715 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20716 int n = 0;
20717
20718 selected_frame = new_frame;
20719 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20720 or window's point, then we'd need select_window_1 here as well. */
20721 XSETWINDOW (selected_window, w);
20722 XFRAME (new_frame)->selected_window = selected_window;
20723
20724 /* These will be set while the mode line specs are processed. */
20725 line_number_displayed = 0;
20726 w->column_number_displayed = -1;
20727
20728 if (WINDOW_WANTS_MODELINE_P (w))
20729 {
20730 struct window *sel_w = XWINDOW (old_selected_window);
20731
20732 /* Select mode line face based on the real selected window. */
20733 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20734 BVAR (current_buffer, mode_line_format));
20735 ++n;
20736 }
20737
20738 if (WINDOW_WANTS_HEADER_LINE_P (w))
20739 {
20740 display_mode_line (w, HEADER_LINE_FACE_ID,
20741 BVAR (current_buffer, header_line_format));
20742 ++n;
20743 }
20744
20745 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20746 selected_frame = old_selected_frame;
20747 selected_window = old_selected_window;
20748 return n;
20749 }
20750
20751
20752 /* Display mode or header line of window W. FACE_ID specifies which
20753 line to display; it is either MODE_LINE_FACE_ID or
20754 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20755 display. Value is the pixel height of the mode/header line
20756 displayed. */
20757
20758 static int
20759 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20760 {
20761 struct it it;
20762 struct face *face;
20763 ptrdiff_t count = SPECPDL_INDEX ();
20764
20765 init_iterator (&it, w, -1, -1, NULL, face_id);
20766 /* Don't extend on a previously drawn mode-line.
20767 This may happen if called from pos_visible_p. */
20768 it.glyph_row->enabled_p = 0;
20769 prepare_desired_row (it.glyph_row);
20770
20771 it.glyph_row->mode_line_p = 1;
20772
20773 /* FIXME: This should be controlled by a user option. But
20774 supporting such an option is not trivial, since the mode line is
20775 made up of many separate strings. */
20776 it.paragraph_embedding = L2R;
20777
20778 record_unwind_protect (unwind_format_mode_line,
20779 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20780
20781 mode_line_target = MODE_LINE_DISPLAY;
20782
20783 /* Temporarily make frame's keyboard the current kboard so that
20784 kboard-local variables in the mode_line_format will get the right
20785 values. */
20786 push_kboard (FRAME_KBOARD (it.f));
20787 record_unwind_save_match_data ();
20788 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20789 pop_kboard ();
20790
20791 unbind_to (count, Qnil);
20792
20793 /* Fill up with spaces. */
20794 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20795
20796 compute_line_metrics (&it);
20797 it.glyph_row->full_width_p = 1;
20798 it.glyph_row->continued_p = 0;
20799 it.glyph_row->truncated_on_left_p = 0;
20800 it.glyph_row->truncated_on_right_p = 0;
20801
20802 /* Make a 3D mode-line have a shadow at its right end. */
20803 face = FACE_FROM_ID (it.f, face_id);
20804 extend_face_to_end_of_line (&it);
20805 if (face->box != FACE_NO_BOX)
20806 {
20807 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20808 + it.glyph_row->used[TEXT_AREA] - 1);
20809 last->right_box_line_p = 1;
20810 }
20811
20812 return it.glyph_row->height;
20813 }
20814
20815 /* Move element ELT in LIST to the front of LIST.
20816 Return the updated list. */
20817
20818 static Lisp_Object
20819 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20820 {
20821 register Lisp_Object tail, prev;
20822 register Lisp_Object tem;
20823
20824 tail = list;
20825 prev = Qnil;
20826 while (CONSP (tail))
20827 {
20828 tem = XCAR (tail);
20829
20830 if (EQ (elt, tem))
20831 {
20832 /* Splice out the link TAIL. */
20833 if (NILP (prev))
20834 list = XCDR (tail);
20835 else
20836 Fsetcdr (prev, XCDR (tail));
20837
20838 /* Now make it the first. */
20839 Fsetcdr (tail, list);
20840 return tail;
20841 }
20842 else
20843 prev = tail;
20844 tail = XCDR (tail);
20845 QUIT;
20846 }
20847
20848 /* Not found--return unchanged LIST. */
20849 return list;
20850 }
20851
20852 /* Contribute ELT to the mode line for window IT->w. How it
20853 translates into text depends on its data type.
20854
20855 IT describes the display environment in which we display, as usual.
20856
20857 DEPTH is the depth in recursion. It is used to prevent
20858 infinite recursion here.
20859
20860 FIELD_WIDTH is the number of characters the display of ELT should
20861 occupy in the mode line, and PRECISION is the maximum number of
20862 characters to display from ELT's representation. See
20863 display_string for details.
20864
20865 Returns the hpos of the end of the text generated by ELT.
20866
20867 PROPS is a property list to add to any string we encounter.
20868
20869 If RISKY is nonzero, remove (disregard) any properties in any string
20870 we encounter, and ignore :eval and :propertize.
20871
20872 The global variable `mode_line_target' determines whether the
20873 output is passed to `store_mode_line_noprop',
20874 `store_mode_line_string', or `display_string'. */
20875
20876 static int
20877 display_mode_element (struct it *it, int depth, int field_width, int precision,
20878 Lisp_Object elt, Lisp_Object props, int risky)
20879 {
20880 int n = 0, field, prec;
20881 int literal = 0;
20882
20883 tail_recurse:
20884 if (depth > 100)
20885 elt = build_string ("*too-deep*");
20886
20887 depth++;
20888
20889 switch (XTYPE (elt))
20890 {
20891 case Lisp_String:
20892 {
20893 /* A string: output it and check for %-constructs within it. */
20894 unsigned char c;
20895 ptrdiff_t offset = 0;
20896
20897 if (SCHARS (elt) > 0
20898 && (!NILP (props) || risky))
20899 {
20900 Lisp_Object oprops, aelt;
20901 oprops = Ftext_properties_at (make_number (0), elt);
20902
20903 /* If the starting string's properties are not what
20904 we want, translate the string. Also, if the string
20905 is risky, do that anyway. */
20906
20907 if (NILP (Fequal (props, oprops)) || risky)
20908 {
20909 /* If the starting string has properties,
20910 merge the specified ones onto the existing ones. */
20911 if (! NILP (oprops) && !risky)
20912 {
20913 Lisp_Object tem;
20914
20915 oprops = Fcopy_sequence (oprops);
20916 tem = props;
20917 while (CONSP (tem))
20918 {
20919 oprops = Fplist_put (oprops, XCAR (tem),
20920 XCAR (XCDR (tem)));
20921 tem = XCDR (XCDR (tem));
20922 }
20923 props = oprops;
20924 }
20925
20926 aelt = Fassoc (elt, mode_line_proptrans_alist);
20927 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20928 {
20929 /* AELT is what we want. Move it to the front
20930 without consing. */
20931 elt = XCAR (aelt);
20932 mode_line_proptrans_alist
20933 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20934 }
20935 else
20936 {
20937 Lisp_Object tem;
20938
20939 /* If AELT has the wrong props, it is useless.
20940 so get rid of it. */
20941 if (! NILP (aelt))
20942 mode_line_proptrans_alist
20943 = Fdelq (aelt, mode_line_proptrans_alist);
20944
20945 elt = Fcopy_sequence (elt);
20946 Fset_text_properties (make_number (0), Flength (elt),
20947 props, elt);
20948 /* Add this item to mode_line_proptrans_alist. */
20949 mode_line_proptrans_alist
20950 = Fcons (Fcons (elt, props),
20951 mode_line_proptrans_alist);
20952 /* Truncate mode_line_proptrans_alist
20953 to at most 50 elements. */
20954 tem = Fnthcdr (make_number (50),
20955 mode_line_proptrans_alist);
20956 if (! NILP (tem))
20957 XSETCDR (tem, Qnil);
20958 }
20959 }
20960 }
20961
20962 offset = 0;
20963
20964 if (literal)
20965 {
20966 prec = precision - n;
20967 switch (mode_line_target)
20968 {
20969 case MODE_LINE_NOPROP:
20970 case MODE_LINE_TITLE:
20971 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20972 break;
20973 case MODE_LINE_STRING:
20974 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20975 break;
20976 case MODE_LINE_DISPLAY:
20977 n += display_string (NULL, elt, Qnil, 0, 0, it,
20978 0, prec, 0, STRING_MULTIBYTE (elt));
20979 break;
20980 }
20981
20982 break;
20983 }
20984
20985 /* Handle the non-literal case. */
20986
20987 while ((precision <= 0 || n < precision)
20988 && SREF (elt, offset) != 0
20989 && (mode_line_target != MODE_LINE_DISPLAY
20990 || it->current_x < it->last_visible_x))
20991 {
20992 ptrdiff_t last_offset = offset;
20993
20994 /* Advance to end of string or next format specifier. */
20995 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20996 ;
20997
20998 if (offset - 1 != last_offset)
20999 {
21000 ptrdiff_t nchars, nbytes;
21001
21002 /* Output to end of string or up to '%'. Field width
21003 is length of string. Don't output more than
21004 PRECISION allows us. */
21005 offset--;
21006
21007 prec = c_string_width (SDATA (elt) + last_offset,
21008 offset - last_offset, precision - n,
21009 &nchars, &nbytes);
21010
21011 switch (mode_line_target)
21012 {
21013 case MODE_LINE_NOPROP:
21014 case MODE_LINE_TITLE:
21015 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21016 break;
21017 case MODE_LINE_STRING:
21018 {
21019 ptrdiff_t bytepos = last_offset;
21020 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21021 ptrdiff_t endpos = (precision <= 0
21022 ? string_byte_to_char (elt, offset)
21023 : charpos + nchars);
21024
21025 n += store_mode_line_string (NULL,
21026 Fsubstring (elt, make_number (charpos),
21027 make_number (endpos)),
21028 0, 0, 0, Qnil);
21029 }
21030 break;
21031 case MODE_LINE_DISPLAY:
21032 {
21033 ptrdiff_t bytepos = last_offset;
21034 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21035
21036 if (precision <= 0)
21037 nchars = string_byte_to_char (elt, offset) - charpos;
21038 n += display_string (NULL, elt, Qnil, 0, charpos,
21039 it, 0, nchars, 0,
21040 STRING_MULTIBYTE (elt));
21041 }
21042 break;
21043 }
21044 }
21045 else /* c == '%' */
21046 {
21047 ptrdiff_t percent_position = offset;
21048
21049 /* Get the specified minimum width. Zero means
21050 don't pad. */
21051 field = 0;
21052 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21053 field = field * 10 + c - '0';
21054
21055 /* Don't pad beyond the total padding allowed. */
21056 if (field_width - n > 0 && field > field_width - n)
21057 field = field_width - n;
21058
21059 /* Note that either PRECISION <= 0 or N < PRECISION. */
21060 prec = precision - n;
21061
21062 if (c == 'M')
21063 n += display_mode_element (it, depth, field, prec,
21064 Vglobal_mode_string, props,
21065 risky);
21066 else if (c != 0)
21067 {
21068 bool multibyte;
21069 ptrdiff_t bytepos, charpos;
21070 const char *spec;
21071 Lisp_Object string;
21072
21073 bytepos = percent_position;
21074 charpos = (STRING_MULTIBYTE (elt)
21075 ? string_byte_to_char (elt, bytepos)
21076 : bytepos);
21077 spec = decode_mode_spec (it->w, c, field, &string);
21078 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21079
21080 switch (mode_line_target)
21081 {
21082 case MODE_LINE_NOPROP:
21083 case MODE_LINE_TITLE:
21084 n += store_mode_line_noprop (spec, field, prec);
21085 break;
21086 case MODE_LINE_STRING:
21087 {
21088 Lisp_Object tem = build_string (spec);
21089 props = Ftext_properties_at (make_number (charpos), elt);
21090 /* Should only keep face property in props */
21091 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21092 }
21093 break;
21094 case MODE_LINE_DISPLAY:
21095 {
21096 int nglyphs_before, nwritten;
21097
21098 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21099 nwritten = display_string (spec, string, elt,
21100 charpos, 0, it,
21101 field, prec, 0,
21102 multibyte);
21103
21104 /* Assign to the glyphs written above the
21105 string where the `%x' came from, position
21106 of the `%'. */
21107 if (nwritten > 0)
21108 {
21109 struct glyph *glyph
21110 = (it->glyph_row->glyphs[TEXT_AREA]
21111 + nglyphs_before);
21112 int i;
21113
21114 for (i = 0; i < nwritten; ++i)
21115 {
21116 glyph[i].object = elt;
21117 glyph[i].charpos = charpos;
21118 }
21119
21120 n += nwritten;
21121 }
21122 }
21123 break;
21124 }
21125 }
21126 else /* c == 0 */
21127 break;
21128 }
21129 }
21130 }
21131 break;
21132
21133 case Lisp_Symbol:
21134 /* A symbol: process the value of the symbol recursively
21135 as if it appeared here directly. Avoid error if symbol void.
21136 Special case: if value of symbol is a string, output the string
21137 literally. */
21138 {
21139 register Lisp_Object tem;
21140
21141 /* If the variable is not marked as risky to set
21142 then its contents are risky to use. */
21143 if (NILP (Fget (elt, Qrisky_local_variable)))
21144 risky = 1;
21145
21146 tem = Fboundp (elt);
21147 if (!NILP (tem))
21148 {
21149 tem = Fsymbol_value (elt);
21150 /* If value is a string, output that string literally:
21151 don't check for % within it. */
21152 if (STRINGP (tem))
21153 literal = 1;
21154
21155 if (!EQ (tem, elt))
21156 {
21157 /* Give up right away for nil or t. */
21158 elt = tem;
21159 goto tail_recurse;
21160 }
21161 }
21162 }
21163 break;
21164
21165 case Lisp_Cons:
21166 {
21167 register Lisp_Object car, tem;
21168
21169 /* A cons cell: five distinct cases.
21170 If first element is :eval or :propertize, do something special.
21171 If first element is a string or a cons, process all the elements
21172 and effectively concatenate them.
21173 If first element is a negative number, truncate displaying cdr to
21174 at most that many characters. If positive, pad (with spaces)
21175 to at least that many characters.
21176 If first element is a symbol, process the cadr or caddr recursively
21177 according to whether the symbol's value is non-nil or nil. */
21178 car = XCAR (elt);
21179 if (EQ (car, QCeval))
21180 {
21181 /* An element of the form (:eval FORM) means evaluate FORM
21182 and use the result as mode line elements. */
21183
21184 if (risky)
21185 break;
21186
21187 if (CONSP (XCDR (elt)))
21188 {
21189 Lisp_Object spec;
21190 spec = safe_eval (XCAR (XCDR (elt)));
21191 n += display_mode_element (it, depth, field_width - n,
21192 precision - n, spec, props,
21193 risky);
21194 }
21195 }
21196 else if (EQ (car, QCpropertize))
21197 {
21198 /* An element of the form (:propertize ELT PROPS...)
21199 means display ELT but applying properties PROPS. */
21200
21201 if (risky)
21202 break;
21203
21204 if (CONSP (XCDR (elt)))
21205 n += display_mode_element (it, depth, field_width - n,
21206 precision - n, XCAR (XCDR (elt)),
21207 XCDR (XCDR (elt)), risky);
21208 }
21209 else if (SYMBOLP (car))
21210 {
21211 tem = Fboundp (car);
21212 elt = XCDR (elt);
21213 if (!CONSP (elt))
21214 goto invalid;
21215 /* elt is now the cdr, and we know it is a cons cell.
21216 Use its car if CAR has a non-nil value. */
21217 if (!NILP (tem))
21218 {
21219 tem = Fsymbol_value (car);
21220 if (!NILP (tem))
21221 {
21222 elt = XCAR (elt);
21223 goto tail_recurse;
21224 }
21225 }
21226 /* Symbol's value is nil (or symbol is unbound)
21227 Get the cddr of the original list
21228 and if possible find the caddr and use that. */
21229 elt = XCDR (elt);
21230 if (NILP (elt))
21231 break;
21232 else if (!CONSP (elt))
21233 goto invalid;
21234 elt = XCAR (elt);
21235 goto tail_recurse;
21236 }
21237 else if (INTEGERP (car))
21238 {
21239 register int lim = XINT (car);
21240 elt = XCDR (elt);
21241 if (lim < 0)
21242 {
21243 /* Negative int means reduce maximum width. */
21244 if (precision <= 0)
21245 precision = -lim;
21246 else
21247 precision = min (precision, -lim);
21248 }
21249 else if (lim > 0)
21250 {
21251 /* Padding specified. Don't let it be more than
21252 current maximum. */
21253 if (precision > 0)
21254 lim = min (precision, lim);
21255
21256 /* If that's more padding than already wanted, queue it.
21257 But don't reduce padding already specified even if
21258 that is beyond the current truncation point. */
21259 field_width = max (lim, field_width);
21260 }
21261 goto tail_recurse;
21262 }
21263 else if (STRINGP (car) || CONSP (car))
21264 {
21265 Lisp_Object halftail = elt;
21266 int len = 0;
21267
21268 while (CONSP (elt)
21269 && (precision <= 0 || n < precision))
21270 {
21271 n += display_mode_element (it, depth,
21272 /* Do padding only after the last
21273 element in the list. */
21274 (! CONSP (XCDR (elt))
21275 ? field_width - n
21276 : 0),
21277 precision - n, XCAR (elt),
21278 props, risky);
21279 elt = XCDR (elt);
21280 len++;
21281 if ((len & 1) == 0)
21282 halftail = XCDR (halftail);
21283 /* Check for cycle. */
21284 if (EQ (halftail, elt))
21285 break;
21286 }
21287 }
21288 }
21289 break;
21290
21291 default:
21292 invalid:
21293 elt = build_string ("*invalid*");
21294 goto tail_recurse;
21295 }
21296
21297 /* Pad to FIELD_WIDTH. */
21298 if (field_width > 0 && n < field_width)
21299 {
21300 switch (mode_line_target)
21301 {
21302 case MODE_LINE_NOPROP:
21303 case MODE_LINE_TITLE:
21304 n += store_mode_line_noprop ("", field_width - n, 0);
21305 break;
21306 case MODE_LINE_STRING:
21307 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21308 break;
21309 case MODE_LINE_DISPLAY:
21310 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21311 0, 0, 0);
21312 break;
21313 }
21314 }
21315
21316 return n;
21317 }
21318
21319 /* Store a mode-line string element in mode_line_string_list.
21320
21321 If STRING is non-null, display that C string. Otherwise, the Lisp
21322 string LISP_STRING is displayed.
21323
21324 FIELD_WIDTH is the minimum number of output glyphs to produce.
21325 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21326 with spaces. FIELD_WIDTH <= 0 means don't pad.
21327
21328 PRECISION is the maximum number of characters to output from
21329 STRING. PRECISION <= 0 means don't truncate the string.
21330
21331 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21332 properties to the string.
21333
21334 PROPS are the properties to add to the string.
21335 The mode_line_string_face face property is always added to the string.
21336 */
21337
21338 static int
21339 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21340 int field_width, int precision, Lisp_Object props)
21341 {
21342 ptrdiff_t len;
21343 int n = 0;
21344
21345 if (string != NULL)
21346 {
21347 len = strlen (string);
21348 if (precision > 0 && len > precision)
21349 len = precision;
21350 lisp_string = make_string (string, len);
21351 if (NILP (props))
21352 props = mode_line_string_face_prop;
21353 else if (!NILP (mode_line_string_face))
21354 {
21355 Lisp_Object face = Fplist_get (props, Qface);
21356 props = Fcopy_sequence (props);
21357 if (NILP (face))
21358 face = mode_line_string_face;
21359 else
21360 face = list2 (face, mode_line_string_face);
21361 props = Fplist_put (props, Qface, face);
21362 }
21363 Fadd_text_properties (make_number (0), make_number (len),
21364 props, lisp_string);
21365 }
21366 else
21367 {
21368 len = XFASTINT (Flength (lisp_string));
21369 if (precision > 0 && len > precision)
21370 {
21371 len = precision;
21372 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21373 precision = -1;
21374 }
21375 if (!NILP (mode_line_string_face))
21376 {
21377 Lisp_Object face;
21378 if (NILP (props))
21379 props = Ftext_properties_at (make_number (0), lisp_string);
21380 face = Fplist_get (props, Qface);
21381 if (NILP (face))
21382 face = mode_line_string_face;
21383 else
21384 face = list2 (face, mode_line_string_face);
21385 props = list2 (Qface, face);
21386 if (copy_string)
21387 lisp_string = Fcopy_sequence (lisp_string);
21388 }
21389 if (!NILP (props))
21390 Fadd_text_properties (make_number (0), make_number (len),
21391 props, lisp_string);
21392 }
21393
21394 if (len > 0)
21395 {
21396 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21397 n += len;
21398 }
21399
21400 if (field_width > len)
21401 {
21402 field_width -= len;
21403 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21404 if (!NILP (props))
21405 Fadd_text_properties (make_number (0), make_number (field_width),
21406 props, lisp_string);
21407 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21408 n += field_width;
21409 }
21410
21411 return n;
21412 }
21413
21414
21415 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21416 1, 4, 0,
21417 doc: /* Format a string out of a mode line format specification.
21418 First arg FORMAT specifies the mode line format (see `mode-line-format'
21419 for details) to use.
21420
21421 By default, the format is evaluated for the currently selected window.
21422
21423 Optional second arg FACE specifies the face property to put on all
21424 characters for which no face is specified. The value nil means the
21425 default face. The value t means whatever face the window's mode line
21426 currently uses (either `mode-line' or `mode-line-inactive',
21427 depending on whether the window is the selected window or not).
21428 An integer value means the value string has no text
21429 properties.
21430
21431 Optional third and fourth args WINDOW and BUFFER specify the window
21432 and buffer to use as the context for the formatting (defaults
21433 are the selected window and the WINDOW's buffer). */)
21434 (Lisp_Object format, Lisp_Object face,
21435 Lisp_Object window, Lisp_Object buffer)
21436 {
21437 struct it it;
21438 int len;
21439 struct window *w;
21440 struct buffer *old_buffer = NULL;
21441 int face_id;
21442 int no_props = INTEGERP (face);
21443 ptrdiff_t count = SPECPDL_INDEX ();
21444 Lisp_Object str;
21445 int string_start = 0;
21446
21447 w = decode_any_window (window);
21448 XSETWINDOW (window, w);
21449
21450 if (NILP (buffer))
21451 buffer = w->contents;
21452 CHECK_BUFFER (buffer);
21453
21454 /* Make formatting the modeline a non-op when noninteractive, otherwise
21455 there will be problems later caused by a partially initialized frame. */
21456 if (NILP (format) || noninteractive)
21457 return empty_unibyte_string;
21458
21459 if (no_props)
21460 face = Qnil;
21461
21462 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21463 : EQ (face, Qt) ? (EQ (window, selected_window)
21464 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21465 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21466 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21467 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21468 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21469 : DEFAULT_FACE_ID;
21470
21471 old_buffer = current_buffer;
21472
21473 /* Save things including mode_line_proptrans_alist,
21474 and set that to nil so that we don't alter the outer value. */
21475 record_unwind_protect (unwind_format_mode_line,
21476 format_mode_line_unwind_data
21477 (XFRAME (WINDOW_FRAME (w)),
21478 old_buffer, selected_window, 1));
21479 mode_line_proptrans_alist = Qnil;
21480
21481 Fselect_window (window, Qt);
21482 set_buffer_internal_1 (XBUFFER (buffer));
21483
21484 init_iterator (&it, w, -1, -1, NULL, face_id);
21485
21486 if (no_props)
21487 {
21488 mode_line_target = MODE_LINE_NOPROP;
21489 mode_line_string_face_prop = Qnil;
21490 mode_line_string_list = Qnil;
21491 string_start = MODE_LINE_NOPROP_LEN (0);
21492 }
21493 else
21494 {
21495 mode_line_target = MODE_LINE_STRING;
21496 mode_line_string_list = Qnil;
21497 mode_line_string_face = face;
21498 mode_line_string_face_prop
21499 = NILP (face) ? Qnil : list2 (Qface, face);
21500 }
21501
21502 push_kboard (FRAME_KBOARD (it.f));
21503 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21504 pop_kboard ();
21505
21506 if (no_props)
21507 {
21508 len = MODE_LINE_NOPROP_LEN (string_start);
21509 str = make_string (mode_line_noprop_buf + string_start, len);
21510 }
21511 else
21512 {
21513 mode_line_string_list = Fnreverse (mode_line_string_list);
21514 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21515 empty_unibyte_string);
21516 }
21517
21518 unbind_to (count, Qnil);
21519 return str;
21520 }
21521
21522 /* Write a null-terminated, right justified decimal representation of
21523 the positive integer D to BUF using a minimal field width WIDTH. */
21524
21525 static void
21526 pint2str (register char *buf, register int width, register ptrdiff_t d)
21527 {
21528 register char *p = buf;
21529
21530 if (d <= 0)
21531 *p++ = '0';
21532 else
21533 {
21534 while (d > 0)
21535 {
21536 *p++ = d % 10 + '0';
21537 d /= 10;
21538 }
21539 }
21540
21541 for (width -= (int) (p - buf); width > 0; --width)
21542 *p++ = ' ';
21543 *p-- = '\0';
21544 while (p > buf)
21545 {
21546 d = *buf;
21547 *buf++ = *p;
21548 *p-- = d;
21549 }
21550 }
21551
21552 /* Write a null-terminated, right justified decimal and "human
21553 readable" representation of the nonnegative integer D to BUF using
21554 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21555
21556 static const char power_letter[] =
21557 {
21558 0, /* no letter */
21559 'k', /* kilo */
21560 'M', /* mega */
21561 'G', /* giga */
21562 'T', /* tera */
21563 'P', /* peta */
21564 'E', /* exa */
21565 'Z', /* zetta */
21566 'Y' /* yotta */
21567 };
21568
21569 static void
21570 pint2hrstr (char *buf, int width, ptrdiff_t d)
21571 {
21572 /* We aim to represent the nonnegative integer D as
21573 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21574 ptrdiff_t quotient = d;
21575 int remainder = 0;
21576 /* -1 means: do not use TENTHS. */
21577 int tenths = -1;
21578 int exponent = 0;
21579
21580 /* Length of QUOTIENT.TENTHS as a string. */
21581 int length;
21582
21583 char * psuffix;
21584 char * p;
21585
21586 if (quotient >= 1000)
21587 {
21588 /* Scale to the appropriate EXPONENT. */
21589 do
21590 {
21591 remainder = quotient % 1000;
21592 quotient /= 1000;
21593 exponent++;
21594 }
21595 while (quotient >= 1000);
21596
21597 /* Round to nearest and decide whether to use TENTHS or not. */
21598 if (quotient <= 9)
21599 {
21600 tenths = remainder / 100;
21601 if (remainder % 100 >= 50)
21602 {
21603 if (tenths < 9)
21604 tenths++;
21605 else
21606 {
21607 quotient++;
21608 if (quotient == 10)
21609 tenths = -1;
21610 else
21611 tenths = 0;
21612 }
21613 }
21614 }
21615 else
21616 if (remainder >= 500)
21617 {
21618 if (quotient < 999)
21619 quotient++;
21620 else
21621 {
21622 quotient = 1;
21623 exponent++;
21624 tenths = 0;
21625 }
21626 }
21627 }
21628
21629 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21630 if (tenths == -1 && quotient <= 99)
21631 if (quotient <= 9)
21632 length = 1;
21633 else
21634 length = 2;
21635 else
21636 length = 3;
21637 p = psuffix = buf + max (width, length);
21638
21639 /* Print EXPONENT. */
21640 *psuffix++ = power_letter[exponent];
21641 *psuffix = '\0';
21642
21643 /* Print TENTHS. */
21644 if (tenths >= 0)
21645 {
21646 *--p = '0' + tenths;
21647 *--p = '.';
21648 }
21649
21650 /* Print QUOTIENT. */
21651 do
21652 {
21653 int digit = quotient % 10;
21654 *--p = '0' + digit;
21655 }
21656 while ((quotient /= 10) != 0);
21657
21658 /* Print leading spaces. */
21659 while (buf < p)
21660 *--p = ' ';
21661 }
21662
21663 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21664 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21665 type of CODING_SYSTEM. Return updated pointer into BUF. */
21666
21667 static unsigned char invalid_eol_type[] = "(*invalid*)";
21668
21669 static char *
21670 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21671 {
21672 Lisp_Object val;
21673 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21674 const unsigned char *eol_str;
21675 int eol_str_len;
21676 /* The EOL conversion we are using. */
21677 Lisp_Object eoltype;
21678
21679 val = CODING_SYSTEM_SPEC (coding_system);
21680 eoltype = Qnil;
21681
21682 if (!VECTORP (val)) /* Not yet decided. */
21683 {
21684 *buf++ = multibyte ? '-' : ' ';
21685 if (eol_flag)
21686 eoltype = eol_mnemonic_undecided;
21687 /* Don't mention EOL conversion if it isn't decided. */
21688 }
21689 else
21690 {
21691 Lisp_Object attrs;
21692 Lisp_Object eolvalue;
21693
21694 attrs = AREF (val, 0);
21695 eolvalue = AREF (val, 2);
21696
21697 *buf++ = multibyte
21698 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21699 : ' ';
21700
21701 if (eol_flag)
21702 {
21703 /* The EOL conversion that is normal on this system. */
21704
21705 if (NILP (eolvalue)) /* Not yet decided. */
21706 eoltype = eol_mnemonic_undecided;
21707 else if (VECTORP (eolvalue)) /* Not yet decided. */
21708 eoltype = eol_mnemonic_undecided;
21709 else /* eolvalue is Qunix, Qdos, or Qmac. */
21710 eoltype = (EQ (eolvalue, Qunix)
21711 ? eol_mnemonic_unix
21712 : (EQ (eolvalue, Qdos) == 1
21713 ? eol_mnemonic_dos : eol_mnemonic_mac));
21714 }
21715 }
21716
21717 if (eol_flag)
21718 {
21719 /* Mention the EOL conversion if it is not the usual one. */
21720 if (STRINGP (eoltype))
21721 {
21722 eol_str = SDATA (eoltype);
21723 eol_str_len = SBYTES (eoltype);
21724 }
21725 else if (CHARACTERP (eoltype))
21726 {
21727 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21728 int c = XFASTINT (eoltype);
21729 eol_str_len = CHAR_STRING (c, tmp);
21730 eol_str = tmp;
21731 }
21732 else
21733 {
21734 eol_str = invalid_eol_type;
21735 eol_str_len = sizeof (invalid_eol_type) - 1;
21736 }
21737 memcpy (buf, eol_str, eol_str_len);
21738 buf += eol_str_len;
21739 }
21740
21741 return buf;
21742 }
21743
21744 /* Return a string for the output of a mode line %-spec for window W,
21745 generated by character C. FIELD_WIDTH > 0 means pad the string
21746 returned with spaces to that value. Return a Lisp string in
21747 *STRING if the resulting string is taken from that Lisp string.
21748
21749 Note we operate on the current buffer for most purposes. */
21750
21751 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21752
21753 static const char *
21754 decode_mode_spec (struct window *w, register int c, int field_width,
21755 Lisp_Object *string)
21756 {
21757 Lisp_Object obj;
21758 struct frame *f = XFRAME (WINDOW_FRAME (w));
21759 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21760 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21761 produce strings from numerical values, so limit preposterously
21762 large values of FIELD_WIDTH to avoid overrunning the buffer's
21763 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21764 bytes plus the terminating null. */
21765 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21766 struct buffer *b = current_buffer;
21767
21768 obj = Qnil;
21769 *string = Qnil;
21770
21771 switch (c)
21772 {
21773 case '*':
21774 if (!NILP (BVAR (b, read_only)))
21775 return "%";
21776 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21777 return "*";
21778 return "-";
21779
21780 case '+':
21781 /* This differs from %* only for a modified read-only buffer. */
21782 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21783 return "*";
21784 if (!NILP (BVAR (b, read_only)))
21785 return "%";
21786 return "-";
21787
21788 case '&':
21789 /* This differs from %* in ignoring read-only-ness. */
21790 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21791 return "*";
21792 return "-";
21793
21794 case '%':
21795 return "%";
21796
21797 case '[':
21798 {
21799 int i;
21800 char *p;
21801
21802 if (command_loop_level > 5)
21803 return "[[[... ";
21804 p = decode_mode_spec_buf;
21805 for (i = 0; i < command_loop_level; i++)
21806 *p++ = '[';
21807 *p = 0;
21808 return decode_mode_spec_buf;
21809 }
21810
21811 case ']':
21812 {
21813 int i;
21814 char *p;
21815
21816 if (command_loop_level > 5)
21817 return " ...]]]";
21818 p = decode_mode_spec_buf;
21819 for (i = 0; i < command_loop_level; i++)
21820 *p++ = ']';
21821 *p = 0;
21822 return decode_mode_spec_buf;
21823 }
21824
21825 case '-':
21826 {
21827 register int i;
21828
21829 /* Let lots_of_dashes be a string of infinite length. */
21830 if (mode_line_target == MODE_LINE_NOPROP
21831 || mode_line_target == MODE_LINE_STRING)
21832 return "--";
21833 if (field_width <= 0
21834 || field_width > sizeof (lots_of_dashes))
21835 {
21836 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21837 decode_mode_spec_buf[i] = '-';
21838 decode_mode_spec_buf[i] = '\0';
21839 return decode_mode_spec_buf;
21840 }
21841 else
21842 return lots_of_dashes;
21843 }
21844
21845 case 'b':
21846 obj = BVAR (b, name);
21847 break;
21848
21849 case 'c':
21850 /* %c and %l are ignored in `frame-title-format'.
21851 (In redisplay_internal, the frame title is drawn _before_ the
21852 windows are updated, so the stuff which depends on actual
21853 window contents (such as %l) may fail to render properly, or
21854 even crash emacs.) */
21855 if (mode_line_target == MODE_LINE_TITLE)
21856 return "";
21857 else
21858 {
21859 ptrdiff_t col = current_column ();
21860 w->column_number_displayed = col;
21861 pint2str (decode_mode_spec_buf, width, col);
21862 return decode_mode_spec_buf;
21863 }
21864
21865 case 'e':
21866 #ifndef SYSTEM_MALLOC
21867 {
21868 if (NILP (Vmemory_full))
21869 return "";
21870 else
21871 return "!MEM FULL! ";
21872 }
21873 #else
21874 return "";
21875 #endif
21876
21877 case 'F':
21878 /* %F displays the frame name. */
21879 if (!NILP (f->title))
21880 return SSDATA (f->title);
21881 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21882 return SSDATA (f->name);
21883 return "Emacs";
21884
21885 case 'f':
21886 obj = BVAR (b, filename);
21887 break;
21888
21889 case 'i':
21890 {
21891 ptrdiff_t size = ZV - BEGV;
21892 pint2str (decode_mode_spec_buf, width, size);
21893 return decode_mode_spec_buf;
21894 }
21895
21896 case 'I':
21897 {
21898 ptrdiff_t size = ZV - BEGV;
21899 pint2hrstr (decode_mode_spec_buf, width, size);
21900 return decode_mode_spec_buf;
21901 }
21902
21903 case 'l':
21904 {
21905 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21906 ptrdiff_t topline, nlines, height;
21907 ptrdiff_t junk;
21908
21909 /* %c and %l are ignored in `frame-title-format'. */
21910 if (mode_line_target == MODE_LINE_TITLE)
21911 return "";
21912
21913 startpos = marker_position (w->start);
21914 startpos_byte = marker_byte_position (w->start);
21915 height = WINDOW_TOTAL_LINES (w);
21916
21917 /* If we decided that this buffer isn't suitable for line numbers,
21918 don't forget that too fast. */
21919 if (w->base_line_pos == -1)
21920 goto no_value;
21921
21922 /* If the buffer is very big, don't waste time. */
21923 if (INTEGERP (Vline_number_display_limit)
21924 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21925 {
21926 w->base_line_pos = 0;
21927 w->base_line_number = 0;
21928 goto no_value;
21929 }
21930
21931 if (w->base_line_number > 0
21932 && w->base_line_pos > 0
21933 && w->base_line_pos <= startpos)
21934 {
21935 line = w->base_line_number;
21936 linepos = w->base_line_pos;
21937 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21938 }
21939 else
21940 {
21941 line = 1;
21942 linepos = BUF_BEGV (b);
21943 linepos_byte = BUF_BEGV_BYTE (b);
21944 }
21945
21946 /* Count lines from base line to window start position. */
21947 nlines = display_count_lines (linepos_byte,
21948 startpos_byte,
21949 startpos, &junk);
21950
21951 topline = nlines + line;
21952
21953 /* Determine a new base line, if the old one is too close
21954 or too far away, or if we did not have one.
21955 "Too close" means it's plausible a scroll-down would
21956 go back past it. */
21957 if (startpos == BUF_BEGV (b))
21958 {
21959 w->base_line_number = topline;
21960 w->base_line_pos = BUF_BEGV (b);
21961 }
21962 else if (nlines < height + 25 || nlines > height * 3 + 50
21963 || linepos == BUF_BEGV (b))
21964 {
21965 ptrdiff_t limit = BUF_BEGV (b);
21966 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21967 ptrdiff_t position;
21968 ptrdiff_t distance =
21969 (height * 2 + 30) * line_number_display_limit_width;
21970
21971 if (startpos - distance > limit)
21972 {
21973 limit = startpos - distance;
21974 limit_byte = CHAR_TO_BYTE (limit);
21975 }
21976
21977 nlines = display_count_lines (startpos_byte,
21978 limit_byte,
21979 - (height * 2 + 30),
21980 &position);
21981 /* If we couldn't find the lines we wanted within
21982 line_number_display_limit_width chars per line,
21983 give up on line numbers for this window. */
21984 if (position == limit_byte && limit == startpos - distance)
21985 {
21986 w->base_line_pos = -1;
21987 w->base_line_number = 0;
21988 goto no_value;
21989 }
21990
21991 w->base_line_number = topline - nlines;
21992 w->base_line_pos = BYTE_TO_CHAR (position);
21993 }
21994
21995 /* Now count lines from the start pos to point. */
21996 nlines = display_count_lines (startpos_byte,
21997 PT_BYTE, PT, &junk);
21998
21999 /* Record that we did display the line number. */
22000 line_number_displayed = 1;
22001
22002 /* Make the string to show. */
22003 pint2str (decode_mode_spec_buf, width, topline + nlines);
22004 return decode_mode_spec_buf;
22005 no_value:
22006 {
22007 char* p = decode_mode_spec_buf;
22008 int pad = width - 2;
22009 while (pad-- > 0)
22010 *p++ = ' ';
22011 *p++ = '?';
22012 *p++ = '?';
22013 *p = '\0';
22014 return decode_mode_spec_buf;
22015 }
22016 }
22017 break;
22018
22019 case 'm':
22020 obj = BVAR (b, mode_name);
22021 break;
22022
22023 case 'n':
22024 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22025 return " Narrow";
22026 break;
22027
22028 case 'p':
22029 {
22030 ptrdiff_t pos = marker_position (w->start);
22031 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22032
22033 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22034 {
22035 if (pos <= BUF_BEGV (b))
22036 return "All";
22037 else
22038 return "Bottom";
22039 }
22040 else if (pos <= BUF_BEGV (b))
22041 return "Top";
22042 else
22043 {
22044 if (total > 1000000)
22045 /* Do it differently for a large value, to avoid overflow. */
22046 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22047 else
22048 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22049 /* We can't normally display a 3-digit number,
22050 so get us a 2-digit number that is close. */
22051 if (total == 100)
22052 total = 99;
22053 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22054 return decode_mode_spec_buf;
22055 }
22056 }
22057
22058 /* Display percentage of size above the bottom of the screen. */
22059 case 'P':
22060 {
22061 ptrdiff_t toppos = marker_position (w->start);
22062 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22063 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22064
22065 if (botpos >= BUF_ZV (b))
22066 {
22067 if (toppos <= BUF_BEGV (b))
22068 return "All";
22069 else
22070 return "Bottom";
22071 }
22072 else
22073 {
22074 if (total > 1000000)
22075 /* Do it differently for a large value, to avoid overflow. */
22076 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22077 else
22078 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22079 /* We can't normally display a 3-digit number,
22080 so get us a 2-digit number that is close. */
22081 if (total == 100)
22082 total = 99;
22083 if (toppos <= BUF_BEGV (b))
22084 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22085 else
22086 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22087 return decode_mode_spec_buf;
22088 }
22089 }
22090
22091 case 's':
22092 /* status of process */
22093 obj = Fget_buffer_process (Fcurrent_buffer ());
22094 if (NILP (obj))
22095 return "no process";
22096 #ifndef MSDOS
22097 obj = Fsymbol_name (Fprocess_status (obj));
22098 #endif
22099 break;
22100
22101 case '@':
22102 {
22103 ptrdiff_t count = inhibit_garbage_collection ();
22104 Lisp_Object val = call1 (intern ("file-remote-p"),
22105 BVAR (current_buffer, directory));
22106 unbind_to (count, Qnil);
22107
22108 if (NILP (val))
22109 return "-";
22110 else
22111 return "@";
22112 }
22113
22114 case 'z':
22115 /* coding-system (not including end-of-line format) */
22116 case 'Z':
22117 /* coding-system (including end-of-line type) */
22118 {
22119 int eol_flag = (c == 'Z');
22120 char *p = decode_mode_spec_buf;
22121
22122 if (! FRAME_WINDOW_P (f))
22123 {
22124 /* No need to mention EOL here--the terminal never needs
22125 to do EOL conversion. */
22126 p = decode_mode_spec_coding (CODING_ID_NAME
22127 (FRAME_KEYBOARD_CODING (f)->id),
22128 p, 0);
22129 p = decode_mode_spec_coding (CODING_ID_NAME
22130 (FRAME_TERMINAL_CODING (f)->id),
22131 p, 0);
22132 }
22133 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22134 p, eol_flag);
22135
22136 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22137 #ifdef subprocesses
22138 obj = Fget_buffer_process (Fcurrent_buffer ());
22139 if (PROCESSP (obj))
22140 {
22141 p = decode_mode_spec_coding
22142 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22143 p = decode_mode_spec_coding
22144 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22145 }
22146 #endif /* subprocesses */
22147 #endif /* 0 */
22148 *p = 0;
22149 return decode_mode_spec_buf;
22150 }
22151 }
22152
22153 if (STRINGP (obj))
22154 {
22155 *string = obj;
22156 return SSDATA (obj);
22157 }
22158 else
22159 return "";
22160 }
22161
22162
22163 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22164 means count lines back from START_BYTE. But don't go beyond
22165 LIMIT_BYTE. Return the number of lines thus found (always
22166 nonnegative).
22167
22168 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22169 either the position COUNT lines after/before START_BYTE, if we
22170 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22171 COUNT lines. */
22172
22173 static ptrdiff_t
22174 display_count_lines (ptrdiff_t start_byte,
22175 ptrdiff_t limit_byte, ptrdiff_t count,
22176 ptrdiff_t *byte_pos_ptr)
22177 {
22178 register unsigned char *cursor;
22179 unsigned char *base;
22180
22181 register ptrdiff_t ceiling;
22182 register unsigned char *ceiling_addr;
22183 ptrdiff_t orig_count = count;
22184
22185 /* If we are not in selective display mode,
22186 check only for newlines. */
22187 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22188 && !INTEGERP (BVAR (current_buffer, selective_display)));
22189
22190 if (count > 0)
22191 {
22192 while (start_byte < limit_byte)
22193 {
22194 ceiling = BUFFER_CEILING_OF (start_byte);
22195 ceiling = min (limit_byte - 1, ceiling);
22196 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22197 base = (cursor = BYTE_POS_ADDR (start_byte));
22198
22199 do
22200 {
22201 if (selective_display)
22202 {
22203 while (*cursor != '\n' && *cursor != 015
22204 && ++cursor != ceiling_addr)
22205 continue;
22206 if (cursor == ceiling_addr)
22207 break;
22208 }
22209 else
22210 {
22211 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22212 if (! cursor)
22213 break;
22214 }
22215
22216 cursor++;
22217
22218 if (--count == 0)
22219 {
22220 start_byte += cursor - base;
22221 *byte_pos_ptr = start_byte;
22222 return orig_count;
22223 }
22224 }
22225 while (cursor < ceiling_addr);
22226
22227 start_byte += ceiling_addr - base;
22228 }
22229 }
22230 else
22231 {
22232 while (start_byte > limit_byte)
22233 {
22234 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22235 ceiling = max (limit_byte, ceiling);
22236 ceiling_addr = BYTE_POS_ADDR (ceiling);
22237 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22238 while (1)
22239 {
22240 if (selective_display)
22241 {
22242 while (--cursor >= ceiling_addr
22243 && *cursor != '\n' && *cursor != 015)
22244 continue;
22245 if (cursor < ceiling_addr)
22246 break;
22247 }
22248 else
22249 {
22250 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22251 if (! cursor)
22252 break;
22253 }
22254
22255 if (++count == 0)
22256 {
22257 start_byte += cursor - base + 1;
22258 *byte_pos_ptr = start_byte;
22259 /* When scanning backwards, we should
22260 not count the newline posterior to which we stop. */
22261 return - orig_count - 1;
22262 }
22263 }
22264 start_byte += ceiling_addr - base;
22265 }
22266 }
22267
22268 *byte_pos_ptr = limit_byte;
22269
22270 if (count < 0)
22271 return - orig_count + count;
22272 return orig_count - count;
22273
22274 }
22275
22276
22277 \f
22278 /***********************************************************************
22279 Displaying strings
22280 ***********************************************************************/
22281
22282 /* Display a NUL-terminated string, starting with index START.
22283
22284 If STRING is non-null, display that C string. Otherwise, the Lisp
22285 string LISP_STRING is displayed. There's a case that STRING is
22286 non-null and LISP_STRING is not nil. It means STRING is a string
22287 data of LISP_STRING. In that case, we display LISP_STRING while
22288 ignoring its text properties.
22289
22290 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22291 FACE_STRING. Display STRING or LISP_STRING with the face at
22292 FACE_STRING_POS in FACE_STRING:
22293
22294 Display the string in the environment given by IT, but use the
22295 standard display table, temporarily.
22296
22297 FIELD_WIDTH is the minimum number of output glyphs to produce.
22298 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22299 with spaces. If STRING has more characters, more than FIELD_WIDTH
22300 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22301
22302 PRECISION is the maximum number of characters to output from
22303 STRING. PRECISION < 0 means don't truncate the string.
22304
22305 This is roughly equivalent to printf format specifiers:
22306
22307 FIELD_WIDTH PRECISION PRINTF
22308 ----------------------------------------
22309 -1 -1 %s
22310 -1 10 %.10s
22311 10 -1 %10s
22312 20 10 %20.10s
22313
22314 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22315 display them, and < 0 means obey the current buffer's value of
22316 enable_multibyte_characters.
22317
22318 Value is the number of columns displayed. */
22319
22320 static int
22321 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22322 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22323 int field_width, int precision, int max_x, int multibyte)
22324 {
22325 int hpos_at_start = it->hpos;
22326 int saved_face_id = it->face_id;
22327 struct glyph_row *row = it->glyph_row;
22328 ptrdiff_t it_charpos;
22329
22330 /* Initialize the iterator IT for iteration over STRING beginning
22331 with index START. */
22332 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22333 precision, field_width, multibyte);
22334 if (string && STRINGP (lisp_string))
22335 /* LISP_STRING is the one returned by decode_mode_spec. We should
22336 ignore its text properties. */
22337 it->stop_charpos = it->end_charpos;
22338
22339 /* If displaying STRING, set up the face of the iterator from
22340 FACE_STRING, if that's given. */
22341 if (STRINGP (face_string))
22342 {
22343 ptrdiff_t endptr;
22344 struct face *face;
22345
22346 it->face_id
22347 = face_at_string_position (it->w, face_string, face_string_pos,
22348 0, &endptr, it->base_face_id, 0);
22349 face = FACE_FROM_ID (it->f, it->face_id);
22350 it->face_box_p = face->box != FACE_NO_BOX;
22351 }
22352
22353 /* Set max_x to the maximum allowed X position. Don't let it go
22354 beyond the right edge of the window. */
22355 if (max_x <= 0)
22356 max_x = it->last_visible_x;
22357 else
22358 max_x = min (max_x, it->last_visible_x);
22359
22360 /* Skip over display elements that are not visible. because IT->w is
22361 hscrolled. */
22362 if (it->current_x < it->first_visible_x)
22363 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22364 MOVE_TO_POS | MOVE_TO_X);
22365
22366 row->ascent = it->max_ascent;
22367 row->height = it->max_ascent + it->max_descent;
22368 row->phys_ascent = it->max_phys_ascent;
22369 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22370 row->extra_line_spacing = it->max_extra_line_spacing;
22371
22372 if (STRINGP (it->string))
22373 it_charpos = IT_STRING_CHARPOS (*it);
22374 else
22375 it_charpos = IT_CHARPOS (*it);
22376
22377 /* This condition is for the case that we are called with current_x
22378 past last_visible_x. */
22379 while (it->current_x < max_x)
22380 {
22381 int x_before, x, n_glyphs_before, i, nglyphs;
22382
22383 /* Get the next display element. */
22384 if (!get_next_display_element (it))
22385 break;
22386
22387 /* Produce glyphs. */
22388 x_before = it->current_x;
22389 n_glyphs_before = row->used[TEXT_AREA];
22390 PRODUCE_GLYPHS (it);
22391
22392 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22393 i = 0;
22394 x = x_before;
22395 while (i < nglyphs)
22396 {
22397 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22398
22399 if (it->line_wrap != TRUNCATE
22400 && x + glyph->pixel_width > max_x)
22401 {
22402 /* End of continued line or max_x reached. */
22403 if (CHAR_GLYPH_PADDING_P (*glyph))
22404 {
22405 /* A wide character is unbreakable. */
22406 if (row->reversed_p)
22407 unproduce_glyphs (it, row->used[TEXT_AREA]
22408 - n_glyphs_before);
22409 row->used[TEXT_AREA] = n_glyphs_before;
22410 it->current_x = x_before;
22411 }
22412 else
22413 {
22414 if (row->reversed_p)
22415 unproduce_glyphs (it, row->used[TEXT_AREA]
22416 - (n_glyphs_before + i));
22417 row->used[TEXT_AREA] = n_glyphs_before + i;
22418 it->current_x = x;
22419 }
22420 break;
22421 }
22422 else if (x + glyph->pixel_width >= it->first_visible_x)
22423 {
22424 /* Glyph is at least partially visible. */
22425 ++it->hpos;
22426 if (x < it->first_visible_x)
22427 row->x = x - it->first_visible_x;
22428 }
22429 else
22430 {
22431 /* Glyph is off the left margin of the display area.
22432 Should not happen. */
22433 emacs_abort ();
22434 }
22435
22436 row->ascent = max (row->ascent, it->max_ascent);
22437 row->height = max (row->height, it->max_ascent + it->max_descent);
22438 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22439 row->phys_height = max (row->phys_height,
22440 it->max_phys_ascent + it->max_phys_descent);
22441 row->extra_line_spacing = max (row->extra_line_spacing,
22442 it->max_extra_line_spacing);
22443 x += glyph->pixel_width;
22444 ++i;
22445 }
22446
22447 /* Stop if max_x reached. */
22448 if (i < nglyphs)
22449 break;
22450
22451 /* Stop at line ends. */
22452 if (ITERATOR_AT_END_OF_LINE_P (it))
22453 {
22454 it->continuation_lines_width = 0;
22455 break;
22456 }
22457
22458 set_iterator_to_next (it, 1);
22459 if (STRINGP (it->string))
22460 it_charpos = IT_STRING_CHARPOS (*it);
22461 else
22462 it_charpos = IT_CHARPOS (*it);
22463
22464 /* Stop if truncating at the right edge. */
22465 if (it->line_wrap == TRUNCATE
22466 && it->current_x >= it->last_visible_x)
22467 {
22468 /* Add truncation mark, but don't do it if the line is
22469 truncated at a padding space. */
22470 if (it_charpos < it->string_nchars)
22471 {
22472 if (!FRAME_WINDOW_P (it->f))
22473 {
22474 int ii, n;
22475
22476 if (it->current_x > it->last_visible_x)
22477 {
22478 if (!row->reversed_p)
22479 {
22480 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22481 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22482 break;
22483 }
22484 else
22485 {
22486 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22487 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22488 break;
22489 unproduce_glyphs (it, ii + 1);
22490 ii = row->used[TEXT_AREA] - (ii + 1);
22491 }
22492 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22493 {
22494 row->used[TEXT_AREA] = ii;
22495 produce_special_glyphs (it, IT_TRUNCATION);
22496 }
22497 }
22498 produce_special_glyphs (it, IT_TRUNCATION);
22499 }
22500 row->truncated_on_right_p = 1;
22501 }
22502 break;
22503 }
22504 }
22505
22506 /* Maybe insert a truncation at the left. */
22507 if (it->first_visible_x
22508 && it_charpos > 0)
22509 {
22510 if (!FRAME_WINDOW_P (it->f)
22511 || (row->reversed_p
22512 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22513 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22514 insert_left_trunc_glyphs (it);
22515 row->truncated_on_left_p = 1;
22516 }
22517
22518 it->face_id = saved_face_id;
22519
22520 /* Value is number of columns displayed. */
22521 return it->hpos - hpos_at_start;
22522 }
22523
22524
22525 \f
22526 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22527 appears as an element of LIST or as the car of an element of LIST.
22528 If PROPVAL is a list, compare each element against LIST in that
22529 way, and return 1/2 if any element of PROPVAL is found in LIST.
22530 Otherwise return 0. This function cannot quit.
22531 The return value is 2 if the text is invisible but with an ellipsis
22532 and 1 if it's invisible and without an ellipsis. */
22533
22534 int
22535 invisible_p (register Lisp_Object propval, Lisp_Object list)
22536 {
22537 register Lisp_Object tail, proptail;
22538
22539 for (tail = list; CONSP (tail); tail = XCDR (tail))
22540 {
22541 register Lisp_Object tem;
22542 tem = XCAR (tail);
22543 if (EQ (propval, tem))
22544 return 1;
22545 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22546 return NILP (XCDR (tem)) ? 1 : 2;
22547 }
22548
22549 if (CONSP (propval))
22550 {
22551 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22552 {
22553 Lisp_Object propelt;
22554 propelt = XCAR (proptail);
22555 for (tail = list; CONSP (tail); tail = XCDR (tail))
22556 {
22557 register Lisp_Object tem;
22558 tem = XCAR (tail);
22559 if (EQ (propelt, tem))
22560 return 1;
22561 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22562 return NILP (XCDR (tem)) ? 1 : 2;
22563 }
22564 }
22565 }
22566
22567 return 0;
22568 }
22569
22570 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22571 doc: /* Non-nil if the property makes the text invisible.
22572 POS-OR-PROP can be a marker or number, in which case it is taken to be
22573 a position in the current buffer and the value of the `invisible' property
22574 is checked; or it can be some other value, which is then presumed to be the
22575 value of the `invisible' property of the text of interest.
22576 The non-nil value returned can be t for truly invisible text or something
22577 else if the text is replaced by an ellipsis. */)
22578 (Lisp_Object pos_or_prop)
22579 {
22580 Lisp_Object prop
22581 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22582 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22583 : pos_or_prop);
22584 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22585 return (invis == 0 ? Qnil
22586 : invis == 1 ? Qt
22587 : make_number (invis));
22588 }
22589
22590 /* Calculate a width or height in pixels from a specification using
22591 the following elements:
22592
22593 SPEC ::=
22594 NUM - a (fractional) multiple of the default font width/height
22595 (NUM) - specifies exactly NUM pixels
22596 UNIT - a fixed number of pixels, see below.
22597 ELEMENT - size of a display element in pixels, see below.
22598 (NUM . SPEC) - equals NUM * SPEC
22599 (+ SPEC SPEC ...) - add pixel values
22600 (- SPEC SPEC ...) - subtract pixel values
22601 (- SPEC) - negate pixel value
22602
22603 NUM ::=
22604 INT or FLOAT - a number constant
22605 SYMBOL - use symbol's (buffer local) variable binding.
22606
22607 UNIT ::=
22608 in - pixels per inch *)
22609 mm - pixels per 1/1000 meter *)
22610 cm - pixels per 1/100 meter *)
22611 width - width of current font in pixels.
22612 height - height of current font in pixels.
22613
22614 *) using the ratio(s) defined in display-pixels-per-inch.
22615
22616 ELEMENT ::=
22617
22618 left-fringe - left fringe width in pixels
22619 right-fringe - right fringe width in pixels
22620
22621 left-margin - left margin width in pixels
22622 right-margin - right margin width in pixels
22623
22624 scroll-bar - scroll-bar area width in pixels
22625
22626 Examples:
22627
22628 Pixels corresponding to 5 inches:
22629 (5 . in)
22630
22631 Total width of non-text areas on left side of window (if scroll-bar is on left):
22632 '(space :width (+ left-fringe left-margin scroll-bar))
22633
22634 Align to first text column (in header line):
22635 '(space :align-to 0)
22636
22637 Align to middle of text area minus half the width of variable `my-image'
22638 containing a loaded image:
22639 '(space :align-to (0.5 . (- text my-image)))
22640
22641 Width of left margin minus width of 1 character in the default font:
22642 '(space :width (- left-margin 1))
22643
22644 Width of left margin minus width of 2 characters in the current font:
22645 '(space :width (- left-margin (2 . width)))
22646
22647 Center 1 character over left-margin (in header line):
22648 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22649
22650 Different ways to express width of left fringe plus left margin minus one pixel:
22651 '(space :width (- (+ left-fringe left-margin) (1)))
22652 '(space :width (+ left-fringe left-margin (- (1))))
22653 '(space :width (+ left-fringe left-margin (-1)))
22654
22655 */
22656
22657 static int
22658 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22659 struct font *font, int width_p, int *align_to)
22660 {
22661 double pixels;
22662
22663 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22664 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22665
22666 if (NILP (prop))
22667 return OK_PIXELS (0);
22668
22669 eassert (FRAME_LIVE_P (it->f));
22670
22671 if (SYMBOLP (prop))
22672 {
22673 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22674 {
22675 char *unit = SSDATA (SYMBOL_NAME (prop));
22676
22677 if (unit[0] == 'i' && unit[1] == 'n')
22678 pixels = 1.0;
22679 else if (unit[0] == 'm' && unit[1] == 'm')
22680 pixels = 25.4;
22681 else if (unit[0] == 'c' && unit[1] == 'm')
22682 pixels = 2.54;
22683 else
22684 pixels = 0;
22685 if (pixels > 0)
22686 {
22687 double ppi = (width_p ? FRAME_RES_X (it->f)
22688 : FRAME_RES_Y (it->f));
22689
22690 if (ppi > 0)
22691 return OK_PIXELS (ppi / pixels);
22692 return 0;
22693 }
22694 }
22695
22696 #ifdef HAVE_WINDOW_SYSTEM
22697 if (EQ (prop, Qheight))
22698 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22699 if (EQ (prop, Qwidth))
22700 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22701 #else
22702 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22703 return OK_PIXELS (1);
22704 #endif
22705
22706 if (EQ (prop, Qtext))
22707 return OK_PIXELS (width_p
22708 ? window_box_width (it->w, TEXT_AREA)
22709 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22710
22711 if (align_to && *align_to < 0)
22712 {
22713 *res = 0;
22714 if (EQ (prop, Qleft))
22715 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22716 if (EQ (prop, Qright))
22717 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22718 if (EQ (prop, Qcenter))
22719 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22720 + window_box_width (it->w, TEXT_AREA) / 2);
22721 if (EQ (prop, Qleft_fringe))
22722 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22723 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22724 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22725 if (EQ (prop, Qright_fringe))
22726 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22727 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22728 : window_box_right_offset (it->w, TEXT_AREA));
22729 if (EQ (prop, Qleft_margin))
22730 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22731 if (EQ (prop, Qright_margin))
22732 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22733 if (EQ (prop, Qscroll_bar))
22734 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22735 ? 0
22736 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22737 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22738 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22739 : 0)));
22740 }
22741 else
22742 {
22743 if (EQ (prop, Qleft_fringe))
22744 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22745 if (EQ (prop, Qright_fringe))
22746 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22747 if (EQ (prop, Qleft_margin))
22748 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22749 if (EQ (prop, Qright_margin))
22750 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22751 if (EQ (prop, Qscroll_bar))
22752 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22753 }
22754
22755 prop = buffer_local_value_1 (prop, it->w->contents);
22756 if (EQ (prop, Qunbound))
22757 prop = Qnil;
22758 }
22759
22760 if (INTEGERP (prop) || FLOATP (prop))
22761 {
22762 int base_unit = (width_p
22763 ? FRAME_COLUMN_WIDTH (it->f)
22764 : FRAME_LINE_HEIGHT (it->f));
22765 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22766 }
22767
22768 if (CONSP (prop))
22769 {
22770 Lisp_Object car = XCAR (prop);
22771 Lisp_Object cdr = XCDR (prop);
22772
22773 if (SYMBOLP (car))
22774 {
22775 #ifdef HAVE_WINDOW_SYSTEM
22776 if (FRAME_WINDOW_P (it->f)
22777 && valid_image_p (prop))
22778 {
22779 ptrdiff_t id = lookup_image (it->f, prop);
22780 struct image *img = IMAGE_FROM_ID (it->f, id);
22781
22782 return OK_PIXELS (width_p ? img->width : img->height);
22783 }
22784 #endif
22785 if (EQ (car, Qplus) || EQ (car, Qminus))
22786 {
22787 int first = 1;
22788 double px;
22789
22790 pixels = 0;
22791 while (CONSP (cdr))
22792 {
22793 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22794 font, width_p, align_to))
22795 return 0;
22796 if (first)
22797 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22798 else
22799 pixels += px;
22800 cdr = XCDR (cdr);
22801 }
22802 if (EQ (car, Qminus))
22803 pixels = -pixels;
22804 return OK_PIXELS (pixels);
22805 }
22806
22807 car = buffer_local_value_1 (car, it->w->contents);
22808 if (EQ (car, Qunbound))
22809 car = Qnil;
22810 }
22811
22812 if (INTEGERP (car) || FLOATP (car))
22813 {
22814 double fact;
22815 pixels = XFLOATINT (car);
22816 if (NILP (cdr))
22817 return OK_PIXELS (pixels);
22818 if (calc_pixel_width_or_height (&fact, it, cdr,
22819 font, width_p, align_to))
22820 return OK_PIXELS (pixels * fact);
22821 return 0;
22822 }
22823
22824 return 0;
22825 }
22826
22827 return 0;
22828 }
22829
22830 \f
22831 /***********************************************************************
22832 Glyph Display
22833 ***********************************************************************/
22834
22835 #ifdef HAVE_WINDOW_SYSTEM
22836
22837 #ifdef GLYPH_DEBUG
22838
22839 void
22840 dump_glyph_string (struct glyph_string *s)
22841 {
22842 fprintf (stderr, "glyph string\n");
22843 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22844 s->x, s->y, s->width, s->height);
22845 fprintf (stderr, " ybase = %d\n", s->ybase);
22846 fprintf (stderr, " hl = %d\n", s->hl);
22847 fprintf (stderr, " left overhang = %d, right = %d\n",
22848 s->left_overhang, s->right_overhang);
22849 fprintf (stderr, " nchars = %d\n", s->nchars);
22850 fprintf (stderr, " extends to end of line = %d\n",
22851 s->extends_to_end_of_line_p);
22852 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22853 fprintf (stderr, " bg width = %d\n", s->background_width);
22854 }
22855
22856 #endif /* GLYPH_DEBUG */
22857
22858 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22859 of XChar2b structures for S; it can't be allocated in
22860 init_glyph_string because it must be allocated via `alloca'. W
22861 is the window on which S is drawn. ROW and AREA are the glyph row
22862 and area within the row from which S is constructed. START is the
22863 index of the first glyph structure covered by S. HL is a
22864 face-override for drawing S. */
22865
22866 #ifdef HAVE_NTGUI
22867 #define OPTIONAL_HDC(hdc) HDC hdc,
22868 #define DECLARE_HDC(hdc) HDC hdc;
22869 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22870 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22871 #endif
22872
22873 #ifndef OPTIONAL_HDC
22874 #define OPTIONAL_HDC(hdc)
22875 #define DECLARE_HDC(hdc)
22876 #define ALLOCATE_HDC(hdc, f)
22877 #define RELEASE_HDC(hdc, f)
22878 #endif
22879
22880 static void
22881 init_glyph_string (struct glyph_string *s,
22882 OPTIONAL_HDC (hdc)
22883 XChar2b *char2b, struct window *w, struct glyph_row *row,
22884 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22885 {
22886 memset (s, 0, sizeof *s);
22887 s->w = w;
22888 s->f = XFRAME (w->frame);
22889 #ifdef HAVE_NTGUI
22890 s->hdc = hdc;
22891 #endif
22892 s->display = FRAME_X_DISPLAY (s->f);
22893 s->window = FRAME_X_WINDOW (s->f);
22894 s->char2b = char2b;
22895 s->hl = hl;
22896 s->row = row;
22897 s->area = area;
22898 s->first_glyph = row->glyphs[area] + start;
22899 s->height = row->height;
22900 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22901 s->ybase = s->y + row->ascent;
22902 }
22903
22904
22905 /* Append the list of glyph strings with head H and tail T to the list
22906 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22907
22908 static void
22909 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22910 struct glyph_string *h, struct glyph_string *t)
22911 {
22912 if (h)
22913 {
22914 if (*head)
22915 (*tail)->next = h;
22916 else
22917 *head = h;
22918 h->prev = *tail;
22919 *tail = t;
22920 }
22921 }
22922
22923
22924 /* Prepend the list of glyph strings with head H and tail T to the
22925 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22926 result. */
22927
22928 static void
22929 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22930 struct glyph_string *h, struct glyph_string *t)
22931 {
22932 if (h)
22933 {
22934 if (*head)
22935 (*head)->prev = t;
22936 else
22937 *tail = t;
22938 t->next = *head;
22939 *head = h;
22940 }
22941 }
22942
22943
22944 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22945 Set *HEAD and *TAIL to the resulting list. */
22946
22947 static void
22948 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22949 struct glyph_string *s)
22950 {
22951 s->next = s->prev = NULL;
22952 append_glyph_string_lists (head, tail, s, s);
22953 }
22954
22955
22956 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22957 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22958 make sure that X resources for the face returned are allocated.
22959 Value is a pointer to a realized face that is ready for display if
22960 DISPLAY_P is non-zero. */
22961
22962 static struct face *
22963 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22964 XChar2b *char2b, int display_p)
22965 {
22966 struct face *face = FACE_FROM_ID (f, face_id);
22967 unsigned code = 0;
22968
22969 if (face->font)
22970 {
22971 code = face->font->driver->encode_char (face->font, c);
22972
22973 if (code == FONT_INVALID_CODE)
22974 code = 0;
22975 }
22976 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22977
22978 /* Make sure X resources of the face are allocated. */
22979 #ifdef HAVE_X_WINDOWS
22980 if (display_p)
22981 #endif
22982 {
22983 eassert (face != NULL);
22984 PREPARE_FACE_FOR_DISPLAY (f, face);
22985 }
22986
22987 return face;
22988 }
22989
22990
22991 /* Get face and two-byte form of character glyph GLYPH on frame F.
22992 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22993 a pointer to a realized face that is ready for display. */
22994
22995 static struct face *
22996 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22997 XChar2b *char2b, int *two_byte_p)
22998 {
22999 struct face *face;
23000 unsigned code = 0;
23001
23002 eassert (glyph->type == CHAR_GLYPH);
23003 face = FACE_FROM_ID (f, glyph->face_id);
23004
23005 /* Make sure X resources of the face are allocated. */
23006 eassert (face != NULL);
23007 PREPARE_FACE_FOR_DISPLAY (f, face);
23008
23009 if (two_byte_p)
23010 *two_byte_p = 0;
23011
23012 if (face->font)
23013 {
23014 if (CHAR_BYTE8_P (glyph->u.ch))
23015 code = CHAR_TO_BYTE8 (glyph->u.ch);
23016 else
23017 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23018
23019 if (code == FONT_INVALID_CODE)
23020 code = 0;
23021 }
23022
23023 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23024 return face;
23025 }
23026
23027
23028 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23029 Return 1 if FONT has a glyph for C, otherwise return 0. */
23030
23031 static int
23032 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23033 {
23034 unsigned code;
23035
23036 if (CHAR_BYTE8_P (c))
23037 code = CHAR_TO_BYTE8 (c);
23038 else
23039 code = font->driver->encode_char (font, c);
23040
23041 if (code == FONT_INVALID_CODE)
23042 return 0;
23043 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23044 return 1;
23045 }
23046
23047
23048 /* Fill glyph string S with composition components specified by S->cmp.
23049
23050 BASE_FACE is the base face of the composition.
23051 S->cmp_from is the index of the first component for S.
23052
23053 OVERLAPS non-zero means S should draw the foreground only, and use
23054 its physical height for clipping. See also draw_glyphs.
23055
23056 Value is the index of a component not in S. */
23057
23058 static int
23059 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23060 int overlaps)
23061 {
23062 int i;
23063 /* For all glyphs of this composition, starting at the offset
23064 S->cmp_from, until we reach the end of the definition or encounter a
23065 glyph that requires the different face, add it to S. */
23066 struct face *face;
23067
23068 eassert (s);
23069
23070 s->for_overlaps = overlaps;
23071 s->face = NULL;
23072 s->font = NULL;
23073 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23074 {
23075 int c = COMPOSITION_GLYPH (s->cmp, i);
23076
23077 /* TAB in a composition means display glyphs with padding space
23078 on the left or right. */
23079 if (c != '\t')
23080 {
23081 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23082 -1, Qnil);
23083
23084 face = get_char_face_and_encoding (s->f, c, face_id,
23085 s->char2b + i, 1);
23086 if (face)
23087 {
23088 if (! s->face)
23089 {
23090 s->face = face;
23091 s->font = s->face->font;
23092 }
23093 else if (s->face != face)
23094 break;
23095 }
23096 }
23097 ++s->nchars;
23098 }
23099 s->cmp_to = i;
23100
23101 if (s->face == NULL)
23102 {
23103 s->face = base_face->ascii_face;
23104 s->font = s->face->font;
23105 }
23106
23107 /* All glyph strings for the same composition has the same width,
23108 i.e. the width set for the first component of the composition. */
23109 s->width = s->first_glyph->pixel_width;
23110
23111 /* If the specified font could not be loaded, use the frame's
23112 default font, but record the fact that we couldn't load it in
23113 the glyph string so that we can draw rectangles for the
23114 characters of the glyph string. */
23115 if (s->font == NULL)
23116 {
23117 s->font_not_found_p = 1;
23118 s->font = FRAME_FONT (s->f);
23119 }
23120
23121 /* Adjust base line for subscript/superscript text. */
23122 s->ybase += s->first_glyph->voffset;
23123
23124 /* This glyph string must always be drawn with 16-bit functions. */
23125 s->two_byte_p = 1;
23126
23127 return s->cmp_to;
23128 }
23129
23130 static int
23131 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23132 int start, int end, int overlaps)
23133 {
23134 struct glyph *glyph, *last;
23135 Lisp_Object lgstring;
23136 int i;
23137
23138 s->for_overlaps = overlaps;
23139 glyph = s->row->glyphs[s->area] + start;
23140 last = s->row->glyphs[s->area] + end;
23141 s->cmp_id = glyph->u.cmp.id;
23142 s->cmp_from = glyph->slice.cmp.from;
23143 s->cmp_to = glyph->slice.cmp.to + 1;
23144 s->face = FACE_FROM_ID (s->f, face_id);
23145 lgstring = composition_gstring_from_id (s->cmp_id);
23146 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23147 glyph++;
23148 while (glyph < last
23149 && glyph->u.cmp.automatic
23150 && glyph->u.cmp.id == s->cmp_id
23151 && s->cmp_to == glyph->slice.cmp.from)
23152 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23153
23154 for (i = s->cmp_from; i < s->cmp_to; i++)
23155 {
23156 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23157 unsigned code = LGLYPH_CODE (lglyph);
23158
23159 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23160 }
23161 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23162 return glyph - s->row->glyphs[s->area];
23163 }
23164
23165
23166 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23167 See the comment of fill_glyph_string for arguments.
23168 Value is the index of the first glyph not in S. */
23169
23170
23171 static int
23172 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23173 int start, int end, int overlaps)
23174 {
23175 struct glyph *glyph, *last;
23176 int voffset;
23177
23178 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23179 s->for_overlaps = overlaps;
23180 glyph = s->row->glyphs[s->area] + start;
23181 last = s->row->glyphs[s->area] + end;
23182 voffset = glyph->voffset;
23183 s->face = FACE_FROM_ID (s->f, face_id);
23184 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23185 s->nchars = 1;
23186 s->width = glyph->pixel_width;
23187 glyph++;
23188 while (glyph < last
23189 && glyph->type == GLYPHLESS_GLYPH
23190 && glyph->voffset == voffset
23191 && glyph->face_id == face_id)
23192 {
23193 s->nchars++;
23194 s->width += glyph->pixel_width;
23195 glyph++;
23196 }
23197 s->ybase += voffset;
23198 return glyph - s->row->glyphs[s->area];
23199 }
23200
23201
23202 /* Fill glyph string S from a sequence of character glyphs.
23203
23204 FACE_ID is the face id of the string. START is the index of the
23205 first glyph to consider, END is the index of the last + 1.
23206 OVERLAPS non-zero means S should draw the foreground only, and use
23207 its physical height for clipping. See also draw_glyphs.
23208
23209 Value is the index of the first glyph not in S. */
23210
23211 static int
23212 fill_glyph_string (struct glyph_string *s, int face_id,
23213 int start, int end, int overlaps)
23214 {
23215 struct glyph *glyph, *last;
23216 int voffset;
23217 int glyph_not_available_p;
23218
23219 eassert (s->f == XFRAME (s->w->frame));
23220 eassert (s->nchars == 0);
23221 eassert (start >= 0 && end > start);
23222
23223 s->for_overlaps = overlaps;
23224 glyph = s->row->glyphs[s->area] + start;
23225 last = s->row->glyphs[s->area] + end;
23226 voffset = glyph->voffset;
23227 s->padding_p = glyph->padding_p;
23228 glyph_not_available_p = glyph->glyph_not_available_p;
23229
23230 while (glyph < last
23231 && glyph->type == CHAR_GLYPH
23232 && glyph->voffset == voffset
23233 /* Same face id implies same font, nowadays. */
23234 && glyph->face_id == face_id
23235 && glyph->glyph_not_available_p == glyph_not_available_p)
23236 {
23237 int two_byte_p;
23238
23239 s->face = get_glyph_face_and_encoding (s->f, glyph,
23240 s->char2b + s->nchars,
23241 &two_byte_p);
23242 s->two_byte_p = two_byte_p;
23243 ++s->nchars;
23244 eassert (s->nchars <= end - start);
23245 s->width += glyph->pixel_width;
23246 if (glyph++->padding_p != s->padding_p)
23247 break;
23248 }
23249
23250 s->font = s->face->font;
23251
23252 /* If the specified font could not be loaded, use the frame's font,
23253 but record the fact that we couldn't load it in
23254 S->font_not_found_p so that we can draw rectangles for the
23255 characters of the glyph string. */
23256 if (s->font == NULL || glyph_not_available_p)
23257 {
23258 s->font_not_found_p = 1;
23259 s->font = FRAME_FONT (s->f);
23260 }
23261
23262 /* Adjust base line for subscript/superscript text. */
23263 s->ybase += voffset;
23264
23265 eassert (s->face && s->face->gc);
23266 return glyph - s->row->glyphs[s->area];
23267 }
23268
23269
23270 /* Fill glyph string S from image glyph S->first_glyph. */
23271
23272 static void
23273 fill_image_glyph_string (struct glyph_string *s)
23274 {
23275 eassert (s->first_glyph->type == IMAGE_GLYPH);
23276 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23277 eassert (s->img);
23278 s->slice = s->first_glyph->slice.img;
23279 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23280 s->font = s->face->font;
23281 s->width = s->first_glyph->pixel_width;
23282
23283 /* Adjust base line for subscript/superscript text. */
23284 s->ybase += s->first_glyph->voffset;
23285 }
23286
23287
23288 /* Fill glyph string S from a sequence of stretch glyphs.
23289
23290 START is the index of the first glyph to consider,
23291 END is the index of the last + 1.
23292
23293 Value is the index of the first glyph not in S. */
23294
23295 static int
23296 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23297 {
23298 struct glyph *glyph, *last;
23299 int voffset, face_id;
23300
23301 eassert (s->first_glyph->type == STRETCH_GLYPH);
23302
23303 glyph = s->row->glyphs[s->area] + start;
23304 last = s->row->glyphs[s->area] + end;
23305 face_id = glyph->face_id;
23306 s->face = FACE_FROM_ID (s->f, face_id);
23307 s->font = s->face->font;
23308 s->width = glyph->pixel_width;
23309 s->nchars = 1;
23310 voffset = glyph->voffset;
23311
23312 for (++glyph;
23313 (glyph < last
23314 && glyph->type == STRETCH_GLYPH
23315 && glyph->voffset == voffset
23316 && glyph->face_id == face_id);
23317 ++glyph)
23318 s->width += glyph->pixel_width;
23319
23320 /* Adjust base line for subscript/superscript text. */
23321 s->ybase += voffset;
23322
23323 /* The case that face->gc == 0 is handled when drawing the glyph
23324 string by calling PREPARE_FACE_FOR_DISPLAY. */
23325 eassert (s->face);
23326 return glyph - s->row->glyphs[s->area];
23327 }
23328
23329 static struct font_metrics *
23330 get_per_char_metric (struct font *font, XChar2b *char2b)
23331 {
23332 static struct font_metrics metrics;
23333 unsigned code;
23334
23335 if (! font)
23336 return NULL;
23337 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23338 if (code == FONT_INVALID_CODE)
23339 return NULL;
23340 font->driver->text_extents (font, &code, 1, &metrics);
23341 return &metrics;
23342 }
23343
23344 /* EXPORT for RIF:
23345 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23346 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23347 assumed to be zero. */
23348
23349 void
23350 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23351 {
23352 *left = *right = 0;
23353
23354 if (glyph->type == CHAR_GLYPH)
23355 {
23356 struct face *face;
23357 XChar2b char2b;
23358 struct font_metrics *pcm;
23359
23360 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23361 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23362 {
23363 if (pcm->rbearing > pcm->width)
23364 *right = pcm->rbearing - pcm->width;
23365 if (pcm->lbearing < 0)
23366 *left = -pcm->lbearing;
23367 }
23368 }
23369 else if (glyph->type == COMPOSITE_GLYPH)
23370 {
23371 if (! glyph->u.cmp.automatic)
23372 {
23373 struct composition *cmp = composition_table[glyph->u.cmp.id];
23374
23375 if (cmp->rbearing > cmp->pixel_width)
23376 *right = cmp->rbearing - cmp->pixel_width;
23377 if (cmp->lbearing < 0)
23378 *left = - cmp->lbearing;
23379 }
23380 else
23381 {
23382 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23383 struct font_metrics metrics;
23384
23385 composition_gstring_width (gstring, glyph->slice.cmp.from,
23386 glyph->slice.cmp.to + 1, &metrics);
23387 if (metrics.rbearing > metrics.width)
23388 *right = metrics.rbearing - metrics.width;
23389 if (metrics.lbearing < 0)
23390 *left = - metrics.lbearing;
23391 }
23392 }
23393 }
23394
23395
23396 /* Return the index of the first glyph preceding glyph string S that
23397 is overwritten by S because of S's left overhang. Value is -1
23398 if no glyphs are overwritten. */
23399
23400 static int
23401 left_overwritten (struct glyph_string *s)
23402 {
23403 int k;
23404
23405 if (s->left_overhang)
23406 {
23407 int x = 0, i;
23408 struct glyph *glyphs = s->row->glyphs[s->area];
23409 int first = s->first_glyph - glyphs;
23410
23411 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23412 x -= glyphs[i].pixel_width;
23413
23414 k = i + 1;
23415 }
23416 else
23417 k = -1;
23418
23419 return k;
23420 }
23421
23422
23423 /* Return the index of the first glyph preceding glyph string S that
23424 is overwriting S because of its right overhang. Value is -1 if no
23425 glyph in front of S overwrites S. */
23426
23427 static int
23428 left_overwriting (struct glyph_string *s)
23429 {
23430 int i, k, x;
23431 struct glyph *glyphs = s->row->glyphs[s->area];
23432 int first = s->first_glyph - glyphs;
23433
23434 k = -1;
23435 x = 0;
23436 for (i = first - 1; i >= 0; --i)
23437 {
23438 int left, right;
23439 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23440 if (x + right > 0)
23441 k = i;
23442 x -= glyphs[i].pixel_width;
23443 }
23444
23445 return k;
23446 }
23447
23448
23449 /* Return the index of the last glyph following glyph string S that is
23450 overwritten by S because of S's right overhang. Value is -1 if
23451 no such glyph is found. */
23452
23453 static int
23454 right_overwritten (struct glyph_string *s)
23455 {
23456 int k = -1;
23457
23458 if (s->right_overhang)
23459 {
23460 int x = 0, i;
23461 struct glyph *glyphs = s->row->glyphs[s->area];
23462 int first = (s->first_glyph - glyphs
23463 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23464 int end = s->row->used[s->area];
23465
23466 for (i = first; i < end && s->right_overhang > x; ++i)
23467 x += glyphs[i].pixel_width;
23468
23469 k = i;
23470 }
23471
23472 return k;
23473 }
23474
23475
23476 /* Return the index of the last glyph following glyph string S that
23477 overwrites S because of its left overhang. Value is negative
23478 if no such glyph is found. */
23479
23480 static int
23481 right_overwriting (struct glyph_string *s)
23482 {
23483 int i, k, x;
23484 int end = s->row->used[s->area];
23485 struct glyph *glyphs = s->row->glyphs[s->area];
23486 int first = (s->first_glyph - glyphs
23487 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23488
23489 k = -1;
23490 x = 0;
23491 for (i = first; i < end; ++i)
23492 {
23493 int left, right;
23494 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23495 if (x - left < 0)
23496 k = i;
23497 x += glyphs[i].pixel_width;
23498 }
23499
23500 return k;
23501 }
23502
23503
23504 /* Set background width of glyph string S. START is the index of the
23505 first glyph following S. LAST_X is the right-most x-position + 1
23506 in the drawing area. */
23507
23508 static void
23509 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23510 {
23511 /* If the face of this glyph string has to be drawn to the end of
23512 the drawing area, set S->extends_to_end_of_line_p. */
23513
23514 if (start == s->row->used[s->area]
23515 && s->area == TEXT_AREA
23516 && ((s->row->fill_line_p
23517 && (s->hl == DRAW_NORMAL_TEXT
23518 || s->hl == DRAW_IMAGE_RAISED
23519 || s->hl == DRAW_IMAGE_SUNKEN))
23520 || s->hl == DRAW_MOUSE_FACE))
23521 s->extends_to_end_of_line_p = 1;
23522
23523 /* If S extends its face to the end of the line, set its
23524 background_width to the distance to the right edge of the drawing
23525 area. */
23526 if (s->extends_to_end_of_line_p)
23527 s->background_width = last_x - s->x + 1;
23528 else
23529 s->background_width = s->width;
23530 }
23531
23532
23533 /* Compute overhangs and x-positions for glyph string S and its
23534 predecessors, or successors. X is the starting x-position for S.
23535 BACKWARD_P non-zero means process predecessors. */
23536
23537 static void
23538 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23539 {
23540 if (backward_p)
23541 {
23542 while (s)
23543 {
23544 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23545 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23546 x -= s->width;
23547 s->x = x;
23548 s = s->prev;
23549 }
23550 }
23551 else
23552 {
23553 while (s)
23554 {
23555 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23556 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23557 s->x = x;
23558 x += s->width;
23559 s = s->next;
23560 }
23561 }
23562 }
23563
23564
23565
23566 /* The following macros are only called from draw_glyphs below.
23567 They reference the following parameters of that function directly:
23568 `w', `row', `area', and `overlap_p'
23569 as well as the following local variables:
23570 `s', `f', and `hdc' (in W32) */
23571
23572 #ifdef HAVE_NTGUI
23573 /* On W32, silently add local `hdc' variable to argument list of
23574 init_glyph_string. */
23575 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23576 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23577 #else
23578 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23579 init_glyph_string (s, char2b, w, row, area, start, hl)
23580 #endif
23581
23582 /* Add a glyph string for a stretch glyph to the list of strings
23583 between HEAD and TAIL. START is the index of the stretch glyph in
23584 row area AREA of glyph row ROW. END is the index of the last glyph
23585 in that glyph row area. X is the current output position assigned
23586 to the new glyph string constructed. HL overrides that face of the
23587 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23588 is the right-most x-position of the drawing area. */
23589
23590 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23591 and below -- keep them on one line. */
23592 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23593 do \
23594 { \
23595 s = alloca (sizeof *s); \
23596 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23597 START = fill_stretch_glyph_string (s, START, END); \
23598 append_glyph_string (&HEAD, &TAIL, s); \
23599 s->x = (X); \
23600 } \
23601 while (0)
23602
23603
23604 /* Add a glyph string for an image glyph to the list of strings
23605 between HEAD and TAIL. START is the index of the image glyph in
23606 row area AREA of glyph row ROW. END is the index of the last glyph
23607 in that glyph row area. X is the current output position assigned
23608 to the new glyph string constructed. HL overrides that face of the
23609 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23610 is the right-most x-position of the drawing area. */
23611
23612 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23613 do \
23614 { \
23615 s = alloca (sizeof *s); \
23616 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23617 fill_image_glyph_string (s); \
23618 append_glyph_string (&HEAD, &TAIL, s); \
23619 ++START; \
23620 s->x = (X); \
23621 } \
23622 while (0)
23623
23624
23625 /* Add a glyph string for a sequence of character glyphs to the list
23626 of strings between HEAD and TAIL. START is the index of the first
23627 glyph in row area AREA of glyph row ROW that is part of the new
23628 glyph string. END is the index of the last glyph in that glyph row
23629 area. X is the current output position assigned to the new glyph
23630 string constructed. HL overrides that face of the glyph; e.g. it
23631 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23632 right-most x-position of the drawing area. */
23633
23634 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23635 do \
23636 { \
23637 int face_id; \
23638 XChar2b *char2b; \
23639 \
23640 face_id = (row)->glyphs[area][START].face_id; \
23641 \
23642 s = alloca (sizeof *s); \
23643 char2b = alloca ((END - START) * sizeof *char2b); \
23644 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23645 append_glyph_string (&HEAD, &TAIL, s); \
23646 s->x = (X); \
23647 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23648 } \
23649 while (0)
23650
23651
23652 /* Add a glyph string for a composite sequence to the list of strings
23653 between HEAD and TAIL. START is the index of the first glyph in
23654 row area AREA of glyph row ROW that is part of the new glyph
23655 string. END is the index of the last glyph in that glyph row area.
23656 X is the current output position assigned to the new glyph string
23657 constructed. HL overrides that face of the glyph; e.g. it is
23658 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23659 x-position of the drawing area. */
23660
23661 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23662 do { \
23663 int face_id = (row)->glyphs[area][START].face_id; \
23664 struct face *base_face = FACE_FROM_ID (f, face_id); \
23665 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23666 struct composition *cmp = composition_table[cmp_id]; \
23667 XChar2b *char2b; \
23668 struct glyph_string *first_s = NULL; \
23669 int n; \
23670 \
23671 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23672 \
23673 /* Make glyph_strings for each glyph sequence that is drawable by \
23674 the same face, and append them to HEAD/TAIL. */ \
23675 for (n = 0; n < cmp->glyph_len;) \
23676 { \
23677 s = alloca (sizeof *s); \
23678 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23679 append_glyph_string (&(HEAD), &(TAIL), s); \
23680 s->cmp = cmp; \
23681 s->cmp_from = n; \
23682 s->x = (X); \
23683 if (n == 0) \
23684 first_s = s; \
23685 n = fill_composite_glyph_string (s, base_face, overlaps); \
23686 } \
23687 \
23688 ++START; \
23689 s = first_s; \
23690 } while (0)
23691
23692
23693 /* Add a glyph string for a glyph-string sequence to the list of strings
23694 between HEAD and TAIL. */
23695
23696 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23697 do { \
23698 int face_id; \
23699 XChar2b *char2b; \
23700 Lisp_Object gstring; \
23701 \
23702 face_id = (row)->glyphs[area][START].face_id; \
23703 gstring = (composition_gstring_from_id \
23704 ((row)->glyphs[area][START].u.cmp.id)); \
23705 s = alloca (sizeof *s); \
23706 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23707 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23708 append_glyph_string (&(HEAD), &(TAIL), s); \
23709 s->x = (X); \
23710 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23711 } while (0)
23712
23713
23714 /* Add a glyph string for a sequence of glyphless character's glyphs
23715 to the list of strings between HEAD and TAIL. The meanings of
23716 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23717
23718 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23719 do \
23720 { \
23721 int face_id; \
23722 \
23723 face_id = (row)->glyphs[area][START].face_id; \
23724 \
23725 s = alloca (sizeof *s); \
23726 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23727 append_glyph_string (&HEAD, &TAIL, s); \
23728 s->x = (X); \
23729 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23730 overlaps); \
23731 } \
23732 while (0)
23733
23734
23735 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23736 of AREA of glyph row ROW on window W between indices START and END.
23737 HL overrides the face for drawing glyph strings, e.g. it is
23738 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23739 x-positions of the drawing area.
23740
23741 This is an ugly monster macro construct because we must use alloca
23742 to allocate glyph strings (because draw_glyphs can be called
23743 asynchronously). */
23744
23745 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23746 do \
23747 { \
23748 HEAD = TAIL = NULL; \
23749 while (START < END) \
23750 { \
23751 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23752 switch (first_glyph->type) \
23753 { \
23754 case CHAR_GLYPH: \
23755 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23756 HL, X, LAST_X); \
23757 break; \
23758 \
23759 case COMPOSITE_GLYPH: \
23760 if (first_glyph->u.cmp.automatic) \
23761 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23762 HL, X, LAST_X); \
23763 else \
23764 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23765 HL, X, LAST_X); \
23766 break; \
23767 \
23768 case STRETCH_GLYPH: \
23769 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23770 HL, X, LAST_X); \
23771 break; \
23772 \
23773 case IMAGE_GLYPH: \
23774 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23775 HL, X, LAST_X); \
23776 break; \
23777 \
23778 case GLYPHLESS_GLYPH: \
23779 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23780 HL, X, LAST_X); \
23781 break; \
23782 \
23783 default: \
23784 emacs_abort (); \
23785 } \
23786 \
23787 if (s) \
23788 { \
23789 set_glyph_string_background_width (s, START, LAST_X); \
23790 (X) += s->width; \
23791 } \
23792 } \
23793 } while (0)
23794
23795
23796 /* Draw glyphs between START and END in AREA of ROW on window W,
23797 starting at x-position X. X is relative to AREA in W. HL is a
23798 face-override with the following meaning:
23799
23800 DRAW_NORMAL_TEXT draw normally
23801 DRAW_CURSOR draw in cursor face
23802 DRAW_MOUSE_FACE draw in mouse face.
23803 DRAW_INVERSE_VIDEO draw in mode line face
23804 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23805 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23806
23807 If OVERLAPS is non-zero, draw only the foreground of characters and
23808 clip to the physical height of ROW. Non-zero value also defines
23809 the overlapping part to be drawn:
23810
23811 OVERLAPS_PRED overlap with preceding rows
23812 OVERLAPS_SUCC overlap with succeeding rows
23813 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23814 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23815
23816 Value is the x-position reached, relative to AREA of W. */
23817
23818 static int
23819 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23820 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23821 enum draw_glyphs_face hl, int overlaps)
23822 {
23823 struct glyph_string *head, *tail;
23824 struct glyph_string *s;
23825 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23826 int i, j, x_reached, last_x, area_left = 0;
23827 struct frame *f = XFRAME (WINDOW_FRAME (w));
23828 DECLARE_HDC (hdc);
23829
23830 ALLOCATE_HDC (hdc, f);
23831
23832 /* Let's rather be paranoid than getting a SEGV. */
23833 end = min (end, row->used[area]);
23834 start = clip_to_bounds (0, start, end);
23835
23836 /* Translate X to frame coordinates. Set last_x to the right
23837 end of the drawing area. */
23838 if (row->full_width_p)
23839 {
23840 /* X is relative to the left edge of W, without scroll bars
23841 or fringes. */
23842 area_left = WINDOW_LEFT_EDGE_X (w);
23843 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23844 }
23845 else
23846 {
23847 area_left = window_box_left (w, area);
23848 last_x = area_left + window_box_width (w, area);
23849 }
23850 x += area_left;
23851
23852 /* Build a doubly-linked list of glyph_string structures between
23853 head and tail from what we have to draw. Note that the macro
23854 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23855 the reason we use a separate variable `i'. */
23856 i = start;
23857 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23858 if (tail)
23859 x_reached = tail->x + tail->background_width;
23860 else
23861 x_reached = x;
23862
23863 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23864 the row, redraw some glyphs in front or following the glyph
23865 strings built above. */
23866 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23867 {
23868 struct glyph_string *h, *t;
23869 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23870 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23871 int check_mouse_face = 0;
23872 int dummy_x = 0;
23873
23874 /* If mouse highlighting is on, we may need to draw adjacent
23875 glyphs using mouse-face highlighting. */
23876 if (area == TEXT_AREA && row->mouse_face_p
23877 && hlinfo->mouse_face_beg_row >= 0
23878 && hlinfo->mouse_face_end_row >= 0)
23879 {
23880 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23881
23882 if (row_vpos >= hlinfo->mouse_face_beg_row
23883 && row_vpos <= hlinfo->mouse_face_end_row)
23884 {
23885 check_mouse_face = 1;
23886 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23887 ? hlinfo->mouse_face_beg_col : 0;
23888 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23889 ? hlinfo->mouse_face_end_col
23890 : row->used[TEXT_AREA];
23891 }
23892 }
23893
23894 /* Compute overhangs for all glyph strings. */
23895 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23896 for (s = head; s; s = s->next)
23897 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23898
23899 /* Prepend glyph strings for glyphs in front of the first glyph
23900 string that are overwritten because of the first glyph
23901 string's left overhang. The background of all strings
23902 prepended must be drawn because the first glyph string
23903 draws over it. */
23904 i = left_overwritten (head);
23905 if (i >= 0)
23906 {
23907 enum draw_glyphs_face overlap_hl;
23908
23909 /* If this row contains mouse highlighting, attempt to draw
23910 the overlapped glyphs with the correct highlight. This
23911 code fails if the overlap encompasses more than one glyph
23912 and mouse-highlight spans only some of these glyphs.
23913 However, making it work perfectly involves a lot more
23914 code, and I don't know if the pathological case occurs in
23915 practice, so we'll stick to this for now. --- cyd */
23916 if (check_mouse_face
23917 && mouse_beg_col < start && mouse_end_col > i)
23918 overlap_hl = DRAW_MOUSE_FACE;
23919 else
23920 overlap_hl = DRAW_NORMAL_TEXT;
23921
23922 j = i;
23923 BUILD_GLYPH_STRINGS (j, start, h, t,
23924 overlap_hl, dummy_x, last_x);
23925 start = i;
23926 compute_overhangs_and_x (t, head->x, 1);
23927 prepend_glyph_string_lists (&head, &tail, h, t);
23928 clip_head = head;
23929 }
23930
23931 /* Prepend glyph strings for glyphs in front of the first glyph
23932 string that overwrite that glyph string because of their
23933 right overhang. For these strings, only the foreground must
23934 be drawn, because it draws over the glyph string at `head'.
23935 The background must not be drawn because this would overwrite
23936 right overhangs of preceding glyphs for which no glyph
23937 strings exist. */
23938 i = left_overwriting (head);
23939 if (i >= 0)
23940 {
23941 enum draw_glyphs_face overlap_hl;
23942
23943 if (check_mouse_face
23944 && mouse_beg_col < start && mouse_end_col > i)
23945 overlap_hl = DRAW_MOUSE_FACE;
23946 else
23947 overlap_hl = DRAW_NORMAL_TEXT;
23948
23949 clip_head = head;
23950 BUILD_GLYPH_STRINGS (i, start, h, t,
23951 overlap_hl, dummy_x, last_x);
23952 for (s = h; s; s = s->next)
23953 s->background_filled_p = 1;
23954 compute_overhangs_and_x (t, head->x, 1);
23955 prepend_glyph_string_lists (&head, &tail, h, t);
23956 }
23957
23958 /* Append glyphs strings for glyphs following the last glyph
23959 string tail that are overwritten by tail. The background of
23960 these strings has to be drawn because tail's foreground draws
23961 over it. */
23962 i = right_overwritten (tail);
23963 if (i >= 0)
23964 {
23965 enum draw_glyphs_face overlap_hl;
23966
23967 if (check_mouse_face
23968 && mouse_beg_col < i && mouse_end_col > end)
23969 overlap_hl = DRAW_MOUSE_FACE;
23970 else
23971 overlap_hl = DRAW_NORMAL_TEXT;
23972
23973 BUILD_GLYPH_STRINGS (end, i, h, t,
23974 overlap_hl, x, last_x);
23975 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23976 we don't have `end = i;' here. */
23977 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23978 append_glyph_string_lists (&head, &tail, h, t);
23979 clip_tail = tail;
23980 }
23981
23982 /* Append glyph strings for glyphs following the last glyph
23983 string tail that overwrite tail. The foreground of such
23984 glyphs has to be drawn because it writes into the background
23985 of tail. The background must not be drawn because it could
23986 paint over the foreground of following glyphs. */
23987 i = right_overwriting (tail);
23988 if (i >= 0)
23989 {
23990 enum draw_glyphs_face overlap_hl;
23991 if (check_mouse_face
23992 && mouse_beg_col < i && mouse_end_col > end)
23993 overlap_hl = DRAW_MOUSE_FACE;
23994 else
23995 overlap_hl = DRAW_NORMAL_TEXT;
23996
23997 clip_tail = tail;
23998 i++; /* We must include the Ith glyph. */
23999 BUILD_GLYPH_STRINGS (end, i, h, t,
24000 overlap_hl, x, last_x);
24001 for (s = h; s; s = s->next)
24002 s->background_filled_p = 1;
24003 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24004 append_glyph_string_lists (&head, &tail, h, t);
24005 }
24006 if (clip_head || clip_tail)
24007 for (s = head; s; s = s->next)
24008 {
24009 s->clip_head = clip_head;
24010 s->clip_tail = clip_tail;
24011 }
24012 }
24013
24014 /* Draw all strings. */
24015 for (s = head; s; s = s->next)
24016 FRAME_RIF (f)->draw_glyph_string (s);
24017
24018 #ifndef HAVE_NS
24019 /* When focus a sole frame and move horizontally, this sets on_p to 0
24020 causing a failure to erase prev cursor position. */
24021 if (area == TEXT_AREA
24022 && !row->full_width_p
24023 /* When drawing overlapping rows, only the glyph strings'
24024 foreground is drawn, which doesn't erase a cursor
24025 completely. */
24026 && !overlaps)
24027 {
24028 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24029 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24030 : (tail ? tail->x + tail->background_width : x));
24031 x0 -= area_left;
24032 x1 -= area_left;
24033
24034 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24035 row->y, MATRIX_ROW_BOTTOM_Y (row));
24036 }
24037 #endif
24038
24039 /* Value is the x-position up to which drawn, relative to AREA of W.
24040 This doesn't include parts drawn because of overhangs. */
24041 if (row->full_width_p)
24042 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24043 else
24044 x_reached -= area_left;
24045
24046 RELEASE_HDC (hdc, f);
24047
24048 return x_reached;
24049 }
24050
24051 /* Expand row matrix if too narrow. Don't expand if area
24052 is not present. */
24053
24054 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24055 { \
24056 if (!it->f->fonts_changed \
24057 && (it->glyph_row->glyphs[area] \
24058 < it->glyph_row->glyphs[area + 1])) \
24059 { \
24060 it->w->ncols_scale_factor++; \
24061 it->f->fonts_changed = 1; \
24062 } \
24063 }
24064
24065 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24066 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24067
24068 static void
24069 append_glyph (struct it *it)
24070 {
24071 struct glyph *glyph;
24072 enum glyph_row_area area = it->area;
24073
24074 eassert (it->glyph_row);
24075 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24076
24077 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24078 if (glyph < it->glyph_row->glyphs[area + 1])
24079 {
24080 /* If the glyph row is reversed, we need to prepend the glyph
24081 rather than append it. */
24082 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24083 {
24084 struct glyph *g;
24085
24086 /* Make room for the additional glyph. */
24087 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24088 g[1] = *g;
24089 glyph = it->glyph_row->glyphs[area];
24090 }
24091 glyph->charpos = CHARPOS (it->position);
24092 glyph->object = it->object;
24093 if (it->pixel_width > 0)
24094 {
24095 glyph->pixel_width = it->pixel_width;
24096 glyph->padding_p = 0;
24097 }
24098 else
24099 {
24100 /* Assure at least 1-pixel width. Otherwise, cursor can't
24101 be displayed correctly. */
24102 glyph->pixel_width = 1;
24103 glyph->padding_p = 1;
24104 }
24105 glyph->ascent = it->ascent;
24106 glyph->descent = it->descent;
24107 glyph->voffset = it->voffset;
24108 glyph->type = CHAR_GLYPH;
24109 glyph->avoid_cursor_p = it->avoid_cursor_p;
24110 glyph->multibyte_p = it->multibyte_p;
24111 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24112 {
24113 /* In R2L rows, the left and the right box edges need to be
24114 drawn in reverse direction. */
24115 glyph->right_box_line_p = it->start_of_box_run_p;
24116 glyph->left_box_line_p = it->end_of_box_run_p;
24117 }
24118 else
24119 {
24120 glyph->left_box_line_p = it->start_of_box_run_p;
24121 glyph->right_box_line_p = it->end_of_box_run_p;
24122 }
24123 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24124 || it->phys_descent > it->descent);
24125 glyph->glyph_not_available_p = it->glyph_not_available_p;
24126 glyph->face_id = it->face_id;
24127 glyph->u.ch = it->char_to_display;
24128 glyph->slice.img = null_glyph_slice;
24129 glyph->font_type = FONT_TYPE_UNKNOWN;
24130 if (it->bidi_p)
24131 {
24132 glyph->resolved_level = it->bidi_it.resolved_level;
24133 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24134 emacs_abort ();
24135 glyph->bidi_type = it->bidi_it.type;
24136 }
24137 else
24138 {
24139 glyph->resolved_level = 0;
24140 glyph->bidi_type = UNKNOWN_BT;
24141 }
24142 ++it->glyph_row->used[area];
24143 }
24144 else
24145 IT_EXPAND_MATRIX_WIDTH (it, area);
24146 }
24147
24148 /* Store one glyph for the composition IT->cmp_it.id in
24149 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24150 non-null. */
24151
24152 static void
24153 append_composite_glyph (struct it *it)
24154 {
24155 struct glyph *glyph;
24156 enum glyph_row_area area = it->area;
24157
24158 eassert (it->glyph_row);
24159
24160 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24161 if (glyph < it->glyph_row->glyphs[area + 1])
24162 {
24163 /* If the glyph row is reversed, we need to prepend the glyph
24164 rather than append it. */
24165 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24166 {
24167 struct glyph *g;
24168
24169 /* Make room for the new glyph. */
24170 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24171 g[1] = *g;
24172 glyph = it->glyph_row->glyphs[it->area];
24173 }
24174 glyph->charpos = it->cmp_it.charpos;
24175 glyph->object = it->object;
24176 glyph->pixel_width = it->pixel_width;
24177 glyph->ascent = it->ascent;
24178 glyph->descent = it->descent;
24179 glyph->voffset = it->voffset;
24180 glyph->type = COMPOSITE_GLYPH;
24181 if (it->cmp_it.ch < 0)
24182 {
24183 glyph->u.cmp.automatic = 0;
24184 glyph->u.cmp.id = it->cmp_it.id;
24185 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24186 }
24187 else
24188 {
24189 glyph->u.cmp.automatic = 1;
24190 glyph->u.cmp.id = it->cmp_it.id;
24191 glyph->slice.cmp.from = it->cmp_it.from;
24192 glyph->slice.cmp.to = it->cmp_it.to - 1;
24193 }
24194 glyph->avoid_cursor_p = it->avoid_cursor_p;
24195 glyph->multibyte_p = it->multibyte_p;
24196 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24197 {
24198 /* In R2L rows, the left and the right box edges need to be
24199 drawn in reverse direction. */
24200 glyph->right_box_line_p = it->start_of_box_run_p;
24201 glyph->left_box_line_p = it->end_of_box_run_p;
24202 }
24203 else
24204 {
24205 glyph->left_box_line_p = it->start_of_box_run_p;
24206 glyph->right_box_line_p = it->end_of_box_run_p;
24207 }
24208 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24209 || it->phys_descent > it->descent);
24210 glyph->padding_p = 0;
24211 glyph->glyph_not_available_p = 0;
24212 glyph->face_id = it->face_id;
24213 glyph->font_type = FONT_TYPE_UNKNOWN;
24214 if (it->bidi_p)
24215 {
24216 glyph->resolved_level = it->bidi_it.resolved_level;
24217 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24218 emacs_abort ();
24219 glyph->bidi_type = it->bidi_it.type;
24220 }
24221 ++it->glyph_row->used[area];
24222 }
24223 else
24224 IT_EXPAND_MATRIX_WIDTH (it, area);
24225 }
24226
24227
24228 /* Change IT->ascent and IT->height according to the setting of
24229 IT->voffset. */
24230
24231 static void
24232 take_vertical_position_into_account (struct it *it)
24233 {
24234 if (it->voffset)
24235 {
24236 if (it->voffset < 0)
24237 /* Increase the ascent so that we can display the text higher
24238 in the line. */
24239 it->ascent -= it->voffset;
24240 else
24241 /* Increase the descent so that we can display the text lower
24242 in the line. */
24243 it->descent += it->voffset;
24244 }
24245 }
24246
24247
24248 /* Produce glyphs/get display metrics for the image IT is loaded with.
24249 See the description of struct display_iterator in dispextern.h for
24250 an overview of struct display_iterator. */
24251
24252 static void
24253 produce_image_glyph (struct it *it)
24254 {
24255 struct image *img;
24256 struct face *face;
24257 int glyph_ascent, crop;
24258 struct glyph_slice slice;
24259
24260 eassert (it->what == IT_IMAGE);
24261
24262 face = FACE_FROM_ID (it->f, it->face_id);
24263 eassert (face);
24264 /* Make sure X resources of the face is loaded. */
24265 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24266
24267 if (it->image_id < 0)
24268 {
24269 /* Fringe bitmap. */
24270 it->ascent = it->phys_ascent = 0;
24271 it->descent = it->phys_descent = 0;
24272 it->pixel_width = 0;
24273 it->nglyphs = 0;
24274 return;
24275 }
24276
24277 img = IMAGE_FROM_ID (it->f, it->image_id);
24278 eassert (img);
24279 /* Make sure X resources of the image is loaded. */
24280 prepare_image_for_display (it->f, img);
24281
24282 slice.x = slice.y = 0;
24283 slice.width = img->width;
24284 slice.height = img->height;
24285
24286 if (INTEGERP (it->slice.x))
24287 slice.x = XINT (it->slice.x);
24288 else if (FLOATP (it->slice.x))
24289 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24290
24291 if (INTEGERP (it->slice.y))
24292 slice.y = XINT (it->slice.y);
24293 else if (FLOATP (it->slice.y))
24294 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24295
24296 if (INTEGERP (it->slice.width))
24297 slice.width = XINT (it->slice.width);
24298 else if (FLOATP (it->slice.width))
24299 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24300
24301 if (INTEGERP (it->slice.height))
24302 slice.height = XINT (it->slice.height);
24303 else if (FLOATP (it->slice.height))
24304 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24305
24306 if (slice.x >= img->width)
24307 slice.x = img->width;
24308 if (slice.y >= img->height)
24309 slice.y = img->height;
24310 if (slice.x + slice.width >= img->width)
24311 slice.width = img->width - slice.x;
24312 if (slice.y + slice.height > img->height)
24313 slice.height = img->height - slice.y;
24314
24315 if (slice.width == 0 || slice.height == 0)
24316 return;
24317
24318 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24319
24320 it->descent = slice.height - glyph_ascent;
24321 if (slice.y == 0)
24322 it->descent += img->vmargin;
24323 if (slice.y + slice.height == img->height)
24324 it->descent += img->vmargin;
24325 it->phys_descent = it->descent;
24326
24327 it->pixel_width = slice.width;
24328 if (slice.x == 0)
24329 it->pixel_width += img->hmargin;
24330 if (slice.x + slice.width == img->width)
24331 it->pixel_width += img->hmargin;
24332
24333 /* It's quite possible for images to have an ascent greater than
24334 their height, so don't get confused in that case. */
24335 if (it->descent < 0)
24336 it->descent = 0;
24337
24338 it->nglyphs = 1;
24339
24340 if (face->box != FACE_NO_BOX)
24341 {
24342 if (face->box_line_width > 0)
24343 {
24344 if (slice.y == 0)
24345 it->ascent += face->box_line_width;
24346 if (slice.y + slice.height == img->height)
24347 it->descent += face->box_line_width;
24348 }
24349
24350 if (it->start_of_box_run_p && slice.x == 0)
24351 it->pixel_width += eabs (face->box_line_width);
24352 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24353 it->pixel_width += eabs (face->box_line_width);
24354 }
24355
24356 take_vertical_position_into_account (it);
24357
24358 /* Automatically crop wide image glyphs at right edge so we can
24359 draw the cursor on same display row. */
24360 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24361 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24362 {
24363 it->pixel_width -= crop;
24364 slice.width -= crop;
24365 }
24366
24367 if (it->glyph_row)
24368 {
24369 struct glyph *glyph;
24370 enum glyph_row_area area = it->area;
24371
24372 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24373 if (glyph < it->glyph_row->glyphs[area + 1])
24374 {
24375 glyph->charpos = CHARPOS (it->position);
24376 glyph->object = it->object;
24377 glyph->pixel_width = it->pixel_width;
24378 glyph->ascent = glyph_ascent;
24379 glyph->descent = it->descent;
24380 glyph->voffset = it->voffset;
24381 glyph->type = IMAGE_GLYPH;
24382 glyph->avoid_cursor_p = it->avoid_cursor_p;
24383 glyph->multibyte_p = it->multibyte_p;
24384 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24385 {
24386 /* In R2L rows, the left and the right box edges need to be
24387 drawn in reverse direction. */
24388 glyph->right_box_line_p = it->start_of_box_run_p;
24389 glyph->left_box_line_p = it->end_of_box_run_p;
24390 }
24391 else
24392 {
24393 glyph->left_box_line_p = it->start_of_box_run_p;
24394 glyph->right_box_line_p = it->end_of_box_run_p;
24395 }
24396 glyph->overlaps_vertically_p = 0;
24397 glyph->padding_p = 0;
24398 glyph->glyph_not_available_p = 0;
24399 glyph->face_id = it->face_id;
24400 glyph->u.img_id = img->id;
24401 glyph->slice.img = slice;
24402 glyph->font_type = FONT_TYPE_UNKNOWN;
24403 if (it->bidi_p)
24404 {
24405 glyph->resolved_level = it->bidi_it.resolved_level;
24406 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24407 emacs_abort ();
24408 glyph->bidi_type = it->bidi_it.type;
24409 }
24410 ++it->glyph_row->used[area];
24411 }
24412 else
24413 IT_EXPAND_MATRIX_WIDTH (it, area);
24414 }
24415 }
24416
24417
24418 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24419 of the glyph, WIDTH and HEIGHT are the width and height of the
24420 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24421
24422 static void
24423 append_stretch_glyph (struct it *it, Lisp_Object object,
24424 int width, int height, int ascent)
24425 {
24426 struct glyph *glyph;
24427 enum glyph_row_area area = it->area;
24428
24429 eassert (ascent >= 0 && ascent <= height);
24430
24431 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24432 if (glyph < it->glyph_row->glyphs[area + 1])
24433 {
24434 /* If the glyph row is reversed, we need to prepend the glyph
24435 rather than append it. */
24436 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24437 {
24438 struct glyph *g;
24439
24440 /* Make room for the additional glyph. */
24441 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24442 g[1] = *g;
24443 glyph = it->glyph_row->glyphs[area];
24444 }
24445 glyph->charpos = CHARPOS (it->position);
24446 glyph->object = object;
24447 glyph->pixel_width = width;
24448 glyph->ascent = ascent;
24449 glyph->descent = height - ascent;
24450 glyph->voffset = it->voffset;
24451 glyph->type = STRETCH_GLYPH;
24452 glyph->avoid_cursor_p = it->avoid_cursor_p;
24453 glyph->multibyte_p = it->multibyte_p;
24454 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24455 {
24456 /* In R2L rows, the left and the right box edges need to be
24457 drawn in reverse direction. */
24458 glyph->right_box_line_p = it->start_of_box_run_p;
24459 glyph->left_box_line_p = it->end_of_box_run_p;
24460 }
24461 else
24462 {
24463 glyph->left_box_line_p = it->start_of_box_run_p;
24464 glyph->right_box_line_p = it->end_of_box_run_p;
24465 }
24466 glyph->overlaps_vertically_p = 0;
24467 glyph->padding_p = 0;
24468 glyph->glyph_not_available_p = 0;
24469 glyph->face_id = it->face_id;
24470 glyph->u.stretch.ascent = ascent;
24471 glyph->u.stretch.height = height;
24472 glyph->slice.img = null_glyph_slice;
24473 glyph->font_type = FONT_TYPE_UNKNOWN;
24474 if (it->bidi_p)
24475 {
24476 glyph->resolved_level = it->bidi_it.resolved_level;
24477 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24478 emacs_abort ();
24479 glyph->bidi_type = it->bidi_it.type;
24480 }
24481 else
24482 {
24483 glyph->resolved_level = 0;
24484 glyph->bidi_type = UNKNOWN_BT;
24485 }
24486 ++it->glyph_row->used[area];
24487 }
24488 else
24489 IT_EXPAND_MATRIX_WIDTH (it, area);
24490 }
24491
24492 #endif /* HAVE_WINDOW_SYSTEM */
24493
24494 /* Produce a stretch glyph for iterator IT. IT->object is the value
24495 of the glyph property displayed. The value must be a list
24496 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24497 being recognized:
24498
24499 1. `:width WIDTH' specifies that the space should be WIDTH *
24500 canonical char width wide. WIDTH may be an integer or floating
24501 point number.
24502
24503 2. `:relative-width FACTOR' specifies that the width of the stretch
24504 should be computed from the width of the first character having the
24505 `glyph' property, and should be FACTOR times that width.
24506
24507 3. `:align-to HPOS' specifies that the space should be wide enough
24508 to reach HPOS, a value in canonical character units.
24509
24510 Exactly one of the above pairs must be present.
24511
24512 4. `:height HEIGHT' specifies that the height of the stretch produced
24513 should be HEIGHT, measured in canonical character units.
24514
24515 5. `:relative-height FACTOR' specifies that the height of the
24516 stretch should be FACTOR times the height of the characters having
24517 the glyph property.
24518
24519 Either none or exactly one of 4 or 5 must be present.
24520
24521 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24522 of the stretch should be used for the ascent of the stretch.
24523 ASCENT must be in the range 0 <= ASCENT <= 100. */
24524
24525 void
24526 produce_stretch_glyph (struct it *it)
24527 {
24528 /* (space :width WIDTH :height HEIGHT ...) */
24529 Lisp_Object prop, plist;
24530 int width = 0, height = 0, align_to = -1;
24531 int zero_width_ok_p = 0;
24532 double tem;
24533 struct font *font = NULL;
24534
24535 #ifdef HAVE_WINDOW_SYSTEM
24536 int ascent = 0;
24537 int zero_height_ok_p = 0;
24538
24539 if (FRAME_WINDOW_P (it->f))
24540 {
24541 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24542 font = face->font ? face->font : FRAME_FONT (it->f);
24543 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24544 }
24545 #endif
24546
24547 /* List should start with `space'. */
24548 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24549 plist = XCDR (it->object);
24550
24551 /* Compute the width of the stretch. */
24552 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24553 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24554 {
24555 /* Absolute width `:width WIDTH' specified and valid. */
24556 zero_width_ok_p = 1;
24557 width = (int)tem;
24558 }
24559 #ifdef HAVE_WINDOW_SYSTEM
24560 else if (FRAME_WINDOW_P (it->f)
24561 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24562 {
24563 /* Relative width `:relative-width FACTOR' specified and valid.
24564 Compute the width of the characters having the `glyph'
24565 property. */
24566 struct it it2;
24567 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24568
24569 it2 = *it;
24570 if (it->multibyte_p)
24571 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24572 else
24573 {
24574 it2.c = it2.char_to_display = *p, it2.len = 1;
24575 if (! ASCII_CHAR_P (it2.c))
24576 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24577 }
24578
24579 it2.glyph_row = NULL;
24580 it2.what = IT_CHARACTER;
24581 x_produce_glyphs (&it2);
24582 width = NUMVAL (prop) * it2.pixel_width;
24583 }
24584 #endif /* HAVE_WINDOW_SYSTEM */
24585 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24586 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24587 {
24588 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24589 align_to = (align_to < 0
24590 ? 0
24591 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24592 else if (align_to < 0)
24593 align_to = window_box_left_offset (it->w, TEXT_AREA);
24594 width = max (0, (int)tem + align_to - it->current_x);
24595 zero_width_ok_p = 1;
24596 }
24597 else
24598 /* Nothing specified -> width defaults to canonical char width. */
24599 width = FRAME_COLUMN_WIDTH (it->f);
24600
24601 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24602 width = 1;
24603
24604 #ifdef HAVE_WINDOW_SYSTEM
24605 /* Compute height. */
24606 if (FRAME_WINDOW_P (it->f))
24607 {
24608 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24609 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24610 {
24611 height = (int)tem;
24612 zero_height_ok_p = 1;
24613 }
24614 else if (prop = Fplist_get (plist, QCrelative_height),
24615 NUMVAL (prop) > 0)
24616 height = FONT_HEIGHT (font) * NUMVAL (prop);
24617 else
24618 height = FONT_HEIGHT (font);
24619
24620 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24621 height = 1;
24622
24623 /* Compute percentage of height used for ascent. If
24624 `:ascent ASCENT' is present and valid, use that. Otherwise,
24625 derive the ascent from the font in use. */
24626 if (prop = Fplist_get (plist, QCascent),
24627 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24628 ascent = height * NUMVAL (prop) / 100.0;
24629 else if (!NILP (prop)
24630 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24631 ascent = min (max (0, (int)tem), height);
24632 else
24633 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24634 }
24635 else
24636 #endif /* HAVE_WINDOW_SYSTEM */
24637 height = 1;
24638
24639 if (width > 0 && it->line_wrap != TRUNCATE
24640 && it->current_x + width > it->last_visible_x)
24641 {
24642 width = it->last_visible_x - it->current_x;
24643 #ifdef HAVE_WINDOW_SYSTEM
24644 /* Subtract one more pixel from the stretch width, but only on
24645 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24646 width -= FRAME_WINDOW_P (it->f);
24647 #endif
24648 }
24649
24650 if (width > 0 && height > 0 && it->glyph_row)
24651 {
24652 Lisp_Object o_object = it->object;
24653 Lisp_Object object = it->stack[it->sp - 1].string;
24654 int n = width;
24655
24656 if (!STRINGP (object))
24657 object = it->w->contents;
24658 #ifdef HAVE_WINDOW_SYSTEM
24659 if (FRAME_WINDOW_P (it->f))
24660 append_stretch_glyph (it, object, width, height, ascent);
24661 else
24662 #endif
24663 {
24664 it->object = object;
24665 it->char_to_display = ' ';
24666 it->pixel_width = it->len = 1;
24667 while (n--)
24668 tty_append_glyph (it);
24669 it->object = o_object;
24670 }
24671 }
24672
24673 it->pixel_width = width;
24674 #ifdef HAVE_WINDOW_SYSTEM
24675 if (FRAME_WINDOW_P (it->f))
24676 {
24677 it->ascent = it->phys_ascent = ascent;
24678 it->descent = it->phys_descent = height - it->ascent;
24679 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24680 take_vertical_position_into_account (it);
24681 }
24682 else
24683 #endif
24684 it->nglyphs = width;
24685 }
24686
24687 /* Get information about special display element WHAT in an
24688 environment described by IT. WHAT is one of IT_TRUNCATION or
24689 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24690 non-null glyph_row member. This function ensures that fields like
24691 face_id, c, len of IT are left untouched. */
24692
24693 static void
24694 produce_special_glyphs (struct it *it, enum display_element_type what)
24695 {
24696 struct it temp_it;
24697 Lisp_Object gc;
24698 GLYPH glyph;
24699
24700 temp_it = *it;
24701 temp_it.object = make_number (0);
24702 memset (&temp_it.current, 0, sizeof temp_it.current);
24703
24704 if (what == IT_CONTINUATION)
24705 {
24706 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24707 if (it->bidi_it.paragraph_dir == R2L)
24708 SET_GLYPH_FROM_CHAR (glyph, '/');
24709 else
24710 SET_GLYPH_FROM_CHAR (glyph, '\\');
24711 if (it->dp
24712 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24713 {
24714 /* FIXME: Should we mirror GC for R2L lines? */
24715 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24716 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24717 }
24718 }
24719 else if (what == IT_TRUNCATION)
24720 {
24721 /* Truncation glyph. */
24722 SET_GLYPH_FROM_CHAR (glyph, '$');
24723 if (it->dp
24724 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24725 {
24726 /* FIXME: Should we mirror GC for R2L lines? */
24727 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24728 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24729 }
24730 }
24731 else
24732 emacs_abort ();
24733
24734 #ifdef HAVE_WINDOW_SYSTEM
24735 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24736 is turned off, we precede the truncation/continuation glyphs by a
24737 stretch glyph whose width is computed such that these special
24738 glyphs are aligned at the window margin, even when very different
24739 fonts are used in different glyph rows. */
24740 if (FRAME_WINDOW_P (temp_it.f)
24741 /* init_iterator calls this with it->glyph_row == NULL, and it
24742 wants only the pixel width of the truncation/continuation
24743 glyphs. */
24744 && temp_it.glyph_row
24745 /* insert_left_trunc_glyphs calls us at the beginning of the
24746 row, and it has its own calculation of the stretch glyph
24747 width. */
24748 && temp_it.glyph_row->used[TEXT_AREA] > 0
24749 && (temp_it.glyph_row->reversed_p
24750 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24751 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24752 {
24753 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24754
24755 if (stretch_width > 0)
24756 {
24757 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24758 struct font *font =
24759 face->font ? face->font : FRAME_FONT (temp_it.f);
24760 int stretch_ascent =
24761 (((temp_it.ascent + temp_it.descent)
24762 * FONT_BASE (font)) / FONT_HEIGHT (font));
24763
24764 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24765 temp_it.ascent + temp_it.descent,
24766 stretch_ascent);
24767 }
24768 }
24769 #endif
24770
24771 temp_it.dp = NULL;
24772 temp_it.what = IT_CHARACTER;
24773 temp_it.len = 1;
24774 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24775 temp_it.face_id = GLYPH_FACE (glyph);
24776 temp_it.len = CHAR_BYTES (temp_it.c);
24777
24778 PRODUCE_GLYPHS (&temp_it);
24779 it->pixel_width = temp_it.pixel_width;
24780 it->nglyphs = temp_it.pixel_width;
24781 }
24782
24783 #ifdef HAVE_WINDOW_SYSTEM
24784
24785 /* Calculate line-height and line-spacing properties.
24786 An integer value specifies explicit pixel value.
24787 A float value specifies relative value to current face height.
24788 A cons (float . face-name) specifies relative value to
24789 height of specified face font.
24790
24791 Returns height in pixels, or nil. */
24792
24793
24794 static Lisp_Object
24795 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24796 int boff, int override)
24797 {
24798 Lisp_Object face_name = Qnil;
24799 int ascent, descent, height;
24800
24801 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24802 return val;
24803
24804 if (CONSP (val))
24805 {
24806 face_name = XCAR (val);
24807 val = XCDR (val);
24808 if (!NUMBERP (val))
24809 val = make_number (1);
24810 if (NILP (face_name))
24811 {
24812 height = it->ascent + it->descent;
24813 goto scale;
24814 }
24815 }
24816
24817 if (NILP (face_name))
24818 {
24819 font = FRAME_FONT (it->f);
24820 boff = FRAME_BASELINE_OFFSET (it->f);
24821 }
24822 else if (EQ (face_name, Qt))
24823 {
24824 override = 0;
24825 }
24826 else
24827 {
24828 int face_id;
24829 struct face *face;
24830
24831 face_id = lookup_named_face (it->f, face_name, 0);
24832 if (face_id < 0)
24833 return make_number (-1);
24834
24835 face = FACE_FROM_ID (it->f, face_id);
24836 font = face->font;
24837 if (font == NULL)
24838 return make_number (-1);
24839 boff = font->baseline_offset;
24840 if (font->vertical_centering)
24841 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24842 }
24843
24844 ascent = FONT_BASE (font) + boff;
24845 descent = FONT_DESCENT (font) - boff;
24846
24847 if (override)
24848 {
24849 it->override_ascent = ascent;
24850 it->override_descent = descent;
24851 it->override_boff = boff;
24852 }
24853
24854 height = ascent + descent;
24855
24856 scale:
24857 if (FLOATP (val))
24858 height = (int)(XFLOAT_DATA (val) * height);
24859 else if (INTEGERP (val))
24860 height *= XINT (val);
24861
24862 return make_number (height);
24863 }
24864
24865
24866 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24867 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24868 and only if this is for a character for which no font was found.
24869
24870 If the display method (it->glyphless_method) is
24871 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24872 length of the acronym or the hexadecimal string, UPPER_XOFF and
24873 UPPER_YOFF are pixel offsets for the upper part of the string,
24874 LOWER_XOFF and LOWER_YOFF are for the lower part.
24875
24876 For the other display methods, LEN through LOWER_YOFF are zero. */
24877
24878 static void
24879 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24880 short upper_xoff, short upper_yoff,
24881 short lower_xoff, short lower_yoff)
24882 {
24883 struct glyph *glyph;
24884 enum glyph_row_area area = it->area;
24885
24886 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24887 if (glyph < it->glyph_row->glyphs[area + 1])
24888 {
24889 /* If the glyph row is reversed, we need to prepend the glyph
24890 rather than append it. */
24891 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24892 {
24893 struct glyph *g;
24894
24895 /* Make room for the additional glyph. */
24896 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24897 g[1] = *g;
24898 glyph = it->glyph_row->glyphs[area];
24899 }
24900 glyph->charpos = CHARPOS (it->position);
24901 glyph->object = it->object;
24902 glyph->pixel_width = it->pixel_width;
24903 glyph->ascent = it->ascent;
24904 glyph->descent = it->descent;
24905 glyph->voffset = it->voffset;
24906 glyph->type = GLYPHLESS_GLYPH;
24907 glyph->u.glyphless.method = it->glyphless_method;
24908 glyph->u.glyphless.for_no_font = for_no_font;
24909 glyph->u.glyphless.len = len;
24910 glyph->u.glyphless.ch = it->c;
24911 glyph->slice.glyphless.upper_xoff = upper_xoff;
24912 glyph->slice.glyphless.upper_yoff = upper_yoff;
24913 glyph->slice.glyphless.lower_xoff = lower_xoff;
24914 glyph->slice.glyphless.lower_yoff = lower_yoff;
24915 glyph->avoid_cursor_p = it->avoid_cursor_p;
24916 glyph->multibyte_p = it->multibyte_p;
24917 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24918 {
24919 /* In R2L rows, the left and the right box edges need to be
24920 drawn in reverse direction. */
24921 glyph->right_box_line_p = it->start_of_box_run_p;
24922 glyph->left_box_line_p = it->end_of_box_run_p;
24923 }
24924 else
24925 {
24926 glyph->left_box_line_p = it->start_of_box_run_p;
24927 glyph->right_box_line_p = it->end_of_box_run_p;
24928 }
24929 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24930 || it->phys_descent > it->descent);
24931 glyph->padding_p = 0;
24932 glyph->glyph_not_available_p = 0;
24933 glyph->face_id = face_id;
24934 glyph->font_type = FONT_TYPE_UNKNOWN;
24935 if (it->bidi_p)
24936 {
24937 glyph->resolved_level = it->bidi_it.resolved_level;
24938 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24939 emacs_abort ();
24940 glyph->bidi_type = it->bidi_it.type;
24941 }
24942 ++it->glyph_row->used[area];
24943 }
24944 else
24945 IT_EXPAND_MATRIX_WIDTH (it, area);
24946 }
24947
24948
24949 /* Produce a glyph for a glyphless character for iterator IT.
24950 IT->glyphless_method specifies which method to use for displaying
24951 the character. See the description of enum
24952 glyphless_display_method in dispextern.h for the detail.
24953
24954 FOR_NO_FONT is nonzero if and only if this is for a character for
24955 which no font was found. ACRONYM, if non-nil, is an acronym string
24956 for the character. */
24957
24958 static void
24959 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24960 {
24961 int face_id;
24962 struct face *face;
24963 struct font *font;
24964 int base_width, base_height, width, height;
24965 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24966 int len;
24967
24968 /* Get the metrics of the base font. We always refer to the current
24969 ASCII face. */
24970 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24971 font = face->font ? face->font : FRAME_FONT (it->f);
24972 it->ascent = FONT_BASE (font) + font->baseline_offset;
24973 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24974 base_height = it->ascent + it->descent;
24975 base_width = font->average_width;
24976
24977 face_id = merge_glyphless_glyph_face (it);
24978
24979 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24980 {
24981 it->pixel_width = THIN_SPACE_WIDTH;
24982 len = 0;
24983 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24984 }
24985 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24986 {
24987 width = CHAR_WIDTH (it->c);
24988 if (width == 0)
24989 width = 1;
24990 else if (width > 4)
24991 width = 4;
24992 it->pixel_width = base_width * width;
24993 len = 0;
24994 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24995 }
24996 else
24997 {
24998 char buf[7];
24999 const char *str;
25000 unsigned int code[6];
25001 int upper_len;
25002 int ascent, descent;
25003 struct font_metrics metrics_upper, metrics_lower;
25004
25005 face = FACE_FROM_ID (it->f, face_id);
25006 font = face->font ? face->font : FRAME_FONT (it->f);
25007 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25008
25009 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25010 {
25011 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25012 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25013 if (CONSP (acronym))
25014 acronym = XCAR (acronym);
25015 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25016 }
25017 else
25018 {
25019 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25020 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25021 str = buf;
25022 }
25023 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25024 code[len] = font->driver->encode_char (font, str[len]);
25025 upper_len = (len + 1) / 2;
25026 font->driver->text_extents (font, code, upper_len,
25027 &metrics_upper);
25028 font->driver->text_extents (font, code + upper_len, len - upper_len,
25029 &metrics_lower);
25030
25031
25032
25033 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25034 width = max (metrics_upper.width, metrics_lower.width) + 4;
25035 upper_xoff = upper_yoff = 2; /* the typical case */
25036 if (base_width >= width)
25037 {
25038 /* Align the upper to the left, the lower to the right. */
25039 it->pixel_width = base_width;
25040 lower_xoff = base_width - 2 - metrics_lower.width;
25041 }
25042 else
25043 {
25044 /* Center the shorter one. */
25045 it->pixel_width = width;
25046 if (metrics_upper.width >= metrics_lower.width)
25047 lower_xoff = (width - metrics_lower.width) / 2;
25048 else
25049 {
25050 /* FIXME: This code doesn't look right. It formerly was
25051 missing the "lower_xoff = 0;", which couldn't have
25052 been right since it left lower_xoff uninitialized. */
25053 lower_xoff = 0;
25054 upper_xoff = (width - metrics_upper.width) / 2;
25055 }
25056 }
25057
25058 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25059 top, bottom, and between upper and lower strings. */
25060 height = (metrics_upper.ascent + metrics_upper.descent
25061 + metrics_lower.ascent + metrics_lower.descent) + 5;
25062 /* Center vertically.
25063 H:base_height, D:base_descent
25064 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25065
25066 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25067 descent = D - H/2 + h/2;
25068 lower_yoff = descent - 2 - ld;
25069 upper_yoff = lower_yoff - la - 1 - ud; */
25070 ascent = - (it->descent - (base_height + height + 1) / 2);
25071 descent = it->descent - (base_height - height) / 2;
25072 lower_yoff = descent - 2 - metrics_lower.descent;
25073 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25074 - metrics_upper.descent);
25075 /* Don't make the height shorter than the base height. */
25076 if (height > base_height)
25077 {
25078 it->ascent = ascent;
25079 it->descent = descent;
25080 }
25081 }
25082
25083 it->phys_ascent = it->ascent;
25084 it->phys_descent = it->descent;
25085 if (it->glyph_row)
25086 append_glyphless_glyph (it, face_id, for_no_font, len,
25087 upper_xoff, upper_yoff,
25088 lower_xoff, lower_yoff);
25089 it->nglyphs = 1;
25090 take_vertical_position_into_account (it);
25091 }
25092
25093
25094 /* RIF:
25095 Produce glyphs/get display metrics for the display element IT is
25096 loaded with. See the description of struct it in dispextern.h
25097 for an overview of struct it. */
25098
25099 void
25100 x_produce_glyphs (struct it *it)
25101 {
25102 int extra_line_spacing = it->extra_line_spacing;
25103
25104 it->glyph_not_available_p = 0;
25105
25106 if (it->what == IT_CHARACTER)
25107 {
25108 XChar2b char2b;
25109 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25110 struct font *font = face->font;
25111 struct font_metrics *pcm = NULL;
25112 int boff; /* Baseline offset. */
25113
25114 if (font == NULL)
25115 {
25116 /* When no suitable font is found, display this character by
25117 the method specified in the first extra slot of
25118 Vglyphless_char_display. */
25119 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25120
25121 eassert (it->what == IT_GLYPHLESS);
25122 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25123 goto done;
25124 }
25125
25126 boff = font->baseline_offset;
25127 if (font->vertical_centering)
25128 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25129
25130 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25131 {
25132 int stretched_p;
25133
25134 it->nglyphs = 1;
25135
25136 if (it->override_ascent >= 0)
25137 {
25138 it->ascent = it->override_ascent;
25139 it->descent = it->override_descent;
25140 boff = it->override_boff;
25141 }
25142 else
25143 {
25144 it->ascent = FONT_BASE (font) + boff;
25145 it->descent = FONT_DESCENT (font) - boff;
25146 }
25147
25148 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25149 {
25150 pcm = get_per_char_metric (font, &char2b);
25151 if (pcm->width == 0
25152 && pcm->rbearing == 0 && pcm->lbearing == 0)
25153 pcm = NULL;
25154 }
25155
25156 if (pcm)
25157 {
25158 it->phys_ascent = pcm->ascent + boff;
25159 it->phys_descent = pcm->descent - boff;
25160 it->pixel_width = pcm->width;
25161 }
25162 else
25163 {
25164 it->glyph_not_available_p = 1;
25165 it->phys_ascent = it->ascent;
25166 it->phys_descent = it->descent;
25167 it->pixel_width = font->space_width;
25168 }
25169
25170 if (it->constrain_row_ascent_descent_p)
25171 {
25172 if (it->descent > it->max_descent)
25173 {
25174 it->ascent += it->descent - it->max_descent;
25175 it->descent = it->max_descent;
25176 }
25177 if (it->ascent > it->max_ascent)
25178 {
25179 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25180 it->ascent = it->max_ascent;
25181 }
25182 it->phys_ascent = min (it->phys_ascent, it->ascent);
25183 it->phys_descent = min (it->phys_descent, it->descent);
25184 extra_line_spacing = 0;
25185 }
25186
25187 /* If this is a space inside a region of text with
25188 `space-width' property, change its width. */
25189 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25190 if (stretched_p)
25191 it->pixel_width *= XFLOATINT (it->space_width);
25192
25193 /* If face has a box, add the box thickness to the character
25194 height. If character has a box line to the left and/or
25195 right, add the box line width to the character's width. */
25196 if (face->box != FACE_NO_BOX)
25197 {
25198 int thick = face->box_line_width;
25199
25200 if (thick > 0)
25201 {
25202 it->ascent += thick;
25203 it->descent += thick;
25204 }
25205 else
25206 thick = -thick;
25207
25208 if (it->start_of_box_run_p)
25209 it->pixel_width += thick;
25210 if (it->end_of_box_run_p)
25211 it->pixel_width += thick;
25212 }
25213
25214 /* If face has an overline, add the height of the overline
25215 (1 pixel) and a 1 pixel margin to the character height. */
25216 if (face->overline_p)
25217 it->ascent += overline_margin;
25218
25219 if (it->constrain_row_ascent_descent_p)
25220 {
25221 if (it->ascent > it->max_ascent)
25222 it->ascent = it->max_ascent;
25223 if (it->descent > it->max_descent)
25224 it->descent = it->max_descent;
25225 }
25226
25227 take_vertical_position_into_account (it);
25228
25229 /* If we have to actually produce glyphs, do it. */
25230 if (it->glyph_row)
25231 {
25232 if (stretched_p)
25233 {
25234 /* Translate a space with a `space-width' property
25235 into a stretch glyph. */
25236 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25237 / FONT_HEIGHT (font));
25238 append_stretch_glyph (it, it->object, it->pixel_width,
25239 it->ascent + it->descent, ascent);
25240 }
25241 else
25242 append_glyph (it);
25243
25244 /* If characters with lbearing or rbearing are displayed
25245 in this line, record that fact in a flag of the
25246 glyph row. This is used to optimize X output code. */
25247 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25248 it->glyph_row->contains_overlapping_glyphs_p = 1;
25249 }
25250 if (! stretched_p && it->pixel_width == 0)
25251 /* We assure that all visible glyphs have at least 1-pixel
25252 width. */
25253 it->pixel_width = 1;
25254 }
25255 else if (it->char_to_display == '\n')
25256 {
25257 /* A newline has no width, but we need the height of the
25258 line. But if previous part of the line sets a height,
25259 don't increase that height. */
25260
25261 Lisp_Object height;
25262 Lisp_Object total_height = Qnil;
25263
25264 it->override_ascent = -1;
25265 it->pixel_width = 0;
25266 it->nglyphs = 0;
25267
25268 height = get_it_property (it, Qline_height);
25269 /* Split (line-height total-height) list. */
25270 if (CONSP (height)
25271 && CONSP (XCDR (height))
25272 && NILP (XCDR (XCDR (height))))
25273 {
25274 total_height = XCAR (XCDR (height));
25275 height = XCAR (height);
25276 }
25277 height = calc_line_height_property (it, height, font, boff, 1);
25278
25279 if (it->override_ascent >= 0)
25280 {
25281 it->ascent = it->override_ascent;
25282 it->descent = it->override_descent;
25283 boff = it->override_boff;
25284 }
25285 else
25286 {
25287 it->ascent = FONT_BASE (font) + boff;
25288 it->descent = FONT_DESCENT (font) - boff;
25289 }
25290
25291 if (EQ (height, Qt))
25292 {
25293 if (it->descent > it->max_descent)
25294 {
25295 it->ascent += it->descent - it->max_descent;
25296 it->descent = it->max_descent;
25297 }
25298 if (it->ascent > it->max_ascent)
25299 {
25300 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25301 it->ascent = it->max_ascent;
25302 }
25303 it->phys_ascent = min (it->phys_ascent, it->ascent);
25304 it->phys_descent = min (it->phys_descent, it->descent);
25305 it->constrain_row_ascent_descent_p = 1;
25306 extra_line_spacing = 0;
25307 }
25308 else
25309 {
25310 Lisp_Object spacing;
25311
25312 it->phys_ascent = it->ascent;
25313 it->phys_descent = it->descent;
25314
25315 if ((it->max_ascent > 0 || it->max_descent > 0)
25316 && face->box != FACE_NO_BOX
25317 && face->box_line_width > 0)
25318 {
25319 it->ascent += face->box_line_width;
25320 it->descent += face->box_line_width;
25321 }
25322 if (!NILP (height)
25323 && XINT (height) > it->ascent + it->descent)
25324 it->ascent = XINT (height) - it->descent;
25325
25326 if (!NILP (total_height))
25327 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25328 else
25329 {
25330 spacing = get_it_property (it, Qline_spacing);
25331 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25332 }
25333 if (INTEGERP (spacing))
25334 {
25335 extra_line_spacing = XINT (spacing);
25336 if (!NILP (total_height))
25337 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25338 }
25339 }
25340 }
25341 else /* i.e. (it->char_to_display == '\t') */
25342 {
25343 if (font->space_width > 0)
25344 {
25345 int tab_width = it->tab_width * font->space_width;
25346 int x = it->current_x + it->continuation_lines_width;
25347 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25348
25349 /* If the distance from the current position to the next tab
25350 stop is less than a space character width, use the
25351 tab stop after that. */
25352 if (next_tab_x - x < font->space_width)
25353 next_tab_x += tab_width;
25354
25355 it->pixel_width = next_tab_x - x;
25356 it->nglyphs = 1;
25357 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25358 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25359
25360 if (it->glyph_row)
25361 {
25362 append_stretch_glyph (it, it->object, it->pixel_width,
25363 it->ascent + it->descent, it->ascent);
25364 }
25365 }
25366 else
25367 {
25368 it->pixel_width = 0;
25369 it->nglyphs = 1;
25370 }
25371 }
25372 }
25373 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25374 {
25375 /* A static composition.
25376
25377 Note: A composition is represented as one glyph in the
25378 glyph matrix. There are no padding glyphs.
25379
25380 Important note: pixel_width, ascent, and descent are the
25381 values of what is drawn by draw_glyphs (i.e. the values of
25382 the overall glyphs composed). */
25383 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25384 int boff; /* baseline offset */
25385 struct composition *cmp = composition_table[it->cmp_it.id];
25386 int glyph_len = cmp->glyph_len;
25387 struct font *font = face->font;
25388
25389 it->nglyphs = 1;
25390
25391 /* If we have not yet calculated pixel size data of glyphs of
25392 the composition for the current face font, calculate them
25393 now. Theoretically, we have to check all fonts for the
25394 glyphs, but that requires much time and memory space. So,
25395 here we check only the font of the first glyph. This may
25396 lead to incorrect display, but it's very rare, and C-l
25397 (recenter-top-bottom) can correct the display anyway. */
25398 if (! cmp->font || cmp->font != font)
25399 {
25400 /* Ascent and descent of the font of the first character
25401 of this composition (adjusted by baseline offset).
25402 Ascent and descent of overall glyphs should not be less
25403 than these, respectively. */
25404 int font_ascent, font_descent, font_height;
25405 /* Bounding box of the overall glyphs. */
25406 int leftmost, rightmost, lowest, highest;
25407 int lbearing, rbearing;
25408 int i, width, ascent, descent;
25409 int left_padded = 0, right_padded = 0;
25410 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25411 XChar2b char2b;
25412 struct font_metrics *pcm;
25413 int font_not_found_p;
25414 ptrdiff_t pos;
25415
25416 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25417 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25418 break;
25419 if (glyph_len < cmp->glyph_len)
25420 right_padded = 1;
25421 for (i = 0; i < glyph_len; i++)
25422 {
25423 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25424 break;
25425 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25426 }
25427 if (i > 0)
25428 left_padded = 1;
25429
25430 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25431 : IT_CHARPOS (*it));
25432 /* If no suitable font is found, use the default font. */
25433 font_not_found_p = font == NULL;
25434 if (font_not_found_p)
25435 {
25436 face = face->ascii_face;
25437 font = face->font;
25438 }
25439 boff = font->baseline_offset;
25440 if (font->vertical_centering)
25441 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25442 font_ascent = FONT_BASE (font) + boff;
25443 font_descent = FONT_DESCENT (font) - boff;
25444 font_height = FONT_HEIGHT (font);
25445
25446 cmp->font = font;
25447
25448 pcm = NULL;
25449 if (! font_not_found_p)
25450 {
25451 get_char_face_and_encoding (it->f, c, it->face_id,
25452 &char2b, 0);
25453 pcm = get_per_char_metric (font, &char2b);
25454 }
25455
25456 /* Initialize the bounding box. */
25457 if (pcm)
25458 {
25459 width = cmp->glyph_len > 0 ? pcm->width : 0;
25460 ascent = pcm->ascent;
25461 descent = pcm->descent;
25462 lbearing = pcm->lbearing;
25463 rbearing = pcm->rbearing;
25464 }
25465 else
25466 {
25467 width = cmp->glyph_len > 0 ? font->space_width : 0;
25468 ascent = FONT_BASE (font);
25469 descent = FONT_DESCENT (font);
25470 lbearing = 0;
25471 rbearing = width;
25472 }
25473
25474 rightmost = width;
25475 leftmost = 0;
25476 lowest = - descent + boff;
25477 highest = ascent + boff;
25478
25479 if (! font_not_found_p
25480 && font->default_ascent
25481 && CHAR_TABLE_P (Vuse_default_ascent)
25482 && !NILP (Faref (Vuse_default_ascent,
25483 make_number (it->char_to_display))))
25484 highest = font->default_ascent + boff;
25485
25486 /* Draw the first glyph at the normal position. It may be
25487 shifted to right later if some other glyphs are drawn
25488 at the left. */
25489 cmp->offsets[i * 2] = 0;
25490 cmp->offsets[i * 2 + 1] = boff;
25491 cmp->lbearing = lbearing;
25492 cmp->rbearing = rbearing;
25493
25494 /* Set cmp->offsets for the remaining glyphs. */
25495 for (i++; i < glyph_len; i++)
25496 {
25497 int left, right, btm, top;
25498 int ch = COMPOSITION_GLYPH (cmp, i);
25499 int face_id;
25500 struct face *this_face;
25501
25502 if (ch == '\t')
25503 ch = ' ';
25504 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25505 this_face = FACE_FROM_ID (it->f, face_id);
25506 font = this_face->font;
25507
25508 if (font == NULL)
25509 pcm = NULL;
25510 else
25511 {
25512 get_char_face_and_encoding (it->f, ch, face_id,
25513 &char2b, 0);
25514 pcm = get_per_char_metric (font, &char2b);
25515 }
25516 if (! pcm)
25517 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25518 else
25519 {
25520 width = pcm->width;
25521 ascent = pcm->ascent;
25522 descent = pcm->descent;
25523 lbearing = pcm->lbearing;
25524 rbearing = pcm->rbearing;
25525 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25526 {
25527 /* Relative composition with or without
25528 alternate chars. */
25529 left = (leftmost + rightmost - width) / 2;
25530 btm = - descent + boff;
25531 if (font->relative_compose
25532 && (! CHAR_TABLE_P (Vignore_relative_composition)
25533 || NILP (Faref (Vignore_relative_composition,
25534 make_number (ch)))))
25535 {
25536
25537 if (- descent >= font->relative_compose)
25538 /* One extra pixel between two glyphs. */
25539 btm = highest + 1;
25540 else if (ascent <= 0)
25541 /* One extra pixel between two glyphs. */
25542 btm = lowest - 1 - ascent - descent;
25543 }
25544 }
25545 else
25546 {
25547 /* A composition rule is specified by an integer
25548 value that encodes global and new reference
25549 points (GREF and NREF). GREF and NREF are
25550 specified by numbers as below:
25551
25552 0---1---2 -- ascent
25553 | |
25554 | |
25555 | |
25556 9--10--11 -- center
25557 | |
25558 ---3---4---5--- baseline
25559 | |
25560 6---7---8 -- descent
25561 */
25562 int rule = COMPOSITION_RULE (cmp, i);
25563 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25564
25565 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25566 grefx = gref % 3, nrefx = nref % 3;
25567 grefy = gref / 3, nrefy = nref / 3;
25568 if (xoff)
25569 xoff = font_height * (xoff - 128) / 256;
25570 if (yoff)
25571 yoff = font_height * (yoff - 128) / 256;
25572
25573 left = (leftmost
25574 + grefx * (rightmost - leftmost) / 2
25575 - nrefx * width / 2
25576 + xoff);
25577
25578 btm = ((grefy == 0 ? highest
25579 : grefy == 1 ? 0
25580 : grefy == 2 ? lowest
25581 : (highest + lowest) / 2)
25582 - (nrefy == 0 ? ascent + descent
25583 : nrefy == 1 ? descent - boff
25584 : nrefy == 2 ? 0
25585 : (ascent + descent) / 2)
25586 + yoff);
25587 }
25588
25589 cmp->offsets[i * 2] = left;
25590 cmp->offsets[i * 2 + 1] = btm + descent;
25591
25592 /* Update the bounding box of the overall glyphs. */
25593 if (width > 0)
25594 {
25595 right = left + width;
25596 if (left < leftmost)
25597 leftmost = left;
25598 if (right > rightmost)
25599 rightmost = right;
25600 }
25601 top = btm + descent + ascent;
25602 if (top > highest)
25603 highest = top;
25604 if (btm < lowest)
25605 lowest = btm;
25606
25607 if (cmp->lbearing > left + lbearing)
25608 cmp->lbearing = left + lbearing;
25609 if (cmp->rbearing < left + rbearing)
25610 cmp->rbearing = left + rbearing;
25611 }
25612 }
25613
25614 /* If there are glyphs whose x-offsets are negative,
25615 shift all glyphs to the right and make all x-offsets
25616 non-negative. */
25617 if (leftmost < 0)
25618 {
25619 for (i = 0; i < cmp->glyph_len; i++)
25620 cmp->offsets[i * 2] -= leftmost;
25621 rightmost -= leftmost;
25622 cmp->lbearing -= leftmost;
25623 cmp->rbearing -= leftmost;
25624 }
25625
25626 if (left_padded && cmp->lbearing < 0)
25627 {
25628 for (i = 0; i < cmp->glyph_len; i++)
25629 cmp->offsets[i * 2] -= cmp->lbearing;
25630 rightmost -= cmp->lbearing;
25631 cmp->rbearing -= cmp->lbearing;
25632 cmp->lbearing = 0;
25633 }
25634 if (right_padded && rightmost < cmp->rbearing)
25635 {
25636 rightmost = cmp->rbearing;
25637 }
25638
25639 cmp->pixel_width = rightmost;
25640 cmp->ascent = highest;
25641 cmp->descent = - lowest;
25642 if (cmp->ascent < font_ascent)
25643 cmp->ascent = font_ascent;
25644 if (cmp->descent < font_descent)
25645 cmp->descent = font_descent;
25646 }
25647
25648 if (it->glyph_row
25649 && (cmp->lbearing < 0
25650 || cmp->rbearing > cmp->pixel_width))
25651 it->glyph_row->contains_overlapping_glyphs_p = 1;
25652
25653 it->pixel_width = cmp->pixel_width;
25654 it->ascent = it->phys_ascent = cmp->ascent;
25655 it->descent = it->phys_descent = cmp->descent;
25656 if (face->box != FACE_NO_BOX)
25657 {
25658 int thick = face->box_line_width;
25659
25660 if (thick > 0)
25661 {
25662 it->ascent += thick;
25663 it->descent += thick;
25664 }
25665 else
25666 thick = - thick;
25667
25668 if (it->start_of_box_run_p)
25669 it->pixel_width += thick;
25670 if (it->end_of_box_run_p)
25671 it->pixel_width += thick;
25672 }
25673
25674 /* If face has an overline, add the height of the overline
25675 (1 pixel) and a 1 pixel margin to the character height. */
25676 if (face->overline_p)
25677 it->ascent += overline_margin;
25678
25679 take_vertical_position_into_account (it);
25680 if (it->ascent < 0)
25681 it->ascent = 0;
25682 if (it->descent < 0)
25683 it->descent = 0;
25684
25685 if (it->glyph_row && cmp->glyph_len > 0)
25686 append_composite_glyph (it);
25687 }
25688 else if (it->what == IT_COMPOSITION)
25689 {
25690 /* A dynamic (automatic) composition. */
25691 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25692 Lisp_Object gstring;
25693 struct font_metrics metrics;
25694
25695 it->nglyphs = 1;
25696
25697 gstring = composition_gstring_from_id (it->cmp_it.id);
25698 it->pixel_width
25699 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25700 &metrics);
25701 if (it->glyph_row
25702 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25703 it->glyph_row->contains_overlapping_glyphs_p = 1;
25704 it->ascent = it->phys_ascent = metrics.ascent;
25705 it->descent = it->phys_descent = metrics.descent;
25706 if (face->box != FACE_NO_BOX)
25707 {
25708 int thick = face->box_line_width;
25709
25710 if (thick > 0)
25711 {
25712 it->ascent += thick;
25713 it->descent += thick;
25714 }
25715 else
25716 thick = - thick;
25717
25718 if (it->start_of_box_run_p)
25719 it->pixel_width += thick;
25720 if (it->end_of_box_run_p)
25721 it->pixel_width += thick;
25722 }
25723 /* If face has an overline, add the height of the overline
25724 (1 pixel) and a 1 pixel margin to the character height. */
25725 if (face->overline_p)
25726 it->ascent += overline_margin;
25727 take_vertical_position_into_account (it);
25728 if (it->ascent < 0)
25729 it->ascent = 0;
25730 if (it->descent < 0)
25731 it->descent = 0;
25732
25733 if (it->glyph_row)
25734 append_composite_glyph (it);
25735 }
25736 else if (it->what == IT_GLYPHLESS)
25737 produce_glyphless_glyph (it, 0, Qnil);
25738 else if (it->what == IT_IMAGE)
25739 produce_image_glyph (it);
25740 else if (it->what == IT_STRETCH)
25741 produce_stretch_glyph (it);
25742
25743 done:
25744 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25745 because this isn't true for images with `:ascent 100'. */
25746 eassert (it->ascent >= 0 && it->descent >= 0);
25747 if (it->area == TEXT_AREA)
25748 it->current_x += it->pixel_width;
25749
25750 if (extra_line_spacing > 0)
25751 {
25752 it->descent += extra_line_spacing;
25753 if (extra_line_spacing > it->max_extra_line_spacing)
25754 it->max_extra_line_spacing = extra_line_spacing;
25755 }
25756
25757 it->max_ascent = max (it->max_ascent, it->ascent);
25758 it->max_descent = max (it->max_descent, it->descent);
25759 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25760 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25761 }
25762
25763 /* EXPORT for RIF:
25764 Output LEN glyphs starting at START at the nominal cursor position.
25765 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25766 being updated, and UPDATED_AREA is the area of that row being updated. */
25767
25768 void
25769 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25770 struct glyph *start, enum glyph_row_area updated_area, int len)
25771 {
25772 int x, hpos, chpos = w->phys_cursor.hpos;
25773
25774 eassert (updated_row);
25775 /* When the window is hscrolled, cursor hpos can legitimately be out
25776 of bounds, but we draw the cursor at the corresponding window
25777 margin in that case. */
25778 if (!updated_row->reversed_p && chpos < 0)
25779 chpos = 0;
25780 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25781 chpos = updated_row->used[TEXT_AREA] - 1;
25782
25783 block_input ();
25784
25785 /* Write glyphs. */
25786
25787 hpos = start - updated_row->glyphs[updated_area];
25788 x = draw_glyphs (w, w->output_cursor.x,
25789 updated_row, updated_area,
25790 hpos, hpos + len,
25791 DRAW_NORMAL_TEXT, 0);
25792
25793 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25794 if (updated_area == TEXT_AREA
25795 && w->phys_cursor_on_p
25796 && w->phys_cursor.vpos == w->output_cursor.vpos
25797 && chpos >= hpos
25798 && chpos < hpos + len)
25799 w->phys_cursor_on_p = 0;
25800
25801 unblock_input ();
25802
25803 /* Advance the output cursor. */
25804 w->output_cursor.hpos += len;
25805 w->output_cursor.x = x;
25806 }
25807
25808
25809 /* EXPORT for RIF:
25810 Insert LEN glyphs from START at the nominal cursor position. */
25811
25812 void
25813 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25814 struct glyph *start, enum glyph_row_area updated_area, int len)
25815 {
25816 struct frame *f;
25817 int line_height, shift_by_width, shifted_region_width;
25818 struct glyph_row *row;
25819 struct glyph *glyph;
25820 int frame_x, frame_y;
25821 ptrdiff_t hpos;
25822
25823 eassert (updated_row);
25824 block_input ();
25825 f = XFRAME (WINDOW_FRAME (w));
25826
25827 /* Get the height of the line we are in. */
25828 row = updated_row;
25829 line_height = row->height;
25830
25831 /* Get the width of the glyphs to insert. */
25832 shift_by_width = 0;
25833 for (glyph = start; glyph < start + len; ++glyph)
25834 shift_by_width += glyph->pixel_width;
25835
25836 /* Get the width of the region to shift right. */
25837 shifted_region_width = (window_box_width (w, updated_area)
25838 - w->output_cursor.x
25839 - shift_by_width);
25840
25841 /* Shift right. */
25842 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25843 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25844
25845 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25846 line_height, shift_by_width);
25847
25848 /* Write the glyphs. */
25849 hpos = start - row->glyphs[updated_area];
25850 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25851 hpos, hpos + len,
25852 DRAW_NORMAL_TEXT, 0);
25853
25854 /* Advance the output cursor. */
25855 w->output_cursor.hpos += len;
25856 w->output_cursor.x += shift_by_width;
25857 unblock_input ();
25858 }
25859
25860
25861 /* EXPORT for RIF:
25862 Erase the current text line from the nominal cursor position
25863 (inclusive) to pixel column TO_X (exclusive). The idea is that
25864 everything from TO_X onward is already erased.
25865
25866 TO_X is a pixel position relative to UPDATED_AREA of currently
25867 updated window W. TO_X == -1 means clear to the end of this area. */
25868
25869 void
25870 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25871 enum glyph_row_area updated_area, int to_x)
25872 {
25873 struct frame *f;
25874 int max_x, min_y, max_y;
25875 int from_x, from_y, to_y;
25876
25877 eassert (updated_row);
25878 f = XFRAME (w->frame);
25879
25880 if (updated_row->full_width_p)
25881 max_x = WINDOW_TOTAL_WIDTH (w);
25882 else
25883 max_x = window_box_width (w, updated_area);
25884 max_y = window_text_bottom_y (w);
25885
25886 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25887 of window. For TO_X > 0, truncate to end of drawing area. */
25888 if (to_x == 0)
25889 return;
25890 else if (to_x < 0)
25891 to_x = max_x;
25892 else
25893 to_x = min (to_x, max_x);
25894
25895 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25896
25897 /* Notice if the cursor will be cleared by this operation. */
25898 if (!updated_row->full_width_p)
25899 notice_overwritten_cursor (w, updated_area,
25900 w->output_cursor.x, -1,
25901 updated_row->y,
25902 MATRIX_ROW_BOTTOM_Y (updated_row));
25903
25904 from_x = w->output_cursor.x;
25905
25906 /* Translate to frame coordinates. */
25907 if (updated_row->full_width_p)
25908 {
25909 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25910 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25911 }
25912 else
25913 {
25914 int area_left = window_box_left (w, updated_area);
25915 from_x += area_left;
25916 to_x += area_left;
25917 }
25918
25919 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25920 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
25921 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25922
25923 /* Prevent inadvertently clearing to end of the X window. */
25924 if (to_x > from_x && to_y > from_y)
25925 {
25926 block_input ();
25927 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25928 to_x - from_x, to_y - from_y);
25929 unblock_input ();
25930 }
25931 }
25932
25933 #endif /* HAVE_WINDOW_SYSTEM */
25934
25935
25936 \f
25937 /***********************************************************************
25938 Cursor types
25939 ***********************************************************************/
25940
25941 /* Value is the internal representation of the specified cursor type
25942 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25943 of the bar cursor. */
25944
25945 static enum text_cursor_kinds
25946 get_specified_cursor_type (Lisp_Object arg, int *width)
25947 {
25948 enum text_cursor_kinds type;
25949
25950 if (NILP (arg))
25951 return NO_CURSOR;
25952
25953 if (EQ (arg, Qbox))
25954 return FILLED_BOX_CURSOR;
25955
25956 if (EQ (arg, Qhollow))
25957 return HOLLOW_BOX_CURSOR;
25958
25959 if (EQ (arg, Qbar))
25960 {
25961 *width = 2;
25962 return BAR_CURSOR;
25963 }
25964
25965 if (CONSP (arg)
25966 && EQ (XCAR (arg), Qbar)
25967 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25968 {
25969 *width = XINT (XCDR (arg));
25970 return BAR_CURSOR;
25971 }
25972
25973 if (EQ (arg, Qhbar))
25974 {
25975 *width = 2;
25976 return HBAR_CURSOR;
25977 }
25978
25979 if (CONSP (arg)
25980 && EQ (XCAR (arg), Qhbar)
25981 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25982 {
25983 *width = XINT (XCDR (arg));
25984 return HBAR_CURSOR;
25985 }
25986
25987 /* Treat anything unknown as "hollow box cursor".
25988 It was bad to signal an error; people have trouble fixing
25989 .Xdefaults with Emacs, when it has something bad in it. */
25990 type = HOLLOW_BOX_CURSOR;
25991
25992 return type;
25993 }
25994
25995 /* Set the default cursor types for specified frame. */
25996 void
25997 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25998 {
25999 int width = 1;
26000 Lisp_Object tem;
26001
26002 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26003 FRAME_CURSOR_WIDTH (f) = width;
26004
26005 /* By default, set up the blink-off state depending on the on-state. */
26006
26007 tem = Fassoc (arg, Vblink_cursor_alist);
26008 if (!NILP (tem))
26009 {
26010 FRAME_BLINK_OFF_CURSOR (f)
26011 = get_specified_cursor_type (XCDR (tem), &width);
26012 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26013 }
26014 else
26015 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26016
26017 /* Make sure the cursor gets redrawn. */
26018 f->cursor_type_changed = 1;
26019 }
26020
26021
26022 #ifdef HAVE_WINDOW_SYSTEM
26023
26024 /* Return the cursor we want to be displayed in window W. Return
26025 width of bar/hbar cursor through WIDTH arg. Return with
26026 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26027 (i.e. if the `system caret' should track this cursor).
26028
26029 In a mini-buffer window, we want the cursor only to appear if we
26030 are reading input from this window. For the selected window, we
26031 want the cursor type given by the frame parameter or buffer local
26032 setting of cursor-type. If explicitly marked off, draw no cursor.
26033 In all other cases, we want a hollow box cursor. */
26034
26035 static enum text_cursor_kinds
26036 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26037 int *active_cursor)
26038 {
26039 struct frame *f = XFRAME (w->frame);
26040 struct buffer *b = XBUFFER (w->contents);
26041 int cursor_type = DEFAULT_CURSOR;
26042 Lisp_Object alt_cursor;
26043 int non_selected = 0;
26044
26045 *active_cursor = 1;
26046
26047 /* Echo area */
26048 if (cursor_in_echo_area
26049 && FRAME_HAS_MINIBUF_P (f)
26050 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26051 {
26052 if (w == XWINDOW (echo_area_window))
26053 {
26054 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26055 {
26056 *width = FRAME_CURSOR_WIDTH (f);
26057 return FRAME_DESIRED_CURSOR (f);
26058 }
26059 else
26060 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26061 }
26062
26063 *active_cursor = 0;
26064 non_selected = 1;
26065 }
26066
26067 /* Detect a nonselected window or nonselected frame. */
26068 else if (w != XWINDOW (f->selected_window)
26069 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26070 {
26071 *active_cursor = 0;
26072
26073 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26074 return NO_CURSOR;
26075
26076 non_selected = 1;
26077 }
26078
26079 /* Never display a cursor in a window in which cursor-type is nil. */
26080 if (NILP (BVAR (b, cursor_type)))
26081 return NO_CURSOR;
26082
26083 /* Get the normal cursor type for this window. */
26084 if (EQ (BVAR (b, cursor_type), Qt))
26085 {
26086 cursor_type = FRAME_DESIRED_CURSOR (f);
26087 *width = FRAME_CURSOR_WIDTH (f);
26088 }
26089 else
26090 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26091
26092 /* Use cursor-in-non-selected-windows instead
26093 for non-selected window or frame. */
26094 if (non_selected)
26095 {
26096 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26097 if (!EQ (Qt, alt_cursor))
26098 return get_specified_cursor_type (alt_cursor, width);
26099 /* t means modify the normal cursor type. */
26100 if (cursor_type == FILLED_BOX_CURSOR)
26101 cursor_type = HOLLOW_BOX_CURSOR;
26102 else if (cursor_type == BAR_CURSOR && *width > 1)
26103 --*width;
26104 return cursor_type;
26105 }
26106
26107 /* Use normal cursor if not blinked off. */
26108 if (!w->cursor_off_p)
26109 {
26110 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26111 {
26112 if (cursor_type == FILLED_BOX_CURSOR)
26113 {
26114 /* Using a block cursor on large images can be very annoying.
26115 So use a hollow cursor for "large" images.
26116 If image is not transparent (no mask), also use hollow cursor. */
26117 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26118 if (img != NULL && IMAGEP (img->spec))
26119 {
26120 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26121 where N = size of default frame font size.
26122 This should cover most of the "tiny" icons people may use. */
26123 if (!img->mask
26124 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26125 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26126 cursor_type = HOLLOW_BOX_CURSOR;
26127 }
26128 }
26129 else if (cursor_type != NO_CURSOR)
26130 {
26131 /* Display current only supports BOX and HOLLOW cursors for images.
26132 So for now, unconditionally use a HOLLOW cursor when cursor is
26133 not a solid box cursor. */
26134 cursor_type = HOLLOW_BOX_CURSOR;
26135 }
26136 }
26137 return cursor_type;
26138 }
26139
26140 /* Cursor is blinked off, so determine how to "toggle" it. */
26141
26142 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26143 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26144 return get_specified_cursor_type (XCDR (alt_cursor), width);
26145
26146 /* Then see if frame has specified a specific blink off cursor type. */
26147 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26148 {
26149 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26150 return FRAME_BLINK_OFF_CURSOR (f);
26151 }
26152
26153 #if 0
26154 /* Some people liked having a permanently visible blinking cursor,
26155 while others had very strong opinions against it. So it was
26156 decided to remove it. KFS 2003-09-03 */
26157
26158 /* Finally perform built-in cursor blinking:
26159 filled box <-> hollow box
26160 wide [h]bar <-> narrow [h]bar
26161 narrow [h]bar <-> no cursor
26162 other type <-> no cursor */
26163
26164 if (cursor_type == FILLED_BOX_CURSOR)
26165 return HOLLOW_BOX_CURSOR;
26166
26167 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26168 {
26169 *width = 1;
26170 return cursor_type;
26171 }
26172 #endif
26173
26174 return NO_CURSOR;
26175 }
26176
26177
26178 /* Notice when the text cursor of window W has been completely
26179 overwritten by a drawing operation that outputs glyphs in AREA
26180 starting at X0 and ending at X1 in the line starting at Y0 and
26181 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26182 the rest of the line after X0 has been written. Y coordinates
26183 are window-relative. */
26184
26185 static void
26186 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26187 int x0, int x1, int y0, int y1)
26188 {
26189 int cx0, cx1, cy0, cy1;
26190 struct glyph_row *row;
26191
26192 if (!w->phys_cursor_on_p)
26193 return;
26194 if (area != TEXT_AREA)
26195 return;
26196
26197 if (w->phys_cursor.vpos < 0
26198 || w->phys_cursor.vpos >= w->current_matrix->nrows
26199 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26200 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26201 return;
26202
26203 if (row->cursor_in_fringe_p)
26204 {
26205 row->cursor_in_fringe_p = 0;
26206 draw_fringe_bitmap (w, row, row->reversed_p);
26207 w->phys_cursor_on_p = 0;
26208 return;
26209 }
26210
26211 cx0 = w->phys_cursor.x;
26212 cx1 = cx0 + w->phys_cursor_width;
26213 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26214 return;
26215
26216 /* The cursor image will be completely removed from the
26217 screen if the output area intersects the cursor area in
26218 y-direction. When we draw in [y0 y1[, and some part of
26219 the cursor is at y < y0, that part must have been drawn
26220 before. When scrolling, the cursor is erased before
26221 actually scrolling, so we don't come here. When not
26222 scrolling, the rows above the old cursor row must have
26223 changed, and in this case these rows must have written
26224 over the cursor image.
26225
26226 Likewise if part of the cursor is below y1, with the
26227 exception of the cursor being in the first blank row at
26228 the buffer and window end because update_text_area
26229 doesn't draw that row. (Except when it does, but
26230 that's handled in update_text_area.) */
26231
26232 cy0 = w->phys_cursor.y;
26233 cy1 = cy0 + w->phys_cursor_height;
26234 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26235 return;
26236
26237 w->phys_cursor_on_p = 0;
26238 }
26239
26240 #endif /* HAVE_WINDOW_SYSTEM */
26241
26242 \f
26243 /************************************************************************
26244 Mouse Face
26245 ************************************************************************/
26246
26247 #ifdef HAVE_WINDOW_SYSTEM
26248
26249 /* EXPORT for RIF:
26250 Fix the display of area AREA of overlapping row ROW in window W
26251 with respect to the overlapping part OVERLAPS. */
26252
26253 void
26254 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26255 enum glyph_row_area area, int overlaps)
26256 {
26257 int i, x;
26258
26259 block_input ();
26260
26261 x = 0;
26262 for (i = 0; i < row->used[area];)
26263 {
26264 if (row->glyphs[area][i].overlaps_vertically_p)
26265 {
26266 int start = i, start_x = x;
26267
26268 do
26269 {
26270 x += row->glyphs[area][i].pixel_width;
26271 ++i;
26272 }
26273 while (i < row->used[area]
26274 && row->glyphs[area][i].overlaps_vertically_p);
26275
26276 draw_glyphs (w, start_x, row, area,
26277 start, i,
26278 DRAW_NORMAL_TEXT, overlaps);
26279 }
26280 else
26281 {
26282 x += row->glyphs[area][i].pixel_width;
26283 ++i;
26284 }
26285 }
26286
26287 unblock_input ();
26288 }
26289
26290
26291 /* EXPORT:
26292 Draw the cursor glyph of window W in glyph row ROW. See the
26293 comment of draw_glyphs for the meaning of HL. */
26294
26295 void
26296 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26297 enum draw_glyphs_face hl)
26298 {
26299 /* If cursor hpos is out of bounds, don't draw garbage. This can
26300 happen in mini-buffer windows when switching between echo area
26301 glyphs and mini-buffer. */
26302 if ((row->reversed_p
26303 ? (w->phys_cursor.hpos >= 0)
26304 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26305 {
26306 int on_p = w->phys_cursor_on_p;
26307 int x1;
26308 int hpos = w->phys_cursor.hpos;
26309
26310 /* When the window is hscrolled, cursor hpos can legitimately be
26311 out of bounds, but we draw the cursor at the corresponding
26312 window margin in that case. */
26313 if (!row->reversed_p && hpos < 0)
26314 hpos = 0;
26315 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26316 hpos = row->used[TEXT_AREA] - 1;
26317
26318 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26319 hl, 0);
26320 w->phys_cursor_on_p = on_p;
26321
26322 if (hl == DRAW_CURSOR)
26323 w->phys_cursor_width = x1 - w->phys_cursor.x;
26324 /* When we erase the cursor, and ROW is overlapped by other
26325 rows, make sure that these overlapping parts of other rows
26326 are redrawn. */
26327 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26328 {
26329 w->phys_cursor_width = x1 - w->phys_cursor.x;
26330
26331 if (row > w->current_matrix->rows
26332 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26333 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26334 OVERLAPS_ERASED_CURSOR);
26335
26336 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26337 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26338 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26339 OVERLAPS_ERASED_CURSOR);
26340 }
26341 }
26342 }
26343
26344
26345 /* Erase the image of a cursor of window W from the screen. */
26346
26347 #ifndef WINDOWSNT
26348 static
26349 #endif
26350 void
26351 erase_phys_cursor (struct window *w)
26352 {
26353 struct frame *f = XFRAME (w->frame);
26354 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26355 int hpos = w->phys_cursor.hpos;
26356 int vpos = w->phys_cursor.vpos;
26357 int mouse_face_here_p = 0;
26358 struct glyph_matrix *active_glyphs = w->current_matrix;
26359 struct glyph_row *cursor_row;
26360 struct glyph *cursor_glyph;
26361 enum draw_glyphs_face hl;
26362
26363 /* No cursor displayed or row invalidated => nothing to do on the
26364 screen. */
26365 if (w->phys_cursor_type == NO_CURSOR)
26366 goto mark_cursor_off;
26367
26368 /* VPOS >= active_glyphs->nrows means that window has been resized.
26369 Don't bother to erase the cursor. */
26370 if (vpos >= active_glyphs->nrows)
26371 goto mark_cursor_off;
26372
26373 /* If row containing cursor is marked invalid, there is nothing we
26374 can do. */
26375 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26376 if (!cursor_row->enabled_p)
26377 goto mark_cursor_off;
26378
26379 /* If line spacing is > 0, old cursor may only be partially visible in
26380 window after split-window. So adjust visible height. */
26381 cursor_row->visible_height = min (cursor_row->visible_height,
26382 window_text_bottom_y (w) - cursor_row->y);
26383
26384 /* If row is completely invisible, don't attempt to delete a cursor which
26385 isn't there. This can happen if cursor is at top of a window, and
26386 we switch to a buffer with a header line in that window. */
26387 if (cursor_row->visible_height <= 0)
26388 goto mark_cursor_off;
26389
26390 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26391 if (cursor_row->cursor_in_fringe_p)
26392 {
26393 cursor_row->cursor_in_fringe_p = 0;
26394 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26395 goto mark_cursor_off;
26396 }
26397
26398 /* This can happen when the new row is shorter than the old one.
26399 In this case, either draw_glyphs or clear_end_of_line
26400 should have cleared the cursor. Note that we wouldn't be
26401 able to erase the cursor in this case because we don't have a
26402 cursor glyph at hand. */
26403 if ((cursor_row->reversed_p
26404 ? (w->phys_cursor.hpos < 0)
26405 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26406 goto mark_cursor_off;
26407
26408 /* When the window is hscrolled, cursor hpos can legitimately be out
26409 of bounds, but we draw the cursor at the corresponding window
26410 margin in that case. */
26411 if (!cursor_row->reversed_p && hpos < 0)
26412 hpos = 0;
26413 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26414 hpos = cursor_row->used[TEXT_AREA] - 1;
26415
26416 /* If the cursor is in the mouse face area, redisplay that when
26417 we clear the cursor. */
26418 if (! NILP (hlinfo->mouse_face_window)
26419 && coords_in_mouse_face_p (w, hpos, vpos)
26420 /* Don't redraw the cursor's spot in mouse face if it is at the
26421 end of a line (on a newline). The cursor appears there, but
26422 mouse highlighting does not. */
26423 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26424 mouse_face_here_p = 1;
26425
26426 /* Maybe clear the display under the cursor. */
26427 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26428 {
26429 int x, y, left_x;
26430 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26431 int width;
26432
26433 cursor_glyph = get_phys_cursor_glyph (w);
26434 if (cursor_glyph == NULL)
26435 goto mark_cursor_off;
26436
26437 width = cursor_glyph->pixel_width;
26438 left_x = window_box_left_offset (w, TEXT_AREA);
26439 x = w->phys_cursor.x;
26440 if (x < left_x)
26441 width -= left_x - x;
26442 width = min (width, window_box_width (w, TEXT_AREA) - x);
26443 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26444 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26445
26446 if (width > 0)
26447 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26448 }
26449
26450 /* Erase the cursor by redrawing the character underneath it. */
26451 if (mouse_face_here_p)
26452 hl = DRAW_MOUSE_FACE;
26453 else
26454 hl = DRAW_NORMAL_TEXT;
26455 draw_phys_cursor_glyph (w, cursor_row, hl);
26456
26457 mark_cursor_off:
26458 w->phys_cursor_on_p = 0;
26459 w->phys_cursor_type = NO_CURSOR;
26460 }
26461
26462
26463 /* EXPORT:
26464 Display or clear cursor of window W. If ON is zero, clear the
26465 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26466 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26467
26468 void
26469 display_and_set_cursor (struct window *w, bool on,
26470 int hpos, int vpos, int x, int y)
26471 {
26472 struct frame *f = XFRAME (w->frame);
26473 int new_cursor_type;
26474 int new_cursor_width;
26475 int active_cursor;
26476 struct glyph_row *glyph_row;
26477 struct glyph *glyph;
26478
26479 /* This is pointless on invisible frames, and dangerous on garbaged
26480 windows and frames; in the latter case, the frame or window may
26481 be in the midst of changing its size, and x and y may be off the
26482 window. */
26483 if (! FRAME_VISIBLE_P (f)
26484 || FRAME_GARBAGED_P (f)
26485 || vpos >= w->current_matrix->nrows
26486 || hpos >= w->current_matrix->matrix_w)
26487 return;
26488
26489 /* If cursor is off and we want it off, return quickly. */
26490 if (!on && !w->phys_cursor_on_p)
26491 return;
26492
26493 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26494 /* If cursor row is not enabled, we don't really know where to
26495 display the cursor. */
26496 if (!glyph_row->enabled_p)
26497 {
26498 w->phys_cursor_on_p = 0;
26499 return;
26500 }
26501
26502 glyph = NULL;
26503 if (!glyph_row->exact_window_width_line_p
26504 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26505 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26506
26507 eassert (input_blocked_p ());
26508
26509 /* Set new_cursor_type to the cursor we want to be displayed. */
26510 new_cursor_type = get_window_cursor_type (w, glyph,
26511 &new_cursor_width, &active_cursor);
26512
26513 /* If cursor is currently being shown and we don't want it to be or
26514 it is in the wrong place, or the cursor type is not what we want,
26515 erase it. */
26516 if (w->phys_cursor_on_p
26517 && (!on
26518 || w->phys_cursor.x != x
26519 || w->phys_cursor.y != y
26520 || new_cursor_type != w->phys_cursor_type
26521 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26522 && new_cursor_width != w->phys_cursor_width)))
26523 erase_phys_cursor (w);
26524
26525 /* Don't check phys_cursor_on_p here because that flag is only set
26526 to zero in some cases where we know that the cursor has been
26527 completely erased, to avoid the extra work of erasing the cursor
26528 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26529 still not be visible, or it has only been partly erased. */
26530 if (on)
26531 {
26532 w->phys_cursor_ascent = glyph_row->ascent;
26533 w->phys_cursor_height = glyph_row->height;
26534
26535 /* Set phys_cursor_.* before x_draw_.* is called because some
26536 of them may need the information. */
26537 w->phys_cursor.x = x;
26538 w->phys_cursor.y = glyph_row->y;
26539 w->phys_cursor.hpos = hpos;
26540 w->phys_cursor.vpos = vpos;
26541 }
26542
26543 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26544 new_cursor_type, new_cursor_width,
26545 on, active_cursor);
26546 }
26547
26548
26549 /* Switch the display of W's cursor on or off, according to the value
26550 of ON. */
26551
26552 static void
26553 update_window_cursor (struct window *w, bool on)
26554 {
26555 /* Don't update cursor in windows whose frame is in the process
26556 of being deleted. */
26557 if (w->current_matrix)
26558 {
26559 int hpos = w->phys_cursor.hpos;
26560 int vpos = w->phys_cursor.vpos;
26561 struct glyph_row *row;
26562
26563 if (vpos >= w->current_matrix->nrows
26564 || hpos >= w->current_matrix->matrix_w)
26565 return;
26566
26567 row = MATRIX_ROW (w->current_matrix, vpos);
26568
26569 /* When the window is hscrolled, cursor hpos can legitimately be
26570 out of bounds, but we draw the cursor at the corresponding
26571 window margin in that case. */
26572 if (!row->reversed_p && hpos < 0)
26573 hpos = 0;
26574 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26575 hpos = row->used[TEXT_AREA] - 1;
26576
26577 block_input ();
26578 display_and_set_cursor (w, on, hpos, vpos,
26579 w->phys_cursor.x, w->phys_cursor.y);
26580 unblock_input ();
26581 }
26582 }
26583
26584
26585 /* Call update_window_cursor with parameter ON_P on all leaf windows
26586 in the window tree rooted at W. */
26587
26588 static void
26589 update_cursor_in_window_tree (struct window *w, bool on_p)
26590 {
26591 while (w)
26592 {
26593 if (WINDOWP (w->contents))
26594 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26595 else
26596 update_window_cursor (w, on_p);
26597
26598 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26599 }
26600 }
26601
26602
26603 /* EXPORT:
26604 Display the cursor on window W, or clear it, according to ON_P.
26605 Don't change the cursor's position. */
26606
26607 void
26608 x_update_cursor (struct frame *f, bool on_p)
26609 {
26610 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26611 }
26612
26613
26614 /* EXPORT:
26615 Clear the cursor of window W to background color, and mark the
26616 cursor as not shown. This is used when the text where the cursor
26617 is about to be rewritten. */
26618
26619 void
26620 x_clear_cursor (struct window *w)
26621 {
26622 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26623 update_window_cursor (w, 0);
26624 }
26625
26626 #endif /* HAVE_WINDOW_SYSTEM */
26627
26628 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26629 and MSDOS. */
26630 static void
26631 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26632 int start_hpos, int end_hpos,
26633 enum draw_glyphs_face draw)
26634 {
26635 #ifdef HAVE_WINDOW_SYSTEM
26636 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26637 {
26638 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26639 return;
26640 }
26641 #endif
26642 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26643 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26644 #endif
26645 }
26646
26647 /* Display the active region described by mouse_face_* according to DRAW. */
26648
26649 static void
26650 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26651 {
26652 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26653 struct frame *f = XFRAME (WINDOW_FRAME (w));
26654
26655 if (/* If window is in the process of being destroyed, don't bother
26656 to do anything. */
26657 w->current_matrix != NULL
26658 /* Don't update mouse highlight if hidden */
26659 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26660 /* Recognize when we are called to operate on rows that don't exist
26661 anymore. This can happen when a window is split. */
26662 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26663 {
26664 int phys_cursor_on_p = w->phys_cursor_on_p;
26665 struct glyph_row *row, *first, *last;
26666
26667 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26668 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26669
26670 for (row = first; row <= last && row->enabled_p; ++row)
26671 {
26672 int start_hpos, end_hpos, start_x;
26673
26674 /* For all but the first row, the highlight starts at column 0. */
26675 if (row == first)
26676 {
26677 /* R2L rows have BEG and END in reversed order, but the
26678 screen drawing geometry is always left to right. So
26679 we need to mirror the beginning and end of the
26680 highlighted area in R2L rows. */
26681 if (!row->reversed_p)
26682 {
26683 start_hpos = hlinfo->mouse_face_beg_col;
26684 start_x = hlinfo->mouse_face_beg_x;
26685 }
26686 else if (row == last)
26687 {
26688 start_hpos = hlinfo->mouse_face_end_col;
26689 start_x = hlinfo->mouse_face_end_x;
26690 }
26691 else
26692 {
26693 start_hpos = 0;
26694 start_x = 0;
26695 }
26696 }
26697 else if (row->reversed_p && row == last)
26698 {
26699 start_hpos = hlinfo->mouse_face_end_col;
26700 start_x = hlinfo->mouse_face_end_x;
26701 }
26702 else
26703 {
26704 start_hpos = 0;
26705 start_x = 0;
26706 }
26707
26708 if (row == last)
26709 {
26710 if (!row->reversed_p)
26711 end_hpos = hlinfo->mouse_face_end_col;
26712 else if (row == first)
26713 end_hpos = hlinfo->mouse_face_beg_col;
26714 else
26715 {
26716 end_hpos = row->used[TEXT_AREA];
26717 if (draw == DRAW_NORMAL_TEXT)
26718 row->fill_line_p = 1; /* Clear to end of line */
26719 }
26720 }
26721 else if (row->reversed_p && row == first)
26722 end_hpos = hlinfo->mouse_face_beg_col;
26723 else
26724 {
26725 end_hpos = row->used[TEXT_AREA];
26726 if (draw == DRAW_NORMAL_TEXT)
26727 row->fill_line_p = 1; /* Clear to end of line */
26728 }
26729
26730 if (end_hpos > start_hpos)
26731 {
26732 draw_row_with_mouse_face (w, start_x, row,
26733 start_hpos, end_hpos, draw);
26734
26735 row->mouse_face_p
26736 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26737 }
26738 }
26739
26740 #ifdef HAVE_WINDOW_SYSTEM
26741 /* When we've written over the cursor, arrange for it to
26742 be displayed again. */
26743 if (FRAME_WINDOW_P (f)
26744 && phys_cursor_on_p && !w->phys_cursor_on_p)
26745 {
26746 int hpos = w->phys_cursor.hpos;
26747
26748 /* When the window is hscrolled, cursor hpos can legitimately be
26749 out of bounds, but we draw the cursor at the corresponding
26750 window margin in that case. */
26751 if (!row->reversed_p && hpos < 0)
26752 hpos = 0;
26753 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26754 hpos = row->used[TEXT_AREA] - 1;
26755
26756 block_input ();
26757 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26758 w->phys_cursor.x, w->phys_cursor.y);
26759 unblock_input ();
26760 }
26761 #endif /* HAVE_WINDOW_SYSTEM */
26762 }
26763
26764 #ifdef HAVE_WINDOW_SYSTEM
26765 /* Change the mouse cursor. */
26766 if (FRAME_WINDOW_P (f))
26767 {
26768 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
26769 if (draw == DRAW_NORMAL_TEXT
26770 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26771 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26772 else
26773 #endif
26774 if (draw == DRAW_MOUSE_FACE)
26775 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26776 else
26777 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26778 }
26779 #endif /* HAVE_WINDOW_SYSTEM */
26780 }
26781
26782 /* EXPORT:
26783 Clear out the mouse-highlighted active region.
26784 Redraw it un-highlighted first. Value is non-zero if mouse
26785 face was actually drawn unhighlighted. */
26786
26787 int
26788 clear_mouse_face (Mouse_HLInfo *hlinfo)
26789 {
26790 int cleared = 0;
26791
26792 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26793 {
26794 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26795 cleared = 1;
26796 }
26797
26798 reset_mouse_highlight (hlinfo);
26799 return cleared;
26800 }
26801
26802 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26803 within the mouse face on that window. */
26804 static int
26805 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26806 {
26807 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26808
26809 /* Quickly resolve the easy cases. */
26810 if (!(WINDOWP (hlinfo->mouse_face_window)
26811 && XWINDOW (hlinfo->mouse_face_window) == w))
26812 return 0;
26813 if (vpos < hlinfo->mouse_face_beg_row
26814 || vpos > hlinfo->mouse_face_end_row)
26815 return 0;
26816 if (vpos > hlinfo->mouse_face_beg_row
26817 && vpos < hlinfo->mouse_face_end_row)
26818 return 1;
26819
26820 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26821 {
26822 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26823 {
26824 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26825 return 1;
26826 }
26827 else if ((vpos == hlinfo->mouse_face_beg_row
26828 && hpos >= hlinfo->mouse_face_beg_col)
26829 || (vpos == hlinfo->mouse_face_end_row
26830 && hpos < hlinfo->mouse_face_end_col))
26831 return 1;
26832 }
26833 else
26834 {
26835 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26836 {
26837 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26838 return 1;
26839 }
26840 else if ((vpos == hlinfo->mouse_face_beg_row
26841 && hpos <= hlinfo->mouse_face_beg_col)
26842 || (vpos == hlinfo->mouse_face_end_row
26843 && hpos > hlinfo->mouse_face_end_col))
26844 return 1;
26845 }
26846 return 0;
26847 }
26848
26849
26850 /* EXPORT:
26851 Non-zero if physical cursor of window W is within mouse face. */
26852
26853 int
26854 cursor_in_mouse_face_p (struct window *w)
26855 {
26856 int hpos = w->phys_cursor.hpos;
26857 int vpos = w->phys_cursor.vpos;
26858 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26859
26860 /* When the window is hscrolled, cursor hpos can legitimately be out
26861 of bounds, but we draw the cursor at the corresponding window
26862 margin in that case. */
26863 if (!row->reversed_p && hpos < 0)
26864 hpos = 0;
26865 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26866 hpos = row->used[TEXT_AREA] - 1;
26867
26868 return coords_in_mouse_face_p (w, hpos, vpos);
26869 }
26870
26871
26872 \f
26873 /* Find the glyph rows START_ROW and END_ROW of window W that display
26874 characters between buffer positions START_CHARPOS and END_CHARPOS
26875 (excluding END_CHARPOS). DISP_STRING is a display string that
26876 covers these buffer positions. This is similar to
26877 row_containing_pos, but is more accurate when bidi reordering makes
26878 buffer positions change non-linearly with glyph rows. */
26879 static void
26880 rows_from_pos_range (struct window *w,
26881 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26882 Lisp_Object disp_string,
26883 struct glyph_row **start, struct glyph_row **end)
26884 {
26885 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26886 int last_y = window_text_bottom_y (w);
26887 struct glyph_row *row;
26888
26889 *start = NULL;
26890 *end = NULL;
26891
26892 while (!first->enabled_p
26893 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26894 first++;
26895
26896 /* Find the START row. */
26897 for (row = first;
26898 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26899 row++)
26900 {
26901 /* A row can potentially be the START row if the range of the
26902 characters it displays intersects the range
26903 [START_CHARPOS..END_CHARPOS). */
26904 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26905 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26906 /* See the commentary in row_containing_pos, for the
26907 explanation of the complicated way to check whether
26908 some position is beyond the end of the characters
26909 displayed by a row. */
26910 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26911 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26912 && !row->ends_at_zv_p
26913 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26914 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26915 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26916 && !row->ends_at_zv_p
26917 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26918 {
26919 /* Found a candidate row. Now make sure at least one of the
26920 glyphs it displays has a charpos from the range
26921 [START_CHARPOS..END_CHARPOS).
26922
26923 This is not obvious because bidi reordering could make
26924 buffer positions of a row be 1,2,3,102,101,100, and if we
26925 want to highlight characters in [50..60), we don't want
26926 this row, even though [50..60) does intersect [1..103),
26927 the range of character positions given by the row's start
26928 and end positions. */
26929 struct glyph *g = row->glyphs[TEXT_AREA];
26930 struct glyph *e = g + row->used[TEXT_AREA];
26931
26932 while (g < e)
26933 {
26934 if (((BUFFERP (g->object) || INTEGERP (g->object))
26935 && start_charpos <= g->charpos && g->charpos < end_charpos)
26936 /* A glyph that comes from DISP_STRING is by
26937 definition to be highlighted. */
26938 || EQ (g->object, disp_string))
26939 *start = row;
26940 g++;
26941 }
26942 if (*start)
26943 break;
26944 }
26945 }
26946
26947 /* Find the END row. */
26948 if (!*start
26949 /* If the last row is partially visible, start looking for END
26950 from that row, instead of starting from FIRST. */
26951 && !(row->enabled_p
26952 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26953 row = first;
26954 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26955 {
26956 struct glyph_row *next = row + 1;
26957 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26958
26959 if (!next->enabled_p
26960 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26961 /* The first row >= START whose range of displayed characters
26962 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26963 is the row END + 1. */
26964 || (start_charpos < next_start
26965 && end_charpos < next_start)
26966 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26967 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26968 && !next->ends_at_zv_p
26969 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26970 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26971 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26972 && !next->ends_at_zv_p
26973 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26974 {
26975 *end = row;
26976 break;
26977 }
26978 else
26979 {
26980 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26981 but none of the characters it displays are in the range, it is
26982 also END + 1. */
26983 struct glyph *g = next->glyphs[TEXT_AREA];
26984 struct glyph *s = g;
26985 struct glyph *e = g + next->used[TEXT_AREA];
26986
26987 while (g < e)
26988 {
26989 if (((BUFFERP (g->object) || INTEGERP (g->object))
26990 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26991 /* If the buffer position of the first glyph in
26992 the row is equal to END_CHARPOS, it means
26993 the last character to be highlighted is the
26994 newline of ROW, and we must consider NEXT as
26995 END, not END+1. */
26996 || (((!next->reversed_p && g == s)
26997 || (next->reversed_p && g == e - 1))
26998 && (g->charpos == end_charpos
26999 /* Special case for when NEXT is an
27000 empty line at ZV. */
27001 || (g->charpos == -1
27002 && !row->ends_at_zv_p
27003 && next_start == end_charpos)))))
27004 /* A glyph that comes from DISP_STRING is by
27005 definition to be highlighted. */
27006 || EQ (g->object, disp_string))
27007 break;
27008 g++;
27009 }
27010 if (g == e)
27011 {
27012 *end = row;
27013 break;
27014 }
27015 /* The first row that ends at ZV must be the last to be
27016 highlighted. */
27017 else if (next->ends_at_zv_p)
27018 {
27019 *end = next;
27020 break;
27021 }
27022 }
27023 }
27024 }
27025
27026 /* This function sets the mouse_face_* elements of HLINFO, assuming
27027 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27028 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27029 for the overlay or run of text properties specifying the mouse
27030 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27031 before-string and after-string that must also be highlighted.
27032 DISP_STRING, if non-nil, is a display string that may cover some
27033 or all of the highlighted text. */
27034
27035 static void
27036 mouse_face_from_buffer_pos (Lisp_Object window,
27037 Mouse_HLInfo *hlinfo,
27038 ptrdiff_t mouse_charpos,
27039 ptrdiff_t start_charpos,
27040 ptrdiff_t end_charpos,
27041 Lisp_Object before_string,
27042 Lisp_Object after_string,
27043 Lisp_Object disp_string)
27044 {
27045 struct window *w = XWINDOW (window);
27046 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27047 struct glyph_row *r1, *r2;
27048 struct glyph *glyph, *end;
27049 ptrdiff_t ignore, pos;
27050 int x;
27051
27052 eassert (NILP (disp_string) || STRINGP (disp_string));
27053 eassert (NILP (before_string) || STRINGP (before_string));
27054 eassert (NILP (after_string) || STRINGP (after_string));
27055
27056 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27057 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27058 if (r1 == NULL)
27059 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27060 /* If the before-string or display-string contains newlines,
27061 rows_from_pos_range skips to its last row. Move back. */
27062 if (!NILP (before_string) || !NILP (disp_string))
27063 {
27064 struct glyph_row *prev;
27065 while ((prev = r1 - 1, prev >= first)
27066 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27067 && prev->used[TEXT_AREA] > 0)
27068 {
27069 struct glyph *beg = prev->glyphs[TEXT_AREA];
27070 glyph = beg + prev->used[TEXT_AREA];
27071 while (--glyph >= beg && INTEGERP (glyph->object));
27072 if (glyph < beg
27073 || !(EQ (glyph->object, before_string)
27074 || EQ (glyph->object, disp_string)))
27075 break;
27076 r1 = prev;
27077 }
27078 }
27079 if (r2 == NULL)
27080 {
27081 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27082 hlinfo->mouse_face_past_end = 1;
27083 }
27084 else if (!NILP (after_string))
27085 {
27086 /* If the after-string has newlines, advance to its last row. */
27087 struct glyph_row *next;
27088 struct glyph_row *last
27089 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27090
27091 for (next = r2 + 1;
27092 next <= last
27093 && next->used[TEXT_AREA] > 0
27094 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27095 ++next)
27096 r2 = next;
27097 }
27098 /* The rest of the display engine assumes that mouse_face_beg_row is
27099 either above mouse_face_end_row or identical to it. But with
27100 bidi-reordered continued lines, the row for START_CHARPOS could
27101 be below the row for END_CHARPOS. If so, swap the rows and store
27102 them in correct order. */
27103 if (r1->y > r2->y)
27104 {
27105 struct glyph_row *tem = r2;
27106
27107 r2 = r1;
27108 r1 = tem;
27109 }
27110
27111 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27112 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27113
27114 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27115 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27116 could be anywhere in the row and in any order. The strategy
27117 below is to find the leftmost and the rightmost glyph that
27118 belongs to either of these 3 strings, or whose position is
27119 between START_CHARPOS and END_CHARPOS, and highlight all the
27120 glyphs between those two. This may cover more than just the text
27121 between START_CHARPOS and END_CHARPOS if the range of characters
27122 strides the bidi level boundary, e.g. if the beginning is in R2L
27123 text while the end is in L2R text or vice versa. */
27124 if (!r1->reversed_p)
27125 {
27126 /* This row is in a left to right paragraph. Scan it left to
27127 right. */
27128 glyph = r1->glyphs[TEXT_AREA];
27129 end = glyph + r1->used[TEXT_AREA];
27130 x = r1->x;
27131
27132 /* Skip truncation glyphs at the start of the glyph row. */
27133 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27134 for (; glyph < end
27135 && INTEGERP (glyph->object)
27136 && glyph->charpos < 0;
27137 ++glyph)
27138 x += glyph->pixel_width;
27139
27140 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27141 or DISP_STRING, and the first glyph from buffer whose
27142 position is between START_CHARPOS and END_CHARPOS. */
27143 for (; glyph < end
27144 && !INTEGERP (glyph->object)
27145 && !EQ (glyph->object, disp_string)
27146 && !(BUFFERP (glyph->object)
27147 && (glyph->charpos >= start_charpos
27148 && glyph->charpos < end_charpos));
27149 ++glyph)
27150 {
27151 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27152 are present at buffer positions between START_CHARPOS and
27153 END_CHARPOS, or if they come from an overlay. */
27154 if (EQ (glyph->object, before_string))
27155 {
27156 pos = string_buffer_position (before_string,
27157 start_charpos);
27158 /* If pos == 0, it means before_string came from an
27159 overlay, not from a buffer position. */
27160 if (!pos || (pos >= start_charpos && pos < end_charpos))
27161 break;
27162 }
27163 else if (EQ (glyph->object, after_string))
27164 {
27165 pos = string_buffer_position (after_string, end_charpos);
27166 if (!pos || (pos >= start_charpos && pos < end_charpos))
27167 break;
27168 }
27169 x += glyph->pixel_width;
27170 }
27171 hlinfo->mouse_face_beg_x = x;
27172 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27173 }
27174 else
27175 {
27176 /* This row is in a right to left paragraph. Scan it right to
27177 left. */
27178 struct glyph *g;
27179
27180 end = r1->glyphs[TEXT_AREA] - 1;
27181 glyph = end + r1->used[TEXT_AREA];
27182
27183 /* Skip truncation glyphs at the start of the glyph row. */
27184 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27185 for (; glyph > end
27186 && INTEGERP (glyph->object)
27187 && glyph->charpos < 0;
27188 --glyph)
27189 ;
27190
27191 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27192 or DISP_STRING, and the first glyph from buffer whose
27193 position is between START_CHARPOS and END_CHARPOS. */
27194 for (; glyph > end
27195 && !INTEGERP (glyph->object)
27196 && !EQ (glyph->object, disp_string)
27197 && !(BUFFERP (glyph->object)
27198 && (glyph->charpos >= start_charpos
27199 && glyph->charpos < end_charpos));
27200 --glyph)
27201 {
27202 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27203 are present at buffer positions between START_CHARPOS and
27204 END_CHARPOS, or if they come from an overlay. */
27205 if (EQ (glyph->object, before_string))
27206 {
27207 pos = string_buffer_position (before_string, start_charpos);
27208 /* If pos == 0, it means before_string came from an
27209 overlay, not from a buffer position. */
27210 if (!pos || (pos >= start_charpos && pos < end_charpos))
27211 break;
27212 }
27213 else if (EQ (glyph->object, after_string))
27214 {
27215 pos = string_buffer_position (after_string, end_charpos);
27216 if (!pos || (pos >= start_charpos && pos < end_charpos))
27217 break;
27218 }
27219 }
27220
27221 glyph++; /* first glyph to the right of the highlighted area */
27222 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27223 x += g->pixel_width;
27224 hlinfo->mouse_face_beg_x = x;
27225 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27226 }
27227
27228 /* If the highlight ends in a different row, compute GLYPH and END
27229 for the end row. Otherwise, reuse the values computed above for
27230 the row where the highlight begins. */
27231 if (r2 != r1)
27232 {
27233 if (!r2->reversed_p)
27234 {
27235 glyph = r2->glyphs[TEXT_AREA];
27236 end = glyph + r2->used[TEXT_AREA];
27237 x = r2->x;
27238 }
27239 else
27240 {
27241 end = r2->glyphs[TEXT_AREA] - 1;
27242 glyph = end + r2->used[TEXT_AREA];
27243 }
27244 }
27245
27246 if (!r2->reversed_p)
27247 {
27248 /* Skip truncation and continuation glyphs near the end of the
27249 row, and also blanks and stretch glyphs inserted by
27250 extend_face_to_end_of_line. */
27251 while (end > glyph
27252 && INTEGERP ((end - 1)->object))
27253 --end;
27254 /* Scan the rest of the glyph row from the end, looking for the
27255 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27256 DISP_STRING, or whose position is between START_CHARPOS
27257 and END_CHARPOS */
27258 for (--end;
27259 end > glyph
27260 && !INTEGERP (end->object)
27261 && !EQ (end->object, disp_string)
27262 && !(BUFFERP (end->object)
27263 && (end->charpos >= start_charpos
27264 && end->charpos < end_charpos));
27265 --end)
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 (end->object, before_string))
27271 {
27272 pos = string_buffer_position (before_string, start_charpos);
27273 if (!pos || (pos >= start_charpos && pos < end_charpos))
27274 break;
27275 }
27276 else if (EQ (end->object, after_string))
27277 {
27278 pos = string_buffer_position (after_string, end_charpos);
27279 if (!pos || (pos >= start_charpos && pos < end_charpos))
27280 break;
27281 }
27282 }
27283 /* Find the X coordinate of the last glyph to be highlighted. */
27284 for (; glyph <= end; ++glyph)
27285 x += glyph->pixel_width;
27286
27287 hlinfo->mouse_face_end_x = x;
27288 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27289 }
27290 else
27291 {
27292 /* Skip truncation and continuation glyphs near the end of the
27293 row, and also blanks and stretch glyphs inserted by
27294 extend_face_to_end_of_line. */
27295 x = r2->x;
27296 end++;
27297 while (end < glyph
27298 && INTEGERP (end->object))
27299 {
27300 x += end->pixel_width;
27301 ++end;
27302 }
27303 /* Scan the rest of the glyph row from the end, looking for the
27304 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27305 DISP_STRING, or whose position is between START_CHARPOS
27306 and END_CHARPOS */
27307 for ( ;
27308 end < glyph
27309 && !INTEGERP (end->object)
27310 && !EQ (end->object, disp_string)
27311 && !(BUFFERP (end->object)
27312 && (end->charpos >= start_charpos
27313 && end->charpos < end_charpos));
27314 ++end)
27315 {
27316 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27317 are present at buffer positions between START_CHARPOS and
27318 END_CHARPOS, or if they come from an overlay. */
27319 if (EQ (end->object, before_string))
27320 {
27321 pos = string_buffer_position (before_string, start_charpos);
27322 if (!pos || (pos >= start_charpos && pos < end_charpos))
27323 break;
27324 }
27325 else if (EQ (end->object, after_string))
27326 {
27327 pos = string_buffer_position (after_string, end_charpos);
27328 if (!pos || (pos >= start_charpos && pos < end_charpos))
27329 break;
27330 }
27331 x += end->pixel_width;
27332 }
27333 /* If we exited the above loop because we arrived at the last
27334 glyph of the row, and its buffer position is still not in
27335 range, it means the last character in range is the preceding
27336 newline. Bump the end column and x values to get past the
27337 last glyph. */
27338 if (end == glyph
27339 && BUFFERP (end->object)
27340 && (end->charpos < start_charpos
27341 || end->charpos >= end_charpos))
27342 {
27343 x += end->pixel_width;
27344 ++end;
27345 }
27346 hlinfo->mouse_face_end_x = x;
27347 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27348 }
27349
27350 hlinfo->mouse_face_window = window;
27351 hlinfo->mouse_face_face_id
27352 = face_at_buffer_position (w, mouse_charpos, &ignore,
27353 mouse_charpos + 1,
27354 !hlinfo->mouse_face_hidden, -1);
27355 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27356 }
27357
27358 /* The following function is not used anymore (replaced with
27359 mouse_face_from_string_pos), but I leave it here for the time
27360 being, in case someone would. */
27361
27362 #if 0 /* not used */
27363
27364 /* Find the position of the glyph for position POS in OBJECT in
27365 window W's current matrix, and return in *X, *Y the pixel
27366 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27367
27368 RIGHT_P non-zero means return the position of the right edge of the
27369 glyph, RIGHT_P zero means return the left edge position.
27370
27371 If no glyph for POS exists in the matrix, return the position of
27372 the glyph with the next smaller position that is in the matrix, if
27373 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27374 exists in the matrix, return the position of the glyph with the
27375 next larger position in OBJECT.
27376
27377 Value is non-zero if a glyph was found. */
27378
27379 static int
27380 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27381 int *hpos, int *vpos, int *x, int *y, int right_p)
27382 {
27383 int yb = window_text_bottom_y (w);
27384 struct glyph_row *r;
27385 struct glyph *best_glyph = NULL;
27386 struct glyph_row *best_row = NULL;
27387 int best_x = 0;
27388
27389 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27390 r->enabled_p && r->y < yb;
27391 ++r)
27392 {
27393 struct glyph *g = r->glyphs[TEXT_AREA];
27394 struct glyph *e = g + r->used[TEXT_AREA];
27395 int gx;
27396
27397 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27398 if (EQ (g->object, object))
27399 {
27400 if (g->charpos == pos)
27401 {
27402 best_glyph = g;
27403 best_x = gx;
27404 best_row = r;
27405 goto found;
27406 }
27407 else if (best_glyph == NULL
27408 || ((eabs (g->charpos - pos)
27409 < eabs (best_glyph->charpos - pos))
27410 && (right_p
27411 ? g->charpos < pos
27412 : g->charpos > pos)))
27413 {
27414 best_glyph = g;
27415 best_x = gx;
27416 best_row = r;
27417 }
27418 }
27419 }
27420
27421 found:
27422
27423 if (best_glyph)
27424 {
27425 *x = best_x;
27426 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27427
27428 if (right_p)
27429 {
27430 *x += best_glyph->pixel_width;
27431 ++*hpos;
27432 }
27433
27434 *y = best_row->y;
27435 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27436 }
27437
27438 return best_glyph != NULL;
27439 }
27440 #endif /* not used */
27441
27442 /* Find the positions of the first and the last glyphs in window W's
27443 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27444 (assumed to be a string), and return in HLINFO's mouse_face_*
27445 members the pixel and column/row coordinates of those glyphs. */
27446
27447 static void
27448 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27449 Lisp_Object object,
27450 ptrdiff_t startpos, ptrdiff_t endpos)
27451 {
27452 int yb = window_text_bottom_y (w);
27453 struct glyph_row *r;
27454 struct glyph *g, *e;
27455 int gx;
27456 int found = 0;
27457
27458 /* Find the glyph row with at least one position in the range
27459 [STARTPOS..ENDPOS), and the first glyph in that row whose
27460 position belongs to that range. */
27461 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27462 r->enabled_p && r->y < yb;
27463 ++r)
27464 {
27465 if (!r->reversed_p)
27466 {
27467 g = r->glyphs[TEXT_AREA];
27468 e = g + r->used[TEXT_AREA];
27469 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27470 if (EQ (g->object, object)
27471 && startpos <= g->charpos && g->charpos < endpos)
27472 {
27473 hlinfo->mouse_face_beg_row
27474 = MATRIX_ROW_VPOS (r, w->current_matrix);
27475 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27476 hlinfo->mouse_face_beg_x = gx;
27477 found = 1;
27478 break;
27479 }
27480 }
27481 else
27482 {
27483 struct glyph *g1;
27484
27485 e = r->glyphs[TEXT_AREA];
27486 g = e + r->used[TEXT_AREA];
27487 for ( ; g > e; --g)
27488 if (EQ ((g-1)->object, object)
27489 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27490 {
27491 hlinfo->mouse_face_beg_row
27492 = MATRIX_ROW_VPOS (r, w->current_matrix);
27493 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27494 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27495 gx += g1->pixel_width;
27496 hlinfo->mouse_face_beg_x = gx;
27497 found = 1;
27498 break;
27499 }
27500 }
27501 if (found)
27502 break;
27503 }
27504
27505 if (!found)
27506 return;
27507
27508 /* Starting with the next row, look for the first row which does NOT
27509 include any glyphs whose positions are in the range. */
27510 for (++r; r->enabled_p && r->y < yb; ++r)
27511 {
27512 g = r->glyphs[TEXT_AREA];
27513 e = g + r->used[TEXT_AREA];
27514 found = 0;
27515 for ( ; g < e; ++g)
27516 if (EQ (g->object, object)
27517 && startpos <= g->charpos && g->charpos < endpos)
27518 {
27519 found = 1;
27520 break;
27521 }
27522 if (!found)
27523 break;
27524 }
27525
27526 /* The highlighted region ends on the previous row. */
27527 r--;
27528
27529 /* Set the end row. */
27530 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27531
27532 /* Compute and set the end column and the end column's horizontal
27533 pixel coordinate. */
27534 if (!r->reversed_p)
27535 {
27536 g = r->glyphs[TEXT_AREA];
27537 e = g + r->used[TEXT_AREA];
27538 for ( ; e > g; --e)
27539 if (EQ ((e-1)->object, object)
27540 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27541 break;
27542 hlinfo->mouse_face_end_col = e - g;
27543
27544 for (gx = r->x; g < e; ++g)
27545 gx += g->pixel_width;
27546 hlinfo->mouse_face_end_x = gx;
27547 }
27548 else
27549 {
27550 e = r->glyphs[TEXT_AREA];
27551 g = e + r->used[TEXT_AREA];
27552 for (gx = r->x ; e < g; ++e)
27553 {
27554 if (EQ (e->object, object)
27555 && startpos <= e->charpos && e->charpos < endpos)
27556 break;
27557 gx += e->pixel_width;
27558 }
27559 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27560 hlinfo->mouse_face_end_x = gx;
27561 }
27562 }
27563
27564 #ifdef HAVE_WINDOW_SYSTEM
27565
27566 /* See if position X, Y is within a hot-spot of an image. */
27567
27568 static int
27569 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27570 {
27571 if (!CONSP (hot_spot))
27572 return 0;
27573
27574 if (EQ (XCAR (hot_spot), Qrect))
27575 {
27576 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27577 Lisp_Object rect = XCDR (hot_spot);
27578 Lisp_Object tem;
27579 if (!CONSP (rect))
27580 return 0;
27581 if (!CONSP (XCAR (rect)))
27582 return 0;
27583 if (!CONSP (XCDR (rect)))
27584 return 0;
27585 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27586 return 0;
27587 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27588 return 0;
27589 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27590 return 0;
27591 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27592 return 0;
27593 return 1;
27594 }
27595 else if (EQ (XCAR (hot_spot), Qcircle))
27596 {
27597 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27598 Lisp_Object circ = XCDR (hot_spot);
27599 Lisp_Object lr, lx0, ly0;
27600 if (CONSP (circ)
27601 && CONSP (XCAR (circ))
27602 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27603 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27604 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27605 {
27606 double r = XFLOATINT (lr);
27607 double dx = XINT (lx0) - x;
27608 double dy = XINT (ly0) - y;
27609 return (dx * dx + dy * dy <= r * r);
27610 }
27611 }
27612 else if (EQ (XCAR (hot_spot), Qpoly))
27613 {
27614 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27615 if (VECTORP (XCDR (hot_spot)))
27616 {
27617 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27618 Lisp_Object *poly = v->contents;
27619 ptrdiff_t n = v->header.size;
27620 ptrdiff_t i;
27621 int inside = 0;
27622 Lisp_Object lx, ly;
27623 int x0, y0;
27624
27625 /* Need an even number of coordinates, and at least 3 edges. */
27626 if (n < 6 || n & 1)
27627 return 0;
27628
27629 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27630 If count is odd, we are inside polygon. Pixels on edges
27631 may or may not be included depending on actual geometry of the
27632 polygon. */
27633 if ((lx = poly[n-2], !INTEGERP (lx))
27634 || (ly = poly[n-1], !INTEGERP (lx)))
27635 return 0;
27636 x0 = XINT (lx), y0 = XINT (ly);
27637 for (i = 0; i < n; i += 2)
27638 {
27639 int x1 = x0, y1 = y0;
27640 if ((lx = poly[i], !INTEGERP (lx))
27641 || (ly = poly[i+1], !INTEGERP (ly)))
27642 return 0;
27643 x0 = XINT (lx), y0 = XINT (ly);
27644
27645 /* Does this segment cross the X line? */
27646 if (x0 >= x)
27647 {
27648 if (x1 >= x)
27649 continue;
27650 }
27651 else if (x1 < x)
27652 continue;
27653 if (y > y0 && y > y1)
27654 continue;
27655 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27656 inside = !inside;
27657 }
27658 return inside;
27659 }
27660 }
27661 return 0;
27662 }
27663
27664 Lisp_Object
27665 find_hot_spot (Lisp_Object map, int x, int y)
27666 {
27667 while (CONSP (map))
27668 {
27669 if (CONSP (XCAR (map))
27670 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27671 return XCAR (map);
27672 map = XCDR (map);
27673 }
27674
27675 return Qnil;
27676 }
27677
27678 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27679 3, 3, 0,
27680 doc: /* Lookup in image map MAP coordinates X and Y.
27681 An image map is an alist where each element has the format (AREA ID PLIST).
27682 An AREA is specified as either a rectangle, a circle, or a polygon:
27683 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27684 pixel coordinates of the upper left and bottom right corners.
27685 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27686 and the radius of the circle; r may be a float or integer.
27687 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27688 vector describes one corner in the polygon.
27689 Returns the alist element for the first matching AREA in MAP. */)
27690 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27691 {
27692 if (NILP (map))
27693 return Qnil;
27694
27695 CHECK_NUMBER (x);
27696 CHECK_NUMBER (y);
27697
27698 return find_hot_spot (map,
27699 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27700 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27701 }
27702
27703
27704 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27705 static void
27706 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27707 {
27708 /* Do not change cursor shape while dragging mouse. */
27709 if (!NILP (do_mouse_tracking))
27710 return;
27711
27712 if (!NILP (pointer))
27713 {
27714 if (EQ (pointer, Qarrow))
27715 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27716 else if (EQ (pointer, Qhand))
27717 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27718 else if (EQ (pointer, Qtext))
27719 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27720 else if (EQ (pointer, intern ("hdrag")))
27721 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27722 #ifdef HAVE_X_WINDOWS
27723 else if (EQ (pointer, intern ("vdrag")))
27724 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27725 #endif
27726 else if (EQ (pointer, intern ("hourglass")))
27727 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27728 else if (EQ (pointer, Qmodeline))
27729 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27730 else
27731 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27732 }
27733
27734 if (cursor != No_Cursor)
27735 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27736 }
27737
27738 #endif /* HAVE_WINDOW_SYSTEM */
27739
27740 /* Take proper action when mouse has moved to the mode or header line
27741 or marginal area AREA of window W, x-position X and y-position Y.
27742 X is relative to the start of the text display area of W, so the
27743 width of bitmap areas and scroll bars must be subtracted to get a
27744 position relative to the start of the mode line. */
27745
27746 static void
27747 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27748 enum window_part area)
27749 {
27750 struct window *w = XWINDOW (window);
27751 struct frame *f = XFRAME (w->frame);
27752 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27753 #ifdef HAVE_WINDOW_SYSTEM
27754 Display_Info *dpyinfo;
27755 #endif
27756 Cursor cursor = No_Cursor;
27757 Lisp_Object pointer = Qnil;
27758 int dx, dy, width, height;
27759 ptrdiff_t charpos;
27760 Lisp_Object string, object = Qnil;
27761 Lisp_Object pos IF_LINT (= Qnil), help;
27762
27763 Lisp_Object mouse_face;
27764 int original_x_pixel = x;
27765 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27766 struct glyph_row *row IF_LINT (= 0);
27767
27768 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27769 {
27770 int x0;
27771 struct glyph *end;
27772
27773 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27774 returns them in row/column units! */
27775 string = mode_line_string (w, area, &x, &y, &charpos,
27776 &object, &dx, &dy, &width, &height);
27777
27778 row = (area == ON_MODE_LINE
27779 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27780 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27781
27782 /* Find the glyph under the mouse pointer. */
27783 if (row->mode_line_p && row->enabled_p)
27784 {
27785 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27786 end = glyph + row->used[TEXT_AREA];
27787
27788 for (x0 = original_x_pixel;
27789 glyph < end && x0 >= glyph->pixel_width;
27790 ++glyph)
27791 x0 -= glyph->pixel_width;
27792
27793 if (glyph >= end)
27794 glyph = NULL;
27795 }
27796 }
27797 else
27798 {
27799 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27800 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27801 returns them in row/column units! */
27802 string = marginal_area_string (w, area, &x, &y, &charpos,
27803 &object, &dx, &dy, &width, &height);
27804 }
27805
27806 help = Qnil;
27807
27808 #ifdef HAVE_WINDOW_SYSTEM
27809 if (IMAGEP (object))
27810 {
27811 Lisp_Object image_map, hotspot;
27812 if ((image_map = Fplist_get (XCDR (object), QCmap),
27813 !NILP (image_map))
27814 && (hotspot = find_hot_spot (image_map, dx, dy),
27815 CONSP (hotspot))
27816 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27817 {
27818 Lisp_Object plist;
27819
27820 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27821 If so, we could look for mouse-enter, mouse-leave
27822 properties in PLIST (and do something...). */
27823 hotspot = XCDR (hotspot);
27824 if (CONSP (hotspot)
27825 && (plist = XCAR (hotspot), CONSP (plist)))
27826 {
27827 pointer = Fplist_get (plist, Qpointer);
27828 if (NILP (pointer))
27829 pointer = Qhand;
27830 help = Fplist_get (plist, Qhelp_echo);
27831 if (!NILP (help))
27832 {
27833 help_echo_string = help;
27834 XSETWINDOW (help_echo_window, w);
27835 help_echo_object = w->contents;
27836 help_echo_pos = charpos;
27837 }
27838 }
27839 }
27840 if (NILP (pointer))
27841 pointer = Fplist_get (XCDR (object), QCpointer);
27842 }
27843 #endif /* HAVE_WINDOW_SYSTEM */
27844
27845 if (STRINGP (string))
27846 pos = make_number (charpos);
27847
27848 /* Set the help text and mouse pointer. If the mouse is on a part
27849 of the mode line without any text (e.g. past the right edge of
27850 the mode line text), use the default help text and pointer. */
27851 if (STRINGP (string) || area == ON_MODE_LINE)
27852 {
27853 /* Arrange to display the help by setting the global variables
27854 help_echo_string, help_echo_object, and help_echo_pos. */
27855 if (NILP (help))
27856 {
27857 if (STRINGP (string))
27858 help = Fget_text_property (pos, Qhelp_echo, string);
27859
27860 if (!NILP (help))
27861 {
27862 help_echo_string = help;
27863 XSETWINDOW (help_echo_window, w);
27864 help_echo_object = string;
27865 help_echo_pos = charpos;
27866 }
27867 else if (area == ON_MODE_LINE)
27868 {
27869 Lisp_Object default_help
27870 = buffer_local_value_1 (Qmode_line_default_help_echo,
27871 w->contents);
27872
27873 if (STRINGP (default_help))
27874 {
27875 help_echo_string = default_help;
27876 XSETWINDOW (help_echo_window, w);
27877 help_echo_object = Qnil;
27878 help_echo_pos = -1;
27879 }
27880 }
27881 }
27882
27883 #ifdef HAVE_WINDOW_SYSTEM
27884 /* Change the mouse pointer according to what is under it. */
27885 if (FRAME_WINDOW_P (f))
27886 {
27887 dpyinfo = FRAME_DISPLAY_INFO (f);
27888 if (STRINGP (string))
27889 {
27890 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27891
27892 if (NILP (pointer))
27893 pointer = Fget_text_property (pos, Qpointer, string);
27894
27895 /* Change the mouse pointer according to what is under X/Y. */
27896 if (NILP (pointer)
27897 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27898 {
27899 Lisp_Object map;
27900 map = Fget_text_property (pos, Qlocal_map, string);
27901 if (!KEYMAPP (map))
27902 map = Fget_text_property (pos, Qkeymap, string);
27903 if (!KEYMAPP (map))
27904 cursor = dpyinfo->vertical_scroll_bar_cursor;
27905 }
27906 }
27907 else
27908 /* Default mode-line pointer. */
27909 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27910 }
27911 #endif
27912 }
27913
27914 /* Change the mouse face according to what is under X/Y. */
27915 if (STRINGP (string))
27916 {
27917 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27918 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27919 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27920 && glyph)
27921 {
27922 Lisp_Object b, e;
27923
27924 struct glyph * tmp_glyph;
27925
27926 int gpos;
27927 int gseq_length;
27928 int total_pixel_width;
27929 ptrdiff_t begpos, endpos, ignore;
27930
27931 int vpos, hpos;
27932
27933 b = Fprevious_single_property_change (make_number (charpos + 1),
27934 Qmouse_face, string, Qnil);
27935 if (NILP (b))
27936 begpos = 0;
27937 else
27938 begpos = XINT (b);
27939
27940 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27941 if (NILP (e))
27942 endpos = SCHARS (string);
27943 else
27944 endpos = XINT (e);
27945
27946 /* Calculate the glyph position GPOS of GLYPH in the
27947 displayed string, relative to the beginning of the
27948 highlighted part of the string.
27949
27950 Note: GPOS is different from CHARPOS. CHARPOS is the
27951 position of GLYPH in the internal string object. A mode
27952 line string format has structures which are converted to
27953 a flattened string by the Emacs Lisp interpreter. The
27954 internal string is an element of those structures. The
27955 displayed string is the flattened string. */
27956 tmp_glyph = row_start_glyph;
27957 while (tmp_glyph < glyph
27958 && (!(EQ (tmp_glyph->object, glyph->object)
27959 && begpos <= tmp_glyph->charpos
27960 && tmp_glyph->charpos < endpos)))
27961 tmp_glyph++;
27962 gpos = glyph - tmp_glyph;
27963
27964 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27965 the highlighted part of the displayed string to which
27966 GLYPH belongs. Note: GSEQ_LENGTH is different from
27967 SCHARS (STRING), because the latter returns the length of
27968 the internal string. */
27969 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27970 tmp_glyph > glyph
27971 && (!(EQ (tmp_glyph->object, glyph->object)
27972 && begpos <= tmp_glyph->charpos
27973 && tmp_glyph->charpos < endpos));
27974 tmp_glyph--)
27975 ;
27976 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27977
27978 /* Calculate the total pixel width of all the glyphs between
27979 the beginning of the highlighted area and GLYPH. */
27980 total_pixel_width = 0;
27981 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27982 total_pixel_width += tmp_glyph->pixel_width;
27983
27984 /* Pre calculation of re-rendering position. Note: X is in
27985 column units here, after the call to mode_line_string or
27986 marginal_area_string. */
27987 hpos = x - gpos;
27988 vpos = (area == ON_MODE_LINE
27989 ? (w->current_matrix)->nrows - 1
27990 : 0);
27991
27992 /* If GLYPH's position is included in the region that is
27993 already drawn in mouse face, we have nothing to do. */
27994 if ( EQ (window, hlinfo->mouse_face_window)
27995 && (!row->reversed_p
27996 ? (hlinfo->mouse_face_beg_col <= hpos
27997 && hpos < hlinfo->mouse_face_end_col)
27998 /* In R2L rows we swap BEG and END, see below. */
27999 : (hlinfo->mouse_face_end_col <= hpos
28000 && hpos < hlinfo->mouse_face_beg_col))
28001 && hlinfo->mouse_face_beg_row == vpos )
28002 return;
28003
28004 if (clear_mouse_face (hlinfo))
28005 cursor = No_Cursor;
28006
28007 if (!row->reversed_p)
28008 {
28009 hlinfo->mouse_face_beg_col = hpos;
28010 hlinfo->mouse_face_beg_x = original_x_pixel
28011 - (total_pixel_width + dx);
28012 hlinfo->mouse_face_end_col = hpos + gseq_length;
28013 hlinfo->mouse_face_end_x = 0;
28014 }
28015 else
28016 {
28017 /* In R2L rows, show_mouse_face expects BEG and END
28018 coordinates to be swapped. */
28019 hlinfo->mouse_face_end_col = hpos;
28020 hlinfo->mouse_face_end_x = original_x_pixel
28021 - (total_pixel_width + dx);
28022 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28023 hlinfo->mouse_face_beg_x = 0;
28024 }
28025
28026 hlinfo->mouse_face_beg_row = vpos;
28027 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28028 hlinfo->mouse_face_past_end = 0;
28029 hlinfo->mouse_face_window = window;
28030
28031 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28032 charpos,
28033 0, &ignore,
28034 glyph->face_id,
28035 1);
28036 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28037
28038 if (NILP (pointer))
28039 pointer = Qhand;
28040 }
28041 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28042 clear_mouse_face (hlinfo);
28043 }
28044 #ifdef HAVE_WINDOW_SYSTEM
28045 if (FRAME_WINDOW_P (f))
28046 define_frame_cursor1 (f, cursor, pointer);
28047 #endif
28048 }
28049
28050
28051 /* EXPORT:
28052 Take proper action when the mouse has moved to position X, Y on
28053 frame F with regards to highlighting portions of display that have
28054 mouse-face properties. Also de-highlight portions of display where
28055 the mouse was before, set the mouse pointer shape as appropriate
28056 for the mouse coordinates, and activate help echo (tooltips).
28057 X and Y can be negative or out of range. */
28058
28059 void
28060 note_mouse_highlight (struct frame *f, int x, int y)
28061 {
28062 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28063 enum window_part part = ON_NOTHING;
28064 Lisp_Object window;
28065 struct window *w;
28066 Cursor cursor = No_Cursor;
28067 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28068 struct buffer *b;
28069
28070 /* When a menu is active, don't highlight because this looks odd. */
28071 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28072 if (popup_activated ())
28073 return;
28074 #endif
28075
28076 if (!f->glyphs_initialized_p
28077 || f->pointer_invisible)
28078 return;
28079
28080 hlinfo->mouse_face_mouse_x = x;
28081 hlinfo->mouse_face_mouse_y = y;
28082 hlinfo->mouse_face_mouse_frame = f;
28083
28084 if (hlinfo->mouse_face_defer)
28085 return;
28086
28087 /* Which window is that in? */
28088 window = window_from_coordinates (f, x, y, &part, 1);
28089
28090 /* If displaying active text in another window, clear that. */
28091 if (! EQ (window, hlinfo->mouse_face_window)
28092 /* Also clear if we move out of text area in same window. */
28093 || (!NILP (hlinfo->mouse_face_window)
28094 && !NILP (window)
28095 && part != ON_TEXT
28096 && part != ON_MODE_LINE
28097 && part != ON_HEADER_LINE))
28098 clear_mouse_face (hlinfo);
28099
28100 /* Not on a window -> return. */
28101 if (!WINDOWP (window))
28102 return;
28103
28104 /* Reset help_echo_string. It will get recomputed below. */
28105 help_echo_string = Qnil;
28106
28107 /* Convert to window-relative pixel coordinates. */
28108 w = XWINDOW (window);
28109 frame_to_window_pixel_xy (w, &x, &y);
28110
28111 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28112 /* Handle tool-bar window differently since it doesn't display a
28113 buffer. */
28114 if (EQ (window, f->tool_bar_window))
28115 {
28116 note_tool_bar_highlight (f, x, y);
28117 return;
28118 }
28119 #endif
28120
28121 /* Mouse is on the mode, header line or margin? */
28122 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28123 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28124 {
28125 note_mode_line_or_margin_highlight (window, x, y, part);
28126 return;
28127 }
28128
28129 #ifdef HAVE_WINDOW_SYSTEM
28130 if (part == ON_VERTICAL_BORDER)
28131 {
28132 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28133 help_echo_string = build_string ("drag-mouse-1: resize");
28134 }
28135 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28136 || part == ON_SCROLL_BAR)
28137 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28138 else
28139 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28140 #endif
28141
28142 /* Are we in a window whose display is up to date?
28143 And verify the buffer's text has not changed. */
28144 b = XBUFFER (w->contents);
28145 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28146 {
28147 int hpos, vpos, dx, dy, area = LAST_AREA;
28148 ptrdiff_t pos;
28149 struct glyph *glyph;
28150 Lisp_Object object;
28151 Lisp_Object mouse_face = Qnil, position;
28152 Lisp_Object *overlay_vec = NULL;
28153 ptrdiff_t i, noverlays;
28154 struct buffer *obuf;
28155 ptrdiff_t obegv, ozv;
28156 int same_region;
28157
28158 /* Find the glyph under X/Y. */
28159 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28160
28161 #ifdef HAVE_WINDOW_SYSTEM
28162 /* Look for :pointer property on image. */
28163 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28164 {
28165 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28166 if (img != NULL && IMAGEP (img->spec))
28167 {
28168 Lisp_Object image_map, hotspot;
28169 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28170 !NILP (image_map))
28171 && (hotspot = find_hot_spot (image_map,
28172 glyph->slice.img.x + dx,
28173 glyph->slice.img.y + dy),
28174 CONSP (hotspot))
28175 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28176 {
28177 Lisp_Object plist;
28178
28179 /* Could check XCAR (hotspot) to see if we enter/leave
28180 this hot-spot.
28181 If so, we could look for mouse-enter, mouse-leave
28182 properties in PLIST (and do something...). */
28183 hotspot = XCDR (hotspot);
28184 if (CONSP (hotspot)
28185 && (plist = XCAR (hotspot), CONSP (plist)))
28186 {
28187 pointer = Fplist_get (plist, Qpointer);
28188 if (NILP (pointer))
28189 pointer = Qhand;
28190 help_echo_string = Fplist_get (plist, Qhelp_echo);
28191 if (!NILP (help_echo_string))
28192 {
28193 help_echo_window = window;
28194 help_echo_object = glyph->object;
28195 help_echo_pos = glyph->charpos;
28196 }
28197 }
28198 }
28199 if (NILP (pointer))
28200 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28201 }
28202 }
28203 #endif /* HAVE_WINDOW_SYSTEM */
28204
28205 /* Clear mouse face if X/Y not over text. */
28206 if (glyph == NULL
28207 || area != TEXT_AREA
28208 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28209 /* Glyph's OBJECT is an integer for glyphs inserted by the
28210 display engine for its internal purposes, like truncation
28211 and continuation glyphs and blanks beyond the end of
28212 line's text on text terminals. If we are over such a
28213 glyph, we are not over any text. */
28214 || INTEGERP (glyph->object)
28215 /* R2L rows have a stretch glyph at their front, which
28216 stands for no text, whereas L2R rows have no glyphs at
28217 all beyond the end of text. Treat such stretch glyphs
28218 like we do with NULL glyphs in L2R rows. */
28219 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28220 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28221 && glyph->type == STRETCH_GLYPH
28222 && glyph->avoid_cursor_p))
28223 {
28224 if (clear_mouse_face (hlinfo))
28225 cursor = No_Cursor;
28226 #ifdef HAVE_WINDOW_SYSTEM
28227 if (FRAME_WINDOW_P (f) && NILP (pointer))
28228 {
28229 if (area != TEXT_AREA)
28230 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28231 else
28232 pointer = Vvoid_text_area_pointer;
28233 }
28234 #endif
28235 goto set_cursor;
28236 }
28237
28238 pos = glyph->charpos;
28239 object = glyph->object;
28240 if (!STRINGP (object) && !BUFFERP (object))
28241 goto set_cursor;
28242
28243 /* If we get an out-of-range value, return now; avoid an error. */
28244 if (BUFFERP (object) && pos > BUF_Z (b))
28245 goto set_cursor;
28246
28247 /* Make the window's buffer temporarily current for
28248 overlays_at and compute_char_face. */
28249 obuf = current_buffer;
28250 current_buffer = b;
28251 obegv = BEGV;
28252 ozv = ZV;
28253 BEGV = BEG;
28254 ZV = Z;
28255
28256 /* Is this char mouse-active or does it have help-echo? */
28257 position = make_number (pos);
28258
28259 if (BUFFERP (object))
28260 {
28261 /* Put all the overlays we want in a vector in overlay_vec. */
28262 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28263 /* Sort overlays into increasing priority order. */
28264 noverlays = sort_overlays (overlay_vec, noverlays, w);
28265 }
28266 else
28267 noverlays = 0;
28268
28269 if (NILP (Vmouse_highlight))
28270 {
28271 clear_mouse_face (hlinfo);
28272 goto check_help_echo;
28273 }
28274
28275 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28276
28277 if (same_region)
28278 cursor = No_Cursor;
28279
28280 /* Check mouse-face highlighting. */
28281 if (! same_region
28282 /* If there exists an overlay with mouse-face overlapping
28283 the one we are currently highlighting, we have to
28284 check if we enter the overlapping overlay, and then
28285 highlight only that. */
28286 || (OVERLAYP (hlinfo->mouse_face_overlay)
28287 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28288 {
28289 /* Find the highest priority overlay with a mouse-face. */
28290 Lisp_Object overlay = Qnil;
28291 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28292 {
28293 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28294 if (!NILP (mouse_face))
28295 overlay = overlay_vec[i];
28296 }
28297
28298 /* If we're highlighting the same overlay as before, there's
28299 no need to do that again. */
28300 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28301 goto check_help_echo;
28302 hlinfo->mouse_face_overlay = overlay;
28303
28304 /* Clear the display of the old active region, if any. */
28305 if (clear_mouse_face (hlinfo))
28306 cursor = No_Cursor;
28307
28308 /* If no overlay applies, get a text property. */
28309 if (NILP (overlay))
28310 mouse_face = Fget_text_property (position, Qmouse_face, object);
28311
28312 /* Next, compute the bounds of the mouse highlighting and
28313 display it. */
28314 if (!NILP (mouse_face) && STRINGP (object))
28315 {
28316 /* The mouse-highlighting comes from a display string
28317 with a mouse-face. */
28318 Lisp_Object s, e;
28319 ptrdiff_t ignore;
28320
28321 s = Fprevious_single_property_change
28322 (make_number (pos + 1), Qmouse_face, object, Qnil);
28323 e = Fnext_single_property_change
28324 (position, Qmouse_face, object, Qnil);
28325 if (NILP (s))
28326 s = make_number (0);
28327 if (NILP (e))
28328 e = make_number (SCHARS (object));
28329 mouse_face_from_string_pos (w, hlinfo, object,
28330 XINT (s), XINT (e));
28331 hlinfo->mouse_face_past_end = 0;
28332 hlinfo->mouse_face_window = window;
28333 hlinfo->mouse_face_face_id
28334 = face_at_string_position (w, object, pos, 0, &ignore,
28335 glyph->face_id, 1);
28336 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28337 cursor = No_Cursor;
28338 }
28339 else
28340 {
28341 /* The mouse-highlighting, if any, comes from an overlay
28342 or text property in the buffer. */
28343 Lisp_Object buffer IF_LINT (= Qnil);
28344 Lisp_Object disp_string IF_LINT (= Qnil);
28345
28346 if (STRINGP (object))
28347 {
28348 /* If we are on a display string with no mouse-face,
28349 check if the text under it has one. */
28350 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28351 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28352 pos = string_buffer_position (object, start);
28353 if (pos > 0)
28354 {
28355 mouse_face = get_char_property_and_overlay
28356 (make_number (pos), Qmouse_face, w->contents, &overlay);
28357 buffer = w->contents;
28358 disp_string = object;
28359 }
28360 }
28361 else
28362 {
28363 buffer = object;
28364 disp_string = Qnil;
28365 }
28366
28367 if (!NILP (mouse_face))
28368 {
28369 Lisp_Object before, after;
28370 Lisp_Object before_string, after_string;
28371 /* To correctly find the limits of mouse highlight
28372 in a bidi-reordered buffer, we must not use the
28373 optimization of limiting the search in
28374 previous-single-property-change and
28375 next-single-property-change, because
28376 rows_from_pos_range needs the real start and end
28377 positions to DTRT in this case. That's because
28378 the first row visible in a window does not
28379 necessarily display the character whose position
28380 is the smallest. */
28381 Lisp_Object lim1
28382 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28383 ? Fmarker_position (w->start)
28384 : Qnil;
28385 Lisp_Object lim2
28386 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28387 ? make_number (BUF_Z (XBUFFER (buffer))
28388 - w->window_end_pos)
28389 : Qnil;
28390
28391 if (NILP (overlay))
28392 {
28393 /* Handle the text property case. */
28394 before = Fprevious_single_property_change
28395 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28396 after = Fnext_single_property_change
28397 (make_number (pos), Qmouse_face, buffer, lim2);
28398 before_string = after_string = Qnil;
28399 }
28400 else
28401 {
28402 /* Handle the overlay case. */
28403 before = Foverlay_start (overlay);
28404 after = Foverlay_end (overlay);
28405 before_string = Foverlay_get (overlay, Qbefore_string);
28406 after_string = Foverlay_get (overlay, Qafter_string);
28407
28408 if (!STRINGP (before_string)) before_string = Qnil;
28409 if (!STRINGP (after_string)) after_string = Qnil;
28410 }
28411
28412 mouse_face_from_buffer_pos (window, hlinfo, pos,
28413 NILP (before)
28414 ? 1
28415 : XFASTINT (before),
28416 NILP (after)
28417 ? BUF_Z (XBUFFER (buffer))
28418 : XFASTINT (after),
28419 before_string, after_string,
28420 disp_string);
28421 cursor = No_Cursor;
28422 }
28423 }
28424 }
28425
28426 check_help_echo:
28427
28428 /* Look for a `help-echo' property. */
28429 if (NILP (help_echo_string)) {
28430 Lisp_Object help, overlay;
28431
28432 /* Check overlays first. */
28433 help = overlay = Qnil;
28434 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28435 {
28436 overlay = overlay_vec[i];
28437 help = Foverlay_get (overlay, Qhelp_echo);
28438 }
28439
28440 if (!NILP (help))
28441 {
28442 help_echo_string = help;
28443 help_echo_window = window;
28444 help_echo_object = overlay;
28445 help_echo_pos = pos;
28446 }
28447 else
28448 {
28449 Lisp_Object obj = glyph->object;
28450 ptrdiff_t charpos = glyph->charpos;
28451
28452 /* Try text properties. */
28453 if (STRINGP (obj)
28454 && charpos >= 0
28455 && charpos < SCHARS (obj))
28456 {
28457 help = Fget_text_property (make_number (charpos),
28458 Qhelp_echo, obj);
28459 if (NILP (help))
28460 {
28461 /* If the string itself doesn't specify a help-echo,
28462 see if the buffer text ``under'' it does. */
28463 struct glyph_row *r
28464 = MATRIX_ROW (w->current_matrix, vpos);
28465 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28466 ptrdiff_t p = string_buffer_position (obj, start);
28467 if (p > 0)
28468 {
28469 help = Fget_char_property (make_number (p),
28470 Qhelp_echo, w->contents);
28471 if (!NILP (help))
28472 {
28473 charpos = p;
28474 obj = w->contents;
28475 }
28476 }
28477 }
28478 }
28479 else if (BUFFERP (obj)
28480 && charpos >= BEGV
28481 && charpos < ZV)
28482 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28483 obj);
28484
28485 if (!NILP (help))
28486 {
28487 help_echo_string = help;
28488 help_echo_window = window;
28489 help_echo_object = obj;
28490 help_echo_pos = charpos;
28491 }
28492 }
28493 }
28494
28495 #ifdef HAVE_WINDOW_SYSTEM
28496 /* Look for a `pointer' property. */
28497 if (FRAME_WINDOW_P (f) && NILP (pointer))
28498 {
28499 /* Check overlays first. */
28500 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28501 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28502
28503 if (NILP (pointer))
28504 {
28505 Lisp_Object obj = glyph->object;
28506 ptrdiff_t charpos = glyph->charpos;
28507
28508 /* Try text properties. */
28509 if (STRINGP (obj)
28510 && charpos >= 0
28511 && charpos < SCHARS (obj))
28512 {
28513 pointer = Fget_text_property (make_number (charpos),
28514 Qpointer, obj);
28515 if (NILP (pointer))
28516 {
28517 /* If the string itself doesn't specify a pointer,
28518 see if the buffer text ``under'' it does. */
28519 struct glyph_row *r
28520 = MATRIX_ROW (w->current_matrix, vpos);
28521 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28522 ptrdiff_t p = string_buffer_position (obj, start);
28523 if (p > 0)
28524 pointer = Fget_char_property (make_number (p),
28525 Qpointer, w->contents);
28526 }
28527 }
28528 else if (BUFFERP (obj)
28529 && charpos >= BEGV
28530 && charpos < ZV)
28531 pointer = Fget_text_property (make_number (charpos),
28532 Qpointer, obj);
28533 }
28534 }
28535 #endif /* HAVE_WINDOW_SYSTEM */
28536
28537 BEGV = obegv;
28538 ZV = ozv;
28539 current_buffer = obuf;
28540 }
28541
28542 set_cursor:
28543
28544 #ifdef HAVE_WINDOW_SYSTEM
28545 if (FRAME_WINDOW_P (f))
28546 define_frame_cursor1 (f, cursor, pointer);
28547 #else
28548 /* This is here to prevent a compiler error, about "label at end of
28549 compound statement". */
28550 return;
28551 #endif
28552 }
28553
28554
28555 /* EXPORT for RIF:
28556 Clear any mouse-face on window W. This function is part of the
28557 redisplay interface, and is called from try_window_id and similar
28558 functions to ensure the mouse-highlight is off. */
28559
28560 void
28561 x_clear_window_mouse_face (struct window *w)
28562 {
28563 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28564 Lisp_Object window;
28565
28566 block_input ();
28567 XSETWINDOW (window, w);
28568 if (EQ (window, hlinfo->mouse_face_window))
28569 clear_mouse_face (hlinfo);
28570 unblock_input ();
28571 }
28572
28573
28574 /* EXPORT:
28575 Just discard the mouse face information for frame F, if any.
28576 This is used when the size of F is changed. */
28577
28578 void
28579 cancel_mouse_face (struct frame *f)
28580 {
28581 Lisp_Object window;
28582 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28583
28584 window = hlinfo->mouse_face_window;
28585 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28586 reset_mouse_highlight (hlinfo);
28587 }
28588
28589
28590 \f
28591 /***********************************************************************
28592 Exposure Events
28593 ***********************************************************************/
28594
28595 #ifdef HAVE_WINDOW_SYSTEM
28596
28597 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28598 which intersects rectangle R. R is in window-relative coordinates. */
28599
28600 static void
28601 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28602 enum glyph_row_area area)
28603 {
28604 struct glyph *first = row->glyphs[area];
28605 struct glyph *end = row->glyphs[area] + row->used[area];
28606 struct glyph *last;
28607 int first_x, start_x, x;
28608
28609 if (area == TEXT_AREA && row->fill_line_p)
28610 /* If row extends face to end of line write the whole line. */
28611 draw_glyphs (w, 0, row, area,
28612 0, row->used[area],
28613 DRAW_NORMAL_TEXT, 0);
28614 else
28615 {
28616 /* Set START_X to the window-relative start position for drawing glyphs of
28617 AREA. The first glyph of the text area can be partially visible.
28618 The first glyphs of other areas cannot. */
28619 start_x = window_box_left_offset (w, area);
28620 x = start_x;
28621 if (area == TEXT_AREA)
28622 x += row->x;
28623
28624 /* Find the first glyph that must be redrawn. */
28625 while (first < end
28626 && x + first->pixel_width < r->x)
28627 {
28628 x += first->pixel_width;
28629 ++first;
28630 }
28631
28632 /* Find the last one. */
28633 last = first;
28634 first_x = x;
28635 while (last < end
28636 && x < r->x + r->width)
28637 {
28638 x += last->pixel_width;
28639 ++last;
28640 }
28641
28642 /* Repaint. */
28643 if (last > first)
28644 draw_glyphs (w, first_x - start_x, row, area,
28645 first - row->glyphs[area], last - row->glyphs[area],
28646 DRAW_NORMAL_TEXT, 0);
28647 }
28648 }
28649
28650
28651 /* Redraw the parts of the glyph row ROW on window W intersecting
28652 rectangle R. R is in window-relative coordinates. Value is
28653 non-zero if mouse-face was overwritten. */
28654
28655 static int
28656 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28657 {
28658 eassert (row->enabled_p);
28659
28660 if (row->mode_line_p || w->pseudo_window_p)
28661 draw_glyphs (w, 0, row, TEXT_AREA,
28662 0, row->used[TEXT_AREA],
28663 DRAW_NORMAL_TEXT, 0);
28664 else
28665 {
28666 if (row->used[LEFT_MARGIN_AREA])
28667 expose_area (w, row, r, LEFT_MARGIN_AREA);
28668 if (row->used[TEXT_AREA])
28669 expose_area (w, row, r, TEXT_AREA);
28670 if (row->used[RIGHT_MARGIN_AREA])
28671 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28672 draw_row_fringe_bitmaps (w, row);
28673 }
28674
28675 return row->mouse_face_p;
28676 }
28677
28678
28679 /* Redraw those parts of glyphs rows during expose event handling that
28680 overlap other rows. Redrawing of an exposed line writes over parts
28681 of lines overlapping that exposed line; this function fixes that.
28682
28683 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28684 row in W's current matrix that is exposed and overlaps other rows.
28685 LAST_OVERLAPPING_ROW is the last such row. */
28686
28687 static void
28688 expose_overlaps (struct window *w,
28689 struct glyph_row *first_overlapping_row,
28690 struct glyph_row *last_overlapping_row,
28691 XRectangle *r)
28692 {
28693 struct glyph_row *row;
28694
28695 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28696 if (row->overlapping_p)
28697 {
28698 eassert (row->enabled_p && !row->mode_line_p);
28699
28700 row->clip = r;
28701 if (row->used[LEFT_MARGIN_AREA])
28702 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28703
28704 if (row->used[TEXT_AREA])
28705 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28706
28707 if (row->used[RIGHT_MARGIN_AREA])
28708 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28709 row->clip = NULL;
28710 }
28711 }
28712
28713
28714 /* Return non-zero if W's cursor intersects rectangle R. */
28715
28716 static int
28717 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28718 {
28719 XRectangle cr, result;
28720 struct glyph *cursor_glyph;
28721 struct glyph_row *row;
28722
28723 if (w->phys_cursor.vpos >= 0
28724 && w->phys_cursor.vpos < w->current_matrix->nrows
28725 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28726 row->enabled_p)
28727 && row->cursor_in_fringe_p)
28728 {
28729 /* Cursor is in the fringe. */
28730 cr.x = window_box_right_offset (w,
28731 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28732 ? RIGHT_MARGIN_AREA
28733 : TEXT_AREA));
28734 cr.y = row->y;
28735 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28736 cr.height = row->height;
28737 return x_intersect_rectangles (&cr, r, &result);
28738 }
28739
28740 cursor_glyph = get_phys_cursor_glyph (w);
28741 if (cursor_glyph)
28742 {
28743 /* r is relative to W's box, but w->phys_cursor.x is relative
28744 to left edge of W's TEXT area. Adjust it. */
28745 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28746 cr.y = w->phys_cursor.y;
28747 cr.width = cursor_glyph->pixel_width;
28748 cr.height = w->phys_cursor_height;
28749 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28750 I assume the effect is the same -- and this is portable. */
28751 return x_intersect_rectangles (&cr, r, &result);
28752 }
28753 /* If we don't understand the format, pretend we're not in the hot-spot. */
28754 return 0;
28755 }
28756
28757
28758 /* EXPORT:
28759 Draw a vertical window border to the right of window W if W doesn't
28760 have vertical scroll bars. */
28761
28762 void
28763 x_draw_vertical_border (struct window *w)
28764 {
28765 struct frame *f = XFRAME (WINDOW_FRAME (w));
28766
28767 /* We could do better, if we knew what type of scroll-bar the adjacent
28768 windows (on either side) have... But we don't :-(
28769 However, I think this works ok. ++KFS 2003-04-25 */
28770
28771 /* Redraw borders between horizontally adjacent windows. Don't
28772 do it for frames with vertical scroll bars because either the
28773 right scroll bar of a window, or the left scroll bar of its
28774 neighbor will suffice as a border. */
28775 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28776 return;
28777
28778 /* Note: It is necessary to redraw both the left and the right
28779 borders, for when only this single window W is being
28780 redisplayed. */
28781 if (!WINDOW_RIGHTMOST_P (w)
28782 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28783 {
28784 int x0, x1, y0, y1;
28785
28786 window_box_edges (w, &x0, &y0, &x1, &y1);
28787 y1 -= 1;
28788
28789 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28790 x1 -= 1;
28791
28792 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28793 }
28794 if (!WINDOW_LEFTMOST_P (w)
28795 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28796 {
28797 int x0, x1, y0, y1;
28798
28799 window_box_edges (w, &x0, &y0, &x1, &y1);
28800 y1 -= 1;
28801
28802 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28803 x0 -= 1;
28804
28805 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28806 }
28807 }
28808
28809
28810 /* Redraw the part of window W intersection rectangle FR. Pixel
28811 coordinates in FR are frame-relative. Call this function with
28812 input blocked. Value is non-zero if the exposure overwrites
28813 mouse-face. */
28814
28815 static int
28816 expose_window (struct window *w, XRectangle *fr)
28817 {
28818 struct frame *f = XFRAME (w->frame);
28819 XRectangle wr, r;
28820 int mouse_face_overwritten_p = 0;
28821
28822 /* If window is not yet fully initialized, do nothing. This can
28823 happen when toolkit scroll bars are used and a window is split.
28824 Reconfiguring the scroll bar will generate an expose for a newly
28825 created window. */
28826 if (w->current_matrix == NULL)
28827 return 0;
28828
28829 /* When we're currently updating the window, display and current
28830 matrix usually don't agree. Arrange for a thorough display
28831 later. */
28832 if (w->must_be_updated_p)
28833 {
28834 SET_FRAME_GARBAGED (f);
28835 return 0;
28836 }
28837
28838 /* Frame-relative pixel rectangle of W. */
28839 wr.x = WINDOW_LEFT_EDGE_X (w);
28840 wr.y = WINDOW_TOP_EDGE_Y (w);
28841 wr.width = WINDOW_TOTAL_WIDTH (w);
28842 wr.height = WINDOW_TOTAL_HEIGHT (w);
28843
28844 if (x_intersect_rectangles (fr, &wr, &r))
28845 {
28846 int yb = window_text_bottom_y (w);
28847 struct glyph_row *row;
28848 int cursor_cleared_p, phys_cursor_on_p;
28849 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28850
28851 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28852 r.x, r.y, r.width, r.height));
28853
28854 /* Convert to window coordinates. */
28855 r.x -= WINDOW_LEFT_EDGE_X (w);
28856 r.y -= WINDOW_TOP_EDGE_Y (w);
28857
28858 /* Turn off the cursor. */
28859 if (!w->pseudo_window_p
28860 && phys_cursor_in_rect_p (w, &r))
28861 {
28862 x_clear_cursor (w);
28863 cursor_cleared_p = 1;
28864 }
28865 else
28866 cursor_cleared_p = 0;
28867
28868 /* If the row containing the cursor extends face to end of line,
28869 then expose_area might overwrite the cursor outside the
28870 rectangle and thus notice_overwritten_cursor might clear
28871 w->phys_cursor_on_p. We remember the original value and
28872 check later if it is changed. */
28873 phys_cursor_on_p = w->phys_cursor_on_p;
28874
28875 /* Update lines intersecting rectangle R. */
28876 first_overlapping_row = last_overlapping_row = NULL;
28877 for (row = w->current_matrix->rows;
28878 row->enabled_p;
28879 ++row)
28880 {
28881 int y0 = row->y;
28882 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28883
28884 if ((y0 >= r.y && y0 < r.y + r.height)
28885 || (y1 > r.y && y1 < r.y + r.height)
28886 || (r.y >= y0 && r.y < y1)
28887 || (r.y + r.height > y0 && r.y + r.height < y1))
28888 {
28889 /* A header line may be overlapping, but there is no need
28890 to fix overlapping areas for them. KFS 2005-02-12 */
28891 if (row->overlapping_p && !row->mode_line_p)
28892 {
28893 if (first_overlapping_row == NULL)
28894 first_overlapping_row = row;
28895 last_overlapping_row = row;
28896 }
28897
28898 row->clip = fr;
28899 if (expose_line (w, row, &r))
28900 mouse_face_overwritten_p = 1;
28901 row->clip = NULL;
28902 }
28903 else if (row->overlapping_p)
28904 {
28905 /* We must redraw a row overlapping the exposed area. */
28906 if (y0 < r.y
28907 ? y0 + row->phys_height > r.y
28908 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28909 {
28910 if (first_overlapping_row == NULL)
28911 first_overlapping_row = row;
28912 last_overlapping_row = row;
28913 }
28914 }
28915
28916 if (y1 >= yb)
28917 break;
28918 }
28919
28920 /* Display the mode line if there is one. */
28921 if (WINDOW_WANTS_MODELINE_P (w)
28922 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28923 row->enabled_p)
28924 && row->y < r.y + r.height)
28925 {
28926 if (expose_line (w, row, &r))
28927 mouse_face_overwritten_p = 1;
28928 }
28929
28930 if (!w->pseudo_window_p)
28931 {
28932 /* Fix the display of overlapping rows. */
28933 if (first_overlapping_row)
28934 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28935 fr);
28936
28937 /* Draw border between windows. */
28938 x_draw_vertical_border (w);
28939
28940 /* Turn the cursor on again. */
28941 if (cursor_cleared_p
28942 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28943 update_window_cursor (w, 1);
28944 }
28945 }
28946
28947 return mouse_face_overwritten_p;
28948 }
28949
28950
28951
28952 /* Redraw (parts) of all windows in the window tree rooted at W that
28953 intersect R. R contains frame pixel coordinates. Value is
28954 non-zero if the exposure overwrites mouse-face. */
28955
28956 static int
28957 expose_window_tree (struct window *w, XRectangle *r)
28958 {
28959 struct frame *f = XFRAME (w->frame);
28960 int mouse_face_overwritten_p = 0;
28961
28962 while (w && !FRAME_GARBAGED_P (f))
28963 {
28964 if (WINDOWP (w->contents))
28965 mouse_face_overwritten_p
28966 |= expose_window_tree (XWINDOW (w->contents), r);
28967 else
28968 mouse_face_overwritten_p |= expose_window (w, r);
28969
28970 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28971 }
28972
28973 return mouse_face_overwritten_p;
28974 }
28975
28976
28977 /* EXPORT:
28978 Redisplay an exposed area of frame F. X and Y are the upper-left
28979 corner of the exposed rectangle. W and H are width and height of
28980 the exposed area. All are pixel values. W or H zero means redraw
28981 the entire frame. */
28982
28983 void
28984 expose_frame (struct frame *f, int x, int y, int w, int h)
28985 {
28986 XRectangle r;
28987 int mouse_face_overwritten_p = 0;
28988
28989 TRACE ((stderr, "expose_frame "));
28990
28991 /* No need to redraw if frame will be redrawn soon. */
28992 if (FRAME_GARBAGED_P (f))
28993 {
28994 TRACE ((stderr, " garbaged\n"));
28995 return;
28996 }
28997
28998 /* If basic faces haven't been realized yet, there is no point in
28999 trying to redraw anything. This can happen when we get an expose
29000 event while Emacs is starting, e.g. by moving another window. */
29001 if (FRAME_FACE_CACHE (f) == NULL
29002 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29003 {
29004 TRACE ((stderr, " no faces\n"));
29005 return;
29006 }
29007
29008 if (w == 0 || h == 0)
29009 {
29010 r.x = r.y = 0;
29011 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29012 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29013 }
29014 else
29015 {
29016 r.x = x;
29017 r.y = y;
29018 r.width = w;
29019 r.height = h;
29020 }
29021
29022 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29023 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29024
29025 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29026 if (WINDOWP (f->tool_bar_window))
29027 mouse_face_overwritten_p
29028 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29029 #endif
29030
29031 #ifdef HAVE_X_WINDOWS
29032 #ifndef MSDOS
29033 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29034 if (WINDOWP (f->menu_bar_window))
29035 mouse_face_overwritten_p
29036 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29037 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29038 #endif
29039 #endif
29040
29041 /* Some window managers support a focus-follows-mouse style with
29042 delayed raising of frames. Imagine a partially obscured frame,
29043 and moving the mouse into partially obscured mouse-face on that
29044 frame. The visible part of the mouse-face will be highlighted,
29045 then the WM raises the obscured frame. With at least one WM, KDE
29046 2.1, Emacs is not getting any event for the raising of the frame
29047 (even tried with SubstructureRedirectMask), only Expose events.
29048 These expose events will draw text normally, i.e. not
29049 highlighted. Which means we must redo the highlight here.
29050 Subsume it under ``we love X''. --gerd 2001-08-15 */
29051 /* Included in Windows version because Windows most likely does not
29052 do the right thing if any third party tool offers
29053 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29054 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29055 {
29056 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29057 if (f == hlinfo->mouse_face_mouse_frame)
29058 {
29059 int mouse_x = hlinfo->mouse_face_mouse_x;
29060 int mouse_y = hlinfo->mouse_face_mouse_y;
29061 clear_mouse_face (hlinfo);
29062 note_mouse_highlight (f, mouse_x, mouse_y);
29063 }
29064 }
29065 }
29066
29067
29068 /* EXPORT:
29069 Determine the intersection of two rectangles R1 and R2. Return
29070 the intersection in *RESULT. Value is non-zero if RESULT is not
29071 empty. */
29072
29073 int
29074 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29075 {
29076 XRectangle *left, *right;
29077 XRectangle *upper, *lower;
29078 int intersection_p = 0;
29079
29080 /* Rearrange so that R1 is the left-most rectangle. */
29081 if (r1->x < r2->x)
29082 left = r1, right = r2;
29083 else
29084 left = r2, right = r1;
29085
29086 /* X0 of the intersection is right.x0, if this is inside R1,
29087 otherwise there is no intersection. */
29088 if (right->x <= left->x + left->width)
29089 {
29090 result->x = right->x;
29091
29092 /* The right end of the intersection is the minimum of
29093 the right ends of left and right. */
29094 result->width = (min (left->x + left->width, right->x + right->width)
29095 - result->x);
29096
29097 /* Same game for Y. */
29098 if (r1->y < r2->y)
29099 upper = r1, lower = r2;
29100 else
29101 upper = r2, lower = r1;
29102
29103 /* The upper end of the intersection is lower.y0, if this is inside
29104 of upper. Otherwise, there is no intersection. */
29105 if (lower->y <= upper->y + upper->height)
29106 {
29107 result->y = lower->y;
29108
29109 /* The lower end of the intersection is the minimum of the lower
29110 ends of upper and lower. */
29111 result->height = (min (lower->y + lower->height,
29112 upper->y + upper->height)
29113 - result->y);
29114 intersection_p = 1;
29115 }
29116 }
29117
29118 return intersection_p;
29119 }
29120
29121 #endif /* HAVE_WINDOW_SYSTEM */
29122
29123 \f
29124 /***********************************************************************
29125 Initialization
29126 ***********************************************************************/
29127
29128 void
29129 syms_of_xdisp (void)
29130 {
29131 Vwith_echo_area_save_vector = Qnil;
29132 staticpro (&Vwith_echo_area_save_vector);
29133
29134 Vmessage_stack = Qnil;
29135 staticpro (&Vmessage_stack);
29136
29137 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29138 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29139
29140 message_dolog_marker1 = Fmake_marker ();
29141 staticpro (&message_dolog_marker1);
29142 message_dolog_marker2 = Fmake_marker ();
29143 staticpro (&message_dolog_marker2);
29144 message_dolog_marker3 = Fmake_marker ();
29145 staticpro (&message_dolog_marker3);
29146
29147 #ifdef GLYPH_DEBUG
29148 defsubr (&Sdump_frame_glyph_matrix);
29149 defsubr (&Sdump_glyph_matrix);
29150 defsubr (&Sdump_glyph_row);
29151 defsubr (&Sdump_tool_bar_row);
29152 defsubr (&Strace_redisplay);
29153 defsubr (&Strace_to_stderr);
29154 #endif
29155 #ifdef HAVE_WINDOW_SYSTEM
29156 defsubr (&Stool_bar_lines_needed);
29157 defsubr (&Slookup_image_map);
29158 #endif
29159 defsubr (&Sline_pixel_height);
29160 defsubr (&Sformat_mode_line);
29161 defsubr (&Sinvisible_p);
29162 defsubr (&Scurrent_bidi_paragraph_direction);
29163 defsubr (&Smove_point_visually);
29164
29165 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29166 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29167 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29168 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29169 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29170 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29171 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29172 DEFSYM (Qeval, "eval");
29173 DEFSYM (QCdata, ":data");
29174 DEFSYM (Qdisplay, "display");
29175 DEFSYM (Qspace_width, "space-width");
29176 DEFSYM (Qraise, "raise");
29177 DEFSYM (Qslice, "slice");
29178 DEFSYM (Qspace, "space");
29179 DEFSYM (Qmargin, "margin");
29180 DEFSYM (Qpointer, "pointer");
29181 DEFSYM (Qleft_margin, "left-margin");
29182 DEFSYM (Qright_margin, "right-margin");
29183 DEFSYM (Qcenter, "center");
29184 DEFSYM (Qline_height, "line-height");
29185 DEFSYM (QCalign_to, ":align-to");
29186 DEFSYM (QCrelative_width, ":relative-width");
29187 DEFSYM (QCrelative_height, ":relative-height");
29188 DEFSYM (QCeval, ":eval");
29189 DEFSYM (QCpropertize, ":propertize");
29190 DEFSYM (QCfile, ":file");
29191 DEFSYM (Qfontified, "fontified");
29192 DEFSYM (Qfontification_functions, "fontification-functions");
29193 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29194 DEFSYM (Qescape_glyph, "escape-glyph");
29195 DEFSYM (Qnobreak_space, "nobreak-space");
29196 DEFSYM (Qimage, "image");
29197 DEFSYM (Qtext, "text");
29198 DEFSYM (Qboth, "both");
29199 DEFSYM (Qboth_horiz, "both-horiz");
29200 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29201 DEFSYM (QCmap, ":map");
29202 DEFSYM (QCpointer, ":pointer");
29203 DEFSYM (Qrect, "rect");
29204 DEFSYM (Qcircle, "circle");
29205 DEFSYM (Qpoly, "poly");
29206 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29207 DEFSYM (Qgrow_only, "grow-only");
29208 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29209 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29210 DEFSYM (Qposition, "position");
29211 DEFSYM (Qbuffer_position, "buffer-position");
29212 DEFSYM (Qobject, "object");
29213 DEFSYM (Qbar, "bar");
29214 DEFSYM (Qhbar, "hbar");
29215 DEFSYM (Qbox, "box");
29216 DEFSYM (Qhollow, "hollow");
29217 DEFSYM (Qhand, "hand");
29218 DEFSYM (Qarrow, "arrow");
29219 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29220
29221 list_of_error = list1 (list2 (intern_c_string ("error"),
29222 intern_c_string ("void-variable")));
29223 staticpro (&list_of_error);
29224
29225 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29226 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29227 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29228 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29229
29230 echo_buffer[0] = echo_buffer[1] = Qnil;
29231 staticpro (&echo_buffer[0]);
29232 staticpro (&echo_buffer[1]);
29233
29234 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29235 staticpro (&echo_area_buffer[0]);
29236 staticpro (&echo_area_buffer[1]);
29237
29238 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29239 staticpro (&Vmessages_buffer_name);
29240
29241 mode_line_proptrans_alist = Qnil;
29242 staticpro (&mode_line_proptrans_alist);
29243 mode_line_string_list = Qnil;
29244 staticpro (&mode_line_string_list);
29245 mode_line_string_face = Qnil;
29246 staticpro (&mode_line_string_face);
29247 mode_line_string_face_prop = Qnil;
29248 staticpro (&mode_line_string_face_prop);
29249 Vmode_line_unwind_vector = Qnil;
29250 staticpro (&Vmode_line_unwind_vector);
29251
29252 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29253
29254 help_echo_string = Qnil;
29255 staticpro (&help_echo_string);
29256 help_echo_object = Qnil;
29257 staticpro (&help_echo_object);
29258 help_echo_window = Qnil;
29259 staticpro (&help_echo_window);
29260 previous_help_echo_string = Qnil;
29261 staticpro (&previous_help_echo_string);
29262 help_echo_pos = -1;
29263
29264 DEFSYM (Qright_to_left, "right-to-left");
29265 DEFSYM (Qleft_to_right, "left-to-right");
29266
29267 #ifdef HAVE_WINDOW_SYSTEM
29268 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29269 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29270 For example, if a block cursor is over a tab, it will be drawn as
29271 wide as that tab on the display. */);
29272 x_stretch_cursor_p = 0;
29273 #endif
29274
29275 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29276 doc: /* Non-nil means highlight trailing whitespace.
29277 The face used for trailing whitespace is `trailing-whitespace'. */);
29278 Vshow_trailing_whitespace = Qnil;
29279
29280 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29281 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29282 If the value is t, Emacs highlights non-ASCII chars which have the
29283 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29284 or `escape-glyph' face respectively.
29285
29286 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29287 U+2011 (non-breaking hyphen) are affected.
29288
29289 Any other non-nil value means to display these characters as a escape
29290 glyph followed by an ordinary space or hyphen.
29291
29292 A value of nil means no special handling of these characters. */);
29293 Vnobreak_char_display = Qt;
29294
29295 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29296 doc: /* The pointer shape to show in void text areas.
29297 A value of nil means to show the text pointer. Other options are `arrow',
29298 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29299 Vvoid_text_area_pointer = Qarrow;
29300
29301 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29302 doc: /* Non-nil means don't actually do any redisplay.
29303 This is used for internal purposes. */);
29304 Vinhibit_redisplay = Qnil;
29305
29306 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29307 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29308 Vglobal_mode_string = Qnil;
29309
29310 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29311 doc: /* Marker for where to display an arrow on top of the buffer text.
29312 This must be the beginning of a line in order to work.
29313 See also `overlay-arrow-string'. */);
29314 Voverlay_arrow_position = Qnil;
29315
29316 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29317 doc: /* String to display as an arrow in non-window frames.
29318 See also `overlay-arrow-position'. */);
29319 Voverlay_arrow_string = build_pure_c_string ("=>");
29320
29321 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29322 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29323 The symbols on this list are examined during redisplay to determine
29324 where to display overlay arrows. */);
29325 Voverlay_arrow_variable_list
29326 = list1 (intern_c_string ("overlay-arrow-position"));
29327
29328 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29329 doc: /* The number of lines to try scrolling a window by when point moves out.
29330 If that fails to bring point back on frame, point is centered instead.
29331 If this is zero, point is always centered after it moves off frame.
29332 If you want scrolling to always be a line at a time, you should set
29333 `scroll-conservatively' to a large value rather than set this to 1. */);
29334
29335 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29336 doc: /* Scroll up to this many lines, to bring point back on screen.
29337 If point moves off-screen, redisplay will scroll by up to
29338 `scroll-conservatively' lines in order to bring point just barely
29339 onto the screen again. If that cannot be done, then redisplay
29340 recenters point as usual.
29341
29342 If the value is greater than 100, redisplay will never recenter point,
29343 but will always scroll just enough text to bring point into view, even
29344 if you move far away.
29345
29346 A value of zero means always recenter point if it moves off screen. */);
29347 scroll_conservatively = 0;
29348
29349 DEFVAR_INT ("scroll-margin", scroll_margin,
29350 doc: /* Number of lines of margin at the top and bottom of a window.
29351 Recenter the window whenever point gets within this many lines
29352 of the top or bottom of the window. */);
29353 scroll_margin = 0;
29354
29355 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29356 doc: /* Pixels per inch value for non-window system displays.
29357 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29358 Vdisplay_pixels_per_inch = make_float (72.0);
29359
29360 #ifdef GLYPH_DEBUG
29361 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29362 #endif
29363
29364 DEFVAR_LISP ("truncate-partial-width-windows",
29365 Vtruncate_partial_width_windows,
29366 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29367 For an integer value, truncate lines in each window narrower than the
29368 full frame width, provided the window width is less than that integer;
29369 otherwise, respect the value of `truncate-lines'.
29370
29371 For any other non-nil value, truncate lines in all windows that do
29372 not span the full frame width.
29373
29374 A value of nil means to respect the value of `truncate-lines'.
29375
29376 If `word-wrap' is enabled, you might want to reduce this. */);
29377 Vtruncate_partial_width_windows = make_number (50);
29378
29379 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29380 doc: /* Maximum buffer size for which line number should be displayed.
29381 If the buffer is bigger than this, the line number does not appear
29382 in the mode line. A value of nil means no limit. */);
29383 Vline_number_display_limit = Qnil;
29384
29385 DEFVAR_INT ("line-number-display-limit-width",
29386 line_number_display_limit_width,
29387 doc: /* Maximum line width (in characters) for line number display.
29388 If the average length of the lines near point is bigger than this, then the
29389 line number may be omitted from the mode line. */);
29390 line_number_display_limit_width = 200;
29391
29392 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29393 doc: /* Non-nil means highlight region even in nonselected windows. */);
29394 highlight_nonselected_windows = 0;
29395
29396 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29397 doc: /* Non-nil if more than one frame is visible on this display.
29398 Minibuffer-only frames don't count, but iconified frames do.
29399 This variable is not guaranteed to be accurate except while processing
29400 `frame-title-format' and `icon-title-format'. */);
29401
29402 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29403 doc: /* Template for displaying the title bar of visible frames.
29404 \(Assuming the window manager supports this feature.)
29405
29406 This variable has the same structure as `mode-line-format', except that
29407 the %c and %l constructs are ignored. It is used only on frames for
29408 which no explicit name has been set \(see `modify-frame-parameters'). */);
29409
29410 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29411 doc: /* Template for displaying the title bar of an iconified frame.
29412 \(Assuming the window manager supports this feature.)
29413 This variable has the same structure as `mode-line-format' (which see),
29414 and is used only on frames for which no explicit name has been set
29415 \(see `modify-frame-parameters'). */);
29416 Vicon_title_format
29417 = Vframe_title_format
29418 = listn (CONSTYPE_PURE, 3,
29419 intern_c_string ("multiple-frames"),
29420 build_pure_c_string ("%b"),
29421 listn (CONSTYPE_PURE, 4,
29422 empty_unibyte_string,
29423 intern_c_string ("invocation-name"),
29424 build_pure_c_string ("@"),
29425 intern_c_string ("system-name")));
29426
29427 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29428 doc: /* Maximum number of lines to keep in the message log buffer.
29429 If nil, disable message logging. If t, log messages but don't truncate
29430 the buffer when it becomes large. */);
29431 Vmessage_log_max = make_number (1000);
29432
29433 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29434 doc: /* Functions called before redisplay, if window sizes have changed.
29435 The value should be a list of functions that take one argument.
29436 Just before redisplay, for each frame, if any of its windows have changed
29437 size since the last redisplay, or have been split or deleted,
29438 all the functions in the list are called, with the frame as argument. */);
29439 Vwindow_size_change_functions = Qnil;
29440
29441 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29442 doc: /* List of functions to call before redisplaying a window with scrolling.
29443 Each function is called with two arguments, the window and its new
29444 display-start position. Note that these functions are also called by
29445 `set-window-buffer'. Also note that the value of `window-end' is not
29446 valid when these functions are called.
29447
29448 Warning: Do not use this feature to alter the way the window
29449 is scrolled. It is not designed for that, and such use probably won't
29450 work. */);
29451 Vwindow_scroll_functions = Qnil;
29452
29453 DEFVAR_LISP ("window-text-change-functions",
29454 Vwindow_text_change_functions,
29455 doc: /* Functions to call in redisplay when text in the window might change. */);
29456 Vwindow_text_change_functions = Qnil;
29457
29458 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29459 doc: /* Functions called when redisplay of a window reaches the end trigger.
29460 Each function is called with two arguments, the window and the end trigger value.
29461 See `set-window-redisplay-end-trigger'. */);
29462 Vredisplay_end_trigger_functions = Qnil;
29463
29464 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29465 doc: /* Non-nil means autoselect window with mouse pointer.
29466 If nil, do not autoselect windows.
29467 A positive number means delay autoselection by that many seconds: a
29468 window is autoselected only after the mouse has remained in that
29469 window for the duration of the delay.
29470 A negative number has a similar effect, but causes windows to be
29471 autoselected only after the mouse has stopped moving. \(Because of
29472 the way Emacs compares mouse events, you will occasionally wait twice
29473 that time before the window gets selected.\)
29474 Any other value means to autoselect window instantaneously when the
29475 mouse pointer enters it.
29476
29477 Autoselection selects the minibuffer only if it is active, and never
29478 unselects the minibuffer if it is active.
29479
29480 When customizing this variable make sure that the actual value of
29481 `focus-follows-mouse' matches the behavior of your window manager. */);
29482 Vmouse_autoselect_window = Qnil;
29483
29484 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29485 doc: /* Non-nil means automatically resize tool-bars.
29486 This dynamically changes the tool-bar's height to the minimum height
29487 that is needed to make all tool-bar items visible.
29488 If value is `grow-only', the tool-bar's height is only increased
29489 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29490 Vauto_resize_tool_bars = Qt;
29491
29492 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29493 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29494 auto_raise_tool_bar_buttons_p = 1;
29495
29496 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29497 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29498 make_cursor_line_fully_visible_p = 1;
29499
29500 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29501 doc: /* Border below tool-bar in pixels.
29502 If an integer, use it as the height of the border.
29503 If it is one of `internal-border-width' or `border-width', use the
29504 value of the corresponding frame parameter.
29505 Otherwise, no border is added below the tool-bar. */);
29506 Vtool_bar_border = Qinternal_border_width;
29507
29508 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29509 doc: /* Margin around tool-bar buttons in pixels.
29510 If an integer, use that for both horizontal and vertical margins.
29511 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29512 HORZ specifying the horizontal margin, and VERT specifying the
29513 vertical margin. */);
29514 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29515
29516 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29517 doc: /* Relief thickness of tool-bar buttons. */);
29518 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29519
29520 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29521 doc: /* Tool bar style to use.
29522 It can be one of
29523 image - show images only
29524 text - show text only
29525 both - show both, text below image
29526 both-horiz - show text to the right of the image
29527 text-image-horiz - show text to the left of the image
29528 any other - use system default or image if no system default.
29529
29530 This variable only affects the GTK+ toolkit version of Emacs. */);
29531 Vtool_bar_style = Qnil;
29532
29533 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29534 doc: /* Maximum number of characters a label can have to be shown.
29535 The tool bar style must also show labels for this to have any effect, see
29536 `tool-bar-style'. */);
29537 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29538
29539 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29540 doc: /* List of functions to call to fontify regions of text.
29541 Each function is called with one argument POS. Functions must
29542 fontify a region starting at POS in the current buffer, and give
29543 fontified regions the property `fontified'. */);
29544 Vfontification_functions = Qnil;
29545 Fmake_variable_buffer_local (Qfontification_functions);
29546
29547 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29548 unibyte_display_via_language_environment,
29549 doc: /* Non-nil means display unibyte text according to language environment.
29550 Specifically, this means that raw bytes in the range 160-255 decimal
29551 are displayed by converting them to the equivalent multibyte characters
29552 according to the current language environment. As a result, they are
29553 displayed according to the current fontset.
29554
29555 Note that this variable affects only how these bytes are displayed,
29556 but does not change the fact they are interpreted as raw bytes. */);
29557 unibyte_display_via_language_environment = 0;
29558
29559 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29560 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29561 If a float, it specifies a fraction of the mini-window frame's height.
29562 If an integer, it specifies a number of lines. */);
29563 Vmax_mini_window_height = make_float (0.25);
29564
29565 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29566 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29567 A value of nil means don't automatically resize mini-windows.
29568 A value of t means resize them to fit the text displayed in them.
29569 A value of `grow-only', the default, means let mini-windows grow only;
29570 they return to their normal size when the minibuffer is closed, or the
29571 echo area becomes empty. */);
29572 Vresize_mini_windows = Qgrow_only;
29573
29574 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29575 doc: /* Alist specifying how to blink the cursor off.
29576 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29577 `cursor-type' frame-parameter or variable equals ON-STATE,
29578 comparing using `equal', Emacs uses OFF-STATE to specify
29579 how to blink it off. ON-STATE and OFF-STATE are values for
29580 the `cursor-type' frame parameter.
29581
29582 If a frame's ON-STATE has no entry in this list,
29583 the frame's other specifications determine how to blink the cursor off. */);
29584 Vblink_cursor_alist = Qnil;
29585
29586 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29587 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29588 If non-nil, windows are automatically scrolled horizontally to make
29589 point visible. */);
29590 automatic_hscrolling_p = 1;
29591 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29592
29593 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29594 doc: /* How many columns away from the window edge point is allowed to get
29595 before automatic hscrolling will horizontally scroll the window. */);
29596 hscroll_margin = 5;
29597
29598 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29599 doc: /* How many columns to scroll the window when point gets too close to the edge.
29600 When point is less than `hscroll-margin' columns from the window
29601 edge, automatic hscrolling will scroll the window by the amount of columns
29602 determined by this variable. If its value is a positive integer, scroll that
29603 many columns. If it's a positive floating-point number, it specifies the
29604 fraction of the window's width to scroll. If it's nil or zero, point will be
29605 centered horizontally after the scroll. Any other value, including negative
29606 numbers, are treated as if the value were zero.
29607
29608 Automatic hscrolling always moves point outside the scroll margin, so if
29609 point was more than scroll step columns inside the margin, the window will
29610 scroll more than the value given by the scroll step.
29611
29612 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29613 and `scroll-right' overrides this variable's effect. */);
29614 Vhscroll_step = make_number (0);
29615
29616 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29617 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29618 Bind this around calls to `message' to let it take effect. */);
29619 message_truncate_lines = 0;
29620
29621 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29622 doc: /* Normal hook run to update the menu bar definitions.
29623 Redisplay runs this hook before it redisplays the menu bar.
29624 This is used to update submenus such as Buffers,
29625 whose contents depend on various data. */);
29626 Vmenu_bar_update_hook = Qnil;
29627
29628 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29629 doc: /* Frame for which we are updating a menu.
29630 The enable predicate for a menu binding should check this variable. */);
29631 Vmenu_updating_frame = Qnil;
29632
29633 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29634 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29635 inhibit_menubar_update = 0;
29636
29637 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29638 doc: /* Prefix prepended to all continuation lines at display time.
29639 The value may be a string, an image, or a stretch-glyph; it is
29640 interpreted in the same way as the value of a `display' text property.
29641
29642 This variable is overridden by any `wrap-prefix' text or overlay
29643 property.
29644
29645 To add a prefix to non-continuation lines, use `line-prefix'. */);
29646 Vwrap_prefix = Qnil;
29647 DEFSYM (Qwrap_prefix, "wrap-prefix");
29648 Fmake_variable_buffer_local (Qwrap_prefix);
29649
29650 DEFVAR_LISP ("line-prefix", Vline_prefix,
29651 doc: /* Prefix prepended to all non-continuation lines at display time.
29652 The value may be a string, an image, or a stretch-glyph; it is
29653 interpreted in the same way as the value of a `display' text property.
29654
29655 This variable is overridden by any `line-prefix' text or overlay
29656 property.
29657
29658 To add a prefix to continuation lines, use `wrap-prefix'. */);
29659 Vline_prefix = Qnil;
29660 DEFSYM (Qline_prefix, "line-prefix");
29661 Fmake_variable_buffer_local (Qline_prefix);
29662
29663 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29664 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29665 inhibit_eval_during_redisplay = 0;
29666
29667 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29668 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29669 inhibit_free_realized_faces = 0;
29670
29671 #ifdef GLYPH_DEBUG
29672 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29673 doc: /* Inhibit try_window_id display optimization. */);
29674 inhibit_try_window_id = 0;
29675
29676 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29677 doc: /* Inhibit try_window_reusing display optimization. */);
29678 inhibit_try_window_reusing = 0;
29679
29680 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29681 doc: /* Inhibit try_cursor_movement display optimization. */);
29682 inhibit_try_cursor_movement = 0;
29683 #endif /* GLYPH_DEBUG */
29684
29685 DEFVAR_INT ("overline-margin", overline_margin,
29686 doc: /* Space between overline and text, in pixels.
29687 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29688 margin to the character height. */);
29689 overline_margin = 2;
29690
29691 DEFVAR_INT ("underline-minimum-offset",
29692 underline_minimum_offset,
29693 doc: /* Minimum distance between baseline and underline.
29694 This can improve legibility of underlined text at small font sizes,
29695 particularly when using variable `x-use-underline-position-properties'
29696 with fonts that specify an UNDERLINE_POSITION relatively close to the
29697 baseline. The default value is 1. */);
29698 underline_minimum_offset = 1;
29699
29700 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29701 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29702 This feature only works when on a window system that can change
29703 cursor shapes. */);
29704 display_hourglass_p = 1;
29705
29706 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29707 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29708 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29709
29710 #ifdef HAVE_WINDOW_SYSTEM
29711 hourglass_atimer = NULL;
29712 hourglass_shown_p = 0;
29713 #endif /* HAVE_WINDOW_SYSTEM */
29714
29715 DEFSYM (Qglyphless_char, "glyphless-char");
29716 DEFSYM (Qhex_code, "hex-code");
29717 DEFSYM (Qempty_box, "empty-box");
29718 DEFSYM (Qthin_space, "thin-space");
29719 DEFSYM (Qzero_width, "zero-width");
29720
29721 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
29722 doc: /* Function run just before redisplay.
29723 It is called with one argument, which is the set of windows that are to
29724 be redisplayed. This set can be nil (meaning, only the selected window),
29725 or t (meaning all windows). */);
29726 Vpre_redisplay_function = intern ("ignore");
29727
29728 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29729 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29730
29731 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29732 doc: /* Char-table defining glyphless characters.
29733 Each element, if non-nil, should be one of the following:
29734 an ASCII acronym string: display this string in a box
29735 `hex-code': display the hexadecimal code of a character in a box
29736 `empty-box': display as an empty box
29737 `thin-space': display as 1-pixel width space
29738 `zero-width': don't display
29739 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29740 display method for graphical terminals and text terminals respectively.
29741 GRAPHICAL and TEXT should each have one of the values listed above.
29742
29743 The char-table has one extra slot to control the display of a character for
29744 which no font is found. This slot only takes effect on graphical terminals.
29745 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29746 `thin-space'. The default is `empty-box'.
29747
29748 If a character has a non-nil entry in an active display table, the
29749 display table takes effect; in this case, Emacs does not consult
29750 `glyphless-char-display' at all. */);
29751 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29752 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29753 Qempty_box);
29754
29755 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29756 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29757 Vdebug_on_message = Qnil;
29758
29759 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
29760 doc: /* */);
29761 Vredisplay__all_windows_cause
29762 = Fmake_vector (make_number (100), make_number (0));
29763
29764 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
29765 doc: /* */);
29766 Vredisplay__mode_lines_cause
29767 = Fmake_vector (make_number (100), make_number (0));
29768 }
29769
29770
29771 /* Initialize this module when Emacs starts. */
29772
29773 void
29774 init_xdisp (void)
29775 {
29776 CHARPOS (this_line_start_pos) = 0;
29777
29778 if (!noninteractive)
29779 {
29780 struct window *m = XWINDOW (minibuf_window);
29781 Lisp_Object frame = m->frame;
29782 struct frame *f = XFRAME (frame);
29783 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29784 struct window *r = XWINDOW (root);
29785 int i;
29786
29787 echo_area_window = minibuf_window;
29788
29789 r->top_line = FRAME_TOP_MARGIN (f);
29790 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29791 r->total_cols = FRAME_COLS (f);
29792
29793 m->top_line = FRAME_LINES (f) - 1;
29794 m->total_lines = 1;
29795 m->total_cols = FRAME_COLS (f);
29796
29797 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29798 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29799 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29800
29801 /* The default ellipsis glyphs `...'. */
29802 for (i = 0; i < 3; ++i)
29803 default_invis_vector[i] = make_number ('.');
29804 }
29805
29806 {
29807 /* Allocate the buffer for frame titles.
29808 Also used for `format-mode-line'. */
29809 int size = 100;
29810 mode_line_noprop_buf = xmalloc (size);
29811 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29812 mode_line_noprop_ptr = mode_line_noprop_buf;
29813 mode_line_target = MODE_LINE_DISPLAY;
29814 }
29815
29816 help_echo_showing_p = 0;
29817 }
29818
29819 #ifdef HAVE_WINDOW_SYSTEM
29820
29821 /* Platform-independent portion of hourglass implementation. */
29822
29823 /* Cancel a currently active hourglass timer, and start a new one. */
29824 void
29825 start_hourglass (void)
29826 {
29827 struct timespec delay;
29828
29829 cancel_hourglass ();
29830
29831 if (INTEGERP (Vhourglass_delay)
29832 && XINT (Vhourglass_delay) > 0)
29833 delay = make_timespec (min (XINT (Vhourglass_delay),
29834 TYPE_MAXIMUM (time_t)),
29835 0);
29836 else if (FLOATP (Vhourglass_delay)
29837 && XFLOAT_DATA (Vhourglass_delay) > 0)
29838 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29839 else
29840 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29841
29842 #ifdef HAVE_NTGUI
29843 {
29844 extern void w32_note_current_window (void);
29845 w32_note_current_window ();
29846 }
29847 #endif /* HAVE_NTGUI */
29848
29849 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29850 show_hourglass, NULL);
29851 }
29852
29853
29854 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29855 shown. */
29856 void
29857 cancel_hourglass (void)
29858 {
29859 if (hourglass_atimer)
29860 {
29861 cancel_atimer (hourglass_atimer);
29862 hourglass_atimer = NULL;
29863 }
29864
29865 if (hourglass_shown_p)
29866 hide_hourglass ();
29867 }
29868
29869 #endif /* HAVE_WINDOW_SYSTEM */