]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Attempt to make redisplay more selective when changing fonts.
[gnu-emacs] / 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,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
102
103 . try_window
104
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
110
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
115
116 Desired matrices.
117
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
124
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
131
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
141
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
148
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
160
161 Frame matrices.
162
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
169
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
181
182 Bidirectional display.
183
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
196
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
205
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
224
225 Bidirectional display and character compositions
226
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
231
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
251
252 Moving an iterator in bidirectional text
253 without producing glyphs
254
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
273
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
277
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
302 #ifdef HAVE_WINDOW_SYSTEM
303 #include TERM_HEADER
304 #endif /* HAVE_WINDOW_SYSTEM */
305
306 #ifndef FRAME_X_OUTPUT
307 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
308 #endif
309
310 #define INFINITY 10000000
311
312 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
313 Lisp_Object Qwindow_scroll_functions;
314 static Lisp_Object Qwindow_text_change_functions;
315 static Lisp_Object Qredisplay_end_trigger_functions;
316 Lisp_Object Qinhibit_point_motion_hooks;
317 static Lisp_Object QCeval, QCpropertize;
318 Lisp_Object QCfile, QCdata;
319 static Lisp_Object Qfontified;
320 static Lisp_Object Qgrow_only;
321 static Lisp_Object Qinhibit_eval_during_redisplay;
322 static Lisp_Object Qbuffer_position, Qposition, Qobject;
323 static Lisp_Object Qright_to_left, Qleft_to_right;
324
325 /* Cursor shapes. */
326 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
327
328 /* Pointer shapes. */
329 static Lisp_Object Qarrow, Qhand;
330 Lisp_Object Qtext;
331
332 /* Holds the list (error). */
333 static Lisp_Object list_of_error;
334
335 static Lisp_Object Qfontification_functions;
336
337 static Lisp_Object Qwrap_prefix;
338 static Lisp_Object Qline_prefix;
339 static Lisp_Object Qredisplay_internal;
340
341 /* Non-nil means don't actually do any redisplay. */
342
343 Lisp_Object Qinhibit_redisplay;
344
345 /* Names of text properties relevant for redisplay. */
346
347 Lisp_Object Qdisplay;
348
349 Lisp_Object Qspace, QCalign_to;
350 static Lisp_Object QCrelative_width, QCrelative_height;
351 Lisp_Object Qleft_margin, Qright_margin;
352 static Lisp_Object Qspace_width, Qraise;
353 static Lisp_Object Qslice;
354 Lisp_Object Qcenter;
355 static Lisp_Object Qmargin, Qpointer;
356 static Lisp_Object Qline_height;
357
358 #ifdef HAVE_WINDOW_SYSTEM
359
360 /* Test if overflow newline into fringe. Called with iterator IT
361 at or past right window margin, and with IT->current_x set. */
362
363 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
364 (!NILP (Voverflow_newline_into_fringe) \
365 && FRAME_WINDOW_P ((IT)->f) \
366 && ((IT)->bidi_it.paragraph_dir == R2L \
367 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
368 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
369 && (IT)->current_x == (IT)->last_visible_x)
370
371 #else /* !HAVE_WINDOW_SYSTEM */
372 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
373 #endif /* HAVE_WINDOW_SYSTEM */
374
375 /* Test if the display element loaded in IT, or the underlying buffer
376 or string character, is a space or a TAB character. This is used
377 to determine where word wrapping can occur. */
378
379 #define IT_DISPLAYING_WHITESPACE(it) \
380 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
381 || ((STRINGP (it->string) \
382 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
383 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
384 || (it->s \
385 && (it->s[IT_BYTEPOS (*it)] == ' ' \
386 || it->s[IT_BYTEPOS (*it)] == '\t')) \
387 || (IT_BYTEPOS (*it) < ZV_BYTE \
388 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
389 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
390
391 /* Name of the face used to highlight trailing whitespace. */
392
393 static Lisp_Object Qtrailing_whitespace;
394
395 /* Name and number of the face used to highlight escape glyphs. */
396
397 static Lisp_Object Qescape_glyph;
398
399 /* Name and number of the face used to highlight non-breaking spaces. */
400
401 static Lisp_Object Qnobreak_space;
402
403 /* The symbol `image' which is the car of the lists used to represent
404 images in Lisp. Also a tool bar style. */
405
406 Lisp_Object Qimage;
407
408 /* The image map types. */
409 Lisp_Object QCmap;
410 static Lisp_Object QCpointer;
411 static Lisp_Object Qrect, Qcircle, Qpoly;
412
413 /* Tool bar styles */
414 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
415
416 /* Non-zero means print newline to stdout before next mini-buffer
417 message. */
418
419 int noninteractive_need_newline;
420
421 /* Non-zero means print newline to message log before next message. */
422
423 static int message_log_need_newline;
424
425 /* Three markers that message_dolog uses.
426 It could allocate them itself, but that causes trouble
427 in handling memory-full errors. */
428 static Lisp_Object message_dolog_marker1;
429 static Lisp_Object message_dolog_marker2;
430 static Lisp_Object message_dolog_marker3;
431 \f
432 /* The buffer position of the first character appearing entirely or
433 partially on the line of the selected window which contains the
434 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
435 redisplay optimization in redisplay_internal. */
436
437 static struct text_pos this_line_start_pos;
438
439 /* Number of characters past the end of the line above, including the
440 terminating newline. */
441
442 static struct text_pos this_line_end_pos;
443
444 /* The vertical positions and the height of this line. */
445
446 static int this_line_vpos;
447 static int this_line_y;
448 static int this_line_pixel_height;
449
450 /* X position at which this display line starts. Usually zero;
451 negative if first character is partially visible. */
452
453 static int this_line_start_x;
454
455 /* The smallest character position seen by move_it_* functions as they
456 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
457 hscrolled lines, see display_line. */
458
459 static struct text_pos this_line_min_pos;
460
461 /* Buffer that this_line_.* variables are referring to. */
462
463 static struct buffer *this_line_buffer;
464
465
466 /* Values of those variables at last redisplay are stored as
467 properties on `overlay-arrow-position' symbol. However, if
468 Voverlay_arrow_position is a marker, last-arrow-position is its
469 numerical position. */
470
471 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
472
473 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
474 properties on a symbol in overlay-arrow-variable-list. */
475
476 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
477
478 Lisp_Object Qmenu_bar_update_hook;
479
480 /* Nonzero if an overlay arrow has been displayed in this window. */
481
482 static int overlay_arrow_seen;
483
484 /* Vector containing glyphs for an ellipsis `...'. */
485
486 static Lisp_Object default_invis_vector[3];
487
488 /* This is the window where the echo area message was displayed. It
489 is always a mini-buffer window, but it may not be the same window
490 currently active as a mini-buffer. */
491
492 Lisp_Object echo_area_window;
493
494 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
495 pushes the current message and the value of
496 message_enable_multibyte on the stack, the function restore_message
497 pops the stack and displays MESSAGE again. */
498
499 static Lisp_Object Vmessage_stack;
500
501 /* Nonzero means multibyte characters were enabled when the echo area
502 message was specified. */
503
504 static int message_enable_multibyte;
505
506 /* Nonzero if we should redraw the mode lines on the next redisplay. */
507
508 int update_mode_lines;
509
510 /* Nonzero if window sizes or contents have changed since last
511 redisplay that finished. */
512
513 int windows_or_buffers_changed;
514
515 /* Nonzero means a frame's cursor type has been changed. */
516
517 static int cursor_type_changed;
518
519 /* Nonzero after display_mode_line if %l was used and it displayed a
520 line number. */
521
522 static int line_number_displayed;
523
524 /* The name of the *Messages* buffer, a string. */
525
526 static Lisp_Object Vmessages_buffer_name;
527
528 /* Current, index 0, and last displayed echo area message. Either
529 buffers from echo_buffers, or nil to indicate no message. */
530
531 Lisp_Object echo_area_buffer[2];
532
533 /* The buffers referenced from echo_area_buffer. */
534
535 static Lisp_Object echo_buffer[2];
536
537 /* A vector saved used in with_area_buffer to reduce consing. */
538
539 static Lisp_Object Vwith_echo_area_save_vector;
540
541 /* Non-zero means display_echo_area should display the last echo area
542 message again. Set by redisplay_preserve_echo_area. */
543
544 static int display_last_displayed_message_p;
545
546 /* Nonzero if echo area is being used by print; zero if being used by
547 message. */
548
549 static int message_buf_print;
550
551 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
552
553 static Lisp_Object Qinhibit_menubar_update;
554 static Lisp_Object Qmessage_truncate_lines;
555
556 /* Set to 1 in clear_message to make redisplay_internal aware
557 of an emptied echo area. */
558
559 static int message_cleared_p;
560
561 /* A scratch glyph row with contents used for generating truncation
562 glyphs. Also used in direct_output_for_insert. */
563
564 #define MAX_SCRATCH_GLYPHS 100
565 static struct glyph_row scratch_glyph_row;
566 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
567
568 /* Ascent and height of the last line processed by move_it_to. */
569
570 static int last_height;
571
572 /* Non-zero if there's a help-echo in the echo area. */
573
574 int help_echo_showing_p;
575
576 /* The maximum distance to look ahead for text properties. Values
577 that are too small let us call compute_char_face and similar
578 functions too often which is expensive. Values that are too large
579 let us call compute_char_face and alike too often because we
580 might not be interested in text properties that far away. */
581
582 #define TEXT_PROP_DISTANCE_LIMIT 100
583
584 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
585 iterator state and later restore it. This is needed because the
586 bidi iterator on bidi.c keeps a stacked cache of its states, which
587 is really a singleton. When we use scratch iterator objects to
588 move around the buffer, we can cause the bidi cache to be pushed or
589 popped, and therefore we need to restore the cache state when we
590 return to the original iterator. */
591 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
592 do { \
593 if (CACHE) \
594 bidi_unshelve_cache (CACHE, 1); \
595 ITCOPY = ITORIG; \
596 CACHE = bidi_shelve_cache (); \
597 } while (0)
598
599 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
600 do { \
601 if (pITORIG != pITCOPY) \
602 *(pITORIG) = *(pITCOPY); \
603 bidi_unshelve_cache (CACHE, 0); \
604 CACHE = NULL; \
605 } while (0)
606
607 #ifdef GLYPH_DEBUG
608
609 /* Non-zero means print traces of redisplay if compiled with
610 GLYPH_DEBUG defined. */
611
612 int trace_redisplay_p;
613
614 #endif /* GLYPH_DEBUG */
615
616 #ifdef DEBUG_TRACE_MOVE
617 /* Non-zero means trace with TRACE_MOVE to stderr. */
618 int trace_move;
619
620 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
621 #else
622 #define TRACE_MOVE(x) (void) 0
623 #endif
624
625 static Lisp_Object Qauto_hscroll_mode;
626
627 /* Buffer being redisplayed -- for redisplay_window_error. */
628
629 static struct buffer *displayed_buffer;
630
631 /* Value returned from text property handlers (see below). */
632
633 enum prop_handled
634 {
635 HANDLED_NORMALLY,
636 HANDLED_RECOMPUTE_PROPS,
637 HANDLED_OVERLAY_STRING_CONSUMED,
638 HANDLED_RETURN
639 };
640
641 /* A description of text properties that redisplay is interested
642 in. */
643
644 struct props
645 {
646 /* The name of the property. */
647 Lisp_Object *name;
648
649 /* A unique index for the property. */
650 enum prop_idx idx;
651
652 /* A handler function called to set up iterator IT from the property
653 at IT's current position. Value is used to steer handle_stop. */
654 enum prop_handled (*handler) (struct it *it);
655 };
656
657 static enum prop_handled handle_face_prop (struct it *);
658 static enum prop_handled handle_invisible_prop (struct it *);
659 static enum prop_handled handle_display_prop (struct it *);
660 static enum prop_handled handle_composition_prop (struct it *);
661 static enum prop_handled handle_overlay_change (struct it *);
662 static enum prop_handled handle_fontified_prop (struct it *);
663
664 /* Properties handled by iterators. */
665
666 static struct props it_props[] =
667 {
668 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
669 /* Handle `face' before `display' because some sub-properties of
670 `display' need to know the face. */
671 {&Qface, FACE_PROP_IDX, handle_face_prop},
672 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
673 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
674 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
675 {NULL, 0, NULL}
676 };
677
678 /* Value is the position described by X. If X is a marker, value is
679 the marker_position of X. Otherwise, value is X. */
680
681 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
682
683 /* Enumeration returned by some move_it_.* functions internally. */
684
685 enum move_it_result
686 {
687 /* Not used. Undefined value. */
688 MOVE_UNDEFINED,
689
690 /* Move ended at the requested buffer position or ZV. */
691 MOVE_POS_MATCH_OR_ZV,
692
693 /* Move ended at the requested X pixel position. */
694 MOVE_X_REACHED,
695
696 /* Move within a line ended at the end of a line that must be
697 continued. */
698 MOVE_LINE_CONTINUED,
699
700 /* Move within a line ended at the end of a line that would
701 be displayed truncated. */
702 MOVE_LINE_TRUNCATED,
703
704 /* Move within a line ended at a line end. */
705 MOVE_NEWLINE_OR_CR
706 };
707
708 /* This counter is used to clear the face cache every once in a while
709 in redisplay_internal. It is incremented for each redisplay.
710 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
711 cleared. */
712
713 #define CLEAR_FACE_CACHE_COUNT 500
714 static int clear_face_cache_count;
715
716 /* Similarly for the image cache. */
717
718 #ifdef HAVE_WINDOW_SYSTEM
719 #define CLEAR_IMAGE_CACHE_COUNT 101
720 static int clear_image_cache_count;
721
722 /* Null glyph slice */
723 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
724 #endif
725
726 /* True while redisplay_internal is in progress. */
727
728 bool redisplaying_p;
729
730 static Lisp_Object Qinhibit_free_realized_faces;
731 static Lisp_Object Qmode_line_default_help_echo;
732
733 /* If a string, XTread_socket generates an event to display that string.
734 (The display is done in read_char.) */
735
736 Lisp_Object help_echo_string;
737 Lisp_Object help_echo_window;
738 Lisp_Object help_echo_object;
739 ptrdiff_t help_echo_pos;
740
741 /* Temporary variable for XTread_socket. */
742
743 Lisp_Object previous_help_echo_string;
744
745 /* Platform-independent portion of hourglass implementation. */
746
747 #ifdef HAVE_WINDOW_SYSTEM
748
749 /* Non-zero means an hourglass cursor is currently shown. */
750 int hourglass_shown_p;
751
752 /* If non-null, an asynchronous timer that, when it expires, displays
753 an hourglass cursor on all frames. */
754 struct atimer *hourglass_atimer;
755
756 #endif /* HAVE_WINDOW_SYSTEM */
757
758 /* Name of the face used to display glyphless characters. */
759 Lisp_Object Qglyphless_char;
760
761 /* Symbol for the purpose of Vglyphless_char_display. */
762 static Lisp_Object Qglyphless_char_display;
763
764 /* Method symbols for Vglyphless_char_display. */
765 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
766
767 /* Default number of seconds to wait before displaying an hourglass
768 cursor. */
769 #define DEFAULT_HOURGLASS_DELAY 1
770
771 #ifdef HAVE_WINDOW_SYSTEM
772
773 /* Default pixel width of `thin-space' display method. */
774 #define THIN_SPACE_WIDTH 1
775
776 #endif /* HAVE_WINDOW_SYSTEM */
777
778 /* Function prototypes. */
779
780 static void setup_for_ellipsis (struct it *, int);
781 static void set_iterator_to_next (struct it *, int);
782 static void mark_window_display_accurate_1 (struct window *, int);
783 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
784 static int display_prop_string_p (Lisp_Object, Lisp_Object);
785 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
786 static int cursor_row_p (struct glyph_row *);
787 static int redisplay_mode_lines (Lisp_Object, int);
788 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
789
790 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
791
792 static void handle_line_prefix (struct it *);
793
794 static void pint2str (char *, int, ptrdiff_t);
795 static void pint2hrstr (char *, int, ptrdiff_t);
796 static struct text_pos run_window_scroll_functions (Lisp_Object,
797 struct text_pos);
798 static int text_outside_line_unchanged_p (struct window *,
799 ptrdiff_t, ptrdiff_t);
800 static void store_mode_line_noprop_char (char);
801 static int store_mode_line_noprop (const char *, int, int);
802 static void handle_stop (struct it *);
803 static void handle_stop_backwards (struct it *, ptrdiff_t);
804 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
805 static void ensure_echo_area_buffers (void);
806 static void unwind_with_echo_area_buffer (Lisp_Object);
807 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
808 static int with_echo_area_buffer (struct window *, int,
809 int (*) (ptrdiff_t, Lisp_Object),
810 ptrdiff_t, Lisp_Object);
811 static void clear_garbaged_frames (void);
812 static int current_message_1 (ptrdiff_t, Lisp_Object);
813 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
814 static void set_message (Lisp_Object);
815 static int set_message_1 (ptrdiff_t, Lisp_Object);
816 static int display_echo_area (struct window *);
817 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
818 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
819 static void unwind_redisplay (void);
820 static int string_char_and_length (const unsigned char *, int *);
821 static struct text_pos display_prop_end (struct it *, Lisp_Object,
822 struct text_pos);
823 static int compute_window_start_on_continuation_line (struct window *);
824 static void insert_left_trunc_glyphs (struct it *);
825 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
826 Lisp_Object);
827 static void extend_face_to_end_of_line (struct it *);
828 static int append_space_for_newline (struct it *, int);
829 static int cursor_row_fully_visible_p (struct window *, int, int);
830 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
831 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
832 static int trailing_whitespace_p (ptrdiff_t);
833 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
834 static void push_it (struct it *, struct text_pos *);
835 static void iterate_out_of_display_property (struct it *);
836 static void pop_it (struct it *);
837 static void sync_frame_with_window_matrix_rows (struct window *);
838 static void redisplay_internal (void);
839 static int echo_area_display (int);
840 static void redisplay_windows (Lisp_Object);
841 static void redisplay_window (Lisp_Object, int);
842 static Lisp_Object redisplay_window_error (Lisp_Object);
843 static Lisp_Object redisplay_window_0 (Lisp_Object);
844 static Lisp_Object redisplay_window_1 (Lisp_Object);
845 static int set_cursor_from_row (struct window *, struct glyph_row *,
846 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
847 int, int);
848 static int update_menu_bar (struct frame *, int, int);
849 static int try_window_reusing_current_matrix (struct window *);
850 static int try_window_id (struct window *);
851 static int display_line (struct it *);
852 static int display_mode_lines (struct window *);
853 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
854 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
855 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
856 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
857 static void display_menu_bar (struct window *);
858 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
859 ptrdiff_t *);
860 static int display_string (const char *, Lisp_Object, Lisp_Object,
861 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
862 static void compute_line_metrics (struct it *);
863 static void run_redisplay_end_trigger_hook (struct it *);
864 static int get_overlay_strings (struct it *, ptrdiff_t);
865 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
866 static void next_overlay_string (struct it *);
867 static void reseat (struct it *, struct text_pos, int);
868 static void reseat_1 (struct it *, struct text_pos, int);
869 static void back_to_previous_visible_line_start (struct it *);
870 static void reseat_at_next_visible_line_start (struct it *, int);
871 static int next_element_from_ellipsis (struct it *);
872 static int next_element_from_display_vector (struct it *);
873 static int next_element_from_string (struct it *);
874 static int next_element_from_c_string (struct it *);
875 static int next_element_from_buffer (struct it *);
876 static int next_element_from_composition (struct it *);
877 static int next_element_from_image (struct it *);
878 static int next_element_from_stretch (struct it *);
879 static void load_overlay_strings (struct it *, ptrdiff_t);
880 static int init_from_display_pos (struct it *, struct window *,
881 struct display_pos *);
882 static void reseat_to_string (struct it *, const char *,
883 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
884 static int get_next_display_element (struct it *);
885 static enum move_it_result
886 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
887 enum move_operation_enum);
888 static void get_visually_first_element (struct it *);
889 static void init_to_row_start (struct it *, struct window *,
890 struct glyph_row *);
891 static int init_to_row_end (struct it *, struct window *,
892 struct glyph_row *);
893 static void back_to_previous_line_start (struct it *);
894 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
895 static struct text_pos string_pos_nchars_ahead (struct text_pos,
896 Lisp_Object, ptrdiff_t);
897 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
898 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
899 static ptrdiff_t number_of_chars (const char *, bool);
900 static void compute_stop_pos (struct it *);
901 static void compute_string_pos (struct text_pos *, struct text_pos,
902 Lisp_Object);
903 static int face_before_or_after_it_pos (struct it *, int);
904 static ptrdiff_t next_overlay_change (ptrdiff_t);
905 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
906 Lisp_Object, struct text_pos *, ptrdiff_t, int);
907 static int handle_single_display_spec (struct it *, Lisp_Object,
908 Lisp_Object, Lisp_Object,
909 struct text_pos *, ptrdiff_t, int, int);
910 static int underlying_face_id (struct it *);
911 static int in_ellipses_for_invisible_text_p (struct display_pos *,
912 struct window *);
913
914 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
915 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
916
917 #ifdef HAVE_WINDOW_SYSTEM
918
919 static void x_consider_frame_title (Lisp_Object);
920 static int tool_bar_lines_needed (struct frame *, int *);
921 static void update_tool_bar (struct frame *, int);
922 static void build_desired_tool_bar_string (struct frame *f);
923 static int redisplay_tool_bar (struct frame *);
924 static void display_tool_bar_line (struct it *, int);
925 static void notice_overwritten_cursor (struct window *,
926 enum glyph_row_area,
927 int, int, int, int);
928 static void append_stretch_glyph (struct it *, Lisp_Object,
929 int, int, int);
930
931
932 #endif /* HAVE_WINDOW_SYSTEM */
933
934 static void produce_special_glyphs (struct it *, enum display_element_type);
935 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
936 static int coords_in_mouse_face_p (struct window *, int, int);
937
938
939 \f
940 /***********************************************************************
941 Window display dimensions
942 ***********************************************************************/
943
944 /* Return the bottom boundary y-position for text lines in window W.
945 This is the first y position at which a line cannot start.
946 It is relative to the top of the window.
947
948 This is the height of W minus the height of a mode line, if any. */
949
950 int
951 window_text_bottom_y (struct window *w)
952 {
953 int height = WINDOW_TOTAL_HEIGHT (w);
954
955 if (WINDOW_WANTS_MODELINE_P (w))
956 height -= CURRENT_MODE_LINE_HEIGHT (w);
957 return height;
958 }
959
960 /* Return the pixel width of display area AREA of window W.
961 ANY_AREA means return the total width of W, not including
962 fringes to the left and right of the window. */
963
964 int
965 window_box_width (struct window *w, enum glyph_row_area area)
966 {
967 int cols = w->total_cols;
968 int pixels = 0;
969
970 if (!w->pseudo_window_p)
971 {
972 cols -= WINDOW_SCROLL_BAR_COLS (w);
973
974 if (area == TEXT_AREA)
975 {
976 cols -= max (0, w->left_margin_cols);
977 cols -= max (0, w->right_margin_cols);
978 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
979 }
980 else if (area == LEFT_MARGIN_AREA)
981 {
982 cols = max (0, w->left_margin_cols);
983 pixels = 0;
984 }
985 else if (area == RIGHT_MARGIN_AREA)
986 {
987 cols = max (0, w->right_margin_cols);
988 pixels = 0;
989 }
990 }
991
992 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
993 }
994
995
996 /* Return the pixel height of the display area of window W, not
997 including mode lines of W, if any. */
998
999 int
1000 window_box_height (struct window *w)
1001 {
1002 struct frame *f = XFRAME (w->frame);
1003 int height = WINDOW_TOTAL_HEIGHT (w);
1004
1005 eassert (height >= 0);
1006
1007 /* Note: the code below that determines the mode-line/header-line
1008 height is essentially the same as that contained in the macro
1009 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1010 the appropriate glyph row has its `mode_line_p' flag set,
1011 and if it doesn't, uses estimate_mode_line_height instead. */
1012
1013 if (WINDOW_WANTS_MODELINE_P (w))
1014 {
1015 struct glyph_row *ml_row
1016 = (w->current_matrix && w->current_matrix->rows
1017 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1018 : 0);
1019 if (ml_row && ml_row->mode_line_p)
1020 height -= ml_row->height;
1021 else
1022 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1023 }
1024
1025 if (WINDOW_WANTS_HEADER_LINE_P (w))
1026 {
1027 struct glyph_row *hl_row
1028 = (w->current_matrix && w->current_matrix->rows
1029 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1030 : 0);
1031 if (hl_row && hl_row->mode_line_p)
1032 height -= hl_row->height;
1033 else
1034 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1035 }
1036
1037 /* With a very small font and a mode-line that's taller than
1038 default, we might end up with a negative height. */
1039 return max (0, height);
1040 }
1041
1042 /* Return the window-relative coordinate of the left edge of display
1043 area AREA of window W. ANY_AREA means return the left edge of the
1044 whole window, to the right of the left fringe of W. */
1045
1046 int
1047 window_box_left_offset (struct window *w, enum glyph_row_area area)
1048 {
1049 int x;
1050
1051 if (w->pseudo_window_p)
1052 return 0;
1053
1054 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1055
1056 if (area == TEXT_AREA)
1057 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1058 + window_box_width (w, LEFT_MARGIN_AREA));
1059 else if (area == RIGHT_MARGIN_AREA)
1060 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1061 + window_box_width (w, LEFT_MARGIN_AREA)
1062 + window_box_width (w, TEXT_AREA)
1063 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1064 ? 0
1065 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1066 else if (area == LEFT_MARGIN_AREA
1067 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1068 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1069
1070 return x;
1071 }
1072
1073
1074 /* Return the window-relative coordinate of the right edge of display
1075 area AREA of window W. ANY_AREA means return the right edge of the
1076 whole window, to the left of the right fringe of W. */
1077
1078 int
1079 window_box_right_offset (struct window *w, enum glyph_row_area area)
1080 {
1081 return window_box_left_offset (w, area) + window_box_width (w, area);
1082 }
1083
1084 /* Return the frame-relative coordinate of the left edge of display
1085 area AREA of window W. ANY_AREA means return the left edge of the
1086 whole window, to the right of the left fringe of W. */
1087
1088 int
1089 window_box_left (struct window *w, enum glyph_row_area area)
1090 {
1091 struct frame *f = XFRAME (w->frame);
1092 int x;
1093
1094 if (w->pseudo_window_p)
1095 return FRAME_INTERNAL_BORDER_WIDTH (f);
1096
1097 x = (WINDOW_LEFT_EDGE_X (w)
1098 + window_box_left_offset (w, area));
1099
1100 return x;
1101 }
1102
1103
1104 /* Return the frame-relative coordinate of the right edge of display
1105 area AREA of window W. ANY_AREA means return the right edge of the
1106 whole window, to the left of the right fringe of W. */
1107
1108 int
1109 window_box_right (struct window *w, enum glyph_row_area area)
1110 {
1111 return window_box_left (w, area) + window_box_width (w, area);
1112 }
1113
1114 /* Get the bounding box of the display area AREA of window W, without
1115 mode lines, in frame-relative coordinates. ANY_AREA means the
1116 whole window, not including the left and right fringes of
1117 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1118 coordinates of the upper-left corner of the box. Return in
1119 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1120
1121 void
1122 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1123 int *box_y, int *box_width, int *box_height)
1124 {
1125 if (box_width)
1126 *box_width = window_box_width (w, area);
1127 if (box_height)
1128 *box_height = window_box_height (w);
1129 if (box_x)
1130 *box_x = window_box_left (w, area);
1131 if (box_y)
1132 {
1133 *box_y = WINDOW_TOP_EDGE_Y (w);
1134 if (WINDOW_WANTS_HEADER_LINE_P (w))
1135 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1136 }
1137 }
1138
1139 #ifdef HAVE_WINDOW_SYSTEM
1140
1141 /* Get the bounding box of the display area AREA of window W, without
1142 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1143 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1144 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1145 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1146 box. */
1147
1148 static void
1149 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1150 int *bottom_right_x, int *bottom_right_y)
1151 {
1152 window_box (w, ANY_AREA, top_left_x, top_left_y,
1153 bottom_right_x, bottom_right_y);
1154 *bottom_right_x += *top_left_x;
1155 *bottom_right_y += *top_left_y;
1156 }
1157
1158 #endif /* HAVE_WINDOW_SYSTEM */
1159
1160 /***********************************************************************
1161 Utilities
1162 ***********************************************************************/
1163
1164 /* Return the bottom y-position of the line the iterator IT is in.
1165 This can modify IT's settings. */
1166
1167 int
1168 line_bottom_y (struct it *it)
1169 {
1170 int line_height = it->max_ascent + it->max_descent;
1171 int line_top_y = it->current_y;
1172
1173 if (line_height == 0)
1174 {
1175 if (last_height)
1176 line_height = last_height;
1177 else if (IT_CHARPOS (*it) < ZV)
1178 {
1179 move_it_by_lines (it, 1);
1180 line_height = (it->max_ascent || it->max_descent
1181 ? it->max_ascent + it->max_descent
1182 : last_height);
1183 }
1184 else
1185 {
1186 struct glyph_row *row = it->glyph_row;
1187
1188 /* Use the default character height. */
1189 it->glyph_row = NULL;
1190 it->what = IT_CHARACTER;
1191 it->c = ' ';
1192 it->len = 1;
1193 PRODUCE_GLYPHS (it);
1194 line_height = it->ascent + it->descent;
1195 it->glyph_row = row;
1196 }
1197 }
1198
1199 return line_top_y + line_height;
1200 }
1201
1202 DEFUN ("line-pixel-height", Fline_pixel_height,
1203 Sline_pixel_height, 0, 0, 0,
1204 doc: /* Return height in pixels of text line in the selected window.
1205
1206 Value is the height in pixels of the line at point. */)
1207 (void)
1208 {
1209 struct it it;
1210 struct text_pos pt;
1211 struct window *w = XWINDOW (selected_window);
1212
1213 SET_TEXT_POS (pt, PT, PT_BYTE);
1214 start_display (&it, w, pt);
1215 it.vpos = it.current_y = 0;
1216 last_height = 0;
1217 return make_number (line_bottom_y (&it));
1218 }
1219
1220 /* Return the default pixel height of text lines in window W. The
1221 value is the canonical height of the W frame's default font, plus
1222 any extra space required by the line-spacing variable or frame
1223 parameter.
1224
1225 Implementation note: this ignores any line-spacing text properties
1226 put on the newline characters. This is because those properties
1227 only affect the _screen_ line ending in the newline (i.e., in a
1228 continued line, only the last screen line will be affected), which
1229 means only a small number of lines in a buffer can ever use this
1230 feature. Since this function is used to compute the default pixel
1231 equivalent of text lines in a window, we can safely ignore those
1232 few lines. For the same reasons, we ignore the line-height
1233 properties. */
1234 int
1235 default_line_pixel_height (struct window *w)
1236 {
1237 struct frame *f = WINDOW_XFRAME (w);
1238 int height = FRAME_LINE_HEIGHT (f);
1239
1240 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1241 {
1242 struct buffer *b = XBUFFER (w->contents);
1243 Lisp_Object val = BVAR (b, extra_line_spacing);
1244
1245 if (NILP (val))
1246 val = BVAR (&buffer_defaults, extra_line_spacing);
1247 if (!NILP (val))
1248 {
1249 if (RANGED_INTEGERP (0, val, INT_MAX))
1250 height += XFASTINT (val);
1251 else if (FLOATP (val))
1252 {
1253 int addon = XFLOAT_DATA (val) * height + 0.5;
1254
1255 if (addon >= 0)
1256 height += addon;
1257 }
1258 }
1259 else
1260 height += f->extra_line_spacing;
1261 }
1262
1263 return height;
1264 }
1265
1266 /* Subroutine of pos_visible_p below. Extracts a display string, if
1267 any, from the display spec given as its argument. */
1268 static Lisp_Object
1269 string_from_display_spec (Lisp_Object spec)
1270 {
1271 if (CONSP (spec))
1272 {
1273 while (CONSP (spec))
1274 {
1275 if (STRINGP (XCAR (spec)))
1276 return XCAR (spec);
1277 spec = XCDR (spec);
1278 }
1279 }
1280 else if (VECTORP (spec))
1281 {
1282 ptrdiff_t i;
1283
1284 for (i = 0; i < ASIZE (spec); i++)
1285 {
1286 if (STRINGP (AREF (spec, i)))
1287 return AREF (spec, i);
1288 }
1289 return Qnil;
1290 }
1291
1292 return spec;
1293 }
1294
1295
1296 /* Limit insanely large values of W->hscroll on frame F to the largest
1297 value that will still prevent first_visible_x and last_visible_x of
1298 'struct it' from overflowing an int. */
1299 static int
1300 window_hscroll_limited (struct window *w, struct frame *f)
1301 {
1302 ptrdiff_t window_hscroll = w->hscroll;
1303 int window_text_width = window_box_width (w, TEXT_AREA);
1304 int colwidth = FRAME_COLUMN_WIDTH (f);
1305
1306 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1307 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1308
1309 return window_hscroll;
1310 }
1311
1312 /* Return 1 if position CHARPOS is visible in window W.
1313 CHARPOS < 0 means return info about WINDOW_END position.
1314 If visible, set *X and *Y to pixel coordinates of top left corner.
1315 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1316 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1317
1318 int
1319 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1320 int *rtop, int *rbot, int *rowh, int *vpos)
1321 {
1322 struct it it;
1323 void *itdata = bidi_shelve_cache ();
1324 struct text_pos top;
1325 int visible_p = 0;
1326 struct buffer *old_buffer = NULL;
1327
1328 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1329 return visible_p;
1330
1331 if (XBUFFER (w->contents) != current_buffer)
1332 {
1333 old_buffer = current_buffer;
1334 set_buffer_internal_1 (XBUFFER (w->contents));
1335 }
1336
1337 SET_TEXT_POS_FROM_MARKER (top, w->start);
1338 /* Scrolling a minibuffer window via scroll bar when the echo area
1339 shows long text sometimes resets the minibuffer contents behind
1340 our backs. */
1341 if (CHARPOS (top) > ZV)
1342 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1343
1344 /* Compute exact mode line heights. */
1345 if (WINDOW_WANTS_MODELINE_P (w))
1346 w->mode_line_height
1347 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1348 BVAR (current_buffer, mode_line_format));
1349
1350 if (WINDOW_WANTS_HEADER_LINE_P (w))
1351 w->header_line_height
1352 = display_mode_line (w, HEADER_LINE_FACE_ID,
1353 BVAR (current_buffer, header_line_format));
1354
1355 start_display (&it, w, top);
1356 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1357 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1358
1359 if (charpos >= 0
1360 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1361 && IT_CHARPOS (it) >= charpos)
1362 /* When scanning backwards under bidi iteration, move_it_to
1363 stops at or _before_ CHARPOS, because it stops at or to
1364 the _right_ of the character at CHARPOS. */
1365 || (it.bidi_p && it.bidi_it.scan_dir == -1
1366 && IT_CHARPOS (it) <= charpos)))
1367 {
1368 /* We have reached CHARPOS, or passed it. How the call to
1369 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1370 or covered by a display property, move_it_to stops at the end
1371 of the invisible text, to the right of CHARPOS. (ii) If
1372 CHARPOS is in a display vector, move_it_to stops on its last
1373 glyph. */
1374 int top_x = it.current_x;
1375 int top_y = it.current_y;
1376 /* Calling line_bottom_y may change it.method, it.position, etc. */
1377 enum it_method it_method = it.method;
1378 int bottom_y = (last_height = 0, line_bottom_y (&it));
1379 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1380
1381 if (top_y < window_top_y)
1382 visible_p = bottom_y > window_top_y;
1383 else if (top_y < it.last_visible_y)
1384 visible_p = 1;
1385 if (bottom_y >= it.last_visible_y
1386 && it.bidi_p && it.bidi_it.scan_dir == -1
1387 && IT_CHARPOS (it) < charpos)
1388 {
1389 /* When the last line of the window is scanned backwards
1390 under bidi iteration, we could be duped into thinking
1391 that we have passed CHARPOS, when in fact move_it_to
1392 simply stopped short of CHARPOS because it reached
1393 last_visible_y. To see if that's what happened, we call
1394 move_it_to again with a slightly larger vertical limit,
1395 and see if it actually moved vertically; if it did, we
1396 didn't really reach CHARPOS, which is beyond window end. */
1397 struct it save_it = it;
1398 /* Why 10? because we don't know how many canonical lines
1399 will the height of the next line(s) be. So we guess. */
1400 int ten_more_lines = 10 * default_line_pixel_height (w);
1401
1402 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1403 MOVE_TO_POS | MOVE_TO_Y);
1404 if (it.current_y > top_y)
1405 visible_p = 0;
1406
1407 it = save_it;
1408 }
1409 if (visible_p)
1410 {
1411 if (it_method == GET_FROM_DISPLAY_VECTOR)
1412 {
1413 /* We stopped on the last glyph of a display vector.
1414 Try and recompute. Hack alert! */
1415 if (charpos < 2 || top.charpos >= charpos)
1416 top_x = it.glyph_row->x;
1417 else
1418 {
1419 struct it it2, it2_prev;
1420 /* The idea is to get to the previous buffer
1421 position, consume the character there, and use
1422 the pixel coordinates we get after that. But if
1423 the previous buffer position is also displayed
1424 from a display vector, we need to consume all of
1425 the glyphs from that display vector. */
1426 start_display (&it2, w, top);
1427 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1428 /* If we didn't get to CHARPOS - 1, there's some
1429 replacing display property at that position, and
1430 we stopped after it. That is exactly the place
1431 whose coordinates we want. */
1432 if (IT_CHARPOS (it2) != charpos - 1)
1433 it2_prev = it2;
1434 else
1435 {
1436 /* Iterate until we get out of the display
1437 vector that displays the character at
1438 CHARPOS - 1. */
1439 do {
1440 get_next_display_element (&it2);
1441 PRODUCE_GLYPHS (&it2);
1442 it2_prev = it2;
1443 set_iterator_to_next (&it2, 1);
1444 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1445 && IT_CHARPOS (it2) < charpos);
1446 }
1447 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1448 || it2_prev.current_x > it2_prev.last_visible_x)
1449 top_x = it.glyph_row->x;
1450 else
1451 {
1452 top_x = it2_prev.current_x;
1453 top_y = it2_prev.current_y;
1454 }
1455 }
1456 }
1457 else if (IT_CHARPOS (it) != charpos)
1458 {
1459 Lisp_Object cpos = make_number (charpos);
1460 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1461 Lisp_Object string = string_from_display_spec (spec);
1462 struct text_pos tpos;
1463 int replacing_spec_p;
1464 bool newline_in_string
1465 = (STRINGP (string)
1466 && memchr (SDATA (string), '\n', SBYTES (string)));
1467
1468 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1469 replacing_spec_p
1470 = (!NILP (spec)
1471 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1472 charpos, FRAME_WINDOW_P (it.f)));
1473 /* The tricky code below is needed because there's a
1474 discrepancy between move_it_to and how we set cursor
1475 when PT is at the beginning of a portion of text
1476 covered by a display property or an overlay with a
1477 display property, or the display line ends in a
1478 newline from a display string. move_it_to will stop
1479 _after_ such display strings, whereas
1480 set_cursor_from_row conspires with cursor_row_p to
1481 place the cursor on the first glyph produced from the
1482 display string. */
1483
1484 /* We have overshoot PT because it is covered by a
1485 display property that replaces the text it covers.
1486 If the string includes embedded newlines, we are also
1487 in the wrong display line. Backtrack to the correct
1488 line, where the display property begins. */
1489 if (replacing_spec_p)
1490 {
1491 Lisp_Object startpos, endpos;
1492 EMACS_INT start, end;
1493 struct it it3;
1494 int it3_moved;
1495
1496 /* Find the first and the last buffer positions
1497 covered by the display string. */
1498 endpos =
1499 Fnext_single_char_property_change (cpos, Qdisplay,
1500 Qnil, Qnil);
1501 startpos =
1502 Fprevious_single_char_property_change (endpos, Qdisplay,
1503 Qnil, Qnil);
1504 start = XFASTINT (startpos);
1505 end = XFASTINT (endpos);
1506 /* Move to the last buffer position before the
1507 display property. */
1508 start_display (&it3, w, top);
1509 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1510 /* Move forward one more line if the position before
1511 the display string is a newline or if it is the
1512 rightmost character on a line that is
1513 continued or word-wrapped. */
1514 if (it3.method == GET_FROM_BUFFER
1515 && (it3.c == '\n'
1516 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1517 move_it_by_lines (&it3, 1);
1518 else if (move_it_in_display_line_to (&it3, -1,
1519 it3.current_x
1520 + it3.pixel_width,
1521 MOVE_TO_X)
1522 == MOVE_LINE_CONTINUED)
1523 {
1524 move_it_by_lines (&it3, 1);
1525 /* When we are under word-wrap, the #$@%!
1526 move_it_by_lines moves 2 lines, so we need to
1527 fix that up. */
1528 if (it3.line_wrap == WORD_WRAP)
1529 move_it_by_lines (&it3, -1);
1530 }
1531
1532 /* Record the vertical coordinate of the display
1533 line where we wound up. */
1534 top_y = it3.current_y;
1535 if (it3.bidi_p)
1536 {
1537 /* When characters are reordered for display,
1538 the character displayed to the left of the
1539 display string could be _after_ the display
1540 property in the logical order. Use the
1541 smallest vertical position of these two. */
1542 start_display (&it3, w, top);
1543 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1544 if (it3.current_y < top_y)
1545 top_y = it3.current_y;
1546 }
1547 /* Move from the top of the window to the beginning
1548 of the display line where the display string
1549 begins. */
1550 start_display (&it3, w, top);
1551 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1552 /* If it3_moved stays zero after the 'while' loop
1553 below, that means we already were at a newline
1554 before the loop (e.g., the display string begins
1555 with a newline), so we don't need to (and cannot)
1556 inspect the glyphs of it3.glyph_row, because
1557 PRODUCE_GLYPHS will not produce anything for a
1558 newline, and thus it3.glyph_row stays at its
1559 stale content it got at top of the window. */
1560 it3_moved = 0;
1561 /* Finally, advance the iterator until we hit the
1562 first display element whose character position is
1563 CHARPOS, or until the first newline from the
1564 display string, which signals the end of the
1565 display line. */
1566 while (get_next_display_element (&it3))
1567 {
1568 PRODUCE_GLYPHS (&it3);
1569 if (IT_CHARPOS (it3) == charpos
1570 || ITERATOR_AT_END_OF_LINE_P (&it3))
1571 break;
1572 it3_moved = 1;
1573 set_iterator_to_next (&it3, 0);
1574 }
1575 top_x = it3.current_x - it3.pixel_width;
1576 /* Normally, we would exit the above loop because we
1577 found the display element whose character
1578 position is CHARPOS. For the contingency that we
1579 didn't, and stopped at the first newline from the
1580 display string, move back over the glyphs
1581 produced from the string, until we find the
1582 rightmost glyph not from the string. */
1583 if (it3_moved
1584 && newline_in_string
1585 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1586 {
1587 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1588 + it3.glyph_row->used[TEXT_AREA];
1589
1590 while (EQ ((g - 1)->object, string))
1591 {
1592 --g;
1593 top_x -= g->pixel_width;
1594 }
1595 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1596 + it3.glyph_row->used[TEXT_AREA]);
1597 }
1598 }
1599 }
1600
1601 *x = top_x;
1602 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1603 *rtop = max (0, window_top_y - top_y);
1604 *rbot = max (0, bottom_y - it.last_visible_y);
1605 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1606 - max (top_y, window_top_y)));
1607 *vpos = it.vpos;
1608 }
1609 }
1610 else
1611 {
1612 /* We were asked to provide info about WINDOW_END. */
1613 struct it it2;
1614 void *it2data = NULL;
1615
1616 SAVE_IT (it2, it, it2data);
1617 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1618 move_it_by_lines (&it, 1);
1619 if (charpos < IT_CHARPOS (it)
1620 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1621 {
1622 visible_p = 1;
1623 RESTORE_IT (&it2, &it2, it2data);
1624 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1625 *x = it2.current_x;
1626 *y = it2.current_y + it2.max_ascent - it2.ascent;
1627 *rtop = max (0, -it2.current_y);
1628 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1629 - it.last_visible_y));
1630 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1631 it.last_visible_y)
1632 - max (it2.current_y,
1633 WINDOW_HEADER_LINE_HEIGHT (w))));
1634 *vpos = it2.vpos;
1635 }
1636 else
1637 bidi_unshelve_cache (it2data, 1);
1638 }
1639 bidi_unshelve_cache (itdata, 0);
1640
1641 if (old_buffer)
1642 set_buffer_internal_1 (old_buffer);
1643
1644 if (visible_p && w->hscroll > 0)
1645 *x -=
1646 window_hscroll_limited (w, WINDOW_XFRAME (w))
1647 * WINDOW_FRAME_COLUMN_WIDTH (w);
1648
1649 #if 0
1650 /* Debugging code. */
1651 if (visible_p)
1652 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1653 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1654 else
1655 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1656 #endif
1657
1658 return visible_p;
1659 }
1660
1661
1662 /* Return the next character from STR. Return in *LEN the length of
1663 the character. This is like STRING_CHAR_AND_LENGTH but never
1664 returns an invalid character. If we find one, we return a `?', but
1665 with the length of the invalid character. */
1666
1667 static int
1668 string_char_and_length (const unsigned char *str, int *len)
1669 {
1670 int c;
1671
1672 c = STRING_CHAR_AND_LENGTH (str, *len);
1673 if (!CHAR_VALID_P (c))
1674 /* We may not change the length here because other places in Emacs
1675 don't use this function, i.e. they silently accept invalid
1676 characters. */
1677 c = '?';
1678
1679 return c;
1680 }
1681
1682
1683
1684 /* Given a position POS containing a valid character and byte position
1685 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1686
1687 static struct text_pos
1688 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1689 {
1690 eassert (STRINGP (string) && nchars >= 0);
1691
1692 if (STRING_MULTIBYTE (string))
1693 {
1694 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1695 int len;
1696
1697 while (nchars--)
1698 {
1699 string_char_and_length (p, &len);
1700 p += len;
1701 CHARPOS (pos) += 1;
1702 BYTEPOS (pos) += len;
1703 }
1704 }
1705 else
1706 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1707
1708 return pos;
1709 }
1710
1711
1712 /* Value is the text position, i.e. character and byte position,
1713 for character position CHARPOS in STRING. */
1714
1715 static struct text_pos
1716 string_pos (ptrdiff_t charpos, Lisp_Object string)
1717 {
1718 struct text_pos pos;
1719 eassert (STRINGP (string));
1720 eassert (charpos >= 0);
1721 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1722 return pos;
1723 }
1724
1725
1726 /* Value is a text position, i.e. character and byte position, for
1727 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1728 means recognize multibyte characters. */
1729
1730 static struct text_pos
1731 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1732 {
1733 struct text_pos pos;
1734
1735 eassert (s != NULL);
1736 eassert (charpos >= 0);
1737
1738 if (multibyte_p)
1739 {
1740 int len;
1741
1742 SET_TEXT_POS (pos, 0, 0);
1743 while (charpos--)
1744 {
1745 string_char_and_length ((const unsigned char *) s, &len);
1746 s += len;
1747 CHARPOS (pos) += 1;
1748 BYTEPOS (pos) += len;
1749 }
1750 }
1751 else
1752 SET_TEXT_POS (pos, charpos, charpos);
1753
1754 return pos;
1755 }
1756
1757
1758 /* Value is the number of characters in C string S. MULTIBYTE_P
1759 non-zero means recognize multibyte characters. */
1760
1761 static ptrdiff_t
1762 number_of_chars (const char *s, bool multibyte_p)
1763 {
1764 ptrdiff_t nchars;
1765
1766 if (multibyte_p)
1767 {
1768 ptrdiff_t rest = strlen (s);
1769 int len;
1770 const unsigned char *p = (const unsigned char *) s;
1771
1772 for (nchars = 0; rest > 0; ++nchars)
1773 {
1774 string_char_and_length (p, &len);
1775 rest -= len, p += len;
1776 }
1777 }
1778 else
1779 nchars = strlen (s);
1780
1781 return nchars;
1782 }
1783
1784
1785 /* Compute byte position NEWPOS->bytepos corresponding to
1786 NEWPOS->charpos. POS is a known position in string STRING.
1787 NEWPOS->charpos must be >= POS.charpos. */
1788
1789 static void
1790 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1791 {
1792 eassert (STRINGP (string));
1793 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1794
1795 if (STRING_MULTIBYTE (string))
1796 *newpos = string_pos_nchars_ahead (pos, string,
1797 CHARPOS (*newpos) - CHARPOS (pos));
1798 else
1799 BYTEPOS (*newpos) = CHARPOS (*newpos);
1800 }
1801
1802 /* EXPORT:
1803 Return an estimation of the pixel height of mode or header lines on
1804 frame F. FACE_ID specifies what line's height to estimate. */
1805
1806 int
1807 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1808 {
1809 #ifdef HAVE_WINDOW_SYSTEM
1810 if (FRAME_WINDOW_P (f))
1811 {
1812 int height = FONT_HEIGHT (FRAME_FONT (f));
1813
1814 /* This function is called so early when Emacs starts that the face
1815 cache and mode line face are not yet initialized. */
1816 if (FRAME_FACE_CACHE (f))
1817 {
1818 struct face *face = FACE_FROM_ID (f, face_id);
1819 if (face)
1820 {
1821 if (face->font)
1822 height = FONT_HEIGHT (face->font);
1823 if (face->box_line_width > 0)
1824 height += 2 * face->box_line_width;
1825 }
1826 }
1827
1828 return height;
1829 }
1830 #endif
1831
1832 return 1;
1833 }
1834
1835 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1836 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1837 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1838 not force the value into range. */
1839
1840 void
1841 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1842 int *x, int *y, NativeRectangle *bounds, int noclip)
1843 {
1844
1845 #ifdef HAVE_WINDOW_SYSTEM
1846 if (FRAME_WINDOW_P (f))
1847 {
1848 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1849 even for negative values. */
1850 if (pix_x < 0)
1851 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1852 if (pix_y < 0)
1853 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1854
1855 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1856 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1857
1858 if (bounds)
1859 STORE_NATIVE_RECT (*bounds,
1860 FRAME_COL_TO_PIXEL_X (f, pix_x),
1861 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1862 FRAME_COLUMN_WIDTH (f) - 1,
1863 FRAME_LINE_HEIGHT (f) - 1);
1864
1865 if (!noclip)
1866 {
1867 if (pix_x < 0)
1868 pix_x = 0;
1869 else if (pix_x > FRAME_TOTAL_COLS (f))
1870 pix_x = FRAME_TOTAL_COLS (f);
1871
1872 if (pix_y < 0)
1873 pix_y = 0;
1874 else if (pix_y > FRAME_LINES (f))
1875 pix_y = FRAME_LINES (f);
1876 }
1877 }
1878 #endif
1879
1880 *x = pix_x;
1881 *y = pix_y;
1882 }
1883
1884
1885 /* Find the glyph under window-relative coordinates X/Y in window W.
1886 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1887 strings. Return in *HPOS and *VPOS the row and column number of
1888 the glyph found. Return in *AREA the glyph area containing X.
1889 Value is a pointer to the glyph found or null if X/Y is not on
1890 text, or we can't tell because W's current matrix is not up to
1891 date. */
1892
1893 static
1894 struct glyph *
1895 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1896 int *dx, int *dy, int *area)
1897 {
1898 struct glyph *glyph, *end;
1899 struct glyph_row *row = NULL;
1900 int x0, i;
1901
1902 /* Find row containing Y. Give up if some row is not enabled. */
1903 for (i = 0; i < w->current_matrix->nrows; ++i)
1904 {
1905 row = MATRIX_ROW (w->current_matrix, i);
1906 if (!row->enabled_p)
1907 return NULL;
1908 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1909 break;
1910 }
1911
1912 *vpos = i;
1913 *hpos = 0;
1914
1915 /* Give up if Y is not in the window. */
1916 if (i == w->current_matrix->nrows)
1917 return NULL;
1918
1919 /* Get the glyph area containing X. */
1920 if (w->pseudo_window_p)
1921 {
1922 *area = TEXT_AREA;
1923 x0 = 0;
1924 }
1925 else
1926 {
1927 if (x < window_box_left_offset (w, TEXT_AREA))
1928 {
1929 *area = LEFT_MARGIN_AREA;
1930 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1931 }
1932 else if (x < window_box_right_offset (w, TEXT_AREA))
1933 {
1934 *area = TEXT_AREA;
1935 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1936 }
1937 else
1938 {
1939 *area = RIGHT_MARGIN_AREA;
1940 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1941 }
1942 }
1943
1944 /* Find glyph containing X. */
1945 glyph = row->glyphs[*area];
1946 end = glyph + row->used[*area];
1947 x -= x0;
1948 while (glyph < end && x >= glyph->pixel_width)
1949 {
1950 x -= glyph->pixel_width;
1951 ++glyph;
1952 }
1953
1954 if (glyph == end)
1955 return NULL;
1956
1957 if (dx)
1958 {
1959 *dx = x;
1960 *dy = y - (row->y + row->ascent - glyph->ascent);
1961 }
1962
1963 *hpos = glyph - row->glyphs[*area];
1964 return glyph;
1965 }
1966
1967 /* Convert frame-relative x/y to coordinates relative to window W.
1968 Takes pseudo-windows into account. */
1969
1970 static void
1971 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1972 {
1973 if (w->pseudo_window_p)
1974 {
1975 /* A pseudo-window is always full-width, and starts at the
1976 left edge of the frame, plus a frame border. */
1977 struct frame *f = XFRAME (w->frame);
1978 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1979 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1980 }
1981 else
1982 {
1983 *x -= WINDOW_LEFT_EDGE_X (w);
1984 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1985 }
1986 }
1987
1988 #ifdef HAVE_WINDOW_SYSTEM
1989
1990 /* EXPORT:
1991 Return in RECTS[] at most N clipping rectangles for glyph string S.
1992 Return the number of stored rectangles. */
1993
1994 int
1995 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1996 {
1997 XRectangle r;
1998
1999 if (n <= 0)
2000 return 0;
2001
2002 if (s->row->full_width_p)
2003 {
2004 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2005 r.x = WINDOW_LEFT_EDGE_X (s->w);
2006 r.width = WINDOW_TOTAL_WIDTH (s->w);
2007
2008 /* Unless displaying a mode or menu bar line, which are always
2009 fully visible, clip to the visible part of the row. */
2010 if (s->w->pseudo_window_p)
2011 r.height = s->row->visible_height;
2012 else
2013 r.height = s->height;
2014 }
2015 else
2016 {
2017 /* This is a text line that may be partially visible. */
2018 r.x = window_box_left (s->w, s->area);
2019 r.width = window_box_width (s->w, s->area);
2020 r.height = s->row->visible_height;
2021 }
2022
2023 if (s->clip_head)
2024 if (r.x < s->clip_head->x)
2025 {
2026 if (r.width >= s->clip_head->x - r.x)
2027 r.width -= s->clip_head->x - r.x;
2028 else
2029 r.width = 0;
2030 r.x = s->clip_head->x;
2031 }
2032 if (s->clip_tail)
2033 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2034 {
2035 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2036 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2037 else
2038 r.width = 0;
2039 }
2040
2041 /* If S draws overlapping rows, it's sufficient to use the top and
2042 bottom of the window for clipping because this glyph string
2043 intentionally draws over other lines. */
2044 if (s->for_overlaps)
2045 {
2046 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2047 r.height = window_text_bottom_y (s->w) - r.y;
2048
2049 /* Alas, the above simple strategy does not work for the
2050 environments with anti-aliased text: if the same text is
2051 drawn onto the same place multiple times, it gets thicker.
2052 If the overlap we are processing is for the erased cursor, we
2053 take the intersection with the rectangle of the cursor. */
2054 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2055 {
2056 XRectangle rc, r_save = r;
2057
2058 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2059 rc.y = s->w->phys_cursor.y;
2060 rc.width = s->w->phys_cursor_width;
2061 rc.height = s->w->phys_cursor_height;
2062
2063 x_intersect_rectangles (&r_save, &rc, &r);
2064 }
2065 }
2066 else
2067 {
2068 /* Don't use S->y for clipping because it doesn't take partially
2069 visible lines into account. For example, it can be negative for
2070 partially visible lines at the top of a window. */
2071 if (!s->row->full_width_p
2072 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2073 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2074 else
2075 r.y = max (0, s->row->y);
2076 }
2077
2078 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2079
2080 /* If drawing the cursor, don't let glyph draw outside its
2081 advertised boundaries. Cleartype does this under some circumstances. */
2082 if (s->hl == DRAW_CURSOR)
2083 {
2084 struct glyph *glyph = s->first_glyph;
2085 int height, max_y;
2086
2087 if (s->x > r.x)
2088 {
2089 r.width -= s->x - r.x;
2090 r.x = s->x;
2091 }
2092 r.width = min (r.width, glyph->pixel_width);
2093
2094 /* If r.y is below window bottom, ensure that we still see a cursor. */
2095 height = min (glyph->ascent + glyph->descent,
2096 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2097 max_y = window_text_bottom_y (s->w) - height;
2098 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2099 if (s->ybase - glyph->ascent > max_y)
2100 {
2101 r.y = max_y;
2102 r.height = height;
2103 }
2104 else
2105 {
2106 /* Don't draw cursor glyph taller than our actual glyph. */
2107 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2108 if (height < r.height)
2109 {
2110 max_y = r.y + r.height;
2111 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2112 r.height = min (max_y - r.y, height);
2113 }
2114 }
2115 }
2116
2117 if (s->row->clip)
2118 {
2119 XRectangle r_save = r;
2120
2121 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2122 r.width = 0;
2123 }
2124
2125 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2126 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2127 {
2128 #ifdef CONVERT_FROM_XRECT
2129 CONVERT_FROM_XRECT (r, *rects);
2130 #else
2131 *rects = r;
2132 #endif
2133 return 1;
2134 }
2135 else
2136 {
2137 /* If we are processing overlapping and allowed to return
2138 multiple clipping rectangles, we exclude the row of the glyph
2139 string from the clipping rectangle. This is to avoid drawing
2140 the same text on the environment with anti-aliasing. */
2141 #ifdef CONVERT_FROM_XRECT
2142 XRectangle rs[2];
2143 #else
2144 XRectangle *rs = rects;
2145 #endif
2146 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2147
2148 if (s->for_overlaps & OVERLAPS_PRED)
2149 {
2150 rs[i] = r;
2151 if (r.y + r.height > row_y)
2152 {
2153 if (r.y < row_y)
2154 rs[i].height = row_y - r.y;
2155 else
2156 rs[i].height = 0;
2157 }
2158 i++;
2159 }
2160 if (s->for_overlaps & OVERLAPS_SUCC)
2161 {
2162 rs[i] = r;
2163 if (r.y < row_y + s->row->visible_height)
2164 {
2165 if (r.y + r.height > row_y + s->row->visible_height)
2166 {
2167 rs[i].y = row_y + s->row->visible_height;
2168 rs[i].height = r.y + r.height - rs[i].y;
2169 }
2170 else
2171 rs[i].height = 0;
2172 }
2173 i++;
2174 }
2175
2176 n = i;
2177 #ifdef CONVERT_FROM_XRECT
2178 for (i = 0; i < n; i++)
2179 CONVERT_FROM_XRECT (rs[i], rects[i]);
2180 #endif
2181 return n;
2182 }
2183 }
2184
2185 /* EXPORT:
2186 Return in *NR the clipping rectangle for glyph string S. */
2187
2188 void
2189 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2190 {
2191 get_glyph_string_clip_rects (s, nr, 1);
2192 }
2193
2194
2195 /* EXPORT:
2196 Return the position and height of the phys cursor in window W.
2197 Set w->phys_cursor_width to width of phys cursor.
2198 */
2199
2200 void
2201 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2202 struct glyph *glyph, int *xp, int *yp, int *heightp)
2203 {
2204 struct frame *f = XFRAME (WINDOW_FRAME (w));
2205 int x, y, wd, h, h0, y0;
2206
2207 /* Compute the width of the rectangle to draw. If on a stretch
2208 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2209 rectangle as wide as the glyph, but use a canonical character
2210 width instead. */
2211 wd = glyph->pixel_width - 1;
2212 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2213 wd++; /* Why? */
2214 #endif
2215
2216 x = w->phys_cursor.x;
2217 if (x < 0)
2218 {
2219 wd += x;
2220 x = 0;
2221 }
2222
2223 if (glyph->type == STRETCH_GLYPH
2224 && !x_stretch_cursor_p)
2225 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2226 w->phys_cursor_width = wd;
2227
2228 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2229
2230 /* If y is below window bottom, ensure that we still see a cursor. */
2231 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2232
2233 h = max (h0, glyph->ascent + glyph->descent);
2234 h0 = min (h0, glyph->ascent + glyph->descent);
2235
2236 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2237 if (y < y0)
2238 {
2239 h = max (h - (y0 - y) + 1, h0);
2240 y = y0 - 1;
2241 }
2242 else
2243 {
2244 y0 = window_text_bottom_y (w) - h0;
2245 if (y > y0)
2246 {
2247 h += y - y0;
2248 y = y0;
2249 }
2250 }
2251
2252 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2253 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2254 *heightp = h;
2255 }
2256
2257 /*
2258 * Remember which glyph the mouse is over.
2259 */
2260
2261 void
2262 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2263 {
2264 Lisp_Object window;
2265 struct window *w;
2266 struct glyph_row *r, *gr, *end_row;
2267 enum window_part part;
2268 enum glyph_row_area area;
2269 int x, y, width, height;
2270
2271 /* Try to determine frame pixel position and size of the glyph under
2272 frame pixel coordinates X/Y on frame F. */
2273
2274 if (!f->glyphs_initialized_p
2275 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2276 NILP (window)))
2277 {
2278 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2279 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2280 goto virtual_glyph;
2281 }
2282
2283 w = XWINDOW (window);
2284 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2285 height = WINDOW_FRAME_LINE_HEIGHT (w);
2286
2287 x = window_relative_x_coord (w, part, gx);
2288 y = gy - WINDOW_TOP_EDGE_Y (w);
2289
2290 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2291 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2292
2293 if (w->pseudo_window_p)
2294 {
2295 area = TEXT_AREA;
2296 part = ON_MODE_LINE; /* Don't adjust margin. */
2297 goto text_glyph;
2298 }
2299
2300 switch (part)
2301 {
2302 case ON_LEFT_MARGIN:
2303 area = LEFT_MARGIN_AREA;
2304 goto text_glyph;
2305
2306 case ON_RIGHT_MARGIN:
2307 area = RIGHT_MARGIN_AREA;
2308 goto text_glyph;
2309
2310 case ON_HEADER_LINE:
2311 case ON_MODE_LINE:
2312 gr = (part == ON_HEADER_LINE
2313 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2314 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2315 gy = gr->y;
2316 area = TEXT_AREA;
2317 goto text_glyph_row_found;
2318
2319 case ON_TEXT:
2320 area = TEXT_AREA;
2321
2322 text_glyph:
2323 gr = 0; gy = 0;
2324 for (; r <= end_row && r->enabled_p; ++r)
2325 if (r->y + r->height > y)
2326 {
2327 gr = r; gy = r->y;
2328 break;
2329 }
2330
2331 text_glyph_row_found:
2332 if (gr && gy <= y)
2333 {
2334 struct glyph *g = gr->glyphs[area];
2335 struct glyph *end = g + gr->used[area];
2336
2337 height = gr->height;
2338 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2339 if (gx + g->pixel_width > x)
2340 break;
2341
2342 if (g < end)
2343 {
2344 if (g->type == IMAGE_GLYPH)
2345 {
2346 /* Don't remember when mouse is over image, as
2347 image may have hot-spots. */
2348 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2349 return;
2350 }
2351 width = g->pixel_width;
2352 }
2353 else
2354 {
2355 /* Use nominal char spacing at end of line. */
2356 x -= gx;
2357 gx += (x / width) * width;
2358 }
2359
2360 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2361 gx += window_box_left_offset (w, area);
2362 }
2363 else
2364 {
2365 /* Use nominal line height at end of window. */
2366 gx = (x / width) * width;
2367 y -= gy;
2368 gy += (y / height) * height;
2369 }
2370 break;
2371
2372 case ON_LEFT_FRINGE:
2373 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2374 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2375 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2376 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2377 goto row_glyph;
2378
2379 case ON_RIGHT_FRINGE:
2380 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2381 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2382 : window_box_right_offset (w, TEXT_AREA));
2383 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2384 goto row_glyph;
2385
2386 case ON_SCROLL_BAR:
2387 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2388 ? 0
2389 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2390 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2391 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2392 : 0)));
2393 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2394
2395 row_glyph:
2396 gr = 0, gy = 0;
2397 for (; r <= end_row && r->enabled_p; ++r)
2398 if (r->y + r->height > y)
2399 {
2400 gr = r; gy = r->y;
2401 break;
2402 }
2403
2404 if (gr && gy <= y)
2405 height = gr->height;
2406 else
2407 {
2408 /* Use nominal line height at end of window. */
2409 y -= gy;
2410 gy += (y / height) * height;
2411 }
2412 break;
2413
2414 default:
2415 ;
2416 virtual_glyph:
2417 /* If there is no glyph under the mouse, then we divide the screen
2418 into a grid of the smallest glyph in the frame, and use that
2419 as our "glyph". */
2420
2421 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2422 round down even for negative values. */
2423 if (gx < 0)
2424 gx -= width - 1;
2425 if (gy < 0)
2426 gy -= height - 1;
2427
2428 gx = (gx / width) * width;
2429 gy = (gy / height) * height;
2430
2431 goto store_rect;
2432 }
2433
2434 gx += WINDOW_LEFT_EDGE_X (w);
2435 gy += WINDOW_TOP_EDGE_Y (w);
2436
2437 store_rect:
2438 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2439
2440 /* Visible feedback for debugging. */
2441 #if 0
2442 #if HAVE_X_WINDOWS
2443 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2444 f->output_data.x->normal_gc,
2445 gx, gy, width, height);
2446 #endif
2447 #endif
2448 }
2449
2450
2451 #endif /* HAVE_WINDOW_SYSTEM */
2452
2453 static void
2454 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2455 {
2456 eassert (w);
2457 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2458 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2459 w->window_end_vpos
2460 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2461 }
2462
2463 /***********************************************************************
2464 Lisp form evaluation
2465 ***********************************************************************/
2466
2467 /* Error handler for safe_eval and safe_call. */
2468
2469 static Lisp_Object
2470 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2471 {
2472 add_to_log ("Error during redisplay: %S signaled %S",
2473 Flist (nargs, args), arg);
2474 return Qnil;
2475 }
2476
2477 /* Call function FUNC with the rest of NARGS - 1 arguments
2478 following. Return the result, or nil if something went
2479 wrong. Prevent redisplay during the evaluation. */
2480
2481 Lisp_Object
2482 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2483 {
2484 Lisp_Object val;
2485
2486 if (inhibit_eval_during_redisplay)
2487 val = Qnil;
2488 else
2489 {
2490 va_list ap;
2491 ptrdiff_t i;
2492 ptrdiff_t count = SPECPDL_INDEX ();
2493 struct gcpro gcpro1;
2494 Lisp_Object *args = alloca (nargs * word_size);
2495
2496 args[0] = func;
2497 va_start (ap, func);
2498 for (i = 1; i < nargs; i++)
2499 args[i] = va_arg (ap, Lisp_Object);
2500 va_end (ap);
2501
2502 GCPRO1 (args[0]);
2503 gcpro1.nvars = nargs;
2504 specbind (Qinhibit_redisplay, Qt);
2505 /* Use Qt to ensure debugger does not run,
2506 so there is no possibility of wanting to redisplay. */
2507 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2508 safe_eval_handler);
2509 UNGCPRO;
2510 val = unbind_to (count, val);
2511 }
2512
2513 return val;
2514 }
2515
2516
2517 /* Call function FN with one argument ARG.
2518 Return the result, or nil if something went wrong. */
2519
2520 Lisp_Object
2521 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2522 {
2523 return safe_call (2, fn, arg);
2524 }
2525
2526 static Lisp_Object Qeval;
2527
2528 Lisp_Object
2529 safe_eval (Lisp_Object sexpr)
2530 {
2531 return safe_call1 (Qeval, sexpr);
2532 }
2533
2534 /* Call function FN with two arguments ARG1 and ARG2.
2535 Return the result, or nil if something went wrong. */
2536
2537 Lisp_Object
2538 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2539 {
2540 return safe_call (3, fn, arg1, arg2);
2541 }
2542
2543
2544 \f
2545 /***********************************************************************
2546 Debugging
2547 ***********************************************************************/
2548
2549 #if 0
2550
2551 /* Define CHECK_IT to perform sanity checks on iterators.
2552 This is for debugging. It is too slow to do unconditionally. */
2553
2554 static void
2555 check_it (struct it *it)
2556 {
2557 if (it->method == GET_FROM_STRING)
2558 {
2559 eassert (STRINGP (it->string));
2560 eassert (IT_STRING_CHARPOS (*it) >= 0);
2561 }
2562 else
2563 {
2564 eassert (IT_STRING_CHARPOS (*it) < 0);
2565 if (it->method == GET_FROM_BUFFER)
2566 {
2567 /* Check that character and byte positions agree. */
2568 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2569 }
2570 }
2571
2572 if (it->dpvec)
2573 eassert (it->current.dpvec_index >= 0);
2574 else
2575 eassert (it->current.dpvec_index < 0);
2576 }
2577
2578 #define CHECK_IT(IT) check_it ((IT))
2579
2580 #else /* not 0 */
2581
2582 #define CHECK_IT(IT) (void) 0
2583
2584 #endif /* not 0 */
2585
2586
2587 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2588
2589 /* Check that the window end of window W is what we expect it
2590 to be---the last row in the current matrix displaying text. */
2591
2592 static void
2593 check_window_end (struct window *w)
2594 {
2595 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2596 {
2597 struct glyph_row *row;
2598 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2599 !row->enabled_p
2600 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2601 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2602 }
2603 }
2604
2605 #define CHECK_WINDOW_END(W) check_window_end ((W))
2606
2607 #else
2608
2609 #define CHECK_WINDOW_END(W) (void) 0
2610
2611 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2612
2613 /* Return mark position if current buffer has the region of non-zero length,
2614 or -1 otherwise. */
2615
2616 static ptrdiff_t
2617 markpos_of_region (void)
2618 {
2619 if (!NILP (Vtransient_mark_mode)
2620 && !NILP (BVAR (current_buffer, mark_active))
2621 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2622 {
2623 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2624
2625 if (markpos != PT)
2626 return markpos;
2627 }
2628 return -1;
2629 }
2630
2631 /***********************************************************************
2632 Iterator initialization
2633 ***********************************************************************/
2634
2635 /* Initialize IT for displaying current_buffer in window W, starting
2636 at character position CHARPOS. CHARPOS < 0 means that no buffer
2637 position is specified which is useful when the iterator is assigned
2638 a position later. BYTEPOS is the byte position corresponding to
2639 CHARPOS.
2640
2641 If ROW is not null, calls to produce_glyphs with IT as parameter
2642 will produce glyphs in that row.
2643
2644 BASE_FACE_ID is the id of a base face to use. It must be one of
2645 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2646 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2647 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2648
2649 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2650 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2651 will be initialized to use the corresponding mode line glyph row of
2652 the desired matrix of W. */
2653
2654 void
2655 init_iterator (struct it *it, struct window *w,
2656 ptrdiff_t charpos, ptrdiff_t bytepos,
2657 struct glyph_row *row, enum face_id base_face_id)
2658 {
2659 ptrdiff_t markpos;
2660 enum face_id remapped_base_face_id = base_face_id;
2661
2662 /* Some precondition checks. */
2663 eassert (w != NULL && it != NULL);
2664 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2665 && charpos <= ZV));
2666
2667 /* If face attributes have been changed since the last redisplay,
2668 free realized faces now because they depend on face definitions
2669 that might have changed. Don't free faces while there might be
2670 desired matrices pending which reference these faces. */
2671 if (face_change_count && !inhibit_free_realized_faces)
2672 {
2673 face_change_count = 0;
2674 free_all_realized_faces (Qnil);
2675 }
2676
2677 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2678 if (! NILP (Vface_remapping_alist))
2679 remapped_base_face_id
2680 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2681
2682 /* Use one of the mode line rows of W's desired matrix if
2683 appropriate. */
2684 if (row == NULL)
2685 {
2686 if (base_face_id == MODE_LINE_FACE_ID
2687 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2688 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2689 else if (base_face_id == HEADER_LINE_FACE_ID)
2690 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2691 }
2692
2693 /* Clear IT. */
2694 memset (it, 0, sizeof *it);
2695 it->current.overlay_string_index = -1;
2696 it->current.dpvec_index = -1;
2697 it->base_face_id = remapped_base_face_id;
2698 it->string = Qnil;
2699 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2700 it->paragraph_embedding = L2R;
2701 it->bidi_it.string.lstring = Qnil;
2702 it->bidi_it.string.s = NULL;
2703 it->bidi_it.string.bufpos = 0;
2704 it->bidi_it.w = w;
2705
2706 /* The window in which we iterate over current_buffer: */
2707 XSETWINDOW (it->window, w);
2708 it->w = w;
2709 it->f = XFRAME (w->frame);
2710
2711 it->cmp_it.id = -1;
2712
2713 /* Extra space between lines (on window systems only). */
2714 if (base_face_id == DEFAULT_FACE_ID
2715 && FRAME_WINDOW_P (it->f))
2716 {
2717 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2718 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2719 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2720 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2721 * FRAME_LINE_HEIGHT (it->f));
2722 else if (it->f->extra_line_spacing > 0)
2723 it->extra_line_spacing = it->f->extra_line_spacing;
2724 it->max_extra_line_spacing = 0;
2725 }
2726
2727 /* If realized faces have been removed, e.g. because of face
2728 attribute changes of named faces, recompute them. When running
2729 in batch mode, the face cache of the initial frame is null. If
2730 we happen to get called, make a dummy face cache. */
2731 if (FRAME_FACE_CACHE (it->f) == NULL)
2732 init_frame_faces (it->f);
2733 if (FRAME_FACE_CACHE (it->f)->used == 0)
2734 recompute_basic_faces (it->f);
2735
2736 /* Current value of the `slice', `space-width', and 'height' properties. */
2737 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2738 it->space_width = Qnil;
2739 it->font_height = Qnil;
2740 it->override_ascent = -1;
2741
2742 /* Are control characters displayed as `^C'? */
2743 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2744
2745 /* -1 means everything between a CR and the following line end
2746 is invisible. >0 means lines indented more than this value are
2747 invisible. */
2748 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2749 ? (clip_to_bounds
2750 (-1, XINT (BVAR (current_buffer, selective_display)),
2751 PTRDIFF_MAX))
2752 : (!NILP (BVAR (current_buffer, selective_display))
2753 ? -1 : 0));
2754 it->selective_display_ellipsis_p
2755 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2756
2757 /* Display table to use. */
2758 it->dp = window_display_table (w);
2759
2760 /* Are multibyte characters enabled in current_buffer? */
2761 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2762
2763 /* If visible region is of non-zero length, set IT->region_beg_charpos
2764 and IT->region_end_charpos to the start and end of a visible region
2765 in window IT->w. Set both to -1 to indicate no region. */
2766 markpos = markpos_of_region ();
2767 if (markpos >= 0
2768 /* Maybe highlight only in selected window. */
2769 && (/* Either show region everywhere. */
2770 highlight_nonselected_windows
2771 /* Or show region in the selected window. */
2772 || w == XWINDOW (selected_window)
2773 /* Or show the region if we are in the mini-buffer and W is
2774 the window the mini-buffer refers to. */
2775 || (MINI_WINDOW_P (XWINDOW (selected_window))
2776 && WINDOWP (minibuf_selected_window)
2777 && w == XWINDOW (minibuf_selected_window))))
2778 {
2779 it->region_beg_charpos = min (PT, markpos);
2780 it->region_end_charpos = max (PT, markpos);
2781 }
2782 else
2783 it->region_beg_charpos = it->region_end_charpos = -1;
2784
2785 /* Get the position at which the redisplay_end_trigger hook should
2786 be run, if it is to be run at all. */
2787 if (MARKERP (w->redisplay_end_trigger)
2788 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2789 it->redisplay_end_trigger_charpos
2790 = marker_position (w->redisplay_end_trigger);
2791 else if (INTEGERP (w->redisplay_end_trigger))
2792 it->redisplay_end_trigger_charpos =
2793 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2794
2795 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2796
2797 /* Are lines in the display truncated? */
2798 if (base_face_id != DEFAULT_FACE_ID
2799 || it->w->hscroll
2800 || (! WINDOW_FULL_WIDTH_P (it->w)
2801 && ((!NILP (Vtruncate_partial_width_windows)
2802 && !INTEGERP (Vtruncate_partial_width_windows))
2803 || (INTEGERP (Vtruncate_partial_width_windows)
2804 && (WINDOW_TOTAL_COLS (it->w)
2805 < XINT (Vtruncate_partial_width_windows))))))
2806 it->line_wrap = TRUNCATE;
2807 else if (NILP (BVAR (current_buffer, truncate_lines)))
2808 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2809 ? WINDOW_WRAP : WORD_WRAP;
2810 else
2811 it->line_wrap = TRUNCATE;
2812
2813 /* Get dimensions of truncation and continuation glyphs. These are
2814 displayed as fringe bitmaps under X, but we need them for such
2815 frames when the fringes are turned off. But leave the dimensions
2816 zero for tooltip frames, as these glyphs look ugly there and also
2817 sabotage calculations of tooltip dimensions in x-show-tip. */
2818 #ifdef HAVE_WINDOW_SYSTEM
2819 if (!(FRAME_WINDOW_P (it->f)
2820 && FRAMEP (tip_frame)
2821 && it->f == XFRAME (tip_frame)))
2822 #endif
2823 {
2824 if (it->line_wrap == TRUNCATE)
2825 {
2826 /* We will need the truncation glyph. */
2827 eassert (it->glyph_row == NULL);
2828 produce_special_glyphs (it, IT_TRUNCATION);
2829 it->truncation_pixel_width = it->pixel_width;
2830 }
2831 else
2832 {
2833 /* We will need the continuation glyph. */
2834 eassert (it->glyph_row == NULL);
2835 produce_special_glyphs (it, IT_CONTINUATION);
2836 it->continuation_pixel_width = it->pixel_width;
2837 }
2838 }
2839
2840 /* Reset these values to zero because the produce_special_glyphs
2841 above has changed them. */
2842 it->pixel_width = it->ascent = it->descent = 0;
2843 it->phys_ascent = it->phys_descent = 0;
2844
2845 /* Set this after getting the dimensions of truncation and
2846 continuation glyphs, so that we don't produce glyphs when calling
2847 produce_special_glyphs, above. */
2848 it->glyph_row = row;
2849 it->area = TEXT_AREA;
2850
2851 /* Forget any previous info about this row being reversed. */
2852 if (it->glyph_row)
2853 it->glyph_row->reversed_p = 0;
2854
2855 /* Get the dimensions of the display area. The display area
2856 consists of the visible window area plus a horizontally scrolled
2857 part to the left of the window. All x-values are relative to the
2858 start of this total display area. */
2859 if (base_face_id != DEFAULT_FACE_ID)
2860 {
2861 /* Mode lines, menu bar in terminal frames. */
2862 it->first_visible_x = 0;
2863 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2864 }
2865 else
2866 {
2867 it->first_visible_x =
2868 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2869 it->last_visible_x = (it->first_visible_x
2870 + window_box_width (w, TEXT_AREA));
2871
2872 /* If we truncate lines, leave room for the truncation glyph(s) at
2873 the right margin. Otherwise, leave room for the continuation
2874 glyph(s). Done only if the window has no fringes. Since we
2875 don't know at this point whether there will be any R2L lines in
2876 the window, we reserve space for truncation/continuation glyphs
2877 even if only one of the fringes is absent. */
2878 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2879 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2880 {
2881 if (it->line_wrap == TRUNCATE)
2882 it->last_visible_x -= it->truncation_pixel_width;
2883 else
2884 it->last_visible_x -= it->continuation_pixel_width;
2885 }
2886
2887 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2888 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2889 }
2890
2891 /* Leave room for a border glyph. */
2892 if (!FRAME_WINDOW_P (it->f)
2893 && !WINDOW_RIGHTMOST_P (it->w))
2894 it->last_visible_x -= 1;
2895
2896 it->last_visible_y = window_text_bottom_y (w);
2897
2898 /* For mode lines and alike, arrange for the first glyph having a
2899 left box line if the face specifies a box. */
2900 if (base_face_id != DEFAULT_FACE_ID)
2901 {
2902 struct face *face;
2903
2904 it->face_id = remapped_base_face_id;
2905
2906 /* If we have a boxed mode line, make the first character appear
2907 with a left box line. */
2908 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2909 if (face->box != FACE_NO_BOX)
2910 it->start_of_box_run_p = 1;
2911 }
2912
2913 /* If a buffer position was specified, set the iterator there,
2914 getting overlays and face properties from that position. */
2915 if (charpos >= BUF_BEG (current_buffer))
2916 {
2917 it->end_charpos = ZV;
2918 eassert (charpos == BYTE_TO_CHAR (bytepos));
2919 IT_CHARPOS (*it) = charpos;
2920 IT_BYTEPOS (*it) = bytepos;
2921
2922 /* We will rely on `reseat' to set this up properly, via
2923 handle_face_prop. */
2924 it->face_id = it->base_face_id;
2925
2926 it->start = it->current;
2927 /* Do we need to reorder bidirectional text? Not if this is a
2928 unibyte buffer: by definition, none of the single-byte
2929 characters are strong R2L, so no reordering is needed. And
2930 bidi.c doesn't support unibyte buffers anyway. Also, don't
2931 reorder while we are loading loadup.el, since the tables of
2932 character properties needed for reordering are not yet
2933 available. */
2934 it->bidi_p =
2935 NILP (Vpurify_flag)
2936 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2937 && it->multibyte_p;
2938
2939 /* If we are to reorder bidirectional text, init the bidi
2940 iterator. */
2941 if (it->bidi_p)
2942 {
2943 /* Note the paragraph direction that this buffer wants to
2944 use. */
2945 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2946 Qleft_to_right))
2947 it->paragraph_embedding = L2R;
2948 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2949 Qright_to_left))
2950 it->paragraph_embedding = R2L;
2951 else
2952 it->paragraph_embedding = NEUTRAL_DIR;
2953 bidi_unshelve_cache (NULL, 0);
2954 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2955 &it->bidi_it);
2956 }
2957
2958 /* Compute faces etc. */
2959 reseat (it, it->current.pos, 1);
2960 }
2961
2962 CHECK_IT (it);
2963 }
2964
2965
2966 /* Initialize IT for the display of window W with window start POS. */
2967
2968 void
2969 start_display (struct it *it, struct window *w, struct text_pos pos)
2970 {
2971 struct glyph_row *row;
2972 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2973
2974 row = w->desired_matrix->rows + first_vpos;
2975 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2976 it->first_vpos = first_vpos;
2977
2978 /* Don't reseat to previous visible line start if current start
2979 position is in a string or image. */
2980 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2981 {
2982 int start_at_line_beg_p;
2983 int first_y = it->current_y;
2984
2985 /* If window start is not at a line start, skip forward to POS to
2986 get the correct continuation lines width. */
2987 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2988 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2989 if (!start_at_line_beg_p)
2990 {
2991 int new_x;
2992
2993 reseat_at_previous_visible_line_start (it);
2994 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2995
2996 new_x = it->current_x + it->pixel_width;
2997
2998 /* If lines are continued, this line may end in the middle
2999 of a multi-glyph character (e.g. a control character
3000 displayed as \003, or in the middle of an overlay
3001 string). In this case move_it_to above will not have
3002 taken us to the start of the continuation line but to the
3003 end of the continued line. */
3004 if (it->current_x > 0
3005 && it->line_wrap != TRUNCATE /* Lines are continued. */
3006 && (/* And glyph doesn't fit on the line. */
3007 new_x > it->last_visible_x
3008 /* Or it fits exactly and we're on a window
3009 system frame. */
3010 || (new_x == it->last_visible_x
3011 && FRAME_WINDOW_P (it->f)
3012 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3013 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3014 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3015 {
3016 if ((it->current.dpvec_index >= 0
3017 || it->current.overlay_string_index >= 0)
3018 /* If we are on a newline from a display vector or
3019 overlay string, then we are already at the end of
3020 a screen line; no need to go to the next line in
3021 that case, as this line is not really continued.
3022 (If we do go to the next line, C-e will not DTRT.) */
3023 && it->c != '\n')
3024 {
3025 set_iterator_to_next (it, 1);
3026 move_it_in_display_line_to (it, -1, -1, 0);
3027 }
3028
3029 it->continuation_lines_width += it->current_x;
3030 }
3031 /* If the character at POS is displayed via a display
3032 vector, move_it_to above stops at the final glyph of
3033 IT->dpvec. To make the caller redisplay that character
3034 again (a.k.a. start at POS), we need to reset the
3035 dpvec_index to the beginning of IT->dpvec. */
3036 else if (it->current.dpvec_index >= 0)
3037 it->current.dpvec_index = 0;
3038
3039 /* We're starting a new display line, not affected by the
3040 height of the continued line, so clear the appropriate
3041 fields in the iterator structure. */
3042 it->max_ascent = it->max_descent = 0;
3043 it->max_phys_ascent = it->max_phys_descent = 0;
3044
3045 it->current_y = first_y;
3046 it->vpos = 0;
3047 it->current_x = it->hpos = 0;
3048 }
3049 }
3050 }
3051
3052
3053 /* Return 1 if POS is a position in ellipses displayed for invisible
3054 text. W is the window we display, for text property lookup. */
3055
3056 static int
3057 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3058 {
3059 Lisp_Object prop, window;
3060 int ellipses_p = 0;
3061 ptrdiff_t charpos = CHARPOS (pos->pos);
3062
3063 /* If POS specifies a position in a display vector, this might
3064 be for an ellipsis displayed for invisible text. We won't
3065 get the iterator set up for delivering that ellipsis unless
3066 we make sure that it gets aware of the invisible text. */
3067 if (pos->dpvec_index >= 0
3068 && pos->overlay_string_index < 0
3069 && CHARPOS (pos->string_pos) < 0
3070 && charpos > BEGV
3071 && (XSETWINDOW (window, w),
3072 prop = Fget_char_property (make_number (charpos),
3073 Qinvisible, window),
3074 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3075 {
3076 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3077 window);
3078 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3079 }
3080
3081 return ellipses_p;
3082 }
3083
3084
3085 /* Initialize IT for stepping through current_buffer in window W,
3086 starting at position POS that includes overlay string and display
3087 vector/ control character translation position information. Value
3088 is zero if there are overlay strings with newlines at POS. */
3089
3090 static int
3091 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3092 {
3093 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3094 int i, overlay_strings_with_newlines = 0;
3095
3096 /* If POS specifies a position in a display vector, this might
3097 be for an ellipsis displayed for invisible text. We won't
3098 get the iterator set up for delivering that ellipsis unless
3099 we make sure that it gets aware of the invisible text. */
3100 if (in_ellipses_for_invisible_text_p (pos, w))
3101 {
3102 --charpos;
3103 bytepos = 0;
3104 }
3105
3106 /* Keep in mind: the call to reseat in init_iterator skips invisible
3107 text, so we might end up at a position different from POS. This
3108 is only a problem when POS is a row start after a newline and an
3109 overlay starts there with an after-string, and the overlay has an
3110 invisible property. Since we don't skip invisible text in
3111 display_line and elsewhere immediately after consuming the
3112 newline before the row start, such a POS will not be in a string,
3113 but the call to init_iterator below will move us to the
3114 after-string. */
3115 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3116
3117 /* This only scans the current chunk -- it should scan all chunks.
3118 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3119 to 16 in 22.1 to make this a lesser problem. */
3120 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3121 {
3122 const char *s = SSDATA (it->overlay_strings[i]);
3123 const char *e = s + SBYTES (it->overlay_strings[i]);
3124
3125 while (s < e && *s != '\n')
3126 ++s;
3127
3128 if (s < e)
3129 {
3130 overlay_strings_with_newlines = 1;
3131 break;
3132 }
3133 }
3134
3135 /* If position is within an overlay string, set up IT to the right
3136 overlay string. */
3137 if (pos->overlay_string_index >= 0)
3138 {
3139 int relative_index;
3140
3141 /* If the first overlay string happens to have a `display'
3142 property for an image, the iterator will be set up for that
3143 image, and we have to undo that setup first before we can
3144 correct the overlay string index. */
3145 if (it->method == GET_FROM_IMAGE)
3146 pop_it (it);
3147
3148 /* We already have the first chunk of overlay strings in
3149 IT->overlay_strings. Load more until the one for
3150 pos->overlay_string_index is in IT->overlay_strings. */
3151 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3152 {
3153 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3154 it->current.overlay_string_index = 0;
3155 while (n--)
3156 {
3157 load_overlay_strings (it, 0);
3158 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3159 }
3160 }
3161
3162 it->current.overlay_string_index = pos->overlay_string_index;
3163 relative_index = (it->current.overlay_string_index
3164 % OVERLAY_STRING_CHUNK_SIZE);
3165 it->string = it->overlay_strings[relative_index];
3166 eassert (STRINGP (it->string));
3167 it->current.string_pos = pos->string_pos;
3168 it->method = GET_FROM_STRING;
3169 it->end_charpos = SCHARS (it->string);
3170 /* Set up the bidi iterator for this overlay string. */
3171 if (it->bidi_p)
3172 {
3173 it->bidi_it.string.lstring = it->string;
3174 it->bidi_it.string.s = NULL;
3175 it->bidi_it.string.schars = SCHARS (it->string);
3176 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3177 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3178 it->bidi_it.string.unibyte = !it->multibyte_p;
3179 it->bidi_it.w = it->w;
3180 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3181 FRAME_WINDOW_P (it->f), &it->bidi_it);
3182
3183 /* Synchronize the state of the bidi iterator with
3184 pos->string_pos. For any string position other than
3185 zero, this will be done automagically when we resume
3186 iteration over the string and get_visually_first_element
3187 is called. But if string_pos is zero, and the string is
3188 to be reordered for display, we need to resync manually,
3189 since it could be that the iteration state recorded in
3190 pos ended at string_pos of 0 moving backwards in string. */
3191 if (CHARPOS (pos->string_pos) == 0)
3192 {
3193 get_visually_first_element (it);
3194 if (IT_STRING_CHARPOS (*it) != 0)
3195 do {
3196 /* Paranoia. */
3197 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3198 bidi_move_to_visually_next (&it->bidi_it);
3199 } while (it->bidi_it.charpos != 0);
3200 }
3201 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3202 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3203 }
3204 }
3205
3206 if (CHARPOS (pos->string_pos) >= 0)
3207 {
3208 /* Recorded position is not in an overlay string, but in another
3209 string. This can only be a string from a `display' property.
3210 IT should already be filled with that string. */
3211 it->current.string_pos = pos->string_pos;
3212 eassert (STRINGP (it->string));
3213 if (it->bidi_p)
3214 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3215 FRAME_WINDOW_P (it->f), &it->bidi_it);
3216 }
3217
3218 /* Restore position in display vector translations, control
3219 character translations or ellipses. */
3220 if (pos->dpvec_index >= 0)
3221 {
3222 if (it->dpvec == NULL)
3223 get_next_display_element (it);
3224 eassert (it->dpvec && it->current.dpvec_index == 0);
3225 it->current.dpvec_index = pos->dpvec_index;
3226 }
3227
3228 CHECK_IT (it);
3229 return !overlay_strings_with_newlines;
3230 }
3231
3232
3233 /* Initialize IT for stepping through current_buffer in window W
3234 starting at ROW->start. */
3235
3236 static void
3237 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3238 {
3239 init_from_display_pos (it, w, &row->start);
3240 it->start = row->start;
3241 it->continuation_lines_width = row->continuation_lines_width;
3242 CHECK_IT (it);
3243 }
3244
3245
3246 /* Initialize IT for stepping through current_buffer in window W
3247 starting in the line following ROW, i.e. starting at ROW->end.
3248 Value is zero if there are overlay strings with newlines at ROW's
3249 end position. */
3250
3251 static int
3252 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3253 {
3254 int success = 0;
3255
3256 if (init_from_display_pos (it, w, &row->end))
3257 {
3258 if (row->continued_p)
3259 it->continuation_lines_width
3260 = row->continuation_lines_width + row->pixel_width;
3261 CHECK_IT (it);
3262 success = 1;
3263 }
3264
3265 return success;
3266 }
3267
3268
3269
3270 \f
3271 /***********************************************************************
3272 Text properties
3273 ***********************************************************************/
3274
3275 /* Called when IT reaches IT->stop_charpos. Handle text property and
3276 overlay changes. Set IT->stop_charpos to the next position where
3277 to stop. */
3278
3279 static void
3280 handle_stop (struct it *it)
3281 {
3282 enum prop_handled handled;
3283 int handle_overlay_change_p;
3284 struct props *p;
3285
3286 it->dpvec = NULL;
3287 it->current.dpvec_index = -1;
3288 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3289 it->ignore_overlay_strings_at_pos_p = 0;
3290 it->ellipsis_p = 0;
3291
3292 /* Use face of preceding text for ellipsis (if invisible) */
3293 if (it->selective_display_ellipsis_p)
3294 it->saved_face_id = it->face_id;
3295
3296 do
3297 {
3298 handled = HANDLED_NORMALLY;
3299
3300 /* Call text property handlers. */
3301 for (p = it_props; p->handler; ++p)
3302 {
3303 handled = p->handler (it);
3304
3305 if (handled == HANDLED_RECOMPUTE_PROPS)
3306 break;
3307 else if (handled == HANDLED_RETURN)
3308 {
3309 /* We still want to show before and after strings from
3310 overlays even if the actual buffer text is replaced. */
3311 if (!handle_overlay_change_p
3312 || it->sp > 1
3313 /* Don't call get_overlay_strings_1 if we already
3314 have overlay strings loaded, because doing so
3315 will load them again and push the iterator state
3316 onto the stack one more time, which is not
3317 expected by the rest of the code that processes
3318 overlay strings. */
3319 || (it->current.overlay_string_index < 0
3320 ? !get_overlay_strings_1 (it, 0, 0)
3321 : 0))
3322 {
3323 if (it->ellipsis_p)
3324 setup_for_ellipsis (it, 0);
3325 /* When handling a display spec, we might load an
3326 empty string. In that case, discard it here. We
3327 used to discard it in handle_single_display_spec,
3328 but that causes get_overlay_strings_1, above, to
3329 ignore overlay strings that we must check. */
3330 if (STRINGP (it->string) && !SCHARS (it->string))
3331 pop_it (it);
3332 return;
3333 }
3334 else if (STRINGP (it->string) && !SCHARS (it->string))
3335 pop_it (it);
3336 else
3337 {
3338 it->ignore_overlay_strings_at_pos_p = 1;
3339 it->string_from_display_prop_p = 0;
3340 it->from_disp_prop_p = 0;
3341 handle_overlay_change_p = 0;
3342 }
3343 handled = HANDLED_RECOMPUTE_PROPS;
3344 break;
3345 }
3346 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3347 handle_overlay_change_p = 0;
3348 }
3349
3350 if (handled != HANDLED_RECOMPUTE_PROPS)
3351 {
3352 /* Don't check for overlay strings below when set to deliver
3353 characters from a display vector. */
3354 if (it->method == GET_FROM_DISPLAY_VECTOR)
3355 handle_overlay_change_p = 0;
3356
3357 /* Handle overlay changes.
3358 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3359 if it finds overlays. */
3360 if (handle_overlay_change_p)
3361 handled = handle_overlay_change (it);
3362 }
3363
3364 if (it->ellipsis_p)
3365 {
3366 setup_for_ellipsis (it, 0);
3367 break;
3368 }
3369 }
3370 while (handled == HANDLED_RECOMPUTE_PROPS);
3371
3372 /* Determine where to stop next. */
3373 if (handled == HANDLED_NORMALLY)
3374 compute_stop_pos (it);
3375 }
3376
3377
3378 /* Compute IT->stop_charpos from text property and overlay change
3379 information for IT's current position. */
3380
3381 static void
3382 compute_stop_pos (struct it *it)
3383 {
3384 register INTERVAL iv, next_iv;
3385 Lisp_Object object, limit, position;
3386 ptrdiff_t charpos, bytepos;
3387
3388 if (STRINGP (it->string))
3389 {
3390 /* Strings are usually short, so don't limit the search for
3391 properties. */
3392 it->stop_charpos = it->end_charpos;
3393 object = it->string;
3394 limit = Qnil;
3395 charpos = IT_STRING_CHARPOS (*it);
3396 bytepos = IT_STRING_BYTEPOS (*it);
3397 }
3398 else
3399 {
3400 ptrdiff_t pos;
3401
3402 /* If end_charpos is out of range for some reason, such as a
3403 misbehaving display function, rationalize it (Bug#5984). */
3404 if (it->end_charpos > ZV)
3405 it->end_charpos = ZV;
3406 it->stop_charpos = it->end_charpos;
3407
3408 /* If next overlay change is in front of the current stop pos
3409 (which is IT->end_charpos), stop there. Note: value of
3410 next_overlay_change is point-max if no overlay change
3411 follows. */
3412 charpos = IT_CHARPOS (*it);
3413 bytepos = IT_BYTEPOS (*it);
3414 pos = next_overlay_change (charpos);
3415 if (pos < it->stop_charpos)
3416 it->stop_charpos = pos;
3417
3418 /* If showing the region, we have to stop at the region
3419 start or end because the face might change there. */
3420 if (it->region_beg_charpos > 0)
3421 {
3422 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3423 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3424 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3425 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3426 }
3427
3428 /* Set up variables for computing the stop position from text
3429 property changes. */
3430 XSETBUFFER (object, current_buffer);
3431 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3432 }
3433
3434 /* Get the interval containing IT's position. Value is a null
3435 interval if there isn't such an interval. */
3436 position = make_number (charpos);
3437 iv = validate_interval_range (object, &position, &position, 0);
3438 if (iv)
3439 {
3440 Lisp_Object values_here[LAST_PROP_IDX];
3441 struct props *p;
3442
3443 /* Get properties here. */
3444 for (p = it_props; p->handler; ++p)
3445 values_here[p->idx] = textget (iv->plist, *p->name);
3446
3447 /* Look for an interval following iv that has different
3448 properties. */
3449 for (next_iv = next_interval (iv);
3450 (next_iv
3451 && (NILP (limit)
3452 || XFASTINT (limit) > next_iv->position));
3453 next_iv = next_interval (next_iv))
3454 {
3455 for (p = it_props; p->handler; ++p)
3456 {
3457 Lisp_Object new_value;
3458
3459 new_value = textget (next_iv->plist, *p->name);
3460 if (!EQ (values_here[p->idx], new_value))
3461 break;
3462 }
3463
3464 if (p->handler)
3465 break;
3466 }
3467
3468 if (next_iv)
3469 {
3470 if (INTEGERP (limit)
3471 && next_iv->position >= XFASTINT (limit))
3472 /* No text property change up to limit. */
3473 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3474 else
3475 /* Text properties change in next_iv. */
3476 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3477 }
3478 }
3479
3480 if (it->cmp_it.id < 0)
3481 {
3482 ptrdiff_t stoppos = it->end_charpos;
3483
3484 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3485 stoppos = -1;
3486 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3487 stoppos, it->string);
3488 }
3489
3490 eassert (STRINGP (it->string)
3491 || (it->stop_charpos >= BEGV
3492 && it->stop_charpos >= IT_CHARPOS (*it)));
3493 }
3494
3495
3496 /* Return the position of the next overlay change after POS in
3497 current_buffer. Value is point-max if no overlay change
3498 follows. This is like `next-overlay-change' but doesn't use
3499 xmalloc. */
3500
3501 static ptrdiff_t
3502 next_overlay_change (ptrdiff_t pos)
3503 {
3504 ptrdiff_t i, noverlays;
3505 ptrdiff_t endpos;
3506 Lisp_Object *overlays;
3507
3508 /* Get all overlays at the given position. */
3509 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3510
3511 /* If any of these overlays ends before endpos,
3512 use its ending point instead. */
3513 for (i = 0; i < noverlays; ++i)
3514 {
3515 Lisp_Object oend;
3516 ptrdiff_t oendpos;
3517
3518 oend = OVERLAY_END (overlays[i]);
3519 oendpos = OVERLAY_POSITION (oend);
3520 endpos = min (endpos, oendpos);
3521 }
3522
3523 return endpos;
3524 }
3525
3526 /* How many characters forward to search for a display property or
3527 display string. Searching too far forward makes the bidi display
3528 sluggish, especially in small windows. */
3529 #define MAX_DISP_SCAN 250
3530
3531 /* Return the character position of a display string at or after
3532 position specified by POSITION. If no display string exists at or
3533 after POSITION, return ZV. A display string is either an overlay
3534 with `display' property whose value is a string, or a `display'
3535 text property whose value is a string. STRING is data about the
3536 string to iterate; if STRING->lstring is nil, we are iterating a
3537 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3538 on a GUI frame. DISP_PROP is set to zero if we searched
3539 MAX_DISP_SCAN characters forward without finding any display
3540 strings, non-zero otherwise. It is set to 2 if the display string
3541 uses any kind of `(space ...)' spec that will produce a stretch of
3542 white space in the text area. */
3543 ptrdiff_t
3544 compute_display_string_pos (struct text_pos *position,
3545 struct bidi_string_data *string,
3546 struct window *w,
3547 int frame_window_p, int *disp_prop)
3548 {
3549 /* OBJECT = nil means current buffer. */
3550 Lisp_Object object, object1;
3551 Lisp_Object pos, spec, limpos;
3552 int string_p = (string && (STRINGP (string->lstring) || string->s));
3553 ptrdiff_t eob = string_p ? string->schars : ZV;
3554 ptrdiff_t begb = string_p ? 0 : BEGV;
3555 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3556 ptrdiff_t lim =
3557 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3558 struct text_pos tpos;
3559 int rv = 0;
3560
3561 if (string && STRINGP (string->lstring))
3562 object1 = object = string->lstring;
3563 else if (w && !string_p)
3564 {
3565 XSETWINDOW (object, w);
3566 object1 = Qnil;
3567 }
3568 else
3569 object1 = object = Qnil;
3570
3571 *disp_prop = 1;
3572
3573 if (charpos >= eob
3574 /* We don't support display properties whose values are strings
3575 that have display string properties. */
3576 || string->from_disp_str
3577 /* C strings cannot have display properties. */
3578 || (string->s && !STRINGP (object)))
3579 {
3580 *disp_prop = 0;
3581 return eob;
3582 }
3583
3584 /* If the character at CHARPOS is where the display string begins,
3585 return CHARPOS. */
3586 pos = make_number (charpos);
3587 if (STRINGP (object))
3588 bufpos = string->bufpos;
3589 else
3590 bufpos = charpos;
3591 tpos = *position;
3592 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3593 && (charpos <= begb
3594 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3595 object),
3596 spec))
3597 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3598 frame_window_p)))
3599 {
3600 if (rv == 2)
3601 *disp_prop = 2;
3602 return charpos;
3603 }
3604
3605 /* Look forward for the first character with a `display' property
3606 that will replace the underlying text when displayed. */
3607 limpos = make_number (lim);
3608 do {
3609 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3610 CHARPOS (tpos) = XFASTINT (pos);
3611 if (CHARPOS (tpos) >= lim)
3612 {
3613 *disp_prop = 0;
3614 break;
3615 }
3616 if (STRINGP (object))
3617 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3618 else
3619 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3620 spec = Fget_char_property (pos, Qdisplay, object);
3621 if (!STRINGP (object))
3622 bufpos = CHARPOS (tpos);
3623 } while (NILP (spec)
3624 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3625 bufpos, frame_window_p)));
3626 if (rv == 2)
3627 *disp_prop = 2;
3628
3629 return CHARPOS (tpos);
3630 }
3631
3632 /* Return the character position of the end of the display string that
3633 started at CHARPOS. If there's no display string at CHARPOS,
3634 return -1. A display string is either an overlay with `display'
3635 property whose value is a string or a `display' text property whose
3636 value is a string. */
3637 ptrdiff_t
3638 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3639 {
3640 /* OBJECT = nil means current buffer. */
3641 Lisp_Object object =
3642 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3643 Lisp_Object pos = make_number (charpos);
3644 ptrdiff_t eob =
3645 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3646
3647 if (charpos >= eob || (string->s && !STRINGP (object)))
3648 return eob;
3649
3650 /* It could happen that the display property or overlay was removed
3651 since we found it in compute_display_string_pos above. One way
3652 this can happen is if JIT font-lock was called (through
3653 handle_fontified_prop), and jit-lock-functions remove text
3654 properties or overlays from the portion of buffer that includes
3655 CHARPOS. Muse mode is known to do that, for example. In this
3656 case, we return -1 to the caller, to signal that no display
3657 string is actually present at CHARPOS. See bidi_fetch_char for
3658 how this is handled.
3659
3660 An alternative would be to never look for display properties past
3661 it->stop_charpos. But neither compute_display_string_pos nor
3662 bidi_fetch_char that calls it know or care where the next
3663 stop_charpos is. */
3664 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3665 return -1;
3666
3667 /* Look forward for the first character where the `display' property
3668 changes. */
3669 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3670
3671 return XFASTINT (pos);
3672 }
3673
3674
3675 \f
3676 /***********************************************************************
3677 Fontification
3678 ***********************************************************************/
3679
3680 /* Handle changes in the `fontified' property of the current buffer by
3681 calling hook functions from Qfontification_functions to fontify
3682 regions of text. */
3683
3684 static enum prop_handled
3685 handle_fontified_prop (struct it *it)
3686 {
3687 Lisp_Object prop, pos;
3688 enum prop_handled handled = HANDLED_NORMALLY;
3689
3690 if (!NILP (Vmemory_full))
3691 return handled;
3692
3693 /* Get the value of the `fontified' property at IT's current buffer
3694 position. (The `fontified' property doesn't have a special
3695 meaning in strings.) If the value is nil, call functions from
3696 Qfontification_functions. */
3697 if (!STRINGP (it->string)
3698 && it->s == NULL
3699 && !NILP (Vfontification_functions)
3700 && !NILP (Vrun_hooks)
3701 && (pos = make_number (IT_CHARPOS (*it)),
3702 prop = Fget_char_property (pos, Qfontified, Qnil),
3703 /* Ignore the special cased nil value always present at EOB since
3704 no amount of fontifying will be able to change it. */
3705 NILP (prop) && IT_CHARPOS (*it) < Z))
3706 {
3707 ptrdiff_t count = SPECPDL_INDEX ();
3708 Lisp_Object val;
3709 struct buffer *obuf = current_buffer;
3710 int begv = BEGV, zv = ZV;
3711 int old_clip_changed = current_buffer->clip_changed;
3712
3713 val = Vfontification_functions;
3714 specbind (Qfontification_functions, Qnil);
3715
3716 eassert (it->end_charpos == ZV);
3717
3718 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3719 safe_call1 (val, pos);
3720 else
3721 {
3722 Lisp_Object fns, fn;
3723 struct gcpro gcpro1, gcpro2;
3724
3725 fns = Qnil;
3726 GCPRO2 (val, fns);
3727
3728 for (; CONSP (val); val = XCDR (val))
3729 {
3730 fn = XCAR (val);
3731
3732 if (EQ (fn, Qt))
3733 {
3734 /* A value of t indicates this hook has a local
3735 binding; it means to run the global binding too.
3736 In a global value, t should not occur. If it
3737 does, we must ignore it to avoid an endless
3738 loop. */
3739 for (fns = Fdefault_value (Qfontification_functions);
3740 CONSP (fns);
3741 fns = XCDR (fns))
3742 {
3743 fn = XCAR (fns);
3744 if (!EQ (fn, Qt))
3745 safe_call1 (fn, pos);
3746 }
3747 }
3748 else
3749 safe_call1 (fn, pos);
3750 }
3751
3752 UNGCPRO;
3753 }
3754
3755 unbind_to (count, Qnil);
3756
3757 /* Fontification functions routinely call `save-restriction'.
3758 Normally, this tags clip_changed, which can confuse redisplay
3759 (see discussion in Bug#6671). Since we don't perform any
3760 special handling of fontification changes in the case where
3761 `save-restriction' isn't called, there's no point doing so in
3762 this case either. So, if the buffer's restrictions are
3763 actually left unchanged, reset clip_changed. */
3764 if (obuf == current_buffer)
3765 {
3766 if (begv == BEGV && zv == ZV)
3767 current_buffer->clip_changed = old_clip_changed;
3768 }
3769 /* There isn't much we can reasonably do to protect against
3770 misbehaving fontification, but here's a fig leaf. */
3771 else if (BUFFER_LIVE_P (obuf))
3772 set_buffer_internal_1 (obuf);
3773
3774 /* The fontification code may have added/removed text.
3775 It could do even a lot worse, but let's at least protect against
3776 the most obvious case where only the text past `pos' gets changed',
3777 as is/was done in grep.el where some escapes sequences are turned
3778 into face properties (bug#7876). */
3779 it->end_charpos = ZV;
3780
3781 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3782 something. This avoids an endless loop if they failed to
3783 fontify the text for which reason ever. */
3784 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3785 handled = HANDLED_RECOMPUTE_PROPS;
3786 }
3787
3788 return handled;
3789 }
3790
3791
3792 \f
3793 /***********************************************************************
3794 Faces
3795 ***********************************************************************/
3796
3797 /* Set up iterator IT from face properties at its current position.
3798 Called from handle_stop. */
3799
3800 static enum prop_handled
3801 handle_face_prop (struct it *it)
3802 {
3803 int new_face_id;
3804 ptrdiff_t next_stop;
3805
3806 if (!STRINGP (it->string))
3807 {
3808 new_face_id
3809 = face_at_buffer_position (it->w,
3810 IT_CHARPOS (*it),
3811 it->region_beg_charpos,
3812 it->region_end_charpos,
3813 &next_stop,
3814 (IT_CHARPOS (*it)
3815 + TEXT_PROP_DISTANCE_LIMIT),
3816 0, it->base_face_id);
3817
3818 /* Is this a start of a run of characters with box face?
3819 Caveat: this can be called for a freshly initialized
3820 iterator; face_id is -1 in this case. We know that the new
3821 face will not change until limit, i.e. if the new face has a
3822 box, all characters up to limit will have one. But, as
3823 usual, we don't know whether limit is really the end. */
3824 if (new_face_id != it->face_id)
3825 {
3826 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3827 /* If it->face_id is -1, old_face below will be NULL, see
3828 the definition of FACE_FROM_ID. This will happen if this
3829 is the initial call that gets the face. */
3830 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3831
3832 /* If the value of face_id of the iterator is -1, we have to
3833 look in front of IT's position and see whether there is a
3834 face there that's different from new_face_id. */
3835 if (!old_face && IT_CHARPOS (*it) > BEG)
3836 {
3837 int prev_face_id = face_before_it_pos (it);
3838
3839 old_face = FACE_FROM_ID (it->f, prev_face_id);
3840 }
3841
3842 /* If the new face has a box, but the old face does not,
3843 this is the start of a run of characters with box face,
3844 i.e. this character has a shadow on the left side. */
3845 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3846 && (old_face == NULL || !old_face->box));
3847 it->face_box_p = new_face->box != FACE_NO_BOX;
3848 }
3849 }
3850 else
3851 {
3852 int base_face_id;
3853 ptrdiff_t bufpos;
3854 int i;
3855 Lisp_Object from_overlay
3856 = (it->current.overlay_string_index >= 0
3857 ? it->string_overlays[it->current.overlay_string_index
3858 % OVERLAY_STRING_CHUNK_SIZE]
3859 : Qnil);
3860
3861 /* See if we got to this string directly or indirectly from
3862 an overlay property. That includes the before-string or
3863 after-string of an overlay, strings in display properties
3864 provided by an overlay, their text properties, etc.
3865
3866 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3867 if (! NILP (from_overlay))
3868 for (i = it->sp - 1; i >= 0; i--)
3869 {
3870 if (it->stack[i].current.overlay_string_index >= 0)
3871 from_overlay
3872 = it->string_overlays[it->stack[i].current.overlay_string_index
3873 % OVERLAY_STRING_CHUNK_SIZE];
3874 else if (! NILP (it->stack[i].from_overlay))
3875 from_overlay = it->stack[i].from_overlay;
3876
3877 if (!NILP (from_overlay))
3878 break;
3879 }
3880
3881 if (! NILP (from_overlay))
3882 {
3883 bufpos = IT_CHARPOS (*it);
3884 /* For a string from an overlay, the base face depends
3885 only on text properties and ignores overlays. */
3886 base_face_id
3887 = face_for_overlay_string (it->w,
3888 IT_CHARPOS (*it),
3889 it->region_beg_charpos,
3890 it->region_end_charpos,
3891 &next_stop,
3892 (IT_CHARPOS (*it)
3893 + TEXT_PROP_DISTANCE_LIMIT),
3894 0,
3895 from_overlay);
3896 }
3897 else
3898 {
3899 bufpos = 0;
3900
3901 /* For strings from a `display' property, use the face at
3902 IT's current buffer position as the base face to merge
3903 with, so that overlay strings appear in the same face as
3904 surrounding text, unless they specify their own faces.
3905 For strings from wrap-prefix and line-prefix properties,
3906 use the default face, possibly remapped via
3907 Vface_remapping_alist. */
3908 base_face_id = it->string_from_prefix_prop_p
3909 ? (!NILP (Vface_remapping_alist)
3910 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3911 : DEFAULT_FACE_ID)
3912 : underlying_face_id (it);
3913 }
3914
3915 new_face_id = face_at_string_position (it->w,
3916 it->string,
3917 IT_STRING_CHARPOS (*it),
3918 bufpos,
3919 it->region_beg_charpos,
3920 it->region_end_charpos,
3921 &next_stop,
3922 base_face_id, 0);
3923
3924 /* Is this a start of a run of characters with box? Caveat:
3925 this can be called for a freshly allocated iterator; face_id
3926 is -1 is this case. We know that the new face will not
3927 change until the next check pos, i.e. if the new face has a
3928 box, all characters up to that position will have a
3929 box. But, as usual, we don't know whether that position
3930 is really the end. */
3931 if (new_face_id != it->face_id)
3932 {
3933 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3934 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3935
3936 /* If new face has a box but old face hasn't, this is the
3937 start of a run of characters with box, i.e. it has a
3938 shadow on the left side. */
3939 it->start_of_box_run_p
3940 = new_face->box && (old_face == NULL || !old_face->box);
3941 it->face_box_p = new_face->box != FACE_NO_BOX;
3942 }
3943 }
3944
3945 it->face_id = new_face_id;
3946 return HANDLED_NORMALLY;
3947 }
3948
3949
3950 /* Return the ID of the face ``underlying'' IT's current position,
3951 which is in a string. If the iterator is associated with a
3952 buffer, return the face at IT's current buffer position.
3953 Otherwise, use the iterator's base_face_id. */
3954
3955 static int
3956 underlying_face_id (struct it *it)
3957 {
3958 int face_id = it->base_face_id, i;
3959
3960 eassert (STRINGP (it->string));
3961
3962 for (i = it->sp - 1; i >= 0; --i)
3963 if (NILP (it->stack[i].string))
3964 face_id = it->stack[i].face_id;
3965
3966 return face_id;
3967 }
3968
3969
3970 /* Compute the face one character before or after the current position
3971 of IT, in the visual order. BEFORE_P non-zero means get the face
3972 in front (to the left in L2R paragraphs, to the right in R2L
3973 paragraphs) of IT's screen position. Value is the ID of the face. */
3974
3975 static int
3976 face_before_or_after_it_pos (struct it *it, int before_p)
3977 {
3978 int face_id, limit;
3979 ptrdiff_t next_check_charpos;
3980 struct it it_copy;
3981 void *it_copy_data = NULL;
3982
3983 eassert (it->s == NULL);
3984
3985 if (STRINGP (it->string))
3986 {
3987 ptrdiff_t bufpos, charpos;
3988 int base_face_id;
3989
3990 /* No face change past the end of the string (for the case
3991 we are padding with spaces). No face change before the
3992 string start. */
3993 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3994 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3995 return it->face_id;
3996
3997 if (!it->bidi_p)
3998 {
3999 /* Set charpos to the position before or after IT's current
4000 position, in the logical order, which in the non-bidi
4001 case is the same as the visual order. */
4002 if (before_p)
4003 charpos = IT_STRING_CHARPOS (*it) - 1;
4004 else if (it->what == IT_COMPOSITION)
4005 /* For composition, we must check the character after the
4006 composition. */
4007 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4008 else
4009 charpos = IT_STRING_CHARPOS (*it) + 1;
4010 }
4011 else
4012 {
4013 if (before_p)
4014 {
4015 /* With bidi iteration, the character before the current
4016 in the visual order cannot be found by simple
4017 iteration, because "reverse" reordering is not
4018 supported. Instead, we need to use the move_it_*
4019 family of functions. */
4020 /* Ignore face changes before the first visible
4021 character on this display line. */
4022 if (it->current_x <= it->first_visible_x)
4023 return it->face_id;
4024 SAVE_IT (it_copy, *it, it_copy_data);
4025 /* Implementation note: Since move_it_in_display_line
4026 works in the iterator geometry, and thinks the first
4027 character is always the leftmost, even in R2L lines,
4028 we don't need to distinguish between the R2L and L2R
4029 cases here. */
4030 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4031 it_copy.current_x - 1, MOVE_TO_X);
4032 charpos = IT_STRING_CHARPOS (it_copy);
4033 RESTORE_IT (it, it, it_copy_data);
4034 }
4035 else
4036 {
4037 /* Set charpos to the string position of the character
4038 that comes after IT's current position in the visual
4039 order. */
4040 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4041
4042 it_copy = *it;
4043 while (n--)
4044 bidi_move_to_visually_next (&it_copy.bidi_it);
4045
4046 charpos = it_copy.bidi_it.charpos;
4047 }
4048 }
4049 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4050
4051 if (it->current.overlay_string_index >= 0)
4052 bufpos = IT_CHARPOS (*it);
4053 else
4054 bufpos = 0;
4055
4056 base_face_id = underlying_face_id (it);
4057
4058 /* Get the face for ASCII, or unibyte. */
4059 face_id = face_at_string_position (it->w,
4060 it->string,
4061 charpos,
4062 bufpos,
4063 it->region_beg_charpos,
4064 it->region_end_charpos,
4065 &next_check_charpos,
4066 base_face_id, 0);
4067
4068 /* Correct the face for charsets different from ASCII. Do it
4069 for the multibyte case only. The face returned above is
4070 suitable for unibyte text if IT->string is unibyte. */
4071 if (STRING_MULTIBYTE (it->string))
4072 {
4073 struct text_pos pos1 = string_pos (charpos, it->string);
4074 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4075 int c, len;
4076 struct face *face = FACE_FROM_ID (it->f, face_id);
4077
4078 c = string_char_and_length (p, &len);
4079 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4080 }
4081 }
4082 else
4083 {
4084 struct text_pos pos;
4085
4086 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4087 || (IT_CHARPOS (*it) <= BEGV && before_p))
4088 return it->face_id;
4089
4090 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4091 pos = it->current.pos;
4092
4093 if (!it->bidi_p)
4094 {
4095 if (before_p)
4096 DEC_TEXT_POS (pos, it->multibyte_p);
4097 else
4098 {
4099 if (it->what == IT_COMPOSITION)
4100 {
4101 /* For composition, we must check the position after
4102 the composition. */
4103 pos.charpos += it->cmp_it.nchars;
4104 pos.bytepos += it->len;
4105 }
4106 else
4107 INC_TEXT_POS (pos, it->multibyte_p);
4108 }
4109 }
4110 else
4111 {
4112 if (before_p)
4113 {
4114 /* With bidi iteration, the character before the current
4115 in the visual order cannot be found by simple
4116 iteration, because "reverse" reordering is not
4117 supported. Instead, we need to use the move_it_*
4118 family of functions. */
4119 /* Ignore face changes before the first visible
4120 character on this display line. */
4121 if (it->current_x <= it->first_visible_x)
4122 return it->face_id;
4123 SAVE_IT (it_copy, *it, it_copy_data);
4124 /* Implementation note: Since move_it_in_display_line
4125 works in the iterator geometry, and thinks the first
4126 character is always the leftmost, even in R2L lines,
4127 we don't need to distinguish between the R2L and L2R
4128 cases here. */
4129 move_it_in_display_line (&it_copy, ZV,
4130 it_copy.current_x - 1, MOVE_TO_X);
4131 pos = it_copy.current.pos;
4132 RESTORE_IT (it, it, it_copy_data);
4133 }
4134 else
4135 {
4136 /* Set charpos to the buffer position of the character
4137 that comes after IT's current position in the visual
4138 order. */
4139 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4140
4141 it_copy = *it;
4142 while (n--)
4143 bidi_move_to_visually_next (&it_copy.bidi_it);
4144
4145 SET_TEXT_POS (pos,
4146 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4147 }
4148 }
4149 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4150
4151 /* Determine face for CHARSET_ASCII, or unibyte. */
4152 face_id = face_at_buffer_position (it->w,
4153 CHARPOS (pos),
4154 it->region_beg_charpos,
4155 it->region_end_charpos,
4156 &next_check_charpos,
4157 limit, 0, -1);
4158
4159 /* Correct the face for charsets different from ASCII. Do it
4160 for the multibyte case only. The face returned above is
4161 suitable for unibyte text if current_buffer is unibyte. */
4162 if (it->multibyte_p)
4163 {
4164 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4165 struct face *face = FACE_FROM_ID (it->f, face_id);
4166 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4167 }
4168 }
4169
4170 return face_id;
4171 }
4172
4173
4174 \f
4175 /***********************************************************************
4176 Invisible text
4177 ***********************************************************************/
4178
4179 /* Set up iterator IT from invisible properties at its current
4180 position. Called from handle_stop. */
4181
4182 static enum prop_handled
4183 handle_invisible_prop (struct it *it)
4184 {
4185 enum prop_handled handled = HANDLED_NORMALLY;
4186 int invis_p;
4187 Lisp_Object prop;
4188
4189 if (STRINGP (it->string))
4190 {
4191 Lisp_Object end_charpos, limit, charpos;
4192
4193 /* Get the value of the invisible text property at the
4194 current position. Value will be nil if there is no such
4195 property. */
4196 charpos = make_number (IT_STRING_CHARPOS (*it));
4197 prop = Fget_text_property (charpos, Qinvisible, it->string);
4198 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4199
4200 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4201 {
4202 /* Record whether we have to display an ellipsis for the
4203 invisible text. */
4204 int display_ellipsis_p = (invis_p == 2);
4205 ptrdiff_t len, endpos;
4206
4207 handled = HANDLED_RECOMPUTE_PROPS;
4208
4209 /* Get the position at which the next visible text can be
4210 found in IT->string, if any. */
4211 endpos = len = SCHARS (it->string);
4212 XSETINT (limit, len);
4213 do
4214 {
4215 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4216 it->string, limit);
4217 if (INTEGERP (end_charpos))
4218 {
4219 endpos = XFASTINT (end_charpos);
4220 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4221 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4222 if (invis_p == 2)
4223 display_ellipsis_p = 1;
4224 }
4225 }
4226 while (invis_p && endpos < len);
4227
4228 if (display_ellipsis_p)
4229 it->ellipsis_p = 1;
4230
4231 if (endpos < len)
4232 {
4233 /* Text at END_CHARPOS is visible. Move IT there. */
4234 struct text_pos old;
4235 ptrdiff_t oldpos;
4236
4237 old = it->current.string_pos;
4238 oldpos = CHARPOS (old);
4239 if (it->bidi_p)
4240 {
4241 if (it->bidi_it.first_elt
4242 && it->bidi_it.charpos < SCHARS (it->string))
4243 bidi_paragraph_init (it->paragraph_embedding,
4244 &it->bidi_it, 1);
4245 /* Bidi-iterate out of the invisible text. */
4246 do
4247 {
4248 bidi_move_to_visually_next (&it->bidi_it);
4249 }
4250 while (oldpos <= it->bidi_it.charpos
4251 && it->bidi_it.charpos < endpos);
4252
4253 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4254 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4255 if (IT_CHARPOS (*it) >= endpos)
4256 it->prev_stop = endpos;
4257 }
4258 else
4259 {
4260 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4261 compute_string_pos (&it->current.string_pos, old, it->string);
4262 }
4263 }
4264 else
4265 {
4266 /* The rest of the string is invisible. If this is an
4267 overlay string, proceed with the next overlay string
4268 or whatever comes and return a character from there. */
4269 if (it->current.overlay_string_index >= 0
4270 && !display_ellipsis_p)
4271 {
4272 next_overlay_string (it);
4273 /* Don't check for overlay strings when we just
4274 finished processing them. */
4275 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4276 }
4277 else
4278 {
4279 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4280 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4281 }
4282 }
4283 }
4284 }
4285 else
4286 {
4287 ptrdiff_t newpos, next_stop, start_charpos, tem;
4288 Lisp_Object pos, overlay;
4289
4290 /* First of all, is there invisible text at this position? */
4291 tem = start_charpos = IT_CHARPOS (*it);
4292 pos = make_number (tem);
4293 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4294 &overlay);
4295 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4296
4297 /* If we are on invisible text, skip over it. */
4298 if (invis_p && start_charpos < it->end_charpos)
4299 {
4300 /* Record whether we have to display an ellipsis for the
4301 invisible text. */
4302 int display_ellipsis_p = invis_p == 2;
4303
4304 handled = HANDLED_RECOMPUTE_PROPS;
4305
4306 /* Loop skipping over invisible text. The loop is left at
4307 ZV or with IT on the first char being visible again. */
4308 do
4309 {
4310 /* Try to skip some invisible text. Return value is the
4311 position reached which can be equal to where we start
4312 if there is nothing invisible there. This skips both
4313 over invisible text properties and overlays with
4314 invisible property. */
4315 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4316
4317 /* If we skipped nothing at all we weren't at invisible
4318 text in the first place. If everything to the end of
4319 the buffer was skipped, end the loop. */
4320 if (newpos == tem || newpos >= ZV)
4321 invis_p = 0;
4322 else
4323 {
4324 /* We skipped some characters but not necessarily
4325 all there are. Check if we ended up on visible
4326 text. Fget_char_property returns the property of
4327 the char before the given position, i.e. if we
4328 get invis_p = 0, this means that the char at
4329 newpos is visible. */
4330 pos = make_number (newpos);
4331 prop = Fget_char_property (pos, Qinvisible, it->window);
4332 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4333 }
4334
4335 /* If we ended up on invisible text, proceed to
4336 skip starting with next_stop. */
4337 if (invis_p)
4338 tem = next_stop;
4339
4340 /* If there are adjacent invisible texts, don't lose the
4341 second one's ellipsis. */
4342 if (invis_p == 2)
4343 display_ellipsis_p = 1;
4344 }
4345 while (invis_p);
4346
4347 /* The position newpos is now either ZV or on visible text. */
4348 if (it->bidi_p)
4349 {
4350 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4351 int on_newline =
4352 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4353 int after_newline =
4354 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4355
4356 /* If the invisible text ends on a newline or on a
4357 character after a newline, we can avoid the costly,
4358 character by character, bidi iteration to NEWPOS, and
4359 instead simply reseat the iterator there. That's
4360 because all bidi reordering information is tossed at
4361 the newline. This is a big win for modes that hide
4362 complete lines, like Outline, Org, etc. */
4363 if (on_newline || after_newline)
4364 {
4365 struct text_pos tpos;
4366 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4367
4368 SET_TEXT_POS (tpos, newpos, bpos);
4369 reseat_1 (it, tpos, 0);
4370 /* If we reseat on a newline/ZV, we need to prep the
4371 bidi iterator for advancing to the next character
4372 after the newline/EOB, keeping the current paragraph
4373 direction (so that PRODUCE_GLYPHS does TRT wrt
4374 prepending/appending glyphs to a glyph row). */
4375 if (on_newline)
4376 {
4377 it->bidi_it.first_elt = 0;
4378 it->bidi_it.paragraph_dir = pdir;
4379 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4380 it->bidi_it.nchars = 1;
4381 it->bidi_it.ch_len = 1;
4382 }
4383 }
4384 else /* Must use the slow method. */
4385 {
4386 /* With bidi iteration, the region of invisible text
4387 could start and/or end in the middle of a
4388 non-base embedding level. Therefore, we need to
4389 skip invisible text using the bidi iterator,
4390 starting at IT's current position, until we find
4391 ourselves outside of the invisible text.
4392 Skipping invisible text _after_ bidi iteration
4393 avoids affecting the visual order of the
4394 displayed text when invisible properties are
4395 added or removed. */
4396 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4397 {
4398 /* If we were `reseat'ed to a new paragraph,
4399 determine the paragraph base direction. We
4400 need to do it now because
4401 next_element_from_buffer may not have a
4402 chance to do it, if we are going to skip any
4403 text at the beginning, which resets the
4404 FIRST_ELT flag. */
4405 bidi_paragraph_init (it->paragraph_embedding,
4406 &it->bidi_it, 1);
4407 }
4408 do
4409 {
4410 bidi_move_to_visually_next (&it->bidi_it);
4411 }
4412 while (it->stop_charpos <= it->bidi_it.charpos
4413 && it->bidi_it.charpos < newpos);
4414 IT_CHARPOS (*it) = it->bidi_it.charpos;
4415 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4416 /* If we overstepped NEWPOS, record its position in
4417 the iterator, so that we skip invisible text if
4418 later the bidi iteration lands us in the
4419 invisible region again. */
4420 if (IT_CHARPOS (*it) >= newpos)
4421 it->prev_stop = newpos;
4422 }
4423 }
4424 else
4425 {
4426 IT_CHARPOS (*it) = newpos;
4427 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4428 }
4429
4430 /* If there are before-strings at the start of invisible
4431 text, and the text is invisible because of a text
4432 property, arrange to show before-strings because 20.x did
4433 it that way. (If the text is invisible because of an
4434 overlay property instead of a text property, this is
4435 already handled in the overlay code.) */
4436 if (NILP (overlay)
4437 && get_overlay_strings (it, it->stop_charpos))
4438 {
4439 handled = HANDLED_RECOMPUTE_PROPS;
4440 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4441 }
4442 else if (display_ellipsis_p)
4443 {
4444 /* Make sure that the glyphs of the ellipsis will get
4445 correct `charpos' values. If we would not update
4446 it->position here, the glyphs would belong to the
4447 last visible character _before_ the invisible
4448 text, which confuses `set_cursor_from_row'.
4449
4450 We use the last invisible position instead of the
4451 first because this way the cursor is always drawn on
4452 the first "." of the ellipsis, whenever PT is inside
4453 the invisible text. Otherwise the cursor would be
4454 placed _after_ the ellipsis when the point is after the
4455 first invisible character. */
4456 if (!STRINGP (it->object))
4457 {
4458 it->position.charpos = newpos - 1;
4459 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4460 }
4461 it->ellipsis_p = 1;
4462 /* Let the ellipsis display before
4463 considering any properties of the following char.
4464 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4465 handled = HANDLED_RETURN;
4466 }
4467 }
4468 }
4469
4470 return handled;
4471 }
4472
4473
4474 /* Make iterator IT return `...' next.
4475 Replaces LEN characters from buffer. */
4476
4477 static void
4478 setup_for_ellipsis (struct it *it, int len)
4479 {
4480 /* Use the display table definition for `...'. Invalid glyphs
4481 will be handled by the method returning elements from dpvec. */
4482 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4483 {
4484 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4485 it->dpvec = v->contents;
4486 it->dpend = v->contents + v->header.size;
4487 }
4488 else
4489 {
4490 /* Default `...'. */
4491 it->dpvec = default_invis_vector;
4492 it->dpend = default_invis_vector + 3;
4493 }
4494
4495 it->dpvec_char_len = len;
4496 it->current.dpvec_index = 0;
4497 it->dpvec_face_id = -1;
4498
4499 /* Remember the current face id in case glyphs specify faces.
4500 IT's face is restored in set_iterator_to_next.
4501 saved_face_id was set to preceding char's face in handle_stop. */
4502 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4503 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4504
4505 it->method = GET_FROM_DISPLAY_VECTOR;
4506 it->ellipsis_p = 1;
4507 }
4508
4509
4510 \f
4511 /***********************************************************************
4512 'display' property
4513 ***********************************************************************/
4514
4515 /* Set up iterator IT from `display' property at its current position.
4516 Called from handle_stop.
4517 We return HANDLED_RETURN if some part of the display property
4518 overrides the display of the buffer text itself.
4519 Otherwise we return HANDLED_NORMALLY. */
4520
4521 static enum prop_handled
4522 handle_display_prop (struct it *it)
4523 {
4524 Lisp_Object propval, object, overlay;
4525 struct text_pos *position;
4526 ptrdiff_t bufpos;
4527 /* Nonzero if some property replaces the display of the text itself. */
4528 int display_replaced_p = 0;
4529
4530 if (STRINGP (it->string))
4531 {
4532 object = it->string;
4533 position = &it->current.string_pos;
4534 bufpos = CHARPOS (it->current.pos);
4535 }
4536 else
4537 {
4538 XSETWINDOW (object, it->w);
4539 position = &it->current.pos;
4540 bufpos = CHARPOS (*position);
4541 }
4542
4543 /* Reset those iterator values set from display property values. */
4544 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4545 it->space_width = Qnil;
4546 it->font_height = Qnil;
4547 it->voffset = 0;
4548
4549 /* We don't support recursive `display' properties, i.e. string
4550 values that have a string `display' property, that have a string
4551 `display' property etc. */
4552 if (!it->string_from_display_prop_p)
4553 it->area = TEXT_AREA;
4554
4555 propval = get_char_property_and_overlay (make_number (position->charpos),
4556 Qdisplay, object, &overlay);
4557 if (NILP (propval))
4558 return HANDLED_NORMALLY;
4559 /* Now OVERLAY is the overlay that gave us this property, or nil
4560 if it was a text property. */
4561
4562 if (!STRINGP (it->string))
4563 object = it->w->contents;
4564
4565 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4566 position, bufpos,
4567 FRAME_WINDOW_P (it->f));
4568
4569 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4570 }
4571
4572 /* Subroutine of handle_display_prop. Returns non-zero if the display
4573 specification in SPEC is a replacing specification, i.e. it would
4574 replace the text covered by `display' property with something else,
4575 such as an image or a display string. If SPEC includes any kind or
4576 `(space ...) specification, the value is 2; this is used by
4577 compute_display_string_pos, which see.
4578
4579 See handle_single_display_spec for documentation of arguments.
4580 frame_window_p is non-zero if the window being redisplayed is on a
4581 GUI frame; this argument is used only if IT is NULL, see below.
4582
4583 IT can be NULL, if this is called by the bidi reordering code
4584 through compute_display_string_pos, which see. In that case, this
4585 function only examines SPEC, but does not otherwise "handle" it, in
4586 the sense that it doesn't set up members of IT from the display
4587 spec. */
4588 static int
4589 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4590 Lisp_Object overlay, struct text_pos *position,
4591 ptrdiff_t bufpos, int frame_window_p)
4592 {
4593 int replacing_p = 0;
4594 int rv;
4595
4596 if (CONSP (spec)
4597 /* Simple specifications. */
4598 && !EQ (XCAR (spec), Qimage)
4599 && !EQ (XCAR (spec), Qspace)
4600 && !EQ (XCAR (spec), Qwhen)
4601 && !EQ (XCAR (spec), Qslice)
4602 && !EQ (XCAR (spec), Qspace_width)
4603 && !EQ (XCAR (spec), Qheight)
4604 && !EQ (XCAR (spec), Qraise)
4605 /* Marginal area specifications. */
4606 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4607 && !EQ (XCAR (spec), Qleft_fringe)
4608 && !EQ (XCAR (spec), Qright_fringe)
4609 && !NILP (XCAR (spec)))
4610 {
4611 for (; CONSP (spec); spec = XCDR (spec))
4612 {
4613 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4614 overlay, position, bufpos,
4615 replacing_p, frame_window_p)))
4616 {
4617 replacing_p = rv;
4618 /* If some text in a string is replaced, `position' no
4619 longer points to the position of `object'. */
4620 if (!it || STRINGP (object))
4621 break;
4622 }
4623 }
4624 }
4625 else if (VECTORP (spec))
4626 {
4627 ptrdiff_t i;
4628 for (i = 0; i < ASIZE (spec); ++i)
4629 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4630 overlay, position, bufpos,
4631 replacing_p, frame_window_p)))
4632 {
4633 replacing_p = rv;
4634 /* If some text in a string is replaced, `position' no
4635 longer points to the position of `object'. */
4636 if (!it || STRINGP (object))
4637 break;
4638 }
4639 }
4640 else
4641 {
4642 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4643 position, bufpos, 0,
4644 frame_window_p)))
4645 replacing_p = rv;
4646 }
4647
4648 return replacing_p;
4649 }
4650
4651 /* Value is the position of the end of the `display' property starting
4652 at START_POS in OBJECT. */
4653
4654 static struct text_pos
4655 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4656 {
4657 Lisp_Object end;
4658 struct text_pos end_pos;
4659
4660 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4661 Qdisplay, object, Qnil);
4662 CHARPOS (end_pos) = XFASTINT (end);
4663 if (STRINGP (object))
4664 compute_string_pos (&end_pos, start_pos, it->string);
4665 else
4666 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4667
4668 return end_pos;
4669 }
4670
4671
4672 /* Set up IT from a single `display' property specification SPEC. OBJECT
4673 is the object in which the `display' property was found. *POSITION
4674 is the position in OBJECT at which the `display' property was found.
4675 BUFPOS is the buffer position of OBJECT (different from POSITION if
4676 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4677 previously saw a display specification which already replaced text
4678 display with something else, for example an image; we ignore such
4679 properties after the first one has been processed.
4680
4681 OVERLAY is the overlay this `display' property came from,
4682 or nil if it was a text property.
4683
4684 If SPEC is a `space' or `image' specification, and in some other
4685 cases too, set *POSITION to the position where the `display'
4686 property ends.
4687
4688 If IT is NULL, only examine the property specification in SPEC, but
4689 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4690 is intended to be displayed in a window on a GUI frame.
4691
4692 Value is non-zero if something was found which replaces the display
4693 of buffer or string text. */
4694
4695 static int
4696 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4697 Lisp_Object overlay, struct text_pos *position,
4698 ptrdiff_t bufpos, int display_replaced_p,
4699 int frame_window_p)
4700 {
4701 Lisp_Object form;
4702 Lisp_Object location, value;
4703 struct text_pos start_pos = *position;
4704 int valid_p;
4705
4706 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4707 If the result is non-nil, use VALUE instead of SPEC. */
4708 form = Qt;
4709 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4710 {
4711 spec = XCDR (spec);
4712 if (!CONSP (spec))
4713 return 0;
4714 form = XCAR (spec);
4715 spec = XCDR (spec);
4716 }
4717
4718 if (!NILP (form) && !EQ (form, Qt))
4719 {
4720 ptrdiff_t count = SPECPDL_INDEX ();
4721 struct gcpro gcpro1;
4722
4723 /* Bind `object' to the object having the `display' property, a
4724 buffer or string. Bind `position' to the position in the
4725 object where the property was found, and `buffer-position'
4726 to the current position in the buffer. */
4727
4728 if (NILP (object))
4729 XSETBUFFER (object, current_buffer);
4730 specbind (Qobject, object);
4731 specbind (Qposition, make_number (CHARPOS (*position)));
4732 specbind (Qbuffer_position, make_number (bufpos));
4733 GCPRO1 (form);
4734 form = safe_eval (form);
4735 UNGCPRO;
4736 unbind_to (count, Qnil);
4737 }
4738
4739 if (NILP (form))
4740 return 0;
4741
4742 /* Handle `(height HEIGHT)' specifications. */
4743 if (CONSP (spec)
4744 && EQ (XCAR (spec), Qheight)
4745 && CONSP (XCDR (spec)))
4746 {
4747 if (it)
4748 {
4749 if (!FRAME_WINDOW_P (it->f))
4750 return 0;
4751
4752 it->font_height = XCAR (XCDR (spec));
4753 if (!NILP (it->font_height))
4754 {
4755 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4756 int new_height = -1;
4757
4758 if (CONSP (it->font_height)
4759 && (EQ (XCAR (it->font_height), Qplus)
4760 || EQ (XCAR (it->font_height), Qminus))
4761 && CONSP (XCDR (it->font_height))
4762 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4763 {
4764 /* `(+ N)' or `(- N)' where N is an integer. */
4765 int steps = XINT (XCAR (XCDR (it->font_height)));
4766 if (EQ (XCAR (it->font_height), Qplus))
4767 steps = - steps;
4768 it->face_id = smaller_face (it->f, it->face_id, steps);
4769 }
4770 else if (FUNCTIONP (it->font_height))
4771 {
4772 /* Call function with current height as argument.
4773 Value is the new height. */
4774 Lisp_Object height;
4775 height = safe_call1 (it->font_height,
4776 face->lface[LFACE_HEIGHT_INDEX]);
4777 if (NUMBERP (height))
4778 new_height = XFLOATINT (height);
4779 }
4780 else if (NUMBERP (it->font_height))
4781 {
4782 /* Value is a multiple of the canonical char height. */
4783 struct face *f;
4784
4785 f = FACE_FROM_ID (it->f,
4786 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4787 new_height = (XFLOATINT (it->font_height)
4788 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4789 }
4790 else
4791 {
4792 /* Evaluate IT->font_height with `height' bound to the
4793 current specified height to get the new height. */
4794 ptrdiff_t count = SPECPDL_INDEX ();
4795
4796 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4797 value = safe_eval (it->font_height);
4798 unbind_to (count, Qnil);
4799
4800 if (NUMBERP (value))
4801 new_height = XFLOATINT (value);
4802 }
4803
4804 if (new_height > 0)
4805 it->face_id = face_with_height (it->f, it->face_id, new_height);
4806 }
4807 }
4808
4809 return 0;
4810 }
4811
4812 /* Handle `(space-width WIDTH)'. */
4813 if (CONSP (spec)
4814 && EQ (XCAR (spec), Qspace_width)
4815 && CONSP (XCDR (spec)))
4816 {
4817 if (it)
4818 {
4819 if (!FRAME_WINDOW_P (it->f))
4820 return 0;
4821
4822 value = XCAR (XCDR (spec));
4823 if (NUMBERP (value) && XFLOATINT (value) > 0)
4824 it->space_width = value;
4825 }
4826
4827 return 0;
4828 }
4829
4830 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4831 if (CONSP (spec)
4832 && EQ (XCAR (spec), Qslice))
4833 {
4834 Lisp_Object tem;
4835
4836 if (it)
4837 {
4838 if (!FRAME_WINDOW_P (it->f))
4839 return 0;
4840
4841 if (tem = XCDR (spec), CONSP (tem))
4842 {
4843 it->slice.x = XCAR (tem);
4844 if (tem = XCDR (tem), CONSP (tem))
4845 {
4846 it->slice.y = XCAR (tem);
4847 if (tem = XCDR (tem), CONSP (tem))
4848 {
4849 it->slice.width = XCAR (tem);
4850 if (tem = XCDR (tem), CONSP (tem))
4851 it->slice.height = XCAR (tem);
4852 }
4853 }
4854 }
4855 }
4856
4857 return 0;
4858 }
4859
4860 /* Handle `(raise FACTOR)'. */
4861 if (CONSP (spec)
4862 && EQ (XCAR (spec), Qraise)
4863 && CONSP (XCDR (spec)))
4864 {
4865 if (it)
4866 {
4867 if (!FRAME_WINDOW_P (it->f))
4868 return 0;
4869
4870 #ifdef HAVE_WINDOW_SYSTEM
4871 value = XCAR (XCDR (spec));
4872 if (NUMBERP (value))
4873 {
4874 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4875 it->voffset = - (XFLOATINT (value)
4876 * (FONT_HEIGHT (face->font)));
4877 }
4878 #endif /* HAVE_WINDOW_SYSTEM */
4879 }
4880
4881 return 0;
4882 }
4883
4884 /* Don't handle the other kinds of display specifications
4885 inside a string that we got from a `display' property. */
4886 if (it && it->string_from_display_prop_p)
4887 return 0;
4888
4889 /* Characters having this form of property are not displayed, so
4890 we have to find the end of the property. */
4891 if (it)
4892 {
4893 start_pos = *position;
4894 *position = display_prop_end (it, object, start_pos);
4895 }
4896 value = Qnil;
4897
4898 /* Stop the scan at that end position--we assume that all
4899 text properties change there. */
4900 if (it)
4901 it->stop_charpos = position->charpos;
4902
4903 /* Handle `(left-fringe BITMAP [FACE])'
4904 and `(right-fringe BITMAP [FACE])'. */
4905 if (CONSP (spec)
4906 && (EQ (XCAR (spec), Qleft_fringe)
4907 || EQ (XCAR (spec), Qright_fringe))
4908 && CONSP (XCDR (spec)))
4909 {
4910 int fringe_bitmap;
4911
4912 if (it)
4913 {
4914 if (!FRAME_WINDOW_P (it->f))
4915 /* If we return here, POSITION has been advanced
4916 across the text with this property. */
4917 {
4918 /* Synchronize the bidi iterator with POSITION. This is
4919 needed because we are not going to push the iterator
4920 on behalf of this display property, so there will be
4921 no pop_it call to do this synchronization for us. */
4922 if (it->bidi_p)
4923 {
4924 it->position = *position;
4925 iterate_out_of_display_property (it);
4926 *position = it->position;
4927 }
4928 return 1;
4929 }
4930 }
4931 else if (!frame_window_p)
4932 return 1;
4933
4934 #ifdef HAVE_WINDOW_SYSTEM
4935 value = XCAR (XCDR (spec));
4936 if (!SYMBOLP (value)
4937 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4938 /* If we return here, POSITION has been advanced
4939 across the text with this property. */
4940 {
4941 if (it && it->bidi_p)
4942 {
4943 it->position = *position;
4944 iterate_out_of_display_property (it);
4945 *position = it->position;
4946 }
4947 return 1;
4948 }
4949
4950 if (it)
4951 {
4952 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4953
4954 if (CONSP (XCDR (XCDR (spec))))
4955 {
4956 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4957 int face_id2 = lookup_derived_face (it->f, face_name,
4958 FRINGE_FACE_ID, 0);
4959 if (face_id2 >= 0)
4960 face_id = face_id2;
4961 }
4962
4963 /* Save current settings of IT so that we can restore them
4964 when we are finished with the glyph property value. */
4965 push_it (it, position);
4966
4967 it->area = TEXT_AREA;
4968 it->what = IT_IMAGE;
4969 it->image_id = -1; /* no image */
4970 it->position = start_pos;
4971 it->object = NILP (object) ? it->w->contents : object;
4972 it->method = GET_FROM_IMAGE;
4973 it->from_overlay = Qnil;
4974 it->face_id = face_id;
4975 it->from_disp_prop_p = 1;
4976
4977 /* Say that we haven't consumed the characters with
4978 `display' property yet. The call to pop_it in
4979 set_iterator_to_next will clean this up. */
4980 *position = start_pos;
4981
4982 if (EQ (XCAR (spec), Qleft_fringe))
4983 {
4984 it->left_user_fringe_bitmap = fringe_bitmap;
4985 it->left_user_fringe_face_id = face_id;
4986 }
4987 else
4988 {
4989 it->right_user_fringe_bitmap = fringe_bitmap;
4990 it->right_user_fringe_face_id = face_id;
4991 }
4992 }
4993 #endif /* HAVE_WINDOW_SYSTEM */
4994 return 1;
4995 }
4996
4997 /* Prepare to handle `((margin left-margin) ...)',
4998 `((margin right-margin) ...)' and `((margin nil) ...)'
4999 prefixes for display specifications. */
5000 location = Qunbound;
5001 if (CONSP (spec) && CONSP (XCAR (spec)))
5002 {
5003 Lisp_Object tem;
5004
5005 value = XCDR (spec);
5006 if (CONSP (value))
5007 value = XCAR (value);
5008
5009 tem = XCAR (spec);
5010 if (EQ (XCAR (tem), Qmargin)
5011 && (tem = XCDR (tem),
5012 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5013 (NILP (tem)
5014 || EQ (tem, Qleft_margin)
5015 || EQ (tem, Qright_margin))))
5016 location = tem;
5017 }
5018
5019 if (EQ (location, Qunbound))
5020 {
5021 location = Qnil;
5022 value = spec;
5023 }
5024
5025 /* After this point, VALUE is the property after any
5026 margin prefix has been stripped. It must be a string,
5027 an image specification, or `(space ...)'.
5028
5029 LOCATION specifies where to display: `left-margin',
5030 `right-margin' or nil. */
5031
5032 valid_p = (STRINGP (value)
5033 #ifdef HAVE_WINDOW_SYSTEM
5034 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5035 && valid_image_p (value))
5036 #endif /* not HAVE_WINDOW_SYSTEM */
5037 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5038
5039 if (valid_p && !display_replaced_p)
5040 {
5041 int retval = 1;
5042
5043 if (!it)
5044 {
5045 /* Callers need to know whether the display spec is any kind
5046 of `(space ...)' spec that is about to affect text-area
5047 display. */
5048 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5049 retval = 2;
5050 return retval;
5051 }
5052
5053 /* Save current settings of IT so that we can restore them
5054 when we are finished with the glyph property value. */
5055 push_it (it, position);
5056 it->from_overlay = overlay;
5057 it->from_disp_prop_p = 1;
5058
5059 if (NILP (location))
5060 it->area = TEXT_AREA;
5061 else if (EQ (location, Qleft_margin))
5062 it->area = LEFT_MARGIN_AREA;
5063 else
5064 it->area = RIGHT_MARGIN_AREA;
5065
5066 if (STRINGP (value))
5067 {
5068 it->string = value;
5069 it->multibyte_p = STRING_MULTIBYTE (it->string);
5070 it->current.overlay_string_index = -1;
5071 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5072 it->end_charpos = it->string_nchars = SCHARS (it->string);
5073 it->method = GET_FROM_STRING;
5074 it->stop_charpos = 0;
5075 it->prev_stop = 0;
5076 it->base_level_stop = 0;
5077 it->string_from_display_prop_p = 1;
5078 /* Say that we haven't consumed the characters with
5079 `display' property yet. The call to pop_it in
5080 set_iterator_to_next will clean this up. */
5081 if (BUFFERP (object))
5082 *position = start_pos;
5083
5084 /* Force paragraph direction to be that of the parent
5085 object. If the parent object's paragraph direction is
5086 not yet determined, default to L2R. */
5087 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5088 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5089 else
5090 it->paragraph_embedding = L2R;
5091
5092 /* Set up the bidi iterator for this display string. */
5093 if (it->bidi_p)
5094 {
5095 it->bidi_it.string.lstring = it->string;
5096 it->bidi_it.string.s = NULL;
5097 it->bidi_it.string.schars = it->end_charpos;
5098 it->bidi_it.string.bufpos = bufpos;
5099 it->bidi_it.string.from_disp_str = 1;
5100 it->bidi_it.string.unibyte = !it->multibyte_p;
5101 it->bidi_it.w = it->w;
5102 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5103 }
5104 }
5105 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5106 {
5107 it->method = GET_FROM_STRETCH;
5108 it->object = value;
5109 *position = it->position = start_pos;
5110 retval = 1 + (it->area == TEXT_AREA);
5111 }
5112 #ifdef HAVE_WINDOW_SYSTEM
5113 else
5114 {
5115 it->what = IT_IMAGE;
5116 it->image_id = lookup_image (it->f, value);
5117 it->position = start_pos;
5118 it->object = NILP (object) ? it->w->contents : object;
5119 it->method = GET_FROM_IMAGE;
5120
5121 /* Say that we haven't consumed the characters with
5122 `display' property yet. The call to pop_it in
5123 set_iterator_to_next will clean this up. */
5124 *position = start_pos;
5125 }
5126 #endif /* HAVE_WINDOW_SYSTEM */
5127
5128 return retval;
5129 }
5130
5131 /* Invalid property or property not supported. Restore
5132 POSITION to what it was before. */
5133 *position = start_pos;
5134 return 0;
5135 }
5136
5137 /* Check if PROP is a display property value whose text should be
5138 treated as intangible. OVERLAY is the overlay from which PROP
5139 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5140 specify the buffer position covered by PROP. */
5141
5142 int
5143 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5144 ptrdiff_t charpos, ptrdiff_t bytepos)
5145 {
5146 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5147 struct text_pos position;
5148
5149 SET_TEXT_POS (position, charpos, bytepos);
5150 return handle_display_spec (NULL, prop, Qnil, overlay,
5151 &position, charpos, frame_window_p);
5152 }
5153
5154
5155 /* Return 1 if PROP is a display sub-property value containing STRING.
5156
5157 Implementation note: this and the following function are really
5158 special cases of handle_display_spec and
5159 handle_single_display_spec, and should ideally use the same code.
5160 Until they do, these two pairs must be consistent and must be
5161 modified in sync. */
5162
5163 static int
5164 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5165 {
5166 if (EQ (string, prop))
5167 return 1;
5168
5169 /* Skip over `when FORM'. */
5170 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5171 {
5172 prop = XCDR (prop);
5173 if (!CONSP (prop))
5174 return 0;
5175 /* Actually, the condition following `when' should be eval'ed,
5176 like handle_single_display_spec does, and we should return
5177 zero if it evaluates to nil. However, this function is
5178 called only when the buffer was already displayed and some
5179 glyph in the glyph matrix was found to come from a display
5180 string. Therefore, the condition was already evaluated, and
5181 the result was non-nil, otherwise the display string wouldn't
5182 have been displayed and we would have never been called for
5183 this property. Thus, we can skip the evaluation and assume
5184 its result is non-nil. */
5185 prop = XCDR (prop);
5186 }
5187
5188 if (CONSP (prop))
5189 /* Skip over `margin LOCATION'. */
5190 if (EQ (XCAR (prop), Qmargin))
5191 {
5192 prop = XCDR (prop);
5193 if (!CONSP (prop))
5194 return 0;
5195
5196 prop = XCDR (prop);
5197 if (!CONSP (prop))
5198 return 0;
5199 }
5200
5201 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5202 }
5203
5204
5205 /* Return 1 if STRING appears in the `display' property PROP. */
5206
5207 static int
5208 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5209 {
5210 if (CONSP (prop)
5211 && !EQ (XCAR (prop), Qwhen)
5212 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5213 {
5214 /* A list of sub-properties. */
5215 while (CONSP (prop))
5216 {
5217 if (single_display_spec_string_p (XCAR (prop), string))
5218 return 1;
5219 prop = XCDR (prop);
5220 }
5221 }
5222 else if (VECTORP (prop))
5223 {
5224 /* A vector of sub-properties. */
5225 ptrdiff_t i;
5226 for (i = 0; i < ASIZE (prop); ++i)
5227 if (single_display_spec_string_p (AREF (prop, i), string))
5228 return 1;
5229 }
5230 else
5231 return single_display_spec_string_p (prop, string);
5232
5233 return 0;
5234 }
5235
5236 /* Look for STRING in overlays and text properties in the current
5237 buffer, between character positions FROM and TO (excluding TO).
5238 BACK_P non-zero means look back (in this case, TO is supposed to be
5239 less than FROM).
5240 Value is the first character position where STRING was found, or
5241 zero if it wasn't found before hitting TO.
5242
5243 This function may only use code that doesn't eval because it is
5244 called asynchronously from note_mouse_highlight. */
5245
5246 static ptrdiff_t
5247 string_buffer_position_lim (Lisp_Object string,
5248 ptrdiff_t from, ptrdiff_t to, int back_p)
5249 {
5250 Lisp_Object limit, prop, pos;
5251 int found = 0;
5252
5253 pos = make_number (max (from, BEGV));
5254
5255 if (!back_p) /* looking forward */
5256 {
5257 limit = make_number (min (to, ZV));
5258 while (!found && !EQ (pos, limit))
5259 {
5260 prop = Fget_char_property (pos, Qdisplay, Qnil);
5261 if (!NILP (prop) && display_prop_string_p (prop, string))
5262 found = 1;
5263 else
5264 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5265 limit);
5266 }
5267 }
5268 else /* looking back */
5269 {
5270 limit = make_number (max (to, BEGV));
5271 while (!found && !EQ (pos, limit))
5272 {
5273 prop = Fget_char_property (pos, Qdisplay, Qnil);
5274 if (!NILP (prop) && display_prop_string_p (prop, string))
5275 found = 1;
5276 else
5277 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5278 limit);
5279 }
5280 }
5281
5282 return found ? XINT (pos) : 0;
5283 }
5284
5285 /* Determine which buffer position in current buffer STRING comes from.
5286 AROUND_CHARPOS is an approximate position where it could come from.
5287 Value is the buffer position or 0 if it couldn't be determined.
5288
5289 This function is necessary because we don't record buffer positions
5290 in glyphs generated from strings (to keep struct glyph small).
5291 This function may only use code that doesn't eval because it is
5292 called asynchronously from note_mouse_highlight. */
5293
5294 static ptrdiff_t
5295 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5296 {
5297 const int MAX_DISTANCE = 1000;
5298 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5299 around_charpos + MAX_DISTANCE,
5300 0);
5301
5302 if (!found)
5303 found = string_buffer_position_lim (string, around_charpos,
5304 around_charpos - MAX_DISTANCE, 1);
5305 return found;
5306 }
5307
5308
5309 \f
5310 /***********************************************************************
5311 `composition' property
5312 ***********************************************************************/
5313
5314 /* Set up iterator IT from `composition' property at its current
5315 position. Called from handle_stop. */
5316
5317 static enum prop_handled
5318 handle_composition_prop (struct it *it)
5319 {
5320 Lisp_Object prop, string;
5321 ptrdiff_t pos, pos_byte, start, end;
5322
5323 if (STRINGP (it->string))
5324 {
5325 unsigned char *s;
5326
5327 pos = IT_STRING_CHARPOS (*it);
5328 pos_byte = IT_STRING_BYTEPOS (*it);
5329 string = it->string;
5330 s = SDATA (string) + pos_byte;
5331 it->c = STRING_CHAR (s);
5332 }
5333 else
5334 {
5335 pos = IT_CHARPOS (*it);
5336 pos_byte = IT_BYTEPOS (*it);
5337 string = Qnil;
5338 it->c = FETCH_CHAR (pos_byte);
5339 }
5340
5341 /* If there's a valid composition and point is not inside of the
5342 composition (in the case that the composition is from the current
5343 buffer), draw a glyph composed from the composition components. */
5344 if (find_composition (pos, -1, &start, &end, &prop, string)
5345 && composition_valid_p (start, end, prop)
5346 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5347 {
5348 if (start < pos)
5349 /* As we can't handle this situation (perhaps font-lock added
5350 a new composition), we just return here hoping that next
5351 redisplay will detect this composition much earlier. */
5352 return HANDLED_NORMALLY;
5353 if (start != pos)
5354 {
5355 if (STRINGP (it->string))
5356 pos_byte = string_char_to_byte (it->string, start);
5357 else
5358 pos_byte = CHAR_TO_BYTE (start);
5359 }
5360 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5361 prop, string);
5362
5363 if (it->cmp_it.id >= 0)
5364 {
5365 it->cmp_it.ch = -1;
5366 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5367 it->cmp_it.nglyphs = -1;
5368 }
5369 }
5370
5371 return HANDLED_NORMALLY;
5372 }
5373
5374
5375 \f
5376 /***********************************************************************
5377 Overlay strings
5378 ***********************************************************************/
5379
5380 /* The following structure is used to record overlay strings for
5381 later sorting in load_overlay_strings. */
5382
5383 struct overlay_entry
5384 {
5385 Lisp_Object overlay;
5386 Lisp_Object string;
5387 EMACS_INT priority;
5388 int after_string_p;
5389 };
5390
5391
5392 /* Set up iterator IT from overlay strings at its current position.
5393 Called from handle_stop. */
5394
5395 static enum prop_handled
5396 handle_overlay_change (struct it *it)
5397 {
5398 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5399 return HANDLED_RECOMPUTE_PROPS;
5400 else
5401 return HANDLED_NORMALLY;
5402 }
5403
5404
5405 /* Set up the next overlay string for delivery by IT, if there is an
5406 overlay string to deliver. Called by set_iterator_to_next when the
5407 end of the current overlay string is reached. If there are more
5408 overlay strings to display, IT->string and
5409 IT->current.overlay_string_index are set appropriately here.
5410 Otherwise IT->string is set to nil. */
5411
5412 static void
5413 next_overlay_string (struct it *it)
5414 {
5415 ++it->current.overlay_string_index;
5416 if (it->current.overlay_string_index == it->n_overlay_strings)
5417 {
5418 /* No more overlay strings. Restore IT's settings to what
5419 they were before overlay strings were processed, and
5420 continue to deliver from current_buffer. */
5421
5422 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5423 pop_it (it);
5424 eassert (it->sp > 0
5425 || (NILP (it->string)
5426 && it->method == GET_FROM_BUFFER
5427 && it->stop_charpos >= BEGV
5428 && it->stop_charpos <= it->end_charpos));
5429 it->current.overlay_string_index = -1;
5430 it->n_overlay_strings = 0;
5431 it->overlay_strings_charpos = -1;
5432 /* If there's an empty display string on the stack, pop the
5433 stack, to resync the bidi iterator with IT's position. Such
5434 empty strings are pushed onto the stack in
5435 get_overlay_strings_1. */
5436 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5437 pop_it (it);
5438
5439 /* If we're at the end of the buffer, record that we have
5440 processed the overlay strings there already, so that
5441 next_element_from_buffer doesn't try it again. */
5442 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5443 it->overlay_strings_at_end_processed_p = 1;
5444 }
5445 else
5446 {
5447 /* There are more overlay strings to process. If
5448 IT->current.overlay_string_index has advanced to a position
5449 where we must load IT->overlay_strings with more strings, do
5450 it. We must load at the IT->overlay_strings_charpos where
5451 IT->n_overlay_strings was originally computed; when invisible
5452 text is present, this might not be IT_CHARPOS (Bug#7016). */
5453 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5454
5455 if (it->current.overlay_string_index && i == 0)
5456 load_overlay_strings (it, it->overlay_strings_charpos);
5457
5458 /* Initialize IT to deliver display elements from the overlay
5459 string. */
5460 it->string = it->overlay_strings[i];
5461 it->multibyte_p = STRING_MULTIBYTE (it->string);
5462 SET_TEXT_POS (it->current.string_pos, 0, 0);
5463 it->method = GET_FROM_STRING;
5464 it->stop_charpos = 0;
5465 it->end_charpos = SCHARS (it->string);
5466 if (it->cmp_it.stop_pos >= 0)
5467 it->cmp_it.stop_pos = 0;
5468 it->prev_stop = 0;
5469 it->base_level_stop = 0;
5470
5471 /* Set up the bidi iterator for this overlay string. */
5472 if (it->bidi_p)
5473 {
5474 it->bidi_it.string.lstring = it->string;
5475 it->bidi_it.string.s = NULL;
5476 it->bidi_it.string.schars = SCHARS (it->string);
5477 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5478 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5479 it->bidi_it.string.unibyte = !it->multibyte_p;
5480 it->bidi_it.w = it->w;
5481 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5482 }
5483 }
5484
5485 CHECK_IT (it);
5486 }
5487
5488
5489 /* Compare two overlay_entry structures E1 and E2. Used as a
5490 comparison function for qsort in load_overlay_strings. Overlay
5491 strings for the same position are sorted so that
5492
5493 1. All after-strings come in front of before-strings, except
5494 when they come from the same overlay.
5495
5496 2. Within after-strings, strings are sorted so that overlay strings
5497 from overlays with higher priorities come first.
5498
5499 2. Within before-strings, strings are sorted so that overlay
5500 strings from overlays with higher priorities come last.
5501
5502 Value is analogous to strcmp. */
5503
5504
5505 static int
5506 compare_overlay_entries (const void *e1, const void *e2)
5507 {
5508 struct overlay_entry const *entry1 = e1;
5509 struct overlay_entry const *entry2 = e2;
5510 int result;
5511
5512 if (entry1->after_string_p != entry2->after_string_p)
5513 {
5514 /* Let after-strings appear in front of before-strings if
5515 they come from different overlays. */
5516 if (EQ (entry1->overlay, entry2->overlay))
5517 result = entry1->after_string_p ? 1 : -1;
5518 else
5519 result = entry1->after_string_p ? -1 : 1;
5520 }
5521 else if (entry1->priority != entry2->priority)
5522 {
5523 if (entry1->after_string_p)
5524 /* After-strings sorted in order of decreasing priority. */
5525 result = entry2->priority < entry1->priority ? -1 : 1;
5526 else
5527 /* Before-strings sorted in order of increasing priority. */
5528 result = entry1->priority < entry2->priority ? -1 : 1;
5529 }
5530 else
5531 result = 0;
5532
5533 return result;
5534 }
5535
5536
5537 /* Load the vector IT->overlay_strings with overlay strings from IT's
5538 current buffer position, or from CHARPOS if that is > 0. Set
5539 IT->n_overlays to the total number of overlay strings found.
5540
5541 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5542 a time. On entry into load_overlay_strings,
5543 IT->current.overlay_string_index gives the number of overlay
5544 strings that have already been loaded by previous calls to this
5545 function.
5546
5547 IT->add_overlay_start contains an additional overlay start
5548 position to consider for taking overlay strings from, if non-zero.
5549 This position comes into play when the overlay has an `invisible'
5550 property, and both before and after-strings. When we've skipped to
5551 the end of the overlay, because of its `invisible' property, we
5552 nevertheless want its before-string to appear.
5553 IT->add_overlay_start will contain the overlay start position
5554 in this case.
5555
5556 Overlay strings are sorted so that after-string strings come in
5557 front of before-string strings. Within before and after-strings,
5558 strings are sorted by overlay priority. See also function
5559 compare_overlay_entries. */
5560
5561 static void
5562 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5563 {
5564 Lisp_Object overlay, window, str, invisible;
5565 struct Lisp_Overlay *ov;
5566 ptrdiff_t start, end;
5567 ptrdiff_t size = 20;
5568 ptrdiff_t n = 0, i, j;
5569 int invis_p;
5570 struct overlay_entry *entries = alloca (size * sizeof *entries);
5571 USE_SAFE_ALLOCA;
5572
5573 if (charpos <= 0)
5574 charpos = IT_CHARPOS (*it);
5575
5576 /* Append the overlay string STRING of overlay OVERLAY to vector
5577 `entries' which has size `size' and currently contains `n'
5578 elements. AFTER_P non-zero means STRING is an after-string of
5579 OVERLAY. */
5580 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5581 do \
5582 { \
5583 Lisp_Object priority; \
5584 \
5585 if (n == size) \
5586 { \
5587 struct overlay_entry *old = entries; \
5588 SAFE_NALLOCA (entries, 2, size); \
5589 memcpy (entries, old, size * sizeof *entries); \
5590 size *= 2; \
5591 } \
5592 \
5593 entries[n].string = (STRING); \
5594 entries[n].overlay = (OVERLAY); \
5595 priority = Foverlay_get ((OVERLAY), Qpriority); \
5596 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5597 entries[n].after_string_p = (AFTER_P); \
5598 ++n; \
5599 } \
5600 while (0)
5601
5602 /* Process overlay before the overlay center. */
5603 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5604 {
5605 XSETMISC (overlay, ov);
5606 eassert (OVERLAYP (overlay));
5607 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5608 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5609
5610 if (end < charpos)
5611 break;
5612
5613 /* Skip this overlay if it doesn't start or end at IT's current
5614 position. */
5615 if (end != charpos && start != charpos)
5616 continue;
5617
5618 /* Skip this overlay if it doesn't apply to IT->w. */
5619 window = Foverlay_get (overlay, Qwindow);
5620 if (WINDOWP (window) && XWINDOW (window) != it->w)
5621 continue;
5622
5623 /* If the text ``under'' the overlay is invisible, both before-
5624 and after-strings from this overlay are visible; start and
5625 end position are indistinguishable. */
5626 invisible = Foverlay_get (overlay, Qinvisible);
5627 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5628
5629 /* If overlay has a non-empty before-string, record it. */
5630 if ((start == charpos || (end == charpos && invis_p))
5631 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5632 && SCHARS (str))
5633 RECORD_OVERLAY_STRING (overlay, str, 0);
5634
5635 /* If overlay has a non-empty after-string, record it. */
5636 if ((end == charpos || (start == charpos && invis_p))
5637 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5638 && SCHARS (str))
5639 RECORD_OVERLAY_STRING (overlay, str, 1);
5640 }
5641
5642 /* Process overlays after the overlay center. */
5643 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5644 {
5645 XSETMISC (overlay, ov);
5646 eassert (OVERLAYP (overlay));
5647 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5648 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5649
5650 if (start > charpos)
5651 break;
5652
5653 /* Skip this overlay if it doesn't start or end at IT's current
5654 position. */
5655 if (end != charpos && start != charpos)
5656 continue;
5657
5658 /* Skip this overlay if it doesn't apply to IT->w. */
5659 window = Foverlay_get (overlay, Qwindow);
5660 if (WINDOWP (window) && XWINDOW (window) != it->w)
5661 continue;
5662
5663 /* If the text ``under'' the overlay is invisible, it has a zero
5664 dimension, and both before- and after-strings apply. */
5665 invisible = Foverlay_get (overlay, Qinvisible);
5666 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5667
5668 /* If overlay has a non-empty before-string, record it. */
5669 if ((start == charpos || (end == charpos && invis_p))
5670 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5671 && SCHARS (str))
5672 RECORD_OVERLAY_STRING (overlay, str, 0);
5673
5674 /* If overlay has a non-empty after-string, record it. */
5675 if ((end == charpos || (start == charpos && invis_p))
5676 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5677 && SCHARS (str))
5678 RECORD_OVERLAY_STRING (overlay, str, 1);
5679 }
5680
5681 #undef RECORD_OVERLAY_STRING
5682
5683 /* Sort entries. */
5684 if (n > 1)
5685 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5686
5687 /* Record number of overlay strings, and where we computed it. */
5688 it->n_overlay_strings = n;
5689 it->overlay_strings_charpos = charpos;
5690
5691 /* IT->current.overlay_string_index is the number of overlay strings
5692 that have already been consumed by IT. Copy some of the
5693 remaining overlay strings to IT->overlay_strings. */
5694 i = 0;
5695 j = it->current.overlay_string_index;
5696 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5697 {
5698 it->overlay_strings[i] = entries[j].string;
5699 it->string_overlays[i++] = entries[j++].overlay;
5700 }
5701
5702 CHECK_IT (it);
5703 SAFE_FREE ();
5704 }
5705
5706
5707 /* Get the first chunk of overlay strings at IT's current buffer
5708 position, or at CHARPOS if that is > 0. Value is non-zero if at
5709 least one overlay string was found. */
5710
5711 static int
5712 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5713 {
5714 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5715 process. This fills IT->overlay_strings with strings, and sets
5716 IT->n_overlay_strings to the total number of strings to process.
5717 IT->pos.overlay_string_index has to be set temporarily to zero
5718 because load_overlay_strings needs this; it must be set to -1
5719 when no overlay strings are found because a zero value would
5720 indicate a position in the first overlay string. */
5721 it->current.overlay_string_index = 0;
5722 load_overlay_strings (it, charpos);
5723
5724 /* If we found overlay strings, set up IT to deliver display
5725 elements from the first one. Otherwise set up IT to deliver
5726 from current_buffer. */
5727 if (it->n_overlay_strings)
5728 {
5729 /* Make sure we know settings in current_buffer, so that we can
5730 restore meaningful values when we're done with the overlay
5731 strings. */
5732 if (compute_stop_p)
5733 compute_stop_pos (it);
5734 eassert (it->face_id >= 0);
5735
5736 /* Save IT's settings. They are restored after all overlay
5737 strings have been processed. */
5738 eassert (!compute_stop_p || it->sp == 0);
5739
5740 /* When called from handle_stop, there might be an empty display
5741 string loaded. In that case, don't bother saving it. But
5742 don't use this optimization with the bidi iterator, since we
5743 need the corresponding pop_it call to resync the bidi
5744 iterator's position with IT's position, after we are done
5745 with the overlay strings. (The corresponding call to pop_it
5746 in case of an empty display string is in
5747 next_overlay_string.) */
5748 if (!(!it->bidi_p
5749 && STRINGP (it->string) && !SCHARS (it->string)))
5750 push_it (it, NULL);
5751
5752 /* Set up IT to deliver display elements from the first overlay
5753 string. */
5754 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5755 it->string = it->overlay_strings[0];
5756 it->from_overlay = Qnil;
5757 it->stop_charpos = 0;
5758 eassert (STRINGP (it->string));
5759 it->end_charpos = SCHARS (it->string);
5760 it->prev_stop = 0;
5761 it->base_level_stop = 0;
5762 it->multibyte_p = STRING_MULTIBYTE (it->string);
5763 it->method = GET_FROM_STRING;
5764 it->from_disp_prop_p = 0;
5765
5766 /* Force paragraph direction to be that of the parent
5767 buffer. */
5768 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5769 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5770 else
5771 it->paragraph_embedding = L2R;
5772
5773 /* Set up the bidi iterator for this overlay string. */
5774 if (it->bidi_p)
5775 {
5776 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5777
5778 it->bidi_it.string.lstring = it->string;
5779 it->bidi_it.string.s = NULL;
5780 it->bidi_it.string.schars = SCHARS (it->string);
5781 it->bidi_it.string.bufpos = pos;
5782 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5783 it->bidi_it.string.unibyte = !it->multibyte_p;
5784 it->bidi_it.w = it->w;
5785 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5786 }
5787 return 1;
5788 }
5789
5790 it->current.overlay_string_index = -1;
5791 return 0;
5792 }
5793
5794 static int
5795 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5796 {
5797 it->string = Qnil;
5798 it->method = GET_FROM_BUFFER;
5799
5800 (void) get_overlay_strings_1 (it, charpos, 1);
5801
5802 CHECK_IT (it);
5803
5804 /* Value is non-zero if we found at least one overlay string. */
5805 return STRINGP (it->string);
5806 }
5807
5808
5809 \f
5810 /***********************************************************************
5811 Saving and restoring state
5812 ***********************************************************************/
5813
5814 /* Save current settings of IT on IT->stack. Called, for example,
5815 before setting up IT for an overlay string, to be able to restore
5816 IT's settings to what they were after the overlay string has been
5817 processed. If POSITION is non-NULL, it is the position to save on
5818 the stack instead of IT->position. */
5819
5820 static void
5821 push_it (struct it *it, struct text_pos *position)
5822 {
5823 struct iterator_stack_entry *p;
5824
5825 eassert (it->sp < IT_STACK_SIZE);
5826 p = it->stack + it->sp;
5827
5828 p->stop_charpos = it->stop_charpos;
5829 p->prev_stop = it->prev_stop;
5830 p->base_level_stop = it->base_level_stop;
5831 p->cmp_it = it->cmp_it;
5832 eassert (it->face_id >= 0);
5833 p->face_id = it->face_id;
5834 p->string = it->string;
5835 p->method = it->method;
5836 p->from_overlay = it->from_overlay;
5837 switch (p->method)
5838 {
5839 case GET_FROM_IMAGE:
5840 p->u.image.object = it->object;
5841 p->u.image.image_id = it->image_id;
5842 p->u.image.slice = it->slice;
5843 break;
5844 case GET_FROM_STRETCH:
5845 p->u.stretch.object = it->object;
5846 break;
5847 }
5848 p->position = position ? *position : it->position;
5849 p->current = it->current;
5850 p->end_charpos = it->end_charpos;
5851 p->string_nchars = it->string_nchars;
5852 p->area = it->area;
5853 p->multibyte_p = it->multibyte_p;
5854 p->avoid_cursor_p = it->avoid_cursor_p;
5855 p->space_width = it->space_width;
5856 p->font_height = it->font_height;
5857 p->voffset = it->voffset;
5858 p->string_from_display_prop_p = it->string_from_display_prop_p;
5859 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5860 p->display_ellipsis_p = 0;
5861 p->line_wrap = it->line_wrap;
5862 p->bidi_p = it->bidi_p;
5863 p->paragraph_embedding = it->paragraph_embedding;
5864 p->from_disp_prop_p = it->from_disp_prop_p;
5865 ++it->sp;
5866
5867 /* Save the state of the bidi iterator as well. */
5868 if (it->bidi_p)
5869 bidi_push_it (&it->bidi_it);
5870 }
5871
5872 static void
5873 iterate_out_of_display_property (struct it *it)
5874 {
5875 int buffer_p = !STRINGP (it->string);
5876 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5877 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5878
5879 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5880
5881 /* Maybe initialize paragraph direction. If we are at the beginning
5882 of a new paragraph, next_element_from_buffer may not have a
5883 chance to do that. */
5884 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5885 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5886 /* prev_stop can be zero, so check against BEGV as well. */
5887 while (it->bidi_it.charpos >= bob
5888 && it->prev_stop <= it->bidi_it.charpos
5889 && it->bidi_it.charpos < CHARPOS (it->position)
5890 && it->bidi_it.charpos < eob)
5891 bidi_move_to_visually_next (&it->bidi_it);
5892 /* Record the stop_pos we just crossed, for when we cross it
5893 back, maybe. */
5894 if (it->bidi_it.charpos > CHARPOS (it->position))
5895 it->prev_stop = CHARPOS (it->position);
5896 /* If we ended up not where pop_it put us, resync IT's
5897 positional members with the bidi iterator. */
5898 if (it->bidi_it.charpos != CHARPOS (it->position))
5899 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5900 if (buffer_p)
5901 it->current.pos = it->position;
5902 else
5903 it->current.string_pos = it->position;
5904 }
5905
5906 /* Restore IT's settings from IT->stack. Called, for example, when no
5907 more overlay strings must be processed, and we return to delivering
5908 display elements from a buffer, or when the end of a string from a
5909 `display' property is reached and we return to delivering display
5910 elements from an overlay string, or from a buffer. */
5911
5912 static void
5913 pop_it (struct it *it)
5914 {
5915 struct iterator_stack_entry *p;
5916 int from_display_prop = it->from_disp_prop_p;
5917
5918 eassert (it->sp > 0);
5919 --it->sp;
5920 p = it->stack + it->sp;
5921 it->stop_charpos = p->stop_charpos;
5922 it->prev_stop = p->prev_stop;
5923 it->base_level_stop = p->base_level_stop;
5924 it->cmp_it = p->cmp_it;
5925 it->face_id = p->face_id;
5926 it->current = p->current;
5927 it->position = p->position;
5928 it->string = p->string;
5929 it->from_overlay = p->from_overlay;
5930 if (NILP (it->string))
5931 SET_TEXT_POS (it->current.string_pos, -1, -1);
5932 it->method = p->method;
5933 switch (it->method)
5934 {
5935 case GET_FROM_IMAGE:
5936 it->image_id = p->u.image.image_id;
5937 it->object = p->u.image.object;
5938 it->slice = p->u.image.slice;
5939 break;
5940 case GET_FROM_STRETCH:
5941 it->object = p->u.stretch.object;
5942 break;
5943 case GET_FROM_BUFFER:
5944 it->object = it->w->contents;
5945 break;
5946 case GET_FROM_STRING:
5947 it->object = it->string;
5948 break;
5949 case GET_FROM_DISPLAY_VECTOR:
5950 if (it->s)
5951 it->method = GET_FROM_C_STRING;
5952 else if (STRINGP (it->string))
5953 it->method = GET_FROM_STRING;
5954 else
5955 {
5956 it->method = GET_FROM_BUFFER;
5957 it->object = it->w->contents;
5958 }
5959 }
5960 it->end_charpos = p->end_charpos;
5961 it->string_nchars = p->string_nchars;
5962 it->area = p->area;
5963 it->multibyte_p = p->multibyte_p;
5964 it->avoid_cursor_p = p->avoid_cursor_p;
5965 it->space_width = p->space_width;
5966 it->font_height = p->font_height;
5967 it->voffset = p->voffset;
5968 it->string_from_display_prop_p = p->string_from_display_prop_p;
5969 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5970 it->line_wrap = p->line_wrap;
5971 it->bidi_p = p->bidi_p;
5972 it->paragraph_embedding = p->paragraph_embedding;
5973 it->from_disp_prop_p = p->from_disp_prop_p;
5974 if (it->bidi_p)
5975 {
5976 bidi_pop_it (&it->bidi_it);
5977 /* Bidi-iterate until we get out of the portion of text, if any,
5978 covered by a `display' text property or by an overlay with
5979 `display' property. (We cannot just jump there, because the
5980 internal coherency of the bidi iterator state can not be
5981 preserved across such jumps.) We also must determine the
5982 paragraph base direction if the overlay we just processed is
5983 at the beginning of a new paragraph. */
5984 if (from_display_prop
5985 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5986 iterate_out_of_display_property (it);
5987
5988 eassert ((BUFFERP (it->object)
5989 && IT_CHARPOS (*it) == it->bidi_it.charpos
5990 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5991 || (STRINGP (it->object)
5992 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5993 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5994 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5995 }
5996 }
5997
5998
5999 \f
6000 /***********************************************************************
6001 Moving over lines
6002 ***********************************************************************/
6003
6004 /* Set IT's current position to the previous line start. */
6005
6006 static void
6007 back_to_previous_line_start (struct it *it)
6008 {
6009 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6010
6011 DEC_BOTH (cp, bp);
6012 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6013 }
6014
6015
6016 /* Move IT to the next line start.
6017
6018 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6019 we skipped over part of the text (as opposed to moving the iterator
6020 continuously over the text). Otherwise, don't change the value
6021 of *SKIPPED_P.
6022
6023 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6024 iterator on the newline, if it was found.
6025
6026 Newlines may come from buffer text, overlay strings, or strings
6027 displayed via the `display' property. That's the reason we can't
6028 simply use find_newline_no_quit.
6029
6030 Note that this function may not skip over invisible text that is so
6031 because of text properties and immediately follows a newline. If
6032 it would, function reseat_at_next_visible_line_start, when called
6033 from set_iterator_to_next, would effectively make invisible
6034 characters following a newline part of the wrong glyph row, which
6035 leads to wrong cursor motion. */
6036
6037 static int
6038 forward_to_next_line_start (struct it *it, int *skipped_p,
6039 struct bidi_it *bidi_it_prev)
6040 {
6041 ptrdiff_t old_selective;
6042 int newline_found_p, n;
6043 const int MAX_NEWLINE_DISTANCE = 500;
6044
6045 /* If already on a newline, just consume it to avoid unintended
6046 skipping over invisible text below. */
6047 if (it->what == IT_CHARACTER
6048 && it->c == '\n'
6049 && CHARPOS (it->position) == IT_CHARPOS (*it))
6050 {
6051 if (it->bidi_p && bidi_it_prev)
6052 *bidi_it_prev = it->bidi_it;
6053 set_iterator_to_next (it, 0);
6054 it->c = 0;
6055 return 1;
6056 }
6057
6058 /* Don't handle selective display in the following. It's (a)
6059 unnecessary because it's done by the caller, and (b) leads to an
6060 infinite recursion because next_element_from_ellipsis indirectly
6061 calls this function. */
6062 old_selective = it->selective;
6063 it->selective = 0;
6064
6065 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6066 from buffer text. */
6067 for (n = newline_found_p = 0;
6068 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6069 n += STRINGP (it->string) ? 0 : 1)
6070 {
6071 if (!get_next_display_element (it))
6072 return 0;
6073 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6074 if (newline_found_p && it->bidi_p && bidi_it_prev)
6075 *bidi_it_prev = it->bidi_it;
6076 set_iterator_to_next (it, 0);
6077 }
6078
6079 /* If we didn't find a newline near enough, see if we can use a
6080 short-cut. */
6081 if (!newline_found_p)
6082 {
6083 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6084 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6085 1, &bytepos);
6086 Lisp_Object pos;
6087
6088 eassert (!STRINGP (it->string));
6089
6090 /* If there isn't any `display' property in sight, and no
6091 overlays, we can just use the position of the newline in
6092 buffer text. */
6093 if (it->stop_charpos >= limit
6094 || ((pos = Fnext_single_property_change (make_number (start),
6095 Qdisplay, Qnil,
6096 make_number (limit)),
6097 NILP (pos))
6098 && next_overlay_change (start) == ZV))
6099 {
6100 if (!it->bidi_p)
6101 {
6102 IT_CHARPOS (*it) = limit;
6103 IT_BYTEPOS (*it) = bytepos;
6104 }
6105 else
6106 {
6107 struct bidi_it bprev;
6108
6109 /* Help bidi.c avoid expensive searches for display
6110 properties and overlays, by telling it that there are
6111 none up to `limit'. */
6112 if (it->bidi_it.disp_pos < limit)
6113 {
6114 it->bidi_it.disp_pos = limit;
6115 it->bidi_it.disp_prop = 0;
6116 }
6117 do {
6118 bprev = it->bidi_it;
6119 bidi_move_to_visually_next (&it->bidi_it);
6120 } while (it->bidi_it.charpos != limit);
6121 IT_CHARPOS (*it) = limit;
6122 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6123 if (bidi_it_prev)
6124 *bidi_it_prev = bprev;
6125 }
6126 *skipped_p = newline_found_p = 1;
6127 }
6128 else
6129 {
6130 while (get_next_display_element (it)
6131 && !newline_found_p)
6132 {
6133 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6134 if (newline_found_p && it->bidi_p && bidi_it_prev)
6135 *bidi_it_prev = it->bidi_it;
6136 set_iterator_to_next (it, 0);
6137 }
6138 }
6139 }
6140
6141 it->selective = old_selective;
6142 return newline_found_p;
6143 }
6144
6145
6146 /* Set IT's current position to the previous visible line start. Skip
6147 invisible text that is so either due to text properties or due to
6148 selective display. Caution: this does not change IT->current_x and
6149 IT->hpos. */
6150
6151 static void
6152 back_to_previous_visible_line_start (struct it *it)
6153 {
6154 while (IT_CHARPOS (*it) > BEGV)
6155 {
6156 back_to_previous_line_start (it);
6157
6158 if (IT_CHARPOS (*it) <= BEGV)
6159 break;
6160
6161 /* If selective > 0, then lines indented more than its value are
6162 invisible. */
6163 if (it->selective > 0
6164 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6165 it->selective))
6166 continue;
6167
6168 /* Check the newline before point for invisibility. */
6169 {
6170 Lisp_Object prop;
6171 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6172 Qinvisible, it->window);
6173 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6174 continue;
6175 }
6176
6177 if (IT_CHARPOS (*it) <= BEGV)
6178 break;
6179
6180 {
6181 struct it it2;
6182 void *it2data = NULL;
6183 ptrdiff_t pos;
6184 ptrdiff_t beg, end;
6185 Lisp_Object val, overlay;
6186
6187 SAVE_IT (it2, *it, it2data);
6188
6189 /* If newline is part of a composition, continue from start of composition */
6190 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6191 && beg < IT_CHARPOS (*it))
6192 goto replaced;
6193
6194 /* If newline is replaced by a display property, find start of overlay
6195 or interval and continue search from that point. */
6196 pos = --IT_CHARPOS (it2);
6197 --IT_BYTEPOS (it2);
6198 it2.sp = 0;
6199 bidi_unshelve_cache (NULL, 0);
6200 it2.string_from_display_prop_p = 0;
6201 it2.from_disp_prop_p = 0;
6202 if (handle_display_prop (&it2) == HANDLED_RETURN
6203 && !NILP (val = get_char_property_and_overlay
6204 (make_number (pos), Qdisplay, Qnil, &overlay))
6205 && (OVERLAYP (overlay)
6206 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6207 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6208 {
6209 RESTORE_IT (it, it, it2data);
6210 goto replaced;
6211 }
6212
6213 /* Newline is not replaced by anything -- so we are done. */
6214 RESTORE_IT (it, it, it2data);
6215 break;
6216
6217 replaced:
6218 if (beg < BEGV)
6219 beg = BEGV;
6220 IT_CHARPOS (*it) = beg;
6221 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6222 }
6223 }
6224
6225 it->continuation_lines_width = 0;
6226
6227 eassert (IT_CHARPOS (*it) >= BEGV);
6228 eassert (IT_CHARPOS (*it) == BEGV
6229 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6230 CHECK_IT (it);
6231 }
6232
6233
6234 /* Reseat iterator IT at the previous visible line start. Skip
6235 invisible text that is so either due to text properties or due to
6236 selective display. At the end, update IT's overlay information,
6237 face information etc. */
6238
6239 void
6240 reseat_at_previous_visible_line_start (struct it *it)
6241 {
6242 back_to_previous_visible_line_start (it);
6243 reseat (it, it->current.pos, 1);
6244 CHECK_IT (it);
6245 }
6246
6247
6248 /* Reseat iterator IT on the next visible line start in the current
6249 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6250 preceding the line start. Skip over invisible text that is so
6251 because of selective display. Compute faces, overlays etc at the
6252 new position. Note that this function does not skip over text that
6253 is invisible because of text properties. */
6254
6255 static void
6256 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6257 {
6258 int newline_found_p, skipped_p = 0;
6259 struct bidi_it bidi_it_prev;
6260
6261 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6262
6263 /* Skip over lines that are invisible because they are indented
6264 more than the value of IT->selective. */
6265 if (it->selective > 0)
6266 while (IT_CHARPOS (*it) < ZV
6267 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6268 it->selective))
6269 {
6270 eassert (IT_BYTEPOS (*it) == BEGV
6271 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6272 newline_found_p =
6273 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6274 }
6275
6276 /* Position on the newline if that's what's requested. */
6277 if (on_newline_p && newline_found_p)
6278 {
6279 if (STRINGP (it->string))
6280 {
6281 if (IT_STRING_CHARPOS (*it) > 0)
6282 {
6283 if (!it->bidi_p)
6284 {
6285 --IT_STRING_CHARPOS (*it);
6286 --IT_STRING_BYTEPOS (*it);
6287 }
6288 else
6289 {
6290 /* We need to restore the bidi iterator to the state
6291 it had on the newline, and resync the IT's
6292 position with that. */
6293 it->bidi_it = bidi_it_prev;
6294 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6295 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6296 }
6297 }
6298 }
6299 else if (IT_CHARPOS (*it) > BEGV)
6300 {
6301 if (!it->bidi_p)
6302 {
6303 --IT_CHARPOS (*it);
6304 --IT_BYTEPOS (*it);
6305 }
6306 else
6307 {
6308 /* We need to restore the bidi iterator to the state it
6309 had on the newline and resync IT with that. */
6310 it->bidi_it = bidi_it_prev;
6311 IT_CHARPOS (*it) = it->bidi_it.charpos;
6312 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6313 }
6314 reseat (it, it->current.pos, 0);
6315 }
6316 }
6317 else if (skipped_p)
6318 reseat (it, it->current.pos, 0);
6319
6320 CHECK_IT (it);
6321 }
6322
6323
6324 \f
6325 /***********************************************************************
6326 Changing an iterator's position
6327 ***********************************************************************/
6328
6329 /* Change IT's current position to POS in current_buffer. If FORCE_P
6330 is non-zero, always check for text properties at the new position.
6331 Otherwise, text properties are only looked up if POS >=
6332 IT->check_charpos of a property. */
6333
6334 static void
6335 reseat (struct it *it, struct text_pos pos, int force_p)
6336 {
6337 ptrdiff_t original_pos = IT_CHARPOS (*it);
6338
6339 reseat_1 (it, pos, 0);
6340
6341 /* Determine where to check text properties. Avoid doing it
6342 where possible because text property lookup is very expensive. */
6343 if (force_p
6344 || CHARPOS (pos) > it->stop_charpos
6345 || CHARPOS (pos) < original_pos)
6346 {
6347 if (it->bidi_p)
6348 {
6349 /* For bidi iteration, we need to prime prev_stop and
6350 base_level_stop with our best estimations. */
6351 /* Implementation note: Of course, POS is not necessarily a
6352 stop position, so assigning prev_pos to it is a lie; we
6353 should have called compute_stop_backwards. However, if
6354 the current buffer does not include any R2L characters,
6355 that call would be a waste of cycles, because the
6356 iterator will never move back, and thus never cross this
6357 "fake" stop position. So we delay that backward search
6358 until the time we really need it, in next_element_from_buffer. */
6359 if (CHARPOS (pos) != it->prev_stop)
6360 it->prev_stop = CHARPOS (pos);
6361 if (CHARPOS (pos) < it->base_level_stop)
6362 it->base_level_stop = 0; /* meaning it's unknown */
6363 handle_stop (it);
6364 }
6365 else
6366 {
6367 handle_stop (it);
6368 it->prev_stop = it->base_level_stop = 0;
6369 }
6370
6371 }
6372
6373 CHECK_IT (it);
6374 }
6375
6376
6377 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6378 IT->stop_pos to POS, also. */
6379
6380 static void
6381 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6382 {
6383 /* Don't call this function when scanning a C string. */
6384 eassert (it->s == NULL);
6385
6386 /* POS must be a reasonable value. */
6387 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6388
6389 it->current.pos = it->position = pos;
6390 it->end_charpos = ZV;
6391 it->dpvec = NULL;
6392 it->current.dpvec_index = -1;
6393 it->current.overlay_string_index = -1;
6394 IT_STRING_CHARPOS (*it) = -1;
6395 IT_STRING_BYTEPOS (*it) = -1;
6396 it->string = Qnil;
6397 it->method = GET_FROM_BUFFER;
6398 it->object = it->w->contents;
6399 it->area = TEXT_AREA;
6400 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6401 it->sp = 0;
6402 it->string_from_display_prop_p = 0;
6403 it->string_from_prefix_prop_p = 0;
6404
6405 it->from_disp_prop_p = 0;
6406 it->face_before_selective_p = 0;
6407 if (it->bidi_p)
6408 {
6409 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6410 &it->bidi_it);
6411 bidi_unshelve_cache (NULL, 0);
6412 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6413 it->bidi_it.string.s = NULL;
6414 it->bidi_it.string.lstring = Qnil;
6415 it->bidi_it.string.bufpos = 0;
6416 it->bidi_it.string.unibyte = 0;
6417 it->bidi_it.w = it->w;
6418 }
6419
6420 if (set_stop_p)
6421 {
6422 it->stop_charpos = CHARPOS (pos);
6423 it->base_level_stop = CHARPOS (pos);
6424 }
6425 /* This make the information stored in it->cmp_it invalidate. */
6426 it->cmp_it.id = -1;
6427 }
6428
6429
6430 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6431 If S is non-null, it is a C string to iterate over. Otherwise,
6432 STRING gives a Lisp string to iterate over.
6433
6434 If PRECISION > 0, don't return more then PRECISION number of
6435 characters from the string.
6436
6437 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6438 characters have been returned. FIELD_WIDTH < 0 means an infinite
6439 field width.
6440
6441 MULTIBYTE = 0 means disable processing of multibyte characters,
6442 MULTIBYTE > 0 means enable it,
6443 MULTIBYTE < 0 means use IT->multibyte_p.
6444
6445 IT must be initialized via a prior call to init_iterator before
6446 calling this function. */
6447
6448 static void
6449 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6450 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6451 int multibyte)
6452 {
6453 /* No region in strings. */
6454 it->region_beg_charpos = it->region_end_charpos = -1;
6455
6456 /* No text property checks performed by default, but see below. */
6457 it->stop_charpos = -1;
6458
6459 /* Set iterator position and end position. */
6460 memset (&it->current, 0, sizeof it->current);
6461 it->current.overlay_string_index = -1;
6462 it->current.dpvec_index = -1;
6463 eassert (charpos >= 0);
6464
6465 /* If STRING is specified, use its multibyteness, otherwise use the
6466 setting of MULTIBYTE, if specified. */
6467 if (multibyte >= 0)
6468 it->multibyte_p = multibyte > 0;
6469
6470 /* Bidirectional reordering of strings is controlled by the default
6471 value of bidi-display-reordering. Don't try to reorder while
6472 loading loadup.el, as the necessary character property tables are
6473 not yet available. */
6474 it->bidi_p =
6475 NILP (Vpurify_flag)
6476 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6477
6478 if (s == NULL)
6479 {
6480 eassert (STRINGP (string));
6481 it->string = string;
6482 it->s = NULL;
6483 it->end_charpos = it->string_nchars = SCHARS (string);
6484 it->method = GET_FROM_STRING;
6485 it->current.string_pos = string_pos (charpos, string);
6486
6487 if (it->bidi_p)
6488 {
6489 it->bidi_it.string.lstring = string;
6490 it->bidi_it.string.s = NULL;
6491 it->bidi_it.string.schars = it->end_charpos;
6492 it->bidi_it.string.bufpos = 0;
6493 it->bidi_it.string.from_disp_str = 0;
6494 it->bidi_it.string.unibyte = !it->multibyte_p;
6495 it->bidi_it.w = it->w;
6496 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6497 FRAME_WINDOW_P (it->f), &it->bidi_it);
6498 }
6499 }
6500 else
6501 {
6502 it->s = (const unsigned char *) s;
6503 it->string = Qnil;
6504
6505 /* Note that we use IT->current.pos, not it->current.string_pos,
6506 for displaying C strings. */
6507 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6508 if (it->multibyte_p)
6509 {
6510 it->current.pos = c_string_pos (charpos, s, 1);
6511 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6512 }
6513 else
6514 {
6515 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6516 it->end_charpos = it->string_nchars = strlen (s);
6517 }
6518
6519 if (it->bidi_p)
6520 {
6521 it->bidi_it.string.lstring = Qnil;
6522 it->bidi_it.string.s = (const unsigned char *) s;
6523 it->bidi_it.string.schars = it->end_charpos;
6524 it->bidi_it.string.bufpos = 0;
6525 it->bidi_it.string.from_disp_str = 0;
6526 it->bidi_it.string.unibyte = !it->multibyte_p;
6527 it->bidi_it.w = it->w;
6528 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6529 &it->bidi_it);
6530 }
6531 it->method = GET_FROM_C_STRING;
6532 }
6533
6534 /* PRECISION > 0 means don't return more than PRECISION characters
6535 from the string. */
6536 if (precision > 0 && it->end_charpos - charpos > precision)
6537 {
6538 it->end_charpos = it->string_nchars = charpos + precision;
6539 if (it->bidi_p)
6540 it->bidi_it.string.schars = it->end_charpos;
6541 }
6542
6543 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6544 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6545 FIELD_WIDTH < 0 means infinite field width. This is useful for
6546 padding with `-' at the end of a mode line. */
6547 if (field_width < 0)
6548 field_width = INFINITY;
6549 /* Implementation note: We deliberately don't enlarge
6550 it->bidi_it.string.schars here to fit it->end_charpos, because
6551 the bidi iterator cannot produce characters out of thin air. */
6552 if (field_width > it->end_charpos - charpos)
6553 it->end_charpos = charpos + field_width;
6554
6555 /* Use the standard display table for displaying strings. */
6556 if (DISP_TABLE_P (Vstandard_display_table))
6557 it->dp = XCHAR_TABLE (Vstandard_display_table);
6558
6559 it->stop_charpos = charpos;
6560 it->prev_stop = charpos;
6561 it->base_level_stop = 0;
6562 if (it->bidi_p)
6563 {
6564 it->bidi_it.first_elt = 1;
6565 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6566 it->bidi_it.disp_pos = -1;
6567 }
6568 if (s == NULL && it->multibyte_p)
6569 {
6570 ptrdiff_t endpos = SCHARS (it->string);
6571 if (endpos > it->end_charpos)
6572 endpos = it->end_charpos;
6573 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6574 it->string);
6575 }
6576 CHECK_IT (it);
6577 }
6578
6579
6580 \f
6581 /***********************************************************************
6582 Iteration
6583 ***********************************************************************/
6584
6585 /* Map enum it_method value to corresponding next_element_from_* function. */
6586
6587 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6588 {
6589 next_element_from_buffer,
6590 next_element_from_display_vector,
6591 next_element_from_string,
6592 next_element_from_c_string,
6593 next_element_from_image,
6594 next_element_from_stretch
6595 };
6596
6597 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6598
6599
6600 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6601 (possibly with the following characters). */
6602
6603 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6604 ((IT)->cmp_it.id >= 0 \
6605 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6606 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6607 END_CHARPOS, (IT)->w, \
6608 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6609 (IT)->string)))
6610
6611
6612 /* Lookup the char-table Vglyphless_char_display for character C (-1
6613 if we want information for no-font case), and return the display
6614 method symbol. By side-effect, update it->what and
6615 it->glyphless_method. This function is called from
6616 get_next_display_element for each character element, and from
6617 x_produce_glyphs when no suitable font was found. */
6618
6619 Lisp_Object
6620 lookup_glyphless_char_display (int c, struct it *it)
6621 {
6622 Lisp_Object glyphless_method = Qnil;
6623
6624 if (CHAR_TABLE_P (Vglyphless_char_display)
6625 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6626 {
6627 if (c >= 0)
6628 {
6629 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6630 if (CONSP (glyphless_method))
6631 glyphless_method = FRAME_WINDOW_P (it->f)
6632 ? XCAR (glyphless_method)
6633 : XCDR (glyphless_method);
6634 }
6635 else
6636 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6637 }
6638
6639 retry:
6640 if (NILP (glyphless_method))
6641 {
6642 if (c >= 0)
6643 /* The default is to display the character by a proper font. */
6644 return Qnil;
6645 /* The default for the no-font case is to display an empty box. */
6646 glyphless_method = Qempty_box;
6647 }
6648 if (EQ (glyphless_method, Qzero_width))
6649 {
6650 if (c >= 0)
6651 return glyphless_method;
6652 /* This method can't be used for the no-font case. */
6653 glyphless_method = Qempty_box;
6654 }
6655 if (EQ (glyphless_method, Qthin_space))
6656 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6657 else if (EQ (glyphless_method, Qempty_box))
6658 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6659 else if (EQ (glyphless_method, Qhex_code))
6660 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6661 else if (STRINGP (glyphless_method))
6662 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6663 else
6664 {
6665 /* Invalid value. We use the default method. */
6666 glyphless_method = Qnil;
6667 goto retry;
6668 }
6669 it->what = IT_GLYPHLESS;
6670 return glyphless_method;
6671 }
6672
6673 /* Load IT's display element fields with information about the next
6674 display element from the current position of IT. Value is zero if
6675 end of buffer (or C string) is reached. */
6676
6677 static struct frame *last_escape_glyph_frame = NULL;
6678 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6679 static int last_escape_glyph_merged_face_id = 0;
6680
6681 struct frame *last_glyphless_glyph_frame = NULL;
6682 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6683 int last_glyphless_glyph_merged_face_id = 0;
6684
6685 static int
6686 get_next_display_element (struct it *it)
6687 {
6688 /* Non-zero means that we found a display element. Zero means that
6689 we hit the end of what we iterate over. Performance note: the
6690 function pointer `method' used here turns out to be faster than
6691 using a sequence of if-statements. */
6692 int success_p;
6693
6694 get_next:
6695 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6696
6697 if (it->what == IT_CHARACTER)
6698 {
6699 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6700 and only if (a) the resolved directionality of that character
6701 is R..." */
6702 /* FIXME: Do we need an exception for characters from display
6703 tables? */
6704 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6705 it->c = bidi_mirror_char (it->c);
6706 /* Map via display table or translate control characters.
6707 IT->c, IT->len etc. have been set to the next character by
6708 the function call above. If we have a display table, and it
6709 contains an entry for IT->c, translate it. Don't do this if
6710 IT->c itself comes from a display table, otherwise we could
6711 end up in an infinite recursion. (An alternative could be to
6712 count the recursion depth of this function and signal an
6713 error when a certain maximum depth is reached.) Is it worth
6714 it? */
6715 if (success_p && it->dpvec == NULL)
6716 {
6717 Lisp_Object dv;
6718 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6719 int nonascii_space_p = 0;
6720 int nonascii_hyphen_p = 0;
6721 int c = it->c; /* This is the character to display. */
6722
6723 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6724 {
6725 eassert (SINGLE_BYTE_CHAR_P (c));
6726 if (unibyte_display_via_language_environment)
6727 {
6728 c = DECODE_CHAR (unibyte, c);
6729 if (c < 0)
6730 c = BYTE8_TO_CHAR (it->c);
6731 }
6732 else
6733 c = BYTE8_TO_CHAR (it->c);
6734 }
6735
6736 if (it->dp
6737 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6738 VECTORP (dv)))
6739 {
6740 struct Lisp_Vector *v = XVECTOR (dv);
6741
6742 /* Return the first character from the display table
6743 entry, if not empty. If empty, don't display the
6744 current character. */
6745 if (v->header.size)
6746 {
6747 it->dpvec_char_len = it->len;
6748 it->dpvec = v->contents;
6749 it->dpend = v->contents + v->header.size;
6750 it->current.dpvec_index = 0;
6751 it->dpvec_face_id = -1;
6752 it->saved_face_id = it->face_id;
6753 it->method = GET_FROM_DISPLAY_VECTOR;
6754 it->ellipsis_p = 0;
6755 }
6756 else
6757 {
6758 set_iterator_to_next (it, 0);
6759 }
6760 goto get_next;
6761 }
6762
6763 if (! NILP (lookup_glyphless_char_display (c, it)))
6764 {
6765 if (it->what == IT_GLYPHLESS)
6766 goto done;
6767 /* Don't display this character. */
6768 set_iterator_to_next (it, 0);
6769 goto get_next;
6770 }
6771
6772 /* If `nobreak-char-display' is non-nil, we display
6773 non-ASCII spaces and hyphens specially. */
6774 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6775 {
6776 if (c == 0xA0)
6777 nonascii_space_p = 1;
6778 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6779 nonascii_hyphen_p = 1;
6780 }
6781
6782 /* Translate control characters into `\003' or `^C' form.
6783 Control characters coming from a display table entry are
6784 currently not translated because we use IT->dpvec to hold
6785 the translation. This could easily be changed but I
6786 don't believe that it is worth doing.
6787
6788 The characters handled by `nobreak-char-display' must be
6789 translated too.
6790
6791 Non-printable characters and raw-byte characters are also
6792 translated to octal form. */
6793 if (((c < ' ' || c == 127) /* ASCII control chars */
6794 ? (it->area != TEXT_AREA
6795 /* In mode line, treat \n, \t like other crl chars. */
6796 || (c != '\t'
6797 && it->glyph_row
6798 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6799 || (c != '\n' && c != '\t'))
6800 : (nonascii_space_p
6801 || nonascii_hyphen_p
6802 || CHAR_BYTE8_P (c)
6803 || ! CHAR_PRINTABLE_P (c))))
6804 {
6805 /* C is a control character, non-ASCII space/hyphen,
6806 raw-byte, or a non-printable character which must be
6807 displayed either as '\003' or as `^C' where the '\\'
6808 and '^' can be defined in the display table. Fill
6809 IT->ctl_chars with glyphs for what we have to
6810 display. Then, set IT->dpvec to these glyphs. */
6811 Lisp_Object gc;
6812 int ctl_len;
6813 int face_id;
6814 int lface_id = 0;
6815 int escape_glyph;
6816
6817 /* Handle control characters with ^. */
6818
6819 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6820 {
6821 int g;
6822
6823 g = '^'; /* default glyph for Control */
6824 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6825 if (it->dp
6826 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6827 {
6828 g = GLYPH_CODE_CHAR (gc);
6829 lface_id = GLYPH_CODE_FACE (gc);
6830 }
6831 if (lface_id)
6832 {
6833 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6834 }
6835 else if (it->f == last_escape_glyph_frame
6836 && it->face_id == last_escape_glyph_face_id)
6837 {
6838 face_id = last_escape_glyph_merged_face_id;
6839 }
6840 else
6841 {
6842 /* Merge the escape-glyph face into the current face. */
6843 face_id = merge_faces (it->f, Qescape_glyph, 0,
6844 it->face_id);
6845 last_escape_glyph_frame = it->f;
6846 last_escape_glyph_face_id = it->face_id;
6847 last_escape_glyph_merged_face_id = face_id;
6848 }
6849
6850 XSETINT (it->ctl_chars[0], g);
6851 XSETINT (it->ctl_chars[1], c ^ 0100);
6852 ctl_len = 2;
6853 goto display_control;
6854 }
6855
6856 /* Handle non-ascii space in the mode where it only gets
6857 highlighting. */
6858
6859 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6860 {
6861 /* Merge `nobreak-space' into the current face. */
6862 face_id = merge_faces (it->f, Qnobreak_space, 0,
6863 it->face_id);
6864 XSETINT (it->ctl_chars[0], ' ');
6865 ctl_len = 1;
6866 goto display_control;
6867 }
6868
6869 /* Handle sequences that start with the "escape glyph". */
6870
6871 /* the default escape glyph is \. */
6872 escape_glyph = '\\';
6873
6874 if (it->dp
6875 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6876 {
6877 escape_glyph = GLYPH_CODE_CHAR (gc);
6878 lface_id = GLYPH_CODE_FACE (gc);
6879 }
6880 if (lface_id)
6881 {
6882 /* The display table specified a face.
6883 Merge it into face_id and also into escape_glyph. */
6884 face_id = merge_faces (it->f, Qt, lface_id,
6885 it->face_id);
6886 }
6887 else if (it->f == last_escape_glyph_frame
6888 && it->face_id == last_escape_glyph_face_id)
6889 {
6890 face_id = last_escape_glyph_merged_face_id;
6891 }
6892 else
6893 {
6894 /* Merge the escape-glyph face into the current face. */
6895 face_id = merge_faces (it->f, Qescape_glyph, 0,
6896 it->face_id);
6897 last_escape_glyph_frame = it->f;
6898 last_escape_glyph_face_id = it->face_id;
6899 last_escape_glyph_merged_face_id = face_id;
6900 }
6901
6902 /* Draw non-ASCII hyphen with just highlighting: */
6903
6904 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6905 {
6906 XSETINT (it->ctl_chars[0], '-');
6907 ctl_len = 1;
6908 goto display_control;
6909 }
6910
6911 /* Draw non-ASCII space/hyphen with escape glyph: */
6912
6913 if (nonascii_space_p || nonascii_hyphen_p)
6914 {
6915 XSETINT (it->ctl_chars[0], escape_glyph);
6916 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6917 ctl_len = 2;
6918 goto display_control;
6919 }
6920
6921 {
6922 char str[10];
6923 int len, i;
6924
6925 if (CHAR_BYTE8_P (c))
6926 /* Display \200 instead of \17777600. */
6927 c = CHAR_TO_BYTE8 (c);
6928 len = sprintf (str, "%03o", c);
6929
6930 XSETINT (it->ctl_chars[0], escape_glyph);
6931 for (i = 0; i < len; i++)
6932 XSETINT (it->ctl_chars[i + 1], str[i]);
6933 ctl_len = len + 1;
6934 }
6935
6936 display_control:
6937 /* Set up IT->dpvec and return first character from it. */
6938 it->dpvec_char_len = it->len;
6939 it->dpvec = it->ctl_chars;
6940 it->dpend = it->dpvec + ctl_len;
6941 it->current.dpvec_index = 0;
6942 it->dpvec_face_id = face_id;
6943 it->saved_face_id = it->face_id;
6944 it->method = GET_FROM_DISPLAY_VECTOR;
6945 it->ellipsis_p = 0;
6946 goto get_next;
6947 }
6948 it->char_to_display = c;
6949 }
6950 else if (success_p)
6951 {
6952 it->char_to_display = it->c;
6953 }
6954 }
6955
6956 /* Adjust face id for a multibyte character. There are no multibyte
6957 character in unibyte text. */
6958 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6959 && it->multibyte_p
6960 && success_p
6961 && FRAME_WINDOW_P (it->f))
6962 {
6963 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6964
6965 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6966 {
6967 /* Automatic composition with glyph-string. */
6968 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6969
6970 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6971 }
6972 else
6973 {
6974 ptrdiff_t pos = (it->s ? -1
6975 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6976 : IT_CHARPOS (*it));
6977 int c;
6978
6979 if (it->what == IT_CHARACTER)
6980 c = it->char_to_display;
6981 else
6982 {
6983 struct composition *cmp = composition_table[it->cmp_it.id];
6984 int i;
6985
6986 c = ' ';
6987 for (i = 0; i < cmp->glyph_len; i++)
6988 /* TAB in a composition means display glyphs with
6989 padding space on the left or right. */
6990 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6991 break;
6992 }
6993 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6994 }
6995 }
6996
6997 done:
6998 /* Is this character the last one of a run of characters with
6999 box? If yes, set IT->end_of_box_run_p to 1. */
7000 if (it->face_box_p
7001 && it->s == NULL)
7002 {
7003 if (it->method == GET_FROM_STRING && it->sp)
7004 {
7005 int face_id = underlying_face_id (it);
7006 struct face *face = FACE_FROM_ID (it->f, face_id);
7007
7008 if (face)
7009 {
7010 if (face->box == FACE_NO_BOX)
7011 {
7012 /* If the box comes from face properties in a
7013 display string, check faces in that string. */
7014 int string_face_id = face_after_it_pos (it);
7015 it->end_of_box_run_p
7016 = (FACE_FROM_ID (it->f, string_face_id)->box
7017 == FACE_NO_BOX);
7018 }
7019 /* Otherwise, the box comes from the underlying face.
7020 If this is the last string character displayed, check
7021 the next buffer location. */
7022 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7023 && (it->current.overlay_string_index
7024 == it->n_overlay_strings - 1))
7025 {
7026 ptrdiff_t ignore;
7027 int next_face_id;
7028 struct text_pos pos = it->current.pos;
7029 INC_TEXT_POS (pos, it->multibyte_p);
7030
7031 next_face_id = face_at_buffer_position
7032 (it->w, CHARPOS (pos), it->region_beg_charpos,
7033 it->region_end_charpos, &ignore,
7034 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7035 -1);
7036 it->end_of_box_run_p
7037 = (FACE_FROM_ID (it->f, next_face_id)->box
7038 == FACE_NO_BOX);
7039 }
7040 }
7041 }
7042 /* next_element_from_display_vector sets this flag according to
7043 faces of the display vector glyphs, see there. */
7044 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7045 {
7046 int face_id = face_after_it_pos (it);
7047 it->end_of_box_run_p
7048 = (face_id != it->face_id
7049 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7050 }
7051 }
7052 /* If we reached the end of the object we've been iterating (e.g., a
7053 display string or an overlay string), and there's something on
7054 IT->stack, proceed with what's on the stack. It doesn't make
7055 sense to return zero if there's unprocessed stuff on the stack,
7056 because otherwise that stuff will never be displayed. */
7057 if (!success_p && it->sp > 0)
7058 {
7059 set_iterator_to_next (it, 0);
7060 success_p = get_next_display_element (it);
7061 }
7062
7063 /* Value is 0 if end of buffer or string reached. */
7064 return success_p;
7065 }
7066
7067
7068 /* Move IT to the next display element.
7069
7070 RESEAT_P non-zero means if called on a newline in buffer text,
7071 skip to the next visible line start.
7072
7073 Functions get_next_display_element and set_iterator_to_next are
7074 separate because I find this arrangement easier to handle than a
7075 get_next_display_element function that also increments IT's
7076 position. The way it is we can first look at an iterator's current
7077 display element, decide whether it fits on a line, and if it does,
7078 increment the iterator position. The other way around we probably
7079 would either need a flag indicating whether the iterator has to be
7080 incremented the next time, or we would have to implement a
7081 decrement position function which would not be easy to write. */
7082
7083 void
7084 set_iterator_to_next (struct it *it, int reseat_p)
7085 {
7086 /* Reset flags indicating start and end of a sequence of characters
7087 with box. Reset them at the start of this function because
7088 moving the iterator to a new position might set them. */
7089 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7090
7091 switch (it->method)
7092 {
7093 case GET_FROM_BUFFER:
7094 /* The current display element of IT is a character from
7095 current_buffer. Advance in the buffer, and maybe skip over
7096 invisible lines that are so because of selective display. */
7097 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7098 reseat_at_next_visible_line_start (it, 0);
7099 else if (it->cmp_it.id >= 0)
7100 {
7101 /* We are currently getting glyphs from a composition. */
7102 int i;
7103
7104 if (! it->bidi_p)
7105 {
7106 IT_CHARPOS (*it) += it->cmp_it.nchars;
7107 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7108 if (it->cmp_it.to < it->cmp_it.nglyphs)
7109 {
7110 it->cmp_it.from = it->cmp_it.to;
7111 }
7112 else
7113 {
7114 it->cmp_it.id = -1;
7115 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7116 IT_BYTEPOS (*it),
7117 it->end_charpos, Qnil);
7118 }
7119 }
7120 else if (! it->cmp_it.reversed_p)
7121 {
7122 /* Composition created while scanning forward. */
7123 /* Update IT's char/byte positions to point to the first
7124 character of the next grapheme cluster, or to the
7125 character visually after the current composition. */
7126 for (i = 0; i < it->cmp_it.nchars; i++)
7127 bidi_move_to_visually_next (&it->bidi_it);
7128 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7129 IT_CHARPOS (*it) = it->bidi_it.charpos;
7130
7131 if (it->cmp_it.to < it->cmp_it.nglyphs)
7132 {
7133 /* Proceed to the next grapheme cluster. */
7134 it->cmp_it.from = it->cmp_it.to;
7135 }
7136 else
7137 {
7138 /* No more grapheme clusters in this composition.
7139 Find the next stop position. */
7140 ptrdiff_t stop = it->end_charpos;
7141 if (it->bidi_it.scan_dir < 0)
7142 /* Now we are scanning backward and don't know
7143 where to stop. */
7144 stop = -1;
7145 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7146 IT_BYTEPOS (*it), stop, Qnil);
7147 }
7148 }
7149 else
7150 {
7151 /* Composition created while scanning backward. */
7152 /* Update IT's char/byte positions to point to the last
7153 character of the previous grapheme cluster, or the
7154 character visually after the current composition. */
7155 for (i = 0; i < it->cmp_it.nchars; i++)
7156 bidi_move_to_visually_next (&it->bidi_it);
7157 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7158 IT_CHARPOS (*it) = it->bidi_it.charpos;
7159 if (it->cmp_it.from > 0)
7160 {
7161 /* Proceed to the previous grapheme cluster. */
7162 it->cmp_it.to = it->cmp_it.from;
7163 }
7164 else
7165 {
7166 /* No more grapheme clusters in this composition.
7167 Find the next stop position. */
7168 ptrdiff_t stop = it->end_charpos;
7169 if (it->bidi_it.scan_dir < 0)
7170 /* Now we are scanning backward and don't know
7171 where to stop. */
7172 stop = -1;
7173 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7174 IT_BYTEPOS (*it), stop, Qnil);
7175 }
7176 }
7177 }
7178 else
7179 {
7180 eassert (it->len != 0);
7181
7182 if (!it->bidi_p)
7183 {
7184 IT_BYTEPOS (*it) += it->len;
7185 IT_CHARPOS (*it) += 1;
7186 }
7187 else
7188 {
7189 int prev_scan_dir = it->bidi_it.scan_dir;
7190 /* If this is a new paragraph, determine its base
7191 direction (a.k.a. its base embedding level). */
7192 if (it->bidi_it.new_paragraph)
7193 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7194 bidi_move_to_visually_next (&it->bidi_it);
7195 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7196 IT_CHARPOS (*it) = it->bidi_it.charpos;
7197 if (prev_scan_dir != it->bidi_it.scan_dir)
7198 {
7199 /* As the scan direction was changed, we must
7200 re-compute the stop position for composition. */
7201 ptrdiff_t stop = it->end_charpos;
7202 if (it->bidi_it.scan_dir < 0)
7203 stop = -1;
7204 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7205 IT_BYTEPOS (*it), stop, Qnil);
7206 }
7207 }
7208 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7209 }
7210 break;
7211
7212 case GET_FROM_C_STRING:
7213 /* Current display element of IT is from a C string. */
7214 if (!it->bidi_p
7215 /* If the string position is beyond string's end, it means
7216 next_element_from_c_string is padding the string with
7217 blanks, in which case we bypass the bidi iterator,
7218 because it cannot deal with such virtual characters. */
7219 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7220 {
7221 IT_BYTEPOS (*it) += it->len;
7222 IT_CHARPOS (*it) += 1;
7223 }
7224 else
7225 {
7226 bidi_move_to_visually_next (&it->bidi_it);
7227 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7228 IT_CHARPOS (*it) = it->bidi_it.charpos;
7229 }
7230 break;
7231
7232 case GET_FROM_DISPLAY_VECTOR:
7233 /* Current display element of IT is from a display table entry.
7234 Advance in the display table definition. Reset it to null if
7235 end reached, and continue with characters from buffers/
7236 strings. */
7237 ++it->current.dpvec_index;
7238
7239 /* Restore face of the iterator to what they were before the
7240 display vector entry (these entries may contain faces). */
7241 it->face_id = it->saved_face_id;
7242
7243 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7244 {
7245 int recheck_faces = it->ellipsis_p;
7246
7247 if (it->s)
7248 it->method = GET_FROM_C_STRING;
7249 else if (STRINGP (it->string))
7250 it->method = GET_FROM_STRING;
7251 else
7252 {
7253 it->method = GET_FROM_BUFFER;
7254 it->object = it->w->contents;
7255 }
7256
7257 it->dpvec = NULL;
7258 it->current.dpvec_index = -1;
7259
7260 /* Skip over characters which were displayed via IT->dpvec. */
7261 if (it->dpvec_char_len < 0)
7262 reseat_at_next_visible_line_start (it, 1);
7263 else if (it->dpvec_char_len > 0)
7264 {
7265 if (it->method == GET_FROM_STRING
7266 && it->current.overlay_string_index >= 0
7267 && it->n_overlay_strings > 0)
7268 it->ignore_overlay_strings_at_pos_p = 1;
7269 it->len = it->dpvec_char_len;
7270 set_iterator_to_next (it, reseat_p);
7271 }
7272
7273 /* Maybe recheck faces after display vector */
7274 if (recheck_faces)
7275 it->stop_charpos = IT_CHARPOS (*it);
7276 }
7277 break;
7278
7279 case GET_FROM_STRING:
7280 /* Current display element is a character from a Lisp string. */
7281 eassert (it->s == NULL && STRINGP (it->string));
7282 /* Don't advance past string end. These conditions are true
7283 when set_iterator_to_next is called at the end of
7284 get_next_display_element, in which case the Lisp string is
7285 already exhausted, and all we want is pop the iterator
7286 stack. */
7287 if (it->current.overlay_string_index >= 0)
7288 {
7289 /* This is an overlay string, so there's no padding with
7290 spaces, and the number of characters in the string is
7291 where the string ends. */
7292 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7293 goto consider_string_end;
7294 }
7295 else
7296 {
7297 /* Not an overlay string. There could be padding, so test
7298 against it->end_charpos . */
7299 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7300 goto consider_string_end;
7301 }
7302 if (it->cmp_it.id >= 0)
7303 {
7304 int i;
7305
7306 if (! it->bidi_p)
7307 {
7308 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7309 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7310 if (it->cmp_it.to < it->cmp_it.nglyphs)
7311 it->cmp_it.from = it->cmp_it.to;
7312 else
7313 {
7314 it->cmp_it.id = -1;
7315 composition_compute_stop_pos (&it->cmp_it,
7316 IT_STRING_CHARPOS (*it),
7317 IT_STRING_BYTEPOS (*it),
7318 it->end_charpos, it->string);
7319 }
7320 }
7321 else if (! it->cmp_it.reversed_p)
7322 {
7323 for (i = 0; i < it->cmp_it.nchars; i++)
7324 bidi_move_to_visually_next (&it->bidi_it);
7325 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7326 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7327
7328 if (it->cmp_it.to < it->cmp_it.nglyphs)
7329 it->cmp_it.from = it->cmp_it.to;
7330 else
7331 {
7332 ptrdiff_t stop = it->end_charpos;
7333 if (it->bidi_it.scan_dir < 0)
7334 stop = -1;
7335 composition_compute_stop_pos (&it->cmp_it,
7336 IT_STRING_CHARPOS (*it),
7337 IT_STRING_BYTEPOS (*it), stop,
7338 it->string);
7339 }
7340 }
7341 else
7342 {
7343 for (i = 0; i < it->cmp_it.nchars; i++)
7344 bidi_move_to_visually_next (&it->bidi_it);
7345 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7346 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7347 if (it->cmp_it.from > 0)
7348 it->cmp_it.to = it->cmp_it.from;
7349 else
7350 {
7351 ptrdiff_t stop = it->end_charpos;
7352 if (it->bidi_it.scan_dir < 0)
7353 stop = -1;
7354 composition_compute_stop_pos (&it->cmp_it,
7355 IT_STRING_CHARPOS (*it),
7356 IT_STRING_BYTEPOS (*it), stop,
7357 it->string);
7358 }
7359 }
7360 }
7361 else
7362 {
7363 if (!it->bidi_p
7364 /* If the string position is beyond string's end, it
7365 means next_element_from_string is padding the string
7366 with blanks, in which case we bypass the bidi
7367 iterator, because it cannot deal with such virtual
7368 characters. */
7369 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7370 {
7371 IT_STRING_BYTEPOS (*it) += it->len;
7372 IT_STRING_CHARPOS (*it) += 1;
7373 }
7374 else
7375 {
7376 int prev_scan_dir = it->bidi_it.scan_dir;
7377
7378 bidi_move_to_visually_next (&it->bidi_it);
7379 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7380 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7381 if (prev_scan_dir != it->bidi_it.scan_dir)
7382 {
7383 ptrdiff_t stop = it->end_charpos;
7384
7385 if (it->bidi_it.scan_dir < 0)
7386 stop = -1;
7387 composition_compute_stop_pos (&it->cmp_it,
7388 IT_STRING_CHARPOS (*it),
7389 IT_STRING_BYTEPOS (*it), stop,
7390 it->string);
7391 }
7392 }
7393 }
7394
7395 consider_string_end:
7396
7397 if (it->current.overlay_string_index >= 0)
7398 {
7399 /* IT->string is an overlay string. Advance to the
7400 next, if there is one. */
7401 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7402 {
7403 it->ellipsis_p = 0;
7404 next_overlay_string (it);
7405 if (it->ellipsis_p)
7406 setup_for_ellipsis (it, 0);
7407 }
7408 }
7409 else
7410 {
7411 /* IT->string is not an overlay string. If we reached
7412 its end, and there is something on IT->stack, proceed
7413 with what is on the stack. This can be either another
7414 string, this time an overlay string, or a buffer. */
7415 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7416 && it->sp > 0)
7417 {
7418 pop_it (it);
7419 if (it->method == GET_FROM_STRING)
7420 goto consider_string_end;
7421 }
7422 }
7423 break;
7424
7425 case GET_FROM_IMAGE:
7426 case GET_FROM_STRETCH:
7427 /* The position etc with which we have to proceed are on
7428 the stack. The position may be at the end of a string,
7429 if the `display' property takes up the whole string. */
7430 eassert (it->sp > 0);
7431 pop_it (it);
7432 if (it->method == GET_FROM_STRING)
7433 goto consider_string_end;
7434 break;
7435
7436 default:
7437 /* There are no other methods defined, so this should be a bug. */
7438 emacs_abort ();
7439 }
7440
7441 eassert (it->method != GET_FROM_STRING
7442 || (STRINGP (it->string)
7443 && IT_STRING_CHARPOS (*it) >= 0));
7444 }
7445
7446 /* Load IT's display element fields with information about the next
7447 display element which comes from a display table entry or from the
7448 result of translating a control character to one of the forms `^C'
7449 or `\003'.
7450
7451 IT->dpvec holds the glyphs to return as characters.
7452 IT->saved_face_id holds the face id before the display vector--it
7453 is restored into IT->face_id in set_iterator_to_next. */
7454
7455 static int
7456 next_element_from_display_vector (struct it *it)
7457 {
7458 Lisp_Object gc;
7459 int prev_face_id = it->face_id;
7460 int next_face_id;
7461
7462 /* Precondition. */
7463 eassert (it->dpvec && it->current.dpvec_index >= 0);
7464
7465 it->face_id = it->saved_face_id;
7466
7467 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7468 That seemed totally bogus - so I changed it... */
7469 gc = it->dpvec[it->current.dpvec_index];
7470
7471 if (GLYPH_CODE_P (gc))
7472 {
7473 struct face *this_face, *prev_face, *next_face;
7474
7475 it->c = GLYPH_CODE_CHAR (gc);
7476 it->len = CHAR_BYTES (it->c);
7477
7478 /* The entry may contain a face id to use. Such a face id is
7479 the id of a Lisp face, not a realized face. A face id of
7480 zero means no face is specified. */
7481 if (it->dpvec_face_id >= 0)
7482 it->face_id = it->dpvec_face_id;
7483 else
7484 {
7485 int lface_id = GLYPH_CODE_FACE (gc);
7486 if (lface_id > 0)
7487 it->face_id = merge_faces (it->f, Qt, lface_id,
7488 it->saved_face_id);
7489 }
7490
7491 /* Glyphs in the display vector could have the box face, so we
7492 need to set the related flags in the iterator, as
7493 appropriate. */
7494 this_face = FACE_FROM_ID (it->f, it->face_id);
7495 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7496
7497 /* Is this character the first character of a box-face run? */
7498 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7499 && (!prev_face
7500 || prev_face->box == FACE_NO_BOX));
7501
7502 /* For the last character of the box-face run, we need to look
7503 either at the next glyph from the display vector, or at the
7504 face we saw before the display vector. */
7505 next_face_id = it->saved_face_id;
7506 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7507 {
7508 if (it->dpvec_face_id >= 0)
7509 next_face_id = it->dpvec_face_id;
7510 else
7511 {
7512 int lface_id =
7513 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7514
7515 if (lface_id > 0)
7516 next_face_id = merge_faces (it->f, Qt, lface_id,
7517 it->saved_face_id);
7518 }
7519 }
7520 next_face = FACE_FROM_ID (it->f, next_face_id);
7521 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7522 && (!next_face
7523 || next_face->box == FACE_NO_BOX));
7524 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7525 }
7526 else
7527 /* Display table entry is invalid. Return a space. */
7528 it->c = ' ', it->len = 1;
7529
7530 /* Don't change position and object of the iterator here. They are
7531 still the values of the character that had this display table
7532 entry or was translated, and that's what we want. */
7533 it->what = IT_CHARACTER;
7534 return 1;
7535 }
7536
7537 /* Get the first element of string/buffer in the visual order, after
7538 being reseated to a new position in a string or a buffer. */
7539 static void
7540 get_visually_first_element (struct it *it)
7541 {
7542 int string_p = STRINGP (it->string) || it->s;
7543 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7544 ptrdiff_t bob = (string_p ? 0 : BEGV);
7545
7546 if (STRINGP (it->string))
7547 {
7548 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7549 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7550 }
7551 else
7552 {
7553 it->bidi_it.charpos = IT_CHARPOS (*it);
7554 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7555 }
7556
7557 if (it->bidi_it.charpos == eob)
7558 {
7559 /* Nothing to do, but reset the FIRST_ELT flag, like
7560 bidi_paragraph_init does, because we are not going to
7561 call it. */
7562 it->bidi_it.first_elt = 0;
7563 }
7564 else if (it->bidi_it.charpos == bob
7565 || (!string_p
7566 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7567 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7568 {
7569 /* If we are at the beginning of a line/string, we can produce
7570 the next element right away. */
7571 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7572 bidi_move_to_visually_next (&it->bidi_it);
7573 }
7574 else
7575 {
7576 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7577
7578 /* We need to prime the bidi iterator starting at the line's or
7579 string's beginning, before we will be able to produce the
7580 next element. */
7581 if (string_p)
7582 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7583 else
7584 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7585 IT_BYTEPOS (*it), -1,
7586 &it->bidi_it.bytepos);
7587 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7588 do
7589 {
7590 /* Now return to buffer/string position where we were asked
7591 to get the next display element, and produce that. */
7592 bidi_move_to_visually_next (&it->bidi_it);
7593 }
7594 while (it->bidi_it.bytepos != orig_bytepos
7595 && it->bidi_it.charpos < eob);
7596 }
7597
7598 /* Adjust IT's position information to where we ended up. */
7599 if (STRINGP (it->string))
7600 {
7601 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7602 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7603 }
7604 else
7605 {
7606 IT_CHARPOS (*it) = it->bidi_it.charpos;
7607 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7608 }
7609
7610 if (STRINGP (it->string) || !it->s)
7611 {
7612 ptrdiff_t stop, charpos, bytepos;
7613
7614 if (STRINGP (it->string))
7615 {
7616 eassert (!it->s);
7617 stop = SCHARS (it->string);
7618 if (stop > it->end_charpos)
7619 stop = it->end_charpos;
7620 charpos = IT_STRING_CHARPOS (*it);
7621 bytepos = IT_STRING_BYTEPOS (*it);
7622 }
7623 else
7624 {
7625 stop = it->end_charpos;
7626 charpos = IT_CHARPOS (*it);
7627 bytepos = IT_BYTEPOS (*it);
7628 }
7629 if (it->bidi_it.scan_dir < 0)
7630 stop = -1;
7631 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7632 it->string);
7633 }
7634 }
7635
7636 /* Load IT with the next display element from Lisp string IT->string.
7637 IT->current.string_pos is the current position within the string.
7638 If IT->current.overlay_string_index >= 0, the Lisp string is an
7639 overlay string. */
7640
7641 static int
7642 next_element_from_string (struct it *it)
7643 {
7644 struct text_pos position;
7645
7646 eassert (STRINGP (it->string));
7647 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7648 eassert (IT_STRING_CHARPOS (*it) >= 0);
7649 position = it->current.string_pos;
7650
7651 /* With bidi reordering, the character to display might not be the
7652 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7653 that we were reseat()ed to a new string, whose paragraph
7654 direction is not known. */
7655 if (it->bidi_p && it->bidi_it.first_elt)
7656 {
7657 get_visually_first_element (it);
7658 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7659 }
7660
7661 /* Time to check for invisible text? */
7662 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7663 {
7664 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7665 {
7666 if (!(!it->bidi_p
7667 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7668 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7669 {
7670 /* With bidi non-linear iteration, we could find
7671 ourselves far beyond the last computed stop_charpos,
7672 with several other stop positions in between that we
7673 missed. Scan them all now, in buffer's logical
7674 order, until we find and handle the last stop_charpos
7675 that precedes our current position. */
7676 handle_stop_backwards (it, it->stop_charpos);
7677 return GET_NEXT_DISPLAY_ELEMENT (it);
7678 }
7679 else
7680 {
7681 if (it->bidi_p)
7682 {
7683 /* Take note of the stop position we just moved
7684 across, for when we will move back across it. */
7685 it->prev_stop = it->stop_charpos;
7686 /* If we are at base paragraph embedding level, take
7687 note of the last stop position seen at this
7688 level. */
7689 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7690 it->base_level_stop = it->stop_charpos;
7691 }
7692 handle_stop (it);
7693
7694 /* Since a handler may have changed IT->method, we must
7695 recurse here. */
7696 return GET_NEXT_DISPLAY_ELEMENT (it);
7697 }
7698 }
7699 else if (it->bidi_p
7700 /* If we are before prev_stop, we may have overstepped
7701 on our way backwards a stop_pos, and if so, we need
7702 to handle that stop_pos. */
7703 && IT_STRING_CHARPOS (*it) < it->prev_stop
7704 /* We can sometimes back up for reasons that have nothing
7705 to do with bidi reordering. E.g., compositions. The
7706 code below is only needed when we are above the base
7707 embedding level, so test for that explicitly. */
7708 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7709 {
7710 /* If we lost track of base_level_stop, we have no better
7711 place for handle_stop_backwards to start from than string
7712 beginning. This happens, e.g., when we were reseated to
7713 the previous screenful of text by vertical-motion. */
7714 if (it->base_level_stop <= 0
7715 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7716 it->base_level_stop = 0;
7717 handle_stop_backwards (it, it->base_level_stop);
7718 return GET_NEXT_DISPLAY_ELEMENT (it);
7719 }
7720 }
7721
7722 if (it->current.overlay_string_index >= 0)
7723 {
7724 /* Get the next character from an overlay string. In overlay
7725 strings, there is no field width or padding with spaces to
7726 do. */
7727 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7728 {
7729 it->what = IT_EOB;
7730 return 0;
7731 }
7732 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7733 IT_STRING_BYTEPOS (*it),
7734 it->bidi_it.scan_dir < 0
7735 ? -1
7736 : SCHARS (it->string))
7737 && next_element_from_composition (it))
7738 {
7739 return 1;
7740 }
7741 else if (STRING_MULTIBYTE (it->string))
7742 {
7743 const unsigned char *s = (SDATA (it->string)
7744 + IT_STRING_BYTEPOS (*it));
7745 it->c = string_char_and_length (s, &it->len);
7746 }
7747 else
7748 {
7749 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7750 it->len = 1;
7751 }
7752 }
7753 else
7754 {
7755 /* Get the next character from a Lisp string that is not an
7756 overlay string. Such strings come from the mode line, for
7757 example. We may have to pad with spaces, or truncate the
7758 string. See also next_element_from_c_string. */
7759 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7760 {
7761 it->what = IT_EOB;
7762 return 0;
7763 }
7764 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7765 {
7766 /* Pad with spaces. */
7767 it->c = ' ', it->len = 1;
7768 CHARPOS (position) = BYTEPOS (position) = -1;
7769 }
7770 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7771 IT_STRING_BYTEPOS (*it),
7772 it->bidi_it.scan_dir < 0
7773 ? -1
7774 : it->string_nchars)
7775 && next_element_from_composition (it))
7776 {
7777 return 1;
7778 }
7779 else if (STRING_MULTIBYTE (it->string))
7780 {
7781 const unsigned char *s = (SDATA (it->string)
7782 + IT_STRING_BYTEPOS (*it));
7783 it->c = string_char_and_length (s, &it->len);
7784 }
7785 else
7786 {
7787 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7788 it->len = 1;
7789 }
7790 }
7791
7792 /* Record what we have and where it came from. */
7793 it->what = IT_CHARACTER;
7794 it->object = it->string;
7795 it->position = position;
7796 return 1;
7797 }
7798
7799
7800 /* Load IT with next display element from C string IT->s.
7801 IT->string_nchars is the maximum number of characters to return
7802 from the string. IT->end_charpos may be greater than
7803 IT->string_nchars when this function is called, in which case we
7804 may have to return padding spaces. Value is zero if end of string
7805 reached, including padding spaces. */
7806
7807 static int
7808 next_element_from_c_string (struct it *it)
7809 {
7810 int success_p = 1;
7811
7812 eassert (it->s);
7813 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7814 it->what = IT_CHARACTER;
7815 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7816 it->object = Qnil;
7817
7818 /* With bidi reordering, the character to display might not be the
7819 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7820 we were reseated to a new string, whose paragraph direction is
7821 not known. */
7822 if (it->bidi_p && it->bidi_it.first_elt)
7823 get_visually_first_element (it);
7824
7825 /* IT's position can be greater than IT->string_nchars in case a
7826 field width or precision has been specified when the iterator was
7827 initialized. */
7828 if (IT_CHARPOS (*it) >= it->end_charpos)
7829 {
7830 /* End of the game. */
7831 it->what = IT_EOB;
7832 success_p = 0;
7833 }
7834 else if (IT_CHARPOS (*it) >= it->string_nchars)
7835 {
7836 /* Pad with spaces. */
7837 it->c = ' ', it->len = 1;
7838 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7839 }
7840 else if (it->multibyte_p)
7841 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7842 else
7843 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7844
7845 return success_p;
7846 }
7847
7848
7849 /* Set up IT to return characters from an ellipsis, if appropriate.
7850 The definition of the ellipsis glyphs may come from a display table
7851 entry. This function fills IT with the first glyph from the
7852 ellipsis if an ellipsis is to be displayed. */
7853
7854 static int
7855 next_element_from_ellipsis (struct it *it)
7856 {
7857 if (it->selective_display_ellipsis_p)
7858 setup_for_ellipsis (it, it->len);
7859 else
7860 {
7861 /* The face at the current position may be different from the
7862 face we find after the invisible text. Remember what it
7863 was in IT->saved_face_id, and signal that it's there by
7864 setting face_before_selective_p. */
7865 it->saved_face_id = it->face_id;
7866 it->method = GET_FROM_BUFFER;
7867 it->object = it->w->contents;
7868 reseat_at_next_visible_line_start (it, 1);
7869 it->face_before_selective_p = 1;
7870 }
7871
7872 return GET_NEXT_DISPLAY_ELEMENT (it);
7873 }
7874
7875
7876 /* Deliver an image display element. The iterator IT is already
7877 filled with image information (done in handle_display_prop). Value
7878 is always 1. */
7879
7880
7881 static int
7882 next_element_from_image (struct it *it)
7883 {
7884 it->what = IT_IMAGE;
7885 it->ignore_overlay_strings_at_pos_p = 0;
7886 return 1;
7887 }
7888
7889
7890 /* Fill iterator IT with next display element from a stretch glyph
7891 property. IT->object is the value of the text property. Value is
7892 always 1. */
7893
7894 static int
7895 next_element_from_stretch (struct it *it)
7896 {
7897 it->what = IT_STRETCH;
7898 return 1;
7899 }
7900
7901 /* Scan backwards from IT's current position until we find a stop
7902 position, or until BEGV. This is called when we find ourself
7903 before both the last known prev_stop and base_level_stop while
7904 reordering bidirectional text. */
7905
7906 static void
7907 compute_stop_pos_backwards (struct it *it)
7908 {
7909 const int SCAN_BACK_LIMIT = 1000;
7910 struct text_pos pos;
7911 struct display_pos save_current = it->current;
7912 struct text_pos save_position = it->position;
7913 ptrdiff_t charpos = IT_CHARPOS (*it);
7914 ptrdiff_t where_we_are = charpos;
7915 ptrdiff_t save_stop_pos = it->stop_charpos;
7916 ptrdiff_t save_end_pos = it->end_charpos;
7917
7918 eassert (NILP (it->string) && !it->s);
7919 eassert (it->bidi_p);
7920 it->bidi_p = 0;
7921 do
7922 {
7923 it->end_charpos = min (charpos + 1, ZV);
7924 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7925 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7926 reseat_1 (it, pos, 0);
7927 compute_stop_pos (it);
7928 /* We must advance forward, right? */
7929 if (it->stop_charpos <= charpos)
7930 emacs_abort ();
7931 }
7932 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7933
7934 if (it->stop_charpos <= where_we_are)
7935 it->prev_stop = it->stop_charpos;
7936 else
7937 it->prev_stop = BEGV;
7938 it->bidi_p = 1;
7939 it->current = save_current;
7940 it->position = save_position;
7941 it->stop_charpos = save_stop_pos;
7942 it->end_charpos = save_end_pos;
7943 }
7944
7945 /* Scan forward from CHARPOS in the current buffer/string, until we
7946 find a stop position > current IT's position. Then handle the stop
7947 position before that. This is called when we bump into a stop
7948 position while reordering bidirectional text. CHARPOS should be
7949 the last previously processed stop_pos (or BEGV/0, if none were
7950 processed yet) whose position is less that IT's current
7951 position. */
7952
7953 static void
7954 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7955 {
7956 int bufp = !STRINGP (it->string);
7957 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7958 struct display_pos save_current = it->current;
7959 struct text_pos save_position = it->position;
7960 struct text_pos pos1;
7961 ptrdiff_t next_stop;
7962
7963 /* Scan in strict logical order. */
7964 eassert (it->bidi_p);
7965 it->bidi_p = 0;
7966 do
7967 {
7968 it->prev_stop = charpos;
7969 if (bufp)
7970 {
7971 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7972 reseat_1 (it, pos1, 0);
7973 }
7974 else
7975 it->current.string_pos = string_pos (charpos, it->string);
7976 compute_stop_pos (it);
7977 /* We must advance forward, right? */
7978 if (it->stop_charpos <= it->prev_stop)
7979 emacs_abort ();
7980 charpos = it->stop_charpos;
7981 }
7982 while (charpos <= where_we_are);
7983
7984 it->bidi_p = 1;
7985 it->current = save_current;
7986 it->position = save_position;
7987 next_stop = it->stop_charpos;
7988 it->stop_charpos = it->prev_stop;
7989 handle_stop (it);
7990 it->stop_charpos = next_stop;
7991 }
7992
7993 /* Load IT with the next display element from current_buffer. Value
7994 is zero if end of buffer reached. IT->stop_charpos is the next
7995 position at which to stop and check for text properties or buffer
7996 end. */
7997
7998 static int
7999 next_element_from_buffer (struct it *it)
8000 {
8001 int success_p = 1;
8002
8003 eassert (IT_CHARPOS (*it) >= BEGV);
8004 eassert (NILP (it->string) && !it->s);
8005 eassert (!it->bidi_p
8006 || (EQ (it->bidi_it.string.lstring, Qnil)
8007 && it->bidi_it.string.s == NULL));
8008
8009 /* With bidi reordering, the character to display might not be the
8010 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8011 we were reseat()ed to a new buffer position, which is potentially
8012 a different paragraph. */
8013 if (it->bidi_p && it->bidi_it.first_elt)
8014 {
8015 get_visually_first_element (it);
8016 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8017 }
8018
8019 if (IT_CHARPOS (*it) >= it->stop_charpos)
8020 {
8021 if (IT_CHARPOS (*it) >= it->end_charpos)
8022 {
8023 int overlay_strings_follow_p;
8024
8025 /* End of the game, except when overlay strings follow that
8026 haven't been returned yet. */
8027 if (it->overlay_strings_at_end_processed_p)
8028 overlay_strings_follow_p = 0;
8029 else
8030 {
8031 it->overlay_strings_at_end_processed_p = 1;
8032 overlay_strings_follow_p = get_overlay_strings (it, 0);
8033 }
8034
8035 if (overlay_strings_follow_p)
8036 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8037 else
8038 {
8039 it->what = IT_EOB;
8040 it->position = it->current.pos;
8041 success_p = 0;
8042 }
8043 }
8044 else if (!(!it->bidi_p
8045 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8046 || IT_CHARPOS (*it) == it->stop_charpos))
8047 {
8048 /* With bidi non-linear iteration, we could find ourselves
8049 far beyond the last computed stop_charpos, with several
8050 other stop positions in between that we missed. Scan
8051 them all now, in buffer's logical order, until we find
8052 and handle the last stop_charpos that precedes our
8053 current position. */
8054 handle_stop_backwards (it, it->stop_charpos);
8055 return GET_NEXT_DISPLAY_ELEMENT (it);
8056 }
8057 else
8058 {
8059 if (it->bidi_p)
8060 {
8061 /* Take note of the stop position we just moved across,
8062 for when we will move back across it. */
8063 it->prev_stop = it->stop_charpos;
8064 /* If we are at base paragraph embedding level, take
8065 note of the last stop position seen at this
8066 level. */
8067 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8068 it->base_level_stop = it->stop_charpos;
8069 }
8070 handle_stop (it);
8071 return GET_NEXT_DISPLAY_ELEMENT (it);
8072 }
8073 }
8074 else if (it->bidi_p
8075 /* If we are before prev_stop, we may have overstepped on
8076 our way backwards a stop_pos, and if so, we need to
8077 handle that stop_pos. */
8078 && IT_CHARPOS (*it) < it->prev_stop
8079 /* We can sometimes back up for reasons that have nothing
8080 to do with bidi reordering. E.g., compositions. The
8081 code below is only needed when we are above the base
8082 embedding level, so test for that explicitly. */
8083 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8084 {
8085 if (it->base_level_stop <= 0
8086 || IT_CHARPOS (*it) < it->base_level_stop)
8087 {
8088 /* If we lost track of base_level_stop, we need to find
8089 prev_stop by looking backwards. This happens, e.g., when
8090 we were reseated to the previous screenful of text by
8091 vertical-motion. */
8092 it->base_level_stop = BEGV;
8093 compute_stop_pos_backwards (it);
8094 handle_stop_backwards (it, it->prev_stop);
8095 }
8096 else
8097 handle_stop_backwards (it, it->base_level_stop);
8098 return GET_NEXT_DISPLAY_ELEMENT (it);
8099 }
8100 else
8101 {
8102 /* No face changes, overlays etc. in sight, so just return a
8103 character from current_buffer. */
8104 unsigned char *p;
8105 ptrdiff_t stop;
8106
8107 /* Maybe run the redisplay end trigger hook. Performance note:
8108 This doesn't seem to cost measurable time. */
8109 if (it->redisplay_end_trigger_charpos
8110 && it->glyph_row
8111 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8112 run_redisplay_end_trigger_hook (it);
8113
8114 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8115 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8116 stop)
8117 && next_element_from_composition (it))
8118 {
8119 return 1;
8120 }
8121
8122 /* Get the next character, maybe multibyte. */
8123 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8124 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8125 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8126 else
8127 it->c = *p, it->len = 1;
8128
8129 /* Record what we have and where it came from. */
8130 it->what = IT_CHARACTER;
8131 it->object = it->w->contents;
8132 it->position = it->current.pos;
8133
8134 /* Normally we return the character found above, except when we
8135 really want to return an ellipsis for selective display. */
8136 if (it->selective)
8137 {
8138 if (it->c == '\n')
8139 {
8140 /* A value of selective > 0 means hide lines indented more
8141 than that number of columns. */
8142 if (it->selective > 0
8143 && IT_CHARPOS (*it) + 1 < ZV
8144 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8145 IT_BYTEPOS (*it) + 1,
8146 it->selective))
8147 {
8148 success_p = next_element_from_ellipsis (it);
8149 it->dpvec_char_len = -1;
8150 }
8151 }
8152 else if (it->c == '\r' && it->selective == -1)
8153 {
8154 /* A value of selective == -1 means that everything from the
8155 CR to the end of the line is invisible, with maybe an
8156 ellipsis displayed for it. */
8157 success_p = next_element_from_ellipsis (it);
8158 it->dpvec_char_len = -1;
8159 }
8160 }
8161 }
8162
8163 /* Value is zero if end of buffer reached. */
8164 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8165 return success_p;
8166 }
8167
8168
8169 /* Run the redisplay end trigger hook for IT. */
8170
8171 static void
8172 run_redisplay_end_trigger_hook (struct it *it)
8173 {
8174 Lisp_Object args[3];
8175
8176 /* IT->glyph_row should be non-null, i.e. we should be actually
8177 displaying something, or otherwise we should not run the hook. */
8178 eassert (it->glyph_row);
8179
8180 /* Set up hook arguments. */
8181 args[0] = Qredisplay_end_trigger_functions;
8182 args[1] = it->window;
8183 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8184 it->redisplay_end_trigger_charpos = 0;
8185
8186 /* Since we are *trying* to run these functions, don't try to run
8187 them again, even if they get an error. */
8188 wset_redisplay_end_trigger (it->w, Qnil);
8189 Frun_hook_with_args (3, args);
8190
8191 /* Notice if it changed the face of the character we are on. */
8192 handle_face_prop (it);
8193 }
8194
8195
8196 /* Deliver a composition display element. Unlike the other
8197 next_element_from_XXX, this function is not registered in the array
8198 get_next_element[]. It is called from next_element_from_buffer and
8199 next_element_from_string when necessary. */
8200
8201 static int
8202 next_element_from_composition (struct it *it)
8203 {
8204 it->what = IT_COMPOSITION;
8205 it->len = it->cmp_it.nbytes;
8206 if (STRINGP (it->string))
8207 {
8208 if (it->c < 0)
8209 {
8210 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8211 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8212 return 0;
8213 }
8214 it->position = it->current.string_pos;
8215 it->object = it->string;
8216 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8217 IT_STRING_BYTEPOS (*it), it->string);
8218 }
8219 else
8220 {
8221 if (it->c < 0)
8222 {
8223 IT_CHARPOS (*it) += it->cmp_it.nchars;
8224 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8225 if (it->bidi_p)
8226 {
8227 if (it->bidi_it.new_paragraph)
8228 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8229 /* Resync the bidi iterator with IT's new position.
8230 FIXME: this doesn't support bidirectional text. */
8231 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8232 bidi_move_to_visually_next (&it->bidi_it);
8233 }
8234 return 0;
8235 }
8236 it->position = it->current.pos;
8237 it->object = it->w->contents;
8238 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8239 IT_BYTEPOS (*it), Qnil);
8240 }
8241 return 1;
8242 }
8243
8244
8245 \f
8246 /***********************************************************************
8247 Moving an iterator without producing glyphs
8248 ***********************************************************************/
8249
8250 /* Check if iterator is at a position corresponding to a valid buffer
8251 position after some move_it_ call. */
8252
8253 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8254 ((it)->method == GET_FROM_STRING \
8255 ? IT_STRING_CHARPOS (*it) == 0 \
8256 : 1)
8257
8258
8259 /* Move iterator IT to a specified buffer or X position within one
8260 line on the display without producing glyphs.
8261
8262 OP should be a bit mask including some or all of these bits:
8263 MOVE_TO_X: Stop upon reaching x-position TO_X.
8264 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8265 Regardless of OP's value, stop upon reaching the end of the display line.
8266
8267 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8268 This means, in particular, that TO_X includes window's horizontal
8269 scroll amount.
8270
8271 The return value has several possible values that
8272 say what condition caused the scan to stop:
8273
8274 MOVE_POS_MATCH_OR_ZV
8275 - when TO_POS or ZV was reached.
8276
8277 MOVE_X_REACHED
8278 -when TO_X was reached before TO_POS or ZV were reached.
8279
8280 MOVE_LINE_CONTINUED
8281 - when we reached the end of the display area and the line must
8282 be continued.
8283
8284 MOVE_LINE_TRUNCATED
8285 - when we reached the end of the display area and the line is
8286 truncated.
8287
8288 MOVE_NEWLINE_OR_CR
8289 - when we stopped at a line end, i.e. a newline or a CR and selective
8290 display is on. */
8291
8292 static enum move_it_result
8293 move_it_in_display_line_to (struct it *it,
8294 ptrdiff_t to_charpos, int to_x,
8295 enum move_operation_enum op)
8296 {
8297 enum move_it_result result = MOVE_UNDEFINED;
8298 struct glyph_row *saved_glyph_row;
8299 struct it wrap_it, atpos_it, atx_it, ppos_it;
8300 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8301 void *ppos_data = NULL;
8302 int may_wrap = 0;
8303 enum it_method prev_method = it->method;
8304 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8305 int saw_smaller_pos = prev_pos < to_charpos;
8306
8307 /* Don't produce glyphs in produce_glyphs. */
8308 saved_glyph_row = it->glyph_row;
8309 it->glyph_row = NULL;
8310
8311 /* Use wrap_it to save a copy of IT wherever a word wrap could
8312 occur. Use atpos_it to save a copy of IT at the desired buffer
8313 position, if found, so that we can scan ahead and check if the
8314 word later overshoots the window edge. Use atx_it similarly, for
8315 pixel positions. */
8316 wrap_it.sp = -1;
8317 atpos_it.sp = -1;
8318 atx_it.sp = -1;
8319
8320 /* Use ppos_it under bidi reordering to save a copy of IT for the
8321 position > CHARPOS that is the closest to CHARPOS. We restore
8322 that position in IT when we have scanned the entire display line
8323 without finding a match for CHARPOS and all the character
8324 positions are greater than CHARPOS. */
8325 if (it->bidi_p)
8326 {
8327 SAVE_IT (ppos_it, *it, ppos_data);
8328 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8329 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8330 SAVE_IT (ppos_it, *it, ppos_data);
8331 }
8332
8333 #define BUFFER_POS_REACHED_P() \
8334 ((op & MOVE_TO_POS) != 0 \
8335 && BUFFERP (it->object) \
8336 && (IT_CHARPOS (*it) == to_charpos \
8337 || ((!it->bidi_p \
8338 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8339 && IT_CHARPOS (*it) > to_charpos) \
8340 || (it->what == IT_COMPOSITION \
8341 && ((IT_CHARPOS (*it) > to_charpos \
8342 && to_charpos >= it->cmp_it.charpos) \
8343 || (IT_CHARPOS (*it) < to_charpos \
8344 && to_charpos <= it->cmp_it.charpos)))) \
8345 && (it->method == GET_FROM_BUFFER \
8346 || (it->method == GET_FROM_DISPLAY_VECTOR \
8347 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8348
8349 /* If there's a line-/wrap-prefix, handle it. */
8350 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8351 && it->current_y < it->last_visible_y)
8352 handle_line_prefix (it);
8353
8354 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8355 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8356
8357 while (1)
8358 {
8359 int x, i, ascent = 0, descent = 0;
8360
8361 /* Utility macro to reset an iterator with x, ascent, and descent. */
8362 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8363 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8364 (IT)->max_descent = descent)
8365
8366 /* Stop if we move beyond TO_CHARPOS (after an image or a
8367 display string or stretch glyph). */
8368 if ((op & MOVE_TO_POS) != 0
8369 && BUFFERP (it->object)
8370 && it->method == GET_FROM_BUFFER
8371 && (((!it->bidi_p
8372 /* When the iterator is at base embedding level, we
8373 are guaranteed that characters are delivered for
8374 display in strictly increasing order of their
8375 buffer positions. */
8376 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8377 && IT_CHARPOS (*it) > to_charpos)
8378 || (it->bidi_p
8379 && (prev_method == GET_FROM_IMAGE
8380 || prev_method == GET_FROM_STRETCH
8381 || prev_method == GET_FROM_STRING)
8382 /* Passed TO_CHARPOS from left to right. */
8383 && ((prev_pos < to_charpos
8384 && IT_CHARPOS (*it) > to_charpos)
8385 /* Passed TO_CHARPOS from right to left. */
8386 || (prev_pos > to_charpos
8387 && IT_CHARPOS (*it) < to_charpos)))))
8388 {
8389 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8390 {
8391 result = MOVE_POS_MATCH_OR_ZV;
8392 break;
8393 }
8394 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8395 /* If wrap_it is valid, the current position might be in a
8396 word that is wrapped. So, save the iterator in
8397 atpos_it and continue to see if wrapping happens. */
8398 SAVE_IT (atpos_it, *it, atpos_data);
8399 }
8400
8401 /* Stop when ZV reached.
8402 We used to stop here when TO_CHARPOS reached as well, but that is
8403 too soon if this glyph does not fit on this line. So we handle it
8404 explicitly below. */
8405 if (!get_next_display_element (it))
8406 {
8407 result = MOVE_POS_MATCH_OR_ZV;
8408 break;
8409 }
8410
8411 if (it->line_wrap == TRUNCATE)
8412 {
8413 if (BUFFER_POS_REACHED_P ())
8414 {
8415 result = MOVE_POS_MATCH_OR_ZV;
8416 break;
8417 }
8418 }
8419 else
8420 {
8421 if (it->line_wrap == WORD_WRAP)
8422 {
8423 if (IT_DISPLAYING_WHITESPACE (it))
8424 may_wrap = 1;
8425 else if (may_wrap)
8426 {
8427 /* We have reached a glyph that follows one or more
8428 whitespace characters. If the position is
8429 already found, we are done. */
8430 if (atpos_it.sp >= 0)
8431 {
8432 RESTORE_IT (it, &atpos_it, atpos_data);
8433 result = MOVE_POS_MATCH_OR_ZV;
8434 goto done;
8435 }
8436 if (atx_it.sp >= 0)
8437 {
8438 RESTORE_IT (it, &atx_it, atx_data);
8439 result = MOVE_X_REACHED;
8440 goto done;
8441 }
8442 /* Otherwise, we can wrap here. */
8443 SAVE_IT (wrap_it, *it, wrap_data);
8444 may_wrap = 0;
8445 }
8446 }
8447 }
8448
8449 /* Remember the line height for the current line, in case
8450 the next element doesn't fit on the line. */
8451 ascent = it->max_ascent;
8452 descent = it->max_descent;
8453
8454 /* The call to produce_glyphs will get the metrics of the
8455 display element IT is loaded with. Record the x-position
8456 before this display element, in case it doesn't fit on the
8457 line. */
8458 x = it->current_x;
8459
8460 PRODUCE_GLYPHS (it);
8461
8462 if (it->area != TEXT_AREA)
8463 {
8464 prev_method = it->method;
8465 if (it->method == GET_FROM_BUFFER)
8466 prev_pos = IT_CHARPOS (*it);
8467 set_iterator_to_next (it, 1);
8468 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8469 SET_TEXT_POS (this_line_min_pos,
8470 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8471 if (it->bidi_p
8472 && (op & MOVE_TO_POS)
8473 && IT_CHARPOS (*it) > to_charpos
8474 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8475 SAVE_IT (ppos_it, *it, ppos_data);
8476 continue;
8477 }
8478
8479 /* The number of glyphs we get back in IT->nglyphs will normally
8480 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8481 character on a terminal frame, or (iii) a line end. For the
8482 second case, IT->nglyphs - 1 padding glyphs will be present.
8483 (On X frames, there is only one glyph produced for a
8484 composite character.)
8485
8486 The behavior implemented below means, for continuation lines,
8487 that as many spaces of a TAB as fit on the current line are
8488 displayed there. For terminal frames, as many glyphs of a
8489 multi-glyph character are displayed in the current line, too.
8490 This is what the old redisplay code did, and we keep it that
8491 way. Under X, the whole shape of a complex character must
8492 fit on the line or it will be completely displayed in the
8493 next line.
8494
8495 Note that both for tabs and padding glyphs, all glyphs have
8496 the same width. */
8497 if (it->nglyphs)
8498 {
8499 /* More than one glyph or glyph doesn't fit on line. All
8500 glyphs have the same width. */
8501 int single_glyph_width = it->pixel_width / it->nglyphs;
8502 int new_x;
8503 int x_before_this_char = x;
8504 int hpos_before_this_char = it->hpos;
8505
8506 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8507 {
8508 new_x = x + single_glyph_width;
8509
8510 /* We want to leave anything reaching TO_X to the caller. */
8511 if ((op & MOVE_TO_X) && new_x > to_x)
8512 {
8513 if (BUFFER_POS_REACHED_P ())
8514 {
8515 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8516 goto buffer_pos_reached;
8517 if (atpos_it.sp < 0)
8518 {
8519 SAVE_IT (atpos_it, *it, atpos_data);
8520 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8521 }
8522 }
8523 else
8524 {
8525 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8526 {
8527 it->current_x = x;
8528 result = MOVE_X_REACHED;
8529 break;
8530 }
8531 if (atx_it.sp < 0)
8532 {
8533 SAVE_IT (atx_it, *it, atx_data);
8534 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8535 }
8536 }
8537 }
8538
8539 if (/* Lines are continued. */
8540 it->line_wrap != TRUNCATE
8541 && (/* And glyph doesn't fit on the line. */
8542 new_x > it->last_visible_x
8543 /* Or it fits exactly and we're on a window
8544 system frame. */
8545 || (new_x == it->last_visible_x
8546 && FRAME_WINDOW_P (it->f)
8547 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8548 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8549 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8550 {
8551 if (/* IT->hpos == 0 means the very first glyph
8552 doesn't fit on the line, e.g. a wide image. */
8553 it->hpos == 0
8554 || (new_x == it->last_visible_x
8555 && FRAME_WINDOW_P (it->f)))
8556 {
8557 ++it->hpos;
8558 it->current_x = new_x;
8559
8560 /* The character's last glyph just barely fits
8561 in this row. */
8562 if (i == it->nglyphs - 1)
8563 {
8564 /* If this is the destination position,
8565 return a position *before* it in this row,
8566 now that we know it fits in this row. */
8567 if (BUFFER_POS_REACHED_P ())
8568 {
8569 if (it->line_wrap != WORD_WRAP
8570 || wrap_it.sp < 0)
8571 {
8572 it->hpos = hpos_before_this_char;
8573 it->current_x = x_before_this_char;
8574 result = MOVE_POS_MATCH_OR_ZV;
8575 break;
8576 }
8577 if (it->line_wrap == WORD_WRAP
8578 && atpos_it.sp < 0)
8579 {
8580 SAVE_IT (atpos_it, *it, atpos_data);
8581 atpos_it.current_x = x_before_this_char;
8582 atpos_it.hpos = hpos_before_this_char;
8583 }
8584 }
8585
8586 prev_method = it->method;
8587 if (it->method == GET_FROM_BUFFER)
8588 prev_pos = IT_CHARPOS (*it);
8589 set_iterator_to_next (it, 1);
8590 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8591 SET_TEXT_POS (this_line_min_pos,
8592 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8593 /* On graphical terminals, newlines may
8594 "overflow" into the fringe if
8595 overflow-newline-into-fringe is non-nil.
8596 On text terminals, and on graphical
8597 terminals with no right margin, newlines
8598 may overflow into the last glyph on the
8599 display line.*/
8600 if (!FRAME_WINDOW_P (it->f)
8601 || ((it->bidi_p
8602 && it->bidi_it.paragraph_dir == R2L)
8603 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8604 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8605 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8606 {
8607 if (!get_next_display_element (it))
8608 {
8609 result = MOVE_POS_MATCH_OR_ZV;
8610 break;
8611 }
8612 if (BUFFER_POS_REACHED_P ())
8613 {
8614 if (ITERATOR_AT_END_OF_LINE_P (it))
8615 result = MOVE_POS_MATCH_OR_ZV;
8616 else
8617 result = MOVE_LINE_CONTINUED;
8618 break;
8619 }
8620 if (ITERATOR_AT_END_OF_LINE_P (it)
8621 && (it->line_wrap != WORD_WRAP
8622 || wrap_it.sp < 0))
8623 {
8624 result = MOVE_NEWLINE_OR_CR;
8625 break;
8626 }
8627 }
8628 }
8629 }
8630 else
8631 IT_RESET_X_ASCENT_DESCENT (it);
8632
8633 if (wrap_it.sp >= 0)
8634 {
8635 RESTORE_IT (it, &wrap_it, wrap_data);
8636 atpos_it.sp = -1;
8637 atx_it.sp = -1;
8638 }
8639
8640 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8641 IT_CHARPOS (*it)));
8642 result = MOVE_LINE_CONTINUED;
8643 break;
8644 }
8645
8646 if (BUFFER_POS_REACHED_P ())
8647 {
8648 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8649 goto buffer_pos_reached;
8650 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8651 {
8652 SAVE_IT (atpos_it, *it, atpos_data);
8653 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8654 }
8655 }
8656
8657 if (new_x > it->first_visible_x)
8658 {
8659 /* Glyph is visible. Increment number of glyphs that
8660 would be displayed. */
8661 ++it->hpos;
8662 }
8663 }
8664
8665 if (result != MOVE_UNDEFINED)
8666 break;
8667 }
8668 else if (BUFFER_POS_REACHED_P ())
8669 {
8670 buffer_pos_reached:
8671 IT_RESET_X_ASCENT_DESCENT (it);
8672 result = MOVE_POS_MATCH_OR_ZV;
8673 break;
8674 }
8675 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8676 {
8677 /* Stop when TO_X specified and reached. This check is
8678 necessary here because of lines consisting of a line end,
8679 only. The line end will not produce any glyphs and we
8680 would never get MOVE_X_REACHED. */
8681 eassert (it->nglyphs == 0);
8682 result = MOVE_X_REACHED;
8683 break;
8684 }
8685
8686 /* Is this a line end? If yes, we're done. */
8687 if (ITERATOR_AT_END_OF_LINE_P (it))
8688 {
8689 /* If we are past TO_CHARPOS, but never saw any character
8690 positions smaller than TO_CHARPOS, return
8691 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8692 did. */
8693 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8694 {
8695 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8696 {
8697 if (IT_CHARPOS (ppos_it) < ZV)
8698 {
8699 RESTORE_IT (it, &ppos_it, ppos_data);
8700 result = MOVE_POS_MATCH_OR_ZV;
8701 }
8702 else
8703 goto buffer_pos_reached;
8704 }
8705 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8706 && IT_CHARPOS (*it) > to_charpos)
8707 goto buffer_pos_reached;
8708 else
8709 result = MOVE_NEWLINE_OR_CR;
8710 }
8711 else
8712 result = MOVE_NEWLINE_OR_CR;
8713 break;
8714 }
8715
8716 prev_method = it->method;
8717 if (it->method == GET_FROM_BUFFER)
8718 prev_pos = IT_CHARPOS (*it);
8719 /* The current display element has been consumed. Advance
8720 to the next. */
8721 set_iterator_to_next (it, 1);
8722 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8723 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8724 if (IT_CHARPOS (*it) < to_charpos)
8725 saw_smaller_pos = 1;
8726 if (it->bidi_p
8727 && (op & MOVE_TO_POS)
8728 && IT_CHARPOS (*it) >= to_charpos
8729 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8730 SAVE_IT (ppos_it, *it, ppos_data);
8731
8732 /* Stop if lines are truncated and IT's current x-position is
8733 past the right edge of the window now. */
8734 if (it->line_wrap == TRUNCATE
8735 && it->current_x >= it->last_visible_x)
8736 {
8737 if (!FRAME_WINDOW_P (it->f)
8738 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8739 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8740 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8741 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8742 {
8743 int at_eob_p = 0;
8744
8745 if ((at_eob_p = !get_next_display_element (it))
8746 || BUFFER_POS_REACHED_P ()
8747 /* If we are past TO_CHARPOS, but never saw any
8748 character positions smaller than TO_CHARPOS,
8749 return MOVE_POS_MATCH_OR_ZV, like the
8750 unidirectional display did. */
8751 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8752 && !saw_smaller_pos
8753 && IT_CHARPOS (*it) > to_charpos))
8754 {
8755 if (it->bidi_p
8756 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8757 RESTORE_IT (it, &ppos_it, ppos_data);
8758 result = MOVE_POS_MATCH_OR_ZV;
8759 break;
8760 }
8761 if (ITERATOR_AT_END_OF_LINE_P (it))
8762 {
8763 result = MOVE_NEWLINE_OR_CR;
8764 break;
8765 }
8766 }
8767 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8768 && !saw_smaller_pos
8769 && IT_CHARPOS (*it) > to_charpos)
8770 {
8771 if (IT_CHARPOS (ppos_it) < ZV)
8772 RESTORE_IT (it, &ppos_it, ppos_data);
8773 result = MOVE_POS_MATCH_OR_ZV;
8774 break;
8775 }
8776 result = MOVE_LINE_TRUNCATED;
8777 break;
8778 }
8779 #undef IT_RESET_X_ASCENT_DESCENT
8780 }
8781
8782 #undef BUFFER_POS_REACHED_P
8783
8784 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8785 restore the saved iterator. */
8786 if (atpos_it.sp >= 0)
8787 RESTORE_IT (it, &atpos_it, atpos_data);
8788 else if (atx_it.sp >= 0)
8789 RESTORE_IT (it, &atx_it, atx_data);
8790
8791 done:
8792
8793 if (atpos_data)
8794 bidi_unshelve_cache (atpos_data, 1);
8795 if (atx_data)
8796 bidi_unshelve_cache (atx_data, 1);
8797 if (wrap_data)
8798 bidi_unshelve_cache (wrap_data, 1);
8799 if (ppos_data)
8800 bidi_unshelve_cache (ppos_data, 1);
8801
8802 /* Restore the iterator settings altered at the beginning of this
8803 function. */
8804 it->glyph_row = saved_glyph_row;
8805 return result;
8806 }
8807
8808 /* For external use. */
8809 void
8810 move_it_in_display_line (struct it *it,
8811 ptrdiff_t to_charpos, int to_x,
8812 enum move_operation_enum op)
8813 {
8814 if (it->line_wrap == WORD_WRAP
8815 && (op & MOVE_TO_X))
8816 {
8817 struct it save_it;
8818 void *save_data = NULL;
8819 int skip;
8820
8821 SAVE_IT (save_it, *it, save_data);
8822 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8823 /* When word-wrap is on, TO_X may lie past the end
8824 of a wrapped line. Then it->current is the
8825 character on the next line, so backtrack to the
8826 space before the wrap point. */
8827 if (skip == MOVE_LINE_CONTINUED)
8828 {
8829 int prev_x = max (it->current_x - 1, 0);
8830 RESTORE_IT (it, &save_it, save_data);
8831 move_it_in_display_line_to
8832 (it, -1, prev_x, MOVE_TO_X);
8833 }
8834 else
8835 bidi_unshelve_cache (save_data, 1);
8836 }
8837 else
8838 move_it_in_display_line_to (it, to_charpos, to_x, op);
8839 }
8840
8841
8842 /* Move IT forward until it satisfies one or more of the criteria in
8843 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8844
8845 OP is a bit-mask that specifies where to stop, and in particular,
8846 which of those four position arguments makes a difference. See the
8847 description of enum move_operation_enum.
8848
8849 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8850 screen line, this function will set IT to the next position that is
8851 displayed to the right of TO_CHARPOS on the screen. */
8852
8853 void
8854 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8855 {
8856 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8857 int line_height, line_start_x = 0, reached = 0;
8858 void *backup_data = NULL;
8859
8860 for (;;)
8861 {
8862 if (op & MOVE_TO_VPOS)
8863 {
8864 /* If no TO_CHARPOS and no TO_X specified, stop at the
8865 start of the line TO_VPOS. */
8866 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8867 {
8868 if (it->vpos == to_vpos)
8869 {
8870 reached = 1;
8871 break;
8872 }
8873 else
8874 skip = move_it_in_display_line_to (it, -1, -1, 0);
8875 }
8876 else
8877 {
8878 /* TO_VPOS >= 0 means stop at TO_X in the line at
8879 TO_VPOS, or at TO_POS, whichever comes first. */
8880 if (it->vpos == to_vpos)
8881 {
8882 reached = 2;
8883 break;
8884 }
8885
8886 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8887
8888 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8889 {
8890 reached = 3;
8891 break;
8892 }
8893 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8894 {
8895 /* We have reached TO_X but not in the line we want. */
8896 skip = move_it_in_display_line_to (it, to_charpos,
8897 -1, MOVE_TO_POS);
8898 if (skip == MOVE_POS_MATCH_OR_ZV)
8899 {
8900 reached = 4;
8901 break;
8902 }
8903 }
8904 }
8905 }
8906 else if (op & MOVE_TO_Y)
8907 {
8908 struct it it_backup;
8909
8910 if (it->line_wrap == WORD_WRAP)
8911 SAVE_IT (it_backup, *it, backup_data);
8912
8913 /* TO_Y specified means stop at TO_X in the line containing
8914 TO_Y---or at TO_CHARPOS if this is reached first. The
8915 problem is that we can't really tell whether the line
8916 contains TO_Y before we have completely scanned it, and
8917 this may skip past TO_X. What we do is to first scan to
8918 TO_X.
8919
8920 If TO_X is not specified, use a TO_X of zero. The reason
8921 is to make the outcome of this function more predictable.
8922 If we didn't use TO_X == 0, we would stop at the end of
8923 the line which is probably not what a caller would expect
8924 to happen. */
8925 skip = move_it_in_display_line_to
8926 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8927 (MOVE_TO_X | (op & MOVE_TO_POS)));
8928
8929 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8930 if (skip == MOVE_POS_MATCH_OR_ZV)
8931 reached = 5;
8932 else if (skip == MOVE_X_REACHED)
8933 {
8934 /* If TO_X was reached, we want to know whether TO_Y is
8935 in the line. We know this is the case if the already
8936 scanned glyphs make the line tall enough. Otherwise,
8937 we must check by scanning the rest of the line. */
8938 line_height = it->max_ascent + it->max_descent;
8939 if (to_y >= it->current_y
8940 && to_y < it->current_y + line_height)
8941 {
8942 reached = 6;
8943 break;
8944 }
8945 SAVE_IT (it_backup, *it, backup_data);
8946 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8947 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8948 op & MOVE_TO_POS);
8949 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8950 line_height = it->max_ascent + it->max_descent;
8951 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8952
8953 if (to_y >= it->current_y
8954 && to_y < it->current_y + line_height)
8955 {
8956 /* If TO_Y is in this line and TO_X was reached
8957 above, we scanned too far. We have to restore
8958 IT's settings to the ones before skipping. But
8959 keep the more accurate values of max_ascent and
8960 max_descent we've found while skipping the rest
8961 of the line, for the sake of callers, such as
8962 pos_visible_p, that need to know the line
8963 height. */
8964 int max_ascent = it->max_ascent;
8965 int max_descent = it->max_descent;
8966
8967 RESTORE_IT (it, &it_backup, backup_data);
8968 it->max_ascent = max_ascent;
8969 it->max_descent = max_descent;
8970 reached = 6;
8971 }
8972 else
8973 {
8974 skip = skip2;
8975 if (skip == MOVE_POS_MATCH_OR_ZV)
8976 reached = 7;
8977 }
8978 }
8979 else
8980 {
8981 /* Check whether TO_Y is in this line. */
8982 line_height = it->max_ascent + it->max_descent;
8983 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8984
8985 if (to_y >= it->current_y
8986 && to_y < it->current_y + line_height)
8987 {
8988 /* When word-wrap is on, TO_X may lie past the end
8989 of a wrapped line. Then it->current is the
8990 character on the next line, so backtrack to the
8991 space before the wrap point. */
8992 if (skip == MOVE_LINE_CONTINUED
8993 && it->line_wrap == WORD_WRAP)
8994 {
8995 int prev_x = max (it->current_x - 1, 0);
8996 RESTORE_IT (it, &it_backup, backup_data);
8997 skip = move_it_in_display_line_to
8998 (it, -1, prev_x, MOVE_TO_X);
8999 }
9000 reached = 6;
9001 }
9002 }
9003
9004 if (reached)
9005 break;
9006 }
9007 else if (BUFFERP (it->object)
9008 && (it->method == GET_FROM_BUFFER
9009 || it->method == GET_FROM_STRETCH)
9010 && IT_CHARPOS (*it) >= to_charpos
9011 /* Under bidi iteration, a call to set_iterator_to_next
9012 can scan far beyond to_charpos if the initial
9013 portion of the next line needs to be reordered. In
9014 that case, give move_it_in_display_line_to another
9015 chance below. */
9016 && !(it->bidi_p
9017 && it->bidi_it.scan_dir == -1))
9018 skip = MOVE_POS_MATCH_OR_ZV;
9019 else
9020 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9021
9022 switch (skip)
9023 {
9024 case MOVE_POS_MATCH_OR_ZV:
9025 reached = 8;
9026 goto out;
9027
9028 case MOVE_NEWLINE_OR_CR:
9029 set_iterator_to_next (it, 1);
9030 it->continuation_lines_width = 0;
9031 break;
9032
9033 case MOVE_LINE_TRUNCATED:
9034 it->continuation_lines_width = 0;
9035 reseat_at_next_visible_line_start (it, 0);
9036 if ((op & MOVE_TO_POS) != 0
9037 && IT_CHARPOS (*it) > to_charpos)
9038 {
9039 reached = 9;
9040 goto out;
9041 }
9042 break;
9043
9044 case MOVE_LINE_CONTINUED:
9045 /* For continued lines ending in a tab, some of the glyphs
9046 associated with the tab are displayed on the current
9047 line. Since it->current_x does not include these glyphs,
9048 we use it->last_visible_x instead. */
9049 if (it->c == '\t')
9050 {
9051 it->continuation_lines_width += it->last_visible_x;
9052 /* When moving by vpos, ensure that the iterator really
9053 advances to the next line (bug#847, bug#969). Fixme:
9054 do we need to do this in other circumstances? */
9055 if (it->current_x != it->last_visible_x
9056 && (op & MOVE_TO_VPOS)
9057 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9058 {
9059 line_start_x = it->current_x + it->pixel_width
9060 - it->last_visible_x;
9061 set_iterator_to_next (it, 0);
9062 }
9063 }
9064 else
9065 it->continuation_lines_width += it->current_x;
9066 break;
9067
9068 default:
9069 emacs_abort ();
9070 }
9071
9072 /* Reset/increment for the next run. */
9073 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9074 it->current_x = line_start_x;
9075 line_start_x = 0;
9076 it->hpos = 0;
9077 it->current_y += it->max_ascent + it->max_descent;
9078 ++it->vpos;
9079 last_height = it->max_ascent + it->max_descent;
9080 it->max_ascent = it->max_descent = 0;
9081 }
9082
9083 out:
9084
9085 /* On text terminals, we may stop at the end of a line in the middle
9086 of a multi-character glyph. If the glyph itself is continued,
9087 i.e. it is actually displayed on the next line, don't treat this
9088 stopping point as valid; move to the next line instead (unless
9089 that brings us offscreen). */
9090 if (!FRAME_WINDOW_P (it->f)
9091 && op & MOVE_TO_POS
9092 && IT_CHARPOS (*it) == to_charpos
9093 && it->what == IT_CHARACTER
9094 && it->nglyphs > 1
9095 && it->line_wrap == WINDOW_WRAP
9096 && it->current_x == it->last_visible_x - 1
9097 && it->c != '\n'
9098 && it->c != '\t'
9099 && it->vpos < it->w->window_end_vpos)
9100 {
9101 it->continuation_lines_width += it->current_x;
9102 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9103 it->current_y += it->max_ascent + it->max_descent;
9104 ++it->vpos;
9105 last_height = it->max_ascent + it->max_descent;
9106 }
9107
9108 if (backup_data)
9109 bidi_unshelve_cache (backup_data, 1);
9110
9111 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9112 }
9113
9114
9115 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9116
9117 If DY > 0, move IT backward at least that many pixels. DY = 0
9118 means move IT backward to the preceding line start or BEGV. This
9119 function may move over more than DY pixels if IT->current_y - DY
9120 ends up in the middle of a line; in this case IT->current_y will be
9121 set to the top of the line moved to. */
9122
9123 void
9124 move_it_vertically_backward (struct it *it, int dy)
9125 {
9126 int nlines, h;
9127 struct it it2, it3;
9128 void *it2data = NULL, *it3data = NULL;
9129 ptrdiff_t start_pos;
9130 int nchars_per_row
9131 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9132 ptrdiff_t pos_limit;
9133
9134 move_further_back:
9135 eassert (dy >= 0);
9136
9137 start_pos = IT_CHARPOS (*it);
9138
9139 /* Estimate how many newlines we must move back. */
9140 nlines = max (1, dy / default_line_pixel_height (it->w));
9141 if (it->line_wrap == TRUNCATE)
9142 pos_limit = BEGV;
9143 else
9144 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9145
9146 /* Set the iterator's position that many lines back. But don't go
9147 back more than NLINES full screen lines -- this wins a day with
9148 buffers which have very long lines. */
9149 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9150 back_to_previous_visible_line_start (it);
9151
9152 /* Reseat the iterator here. When moving backward, we don't want
9153 reseat to skip forward over invisible text, set up the iterator
9154 to deliver from overlay strings at the new position etc. So,
9155 use reseat_1 here. */
9156 reseat_1 (it, it->current.pos, 1);
9157
9158 /* We are now surely at a line start. */
9159 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9160 reordering is in effect. */
9161 it->continuation_lines_width = 0;
9162
9163 /* Move forward and see what y-distance we moved. First move to the
9164 start of the next line so that we get its height. We need this
9165 height to be able to tell whether we reached the specified
9166 y-distance. */
9167 SAVE_IT (it2, *it, it2data);
9168 it2.max_ascent = it2.max_descent = 0;
9169 do
9170 {
9171 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9172 MOVE_TO_POS | MOVE_TO_VPOS);
9173 }
9174 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9175 /* If we are in a display string which starts at START_POS,
9176 and that display string includes a newline, and we are
9177 right after that newline (i.e. at the beginning of a
9178 display line), exit the loop, because otherwise we will
9179 infloop, since move_it_to will see that it is already at
9180 START_POS and will not move. */
9181 || (it2.method == GET_FROM_STRING
9182 && IT_CHARPOS (it2) == start_pos
9183 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9184 eassert (IT_CHARPOS (*it) >= BEGV);
9185 SAVE_IT (it3, it2, it3data);
9186
9187 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9188 eassert (IT_CHARPOS (*it) >= BEGV);
9189 /* H is the actual vertical distance from the position in *IT
9190 and the starting position. */
9191 h = it2.current_y - it->current_y;
9192 /* NLINES is the distance in number of lines. */
9193 nlines = it2.vpos - it->vpos;
9194
9195 /* Correct IT's y and vpos position
9196 so that they are relative to the starting point. */
9197 it->vpos -= nlines;
9198 it->current_y -= h;
9199
9200 if (dy == 0)
9201 {
9202 /* DY == 0 means move to the start of the screen line. The
9203 value of nlines is > 0 if continuation lines were involved,
9204 or if the original IT position was at start of a line. */
9205 RESTORE_IT (it, it, it2data);
9206 if (nlines > 0)
9207 move_it_by_lines (it, nlines);
9208 /* The above code moves us to some position NLINES down,
9209 usually to its first glyph (leftmost in an L2R line), but
9210 that's not necessarily the start of the line, under bidi
9211 reordering. We want to get to the character position
9212 that is immediately after the newline of the previous
9213 line. */
9214 if (it->bidi_p
9215 && !it->continuation_lines_width
9216 && !STRINGP (it->string)
9217 && IT_CHARPOS (*it) > BEGV
9218 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9219 {
9220 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9221
9222 DEC_BOTH (cp, bp);
9223 cp = find_newline_no_quit (cp, bp, -1, NULL);
9224 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9225 }
9226 bidi_unshelve_cache (it3data, 1);
9227 }
9228 else
9229 {
9230 /* The y-position we try to reach, relative to *IT.
9231 Note that H has been subtracted in front of the if-statement. */
9232 int target_y = it->current_y + h - dy;
9233 int y0 = it3.current_y;
9234 int y1;
9235 int line_height;
9236
9237 RESTORE_IT (&it3, &it3, it3data);
9238 y1 = line_bottom_y (&it3);
9239 line_height = y1 - y0;
9240 RESTORE_IT (it, it, it2data);
9241 /* If we did not reach target_y, try to move further backward if
9242 we can. If we moved too far backward, try to move forward. */
9243 if (target_y < it->current_y
9244 /* This is heuristic. In a window that's 3 lines high, with
9245 a line height of 13 pixels each, recentering with point
9246 on the bottom line will try to move -39/2 = 19 pixels
9247 backward. Try to avoid moving into the first line. */
9248 && (it->current_y - target_y
9249 > min (window_box_height (it->w), line_height * 2 / 3))
9250 && IT_CHARPOS (*it) > BEGV)
9251 {
9252 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9253 target_y - it->current_y));
9254 dy = it->current_y - target_y;
9255 goto move_further_back;
9256 }
9257 else if (target_y >= it->current_y + line_height
9258 && IT_CHARPOS (*it) < ZV)
9259 {
9260 /* Should move forward by at least one line, maybe more.
9261
9262 Note: Calling move_it_by_lines can be expensive on
9263 terminal frames, where compute_motion is used (via
9264 vmotion) to do the job, when there are very long lines
9265 and truncate-lines is nil. That's the reason for
9266 treating terminal frames specially here. */
9267
9268 if (!FRAME_WINDOW_P (it->f))
9269 move_it_vertically (it, target_y - (it->current_y + line_height));
9270 else
9271 {
9272 do
9273 {
9274 move_it_by_lines (it, 1);
9275 }
9276 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9277 }
9278 }
9279 }
9280 }
9281
9282
9283 /* Move IT by a specified amount of pixel lines DY. DY negative means
9284 move backwards. DY = 0 means move to start of screen line. At the
9285 end, IT will be on the start of a screen line. */
9286
9287 void
9288 move_it_vertically (struct it *it, int dy)
9289 {
9290 if (dy <= 0)
9291 move_it_vertically_backward (it, -dy);
9292 else
9293 {
9294 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9295 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9296 MOVE_TO_POS | MOVE_TO_Y);
9297 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9298
9299 /* If buffer ends in ZV without a newline, move to the start of
9300 the line to satisfy the post-condition. */
9301 if (IT_CHARPOS (*it) == ZV
9302 && ZV > BEGV
9303 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9304 move_it_by_lines (it, 0);
9305 }
9306 }
9307
9308
9309 /* Move iterator IT past the end of the text line it is in. */
9310
9311 void
9312 move_it_past_eol (struct it *it)
9313 {
9314 enum move_it_result rc;
9315
9316 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9317 if (rc == MOVE_NEWLINE_OR_CR)
9318 set_iterator_to_next (it, 0);
9319 }
9320
9321
9322 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9323 negative means move up. DVPOS == 0 means move to the start of the
9324 screen line.
9325
9326 Optimization idea: If we would know that IT->f doesn't use
9327 a face with proportional font, we could be faster for
9328 truncate-lines nil. */
9329
9330 void
9331 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9332 {
9333
9334 /* The commented-out optimization uses vmotion on terminals. This
9335 gives bad results, because elements like it->what, on which
9336 callers such as pos_visible_p rely, aren't updated. */
9337 /* struct position pos;
9338 if (!FRAME_WINDOW_P (it->f))
9339 {
9340 struct text_pos textpos;
9341
9342 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9343 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9344 reseat (it, textpos, 1);
9345 it->vpos += pos.vpos;
9346 it->current_y += pos.vpos;
9347 }
9348 else */
9349
9350 if (dvpos == 0)
9351 {
9352 /* DVPOS == 0 means move to the start of the screen line. */
9353 move_it_vertically_backward (it, 0);
9354 /* Let next call to line_bottom_y calculate real line height */
9355 last_height = 0;
9356 }
9357 else if (dvpos > 0)
9358 {
9359 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9360 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9361 {
9362 /* Only move to the next buffer position if we ended up in a
9363 string from display property, not in an overlay string
9364 (before-string or after-string). That is because the
9365 latter don't conceal the underlying buffer position, so
9366 we can ask to move the iterator to the exact position we
9367 are interested in. Note that, even if we are already at
9368 IT_CHARPOS (*it), the call below is not a no-op, as it
9369 will detect that we are at the end of the string, pop the
9370 iterator, and compute it->current_x and it->hpos
9371 correctly. */
9372 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9373 -1, -1, -1, MOVE_TO_POS);
9374 }
9375 }
9376 else
9377 {
9378 struct it it2;
9379 void *it2data = NULL;
9380 ptrdiff_t start_charpos, i;
9381 int nchars_per_row
9382 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9383 ptrdiff_t pos_limit;
9384
9385 /* Start at the beginning of the screen line containing IT's
9386 position. This may actually move vertically backwards,
9387 in case of overlays, so adjust dvpos accordingly. */
9388 dvpos += it->vpos;
9389 move_it_vertically_backward (it, 0);
9390 dvpos -= it->vpos;
9391
9392 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9393 screen lines, and reseat the iterator there. */
9394 start_charpos = IT_CHARPOS (*it);
9395 if (it->line_wrap == TRUNCATE)
9396 pos_limit = BEGV;
9397 else
9398 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9399 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9400 back_to_previous_visible_line_start (it);
9401 reseat (it, it->current.pos, 1);
9402
9403 /* Move further back if we end up in a string or an image. */
9404 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9405 {
9406 /* First try to move to start of display line. */
9407 dvpos += it->vpos;
9408 move_it_vertically_backward (it, 0);
9409 dvpos -= it->vpos;
9410 if (IT_POS_VALID_AFTER_MOVE_P (it))
9411 break;
9412 /* If start of line is still in string or image,
9413 move further back. */
9414 back_to_previous_visible_line_start (it);
9415 reseat (it, it->current.pos, 1);
9416 dvpos--;
9417 }
9418
9419 it->current_x = it->hpos = 0;
9420
9421 /* Above call may have moved too far if continuation lines
9422 are involved. Scan forward and see if it did. */
9423 SAVE_IT (it2, *it, it2data);
9424 it2.vpos = it2.current_y = 0;
9425 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9426 it->vpos -= it2.vpos;
9427 it->current_y -= it2.current_y;
9428 it->current_x = it->hpos = 0;
9429
9430 /* If we moved too far back, move IT some lines forward. */
9431 if (it2.vpos > -dvpos)
9432 {
9433 int delta = it2.vpos + dvpos;
9434
9435 RESTORE_IT (&it2, &it2, it2data);
9436 SAVE_IT (it2, *it, it2data);
9437 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9438 /* Move back again if we got too far ahead. */
9439 if (IT_CHARPOS (*it) >= start_charpos)
9440 RESTORE_IT (it, &it2, it2data);
9441 else
9442 bidi_unshelve_cache (it2data, 1);
9443 }
9444 else
9445 RESTORE_IT (it, it, it2data);
9446 }
9447 }
9448
9449 /* Return 1 if IT points into the middle of a display vector. */
9450
9451 int
9452 in_display_vector_p (struct it *it)
9453 {
9454 return (it->method == GET_FROM_DISPLAY_VECTOR
9455 && it->current.dpvec_index > 0
9456 && it->dpvec + it->current.dpvec_index != it->dpend);
9457 }
9458
9459 \f
9460 /***********************************************************************
9461 Messages
9462 ***********************************************************************/
9463
9464
9465 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9466 to *Messages*. */
9467
9468 void
9469 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9470 {
9471 Lisp_Object args[3];
9472 Lisp_Object msg, fmt;
9473 char *buffer;
9474 ptrdiff_t len;
9475 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9476 USE_SAFE_ALLOCA;
9477
9478 fmt = msg = Qnil;
9479 GCPRO4 (fmt, msg, arg1, arg2);
9480
9481 args[0] = fmt = build_string (format);
9482 args[1] = arg1;
9483 args[2] = arg2;
9484 msg = Fformat (3, args);
9485
9486 len = SBYTES (msg) + 1;
9487 buffer = SAFE_ALLOCA (len);
9488 memcpy (buffer, SDATA (msg), len);
9489
9490 message_dolog (buffer, len - 1, 1, 0);
9491 SAFE_FREE ();
9492
9493 UNGCPRO;
9494 }
9495
9496
9497 /* Output a newline in the *Messages* buffer if "needs" one. */
9498
9499 void
9500 message_log_maybe_newline (void)
9501 {
9502 if (message_log_need_newline)
9503 message_dolog ("", 0, 1, 0);
9504 }
9505
9506
9507 /* Add a string M of length NBYTES to the message log, optionally
9508 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9509 true, means interpret the contents of M as multibyte. This
9510 function calls low-level routines in order to bypass text property
9511 hooks, etc. which might not be safe to run.
9512
9513 This may GC (insert may run before/after change hooks),
9514 so the buffer M must NOT point to a Lisp string. */
9515
9516 void
9517 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9518 {
9519 const unsigned char *msg = (const unsigned char *) m;
9520
9521 if (!NILP (Vmemory_full))
9522 return;
9523
9524 if (!NILP (Vmessage_log_max))
9525 {
9526 struct buffer *oldbuf;
9527 Lisp_Object oldpoint, oldbegv, oldzv;
9528 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9529 ptrdiff_t point_at_end = 0;
9530 ptrdiff_t zv_at_end = 0;
9531 Lisp_Object old_deactivate_mark;
9532 bool shown;
9533 struct gcpro gcpro1;
9534
9535 old_deactivate_mark = Vdeactivate_mark;
9536 oldbuf = current_buffer;
9537 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9538 bset_undo_list (current_buffer, Qt);
9539
9540 oldpoint = message_dolog_marker1;
9541 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9542 oldbegv = message_dolog_marker2;
9543 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9544 oldzv = message_dolog_marker3;
9545 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9546 GCPRO1 (old_deactivate_mark);
9547
9548 if (PT == Z)
9549 point_at_end = 1;
9550 if (ZV == Z)
9551 zv_at_end = 1;
9552
9553 BEGV = BEG;
9554 BEGV_BYTE = BEG_BYTE;
9555 ZV = Z;
9556 ZV_BYTE = Z_BYTE;
9557 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9558
9559 /* Insert the string--maybe converting multibyte to single byte
9560 or vice versa, so that all the text fits the buffer. */
9561 if (multibyte
9562 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9563 {
9564 ptrdiff_t i;
9565 int c, char_bytes;
9566 char work[1];
9567
9568 /* Convert a multibyte string to single-byte
9569 for the *Message* buffer. */
9570 for (i = 0; i < nbytes; i += char_bytes)
9571 {
9572 c = string_char_and_length (msg + i, &char_bytes);
9573 work[0] = (ASCII_CHAR_P (c)
9574 ? c
9575 : multibyte_char_to_unibyte (c));
9576 insert_1_both (work, 1, 1, 1, 0, 0);
9577 }
9578 }
9579 else if (! multibyte
9580 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9581 {
9582 ptrdiff_t i;
9583 int c, char_bytes;
9584 unsigned char str[MAX_MULTIBYTE_LENGTH];
9585 /* Convert a single-byte string to multibyte
9586 for the *Message* buffer. */
9587 for (i = 0; i < nbytes; i++)
9588 {
9589 c = msg[i];
9590 MAKE_CHAR_MULTIBYTE (c);
9591 char_bytes = CHAR_STRING (c, str);
9592 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9593 }
9594 }
9595 else if (nbytes)
9596 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9597
9598 if (nlflag)
9599 {
9600 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9601 printmax_t dups;
9602
9603 insert_1_both ("\n", 1, 1, 1, 0, 0);
9604
9605 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9606 this_bol = PT;
9607 this_bol_byte = PT_BYTE;
9608
9609 /* See if this line duplicates the previous one.
9610 If so, combine duplicates. */
9611 if (this_bol > BEG)
9612 {
9613 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9614 prev_bol = PT;
9615 prev_bol_byte = PT_BYTE;
9616
9617 dups = message_log_check_duplicate (prev_bol_byte,
9618 this_bol_byte);
9619 if (dups)
9620 {
9621 del_range_both (prev_bol, prev_bol_byte,
9622 this_bol, this_bol_byte, 0);
9623 if (dups > 1)
9624 {
9625 char dupstr[sizeof " [ times]"
9626 + INT_STRLEN_BOUND (printmax_t)];
9627
9628 /* If you change this format, don't forget to also
9629 change message_log_check_duplicate. */
9630 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9631 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9632 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9633 }
9634 }
9635 }
9636
9637 /* If we have more than the desired maximum number of lines
9638 in the *Messages* buffer now, delete the oldest ones.
9639 This is safe because we don't have undo in this buffer. */
9640
9641 if (NATNUMP (Vmessage_log_max))
9642 {
9643 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9644 -XFASTINT (Vmessage_log_max) - 1, 0);
9645 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9646 }
9647 }
9648 BEGV = marker_position (oldbegv);
9649 BEGV_BYTE = marker_byte_position (oldbegv);
9650
9651 if (zv_at_end)
9652 {
9653 ZV = Z;
9654 ZV_BYTE = Z_BYTE;
9655 }
9656 else
9657 {
9658 ZV = marker_position (oldzv);
9659 ZV_BYTE = marker_byte_position (oldzv);
9660 }
9661
9662 if (point_at_end)
9663 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9664 else
9665 /* We can't do Fgoto_char (oldpoint) because it will run some
9666 Lisp code. */
9667 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9668 marker_byte_position (oldpoint));
9669
9670 UNGCPRO;
9671 unchain_marker (XMARKER (oldpoint));
9672 unchain_marker (XMARKER (oldbegv));
9673 unchain_marker (XMARKER (oldzv));
9674
9675 shown = buffer_window_count (current_buffer) > 0;
9676 set_buffer_internal (oldbuf);
9677 /* We called insert_1_both above with its 5th argument (PREPARE)
9678 zero, which prevents insert_1_both from calling
9679 prepare_to_modify_buffer, which in turns prevents us from
9680 incrementing windows_or_buffers_changed even if *Messages* is
9681 shown in some window. So we must manually incrementing
9682 windows_or_buffers_changed here to make up for that. */
9683 if (shown)
9684 windows_or_buffers_changed++;
9685 else
9686 windows_or_buffers_changed = old_windows_or_buffers_changed;
9687 message_log_need_newline = !nlflag;
9688 Vdeactivate_mark = old_deactivate_mark;
9689 }
9690 }
9691
9692
9693 /* We are at the end of the buffer after just having inserted a newline.
9694 (Note: We depend on the fact we won't be crossing the gap.)
9695 Check to see if the most recent message looks a lot like the previous one.
9696 Return 0 if different, 1 if the new one should just replace it, or a
9697 value N > 1 if we should also append " [N times]". */
9698
9699 static intmax_t
9700 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9701 {
9702 ptrdiff_t i;
9703 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9704 int seen_dots = 0;
9705 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9706 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9707
9708 for (i = 0; i < len; i++)
9709 {
9710 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9711 seen_dots = 1;
9712 if (p1[i] != p2[i])
9713 return seen_dots;
9714 }
9715 p1 += len;
9716 if (*p1 == '\n')
9717 return 2;
9718 if (*p1++ == ' ' && *p1++ == '[')
9719 {
9720 char *pend;
9721 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9722 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9723 return n + 1;
9724 }
9725 return 0;
9726 }
9727 \f
9728
9729 /* Display an echo area message M with a specified length of NBYTES
9730 bytes. The string may include null characters. If M is not a
9731 string, clear out any existing message, and let the mini-buffer
9732 text show through.
9733
9734 This function cancels echoing. */
9735
9736 void
9737 message3 (Lisp_Object m)
9738 {
9739 struct gcpro gcpro1;
9740
9741 GCPRO1 (m);
9742 clear_message (1,1);
9743 cancel_echoing ();
9744
9745 /* First flush out any partial line written with print. */
9746 message_log_maybe_newline ();
9747 if (STRINGP (m))
9748 {
9749 ptrdiff_t nbytes = SBYTES (m);
9750 bool multibyte = STRING_MULTIBYTE (m);
9751 USE_SAFE_ALLOCA;
9752 char *buffer = SAFE_ALLOCA (nbytes);
9753 memcpy (buffer, SDATA (m), nbytes);
9754 message_dolog (buffer, nbytes, 1, multibyte);
9755 SAFE_FREE ();
9756 }
9757 message3_nolog (m);
9758
9759 UNGCPRO;
9760 }
9761
9762
9763 /* The non-logging version of message3.
9764 This does not cancel echoing, because it is used for echoing.
9765 Perhaps we need to make a separate function for echoing
9766 and make this cancel echoing. */
9767
9768 void
9769 message3_nolog (Lisp_Object m)
9770 {
9771 struct frame *sf = SELECTED_FRAME ();
9772
9773 if (FRAME_INITIAL_P (sf))
9774 {
9775 if (noninteractive_need_newline)
9776 putc ('\n', stderr);
9777 noninteractive_need_newline = 0;
9778 if (STRINGP (m))
9779 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9780 if (cursor_in_echo_area == 0)
9781 fprintf (stderr, "\n");
9782 fflush (stderr);
9783 }
9784 /* Error messages get reported properly by cmd_error, so this must be just an
9785 informative message; if the frame hasn't really been initialized yet, just
9786 toss it. */
9787 else if (INTERACTIVE && sf->glyphs_initialized_p)
9788 {
9789 /* Get the frame containing the mini-buffer
9790 that the selected frame is using. */
9791 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9792 Lisp_Object frame = XWINDOW (mini_window)->frame;
9793 struct frame *f = XFRAME (frame);
9794
9795 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9796 Fmake_frame_visible (frame);
9797
9798 if (STRINGP (m) && SCHARS (m) > 0)
9799 {
9800 set_message (m);
9801 if (minibuffer_auto_raise)
9802 Fraise_frame (frame);
9803 /* Assume we are not echoing.
9804 (If we are, echo_now will override this.) */
9805 echo_message_buffer = Qnil;
9806 }
9807 else
9808 clear_message (1, 1);
9809
9810 do_pending_window_change (0);
9811 echo_area_display (1);
9812 do_pending_window_change (0);
9813 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9814 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9815 }
9816 }
9817
9818
9819 /* Display a null-terminated echo area message M. If M is 0, clear
9820 out any existing message, and let the mini-buffer text show through.
9821
9822 The buffer M must continue to exist until after the echo area gets
9823 cleared or some other message gets displayed there. Do not pass
9824 text that is stored in a Lisp string. Do not pass text in a buffer
9825 that was alloca'd. */
9826
9827 void
9828 message1 (const char *m)
9829 {
9830 message3 (m ? build_unibyte_string (m) : Qnil);
9831 }
9832
9833
9834 /* The non-logging counterpart of message1. */
9835
9836 void
9837 message1_nolog (const char *m)
9838 {
9839 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9840 }
9841
9842 /* Display a message M which contains a single %s
9843 which gets replaced with STRING. */
9844
9845 void
9846 message_with_string (const char *m, Lisp_Object string, int log)
9847 {
9848 CHECK_STRING (string);
9849
9850 if (noninteractive)
9851 {
9852 if (m)
9853 {
9854 if (noninteractive_need_newline)
9855 putc ('\n', stderr);
9856 noninteractive_need_newline = 0;
9857 fprintf (stderr, m, SDATA (string));
9858 if (!cursor_in_echo_area)
9859 fprintf (stderr, "\n");
9860 fflush (stderr);
9861 }
9862 }
9863 else if (INTERACTIVE)
9864 {
9865 /* The frame whose minibuffer we're going to display the message on.
9866 It may be larger than the selected frame, so we need
9867 to use its buffer, not the selected frame's buffer. */
9868 Lisp_Object mini_window;
9869 struct frame *f, *sf = SELECTED_FRAME ();
9870
9871 /* Get the frame containing the minibuffer
9872 that the selected frame is using. */
9873 mini_window = FRAME_MINIBUF_WINDOW (sf);
9874 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9875
9876 /* Error messages get reported properly by cmd_error, so this must be
9877 just an informative message; if the frame hasn't really been
9878 initialized yet, just toss it. */
9879 if (f->glyphs_initialized_p)
9880 {
9881 Lisp_Object args[2], msg;
9882 struct gcpro gcpro1, gcpro2;
9883
9884 args[0] = build_string (m);
9885 args[1] = msg = string;
9886 GCPRO2 (args[0], msg);
9887 gcpro1.nvars = 2;
9888
9889 msg = Fformat (2, args);
9890
9891 if (log)
9892 message3 (msg);
9893 else
9894 message3_nolog (msg);
9895
9896 UNGCPRO;
9897
9898 /* Print should start at the beginning of the message
9899 buffer next time. */
9900 message_buf_print = 0;
9901 }
9902 }
9903 }
9904
9905
9906 /* Dump an informative message to the minibuf. If M is 0, clear out
9907 any existing message, and let the mini-buffer text show through. */
9908
9909 static void
9910 vmessage (const char *m, va_list ap)
9911 {
9912 if (noninteractive)
9913 {
9914 if (m)
9915 {
9916 if (noninteractive_need_newline)
9917 putc ('\n', stderr);
9918 noninteractive_need_newline = 0;
9919 vfprintf (stderr, m, ap);
9920 if (cursor_in_echo_area == 0)
9921 fprintf (stderr, "\n");
9922 fflush (stderr);
9923 }
9924 }
9925 else if (INTERACTIVE)
9926 {
9927 /* The frame whose mini-buffer we're going to display the message
9928 on. It may be larger than the selected frame, so we need to
9929 use its buffer, not the selected frame's buffer. */
9930 Lisp_Object mini_window;
9931 struct frame *f, *sf = SELECTED_FRAME ();
9932
9933 /* Get the frame containing the mini-buffer
9934 that the selected frame is using. */
9935 mini_window = FRAME_MINIBUF_WINDOW (sf);
9936 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9937
9938 /* Error messages get reported properly by cmd_error, so this must be
9939 just an informative message; if the frame hasn't really been
9940 initialized yet, just toss it. */
9941 if (f->glyphs_initialized_p)
9942 {
9943 if (m)
9944 {
9945 ptrdiff_t len;
9946 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9947 char *message_buf = alloca (maxsize + 1);
9948
9949 len = doprnt (message_buf, maxsize, m, 0, ap);
9950
9951 message3 (make_string (message_buf, len));
9952 }
9953 else
9954 message1 (0);
9955
9956 /* Print should start at the beginning of the message
9957 buffer next time. */
9958 message_buf_print = 0;
9959 }
9960 }
9961 }
9962
9963 void
9964 message (const char *m, ...)
9965 {
9966 va_list ap;
9967 va_start (ap, m);
9968 vmessage (m, ap);
9969 va_end (ap);
9970 }
9971
9972
9973 #if 0
9974 /* The non-logging version of message. */
9975
9976 void
9977 message_nolog (const char *m, ...)
9978 {
9979 Lisp_Object old_log_max;
9980 va_list ap;
9981 va_start (ap, m);
9982 old_log_max = Vmessage_log_max;
9983 Vmessage_log_max = Qnil;
9984 vmessage (m, ap);
9985 Vmessage_log_max = old_log_max;
9986 va_end (ap);
9987 }
9988 #endif
9989
9990
9991 /* Display the current message in the current mini-buffer. This is
9992 only called from error handlers in process.c, and is not time
9993 critical. */
9994
9995 void
9996 update_echo_area (void)
9997 {
9998 if (!NILP (echo_area_buffer[0]))
9999 {
10000 Lisp_Object string;
10001 string = Fcurrent_message ();
10002 message3 (string);
10003 }
10004 }
10005
10006
10007 /* Make sure echo area buffers in `echo_buffers' are live.
10008 If they aren't, make new ones. */
10009
10010 static void
10011 ensure_echo_area_buffers (void)
10012 {
10013 int i;
10014
10015 for (i = 0; i < 2; ++i)
10016 if (!BUFFERP (echo_buffer[i])
10017 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10018 {
10019 char name[30];
10020 Lisp_Object old_buffer;
10021 int j;
10022
10023 old_buffer = echo_buffer[i];
10024 echo_buffer[i] = Fget_buffer_create
10025 (make_formatted_string (name, " *Echo Area %d*", i));
10026 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10027 /* to force word wrap in echo area -
10028 it was decided to postpone this*/
10029 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10030
10031 for (j = 0; j < 2; ++j)
10032 if (EQ (old_buffer, echo_area_buffer[j]))
10033 echo_area_buffer[j] = echo_buffer[i];
10034 }
10035 }
10036
10037
10038 /* Call FN with args A1..A2 with either the current or last displayed
10039 echo_area_buffer as current buffer.
10040
10041 WHICH zero means use the current message buffer
10042 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10043 from echo_buffer[] and clear it.
10044
10045 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10046 suitable buffer from echo_buffer[] and clear it.
10047
10048 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10049 that the current message becomes the last displayed one, make
10050 choose a suitable buffer for echo_area_buffer[0], and clear it.
10051
10052 Value is what FN returns. */
10053
10054 static int
10055 with_echo_area_buffer (struct window *w, int which,
10056 int (*fn) (ptrdiff_t, Lisp_Object),
10057 ptrdiff_t a1, Lisp_Object a2)
10058 {
10059 Lisp_Object buffer;
10060 int this_one, the_other, clear_buffer_p, rc;
10061 ptrdiff_t count = SPECPDL_INDEX ();
10062
10063 /* If buffers aren't live, make new ones. */
10064 ensure_echo_area_buffers ();
10065
10066 clear_buffer_p = 0;
10067
10068 if (which == 0)
10069 this_one = 0, the_other = 1;
10070 else if (which > 0)
10071 this_one = 1, the_other = 0;
10072 else
10073 {
10074 this_one = 0, the_other = 1;
10075 clear_buffer_p = 1;
10076
10077 /* We need a fresh one in case the current echo buffer equals
10078 the one containing the last displayed echo area message. */
10079 if (!NILP (echo_area_buffer[this_one])
10080 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10081 echo_area_buffer[this_one] = Qnil;
10082 }
10083
10084 /* Choose a suitable buffer from echo_buffer[] is we don't
10085 have one. */
10086 if (NILP (echo_area_buffer[this_one]))
10087 {
10088 echo_area_buffer[this_one]
10089 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10090 ? echo_buffer[the_other]
10091 : echo_buffer[this_one]);
10092 clear_buffer_p = 1;
10093 }
10094
10095 buffer = echo_area_buffer[this_one];
10096
10097 /* Don't get confused by reusing the buffer used for echoing
10098 for a different purpose. */
10099 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10100 cancel_echoing ();
10101
10102 record_unwind_protect (unwind_with_echo_area_buffer,
10103 with_echo_area_buffer_unwind_data (w));
10104
10105 /* Make the echo area buffer current. Note that for display
10106 purposes, it is not necessary that the displayed window's buffer
10107 == current_buffer, except for text property lookup. So, let's
10108 only set that buffer temporarily here without doing a full
10109 Fset_window_buffer. We must also change w->pointm, though,
10110 because otherwise an assertions in unshow_buffer fails, and Emacs
10111 aborts. */
10112 set_buffer_internal_1 (XBUFFER (buffer));
10113 if (w)
10114 {
10115 wset_buffer (w, buffer);
10116 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10117 }
10118
10119 bset_undo_list (current_buffer, Qt);
10120 bset_read_only (current_buffer, Qnil);
10121 specbind (Qinhibit_read_only, Qt);
10122 specbind (Qinhibit_modification_hooks, Qt);
10123
10124 if (clear_buffer_p && Z > BEG)
10125 del_range (BEG, Z);
10126
10127 eassert (BEGV >= BEG);
10128 eassert (ZV <= Z && ZV >= BEGV);
10129
10130 rc = fn (a1, a2);
10131
10132 eassert (BEGV >= BEG);
10133 eassert (ZV <= Z && ZV >= BEGV);
10134
10135 unbind_to (count, Qnil);
10136 return rc;
10137 }
10138
10139
10140 /* Save state that should be preserved around the call to the function
10141 FN called in with_echo_area_buffer. */
10142
10143 static Lisp_Object
10144 with_echo_area_buffer_unwind_data (struct window *w)
10145 {
10146 int i = 0;
10147 Lisp_Object vector, tmp;
10148
10149 /* Reduce consing by keeping one vector in
10150 Vwith_echo_area_save_vector. */
10151 vector = Vwith_echo_area_save_vector;
10152 Vwith_echo_area_save_vector = Qnil;
10153
10154 if (NILP (vector))
10155 vector = Fmake_vector (make_number (9), Qnil);
10156
10157 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10158 ASET (vector, i, Vdeactivate_mark); ++i;
10159 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10160
10161 if (w)
10162 {
10163 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10164 ASET (vector, i, w->contents); ++i;
10165 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10166 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10167 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10168 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10169 }
10170 else
10171 {
10172 int end = i + 6;
10173 for (; i < end; ++i)
10174 ASET (vector, i, Qnil);
10175 }
10176
10177 eassert (i == ASIZE (vector));
10178 return vector;
10179 }
10180
10181
10182 /* Restore global state from VECTOR which was created by
10183 with_echo_area_buffer_unwind_data. */
10184
10185 static void
10186 unwind_with_echo_area_buffer (Lisp_Object vector)
10187 {
10188 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10189 Vdeactivate_mark = AREF (vector, 1);
10190 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10191
10192 if (WINDOWP (AREF (vector, 3)))
10193 {
10194 struct window *w;
10195 Lisp_Object buffer;
10196
10197 w = XWINDOW (AREF (vector, 3));
10198 buffer = AREF (vector, 4);
10199
10200 wset_buffer (w, buffer);
10201 set_marker_both (w->pointm, buffer,
10202 XFASTINT (AREF (vector, 5)),
10203 XFASTINT (AREF (vector, 6)));
10204 set_marker_both (w->start, buffer,
10205 XFASTINT (AREF (vector, 7)),
10206 XFASTINT (AREF (vector, 8)));
10207 }
10208
10209 Vwith_echo_area_save_vector = vector;
10210 }
10211
10212
10213 /* Set up the echo area for use by print functions. MULTIBYTE_P
10214 non-zero means we will print multibyte. */
10215
10216 void
10217 setup_echo_area_for_printing (int multibyte_p)
10218 {
10219 /* If we can't find an echo area any more, exit. */
10220 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10221 Fkill_emacs (Qnil);
10222
10223 ensure_echo_area_buffers ();
10224
10225 if (!message_buf_print)
10226 {
10227 /* A message has been output since the last time we printed.
10228 Choose a fresh echo area buffer. */
10229 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10230 echo_area_buffer[0] = echo_buffer[1];
10231 else
10232 echo_area_buffer[0] = echo_buffer[0];
10233
10234 /* Switch to that buffer and clear it. */
10235 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10236 bset_truncate_lines (current_buffer, Qnil);
10237
10238 if (Z > BEG)
10239 {
10240 ptrdiff_t count = SPECPDL_INDEX ();
10241 specbind (Qinhibit_read_only, Qt);
10242 /* Note that undo recording is always disabled. */
10243 del_range (BEG, Z);
10244 unbind_to (count, Qnil);
10245 }
10246 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10247
10248 /* Set up the buffer for the multibyteness we need. */
10249 if (multibyte_p
10250 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10251 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10252
10253 /* Raise the frame containing the echo area. */
10254 if (minibuffer_auto_raise)
10255 {
10256 struct frame *sf = SELECTED_FRAME ();
10257 Lisp_Object mini_window;
10258 mini_window = FRAME_MINIBUF_WINDOW (sf);
10259 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10260 }
10261
10262 message_log_maybe_newline ();
10263 message_buf_print = 1;
10264 }
10265 else
10266 {
10267 if (NILP (echo_area_buffer[0]))
10268 {
10269 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10270 echo_area_buffer[0] = echo_buffer[1];
10271 else
10272 echo_area_buffer[0] = echo_buffer[0];
10273 }
10274
10275 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10276 {
10277 /* Someone switched buffers between print requests. */
10278 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10279 bset_truncate_lines (current_buffer, Qnil);
10280 }
10281 }
10282 }
10283
10284
10285 /* Display an echo area message in window W. Value is non-zero if W's
10286 height is changed. If display_last_displayed_message_p is
10287 non-zero, display the message that was last displayed, otherwise
10288 display the current message. */
10289
10290 static int
10291 display_echo_area (struct window *w)
10292 {
10293 int i, no_message_p, window_height_changed_p;
10294
10295 /* Temporarily disable garbage collections while displaying the echo
10296 area. This is done because a GC can print a message itself.
10297 That message would modify the echo area buffer's contents while a
10298 redisplay of the buffer is going on, and seriously confuse
10299 redisplay. */
10300 ptrdiff_t count = inhibit_garbage_collection ();
10301
10302 /* If there is no message, we must call display_echo_area_1
10303 nevertheless because it resizes the window. But we will have to
10304 reset the echo_area_buffer in question to nil at the end because
10305 with_echo_area_buffer will sets it to an empty buffer. */
10306 i = display_last_displayed_message_p ? 1 : 0;
10307 no_message_p = NILP (echo_area_buffer[i]);
10308
10309 window_height_changed_p
10310 = with_echo_area_buffer (w, display_last_displayed_message_p,
10311 display_echo_area_1,
10312 (intptr_t) w, Qnil);
10313
10314 if (no_message_p)
10315 echo_area_buffer[i] = Qnil;
10316
10317 unbind_to (count, Qnil);
10318 return window_height_changed_p;
10319 }
10320
10321
10322 /* Helper for display_echo_area. Display the current buffer which
10323 contains the current echo area message in window W, a mini-window,
10324 a pointer to which is passed in A1. A2..A4 are currently not used.
10325 Change the height of W so that all of the message is displayed.
10326 Value is non-zero if height of W was changed. */
10327
10328 static int
10329 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10330 {
10331 intptr_t i1 = a1;
10332 struct window *w = (struct window *) i1;
10333 Lisp_Object window;
10334 struct text_pos start;
10335 int window_height_changed_p = 0;
10336
10337 /* Do this before displaying, so that we have a large enough glyph
10338 matrix for the display. If we can't get enough space for the
10339 whole text, display the last N lines. That works by setting w->start. */
10340 window_height_changed_p = resize_mini_window (w, 0);
10341
10342 /* Use the starting position chosen by resize_mini_window. */
10343 SET_TEXT_POS_FROM_MARKER (start, w->start);
10344
10345 /* Display. */
10346 clear_glyph_matrix (w->desired_matrix);
10347 XSETWINDOW (window, w);
10348 try_window (window, start, 0);
10349
10350 return window_height_changed_p;
10351 }
10352
10353
10354 /* Resize the echo area window to exactly the size needed for the
10355 currently displayed message, if there is one. If a mini-buffer
10356 is active, don't shrink it. */
10357
10358 void
10359 resize_echo_area_exactly (void)
10360 {
10361 if (BUFFERP (echo_area_buffer[0])
10362 && WINDOWP (echo_area_window))
10363 {
10364 struct window *w = XWINDOW (echo_area_window);
10365 int resized_p;
10366 Lisp_Object resize_exactly;
10367
10368 if (minibuf_level == 0)
10369 resize_exactly = Qt;
10370 else
10371 resize_exactly = Qnil;
10372
10373 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10374 (intptr_t) w, resize_exactly);
10375 if (resized_p)
10376 {
10377 ++windows_or_buffers_changed;
10378 ++update_mode_lines;
10379 redisplay_internal ();
10380 }
10381 }
10382 }
10383
10384
10385 /* Callback function for with_echo_area_buffer, when used from
10386 resize_echo_area_exactly. A1 contains a pointer to the window to
10387 resize, EXACTLY non-nil means resize the mini-window exactly to the
10388 size of the text displayed. A3 and A4 are not used. Value is what
10389 resize_mini_window returns. */
10390
10391 static int
10392 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10393 {
10394 intptr_t i1 = a1;
10395 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10396 }
10397
10398
10399 /* Resize mini-window W to fit the size of its contents. EXACT_P
10400 means size the window exactly to the size needed. Otherwise, it's
10401 only enlarged until W's buffer is empty.
10402
10403 Set W->start to the right place to begin display. If the whole
10404 contents fit, start at the beginning. Otherwise, start so as
10405 to make the end of the contents appear. This is particularly
10406 important for y-or-n-p, but seems desirable generally.
10407
10408 Value is non-zero if the window height has been changed. */
10409
10410 int
10411 resize_mini_window (struct window *w, int exact_p)
10412 {
10413 struct frame *f = XFRAME (w->frame);
10414 int window_height_changed_p = 0;
10415
10416 eassert (MINI_WINDOW_P (w));
10417
10418 /* By default, start display at the beginning. */
10419 set_marker_both (w->start, w->contents,
10420 BUF_BEGV (XBUFFER (w->contents)),
10421 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10422
10423 /* Don't resize windows while redisplaying a window; it would
10424 confuse redisplay functions when the size of the window they are
10425 displaying changes from under them. Such a resizing can happen,
10426 for instance, when which-func prints a long message while
10427 we are running fontification-functions. We're running these
10428 functions with safe_call which binds inhibit-redisplay to t. */
10429 if (!NILP (Vinhibit_redisplay))
10430 return 0;
10431
10432 /* Nil means don't try to resize. */
10433 if (NILP (Vresize_mini_windows)
10434 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10435 return 0;
10436
10437 if (!FRAME_MINIBUF_ONLY_P (f))
10438 {
10439 struct it it;
10440 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10441 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10442 int height;
10443 EMACS_INT max_height;
10444 int unit = FRAME_LINE_HEIGHT (f);
10445 struct text_pos start;
10446 struct buffer *old_current_buffer = NULL;
10447
10448 if (current_buffer != XBUFFER (w->contents))
10449 {
10450 old_current_buffer = current_buffer;
10451 set_buffer_internal (XBUFFER (w->contents));
10452 }
10453
10454 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10455
10456 /* Compute the max. number of lines specified by the user. */
10457 if (FLOATP (Vmax_mini_window_height))
10458 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10459 else if (INTEGERP (Vmax_mini_window_height))
10460 max_height = XINT (Vmax_mini_window_height);
10461 else
10462 max_height = total_height / 4;
10463
10464 /* Correct that max. height if it's bogus. */
10465 max_height = clip_to_bounds (1, max_height, total_height);
10466
10467 /* Find out the height of the text in the window. */
10468 if (it.line_wrap == TRUNCATE)
10469 height = 1;
10470 else
10471 {
10472 last_height = 0;
10473 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10474 if (it.max_ascent == 0 && it.max_descent == 0)
10475 height = it.current_y + last_height;
10476 else
10477 height = it.current_y + it.max_ascent + it.max_descent;
10478 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10479 height = (height + unit - 1) / unit;
10480 }
10481
10482 /* Compute a suitable window start. */
10483 if (height > max_height)
10484 {
10485 height = max_height;
10486 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10487 move_it_vertically_backward (&it, (height - 1) * unit);
10488 start = it.current.pos;
10489 }
10490 else
10491 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10492 SET_MARKER_FROM_TEXT_POS (w->start, start);
10493
10494 if (EQ (Vresize_mini_windows, Qgrow_only))
10495 {
10496 /* Let it grow only, until we display an empty message, in which
10497 case the window shrinks again. */
10498 if (height > WINDOW_TOTAL_LINES (w))
10499 {
10500 int old_height = WINDOW_TOTAL_LINES (w);
10501
10502 FRAME_WINDOWS_FROZEN (f) = 1;
10503 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10504 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10505 }
10506 else if (height < WINDOW_TOTAL_LINES (w)
10507 && (exact_p || BEGV == ZV))
10508 {
10509 int old_height = WINDOW_TOTAL_LINES (w);
10510
10511 FRAME_WINDOWS_FROZEN (f) = 0;
10512 shrink_mini_window (w);
10513 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10514 }
10515 }
10516 else
10517 {
10518 /* Always resize to exact size needed. */
10519 if (height > WINDOW_TOTAL_LINES (w))
10520 {
10521 int old_height = WINDOW_TOTAL_LINES (w);
10522
10523 FRAME_WINDOWS_FROZEN (f) = 1;
10524 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10525 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10526 }
10527 else if (height < WINDOW_TOTAL_LINES (w))
10528 {
10529 int old_height = WINDOW_TOTAL_LINES (w);
10530
10531 FRAME_WINDOWS_FROZEN (f) = 0;
10532 shrink_mini_window (w);
10533
10534 if (height)
10535 {
10536 FRAME_WINDOWS_FROZEN (f) = 1;
10537 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10538 }
10539
10540 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10541 }
10542 }
10543
10544 if (old_current_buffer)
10545 set_buffer_internal (old_current_buffer);
10546 }
10547
10548 return window_height_changed_p;
10549 }
10550
10551
10552 /* Value is the current message, a string, or nil if there is no
10553 current message. */
10554
10555 Lisp_Object
10556 current_message (void)
10557 {
10558 Lisp_Object msg;
10559
10560 if (!BUFFERP (echo_area_buffer[0]))
10561 msg = Qnil;
10562 else
10563 {
10564 with_echo_area_buffer (0, 0, current_message_1,
10565 (intptr_t) &msg, Qnil);
10566 if (NILP (msg))
10567 echo_area_buffer[0] = Qnil;
10568 }
10569
10570 return msg;
10571 }
10572
10573
10574 static int
10575 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10576 {
10577 intptr_t i1 = a1;
10578 Lisp_Object *msg = (Lisp_Object *) i1;
10579
10580 if (Z > BEG)
10581 *msg = make_buffer_string (BEG, Z, 1);
10582 else
10583 *msg = Qnil;
10584 return 0;
10585 }
10586
10587
10588 /* Push the current message on Vmessage_stack for later restoration
10589 by restore_message. Value is non-zero if the current message isn't
10590 empty. This is a relatively infrequent operation, so it's not
10591 worth optimizing. */
10592
10593 bool
10594 push_message (void)
10595 {
10596 Lisp_Object msg = current_message ();
10597 Vmessage_stack = Fcons (msg, Vmessage_stack);
10598 return STRINGP (msg);
10599 }
10600
10601
10602 /* Restore message display from the top of Vmessage_stack. */
10603
10604 void
10605 restore_message (void)
10606 {
10607 eassert (CONSP (Vmessage_stack));
10608 message3_nolog (XCAR (Vmessage_stack));
10609 }
10610
10611
10612 /* Handler for unwind-protect calling pop_message. */
10613
10614 void
10615 pop_message_unwind (void)
10616 {
10617 /* Pop the top-most entry off Vmessage_stack. */
10618 eassert (CONSP (Vmessage_stack));
10619 Vmessage_stack = XCDR (Vmessage_stack);
10620 }
10621
10622
10623 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10624 exits. If the stack is not empty, we have a missing pop_message
10625 somewhere. */
10626
10627 void
10628 check_message_stack (void)
10629 {
10630 if (!NILP (Vmessage_stack))
10631 emacs_abort ();
10632 }
10633
10634
10635 /* Truncate to NCHARS what will be displayed in the echo area the next
10636 time we display it---but don't redisplay it now. */
10637
10638 void
10639 truncate_echo_area (ptrdiff_t nchars)
10640 {
10641 if (nchars == 0)
10642 echo_area_buffer[0] = Qnil;
10643 else if (!noninteractive
10644 && INTERACTIVE
10645 && !NILP (echo_area_buffer[0]))
10646 {
10647 struct frame *sf = SELECTED_FRAME ();
10648 /* Error messages get reported properly by cmd_error, so this must be
10649 just an informative message; if the frame hasn't really been
10650 initialized yet, just toss it. */
10651 if (sf->glyphs_initialized_p)
10652 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10653 }
10654 }
10655
10656
10657 /* Helper function for truncate_echo_area. Truncate the current
10658 message to at most NCHARS characters. */
10659
10660 static int
10661 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10662 {
10663 if (BEG + nchars < Z)
10664 del_range (BEG + nchars, Z);
10665 if (Z == BEG)
10666 echo_area_buffer[0] = Qnil;
10667 return 0;
10668 }
10669
10670 /* Set the current message to STRING. */
10671
10672 static void
10673 set_message (Lisp_Object string)
10674 {
10675 eassert (STRINGP (string));
10676
10677 message_enable_multibyte = STRING_MULTIBYTE (string);
10678
10679 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10680 message_buf_print = 0;
10681 help_echo_showing_p = 0;
10682
10683 if (STRINGP (Vdebug_on_message)
10684 && STRINGP (string)
10685 && fast_string_match (Vdebug_on_message, string) >= 0)
10686 call_debugger (list2 (Qerror, string));
10687 }
10688
10689
10690 /* Helper function for set_message. First argument is ignored and second
10691 argument has the same meaning as for set_message.
10692 This function is called with the echo area buffer being current. */
10693
10694 static int
10695 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10696 {
10697 eassert (STRINGP (string));
10698
10699 /* Change multibyteness of the echo buffer appropriately. */
10700 if (message_enable_multibyte
10701 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10702 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10703
10704 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10705 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10706 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10707
10708 /* Insert new message at BEG. */
10709 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10710
10711 /* This function takes care of single/multibyte conversion.
10712 We just have to ensure that the echo area buffer has the right
10713 setting of enable_multibyte_characters. */
10714 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10715
10716 return 0;
10717 }
10718
10719
10720 /* Clear messages. CURRENT_P non-zero means clear the current
10721 message. LAST_DISPLAYED_P non-zero means clear the message
10722 last displayed. */
10723
10724 void
10725 clear_message (int current_p, int last_displayed_p)
10726 {
10727 if (current_p)
10728 {
10729 echo_area_buffer[0] = Qnil;
10730 message_cleared_p = 1;
10731 }
10732
10733 if (last_displayed_p)
10734 echo_area_buffer[1] = Qnil;
10735
10736 message_buf_print = 0;
10737 }
10738
10739 /* Clear garbaged frames.
10740
10741 This function is used where the old redisplay called
10742 redraw_garbaged_frames which in turn called redraw_frame which in
10743 turn called clear_frame. The call to clear_frame was a source of
10744 flickering. I believe a clear_frame is not necessary. It should
10745 suffice in the new redisplay to invalidate all current matrices,
10746 and ensure a complete redisplay of all windows. */
10747
10748 static void
10749 clear_garbaged_frames (void)
10750 {
10751 if (frame_garbaged)
10752 {
10753 Lisp_Object tail, frame;
10754 int changed_count = 0;
10755
10756 FOR_EACH_FRAME (tail, frame)
10757 {
10758 struct frame *f = XFRAME (frame);
10759
10760 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10761 {
10762 if (f->resized_p)
10763 {
10764 redraw_frame (f);
10765 f->force_flush_display_p = 1;
10766 }
10767 clear_current_matrices (f);
10768 changed_count++;
10769 f->garbaged = 0;
10770 f->resized_p = 0;
10771 }
10772 }
10773
10774 frame_garbaged = 0;
10775 if (changed_count)
10776 ++windows_or_buffers_changed;
10777 }
10778 }
10779
10780
10781 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10782 is non-zero update selected_frame. Value is non-zero if the
10783 mini-windows height has been changed. */
10784
10785 static int
10786 echo_area_display (int update_frame_p)
10787 {
10788 Lisp_Object mini_window;
10789 struct window *w;
10790 struct frame *f;
10791 int window_height_changed_p = 0;
10792 struct frame *sf = SELECTED_FRAME ();
10793
10794 mini_window = FRAME_MINIBUF_WINDOW (sf);
10795 w = XWINDOW (mini_window);
10796 f = XFRAME (WINDOW_FRAME (w));
10797
10798 /* Don't display if frame is invisible or not yet initialized. */
10799 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10800 return 0;
10801
10802 #ifdef HAVE_WINDOW_SYSTEM
10803 /* When Emacs starts, selected_frame may be the initial terminal
10804 frame. If we let this through, a message would be displayed on
10805 the terminal. */
10806 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10807 return 0;
10808 #endif /* HAVE_WINDOW_SYSTEM */
10809
10810 /* Redraw garbaged frames. */
10811 clear_garbaged_frames ();
10812
10813 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10814 {
10815 echo_area_window = mini_window;
10816 window_height_changed_p = display_echo_area (w);
10817 w->must_be_updated_p = 1;
10818
10819 /* Update the display, unless called from redisplay_internal.
10820 Also don't update the screen during redisplay itself. The
10821 update will happen at the end of redisplay, and an update
10822 here could cause confusion. */
10823 if (update_frame_p && !redisplaying_p)
10824 {
10825 int n = 0;
10826
10827 /* If the display update has been interrupted by pending
10828 input, update mode lines in the frame. Due to the
10829 pending input, it might have been that redisplay hasn't
10830 been called, so that mode lines above the echo area are
10831 garbaged. This looks odd, so we prevent it here. */
10832 if (!display_completed)
10833 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10834
10835 if (window_height_changed_p
10836 /* Don't do this if Emacs is shutting down. Redisplay
10837 needs to run hooks. */
10838 && !NILP (Vrun_hooks))
10839 {
10840 /* Must update other windows. Likewise as in other
10841 cases, don't let this update be interrupted by
10842 pending input. */
10843 ptrdiff_t count = SPECPDL_INDEX ();
10844 specbind (Qredisplay_dont_pause, Qt);
10845 windows_or_buffers_changed = 1;
10846 redisplay_internal ();
10847 unbind_to (count, Qnil);
10848 }
10849 else if (FRAME_WINDOW_P (f) && n == 0)
10850 {
10851 /* Window configuration is the same as before.
10852 Can do with a display update of the echo area,
10853 unless we displayed some mode lines. */
10854 update_single_window (w, 1);
10855 FRAME_RIF (f)->flush_display (f);
10856 }
10857 else
10858 update_frame (f, 1, 1);
10859
10860 /* If cursor is in the echo area, make sure that the next
10861 redisplay displays the minibuffer, so that the cursor will
10862 be replaced with what the minibuffer wants. */
10863 if (cursor_in_echo_area)
10864 ++windows_or_buffers_changed;
10865 }
10866 }
10867 else if (!EQ (mini_window, selected_window))
10868 windows_or_buffers_changed++;
10869
10870 /* Last displayed message is now the current message. */
10871 echo_area_buffer[1] = echo_area_buffer[0];
10872 /* Inform read_char that we're not echoing. */
10873 echo_message_buffer = Qnil;
10874
10875 /* Prevent redisplay optimization in redisplay_internal by resetting
10876 this_line_start_pos. This is done because the mini-buffer now
10877 displays the message instead of its buffer text. */
10878 if (EQ (mini_window, selected_window))
10879 CHARPOS (this_line_start_pos) = 0;
10880
10881 return window_height_changed_p;
10882 }
10883
10884 /* Nonzero if the current window's buffer is shown in more than one
10885 window and was modified since last redisplay. */
10886
10887 static int
10888 buffer_shared_and_changed (void)
10889 {
10890 return (buffer_window_count (current_buffer) > 1
10891 && UNCHANGED_MODIFIED < MODIFF);
10892 }
10893
10894 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10895 is enabled and mark of W's buffer was changed since last W's update. */
10896
10897 static int
10898 window_buffer_changed (struct window *w)
10899 {
10900 struct buffer *b = XBUFFER (w->contents);
10901
10902 eassert (BUFFER_LIVE_P (b));
10903
10904 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10905 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10906 != (w->region_showing != 0)));
10907 }
10908
10909 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10910
10911 static int
10912 mode_line_update_needed (struct window *w)
10913 {
10914 return (w->column_number_displayed != -1
10915 && !(PT == w->last_point && !window_outdated (w))
10916 && (w->column_number_displayed != current_column ()));
10917 }
10918
10919 /* Nonzero if window start of W is frozen and may not be changed during
10920 redisplay. */
10921
10922 static bool
10923 window_frozen_p (struct window *w)
10924 {
10925 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10926 {
10927 Lisp_Object window;
10928
10929 XSETWINDOW (window, w);
10930 if (MINI_WINDOW_P (w))
10931 return 0;
10932 else if (EQ (window, selected_window))
10933 return 0;
10934 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10935 && EQ (window, Vminibuf_scroll_window))
10936 /* This special window can't be frozen too. */
10937 return 0;
10938 else
10939 return 1;
10940 }
10941 return 0;
10942 }
10943
10944 /***********************************************************************
10945 Mode Lines and Frame Titles
10946 ***********************************************************************/
10947
10948 /* A buffer for constructing non-propertized mode-line strings and
10949 frame titles in it; allocated from the heap in init_xdisp and
10950 resized as needed in store_mode_line_noprop_char. */
10951
10952 static char *mode_line_noprop_buf;
10953
10954 /* The buffer's end, and a current output position in it. */
10955
10956 static char *mode_line_noprop_buf_end;
10957 static char *mode_line_noprop_ptr;
10958
10959 #define MODE_LINE_NOPROP_LEN(start) \
10960 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10961
10962 static enum {
10963 MODE_LINE_DISPLAY = 0,
10964 MODE_LINE_TITLE,
10965 MODE_LINE_NOPROP,
10966 MODE_LINE_STRING
10967 } mode_line_target;
10968
10969 /* Alist that caches the results of :propertize.
10970 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10971 static Lisp_Object mode_line_proptrans_alist;
10972
10973 /* List of strings making up the mode-line. */
10974 static Lisp_Object mode_line_string_list;
10975
10976 /* Base face property when building propertized mode line string. */
10977 static Lisp_Object mode_line_string_face;
10978 static Lisp_Object mode_line_string_face_prop;
10979
10980
10981 /* Unwind data for mode line strings */
10982
10983 static Lisp_Object Vmode_line_unwind_vector;
10984
10985 static Lisp_Object
10986 format_mode_line_unwind_data (struct frame *target_frame,
10987 struct buffer *obuf,
10988 Lisp_Object owin,
10989 int save_proptrans)
10990 {
10991 Lisp_Object vector, tmp;
10992
10993 /* Reduce consing by keeping one vector in
10994 Vwith_echo_area_save_vector. */
10995 vector = Vmode_line_unwind_vector;
10996 Vmode_line_unwind_vector = Qnil;
10997
10998 if (NILP (vector))
10999 vector = Fmake_vector (make_number (10), Qnil);
11000
11001 ASET (vector, 0, make_number (mode_line_target));
11002 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11003 ASET (vector, 2, mode_line_string_list);
11004 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11005 ASET (vector, 4, mode_line_string_face);
11006 ASET (vector, 5, mode_line_string_face_prop);
11007
11008 if (obuf)
11009 XSETBUFFER (tmp, obuf);
11010 else
11011 tmp = Qnil;
11012 ASET (vector, 6, tmp);
11013 ASET (vector, 7, owin);
11014 if (target_frame)
11015 {
11016 /* Similarly to `with-selected-window', if the operation selects
11017 a window on another frame, we must restore that frame's
11018 selected window, and (for a tty) the top-frame. */
11019 ASET (vector, 8, target_frame->selected_window);
11020 if (FRAME_TERMCAP_P (target_frame))
11021 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11022 }
11023
11024 return vector;
11025 }
11026
11027 static void
11028 unwind_format_mode_line (Lisp_Object vector)
11029 {
11030 Lisp_Object old_window = AREF (vector, 7);
11031 Lisp_Object target_frame_window = AREF (vector, 8);
11032 Lisp_Object old_top_frame = AREF (vector, 9);
11033
11034 mode_line_target = XINT (AREF (vector, 0));
11035 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11036 mode_line_string_list = AREF (vector, 2);
11037 if (! EQ (AREF (vector, 3), Qt))
11038 mode_line_proptrans_alist = AREF (vector, 3);
11039 mode_line_string_face = AREF (vector, 4);
11040 mode_line_string_face_prop = AREF (vector, 5);
11041
11042 /* Select window before buffer, since it may change the buffer. */
11043 if (!NILP (old_window))
11044 {
11045 /* If the operation that we are unwinding had selected a window
11046 on a different frame, reset its frame-selected-window. For a
11047 text terminal, reset its top-frame if necessary. */
11048 if (!NILP (target_frame_window))
11049 {
11050 Lisp_Object frame
11051 = WINDOW_FRAME (XWINDOW (target_frame_window));
11052
11053 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11054 Fselect_window (target_frame_window, Qt);
11055
11056 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11057 Fselect_frame (old_top_frame, Qt);
11058 }
11059
11060 Fselect_window (old_window, Qt);
11061 }
11062
11063 if (!NILP (AREF (vector, 6)))
11064 {
11065 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11066 ASET (vector, 6, Qnil);
11067 }
11068
11069 Vmode_line_unwind_vector = vector;
11070 }
11071
11072
11073 /* Store a single character C for the frame title in mode_line_noprop_buf.
11074 Re-allocate mode_line_noprop_buf if necessary. */
11075
11076 static void
11077 store_mode_line_noprop_char (char c)
11078 {
11079 /* If output position has reached the end of the allocated buffer,
11080 increase the buffer's size. */
11081 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11082 {
11083 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11084 ptrdiff_t size = len;
11085 mode_line_noprop_buf =
11086 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11087 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11088 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11089 }
11090
11091 *mode_line_noprop_ptr++ = c;
11092 }
11093
11094
11095 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11096 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11097 characters that yield more columns than PRECISION; PRECISION <= 0
11098 means copy the whole string. Pad with spaces until FIELD_WIDTH
11099 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11100 pad. Called from display_mode_element when it is used to build a
11101 frame title. */
11102
11103 static int
11104 store_mode_line_noprop (const char *string, int field_width, int precision)
11105 {
11106 const unsigned char *str = (const unsigned char *) string;
11107 int n = 0;
11108 ptrdiff_t dummy, nbytes;
11109
11110 /* Copy at most PRECISION chars from STR. */
11111 nbytes = strlen (string);
11112 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11113 while (nbytes--)
11114 store_mode_line_noprop_char (*str++);
11115
11116 /* Fill up with spaces until FIELD_WIDTH reached. */
11117 while (field_width > 0
11118 && n < field_width)
11119 {
11120 store_mode_line_noprop_char (' ');
11121 ++n;
11122 }
11123
11124 return n;
11125 }
11126
11127 /***********************************************************************
11128 Frame Titles
11129 ***********************************************************************/
11130
11131 #ifdef HAVE_WINDOW_SYSTEM
11132
11133 /* Set the title of FRAME, if it has changed. The title format is
11134 Vicon_title_format if FRAME is iconified, otherwise it is
11135 frame_title_format. */
11136
11137 static void
11138 x_consider_frame_title (Lisp_Object frame)
11139 {
11140 struct frame *f = XFRAME (frame);
11141
11142 if (FRAME_WINDOW_P (f)
11143 || FRAME_MINIBUF_ONLY_P (f)
11144 || f->explicit_name)
11145 {
11146 /* Do we have more than one visible frame on this X display? */
11147 Lisp_Object tail, other_frame, fmt;
11148 ptrdiff_t title_start;
11149 char *title;
11150 ptrdiff_t len;
11151 struct it it;
11152 ptrdiff_t count = SPECPDL_INDEX ();
11153
11154 FOR_EACH_FRAME (tail, other_frame)
11155 {
11156 struct frame *tf = XFRAME (other_frame);
11157
11158 if (tf != f
11159 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11160 && !FRAME_MINIBUF_ONLY_P (tf)
11161 && !EQ (other_frame, tip_frame)
11162 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11163 break;
11164 }
11165
11166 /* Set global variable indicating that multiple frames exist. */
11167 multiple_frames = CONSP (tail);
11168
11169 /* Switch to the buffer of selected window of the frame. Set up
11170 mode_line_target so that display_mode_element will output into
11171 mode_line_noprop_buf; then display the title. */
11172 record_unwind_protect (unwind_format_mode_line,
11173 format_mode_line_unwind_data
11174 (f, current_buffer, selected_window, 0));
11175
11176 Fselect_window (f->selected_window, Qt);
11177 set_buffer_internal_1
11178 (XBUFFER (XWINDOW (f->selected_window)->contents));
11179 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11180
11181 mode_line_target = MODE_LINE_TITLE;
11182 title_start = MODE_LINE_NOPROP_LEN (0);
11183 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11184 NULL, DEFAULT_FACE_ID);
11185 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11186 len = MODE_LINE_NOPROP_LEN (title_start);
11187 title = mode_line_noprop_buf + title_start;
11188 unbind_to (count, Qnil);
11189
11190 /* Set the title only if it's changed. This avoids consing in
11191 the common case where it hasn't. (If it turns out that we've
11192 already wasted too much time by walking through the list with
11193 display_mode_element, then we might need to optimize at a
11194 higher level than this.) */
11195 if (! STRINGP (f->name)
11196 || SBYTES (f->name) != len
11197 || memcmp (title, SDATA (f->name), len) != 0)
11198 x_implicitly_set_name (f, make_string (title, len), Qnil);
11199 }
11200 }
11201
11202 #endif /* not HAVE_WINDOW_SYSTEM */
11203
11204 \f
11205 /***********************************************************************
11206 Menu Bars
11207 ***********************************************************************/
11208
11209
11210 /* Prepare for redisplay by updating menu-bar item lists when
11211 appropriate. This can call eval. */
11212
11213 void
11214 prepare_menu_bars (void)
11215 {
11216 int all_windows;
11217 struct gcpro gcpro1, gcpro2;
11218 struct frame *f;
11219 Lisp_Object tooltip_frame;
11220
11221 #ifdef HAVE_WINDOW_SYSTEM
11222 tooltip_frame = tip_frame;
11223 #else
11224 tooltip_frame = Qnil;
11225 #endif
11226
11227 /* Update all frame titles based on their buffer names, etc. We do
11228 this before the menu bars so that the buffer-menu will show the
11229 up-to-date frame titles. */
11230 #ifdef HAVE_WINDOW_SYSTEM
11231 if (windows_or_buffers_changed || update_mode_lines)
11232 {
11233 Lisp_Object tail, frame;
11234
11235 FOR_EACH_FRAME (tail, frame)
11236 {
11237 f = XFRAME (frame);
11238 if (!EQ (frame, tooltip_frame)
11239 && (FRAME_ICONIFIED_P (f)
11240 || FRAME_VISIBLE_P (f) == 1
11241 /* Exclude TTY frames that are obscured because they
11242 are not the top frame on their console. This is
11243 because x_consider_frame_title actually switches
11244 to the frame, which for TTY frames means it is
11245 marked as garbaged, and will be completely
11246 redrawn on the next redisplay cycle. This causes
11247 TTY frames to be completely redrawn, when there
11248 are more than one of them, even though nothing
11249 should be changed on display. */
11250 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11251 x_consider_frame_title (frame);
11252 }
11253 }
11254 #endif /* HAVE_WINDOW_SYSTEM */
11255
11256 /* Update the menu bar item lists, if appropriate. This has to be
11257 done before any actual redisplay or generation of display lines. */
11258 all_windows = (update_mode_lines
11259 || buffer_shared_and_changed ()
11260 || windows_or_buffers_changed);
11261 if (all_windows)
11262 {
11263 Lisp_Object tail, frame;
11264 ptrdiff_t count = SPECPDL_INDEX ();
11265 /* 1 means that update_menu_bar has run its hooks
11266 so any further calls to update_menu_bar shouldn't do so again. */
11267 int menu_bar_hooks_run = 0;
11268
11269 record_unwind_save_match_data ();
11270
11271 FOR_EACH_FRAME (tail, frame)
11272 {
11273 f = XFRAME (frame);
11274
11275 /* Ignore tooltip frame. */
11276 if (EQ (frame, tooltip_frame))
11277 continue;
11278
11279 /* If a window on this frame changed size, report that to
11280 the user and clear the size-change flag. */
11281 if (FRAME_WINDOW_SIZES_CHANGED (f))
11282 {
11283 Lisp_Object functions;
11284
11285 /* Clear flag first in case we get an error below. */
11286 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11287 functions = Vwindow_size_change_functions;
11288 GCPRO2 (tail, functions);
11289
11290 while (CONSP (functions))
11291 {
11292 if (!EQ (XCAR (functions), Qt))
11293 call1 (XCAR (functions), frame);
11294 functions = XCDR (functions);
11295 }
11296 UNGCPRO;
11297 }
11298
11299 GCPRO1 (tail);
11300 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11301 #ifdef HAVE_WINDOW_SYSTEM
11302 update_tool_bar (f, 0);
11303 #endif
11304 #ifdef HAVE_NS
11305 if (windows_or_buffers_changed
11306 && FRAME_NS_P (f))
11307 ns_set_doc_edited
11308 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11309 #endif
11310 UNGCPRO;
11311 }
11312
11313 unbind_to (count, Qnil);
11314 }
11315 else
11316 {
11317 struct frame *sf = SELECTED_FRAME ();
11318 update_menu_bar (sf, 1, 0);
11319 #ifdef HAVE_WINDOW_SYSTEM
11320 update_tool_bar (sf, 1);
11321 #endif
11322 }
11323 }
11324
11325
11326 /* Update the menu bar item list for frame F. This has to be done
11327 before we start to fill in any display lines, because it can call
11328 eval.
11329
11330 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11331
11332 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11333 already ran the menu bar hooks for this redisplay, so there
11334 is no need to run them again. The return value is the
11335 updated value of this flag, to pass to the next call. */
11336
11337 static int
11338 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11339 {
11340 Lisp_Object window;
11341 register struct window *w;
11342
11343 /* If called recursively during a menu update, do nothing. This can
11344 happen when, for instance, an activate-menubar-hook causes a
11345 redisplay. */
11346 if (inhibit_menubar_update)
11347 return hooks_run;
11348
11349 window = FRAME_SELECTED_WINDOW (f);
11350 w = XWINDOW (window);
11351
11352 if (FRAME_WINDOW_P (f)
11353 ?
11354 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11355 || defined (HAVE_NS) || defined (USE_GTK)
11356 FRAME_EXTERNAL_MENU_BAR (f)
11357 #else
11358 FRAME_MENU_BAR_LINES (f) > 0
11359 #endif
11360 : FRAME_MENU_BAR_LINES (f) > 0)
11361 {
11362 /* If the user has switched buffers or windows, we need to
11363 recompute to reflect the new bindings. But we'll
11364 recompute when update_mode_lines is set too; that means
11365 that people can use force-mode-line-update to request
11366 that the menu bar be recomputed. The adverse effect on
11367 the rest of the redisplay algorithm is about the same as
11368 windows_or_buffers_changed anyway. */
11369 if (windows_or_buffers_changed
11370 /* This used to test w->update_mode_line, but we believe
11371 there is no need to recompute the menu in that case. */
11372 || update_mode_lines
11373 || window_buffer_changed (w))
11374 {
11375 struct buffer *prev = current_buffer;
11376 ptrdiff_t count = SPECPDL_INDEX ();
11377
11378 specbind (Qinhibit_menubar_update, Qt);
11379
11380 set_buffer_internal_1 (XBUFFER (w->contents));
11381 if (save_match_data)
11382 record_unwind_save_match_data ();
11383 if (NILP (Voverriding_local_map_menu_flag))
11384 {
11385 specbind (Qoverriding_terminal_local_map, Qnil);
11386 specbind (Qoverriding_local_map, Qnil);
11387 }
11388
11389 if (!hooks_run)
11390 {
11391 /* Run the Lucid hook. */
11392 safe_run_hooks (Qactivate_menubar_hook);
11393
11394 /* If it has changed current-menubar from previous value,
11395 really recompute the menu-bar from the value. */
11396 if (! NILP (Vlucid_menu_bar_dirty_flag))
11397 call0 (Qrecompute_lucid_menubar);
11398
11399 safe_run_hooks (Qmenu_bar_update_hook);
11400
11401 hooks_run = 1;
11402 }
11403
11404 XSETFRAME (Vmenu_updating_frame, f);
11405 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11406
11407 /* Redisplay the menu bar in case we changed it. */
11408 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11409 || defined (HAVE_NS) || defined (USE_GTK)
11410 if (FRAME_WINDOW_P (f))
11411 {
11412 #if defined (HAVE_NS)
11413 /* All frames on Mac OS share the same menubar. So only
11414 the selected frame should be allowed to set it. */
11415 if (f == SELECTED_FRAME ())
11416 #endif
11417 set_frame_menubar (f, 0, 0);
11418 }
11419 else
11420 /* On a terminal screen, the menu bar is an ordinary screen
11421 line, and this makes it get updated. */
11422 w->update_mode_line = 1;
11423 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11424 /* In the non-toolkit version, the menu bar is an ordinary screen
11425 line, and this makes it get updated. */
11426 w->update_mode_line = 1;
11427 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11428
11429 unbind_to (count, Qnil);
11430 set_buffer_internal_1 (prev);
11431 }
11432 }
11433
11434 return hooks_run;
11435 }
11436
11437 /***********************************************************************
11438 Tool-bars
11439 ***********************************************************************/
11440
11441 #ifdef HAVE_WINDOW_SYSTEM
11442
11443 /* Where the mouse was last time we reported a mouse event. */
11444
11445 struct frame *last_mouse_frame;
11446
11447 /* Tool-bar item index of the item on which a mouse button was pressed
11448 or -1. */
11449
11450 int last_tool_bar_item;
11451
11452 /* Select `frame' temporarily without running all the code in
11453 do_switch_frame.
11454 FIXME: Maybe do_switch_frame should be trimmed down similarly
11455 when `norecord' is set. */
11456 static void
11457 fast_set_selected_frame (Lisp_Object frame)
11458 {
11459 if (!EQ (selected_frame, frame))
11460 {
11461 selected_frame = frame;
11462 selected_window = XFRAME (frame)->selected_window;
11463 }
11464 }
11465
11466 /* Update the tool-bar item list for frame F. This has to be done
11467 before we start to fill in any display lines. Called from
11468 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11469 and restore it here. */
11470
11471 static void
11472 update_tool_bar (struct frame *f, int save_match_data)
11473 {
11474 #if defined (USE_GTK) || defined (HAVE_NS)
11475 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11476 #else
11477 int do_update = WINDOWP (f->tool_bar_window)
11478 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11479 #endif
11480
11481 if (do_update)
11482 {
11483 Lisp_Object window;
11484 struct window *w;
11485
11486 window = FRAME_SELECTED_WINDOW (f);
11487 w = XWINDOW (window);
11488
11489 /* If the user has switched buffers or windows, we need to
11490 recompute to reflect the new bindings. But we'll
11491 recompute when update_mode_lines is set too; that means
11492 that people can use force-mode-line-update to request
11493 that the menu bar be recomputed. The adverse effect on
11494 the rest of the redisplay algorithm is about the same as
11495 windows_or_buffers_changed anyway. */
11496 if (windows_or_buffers_changed
11497 || w->update_mode_line
11498 || update_mode_lines
11499 || window_buffer_changed (w))
11500 {
11501 struct buffer *prev = current_buffer;
11502 ptrdiff_t count = SPECPDL_INDEX ();
11503 Lisp_Object frame, new_tool_bar;
11504 int new_n_tool_bar;
11505 struct gcpro gcpro1;
11506
11507 /* Set current_buffer to the buffer of the selected
11508 window of the frame, so that we get the right local
11509 keymaps. */
11510 set_buffer_internal_1 (XBUFFER (w->contents));
11511
11512 /* Save match data, if we must. */
11513 if (save_match_data)
11514 record_unwind_save_match_data ();
11515
11516 /* Make sure that we don't accidentally use bogus keymaps. */
11517 if (NILP (Voverriding_local_map_menu_flag))
11518 {
11519 specbind (Qoverriding_terminal_local_map, Qnil);
11520 specbind (Qoverriding_local_map, Qnil);
11521 }
11522
11523 GCPRO1 (new_tool_bar);
11524
11525 /* We must temporarily set the selected frame to this frame
11526 before calling tool_bar_items, because the calculation of
11527 the tool-bar keymap uses the selected frame (see
11528 `tool-bar-make-keymap' in tool-bar.el). */
11529 eassert (EQ (selected_window,
11530 /* Since we only explicitly preserve selected_frame,
11531 check that selected_window would be redundant. */
11532 XFRAME (selected_frame)->selected_window));
11533 record_unwind_protect (fast_set_selected_frame, selected_frame);
11534 XSETFRAME (frame, f);
11535 fast_set_selected_frame (frame);
11536
11537 /* Build desired tool-bar items from keymaps. */
11538 new_tool_bar
11539 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11540 &new_n_tool_bar);
11541
11542 /* Redisplay the tool-bar if we changed it. */
11543 if (new_n_tool_bar != f->n_tool_bar_items
11544 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11545 {
11546 /* Redisplay that happens asynchronously due to an expose event
11547 may access f->tool_bar_items. Make sure we update both
11548 variables within BLOCK_INPUT so no such event interrupts. */
11549 block_input ();
11550 fset_tool_bar_items (f, new_tool_bar);
11551 f->n_tool_bar_items = new_n_tool_bar;
11552 w->update_mode_line = 1;
11553 unblock_input ();
11554 }
11555
11556 UNGCPRO;
11557
11558 unbind_to (count, Qnil);
11559 set_buffer_internal_1 (prev);
11560 }
11561 }
11562 }
11563
11564
11565 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11566 F's desired tool-bar contents. F->tool_bar_items must have
11567 been set up previously by calling prepare_menu_bars. */
11568
11569 static void
11570 build_desired_tool_bar_string (struct frame *f)
11571 {
11572 int i, size, size_needed;
11573 struct gcpro gcpro1, gcpro2, gcpro3;
11574 Lisp_Object image, plist, props;
11575
11576 image = plist = props = Qnil;
11577 GCPRO3 (image, plist, props);
11578
11579 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11580 Otherwise, make a new string. */
11581
11582 /* The size of the string we might be able to reuse. */
11583 size = (STRINGP (f->desired_tool_bar_string)
11584 ? SCHARS (f->desired_tool_bar_string)
11585 : 0);
11586
11587 /* We need one space in the string for each image. */
11588 size_needed = f->n_tool_bar_items;
11589
11590 /* Reuse f->desired_tool_bar_string, if possible. */
11591 if (size < size_needed || NILP (f->desired_tool_bar_string))
11592 fset_desired_tool_bar_string
11593 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11594 else
11595 {
11596 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11597 Fremove_text_properties (make_number (0), make_number (size),
11598 props, f->desired_tool_bar_string);
11599 }
11600
11601 /* Put a `display' property on the string for the images to display,
11602 put a `menu_item' property on tool-bar items with a value that
11603 is the index of the item in F's tool-bar item vector. */
11604 for (i = 0; i < f->n_tool_bar_items; ++i)
11605 {
11606 #define PROP(IDX) \
11607 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11608
11609 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11610 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11611 int hmargin, vmargin, relief, idx, end;
11612
11613 /* If image is a vector, choose the image according to the
11614 button state. */
11615 image = PROP (TOOL_BAR_ITEM_IMAGES);
11616 if (VECTORP (image))
11617 {
11618 if (enabled_p)
11619 idx = (selected_p
11620 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11621 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11622 else
11623 idx = (selected_p
11624 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11625 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11626
11627 eassert (ASIZE (image) >= idx);
11628 image = AREF (image, idx);
11629 }
11630 else
11631 idx = -1;
11632
11633 /* Ignore invalid image specifications. */
11634 if (!valid_image_p (image))
11635 continue;
11636
11637 /* Display the tool-bar button pressed, or depressed. */
11638 plist = Fcopy_sequence (XCDR (image));
11639
11640 /* Compute margin and relief to draw. */
11641 relief = (tool_bar_button_relief >= 0
11642 ? tool_bar_button_relief
11643 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11644 hmargin = vmargin = relief;
11645
11646 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11647 INT_MAX - max (hmargin, vmargin)))
11648 {
11649 hmargin += XFASTINT (Vtool_bar_button_margin);
11650 vmargin += XFASTINT (Vtool_bar_button_margin);
11651 }
11652 else if (CONSP (Vtool_bar_button_margin))
11653 {
11654 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11655 INT_MAX - hmargin))
11656 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11657
11658 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11659 INT_MAX - vmargin))
11660 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11661 }
11662
11663 if (auto_raise_tool_bar_buttons_p)
11664 {
11665 /* Add a `:relief' property to the image spec if the item is
11666 selected. */
11667 if (selected_p)
11668 {
11669 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11670 hmargin -= relief;
11671 vmargin -= relief;
11672 }
11673 }
11674 else
11675 {
11676 /* If image is selected, display it pressed, i.e. with a
11677 negative relief. If it's not selected, display it with a
11678 raised relief. */
11679 plist = Fplist_put (plist, QCrelief,
11680 (selected_p
11681 ? make_number (-relief)
11682 : make_number (relief)));
11683 hmargin -= relief;
11684 vmargin -= relief;
11685 }
11686
11687 /* Put a margin around the image. */
11688 if (hmargin || vmargin)
11689 {
11690 if (hmargin == vmargin)
11691 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11692 else
11693 plist = Fplist_put (plist, QCmargin,
11694 Fcons (make_number (hmargin),
11695 make_number (vmargin)));
11696 }
11697
11698 /* If button is not enabled, and we don't have special images
11699 for the disabled state, make the image appear disabled by
11700 applying an appropriate algorithm to it. */
11701 if (!enabled_p && idx < 0)
11702 plist = Fplist_put (plist, QCconversion, Qdisabled);
11703
11704 /* Put a `display' text property on the string for the image to
11705 display. Put a `menu-item' property on the string that gives
11706 the start of this item's properties in the tool-bar items
11707 vector. */
11708 image = Fcons (Qimage, plist);
11709 props = list4 (Qdisplay, image,
11710 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11711
11712 /* Let the last image hide all remaining spaces in the tool bar
11713 string. The string can be longer than needed when we reuse a
11714 previous string. */
11715 if (i + 1 == f->n_tool_bar_items)
11716 end = SCHARS (f->desired_tool_bar_string);
11717 else
11718 end = i + 1;
11719 Fadd_text_properties (make_number (i), make_number (end),
11720 props, f->desired_tool_bar_string);
11721 #undef PROP
11722 }
11723
11724 UNGCPRO;
11725 }
11726
11727
11728 /* Display one line of the tool-bar of frame IT->f.
11729
11730 HEIGHT specifies the desired height of the tool-bar line.
11731 If the actual height of the glyph row is less than HEIGHT, the
11732 row's height is increased to HEIGHT, and the icons are centered
11733 vertically in the new height.
11734
11735 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11736 count a final empty row in case the tool-bar width exactly matches
11737 the window width.
11738 */
11739
11740 static void
11741 display_tool_bar_line (struct it *it, int height)
11742 {
11743 struct glyph_row *row = it->glyph_row;
11744 int max_x = it->last_visible_x;
11745 struct glyph *last;
11746
11747 prepare_desired_row (row);
11748 row->y = it->current_y;
11749
11750 /* Note that this isn't made use of if the face hasn't a box,
11751 so there's no need to check the face here. */
11752 it->start_of_box_run_p = 1;
11753
11754 while (it->current_x < max_x)
11755 {
11756 int x, n_glyphs_before, i, nglyphs;
11757 struct it it_before;
11758
11759 /* Get the next display element. */
11760 if (!get_next_display_element (it))
11761 {
11762 /* Don't count empty row if we are counting needed tool-bar lines. */
11763 if (height < 0 && !it->hpos)
11764 return;
11765 break;
11766 }
11767
11768 /* Produce glyphs. */
11769 n_glyphs_before = row->used[TEXT_AREA];
11770 it_before = *it;
11771
11772 PRODUCE_GLYPHS (it);
11773
11774 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11775 i = 0;
11776 x = it_before.current_x;
11777 while (i < nglyphs)
11778 {
11779 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11780
11781 if (x + glyph->pixel_width > max_x)
11782 {
11783 /* Glyph doesn't fit on line. Backtrack. */
11784 row->used[TEXT_AREA] = n_glyphs_before;
11785 *it = it_before;
11786 /* If this is the only glyph on this line, it will never fit on the
11787 tool-bar, so skip it. But ensure there is at least one glyph,
11788 so we don't accidentally disable the tool-bar. */
11789 if (n_glyphs_before == 0
11790 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11791 break;
11792 goto out;
11793 }
11794
11795 ++it->hpos;
11796 x += glyph->pixel_width;
11797 ++i;
11798 }
11799
11800 /* Stop at line end. */
11801 if (ITERATOR_AT_END_OF_LINE_P (it))
11802 break;
11803
11804 set_iterator_to_next (it, 1);
11805 }
11806
11807 out:;
11808
11809 row->displays_text_p = row->used[TEXT_AREA] != 0;
11810
11811 /* Use default face for the border below the tool bar.
11812
11813 FIXME: When auto-resize-tool-bars is grow-only, there is
11814 no additional border below the possibly empty tool-bar lines.
11815 So to make the extra empty lines look "normal", we have to
11816 use the tool-bar face for the border too. */
11817 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11818 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11819 it->face_id = DEFAULT_FACE_ID;
11820
11821 extend_face_to_end_of_line (it);
11822 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11823 last->right_box_line_p = 1;
11824 if (last == row->glyphs[TEXT_AREA])
11825 last->left_box_line_p = 1;
11826
11827 /* Make line the desired height and center it vertically. */
11828 if ((height -= it->max_ascent + it->max_descent) > 0)
11829 {
11830 /* Don't add more than one line height. */
11831 height %= FRAME_LINE_HEIGHT (it->f);
11832 it->max_ascent += height / 2;
11833 it->max_descent += (height + 1) / 2;
11834 }
11835
11836 compute_line_metrics (it);
11837
11838 /* If line is empty, make it occupy the rest of the tool-bar. */
11839 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11840 {
11841 row->height = row->phys_height = it->last_visible_y - row->y;
11842 row->visible_height = row->height;
11843 row->ascent = row->phys_ascent = 0;
11844 row->extra_line_spacing = 0;
11845 }
11846
11847 row->full_width_p = 1;
11848 row->continued_p = 0;
11849 row->truncated_on_left_p = 0;
11850 row->truncated_on_right_p = 0;
11851
11852 it->current_x = it->hpos = 0;
11853 it->current_y += row->height;
11854 ++it->vpos;
11855 ++it->glyph_row;
11856 }
11857
11858
11859 /* Max tool-bar height. */
11860
11861 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11862 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11863
11864 /* Value is the number of screen lines needed to make all tool-bar
11865 items of frame F visible. The number of actual rows needed is
11866 returned in *N_ROWS if non-NULL. */
11867
11868 static int
11869 tool_bar_lines_needed (struct frame *f, int *n_rows)
11870 {
11871 struct window *w = XWINDOW (f->tool_bar_window);
11872 struct it it;
11873 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11874 the desired matrix, so use (unused) mode-line row as temporary row to
11875 avoid destroying the first tool-bar row. */
11876 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11877
11878 /* Initialize an iterator for iteration over
11879 F->desired_tool_bar_string in the tool-bar window of frame F. */
11880 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11881 it.first_visible_x = 0;
11882 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11883 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11884 it.paragraph_embedding = L2R;
11885
11886 while (!ITERATOR_AT_END_P (&it))
11887 {
11888 clear_glyph_row (temp_row);
11889 it.glyph_row = temp_row;
11890 display_tool_bar_line (&it, -1);
11891 }
11892 clear_glyph_row (temp_row);
11893
11894 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11895 if (n_rows)
11896 *n_rows = it.vpos > 0 ? it.vpos : -1;
11897
11898 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11899 }
11900
11901
11902 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11903 0, 1, 0,
11904 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11905 If FRAME is nil or omitted, use the selected frame. */)
11906 (Lisp_Object frame)
11907 {
11908 struct frame *f = decode_any_frame (frame);
11909 struct window *w;
11910 int nlines = 0;
11911
11912 if (WINDOWP (f->tool_bar_window)
11913 && (w = XWINDOW (f->tool_bar_window),
11914 WINDOW_TOTAL_LINES (w) > 0))
11915 {
11916 update_tool_bar (f, 1);
11917 if (f->n_tool_bar_items)
11918 {
11919 build_desired_tool_bar_string (f);
11920 nlines = tool_bar_lines_needed (f, NULL);
11921 }
11922 }
11923
11924 return make_number (nlines);
11925 }
11926
11927
11928 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11929 height should be changed. */
11930
11931 static int
11932 redisplay_tool_bar (struct frame *f)
11933 {
11934 struct window *w;
11935 struct it it;
11936 struct glyph_row *row;
11937
11938 #if defined (USE_GTK) || defined (HAVE_NS)
11939 if (FRAME_EXTERNAL_TOOL_BAR (f))
11940 update_frame_tool_bar (f);
11941 return 0;
11942 #endif
11943
11944 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11945 do anything. This means you must start with tool-bar-lines
11946 non-zero to get the auto-sizing effect. Or in other words, you
11947 can turn off tool-bars by specifying tool-bar-lines zero. */
11948 if (!WINDOWP (f->tool_bar_window)
11949 || (w = XWINDOW (f->tool_bar_window),
11950 WINDOW_TOTAL_LINES (w) == 0))
11951 return 0;
11952
11953 /* Set up an iterator for the tool-bar window. */
11954 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11955 it.first_visible_x = 0;
11956 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11957 row = it.glyph_row;
11958
11959 /* Build a string that represents the contents of the tool-bar. */
11960 build_desired_tool_bar_string (f);
11961 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11962 /* FIXME: This should be controlled by a user option. But it
11963 doesn't make sense to have an R2L tool bar if the menu bar cannot
11964 be drawn also R2L, and making the menu bar R2L is tricky due
11965 toolkit-specific code that implements it. If an R2L tool bar is
11966 ever supported, display_tool_bar_line should also be augmented to
11967 call unproduce_glyphs like display_line and display_string
11968 do. */
11969 it.paragraph_embedding = L2R;
11970
11971 if (f->n_tool_bar_rows == 0)
11972 {
11973 int nlines;
11974
11975 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11976 nlines != WINDOW_TOTAL_LINES (w)))
11977 {
11978 Lisp_Object frame;
11979 int old_height = WINDOW_TOTAL_LINES (w);
11980
11981 XSETFRAME (frame, f);
11982 Fmodify_frame_parameters (frame,
11983 list1 (Fcons (Qtool_bar_lines,
11984 make_number (nlines))));
11985 if (WINDOW_TOTAL_LINES (w) != old_height)
11986 {
11987 clear_glyph_matrix (w->desired_matrix);
11988 f->fonts_changed = 1;
11989 return 1;
11990 }
11991 }
11992 }
11993
11994 /* Display as many lines as needed to display all tool-bar items. */
11995
11996 if (f->n_tool_bar_rows > 0)
11997 {
11998 int border, rows, height, extra;
11999
12000 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12001 border = XINT (Vtool_bar_border);
12002 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12003 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12004 else if (EQ (Vtool_bar_border, Qborder_width))
12005 border = f->border_width;
12006 else
12007 border = 0;
12008 if (border < 0)
12009 border = 0;
12010
12011 rows = f->n_tool_bar_rows;
12012 height = max (1, (it.last_visible_y - border) / rows);
12013 extra = it.last_visible_y - border - height * rows;
12014
12015 while (it.current_y < it.last_visible_y)
12016 {
12017 int h = 0;
12018 if (extra > 0 && rows-- > 0)
12019 {
12020 h = (extra + rows - 1) / rows;
12021 extra -= h;
12022 }
12023 display_tool_bar_line (&it, height + h);
12024 }
12025 }
12026 else
12027 {
12028 while (it.current_y < it.last_visible_y)
12029 display_tool_bar_line (&it, 0);
12030 }
12031
12032 /* It doesn't make much sense to try scrolling in the tool-bar
12033 window, so don't do it. */
12034 w->desired_matrix->no_scrolling_p = 1;
12035 w->must_be_updated_p = 1;
12036
12037 if (!NILP (Vauto_resize_tool_bars))
12038 {
12039 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12040 int change_height_p = 0;
12041
12042 /* If we couldn't display everything, change the tool-bar's
12043 height if there is room for more. */
12044 if (IT_STRING_CHARPOS (it) < it.end_charpos
12045 && it.current_y < max_tool_bar_height)
12046 change_height_p = 1;
12047
12048 row = it.glyph_row - 1;
12049
12050 /* If there are blank lines at the end, except for a partially
12051 visible blank line at the end that is smaller than
12052 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12053 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12054 && row->height >= FRAME_LINE_HEIGHT (f))
12055 change_height_p = 1;
12056
12057 /* If row displays tool-bar items, but is partially visible,
12058 change the tool-bar's height. */
12059 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12060 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12061 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12062 change_height_p = 1;
12063
12064 /* Resize windows as needed by changing the `tool-bar-lines'
12065 frame parameter. */
12066 if (change_height_p)
12067 {
12068 Lisp_Object frame;
12069 int old_height = WINDOW_TOTAL_LINES (w);
12070 int nrows;
12071 int nlines = tool_bar_lines_needed (f, &nrows);
12072
12073 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12074 && !f->minimize_tool_bar_window_p)
12075 ? (nlines > old_height)
12076 : (nlines != old_height));
12077 f->minimize_tool_bar_window_p = 0;
12078
12079 if (change_height_p)
12080 {
12081 XSETFRAME (frame, f);
12082 Fmodify_frame_parameters (frame,
12083 list1 (Fcons (Qtool_bar_lines,
12084 make_number (nlines))));
12085 if (WINDOW_TOTAL_LINES (w) != old_height)
12086 {
12087 clear_glyph_matrix (w->desired_matrix);
12088 f->n_tool_bar_rows = nrows;
12089 f->fonts_changed = 1;
12090 return 1;
12091 }
12092 }
12093 }
12094 }
12095
12096 f->minimize_tool_bar_window_p = 0;
12097 return 0;
12098 }
12099
12100
12101 /* Get information about the tool-bar item which is displayed in GLYPH
12102 on frame F. Return in *PROP_IDX the index where tool-bar item
12103 properties start in F->tool_bar_items. Value is zero if
12104 GLYPH doesn't display a tool-bar item. */
12105
12106 static int
12107 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12108 {
12109 Lisp_Object prop;
12110 int success_p;
12111 int charpos;
12112
12113 /* This function can be called asynchronously, which means we must
12114 exclude any possibility that Fget_text_property signals an
12115 error. */
12116 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12117 charpos = max (0, charpos);
12118
12119 /* Get the text property `menu-item' at pos. The value of that
12120 property is the start index of this item's properties in
12121 F->tool_bar_items. */
12122 prop = Fget_text_property (make_number (charpos),
12123 Qmenu_item, f->current_tool_bar_string);
12124 if (INTEGERP (prop))
12125 {
12126 *prop_idx = XINT (prop);
12127 success_p = 1;
12128 }
12129 else
12130 success_p = 0;
12131
12132 return success_p;
12133 }
12134
12135 \f
12136 /* Get information about the tool-bar item at position X/Y on frame F.
12137 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12138 the current matrix of the tool-bar window of F, or NULL if not
12139 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12140 item in F->tool_bar_items. Value is
12141
12142 -1 if X/Y is not on a tool-bar item
12143 0 if X/Y is on the same item that was highlighted before.
12144 1 otherwise. */
12145
12146 static int
12147 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12148 int *hpos, int *vpos, int *prop_idx)
12149 {
12150 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12151 struct window *w = XWINDOW (f->tool_bar_window);
12152 int area;
12153
12154 /* Find the glyph under X/Y. */
12155 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12156 if (*glyph == NULL)
12157 return -1;
12158
12159 /* Get the start of this tool-bar item's properties in
12160 f->tool_bar_items. */
12161 if (!tool_bar_item_info (f, *glyph, prop_idx))
12162 return -1;
12163
12164 /* Is mouse on the highlighted item? */
12165 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12166 && *vpos >= hlinfo->mouse_face_beg_row
12167 && *vpos <= hlinfo->mouse_face_end_row
12168 && (*vpos > hlinfo->mouse_face_beg_row
12169 || *hpos >= hlinfo->mouse_face_beg_col)
12170 && (*vpos < hlinfo->mouse_face_end_row
12171 || *hpos < hlinfo->mouse_face_end_col
12172 || hlinfo->mouse_face_past_end))
12173 return 0;
12174
12175 return 1;
12176 }
12177
12178
12179 /* EXPORT:
12180 Handle mouse button event on the tool-bar of frame F, at
12181 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12182 0 for button release. MODIFIERS is event modifiers for button
12183 release. */
12184
12185 void
12186 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12187 int modifiers)
12188 {
12189 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12190 struct window *w = XWINDOW (f->tool_bar_window);
12191 int hpos, vpos, prop_idx;
12192 struct glyph *glyph;
12193 Lisp_Object enabled_p;
12194 int ts;
12195
12196 /* If not on the highlighted tool-bar item, and mouse-highlight is
12197 non-nil, return. This is so we generate the tool-bar button
12198 click only when the mouse button is released on the same item as
12199 where it was pressed. However, when mouse-highlight is disabled,
12200 generate the click when the button is released regardless of the
12201 highlight, since tool-bar items are not highlighted in that
12202 case. */
12203 frame_to_window_pixel_xy (w, &x, &y);
12204 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12205 if (ts == -1
12206 || (ts != 0 && !NILP (Vmouse_highlight)))
12207 return;
12208
12209 /* When mouse-highlight is off, generate the click for the item
12210 where the button was pressed, disregarding where it was
12211 released. */
12212 if (NILP (Vmouse_highlight) && !down_p)
12213 prop_idx = last_tool_bar_item;
12214
12215 /* If item is disabled, do nothing. */
12216 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12217 if (NILP (enabled_p))
12218 return;
12219
12220 if (down_p)
12221 {
12222 /* Show item in pressed state. */
12223 if (!NILP (Vmouse_highlight))
12224 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12225 last_tool_bar_item = prop_idx;
12226 }
12227 else
12228 {
12229 Lisp_Object key, frame;
12230 struct input_event event;
12231 EVENT_INIT (event);
12232
12233 /* Show item in released state. */
12234 if (!NILP (Vmouse_highlight))
12235 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12236
12237 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12238
12239 XSETFRAME (frame, f);
12240 event.kind = TOOL_BAR_EVENT;
12241 event.frame_or_window = frame;
12242 event.arg = frame;
12243 kbd_buffer_store_event (&event);
12244
12245 event.kind = TOOL_BAR_EVENT;
12246 event.frame_or_window = frame;
12247 event.arg = key;
12248 event.modifiers = modifiers;
12249 kbd_buffer_store_event (&event);
12250 last_tool_bar_item = -1;
12251 }
12252 }
12253
12254
12255 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12256 tool-bar window-relative coordinates X/Y. Called from
12257 note_mouse_highlight. */
12258
12259 static void
12260 note_tool_bar_highlight (struct frame *f, int x, int y)
12261 {
12262 Lisp_Object window = f->tool_bar_window;
12263 struct window *w = XWINDOW (window);
12264 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12265 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12266 int hpos, vpos;
12267 struct glyph *glyph;
12268 struct glyph_row *row;
12269 int i;
12270 Lisp_Object enabled_p;
12271 int prop_idx;
12272 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12273 int mouse_down_p, rc;
12274
12275 /* Function note_mouse_highlight is called with negative X/Y
12276 values when mouse moves outside of the frame. */
12277 if (x <= 0 || y <= 0)
12278 {
12279 clear_mouse_face (hlinfo);
12280 return;
12281 }
12282
12283 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12284 if (rc < 0)
12285 {
12286 /* Not on tool-bar item. */
12287 clear_mouse_face (hlinfo);
12288 return;
12289 }
12290 else if (rc == 0)
12291 /* On same tool-bar item as before. */
12292 goto set_help_echo;
12293
12294 clear_mouse_face (hlinfo);
12295
12296 /* Mouse is down, but on different tool-bar item? */
12297 mouse_down_p = (dpyinfo->grabbed
12298 && f == last_mouse_frame
12299 && FRAME_LIVE_P (f));
12300 if (mouse_down_p
12301 && last_tool_bar_item != prop_idx)
12302 return;
12303
12304 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12305
12306 /* If tool-bar item is not enabled, don't highlight it. */
12307 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12308 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12309 {
12310 /* Compute the x-position of the glyph. In front and past the
12311 image is a space. We include this in the highlighted area. */
12312 row = MATRIX_ROW (w->current_matrix, vpos);
12313 for (i = x = 0; i < hpos; ++i)
12314 x += row->glyphs[TEXT_AREA][i].pixel_width;
12315
12316 /* Record this as the current active region. */
12317 hlinfo->mouse_face_beg_col = hpos;
12318 hlinfo->mouse_face_beg_row = vpos;
12319 hlinfo->mouse_face_beg_x = x;
12320 hlinfo->mouse_face_past_end = 0;
12321
12322 hlinfo->mouse_face_end_col = hpos + 1;
12323 hlinfo->mouse_face_end_row = vpos;
12324 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12325 hlinfo->mouse_face_window = window;
12326 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12327
12328 /* Display it as active. */
12329 show_mouse_face (hlinfo, draw);
12330 }
12331
12332 set_help_echo:
12333
12334 /* Set help_echo_string to a help string to display for this tool-bar item.
12335 XTread_socket does the rest. */
12336 help_echo_object = help_echo_window = Qnil;
12337 help_echo_pos = -1;
12338 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12339 if (NILP (help_echo_string))
12340 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12341 }
12342
12343 #endif /* HAVE_WINDOW_SYSTEM */
12344
12345
12346 \f
12347 /************************************************************************
12348 Horizontal scrolling
12349 ************************************************************************/
12350
12351 static int hscroll_window_tree (Lisp_Object);
12352 static int hscroll_windows (Lisp_Object);
12353
12354 /* For all leaf windows in the window tree rooted at WINDOW, set their
12355 hscroll value so that PT is (i) visible in the window, and (ii) so
12356 that it is not within a certain margin at the window's left and
12357 right border. Value is non-zero if any window's hscroll has been
12358 changed. */
12359
12360 static int
12361 hscroll_window_tree (Lisp_Object window)
12362 {
12363 int hscrolled_p = 0;
12364 int hscroll_relative_p = FLOATP (Vhscroll_step);
12365 int hscroll_step_abs = 0;
12366 double hscroll_step_rel = 0;
12367
12368 if (hscroll_relative_p)
12369 {
12370 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12371 if (hscroll_step_rel < 0)
12372 {
12373 hscroll_relative_p = 0;
12374 hscroll_step_abs = 0;
12375 }
12376 }
12377 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12378 {
12379 hscroll_step_abs = XINT (Vhscroll_step);
12380 if (hscroll_step_abs < 0)
12381 hscroll_step_abs = 0;
12382 }
12383 else
12384 hscroll_step_abs = 0;
12385
12386 while (WINDOWP (window))
12387 {
12388 struct window *w = XWINDOW (window);
12389
12390 if (WINDOWP (w->contents))
12391 hscrolled_p |= hscroll_window_tree (w->contents);
12392 else if (w->cursor.vpos >= 0)
12393 {
12394 int h_margin;
12395 int text_area_width;
12396 struct glyph_row *current_cursor_row
12397 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12398 struct glyph_row *desired_cursor_row
12399 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12400 struct glyph_row *cursor_row
12401 = (desired_cursor_row->enabled_p
12402 ? desired_cursor_row
12403 : current_cursor_row);
12404 int row_r2l_p = cursor_row->reversed_p;
12405
12406 text_area_width = window_box_width (w, TEXT_AREA);
12407
12408 /* Scroll when cursor is inside this scroll margin. */
12409 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12410
12411 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12412 /* For left-to-right rows, hscroll when cursor is either
12413 (i) inside the right hscroll margin, or (ii) if it is
12414 inside the left margin and the window is already
12415 hscrolled. */
12416 && ((!row_r2l_p
12417 && ((w->hscroll
12418 && w->cursor.x <= h_margin)
12419 || (cursor_row->enabled_p
12420 && cursor_row->truncated_on_right_p
12421 && (w->cursor.x >= text_area_width - h_margin))))
12422 /* For right-to-left rows, the logic is similar,
12423 except that rules for scrolling to left and right
12424 are reversed. E.g., if cursor.x <= h_margin, we
12425 need to hscroll "to the right" unconditionally,
12426 and that will scroll the screen to the left so as
12427 to reveal the next portion of the row. */
12428 || (row_r2l_p
12429 && ((cursor_row->enabled_p
12430 /* FIXME: It is confusing to set the
12431 truncated_on_right_p flag when R2L rows
12432 are actually truncated on the left. */
12433 && cursor_row->truncated_on_right_p
12434 && w->cursor.x <= h_margin)
12435 || (w->hscroll
12436 && (w->cursor.x >= text_area_width - h_margin))))))
12437 {
12438 struct it it;
12439 ptrdiff_t hscroll;
12440 struct buffer *saved_current_buffer;
12441 ptrdiff_t pt;
12442 int wanted_x;
12443
12444 /* Find point in a display of infinite width. */
12445 saved_current_buffer = current_buffer;
12446 current_buffer = XBUFFER (w->contents);
12447
12448 if (w == XWINDOW (selected_window))
12449 pt = PT;
12450 else
12451 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12452
12453 /* Move iterator to pt starting at cursor_row->start in
12454 a line with infinite width. */
12455 init_to_row_start (&it, w, cursor_row);
12456 it.last_visible_x = INFINITY;
12457 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12458 current_buffer = saved_current_buffer;
12459
12460 /* Position cursor in window. */
12461 if (!hscroll_relative_p && hscroll_step_abs == 0)
12462 hscroll = max (0, (it.current_x
12463 - (ITERATOR_AT_END_OF_LINE_P (&it)
12464 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12465 : (text_area_width / 2))))
12466 / FRAME_COLUMN_WIDTH (it.f);
12467 else if ((!row_r2l_p
12468 && w->cursor.x >= text_area_width - h_margin)
12469 || (row_r2l_p && w->cursor.x <= h_margin))
12470 {
12471 if (hscroll_relative_p)
12472 wanted_x = text_area_width * (1 - hscroll_step_rel)
12473 - h_margin;
12474 else
12475 wanted_x = text_area_width
12476 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12477 - h_margin;
12478 hscroll
12479 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12480 }
12481 else
12482 {
12483 if (hscroll_relative_p)
12484 wanted_x = text_area_width * hscroll_step_rel
12485 + h_margin;
12486 else
12487 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12488 + h_margin;
12489 hscroll
12490 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12491 }
12492 hscroll = max (hscroll, w->min_hscroll);
12493
12494 /* Don't prevent redisplay optimizations if hscroll
12495 hasn't changed, as it will unnecessarily slow down
12496 redisplay. */
12497 if (w->hscroll != hscroll)
12498 {
12499 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12500 w->hscroll = hscroll;
12501 hscrolled_p = 1;
12502 }
12503 }
12504 }
12505
12506 window = w->next;
12507 }
12508
12509 /* Value is non-zero if hscroll of any leaf window has been changed. */
12510 return hscrolled_p;
12511 }
12512
12513
12514 /* Set hscroll so that cursor is visible and not inside horizontal
12515 scroll margins for all windows in the tree rooted at WINDOW. See
12516 also hscroll_window_tree above. Value is non-zero if any window's
12517 hscroll has been changed. If it has, desired matrices on the frame
12518 of WINDOW are cleared. */
12519
12520 static int
12521 hscroll_windows (Lisp_Object window)
12522 {
12523 int hscrolled_p = hscroll_window_tree (window);
12524 if (hscrolled_p)
12525 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12526 return hscrolled_p;
12527 }
12528
12529
12530 \f
12531 /************************************************************************
12532 Redisplay
12533 ************************************************************************/
12534
12535 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12536 to a non-zero value. This is sometimes handy to have in a debugger
12537 session. */
12538
12539 #ifdef GLYPH_DEBUG
12540
12541 /* First and last unchanged row for try_window_id. */
12542
12543 static int debug_first_unchanged_at_end_vpos;
12544 static int debug_last_unchanged_at_beg_vpos;
12545
12546 /* Delta vpos and y. */
12547
12548 static int debug_dvpos, debug_dy;
12549
12550 /* Delta in characters and bytes for try_window_id. */
12551
12552 static ptrdiff_t debug_delta, debug_delta_bytes;
12553
12554 /* Values of window_end_pos and window_end_vpos at the end of
12555 try_window_id. */
12556
12557 static ptrdiff_t debug_end_vpos;
12558
12559 /* Append a string to W->desired_matrix->method. FMT is a printf
12560 format string. If trace_redisplay_p is non-zero also printf the
12561 resulting string to stderr. */
12562
12563 static void debug_method_add (struct window *, char const *, ...)
12564 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12565
12566 static void
12567 debug_method_add (struct window *w, char const *fmt, ...)
12568 {
12569 void *ptr = w;
12570 char *method = w->desired_matrix->method;
12571 int len = strlen (method);
12572 int size = sizeof w->desired_matrix->method;
12573 int remaining = size - len - 1;
12574 va_list ap;
12575
12576 if (len && remaining)
12577 {
12578 method[len] = '|';
12579 --remaining, ++len;
12580 }
12581
12582 va_start (ap, fmt);
12583 vsnprintf (method + len, remaining + 1, fmt, ap);
12584 va_end (ap);
12585
12586 if (trace_redisplay_p)
12587 fprintf (stderr, "%p (%s): %s\n",
12588 ptr,
12589 ((BUFFERP (w->contents)
12590 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12591 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12592 : "no buffer"),
12593 method + len);
12594 }
12595
12596 #endif /* GLYPH_DEBUG */
12597
12598
12599 /* Value is non-zero if all changes in window W, which displays
12600 current_buffer, are in the text between START and END. START is a
12601 buffer position, END is given as a distance from Z. Used in
12602 redisplay_internal for display optimization. */
12603
12604 static int
12605 text_outside_line_unchanged_p (struct window *w,
12606 ptrdiff_t start, ptrdiff_t end)
12607 {
12608 int unchanged_p = 1;
12609
12610 /* If text or overlays have changed, see where. */
12611 if (window_outdated (w))
12612 {
12613 /* Gap in the line? */
12614 if (GPT < start || Z - GPT < end)
12615 unchanged_p = 0;
12616
12617 /* Changes start in front of the line, or end after it? */
12618 if (unchanged_p
12619 && (BEG_UNCHANGED < start - 1
12620 || END_UNCHANGED < end))
12621 unchanged_p = 0;
12622
12623 /* If selective display, can't optimize if changes start at the
12624 beginning of the line. */
12625 if (unchanged_p
12626 && INTEGERP (BVAR (current_buffer, selective_display))
12627 && XINT (BVAR (current_buffer, selective_display)) > 0
12628 && (BEG_UNCHANGED < start || GPT <= start))
12629 unchanged_p = 0;
12630
12631 /* If there are overlays at the start or end of the line, these
12632 may have overlay strings with newlines in them. A change at
12633 START, for instance, may actually concern the display of such
12634 overlay strings as well, and they are displayed on different
12635 lines. So, quickly rule out this case. (For the future, it
12636 might be desirable to implement something more telling than
12637 just BEG/END_UNCHANGED.) */
12638 if (unchanged_p)
12639 {
12640 if (BEG + BEG_UNCHANGED == start
12641 && overlay_touches_p (start))
12642 unchanged_p = 0;
12643 if (END_UNCHANGED == end
12644 && overlay_touches_p (Z - end))
12645 unchanged_p = 0;
12646 }
12647
12648 /* Under bidi reordering, adding or deleting a character in the
12649 beginning of a paragraph, before the first strong directional
12650 character, can change the base direction of the paragraph (unless
12651 the buffer specifies a fixed paragraph direction), which will
12652 require to redisplay the whole paragraph. It might be worthwhile
12653 to find the paragraph limits and widen the range of redisplayed
12654 lines to that, but for now just give up this optimization. */
12655 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12656 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12657 unchanged_p = 0;
12658 }
12659
12660 return unchanged_p;
12661 }
12662
12663
12664 /* Do a frame update, taking possible shortcuts into account. This is
12665 the main external entry point for redisplay.
12666
12667 If the last redisplay displayed an echo area message and that message
12668 is no longer requested, we clear the echo area or bring back the
12669 mini-buffer if that is in use. */
12670
12671 void
12672 redisplay (void)
12673 {
12674 redisplay_internal ();
12675 }
12676
12677
12678 static Lisp_Object
12679 overlay_arrow_string_or_property (Lisp_Object var)
12680 {
12681 Lisp_Object val;
12682
12683 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12684 return val;
12685
12686 return Voverlay_arrow_string;
12687 }
12688
12689 /* Return 1 if there are any overlay-arrows in current_buffer. */
12690 static int
12691 overlay_arrow_in_current_buffer_p (void)
12692 {
12693 Lisp_Object vlist;
12694
12695 for (vlist = Voverlay_arrow_variable_list;
12696 CONSP (vlist);
12697 vlist = XCDR (vlist))
12698 {
12699 Lisp_Object var = XCAR (vlist);
12700 Lisp_Object val;
12701
12702 if (!SYMBOLP (var))
12703 continue;
12704 val = find_symbol_value (var);
12705 if (MARKERP (val)
12706 && current_buffer == XMARKER (val)->buffer)
12707 return 1;
12708 }
12709 return 0;
12710 }
12711
12712
12713 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12714 has changed. */
12715
12716 static int
12717 overlay_arrows_changed_p (void)
12718 {
12719 Lisp_Object vlist;
12720
12721 for (vlist = Voverlay_arrow_variable_list;
12722 CONSP (vlist);
12723 vlist = XCDR (vlist))
12724 {
12725 Lisp_Object var = XCAR (vlist);
12726 Lisp_Object val, pstr;
12727
12728 if (!SYMBOLP (var))
12729 continue;
12730 val = find_symbol_value (var);
12731 if (!MARKERP (val))
12732 continue;
12733 if (! EQ (COERCE_MARKER (val),
12734 Fget (var, Qlast_arrow_position))
12735 || ! (pstr = overlay_arrow_string_or_property (var),
12736 EQ (pstr, Fget (var, Qlast_arrow_string))))
12737 return 1;
12738 }
12739 return 0;
12740 }
12741
12742 /* Mark overlay arrows to be updated on next redisplay. */
12743
12744 static void
12745 update_overlay_arrows (int up_to_date)
12746 {
12747 Lisp_Object vlist;
12748
12749 for (vlist = Voverlay_arrow_variable_list;
12750 CONSP (vlist);
12751 vlist = XCDR (vlist))
12752 {
12753 Lisp_Object var = XCAR (vlist);
12754
12755 if (!SYMBOLP (var))
12756 continue;
12757
12758 if (up_to_date > 0)
12759 {
12760 Lisp_Object val = find_symbol_value (var);
12761 Fput (var, Qlast_arrow_position,
12762 COERCE_MARKER (val));
12763 Fput (var, Qlast_arrow_string,
12764 overlay_arrow_string_or_property (var));
12765 }
12766 else if (up_to_date < 0
12767 || !NILP (Fget (var, Qlast_arrow_position)))
12768 {
12769 Fput (var, Qlast_arrow_position, Qt);
12770 Fput (var, Qlast_arrow_string, Qt);
12771 }
12772 }
12773 }
12774
12775
12776 /* Return overlay arrow string to display at row.
12777 Return integer (bitmap number) for arrow bitmap in left fringe.
12778 Return nil if no overlay arrow. */
12779
12780 static Lisp_Object
12781 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12782 {
12783 Lisp_Object vlist;
12784
12785 for (vlist = Voverlay_arrow_variable_list;
12786 CONSP (vlist);
12787 vlist = XCDR (vlist))
12788 {
12789 Lisp_Object var = XCAR (vlist);
12790 Lisp_Object val;
12791
12792 if (!SYMBOLP (var))
12793 continue;
12794
12795 val = find_symbol_value (var);
12796
12797 if (MARKERP (val)
12798 && current_buffer == XMARKER (val)->buffer
12799 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12800 {
12801 if (FRAME_WINDOW_P (it->f)
12802 /* FIXME: if ROW->reversed_p is set, this should test
12803 the right fringe, not the left one. */
12804 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12805 {
12806 #ifdef HAVE_WINDOW_SYSTEM
12807 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12808 {
12809 int fringe_bitmap;
12810 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12811 return make_number (fringe_bitmap);
12812 }
12813 #endif
12814 return make_number (-1); /* Use default arrow bitmap. */
12815 }
12816 return overlay_arrow_string_or_property (var);
12817 }
12818 }
12819
12820 return Qnil;
12821 }
12822
12823 /* Return 1 if point moved out of or into a composition. Otherwise
12824 return 0. PREV_BUF and PREV_PT are the last point buffer and
12825 position. BUF and PT are the current point buffer and position. */
12826
12827 static int
12828 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12829 struct buffer *buf, ptrdiff_t pt)
12830 {
12831 ptrdiff_t start, end;
12832 Lisp_Object prop;
12833 Lisp_Object buffer;
12834
12835 XSETBUFFER (buffer, buf);
12836 /* Check a composition at the last point if point moved within the
12837 same buffer. */
12838 if (prev_buf == buf)
12839 {
12840 if (prev_pt == pt)
12841 /* Point didn't move. */
12842 return 0;
12843
12844 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12845 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12846 && composition_valid_p (start, end, prop)
12847 && start < prev_pt && end > prev_pt)
12848 /* The last point was within the composition. Return 1 iff
12849 point moved out of the composition. */
12850 return (pt <= start || pt >= end);
12851 }
12852
12853 /* Check a composition at the current point. */
12854 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12855 && find_composition (pt, -1, &start, &end, &prop, buffer)
12856 && composition_valid_p (start, end, prop)
12857 && start < pt && end > pt);
12858 }
12859
12860 /* Reconsider the clip changes of buffer which is displayed in W. */
12861
12862 static void
12863 reconsider_clip_changes (struct window *w)
12864 {
12865 struct buffer *b = XBUFFER (w->contents);
12866
12867 if (b->clip_changed
12868 && w->window_end_valid
12869 && w->current_matrix->buffer == b
12870 && w->current_matrix->zv == BUF_ZV (b)
12871 && w->current_matrix->begv == BUF_BEGV (b))
12872 b->clip_changed = 0;
12873
12874 /* If display wasn't paused, and W is not a tool bar window, see if
12875 point has been moved into or out of a composition. In that case,
12876 we set b->clip_changed to 1 to force updating the screen. If
12877 b->clip_changed has already been set to 1, we can skip this
12878 check. */
12879 if (!b->clip_changed && w->window_end_valid)
12880 {
12881 ptrdiff_t pt = (w == XWINDOW (selected_window)
12882 ? PT : marker_position (w->pointm));
12883
12884 if ((w->current_matrix->buffer != b || pt != w->last_point)
12885 && check_point_in_composition (w->current_matrix->buffer,
12886 w->last_point, b, pt))
12887 b->clip_changed = 1;
12888 }
12889 }
12890
12891 #define STOP_POLLING \
12892 do { if (! polling_stopped_here) stop_polling (); \
12893 polling_stopped_here = 1; } while (0)
12894
12895 #define RESUME_POLLING \
12896 do { if (polling_stopped_here) start_polling (); \
12897 polling_stopped_here = 0; } while (0)
12898
12899
12900 /* Perhaps in the future avoid recentering windows if it
12901 is not necessary; currently that causes some problems. */
12902
12903 static void
12904 redisplay_internal (void)
12905 {
12906 struct window *w = XWINDOW (selected_window);
12907 struct window *sw;
12908 struct frame *fr;
12909 int pending;
12910 bool must_finish = 0, match_p;
12911 struct text_pos tlbufpos, tlendpos;
12912 int number_of_visible_frames;
12913 ptrdiff_t count;
12914 struct frame *sf;
12915 int polling_stopped_here = 0;
12916 Lisp_Object tail, frame;
12917
12918 /* Non-zero means redisplay has to consider all windows on all
12919 frames. Zero means, only selected_window is considered. */
12920 int consider_all_windows_p;
12921
12922 /* Non-zero means redisplay has to redisplay the miniwindow. */
12923 int update_miniwindow_p = 0;
12924
12925 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12926
12927 /* No redisplay if running in batch mode or frame is not yet fully
12928 initialized, or redisplay is explicitly turned off by setting
12929 Vinhibit_redisplay. */
12930 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12931 || !NILP (Vinhibit_redisplay))
12932 return;
12933
12934 /* Don't examine these until after testing Vinhibit_redisplay.
12935 When Emacs is shutting down, perhaps because its connection to
12936 X has dropped, we should not look at them at all. */
12937 fr = XFRAME (w->frame);
12938 sf = SELECTED_FRAME ();
12939
12940 if (!fr->glyphs_initialized_p)
12941 return;
12942
12943 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12944 if (popup_activated ())
12945 return;
12946 #endif
12947
12948 /* I don't think this happens but let's be paranoid. */
12949 if (redisplaying_p)
12950 return;
12951
12952 /* Record a function that clears redisplaying_p
12953 when we leave this function. */
12954 count = SPECPDL_INDEX ();
12955 record_unwind_protect_void (unwind_redisplay);
12956 redisplaying_p = 1;
12957 specbind (Qinhibit_free_realized_faces, Qnil);
12958
12959 /* Record this function, so it appears on the profiler's backtraces. */
12960 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12961
12962 FOR_EACH_FRAME (tail, frame)
12963 XFRAME (frame)->already_hscrolled_p = 0;
12964
12965 retry:
12966 /* Remember the currently selected window. */
12967 sw = w;
12968
12969 pending = 0;
12970 last_escape_glyph_frame = NULL;
12971 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12972 last_glyphless_glyph_frame = NULL;
12973 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12974
12975 /* If face_change_count is non-zero, init_iterator will free all
12976 realized faces, which includes the faces referenced from current
12977 matrices. So, we can't reuse current matrices in this case. */
12978 if (face_change_count)
12979 ++windows_or_buffers_changed;
12980
12981 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12982 && FRAME_TTY (sf)->previous_frame != sf)
12983 {
12984 /* Since frames on a single ASCII terminal share the same
12985 display area, displaying a different frame means redisplay
12986 the whole thing. */
12987 windows_or_buffers_changed++;
12988 SET_FRAME_GARBAGED (sf);
12989 #ifndef DOS_NT
12990 set_tty_color_mode (FRAME_TTY (sf), sf);
12991 #endif
12992 FRAME_TTY (sf)->previous_frame = sf;
12993 }
12994
12995 /* Set the visible flags for all frames. Do this before checking for
12996 resized or garbaged frames; they want to know if their frames are
12997 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
12998 number_of_visible_frames = 0;
12999
13000 FOR_EACH_FRAME (tail, frame)
13001 {
13002 struct frame *f = XFRAME (frame);
13003
13004 if (FRAME_VISIBLE_P (f))
13005 {
13006 ++number_of_visible_frames;
13007 /* Adjust matrices for visible frames only. */
13008 if (f->fonts_changed)
13009 {
13010 adjust_frame_glyphs (f);
13011 f->fonts_changed = 0;
13012 }
13013 }
13014 clear_desired_matrices (f);
13015 }
13016
13017 /* Notice any pending interrupt request to change frame size. */
13018 do_pending_window_change (1);
13019
13020 /* do_pending_window_change could change the selected_window due to
13021 frame resizing which makes the selected window too small. */
13022 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13023 sw = w;
13024
13025 /* Clear frames marked as garbaged. */
13026 clear_garbaged_frames ();
13027
13028 /* Build menubar and tool-bar items. */
13029 if (NILP (Vmemory_full))
13030 prepare_menu_bars ();
13031
13032 if (windows_or_buffers_changed)
13033 update_mode_lines++;
13034
13035 reconsider_clip_changes (w);
13036
13037 /* In most cases selected window displays current buffer. */
13038 match_p = XBUFFER (w->contents) == current_buffer;
13039 if (match_p)
13040 {
13041 ptrdiff_t count1;
13042
13043 /* Detect case that we need to write or remove a star in the mode line. */
13044 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13045 {
13046 w->update_mode_line = 1;
13047 if (buffer_shared_and_changed ())
13048 update_mode_lines++;
13049 }
13050
13051 /* Avoid invocation of point motion hooks by `current_column' below. */
13052 count1 = SPECPDL_INDEX ();
13053 specbind (Qinhibit_point_motion_hooks, Qt);
13054
13055 if (mode_line_update_needed (w))
13056 w->update_mode_line = 1;
13057
13058 unbind_to (count1, Qnil);
13059 }
13060
13061 consider_all_windows_p = (update_mode_lines
13062 || buffer_shared_and_changed ()
13063 || cursor_type_changed);
13064
13065 /* If specs for an arrow have changed, do thorough redisplay
13066 to ensure we remove any arrow that should no longer exist. */
13067 if (overlay_arrows_changed_p ())
13068 consider_all_windows_p = windows_or_buffers_changed = 1;
13069
13070 /* Normally the message* functions will have already displayed and
13071 updated the echo area, but the frame may have been trashed, or
13072 the update may have been preempted, so display the echo area
13073 again here. Checking message_cleared_p captures the case that
13074 the echo area should be cleared. */
13075 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13076 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13077 || (message_cleared_p
13078 && minibuf_level == 0
13079 /* If the mini-window is currently selected, this means the
13080 echo-area doesn't show through. */
13081 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13082 {
13083 int window_height_changed_p = echo_area_display (0);
13084
13085 if (message_cleared_p)
13086 update_miniwindow_p = 1;
13087
13088 must_finish = 1;
13089
13090 /* If we don't display the current message, don't clear the
13091 message_cleared_p flag, because, if we did, we wouldn't clear
13092 the echo area in the next redisplay which doesn't preserve
13093 the echo area. */
13094 if (!display_last_displayed_message_p)
13095 message_cleared_p = 0;
13096
13097 if (window_height_changed_p)
13098 {
13099 consider_all_windows_p = 1;
13100 ++update_mode_lines;
13101 ++windows_or_buffers_changed;
13102
13103 /* If window configuration was changed, frames may have been
13104 marked garbaged. Clear them or we will experience
13105 surprises wrt scrolling. */
13106 clear_garbaged_frames ();
13107 }
13108 }
13109 else if (EQ (selected_window, minibuf_window)
13110 && (current_buffer->clip_changed || window_outdated (w))
13111 && resize_mini_window (w, 0))
13112 {
13113 /* Resized active mini-window to fit the size of what it is
13114 showing if its contents might have changed. */
13115 must_finish = 1;
13116 /* FIXME: this causes all frames to be updated, which seems unnecessary
13117 since only the current frame needs to be considered. This function
13118 needs to be rewritten with two variables, consider_all_windows and
13119 consider_all_frames. */
13120 consider_all_windows_p = 1;
13121 ++windows_or_buffers_changed;
13122 ++update_mode_lines;
13123
13124 /* If window configuration was changed, frames may have been
13125 marked garbaged. Clear them or we will experience
13126 surprises wrt scrolling. */
13127 clear_garbaged_frames ();
13128 }
13129
13130 /* If showing the region, and mark has changed, we must redisplay
13131 the whole window. The assignment to this_line_start_pos prevents
13132 the optimization directly below this if-statement. */
13133 if (((!NILP (Vtransient_mark_mode)
13134 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13135 != (w->region_showing > 0))
13136 || (w->region_showing
13137 && w->region_showing
13138 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13139 CHARPOS (this_line_start_pos) = 0;
13140
13141 /* Optimize the case that only the line containing the cursor in the
13142 selected window has changed. Variables starting with this_ are
13143 set in display_line and record information about the line
13144 containing the cursor. */
13145 tlbufpos = this_line_start_pos;
13146 tlendpos = this_line_end_pos;
13147 if (!consider_all_windows_p
13148 && CHARPOS (tlbufpos) > 0
13149 && !w->update_mode_line
13150 && !current_buffer->clip_changed
13151 && !current_buffer->prevent_redisplay_optimizations_p
13152 && FRAME_VISIBLE_P (XFRAME (w->frame))
13153 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13154 /* Make sure recorded data applies to current buffer, etc. */
13155 && this_line_buffer == current_buffer
13156 && match_p
13157 && !w->force_start
13158 && !w->optional_new_start
13159 /* Point must be on the line that we have info recorded about. */
13160 && PT >= CHARPOS (tlbufpos)
13161 && PT <= Z - CHARPOS (tlendpos)
13162 /* All text outside that line, including its final newline,
13163 must be unchanged. */
13164 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13165 CHARPOS (tlendpos)))
13166 {
13167 if (CHARPOS (tlbufpos) > BEGV
13168 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13169 && (CHARPOS (tlbufpos) == ZV
13170 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13171 /* Former continuation line has disappeared by becoming empty. */
13172 goto cancel;
13173 else if (window_outdated (w) || MINI_WINDOW_P (w))
13174 {
13175 /* We have to handle the case of continuation around a
13176 wide-column character (see the comment in indent.c around
13177 line 1340).
13178
13179 For instance, in the following case:
13180
13181 -------- Insert --------
13182 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13183 J_I_ ==> J_I_ `^^' are cursors.
13184 ^^ ^^
13185 -------- --------
13186
13187 As we have to redraw the line above, we cannot use this
13188 optimization. */
13189
13190 struct it it;
13191 int line_height_before = this_line_pixel_height;
13192
13193 /* Note that start_display will handle the case that the
13194 line starting at tlbufpos is a continuation line. */
13195 start_display (&it, w, tlbufpos);
13196
13197 /* Implementation note: It this still necessary? */
13198 if (it.current_x != this_line_start_x)
13199 goto cancel;
13200
13201 TRACE ((stderr, "trying display optimization 1\n"));
13202 w->cursor.vpos = -1;
13203 overlay_arrow_seen = 0;
13204 it.vpos = this_line_vpos;
13205 it.current_y = this_line_y;
13206 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13207 display_line (&it);
13208
13209 /* If line contains point, is not continued,
13210 and ends at same distance from eob as before, we win. */
13211 if (w->cursor.vpos >= 0
13212 /* Line is not continued, otherwise this_line_start_pos
13213 would have been set to 0 in display_line. */
13214 && CHARPOS (this_line_start_pos)
13215 /* Line ends as before. */
13216 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13217 /* Line has same height as before. Otherwise other lines
13218 would have to be shifted up or down. */
13219 && this_line_pixel_height == line_height_before)
13220 {
13221 /* If this is not the window's last line, we must adjust
13222 the charstarts of the lines below. */
13223 if (it.current_y < it.last_visible_y)
13224 {
13225 struct glyph_row *row
13226 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13227 ptrdiff_t delta, delta_bytes;
13228
13229 /* We used to distinguish between two cases here,
13230 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13231 when the line ends in a newline or the end of the
13232 buffer's accessible portion. But both cases did
13233 the same, so they were collapsed. */
13234 delta = (Z
13235 - CHARPOS (tlendpos)
13236 - MATRIX_ROW_START_CHARPOS (row));
13237 delta_bytes = (Z_BYTE
13238 - BYTEPOS (tlendpos)
13239 - MATRIX_ROW_START_BYTEPOS (row));
13240
13241 increment_matrix_positions (w->current_matrix,
13242 this_line_vpos + 1,
13243 w->current_matrix->nrows,
13244 delta, delta_bytes);
13245 }
13246
13247 /* If this row displays text now but previously didn't,
13248 or vice versa, w->window_end_vpos may have to be
13249 adjusted. */
13250 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13251 {
13252 if (w->window_end_vpos < this_line_vpos)
13253 w->window_end_vpos = this_line_vpos;
13254 }
13255 else if (w->window_end_vpos == this_line_vpos
13256 && this_line_vpos > 0)
13257 w->window_end_vpos = this_line_vpos - 1;
13258 w->window_end_valid = 0;
13259
13260 /* Update hint: No need to try to scroll in update_window. */
13261 w->desired_matrix->no_scrolling_p = 1;
13262
13263 #ifdef GLYPH_DEBUG
13264 *w->desired_matrix->method = 0;
13265 debug_method_add (w, "optimization 1");
13266 #endif
13267 #ifdef HAVE_WINDOW_SYSTEM
13268 update_window_fringes (w, 0);
13269 #endif
13270 goto update;
13271 }
13272 else
13273 goto cancel;
13274 }
13275 else if (/* Cursor position hasn't changed. */
13276 PT == w->last_point
13277 /* Make sure the cursor was last displayed
13278 in this window. Otherwise we have to reposition it. */
13279 && 0 <= w->cursor.vpos
13280 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13281 {
13282 if (!must_finish)
13283 {
13284 do_pending_window_change (1);
13285 /* If selected_window changed, redisplay again. */
13286 if (WINDOWP (selected_window)
13287 && (w = XWINDOW (selected_window)) != sw)
13288 goto retry;
13289
13290 /* We used to always goto end_of_redisplay here, but this
13291 isn't enough if we have a blinking cursor. */
13292 if (w->cursor_off_p == w->last_cursor_off_p)
13293 goto end_of_redisplay;
13294 }
13295 goto update;
13296 }
13297 /* If highlighting the region, or if the cursor is in the echo area,
13298 then we can't just move the cursor. */
13299 else if (! (!NILP (Vtransient_mark_mode)
13300 && !NILP (BVAR (current_buffer, mark_active)))
13301 && (EQ (selected_window,
13302 BVAR (current_buffer, last_selected_window))
13303 || highlight_nonselected_windows)
13304 && !w->region_showing
13305 && NILP (Vshow_trailing_whitespace)
13306 && !cursor_in_echo_area)
13307 {
13308 struct it it;
13309 struct glyph_row *row;
13310
13311 /* Skip from tlbufpos to PT and see where it is. Note that
13312 PT may be in invisible text. If so, we will end at the
13313 next visible position. */
13314 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13315 NULL, DEFAULT_FACE_ID);
13316 it.current_x = this_line_start_x;
13317 it.current_y = this_line_y;
13318 it.vpos = this_line_vpos;
13319
13320 /* The call to move_it_to stops in front of PT, but
13321 moves over before-strings. */
13322 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13323
13324 if (it.vpos == this_line_vpos
13325 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13326 row->enabled_p))
13327 {
13328 eassert (this_line_vpos == it.vpos);
13329 eassert (this_line_y == it.current_y);
13330 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13331 #ifdef GLYPH_DEBUG
13332 *w->desired_matrix->method = 0;
13333 debug_method_add (w, "optimization 3");
13334 #endif
13335 goto update;
13336 }
13337 else
13338 goto cancel;
13339 }
13340
13341 cancel:
13342 /* Text changed drastically or point moved off of line. */
13343 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13344 }
13345
13346 CHARPOS (this_line_start_pos) = 0;
13347 consider_all_windows_p |= buffer_shared_and_changed ();
13348 ++clear_face_cache_count;
13349 #ifdef HAVE_WINDOW_SYSTEM
13350 ++clear_image_cache_count;
13351 #endif
13352
13353 /* Build desired matrices, and update the display. If
13354 consider_all_windows_p is non-zero, do it for all windows on all
13355 frames. Otherwise do it for selected_window, only. */
13356
13357 if (consider_all_windows_p)
13358 {
13359 FOR_EACH_FRAME (tail, frame)
13360 XFRAME (frame)->updated_p = 0;
13361
13362 FOR_EACH_FRAME (tail, frame)
13363 {
13364 struct frame *f = XFRAME (frame);
13365
13366 /* We don't have to do anything for unselected terminal
13367 frames. */
13368 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13369 && !EQ (FRAME_TTY (f)->top_frame, frame))
13370 continue;
13371
13372 retry_frame:
13373
13374 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13375 {
13376 /* Mark all the scroll bars to be removed; we'll redeem
13377 the ones we want when we redisplay their windows. */
13378 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13379 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13380
13381 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13382 redisplay_windows (FRAME_ROOT_WINDOW (f));
13383
13384 /* The X error handler may have deleted that frame. */
13385 if (!FRAME_LIVE_P (f))
13386 continue;
13387
13388 /* Any scroll bars which redisplay_windows should have
13389 nuked should now go away. */
13390 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13391 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13392
13393 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13394 {
13395 /* If fonts changed on visible frame, display again. */
13396 if (f->fonts_changed)
13397 {
13398 adjust_frame_glyphs (f);
13399 f->fonts_changed = 0;
13400 goto retry_frame;
13401 }
13402
13403 /* See if we have to hscroll. */
13404 if (!f->already_hscrolled_p)
13405 {
13406 f->already_hscrolled_p = 1;
13407 if (hscroll_windows (f->root_window))
13408 goto retry_frame;
13409 }
13410
13411 /* Prevent various kinds of signals during display
13412 update. stdio is not robust about handling
13413 signals, which can cause an apparent I/O
13414 error. */
13415 if (interrupt_input)
13416 unrequest_sigio ();
13417 STOP_POLLING;
13418
13419 /* Update the display. */
13420 set_window_update_flags (XWINDOW (f->root_window), 1);
13421 pending |= update_frame (f, 0, 0);
13422 f->updated_p = 1;
13423 }
13424 }
13425 }
13426
13427 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13428
13429 if (!pending)
13430 {
13431 /* Do the mark_window_display_accurate after all windows have
13432 been redisplayed because this call resets flags in buffers
13433 which are needed for proper redisplay. */
13434 FOR_EACH_FRAME (tail, frame)
13435 {
13436 struct frame *f = XFRAME (frame);
13437 if (f->updated_p)
13438 {
13439 mark_window_display_accurate (f->root_window, 1);
13440 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13441 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13442 }
13443 }
13444 }
13445 }
13446 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13447 {
13448 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13449 struct frame *mini_frame;
13450
13451 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13452 /* Use list_of_error, not Qerror, so that
13453 we catch only errors and don't run the debugger. */
13454 internal_condition_case_1 (redisplay_window_1, selected_window,
13455 list_of_error,
13456 redisplay_window_error);
13457 if (update_miniwindow_p)
13458 internal_condition_case_1 (redisplay_window_1, mini_window,
13459 list_of_error,
13460 redisplay_window_error);
13461
13462 /* Compare desired and current matrices, perform output. */
13463
13464 update:
13465 /* If fonts changed, display again. */
13466 if (sf->fonts_changed)
13467 goto retry;
13468
13469 /* Prevent various kinds of signals during display update.
13470 stdio is not robust about handling signals,
13471 which can cause an apparent I/O error. */
13472 if (interrupt_input)
13473 unrequest_sigio ();
13474 STOP_POLLING;
13475
13476 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13477 {
13478 if (hscroll_windows (selected_window))
13479 goto retry;
13480
13481 XWINDOW (selected_window)->must_be_updated_p = 1;
13482 pending = update_frame (sf, 0, 0);
13483 }
13484
13485 /* We may have called echo_area_display at the top of this
13486 function. If the echo area is on another frame, that may
13487 have put text on a frame other than the selected one, so the
13488 above call to update_frame would not have caught it. Catch
13489 it here. */
13490 mini_window = FRAME_MINIBUF_WINDOW (sf);
13491 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13492
13493 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13494 {
13495 XWINDOW (mini_window)->must_be_updated_p = 1;
13496 pending |= update_frame (mini_frame, 0, 0);
13497 if (!pending && hscroll_windows (mini_window))
13498 goto retry;
13499 }
13500 }
13501
13502 /* If display was paused because of pending input, make sure we do a
13503 thorough update the next time. */
13504 if (pending)
13505 {
13506 /* Prevent the optimization at the beginning of
13507 redisplay_internal that tries a single-line update of the
13508 line containing the cursor in the selected window. */
13509 CHARPOS (this_line_start_pos) = 0;
13510
13511 /* Let the overlay arrow be updated the next time. */
13512 update_overlay_arrows (0);
13513
13514 /* If we pause after scrolling, some rows in the current
13515 matrices of some windows are not valid. */
13516 if (!WINDOW_FULL_WIDTH_P (w)
13517 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13518 update_mode_lines = 1;
13519 }
13520 else
13521 {
13522 if (!consider_all_windows_p)
13523 {
13524 /* This has already been done above if
13525 consider_all_windows_p is set. */
13526 mark_window_display_accurate_1 (w, 1);
13527
13528 /* Say overlay arrows are up to date. */
13529 update_overlay_arrows (1);
13530
13531 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13532 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13533 }
13534
13535 update_mode_lines = 0;
13536 windows_or_buffers_changed = 0;
13537 cursor_type_changed = 0;
13538 }
13539
13540 /* Start SIGIO interrupts coming again. Having them off during the
13541 code above makes it less likely one will discard output, but not
13542 impossible, since there might be stuff in the system buffer here.
13543 But it is much hairier to try to do anything about that. */
13544 if (interrupt_input)
13545 request_sigio ();
13546 RESUME_POLLING;
13547
13548 /* If a frame has become visible which was not before, redisplay
13549 again, so that we display it. Expose events for such a frame
13550 (which it gets when becoming visible) don't call the parts of
13551 redisplay constructing glyphs, so simply exposing a frame won't
13552 display anything in this case. So, we have to display these
13553 frames here explicitly. */
13554 if (!pending)
13555 {
13556 int new_count = 0;
13557
13558 FOR_EACH_FRAME (tail, frame)
13559 {
13560 int this_is_visible = 0;
13561
13562 if (XFRAME (frame)->visible)
13563 this_is_visible = 1;
13564
13565 if (this_is_visible)
13566 new_count++;
13567 }
13568
13569 if (new_count != number_of_visible_frames)
13570 windows_or_buffers_changed++;
13571 }
13572
13573 /* Change frame size now if a change is pending. */
13574 do_pending_window_change (1);
13575
13576 /* If we just did a pending size change, or have additional
13577 visible frames, or selected_window changed, redisplay again. */
13578 if ((windows_or_buffers_changed && !pending)
13579 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13580 goto retry;
13581
13582 /* Clear the face and image caches.
13583
13584 We used to do this only if consider_all_windows_p. But the cache
13585 needs to be cleared if a timer creates images in the current
13586 buffer (e.g. the test case in Bug#6230). */
13587
13588 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13589 {
13590 clear_face_cache (0);
13591 clear_face_cache_count = 0;
13592 }
13593
13594 #ifdef HAVE_WINDOW_SYSTEM
13595 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13596 {
13597 clear_image_caches (Qnil);
13598 clear_image_cache_count = 0;
13599 }
13600 #endif /* HAVE_WINDOW_SYSTEM */
13601
13602 end_of_redisplay:
13603 unbind_to (count, Qnil);
13604 RESUME_POLLING;
13605 }
13606
13607
13608 /* Redisplay, but leave alone any recent echo area message unless
13609 another message has been requested in its place.
13610
13611 This is useful in situations where you need to redisplay but no
13612 user action has occurred, making it inappropriate for the message
13613 area to be cleared. See tracking_off and
13614 wait_reading_process_output for examples of these situations.
13615
13616 FROM_WHERE is an integer saying from where this function was
13617 called. This is useful for debugging. */
13618
13619 void
13620 redisplay_preserve_echo_area (int from_where)
13621 {
13622 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13623
13624 if (!NILP (echo_area_buffer[1]))
13625 {
13626 /* We have a previously displayed message, but no current
13627 message. Redisplay the previous message. */
13628 display_last_displayed_message_p = 1;
13629 redisplay_internal ();
13630 display_last_displayed_message_p = 0;
13631 }
13632 else
13633 redisplay_internal ();
13634
13635 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13636 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13637 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13638 }
13639
13640
13641 /* Function registered with record_unwind_protect in redisplay_internal. */
13642
13643 static void
13644 unwind_redisplay (void)
13645 {
13646 redisplaying_p = 0;
13647 }
13648
13649
13650 /* Mark the display of leaf window W as accurate or inaccurate.
13651 If ACCURATE_P is non-zero mark display of W as accurate. If
13652 ACCURATE_P is zero, arrange for W to be redisplayed the next
13653 time redisplay_internal is called. */
13654
13655 static void
13656 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13657 {
13658 struct buffer *b = XBUFFER (w->contents);
13659
13660 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13661 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13662 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13663
13664 if (accurate_p)
13665 {
13666 b->clip_changed = 0;
13667 b->prevent_redisplay_optimizations_p = 0;
13668
13669 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13670 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13671 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13672 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13673
13674 w->current_matrix->buffer = b;
13675 w->current_matrix->begv = BUF_BEGV (b);
13676 w->current_matrix->zv = BUF_ZV (b);
13677
13678 w->last_cursor_vpos = w->cursor.vpos;
13679 w->last_cursor_off_p = w->cursor_off_p;
13680
13681 if (w == XWINDOW (selected_window))
13682 w->last_point = BUF_PT (b);
13683 else
13684 w->last_point = marker_position (w->pointm);
13685
13686 w->window_end_valid = 1;
13687 w->update_mode_line = 0;
13688 }
13689 }
13690
13691
13692 /* Mark the display of windows in the window tree rooted at WINDOW as
13693 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13694 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13695 be redisplayed the next time redisplay_internal is called. */
13696
13697 void
13698 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13699 {
13700 struct window *w;
13701
13702 for (; !NILP (window); window = w->next)
13703 {
13704 w = XWINDOW (window);
13705 if (WINDOWP (w->contents))
13706 mark_window_display_accurate (w->contents, accurate_p);
13707 else
13708 mark_window_display_accurate_1 (w, accurate_p);
13709 }
13710
13711 if (accurate_p)
13712 update_overlay_arrows (1);
13713 else
13714 /* Force a thorough redisplay the next time by setting
13715 last_arrow_position and last_arrow_string to t, which is
13716 unequal to any useful value of Voverlay_arrow_... */
13717 update_overlay_arrows (-1);
13718 }
13719
13720
13721 /* Return value in display table DP (Lisp_Char_Table *) for character
13722 C. Since a display table doesn't have any parent, we don't have to
13723 follow parent. Do not call this function directly but use the
13724 macro DISP_CHAR_VECTOR. */
13725
13726 Lisp_Object
13727 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13728 {
13729 Lisp_Object val;
13730
13731 if (ASCII_CHAR_P (c))
13732 {
13733 val = dp->ascii;
13734 if (SUB_CHAR_TABLE_P (val))
13735 val = XSUB_CHAR_TABLE (val)->contents[c];
13736 }
13737 else
13738 {
13739 Lisp_Object table;
13740
13741 XSETCHAR_TABLE (table, dp);
13742 val = char_table_ref (table, c);
13743 }
13744 if (NILP (val))
13745 val = dp->defalt;
13746 return val;
13747 }
13748
13749
13750 \f
13751 /***********************************************************************
13752 Window Redisplay
13753 ***********************************************************************/
13754
13755 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13756
13757 static void
13758 redisplay_windows (Lisp_Object window)
13759 {
13760 while (!NILP (window))
13761 {
13762 struct window *w = XWINDOW (window);
13763
13764 if (WINDOWP (w->contents))
13765 redisplay_windows (w->contents);
13766 else if (BUFFERP (w->contents))
13767 {
13768 displayed_buffer = XBUFFER (w->contents);
13769 /* Use list_of_error, not Qerror, so that
13770 we catch only errors and don't run the debugger. */
13771 internal_condition_case_1 (redisplay_window_0, window,
13772 list_of_error,
13773 redisplay_window_error);
13774 }
13775
13776 window = w->next;
13777 }
13778 }
13779
13780 static Lisp_Object
13781 redisplay_window_error (Lisp_Object ignore)
13782 {
13783 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13784 return Qnil;
13785 }
13786
13787 static Lisp_Object
13788 redisplay_window_0 (Lisp_Object window)
13789 {
13790 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13791 redisplay_window (window, 0);
13792 return Qnil;
13793 }
13794
13795 static Lisp_Object
13796 redisplay_window_1 (Lisp_Object window)
13797 {
13798 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13799 redisplay_window (window, 1);
13800 return Qnil;
13801 }
13802 \f
13803
13804 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13805 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13806 which positions recorded in ROW differ from current buffer
13807 positions.
13808
13809 Return 0 if cursor is not on this row, 1 otherwise. */
13810
13811 static int
13812 set_cursor_from_row (struct window *w, struct glyph_row *row,
13813 struct glyph_matrix *matrix,
13814 ptrdiff_t delta, ptrdiff_t delta_bytes,
13815 int dy, int dvpos)
13816 {
13817 struct glyph *glyph = row->glyphs[TEXT_AREA];
13818 struct glyph *end = glyph + row->used[TEXT_AREA];
13819 struct glyph *cursor = NULL;
13820 /* The last known character position in row. */
13821 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13822 int x = row->x;
13823 ptrdiff_t pt_old = PT - delta;
13824 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13825 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13826 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13827 /* A glyph beyond the edge of TEXT_AREA which we should never
13828 touch. */
13829 struct glyph *glyphs_end = end;
13830 /* Non-zero means we've found a match for cursor position, but that
13831 glyph has the avoid_cursor_p flag set. */
13832 int match_with_avoid_cursor = 0;
13833 /* Non-zero means we've seen at least one glyph that came from a
13834 display string. */
13835 int string_seen = 0;
13836 /* Largest and smallest buffer positions seen so far during scan of
13837 glyph row. */
13838 ptrdiff_t bpos_max = pos_before;
13839 ptrdiff_t bpos_min = pos_after;
13840 /* Last buffer position covered by an overlay string with an integer
13841 `cursor' property. */
13842 ptrdiff_t bpos_covered = 0;
13843 /* Non-zero means the display string on which to display the cursor
13844 comes from a text property, not from an overlay. */
13845 int string_from_text_prop = 0;
13846
13847 /* Don't even try doing anything if called for a mode-line or
13848 header-line row, since the rest of the code isn't prepared to
13849 deal with such calamities. */
13850 eassert (!row->mode_line_p);
13851 if (row->mode_line_p)
13852 return 0;
13853
13854 /* Skip over glyphs not having an object at the start and the end of
13855 the row. These are special glyphs like truncation marks on
13856 terminal frames. */
13857 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13858 {
13859 if (!row->reversed_p)
13860 {
13861 while (glyph < end
13862 && INTEGERP (glyph->object)
13863 && glyph->charpos < 0)
13864 {
13865 x += glyph->pixel_width;
13866 ++glyph;
13867 }
13868 while (end > glyph
13869 && INTEGERP ((end - 1)->object)
13870 /* CHARPOS is zero for blanks and stretch glyphs
13871 inserted by extend_face_to_end_of_line. */
13872 && (end - 1)->charpos <= 0)
13873 --end;
13874 glyph_before = glyph - 1;
13875 glyph_after = end;
13876 }
13877 else
13878 {
13879 struct glyph *g;
13880
13881 /* If the glyph row is reversed, we need to process it from back
13882 to front, so swap the edge pointers. */
13883 glyphs_end = end = glyph - 1;
13884 glyph += row->used[TEXT_AREA] - 1;
13885
13886 while (glyph > end + 1
13887 && INTEGERP (glyph->object)
13888 && glyph->charpos < 0)
13889 {
13890 --glyph;
13891 x -= glyph->pixel_width;
13892 }
13893 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13894 --glyph;
13895 /* By default, in reversed rows we put the cursor on the
13896 rightmost (first in the reading order) glyph. */
13897 for (g = end + 1; g < glyph; g++)
13898 x += g->pixel_width;
13899 while (end < glyph
13900 && INTEGERP ((end + 1)->object)
13901 && (end + 1)->charpos <= 0)
13902 ++end;
13903 glyph_before = glyph + 1;
13904 glyph_after = end;
13905 }
13906 }
13907 else if (row->reversed_p)
13908 {
13909 /* In R2L rows that don't display text, put the cursor on the
13910 rightmost glyph. Case in point: an empty last line that is
13911 part of an R2L paragraph. */
13912 cursor = end - 1;
13913 /* Avoid placing the cursor on the last glyph of the row, where
13914 on terminal frames we hold the vertical border between
13915 adjacent windows. */
13916 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13917 && !WINDOW_RIGHTMOST_P (w)
13918 && cursor == row->glyphs[LAST_AREA] - 1)
13919 cursor--;
13920 x = -1; /* will be computed below, at label compute_x */
13921 }
13922
13923 /* Step 1: Try to find the glyph whose character position
13924 corresponds to point. If that's not possible, find 2 glyphs
13925 whose character positions are the closest to point, one before
13926 point, the other after it. */
13927 if (!row->reversed_p)
13928 while (/* not marched to end of glyph row */
13929 glyph < end
13930 /* glyph was not inserted by redisplay for internal purposes */
13931 && !INTEGERP (glyph->object))
13932 {
13933 if (BUFFERP (glyph->object))
13934 {
13935 ptrdiff_t dpos = glyph->charpos - pt_old;
13936
13937 if (glyph->charpos > bpos_max)
13938 bpos_max = glyph->charpos;
13939 if (glyph->charpos < bpos_min)
13940 bpos_min = glyph->charpos;
13941 if (!glyph->avoid_cursor_p)
13942 {
13943 /* If we hit point, we've found the glyph on which to
13944 display the cursor. */
13945 if (dpos == 0)
13946 {
13947 match_with_avoid_cursor = 0;
13948 break;
13949 }
13950 /* See if we've found a better approximation to
13951 POS_BEFORE or to POS_AFTER. */
13952 if (0 > dpos && dpos > pos_before - pt_old)
13953 {
13954 pos_before = glyph->charpos;
13955 glyph_before = glyph;
13956 }
13957 else if (0 < dpos && dpos < pos_after - pt_old)
13958 {
13959 pos_after = glyph->charpos;
13960 glyph_after = glyph;
13961 }
13962 }
13963 else if (dpos == 0)
13964 match_with_avoid_cursor = 1;
13965 }
13966 else if (STRINGP (glyph->object))
13967 {
13968 Lisp_Object chprop;
13969 ptrdiff_t glyph_pos = glyph->charpos;
13970
13971 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13972 glyph->object);
13973 if (!NILP (chprop))
13974 {
13975 /* If the string came from a `display' text property,
13976 look up the buffer position of that property and
13977 use that position to update bpos_max, as if we
13978 actually saw such a position in one of the row's
13979 glyphs. This helps with supporting integer values
13980 of `cursor' property on the display string in
13981 situations where most or all of the row's buffer
13982 text is completely covered by display properties,
13983 so that no glyph with valid buffer positions is
13984 ever seen in the row. */
13985 ptrdiff_t prop_pos =
13986 string_buffer_position_lim (glyph->object, pos_before,
13987 pos_after, 0);
13988
13989 if (prop_pos >= pos_before)
13990 bpos_max = prop_pos - 1;
13991 }
13992 if (INTEGERP (chprop))
13993 {
13994 bpos_covered = bpos_max + XINT (chprop);
13995 /* If the `cursor' property covers buffer positions up
13996 to and including point, we should display cursor on
13997 this glyph. Note that, if a `cursor' property on one
13998 of the string's characters has an integer value, we
13999 will break out of the loop below _before_ we get to
14000 the position match above. IOW, integer values of
14001 the `cursor' property override the "exact match for
14002 point" strategy of positioning the cursor. */
14003 /* Implementation note: bpos_max == pt_old when, e.g.,
14004 we are in an empty line, where bpos_max is set to
14005 MATRIX_ROW_START_CHARPOS, see above. */
14006 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14007 {
14008 cursor = glyph;
14009 break;
14010 }
14011 }
14012
14013 string_seen = 1;
14014 }
14015 x += glyph->pixel_width;
14016 ++glyph;
14017 }
14018 else if (glyph > end) /* row is reversed */
14019 while (!INTEGERP (glyph->object))
14020 {
14021 if (BUFFERP (glyph->object))
14022 {
14023 ptrdiff_t dpos = glyph->charpos - pt_old;
14024
14025 if (glyph->charpos > bpos_max)
14026 bpos_max = glyph->charpos;
14027 if (glyph->charpos < bpos_min)
14028 bpos_min = glyph->charpos;
14029 if (!glyph->avoid_cursor_p)
14030 {
14031 if (dpos == 0)
14032 {
14033 match_with_avoid_cursor = 0;
14034 break;
14035 }
14036 if (0 > dpos && dpos > pos_before - pt_old)
14037 {
14038 pos_before = glyph->charpos;
14039 glyph_before = glyph;
14040 }
14041 else if (0 < dpos && dpos < pos_after - pt_old)
14042 {
14043 pos_after = glyph->charpos;
14044 glyph_after = glyph;
14045 }
14046 }
14047 else if (dpos == 0)
14048 match_with_avoid_cursor = 1;
14049 }
14050 else if (STRINGP (glyph->object))
14051 {
14052 Lisp_Object chprop;
14053 ptrdiff_t glyph_pos = glyph->charpos;
14054
14055 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14056 glyph->object);
14057 if (!NILP (chprop))
14058 {
14059 ptrdiff_t prop_pos =
14060 string_buffer_position_lim (glyph->object, pos_before,
14061 pos_after, 0);
14062
14063 if (prop_pos >= pos_before)
14064 bpos_max = prop_pos - 1;
14065 }
14066 if (INTEGERP (chprop))
14067 {
14068 bpos_covered = bpos_max + XINT (chprop);
14069 /* If the `cursor' property covers buffer positions up
14070 to and including point, we should display cursor on
14071 this glyph. */
14072 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14073 {
14074 cursor = glyph;
14075 break;
14076 }
14077 }
14078 string_seen = 1;
14079 }
14080 --glyph;
14081 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14082 {
14083 x--; /* can't use any pixel_width */
14084 break;
14085 }
14086 x -= glyph->pixel_width;
14087 }
14088
14089 /* Step 2: If we didn't find an exact match for point, we need to
14090 look for a proper place to put the cursor among glyphs between
14091 GLYPH_BEFORE and GLYPH_AFTER. */
14092 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14093 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14094 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14095 {
14096 /* An empty line has a single glyph whose OBJECT is zero and
14097 whose CHARPOS is the position of a newline on that line.
14098 Note that on a TTY, there are more glyphs after that, which
14099 were produced by extend_face_to_end_of_line, but their
14100 CHARPOS is zero or negative. */
14101 int empty_line_p =
14102 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14103 && INTEGERP (glyph->object) && glyph->charpos > 0
14104 /* On a TTY, continued and truncated rows also have a glyph at
14105 their end whose OBJECT is zero and whose CHARPOS is
14106 positive (the continuation and truncation glyphs), but such
14107 rows are obviously not "empty". */
14108 && !(row->continued_p || row->truncated_on_right_p);
14109
14110 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14111 {
14112 ptrdiff_t ellipsis_pos;
14113
14114 /* Scan back over the ellipsis glyphs. */
14115 if (!row->reversed_p)
14116 {
14117 ellipsis_pos = (glyph - 1)->charpos;
14118 while (glyph > row->glyphs[TEXT_AREA]
14119 && (glyph - 1)->charpos == ellipsis_pos)
14120 glyph--, x -= glyph->pixel_width;
14121 /* That loop always goes one position too far, including
14122 the glyph before the ellipsis. So scan forward over
14123 that one. */
14124 x += glyph->pixel_width;
14125 glyph++;
14126 }
14127 else /* row is reversed */
14128 {
14129 ellipsis_pos = (glyph + 1)->charpos;
14130 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14131 && (glyph + 1)->charpos == ellipsis_pos)
14132 glyph++, x += glyph->pixel_width;
14133 x -= glyph->pixel_width;
14134 glyph--;
14135 }
14136 }
14137 else if (match_with_avoid_cursor)
14138 {
14139 cursor = glyph_after;
14140 x = -1;
14141 }
14142 else if (string_seen)
14143 {
14144 int incr = row->reversed_p ? -1 : +1;
14145
14146 /* Need to find the glyph that came out of a string which is
14147 present at point. That glyph is somewhere between
14148 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14149 positioned between POS_BEFORE and POS_AFTER in the
14150 buffer. */
14151 struct glyph *start, *stop;
14152 ptrdiff_t pos = pos_before;
14153
14154 x = -1;
14155
14156 /* If the row ends in a newline from a display string,
14157 reordering could have moved the glyphs belonging to the
14158 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14159 in this case we extend the search to the last glyph in
14160 the row that was not inserted by redisplay. */
14161 if (row->ends_in_newline_from_string_p)
14162 {
14163 glyph_after = end;
14164 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14165 }
14166
14167 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14168 correspond to POS_BEFORE and POS_AFTER, respectively. We
14169 need START and STOP in the order that corresponds to the
14170 row's direction as given by its reversed_p flag. If the
14171 directionality of characters between POS_BEFORE and
14172 POS_AFTER is the opposite of the row's base direction,
14173 these characters will have been reordered for display,
14174 and we need to reverse START and STOP. */
14175 if (!row->reversed_p)
14176 {
14177 start = min (glyph_before, glyph_after);
14178 stop = max (glyph_before, glyph_after);
14179 }
14180 else
14181 {
14182 start = max (glyph_before, glyph_after);
14183 stop = min (glyph_before, glyph_after);
14184 }
14185 for (glyph = start + incr;
14186 row->reversed_p ? glyph > stop : glyph < stop; )
14187 {
14188
14189 /* Any glyphs that come from the buffer are here because
14190 of bidi reordering. Skip them, and only pay
14191 attention to glyphs that came from some string. */
14192 if (STRINGP (glyph->object))
14193 {
14194 Lisp_Object str;
14195 ptrdiff_t tem;
14196 /* If the display property covers the newline, we
14197 need to search for it one position farther. */
14198 ptrdiff_t lim = pos_after
14199 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14200
14201 string_from_text_prop = 0;
14202 str = glyph->object;
14203 tem = string_buffer_position_lim (str, pos, lim, 0);
14204 if (tem == 0 /* from overlay */
14205 || pos <= tem)
14206 {
14207 /* If the string from which this glyph came is
14208 found in the buffer at point, or at position
14209 that is closer to point than pos_after, then
14210 we've found the glyph we've been looking for.
14211 If it comes from an overlay (tem == 0), and
14212 it has the `cursor' property on one of its
14213 glyphs, record that glyph as a candidate for
14214 displaying the cursor. (As in the
14215 unidirectional version, we will display the
14216 cursor on the last candidate we find.) */
14217 if (tem == 0
14218 || tem == pt_old
14219 || (tem - pt_old > 0 && tem < pos_after))
14220 {
14221 /* The glyphs from this string could have
14222 been reordered. Find the one with the
14223 smallest string position. Or there could
14224 be a character in the string with the
14225 `cursor' property, which means display
14226 cursor on that character's glyph. */
14227 ptrdiff_t strpos = glyph->charpos;
14228
14229 if (tem)
14230 {
14231 cursor = glyph;
14232 string_from_text_prop = 1;
14233 }
14234 for ( ;
14235 (row->reversed_p ? glyph > stop : glyph < stop)
14236 && EQ (glyph->object, str);
14237 glyph += incr)
14238 {
14239 Lisp_Object cprop;
14240 ptrdiff_t gpos = glyph->charpos;
14241
14242 cprop = Fget_char_property (make_number (gpos),
14243 Qcursor,
14244 glyph->object);
14245 if (!NILP (cprop))
14246 {
14247 cursor = glyph;
14248 break;
14249 }
14250 if (tem && glyph->charpos < strpos)
14251 {
14252 strpos = glyph->charpos;
14253 cursor = glyph;
14254 }
14255 }
14256
14257 if (tem == pt_old
14258 || (tem - pt_old > 0 && tem < pos_after))
14259 goto compute_x;
14260 }
14261 if (tem)
14262 pos = tem + 1; /* don't find previous instances */
14263 }
14264 /* This string is not what we want; skip all of the
14265 glyphs that came from it. */
14266 while ((row->reversed_p ? glyph > stop : glyph < stop)
14267 && EQ (glyph->object, str))
14268 glyph += incr;
14269 }
14270 else
14271 glyph += incr;
14272 }
14273
14274 /* If we reached the end of the line, and END was from a string,
14275 the cursor is not on this line. */
14276 if (cursor == NULL
14277 && (row->reversed_p ? glyph <= end : glyph >= end)
14278 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14279 && STRINGP (end->object)
14280 && row->continued_p)
14281 return 0;
14282 }
14283 /* A truncated row may not include PT among its character positions.
14284 Setting the cursor inside the scroll margin will trigger
14285 recalculation of hscroll in hscroll_window_tree. But if a
14286 display string covers point, defer to the string-handling
14287 code below to figure this out. */
14288 else if (row->truncated_on_left_p && pt_old < bpos_min)
14289 {
14290 cursor = glyph_before;
14291 x = -1;
14292 }
14293 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14294 /* Zero-width characters produce no glyphs. */
14295 || (!empty_line_p
14296 && (row->reversed_p
14297 ? glyph_after > glyphs_end
14298 : glyph_after < glyphs_end)))
14299 {
14300 cursor = glyph_after;
14301 x = -1;
14302 }
14303 }
14304
14305 compute_x:
14306 if (cursor != NULL)
14307 glyph = cursor;
14308 else if (glyph == glyphs_end
14309 && pos_before == pos_after
14310 && STRINGP ((row->reversed_p
14311 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14312 : row->glyphs[TEXT_AREA])->object))
14313 {
14314 /* If all the glyphs of this row came from strings, put the
14315 cursor on the first glyph of the row. This avoids having the
14316 cursor outside of the text area in this very rare and hard
14317 use case. */
14318 glyph =
14319 row->reversed_p
14320 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14321 : row->glyphs[TEXT_AREA];
14322 }
14323 if (x < 0)
14324 {
14325 struct glyph *g;
14326
14327 /* Need to compute x that corresponds to GLYPH. */
14328 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14329 {
14330 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14331 emacs_abort ();
14332 x += g->pixel_width;
14333 }
14334 }
14335
14336 /* ROW could be part of a continued line, which, under bidi
14337 reordering, might have other rows whose start and end charpos
14338 occlude point. Only set w->cursor if we found a better
14339 approximation to the cursor position than we have from previously
14340 examined candidate rows belonging to the same continued line. */
14341 if (/* we already have a candidate row */
14342 w->cursor.vpos >= 0
14343 /* that candidate is not the row we are processing */
14344 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14345 /* Make sure cursor.vpos specifies a row whose start and end
14346 charpos occlude point, and it is valid candidate for being a
14347 cursor-row. This is because some callers of this function
14348 leave cursor.vpos at the row where the cursor was displayed
14349 during the last redisplay cycle. */
14350 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14351 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14352 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14353 {
14354 struct glyph *g1 =
14355 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14356
14357 /* Don't consider glyphs that are outside TEXT_AREA. */
14358 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14359 return 0;
14360 /* Keep the candidate whose buffer position is the closest to
14361 point or has the `cursor' property. */
14362 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14363 w->cursor.hpos >= 0
14364 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14365 && ((BUFFERP (g1->object)
14366 && (g1->charpos == pt_old /* an exact match always wins */
14367 || (BUFFERP (glyph->object)
14368 && eabs (g1->charpos - pt_old)
14369 < eabs (glyph->charpos - pt_old))))
14370 /* previous candidate is a glyph from a string that has
14371 a non-nil `cursor' property */
14372 || (STRINGP (g1->object)
14373 && (!NILP (Fget_char_property (make_number (g1->charpos),
14374 Qcursor, g1->object))
14375 /* previous candidate is from the same display
14376 string as this one, and the display string
14377 came from a text property */
14378 || (EQ (g1->object, glyph->object)
14379 && string_from_text_prop)
14380 /* this candidate is from newline and its
14381 position is not an exact match */
14382 || (INTEGERP (glyph->object)
14383 && glyph->charpos != pt_old)))))
14384 return 0;
14385 /* If this candidate gives an exact match, use that. */
14386 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14387 /* If this candidate is a glyph created for the
14388 terminating newline of a line, and point is on that
14389 newline, it wins because it's an exact match. */
14390 || (!row->continued_p
14391 && INTEGERP (glyph->object)
14392 && glyph->charpos == 0
14393 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14394 /* Otherwise, keep the candidate that comes from a row
14395 spanning less buffer positions. This may win when one or
14396 both candidate positions are on glyphs that came from
14397 display strings, for which we cannot compare buffer
14398 positions. */
14399 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14400 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14401 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14402 return 0;
14403 }
14404 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14405 w->cursor.x = x;
14406 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14407 w->cursor.y = row->y + dy;
14408
14409 if (w == XWINDOW (selected_window))
14410 {
14411 if (!row->continued_p
14412 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14413 && row->x == 0)
14414 {
14415 this_line_buffer = XBUFFER (w->contents);
14416
14417 CHARPOS (this_line_start_pos)
14418 = MATRIX_ROW_START_CHARPOS (row) + delta;
14419 BYTEPOS (this_line_start_pos)
14420 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14421
14422 CHARPOS (this_line_end_pos)
14423 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14424 BYTEPOS (this_line_end_pos)
14425 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14426
14427 this_line_y = w->cursor.y;
14428 this_line_pixel_height = row->height;
14429 this_line_vpos = w->cursor.vpos;
14430 this_line_start_x = row->x;
14431 }
14432 else
14433 CHARPOS (this_line_start_pos) = 0;
14434 }
14435
14436 return 1;
14437 }
14438
14439
14440 /* Run window scroll functions, if any, for WINDOW with new window
14441 start STARTP. Sets the window start of WINDOW to that position.
14442
14443 We assume that the window's buffer is really current. */
14444
14445 static struct text_pos
14446 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14447 {
14448 struct window *w = XWINDOW (window);
14449 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14450
14451 eassert (current_buffer == XBUFFER (w->contents));
14452
14453 if (!NILP (Vwindow_scroll_functions))
14454 {
14455 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14456 make_number (CHARPOS (startp)));
14457 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14458 /* In case the hook functions switch buffers. */
14459 set_buffer_internal (XBUFFER (w->contents));
14460 }
14461
14462 return startp;
14463 }
14464
14465
14466 /* Make sure the line containing the cursor is fully visible.
14467 A value of 1 means there is nothing to be done.
14468 (Either the line is fully visible, or it cannot be made so,
14469 or we cannot tell.)
14470
14471 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14472 is higher than window.
14473
14474 A value of 0 means the caller should do scrolling
14475 as if point had gone off the screen. */
14476
14477 static int
14478 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14479 {
14480 struct glyph_matrix *matrix;
14481 struct glyph_row *row;
14482 int window_height;
14483
14484 if (!make_cursor_line_fully_visible_p)
14485 return 1;
14486
14487 /* It's not always possible to find the cursor, e.g, when a window
14488 is full of overlay strings. Don't do anything in that case. */
14489 if (w->cursor.vpos < 0)
14490 return 1;
14491
14492 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14493 row = MATRIX_ROW (matrix, w->cursor.vpos);
14494
14495 /* If the cursor row is not partially visible, there's nothing to do. */
14496 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14497 return 1;
14498
14499 /* If the row the cursor is in is taller than the window's height,
14500 it's not clear what to do, so do nothing. */
14501 window_height = window_box_height (w);
14502 if (row->height >= window_height)
14503 {
14504 if (!force_p || MINI_WINDOW_P (w)
14505 || w->vscroll || w->cursor.vpos == 0)
14506 return 1;
14507 }
14508 return 0;
14509 }
14510
14511
14512 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14513 non-zero means only WINDOW is redisplayed in redisplay_internal.
14514 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14515 in redisplay_window to bring a partially visible line into view in
14516 the case that only the cursor has moved.
14517
14518 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14519 last screen line's vertical height extends past the end of the screen.
14520
14521 Value is
14522
14523 1 if scrolling succeeded
14524
14525 0 if scrolling didn't find point.
14526
14527 -1 if new fonts have been loaded so that we must interrupt
14528 redisplay, adjust glyph matrices, and try again. */
14529
14530 enum
14531 {
14532 SCROLLING_SUCCESS,
14533 SCROLLING_FAILED,
14534 SCROLLING_NEED_LARGER_MATRICES
14535 };
14536
14537 /* If scroll-conservatively is more than this, never recenter.
14538
14539 If you change this, don't forget to update the doc string of
14540 `scroll-conservatively' and the Emacs manual. */
14541 #define SCROLL_LIMIT 100
14542
14543 static int
14544 try_scrolling (Lisp_Object window, int just_this_one_p,
14545 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14546 int temp_scroll_step, int last_line_misfit)
14547 {
14548 struct window *w = XWINDOW (window);
14549 struct frame *f = XFRAME (w->frame);
14550 struct text_pos pos, startp;
14551 struct it it;
14552 int this_scroll_margin, scroll_max, rc, height;
14553 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14554 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14555 Lisp_Object aggressive;
14556 /* We will never try scrolling more than this number of lines. */
14557 int scroll_limit = SCROLL_LIMIT;
14558 int frame_line_height = default_line_pixel_height (w);
14559 int window_total_lines
14560 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14561
14562 #ifdef GLYPH_DEBUG
14563 debug_method_add (w, "try_scrolling");
14564 #endif
14565
14566 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14567
14568 /* Compute scroll margin height in pixels. We scroll when point is
14569 within this distance from the top or bottom of the window. */
14570 if (scroll_margin > 0)
14571 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14572 * frame_line_height;
14573 else
14574 this_scroll_margin = 0;
14575
14576 /* Force arg_scroll_conservatively to have a reasonable value, to
14577 avoid scrolling too far away with slow move_it_* functions. Note
14578 that the user can supply scroll-conservatively equal to
14579 `most-positive-fixnum', which can be larger than INT_MAX. */
14580 if (arg_scroll_conservatively > scroll_limit)
14581 {
14582 arg_scroll_conservatively = scroll_limit + 1;
14583 scroll_max = scroll_limit * frame_line_height;
14584 }
14585 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14586 /* Compute how much we should try to scroll maximally to bring
14587 point into view. */
14588 scroll_max = (max (scroll_step,
14589 max (arg_scroll_conservatively, temp_scroll_step))
14590 * frame_line_height);
14591 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14592 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14593 /* We're trying to scroll because of aggressive scrolling but no
14594 scroll_step is set. Choose an arbitrary one. */
14595 scroll_max = 10 * frame_line_height;
14596 else
14597 scroll_max = 0;
14598
14599 too_near_end:
14600
14601 /* Decide whether to scroll down. */
14602 if (PT > CHARPOS (startp))
14603 {
14604 int scroll_margin_y;
14605
14606 /* Compute the pixel ypos of the scroll margin, then move IT to
14607 either that ypos or PT, whichever comes first. */
14608 start_display (&it, w, startp);
14609 scroll_margin_y = it.last_visible_y - this_scroll_margin
14610 - frame_line_height * extra_scroll_margin_lines;
14611 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14612 (MOVE_TO_POS | MOVE_TO_Y));
14613
14614 if (PT > CHARPOS (it.current.pos))
14615 {
14616 int y0 = line_bottom_y (&it);
14617 /* Compute how many pixels below window bottom to stop searching
14618 for PT. This avoids costly search for PT that is far away if
14619 the user limited scrolling by a small number of lines, but
14620 always finds PT if scroll_conservatively is set to a large
14621 number, such as most-positive-fixnum. */
14622 int slack = max (scroll_max, 10 * frame_line_height);
14623 int y_to_move = it.last_visible_y + slack;
14624
14625 /* Compute the distance from the scroll margin to PT or to
14626 the scroll limit, whichever comes first. This should
14627 include the height of the cursor line, to make that line
14628 fully visible. */
14629 move_it_to (&it, PT, -1, y_to_move,
14630 -1, MOVE_TO_POS | MOVE_TO_Y);
14631 dy = line_bottom_y (&it) - y0;
14632
14633 if (dy > scroll_max)
14634 return SCROLLING_FAILED;
14635
14636 if (dy > 0)
14637 scroll_down_p = 1;
14638 }
14639 }
14640
14641 if (scroll_down_p)
14642 {
14643 /* Point is in or below the bottom scroll margin, so move the
14644 window start down. If scrolling conservatively, move it just
14645 enough down to make point visible. If scroll_step is set,
14646 move it down by scroll_step. */
14647 if (arg_scroll_conservatively)
14648 amount_to_scroll
14649 = min (max (dy, frame_line_height),
14650 frame_line_height * arg_scroll_conservatively);
14651 else if (scroll_step || temp_scroll_step)
14652 amount_to_scroll = scroll_max;
14653 else
14654 {
14655 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14656 height = WINDOW_BOX_TEXT_HEIGHT (w);
14657 if (NUMBERP (aggressive))
14658 {
14659 double float_amount = XFLOATINT (aggressive) * height;
14660 int aggressive_scroll = float_amount;
14661 if (aggressive_scroll == 0 && float_amount > 0)
14662 aggressive_scroll = 1;
14663 /* Don't let point enter the scroll margin near top of
14664 the window. This could happen if the value of
14665 scroll_up_aggressively is too large and there are
14666 non-zero margins, because scroll_up_aggressively
14667 means put point that fraction of window height
14668 _from_the_bottom_margin_. */
14669 if (aggressive_scroll + 2*this_scroll_margin > height)
14670 aggressive_scroll = height - 2*this_scroll_margin;
14671 amount_to_scroll = dy + aggressive_scroll;
14672 }
14673 }
14674
14675 if (amount_to_scroll <= 0)
14676 return SCROLLING_FAILED;
14677
14678 start_display (&it, w, startp);
14679 if (arg_scroll_conservatively <= scroll_limit)
14680 move_it_vertically (&it, amount_to_scroll);
14681 else
14682 {
14683 /* Extra precision for users who set scroll-conservatively
14684 to a large number: make sure the amount we scroll
14685 the window start is never less than amount_to_scroll,
14686 which was computed as distance from window bottom to
14687 point. This matters when lines at window top and lines
14688 below window bottom have different height. */
14689 struct it it1;
14690 void *it1data = NULL;
14691 /* We use a temporary it1 because line_bottom_y can modify
14692 its argument, if it moves one line down; see there. */
14693 int start_y;
14694
14695 SAVE_IT (it1, it, it1data);
14696 start_y = line_bottom_y (&it1);
14697 do {
14698 RESTORE_IT (&it, &it, it1data);
14699 move_it_by_lines (&it, 1);
14700 SAVE_IT (it1, it, it1data);
14701 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14702 }
14703
14704 /* If STARTP is unchanged, move it down another screen line. */
14705 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14706 move_it_by_lines (&it, 1);
14707 startp = it.current.pos;
14708 }
14709 else
14710 {
14711 struct text_pos scroll_margin_pos = startp;
14712 int y_offset = 0;
14713
14714 /* See if point is inside the scroll margin at the top of the
14715 window. */
14716 if (this_scroll_margin)
14717 {
14718 int y_start;
14719
14720 start_display (&it, w, startp);
14721 y_start = it.current_y;
14722 move_it_vertically (&it, this_scroll_margin);
14723 scroll_margin_pos = it.current.pos;
14724 /* If we didn't move enough before hitting ZV, request
14725 additional amount of scroll, to move point out of the
14726 scroll margin. */
14727 if (IT_CHARPOS (it) == ZV
14728 && it.current_y - y_start < this_scroll_margin)
14729 y_offset = this_scroll_margin - (it.current_y - y_start);
14730 }
14731
14732 if (PT < CHARPOS (scroll_margin_pos))
14733 {
14734 /* Point is in the scroll margin at the top of the window or
14735 above what is displayed in the window. */
14736 int y0, y_to_move;
14737
14738 /* Compute the vertical distance from PT to the scroll
14739 margin position. Move as far as scroll_max allows, or
14740 one screenful, or 10 screen lines, whichever is largest.
14741 Give up if distance is greater than scroll_max or if we
14742 didn't reach the scroll margin position. */
14743 SET_TEXT_POS (pos, PT, PT_BYTE);
14744 start_display (&it, w, pos);
14745 y0 = it.current_y;
14746 y_to_move = max (it.last_visible_y,
14747 max (scroll_max, 10 * frame_line_height));
14748 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14749 y_to_move, -1,
14750 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14751 dy = it.current_y - y0;
14752 if (dy > scroll_max
14753 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14754 return SCROLLING_FAILED;
14755
14756 /* Additional scroll for when ZV was too close to point. */
14757 dy += y_offset;
14758
14759 /* Compute new window start. */
14760 start_display (&it, w, startp);
14761
14762 if (arg_scroll_conservatively)
14763 amount_to_scroll = max (dy, frame_line_height *
14764 max (scroll_step, temp_scroll_step));
14765 else if (scroll_step || temp_scroll_step)
14766 amount_to_scroll = scroll_max;
14767 else
14768 {
14769 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14770 height = WINDOW_BOX_TEXT_HEIGHT (w);
14771 if (NUMBERP (aggressive))
14772 {
14773 double float_amount = XFLOATINT (aggressive) * height;
14774 int aggressive_scroll = float_amount;
14775 if (aggressive_scroll == 0 && float_amount > 0)
14776 aggressive_scroll = 1;
14777 /* Don't let point enter the scroll margin near
14778 bottom of the window, if the value of
14779 scroll_down_aggressively happens to be too
14780 large. */
14781 if (aggressive_scroll + 2*this_scroll_margin > height)
14782 aggressive_scroll = height - 2*this_scroll_margin;
14783 amount_to_scroll = dy + aggressive_scroll;
14784 }
14785 }
14786
14787 if (amount_to_scroll <= 0)
14788 return SCROLLING_FAILED;
14789
14790 move_it_vertically_backward (&it, amount_to_scroll);
14791 startp = it.current.pos;
14792 }
14793 }
14794
14795 /* Run window scroll functions. */
14796 startp = run_window_scroll_functions (window, startp);
14797
14798 /* Display the window. Give up if new fonts are loaded, or if point
14799 doesn't appear. */
14800 if (!try_window (window, startp, 0))
14801 rc = SCROLLING_NEED_LARGER_MATRICES;
14802 else if (w->cursor.vpos < 0)
14803 {
14804 clear_glyph_matrix (w->desired_matrix);
14805 rc = SCROLLING_FAILED;
14806 }
14807 else
14808 {
14809 /* Maybe forget recorded base line for line number display. */
14810 if (!just_this_one_p
14811 || current_buffer->clip_changed
14812 || BEG_UNCHANGED < CHARPOS (startp))
14813 w->base_line_number = 0;
14814
14815 /* If cursor ends up on a partially visible line,
14816 treat that as being off the bottom of the screen. */
14817 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14818 /* It's possible that the cursor is on the first line of the
14819 buffer, which is partially obscured due to a vscroll
14820 (Bug#7537). In that case, avoid looping forever . */
14821 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14822 {
14823 clear_glyph_matrix (w->desired_matrix);
14824 ++extra_scroll_margin_lines;
14825 goto too_near_end;
14826 }
14827 rc = SCROLLING_SUCCESS;
14828 }
14829
14830 return rc;
14831 }
14832
14833
14834 /* Compute a suitable window start for window W if display of W starts
14835 on a continuation line. Value is non-zero if a new window start
14836 was computed.
14837
14838 The new window start will be computed, based on W's width, starting
14839 from the start of the continued line. It is the start of the
14840 screen line with the minimum distance from the old start W->start. */
14841
14842 static int
14843 compute_window_start_on_continuation_line (struct window *w)
14844 {
14845 struct text_pos pos, start_pos;
14846 int window_start_changed_p = 0;
14847
14848 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14849
14850 /* If window start is on a continuation line... Window start may be
14851 < BEGV in case there's invisible text at the start of the
14852 buffer (M-x rmail, for example). */
14853 if (CHARPOS (start_pos) > BEGV
14854 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14855 {
14856 struct it it;
14857 struct glyph_row *row;
14858
14859 /* Handle the case that the window start is out of range. */
14860 if (CHARPOS (start_pos) < BEGV)
14861 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14862 else if (CHARPOS (start_pos) > ZV)
14863 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14864
14865 /* Find the start of the continued line. This should be fast
14866 because find_newline is fast (newline cache). */
14867 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14868 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14869 row, DEFAULT_FACE_ID);
14870 reseat_at_previous_visible_line_start (&it);
14871
14872 /* If the line start is "too far" away from the window start,
14873 say it takes too much time to compute a new window start. */
14874 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14875 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14876 {
14877 int min_distance, distance;
14878
14879 /* Move forward by display lines to find the new window
14880 start. If window width was enlarged, the new start can
14881 be expected to be > the old start. If window width was
14882 decreased, the new window start will be < the old start.
14883 So, we're looking for the display line start with the
14884 minimum distance from the old window start. */
14885 pos = it.current.pos;
14886 min_distance = INFINITY;
14887 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14888 distance < min_distance)
14889 {
14890 min_distance = distance;
14891 pos = it.current.pos;
14892 if (it.line_wrap == WORD_WRAP)
14893 {
14894 /* Under WORD_WRAP, move_it_by_lines is likely to
14895 overshoot and stop not at the first, but the
14896 second character from the left margin. So in
14897 that case, we need a more tight control on the X
14898 coordinate of the iterator than move_it_by_lines
14899 promises in its contract. The method is to first
14900 go to the last (rightmost) visible character of a
14901 line, then move to the leftmost character on the
14902 next line in a separate call. */
14903 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14904 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14905 move_it_to (&it, ZV, 0,
14906 it.current_y + it.max_ascent + it.max_descent, -1,
14907 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14908 }
14909 else
14910 move_it_by_lines (&it, 1);
14911 }
14912
14913 /* Set the window start there. */
14914 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14915 window_start_changed_p = 1;
14916 }
14917 }
14918
14919 return window_start_changed_p;
14920 }
14921
14922
14923 /* Try cursor movement in case text has not changed in window WINDOW,
14924 with window start STARTP. Value is
14925
14926 CURSOR_MOVEMENT_SUCCESS if successful
14927
14928 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14929
14930 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14931 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14932 we want to scroll as if scroll-step were set to 1. See the code.
14933
14934 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14935 which case we have to abort this redisplay, and adjust matrices
14936 first. */
14937
14938 enum
14939 {
14940 CURSOR_MOVEMENT_SUCCESS,
14941 CURSOR_MOVEMENT_CANNOT_BE_USED,
14942 CURSOR_MOVEMENT_MUST_SCROLL,
14943 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14944 };
14945
14946 static int
14947 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14948 {
14949 struct window *w = XWINDOW (window);
14950 struct frame *f = XFRAME (w->frame);
14951 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14952
14953 #ifdef GLYPH_DEBUG
14954 if (inhibit_try_cursor_movement)
14955 return rc;
14956 #endif
14957
14958 /* Previously, there was a check for Lisp integer in the
14959 if-statement below. Now, this field is converted to
14960 ptrdiff_t, thus zero means invalid position in a buffer. */
14961 eassert (w->last_point > 0);
14962 /* Likewise there was a check whether window_end_vpos is nil or larger
14963 than the window. Now window_end_vpos is int and so never nil, but
14964 let's leave eassert to check whether it fits in the window. */
14965 eassert (w->window_end_vpos < w->current_matrix->nrows);
14966
14967 /* Handle case where text has not changed, only point, and it has
14968 not moved off the frame. */
14969 if (/* Point may be in this window. */
14970 PT >= CHARPOS (startp)
14971 /* Selective display hasn't changed. */
14972 && !current_buffer->clip_changed
14973 /* Function force-mode-line-update is used to force a thorough
14974 redisplay. It sets either windows_or_buffers_changed or
14975 update_mode_lines. So don't take a shortcut here for these
14976 cases. */
14977 && !update_mode_lines
14978 && !windows_or_buffers_changed
14979 && !cursor_type_changed
14980 /* Can't use this case if highlighting a region. When a
14981 region exists, cursor movement has to do more than just
14982 set the cursor. */
14983 && markpos_of_region () < 0
14984 && !w->region_showing
14985 && NILP (Vshow_trailing_whitespace)
14986 /* This code is not used for mini-buffer for the sake of the case
14987 of redisplaying to replace an echo area message; since in
14988 that case the mini-buffer contents per se are usually
14989 unchanged. This code is of no real use in the mini-buffer
14990 since the handling of this_line_start_pos, etc., in redisplay
14991 handles the same cases. */
14992 && !EQ (window, minibuf_window)
14993 && (FRAME_WINDOW_P (f)
14994 || !overlay_arrow_in_current_buffer_p ()))
14995 {
14996 int this_scroll_margin, top_scroll_margin;
14997 struct glyph_row *row = NULL;
14998 int frame_line_height = default_line_pixel_height (w);
14999 int window_total_lines
15000 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15001
15002 #ifdef GLYPH_DEBUG
15003 debug_method_add (w, "cursor movement");
15004 #endif
15005
15006 /* Scroll if point within this distance from the top or bottom
15007 of the window. This is a pixel value. */
15008 if (scroll_margin > 0)
15009 {
15010 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15011 this_scroll_margin *= frame_line_height;
15012 }
15013 else
15014 this_scroll_margin = 0;
15015
15016 top_scroll_margin = this_scroll_margin;
15017 if (WINDOW_WANTS_HEADER_LINE_P (w))
15018 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15019
15020 /* Start with the row the cursor was displayed during the last
15021 not paused redisplay. Give up if that row is not valid. */
15022 if (w->last_cursor_vpos < 0
15023 || w->last_cursor_vpos >= w->current_matrix->nrows)
15024 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15025 else
15026 {
15027 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15028 if (row->mode_line_p)
15029 ++row;
15030 if (!row->enabled_p)
15031 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15032 }
15033
15034 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15035 {
15036 int scroll_p = 0, must_scroll = 0;
15037 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15038
15039 if (PT > w->last_point)
15040 {
15041 /* Point has moved forward. */
15042 while (MATRIX_ROW_END_CHARPOS (row) < PT
15043 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15044 {
15045 eassert (row->enabled_p);
15046 ++row;
15047 }
15048
15049 /* If the end position of a row equals the start
15050 position of the next row, and PT is at that position,
15051 we would rather display cursor in the next line. */
15052 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15053 && MATRIX_ROW_END_CHARPOS (row) == PT
15054 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15055 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15056 && !cursor_row_p (row))
15057 ++row;
15058
15059 /* If within the scroll margin, scroll. Note that
15060 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15061 the next line would be drawn, and that
15062 this_scroll_margin can be zero. */
15063 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15064 || PT > MATRIX_ROW_END_CHARPOS (row)
15065 /* Line is completely visible last line in window
15066 and PT is to be set in the next line. */
15067 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15068 && PT == MATRIX_ROW_END_CHARPOS (row)
15069 && !row->ends_at_zv_p
15070 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15071 scroll_p = 1;
15072 }
15073 else if (PT < w->last_point)
15074 {
15075 /* Cursor has to be moved backward. Note that PT >=
15076 CHARPOS (startp) because of the outer if-statement. */
15077 while (!row->mode_line_p
15078 && (MATRIX_ROW_START_CHARPOS (row) > PT
15079 || (MATRIX_ROW_START_CHARPOS (row) == PT
15080 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15081 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15082 row > w->current_matrix->rows
15083 && (row-1)->ends_in_newline_from_string_p))))
15084 && (row->y > top_scroll_margin
15085 || CHARPOS (startp) == BEGV))
15086 {
15087 eassert (row->enabled_p);
15088 --row;
15089 }
15090
15091 /* Consider the following case: Window starts at BEGV,
15092 there is invisible, intangible text at BEGV, so that
15093 display starts at some point START > BEGV. It can
15094 happen that we are called with PT somewhere between
15095 BEGV and START. Try to handle that case. */
15096 if (row < w->current_matrix->rows
15097 || row->mode_line_p)
15098 {
15099 row = w->current_matrix->rows;
15100 if (row->mode_line_p)
15101 ++row;
15102 }
15103
15104 /* Due to newlines in overlay strings, we may have to
15105 skip forward over overlay strings. */
15106 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15107 && MATRIX_ROW_END_CHARPOS (row) == PT
15108 && !cursor_row_p (row))
15109 ++row;
15110
15111 /* If within the scroll margin, scroll. */
15112 if (row->y < top_scroll_margin
15113 && CHARPOS (startp) != BEGV)
15114 scroll_p = 1;
15115 }
15116 else
15117 {
15118 /* Cursor did not move. So don't scroll even if cursor line
15119 is partially visible, as it was so before. */
15120 rc = CURSOR_MOVEMENT_SUCCESS;
15121 }
15122
15123 if (PT < MATRIX_ROW_START_CHARPOS (row)
15124 || PT > MATRIX_ROW_END_CHARPOS (row))
15125 {
15126 /* if PT is not in the glyph row, give up. */
15127 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15128 must_scroll = 1;
15129 }
15130 else if (rc != CURSOR_MOVEMENT_SUCCESS
15131 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15132 {
15133 struct glyph_row *row1;
15134
15135 /* If rows are bidi-reordered and point moved, back up
15136 until we find a row that does not belong to a
15137 continuation line. This is because we must consider
15138 all rows of a continued line as candidates for the
15139 new cursor positioning, since row start and end
15140 positions change non-linearly with vertical position
15141 in such rows. */
15142 /* FIXME: Revisit this when glyph ``spilling'' in
15143 continuation lines' rows is implemented for
15144 bidi-reordered rows. */
15145 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15146 MATRIX_ROW_CONTINUATION_LINE_P (row);
15147 --row)
15148 {
15149 /* If we hit the beginning of the displayed portion
15150 without finding the first row of a continued
15151 line, give up. */
15152 if (row <= row1)
15153 {
15154 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15155 break;
15156 }
15157 eassert (row->enabled_p);
15158 }
15159 }
15160 if (must_scroll)
15161 ;
15162 else if (rc != CURSOR_MOVEMENT_SUCCESS
15163 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15164 /* Make sure this isn't a header line by any chance, since
15165 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15166 && !row->mode_line_p
15167 && make_cursor_line_fully_visible_p)
15168 {
15169 if (PT == MATRIX_ROW_END_CHARPOS (row)
15170 && !row->ends_at_zv_p
15171 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15172 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15173 else if (row->height > window_box_height (w))
15174 {
15175 /* If we end up in a partially visible line, let's
15176 make it fully visible, except when it's taller
15177 than the window, in which case we can't do much
15178 about it. */
15179 *scroll_step = 1;
15180 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15181 }
15182 else
15183 {
15184 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15185 if (!cursor_row_fully_visible_p (w, 0, 1))
15186 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15187 else
15188 rc = CURSOR_MOVEMENT_SUCCESS;
15189 }
15190 }
15191 else if (scroll_p)
15192 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15193 else if (rc != CURSOR_MOVEMENT_SUCCESS
15194 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15195 {
15196 /* With bidi-reordered rows, there could be more than
15197 one candidate row whose start and end positions
15198 occlude point. We need to let set_cursor_from_row
15199 find the best candidate. */
15200 /* FIXME: Revisit this when glyph ``spilling'' in
15201 continuation lines' rows is implemented for
15202 bidi-reordered rows. */
15203 int rv = 0;
15204
15205 do
15206 {
15207 int at_zv_p = 0, exact_match_p = 0;
15208
15209 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15210 && PT <= MATRIX_ROW_END_CHARPOS (row)
15211 && cursor_row_p (row))
15212 rv |= set_cursor_from_row (w, row, w->current_matrix,
15213 0, 0, 0, 0);
15214 /* As soon as we've found the exact match for point,
15215 or the first suitable row whose ends_at_zv_p flag
15216 is set, we are done. */
15217 at_zv_p =
15218 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15219 if (rv && !at_zv_p
15220 && w->cursor.hpos >= 0
15221 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15222 w->cursor.vpos))
15223 {
15224 struct glyph_row *candidate =
15225 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15226 struct glyph *g =
15227 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15228 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15229
15230 exact_match_p =
15231 (BUFFERP (g->object) && g->charpos == PT)
15232 || (INTEGERP (g->object)
15233 && (g->charpos == PT
15234 || (g->charpos == 0 && endpos - 1 == PT)));
15235 }
15236 if (rv && (at_zv_p || exact_match_p))
15237 {
15238 rc = CURSOR_MOVEMENT_SUCCESS;
15239 break;
15240 }
15241 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15242 break;
15243 ++row;
15244 }
15245 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15246 || row->continued_p)
15247 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15248 || (MATRIX_ROW_START_CHARPOS (row) == PT
15249 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15250 /* If we didn't find any candidate rows, or exited the
15251 loop before all the candidates were examined, signal
15252 to the caller that this method failed. */
15253 if (rc != CURSOR_MOVEMENT_SUCCESS
15254 && !(rv
15255 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15256 && !row->continued_p))
15257 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15258 else if (rv)
15259 rc = CURSOR_MOVEMENT_SUCCESS;
15260 }
15261 else
15262 {
15263 do
15264 {
15265 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15266 {
15267 rc = CURSOR_MOVEMENT_SUCCESS;
15268 break;
15269 }
15270 ++row;
15271 }
15272 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15273 && MATRIX_ROW_START_CHARPOS (row) == PT
15274 && cursor_row_p (row));
15275 }
15276 }
15277 }
15278
15279 return rc;
15280 }
15281
15282 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15283 static
15284 #endif
15285 void
15286 set_vertical_scroll_bar (struct window *w)
15287 {
15288 ptrdiff_t start, end, whole;
15289
15290 /* Calculate the start and end positions for the current window.
15291 At some point, it would be nice to choose between scrollbars
15292 which reflect the whole buffer size, with special markers
15293 indicating narrowing, and scrollbars which reflect only the
15294 visible region.
15295
15296 Note that mini-buffers sometimes aren't displaying any text. */
15297 if (!MINI_WINDOW_P (w)
15298 || (w == XWINDOW (minibuf_window)
15299 && NILP (echo_area_buffer[0])))
15300 {
15301 struct buffer *buf = XBUFFER (w->contents);
15302 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15303 start = marker_position (w->start) - BUF_BEGV (buf);
15304 /* I don't think this is guaranteed to be right. For the
15305 moment, we'll pretend it is. */
15306 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15307
15308 if (end < start)
15309 end = start;
15310 if (whole < (end - start))
15311 whole = end - start;
15312 }
15313 else
15314 start = end = whole = 0;
15315
15316 /* Indicate what this scroll bar ought to be displaying now. */
15317 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15318 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15319 (w, end - start, whole, start);
15320 }
15321
15322
15323 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15324 selected_window is redisplayed.
15325
15326 We can return without actually redisplaying the window if fonts has been
15327 changed on window's frame. In that case, redisplay_internal will retry. */
15328
15329 static void
15330 redisplay_window (Lisp_Object window, int just_this_one_p)
15331 {
15332 struct window *w = XWINDOW (window);
15333 struct frame *f = XFRAME (w->frame);
15334 struct buffer *buffer = XBUFFER (w->contents);
15335 struct buffer *old = current_buffer;
15336 struct text_pos lpoint, opoint, startp;
15337 int update_mode_line;
15338 int tem;
15339 struct it it;
15340 /* Record it now because it's overwritten. */
15341 int current_matrix_up_to_date_p = 0;
15342 int used_current_matrix_p = 0;
15343 /* This is less strict than current_matrix_up_to_date_p.
15344 It indicates that the buffer contents and narrowing are unchanged. */
15345 int buffer_unchanged_p = 0;
15346 int temp_scroll_step = 0;
15347 ptrdiff_t count = SPECPDL_INDEX ();
15348 int rc;
15349 int centering_position = -1;
15350 int last_line_misfit = 0;
15351 ptrdiff_t beg_unchanged, end_unchanged;
15352 int frame_line_height;
15353
15354 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15355 opoint = lpoint;
15356
15357 #ifdef GLYPH_DEBUG
15358 *w->desired_matrix->method = 0;
15359 #endif
15360
15361 /* Make sure that both W's markers are valid. */
15362 eassert (XMARKER (w->start)->buffer == buffer);
15363 eassert (XMARKER (w->pointm)->buffer == buffer);
15364
15365 restart:
15366 reconsider_clip_changes (w);
15367 frame_line_height = default_line_pixel_height (w);
15368
15369 /* Has the mode line to be updated? */
15370 update_mode_line = (w->update_mode_line
15371 || update_mode_lines
15372 || buffer->clip_changed
15373 || buffer->prevent_redisplay_optimizations_p);
15374
15375 if (MINI_WINDOW_P (w))
15376 {
15377 if (w == XWINDOW (echo_area_window)
15378 && !NILP (echo_area_buffer[0]))
15379 {
15380 if (update_mode_line)
15381 /* We may have to update a tty frame's menu bar or a
15382 tool-bar. Example `M-x C-h C-h C-g'. */
15383 goto finish_menu_bars;
15384 else
15385 /* We've already displayed the echo area glyphs in this window. */
15386 goto finish_scroll_bars;
15387 }
15388 else if ((w != XWINDOW (minibuf_window)
15389 || minibuf_level == 0)
15390 /* When buffer is nonempty, redisplay window normally. */
15391 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15392 /* Quail displays non-mini buffers in minibuffer window.
15393 In that case, redisplay the window normally. */
15394 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15395 {
15396 /* W is a mini-buffer window, but it's not active, so clear
15397 it. */
15398 int yb = window_text_bottom_y (w);
15399 struct glyph_row *row;
15400 int y;
15401
15402 for (y = 0, row = w->desired_matrix->rows;
15403 y < yb;
15404 y += row->height, ++row)
15405 blank_row (w, row, y);
15406 goto finish_scroll_bars;
15407 }
15408
15409 clear_glyph_matrix (w->desired_matrix);
15410 }
15411
15412 /* Otherwise set up data on this window; select its buffer and point
15413 value. */
15414 /* Really select the buffer, for the sake of buffer-local
15415 variables. */
15416 set_buffer_internal_1 (XBUFFER (w->contents));
15417
15418 current_matrix_up_to_date_p
15419 = (w->window_end_valid
15420 && !current_buffer->clip_changed
15421 && !current_buffer->prevent_redisplay_optimizations_p
15422 && !window_outdated (w));
15423
15424 /* Run the window-bottom-change-functions
15425 if it is possible that the text on the screen has changed
15426 (either due to modification of the text, or any other reason). */
15427 if (!current_matrix_up_to_date_p
15428 && !NILP (Vwindow_text_change_functions))
15429 {
15430 safe_run_hooks (Qwindow_text_change_functions);
15431 goto restart;
15432 }
15433
15434 beg_unchanged = BEG_UNCHANGED;
15435 end_unchanged = END_UNCHANGED;
15436
15437 SET_TEXT_POS (opoint, PT, PT_BYTE);
15438
15439 specbind (Qinhibit_point_motion_hooks, Qt);
15440
15441 buffer_unchanged_p
15442 = (w->window_end_valid
15443 && !current_buffer->clip_changed
15444 && !window_outdated (w));
15445
15446 /* When windows_or_buffers_changed is non-zero, we can't rely
15447 on the window end being valid, so set it to zero there. */
15448 if (windows_or_buffers_changed)
15449 {
15450 /* If window starts on a continuation line, maybe adjust the
15451 window start in case the window's width changed. */
15452 if (XMARKER (w->start)->buffer == current_buffer)
15453 compute_window_start_on_continuation_line (w);
15454
15455 w->window_end_valid = 0;
15456 /* If so, we also can't rely on current matrix
15457 and should not fool try_cursor_movement below. */
15458 current_matrix_up_to_date_p = 0;
15459 }
15460
15461 /* Some sanity checks. */
15462 CHECK_WINDOW_END (w);
15463 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15464 emacs_abort ();
15465 if (BYTEPOS (opoint) < CHARPOS (opoint))
15466 emacs_abort ();
15467
15468 if (mode_line_update_needed (w))
15469 update_mode_line = 1;
15470
15471 /* Point refers normally to the selected window. For any other
15472 window, set up appropriate value. */
15473 if (!EQ (window, selected_window))
15474 {
15475 ptrdiff_t new_pt = marker_position (w->pointm);
15476 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15477 if (new_pt < BEGV)
15478 {
15479 new_pt = BEGV;
15480 new_pt_byte = BEGV_BYTE;
15481 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15482 }
15483 else if (new_pt > (ZV - 1))
15484 {
15485 new_pt = ZV;
15486 new_pt_byte = ZV_BYTE;
15487 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15488 }
15489
15490 /* We don't use SET_PT so that the point-motion hooks don't run. */
15491 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15492 }
15493
15494 /* If any of the character widths specified in the display table
15495 have changed, invalidate the width run cache. It's true that
15496 this may be a bit late to catch such changes, but the rest of
15497 redisplay goes (non-fatally) haywire when the display table is
15498 changed, so why should we worry about doing any better? */
15499 if (current_buffer->width_run_cache)
15500 {
15501 struct Lisp_Char_Table *disptab = buffer_display_table ();
15502
15503 if (! disptab_matches_widthtab
15504 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15505 {
15506 invalidate_region_cache (current_buffer,
15507 current_buffer->width_run_cache,
15508 BEG, Z);
15509 recompute_width_table (current_buffer, disptab);
15510 }
15511 }
15512
15513 /* If window-start is screwed up, choose a new one. */
15514 if (XMARKER (w->start)->buffer != current_buffer)
15515 goto recenter;
15516
15517 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15518
15519 /* If someone specified a new starting point but did not insist,
15520 check whether it can be used. */
15521 if (w->optional_new_start
15522 && CHARPOS (startp) >= BEGV
15523 && CHARPOS (startp) <= ZV)
15524 {
15525 w->optional_new_start = 0;
15526 start_display (&it, w, startp);
15527 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15528 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15529 if (IT_CHARPOS (it) == PT)
15530 w->force_start = 1;
15531 /* IT may overshoot PT if text at PT is invisible. */
15532 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15533 w->force_start = 1;
15534 }
15535
15536 force_start:
15537
15538 /* Handle case where place to start displaying has been specified,
15539 unless the specified location is outside the accessible range. */
15540 if (w->force_start || window_frozen_p (w))
15541 {
15542 /* We set this later on if we have to adjust point. */
15543 int new_vpos = -1;
15544
15545 w->force_start = 0;
15546 w->vscroll = 0;
15547 w->window_end_valid = 0;
15548
15549 /* Forget any recorded base line for line number display. */
15550 if (!buffer_unchanged_p)
15551 w->base_line_number = 0;
15552
15553 /* Redisplay the mode line. Select the buffer properly for that.
15554 Also, run the hook window-scroll-functions
15555 because we have scrolled. */
15556 /* Note, we do this after clearing force_start because
15557 if there's an error, it is better to forget about force_start
15558 than to get into an infinite loop calling the hook functions
15559 and having them get more errors. */
15560 if (!update_mode_line
15561 || ! NILP (Vwindow_scroll_functions))
15562 {
15563 update_mode_line = 1;
15564 w->update_mode_line = 1;
15565 startp = run_window_scroll_functions (window, startp);
15566 }
15567
15568 if (CHARPOS (startp) < BEGV)
15569 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15570 else if (CHARPOS (startp) > ZV)
15571 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15572
15573 /* Redisplay, then check if cursor has been set during the
15574 redisplay. Give up if new fonts were loaded. */
15575 /* We used to issue a CHECK_MARGINS argument to try_window here,
15576 but this causes scrolling to fail when point begins inside
15577 the scroll margin (bug#148) -- cyd */
15578 if (!try_window (window, startp, 0))
15579 {
15580 w->force_start = 1;
15581 clear_glyph_matrix (w->desired_matrix);
15582 goto need_larger_matrices;
15583 }
15584
15585 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15586 {
15587 /* If point does not appear, try to move point so it does
15588 appear. The desired matrix has been built above, so we
15589 can use it here. */
15590 new_vpos = window_box_height (w) / 2;
15591 }
15592
15593 if (!cursor_row_fully_visible_p (w, 0, 0))
15594 {
15595 /* Point does appear, but on a line partly visible at end of window.
15596 Move it back to a fully-visible line. */
15597 new_vpos = window_box_height (w);
15598 }
15599 else if (w->cursor.vpos >=0)
15600 {
15601 /* Some people insist on not letting point enter the scroll
15602 margin, even though this part handles windows that didn't
15603 scroll at all. */
15604 int window_total_lines
15605 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15606 int margin = min (scroll_margin, window_total_lines / 4);
15607 int pixel_margin = margin * frame_line_height;
15608 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15609
15610 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15611 below, which finds the row to move point to, advances by
15612 the Y coordinate of the _next_ row, see the definition of
15613 MATRIX_ROW_BOTTOM_Y. */
15614 if (w->cursor.vpos < margin + header_line)
15615 {
15616 w->cursor.vpos = -1;
15617 clear_glyph_matrix (w->desired_matrix);
15618 goto try_to_scroll;
15619 }
15620 else
15621 {
15622 int window_height = window_box_height (w);
15623
15624 if (header_line)
15625 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15626 if (w->cursor.y >= window_height - pixel_margin)
15627 {
15628 w->cursor.vpos = -1;
15629 clear_glyph_matrix (w->desired_matrix);
15630 goto try_to_scroll;
15631 }
15632 }
15633 }
15634
15635 /* If we need to move point for either of the above reasons,
15636 now actually do it. */
15637 if (new_vpos >= 0)
15638 {
15639 struct glyph_row *row;
15640
15641 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15642 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15643 ++row;
15644
15645 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15646 MATRIX_ROW_START_BYTEPOS (row));
15647
15648 if (w != XWINDOW (selected_window))
15649 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15650 else if (current_buffer == old)
15651 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15652
15653 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15654
15655 /* If we are highlighting the region, then we just changed
15656 the region, so redisplay to show it. */
15657 if (markpos_of_region () >= 0)
15658 {
15659 clear_glyph_matrix (w->desired_matrix);
15660 if (!try_window (window, startp, 0))
15661 goto need_larger_matrices;
15662 }
15663 }
15664
15665 #ifdef GLYPH_DEBUG
15666 debug_method_add (w, "forced window start");
15667 #endif
15668 goto done;
15669 }
15670
15671 /* Handle case where text has not changed, only point, and it has
15672 not moved off the frame, and we are not retrying after hscroll.
15673 (current_matrix_up_to_date_p is nonzero when retrying.) */
15674 if (current_matrix_up_to_date_p
15675 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15676 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15677 {
15678 switch (rc)
15679 {
15680 case CURSOR_MOVEMENT_SUCCESS:
15681 used_current_matrix_p = 1;
15682 goto done;
15683
15684 case CURSOR_MOVEMENT_MUST_SCROLL:
15685 goto try_to_scroll;
15686
15687 default:
15688 emacs_abort ();
15689 }
15690 }
15691 /* If current starting point was originally the beginning of a line
15692 but no longer is, find a new starting point. */
15693 else if (w->start_at_line_beg
15694 && !(CHARPOS (startp) <= BEGV
15695 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15696 {
15697 #ifdef GLYPH_DEBUG
15698 debug_method_add (w, "recenter 1");
15699 #endif
15700 goto recenter;
15701 }
15702
15703 /* Try scrolling with try_window_id. Value is > 0 if update has
15704 been done, it is -1 if we know that the same window start will
15705 not work. It is 0 if unsuccessful for some other reason. */
15706 else if ((tem = try_window_id (w)) != 0)
15707 {
15708 #ifdef GLYPH_DEBUG
15709 debug_method_add (w, "try_window_id %d", tem);
15710 #endif
15711
15712 if (f->fonts_changed)
15713 goto need_larger_matrices;
15714 if (tem > 0)
15715 goto done;
15716
15717 /* Otherwise try_window_id has returned -1 which means that we
15718 don't want the alternative below this comment to execute. */
15719 }
15720 else if (CHARPOS (startp) >= BEGV
15721 && CHARPOS (startp) <= ZV
15722 && PT >= CHARPOS (startp)
15723 && (CHARPOS (startp) < ZV
15724 /* Avoid starting at end of buffer. */
15725 || CHARPOS (startp) == BEGV
15726 || !window_outdated (w)))
15727 {
15728 int d1, d2, d3, d4, d5, d6;
15729
15730 /* If first window line is a continuation line, and window start
15731 is inside the modified region, but the first change is before
15732 current window start, we must select a new window start.
15733
15734 However, if this is the result of a down-mouse event (e.g. by
15735 extending the mouse-drag-overlay), we don't want to select a
15736 new window start, since that would change the position under
15737 the mouse, resulting in an unwanted mouse-movement rather
15738 than a simple mouse-click. */
15739 if (!w->start_at_line_beg
15740 && NILP (do_mouse_tracking)
15741 && CHARPOS (startp) > BEGV
15742 && CHARPOS (startp) > BEG + beg_unchanged
15743 && CHARPOS (startp) <= Z - end_unchanged
15744 /* Even if w->start_at_line_beg is nil, a new window may
15745 start at a line_beg, since that's how set_buffer_window
15746 sets it. So, we need to check the return value of
15747 compute_window_start_on_continuation_line. (See also
15748 bug#197). */
15749 && XMARKER (w->start)->buffer == current_buffer
15750 && compute_window_start_on_continuation_line (w)
15751 /* It doesn't make sense to force the window start like we
15752 do at label force_start if it is already known that point
15753 will not be visible in the resulting window, because
15754 doing so will move point from its correct position
15755 instead of scrolling the window to bring point into view.
15756 See bug#9324. */
15757 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15758 {
15759 w->force_start = 1;
15760 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15761 goto force_start;
15762 }
15763
15764 #ifdef GLYPH_DEBUG
15765 debug_method_add (w, "same window start");
15766 #endif
15767
15768 /* Try to redisplay starting at same place as before.
15769 If point has not moved off frame, accept the results. */
15770 if (!current_matrix_up_to_date_p
15771 /* Don't use try_window_reusing_current_matrix in this case
15772 because a window scroll function can have changed the
15773 buffer. */
15774 || !NILP (Vwindow_scroll_functions)
15775 || MINI_WINDOW_P (w)
15776 || !(used_current_matrix_p
15777 = try_window_reusing_current_matrix (w)))
15778 {
15779 IF_DEBUG (debug_method_add (w, "1"));
15780 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15781 /* -1 means we need to scroll.
15782 0 means we need new matrices, but fonts_changed
15783 is set in that case, so we will detect it below. */
15784 goto try_to_scroll;
15785 }
15786
15787 if (f->fonts_changed)
15788 goto need_larger_matrices;
15789
15790 if (w->cursor.vpos >= 0)
15791 {
15792 if (!just_this_one_p
15793 || current_buffer->clip_changed
15794 || BEG_UNCHANGED < CHARPOS (startp))
15795 /* Forget any recorded base line for line number display. */
15796 w->base_line_number = 0;
15797
15798 if (!cursor_row_fully_visible_p (w, 1, 0))
15799 {
15800 clear_glyph_matrix (w->desired_matrix);
15801 last_line_misfit = 1;
15802 }
15803 /* Drop through and scroll. */
15804 else
15805 goto done;
15806 }
15807 else
15808 clear_glyph_matrix (w->desired_matrix);
15809 }
15810
15811 try_to_scroll:
15812
15813 /* Redisplay the mode line. Select the buffer properly for that. */
15814 if (!update_mode_line)
15815 {
15816 update_mode_line = 1;
15817 w->update_mode_line = 1;
15818 }
15819
15820 /* Try to scroll by specified few lines. */
15821 if ((scroll_conservatively
15822 || emacs_scroll_step
15823 || temp_scroll_step
15824 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15825 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15826 && CHARPOS (startp) >= BEGV
15827 && CHARPOS (startp) <= ZV)
15828 {
15829 /* The function returns -1 if new fonts were loaded, 1 if
15830 successful, 0 if not successful. */
15831 int ss = try_scrolling (window, just_this_one_p,
15832 scroll_conservatively,
15833 emacs_scroll_step,
15834 temp_scroll_step, last_line_misfit);
15835 switch (ss)
15836 {
15837 case SCROLLING_SUCCESS:
15838 goto done;
15839
15840 case SCROLLING_NEED_LARGER_MATRICES:
15841 goto need_larger_matrices;
15842
15843 case SCROLLING_FAILED:
15844 break;
15845
15846 default:
15847 emacs_abort ();
15848 }
15849 }
15850
15851 /* Finally, just choose a place to start which positions point
15852 according to user preferences. */
15853
15854 recenter:
15855
15856 #ifdef GLYPH_DEBUG
15857 debug_method_add (w, "recenter");
15858 #endif
15859
15860 /* Forget any previously recorded base line for line number display. */
15861 if (!buffer_unchanged_p)
15862 w->base_line_number = 0;
15863
15864 /* Determine the window start relative to point. */
15865 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15866 it.current_y = it.last_visible_y;
15867 if (centering_position < 0)
15868 {
15869 int window_total_lines
15870 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15871 int margin =
15872 scroll_margin > 0
15873 ? min (scroll_margin, window_total_lines / 4)
15874 : 0;
15875 ptrdiff_t margin_pos = CHARPOS (startp);
15876 Lisp_Object aggressive;
15877 int scrolling_up;
15878
15879 /* If there is a scroll margin at the top of the window, find
15880 its character position. */
15881 if (margin
15882 /* Cannot call start_display if startp is not in the
15883 accessible region of the buffer. This can happen when we
15884 have just switched to a different buffer and/or changed
15885 its restriction. In that case, startp is initialized to
15886 the character position 1 (BEGV) because we did not yet
15887 have chance to display the buffer even once. */
15888 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15889 {
15890 struct it it1;
15891 void *it1data = NULL;
15892
15893 SAVE_IT (it1, it, it1data);
15894 start_display (&it1, w, startp);
15895 move_it_vertically (&it1, margin * frame_line_height);
15896 margin_pos = IT_CHARPOS (it1);
15897 RESTORE_IT (&it, &it, it1data);
15898 }
15899 scrolling_up = PT > margin_pos;
15900 aggressive =
15901 scrolling_up
15902 ? BVAR (current_buffer, scroll_up_aggressively)
15903 : BVAR (current_buffer, scroll_down_aggressively);
15904
15905 if (!MINI_WINDOW_P (w)
15906 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15907 {
15908 int pt_offset = 0;
15909
15910 /* Setting scroll-conservatively overrides
15911 scroll-*-aggressively. */
15912 if (!scroll_conservatively && NUMBERP (aggressive))
15913 {
15914 double float_amount = XFLOATINT (aggressive);
15915
15916 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15917 if (pt_offset == 0 && float_amount > 0)
15918 pt_offset = 1;
15919 if (pt_offset && margin > 0)
15920 margin -= 1;
15921 }
15922 /* Compute how much to move the window start backward from
15923 point so that point will be displayed where the user
15924 wants it. */
15925 if (scrolling_up)
15926 {
15927 centering_position = it.last_visible_y;
15928 if (pt_offset)
15929 centering_position -= pt_offset;
15930 centering_position -=
15931 frame_line_height * (1 + margin + (last_line_misfit != 0))
15932 + WINDOW_HEADER_LINE_HEIGHT (w);
15933 /* Don't let point enter the scroll margin near top of
15934 the window. */
15935 if (centering_position < margin * frame_line_height)
15936 centering_position = margin * frame_line_height;
15937 }
15938 else
15939 centering_position = margin * frame_line_height + pt_offset;
15940 }
15941 else
15942 /* Set the window start half the height of the window backward
15943 from point. */
15944 centering_position = window_box_height (w) / 2;
15945 }
15946 move_it_vertically_backward (&it, centering_position);
15947
15948 eassert (IT_CHARPOS (it) >= BEGV);
15949
15950 /* The function move_it_vertically_backward may move over more
15951 than the specified y-distance. If it->w is small, e.g. a
15952 mini-buffer window, we may end up in front of the window's
15953 display area. Start displaying at the start of the line
15954 containing PT in this case. */
15955 if (it.current_y <= 0)
15956 {
15957 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15958 move_it_vertically_backward (&it, 0);
15959 it.current_y = 0;
15960 }
15961
15962 it.current_x = it.hpos = 0;
15963
15964 /* Set the window start position here explicitly, to avoid an
15965 infinite loop in case the functions in window-scroll-functions
15966 get errors. */
15967 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15968
15969 /* Run scroll hooks. */
15970 startp = run_window_scroll_functions (window, it.current.pos);
15971
15972 /* Redisplay the window. */
15973 if (!current_matrix_up_to_date_p
15974 || windows_or_buffers_changed
15975 || cursor_type_changed
15976 /* Don't use try_window_reusing_current_matrix in this case
15977 because it can have changed the buffer. */
15978 || !NILP (Vwindow_scroll_functions)
15979 || !just_this_one_p
15980 || MINI_WINDOW_P (w)
15981 || !(used_current_matrix_p
15982 = try_window_reusing_current_matrix (w)))
15983 try_window (window, startp, 0);
15984
15985 /* If new fonts have been loaded (due to fontsets), give up. We
15986 have to start a new redisplay since we need to re-adjust glyph
15987 matrices. */
15988 if (f->fonts_changed)
15989 goto need_larger_matrices;
15990
15991 /* If cursor did not appear assume that the middle of the window is
15992 in the first line of the window. Do it again with the next line.
15993 (Imagine a window of height 100, displaying two lines of height
15994 60. Moving back 50 from it->last_visible_y will end in the first
15995 line.) */
15996 if (w->cursor.vpos < 0)
15997 {
15998 if (w->window_end_valid && PT >= Z - w->window_end_pos)
15999 {
16000 clear_glyph_matrix (w->desired_matrix);
16001 move_it_by_lines (&it, 1);
16002 try_window (window, it.current.pos, 0);
16003 }
16004 else if (PT < IT_CHARPOS (it))
16005 {
16006 clear_glyph_matrix (w->desired_matrix);
16007 move_it_by_lines (&it, -1);
16008 try_window (window, it.current.pos, 0);
16009 }
16010 else
16011 {
16012 /* Not much we can do about it. */
16013 }
16014 }
16015
16016 /* Consider the following case: Window starts at BEGV, there is
16017 invisible, intangible text at BEGV, so that display starts at
16018 some point START > BEGV. It can happen that we are called with
16019 PT somewhere between BEGV and START. Try to handle that case. */
16020 if (w->cursor.vpos < 0)
16021 {
16022 struct glyph_row *row = w->current_matrix->rows;
16023 if (row->mode_line_p)
16024 ++row;
16025 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16026 }
16027
16028 if (!cursor_row_fully_visible_p (w, 0, 0))
16029 {
16030 /* If vscroll is enabled, disable it and try again. */
16031 if (w->vscroll)
16032 {
16033 w->vscroll = 0;
16034 clear_glyph_matrix (w->desired_matrix);
16035 goto recenter;
16036 }
16037
16038 /* Users who set scroll-conservatively to a large number want
16039 point just above/below the scroll margin. If we ended up
16040 with point's row partially visible, move the window start to
16041 make that row fully visible and out of the margin. */
16042 if (scroll_conservatively > SCROLL_LIMIT)
16043 {
16044 int window_total_lines
16045 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16046 int margin =
16047 scroll_margin > 0
16048 ? min (scroll_margin, window_total_lines / 4)
16049 : 0;
16050 int move_down = w->cursor.vpos >= window_total_lines / 2;
16051
16052 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16053 clear_glyph_matrix (w->desired_matrix);
16054 if (1 == try_window (window, it.current.pos,
16055 TRY_WINDOW_CHECK_MARGINS))
16056 goto done;
16057 }
16058
16059 /* If centering point failed to make the whole line visible,
16060 put point at the top instead. That has to make the whole line
16061 visible, if it can be done. */
16062 if (centering_position == 0)
16063 goto done;
16064
16065 clear_glyph_matrix (w->desired_matrix);
16066 centering_position = 0;
16067 goto recenter;
16068 }
16069
16070 done:
16071
16072 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16073 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16074 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16075
16076 /* Display the mode line, if we must. */
16077 if ((update_mode_line
16078 /* If window not full width, must redo its mode line
16079 if (a) the window to its side is being redone and
16080 (b) we do a frame-based redisplay. This is a consequence
16081 of how inverted lines are drawn in frame-based redisplay. */
16082 || (!just_this_one_p
16083 && !FRAME_WINDOW_P (f)
16084 && !WINDOW_FULL_WIDTH_P (w))
16085 /* Line number to display. */
16086 || w->base_line_pos > 0
16087 /* Column number is displayed and different from the one displayed. */
16088 || (w->column_number_displayed != -1
16089 && (w->column_number_displayed != current_column ())))
16090 /* This means that the window has a mode line. */
16091 && (WINDOW_WANTS_MODELINE_P (w)
16092 || WINDOW_WANTS_HEADER_LINE_P (w)))
16093 {
16094 display_mode_lines (w);
16095
16096 /* If mode line height has changed, arrange for a thorough
16097 immediate redisplay using the correct mode line height. */
16098 if (WINDOW_WANTS_MODELINE_P (w)
16099 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16100 {
16101 f->fonts_changed = 1;
16102 w->mode_line_height = -1;
16103 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16104 = DESIRED_MODE_LINE_HEIGHT (w);
16105 }
16106
16107 /* If header line height has changed, arrange for a thorough
16108 immediate redisplay using the correct header line height. */
16109 if (WINDOW_WANTS_HEADER_LINE_P (w)
16110 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16111 {
16112 f->fonts_changed = 1;
16113 w->header_line_height = -1;
16114 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16115 = DESIRED_HEADER_LINE_HEIGHT (w);
16116 }
16117
16118 if (f->fonts_changed)
16119 goto need_larger_matrices;
16120 }
16121
16122 if (!line_number_displayed && w->base_line_pos != -1)
16123 {
16124 w->base_line_pos = 0;
16125 w->base_line_number = 0;
16126 }
16127
16128 finish_menu_bars:
16129
16130 /* When we reach a frame's selected window, redo the frame's menu bar. */
16131 if (update_mode_line
16132 && EQ (FRAME_SELECTED_WINDOW (f), window))
16133 {
16134 int redisplay_menu_p = 0;
16135
16136 if (FRAME_WINDOW_P (f))
16137 {
16138 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16139 || defined (HAVE_NS) || defined (USE_GTK)
16140 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16141 #else
16142 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16143 #endif
16144 }
16145 else
16146 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16147
16148 if (redisplay_menu_p)
16149 display_menu_bar (w);
16150
16151 #ifdef HAVE_WINDOW_SYSTEM
16152 if (FRAME_WINDOW_P (f))
16153 {
16154 #if defined (USE_GTK) || defined (HAVE_NS)
16155 if (FRAME_EXTERNAL_TOOL_BAR (f))
16156 redisplay_tool_bar (f);
16157 #else
16158 if (WINDOWP (f->tool_bar_window)
16159 && (FRAME_TOOL_BAR_LINES (f) > 0
16160 || !NILP (Vauto_resize_tool_bars))
16161 && redisplay_tool_bar (f))
16162 ignore_mouse_drag_p = 1;
16163 #endif
16164 }
16165 #endif
16166 }
16167
16168 #ifdef HAVE_WINDOW_SYSTEM
16169 if (FRAME_WINDOW_P (f)
16170 && update_window_fringes (w, (just_this_one_p
16171 || (!used_current_matrix_p && !overlay_arrow_seen)
16172 || w->pseudo_window_p)))
16173 {
16174 update_begin (f);
16175 block_input ();
16176 if (draw_window_fringes (w, 1))
16177 x_draw_vertical_border (w);
16178 unblock_input ();
16179 update_end (f);
16180 }
16181 #endif /* HAVE_WINDOW_SYSTEM */
16182
16183 /* We go to this label, with fonts_changed set, if it is
16184 necessary to try again using larger glyph matrices.
16185 We have to redeem the scroll bar even in this case,
16186 because the loop in redisplay_internal expects that. */
16187 need_larger_matrices:
16188 ;
16189 finish_scroll_bars:
16190
16191 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16192 {
16193 /* Set the thumb's position and size. */
16194 set_vertical_scroll_bar (w);
16195
16196 /* Note that we actually used the scroll bar attached to this
16197 window, so it shouldn't be deleted at the end of redisplay. */
16198 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16199 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16200 }
16201
16202 /* Restore current_buffer and value of point in it. The window
16203 update may have changed the buffer, so first make sure `opoint'
16204 is still valid (Bug#6177). */
16205 if (CHARPOS (opoint) < BEGV)
16206 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16207 else if (CHARPOS (opoint) > ZV)
16208 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16209 else
16210 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16211
16212 set_buffer_internal_1 (old);
16213 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16214 shorter. This can be caused by log truncation in *Messages*. */
16215 if (CHARPOS (lpoint) <= ZV)
16216 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16217
16218 unbind_to (count, Qnil);
16219 }
16220
16221
16222 /* Build the complete desired matrix of WINDOW with a window start
16223 buffer position POS.
16224
16225 Value is 1 if successful. It is zero if fonts were loaded during
16226 redisplay which makes re-adjusting glyph matrices necessary, and -1
16227 if point would appear in the scroll margins.
16228 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16229 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16230 set in FLAGS.) */
16231
16232 int
16233 try_window (Lisp_Object window, struct text_pos pos, int flags)
16234 {
16235 struct window *w = XWINDOW (window);
16236 struct it it;
16237 struct glyph_row *last_text_row = NULL;
16238 struct frame *f = XFRAME (w->frame);
16239 int frame_line_height = default_line_pixel_height (w);
16240
16241 /* Make POS the new window start. */
16242 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16243
16244 /* Mark cursor position as unknown. No overlay arrow seen. */
16245 w->cursor.vpos = -1;
16246 overlay_arrow_seen = 0;
16247
16248 /* Initialize iterator and info to start at POS. */
16249 start_display (&it, w, pos);
16250
16251 /* Display all lines of W. */
16252 while (it.current_y < it.last_visible_y)
16253 {
16254 if (display_line (&it))
16255 last_text_row = it.glyph_row - 1;
16256 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16257 return 0;
16258 }
16259
16260 /* Don't let the cursor end in the scroll margins. */
16261 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16262 && !MINI_WINDOW_P (w))
16263 {
16264 int this_scroll_margin;
16265 int window_total_lines
16266 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16267
16268 if (scroll_margin > 0)
16269 {
16270 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16271 this_scroll_margin *= frame_line_height;
16272 }
16273 else
16274 this_scroll_margin = 0;
16275
16276 if ((w->cursor.y >= 0 /* not vscrolled */
16277 && w->cursor.y < this_scroll_margin
16278 && CHARPOS (pos) > BEGV
16279 && IT_CHARPOS (it) < ZV)
16280 /* rms: considering make_cursor_line_fully_visible_p here
16281 seems to give wrong results. We don't want to recenter
16282 when the last line is partly visible, we want to allow
16283 that case to be handled in the usual way. */
16284 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16285 {
16286 w->cursor.vpos = -1;
16287 clear_glyph_matrix (w->desired_matrix);
16288 return -1;
16289 }
16290 }
16291
16292 /* If bottom moved off end of frame, change mode line percentage. */
16293 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16294 w->update_mode_line = 1;
16295
16296 /* Set window_end_pos to the offset of the last character displayed
16297 on the window from the end of current_buffer. Set
16298 window_end_vpos to its row number. */
16299 if (last_text_row)
16300 {
16301 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16302 adjust_window_ends (w, last_text_row, 0);
16303 eassert
16304 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16305 w->window_end_vpos)));
16306 }
16307 else
16308 {
16309 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16310 w->window_end_pos = Z - ZV;
16311 w->window_end_vpos = 0;
16312 }
16313
16314 /* But that is not valid info until redisplay finishes. */
16315 w->window_end_valid = 0;
16316 return 1;
16317 }
16318
16319
16320 \f
16321 /************************************************************************
16322 Window redisplay reusing current matrix when buffer has not changed
16323 ************************************************************************/
16324
16325 /* Try redisplay of window W showing an unchanged buffer with a
16326 different window start than the last time it was displayed by
16327 reusing its current matrix. Value is non-zero if successful.
16328 W->start is the new window start. */
16329
16330 static int
16331 try_window_reusing_current_matrix (struct window *w)
16332 {
16333 struct frame *f = XFRAME (w->frame);
16334 struct glyph_row *bottom_row;
16335 struct it it;
16336 struct run run;
16337 struct text_pos start, new_start;
16338 int nrows_scrolled, i;
16339 struct glyph_row *last_text_row;
16340 struct glyph_row *last_reused_text_row;
16341 struct glyph_row *start_row;
16342 int start_vpos, min_y, max_y;
16343
16344 #ifdef GLYPH_DEBUG
16345 if (inhibit_try_window_reusing)
16346 return 0;
16347 #endif
16348
16349 if (/* This function doesn't handle terminal frames. */
16350 !FRAME_WINDOW_P (f)
16351 /* Don't try to reuse the display if windows have been split
16352 or such. */
16353 || windows_or_buffers_changed
16354 || cursor_type_changed)
16355 return 0;
16356
16357 /* Can't do this if region may have changed. */
16358 if (markpos_of_region () >= 0
16359 || w->region_showing
16360 || !NILP (Vshow_trailing_whitespace))
16361 return 0;
16362
16363 /* If top-line visibility has changed, give up. */
16364 if (WINDOW_WANTS_HEADER_LINE_P (w)
16365 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16366 return 0;
16367
16368 /* Give up if old or new display is scrolled vertically. We could
16369 make this function handle this, but right now it doesn't. */
16370 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16371 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16372 return 0;
16373
16374 /* The variable new_start now holds the new window start. The old
16375 start `start' can be determined from the current matrix. */
16376 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16377 start = start_row->minpos;
16378 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16379
16380 /* Clear the desired matrix for the display below. */
16381 clear_glyph_matrix (w->desired_matrix);
16382
16383 if (CHARPOS (new_start) <= CHARPOS (start))
16384 {
16385 /* Don't use this method if the display starts with an ellipsis
16386 displayed for invisible text. It's not easy to handle that case
16387 below, and it's certainly not worth the effort since this is
16388 not a frequent case. */
16389 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16390 return 0;
16391
16392 IF_DEBUG (debug_method_add (w, "twu1"));
16393
16394 /* Display up to a row that can be reused. The variable
16395 last_text_row is set to the last row displayed that displays
16396 text. Note that it.vpos == 0 if or if not there is a
16397 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16398 start_display (&it, w, new_start);
16399 w->cursor.vpos = -1;
16400 last_text_row = last_reused_text_row = NULL;
16401
16402 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16403 {
16404 /* If we have reached into the characters in the START row,
16405 that means the line boundaries have changed. So we
16406 can't start copying with the row START. Maybe it will
16407 work to start copying with the following row. */
16408 while (IT_CHARPOS (it) > CHARPOS (start))
16409 {
16410 /* Advance to the next row as the "start". */
16411 start_row++;
16412 start = start_row->minpos;
16413 /* If there are no more rows to try, or just one, give up. */
16414 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16415 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16416 || CHARPOS (start) == ZV)
16417 {
16418 clear_glyph_matrix (w->desired_matrix);
16419 return 0;
16420 }
16421
16422 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16423 }
16424 /* If we have reached alignment, we can copy the rest of the
16425 rows. */
16426 if (IT_CHARPOS (it) == CHARPOS (start)
16427 /* Don't accept "alignment" inside a display vector,
16428 since start_row could have started in the middle of
16429 that same display vector (thus their character
16430 positions match), and we have no way of telling if
16431 that is the case. */
16432 && it.current.dpvec_index < 0)
16433 break;
16434
16435 if (display_line (&it))
16436 last_text_row = it.glyph_row - 1;
16437
16438 }
16439
16440 /* A value of current_y < last_visible_y means that we stopped
16441 at the previous window start, which in turn means that we
16442 have at least one reusable row. */
16443 if (it.current_y < it.last_visible_y)
16444 {
16445 struct glyph_row *row;
16446
16447 /* IT.vpos always starts from 0; it counts text lines. */
16448 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16449
16450 /* Find PT if not already found in the lines displayed. */
16451 if (w->cursor.vpos < 0)
16452 {
16453 int dy = it.current_y - start_row->y;
16454
16455 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16456 row = row_containing_pos (w, PT, row, NULL, dy);
16457 if (row)
16458 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16459 dy, nrows_scrolled);
16460 else
16461 {
16462 clear_glyph_matrix (w->desired_matrix);
16463 return 0;
16464 }
16465 }
16466
16467 /* Scroll the display. Do it before the current matrix is
16468 changed. The problem here is that update has not yet
16469 run, i.e. part of the current matrix is not up to date.
16470 scroll_run_hook will clear the cursor, and use the
16471 current matrix to get the height of the row the cursor is
16472 in. */
16473 run.current_y = start_row->y;
16474 run.desired_y = it.current_y;
16475 run.height = it.last_visible_y - it.current_y;
16476
16477 if (run.height > 0 && run.current_y != run.desired_y)
16478 {
16479 update_begin (f);
16480 FRAME_RIF (f)->update_window_begin_hook (w);
16481 FRAME_RIF (f)->clear_window_mouse_face (w);
16482 FRAME_RIF (f)->scroll_run_hook (w, &run);
16483 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16484 update_end (f);
16485 }
16486
16487 /* Shift current matrix down by nrows_scrolled lines. */
16488 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16489 rotate_matrix (w->current_matrix,
16490 start_vpos,
16491 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16492 nrows_scrolled);
16493
16494 /* Disable lines that must be updated. */
16495 for (i = 0; i < nrows_scrolled; ++i)
16496 (start_row + i)->enabled_p = 0;
16497
16498 /* Re-compute Y positions. */
16499 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16500 max_y = it.last_visible_y;
16501 for (row = start_row + nrows_scrolled;
16502 row < bottom_row;
16503 ++row)
16504 {
16505 row->y = it.current_y;
16506 row->visible_height = row->height;
16507
16508 if (row->y < min_y)
16509 row->visible_height -= min_y - row->y;
16510 if (row->y + row->height > max_y)
16511 row->visible_height -= row->y + row->height - max_y;
16512 if (row->fringe_bitmap_periodic_p)
16513 row->redraw_fringe_bitmaps_p = 1;
16514
16515 it.current_y += row->height;
16516
16517 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16518 last_reused_text_row = row;
16519 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16520 break;
16521 }
16522
16523 /* Disable lines in the current matrix which are now
16524 below the window. */
16525 for (++row; row < bottom_row; ++row)
16526 row->enabled_p = row->mode_line_p = 0;
16527 }
16528
16529 /* Update window_end_pos etc.; last_reused_text_row is the last
16530 reused row from the current matrix containing text, if any.
16531 The value of last_text_row is the last displayed line
16532 containing text. */
16533 if (last_reused_text_row)
16534 adjust_window_ends (w, last_reused_text_row, 1);
16535 else if (last_text_row)
16536 adjust_window_ends (w, last_text_row, 0);
16537 else
16538 {
16539 /* This window must be completely empty. */
16540 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16541 w->window_end_pos = Z - ZV;
16542 w->window_end_vpos = 0;
16543 }
16544 w->window_end_valid = 0;
16545
16546 /* Update hint: don't try scrolling again in update_window. */
16547 w->desired_matrix->no_scrolling_p = 1;
16548
16549 #ifdef GLYPH_DEBUG
16550 debug_method_add (w, "try_window_reusing_current_matrix 1");
16551 #endif
16552 return 1;
16553 }
16554 else if (CHARPOS (new_start) > CHARPOS (start))
16555 {
16556 struct glyph_row *pt_row, *row;
16557 struct glyph_row *first_reusable_row;
16558 struct glyph_row *first_row_to_display;
16559 int dy;
16560 int yb = window_text_bottom_y (w);
16561
16562 /* Find the row starting at new_start, if there is one. Don't
16563 reuse a partially visible line at the end. */
16564 first_reusable_row = start_row;
16565 while (first_reusable_row->enabled_p
16566 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16567 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16568 < CHARPOS (new_start)))
16569 ++first_reusable_row;
16570
16571 /* Give up if there is no row to reuse. */
16572 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16573 || !first_reusable_row->enabled_p
16574 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16575 != CHARPOS (new_start)))
16576 return 0;
16577
16578 /* We can reuse fully visible rows beginning with
16579 first_reusable_row to the end of the window. Set
16580 first_row_to_display to the first row that cannot be reused.
16581 Set pt_row to the row containing point, if there is any. */
16582 pt_row = NULL;
16583 for (first_row_to_display = first_reusable_row;
16584 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16585 ++first_row_to_display)
16586 {
16587 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16588 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16589 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16590 && first_row_to_display->ends_at_zv_p
16591 && pt_row == NULL)))
16592 pt_row = first_row_to_display;
16593 }
16594
16595 /* Start displaying at the start of first_row_to_display. */
16596 eassert (first_row_to_display->y < yb);
16597 init_to_row_start (&it, w, first_row_to_display);
16598
16599 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16600 - start_vpos);
16601 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16602 - nrows_scrolled);
16603 it.current_y = (first_row_to_display->y - first_reusable_row->y
16604 + WINDOW_HEADER_LINE_HEIGHT (w));
16605
16606 /* Display lines beginning with first_row_to_display in the
16607 desired matrix. Set last_text_row to the last row displayed
16608 that displays text. */
16609 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16610 if (pt_row == NULL)
16611 w->cursor.vpos = -1;
16612 last_text_row = NULL;
16613 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16614 if (display_line (&it))
16615 last_text_row = it.glyph_row - 1;
16616
16617 /* If point is in a reused row, adjust y and vpos of the cursor
16618 position. */
16619 if (pt_row)
16620 {
16621 w->cursor.vpos -= nrows_scrolled;
16622 w->cursor.y -= first_reusable_row->y - start_row->y;
16623 }
16624
16625 /* Give up if point isn't in a row displayed or reused. (This
16626 also handles the case where w->cursor.vpos < nrows_scrolled
16627 after the calls to display_line, which can happen with scroll
16628 margins. See bug#1295.) */
16629 if (w->cursor.vpos < 0)
16630 {
16631 clear_glyph_matrix (w->desired_matrix);
16632 return 0;
16633 }
16634
16635 /* Scroll the display. */
16636 run.current_y = first_reusable_row->y;
16637 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16638 run.height = it.last_visible_y - run.current_y;
16639 dy = run.current_y - run.desired_y;
16640
16641 if (run.height)
16642 {
16643 update_begin (f);
16644 FRAME_RIF (f)->update_window_begin_hook (w);
16645 FRAME_RIF (f)->clear_window_mouse_face (w);
16646 FRAME_RIF (f)->scroll_run_hook (w, &run);
16647 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16648 update_end (f);
16649 }
16650
16651 /* Adjust Y positions of reused rows. */
16652 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16653 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16654 max_y = it.last_visible_y;
16655 for (row = first_reusable_row; row < first_row_to_display; ++row)
16656 {
16657 row->y -= dy;
16658 row->visible_height = row->height;
16659 if (row->y < min_y)
16660 row->visible_height -= min_y - row->y;
16661 if (row->y + row->height > max_y)
16662 row->visible_height -= row->y + row->height - max_y;
16663 if (row->fringe_bitmap_periodic_p)
16664 row->redraw_fringe_bitmaps_p = 1;
16665 }
16666
16667 /* Scroll the current matrix. */
16668 eassert (nrows_scrolled > 0);
16669 rotate_matrix (w->current_matrix,
16670 start_vpos,
16671 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16672 -nrows_scrolled);
16673
16674 /* Disable rows not reused. */
16675 for (row -= nrows_scrolled; row < bottom_row; ++row)
16676 row->enabled_p = 0;
16677
16678 /* Point may have moved to a different line, so we cannot assume that
16679 the previous cursor position is valid; locate the correct row. */
16680 if (pt_row)
16681 {
16682 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16683 row < bottom_row
16684 && PT >= MATRIX_ROW_END_CHARPOS (row)
16685 && !row->ends_at_zv_p;
16686 row++)
16687 {
16688 w->cursor.vpos++;
16689 w->cursor.y = row->y;
16690 }
16691 if (row < bottom_row)
16692 {
16693 /* Can't simply scan the row for point with
16694 bidi-reordered glyph rows. Let set_cursor_from_row
16695 figure out where to put the cursor, and if it fails,
16696 give up. */
16697 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16698 {
16699 if (!set_cursor_from_row (w, row, w->current_matrix,
16700 0, 0, 0, 0))
16701 {
16702 clear_glyph_matrix (w->desired_matrix);
16703 return 0;
16704 }
16705 }
16706 else
16707 {
16708 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16709 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16710
16711 for (; glyph < end
16712 && (!BUFFERP (glyph->object)
16713 || glyph->charpos < PT);
16714 glyph++)
16715 {
16716 w->cursor.hpos++;
16717 w->cursor.x += glyph->pixel_width;
16718 }
16719 }
16720 }
16721 }
16722
16723 /* Adjust window end. A null value of last_text_row means that
16724 the window end is in reused rows which in turn means that
16725 only its vpos can have changed. */
16726 if (last_text_row)
16727 adjust_window_ends (w, last_text_row, 0);
16728 else
16729 w->window_end_vpos -= nrows_scrolled;
16730
16731 w->window_end_valid = 0;
16732 w->desired_matrix->no_scrolling_p = 1;
16733
16734 #ifdef GLYPH_DEBUG
16735 debug_method_add (w, "try_window_reusing_current_matrix 2");
16736 #endif
16737 return 1;
16738 }
16739
16740 return 0;
16741 }
16742
16743
16744 \f
16745 /************************************************************************
16746 Window redisplay reusing current matrix when buffer has changed
16747 ************************************************************************/
16748
16749 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16750 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16751 ptrdiff_t *, ptrdiff_t *);
16752 static struct glyph_row *
16753 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16754 struct glyph_row *);
16755
16756
16757 /* Return the last row in MATRIX displaying text. If row START is
16758 non-null, start searching with that row. IT gives the dimensions
16759 of the display. Value is null if matrix is empty; otherwise it is
16760 a pointer to the row found. */
16761
16762 static struct glyph_row *
16763 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16764 struct glyph_row *start)
16765 {
16766 struct glyph_row *row, *row_found;
16767
16768 /* Set row_found to the last row in IT->w's current matrix
16769 displaying text. The loop looks funny but think of partially
16770 visible lines. */
16771 row_found = NULL;
16772 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16773 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16774 {
16775 eassert (row->enabled_p);
16776 row_found = row;
16777 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16778 break;
16779 ++row;
16780 }
16781
16782 return row_found;
16783 }
16784
16785
16786 /* Return the last row in the current matrix of W that is not affected
16787 by changes at the start of current_buffer that occurred since W's
16788 current matrix was built. Value is null if no such row exists.
16789
16790 BEG_UNCHANGED us the number of characters unchanged at the start of
16791 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16792 first changed character in current_buffer. Characters at positions <
16793 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16794 when the current matrix was built. */
16795
16796 static struct glyph_row *
16797 find_last_unchanged_at_beg_row (struct window *w)
16798 {
16799 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16800 struct glyph_row *row;
16801 struct glyph_row *row_found = NULL;
16802 int yb = window_text_bottom_y (w);
16803
16804 /* Find the last row displaying unchanged text. */
16805 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16806 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16807 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16808 ++row)
16809 {
16810 if (/* If row ends before first_changed_pos, it is unchanged,
16811 except in some case. */
16812 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16813 /* When row ends in ZV and we write at ZV it is not
16814 unchanged. */
16815 && !row->ends_at_zv_p
16816 /* When first_changed_pos is the end of a continued line,
16817 row is not unchanged because it may be no longer
16818 continued. */
16819 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16820 && (row->continued_p
16821 || row->exact_window_width_line_p))
16822 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16823 needs to be recomputed, so don't consider this row as
16824 unchanged. This happens when the last line was
16825 bidi-reordered and was killed immediately before this
16826 redisplay cycle. In that case, ROW->end stores the
16827 buffer position of the first visual-order character of
16828 the killed text, which is now beyond ZV. */
16829 && CHARPOS (row->end.pos) <= ZV)
16830 row_found = row;
16831
16832 /* Stop if last visible row. */
16833 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16834 break;
16835 }
16836
16837 return row_found;
16838 }
16839
16840
16841 /* Find the first glyph row in the current matrix of W that is not
16842 affected by changes at the end of current_buffer since the
16843 time W's current matrix was built.
16844
16845 Return in *DELTA the number of chars by which buffer positions in
16846 unchanged text at the end of current_buffer must be adjusted.
16847
16848 Return in *DELTA_BYTES the corresponding number of bytes.
16849
16850 Value is null if no such row exists, i.e. all rows are affected by
16851 changes. */
16852
16853 static struct glyph_row *
16854 find_first_unchanged_at_end_row (struct window *w,
16855 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16856 {
16857 struct glyph_row *row;
16858 struct glyph_row *row_found = NULL;
16859
16860 *delta = *delta_bytes = 0;
16861
16862 /* Display must not have been paused, otherwise the current matrix
16863 is not up to date. */
16864 eassert (w->window_end_valid);
16865
16866 /* A value of window_end_pos >= END_UNCHANGED means that the window
16867 end is in the range of changed text. If so, there is no
16868 unchanged row at the end of W's current matrix. */
16869 if (w->window_end_pos >= END_UNCHANGED)
16870 return NULL;
16871
16872 /* Set row to the last row in W's current matrix displaying text. */
16873 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16874
16875 /* If matrix is entirely empty, no unchanged row exists. */
16876 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16877 {
16878 /* The value of row is the last glyph row in the matrix having a
16879 meaningful buffer position in it. The end position of row
16880 corresponds to window_end_pos. This allows us to translate
16881 buffer positions in the current matrix to current buffer
16882 positions for characters not in changed text. */
16883 ptrdiff_t Z_old =
16884 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16885 ptrdiff_t Z_BYTE_old =
16886 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16887 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16888 struct glyph_row *first_text_row
16889 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16890
16891 *delta = Z - Z_old;
16892 *delta_bytes = Z_BYTE - Z_BYTE_old;
16893
16894 /* Set last_unchanged_pos to the buffer position of the last
16895 character in the buffer that has not been changed. Z is the
16896 index + 1 of the last character in current_buffer, i.e. by
16897 subtracting END_UNCHANGED we get the index of the last
16898 unchanged character, and we have to add BEG to get its buffer
16899 position. */
16900 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16901 last_unchanged_pos_old = last_unchanged_pos - *delta;
16902
16903 /* Search backward from ROW for a row displaying a line that
16904 starts at a minimum position >= last_unchanged_pos_old. */
16905 for (; row > first_text_row; --row)
16906 {
16907 /* This used to abort, but it can happen.
16908 It is ok to just stop the search instead here. KFS. */
16909 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16910 break;
16911
16912 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16913 row_found = row;
16914 }
16915 }
16916
16917 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16918
16919 return row_found;
16920 }
16921
16922
16923 /* Make sure that glyph rows in the current matrix of window W
16924 reference the same glyph memory as corresponding rows in the
16925 frame's frame matrix. This function is called after scrolling W's
16926 current matrix on a terminal frame in try_window_id and
16927 try_window_reusing_current_matrix. */
16928
16929 static void
16930 sync_frame_with_window_matrix_rows (struct window *w)
16931 {
16932 struct frame *f = XFRAME (w->frame);
16933 struct glyph_row *window_row, *window_row_end, *frame_row;
16934
16935 /* Preconditions: W must be a leaf window and full-width. Its frame
16936 must have a frame matrix. */
16937 eassert (BUFFERP (w->contents));
16938 eassert (WINDOW_FULL_WIDTH_P (w));
16939 eassert (!FRAME_WINDOW_P (f));
16940
16941 /* If W is a full-width window, glyph pointers in W's current matrix
16942 have, by definition, to be the same as glyph pointers in the
16943 corresponding frame matrix. Note that frame matrices have no
16944 marginal areas (see build_frame_matrix). */
16945 window_row = w->current_matrix->rows;
16946 window_row_end = window_row + w->current_matrix->nrows;
16947 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16948 while (window_row < window_row_end)
16949 {
16950 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16951 struct glyph *end = window_row->glyphs[LAST_AREA];
16952
16953 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16954 frame_row->glyphs[TEXT_AREA] = start;
16955 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16956 frame_row->glyphs[LAST_AREA] = end;
16957
16958 /* Disable frame rows whose corresponding window rows have
16959 been disabled in try_window_id. */
16960 if (!window_row->enabled_p)
16961 frame_row->enabled_p = 0;
16962
16963 ++window_row, ++frame_row;
16964 }
16965 }
16966
16967
16968 /* Find the glyph row in window W containing CHARPOS. Consider all
16969 rows between START and END (not inclusive). END null means search
16970 all rows to the end of the display area of W. Value is the row
16971 containing CHARPOS or null. */
16972
16973 struct glyph_row *
16974 row_containing_pos (struct window *w, ptrdiff_t charpos,
16975 struct glyph_row *start, struct glyph_row *end, int dy)
16976 {
16977 struct glyph_row *row = start;
16978 struct glyph_row *best_row = NULL;
16979 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16980 int last_y;
16981
16982 /* If we happen to start on a header-line, skip that. */
16983 if (row->mode_line_p)
16984 ++row;
16985
16986 if ((end && row >= end) || !row->enabled_p)
16987 return NULL;
16988
16989 last_y = window_text_bottom_y (w) - dy;
16990
16991 while (1)
16992 {
16993 /* Give up if we have gone too far. */
16994 if (end && row >= end)
16995 return NULL;
16996 /* This formerly returned if they were equal.
16997 I think that both quantities are of a "last plus one" type;
16998 if so, when they are equal, the row is within the screen. -- rms. */
16999 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17000 return NULL;
17001
17002 /* If it is in this row, return this row. */
17003 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17004 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17005 /* The end position of a row equals the start
17006 position of the next row. If CHARPOS is there, we
17007 would rather consider it displayed in the next
17008 line, except when this line ends in ZV. */
17009 && !row_for_charpos_p (row, charpos)))
17010 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17011 {
17012 struct glyph *g;
17013
17014 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17015 || (!best_row && !row->continued_p))
17016 return row;
17017 /* In bidi-reordered rows, there could be several rows whose
17018 edges surround CHARPOS, all of these rows belonging to
17019 the same continued line. We need to find the row which
17020 fits CHARPOS the best. */
17021 for (g = row->glyphs[TEXT_AREA];
17022 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17023 g++)
17024 {
17025 if (!STRINGP (g->object))
17026 {
17027 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17028 {
17029 mindif = eabs (g->charpos - charpos);
17030 best_row = row;
17031 /* Exact match always wins. */
17032 if (mindif == 0)
17033 return best_row;
17034 }
17035 }
17036 }
17037 }
17038 else if (best_row && !row->continued_p)
17039 return best_row;
17040 ++row;
17041 }
17042 }
17043
17044
17045 /* Try to redisplay window W by reusing its existing display. W's
17046 current matrix must be up to date when this function is called,
17047 i.e. window_end_valid must be nonzero.
17048
17049 Value is
17050
17051 1 if display has been updated
17052 0 if otherwise unsuccessful
17053 -1 if redisplay with same window start is known not to succeed
17054
17055 The following steps are performed:
17056
17057 1. Find the last row in the current matrix of W that is not
17058 affected by changes at the start of current_buffer. If no such row
17059 is found, give up.
17060
17061 2. Find the first row in W's current matrix that is not affected by
17062 changes at the end of current_buffer. Maybe there is no such row.
17063
17064 3. Display lines beginning with the row + 1 found in step 1 to the
17065 row found in step 2 or, if step 2 didn't find a row, to the end of
17066 the window.
17067
17068 4. If cursor is not known to appear on the window, give up.
17069
17070 5. If display stopped at the row found in step 2, scroll the
17071 display and current matrix as needed.
17072
17073 6. Maybe display some lines at the end of W, if we must. This can
17074 happen under various circumstances, like a partially visible line
17075 becoming fully visible, or because newly displayed lines are displayed
17076 in smaller font sizes.
17077
17078 7. Update W's window end information. */
17079
17080 static int
17081 try_window_id (struct window *w)
17082 {
17083 struct frame *f = XFRAME (w->frame);
17084 struct glyph_matrix *current_matrix = w->current_matrix;
17085 struct glyph_matrix *desired_matrix = w->desired_matrix;
17086 struct glyph_row *last_unchanged_at_beg_row;
17087 struct glyph_row *first_unchanged_at_end_row;
17088 struct glyph_row *row;
17089 struct glyph_row *bottom_row;
17090 int bottom_vpos;
17091 struct it it;
17092 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17093 int dvpos, dy;
17094 struct text_pos start_pos;
17095 struct run run;
17096 int first_unchanged_at_end_vpos = 0;
17097 struct glyph_row *last_text_row, *last_text_row_at_end;
17098 struct text_pos start;
17099 ptrdiff_t first_changed_charpos, last_changed_charpos;
17100
17101 #ifdef GLYPH_DEBUG
17102 if (inhibit_try_window_id)
17103 return 0;
17104 #endif
17105
17106 /* This is handy for debugging. */
17107 #if 0
17108 #define GIVE_UP(X) \
17109 do { \
17110 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17111 return 0; \
17112 } while (0)
17113 #else
17114 #define GIVE_UP(X) return 0
17115 #endif
17116
17117 SET_TEXT_POS_FROM_MARKER (start, w->start);
17118
17119 /* Don't use this for mini-windows because these can show
17120 messages and mini-buffers, and we don't handle that here. */
17121 if (MINI_WINDOW_P (w))
17122 GIVE_UP (1);
17123
17124 /* This flag is used to prevent redisplay optimizations. */
17125 if (windows_or_buffers_changed || cursor_type_changed)
17126 GIVE_UP (2);
17127
17128 /* Verify that narrowing has not changed.
17129 Also verify that we were not told to prevent redisplay optimizations.
17130 It would be nice to further
17131 reduce the number of cases where this prevents try_window_id. */
17132 if (current_buffer->clip_changed
17133 || current_buffer->prevent_redisplay_optimizations_p)
17134 GIVE_UP (3);
17135
17136 /* Window must either use window-based redisplay or be full width. */
17137 if (!FRAME_WINDOW_P (f)
17138 && (!FRAME_LINE_INS_DEL_OK (f)
17139 || !WINDOW_FULL_WIDTH_P (w)))
17140 GIVE_UP (4);
17141
17142 /* Give up if point is known NOT to appear in W. */
17143 if (PT < CHARPOS (start))
17144 GIVE_UP (5);
17145
17146 /* Another way to prevent redisplay optimizations. */
17147 if (w->last_modified == 0)
17148 GIVE_UP (6);
17149
17150 /* Verify that window is not hscrolled. */
17151 if (w->hscroll != 0)
17152 GIVE_UP (7);
17153
17154 /* Verify that display wasn't paused. */
17155 if (!w->window_end_valid)
17156 GIVE_UP (8);
17157
17158 /* Can't use this if highlighting a region because a cursor movement
17159 will do more than just set the cursor. */
17160 if (markpos_of_region () >= 0)
17161 GIVE_UP (9);
17162
17163 /* Likewise if highlighting trailing whitespace. */
17164 if (!NILP (Vshow_trailing_whitespace))
17165 GIVE_UP (11);
17166
17167 /* Likewise if showing a region. */
17168 if (w->region_showing)
17169 GIVE_UP (10);
17170
17171 /* Can't use this if overlay arrow position and/or string have
17172 changed. */
17173 if (overlay_arrows_changed_p ())
17174 GIVE_UP (12);
17175
17176 /* When word-wrap is on, adding a space to the first word of a
17177 wrapped line can change the wrap position, altering the line
17178 above it. It might be worthwhile to handle this more
17179 intelligently, but for now just redisplay from scratch. */
17180 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17181 GIVE_UP (21);
17182
17183 /* Under bidi reordering, adding or deleting a character in the
17184 beginning of a paragraph, before the first strong directional
17185 character, can change the base direction of the paragraph (unless
17186 the buffer specifies a fixed paragraph direction), which will
17187 require to redisplay the whole paragraph. It might be worthwhile
17188 to find the paragraph limits and widen the range of redisplayed
17189 lines to that, but for now just give up this optimization and
17190 redisplay from scratch. */
17191 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17192 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17193 GIVE_UP (22);
17194
17195 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17196 only if buffer has really changed. The reason is that the gap is
17197 initially at Z for freshly visited files. The code below would
17198 set end_unchanged to 0 in that case. */
17199 if (MODIFF > SAVE_MODIFF
17200 /* This seems to happen sometimes after saving a buffer. */
17201 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17202 {
17203 if (GPT - BEG < BEG_UNCHANGED)
17204 BEG_UNCHANGED = GPT - BEG;
17205 if (Z - GPT < END_UNCHANGED)
17206 END_UNCHANGED = Z - GPT;
17207 }
17208
17209 /* The position of the first and last character that has been changed. */
17210 first_changed_charpos = BEG + BEG_UNCHANGED;
17211 last_changed_charpos = Z - END_UNCHANGED;
17212
17213 /* If window starts after a line end, and the last change is in
17214 front of that newline, then changes don't affect the display.
17215 This case happens with stealth-fontification. Note that although
17216 the display is unchanged, glyph positions in the matrix have to
17217 be adjusted, of course. */
17218 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17219 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17220 && ((last_changed_charpos < CHARPOS (start)
17221 && CHARPOS (start) == BEGV)
17222 || (last_changed_charpos < CHARPOS (start) - 1
17223 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17224 {
17225 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17226 struct glyph_row *r0;
17227
17228 /* Compute how many chars/bytes have been added to or removed
17229 from the buffer. */
17230 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17231 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17232 Z_delta = Z - Z_old;
17233 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17234
17235 /* Give up if PT is not in the window. Note that it already has
17236 been checked at the start of try_window_id that PT is not in
17237 front of the window start. */
17238 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17239 GIVE_UP (13);
17240
17241 /* If window start is unchanged, we can reuse the whole matrix
17242 as is, after adjusting glyph positions. No need to compute
17243 the window end again, since its offset from Z hasn't changed. */
17244 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17245 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17246 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17247 /* PT must not be in a partially visible line. */
17248 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17249 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17250 {
17251 /* Adjust positions in the glyph matrix. */
17252 if (Z_delta || Z_delta_bytes)
17253 {
17254 struct glyph_row *r1
17255 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17256 increment_matrix_positions (w->current_matrix,
17257 MATRIX_ROW_VPOS (r0, current_matrix),
17258 MATRIX_ROW_VPOS (r1, current_matrix),
17259 Z_delta, Z_delta_bytes);
17260 }
17261
17262 /* Set the cursor. */
17263 row = row_containing_pos (w, PT, r0, NULL, 0);
17264 if (row)
17265 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17266 else
17267 emacs_abort ();
17268 return 1;
17269 }
17270 }
17271
17272 /* Handle the case that changes are all below what is displayed in
17273 the window, and that PT is in the window. This shortcut cannot
17274 be taken if ZV is visible in the window, and text has been added
17275 there that is visible in the window. */
17276 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17277 /* ZV is not visible in the window, or there are no
17278 changes at ZV, actually. */
17279 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17280 || first_changed_charpos == last_changed_charpos))
17281 {
17282 struct glyph_row *r0;
17283
17284 /* Give up if PT is not in the window. Note that it already has
17285 been checked at the start of try_window_id that PT is not in
17286 front of the window start. */
17287 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17288 GIVE_UP (14);
17289
17290 /* If window start is unchanged, we can reuse the whole matrix
17291 as is, without changing glyph positions since no text has
17292 been added/removed in front of the window end. */
17293 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17294 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17295 /* PT must not be in a partially visible line. */
17296 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17297 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17298 {
17299 /* We have to compute the window end anew since text
17300 could have been added/removed after it. */
17301 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17302 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17303
17304 /* Set the cursor. */
17305 row = row_containing_pos (w, PT, r0, NULL, 0);
17306 if (row)
17307 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17308 else
17309 emacs_abort ();
17310 return 2;
17311 }
17312 }
17313
17314 /* Give up if window start is in the changed area.
17315
17316 The condition used to read
17317
17318 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17319
17320 but why that was tested escapes me at the moment. */
17321 if (CHARPOS (start) >= first_changed_charpos
17322 && CHARPOS (start) <= last_changed_charpos)
17323 GIVE_UP (15);
17324
17325 /* Check that window start agrees with the start of the first glyph
17326 row in its current matrix. Check this after we know the window
17327 start is not in changed text, otherwise positions would not be
17328 comparable. */
17329 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17330 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17331 GIVE_UP (16);
17332
17333 /* Give up if the window ends in strings. Overlay strings
17334 at the end are difficult to handle, so don't try. */
17335 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17336 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17337 GIVE_UP (20);
17338
17339 /* Compute the position at which we have to start displaying new
17340 lines. Some of the lines at the top of the window might be
17341 reusable because they are not displaying changed text. Find the
17342 last row in W's current matrix not affected by changes at the
17343 start of current_buffer. Value is null if changes start in the
17344 first line of window. */
17345 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17346 if (last_unchanged_at_beg_row)
17347 {
17348 /* Avoid starting to display in the middle of a character, a TAB
17349 for instance. This is easier than to set up the iterator
17350 exactly, and it's not a frequent case, so the additional
17351 effort wouldn't really pay off. */
17352 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17353 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17354 && last_unchanged_at_beg_row > w->current_matrix->rows)
17355 --last_unchanged_at_beg_row;
17356
17357 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17358 GIVE_UP (17);
17359
17360 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17361 GIVE_UP (18);
17362 start_pos = it.current.pos;
17363
17364 /* Start displaying new lines in the desired matrix at the same
17365 vpos we would use in the current matrix, i.e. below
17366 last_unchanged_at_beg_row. */
17367 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17368 current_matrix);
17369 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17370 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17371
17372 eassert (it.hpos == 0 && it.current_x == 0);
17373 }
17374 else
17375 {
17376 /* There are no reusable lines at the start of the window.
17377 Start displaying in the first text line. */
17378 start_display (&it, w, start);
17379 it.vpos = it.first_vpos;
17380 start_pos = it.current.pos;
17381 }
17382
17383 /* Find the first row that is not affected by changes at the end of
17384 the buffer. Value will be null if there is no unchanged row, in
17385 which case we must redisplay to the end of the window. delta
17386 will be set to the value by which buffer positions beginning with
17387 first_unchanged_at_end_row have to be adjusted due to text
17388 changes. */
17389 first_unchanged_at_end_row
17390 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17391 IF_DEBUG (debug_delta = delta);
17392 IF_DEBUG (debug_delta_bytes = delta_bytes);
17393
17394 /* Set stop_pos to the buffer position up to which we will have to
17395 display new lines. If first_unchanged_at_end_row != NULL, this
17396 is the buffer position of the start of the line displayed in that
17397 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17398 that we don't stop at a buffer position. */
17399 stop_pos = 0;
17400 if (first_unchanged_at_end_row)
17401 {
17402 eassert (last_unchanged_at_beg_row == NULL
17403 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17404
17405 /* If this is a continuation line, move forward to the next one
17406 that isn't. Changes in lines above affect this line.
17407 Caution: this may move first_unchanged_at_end_row to a row
17408 not displaying text. */
17409 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17410 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17411 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17412 < it.last_visible_y))
17413 ++first_unchanged_at_end_row;
17414
17415 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17416 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17417 >= it.last_visible_y))
17418 first_unchanged_at_end_row = NULL;
17419 else
17420 {
17421 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17422 + delta);
17423 first_unchanged_at_end_vpos
17424 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17425 eassert (stop_pos >= Z - END_UNCHANGED);
17426 }
17427 }
17428 else if (last_unchanged_at_beg_row == NULL)
17429 GIVE_UP (19);
17430
17431
17432 #ifdef GLYPH_DEBUG
17433
17434 /* Either there is no unchanged row at the end, or the one we have
17435 now displays text. This is a necessary condition for the window
17436 end pos calculation at the end of this function. */
17437 eassert (first_unchanged_at_end_row == NULL
17438 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17439
17440 debug_last_unchanged_at_beg_vpos
17441 = (last_unchanged_at_beg_row
17442 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17443 : -1);
17444 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17445
17446 #endif /* GLYPH_DEBUG */
17447
17448
17449 /* Display new lines. Set last_text_row to the last new line
17450 displayed which has text on it, i.e. might end up as being the
17451 line where the window_end_vpos is. */
17452 w->cursor.vpos = -1;
17453 last_text_row = NULL;
17454 overlay_arrow_seen = 0;
17455 while (it.current_y < it.last_visible_y
17456 && !f->fonts_changed
17457 && (first_unchanged_at_end_row == NULL
17458 || IT_CHARPOS (it) < stop_pos))
17459 {
17460 if (display_line (&it))
17461 last_text_row = it.glyph_row - 1;
17462 }
17463
17464 if (f->fonts_changed)
17465 return -1;
17466
17467
17468 /* Compute differences in buffer positions, y-positions etc. for
17469 lines reused at the bottom of the window. Compute what we can
17470 scroll. */
17471 if (first_unchanged_at_end_row
17472 /* No lines reused because we displayed everything up to the
17473 bottom of the window. */
17474 && it.current_y < it.last_visible_y)
17475 {
17476 dvpos = (it.vpos
17477 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17478 current_matrix));
17479 dy = it.current_y - first_unchanged_at_end_row->y;
17480 run.current_y = first_unchanged_at_end_row->y;
17481 run.desired_y = run.current_y + dy;
17482 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17483 }
17484 else
17485 {
17486 delta = delta_bytes = dvpos = dy
17487 = run.current_y = run.desired_y = run.height = 0;
17488 first_unchanged_at_end_row = NULL;
17489 }
17490 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17491
17492
17493 /* Find the cursor if not already found. We have to decide whether
17494 PT will appear on this window (it sometimes doesn't, but this is
17495 not a very frequent case.) This decision has to be made before
17496 the current matrix is altered. A value of cursor.vpos < 0 means
17497 that PT is either in one of the lines beginning at
17498 first_unchanged_at_end_row or below the window. Don't care for
17499 lines that might be displayed later at the window end; as
17500 mentioned, this is not a frequent case. */
17501 if (w->cursor.vpos < 0)
17502 {
17503 /* Cursor in unchanged rows at the top? */
17504 if (PT < CHARPOS (start_pos)
17505 && last_unchanged_at_beg_row)
17506 {
17507 row = row_containing_pos (w, PT,
17508 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17509 last_unchanged_at_beg_row + 1, 0);
17510 if (row)
17511 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17512 }
17513
17514 /* Start from first_unchanged_at_end_row looking for PT. */
17515 else if (first_unchanged_at_end_row)
17516 {
17517 row = row_containing_pos (w, PT - delta,
17518 first_unchanged_at_end_row, NULL, 0);
17519 if (row)
17520 set_cursor_from_row (w, row, w->current_matrix, delta,
17521 delta_bytes, dy, dvpos);
17522 }
17523
17524 /* Give up if cursor was not found. */
17525 if (w->cursor.vpos < 0)
17526 {
17527 clear_glyph_matrix (w->desired_matrix);
17528 return -1;
17529 }
17530 }
17531
17532 /* Don't let the cursor end in the scroll margins. */
17533 {
17534 int this_scroll_margin, cursor_height;
17535 int frame_line_height = default_line_pixel_height (w);
17536 int window_total_lines
17537 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17538
17539 this_scroll_margin =
17540 max (0, min (scroll_margin, window_total_lines / 4));
17541 this_scroll_margin *= frame_line_height;
17542 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17543
17544 if ((w->cursor.y < this_scroll_margin
17545 && CHARPOS (start) > BEGV)
17546 /* Old redisplay didn't take scroll margin into account at the bottom,
17547 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17548 || (w->cursor.y + (make_cursor_line_fully_visible_p
17549 ? cursor_height + this_scroll_margin
17550 : 1)) > it.last_visible_y)
17551 {
17552 w->cursor.vpos = -1;
17553 clear_glyph_matrix (w->desired_matrix);
17554 return -1;
17555 }
17556 }
17557
17558 /* Scroll the display. Do it before changing the current matrix so
17559 that xterm.c doesn't get confused about where the cursor glyph is
17560 found. */
17561 if (dy && run.height)
17562 {
17563 update_begin (f);
17564
17565 if (FRAME_WINDOW_P (f))
17566 {
17567 FRAME_RIF (f)->update_window_begin_hook (w);
17568 FRAME_RIF (f)->clear_window_mouse_face (w);
17569 FRAME_RIF (f)->scroll_run_hook (w, &run);
17570 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17571 }
17572 else
17573 {
17574 /* Terminal frame. In this case, dvpos gives the number of
17575 lines to scroll by; dvpos < 0 means scroll up. */
17576 int from_vpos
17577 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17578 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17579 int end = (WINDOW_TOP_EDGE_LINE (w)
17580 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17581 + window_internal_height (w));
17582
17583 #if defined (HAVE_GPM) || defined (MSDOS)
17584 x_clear_window_mouse_face (w);
17585 #endif
17586 /* Perform the operation on the screen. */
17587 if (dvpos > 0)
17588 {
17589 /* Scroll last_unchanged_at_beg_row to the end of the
17590 window down dvpos lines. */
17591 set_terminal_window (f, end);
17592
17593 /* On dumb terminals delete dvpos lines at the end
17594 before inserting dvpos empty lines. */
17595 if (!FRAME_SCROLL_REGION_OK (f))
17596 ins_del_lines (f, end - dvpos, -dvpos);
17597
17598 /* Insert dvpos empty lines in front of
17599 last_unchanged_at_beg_row. */
17600 ins_del_lines (f, from, dvpos);
17601 }
17602 else if (dvpos < 0)
17603 {
17604 /* Scroll up last_unchanged_at_beg_vpos to the end of
17605 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17606 set_terminal_window (f, end);
17607
17608 /* Delete dvpos lines in front of
17609 last_unchanged_at_beg_vpos. ins_del_lines will set
17610 the cursor to the given vpos and emit |dvpos| delete
17611 line sequences. */
17612 ins_del_lines (f, from + dvpos, dvpos);
17613
17614 /* On a dumb terminal insert dvpos empty lines at the
17615 end. */
17616 if (!FRAME_SCROLL_REGION_OK (f))
17617 ins_del_lines (f, end + dvpos, -dvpos);
17618 }
17619
17620 set_terminal_window (f, 0);
17621 }
17622
17623 update_end (f);
17624 }
17625
17626 /* Shift reused rows of the current matrix to the right position.
17627 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17628 text. */
17629 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17630 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17631 if (dvpos < 0)
17632 {
17633 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17634 bottom_vpos, dvpos);
17635 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17636 bottom_vpos);
17637 }
17638 else if (dvpos > 0)
17639 {
17640 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17641 bottom_vpos, dvpos);
17642 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17643 first_unchanged_at_end_vpos + dvpos);
17644 }
17645
17646 /* For frame-based redisplay, make sure that current frame and window
17647 matrix are in sync with respect to glyph memory. */
17648 if (!FRAME_WINDOW_P (f))
17649 sync_frame_with_window_matrix_rows (w);
17650
17651 /* Adjust buffer positions in reused rows. */
17652 if (delta || delta_bytes)
17653 increment_matrix_positions (current_matrix,
17654 first_unchanged_at_end_vpos + dvpos,
17655 bottom_vpos, delta, delta_bytes);
17656
17657 /* Adjust Y positions. */
17658 if (dy)
17659 shift_glyph_matrix (w, current_matrix,
17660 first_unchanged_at_end_vpos + dvpos,
17661 bottom_vpos, dy);
17662
17663 if (first_unchanged_at_end_row)
17664 {
17665 first_unchanged_at_end_row += dvpos;
17666 if (first_unchanged_at_end_row->y >= it.last_visible_y
17667 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17668 first_unchanged_at_end_row = NULL;
17669 }
17670
17671 /* If scrolling up, there may be some lines to display at the end of
17672 the window. */
17673 last_text_row_at_end = NULL;
17674 if (dy < 0)
17675 {
17676 /* Scrolling up can leave for example a partially visible line
17677 at the end of the window to be redisplayed. */
17678 /* Set last_row to the glyph row in the current matrix where the
17679 window end line is found. It has been moved up or down in
17680 the matrix by dvpos. */
17681 int last_vpos = w->window_end_vpos + dvpos;
17682 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17683
17684 /* If last_row is the window end line, it should display text. */
17685 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17686
17687 /* If window end line was partially visible before, begin
17688 displaying at that line. Otherwise begin displaying with the
17689 line following it. */
17690 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17691 {
17692 init_to_row_start (&it, w, last_row);
17693 it.vpos = last_vpos;
17694 it.current_y = last_row->y;
17695 }
17696 else
17697 {
17698 init_to_row_end (&it, w, last_row);
17699 it.vpos = 1 + last_vpos;
17700 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17701 ++last_row;
17702 }
17703
17704 /* We may start in a continuation line. If so, we have to
17705 get the right continuation_lines_width and current_x. */
17706 it.continuation_lines_width = last_row->continuation_lines_width;
17707 it.hpos = it.current_x = 0;
17708
17709 /* Display the rest of the lines at the window end. */
17710 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17711 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17712 {
17713 /* Is it always sure that the display agrees with lines in
17714 the current matrix? I don't think so, so we mark rows
17715 displayed invalid in the current matrix by setting their
17716 enabled_p flag to zero. */
17717 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17718 if (display_line (&it))
17719 last_text_row_at_end = it.glyph_row - 1;
17720 }
17721 }
17722
17723 /* Update window_end_pos and window_end_vpos. */
17724 if (first_unchanged_at_end_row && !last_text_row_at_end)
17725 {
17726 /* Window end line if one of the preserved rows from the current
17727 matrix. Set row to the last row displaying text in current
17728 matrix starting at first_unchanged_at_end_row, after
17729 scrolling. */
17730 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17731 row = find_last_row_displaying_text (w->current_matrix, &it,
17732 first_unchanged_at_end_row);
17733 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17734 adjust_window_ends (w, row, 1);
17735 eassert (w->window_end_bytepos >= 0);
17736 IF_DEBUG (debug_method_add (w, "A"));
17737 }
17738 else if (last_text_row_at_end)
17739 {
17740 adjust_window_ends (w, last_text_row_at_end, 0);
17741 eassert (w->window_end_bytepos >= 0);
17742 IF_DEBUG (debug_method_add (w, "B"));
17743 }
17744 else if (last_text_row)
17745 {
17746 /* We have displayed either to the end of the window or at the
17747 end of the window, i.e. the last row with text is to be found
17748 in the desired matrix. */
17749 adjust_window_ends (w, last_text_row, 0);
17750 eassert (w->window_end_bytepos >= 0);
17751 }
17752 else if (first_unchanged_at_end_row == NULL
17753 && last_text_row == NULL
17754 && last_text_row_at_end == NULL)
17755 {
17756 /* Displayed to end of window, but no line containing text was
17757 displayed. Lines were deleted at the end of the window. */
17758 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17759 int vpos = w->window_end_vpos;
17760 struct glyph_row *current_row = current_matrix->rows + vpos;
17761 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17762
17763 for (row = NULL;
17764 row == NULL && vpos >= first_vpos;
17765 --vpos, --current_row, --desired_row)
17766 {
17767 if (desired_row->enabled_p)
17768 {
17769 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17770 row = desired_row;
17771 }
17772 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17773 row = current_row;
17774 }
17775
17776 eassert (row != NULL);
17777 w->window_end_vpos = vpos + 1;
17778 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17779 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17780 eassert (w->window_end_bytepos >= 0);
17781 IF_DEBUG (debug_method_add (w, "C"));
17782 }
17783 else
17784 emacs_abort ();
17785
17786 IF_DEBUG (debug_end_pos = w->window_end_pos;
17787 debug_end_vpos = w->window_end_vpos);
17788
17789 /* Record that display has not been completed. */
17790 w->window_end_valid = 0;
17791 w->desired_matrix->no_scrolling_p = 1;
17792 return 3;
17793
17794 #undef GIVE_UP
17795 }
17796
17797
17798 \f
17799 /***********************************************************************
17800 More debugging support
17801 ***********************************************************************/
17802
17803 #ifdef GLYPH_DEBUG
17804
17805 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17806 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17807 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17808
17809
17810 /* Dump the contents of glyph matrix MATRIX on stderr.
17811
17812 GLYPHS 0 means don't show glyph contents.
17813 GLYPHS 1 means show glyphs in short form
17814 GLYPHS > 1 means show glyphs in long form. */
17815
17816 void
17817 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17818 {
17819 int i;
17820 for (i = 0; i < matrix->nrows; ++i)
17821 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17822 }
17823
17824
17825 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17826 the glyph row and area where the glyph comes from. */
17827
17828 void
17829 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17830 {
17831 if (glyph->type == CHAR_GLYPH
17832 || glyph->type == GLYPHLESS_GLYPH)
17833 {
17834 fprintf (stderr,
17835 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17836 glyph - row->glyphs[TEXT_AREA],
17837 (glyph->type == CHAR_GLYPH
17838 ? 'C'
17839 : 'G'),
17840 glyph->charpos,
17841 (BUFFERP (glyph->object)
17842 ? 'B'
17843 : (STRINGP (glyph->object)
17844 ? 'S'
17845 : (INTEGERP (glyph->object)
17846 ? '0'
17847 : '-'))),
17848 glyph->pixel_width,
17849 glyph->u.ch,
17850 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17851 ? glyph->u.ch
17852 : '.'),
17853 glyph->face_id,
17854 glyph->left_box_line_p,
17855 glyph->right_box_line_p);
17856 }
17857 else if (glyph->type == STRETCH_GLYPH)
17858 {
17859 fprintf (stderr,
17860 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17861 glyph - row->glyphs[TEXT_AREA],
17862 'S',
17863 glyph->charpos,
17864 (BUFFERP (glyph->object)
17865 ? 'B'
17866 : (STRINGP (glyph->object)
17867 ? 'S'
17868 : (INTEGERP (glyph->object)
17869 ? '0'
17870 : '-'))),
17871 glyph->pixel_width,
17872 0,
17873 ' ',
17874 glyph->face_id,
17875 glyph->left_box_line_p,
17876 glyph->right_box_line_p);
17877 }
17878 else if (glyph->type == IMAGE_GLYPH)
17879 {
17880 fprintf (stderr,
17881 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17882 glyph - row->glyphs[TEXT_AREA],
17883 'I',
17884 glyph->charpos,
17885 (BUFFERP (glyph->object)
17886 ? 'B'
17887 : (STRINGP (glyph->object)
17888 ? 'S'
17889 : (INTEGERP (glyph->object)
17890 ? '0'
17891 : '-'))),
17892 glyph->pixel_width,
17893 glyph->u.img_id,
17894 '.',
17895 glyph->face_id,
17896 glyph->left_box_line_p,
17897 glyph->right_box_line_p);
17898 }
17899 else if (glyph->type == COMPOSITE_GLYPH)
17900 {
17901 fprintf (stderr,
17902 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17903 glyph - row->glyphs[TEXT_AREA],
17904 '+',
17905 glyph->charpos,
17906 (BUFFERP (glyph->object)
17907 ? 'B'
17908 : (STRINGP (glyph->object)
17909 ? 'S'
17910 : (INTEGERP (glyph->object)
17911 ? '0'
17912 : '-'))),
17913 glyph->pixel_width,
17914 glyph->u.cmp.id);
17915 if (glyph->u.cmp.automatic)
17916 fprintf (stderr,
17917 "[%d-%d]",
17918 glyph->slice.cmp.from, glyph->slice.cmp.to);
17919 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17920 glyph->face_id,
17921 glyph->left_box_line_p,
17922 glyph->right_box_line_p);
17923 }
17924 }
17925
17926
17927 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17928 GLYPHS 0 means don't show glyph contents.
17929 GLYPHS 1 means show glyphs in short form
17930 GLYPHS > 1 means show glyphs in long form. */
17931
17932 void
17933 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17934 {
17935 if (glyphs != 1)
17936 {
17937 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17938 fprintf (stderr, "==============================================================================\n");
17939
17940 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17941 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17942 vpos,
17943 MATRIX_ROW_START_CHARPOS (row),
17944 MATRIX_ROW_END_CHARPOS (row),
17945 row->used[TEXT_AREA],
17946 row->contains_overlapping_glyphs_p,
17947 row->enabled_p,
17948 row->truncated_on_left_p,
17949 row->truncated_on_right_p,
17950 row->continued_p,
17951 MATRIX_ROW_CONTINUATION_LINE_P (row),
17952 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17953 row->ends_at_zv_p,
17954 row->fill_line_p,
17955 row->ends_in_middle_of_char_p,
17956 row->starts_in_middle_of_char_p,
17957 row->mouse_face_p,
17958 row->x,
17959 row->y,
17960 row->pixel_width,
17961 row->height,
17962 row->visible_height,
17963 row->ascent,
17964 row->phys_ascent);
17965 /* The next 3 lines should align to "Start" in the header. */
17966 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17967 row->end.overlay_string_index,
17968 row->continuation_lines_width);
17969 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17970 CHARPOS (row->start.string_pos),
17971 CHARPOS (row->end.string_pos));
17972 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17973 row->end.dpvec_index);
17974 }
17975
17976 if (glyphs > 1)
17977 {
17978 int area;
17979
17980 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17981 {
17982 struct glyph *glyph = row->glyphs[area];
17983 struct glyph *glyph_end = glyph + row->used[area];
17984
17985 /* Glyph for a line end in text. */
17986 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17987 ++glyph_end;
17988
17989 if (glyph < glyph_end)
17990 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
17991
17992 for (; glyph < glyph_end; ++glyph)
17993 dump_glyph (row, glyph, area);
17994 }
17995 }
17996 else if (glyphs == 1)
17997 {
17998 int area;
17999
18000 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18001 {
18002 char *s = alloca (row->used[area] + 4);
18003 int i;
18004
18005 for (i = 0; i < row->used[area]; ++i)
18006 {
18007 struct glyph *glyph = row->glyphs[area] + i;
18008 if (i == row->used[area] - 1
18009 && area == TEXT_AREA
18010 && INTEGERP (glyph->object)
18011 && glyph->type == CHAR_GLYPH
18012 && glyph->u.ch == ' ')
18013 {
18014 strcpy (&s[i], "[\\n]");
18015 i += 4;
18016 }
18017 else if (glyph->type == CHAR_GLYPH
18018 && glyph->u.ch < 0x80
18019 && glyph->u.ch >= ' ')
18020 s[i] = glyph->u.ch;
18021 else
18022 s[i] = '.';
18023 }
18024
18025 s[i] = '\0';
18026 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18027 }
18028 }
18029 }
18030
18031
18032 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18033 Sdump_glyph_matrix, 0, 1, "p",
18034 doc: /* Dump the current matrix of the selected window to stderr.
18035 Shows contents of glyph row structures. With non-nil
18036 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18037 glyphs in short form, otherwise show glyphs in long form. */)
18038 (Lisp_Object glyphs)
18039 {
18040 struct window *w = XWINDOW (selected_window);
18041 struct buffer *buffer = XBUFFER (w->contents);
18042
18043 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18044 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18045 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18046 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18047 fprintf (stderr, "=============================================\n");
18048 dump_glyph_matrix (w->current_matrix,
18049 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18050 return Qnil;
18051 }
18052
18053
18054 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18055 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18056 (void)
18057 {
18058 struct frame *f = XFRAME (selected_frame);
18059 dump_glyph_matrix (f->current_matrix, 1);
18060 return Qnil;
18061 }
18062
18063
18064 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18065 doc: /* Dump glyph row ROW to stderr.
18066 GLYPH 0 means don't dump glyphs.
18067 GLYPH 1 means dump glyphs in short form.
18068 GLYPH > 1 or omitted means dump glyphs in long form. */)
18069 (Lisp_Object row, Lisp_Object glyphs)
18070 {
18071 struct glyph_matrix *matrix;
18072 EMACS_INT vpos;
18073
18074 CHECK_NUMBER (row);
18075 matrix = XWINDOW (selected_window)->current_matrix;
18076 vpos = XINT (row);
18077 if (vpos >= 0 && vpos < matrix->nrows)
18078 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18079 vpos,
18080 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18081 return Qnil;
18082 }
18083
18084
18085 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18086 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18087 GLYPH 0 means don't dump glyphs.
18088 GLYPH 1 means dump glyphs in short form.
18089 GLYPH > 1 or omitted means dump glyphs in long form. */)
18090 (Lisp_Object row, Lisp_Object glyphs)
18091 {
18092 struct frame *sf = SELECTED_FRAME ();
18093 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18094 EMACS_INT vpos;
18095
18096 CHECK_NUMBER (row);
18097 vpos = XINT (row);
18098 if (vpos >= 0 && vpos < m->nrows)
18099 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18100 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18101 return Qnil;
18102 }
18103
18104
18105 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18106 doc: /* Toggle tracing of redisplay.
18107 With ARG, turn tracing on if and only if ARG is positive. */)
18108 (Lisp_Object arg)
18109 {
18110 if (NILP (arg))
18111 trace_redisplay_p = !trace_redisplay_p;
18112 else
18113 {
18114 arg = Fprefix_numeric_value (arg);
18115 trace_redisplay_p = XINT (arg) > 0;
18116 }
18117
18118 return Qnil;
18119 }
18120
18121
18122 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18123 doc: /* Like `format', but print result to stderr.
18124 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18125 (ptrdiff_t nargs, Lisp_Object *args)
18126 {
18127 Lisp_Object s = Fformat (nargs, args);
18128 fprintf (stderr, "%s", SDATA (s));
18129 return Qnil;
18130 }
18131
18132 #endif /* GLYPH_DEBUG */
18133
18134
18135 \f
18136 /***********************************************************************
18137 Building Desired Matrix Rows
18138 ***********************************************************************/
18139
18140 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18141 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18142
18143 static struct glyph_row *
18144 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18145 {
18146 struct frame *f = XFRAME (WINDOW_FRAME (w));
18147 struct buffer *buffer = XBUFFER (w->contents);
18148 struct buffer *old = current_buffer;
18149 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18150 int arrow_len = SCHARS (overlay_arrow_string);
18151 const unsigned char *arrow_end = arrow_string + arrow_len;
18152 const unsigned char *p;
18153 struct it it;
18154 bool multibyte_p;
18155 int n_glyphs_before;
18156
18157 set_buffer_temp (buffer);
18158 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18159 it.glyph_row->used[TEXT_AREA] = 0;
18160 SET_TEXT_POS (it.position, 0, 0);
18161
18162 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18163 p = arrow_string;
18164 while (p < arrow_end)
18165 {
18166 Lisp_Object face, ilisp;
18167
18168 /* Get the next character. */
18169 if (multibyte_p)
18170 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18171 else
18172 {
18173 it.c = it.char_to_display = *p, it.len = 1;
18174 if (! ASCII_CHAR_P (it.c))
18175 it.char_to_display = BYTE8_TO_CHAR (it.c);
18176 }
18177 p += it.len;
18178
18179 /* Get its face. */
18180 ilisp = make_number (p - arrow_string);
18181 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18182 it.face_id = compute_char_face (f, it.char_to_display, face);
18183
18184 /* Compute its width, get its glyphs. */
18185 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18186 SET_TEXT_POS (it.position, -1, -1);
18187 PRODUCE_GLYPHS (&it);
18188
18189 /* If this character doesn't fit any more in the line, we have
18190 to remove some glyphs. */
18191 if (it.current_x > it.last_visible_x)
18192 {
18193 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18194 break;
18195 }
18196 }
18197
18198 set_buffer_temp (old);
18199 return it.glyph_row;
18200 }
18201
18202
18203 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18204 glyphs to insert is determined by produce_special_glyphs. */
18205
18206 static void
18207 insert_left_trunc_glyphs (struct it *it)
18208 {
18209 struct it truncate_it;
18210 struct glyph *from, *end, *to, *toend;
18211
18212 eassert (!FRAME_WINDOW_P (it->f)
18213 || (!it->glyph_row->reversed_p
18214 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18215 || (it->glyph_row->reversed_p
18216 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18217
18218 /* Get the truncation glyphs. */
18219 truncate_it = *it;
18220 truncate_it.current_x = 0;
18221 truncate_it.face_id = DEFAULT_FACE_ID;
18222 truncate_it.glyph_row = &scratch_glyph_row;
18223 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18224 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18225 truncate_it.object = make_number (0);
18226 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18227
18228 /* Overwrite glyphs from IT with truncation glyphs. */
18229 if (!it->glyph_row->reversed_p)
18230 {
18231 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18232
18233 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18234 end = from + tused;
18235 to = it->glyph_row->glyphs[TEXT_AREA];
18236 toend = to + it->glyph_row->used[TEXT_AREA];
18237 if (FRAME_WINDOW_P (it->f))
18238 {
18239 /* On GUI frames, when variable-size fonts are displayed,
18240 the truncation glyphs may need more pixels than the row's
18241 glyphs they overwrite. We overwrite more glyphs to free
18242 enough screen real estate, and enlarge the stretch glyph
18243 on the right (see display_line), if there is one, to
18244 preserve the screen position of the truncation glyphs on
18245 the right. */
18246 int w = 0;
18247 struct glyph *g = to;
18248 short used;
18249
18250 /* The first glyph could be partially visible, in which case
18251 it->glyph_row->x will be negative. But we want the left
18252 truncation glyphs to be aligned at the left margin of the
18253 window, so we override the x coordinate at which the row
18254 will begin. */
18255 it->glyph_row->x = 0;
18256 while (g < toend && w < it->truncation_pixel_width)
18257 {
18258 w += g->pixel_width;
18259 ++g;
18260 }
18261 if (g - to - tused > 0)
18262 {
18263 memmove (to + tused, g, (toend - g) * sizeof(*g));
18264 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18265 }
18266 used = it->glyph_row->used[TEXT_AREA];
18267 if (it->glyph_row->truncated_on_right_p
18268 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18269 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18270 == STRETCH_GLYPH)
18271 {
18272 int extra = w - it->truncation_pixel_width;
18273
18274 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18275 }
18276 }
18277
18278 while (from < end)
18279 *to++ = *from++;
18280
18281 /* There may be padding glyphs left over. Overwrite them too. */
18282 if (!FRAME_WINDOW_P (it->f))
18283 {
18284 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18285 {
18286 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18287 while (from < end)
18288 *to++ = *from++;
18289 }
18290 }
18291
18292 if (to > toend)
18293 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18294 }
18295 else
18296 {
18297 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18298
18299 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18300 that back to front. */
18301 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18302 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18303 toend = it->glyph_row->glyphs[TEXT_AREA];
18304 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18305 if (FRAME_WINDOW_P (it->f))
18306 {
18307 int w = 0;
18308 struct glyph *g = to;
18309
18310 while (g >= toend && w < it->truncation_pixel_width)
18311 {
18312 w += g->pixel_width;
18313 --g;
18314 }
18315 if (to - g - tused > 0)
18316 to = g + tused;
18317 if (it->glyph_row->truncated_on_right_p
18318 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18319 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18320 {
18321 int extra = w - it->truncation_pixel_width;
18322
18323 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18324 }
18325 }
18326
18327 while (from >= end && to >= toend)
18328 *to-- = *from--;
18329 if (!FRAME_WINDOW_P (it->f))
18330 {
18331 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18332 {
18333 from =
18334 truncate_it.glyph_row->glyphs[TEXT_AREA]
18335 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18336 while (from >= end && to >= toend)
18337 *to-- = *from--;
18338 }
18339 }
18340 if (from >= end)
18341 {
18342 /* Need to free some room before prepending additional
18343 glyphs. */
18344 int move_by = from - end + 1;
18345 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18346 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18347
18348 for ( ; g >= g0; g--)
18349 g[move_by] = *g;
18350 while (from >= end)
18351 *to-- = *from--;
18352 it->glyph_row->used[TEXT_AREA] += move_by;
18353 }
18354 }
18355 }
18356
18357 /* Compute the hash code for ROW. */
18358 unsigned
18359 row_hash (struct glyph_row *row)
18360 {
18361 int area, k;
18362 unsigned hashval = 0;
18363
18364 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18365 for (k = 0; k < row->used[area]; ++k)
18366 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18367 + row->glyphs[area][k].u.val
18368 + row->glyphs[area][k].face_id
18369 + row->glyphs[area][k].padding_p
18370 + (row->glyphs[area][k].type << 2));
18371
18372 return hashval;
18373 }
18374
18375 /* Compute the pixel height and width of IT->glyph_row.
18376
18377 Most of the time, ascent and height of a display line will be equal
18378 to the max_ascent and max_height values of the display iterator
18379 structure. This is not the case if
18380
18381 1. We hit ZV without displaying anything. In this case, max_ascent
18382 and max_height will be zero.
18383
18384 2. We have some glyphs that don't contribute to the line height.
18385 (The glyph row flag contributes_to_line_height_p is for future
18386 pixmap extensions).
18387
18388 The first case is easily covered by using default values because in
18389 these cases, the line height does not really matter, except that it
18390 must not be zero. */
18391
18392 static void
18393 compute_line_metrics (struct it *it)
18394 {
18395 struct glyph_row *row = it->glyph_row;
18396
18397 if (FRAME_WINDOW_P (it->f))
18398 {
18399 int i, min_y, max_y;
18400
18401 /* The line may consist of one space only, that was added to
18402 place the cursor on it. If so, the row's height hasn't been
18403 computed yet. */
18404 if (row->height == 0)
18405 {
18406 if (it->max_ascent + it->max_descent == 0)
18407 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18408 row->ascent = it->max_ascent;
18409 row->height = it->max_ascent + it->max_descent;
18410 row->phys_ascent = it->max_phys_ascent;
18411 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18412 row->extra_line_spacing = it->max_extra_line_spacing;
18413 }
18414
18415 /* Compute the width of this line. */
18416 row->pixel_width = row->x;
18417 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18418 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18419
18420 eassert (row->pixel_width >= 0);
18421 eassert (row->ascent >= 0 && row->height > 0);
18422
18423 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18424 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18425
18426 /* If first line's physical ascent is larger than its logical
18427 ascent, use the physical ascent, and make the row taller.
18428 This makes accented characters fully visible. */
18429 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18430 && row->phys_ascent > row->ascent)
18431 {
18432 row->height += row->phys_ascent - row->ascent;
18433 row->ascent = row->phys_ascent;
18434 }
18435
18436 /* Compute how much of the line is visible. */
18437 row->visible_height = row->height;
18438
18439 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18440 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18441
18442 if (row->y < min_y)
18443 row->visible_height -= min_y - row->y;
18444 if (row->y + row->height > max_y)
18445 row->visible_height -= row->y + row->height - max_y;
18446 }
18447 else
18448 {
18449 row->pixel_width = row->used[TEXT_AREA];
18450 if (row->continued_p)
18451 row->pixel_width -= it->continuation_pixel_width;
18452 else if (row->truncated_on_right_p)
18453 row->pixel_width -= it->truncation_pixel_width;
18454 row->ascent = row->phys_ascent = 0;
18455 row->height = row->phys_height = row->visible_height = 1;
18456 row->extra_line_spacing = 0;
18457 }
18458
18459 /* Compute a hash code for this row. */
18460 row->hash = row_hash (row);
18461
18462 it->max_ascent = it->max_descent = 0;
18463 it->max_phys_ascent = it->max_phys_descent = 0;
18464 }
18465
18466
18467 /* Append one space to the glyph row of iterator IT if doing a
18468 window-based redisplay. The space has the same face as
18469 IT->face_id. Value is non-zero if a space was added.
18470
18471 This function is called to make sure that there is always one glyph
18472 at the end of a glyph row that the cursor can be set on under
18473 window-systems. (If there weren't such a glyph we would not know
18474 how wide and tall a box cursor should be displayed).
18475
18476 At the same time this space let's a nicely handle clearing to the
18477 end of the line if the row ends in italic text. */
18478
18479 static int
18480 append_space_for_newline (struct it *it, int default_face_p)
18481 {
18482 if (FRAME_WINDOW_P (it->f))
18483 {
18484 int n = it->glyph_row->used[TEXT_AREA];
18485
18486 if (it->glyph_row->glyphs[TEXT_AREA] + n
18487 < it->glyph_row->glyphs[1 + TEXT_AREA])
18488 {
18489 /* Save some values that must not be changed.
18490 Must save IT->c and IT->len because otherwise
18491 ITERATOR_AT_END_P wouldn't work anymore after
18492 append_space_for_newline has been called. */
18493 enum display_element_type saved_what = it->what;
18494 int saved_c = it->c, saved_len = it->len;
18495 int saved_char_to_display = it->char_to_display;
18496 int saved_x = it->current_x;
18497 int saved_face_id = it->face_id;
18498 int saved_box_end = it->end_of_box_run_p;
18499 struct text_pos saved_pos;
18500 Lisp_Object saved_object;
18501 struct face *face;
18502
18503 saved_object = it->object;
18504 saved_pos = it->position;
18505
18506 it->what = IT_CHARACTER;
18507 memset (&it->position, 0, sizeof it->position);
18508 it->object = make_number (0);
18509 it->c = it->char_to_display = ' ';
18510 it->len = 1;
18511
18512 /* If the default face was remapped, be sure to use the
18513 remapped face for the appended newline. */
18514 if (default_face_p)
18515 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18516 else if (it->face_before_selective_p)
18517 it->face_id = it->saved_face_id;
18518 face = FACE_FROM_ID (it->f, it->face_id);
18519 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18520 /* In R2L rows, we will prepend a stretch glyph that will
18521 have the end_of_box_run_p flag set for it, so there's no
18522 need for the appended newline glyph to have that flag
18523 set. */
18524 if (it->glyph_row->reversed_p
18525 /* But if the appended newline glyph goes all the way to
18526 the end of the row, there will be no stretch glyph,
18527 so leave the box flag set. */
18528 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18529 it->end_of_box_run_p = 0;
18530
18531 PRODUCE_GLYPHS (it);
18532
18533 it->override_ascent = -1;
18534 it->constrain_row_ascent_descent_p = 0;
18535 it->current_x = saved_x;
18536 it->object = saved_object;
18537 it->position = saved_pos;
18538 it->what = saved_what;
18539 it->face_id = saved_face_id;
18540 it->len = saved_len;
18541 it->c = saved_c;
18542 it->char_to_display = saved_char_to_display;
18543 it->end_of_box_run_p = saved_box_end;
18544 return 1;
18545 }
18546 }
18547
18548 return 0;
18549 }
18550
18551
18552 /* Extend the face of the last glyph in the text area of IT->glyph_row
18553 to the end of the display line. Called from display_line. If the
18554 glyph row is empty, add a space glyph to it so that we know the
18555 face to draw. Set the glyph row flag fill_line_p. If the glyph
18556 row is R2L, prepend a stretch glyph to cover the empty space to the
18557 left of the leftmost glyph. */
18558
18559 static void
18560 extend_face_to_end_of_line (struct it *it)
18561 {
18562 struct face *face, *default_face;
18563 struct frame *f = it->f;
18564
18565 /* If line is already filled, do nothing. Non window-system frames
18566 get a grace of one more ``pixel'' because their characters are
18567 1-``pixel'' wide, so they hit the equality too early. This grace
18568 is needed only for R2L rows that are not continued, to produce
18569 one extra blank where we could display the cursor. */
18570 if (it->current_x >= it->last_visible_x
18571 + (!FRAME_WINDOW_P (f)
18572 && it->glyph_row->reversed_p
18573 && !it->glyph_row->continued_p))
18574 return;
18575
18576 /* The default face, possibly remapped. */
18577 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18578
18579 /* Face extension extends the background and box of IT->face_id
18580 to the end of the line. If the background equals the background
18581 of the frame, we don't have to do anything. */
18582 if (it->face_before_selective_p)
18583 face = FACE_FROM_ID (f, it->saved_face_id);
18584 else
18585 face = FACE_FROM_ID (f, it->face_id);
18586
18587 if (FRAME_WINDOW_P (f)
18588 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18589 && face->box == FACE_NO_BOX
18590 && face->background == FRAME_BACKGROUND_PIXEL (f)
18591 && !face->stipple
18592 && !it->glyph_row->reversed_p)
18593 return;
18594
18595 /* Set the glyph row flag indicating that the face of the last glyph
18596 in the text area has to be drawn to the end of the text area. */
18597 it->glyph_row->fill_line_p = 1;
18598
18599 /* If current character of IT is not ASCII, make sure we have the
18600 ASCII face. This will be automatically undone the next time
18601 get_next_display_element returns a multibyte character. Note
18602 that the character will always be single byte in unibyte
18603 text. */
18604 if (!ASCII_CHAR_P (it->c))
18605 {
18606 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18607 }
18608
18609 if (FRAME_WINDOW_P (f))
18610 {
18611 /* If the row is empty, add a space with the current face of IT,
18612 so that we know which face to draw. */
18613 if (it->glyph_row->used[TEXT_AREA] == 0)
18614 {
18615 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18616 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18617 it->glyph_row->used[TEXT_AREA] = 1;
18618 }
18619 #ifdef HAVE_WINDOW_SYSTEM
18620 if (it->glyph_row->reversed_p)
18621 {
18622 /* Prepend a stretch glyph to the row, such that the
18623 rightmost glyph will be drawn flushed all the way to the
18624 right margin of the window. The stretch glyph that will
18625 occupy the empty space, if any, to the left of the
18626 glyphs. */
18627 struct font *font = face->font ? face->font : FRAME_FONT (f);
18628 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18629 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18630 struct glyph *g;
18631 int row_width, stretch_ascent, stretch_width;
18632 struct text_pos saved_pos;
18633 int saved_face_id, saved_avoid_cursor, saved_box_start;
18634
18635 for (row_width = 0, g = row_start; g < row_end; g++)
18636 row_width += g->pixel_width;
18637 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18638 if (stretch_width > 0)
18639 {
18640 stretch_ascent =
18641 (((it->ascent + it->descent)
18642 * FONT_BASE (font)) / FONT_HEIGHT (font));
18643 saved_pos = it->position;
18644 memset (&it->position, 0, sizeof it->position);
18645 saved_avoid_cursor = it->avoid_cursor_p;
18646 it->avoid_cursor_p = 1;
18647 saved_face_id = it->face_id;
18648 saved_box_start = it->start_of_box_run_p;
18649 /* The last row's stretch glyph should get the default
18650 face, to avoid painting the rest of the window with
18651 the region face, if the region ends at ZV. */
18652 if (it->glyph_row->ends_at_zv_p)
18653 it->face_id = default_face->id;
18654 else
18655 it->face_id = face->id;
18656 it->start_of_box_run_p = 0;
18657 append_stretch_glyph (it, make_number (0), stretch_width,
18658 it->ascent + it->descent, stretch_ascent);
18659 it->position = saved_pos;
18660 it->avoid_cursor_p = saved_avoid_cursor;
18661 it->face_id = saved_face_id;
18662 it->start_of_box_run_p = saved_box_start;
18663 }
18664 }
18665 #endif /* HAVE_WINDOW_SYSTEM */
18666 }
18667 else
18668 {
18669 /* Save some values that must not be changed. */
18670 int saved_x = it->current_x;
18671 struct text_pos saved_pos;
18672 Lisp_Object saved_object;
18673 enum display_element_type saved_what = it->what;
18674 int saved_face_id = it->face_id;
18675
18676 saved_object = it->object;
18677 saved_pos = it->position;
18678
18679 it->what = IT_CHARACTER;
18680 memset (&it->position, 0, sizeof it->position);
18681 it->object = make_number (0);
18682 it->c = it->char_to_display = ' ';
18683 it->len = 1;
18684 /* The last row's blank glyphs should get the default face, to
18685 avoid painting the rest of the window with the region face,
18686 if the region ends at ZV. */
18687 if (it->glyph_row->ends_at_zv_p)
18688 it->face_id = default_face->id;
18689 else
18690 it->face_id = face->id;
18691
18692 PRODUCE_GLYPHS (it);
18693
18694 while (it->current_x <= it->last_visible_x)
18695 PRODUCE_GLYPHS (it);
18696
18697 /* Don't count these blanks really. It would let us insert a left
18698 truncation glyph below and make us set the cursor on them, maybe. */
18699 it->current_x = saved_x;
18700 it->object = saved_object;
18701 it->position = saved_pos;
18702 it->what = saved_what;
18703 it->face_id = saved_face_id;
18704 }
18705 }
18706
18707
18708 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18709 trailing whitespace. */
18710
18711 static int
18712 trailing_whitespace_p (ptrdiff_t charpos)
18713 {
18714 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18715 int c = 0;
18716
18717 while (bytepos < ZV_BYTE
18718 && (c = FETCH_CHAR (bytepos),
18719 c == ' ' || c == '\t'))
18720 ++bytepos;
18721
18722 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18723 {
18724 if (bytepos != PT_BYTE)
18725 return 1;
18726 }
18727 return 0;
18728 }
18729
18730
18731 /* Highlight trailing whitespace, if any, in ROW. */
18732
18733 static void
18734 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18735 {
18736 int used = row->used[TEXT_AREA];
18737
18738 if (used)
18739 {
18740 struct glyph *start = row->glyphs[TEXT_AREA];
18741 struct glyph *glyph = start + used - 1;
18742
18743 if (row->reversed_p)
18744 {
18745 /* Right-to-left rows need to be processed in the opposite
18746 direction, so swap the edge pointers. */
18747 glyph = start;
18748 start = row->glyphs[TEXT_AREA] + used - 1;
18749 }
18750
18751 /* Skip over glyphs inserted to display the cursor at the
18752 end of a line, for extending the face of the last glyph
18753 to the end of the line on terminals, and for truncation
18754 and continuation glyphs. */
18755 if (!row->reversed_p)
18756 {
18757 while (glyph >= start
18758 && glyph->type == CHAR_GLYPH
18759 && INTEGERP (glyph->object))
18760 --glyph;
18761 }
18762 else
18763 {
18764 while (glyph <= start
18765 && glyph->type == CHAR_GLYPH
18766 && INTEGERP (glyph->object))
18767 ++glyph;
18768 }
18769
18770 /* If last glyph is a space or stretch, and it's trailing
18771 whitespace, set the face of all trailing whitespace glyphs in
18772 IT->glyph_row to `trailing-whitespace'. */
18773 if ((row->reversed_p ? glyph <= start : glyph >= start)
18774 && BUFFERP (glyph->object)
18775 && (glyph->type == STRETCH_GLYPH
18776 || (glyph->type == CHAR_GLYPH
18777 && glyph->u.ch == ' '))
18778 && trailing_whitespace_p (glyph->charpos))
18779 {
18780 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18781 if (face_id < 0)
18782 return;
18783
18784 if (!row->reversed_p)
18785 {
18786 while (glyph >= start
18787 && BUFFERP (glyph->object)
18788 && (glyph->type == STRETCH_GLYPH
18789 || (glyph->type == CHAR_GLYPH
18790 && glyph->u.ch == ' ')))
18791 (glyph--)->face_id = face_id;
18792 }
18793 else
18794 {
18795 while (glyph <= start
18796 && BUFFERP (glyph->object)
18797 && (glyph->type == STRETCH_GLYPH
18798 || (glyph->type == CHAR_GLYPH
18799 && glyph->u.ch == ' ')))
18800 (glyph++)->face_id = face_id;
18801 }
18802 }
18803 }
18804 }
18805
18806
18807 /* Value is non-zero if glyph row ROW should be
18808 considered to hold the buffer position CHARPOS. */
18809
18810 static int
18811 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18812 {
18813 int result = 1;
18814
18815 if (charpos == CHARPOS (row->end.pos)
18816 || charpos == MATRIX_ROW_END_CHARPOS (row))
18817 {
18818 /* Suppose the row ends on a string.
18819 Unless the row is continued, that means it ends on a newline
18820 in the string. If it's anything other than a display string
18821 (e.g., a before-string from an overlay), we don't want the
18822 cursor there. (This heuristic seems to give the optimal
18823 behavior for the various types of multi-line strings.)
18824 One exception: if the string has `cursor' property on one of
18825 its characters, we _do_ want the cursor there. */
18826 if (CHARPOS (row->end.string_pos) >= 0)
18827 {
18828 if (row->continued_p)
18829 result = 1;
18830 else
18831 {
18832 /* Check for `display' property. */
18833 struct glyph *beg = row->glyphs[TEXT_AREA];
18834 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18835 struct glyph *glyph;
18836
18837 result = 0;
18838 for (glyph = end; glyph >= beg; --glyph)
18839 if (STRINGP (glyph->object))
18840 {
18841 Lisp_Object prop
18842 = Fget_char_property (make_number (charpos),
18843 Qdisplay, Qnil);
18844 result =
18845 (!NILP (prop)
18846 && display_prop_string_p (prop, glyph->object));
18847 /* If there's a `cursor' property on one of the
18848 string's characters, this row is a cursor row,
18849 even though this is not a display string. */
18850 if (!result)
18851 {
18852 Lisp_Object s = glyph->object;
18853
18854 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18855 {
18856 ptrdiff_t gpos = glyph->charpos;
18857
18858 if (!NILP (Fget_char_property (make_number (gpos),
18859 Qcursor, s)))
18860 {
18861 result = 1;
18862 break;
18863 }
18864 }
18865 }
18866 break;
18867 }
18868 }
18869 }
18870 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18871 {
18872 /* If the row ends in middle of a real character,
18873 and the line is continued, we want the cursor here.
18874 That's because CHARPOS (ROW->end.pos) would equal
18875 PT if PT is before the character. */
18876 if (!row->ends_in_ellipsis_p)
18877 result = row->continued_p;
18878 else
18879 /* If the row ends in an ellipsis, then
18880 CHARPOS (ROW->end.pos) will equal point after the
18881 invisible text. We want that position to be displayed
18882 after the ellipsis. */
18883 result = 0;
18884 }
18885 /* If the row ends at ZV, display the cursor at the end of that
18886 row instead of at the start of the row below. */
18887 else if (row->ends_at_zv_p)
18888 result = 1;
18889 else
18890 result = 0;
18891 }
18892
18893 return result;
18894 }
18895
18896 /* Value is non-zero if glyph row ROW should be
18897 used to hold the cursor. */
18898
18899 static int
18900 cursor_row_p (struct glyph_row *row)
18901 {
18902 return row_for_charpos_p (row, PT);
18903 }
18904
18905 \f
18906
18907 /* Push the property PROP so that it will be rendered at the current
18908 position in IT. Return 1 if PROP was successfully pushed, 0
18909 otherwise. Called from handle_line_prefix to handle the
18910 `line-prefix' and `wrap-prefix' properties. */
18911
18912 static int
18913 push_prefix_prop (struct it *it, Lisp_Object prop)
18914 {
18915 struct text_pos pos =
18916 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18917
18918 eassert (it->method == GET_FROM_BUFFER
18919 || it->method == GET_FROM_DISPLAY_VECTOR
18920 || it->method == GET_FROM_STRING);
18921
18922 /* We need to save the current buffer/string position, so it will be
18923 restored by pop_it, because iterate_out_of_display_property
18924 depends on that being set correctly, but some situations leave
18925 it->position not yet set when this function is called. */
18926 push_it (it, &pos);
18927
18928 if (STRINGP (prop))
18929 {
18930 if (SCHARS (prop) == 0)
18931 {
18932 pop_it (it);
18933 return 0;
18934 }
18935
18936 it->string = prop;
18937 it->string_from_prefix_prop_p = 1;
18938 it->multibyte_p = STRING_MULTIBYTE (it->string);
18939 it->current.overlay_string_index = -1;
18940 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18941 it->end_charpos = it->string_nchars = SCHARS (it->string);
18942 it->method = GET_FROM_STRING;
18943 it->stop_charpos = 0;
18944 it->prev_stop = 0;
18945 it->base_level_stop = 0;
18946
18947 /* Force paragraph direction to be that of the parent
18948 buffer/string. */
18949 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18950 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18951 else
18952 it->paragraph_embedding = L2R;
18953
18954 /* Set up the bidi iterator for this display string. */
18955 if (it->bidi_p)
18956 {
18957 it->bidi_it.string.lstring = it->string;
18958 it->bidi_it.string.s = NULL;
18959 it->bidi_it.string.schars = it->end_charpos;
18960 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18961 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18962 it->bidi_it.string.unibyte = !it->multibyte_p;
18963 it->bidi_it.w = it->w;
18964 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18965 }
18966 }
18967 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18968 {
18969 it->method = GET_FROM_STRETCH;
18970 it->object = prop;
18971 }
18972 #ifdef HAVE_WINDOW_SYSTEM
18973 else if (IMAGEP (prop))
18974 {
18975 it->what = IT_IMAGE;
18976 it->image_id = lookup_image (it->f, prop);
18977 it->method = GET_FROM_IMAGE;
18978 }
18979 #endif /* HAVE_WINDOW_SYSTEM */
18980 else
18981 {
18982 pop_it (it); /* bogus display property, give up */
18983 return 0;
18984 }
18985
18986 return 1;
18987 }
18988
18989 /* Return the character-property PROP at the current position in IT. */
18990
18991 static Lisp_Object
18992 get_it_property (struct it *it, Lisp_Object prop)
18993 {
18994 Lisp_Object position, object = it->object;
18995
18996 if (STRINGP (object))
18997 position = make_number (IT_STRING_CHARPOS (*it));
18998 else if (BUFFERP (object))
18999 {
19000 position = make_number (IT_CHARPOS (*it));
19001 object = it->window;
19002 }
19003 else
19004 return Qnil;
19005
19006 return Fget_char_property (position, prop, object);
19007 }
19008
19009 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19010
19011 static void
19012 handle_line_prefix (struct it *it)
19013 {
19014 Lisp_Object prefix;
19015
19016 if (it->continuation_lines_width > 0)
19017 {
19018 prefix = get_it_property (it, Qwrap_prefix);
19019 if (NILP (prefix))
19020 prefix = Vwrap_prefix;
19021 }
19022 else
19023 {
19024 prefix = get_it_property (it, Qline_prefix);
19025 if (NILP (prefix))
19026 prefix = Vline_prefix;
19027 }
19028 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19029 {
19030 /* If the prefix is wider than the window, and we try to wrap
19031 it, it would acquire its own wrap prefix, and so on till the
19032 iterator stack overflows. So, don't wrap the prefix. */
19033 it->line_wrap = TRUNCATE;
19034 it->avoid_cursor_p = 1;
19035 }
19036 }
19037
19038 \f
19039
19040 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19041 only for R2L lines from display_line and display_string, when they
19042 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19043 the line/string needs to be continued on the next glyph row. */
19044 static void
19045 unproduce_glyphs (struct it *it, int n)
19046 {
19047 struct glyph *glyph, *end;
19048
19049 eassert (it->glyph_row);
19050 eassert (it->glyph_row->reversed_p);
19051 eassert (it->area == TEXT_AREA);
19052 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19053
19054 if (n > it->glyph_row->used[TEXT_AREA])
19055 n = it->glyph_row->used[TEXT_AREA];
19056 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19057 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19058 for ( ; glyph < end; glyph++)
19059 glyph[-n] = *glyph;
19060 }
19061
19062 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19063 and ROW->maxpos. */
19064 static void
19065 find_row_edges (struct it *it, struct glyph_row *row,
19066 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19067 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19068 {
19069 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19070 lines' rows is implemented for bidi-reordered rows. */
19071
19072 /* ROW->minpos is the value of min_pos, the minimal buffer position
19073 we have in ROW, or ROW->start.pos if that is smaller. */
19074 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19075 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19076 else
19077 /* We didn't find buffer positions smaller than ROW->start, or
19078 didn't find _any_ valid buffer positions in any of the glyphs,
19079 so we must trust the iterator's computed positions. */
19080 row->minpos = row->start.pos;
19081 if (max_pos <= 0)
19082 {
19083 max_pos = CHARPOS (it->current.pos);
19084 max_bpos = BYTEPOS (it->current.pos);
19085 }
19086
19087 /* Here are the various use-cases for ending the row, and the
19088 corresponding values for ROW->maxpos:
19089
19090 Line ends in a newline from buffer eol_pos + 1
19091 Line is continued from buffer max_pos + 1
19092 Line is truncated on right it->current.pos
19093 Line ends in a newline from string max_pos + 1(*)
19094 (*) + 1 only when line ends in a forward scan
19095 Line is continued from string max_pos
19096 Line is continued from display vector max_pos
19097 Line is entirely from a string min_pos == max_pos
19098 Line is entirely from a display vector min_pos == max_pos
19099 Line that ends at ZV ZV
19100
19101 If you discover other use-cases, please add them here as
19102 appropriate. */
19103 if (row->ends_at_zv_p)
19104 row->maxpos = it->current.pos;
19105 else if (row->used[TEXT_AREA])
19106 {
19107 int seen_this_string = 0;
19108 struct glyph_row *r1 = row - 1;
19109
19110 /* Did we see the same display string on the previous row? */
19111 if (STRINGP (it->object)
19112 /* this is not the first row */
19113 && row > it->w->desired_matrix->rows
19114 /* previous row is not the header line */
19115 && !r1->mode_line_p
19116 /* previous row also ends in a newline from a string */
19117 && r1->ends_in_newline_from_string_p)
19118 {
19119 struct glyph *start, *end;
19120
19121 /* Search for the last glyph of the previous row that came
19122 from buffer or string. Depending on whether the row is
19123 L2R or R2L, we need to process it front to back or the
19124 other way round. */
19125 if (!r1->reversed_p)
19126 {
19127 start = r1->glyphs[TEXT_AREA];
19128 end = start + r1->used[TEXT_AREA];
19129 /* Glyphs inserted by redisplay have an integer (zero)
19130 as their object. */
19131 while (end > start
19132 && INTEGERP ((end - 1)->object)
19133 && (end - 1)->charpos <= 0)
19134 --end;
19135 if (end > start)
19136 {
19137 if (EQ ((end - 1)->object, it->object))
19138 seen_this_string = 1;
19139 }
19140 else
19141 /* If all the glyphs of the previous row were inserted
19142 by redisplay, it means the previous row was
19143 produced from a single newline, which is only
19144 possible if that newline came from the same string
19145 as the one which produced this ROW. */
19146 seen_this_string = 1;
19147 }
19148 else
19149 {
19150 end = r1->glyphs[TEXT_AREA] - 1;
19151 start = end + r1->used[TEXT_AREA];
19152 while (end < start
19153 && INTEGERP ((end + 1)->object)
19154 && (end + 1)->charpos <= 0)
19155 ++end;
19156 if (end < start)
19157 {
19158 if (EQ ((end + 1)->object, it->object))
19159 seen_this_string = 1;
19160 }
19161 else
19162 seen_this_string = 1;
19163 }
19164 }
19165 /* Take note of each display string that covers a newline only
19166 once, the first time we see it. This is for when a display
19167 string includes more than one newline in it. */
19168 if (row->ends_in_newline_from_string_p && !seen_this_string)
19169 {
19170 /* If we were scanning the buffer forward when we displayed
19171 the string, we want to account for at least one buffer
19172 position that belongs to this row (position covered by
19173 the display string), so that cursor positioning will
19174 consider this row as a candidate when point is at the end
19175 of the visual line represented by this row. This is not
19176 required when scanning back, because max_pos will already
19177 have a much larger value. */
19178 if (CHARPOS (row->end.pos) > max_pos)
19179 INC_BOTH (max_pos, max_bpos);
19180 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19181 }
19182 else if (CHARPOS (it->eol_pos) > 0)
19183 SET_TEXT_POS (row->maxpos,
19184 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19185 else if (row->continued_p)
19186 {
19187 /* If max_pos is different from IT's current position, it
19188 means IT->method does not belong to the display element
19189 at max_pos. However, it also means that the display
19190 element at max_pos was displayed in its entirety on this
19191 line, which is equivalent to saying that the next line
19192 starts at the next buffer position. */
19193 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19194 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19195 else
19196 {
19197 INC_BOTH (max_pos, max_bpos);
19198 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19199 }
19200 }
19201 else if (row->truncated_on_right_p)
19202 /* display_line already called reseat_at_next_visible_line_start,
19203 which puts the iterator at the beginning of the next line, in
19204 the logical order. */
19205 row->maxpos = it->current.pos;
19206 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19207 /* A line that is entirely from a string/image/stretch... */
19208 row->maxpos = row->minpos;
19209 else
19210 emacs_abort ();
19211 }
19212 else
19213 row->maxpos = it->current.pos;
19214 }
19215
19216 /* Construct the glyph row IT->glyph_row in the desired matrix of
19217 IT->w from text at the current position of IT. See dispextern.h
19218 for an overview of struct it. Value is non-zero if
19219 IT->glyph_row displays text, as opposed to a line displaying ZV
19220 only. */
19221
19222 static int
19223 display_line (struct it *it)
19224 {
19225 struct glyph_row *row = it->glyph_row;
19226 Lisp_Object overlay_arrow_string;
19227 struct it wrap_it;
19228 void *wrap_data = NULL;
19229 int may_wrap = 0, wrap_x IF_LINT (= 0);
19230 int wrap_row_used = -1;
19231 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19232 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19233 int wrap_row_extra_line_spacing IF_LINT (= 0);
19234 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19235 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19236 int cvpos;
19237 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19238 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19239
19240 /* We always start displaying at hpos zero even if hscrolled. */
19241 eassert (it->hpos == 0 && it->current_x == 0);
19242
19243 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19244 >= it->w->desired_matrix->nrows)
19245 {
19246 it->w->nrows_scale_factor++;
19247 it->f->fonts_changed = 1;
19248 return 0;
19249 }
19250
19251 /* Is IT->w showing the region? */
19252 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19253
19254 /* Clear the result glyph row and enable it. */
19255 prepare_desired_row (row);
19256
19257 row->y = it->current_y;
19258 row->start = it->start;
19259 row->continuation_lines_width = it->continuation_lines_width;
19260 row->displays_text_p = 1;
19261 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19262 it->starts_in_middle_of_char_p = 0;
19263
19264 /* Arrange the overlays nicely for our purposes. Usually, we call
19265 display_line on only one line at a time, in which case this
19266 can't really hurt too much, or we call it on lines which appear
19267 one after another in the buffer, in which case all calls to
19268 recenter_overlay_lists but the first will be pretty cheap. */
19269 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19270
19271 /* Move over display elements that are not visible because we are
19272 hscrolled. This may stop at an x-position < IT->first_visible_x
19273 if the first glyph is partially visible or if we hit a line end. */
19274 if (it->current_x < it->first_visible_x)
19275 {
19276 enum move_it_result move_result;
19277
19278 this_line_min_pos = row->start.pos;
19279 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19280 MOVE_TO_POS | MOVE_TO_X);
19281 /* If we are under a large hscroll, move_it_in_display_line_to
19282 could hit the end of the line without reaching
19283 it->first_visible_x. Pretend that we did reach it. This is
19284 especially important on a TTY, where we will call
19285 extend_face_to_end_of_line, which needs to know how many
19286 blank glyphs to produce. */
19287 if (it->current_x < it->first_visible_x
19288 && (move_result == MOVE_NEWLINE_OR_CR
19289 || move_result == MOVE_POS_MATCH_OR_ZV))
19290 it->current_x = it->first_visible_x;
19291
19292 /* Record the smallest positions seen while we moved over
19293 display elements that are not visible. This is needed by
19294 redisplay_internal for optimizing the case where the cursor
19295 stays inside the same line. The rest of this function only
19296 considers positions that are actually displayed, so
19297 RECORD_MAX_MIN_POS will not otherwise record positions that
19298 are hscrolled to the left of the left edge of the window. */
19299 min_pos = CHARPOS (this_line_min_pos);
19300 min_bpos = BYTEPOS (this_line_min_pos);
19301 }
19302 else
19303 {
19304 /* We only do this when not calling `move_it_in_display_line_to'
19305 above, because move_it_in_display_line_to calls
19306 handle_line_prefix itself. */
19307 handle_line_prefix (it);
19308 }
19309
19310 /* Get the initial row height. This is either the height of the
19311 text hscrolled, if there is any, or zero. */
19312 row->ascent = it->max_ascent;
19313 row->height = it->max_ascent + it->max_descent;
19314 row->phys_ascent = it->max_phys_ascent;
19315 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19316 row->extra_line_spacing = it->max_extra_line_spacing;
19317
19318 /* Utility macro to record max and min buffer positions seen until now. */
19319 #define RECORD_MAX_MIN_POS(IT) \
19320 do \
19321 { \
19322 int composition_p = !STRINGP ((IT)->string) \
19323 && ((IT)->what == IT_COMPOSITION); \
19324 ptrdiff_t current_pos = \
19325 composition_p ? (IT)->cmp_it.charpos \
19326 : IT_CHARPOS (*(IT)); \
19327 ptrdiff_t current_bpos = \
19328 composition_p ? CHAR_TO_BYTE (current_pos) \
19329 : IT_BYTEPOS (*(IT)); \
19330 if (current_pos < min_pos) \
19331 { \
19332 min_pos = current_pos; \
19333 min_bpos = current_bpos; \
19334 } \
19335 if (IT_CHARPOS (*it) > max_pos) \
19336 { \
19337 max_pos = IT_CHARPOS (*it); \
19338 max_bpos = IT_BYTEPOS (*it); \
19339 } \
19340 } \
19341 while (0)
19342
19343 /* Loop generating characters. The loop is left with IT on the next
19344 character to display. */
19345 while (1)
19346 {
19347 int n_glyphs_before, hpos_before, x_before;
19348 int x, nglyphs;
19349 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19350
19351 /* Retrieve the next thing to display. Value is zero if end of
19352 buffer reached. */
19353 if (!get_next_display_element (it))
19354 {
19355 /* Maybe add a space at the end of this line that is used to
19356 display the cursor there under X. Set the charpos of the
19357 first glyph of blank lines not corresponding to any text
19358 to -1. */
19359 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19360 row->exact_window_width_line_p = 1;
19361 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19362 || row->used[TEXT_AREA] == 0)
19363 {
19364 row->glyphs[TEXT_AREA]->charpos = -1;
19365 row->displays_text_p = 0;
19366
19367 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19368 && (!MINI_WINDOW_P (it->w)
19369 || (minibuf_level && EQ (it->window, minibuf_window))))
19370 row->indicate_empty_line_p = 1;
19371 }
19372
19373 it->continuation_lines_width = 0;
19374 row->ends_at_zv_p = 1;
19375 /* A row that displays right-to-left text must always have
19376 its last face extended all the way to the end of line,
19377 even if this row ends in ZV, because we still write to
19378 the screen left to right. We also need to extend the
19379 last face if the default face is remapped to some
19380 different face, otherwise the functions that clear
19381 portions of the screen will clear with the default face's
19382 background color. */
19383 if (row->reversed_p
19384 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19385 extend_face_to_end_of_line (it);
19386 break;
19387 }
19388
19389 /* Now, get the metrics of what we want to display. This also
19390 generates glyphs in `row' (which is IT->glyph_row). */
19391 n_glyphs_before = row->used[TEXT_AREA];
19392 x = it->current_x;
19393
19394 /* Remember the line height so far in case the next element doesn't
19395 fit on the line. */
19396 if (it->line_wrap != TRUNCATE)
19397 {
19398 ascent = it->max_ascent;
19399 descent = it->max_descent;
19400 phys_ascent = it->max_phys_ascent;
19401 phys_descent = it->max_phys_descent;
19402
19403 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19404 {
19405 if (IT_DISPLAYING_WHITESPACE (it))
19406 may_wrap = 1;
19407 else if (may_wrap)
19408 {
19409 SAVE_IT (wrap_it, *it, wrap_data);
19410 wrap_x = x;
19411 wrap_row_used = row->used[TEXT_AREA];
19412 wrap_row_ascent = row->ascent;
19413 wrap_row_height = row->height;
19414 wrap_row_phys_ascent = row->phys_ascent;
19415 wrap_row_phys_height = row->phys_height;
19416 wrap_row_extra_line_spacing = row->extra_line_spacing;
19417 wrap_row_min_pos = min_pos;
19418 wrap_row_min_bpos = min_bpos;
19419 wrap_row_max_pos = max_pos;
19420 wrap_row_max_bpos = max_bpos;
19421 may_wrap = 0;
19422 }
19423 }
19424 }
19425
19426 PRODUCE_GLYPHS (it);
19427
19428 /* If this display element was in marginal areas, continue with
19429 the next one. */
19430 if (it->area != TEXT_AREA)
19431 {
19432 row->ascent = max (row->ascent, it->max_ascent);
19433 row->height = max (row->height, it->max_ascent + it->max_descent);
19434 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19435 row->phys_height = max (row->phys_height,
19436 it->max_phys_ascent + it->max_phys_descent);
19437 row->extra_line_spacing = max (row->extra_line_spacing,
19438 it->max_extra_line_spacing);
19439 set_iterator_to_next (it, 1);
19440 continue;
19441 }
19442
19443 /* Does the display element fit on the line? If we truncate
19444 lines, we should draw past the right edge of the window. If
19445 we don't truncate, we want to stop so that we can display the
19446 continuation glyph before the right margin. If lines are
19447 continued, there are two possible strategies for characters
19448 resulting in more than 1 glyph (e.g. tabs): Display as many
19449 glyphs as possible in this line and leave the rest for the
19450 continuation line, or display the whole element in the next
19451 line. Original redisplay did the former, so we do it also. */
19452 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19453 hpos_before = it->hpos;
19454 x_before = x;
19455
19456 if (/* Not a newline. */
19457 nglyphs > 0
19458 /* Glyphs produced fit entirely in the line. */
19459 && it->current_x < it->last_visible_x)
19460 {
19461 it->hpos += nglyphs;
19462 row->ascent = max (row->ascent, it->max_ascent);
19463 row->height = max (row->height, it->max_ascent + it->max_descent);
19464 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19465 row->phys_height = max (row->phys_height,
19466 it->max_phys_ascent + it->max_phys_descent);
19467 row->extra_line_spacing = max (row->extra_line_spacing,
19468 it->max_extra_line_spacing);
19469 if (it->current_x - it->pixel_width < it->first_visible_x)
19470 row->x = x - it->first_visible_x;
19471 /* Record the maximum and minimum buffer positions seen so
19472 far in glyphs that will be displayed by this row. */
19473 if (it->bidi_p)
19474 RECORD_MAX_MIN_POS (it);
19475 }
19476 else
19477 {
19478 int i, new_x;
19479 struct glyph *glyph;
19480
19481 for (i = 0; i < nglyphs; ++i, x = new_x)
19482 {
19483 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19484 new_x = x + glyph->pixel_width;
19485
19486 if (/* Lines are continued. */
19487 it->line_wrap != TRUNCATE
19488 && (/* Glyph doesn't fit on the line. */
19489 new_x > it->last_visible_x
19490 /* Or it fits exactly on a window system frame. */
19491 || (new_x == it->last_visible_x
19492 && FRAME_WINDOW_P (it->f)
19493 && (row->reversed_p
19494 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19495 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19496 {
19497 /* End of a continued line. */
19498
19499 if (it->hpos == 0
19500 || (new_x == it->last_visible_x
19501 && FRAME_WINDOW_P (it->f)
19502 && (row->reversed_p
19503 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19504 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19505 {
19506 /* Current glyph is the only one on the line or
19507 fits exactly on the line. We must continue
19508 the line because we can't draw the cursor
19509 after the glyph. */
19510 row->continued_p = 1;
19511 it->current_x = new_x;
19512 it->continuation_lines_width += new_x;
19513 ++it->hpos;
19514 if (i == nglyphs - 1)
19515 {
19516 /* If line-wrap is on, check if a previous
19517 wrap point was found. */
19518 if (wrap_row_used > 0
19519 /* Even if there is a previous wrap
19520 point, continue the line here as
19521 usual, if (i) the previous character
19522 was a space or tab AND (ii) the
19523 current character is not. */
19524 && (!may_wrap
19525 || IT_DISPLAYING_WHITESPACE (it)))
19526 goto back_to_wrap;
19527
19528 /* Record the maximum and minimum buffer
19529 positions seen so far in glyphs that will be
19530 displayed by this row. */
19531 if (it->bidi_p)
19532 RECORD_MAX_MIN_POS (it);
19533 set_iterator_to_next (it, 1);
19534 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19535 {
19536 if (!get_next_display_element (it))
19537 {
19538 row->exact_window_width_line_p = 1;
19539 it->continuation_lines_width = 0;
19540 row->continued_p = 0;
19541 row->ends_at_zv_p = 1;
19542 }
19543 else if (ITERATOR_AT_END_OF_LINE_P (it))
19544 {
19545 row->continued_p = 0;
19546 row->exact_window_width_line_p = 1;
19547 }
19548 }
19549 }
19550 else if (it->bidi_p)
19551 RECORD_MAX_MIN_POS (it);
19552 }
19553 else if (CHAR_GLYPH_PADDING_P (*glyph)
19554 && !FRAME_WINDOW_P (it->f))
19555 {
19556 /* A padding glyph that doesn't fit on this line.
19557 This means the whole character doesn't fit
19558 on the line. */
19559 if (row->reversed_p)
19560 unproduce_glyphs (it, row->used[TEXT_AREA]
19561 - n_glyphs_before);
19562 row->used[TEXT_AREA] = n_glyphs_before;
19563
19564 /* Fill the rest of the row with continuation
19565 glyphs like in 20.x. */
19566 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19567 < row->glyphs[1 + TEXT_AREA])
19568 produce_special_glyphs (it, IT_CONTINUATION);
19569
19570 row->continued_p = 1;
19571 it->current_x = x_before;
19572 it->continuation_lines_width += x_before;
19573
19574 /* Restore the height to what it was before the
19575 element not fitting on the line. */
19576 it->max_ascent = ascent;
19577 it->max_descent = descent;
19578 it->max_phys_ascent = phys_ascent;
19579 it->max_phys_descent = phys_descent;
19580 }
19581 else if (wrap_row_used > 0)
19582 {
19583 back_to_wrap:
19584 if (row->reversed_p)
19585 unproduce_glyphs (it,
19586 row->used[TEXT_AREA] - wrap_row_used);
19587 RESTORE_IT (it, &wrap_it, wrap_data);
19588 it->continuation_lines_width += wrap_x;
19589 row->used[TEXT_AREA] = wrap_row_used;
19590 row->ascent = wrap_row_ascent;
19591 row->height = wrap_row_height;
19592 row->phys_ascent = wrap_row_phys_ascent;
19593 row->phys_height = wrap_row_phys_height;
19594 row->extra_line_spacing = wrap_row_extra_line_spacing;
19595 min_pos = wrap_row_min_pos;
19596 min_bpos = wrap_row_min_bpos;
19597 max_pos = wrap_row_max_pos;
19598 max_bpos = wrap_row_max_bpos;
19599 row->continued_p = 1;
19600 row->ends_at_zv_p = 0;
19601 row->exact_window_width_line_p = 0;
19602 it->continuation_lines_width += x;
19603
19604 /* Make sure that a non-default face is extended
19605 up to the right margin of the window. */
19606 extend_face_to_end_of_line (it);
19607 }
19608 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19609 {
19610 /* A TAB that extends past the right edge of the
19611 window. This produces a single glyph on
19612 window system frames. We leave the glyph in
19613 this row and let it fill the row, but don't
19614 consume the TAB. */
19615 if ((row->reversed_p
19616 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19617 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19618 produce_special_glyphs (it, IT_CONTINUATION);
19619 it->continuation_lines_width += it->last_visible_x;
19620 row->ends_in_middle_of_char_p = 1;
19621 row->continued_p = 1;
19622 glyph->pixel_width = it->last_visible_x - x;
19623 it->starts_in_middle_of_char_p = 1;
19624 }
19625 else
19626 {
19627 /* Something other than a TAB that draws past
19628 the right edge of the window. Restore
19629 positions to values before the element. */
19630 if (row->reversed_p)
19631 unproduce_glyphs (it, row->used[TEXT_AREA]
19632 - (n_glyphs_before + i));
19633 row->used[TEXT_AREA] = n_glyphs_before + i;
19634
19635 /* Display continuation glyphs. */
19636 it->current_x = x_before;
19637 it->continuation_lines_width += x;
19638 if (!FRAME_WINDOW_P (it->f)
19639 || (row->reversed_p
19640 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19641 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19642 produce_special_glyphs (it, IT_CONTINUATION);
19643 row->continued_p = 1;
19644
19645 extend_face_to_end_of_line (it);
19646
19647 if (nglyphs > 1 && i > 0)
19648 {
19649 row->ends_in_middle_of_char_p = 1;
19650 it->starts_in_middle_of_char_p = 1;
19651 }
19652
19653 /* Restore the height to what it was before the
19654 element not fitting on the line. */
19655 it->max_ascent = ascent;
19656 it->max_descent = descent;
19657 it->max_phys_ascent = phys_ascent;
19658 it->max_phys_descent = phys_descent;
19659 }
19660
19661 break;
19662 }
19663 else if (new_x > it->first_visible_x)
19664 {
19665 /* Increment number of glyphs actually displayed. */
19666 ++it->hpos;
19667
19668 /* Record the maximum and minimum buffer positions
19669 seen so far in glyphs that will be displayed by
19670 this row. */
19671 if (it->bidi_p)
19672 RECORD_MAX_MIN_POS (it);
19673
19674 if (x < it->first_visible_x)
19675 /* Glyph is partially visible, i.e. row starts at
19676 negative X position. */
19677 row->x = x - it->first_visible_x;
19678 }
19679 else
19680 {
19681 /* Glyph is completely off the left margin of the
19682 window. This should not happen because of the
19683 move_it_in_display_line at the start of this
19684 function, unless the text display area of the
19685 window is empty. */
19686 eassert (it->first_visible_x <= it->last_visible_x);
19687 }
19688 }
19689 /* Even if this display element produced no glyphs at all,
19690 we want to record its position. */
19691 if (it->bidi_p && nglyphs == 0)
19692 RECORD_MAX_MIN_POS (it);
19693
19694 row->ascent = max (row->ascent, it->max_ascent);
19695 row->height = max (row->height, it->max_ascent + it->max_descent);
19696 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19697 row->phys_height = max (row->phys_height,
19698 it->max_phys_ascent + it->max_phys_descent);
19699 row->extra_line_spacing = max (row->extra_line_spacing,
19700 it->max_extra_line_spacing);
19701
19702 /* End of this display line if row is continued. */
19703 if (row->continued_p || row->ends_at_zv_p)
19704 break;
19705 }
19706
19707 at_end_of_line:
19708 /* Is this a line end? If yes, we're also done, after making
19709 sure that a non-default face is extended up to the right
19710 margin of the window. */
19711 if (ITERATOR_AT_END_OF_LINE_P (it))
19712 {
19713 int used_before = row->used[TEXT_AREA];
19714
19715 row->ends_in_newline_from_string_p = STRINGP (it->object);
19716
19717 /* Add a space at the end of the line that is used to
19718 display the cursor there. */
19719 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19720 append_space_for_newline (it, 0);
19721
19722 /* Extend the face to the end of the line. */
19723 extend_face_to_end_of_line (it);
19724
19725 /* Make sure we have the position. */
19726 if (used_before == 0)
19727 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19728
19729 /* Record the position of the newline, for use in
19730 find_row_edges. */
19731 it->eol_pos = it->current.pos;
19732
19733 /* Consume the line end. This skips over invisible lines. */
19734 set_iterator_to_next (it, 1);
19735 it->continuation_lines_width = 0;
19736 break;
19737 }
19738
19739 /* Proceed with next display element. Note that this skips
19740 over lines invisible because of selective display. */
19741 set_iterator_to_next (it, 1);
19742
19743 /* If we truncate lines, we are done when the last displayed
19744 glyphs reach past the right margin of the window. */
19745 if (it->line_wrap == TRUNCATE
19746 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19747 ? (it->current_x >= it->last_visible_x)
19748 : (it->current_x > it->last_visible_x)))
19749 {
19750 /* Maybe add truncation glyphs. */
19751 if (!FRAME_WINDOW_P (it->f)
19752 || (row->reversed_p
19753 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19754 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19755 {
19756 int i, n;
19757
19758 if (!row->reversed_p)
19759 {
19760 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19761 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19762 break;
19763 }
19764 else
19765 {
19766 for (i = 0; i < row->used[TEXT_AREA]; i++)
19767 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19768 break;
19769 /* Remove any padding glyphs at the front of ROW, to
19770 make room for the truncation glyphs we will be
19771 adding below. The loop below always inserts at
19772 least one truncation glyph, so also remove the
19773 last glyph added to ROW. */
19774 unproduce_glyphs (it, i + 1);
19775 /* Adjust i for the loop below. */
19776 i = row->used[TEXT_AREA] - (i + 1);
19777 }
19778
19779 it->current_x = x_before;
19780 if (!FRAME_WINDOW_P (it->f))
19781 {
19782 for (n = row->used[TEXT_AREA]; i < n; ++i)
19783 {
19784 row->used[TEXT_AREA] = i;
19785 produce_special_glyphs (it, IT_TRUNCATION);
19786 }
19787 }
19788 else
19789 {
19790 row->used[TEXT_AREA] = i;
19791 produce_special_glyphs (it, IT_TRUNCATION);
19792 }
19793 }
19794 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19795 {
19796 /* Don't truncate if we can overflow newline into fringe. */
19797 if (!get_next_display_element (it))
19798 {
19799 it->continuation_lines_width = 0;
19800 row->ends_at_zv_p = 1;
19801 row->exact_window_width_line_p = 1;
19802 break;
19803 }
19804 if (ITERATOR_AT_END_OF_LINE_P (it))
19805 {
19806 row->exact_window_width_line_p = 1;
19807 goto at_end_of_line;
19808 }
19809 it->current_x = x_before;
19810 }
19811
19812 row->truncated_on_right_p = 1;
19813 it->continuation_lines_width = 0;
19814 reseat_at_next_visible_line_start (it, 0);
19815 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19816 it->hpos = hpos_before;
19817 break;
19818 }
19819 }
19820
19821 if (wrap_data)
19822 bidi_unshelve_cache (wrap_data, 1);
19823
19824 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19825 at the left window margin. */
19826 if (it->first_visible_x
19827 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19828 {
19829 if (!FRAME_WINDOW_P (it->f)
19830 || (row->reversed_p
19831 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19832 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19833 insert_left_trunc_glyphs (it);
19834 row->truncated_on_left_p = 1;
19835 }
19836
19837 /* Remember the position at which this line ends.
19838
19839 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19840 cannot be before the call to find_row_edges below, since that is
19841 where these positions are determined. */
19842 row->end = it->current;
19843 if (!it->bidi_p)
19844 {
19845 row->minpos = row->start.pos;
19846 row->maxpos = row->end.pos;
19847 }
19848 else
19849 {
19850 /* ROW->minpos and ROW->maxpos must be the smallest and
19851 `1 + the largest' buffer positions in ROW. But if ROW was
19852 bidi-reordered, these two positions can be anywhere in the
19853 row, so we must determine them now. */
19854 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19855 }
19856
19857 /* If the start of this line is the overlay arrow-position, then
19858 mark this glyph row as the one containing the overlay arrow.
19859 This is clearly a mess with variable size fonts. It would be
19860 better to let it be displayed like cursors under X. */
19861 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19862 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19863 !NILP (overlay_arrow_string)))
19864 {
19865 /* Overlay arrow in window redisplay is a fringe bitmap. */
19866 if (STRINGP (overlay_arrow_string))
19867 {
19868 struct glyph_row *arrow_row
19869 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19870 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19871 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19872 struct glyph *p = row->glyphs[TEXT_AREA];
19873 struct glyph *p2, *end;
19874
19875 /* Copy the arrow glyphs. */
19876 while (glyph < arrow_end)
19877 *p++ = *glyph++;
19878
19879 /* Throw away padding glyphs. */
19880 p2 = p;
19881 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19882 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19883 ++p2;
19884 if (p2 > p)
19885 {
19886 while (p2 < end)
19887 *p++ = *p2++;
19888 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19889 }
19890 }
19891 else
19892 {
19893 eassert (INTEGERP (overlay_arrow_string));
19894 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19895 }
19896 overlay_arrow_seen = 1;
19897 }
19898
19899 /* Highlight trailing whitespace. */
19900 if (!NILP (Vshow_trailing_whitespace))
19901 highlight_trailing_whitespace (it->f, it->glyph_row);
19902
19903 /* Compute pixel dimensions of this line. */
19904 compute_line_metrics (it);
19905
19906 /* Implementation note: No changes in the glyphs of ROW or in their
19907 faces can be done past this point, because compute_line_metrics
19908 computes ROW's hash value and stores it within the glyph_row
19909 structure. */
19910
19911 /* Record whether this row ends inside an ellipsis. */
19912 row->ends_in_ellipsis_p
19913 = (it->method == GET_FROM_DISPLAY_VECTOR
19914 && it->ellipsis_p);
19915
19916 /* Save fringe bitmaps in this row. */
19917 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19918 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19919 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19920 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19921
19922 it->left_user_fringe_bitmap = 0;
19923 it->left_user_fringe_face_id = 0;
19924 it->right_user_fringe_bitmap = 0;
19925 it->right_user_fringe_face_id = 0;
19926
19927 /* Maybe set the cursor. */
19928 cvpos = it->w->cursor.vpos;
19929 if ((cvpos < 0
19930 /* In bidi-reordered rows, keep checking for proper cursor
19931 position even if one has been found already, because buffer
19932 positions in such rows change non-linearly with ROW->VPOS,
19933 when a line is continued. One exception: when we are at ZV,
19934 display cursor on the first suitable glyph row, since all
19935 the empty rows after that also have their position set to ZV. */
19936 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19937 lines' rows is implemented for bidi-reordered rows. */
19938 || (it->bidi_p
19939 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19940 && PT >= MATRIX_ROW_START_CHARPOS (row)
19941 && PT <= MATRIX_ROW_END_CHARPOS (row)
19942 && cursor_row_p (row))
19943 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19944
19945 /* Prepare for the next line. This line starts horizontally at (X
19946 HPOS) = (0 0). Vertical positions are incremented. As a
19947 convenience for the caller, IT->glyph_row is set to the next
19948 row to be used. */
19949 it->current_x = it->hpos = 0;
19950 it->current_y += row->height;
19951 SET_TEXT_POS (it->eol_pos, 0, 0);
19952 ++it->vpos;
19953 ++it->glyph_row;
19954 /* The next row should by default use the same value of the
19955 reversed_p flag as this one. set_iterator_to_next decides when
19956 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19957 the flag accordingly. */
19958 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19959 it->glyph_row->reversed_p = row->reversed_p;
19960 it->start = row->end;
19961 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19962
19963 #undef RECORD_MAX_MIN_POS
19964 }
19965
19966 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19967 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19968 doc: /* Return paragraph direction at point in BUFFER.
19969 Value is either `left-to-right' or `right-to-left'.
19970 If BUFFER is omitted or nil, it defaults to the current buffer.
19971
19972 Paragraph direction determines how the text in the paragraph is displayed.
19973 In left-to-right paragraphs, text begins at the left margin of the window
19974 and the reading direction is generally left to right. In right-to-left
19975 paragraphs, text begins at the right margin and is read from right to left.
19976
19977 See also `bidi-paragraph-direction'. */)
19978 (Lisp_Object buffer)
19979 {
19980 struct buffer *buf = current_buffer;
19981 struct buffer *old = buf;
19982
19983 if (! NILP (buffer))
19984 {
19985 CHECK_BUFFER (buffer);
19986 buf = XBUFFER (buffer);
19987 }
19988
19989 if (NILP (BVAR (buf, bidi_display_reordering))
19990 || NILP (BVAR (buf, enable_multibyte_characters))
19991 /* When we are loading loadup.el, the character property tables
19992 needed for bidi iteration are not yet available. */
19993 || !NILP (Vpurify_flag))
19994 return Qleft_to_right;
19995 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19996 return BVAR (buf, bidi_paragraph_direction);
19997 else
19998 {
19999 /* Determine the direction from buffer text. We could try to
20000 use current_matrix if it is up to date, but this seems fast
20001 enough as it is. */
20002 struct bidi_it itb;
20003 ptrdiff_t pos = BUF_PT (buf);
20004 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20005 int c;
20006 void *itb_data = bidi_shelve_cache ();
20007
20008 set_buffer_temp (buf);
20009 /* bidi_paragraph_init finds the base direction of the paragraph
20010 by searching forward from paragraph start. We need the base
20011 direction of the current or _previous_ paragraph, so we need
20012 to make sure we are within that paragraph. To that end, find
20013 the previous non-empty line. */
20014 if (pos >= ZV && pos > BEGV)
20015 DEC_BOTH (pos, bytepos);
20016 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20017 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20018 {
20019 while ((c = FETCH_BYTE (bytepos)) == '\n'
20020 || c == ' ' || c == '\t' || c == '\f')
20021 {
20022 if (bytepos <= BEGV_BYTE)
20023 break;
20024 bytepos--;
20025 pos--;
20026 }
20027 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20028 bytepos--;
20029 }
20030 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20031 itb.paragraph_dir = NEUTRAL_DIR;
20032 itb.string.s = NULL;
20033 itb.string.lstring = Qnil;
20034 itb.string.bufpos = 0;
20035 itb.string.unibyte = 0;
20036 /* We have no window to use here for ignoring window-specific
20037 overlays. Using NULL for window pointer will cause
20038 compute_display_string_pos to use the current buffer. */
20039 itb.w = NULL;
20040 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20041 bidi_unshelve_cache (itb_data, 0);
20042 set_buffer_temp (old);
20043 switch (itb.paragraph_dir)
20044 {
20045 case L2R:
20046 return Qleft_to_right;
20047 break;
20048 case R2L:
20049 return Qright_to_left;
20050 break;
20051 default:
20052 emacs_abort ();
20053 }
20054 }
20055 }
20056
20057 DEFUN ("move-point-visually", Fmove_point_visually,
20058 Smove_point_visually, 1, 1, 0,
20059 doc: /* Move point in the visual order in the specified DIRECTION.
20060 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20061 left.
20062
20063 Value is the new character position of point. */)
20064 (Lisp_Object direction)
20065 {
20066 struct window *w = XWINDOW (selected_window);
20067 struct buffer *b = XBUFFER (w->contents);
20068 struct glyph_row *row;
20069 int dir;
20070 Lisp_Object paragraph_dir;
20071
20072 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20073 (!(ROW)->continued_p \
20074 && INTEGERP ((GLYPH)->object) \
20075 && (GLYPH)->type == CHAR_GLYPH \
20076 && (GLYPH)->u.ch == ' ' \
20077 && (GLYPH)->charpos >= 0 \
20078 && !(GLYPH)->avoid_cursor_p)
20079
20080 CHECK_NUMBER (direction);
20081 dir = XINT (direction);
20082 if (dir > 0)
20083 dir = 1;
20084 else
20085 dir = -1;
20086
20087 /* If current matrix is up-to-date, we can use the information
20088 recorded in the glyphs, at least as long as the goal is on the
20089 screen. */
20090 if (w->window_end_valid
20091 && !windows_or_buffers_changed
20092 && b
20093 && !b->clip_changed
20094 && !b->prevent_redisplay_optimizations_p
20095 && !window_outdated (w)
20096 && w->cursor.vpos >= 0
20097 && w->cursor.vpos < w->current_matrix->nrows
20098 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20099 {
20100 struct glyph *g = row->glyphs[TEXT_AREA];
20101 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20102 struct glyph *gpt = g + w->cursor.hpos;
20103
20104 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20105 {
20106 if (BUFFERP (g->object) && g->charpos != PT)
20107 {
20108 SET_PT (g->charpos);
20109 w->cursor.vpos = -1;
20110 return make_number (PT);
20111 }
20112 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20113 {
20114 ptrdiff_t new_pos;
20115
20116 if (BUFFERP (gpt->object))
20117 {
20118 new_pos = PT;
20119 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20120 new_pos += (row->reversed_p ? -dir : dir);
20121 else
20122 new_pos -= (row->reversed_p ? -dir : dir);;
20123 }
20124 else if (BUFFERP (g->object))
20125 new_pos = g->charpos;
20126 else
20127 break;
20128 SET_PT (new_pos);
20129 w->cursor.vpos = -1;
20130 return make_number (PT);
20131 }
20132 else if (ROW_GLYPH_NEWLINE_P (row, g))
20133 {
20134 /* Glyphs inserted at the end of a non-empty line for
20135 positioning the cursor have zero charpos, so we must
20136 deduce the value of point by other means. */
20137 if (g->charpos > 0)
20138 SET_PT (g->charpos);
20139 else if (row->ends_at_zv_p && PT != ZV)
20140 SET_PT (ZV);
20141 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20142 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20143 else
20144 break;
20145 w->cursor.vpos = -1;
20146 return make_number (PT);
20147 }
20148 }
20149 if (g == e || INTEGERP (g->object))
20150 {
20151 if (row->truncated_on_left_p || row->truncated_on_right_p)
20152 goto simulate_display;
20153 if (!row->reversed_p)
20154 row += dir;
20155 else
20156 row -= dir;
20157 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20158 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20159 goto simulate_display;
20160
20161 if (dir > 0)
20162 {
20163 if (row->reversed_p && !row->continued_p)
20164 {
20165 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20166 w->cursor.vpos = -1;
20167 return make_number (PT);
20168 }
20169 g = row->glyphs[TEXT_AREA];
20170 e = g + row->used[TEXT_AREA];
20171 for ( ; g < e; g++)
20172 {
20173 if (BUFFERP (g->object)
20174 /* Empty lines have only one glyph, which stands
20175 for the newline, and whose charpos is the
20176 buffer position of the newline. */
20177 || ROW_GLYPH_NEWLINE_P (row, g)
20178 /* When the buffer ends in a newline, the line at
20179 EOB also has one glyph, but its charpos is -1. */
20180 || (row->ends_at_zv_p
20181 && !row->reversed_p
20182 && INTEGERP (g->object)
20183 && g->type == CHAR_GLYPH
20184 && g->u.ch == ' '))
20185 {
20186 if (g->charpos > 0)
20187 SET_PT (g->charpos);
20188 else if (!row->reversed_p
20189 && row->ends_at_zv_p
20190 && PT != ZV)
20191 SET_PT (ZV);
20192 else
20193 continue;
20194 w->cursor.vpos = -1;
20195 return make_number (PT);
20196 }
20197 }
20198 }
20199 else
20200 {
20201 if (!row->reversed_p && !row->continued_p)
20202 {
20203 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20204 w->cursor.vpos = -1;
20205 return make_number (PT);
20206 }
20207 e = row->glyphs[TEXT_AREA];
20208 g = e + row->used[TEXT_AREA] - 1;
20209 for ( ; g >= e; g--)
20210 {
20211 if (BUFFERP (g->object)
20212 || (ROW_GLYPH_NEWLINE_P (row, g)
20213 && g->charpos > 0)
20214 /* Empty R2L lines on GUI frames have the buffer
20215 position of the newline stored in the stretch
20216 glyph. */
20217 || g->type == STRETCH_GLYPH
20218 || (row->ends_at_zv_p
20219 && row->reversed_p
20220 && INTEGERP (g->object)
20221 && g->type == CHAR_GLYPH
20222 && g->u.ch == ' '))
20223 {
20224 if (g->charpos > 0)
20225 SET_PT (g->charpos);
20226 else if (row->reversed_p
20227 && row->ends_at_zv_p
20228 && PT != ZV)
20229 SET_PT (ZV);
20230 else
20231 continue;
20232 w->cursor.vpos = -1;
20233 return make_number (PT);
20234 }
20235 }
20236 }
20237 }
20238 }
20239
20240 simulate_display:
20241
20242 /* If we wind up here, we failed to move by using the glyphs, so we
20243 need to simulate display instead. */
20244
20245 if (b)
20246 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20247 else
20248 paragraph_dir = Qleft_to_right;
20249 if (EQ (paragraph_dir, Qright_to_left))
20250 dir = -dir;
20251 if (PT <= BEGV && dir < 0)
20252 xsignal0 (Qbeginning_of_buffer);
20253 else if (PT >= ZV && dir > 0)
20254 xsignal0 (Qend_of_buffer);
20255 else
20256 {
20257 struct text_pos pt;
20258 struct it it;
20259 int pt_x, target_x, pixel_width, pt_vpos;
20260 bool at_eol_p;
20261 bool overshoot_expected = false;
20262 bool target_is_eol_p = false;
20263
20264 /* Setup the arena. */
20265 SET_TEXT_POS (pt, PT, PT_BYTE);
20266 start_display (&it, w, pt);
20267
20268 if (it.cmp_it.id < 0
20269 && it.method == GET_FROM_STRING
20270 && it.area == TEXT_AREA
20271 && it.string_from_display_prop_p
20272 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20273 overshoot_expected = true;
20274
20275 /* Find the X coordinate of point. We start from the beginning
20276 of this or previous line to make sure we are before point in
20277 the logical order (since the move_it_* functions can only
20278 move forward). */
20279 reseat_at_previous_visible_line_start (&it);
20280 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20281 if (IT_CHARPOS (it) != PT)
20282 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20283 -1, -1, -1, MOVE_TO_POS);
20284 pt_x = it.current_x;
20285 pt_vpos = it.vpos;
20286 if (dir > 0 || overshoot_expected)
20287 {
20288 struct glyph_row *row = it.glyph_row;
20289
20290 /* When point is at beginning of line, we don't have
20291 information about the glyph there loaded into struct
20292 it. Calling get_next_display_element fixes that. */
20293 if (pt_x == 0)
20294 get_next_display_element (&it);
20295 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20296 it.glyph_row = NULL;
20297 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20298 it.glyph_row = row;
20299 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20300 it, lest it will become out of sync with it's buffer
20301 position. */
20302 it.current_x = pt_x;
20303 }
20304 else
20305 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20306 pixel_width = it.pixel_width;
20307 if (overshoot_expected && at_eol_p)
20308 pixel_width = 0;
20309 else if (pixel_width <= 0)
20310 pixel_width = 1;
20311
20312 /* If there's a display string at point, we are actually at the
20313 glyph to the left of point, so we need to correct the X
20314 coordinate. */
20315 if (overshoot_expected)
20316 pt_x += pixel_width;
20317
20318 /* Compute target X coordinate, either to the left or to the
20319 right of point. On TTY frames, all characters have the same
20320 pixel width of 1, so we can use that. On GUI frames we don't
20321 have an easy way of getting at the pixel width of the
20322 character to the left of point, so we use a different method
20323 of getting to that place. */
20324 if (dir > 0)
20325 target_x = pt_x + pixel_width;
20326 else
20327 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20328
20329 /* Target X coordinate could be one line above or below the line
20330 of point, in which case we need to adjust the target X
20331 coordinate. Also, if moving to the left, we need to begin at
20332 the left edge of the point's screen line. */
20333 if (dir < 0)
20334 {
20335 if (pt_x > 0)
20336 {
20337 start_display (&it, w, pt);
20338 reseat_at_previous_visible_line_start (&it);
20339 it.current_x = it.current_y = it.hpos = 0;
20340 if (pt_vpos != 0)
20341 move_it_by_lines (&it, pt_vpos);
20342 }
20343 else
20344 {
20345 move_it_by_lines (&it, -1);
20346 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20347 target_is_eol_p = true;
20348 }
20349 }
20350 else
20351 {
20352 if (at_eol_p
20353 || (target_x >= it.last_visible_x
20354 && it.line_wrap != TRUNCATE))
20355 {
20356 if (pt_x > 0)
20357 move_it_by_lines (&it, 0);
20358 move_it_by_lines (&it, 1);
20359 target_x = 0;
20360 }
20361 }
20362
20363 /* Move to the target X coordinate. */
20364 #ifdef HAVE_WINDOW_SYSTEM
20365 /* On GUI frames, as we don't know the X coordinate of the
20366 character to the left of point, moving point to the left
20367 requires walking, one grapheme cluster at a time, until we
20368 find ourself at a place immediately to the left of the
20369 character at point. */
20370 if (FRAME_WINDOW_P (it.f) && dir < 0)
20371 {
20372 struct text_pos new_pos = it.current.pos;
20373 enum move_it_result rc = MOVE_X_REACHED;
20374
20375 while (it.current_x + it.pixel_width <= target_x
20376 && rc == MOVE_X_REACHED)
20377 {
20378 int new_x = it.current_x + it.pixel_width;
20379
20380 new_pos = it.current.pos;
20381 if (new_x == it.current_x)
20382 new_x++;
20383 rc = move_it_in_display_line_to (&it, ZV, new_x,
20384 MOVE_TO_POS | MOVE_TO_X);
20385 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20386 break;
20387 }
20388 /* If we ended up on a composed character inside
20389 bidi-reordered text (e.g., Hebrew text with diacritics),
20390 the iterator gives us the buffer position of the last (in
20391 logical order) character of the composed grapheme cluster,
20392 which is not what we want. So we cheat: we compute the
20393 character position of the character that follows (in the
20394 logical order) the one where the above loop stopped. That
20395 character will appear on display to the left of point. */
20396 if (it.bidi_p
20397 && it.bidi_it.scan_dir == -1
20398 && new_pos.charpos - IT_CHARPOS (it) > 1)
20399 {
20400 new_pos.charpos = IT_CHARPOS (it) + 1;
20401 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20402 }
20403 it.current.pos = new_pos;
20404 }
20405 else
20406 #endif
20407 if (it.current_x != target_x)
20408 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20409
20410 /* When lines are truncated, the above loop will stop at the
20411 window edge. But we want to get to the end of line, even if
20412 it is beyond the window edge; automatic hscroll will then
20413 scroll the window to show point as appropriate. */
20414 if (target_is_eol_p && it.line_wrap == TRUNCATE
20415 && get_next_display_element (&it))
20416 {
20417 struct text_pos new_pos = it.current.pos;
20418
20419 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20420 {
20421 set_iterator_to_next (&it, 0);
20422 if (it.method == GET_FROM_BUFFER)
20423 new_pos = it.current.pos;
20424 if (!get_next_display_element (&it))
20425 break;
20426 }
20427
20428 it.current.pos = new_pos;
20429 }
20430
20431 /* If we ended up in a display string that covers point, move to
20432 buffer position to the right in the visual order. */
20433 if (dir > 0)
20434 {
20435 while (IT_CHARPOS (it) == PT)
20436 {
20437 set_iterator_to_next (&it, 0);
20438 if (!get_next_display_element (&it))
20439 break;
20440 }
20441 }
20442
20443 /* Move point to that position. */
20444 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20445 }
20446
20447 return make_number (PT);
20448
20449 #undef ROW_GLYPH_NEWLINE_P
20450 }
20451
20452 \f
20453 /***********************************************************************
20454 Menu Bar
20455 ***********************************************************************/
20456
20457 /* Redisplay the menu bar in the frame for window W.
20458
20459 The menu bar of X frames that don't have X toolkit support is
20460 displayed in a special window W->frame->menu_bar_window.
20461
20462 The menu bar of terminal frames is treated specially as far as
20463 glyph matrices are concerned. Menu bar lines are not part of
20464 windows, so the update is done directly on the frame matrix rows
20465 for the menu bar. */
20466
20467 static void
20468 display_menu_bar (struct window *w)
20469 {
20470 struct frame *f = XFRAME (WINDOW_FRAME (w));
20471 struct it it;
20472 Lisp_Object items;
20473 int i;
20474
20475 /* Don't do all this for graphical frames. */
20476 #ifdef HAVE_NTGUI
20477 if (FRAME_W32_P (f))
20478 return;
20479 #endif
20480 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20481 if (FRAME_X_P (f))
20482 return;
20483 #endif
20484
20485 #ifdef HAVE_NS
20486 if (FRAME_NS_P (f))
20487 return;
20488 #endif /* HAVE_NS */
20489
20490 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20491 eassert (!FRAME_WINDOW_P (f));
20492 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20493 it.first_visible_x = 0;
20494 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20495 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20496 if (FRAME_WINDOW_P (f))
20497 {
20498 /* Menu bar lines are displayed in the desired matrix of the
20499 dummy window menu_bar_window. */
20500 struct window *menu_w;
20501 menu_w = XWINDOW (f->menu_bar_window);
20502 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20503 MENU_FACE_ID);
20504 it.first_visible_x = 0;
20505 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20506 }
20507 else
20508 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20509 {
20510 /* This is a TTY frame, i.e. character hpos/vpos are used as
20511 pixel x/y. */
20512 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20513 MENU_FACE_ID);
20514 it.first_visible_x = 0;
20515 it.last_visible_x = FRAME_COLS (f);
20516 }
20517
20518 /* FIXME: This should be controlled by a user option. See the
20519 comments in redisplay_tool_bar and display_mode_line about
20520 this. */
20521 it.paragraph_embedding = L2R;
20522
20523 /* Clear all rows of the menu bar. */
20524 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20525 {
20526 struct glyph_row *row = it.glyph_row + i;
20527 clear_glyph_row (row);
20528 row->enabled_p = 1;
20529 row->full_width_p = 1;
20530 }
20531
20532 /* Display all items of the menu bar. */
20533 items = FRAME_MENU_BAR_ITEMS (it.f);
20534 for (i = 0; i < ASIZE (items); i += 4)
20535 {
20536 Lisp_Object string;
20537
20538 /* Stop at nil string. */
20539 string = AREF (items, i + 1);
20540 if (NILP (string))
20541 break;
20542
20543 /* Remember where item was displayed. */
20544 ASET (items, i + 3, make_number (it.hpos));
20545
20546 /* Display the item, pad with one space. */
20547 if (it.current_x < it.last_visible_x)
20548 display_string (NULL, string, Qnil, 0, 0, &it,
20549 SCHARS (string) + 1, 0, 0, -1);
20550 }
20551
20552 /* Fill out the line with spaces. */
20553 if (it.current_x < it.last_visible_x)
20554 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20555
20556 /* Compute the total height of the lines. */
20557 compute_line_metrics (&it);
20558 }
20559
20560
20561 \f
20562 /***********************************************************************
20563 Mode Line
20564 ***********************************************************************/
20565
20566 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20567 FORCE is non-zero, redisplay mode lines unconditionally.
20568 Otherwise, redisplay only mode lines that are garbaged. Value is
20569 the number of windows whose mode lines were redisplayed. */
20570
20571 static int
20572 redisplay_mode_lines (Lisp_Object window, int force)
20573 {
20574 int nwindows = 0;
20575
20576 while (!NILP (window))
20577 {
20578 struct window *w = XWINDOW (window);
20579
20580 if (WINDOWP (w->contents))
20581 nwindows += redisplay_mode_lines (w->contents, force);
20582 else if (force
20583 || FRAME_GARBAGED_P (XFRAME (w->frame))
20584 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20585 {
20586 struct text_pos lpoint;
20587 struct buffer *old = current_buffer;
20588
20589 /* Set the window's buffer for the mode line display. */
20590 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20591 set_buffer_internal_1 (XBUFFER (w->contents));
20592
20593 /* Point refers normally to the selected window. For any
20594 other window, set up appropriate value. */
20595 if (!EQ (window, selected_window))
20596 {
20597 struct text_pos pt;
20598
20599 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20600 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20601 }
20602
20603 /* Display mode lines. */
20604 clear_glyph_matrix (w->desired_matrix);
20605 if (display_mode_lines (w))
20606 {
20607 ++nwindows;
20608 w->must_be_updated_p = 1;
20609 }
20610
20611 /* Restore old settings. */
20612 set_buffer_internal_1 (old);
20613 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20614 }
20615
20616 window = w->next;
20617 }
20618
20619 return nwindows;
20620 }
20621
20622
20623 /* Display the mode and/or header line of window W. Value is the
20624 sum number of mode lines and header lines displayed. */
20625
20626 static int
20627 display_mode_lines (struct window *w)
20628 {
20629 Lisp_Object old_selected_window = selected_window;
20630 Lisp_Object old_selected_frame = selected_frame;
20631 Lisp_Object new_frame = w->frame;
20632 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20633 int n = 0;
20634
20635 selected_frame = new_frame;
20636 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20637 or window's point, then we'd need select_window_1 here as well. */
20638 XSETWINDOW (selected_window, w);
20639 XFRAME (new_frame)->selected_window = selected_window;
20640
20641 /* These will be set while the mode line specs are processed. */
20642 line_number_displayed = 0;
20643 w->column_number_displayed = -1;
20644
20645 if (WINDOW_WANTS_MODELINE_P (w))
20646 {
20647 struct window *sel_w = XWINDOW (old_selected_window);
20648
20649 /* Select mode line face based on the real selected window. */
20650 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20651 BVAR (current_buffer, mode_line_format));
20652 ++n;
20653 }
20654
20655 if (WINDOW_WANTS_HEADER_LINE_P (w))
20656 {
20657 display_mode_line (w, HEADER_LINE_FACE_ID,
20658 BVAR (current_buffer, header_line_format));
20659 ++n;
20660 }
20661
20662 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20663 selected_frame = old_selected_frame;
20664 selected_window = old_selected_window;
20665 return n;
20666 }
20667
20668
20669 /* Display mode or header line of window W. FACE_ID specifies which
20670 line to display; it is either MODE_LINE_FACE_ID or
20671 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20672 display. Value is the pixel height of the mode/header line
20673 displayed. */
20674
20675 static int
20676 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20677 {
20678 struct it it;
20679 struct face *face;
20680 ptrdiff_t count = SPECPDL_INDEX ();
20681
20682 init_iterator (&it, w, -1, -1, NULL, face_id);
20683 /* Don't extend on a previously drawn mode-line.
20684 This may happen if called from pos_visible_p. */
20685 it.glyph_row->enabled_p = 0;
20686 prepare_desired_row (it.glyph_row);
20687
20688 it.glyph_row->mode_line_p = 1;
20689
20690 /* FIXME: This should be controlled by a user option. But
20691 supporting such an option is not trivial, since the mode line is
20692 made up of many separate strings. */
20693 it.paragraph_embedding = L2R;
20694
20695 record_unwind_protect (unwind_format_mode_line,
20696 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20697
20698 mode_line_target = MODE_LINE_DISPLAY;
20699
20700 /* Temporarily make frame's keyboard the current kboard so that
20701 kboard-local variables in the mode_line_format will get the right
20702 values. */
20703 push_kboard (FRAME_KBOARD (it.f));
20704 record_unwind_save_match_data ();
20705 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20706 pop_kboard ();
20707
20708 unbind_to (count, Qnil);
20709
20710 /* Fill up with spaces. */
20711 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20712
20713 compute_line_metrics (&it);
20714 it.glyph_row->full_width_p = 1;
20715 it.glyph_row->continued_p = 0;
20716 it.glyph_row->truncated_on_left_p = 0;
20717 it.glyph_row->truncated_on_right_p = 0;
20718
20719 /* Make a 3D mode-line have a shadow at its right end. */
20720 face = FACE_FROM_ID (it.f, face_id);
20721 extend_face_to_end_of_line (&it);
20722 if (face->box != FACE_NO_BOX)
20723 {
20724 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20725 + it.glyph_row->used[TEXT_AREA] - 1);
20726 last->right_box_line_p = 1;
20727 }
20728
20729 return it.glyph_row->height;
20730 }
20731
20732 /* Move element ELT in LIST to the front of LIST.
20733 Return the updated list. */
20734
20735 static Lisp_Object
20736 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20737 {
20738 register Lisp_Object tail, prev;
20739 register Lisp_Object tem;
20740
20741 tail = list;
20742 prev = Qnil;
20743 while (CONSP (tail))
20744 {
20745 tem = XCAR (tail);
20746
20747 if (EQ (elt, tem))
20748 {
20749 /* Splice out the link TAIL. */
20750 if (NILP (prev))
20751 list = XCDR (tail);
20752 else
20753 Fsetcdr (prev, XCDR (tail));
20754
20755 /* Now make it the first. */
20756 Fsetcdr (tail, list);
20757 return tail;
20758 }
20759 else
20760 prev = tail;
20761 tail = XCDR (tail);
20762 QUIT;
20763 }
20764
20765 /* Not found--return unchanged LIST. */
20766 return list;
20767 }
20768
20769 /* Contribute ELT to the mode line for window IT->w. How it
20770 translates into text depends on its data type.
20771
20772 IT describes the display environment in which we display, as usual.
20773
20774 DEPTH is the depth in recursion. It is used to prevent
20775 infinite recursion here.
20776
20777 FIELD_WIDTH is the number of characters the display of ELT should
20778 occupy in the mode line, and PRECISION is the maximum number of
20779 characters to display from ELT's representation. See
20780 display_string for details.
20781
20782 Returns the hpos of the end of the text generated by ELT.
20783
20784 PROPS is a property list to add to any string we encounter.
20785
20786 If RISKY is nonzero, remove (disregard) any properties in any string
20787 we encounter, and ignore :eval and :propertize.
20788
20789 The global variable `mode_line_target' determines whether the
20790 output is passed to `store_mode_line_noprop',
20791 `store_mode_line_string', or `display_string'. */
20792
20793 static int
20794 display_mode_element (struct it *it, int depth, int field_width, int precision,
20795 Lisp_Object elt, Lisp_Object props, int risky)
20796 {
20797 int n = 0, field, prec;
20798 int literal = 0;
20799
20800 tail_recurse:
20801 if (depth > 100)
20802 elt = build_string ("*too-deep*");
20803
20804 depth++;
20805
20806 switch (XTYPE (elt))
20807 {
20808 case Lisp_String:
20809 {
20810 /* A string: output it and check for %-constructs within it. */
20811 unsigned char c;
20812 ptrdiff_t offset = 0;
20813
20814 if (SCHARS (elt) > 0
20815 && (!NILP (props) || risky))
20816 {
20817 Lisp_Object oprops, aelt;
20818 oprops = Ftext_properties_at (make_number (0), elt);
20819
20820 /* If the starting string's properties are not what
20821 we want, translate the string. Also, if the string
20822 is risky, do that anyway. */
20823
20824 if (NILP (Fequal (props, oprops)) || risky)
20825 {
20826 /* If the starting string has properties,
20827 merge the specified ones onto the existing ones. */
20828 if (! NILP (oprops) && !risky)
20829 {
20830 Lisp_Object tem;
20831
20832 oprops = Fcopy_sequence (oprops);
20833 tem = props;
20834 while (CONSP (tem))
20835 {
20836 oprops = Fplist_put (oprops, XCAR (tem),
20837 XCAR (XCDR (tem)));
20838 tem = XCDR (XCDR (tem));
20839 }
20840 props = oprops;
20841 }
20842
20843 aelt = Fassoc (elt, mode_line_proptrans_alist);
20844 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20845 {
20846 /* AELT is what we want. Move it to the front
20847 without consing. */
20848 elt = XCAR (aelt);
20849 mode_line_proptrans_alist
20850 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20851 }
20852 else
20853 {
20854 Lisp_Object tem;
20855
20856 /* If AELT has the wrong props, it is useless.
20857 so get rid of it. */
20858 if (! NILP (aelt))
20859 mode_line_proptrans_alist
20860 = Fdelq (aelt, mode_line_proptrans_alist);
20861
20862 elt = Fcopy_sequence (elt);
20863 Fset_text_properties (make_number (0), Flength (elt),
20864 props, elt);
20865 /* Add this item to mode_line_proptrans_alist. */
20866 mode_line_proptrans_alist
20867 = Fcons (Fcons (elt, props),
20868 mode_line_proptrans_alist);
20869 /* Truncate mode_line_proptrans_alist
20870 to at most 50 elements. */
20871 tem = Fnthcdr (make_number (50),
20872 mode_line_proptrans_alist);
20873 if (! NILP (tem))
20874 XSETCDR (tem, Qnil);
20875 }
20876 }
20877 }
20878
20879 offset = 0;
20880
20881 if (literal)
20882 {
20883 prec = precision - n;
20884 switch (mode_line_target)
20885 {
20886 case MODE_LINE_NOPROP:
20887 case MODE_LINE_TITLE:
20888 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20889 break;
20890 case MODE_LINE_STRING:
20891 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20892 break;
20893 case MODE_LINE_DISPLAY:
20894 n += display_string (NULL, elt, Qnil, 0, 0, it,
20895 0, prec, 0, STRING_MULTIBYTE (elt));
20896 break;
20897 }
20898
20899 break;
20900 }
20901
20902 /* Handle the non-literal case. */
20903
20904 while ((precision <= 0 || n < precision)
20905 && SREF (elt, offset) != 0
20906 && (mode_line_target != MODE_LINE_DISPLAY
20907 || it->current_x < it->last_visible_x))
20908 {
20909 ptrdiff_t last_offset = offset;
20910
20911 /* Advance to end of string or next format specifier. */
20912 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20913 ;
20914
20915 if (offset - 1 != last_offset)
20916 {
20917 ptrdiff_t nchars, nbytes;
20918
20919 /* Output to end of string or up to '%'. Field width
20920 is length of string. Don't output more than
20921 PRECISION allows us. */
20922 offset--;
20923
20924 prec = c_string_width (SDATA (elt) + last_offset,
20925 offset - last_offset, precision - n,
20926 &nchars, &nbytes);
20927
20928 switch (mode_line_target)
20929 {
20930 case MODE_LINE_NOPROP:
20931 case MODE_LINE_TITLE:
20932 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20933 break;
20934 case MODE_LINE_STRING:
20935 {
20936 ptrdiff_t bytepos = last_offset;
20937 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20938 ptrdiff_t endpos = (precision <= 0
20939 ? string_byte_to_char (elt, offset)
20940 : charpos + nchars);
20941
20942 n += store_mode_line_string (NULL,
20943 Fsubstring (elt, make_number (charpos),
20944 make_number (endpos)),
20945 0, 0, 0, Qnil);
20946 }
20947 break;
20948 case MODE_LINE_DISPLAY:
20949 {
20950 ptrdiff_t bytepos = last_offset;
20951 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20952
20953 if (precision <= 0)
20954 nchars = string_byte_to_char (elt, offset) - charpos;
20955 n += display_string (NULL, elt, Qnil, 0, charpos,
20956 it, 0, nchars, 0,
20957 STRING_MULTIBYTE (elt));
20958 }
20959 break;
20960 }
20961 }
20962 else /* c == '%' */
20963 {
20964 ptrdiff_t percent_position = offset;
20965
20966 /* Get the specified minimum width. Zero means
20967 don't pad. */
20968 field = 0;
20969 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20970 field = field * 10 + c - '0';
20971
20972 /* Don't pad beyond the total padding allowed. */
20973 if (field_width - n > 0 && field > field_width - n)
20974 field = field_width - n;
20975
20976 /* Note that either PRECISION <= 0 or N < PRECISION. */
20977 prec = precision - n;
20978
20979 if (c == 'M')
20980 n += display_mode_element (it, depth, field, prec,
20981 Vglobal_mode_string, props,
20982 risky);
20983 else if (c != 0)
20984 {
20985 bool multibyte;
20986 ptrdiff_t bytepos, charpos;
20987 const char *spec;
20988 Lisp_Object string;
20989
20990 bytepos = percent_position;
20991 charpos = (STRING_MULTIBYTE (elt)
20992 ? string_byte_to_char (elt, bytepos)
20993 : bytepos);
20994 spec = decode_mode_spec (it->w, c, field, &string);
20995 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20996
20997 switch (mode_line_target)
20998 {
20999 case MODE_LINE_NOPROP:
21000 case MODE_LINE_TITLE:
21001 n += store_mode_line_noprop (spec, field, prec);
21002 break;
21003 case MODE_LINE_STRING:
21004 {
21005 Lisp_Object tem = build_string (spec);
21006 props = Ftext_properties_at (make_number (charpos), elt);
21007 /* Should only keep face property in props */
21008 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21009 }
21010 break;
21011 case MODE_LINE_DISPLAY:
21012 {
21013 int nglyphs_before, nwritten;
21014
21015 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21016 nwritten = display_string (spec, string, elt,
21017 charpos, 0, it,
21018 field, prec, 0,
21019 multibyte);
21020
21021 /* Assign to the glyphs written above the
21022 string where the `%x' came from, position
21023 of the `%'. */
21024 if (nwritten > 0)
21025 {
21026 struct glyph *glyph
21027 = (it->glyph_row->glyphs[TEXT_AREA]
21028 + nglyphs_before);
21029 int i;
21030
21031 for (i = 0; i < nwritten; ++i)
21032 {
21033 glyph[i].object = elt;
21034 glyph[i].charpos = charpos;
21035 }
21036
21037 n += nwritten;
21038 }
21039 }
21040 break;
21041 }
21042 }
21043 else /* c == 0 */
21044 break;
21045 }
21046 }
21047 }
21048 break;
21049
21050 case Lisp_Symbol:
21051 /* A symbol: process the value of the symbol recursively
21052 as if it appeared here directly. Avoid error if symbol void.
21053 Special case: if value of symbol is a string, output the string
21054 literally. */
21055 {
21056 register Lisp_Object tem;
21057
21058 /* If the variable is not marked as risky to set
21059 then its contents are risky to use. */
21060 if (NILP (Fget (elt, Qrisky_local_variable)))
21061 risky = 1;
21062
21063 tem = Fboundp (elt);
21064 if (!NILP (tem))
21065 {
21066 tem = Fsymbol_value (elt);
21067 /* If value is a string, output that string literally:
21068 don't check for % within it. */
21069 if (STRINGP (tem))
21070 literal = 1;
21071
21072 if (!EQ (tem, elt))
21073 {
21074 /* Give up right away for nil or t. */
21075 elt = tem;
21076 goto tail_recurse;
21077 }
21078 }
21079 }
21080 break;
21081
21082 case Lisp_Cons:
21083 {
21084 register Lisp_Object car, tem;
21085
21086 /* A cons cell: five distinct cases.
21087 If first element is :eval or :propertize, do something special.
21088 If first element is a string or a cons, process all the elements
21089 and effectively concatenate them.
21090 If first element is a negative number, truncate displaying cdr to
21091 at most that many characters. If positive, pad (with spaces)
21092 to at least that many characters.
21093 If first element is a symbol, process the cadr or caddr recursively
21094 according to whether the symbol's value is non-nil or nil. */
21095 car = XCAR (elt);
21096 if (EQ (car, QCeval))
21097 {
21098 /* An element of the form (:eval FORM) means evaluate FORM
21099 and use the result as mode line elements. */
21100
21101 if (risky)
21102 break;
21103
21104 if (CONSP (XCDR (elt)))
21105 {
21106 Lisp_Object spec;
21107 spec = safe_eval (XCAR (XCDR (elt)));
21108 n += display_mode_element (it, depth, field_width - n,
21109 precision - n, spec, props,
21110 risky);
21111 }
21112 }
21113 else if (EQ (car, QCpropertize))
21114 {
21115 /* An element of the form (:propertize ELT PROPS...)
21116 means display ELT but applying properties PROPS. */
21117
21118 if (risky)
21119 break;
21120
21121 if (CONSP (XCDR (elt)))
21122 n += display_mode_element (it, depth, field_width - n,
21123 precision - n, XCAR (XCDR (elt)),
21124 XCDR (XCDR (elt)), risky);
21125 }
21126 else if (SYMBOLP (car))
21127 {
21128 tem = Fboundp (car);
21129 elt = XCDR (elt);
21130 if (!CONSP (elt))
21131 goto invalid;
21132 /* elt is now the cdr, and we know it is a cons cell.
21133 Use its car if CAR has a non-nil value. */
21134 if (!NILP (tem))
21135 {
21136 tem = Fsymbol_value (car);
21137 if (!NILP (tem))
21138 {
21139 elt = XCAR (elt);
21140 goto tail_recurse;
21141 }
21142 }
21143 /* Symbol's value is nil (or symbol is unbound)
21144 Get the cddr of the original list
21145 and if possible find the caddr and use that. */
21146 elt = XCDR (elt);
21147 if (NILP (elt))
21148 break;
21149 else if (!CONSP (elt))
21150 goto invalid;
21151 elt = XCAR (elt);
21152 goto tail_recurse;
21153 }
21154 else if (INTEGERP (car))
21155 {
21156 register int lim = XINT (car);
21157 elt = XCDR (elt);
21158 if (lim < 0)
21159 {
21160 /* Negative int means reduce maximum width. */
21161 if (precision <= 0)
21162 precision = -lim;
21163 else
21164 precision = min (precision, -lim);
21165 }
21166 else if (lim > 0)
21167 {
21168 /* Padding specified. Don't let it be more than
21169 current maximum. */
21170 if (precision > 0)
21171 lim = min (precision, lim);
21172
21173 /* If that's more padding than already wanted, queue it.
21174 But don't reduce padding already specified even if
21175 that is beyond the current truncation point. */
21176 field_width = max (lim, field_width);
21177 }
21178 goto tail_recurse;
21179 }
21180 else if (STRINGP (car) || CONSP (car))
21181 {
21182 Lisp_Object halftail = elt;
21183 int len = 0;
21184
21185 while (CONSP (elt)
21186 && (precision <= 0 || n < precision))
21187 {
21188 n += display_mode_element (it, depth,
21189 /* Do padding only after the last
21190 element in the list. */
21191 (! CONSP (XCDR (elt))
21192 ? field_width - n
21193 : 0),
21194 precision - n, XCAR (elt),
21195 props, risky);
21196 elt = XCDR (elt);
21197 len++;
21198 if ((len & 1) == 0)
21199 halftail = XCDR (halftail);
21200 /* Check for cycle. */
21201 if (EQ (halftail, elt))
21202 break;
21203 }
21204 }
21205 }
21206 break;
21207
21208 default:
21209 invalid:
21210 elt = build_string ("*invalid*");
21211 goto tail_recurse;
21212 }
21213
21214 /* Pad to FIELD_WIDTH. */
21215 if (field_width > 0 && n < field_width)
21216 {
21217 switch (mode_line_target)
21218 {
21219 case MODE_LINE_NOPROP:
21220 case MODE_LINE_TITLE:
21221 n += store_mode_line_noprop ("", field_width - n, 0);
21222 break;
21223 case MODE_LINE_STRING:
21224 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21225 break;
21226 case MODE_LINE_DISPLAY:
21227 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21228 0, 0, 0);
21229 break;
21230 }
21231 }
21232
21233 return n;
21234 }
21235
21236 /* Store a mode-line string element in mode_line_string_list.
21237
21238 If STRING is non-null, display that C string. Otherwise, the Lisp
21239 string LISP_STRING is displayed.
21240
21241 FIELD_WIDTH is the minimum number of output glyphs to produce.
21242 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21243 with spaces. FIELD_WIDTH <= 0 means don't pad.
21244
21245 PRECISION is the maximum number of characters to output from
21246 STRING. PRECISION <= 0 means don't truncate the string.
21247
21248 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21249 properties to the string.
21250
21251 PROPS are the properties to add to the string.
21252 The mode_line_string_face face property is always added to the string.
21253 */
21254
21255 static int
21256 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21257 int field_width, int precision, Lisp_Object props)
21258 {
21259 ptrdiff_t len;
21260 int n = 0;
21261
21262 if (string != NULL)
21263 {
21264 len = strlen (string);
21265 if (precision > 0 && len > precision)
21266 len = precision;
21267 lisp_string = make_string (string, len);
21268 if (NILP (props))
21269 props = mode_line_string_face_prop;
21270 else if (!NILP (mode_line_string_face))
21271 {
21272 Lisp_Object face = Fplist_get (props, Qface);
21273 props = Fcopy_sequence (props);
21274 if (NILP (face))
21275 face = mode_line_string_face;
21276 else
21277 face = list2 (face, mode_line_string_face);
21278 props = Fplist_put (props, Qface, face);
21279 }
21280 Fadd_text_properties (make_number (0), make_number (len),
21281 props, lisp_string);
21282 }
21283 else
21284 {
21285 len = XFASTINT (Flength (lisp_string));
21286 if (precision > 0 && len > precision)
21287 {
21288 len = precision;
21289 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21290 precision = -1;
21291 }
21292 if (!NILP (mode_line_string_face))
21293 {
21294 Lisp_Object face;
21295 if (NILP (props))
21296 props = Ftext_properties_at (make_number (0), lisp_string);
21297 face = Fplist_get (props, Qface);
21298 if (NILP (face))
21299 face = mode_line_string_face;
21300 else
21301 face = list2 (face, mode_line_string_face);
21302 props = list2 (Qface, face);
21303 if (copy_string)
21304 lisp_string = Fcopy_sequence (lisp_string);
21305 }
21306 if (!NILP (props))
21307 Fadd_text_properties (make_number (0), make_number (len),
21308 props, lisp_string);
21309 }
21310
21311 if (len > 0)
21312 {
21313 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21314 n += len;
21315 }
21316
21317 if (field_width > len)
21318 {
21319 field_width -= len;
21320 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21321 if (!NILP (props))
21322 Fadd_text_properties (make_number (0), make_number (field_width),
21323 props, lisp_string);
21324 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21325 n += field_width;
21326 }
21327
21328 return n;
21329 }
21330
21331
21332 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21333 1, 4, 0,
21334 doc: /* Format a string out of a mode line format specification.
21335 First arg FORMAT specifies the mode line format (see `mode-line-format'
21336 for details) to use.
21337
21338 By default, the format is evaluated for the currently selected window.
21339
21340 Optional second arg FACE specifies the face property to put on all
21341 characters for which no face is specified. The value nil means the
21342 default face. The value t means whatever face the window's mode line
21343 currently uses (either `mode-line' or `mode-line-inactive',
21344 depending on whether the window is the selected window or not).
21345 An integer value means the value string has no text
21346 properties.
21347
21348 Optional third and fourth args WINDOW and BUFFER specify the window
21349 and buffer to use as the context for the formatting (defaults
21350 are the selected window and the WINDOW's buffer). */)
21351 (Lisp_Object format, Lisp_Object face,
21352 Lisp_Object window, Lisp_Object buffer)
21353 {
21354 struct it it;
21355 int len;
21356 struct window *w;
21357 struct buffer *old_buffer = NULL;
21358 int face_id;
21359 int no_props = INTEGERP (face);
21360 ptrdiff_t count = SPECPDL_INDEX ();
21361 Lisp_Object str;
21362 int string_start = 0;
21363
21364 w = decode_any_window (window);
21365 XSETWINDOW (window, w);
21366
21367 if (NILP (buffer))
21368 buffer = w->contents;
21369 CHECK_BUFFER (buffer);
21370
21371 /* Make formatting the modeline a non-op when noninteractive, otherwise
21372 there will be problems later caused by a partially initialized frame. */
21373 if (NILP (format) || noninteractive)
21374 return empty_unibyte_string;
21375
21376 if (no_props)
21377 face = Qnil;
21378
21379 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21380 : EQ (face, Qt) ? (EQ (window, selected_window)
21381 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21382 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21383 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21384 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21385 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21386 : DEFAULT_FACE_ID;
21387
21388 old_buffer = current_buffer;
21389
21390 /* Save things including mode_line_proptrans_alist,
21391 and set that to nil so that we don't alter the outer value. */
21392 record_unwind_protect (unwind_format_mode_line,
21393 format_mode_line_unwind_data
21394 (XFRAME (WINDOW_FRAME (w)),
21395 old_buffer, selected_window, 1));
21396 mode_line_proptrans_alist = Qnil;
21397
21398 Fselect_window (window, Qt);
21399 set_buffer_internal_1 (XBUFFER (buffer));
21400
21401 init_iterator (&it, w, -1, -1, NULL, face_id);
21402
21403 if (no_props)
21404 {
21405 mode_line_target = MODE_LINE_NOPROP;
21406 mode_line_string_face_prop = Qnil;
21407 mode_line_string_list = Qnil;
21408 string_start = MODE_LINE_NOPROP_LEN (0);
21409 }
21410 else
21411 {
21412 mode_line_target = MODE_LINE_STRING;
21413 mode_line_string_list = Qnil;
21414 mode_line_string_face = face;
21415 mode_line_string_face_prop
21416 = NILP (face) ? Qnil : list2 (Qface, face);
21417 }
21418
21419 push_kboard (FRAME_KBOARD (it.f));
21420 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21421 pop_kboard ();
21422
21423 if (no_props)
21424 {
21425 len = MODE_LINE_NOPROP_LEN (string_start);
21426 str = make_string (mode_line_noprop_buf + string_start, len);
21427 }
21428 else
21429 {
21430 mode_line_string_list = Fnreverse (mode_line_string_list);
21431 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21432 empty_unibyte_string);
21433 }
21434
21435 unbind_to (count, Qnil);
21436 return str;
21437 }
21438
21439 /* Write a null-terminated, right justified decimal representation of
21440 the positive integer D to BUF using a minimal field width WIDTH. */
21441
21442 static void
21443 pint2str (register char *buf, register int width, register ptrdiff_t d)
21444 {
21445 register char *p = buf;
21446
21447 if (d <= 0)
21448 *p++ = '0';
21449 else
21450 {
21451 while (d > 0)
21452 {
21453 *p++ = d % 10 + '0';
21454 d /= 10;
21455 }
21456 }
21457
21458 for (width -= (int) (p - buf); width > 0; --width)
21459 *p++ = ' ';
21460 *p-- = '\0';
21461 while (p > buf)
21462 {
21463 d = *buf;
21464 *buf++ = *p;
21465 *p-- = d;
21466 }
21467 }
21468
21469 /* Write a null-terminated, right justified decimal and "human
21470 readable" representation of the nonnegative integer D to BUF using
21471 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21472
21473 static const char power_letter[] =
21474 {
21475 0, /* no letter */
21476 'k', /* kilo */
21477 'M', /* mega */
21478 'G', /* giga */
21479 'T', /* tera */
21480 'P', /* peta */
21481 'E', /* exa */
21482 'Z', /* zetta */
21483 'Y' /* yotta */
21484 };
21485
21486 static void
21487 pint2hrstr (char *buf, int width, ptrdiff_t d)
21488 {
21489 /* We aim to represent the nonnegative integer D as
21490 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21491 ptrdiff_t quotient = d;
21492 int remainder = 0;
21493 /* -1 means: do not use TENTHS. */
21494 int tenths = -1;
21495 int exponent = 0;
21496
21497 /* Length of QUOTIENT.TENTHS as a string. */
21498 int length;
21499
21500 char * psuffix;
21501 char * p;
21502
21503 if (quotient >= 1000)
21504 {
21505 /* Scale to the appropriate EXPONENT. */
21506 do
21507 {
21508 remainder = quotient % 1000;
21509 quotient /= 1000;
21510 exponent++;
21511 }
21512 while (quotient >= 1000);
21513
21514 /* Round to nearest and decide whether to use TENTHS or not. */
21515 if (quotient <= 9)
21516 {
21517 tenths = remainder / 100;
21518 if (remainder % 100 >= 50)
21519 {
21520 if (tenths < 9)
21521 tenths++;
21522 else
21523 {
21524 quotient++;
21525 if (quotient == 10)
21526 tenths = -1;
21527 else
21528 tenths = 0;
21529 }
21530 }
21531 }
21532 else
21533 if (remainder >= 500)
21534 {
21535 if (quotient < 999)
21536 quotient++;
21537 else
21538 {
21539 quotient = 1;
21540 exponent++;
21541 tenths = 0;
21542 }
21543 }
21544 }
21545
21546 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21547 if (tenths == -1 && quotient <= 99)
21548 if (quotient <= 9)
21549 length = 1;
21550 else
21551 length = 2;
21552 else
21553 length = 3;
21554 p = psuffix = buf + max (width, length);
21555
21556 /* Print EXPONENT. */
21557 *psuffix++ = power_letter[exponent];
21558 *psuffix = '\0';
21559
21560 /* Print TENTHS. */
21561 if (tenths >= 0)
21562 {
21563 *--p = '0' + tenths;
21564 *--p = '.';
21565 }
21566
21567 /* Print QUOTIENT. */
21568 do
21569 {
21570 int digit = quotient % 10;
21571 *--p = '0' + digit;
21572 }
21573 while ((quotient /= 10) != 0);
21574
21575 /* Print leading spaces. */
21576 while (buf < p)
21577 *--p = ' ';
21578 }
21579
21580 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21581 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21582 type of CODING_SYSTEM. Return updated pointer into BUF. */
21583
21584 static unsigned char invalid_eol_type[] = "(*invalid*)";
21585
21586 static char *
21587 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21588 {
21589 Lisp_Object val;
21590 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21591 const unsigned char *eol_str;
21592 int eol_str_len;
21593 /* The EOL conversion we are using. */
21594 Lisp_Object eoltype;
21595
21596 val = CODING_SYSTEM_SPEC (coding_system);
21597 eoltype = Qnil;
21598
21599 if (!VECTORP (val)) /* Not yet decided. */
21600 {
21601 *buf++ = multibyte ? '-' : ' ';
21602 if (eol_flag)
21603 eoltype = eol_mnemonic_undecided;
21604 /* Don't mention EOL conversion if it isn't decided. */
21605 }
21606 else
21607 {
21608 Lisp_Object attrs;
21609 Lisp_Object eolvalue;
21610
21611 attrs = AREF (val, 0);
21612 eolvalue = AREF (val, 2);
21613
21614 *buf++ = multibyte
21615 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21616 : ' ';
21617
21618 if (eol_flag)
21619 {
21620 /* The EOL conversion that is normal on this system. */
21621
21622 if (NILP (eolvalue)) /* Not yet decided. */
21623 eoltype = eol_mnemonic_undecided;
21624 else if (VECTORP (eolvalue)) /* Not yet decided. */
21625 eoltype = eol_mnemonic_undecided;
21626 else /* eolvalue is Qunix, Qdos, or Qmac. */
21627 eoltype = (EQ (eolvalue, Qunix)
21628 ? eol_mnemonic_unix
21629 : (EQ (eolvalue, Qdos) == 1
21630 ? eol_mnemonic_dos : eol_mnemonic_mac));
21631 }
21632 }
21633
21634 if (eol_flag)
21635 {
21636 /* Mention the EOL conversion if it is not the usual one. */
21637 if (STRINGP (eoltype))
21638 {
21639 eol_str = SDATA (eoltype);
21640 eol_str_len = SBYTES (eoltype);
21641 }
21642 else if (CHARACTERP (eoltype))
21643 {
21644 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21645 int c = XFASTINT (eoltype);
21646 eol_str_len = CHAR_STRING (c, tmp);
21647 eol_str = tmp;
21648 }
21649 else
21650 {
21651 eol_str = invalid_eol_type;
21652 eol_str_len = sizeof (invalid_eol_type) - 1;
21653 }
21654 memcpy (buf, eol_str, eol_str_len);
21655 buf += eol_str_len;
21656 }
21657
21658 return buf;
21659 }
21660
21661 /* Return a string for the output of a mode line %-spec for window W,
21662 generated by character C. FIELD_WIDTH > 0 means pad the string
21663 returned with spaces to that value. Return a Lisp string in
21664 *STRING if the resulting string is taken from that Lisp string.
21665
21666 Note we operate on the current buffer for most purposes. */
21667
21668 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21669
21670 static const char *
21671 decode_mode_spec (struct window *w, register int c, int field_width,
21672 Lisp_Object *string)
21673 {
21674 Lisp_Object obj;
21675 struct frame *f = XFRAME (WINDOW_FRAME (w));
21676 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21677 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21678 produce strings from numerical values, so limit preposterously
21679 large values of FIELD_WIDTH to avoid overrunning the buffer's
21680 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21681 bytes plus the terminating null. */
21682 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21683 struct buffer *b = current_buffer;
21684
21685 obj = Qnil;
21686 *string = Qnil;
21687
21688 switch (c)
21689 {
21690 case '*':
21691 if (!NILP (BVAR (b, read_only)))
21692 return "%";
21693 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21694 return "*";
21695 return "-";
21696
21697 case '+':
21698 /* This differs from %* only for a modified read-only buffer. */
21699 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21700 return "*";
21701 if (!NILP (BVAR (b, read_only)))
21702 return "%";
21703 return "-";
21704
21705 case '&':
21706 /* This differs from %* in ignoring read-only-ness. */
21707 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21708 return "*";
21709 return "-";
21710
21711 case '%':
21712 return "%";
21713
21714 case '[':
21715 {
21716 int i;
21717 char *p;
21718
21719 if (command_loop_level > 5)
21720 return "[[[... ";
21721 p = decode_mode_spec_buf;
21722 for (i = 0; i < command_loop_level; i++)
21723 *p++ = '[';
21724 *p = 0;
21725 return decode_mode_spec_buf;
21726 }
21727
21728 case ']':
21729 {
21730 int i;
21731 char *p;
21732
21733 if (command_loop_level > 5)
21734 return " ...]]]";
21735 p = decode_mode_spec_buf;
21736 for (i = 0; i < command_loop_level; i++)
21737 *p++ = ']';
21738 *p = 0;
21739 return decode_mode_spec_buf;
21740 }
21741
21742 case '-':
21743 {
21744 register int i;
21745
21746 /* Let lots_of_dashes be a string of infinite length. */
21747 if (mode_line_target == MODE_LINE_NOPROP
21748 || mode_line_target == MODE_LINE_STRING)
21749 return "--";
21750 if (field_width <= 0
21751 || field_width > sizeof (lots_of_dashes))
21752 {
21753 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21754 decode_mode_spec_buf[i] = '-';
21755 decode_mode_spec_buf[i] = '\0';
21756 return decode_mode_spec_buf;
21757 }
21758 else
21759 return lots_of_dashes;
21760 }
21761
21762 case 'b':
21763 obj = BVAR (b, name);
21764 break;
21765
21766 case 'c':
21767 /* %c and %l are ignored in `frame-title-format'.
21768 (In redisplay_internal, the frame title is drawn _before_ the
21769 windows are updated, so the stuff which depends on actual
21770 window contents (such as %l) may fail to render properly, or
21771 even crash emacs.) */
21772 if (mode_line_target == MODE_LINE_TITLE)
21773 return "";
21774 else
21775 {
21776 ptrdiff_t col = current_column ();
21777 w->column_number_displayed = col;
21778 pint2str (decode_mode_spec_buf, width, col);
21779 return decode_mode_spec_buf;
21780 }
21781
21782 case 'e':
21783 #ifndef SYSTEM_MALLOC
21784 {
21785 if (NILP (Vmemory_full))
21786 return "";
21787 else
21788 return "!MEM FULL! ";
21789 }
21790 #else
21791 return "";
21792 #endif
21793
21794 case 'F':
21795 /* %F displays the frame name. */
21796 if (!NILP (f->title))
21797 return SSDATA (f->title);
21798 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21799 return SSDATA (f->name);
21800 return "Emacs";
21801
21802 case 'f':
21803 obj = BVAR (b, filename);
21804 break;
21805
21806 case 'i':
21807 {
21808 ptrdiff_t size = ZV - BEGV;
21809 pint2str (decode_mode_spec_buf, width, size);
21810 return decode_mode_spec_buf;
21811 }
21812
21813 case 'I':
21814 {
21815 ptrdiff_t size = ZV - BEGV;
21816 pint2hrstr (decode_mode_spec_buf, width, size);
21817 return decode_mode_spec_buf;
21818 }
21819
21820 case 'l':
21821 {
21822 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21823 ptrdiff_t topline, nlines, height;
21824 ptrdiff_t junk;
21825
21826 /* %c and %l are ignored in `frame-title-format'. */
21827 if (mode_line_target == MODE_LINE_TITLE)
21828 return "";
21829
21830 startpos = marker_position (w->start);
21831 startpos_byte = marker_byte_position (w->start);
21832 height = WINDOW_TOTAL_LINES (w);
21833
21834 /* If we decided that this buffer isn't suitable for line numbers,
21835 don't forget that too fast. */
21836 if (w->base_line_pos == -1)
21837 goto no_value;
21838
21839 /* If the buffer is very big, don't waste time. */
21840 if (INTEGERP (Vline_number_display_limit)
21841 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21842 {
21843 w->base_line_pos = 0;
21844 w->base_line_number = 0;
21845 goto no_value;
21846 }
21847
21848 if (w->base_line_number > 0
21849 && w->base_line_pos > 0
21850 && w->base_line_pos <= startpos)
21851 {
21852 line = w->base_line_number;
21853 linepos = w->base_line_pos;
21854 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21855 }
21856 else
21857 {
21858 line = 1;
21859 linepos = BUF_BEGV (b);
21860 linepos_byte = BUF_BEGV_BYTE (b);
21861 }
21862
21863 /* Count lines from base line to window start position. */
21864 nlines = display_count_lines (linepos_byte,
21865 startpos_byte,
21866 startpos, &junk);
21867
21868 topline = nlines + line;
21869
21870 /* Determine a new base line, if the old one is too close
21871 or too far away, or if we did not have one.
21872 "Too close" means it's plausible a scroll-down would
21873 go back past it. */
21874 if (startpos == BUF_BEGV (b))
21875 {
21876 w->base_line_number = topline;
21877 w->base_line_pos = BUF_BEGV (b);
21878 }
21879 else if (nlines < height + 25 || nlines > height * 3 + 50
21880 || linepos == BUF_BEGV (b))
21881 {
21882 ptrdiff_t limit = BUF_BEGV (b);
21883 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21884 ptrdiff_t position;
21885 ptrdiff_t distance =
21886 (height * 2 + 30) * line_number_display_limit_width;
21887
21888 if (startpos - distance > limit)
21889 {
21890 limit = startpos - distance;
21891 limit_byte = CHAR_TO_BYTE (limit);
21892 }
21893
21894 nlines = display_count_lines (startpos_byte,
21895 limit_byte,
21896 - (height * 2 + 30),
21897 &position);
21898 /* If we couldn't find the lines we wanted within
21899 line_number_display_limit_width chars per line,
21900 give up on line numbers for this window. */
21901 if (position == limit_byte && limit == startpos - distance)
21902 {
21903 w->base_line_pos = -1;
21904 w->base_line_number = 0;
21905 goto no_value;
21906 }
21907
21908 w->base_line_number = topline - nlines;
21909 w->base_line_pos = BYTE_TO_CHAR (position);
21910 }
21911
21912 /* Now count lines from the start pos to point. */
21913 nlines = display_count_lines (startpos_byte,
21914 PT_BYTE, PT, &junk);
21915
21916 /* Record that we did display the line number. */
21917 line_number_displayed = 1;
21918
21919 /* Make the string to show. */
21920 pint2str (decode_mode_spec_buf, width, topline + nlines);
21921 return decode_mode_spec_buf;
21922 no_value:
21923 {
21924 char* p = decode_mode_spec_buf;
21925 int pad = width - 2;
21926 while (pad-- > 0)
21927 *p++ = ' ';
21928 *p++ = '?';
21929 *p++ = '?';
21930 *p = '\0';
21931 return decode_mode_spec_buf;
21932 }
21933 }
21934 break;
21935
21936 case 'm':
21937 obj = BVAR (b, mode_name);
21938 break;
21939
21940 case 'n':
21941 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21942 return " Narrow";
21943 break;
21944
21945 case 'p':
21946 {
21947 ptrdiff_t pos = marker_position (w->start);
21948 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21949
21950 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
21951 {
21952 if (pos <= BUF_BEGV (b))
21953 return "All";
21954 else
21955 return "Bottom";
21956 }
21957 else if (pos <= BUF_BEGV (b))
21958 return "Top";
21959 else
21960 {
21961 if (total > 1000000)
21962 /* Do it differently for a large value, to avoid overflow. */
21963 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21964 else
21965 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21966 /* We can't normally display a 3-digit number,
21967 so get us a 2-digit number that is close. */
21968 if (total == 100)
21969 total = 99;
21970 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21971 return decode_mode_spec_buf;
21972 }
21973 }
21974
21975 /* Display percentage of size above the bottom of the screen. */
21976 case 'P':
21977 {
21978 ptrdiff_t toppos = marker_position (w->start);
21979 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
21980 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21981
21982 if (botpos >= BUF_ZV (b))
21983 {
21984 if (toppos <= BUF_BEGV (b))
21985 return "All";
21986 else
21987 return "Bottom";
21988 }
21989 else
21990 {
21991 if (total > 1000000)
21992 /* Do it differently for a large value, to avoid overflow. */
21993 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21994 else
21995 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21996 /* We can't normally display a 3-digit number,
21997 so get us a 2-digit number that is close. */
21998 if (total == 100)
21999 total = 99;
22000 if (toppos <= BUF_BEGV (b))
22001 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22002 else
22003 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22004 return decode_mode_spec_buf;
22005 }
22006 }
22007
22008 case 's':
22009 /* status of process */
22010 obj = Fget_buffer_process (Fcurrent_buffer ());
22011 if (NILP (obj))
22012 return "no process";
22013 #ifndef MSDOS
22014 obj = Fsymbol_name (Fprocess_status (obj));
22015 #endif
22016 break;
22017
22018 case '@':
22019 {
22020 ptrdiff_t count = inhibit_garbage_collection ();
22021 Lisp_Object val = call1 (intern ("file-remote-p"),
22022 BVAR (current_buffer, directory));
22023 unbind_to (count, Qnil);
22024
22025 if (NILP (val))
22026 return "-";
22027 else
22028 return "@";
22029 }
22030
22031 case 'z':
22032 /* coding-system (not including end-of-line format) */
22033 case 'Z':
22034 /* coding-system (including end-of-line type) */
22035 {
22036 int eol_flag = (c == 'Z');
22037 char *p = decode_mode_spec_buf;
22038
22039 if (! FRAME_WINDOW_P (f))
22040 {
22041 /* No need to mention EOL here--the terminal never needs
22042 to do EOL conversion. */
22043 p = decode_mode_spec_coding (CODING_ID_NAME
22044 (FRAME_KEYBOARD_CODING (f)->id),
22045 p, 0);
22046 p = decode_mode_spec_coding (CODING_ID_NAME
22047 (FRAME_TERMINAL_CODING (f)->id),
22048 p, 0);
22049 }
22050 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22051 p, eol_flag);
22052
22053 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22054 #ifdef subprocesses
22055 obj = Fget_buffer_process (Fcurrent_buffer ());
22056 if (PROCESSP (obj))
22057 {
22058 p = decode_mode_spec_coding
22059 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22060 p = decode_mode_spec_coding
22061 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22062 }
22063 #endif /* subprocesses */
22064 #endif /* 0 */
22065 *p = 0;
22066 return decode_mode_spec_buf;
22067 }
22068 }
22069
22070 if (STRINGP (obj))
22071 {
22072 *string = obj;
22073 return SSDATA (obj);
22074 }
22075 else
22076 return "";
22077 }
22078
22079
22080 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22081 means count lines back from START_BYTE. But don't go beyond
22082 LIMIT_BYTE. Return the number of lines thus found (always
22083 nonnegative).
22084
22085 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22086 either the position COUNT lines after/before START_BYTE, if we
22087 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22088 COUNT lines. */
22089
22090 static ptrdiff_t
22091 display_count_lines (ptrdiff_t start_byte,
22092 ptrdiff_t limit_byte, ptrdiff_t count,
22093 ptrdiff_t *byte_pos_ptr)
22094 {
22095 register unsigned char *cursor;
22096 unsigned char *base;
22097
22098 register ptrdiff_t ceiling;
22099 register unsigned char *ceiling_addr;
22100 ptrdiff_t orig_count = count;
22101
22102 /* If we are not in selective display mode,
22103 check only for newlines. */
22104 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22105 && !INTEGERP (BVAR (current_buffer, selective_display)));
22106
22107 if (count > 0)
22108 {
22109 while (start_byte < limit_byte)
22110 {
22111 ceiling = BUFFER_CEILING_OF (start_byte);
22112 ceiling = min (limit_byte - 1, ceiling);
22113 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22114 base = (cursor = BYTE_POS_ADDR (start_byte));
22115
22116 do
22117 {
22118 if (selective_display)
22119 {
22120 while (*cursor != '\n' && *cursor != 015
22121 && ++cursor != ceiling_addr)
22122 continue;
22123 if (cursor == ceiling_addr)
22124 break;
22125 }
22126 else
22127 {
22128 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22129 if (! cursor)
22130 break;
22131 }
22132
22133 cursor++;
22134
22135 if (--count == 0)
22136 {
22137 start_byte += cursor - base;
22138 *byte_pos_ptr = start_byte;
22139 return orig_count;
22140 }
22141 }
22142 while (cursor < ceiling_addr);
22143
22144 start_byte += ceiling_addr - base;
22145 }
22146 }
22147 else
22148 {
22149 while (start_byte > limit_byte)
22150 {
22151 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22152 ceiling = max (limit_byte, ceiling);
22153 ceiling_addr = BYTE_POS_ADDR (ceiling);
22154 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22155 while (1)
22156 {
22157 if (selective_display)
22158 {
22159 while (--cursor >= ceiling_addr
22160 && *cursor != '\n' && *cursor != 015)
22161 continue;
22162 if (cursor < ceiling_addr)
22163 break;
22164 }
22165 else
22166 {
22167 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22168 if (! cursor)
22169 break;
22170 }
22171
22172 if (++count == 0)
22173 {
22174 start_byte += cursor - base + 1;
22175 *byte_pos_ptr = start_byte;
22176 /* When scanning backwards, we should
22177 not count the newline posterior to which we stop. */
22178 return - orig_count - 1;
22179 }
22180 }
22181 start_byte += ceiling_addr - base;
22182 }
22183 }
22184
22185 *byte_pos_ptr = limit_byte;
22186
22187 if (count < 0)
22188 return - orig_count + count;
22189 return orig_count - count;
22190
22191 }
22192
22193
22194 \f
22195 /***********************************************************************
22196 Displaying strings
22197 ***********************************************************************/
22198
22199 /* Display a NUL-terminated string, starting with index START.
22200
22201 If STRING is non-null, display that C string. Otherwise, the Lisp
22202 string LISP_STRING is displayed. There's a case that STRING is
22203 non-null and LISP_STRING is not nil. It means STRING is a string
22204 data of LISP_STRING. In that case, we display LISP_STRING while
22205 ignoring its text properties.
22206
22207 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22208 FACE_STRING. Display STRING or LISP_STRING with the face at
22209 FACE_STRING_POS in FACE_STRING:
22210
22211 Display the string in the environment given by IT, but use the
22212 standard display table, temporarily.
22213
22214 FIELD_WIDTH is the minimum number of output glyphs to produce.
22215 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22216 with spaces. If STRING has more characters, more than FIELD_WIDTH
22217 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22218
22219 PRECISION is the maximum number of characters to output from
22220 STRING. PRECISION < 0 means don't truncate the string.
22221
22222 This is roughly equivalent to printf format specifiers:
22223
22224 FIELD_WIDTH PRECISION PRINTF
22225 ----------------------------------------
22226 -1 -1 %s
22227 -1 10 %.10s
22228 10 -1 %10s
22229 20 10 %20.10s
22230
22231 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22232 display them, and < 0 means obey the current buffer's value of
22233 enable_multibyte_characters.
22234
22235 Value is the number of columns displayed. */
22236
22237 static int
22238 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22239 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22240 int field_width, int precision, int max_x, int multibyte)
22241 {
22242 int hpos_at_start = it->hpos;
22243 int saved_face_id = it->face_id;
22244 struct glyph_row *row = it->glyph_row;
22245 ptrdiff_t it_charpos;
22246
22247 /* Initialize the iterator IT for iteration over STRING beginning
22248 with index START. */
22249 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22250 precision, field_width, multibyte);
22251 if (string && STRINGP (lisp_string))
22252 /* LISP_STRING is the one returned by decode_mode_spec. We should
22253 ignore its text properties. */
22254 it->stop_charpos = it->end_charpos;
22255
22256 /* If displaying STRING, set up the face of the iterator from
22257 FACE_STRING, if that's given. */
22258 if (STRINGP (face_string))
22259 {
22260 ptrdiff_t endptr;
22261 struct face *face;
22262
22263 it->face_id
22264 = face_at_string_position (it->w, face_string, face_string_pos,
22265 0, it->region_beg_charpos,
22266 it->region_end_charpos,
22267 &endptr, it->base_face_id, 0);
22268 face = FACE_FROM_ID (it->f, it->face_id);
22269 it->face_box_p = face->box != FACE_NO_BOX;
22270 }
22271
22272 /* Set max_x to the maximum allowed X position. Don't let it go
22273 beyond the right edge of the window. */
22274 if (max_x <= 0)
22275 max_x = it->last_visible_x;
22276 else
22277 max_x = min (max_x, it->last_visible_x);
22278
22279 /* Skip over display elements that are not visible. because IT->w is
22280 hscrolled. */
22281 if (it->current_x < it->first_visible_x)
22282 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22283 MOVE_TO_POS | MOVE_TO_X);
22284
22285 row->ascent = it->max_ascent;
22286 row->height = it->max_ascent + it->max_descent;
22287 row->phys_ascent = it->max_phys_ascent;
22288 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22289 row->extra_line_spacing = it->max_extra_line_spacing;
22290
22291 if (STRINGP (it->string))
22292 it_charpos = IT_STRING_CHARPOS (*it);
22293 else
22294 it_charpos = IT_CHARPOS (*it);
22295
22296 /* This condition is for the case that we are called with current_x
22297 past last_visible_x. */
22298 while (it->current_x < max_x)
22299 {
22300 int x_before, x, n_glyphs_before, i, nglyphs;
22301
22302 /* Get the next display element. */
22303 if (!get_next_display_element (it))
22304 break;
22305
22306 /* Produce glyphs. */
22307 x_before = it->current_x;
22308 n_glyphs_before = row->used[TEXT_AREA];
22309 PRODUCE_GLYPHS (it);
22310
22311 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22312 i = 0;
22313 x = x_before;
22314 while (i < nglyphs)
22315 {
22316 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22317
22318 if (it->line_wrap != TRUNCATE
22319 && x + glyph->pixel_width > max_x)
22320 {
22321 /* End of continued line or max_x reached. */
22322 if (CHAR_GLYPH_PADDING_P (*glyph))
22323 {
22324 /* A wide character is unbreakable. */
22325 if (row->reversed_p)
22326 unproduce_glyphs (it, row->used[TEXT_AREA]
22327 - n_glyphs_before);
22328 row->used[TEXT_AREA] = n_glyphs_before;
22329 it->current_x = x_before;
22330 }
22331 else
22332 {
22333 if (row->reversed_p)
22334 unproduce_glyphs (it, row->used[TEXT_AREA]
22335 - (n_glyphs_before + i));
22336 row->used[TEXT_AREA] = n_glyphs_before + i;
22337 it->current_x = x;
22338 }
22339 break;
22340 }
22341 else if (x + glyph->pixel_width >= it->first_visible_x)
22342 {
22343 /* Glyph is at least partially visible. */
22344 ++it->hpos;
22345 if (x < it->first_visible_x)
22346 row->x = x - it->first_visible_x;
22347 }
22348 else
22349 {
22350 /* Glyph is off the left margin of the display area.
22351 Should not happen. */
22352 emacs_abort ();
22353 }
22354
22355 row->ascent = max (row->ascent, it->max_ascent);
22356 row->height = max (row->height, it->max_ascent + it->max_descent);
22357 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22358 row->phys_height = max (row->phys_height,
22359 it->max_phys_ascent + it->max_phys_descent);
22360 row->extra_line_spacing = max (row->extra_line_spacing,
22361 it->max_extra_line_spacing);
22362 x += glyph->pixel_width;
22363 ++i;
22364 }
22365
22366 /* Stop if max_x reached. */
22367 if (i < nglyphs)
22368 break;
22369
22370 /* Stop at line ends. */
22371 if (ITERATOR_AT_END_OF_LINE_P (it))
22372 {
22373 it->continuation_lines_width = 0;
22374 break;
22375 }
22376
22377 set_iterator_to_next (it, 1);
22378 if (STRINGP (it->string))
22379 it_charpos = IT_STRING_CHARPOS (*it);
22380 else
22381 it_charpos = IT_CHARPOS (*it);
22382
22383 /* Stop if truncating at the right edge. */
22384 if (it->line_wrap == TRUNCATE
22385 && it->current_x >= it->last_visible_x)
22386 {
22387 /* Add truncation mark, but don't do it if the line is
22388 truncated at a padding space. */
22389 if (it_charpos < it->string_nchars)
22390 {
22391 if (!FRAME_WINDOW_P (it->f))
22392 {
22393 int ii, n;
22394
22395 if (it->current_x > it->last_visible_x)
22396 {
22397 if (!row->reversed_p)
22398 {
22399 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22400 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22401 break;
22402 }
22403 else
22404 {
22405 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22406 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22407 break;
22408 unproduce_glyphs (it, ii + 1);
22409 ii = row->used[TEXT_AREA] - (ii + 1);
22410 }
22411 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22412 {
22413 row->used[TEXT_AREA] = ii;
22414 produce_special_glyphs (it, IT_TRUNCATION);
22415 }
22416 }
22417 produce_special_glyphs (it, IT_TRUNCATION);
22418 }
22419 row->truncated_on_right_p = 1;
22420 }
22421 break;
22422 }
22423 }
22424
22425 /* Maybe insert a truncation at the left. */
22426 if (it->first_visible_x
22427 && it_charpos > 0)
22428 {
22429 if (!FRAME_WINDOW_P (it->f)
22430 || (row->reversed_p
22431 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22432 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22433 insert_left_trunc_glyphs (it);
22434 row->truncated_on_left_p = 1;
22435 }
22436
22437 it->face_id = saved_face_id;
22438
22439 /* Value is number of columns displayed. */
22440 return it->hpos - hpos_at_start;
22441 }
22442
22443
22444 \f
22445 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22446 appears as an element of LIST or as the car of an element of LIST.
22447 If PROPVAL is a list, compare each element against LIST in that
22448 way, and return 1/2 if any element of PROPVAL is found in LIST.
22449 Otherwise return 0. This function cannot quit.
22450 The return value is 2 if the text is invisible but with an ellipsis
22451 and 1 if it's invisible and without an ellipsis. */
22452
22453 int
22454 invisible_p (register Lisp_Object propval, Lisp_Object list)
22455 {
22456 register Lisp_Object tail, proptail;
22457
22458 for (tail = list; CONSP (tail); tail = XCDR (tail))
22459 {
22460 register Lisp_Object tem;
22461 tem = XCAR (tail);
22462 if (EQ (propval, tem))
22463 return 1;
22464 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22465 return NILP (XCDR (tem)) ? 1 : 2;
22466 }
22467
22468 if (CONSP (propval))
22469 {
22470 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22471 {
22472 Lisp_Object propelt;
22473 propelt = XCAR (proptail);
22474 for (tail = list; CONSP (tail); tail = XCDR (tail))
22475 {
22476 register Lisp_Object tem;
22477 tem = XCAR (tail);
22478 if (EQ (propelt, tem))
22479 return 1;
22480 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22481 return NILP (XCDR (tem)) ? 1 : 2;
22482 }
22483 }
22484 }
22485
22486 return 0;
22487 }
22488
22489 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22490 doc: /* Non-nil if the property makes the text invisible.
22491 POS-OR-PROP can be a marker or number, in which case it is taken to be
22492 a position in the current buffer and the value of the `invisible' property
22493 is checked; or it can be some other value, which is then presumed to be the
22494 value of the `invisible' property of the text of interest.
22495 The non-nil value returned can be t for truly invisible text or something
22496 else if the text is replaced by an ellipsis. */)
22497 (Lisp_Object pos_or_prop)
22498 {
22499 Lisp_Object prop
22500 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22501 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22502 : pos_or_prop);
22503 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22504 return (invis == 0 ? Qnil
22505 : invis == 1 ? Qt
22506 : make_number (invis));
22507 }
22508
22509 /* Calculate a width or height in pixels from a specification using
22510 the following elements:
22511
22512 SPEC ::=
22513 NUM - a (fractional) multiple of the default font width/height
22514 (NUM) - specifies exactly NUM pixels
22515 UNIT - a fixed number of pixels, see below.
22516 ELEMENT - size of a display element in pixels, see below.
22517 (NUM . SPEC) - equals NUM * SPEC
22518 (+ SPEC SPEC ...) - add pixel values
22519 (- SPEC SPEC ...) - subtract pixel values
22520 (- SPEC) - negate pixel value
22521
22522 NUM ::=
22523 INT or FLOAT - a number constant
22524 SYMBOL - use symbol's (buffer local) variable binding.
22525
22526 UNIT ::=
22527 in - pixels per inch *)
22528 mm - pixels per 1/1000 meter *)
22529 cm - pixels per 1/100 meter *)
22530 width - width of current font in pixels.
22531 height - height of current font in pixels.
22532
22533 *) using the ratio(s) defined in display-pixels-per-inch.
22534
22535 ELEMENT ::=
22536
22537 left-fringe - left fringe width in pixels
22538 right-fringe - right fringe width in pixels
22539
22540 left-margin - left margin width in pixels
22541 right-margin - right margin width in pixels
22542
22543 scroll-bar - scroll-bar area width in pixels
22544
22545 Examples:
22546
22547 Pixels corresponding to 5 inches:
22548 (5 . in)
22549
22550 Total width of non-text areas on left side of window (if scroll-bar is on left):
22551 '(space :width (+ left-fringe left-margin scroll-bar))
22552
22553 Align to first text column (in header line):
22554 '(space :align-to 0)
22555
22556 Align to middle of text area minus half the width of variable `my-image'
22557 containing a loaded image:
22558 '(space :align-to (0.5 . (- text my-image)))
22559
22560 Width of left margin minus width of 1 character in the default font:
22561 '(space :width (- left-margin 1))
22562
22563 Width of left margin minus width of 2 characters in the current font:
22564 '(space :width (- left-margin (2 . width)))
22565
22566 Center 1 character over left-margin (in header line):
22567 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22568
22569 Different ways to express width of left fringe plus left margin minus one pixel:
22570 '(space :width (- (+ left-fringe left-margin) (1)))
22571 '(space :width (+ left-fringe left-margin (- (1))))
22572 '(space :width (+ left-fringe left-margin (-1)))
22573
22574 */
22575
22576 static int
22577 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22578 struct font *font, int width_p, int *align_to)
22579 {
22580 double pixels;
22581
22582 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22583 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22584
22585 if (NILP (prop))
22586 return OK_PIXELS (0);
22587
22588 eassert (FRAME_LIVE_P (it->f));
22589
22590 if (SYMBOLP (prop))
22591 {
22592 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22593 {
22594 char *unit = SSDATA (SYMBOL_NAME (prop));
22595
22596 if (unit[0] == 'i' && unit[1] == 'n')
22597 pixels = 1.0;
22598 else if (unit[0] == 'm' && unit[1] == 'm')
22599 pixels = 25.4;
22600 else if (unit[0] == 'c' && unit[1] == 'm')
22601 pixels = 2.54;
22602 else
22603 pixels = 0;
22604 if (pixels > 0)
22605 {
22606 double ppi = (width_p ? FRAME_RES_X (it->f)
22607 : FRAME_RES_Y (it->f));
22608
22609 if (ppi > 0)
22610 return OK_PIXELS (ppi / pixels);
22611 return 0;
22612 }
22613 }
22614
22615 #ifdef HAVE_WINDOW_SYSTEM
22616 if (EQ (prop, Qheight))
22617 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22618 if (EQ (prop, Qwidth))
22619 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22620 #else
22621 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22622 return OK_PIXELS (1);
22623 #endif
22624
22625 if (EQ (prop, Qtext))
22626 return OK_PIXELS (width_p
22627 ? window_box_width (it->w, TEXT_AREA)
22628 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22629
22630 if (align_to && *align_to < 0)
22631 {
22632 *res = 0;
22633 if (EQ (prop, Qleft))
22634 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22635 if (EQ (prop, Qright))
22636 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22637 if (EQ (prop, Qcenter))
22638 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22639 + window_box_width (it->w, TEXT_AREA) / 2);
22640 if (EQ (prop, Qleft_fringe))
22641 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22642 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22643 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22644 if (EQ (prop, Qright_fringe))
22645 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22646 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22647 : window_box_right_offset (it->w, TEXT_AREA));
22648 if (EQ (prop, Qleft_margin))
22649 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22650 if (EQ (prop, Qright_margin))
22651 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22652 if (EQ (prop, Qscroll_bar))
22653 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22654 ? 0
22655 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22656 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22657 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22658 : 0)));
22659 }
22660 else
22661 {
22662 if (EQ (prop, Qleft_fringe))
22663 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22664 if (EQ (prop, Qright_fringe))
22665 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22666 if (EQ (prop, Qleft_margin))
22667 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22668 if (EQ (prop, Qright_margin))
22669 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22670 if (EQ (prop, Qscroll_bar))
22671 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22672 }
22673
22674 prop = buffer_local_value_1 (prop, it->w->contents);
22675 if (EQ (prop, Qunbound))
22676 prop = Qnil;
22677 }
22678
22679 if (INTEGERP (prop) || FLOATP (prop))
22680 {
22681 int base_unit = (width_p
22682 ? FRAME_COLUMN_WIDTH (it->f)
22683 : FRAME_LINE_HEIGHT (it->f));
22684 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22685 }
22686
22687 if (CONSP (prop))
22688 {
22689 Lisp_Object car = XCAR (prop);
22690 Lisp_Object cdr = XCDR (prop);
22691
22692 if (SYMBOLP (car))
22693 {
22694 #ifdef HAVE_WINDOW_SYSTEM
22695 if (FRAME_WINDOW_P (it->f)
22696 && valid_image_p (prop))
22697 {
22698 ptrdiff_t id = lookup_image (it->f, prop);
22699 struct image *img = IMAGE_FROM_ID (it->f, id);
22700
22701 return OK_PIXELS (width_p ? img->width : img->height);
22702 }
22703 #endif
22704 if (EQ (car, Qplus) || EQ (car, Qminus))
22705 {
22706 int first = 1;
22707 double px;
22708
22709 pixels = 0;
22710 while (CONSP (cdr))
22711 {
22712 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22713 font, width_p, align_to))
22714 return 0;
22715 if (first)
22716 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22717 else
22718 pixels += px;
22719 cdr = XCDR (cdr);
22720 }
22721 if (EQ (car, Qminus))
22722 pixels = -pixels;
22723 return OK_PIXELS (pixels);
22724 }
22725
22726 car = buffer_local_value_1 (car, it->w->contents);
22727 if (EQ (car, Qunbound))
22728 car = Qnil;
22729 }
22730
22731 if (INTEGERP (car) || FLOATP (car))
22732 {
22733 double fact;
22734 pixels = XFLOATINT (car);
22735 if (NILP (cdr))
22736 return OK_PIXELS (pixels);
22737 if (calc_pixel_width_or_height (&fact, it, cdr,
22738 font, width_p, align_to))
22739 return OK_PIXELS (pixels * fact);
22740 return 0;
22741 }
22742
22743 return 0;
22744 }
22745
22746 return 0;
22747 }
22748
22749 \f
22750 /***********************************************************************
22751 Glyph Display
22752 ***********************************************************************/
22753
22754 #ifdef HAVE_WINDOW_SYSTEM
22755
22756 #ifdef GLYPH_DEBUG
22757
22758 void
22759 dump_glyph_string (struct glyph_string *s)
22760 {
22761 fprintf (stderr, "glyph string\n");
22762 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22763 s->x, s->y, s->width, s->height);
22764 fprintf (stderr, " ybase = %d\n", s->ybase);
22765 fprintf (stderr, " hl = %d\n", s->hl);
22766 fprintf (stderr, " left overhang = %d, right = %d\n",
22767 s->left_overhang, s->right_overhang);
22768 fprintf (stderr, " nchars = %d\n", s->nchars);
22769 fprintf (stderr, " extends to end of line = %d\n",
22770 s->extends_to_end_of_line_p);
22771 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22772 fprintf (stderr, " bg width = %d\n", s->background_width);
22773 }
22774
22775 #endif /* GLYPH_DEBUG */
22776
22777 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22778 of XChar2b structures for S; it can't be allocated in
22779 init_glyph_string because it must be allocated via `alloca'. W
22780 is the window on which S is drawn. ROW and AREA are the glyph row
22781 and area within the row from which S is constructed. START is the
22782 index of the first glyph structure covered by S. HL is a
22783 face-override for drawing S. */
22784
22785 #ifdef HAVE_NTGUI
22786 #define OPTIONAL_HDC(hdc) HDC hdc,
22787 #define DECLARE_HDC(hdc) HDC hdc;
22788 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22789 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22790 #endif
22791
22792 #ifndef OPTIONAL_HDC
22793 #define OPTIONAL_HDC(hdc)
22794 #define DECLARE_HDC(hdc)
22795 #define ALLOCATE_HDC(hdc, f)
22796 #define RELEASE_HDC(hdc, f)
22797 #endif
22798
22799 static void
22800 init_glyph_string (struct glyph_string *s,
22801 OPTIONAL_HDC (hdc)
22802 XChar2b *char2b, struct window *w, struct glyph_row *row,
22803 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22804 {
22805 memset (s, 0, sizeof *s);
22806 s->w = w;
22807 s->f = XFRAME (w->frame);
22808 #ifdef HAVE_NTGUI
22809 s->hdc = hdc;
22810 #endif
22811 s->display = FRAME_X_DISPLAY (s->f);
22812 s->window = FRAME_X_WINDOW (s->f);
22813 s->char2b = char2b;
22814 s->hl = hl;
22815 s->row = row;
22816 s->area = area;
22817 s->first_glyph = row->glyphs[area] + start;
22818 s->height = row->height;
22819 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22820 s->ybase = s->y + row->ascent;
22821 }
22822
22823
22824 /* Append the list of glyph strings with head H and tail T to the list
22825 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22826
22827 static void
22828 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22829 struct glyph_string *h, struct glyph_string *t)
22830 {
22831 if (h)
22832 {
22833 if (*head)
22834 (*tail)->next = h;
22835 else
22836 *head = h;
22837 h->prev = *tail;
22838 *tail = t;
22839 }
22840 }
22841
22842
22843 /* Prepend the list of glyph strings with head H and tail T to the
22844 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22845 result. */
22846
22847 static void
22848 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22849 struct glyph_string *h, struct glyph_string *t)
22850 {
22851 if (h)
22852 {
22853 if (*head)
22854 (*head)->prev = t;
22855 else
22856 *tail = t;
22857 t->next = *head;
22858 *head = h;
22859 }
22860 }
22861
22862
22863 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22864 Set *HEAD and *TAIL to the resulting list. */
22865
22866 static void
22867 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22868 struct glyph_string *s)
22869 {
22870 s->next = s->prev = NULL;
22871 append_glyph_string_lists (head, tail, s, s);
22872 }
22873
22874
22875 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22876 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22877 make sure that X resources for the face returned are allocated.
22878 Value is a pointer to a realized face that is ready for display if
22879 DISPLAY_P is non-zero. */
22880
22881 static struct face *
22882 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22883 XChar2b *char2b, int display_p)
22884 {
22885 struct face *face = FACE_FROM_ID (f, face_id);
22886 unsigned code = 0;
22887
22888 if (face->font)
22889 {
22890 code = face->font->driver->encode_char (face->font, c);
22891
22892 if (code == FONT_INVALID_CODE)
22893 code = 0;
22894 }
22895 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22896
22897 /* Make sure X resources of the face are allocated. */
22898 #ifdef HAVE_X_WINDOWS
22899 if (display_p)
22900 #endif
22901 {
22902 eassert (face != NULL);
22903 PREPARE_FACE_FOR_DISPLAY (f, face);
22904 }
22905
22906 return face;
22907 }
22908
22909
22910 /* Get face and two-byte form of character glyph GLYPH on frame F.
22911 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22912 a pointer to a realized face that is ready for display. */
22913
22914 static struct face *
22915 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22916 XChar2b *char2b, int *two_byte_p)
22917 {
22918 struct face *face;
22919 unsigned code = 0;
22920
22921 eassert (glyph->type == CHAR_GLYPH);
22922 face = FACE_FROM_ID (f, glyph->face_id);
22923
22924 /* Make sure X resources of the face are allocated. */
22925 eassert (face != NULL);
22926 PREPARE_FACE_FOR_DISPLAY (f, face);
22927
22928 if (two_byte_p)
22929 *two_byte_p = 0;
22930
22931 if (face->font)
22932 {
22933 if (CHAR_BYTE8_P (glyph->u.ch))
22934 code = CHAR_TO_BYTE8 (glyph->u.ch);
22935 else
22936 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22937
22938 if (code == FONT_INVALID_CODE)
22939 code = 0;
22940 }
22941
22942 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22943 return face;
22944 }
22945
22946
22947 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22948 Return 1 if FONT has a glyph for C, otherwise return 0. */
22949
22950 static int
22951 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22952 {
22953 unsigned code;
22954
22955 if (CHAR_BYTE8_P (c))
22956 code = CHAR_TO_BYTE8 (c);
22957 else
22958 code = font->driver->encode_char (font, c);
22959
22960 if (code == FONT_INVALID_CODE)
22961 return 0;
22962 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22963 return 1;
22964 }
22965
22966
22967 /* Fill glyph string S with composition components specified by S->cmp.
22968
22969 BASE_FACE is the base face of the composition.
22970 S->cmp_from is the index of the first component for S.
22971
22972 OVERLAPS non-zero means S should draw the foreground only, and use
22973 its physical height for clipping. See also draw_glyphs.
22974
22975 Value is the index of a component not in S. */
22976
22977 static int
22978 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22979 int overlaps)
22980 {
22981 int i;
22982 /* For all glyphs of this composition, starting at the offset
22983 S->cmp_from, until we reach the end of the definition or encounter a
22984 glyph that requires the different face, add it to S. */
22985 struct face *face;
22986
22987 eassert (s);
22988
22989 s->for_overlaps = overlaps;
22990 s->face = NULL;
22991 s->font = NULL;
22992 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22993 {
22994 int c = COMPOSITION_GLYPH (s->cmp, i);
22995
22996 /* TAB in a composition means display glyphs with padding space
22997 on the left or right. */
22998 if (c != '\t')
22999 {
23000 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23001 -1, Qnil);
23002
23003 face = get_char_face_and_encoding (s->f, c, face_id,
23004 s->char2b + i, 1);
23005 if (face)
23006 {
23007 if (! s->face)
23008 {
23009 s->face = face;
23010 s->font = s->face->font;
23011 }
23012 else if (s->face != face)
23013 break;
23014 }
23015 }
23016 ++s->nchars;
23017 }
23018 s->cmp_to = i;
23019
23020 if (s->face == NULL)
23021 {
23022 s->face = base_face->ascii_face;
23023 s->font = s->face->font;
23024 }
23025
23026 /* All glyph strings for the same composition has the same width,
23027 i.e. the width set for the first component of the composition. */
23028 s->width = s->first_glyph->pixel_width;
23029
23030 /* If the specified font could not be loaded, use the frame's
23031 default font, but record the fact that we couldn't load it in
23032 the glyph string so that we can draw rectangles for the
23033 characters of the glyph string. */
23034 if (s->font == NULL)
23035 {
23036 s->font_not_found_p = 1;
23037 s->font = FRAME_FONT (s->f);
23038 }
23039
23040 /* Adjust base line for subscript/superscript text. */
23041 s->ybase += s->first_glyph->voffset;
23042
23043 /* This glyph string must always be drawn with 16-bit functions. */
23044 s->two_byte_p = 1;
23045
23046 return s->cmp_to;
23047 }
23048
23049 static int
23050 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23051 int start, int end, int overlaps)
23052 {
23053 struct glyph *glyph, *last;
23054 Lisp_Object lgstring;
23055 int i;
23056
23057 s->for_overlaps = overlaps;
23058 glyph = s->row->glyphs[s->area] + start;
23059 last = s->row->glyphs[s->area] + end;
23060 s->cmp_id = glyph->u.cmp.id;
23061 s->cmp_from = glyph->slice.cmp.from;
23062 s->cmp_to = glyph->slice.cmp.to + 1;
23063 s->face = FACE_FROM_ID (s->f, face_id);
23064 lgstring = composition_gstring_from_id (s->cmp_id);
23065 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23066 glyph++;
23067 while (glyph < last
23068 && glyph->u.cmp.automatic
23069 && glyph->u.cmp.id == s->cmp_id
23070 && s->cmp_to == glyph->slice.cmp.from)
23071 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23072
23073 for (i = s->cmp_from; i < s->cmp_to; i++)
23074 {
23075 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23076 unsigned code = LGLYPH_CODE (lglyph);
23077
23078 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23079 }
23080 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23081 return glyph - s->row->glyphs[s->area];
23082 }
23083
23084
23085 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23086 See the comment of fill_glyph_string for arguments.
23087 Value is the index of the first glyph not in S. */
23088
23089
23090 static int
23091 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23092 int start, int end, int overlaps)
23093 {
23094 struct glyph *glyph, *last;
23095 int voffset;
23096
23097 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23098 s->for_overlaps = overlaps;
23099 glyph = s->row->glyphs[s->area] + start;
23100 last = s->row->glyphs[s->area] + end;
23101 voffset = glyph->voffset;
23102 s->face = FACE_FROM_ID (s->f, face_id);
23103 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23104 s->nchars = 1;
23105 s->width = glyph->pixel_width;
23106 glyph++;
23107 while (glyph < last
23108 && glyph->type == GLYPHLESS_GLYPH
23109 && glyph->voffset == voffset
23110 && glyph->face_id == face_id)
23111 {
23112 s->nchars++;
23113 s->width += glyph->pixel_width;
23114 glyph++;
23115 }
23116 s->ybase += voffset;
23117 return glyph - s->row->glyphs[s->area];
23118 }
23119
23120
23121 /* Fill glyph string S from a sequence of character glyphs.
23122
23123 FACE_ID is the face id of the string. START is the index of the
23124 first glyph to consider, END is the index of the last + 1.
23125 OVERLAPS non-zero means S should draw the foreground only, and use
23126 its physical height for clipping. See also draw_glyphs.
23127
23128 Value is the index of the first glyph not in S. */
23129
23130 static int
23131 fill_glyph_string (struct glyph_string *s, int face_id,
23132 int start, int end, int overlaps)
23133 {
23134 struct glyph *glyph, *last;
23135 int voffset;
23136 int glyph_not_available_p;
23137
23138 eassert (s->f == XFRAME (s->w->frame));
23139 eassert (s->nchars == 0);
23140 eassert (start >= 0 && end > start);
23141
23142 s->for_overlaps = overlaps;
23143 glyph = s->row->glyphs[s->area] + start;
23144 last = s->row->glyphs[s->area] + end;
23145 voffset = glyph->voffset;
23146 s->padding_p = glyph->padding_p;
23147 glyph_not_available_p = glyph->glyph_not_available_p;
23148
23149 while (glyph < last
23150 && glyph->type == CHAR_GLYPH
23151 && glyph->voffset == voffset
23152 /* Same face id implies same font, nowadays. */
23153 && glyph->face_id == face_id
23154 && glyph->glyph_not_available_p == glyph_not_available_p)
23155 {
23156 int two_byte_p;
23157
23158 s->face = get_glyph_face_and_encoding (s->f, glyph,
23159 s->char2b + s->nchars,
23160 &two_byte_p);
23161 s->two_byte_p = two_byte_p;
23162 ++s->nchars;
23163 eassert (s->nchars <= end - start);
23164 s->width += glyph->pixel_width;
23165 if (glyph++->padding_p != s->padding_p)
23166 break;
23167 }
23168
23169 s->font = s->face->font;
23170
23171 /* If the specified font could not be loaded, use the frame's font,
23172 but record the fact that we couldn't load it in
23173 S->font_not_found_p so that we can draw rectangles for the
23174 characters of the glyph string. */
23175 if (s->font == NULL || glyph_not_available_p)
23176 {
23177 s->font_not_found_p = 1;
23178 s->font = FRAME_FONT (s->f);
23179 }
23180
23181 /* Adjust base line for subscript/superscript text. */
23182 s->ybase += voffset;
23183
23184 eassert (s->face && s->face->gc);
23185 return glyph - s->row->glyphs[s->area];
23186 }
23187
23188
23189 /* Fill glyph string S from image glyph S->first_glyph. */
23190
23191 static void
23192 fill_image_glyph_string (struct glyph_string *s)
23193 {
23194 eassert (s->first_glyph->type == IMAGE_GLYPH);
23195 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23196 eassert (s->img);
23197 s->slice = s->first_glyph->slice.img;
23198 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23199 s->font = s->face->font;
23200 s->width = s->first_glyph->pixel_width;
23201
23202 /* Adjust base line for subscript/superscript text. */
23203 s->ybase += s->first_glyph->voffset;
23204 }
23205
23206
23207 /* Fill glyph string S from a sequence of stretch glyphs.
23208
23209 START is the index of the first glyph to consider,
23210 END is the index of the last + 1.
23211
23212 Value is the index of the first glyph not in S. */
23213
23214 static int
23215 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23216 {
23217 struct glyph *glyph, *last;
23218 int voffset, face_id;
23219
23220 eassert (s->first_glyph->type == STRETCH_GLYPH);
23221
23222 glyph = s->row->glyphs[s->area] + start;
23223 last = s->row->glyphs[s->area] + end;
23224 face_id = glyph->face_id;
23225 s->face = FACE_FROM_ID (s->f, face_id);
23226 s->font = s->face->font;
23227 s->width = glyph->pixel_width;
23228 s->nchars = 1;
23229 voffset = glyph->voffset;
23230
23231 for (++glyph;
23232 (glyph < last
23233 && glyph->type == STRETCH_GLYPH
23234 && glyph->voffset == voffset
23235 && glyph->face_id == face_id);
23236 ++glyph)
23237 s->width += glyph->pixel_width;
23238
23239 /* Adjust base line for subscript/superscript text. */
23240 s->ybase += voffset;
23241
23242 /* The case that face->gc == 0 is handled when drawing the glyph
23243 string by calling PREPARE_FACE_FOR_DISPLAY. */
23244 eassert (s->face);
23245 return glyph - s->row->glyphs[s->area];
23246 }
23247
23248 static struct font_metrics *
23249 get_per_char_metric (struct font *font, XChar2b *char2b)
23250 {
23251 static struct font_metrics metrics;
23252 unsigned code;
23253
23254 if (! font)
23255 return NULL;
23256 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23257 if (code == FONT_INVALID_CODE)
23258 return NULL;
23259 font->driver->text_extents (font, &code, 1, &metrics);
23260 return &metrics;
23261 }
23262
23263 /* EXPORT for RIF:
23264 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23265 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23266 assumed to be zero. */
23267
23268 void
23269 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23270 {
23271 *left = *right = 0;
23272
23273 if (glyph->type == CHAR_GLYPH)
23274 {
23275 struct face *face;
23276 XChar2b char2b;
23277 struct font_metrics *pcm;
23278
23279 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23280 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23281 {
23282 if (pcm->rbearing > pcm->width)
23283 *right = pcm->rbearing - pcm->width;
23284 if (pcm->lbearing < 0)
23285 *left = -pcm->lbearing;
23286 }
23287 }
23288 else if (glyph->type == COMPOSITE_GLYPH)
23289 {
23290 if (! glyph->u.cmp.automatic)
23291 {
23292 struct composition *cmp = composition_table[glyph->u.cmp.id];
23293
23294 if (cmp->rbearing > cmp->pixel_width)
23295 *right = cmp->rbearing - cmp->pixel_width;
23296 if (cmp->lbearing < 0)
23297 *left = - cmp->lbearing;
23298 }
23299 else
23300 {
23301 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23302 struct font_metrics metrics;
23303
23304 composition_gstring_width (gstring, glyph->slice.cmp.from,
23305 glyph->slice.cmp.to + 1, &metrics);
23306 if (metrics.rbearing > metrics.width)
23307 *right = metrics.rbearing - metrics.width;
23308 if (metrics.lbearing < 0)
23309 *left = - metrics.lbearing;
23310 }
23311 }
23312 }
23313
23314
23315 /* Return the index of the first glyph preceding glyph string S that
23316 is overwritten by S because of S's left overhang. Value is -1
23317 if no glyphs are overwritten. */
23318
23319 static int
23320 left_overwritten (struct glyph_string *s)
23321 {
23322 int k;
23323
23324 if (s->left_overhang)
23325 {
23326 int x = 0, i;
23327 struct glyph *glyphs = s->row->glyphs[s->area];
23328 int first = s->first_glyph - glyphs;
23329
23330 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23331 x -= glyphs[i].pixel_width;
23332
23333 k = i + 1;
23334 }
23335 else
23336 k = -1;
23337
23338 return k;
23339 }
23340
23341
23342 /* Return the index of the first glyph preceding glyph string S that
23343 is overwriting S because of its right overhang. Value is -1 if no
23344 glyph in front of S overwrites S. */
23345
23346 static int
23347 left_overwriting (struct glyph_string *s)
23348 {
23349 int i, k, x;
23350 struct glyph *glyphs = s->row->glyphs[s->area];
23351 int first = s->first_glyph - glyphs;
23352
23353 k = -1;
23354 x = 0;
23355 for (i = first - 1; i >= 0; --i)
23356 {
23357 int left, right;
23358 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23359 if (x + right > 0)
23360 k = i;
23361 x -= glyphs[i].pixel_width;
23362 }
23363
23364 return k;
23365 }
23366
23367
23368 /* Return the index of the last glyph following glyph string S that is
23369 overwritten by S because of S's right overhang. Value is -1 if
23370 no such glyph is found. */
23371
23372 static int
23373 right_overwritten (struct glyph_string *s)
23374 {
23375 int k = -1;
23376
23377 if (s->right_overhang)
23378 {
23379 int x = 0, i;
23380 struct glyph *glyphs = s->row->glyphs[s->area];
23381 int first = (s->first_glyph - glyphs
23382 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23383 int end = s->row->used[s->area];
23384
23385 for (i = first; i < end && s->right_overhang > x; ++i)
23386 x += glyphs[i].pixel_width;
23387
23388 k = i;
23389 }
23390
23391 return k;
23392 }
23393
23394
23395 /* Return the index of the last glyph following glyph string S that
23396 overwrites S because of its left overhang. Value is negative
23397 if no such glyph is found. */
23398
23399 static int
23400 right_overwriting (struct glyph_string *s)
23401 {
23402 int i, k, x;
23403 int end = s->row->used[s->area];
23404 struct glyph *glyphs = s->row->glyphs[s->area];
23405 int first = (s->first_glyph - glyphs
23406 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23407
23408 k = -1;
23409 x = 0;
23410 for (i = first; i < end; ++i)
23411 {
23412 int left, right;
23413 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23414 if (x - left < 0)
23415 k = i;
23416 x += glyphs[i].pixel_width;
23417 }
23418
23419 return k;
23420 }
23421
23422
23423 /* Set background width of glyph string S. START is the index of the
23424 first glyph following S. LAST_X is the right-most x-position + 1
23425 in the drawing area. */
23426
23427 static void
23428 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23429 {
23430 /* If the face of this glyph string has to be drawn to the end of
23431 the drawing area, set S->extends_to_end_of_line_p. */
23432
23433 if (start == s->row->used[s->area]
23434 && s->area == TEXT_AREA
23435 && ((s->row->fill_line_p
23436 && (s->hl == DRAW_NORMAL_TEXT
23437 || s->hl == DRAW_IMAGE_RAISED
23438 || s->hl == DRAW_IMAGE_SUNKEN))
23439 || s->hl == DRAW_MOUSE_FACE))
23440 s->extends_to_end_of_line_p = 1;
23441
23442 /* If S extends its face to the end of the line, set its
23443 background_width to the distance to the right edge of the drawing
23444 area. */
23445 if (s->extends_to_end_of_line_p)
23446 s->background_width = last_x - s->x + 1;
23447 else
23448 s->background_width = s->width;
23449 }
23450
23451
23452 /* Compute overhangs and x-positions for glyph string S and its
23453 predecessors, or successors. X is the starting x-position for S.
23454 BACKWARD_P non-zero means process predecessors. */
23455
23456 static void
23457 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23458 {
23459 if (backward_p)
23460 {
23461 while (s)
23462 {
23463 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23464 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23465 x -= s->width;
23466 s->x = x;
23467 s = s->prev;
23468 }
23469 }
23470 else
23471 {
23472 while (s)
23473 {
23474 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23475 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23476 s->x = x;
23477 x += s->width;
23478 s = s->next;
23479 }
23480 }
23481 }
23482
23483
23484
23485 /* The following macros are only called from draw_glyphs below.
23486 They reference the following parameters of that function directly:
23487 `w', `row', `area', and `overlap_p'
23488 as well as the following local variables:
23489 `s', `f', and `hdc' (in W32) */
23490
23491 #ifdef HAVE_NTGUI
23492 /* On W32, silently add local `hdc' variable to argument list of
23493 init_glyph_string. */
23494 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23495 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23496 #else
23497 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23498 init_glyph_string (s, char2b, w, row, area, start, hl)
23499 #endif
23500
23501 /* Add a glyph string for a stretch glyph to the list of strings
23502 between HEAD and TAIL. START is the index of the stretch glyph in
23503 row area AREA of glyph row ROW. END is the index of the last glyph
23504 in that glyph row area. X is the current output position assigned
23505 to the new glyph string constructed. HL overrides that face of the
23506 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23507 is the right-most x-position of the drawing area. */
23508
23509 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23510 and below -- keep them on one line. */
23511 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23512 do \
23513 { \
23514 s = alloca (sizeof *s); \
23515 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23516 START = fill_stretch_glyph_string (s, START, END); \
23517 append_glyph_string (&HEAD, &TAIL, s); \
23518 s->x = (X); \
23519 } \
23520 while (0)
23521
23522
23523 /* Add a glyph string for an image glyph to the list of strings
23524 between HEAD and TAIL. START is the index of the image glyph in
23525 row area AREA of glyph row ROW. END is the index of the last glyph
23526 in that glyph row area. X is the current output position assigned
23527 to the new glyph string constructed. HL overrides that face of the
23528 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23529 is the right-most x-position of the drawing area. */
23530
23531 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23532 do \
23533 { \
23534 s = alloca (sizeof *s); \
23535 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23536 fill_image_glyph_string (s); \
23537 append_glyph_string (&HEAD, &TAIL, s); \
23538 ++START; \
23539 s->x = (X); \
23540 } \
23541 while (0)
23542
23543
23544 /* Add a glyph string for a sequence of character glyphs to the list
23545 of strings between HEAD and TAIL. START is the index of the first
23546 glyph in row area AREA of glyph row ROW that is part of the new
23547 glyph string. END is the index of the last glyph in that glyph row
23548 area. X is the current output position assigned to the new glyph
23549 string constructed. HL overrides that face of the glyph; e.g. it
23550 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23551 right-most x-position of the drawing area. */
23552
23553 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23554 do \
23555 { \
23556 int face_id; \
23557 XChar2b *char2b; \
23558 \
23559 face_id = (row)->glyphs[area][START].face_id; \
23560 \
23561 s = alloca (sizeof *s); \
23562 char2b = alloca ((END - START) * sizeof *char2b); \
23563 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23564 append_glyph_string (&HEAD, &TAIL, s); \
23565 s->x = (X); \
23566 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23567 } \
23568 while (0)
23569
23570
23571 /* Add a glyph string for a composite sequence to the list of strings
23572 between HEAD and TAIL. START is the index of the first glyph in
23573 row area AREA of glyph row ROW that is part of the new glyph
23574 string. END is the index of the last glyph in that glyph row area.
23575 X is the current output position assigned to the new glyph string
23576 constructed. HL overrides that face of the glyph; e.g. it is
23577 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23578 x-position of the drawing area. */
23579
23580 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23581 do { \
23582 int face_id = (row)->glyphs[area][START].face_id; \
23583 struct face *base_face = FACE_FROM_ID (f, face_id); \
23584 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23585 struct composition *cmp = composition_table[cmp_id]; \
23586 XChar2b *char2b; \
23587 struct glyph_string *first_s = NULL; \
23588 int n; \
23589 \
23590 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23591 \
23592 /* Make glyph_strings for each glyph sequence that is drawable by \
23593 the same face, and append them to HEAD/TAIL. */ \
23594 for (n = 0; n < cmp->glyph_len;) \
23595 { \
23596 s = alloca (sizeof *s); \
23597 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23598 append_glyph_string (&(HEAD), &(TAIL), s); \
23599 s->cmp = cmp; \
23600 s->cmp_from = n; \
23601 s->x = (X); \
23602 if (n == 0) \
23603 first_s = s; \
23604 n = fill_composite_glyph_string (s, base_face, overlaps); \
23605 } \
23606 \
23607 ++START; \
23608 s = first_s; \
23609 } while (0)
23610
23611
23612 /* Add a glyph string for a glyph-string sequence to the list of strings
23613 between HEAD and TAIL. */
23614
23615 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23616 do { \
23617 int face_id; \
23618 XChar2b *char2b; \
23619 Lisp_Object gstring; \
23620 \
23621 face_id = (row)->glyphs[area][START].face_id; \
23622 gstring = (composition_gstring_from_id \
23623 ((row)->glyphs[area][START].u.cmp.id)); \
23624 s = alloca (sizeof *s); \
23625 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23626 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23627 append_glyph_string (&(HEAD), &(TAIL), s); \
23628 s->x = (X); \
23629 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23630 } while (0)
23631
23632
23633 /* Add a glyph string for a sequence of glyphless character's glyphs
23634 to the list of strings between HEAD and TAIL. The meanings of
23635 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23636
23637 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23638 do \
23639 { \
23640 int face_id; \
23641 \
23642 face_id = (row)->glyphs[area][START].face_id; \
23643 \
23644 s = alloca (sizeof *s); \
23645 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23646 append_glyph_string (&HEAD, &TAIL, s); \
23647 s->x = (X); \
23648 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23649 overlaps); \
23650 } \
23651 while (0)
23652
23653
23654 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23655 of AREA of glyph row ROW on window W between indices START and END.
23656 HL overrides the face for drawing glyph strings, e.g. it is
23657 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23658 x-positions of the drawing area.
23659
23660 This is an ugly monster macro construct because we must use alloca
23661 to allocate glyph strings (because draw_glyphs can be called
23662 asynchronously). */
23663
23664 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23665 do \
23666 { \
23667 HEAD = TAIL = NULL; \
23668 while (START < END) \
23669 { \
23670 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23671 switch (first_glyph->type) \
23672 { \
23673 case CHAR_GLYPH: \
23674 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23675 HL, X, LAST_X); \
23676 break; \
23677 \
23678 case COMPOSITE_GLYPH: \
23679 if (first_glyph->u.cmp.automatic) \
23680 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23681 HL, X, LAST_X); \
23682 else \
23683 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23684 HL, X, LAST_X); \
23685 break; \
23686 \
23687 case STRETCH_GLYPH: \
23688 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23689 HL, X, LAST_X); \
23690 break; \
23691 \
23692 case IMAGE_GLYPH: \
23693 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23694 HL, X, LAST_X); \
23695 break; \
23696 \
23697 case GLYPHLESS_GLYPH: \
23698 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23699 HL, X, LAST_X); \
23700 break; \
23701 \
23702 default: \
23703 emacs_abort (); \
23704 } \
23705 \
23706 if (s) \
23707 { \
23708 set_glyph_string_background_width (s, START, LAST_X); \
23709 (X) += s->width; \
23710 } \
23711 } \
23712 } while (0)
23713
23714
23715 /* Draw glyphs between START and END in AREA of ROW on window W,
23716 starting at x-position X. X is relative to AREA in W. HL is a
23717 face-override with the following meaning:
23718
23719 DRAW_NORMAL_TEXT draw normally
23720 DRAW_CURSOR draw in cursor face
23721 DRAW_MOUSE_FACE draw in mouse face.
23722 DRAW_INVERSE_VIDEO draw in mode line face
23723 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23724 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23725
23726 If OVERLAPS is non-zero, draw only the foreground of characters and
23727 clip to the physical height of ROW. Non-zero value also defines
23728 the overlapping part to be drawn:
23729
23730 OVERLAPS_PRED overlap with preceding rows
23731 OVERLAPS_SUCC overlap with succeeding rows
23732 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23733 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23734
23735 Value is the x-position reached, relative to AREA of W. */
23736
23737 static int
23738 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23739 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23740 enum draw_glyphs_face hl, int overlaps)
23741 {
23742 struct glyph_string *head, *tail;
23743 struct glyph_string *s;
23744 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23745 int i, j, x_reached, last_x, area_left = 0;
23746 struct frame *f = XFRAME (WINDOW_FRAME (w));
23747 DECLARE_HDC (hdc);
23748
23749 ALLOCATE_HDC (hdc, f);
23750
23751 /* Let's rather be paranoid than getting a SEGV. */
23752 end = min (end, row->used[area]);
23753 start = clip_to_bounds (0, start, end);
23754
23755 /* Translate X to frame coordinates. Set last_x to the right
23756 end of the drawing area. */
23757 if (row->full_width_p)
23758 {
23759 /* X is relative to the left edge of W, without scroll bars
23760 or fringes. */
23761 area_left = WINDOW_LEFT_EDGE_X (w);
23762 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23763 }
23764 else
23765 {
23766 area_left = window_box_left (w, area);
23767 last_x = area_left + window_box_width (w, area);
23768 }
23769 x += area_left;
23770
23771 /* Build a doubly-linked list of glyph_string structures between
23772 head and tail from what we have to draw. Note that the macro
23773 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23774 the reason we use a separate variable `i'. */
23775 i = start;
23776 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23777 if (tail)
23778 x_reached = tail->x + tail->background_width;
23779 else
23780 x_reached = x;
23781
23782 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23783 the row, redraw some glyphs in front or following the glyph
23784 strings built above. */
23785 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23786 {
23787 struct glyph_string *h, *t;
23788 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23789 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23790 int check_mouse_face = 0;
23791 int dummy_x = 0;
23792
23793 /* If mouse highlighting is on, we may need to draw adjacent
23794 glyphs using mouse-face highlighting. */
23795 if (area == TEXT_AREA && row->mouse_face_p
23796 && hlinfo->mouse_face_beg_row >= 0
23797 && hlinfo->mouse_face_end_row >= 0)
23798 {
23799 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23800
23801 if (row_vpos >= hlinfo->mouse_face_beg_row
23802 && row_vpos <= hlinfo->mouse_face_end_row)
23803 {
23804 check_mouse_face = 1;
23805 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23806 ? hlinfo->mouse_face_beg_col : 0;
23807 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23808 ? hlinfo->mouse_face_end_col
23809 : row->used[TEXT_AREA];
23810 }
23811 }
23812
23813 /* Compute overhangs for all glyph strings. */
23814 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23815 for (s = head; s; s = s->next)
23816 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23817
23818 /* Prepend glyph strings for glyphs in front of the first glyph
23819 string that are overwritten because of the first glyph
23820 string's left overhang. The background of all strings
23821 prepended must be drawn because the first glyph string
23822 draws over it. */
23823 i = left_overwritten (head);
23824 if (i >= 0)
23825 {
23826 enum draw_glyphs_face overlap_hl;
23827
23828 /* If this row contains mouse highlighting, attempt to draw
23829 the overlapped glyphs with the correct highlight. This
23830 code fails if the overlap encompasses more than one glyph
23831 and mouse-highlight spans only some of these glyphs.
23832 However, making it work perfectly involves a lot more
23833 code, and I don't know if the pathological case occurs in
23834 practice, so we'll stick to this for now. --- cyd */
23835 if (check_mouse_face
23836 && mouse_beg_col < start && mouse_end_col > i)
23837 overlap_hl = DRAW_MOUSE_FACE;
23838 else
23839 overlap_hl = DRAW_NORMAL_TEXT;
23840
23841 j = i;
23842 BUILD_GLYPH_STRINGS (j, start, h, t,
23843 overlap_hl, dummy_x, last_x);
23844 start = i;
23845 compute_overhangs_and_x (t, head->x, 1);
23846 prepend_glyph_string_lists (&head, &tail, h, t);
23847 clip_head = head;
23848 }
23849
23850 /* Prepend glyph strings for glyphs in front of the first glyph
23851 string that overwrite that glyph string because of their
23852 right overhang. For these strings, only the foreground must
23853 be drawn, because it draws over the glyph string at `head'.
23854 The background must not be drawn because this would overwrite
23855 right overhangs of preceding glyphs for which no glyph
23856 strings exist. */
23857 i = left_overwriting (head);
23858 if (i >= 0)
23859 {
23860 enum draw_glyphs_face overlap_hl;
23861
23862 if (check_mouse_face
23863 && mouse_beg_col < start && mouse_end_col > i)
23864 overlap_hl = DRAW_MOUSE_FACE;
23865 else
23866 overlap_hl = DRAW_NORMAL_TEXT;
23867
23868 clip_head = head;
23869 BUILD_GLYPH_STRINGS (i, start, h, t,
23870 overlap_hl, dummy_x, last_x);
23871 for (s = h; s; s = s->next)
23872 s->background_filled_p = 1;
23873 compute_overhangs_and_x (t, head->x, 1);
23874 prepend_glyph_string_lists (&head, &tail, h, t);
23875 }
23876
23877 /* Append glyphs strings for glyphs following the last glyph
23878 string tail that are overwritten by tail. The background of
23879 these strings has to be drawn because tail's foreground draws
23880 over it. */
23881 i = right_overwritten (tail);
23882 if (i >= 0)
23883 {
23884 enum draw_glyphs_face overlap_hl;
23885
23886 if (check_mouse_face
23887 && mouse_beg_col < i && mouse_end_col > end)
23888 overlap_hl = DRAW_MOUSE_FACE;
23889 else
23890 overlap_hl = DRAW_NORMAL_TEXT;
23891
23892 BUILD_GLYPH_STRINGS (end, i, h, t,
23893 overlap_hl, x, last_x);
23894 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23895 we don't have `end = i;' here. */
23896 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23897 append_glyph_string_lists (&head, &tail, h, t);
23898 clip_tail = tail;
23899 }
23900
23901 /* Append glyph strings for glyphs following the last glyph
23902 string tail that overwrite tail. The foreground of such
23903 glyphs has to be drawn because it writes into the background
23904 of tail. The background must not be drawn because it could
23905 paint over the foreground of following glyphs. */
23906 i = right_overwriting (tail);
23907 if (i >= 0)
23908 {
23909 enum draw_glyphs_face overlap_hl;
23910 if (check_mouse_face
23911 && mouse_beg_col < i && mouse_end_col > end)
23912 overlap_hl = DRAW_MOUSE_FACE;
23913 else
23914 overlap_hl = DRAW_NORMAL_TEXT;
23915
23916 clip_tail = tail;
23917 i++; /* We must include the Ith glyph. */
23918 BUILD_GLYPH_STRINGS (end, i, h, t,
23919 overlap_hl, x, last_x);
23920 for (s = h; s; s = s->next)
23921 s->background_filled_p = 1;
23922 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23923 append_glyph_string_lists (&head, &tail, h, t);
23924 }
23925 if (clip_head || clip_tail)
23926 for (s = head; s; s = s->next)
23927 {
23928 s->clip_head = clip_head;
23929 s->clip_tail = clip_tail;
23930 }
23931 }
23932
23933 /* Draw all strings. */
23934 for (s = head; s; s = s->next)
23935 FRAME_RIF (f)->draw_glyph_string (s);
23936
23937 #ifndef HAVE_NS
23938 /* When focus a sole frame and move horizontally, this sets on_p to 0
23939 causing a failure to erase prev cursor position. */
23940 if (area == TEXT_AREA
23941 && !row->full_width_p
23942 /* When drawing overlapping rows, only the glyph strings'
23943 foreground is drawn, which doesn't erase a cursor
23944 completely. */
23945 && !overlaps)
23946 {
23947 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23948 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23949 : (tail ? tail->x + tail->background_width : x));
23950 x0 -= area_left;
23951 x1 -= area_left;
23952
23953 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23954 row->y, MATRIX_ROW_BOTTOM_Y (row));
23955 }
23956 #endif
23957
23958 /* Value is the x-position up to which drawn, relative to AREA of W.
23959 This doesn't include parts drawn because of overhangs. */
23960 if (row->full_width_p)
23961 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23962 else
23963 x_reached -= area_left;
23964
23965 RELEASE_HDC (hdc, f);
23966
23967 return x_reached;
23968 }
23969
23970 /* Expand row matrix if too narrow. Don't expand if area
23971 is not present. */
23972
23973 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23974 { \
23975 if (!it->f->fonts_changed \
23976 && (it->glyph_row->glyphs[area] \
23977 < it->glyph_row->glyphs[area + 1])) \
23978 { \
23979 it->w->ncols_scale_factor++; \
23980 it->f->fonts_changed = 1; \
23981 } \
23982 }
23983
23984 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23985 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23986
23987 static void
23988 append_glyph (struct it *it)
23989 {
23990 struct glyph *glyph;
23991 enum glyph_row_area area = it->area;
23992
23993 eassert (it->glyph_row);
23994 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23995
23996 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23997 if (glyph < it->glyph_row->glyphs[area + 1])
23998 {
23999 /* If the glyph row is reversed, we need to prepend the glyph
24000 rather than append it. */
24001 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24002 {
24003 struct glyph *g;
24004
24005 /* Make room for the additional glyph. */
24006 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24007 g[1] = *g;
24008 glyph = it->glyph_row->glyphs[area];
24009 }
24010 glyph->charpos = CHARPOS (it->position);
24011 glyph->object = it->object;
24012 if (it->pixel_width > 0)
24013 {
24014 glyph->pixel_width = it->pixel_width;
24015 glyph->padding_p = 0;
24016 }
24017 else
24018 {
24019 /* Assure at least 1-pixel width. Otherwise, cursor can't
24020 be displayed correctly. */
24021 glyph->pixel_width = 1;
24022 glyph->padding_p = 1;
24023 }
24024 glyph->ascent = it->ascent;
24025 glyph->descent = it->descent;
24026 glyph->voffset = it->voffset;
24027 glyph->type = CHAR_GLYPH;
24028 glyph->avoid_cursor_p = it->avoid_cursor_p;
24029 glyph->multibyte_p = it->multibyte_p;
24030 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24031 {
24032 /* In R2L rows, the left and the right box edges need to be
24033 drawn in reverse direction. */
24034 glyph->right_box_line_p = it->start_of_box_run_p;
24035 glyph->left_box_line_p = it->end_of_box_run_p;
24036 }
24037 else
24038 {
24039 glyph->left_box_line_p = it->start_of_box_run_p;
24040 glyph->right_box_line_p = it->end_of_box_run_p;
24041 }
24042 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24043 || it->phys_descent > it->descent);
24044 glyph->glyph_not_available_p = it->glyph_not_available_p;
24045 glyph->face_id = it->face_id;
24046 glyph->u.ch = it->char_to_display;
24047 glyph->slice.img = null_glyph_slice;
24048 glyph->font_type = FONT_TYPE_UNKNOWN;
24049 if (it->bidi_p)
24050 {
24051 glyph->resolved_level = it->bidi_it.resolved_level;
24052 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24053 emacs_abort ();
24054 glyph->bidi_type = it->bidi_it.type;
24055 }
24056 else
24057 {
24058 glyph->resolved_level = 0;
24059 glyph->bidi_type = UNKNOWN_BT;
24060 }
24061 ++it->glyph_row->used[area];
24062 }
24063 else
24064 IT_EXPAND_MATRIX_WIDTH (it, area);
24065 }
24066
24067 /* Store one glyph for the composition IT->cmp_it.id in
24068 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24069 non-null. */
24070
24071 static void
24072 append_composite_glyph (struct it *it)
24073 {
24074 struct glyph *glyph;
24075 enum glyph_row_area area = it->area;
24076
24077 eassert (it->glyph_row);
24078
24079 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24080 if (glyph < it->glyph_row->glyphs[area + 1])
24081 {
24082 /* If the glyph row is reversed, we need to prepend the glyph
24083 rather than append it. */
24084 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24085 {
24086 struct glyph *g;
24087
24088 /* Make room for the new glyph. */
24089 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24090 g[1] = *g;
24091 glyph = it->glyph_row->glyphs[it->area];
24092 }
24093 glyph->charpos = it->cmp_it.charpos;
24094 glyph->object = it->object;
24095 glyph->pixel_width = it->pixel_width;
24096 glyph->ascent = it->ascent;
24097 glyph->descent = it->descent;
24098 glyph->voffset = it->voffset;
24099 glyph->type = COMPOSITE_GLYPH;
24100 if (it->cmp_it.ch < 0)
24101 {
24102 glyph->u.cmp.automatic = 0;
24103 glyph->u.cmp.id = it->cmp_it.id;
24104 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24105 }
24106 else
24107 {
24108 glyph->u.cmp.automatic = 1;
24109 glyph->u.cmp.id = it->cmp_it.id;
24110 glyph->slice.cmp.from = it->cmp_it.from;
24111 glyph->slice.cmp.to = it->cmp_it.to - 1;
24112 }
24113 glyph->avoid_cursor_p = it->avoid_cursor_p;
24114 glyph->multibyte_p = it->multibyte_p;
24115 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24116 {
24117 /* In R2L rows, the left and the right box edges need to be
24118 drawn in reverse direction. */
24119 glyph->right_box_line_p = it->start_of_box_run_p;
24120 glyph->left_box_line_p = it->end_of_box_run_p;
24121 }
24122 else
24123 {
24124 glyph->left_box_line_p = it->start_of_box_run_p;
24125 glyph->right_box_line_p = it->end_of_box_run_p;
24126 }
24127 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24128 || it->phys_descent > it->descent);
24129 glyph->padding_p = 0;
24130 glyph->glyph_not_available_p = 0;
24131 glyph->face_id = it->face_id;
24132 glyph->font_type = FONT_TYPE_UNKNOWN;
24133 if (it->bidi_p)
24134 {
24135 glyph->resolved_level = it->bidi_it.resolved_level;
24136 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24137 emacs_abort ();
24138 glyph->bidi_type = it->bidi_it.type;
24139 }
24140 ++it->glyph_row->used[area];
24141 }
24142 else
24143 IT_EXPAND_MATRIX_WIDTH (it, area);
24144 }
24145
24146
24147 /* Change IT->ascent and IT->height according to the setting of
24148 IT->voffset. */
24149
24150 static void
24151 take_vertical_position_into_account (struct it *it)
24152 {
24153 if (it->voffset)
24154 {
24155 if (it->voffset < 0)
24156 /* Increase the ascent so that we can display the text higher
24157 in the line. */
24158 it->ascent -= it->voffset;
24159 else
24160 /* Increase the descent so that we can display the text lower
24161 in the line. */
24162 it->descent += it->voffset;
24163 }
24164 }
24165
24166
24167 /* Produce glyphs/get display metrics for the image IT is loaded with.
24168 See the description of struct display_iterator in dispextern.h for
24169 an overview of struct display_iterator. */
24170
24171 static void
24172 produce_image_glyph (struct it *it)
24173 {
24174 struct image *img;
24175 struct face *face;
24176 int glyph_ascent, crop;
24177 struct glyph_slice slice;
24178
24179 eassert (it->what == IT_IMAGE);
24180
24181 face = FACE_FROM_ID (it->f, it->face_id);
24182 eassert (face);
24183 /* Make sure X resources of the face is loaded. */
24184 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24185
24186 if (it->image_id < 0)
24187 {
24188 /* Fringe bitmap. */
24189 it->ascent = it->phys_ascent = 0;
24190 it->descent = it->phys_descent = 0;
24191 it->pixel_width = 0;
24192 it->nglyphs = 0;
24193 return;
24194 }
24195
24196 img = IMAGE_FROM_ID (it->f, it->image_id);
24197 eassert (img);
24198 /* Make sure X resources of the image is loaded. */
24199 prepare_image_for_display (it->f, img);
24200
24201 slice.x = slice.y = 0;
24202 slice.width = img->width;
24203 slice.height = img->height;
24204
24205 if (INTEGERP (it->slice.x))
24206 slice.x = XINT (it->slice.x);
24207 else if (FLOATP (it->slice.x))
24208 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24209
24210 if (INTEGERP (it->slice.y))
24211 slice.y = XINT (it->slice.y);
24212 else if (FLOATP (it->slice.y))
24213 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24214
24215 if (INTEGERP (it->slice.width))
24216 slice.width = XINT (it->slice.width);
24217 else if (FLOATP (it->slice.width))
24218 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24219
24220 if (INTEGERP (it->slice.height))
24221 slice.height = XINT (it->slice.height);
24222 else if (FLOATP (it->slice.height))
24223 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24224
24225 if (slice.x >= img->width)
24226 slice.x = img->width;
24227 if (slice.y >= img->height)
24228 slice.y = img->height;
24229 if (slice.x + slice.width >= img->width)
24230 slice.width = img->width - slice.x;
24231 if (slice.y + slice.height > img->height)
24232 slice.height = img->height - slice.y;
24233
24234 if (slice.width == 0 || slice.height == 0)
24235 return;
24236
24237 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24238
24239 it->descent = slice.height - glyph_ascent;
24240 if (slice.y == 0)
24241 it->descent += img->vmargin;
24242 if (slice.y + slice.height == img->height)
24243 it->descent += img->vmargin;
24244 it->phys_descent = it->descent;
24245
24246 it->pixel_width = slice.width;
24247 if (slice.x == 0)
24248 it->pixel_width += img->hmargin;
24249 if (slice.x + slice.width == img->width)
24250 it->pixel_width += img->hmargin;
24251
24252 /* It's quite possible for images to have an ascent greater than
24253 their height, so don't get confused in that case. */
24254 if (it->descent < 0)
24255 it->descent = 0;
24256
24257 it->nglyphs = 1;
24258
24259 if (face->box != FACE_NO_BOX)
24260 {
24261 if (face->box_line_width > 0)
24262 {
24263 if (slice.y == 0)
24264 it->ascent += face->box_line_width;
24265 if (slice.y + slice.height == img->height)
24266 it->descent += face->box_line_width;
24267 }
24268
24269 if (it->start_of_box_run_p && slice.x == 0)
24270 it->pixel_width += eabs (face->box_line_width);
24271 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24272 it->pixel_width += eabs (face->box_line_width);
24273 }
24274
24275 take_vertical_position_into_account (it);
24276
24277 /* Automatically crop wide image glyphs at right edge so we can
24278 draw the cursor on same display row. */
24279 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24280 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24281 {
24282 it->pixel_width -= crop;
24283 slice.width -= crop;
24284 }
24285
24286 if (it->glyph_row)
24287 {
24288 struct glyph *glyph;
24289 enum glyph_row_area area = it->area;
24290
24291 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24292 if (glyph < it->glyph_row->glyphs[area + 1])
24293 {
24294 glyph->charpos = CHARPOS (it->position);
24295 glyph->object = it->object;
24296 glyph->pixel_width = it->pixel_width;
24297 glyph->ascent = glyph_ascent;
24298 glyph->descent = it->descent;
24299 glyph->voffset = it->voffset;
24300 glyph->type = IMAGE_GLYPH;
24301 glyph->avoid_cursor_p = it->avoid_cursor_p;
24302 glyph->multibyte_p = it->multibyte_p;
24303 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24304 {
24305 /* In R2L rows, the left and the right box edges need to be
24306 drawn in reverse direction. */
24307 glyph->right_box_line_p = it->start_of_box_run_p;
24308 glyph->left_box_line_p = it->end_of_box_run_p;
24309 }
24310 else
24311 {
24312 glyph->left_box_line_p = it->start_of_box_run_p;
24313 glyph->right_box_line_p = it->end_of_box_run_p;
24314 }
24315 glyph->overlaps_vertically_p = 0;
24316 glyph->padding_p = 0;
24317 glyph->glyph_not_available_p = 0;
24318 glyph->face_id = it->face_id;
24319 glyph->u.img_id = img->id;
24320 glyph->slice.img = slice;
24321 glyph->font_type = FONT_TYPE_UNKNOWN;
24322 if (it->bidi_p)
24323 {
24324 glyph->resolved_level = it->bidi_it.resolved_level;
24325 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24326 emacs_abort ();
24327 glyph->bidi_type = it->bidi_it.type;
24328 }
24329 ++it->glyph_row->used[area];
24330 }
24331 else
24332 IT_EXPAND_MATRIX_WIDTH (it, area);
24333 }
24334 }
24335
24336
24337 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24338 of the glyph, WIDTH and HEIGHT are the width and height of the
24339 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24340
24341 static void
24342 append_stretch_glyph (struct it *it, Lisp_Object object,
24343 int width, int height, int ascent)
24344 {
24345 struct glyph *glyph;
24346 enum glyph_row_area area = it->area;
24347
24348 eassert (ascent >= 0 && ascent <= height);
24349
24350 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24351 if (glyph < it->glyph_row->glyphs[area + 1])
24352 {
24353 /* If the glyph row is reversed, we need to prepend the glyph
24354 rather than append it. */
24355 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24356 {
24357 struct glyph *g;
24358
24359 /* Make room for the additional glyph. */
24360 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24361 g[1] = *g;
24362 glyph = it->glyph_row->glyphs[area];
24363 }
24364 glyph->charpos = CHARPOS (it->position);
24365 glyph->object = object;
24366 glyph->pixel_width = width;
24367 glyph->ascent = ascent;
24368 glyph->descent = height - ascent;
24369 glyph->voffset = it->voffset;
24370 glyph->type = STRETCH_GLYPH;
24371 glyph->avoid_cursor_p = it->avoid_cursor_p;
24372 glyph->multibyte_p = it->multibyte_p;
24373 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24374 {
24375 /* In R2L rows, the left and the right box edges need to be
24376 drawn in reverse direction. */
24377 glyph->right_box_line_p = it->start_of_box_run_p;
24378 glyph->left_box_line_p = it->end_of_box_run_p;
24379 }
24380 else
24381 {
24382 glyph->left_box_line_p = it->start_of_box_run_p;
24383 glyph->right_box_line_p = it->end_of_box_run_p;
24384 }
24385 glyph->overlaps_vertically_p = 0;
24386 glyph->padding_p = 0;
24387 glyph->glyph_not_available_p = 0;
24388 glyph->face_id = it->face_id;
24389 glyph->u.stretch.ascent = ascent;
24390 glyph->u.stretch.height = height;
24391 glyph->slice.img = null_glyph_slice;
24392 glyph->font_type = FONT_TYPE_UNKNOWN;
24393 if (it->bidi_p)
24394 {
24395 glyph->resolved_level = it->bidi_it.resolved_level;
24396 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24397 emacs_abort ();
24398 glyph->bidi_type = it->bidi_it.type;
24399 }
24400 else
24401 {
24402 glyph->resolved_level = 0;
24403 glyph->bidi_type = UNKNOWN_BT;
24404 }
24405 ++it->glyph_row->used[area];
24406 }
24407 else
24408 IT_EXPAND_MATRIX_WIDTH (it, area);
24409 }
24410
24411 #endif /* HAVE_WINDOW_SYSTEM */
24412
24413 /* Produce a stretch glyph for iterator IT. IT->object is the value
24414 of the glyph property displayed. The value must be a list
24415 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24416 being recognized:
24417
24418 1. `:width WIDTH' specifies that the space should be WIDTH *
24419 canonical char width wide. WIDTH may be an integer or floating
24420 point number.
24421
24422 2. `:relative-width FACTOR' specifies that the width of the stretch
24423 should be computed from the width of the first character having the
24424 `glyph' property, and should be FACTOR times that width.
24425
24426 3. `:align-to HPOS' specifies that the space should be wide enough
24427 to reach HPOS, a value in canonical character units.
24428
24429 Exactly one of the above pairs must be present.
24430
24431 4. `:height HEIGHT' specifies that the height of the stretch produced
24432 should be HEIGHT, measured in canonical character units.
24433
24434 5. `:relative-height FACTOR' specifies that the height of the
24435 stretch should be FACTOR times the height of the characters having
24436 the glyph property.
24437
24438 Either none or exactly one of 4 or 5 must be present.
24439
24440 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24441 of the stretch should be used for the ascent of the stretch.
24442 ASCENT must be in the range 0 <= ASCENT <= 100. */
24443
24444 void
24445 produce_stretch_glyph (struct it *it)
24446 {
24447 /* (space :width WIDTH :height HEIGHT ...) */
24448 Lisp_Object prop, plist;
24449 int width = 0, height = 0, align_to = -1;
24450 int zero_width_ok_p = 0;
24451 double tem;
24452 struct font *font = NULL;
24453
24454 #ifdef HAVE_WINDOW_SYSTEM
24455 int ascent = 0;
24456 int zero_height_ok_p = 0;
24457
24458 if (FRAME_WINDOW_P (it->f))
24459 {
24460 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24461 font = face->font ? face->font : FRAME_FONT (it->f);
24462 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24463 }
24464 #endif
24465
24466 /* List should start with `space'. */
24467 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24468 plist = XCDR (it->object);
24469
24470 /* Compute the width of the stretch. */
24471 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24472 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24473 {
24474 /* Absolute width `:width WIDTH' specified and valid. */
24475 zero_width_ok_p = 1;
24476 width = (int)tem;
24477 }
24478 #ifdef HAVE_WINDOW_SYSTEM
24479 else if (FRAME_WINDOW_P (it->f)
24480 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24481 {
24482 /* Relative width `:relative-width FACTOR' specified and valid.
24483 Compute the width of the characters having the `glyph'
24484 property. */
24485 struct it it2;
24486 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24487
24488 it2 = *it;
24489 if (it->multibyte_p)
24490 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24491 else
24492 {
24493 it2.c = it2.char_to_display = *p, it2.len = 1;
24494 if (! ASCII_CHAR_P (it2.c))
24495 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24496 }
24497
24498 it2.glyph_row = NULL;
24499 it2.what = IT_CHARACTER;
24500 x_produce_glyphs (&it2);
24501 width = NUMVAL (prop) * it2.pixel_width;
24502 }
24503 #endif /* HAVE_WINDOW_SYSTEM */
24504 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24505 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24506 {
24507 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24508 align_to = (align_to < 0
24509 ? 0
24510 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24511 else if (align_to < 0)
24512 align_to = window_box_left_offset (it->w, TEXT_AREA);
24513 width = max (0, (int)tem + align_to - it->current_x);
24514 zero_width_ok_p = 1;
24515 }
24516 else
24517 /* Nothing specified -> width defaults to canonical char width. */
24518 width = FRAME_COLUMN_WIDTH (it->f);
24519
24520 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24521 width = 1;
24522
24523 #ifdef HAVE_WINDOW_SYSTEM
24524 /* Compute height. */
24525 if (FRAME_WINDOW_P (it->f))
24526 {
24527 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24528 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24529 {
24530 height = (int)tem;
24531 zero_height_ok_p = 1;
24532 }
24533 else if (prop = Fplist_get (plist, QCrelative_height),
24534 NUMVAL (prop) > 0)
24535 height = FONT_HEIGHT (font) * NUMVAL (prop);
24536 else
24537 height = FONT_HEIGHT (font);
24538
24539 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24540 height = 1;
24541
24542 /* Compute percentage of height used for ascent. If
24543 `:ascent ASCENT' is present and valid, use that. Otherwise,
24544 derive the ascent from the font in use. */
24545 if (prop = Fplist_get (plist, QCascent),
24546 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24547 ascent = height * NUMVAL (prop) / 100.0;
24548 else if (!NILP (prop)
24549 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24550 ascent = min (max (0, (int)tem), height);
24551 else
24552 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24553 }
24554 else
24555 #endif /* HAVE_WINDOW_SYSTEM */
24556 height = 1;
24557
24558 if (width > 0 && it->line_wrap != TRUNCATE
24559 && it->current_x + width > it->last_visible_x)
24560 {
24561 width = it->last_visible_x - it->current_x;
24562 #ifdef HAVE_WINDOW_SYSTEM
24563 /* Subtract one more pixel from the stretch width, but only on
24564 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24565 width -= FRAME_WINDOW_P (it->f);
24566 #endif
24567 }
24568
24569 if (width > 0 && height > 0 && it->glyph_row)
24570 {
24571 Lisp_Object o_object = it->object;
24572 Lisp_Object object = it->stack[it->sp - 1].string;
24573 int n = width;
24574
24575 if (!STRINGP (object))
24576 object = it->w->contents;
24577 #ifdef HAVE_WINDOW_SYSTEM
24578 if (FRAME_WINDOW_P (it->f))
24579 append_stretch_glyph (it, object, width, height, ascent);
24580 else
24581 #endif
24582 {
24583 it->object = object;
24584 it->char_to_display = ' ';
24585 it->pixel_width = it->len = 1;
24586 while (n--)
24587 tty_append_glyph (it);
24588 it->object = o_object;
24589 }
24590 }
24591
24592 it->pixel_width = width;
24593 #ifdef HAVE_WINDOW_SYSTEM
24594 if (FRAME_WINDOW_P (it->f))
24595 {
24596 it->ascent = it->phys_ascent = ascent;
24597 it->descent = it->phys_descent = height - it->ascent;
24598 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24599 take_vertical_position_into_account (it);
24600 }
24601 else
24602 #endif
24603 it->nglyphs = width;
24604 }
24605
24606 /* Get information about special display element WHAT in an
24607 environment described by IT. WHAT is one of IT_TRUNCATION or
24608 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24609 non-null glyph_row member. This function ensures that fields like
24610 face_id, c, len of IT are left untouched. */
24611
24612 static void
24613 produce_special_glyphs (struct it *it, enum display_element_type what)
24614 {
24615 struct it temp_it;
24616 Lisp_Object gc;
24617 GLYPH glyph;
24618
24619 temp_it = *it;
24620 temp_it.object = make_number (0);
24621 memset (&temp_it.current, 0, sizeof temp_it.current);
24622
24623 if (what == IT_CONTINUATION)
24624 {
24625 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24626 if (it->bidi_it.paragraph_dir == R2L)
24627 SET_GLYPH_FROM_CHAR (glyph, '/');
24628 else
24629 SET_GLYPH_FROM_CHAR (glyph, '\\');
24630 if (it->dp
24631 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24632 {
24633 /* FIXME: Should we mirror GC for R2L lines? */
24634 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24635 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24636 }
24637 }
24638 else if (what == IT_TRUNCATION)
24639 {
24640 /* Truncation glyph. */
24641 SET_GLYPH_FROM_CHAR (glyph, '$');
24642 if (it->dp
24643 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24644 {
24645 /* FIXME: Should we mirror GC for R2L lines? */
24646 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24647 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24648 }
24649 }
24650 else
24651 emacs_abort ();
24652
24653 #ifdef HAVE_WINDOW_SYSTEM
24654 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24655 is turned off, we precede the truncation/continuation glyphs by a
24656 stretch glyph whose width is computed such that these special
24657 glyphs are aligned at the window margin, even when very different
24658 fonts are used in different glyph rows. */
24659 if (FRAME_WINDOW_P (temp_it.f)
24660 /* init_iterator calls this with it->glyph_row == NULL, and it
24661 wants only the pixel width of the truncation/continuation
24662 glyphs. */
24663 && temp_it.glyph_row
24664 /* insert_left_trunc_glyphs calls us at the beginning of the
24665 row, and it has its own calculation of the stretch glyph
24666 width. */
24667 && temp_it.glyph_row->used[TEXT_AREA] > 0
24668 && (temp_it.glyph_row->reversed_p
24669 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24670 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24671 {
24672 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24673
24674 if (stretch_width > 0)
24675 {
24676 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24677 struct font *font =
24678 face->font ? face->font : FRAME_FONT (temp_it.f);
24679 int stretch_ascent =
24680 (((temp_it.ascent + temp_it.descent)
24681 * FONT_BASE (font)) / FONT_HEIGHT (font));
24682
24683 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24684 temp_it.ascent + temp_it.descent,
24685 stretch_ascent);
24686 }
24687 }
24688 #endif
24689
24690 temp_it.dp = NULL;
24691 temp_it.what = IT_CHARACTER;
24692 temp_it.len = 1;
24693 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24694 temp_it.face_id = GLYPH_FACE (glyph);
24695 temp_it.len = CHAR_BYTES (temp_it.c);
24696
24697 PRODUCE_GLYPHS (&temp_it);
24698 it->pixel_width = temp_it.pixel_width;
24699 it->nglyphs = temp_it.pixel_width;
24700 }
24701
24702 #ifdef HAVE_WINDOW_SYSTEM
24703
24704 /* Calculate line-height and line-spacing properties.
24705 An integer value specifies explicit pixel value.
24706 A float value specifies relative value to current face height.
24707 A cons (float . face-name) specifies relative value to
24708 height of specified face font.
24709
24710 Returns height in pixels, or nil. */
24711
24712
24713 static Lisp_Object
24714 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24715 int boff, int override)
24716 {
24717 Lisp_Object face_name = Qnil;
24718 int ascent, descent, height;
24719
24720 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24721 return val;
24722
24723 if (CONSP (val))
24724 {
24725 face_name = XCAR (val);
24726 val = XCDR (val);
24727 if (!NUMBERP (val))
24728 val = make_number (1);
24729 if (NILP (face_name))
24730 {
24731 height = it->ascent + it->descent;
24732 goto scale;
24733 }
24734 }
24735
24736 if (NILP (face_name))
24737 {
24738 font = FRAME_FONT (it->f);
24739 boff = FRAME_BASELINE_OFFSET (it->f);
24740 }
24741 else if (EQ (face_name, Qt))
24742 {
24743 override = 0;
24744 }
24745 else
24746 {
24747 int face_id;
24748 struct face *face;
24749
24750 face_id = lookup_named_face (it->f, face_name, 0);
24751 if (face_id < 0)
24752 return make_number (-1);
24753
24754 face = FACE_FROM_ID (it->f, face_id);
24755 font = face->font;
24756 if (font == NULL)
24757 return make_number (-1);
24758 boff = font->baseline_offset;
24759 if (font->vertical_centering)
24760 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24761 }
24762
24763 ascent = FONT_BASE (font) + boff;
24764 descent = FONT_DESCENT (font) - boff;
24765
24766 if (override)
24767 {
24768 it->override_ascent = ascent;
24769 it->override_descent = descent;
24770 it->override_boff = boff;
24771 }
24772
24773 height = ascent + descent;
24774
24775 scale:
24776 if (FLOATP (val))
24777 height = (int)(XFLOAT_DATA (val) * height);
24778 else if (INTEGERP (val))
24779 height *= XINT (val);
24780
24781 return make_number (height);
24782 }
24783
24784
24785 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24786 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24787 and only if this is for a character for which no font was found.
24788
24789 If the display method (it->glyphless_method) is
24790 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24791 length of the acronym or the hexadecimal string, UPPER_XOFF and
24792 UPPER_YOFF are pixel offsets for the upper part of the string,
24793 LOWER_XOFF and LOWER_YOFF are for the lower part.
24794
24795 For the other display methods, LEN through LOWER_YOFF are zero. */
24796
24797 static void
24798 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24799 short upper_xoff, short upper_yoff,
24800 short lower_xoff, short lower_yoff)
24801 {
24802 struct glyph *glyph;
24803 enum glyph_row_area area = it->area;
24804
24805 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24806 if (glyph < it->glyph_row->glyphs[area + 1])
24807 {
24808 /* If the glyph row is reversed, we need to prepend the glyph
24809 rather than append it. */
24810 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24811 {
24812 struct glyph *g;
24813
24814 /* Make room for the additional glyph. */
24815 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24816 g[1] = *g;
24817 glyph = it->glyph_row->glyphs[area];
24818 }
24819 glyph->charpos = CHARPOS (it->position);
24820 glyph->object = it->object;
24821 glyph->pixel_width = it->pixel_width;
24822 glyph->ascent = it->ascent;
24823 glyph->descent = it->descent;
24824 glyph->voffset = it->voffset;
24825 glyph->type = GLYPHLESS_GLYPH;
24826 glyph->u.glyphless.method = it->glyphless_method;
24827 glyph->u.glyphless.for_no_font = for_no_font;
24828 glyph->u.glyphless.len = len;
24829 glyph->u.glyphless.ch = it->c;
24830 glyph->slice.glyphless.upper_xoff = upper_xoff;
24831 glyph->slice.glyphless.upper_yoff = upper_yoff;
24832 glyph->slice.glyphless.lower_xoff = lower_xoff;
24833 glyph->slice.glyphless.lower_yoff = lower_yoff;
24834 glyph->avoid_cursor_p = it->avoid_cursor_p;
24835 glyph->multibyte_p = it->multibyte_p;
24836 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24837 {
24838 /* In R2L rows, the left and the right box edges need to be
24839 drawn in reverse direction. */
24840 glyph->right_box_line_p = it->start_of_box_run_p;
24841 glyph->left_box_line_p = it->end_of_box_run_p;
24842 }
24843 else
24844 {
24845 glyph->left_box_line_p = it->start_of_box_run_p;
24846 glyph->right_box_line_p = it->end_of_box_run_p;
24847 }
24848 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24849 || it->phys_descent > it->descent);
24850 glyph->padding_p = 0;
24851 glyph->glyph_not_available_p = 0;
24852 glyph->face_id = face_id;
24853 glyph->font_type = FONT_TYPE_UNKNOWN;
24854 if (it->bidi_p)
24855 {
24856 glyph->resolved_level = it->bidi_it.resolved_level;
24857 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24858 emacs_abort ();
24859 glyph->bidi_type = it->bidi_it.type;
24860 }
24861 ++it->glyph_row->used[area];
24862 }
24863 else
24864 IT_EXPAND_MATRIX_WIDTH (it, area);
24865 }
24866
24867
24868 /* Produce a glyph for a glyphless character for iterator IT.
24869 IT->glyphless_method specifies which method to use for displaying
24870 the character. See the description of enum
24871 glyphless_display_method in dispextern.h for the detail.
24872
24873 FOR_NO_FONT is nonzero if and only if this is for a character for
24874 which no font was found. ACRONYM, if non-nil, is an acronym string
24875 for the character. */
24876
24877 static void
24878 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24879 {
24880 int face_id;
24881 struct face *face;
24882 struct font *font;
24883 int base_width, base_height, width, height;
24884 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24885 int len;
24886
24887 /* Get the metrics of the base font. We always refer to the current
24888 ASCII face. */
24889 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24890 font = face->font ? face->font : FRAME_FONT (it->f);
24891 it->ascent = FONT_BASE (font) + font->baseline_offset;
24892 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24893 base_height = it->ascent + it->descent;
24894 base_width = font->average_width;
24895
24896 /* Get a face ID for the glyph by utilizing a cache (the same way as
24897 done for `escape-glyph' in get_next_display_element). */
24898 if (it->f == last_glyphless_glyph_frame
24899 && it->face_id == last_glyphless_glyph_face_id)
24900 {
24901 face_id = last_glyphless_glyph_merged_face_id;
24902 }
24903 else
24904 {
24905 /* Merge the `glyphless-char' face into the current face. */
24906 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24907 last_glyphless_glyph_frame = it->f;
24908 last_glyphless_glyph_face_id = it->face_id;
24909 last_glyphless_glyph_merged_face_id = face_id;
24910 }
24911
24912 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24913 {
24914 it->pixel_width = THIN_SPACE_WIDTH;
24915 len = 0;
24916 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24917 }
24918 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24919 {
24920 width = CHAR_WIDTH (it->c);
24921 if (width == 0)
24922 width = 1;
24923 else if (width > 4)
24924 width = 4;
24925 it->pixel_width = base_width * width;
24926 len = 0;
24927 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24928 }
24929 else
24930 {
24931 char buf[7];
24932 const char *str;
24933 unsigned int code[6];
24934 int upper_len;
24935 int ascent, descent;
24936 struct font_metrics metrics_upper, metrics_lower;
24937
24938 face = FACE_FROM_ID (it->f, face_id);
24939 font = face->font ? face->font : FRAME_FONT (it->f);
24940 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24941
24942 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24943 {
24944 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24945 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24946 if (CONSP (acronym))
24947 acronym = XCAR (acronym);
24948 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24949 }
24950 else
24951 {
24952 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24953 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24954 str = buf;
24955 }
24956 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24957 code[len] = font->driver->encode_char (font, str[len]);
24958 upper_len = (len + 1) / 2;
24959 font->driver->text_extents (font, code, upper_len,
24960 &metrics_upper);
24961 font->driver->text_extents (font, code + upper_len, len - upper_len,
24962 &metrics_lower);
24963
24964
24965
24966 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24967 width = max (metrics_upper.width, metrics_lower.width) + 4;
24968 upper_xoff = upper_yoff = 2; /* the typical case */
24969 if (base_width >= width)
24970 {
24971 /* Align the upper to the left, the lower to the right. */
24972 it->pixel_width = base_width;
24973 lower_xoff = base_width - 2 - metrics_lower.width;
24974 }
24975 else
24976 {
24977 /* Center the shorter one. */
24978 it->pixel_width = width;
24979 if (metrics_upper.width >= metrics_lower.width)
24980 lower_xoff = (width - metrics_lower.width) / 2;
24981 else
24982 {
24983 /* FIXME: This code doesn't look right. It formerly was
24984 missing the "lower_xoff = 0;", which couldn't have
24985 been right since it left lower_xoff uninitialized. */
24986 lower_xoff = 0;
24987 upper_xoff = (width - metrics_upper.width) / 2;
24988 }
24989 }
24990
24991 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24992 top, bottom, and between upper and lower strings. */
24993 height = (metrics_upper.ascent + metrics_upper.descent
24994 + metrics_lower.ascent + metrics_lower.descent) + 5;
24995 /* Center vertically.
24996 H:base_height, D:base_descent
24997 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24998
24999 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25000 descent = D - H/2 + h/2;
25001 lower_yoff = descent - 2 - ld;
25002 upper_yoff = lower_yoff - la - 1 - ud; */
25003 ascent = - (it->descent - (base_height + height + 1) / 2);
25004 descent = it->descent - (base_height - height) / 2;
25005 lower_yoff = descent - 2 - metrics_lower.descent;
25006 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25007 - metrics_upper.descent);
25008 /* Don't make the height shorter than the base height. */
25009 if (height > base_height)
25010 {
25011 it->ascent = ascent;
25012 it->descent = descent;
25013 }
25014 }
25015
25016 it->phys_ascent = it->ascent;
25017 it->phys_descent = it->descent;
25018 if (it->glyph_row)
25019 append_glyphless_glyph (it, face_id, for_no_font, len,
25020 upper_xoff, upper_yoff,
25021 lower_xoff, lower_yoff);
25022 it->nglyphs = 1;
25023 take_vertical_position_into_account (it);
25024 }
25025
25026
25027 /* RIF:
25028 Produce glyphs/get display metrics for the display element IT is
25029 loaded with. See the description of struct it in dispextern.h
25030 for an overview of struct it. */
25031
25032 void
25033 x_produce_glyphs (struct it *it)
25034 {
25035 int extra_line_spacing = it->extra_line_spacing;
25036
25037 it->glyph_not_available_p = 0;
25038
25039 if (it->what == IT_CHARACTER)
25040 {
25041 XChar2b char2b;
25042 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25043 struct font *font = face->font;
25044 struct font_metrics *pcm = NULL;
25045 int boff; /* baseline offset */
25046
25047 if (font == NULL)
25048 {
25049 /* When no suitable font is found, display this character by
25050 the method specified in the first extra slot of
25051 Vglyphless_char_display. */
25052 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25053
25054 eassert (it->what == IT_GLYPHLESS);
25055 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25056 goto done;
25057 }
25058
25059 boff = font->baseline_offset;
25060 if (font->vertical_centering)
25061 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25062
25063 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25064 {
25065 int stretched_p;
25066
25067 it->nglyphs = 1;
25068
25069 if (it->override_ascent >= 0)
25070 {
25071 it->ascent = it->override_ascent;
25072 it->descent = it->override_descent;
25073 boff = it->override_boff;
25074 }
25075 else
25076 {
25077 it->ascent = FONT_BASE (font) + boff;
25078 it->descent = FONT_DESCENT (font) - boff;
25079 }
25080
25081 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25082 {
25083 pcm = get_per_char_metric (font, &char2b);
25084 if (pcm->width == 0
25085 && pcm->rbearing == 0 && pcm->lbearing == 0)
25086 pcm = NULL;
25087 }
25088
25089 if (pcm)
25090 {
25091 it->phys_ascent = pcm->ascent + boff;
25092 it->phys_descent = pcm->descent - boff;
25093 it->pixel_width = pcm->width;
25094 }
25095 else
25096 {
25097 it->glyph_not_available_p = 1;
25098 it->phys_ascent = it->ascent;
25099 it->phys_descent = it->descent;
25100 it->pixel_width = font->space_width;
25101 }
25102
25103 if (it->constrain_row_ascent_descent_p)
25104 {
25105 if (it->descent > it->max_descent)
25106 {
25107 it->ascent += it->descent - it->max_descent;
25108 it->descent = it->max_descent;
25109 }
25110 if (it->ascent > it->max_ascent)
25111 {
25112 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25113 it->ascent = it->max_ascent;
25114 }
25115 it->phys_ascent = min (it->phys_ascent, it->ascent);
25116 it->phys_descent = min (it->phys_descent, it->descent);
25117 extra_line_spacing = 0;
25118 }
25119
25120 /* If this is a space inside a region of text with
25121 `space-width' property, change its width. */
25122 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25123 if (stretched_p)
25124 it->pixel_width *= XFLOATINT (it->space_width);
25125
25126 /* If face has a box, add the box thickness to the character
25127 height. If character has a box line to the left and/or
25128 right, add the box line width to the character's width. */
25129 if (face->box != FACE_NO_BOX)
25130 {
25131 int thick = face->box_line_width;
25132
25133 if (thick > 0)
25134 {
25135 it->ascent += thick;
25136 it->descent += thick;
25137 }
25138 else
25139 thick = -thick;
25140
25141 if (it->start_of_box_run_p)
25142 it->pixel_width += thick;
25143 if (it->end_of_box_run_p)
25144 it->pixel_width += thick;
25145 }
25146
25147 /* If face has an overline, add the height of the overline
25148 (1 pixel) and a 1 pixel margin to the character height. */
25149 if (face->overline_p)
25150 it->ascent += overline_margin;
25151
25152 if (it->constrain_row_ascent_descent_p)
25153 {
25154 if (it->ascent > it->max_ascent)
25155 it->ascent = it->max_ascent;
25156 if (it->descent > it->max_descent)
25157 it->descent = it->max_descent;
25158 }
25159
25160 take_vertical_position_into_account (it);
25161
25162 /* If we have to actually produce glyphs, do it. */
25163 if (it->glyph_row)
25164 {
25165 if (stretched_p)
25166 {
25167 /* Translate a space with a `space-width' property
25168 into a stretch glyph. */
25169 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25170 / FONT_HEIGHT (font));
25171 append_stretch_glyph (it, it->object, it->pixel_width,
25172 it->ascent + it->descent, ascent);
25173 }
25174 else
25175 append_glyph (it);
25176
25177 /* If characters with lbearing or rbearing are displayed
25178 in this line, record that fact in a flag of the
25179 glyph row. This is used to optimize X output code. */
25180 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25181 it->glyph_row->contains_overlapping_glyphs_p = 1;
25182 }
25183 if (! stretched_p && it->pixel_width == 0)
25184 /* We assure that all visible glyphs have at least 1-pixel
25185 width. */
25186 it->pixel_width = 1;
25187 }
25188 else if (it->char_to_display == '\n')
25189 {
25190 /* A newline has no width, but we need the height of the
25191 line. But if previous part of the line sets a height,
25192 don't increase that height */
25193
25194 Lisp_Object height;
25195 Lisp_Object total_height = Qnil;
25196
25197 it->override_ascent = -1;
25198 it->pixel_width = 0;
25199 it->nglyphs = 0;
25200
25201 height = get_it_property (it, Qline_height);
25202 /* Split (line-height total-height) list */
25203 if (CONSP (height)
25204 && CONSP (XCDR (height))
25205 && NILP (XCDR (XCDR (height))))
25206 {
25207 total_height = XCAR (XCDR (height));
25208 height = XCAR (height);
25209 }
25210 height = calc_line_height_property (it, height, font, boff, 1);
25211
25212 if (it->override_ascent >= 0)
25213 {
25214 it->ascent = it->override_ascent;
25215 it->descent = it->override_descent;
25216 boff = it->override_boff;
25217 }
25218 else
25219 {
25220 it->ascent = FONT_BASE (font) + boff;
25221 it->descent = FONT_DESCENT (font) - boff;
25222 }
25223
25224 if (EQ (height, Qt))
25225 {
25226 if (it->descent > it->max_descent)
25227 {
25228 it->ascent += it->descent - it->max_descent;
25229 it->descent = it->max_descent;
25230 }
25231 if (it->ascent > it->max_ascent)
25232 {
25233 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25234 it->ascent = it->max_ascent;
25235 }
25236 it->phys_ascent = min (it->phys_ascent, it->ascent);
25237 it->phys_descent = min (it->phys_descent, it->descent);
25238 it->constrain_row_ascent_descent_p = 1;
25239 extra_line_spacing = 0;
25240 }
25241 else
25242 {
25243 Lisp_Object spacing;
25244
25245 it->phys_ascent = it->ascent;
25246 it->phys_descent = it->descent;
25247
25248 if ((it->max_ascent > 0 || it->max_descent > 0)
25249 && face->box != FACE_NO_BOX
25250 && face->box_line_width > 0)
25251 {
25252 it->ascent += face->box_line_width;
25253 it->descent += face->box_line_width;
25254 }
25255 if (!NILP (height)
25256 && XINT (height) > it->ascent + it->descent)
25257 it->ascent = XINT (height) - it->descent;
25258
25259 if (!NILP (total_height))
25260 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25261 else
25262 {
25263 spacing = get_it_property (it, Qline_spacing);
25264 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25265 }
25266 if (INTEGERP (spacing))
25267 {
25268 extra_line_spacing = XINT (spacing);
25269 if (!NILP (total_height))
25270 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25271 }
25272 }
25273 }
25274 else /* i.e. (it->char_to_display == '\t') */
25275 {
25276 if (font->space_width > 0)
25277 {
25278 int tab_width = it->tab_width * font->space_width;
25279 int x = it->current_x + it->continuation_lines_width;
25280 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25281
25282 /* If the distance from the current position to the next tab
25283 stop is less than a space character width, use the
25284 tab stop after that. */
25285 if (next_tab_x - x < font->space_width)
25286 next_tab_x += tab_width;
25287
25288 it->pixel_width = next_tab_x - x;
25289 it->nglyphs = 1;
25290 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25291 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25292
25293 if (it->glyph_row)
25294 {
25295 append_stretch_glyph (it, it->object, it->pixel_width,
25296 it->ascent + it->descent, it->ascent);
25297 }
25298 }
25299 else
25300 {
25301 it->pixel_width = 0;
25302 it->nglyphs = 1;
25303 }
25304 }
25305 }
25306 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25307 {
25308 /* A static composition.
25309
25310 Note: A composition is represented as one glyph in the
25311 glyph matrix. There are no padding glyphs.
25312
25313 Important note: pixel_width, ascent, and descent are the
25314 values of what is drawn by draw_glyphs (i.e. the values of
25315 the overall glyphs composed). */
25316 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25317 int boff; /* baseline offset */
25318 struct composition *cmp = composition_table[it->cmp_it.id];
25319 int glyph_len = cmp->glyph_len;
25320 struct font *font = face->font;
25321
25322 it->nglyphs = 1;
25323
25324 /* If we have not yet calculated pixel size data of glyphs of
25325 the composition for the current face font, calculate them
25326 now. Theoretically, we have to check all fonts for the
25327 glyphs, but that requires much time and memory space. So,
25328 here we check only the font of the first glyph. This may
25329 lead to incorrect display, but it's very rare, and C-l
25330 (recenter-top-bottom) can correct the display anyway. */
25331 if (! cmp->font || cmp->font != font)
25332 {
25333 /* Ascent and descent of the font of the first character
25334 of this composition (adjusted by baseline offset).
25335 Ascent and descent of overall glyphs should not be less
25336 than these, respectively. */
25337 int font_ascent, font_descent, font_height;
25338 /* Bounding box of the overall glyphs. */
25339 int leftmost, rightmost, lowest, highest;
25340 int lbearing, rbearing;
25341 int i, width, ascent, descent;
25342 int left_padded = 0, right_padded = 0;
25343 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25344 XChar2b char2b;
25345 struct font_metrics *pcm;
25346 int font_not_found_p;
25347 ptrdiff_t pos;
25348
25349 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25350 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25351 break;
25352 if (glyph_len < cmp->glyph_len)
25353 right_padded = 1;
25354 for (i = 0; i < glyph_len; i++)
25355 {
25356 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25357 break;
25358 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25359 }
25360 if (i > 0)
25361 left_padded = 1;
25362
25363 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25364 : IT_CHARPOS (*it));
25365 /* If no suitable font is found, use the default font. */
25366 font_not_found_p = font == NULL;
25367 if (font_not_found_p)
25368 {
25369 face = face->ascii_face;
25370 font = face->font;
25371 }
25372 boff = font->baseline_offset;
25373 if (font->vertical_centering)
25374 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25375 font_ascent = FONT_BASE (font) + boff;
25376 font_descent = FONT_DESCENT (font) - boff;
25377 font_height = FONT_HEIGHT (font);
25378
25379 cmp->font = font;
25380
25381 pcm = NULL;
25382 if (! font_not_found_p)
25383 {
25384 get_char_face_and_encoding (it->f, c, it->face_id,
25385 &char2b, 0);
25386 pcm = get_per_char_metric (font, &char2b);
25387 }
25388
25389 /* Initialize the bounding box. */
25390 if (pcm)
25391 {
25392 width = cmp->glyph_len > 0 ? pcm->width : 0;
25393 ascent = pcm->ascent;
25394 descent = pcm->descent;
25395 lbearing = pcm->lbearing;
25396 rbearing = pcm->rbearing;
25397 }
25398 else
25399 {
25400 width = cmp->glyph_len > 0 ? font->space_width : 0;
25401 ascent = FONT_BASE (font);
25402 descent = FONT_DESCENT (font);
25403 lbearing = 0;
25404 rbearing = width;
25405 }
25406
25407 rightmost = width;
25408 leftmost = 0;
25409 lowest = - descent + boff;
25410 highest = ascent + boff;
25411
25412 if (! font_not_found_p
25413 && font->default_ascent
25414 && CHAR_TABLE_P (Vuse_default_ascent)
25415 && !NILP (Faref (Vuse_default_ascent,
25416 make_number (it->char_to_display))))
25417 highest = font->default_ascent + boff;
25418
25419 /* Draw the first glyph at the normal position. It may be
25420 shifted to right later if some other glyphs are drawn
25421 at the left. */
25422 cmp->offsets[i * 2] = 0;
25423 cmp->offsets[i * 2 + 1] = boff;
25424 cmp->lbearing = lbearing;
25425 cmp->rbearing = rbearing;
25426
25427 /* Set cmp->offsets for the remaining glyphs. */
25428 for (i++; i < glyph_len; i++)
25429 {
25430 int left, right, btm, top;
25431 int ch = COMPOSITION_GLYPH (cmp, i);
25432 int face_id;
25433 struct face *this_face;
25434
25435 if (ch == '\t')
25436 ch = ' ';
25437 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25438 this_face = FACE_FROM_ID (it->f, face_id);
25439 font = this_face->font;
25440
25441 if (font == NULL)
25442 pcm = NULL;
25443 else
25444 {
25445 get_char_face_and_encoding (it->f, ch, face_id,
25446 &char2b, 0);
25447 pcm = get_per_char_metric (font, &char2b);
25448 }
25449 if (! pcm)
25450 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25451 else
25452 {
25453 width = pcm->width;
25454 ascent = pcm->ascent;
25455 descent = pcm->descent;
25456 lbearing = pcm->lbearing;
25457 rbearing = pcm->rbearing;
25458 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25459 {
25460 /* Relative composition with or without
25461 alternate chars. */
25462 left = (leftmost + rightmost - width) / 2;
25463 btm = - descent + boff;
25464 if (font->relative_compose
25465 && (! CHAR_TABLE_P (Vignore_relative_composition)
25466 || NILP (Faref (Vignore_relative_composition,
25467 make_number (ch)))))
25468 {
25469
25470 if (- descent >= font->relative_compose)
25471 /* One extra pixel between two glyphs. */
25472 btm = highest + 1;
25473 else if (ascent <= 0)
25474 /* One extra pixel between two glyphs. */
25475 btm = lowest - 1 - ascent - descent;
25476 }
25477 }
25478 else
25479 {
25480 /* A composition rule is specified by an integer
25481 value that encodes global and new reference
25482 points (GREF and NREF). GREF and NREF are
25483 specified by numbers as below:
25484
25485 0---1---2 -- ascent
25486 | |
25487 | |
25488 | |
25489 9--10--11 -- center
25490 | |
25491 ---3---4---5--- baseline
25492 | |
25493 6---7---8 -- descent
25494 */
25495 int rule = COMPOSITION_RULE (cmp, i);
25496 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25497
25498 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25499 grefx = gref % 3, nrefx = nref % 3;
25500 grefy = gref / 3, nrefy = nref / 3;
25501 if (xoff)
25502 xoff = font_height * (xoff - 128) / 256;
25503 if (yoff)
25504 yoff = font_height * (yoff - 128) / 256;
25505
25506 left = (leftmost
25507 + grefx * (rightmost - leftmost) / 2
25508 - nrefx * width / 2
25509 + xoff);
25510
25511 btm = ((grefy == 0 ? highest
25512 : grefy == 1 ? 0
25513 : grefy == 2 ? lowest
25514 : (highest + lowest) / 2)
25515 - (nrefy == 0 ? ascent + descent
25516 : nrefy == 1 ? descent - boff
25517 : nrefy == 2 ? 0
25518 : (ascent + descent) / 2)
25519 + yoff);
25520 }
25521
25522 cmp->offsets[i * 2] = left;
25523 cmp->offsets[i * 2 + 1] = btm + descent;
25524
25525 /* Update the bounding box of the overall glyphs. */
25526 if (width > 0)
25527 {
25528 right = left + width;
25529 if (left < leftmost)
25530 leftmost = left;
25531 if (right > rightmost)
25532 rightmost = right;
25533 }
25534 top = btm + descent + ascent;
25535 if (top > highest)
25536 highest = top;
25537 if (btm < lowest)
25538 lowest = btm;
25539
25540 if (cmp->lbearing > left + lbearing)
25541 cmp->lbearing = left + lbearing;
25542 if (cmp->rbearing < left + rbearing)
25543 cmp->rbearing = left + rbearing;
25544 }
25545 }
25546
25547 /* If there are glyphs whose x-offsets are negative,
25548 shift all glyphs to the right and make all x-offsets
25549 non-negative. */
25550 if (leftmost < 0)
25551 {
25552 for (i = 0; i < cmp->glyph_len; i++)
25553 cmp->offsets[i * 2] -= leftmost;
25554 rightmost -= leftmost;
25555 cmp->lbearing -= leftmost;
25556 cmp->rbearing -= leftmost;
25557 }
25558
25559 if (left_padded && cmp->lbearing < 0)
25560 {
25561 for (i = 0; i < cmp->glyph_len; i++)
25562 cmp->offsets[i * 2] -= cmp->lbearing;
25563 rightmost -= cmp->lbearing;
25564 cmp->rbearing -= cmp->lbearing;
25565 cmp->lbearing = 0;
25566 }
25567 if (right_padded && rightmost < cmp->rbearing)
25568 {
25569 rightmost = cmp->rbearing;
25570 }
25571
25572 cmp->pixel_width = rightmost;
25573 cmp->ascent = highest;
25574 cmp->descent = - lowest;
25575 if (cmp->ascent < font_ascent)
25576 cmp->ascent = font_ascent;
25577 if (cmp->descent < font_descent)
25578 cmp->descent = font_descent;
25579 }
25580
25581 if (it->glyph_row
25582 && (cmp->lbearing < 0
25583 || cmp->rbearing > cmp->pixel_width))
25584 it->glyph_row->contains_overlapping_glyphs_p = 1;
25585
25586 it->pixel_width = cmp->pixel_width;
25587 it->ascent = it->phys_ascent = cmp->ascent;
25588 it->descent = it->phys_descent = cmp->descent;
25589 if (face->box != FACE_NO_BOX)
25590 {
25591 int thick = face->box_line_width;
25592
25593 if (thick > 0)
25594 {
25595 it->ascent += thick;
25596 it->descent += thick;
25597 }
25598 else
25599 thick = - thick;
25600
25601 if (it->start_of_box_run_p)
25602 it->pixel_width += thick;
25603 if (it->end_of_box_run_p)
25604 it->pixel_width += thick;
25605 }
25606
25607 /* If face has an overline, add the height of the overline
25608 (1 pixel) and a 1 pixel margin to the character height. */
25609 if (face->overline_p)
25610 it->ascent += overline_margin;
25611
25612 take_vertical_position_into_account (it);
25613 if (it->ascent < 0)
25614 it->ascent = 0;
25615 if (it->descent < 0)
25616 it->descent = 0;
25617
25618 if (it->glyph_row && cmp->glyph_len > 0)
25619 append_composite_glyph (it);
25620 }
25621 else if (it->what == IT_COMPOSITION)
25622 {
25623 /* A dynamic (automatic) composition. */
25624 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25625 Lisp_Object gstring;
25626 struct font_metrics metrics;
25627
25628 it->nglyphs = 1;
25629
25630 gstring = composition_gstring_from_id (it->cmp_it.id);
25631 it->pixel_width
25632 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25633 &metrics);
25634 if (it->glyph_row
25635 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25636 it->glyph_row->contains_overlapping_glyphs_p = 1;
25637 it->ascent = it->phys_ascent = metrics.ascent;
25638 it->descent = it->phys_descent = metrics.descent;
25639 if (face->box != FACE_NO_BOX)
25640 {
25641 int thick = face->box_line_width;
25642
25643 if (thick > 0)
25644 {
25645 it->ascent += thick;
25646 it->descent += thick;
25647 }
25648 else
25649 thick = - thick;
25650
25651 if (it->start_of_box_run_p)
25652 it->pixel_width += thick;
25653 if (it->end_of_box_run_p)
25654 it->pixel_width += thick;
25655 }
25656 /* If face has an overline, add the height of the overline
25657 (1 pixel) and a 1 pixel margin to the character height. */
25658 if (face->overline_p)
25659 it->ascent += overline_margin;
25660 take_vertical_position_into_account (it);
25661 if (it->ascent < 0)
25662 it->ascent = 0;
25663 if (it->descent < 0)
25664 it->descent = 0;
25665
25666 if (it->glyph_row)
25667 append_composite_glyph (it);
25668 }
25669 else if (it->what == IT_GLYPHLESS)
25670 produce_glyphless_glyph (it, 0, Qnil);
25671 else if (it->what == IT_IMAGE)
25672 produce_image_glyph (it);
25673 else if (it->what == IT_STRETCH)
25674 produce_stretch_glyph (it);
25675
25676 done:
25677 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25678 because this isn't true for images with `:ascent 100'. */
25679 eassert (it->ascent >= 0 && it->descent >= 0);
25680 if (it->area == TEXT_AREA)
25681 it->current_x += it->pixel_width;
25682
25683 if (extra_line_spacing > 0)
25684 {
25685 it->descent += extra_line_spacing;
25686 if (extra_line_spacing > it->max_extra_line_spacing)
25687 it->max_extra_line_spacing = extra_line_spacing;
25688 }
25689
25690 it->max_ascent = max (it->max_ascent, it->ascent);
25691 it->max_descent = max (it->max_descent, it->descent);
25692 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25693 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25694 }
25695
25696 /* EXPORT for RIF:
25697 Output LEN glyphs starting at START at the nominal cursor position.
25698 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25699 being updated, and UPDATED_AREA is the area of that row being updated. */
25700
25701 void
25702 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25703 struct glyph *start, enum glyph_row_area updated_area, int len)
25704 {
25705 int x, hpos, chpos = w->phys_cursor.hpos;
25706
25707 eassert (updated_row);
25708 /* When the window is hscrolled, cursor hpos can legitimately be out
25709 of bounds, but we draw the cursor at the corresponding window
25710 margin in that case. */
25711 if (!updated_row->reversed_p && chpos < 0)
25712 chpos = 0;
25713 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25714 chpos = updated_row->used[TEXT_AREA] - 1;
25715
25716 block_input ();
25717
25718 /* Write glyphs. */
25719
25720 hpos = start - updated_row->glyphs[updated_area];
25721 x = draw_glyphs (w, w->output_cursor.x,
25722 updated_row, updated_area,
25723 hpos, hpos + len,
25724 DRAW_NORMAL_TEXT, 0);
25725
25726 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25727 if (updated_area == TEXT_AREA
25728 && w->phys_cursor_on_p
25729 && w->phys_cursor.vpos == w->output_cursor.vpos
25730 && chpos >= hpos
25731 && chpos < hpos + len)
25732 w->phys_cursor_on_p = 0;
25733
25734 unblock_input ();
25735
25736 /* Advance the output cursor. */
25737 w->output_cursor.hpos += len;
25738 w->output_cursor.x = x;
25739 }
25740
25741
25742 /* EXPORT for RIF:
25743 Insert LEN glyphs from START at the nominal cursor position. */
25744
25745 void
25746 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25747 struct glyph *start, enum glyph_row_area updated_area, int len)
25748 {
25749 struct frame *f;
25750 int line_height, shift_by_width, shifted_region_width;
25751 struct glyph_row *row;
25752 struct glyph *glyph;
25753 int frame_x, frame_y;
25754 ptrdiff_t hpos;
25755
25756 eassert (updated_row);
25757 block_input ();
25758 f = XFRAME (WINDOW_FRAME (w));
25759
25760 /* Get the height of the line we are in. */
25761 row = updated_row;
25762 line_height = row->height;
25763
25764 /* Get the width of the glyphs to insert. */
25765 shift_by_width = 0;
25766 for (glyph = start; glyph < start + len; ++glyph)
25767 shift_by_width += glyph->pixel_width;
25768
25769 /* Get the width of the region to shift right. */
25770 shifted_region_width = (window_box_width (w, updated_area)
25771 - w->output_cursor.x
25772 - shift_by_width);
25773
25774 /* Shift right. */
25775 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25776 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25777
25778 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25779 line_height, shift_by_width);
25780
25781 /* Write the glyphs. */
25782 hpos = start - row->glyphs[updated_area];
25783 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25784 hpos, hpos + len,
25785 DRAW_NORMAL_TEXT, 0);
25786
25787 /* Advance the output cursor. */
25788 w->output_cursor.hpos += len;
25789 w->output_cursor.x += shift_by_width;
25790 unblock_input ();
25791 }
25792
25793
25794 /* EXPORT for RIF:
25795 Erase the current text line from the nominal cursor position
25796 (inclusive) to pixel column TO_X (exclusive). The idea is that
25797 everything from TO_X onward is already erased.
25798
25799 TO_X is a pixel position relative to UPDATED_AREA of currently
25800 updated window W. TO_X == -1 means clear to the end of this area. */
25801
25802 void
25803 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25804 enum glyph_row_area updated_area, int to_x)
25805 {
25806 struct frame *f;
25807 int max_x, min_y, max_y;
25808 int from_x, from_y, to_y;
25809
25810 eassert (updated_row);
25811 f = XFRAME (w->frame);
25812
25813 if (updated_row->full_width_p)
25814 max_x = WINDOW_TOTAL_WIDTH (w);
25815 else
25816 max_x = window_box_width (w, updated_area);
25817 max_y = window_text_bottom_y (w);
25818
25819 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25820 of window. For TO_X > 0, truncate to end of drawing area. */
25821 if (to_x == 0)
25822 return;
25823 else if (to_x < 0)
25824 to_x = max_x;
25825 else
25826 to_x = min (to_x, max_x);
25827
25828 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25829
25830 /* Notice if the cursor will be cleared by this operation. */
25831 if (!updated_row->full_width_p)
25832 notice_overwritten_cursor (w, updated_area,
25833 w->output_cursor.x, -1,
25834 updated_row->y,
25835 MATRIX_ROW_BOTTOM_Y (updated_row));
25836
25837 from_x = w->output_cursor.x;
25838
25839 /* Translate to frame coordinates. */
25840 if (updated_row->full_width_p)
25841 {
25842 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25843 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25844 }
25845 else
25846 {
25847 int area_left = window_box_left (w, updated_area);
25848 from_x += area_left;
25849 to_x += area_left;
25850 }
25851
25852 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25853 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
25854 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25855
25856 /* Prevent inadvertently clearing to end of the X window. */
25857 if (to_x > from_x && to_y > from_y)
25858 {
25859 block_input ();
25860 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25861 to_x - from_x, to_y - from_y);
25862 unblock_input ();
25863 }
25864 }
25865
25866 #endif /* HAVE_WINDOW_SYSTEM */
25867
25868
25869 \f
25870 /***********************************************************************
25871 Cursor types
25872 ***********************************************************************/
25873
25874 /* Value is the internal representation of the specified cursor type
25875 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25876 of the bar cursor. */
25877
25878 static enum text_cursor_kinds
25879 get_specified_cursor_type (Lisp_Object arg, int *width)
25880 {
25881 enum text_cursor_kinds type;
25882
25883 if (NILP (arg))
25884 return NO_CURSOR;
25885
25886 if (EQ (arg, Qbox))
25887 return FILLED_BOX_CURSOR;
25888
25889 if (EQ (arg, Qhollow))
25890 return HOLLOW_BOX_CURSOR;
25891
25892 if (EQ (arg, Qbar))
25893 {
25894 *width = 2;
25895 return BAR_CURSOR;
25896 }
25897
25898 if (CONSP (arg)
25899 && EQ (XCAR (arg), Qbar)
25900 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25901 {
25902 *width = XINT (XCDR (arg));
25903 return BAR_CURSOR;
25904 }
25905
25906 if (EQ (arg, Qhbar))
25907 {
25908 *width = 2;
25909 return HBAR_CURSOR;
25910 }
25911
25912 if (CONSP (arg)
25913 && EQ (XCAR (arg), Qhbar)
25914 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25915 {
25916 *width = XINT (XCDR (arg));
25917 return HBAR_CURSOR;
25918 }
25919
25920 /* Treat anything unknown as "hollow box cursor".
25921 It was bad to signal an error; people have trouble fixing
25922 .Xdefaults with Emacs, when it has something bad in it. */
25923 type = HOLLOW_BOX_CURSOR;
25924
25925 return type;
25926 }
25927
25928 /* Set the default cursor types for specified frame. */
25929 void
25930 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25931 {
25932 int width = 1;
25933 Lisp_Object tem;
25934
25935 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25936 FRAME_CURSOR_WIDTH (f) = width;
25937
25938 /* By default, set up the blink-off state depending on the on-state. */
25939
25940 tem = Fassoc (arg, Vblink_cursor_alist);
25941 if (!NILP (tem))
25942 {
25943 FRAME_BLINK_OFF_CURSOR (f)
25944 = get_specified_cursor_type (XCDR (tem), &width);
25945 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25946 }
25947 else
25948 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25949
25950 /* Make sure the cursor gets redrawn. */
25951 cursor_type_changed = 1;
25952 }
25953
25954
25955 #ifdef HAVE_WINDOW_SYSTEM
25956
25957 /* Return the cursor we want to be displayed in window W. Return
25958 width of bar/hbar cursor through WIDTH arg. Return with
25959 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25960 (i.e. if the `system caret' should track this cursor).
25961
25962 In a mini-buffer window, we want the cursor only to appear if we
25963 are reading input from this window. For the selected window, we
25964 want the cursor type given by the frame parameter or buffer local
25965 setting of cursor-type. If explicitly marked off, draw no cursor.
25966 In all other cases, we want a hollow box cursor. */
25967
25968 static enum text_cursor_kinds
25969 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25970 int *active_cursor)
25971 {
25972 struct frame *f = XFRAME (w->frame);
25973 struct buffer *b = XBUFFER (w->contents);
25974 int cursor_type = DEFAULT_CURSOR;
25975 Lisp_Object alt_cursor;
25976 int non_selected = 0;
25977
25978 *active_cursor = 1;
25979
25980 /* Echo area */
25981 if (cursor_in_echo_area
25982 && FRAME_HAS_MINIBUF_P (f)
25983 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25984 {
25985 if (w == XWINDOW (echo_area_window))
25986 {
25987 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25988 {
25989 *width = FRAME_CURSOR_WIDTH (f);
25990 return FRAME_DESIRED_CURSOR (f);
25991 }
25992 else
25993 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25994 }
25995
25996 *active_cursor = 0;
25997 non_selected = 1;
25998 }
25999
26000 /* Detect a nonselected window or nonselected frame. */
26001 else if (w != XWINDOW (f->selected_window)
26002 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26003 {
26004 *active_cursor = 0;
26005
26006 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26007 return NO_CURSOR;
26008
26009 non_selected = 1;
26010 }
26011
26012 /* Never display a cursor in a window in which cursor-type is nil. */
26013 if (NILP (BVAR (b, cursor_type)))
26014 return NO_CURSOR;
26015
26016 /* Get the normal cursor type for this window. */
26017 if (EQ (BVAR (b, cursor_type), Qt))
26018 {
26019 cursor_type = FRAME_DESIRED_CURSOR (f);
26020 *width = FRAME_CURSOR_WIDTH (f);
26021 }
26022 else
26023 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26024
26025 /* Use cursor-in-non-selected-windows instead
26026 for non-selected window or frame. */
26027 if (non_selected)
26028 {
26029 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26030 if (!EQ (Qt, alt_cursor))
26031 return get_specified_cursor_type (alt_cursor, width);
26032 /* t means modify the normal cursor type. */
26033 if (cursor_type == FILLED_BOX_CURSOR)
26034 cursor_type = HOLLOW_BOX_CURSOR;
26035 else if (cursor_type == BAR_CURSOR && *width > 1)
26036 --*width;
26037 return cursor_type;
26038 }
26039
26040 /* Use normal cursor if not blinked off. */
26041 if (!w->cursor_off_p)
26042 {
26043 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26044 {
26045 if (cursor_type == FILLED_BOX_CURSOR)
26046 {
26047 /* Using a block cursor on large images can be very annoying.
26048 So use a hollow cursor for "large" images.
26049 If image is not transparent (no mask), also use hollow cursor. */
26050 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26051 if (img != NULL && IMAGEP (img->spec))
26052 {
26053 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26054 where N = size of default frame font size.
26055 This should cover most of the "tiny" icons people may use. */
26056 if (!img->mask
26057 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26058 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26059 cursor_type = HOLLOW_BOX_CURSOR;
26060 }
26061 }
26062 else if (cursor_type != NO_CURSOR)
26063 {
26064 /* Display current only supports BOX and HOLLOW cursors for images.
26065 So for now, unconditionally use a HOLLOW cursor when cursor is
26066 not a solid box cursor. */
26067 cursor_type = HOLLOW_BOX_CURSOR;
26068 }
26069 }
26070 return cursor_type;
26071 }
26072
26073 /* Cursor is blinked off, so determine how to "toggle" it. */
26074
26075 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26076 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26077 return get_specified_cursor_type (XCDR (alt_cursor), width);
26078
26079 /* Then see if frame has specified a specific blink off cursor type. */
26080 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26081 {
26082 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26083 return FRAME_BLINK_OFF_CURSOR (f);
26084 }
26085
26086 #if 0
26087 /* Some people liked having a permanently visible blinking cursor,
26088 while others had very strong opinions against it. So it was
26089 decided to remove it. KFS 2003-09-03 */
26090
26091 /* Finally perform built-in cursor blinking:
26092 filled box <-> hollow box
26093 wide [h]bar <-> narrow [h]bar
26094 narrow [h]bar <-> no cursor
26095 other type <-> no cursor */
26096
26097 if (cursor_type == FILLED_BOX_CURSOR)
26098 return HOLLOW_BOX_CURSOR;
26099
26100 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26101 {
26102 *width = 1;
26103 return cursor_type;
26104 }
26105 #endif
26106
26107 return NO_CURSOR;
26108 }
26109
26110
26111 /* Notice when the text cursor of window W has been completely
26112 overwritten by a drawing operation that outputs glyphs in AREA
26113 starting at X0 and ending at X1 in the line starting at Y0 and
26114 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26115 the rest of the line after X0 has been written. Y coordinates
26116 are window-relative. */
26117
26118 static void
26119 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26120 int x0, int x1, int y0, int y1)
26121 {
26122 int cx0, cx1, cy0, cy1;
26123 struct glyph_row *row;
26124
26125 if (!w->phys_cursor_on_p)
26126 return;
26127 if (area != TEXT_AREA)
26128 return;
26129
26130 if (w->phys_cursor.vpos < 0
26131 || w->phys_cursor.vpos >= w->current_matrix->nrows
26132 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26133 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26134 return;
26135
26136 if (row->cursor_in_fringe_p)
26137 {
26138 row->cursor_in_fringe_p = 0;
26139 draw_fringe_bitmap (w, row, row->reversed_p);
26140 w->phys_cursor_on_p = 0;
26141 return;
26142 }
26143
26144 cx0 = w->phys_cursor.x;
26145 cx1 = cx0 + w->phys_cursor_width;
26146 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26147 return;
26148
26149 /* The cursor image will be completely removed from the
26150 screen if the output area intersects the cursor area in
26151 y-direction. When we draw in [y0 y1[, and some part of
26152 the cursor is at y < y0, that part must have been drawn
26153 before. When scrolling, the cursor is erased before
26154 actually scrolling, so we don't come here. When not
26155 scrolling, the rows above the old cursor row must have
26156 changed, and in this case these rows must have written
26157 over the cursor image.
26158
26159 Likewise if part of the cursor is below y1, with the
26160 exception of the cursor being in the first blank row at
26161 the buffer and window end because update_text_area
26162 doesn't draw that row. (Except when it does, but
26163 that's handled in update_text_area.) */
26164
26165 cy0 = w->phys_cursor.y;
26166 cy1 = cy0 + w->phys_cursor_height;
26167 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26168 return;
26169
26170 w->phys_cursor_on_p = 0;
26171 }
26172
26173 #endif /* HAVE_WINDOW_SYSTEM */
26174
26175 \f
26176 /************************************************************************
26177 Mouse Face
26178 ************************************************************************/
26179
26180 #ifdef HAVE_WINDOW_SYSTEM
26181
26182 /* EXPORT for RIF:
26183 Fix the display of area AREA of overlapping row ROW in window W
26184 with respect to the overlapping part OVERLAPS. */
26185
26186 void
26187 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26188 enum glyph_row_area area, int overlaps)
26189 {
26190 int i, x;
26191
26192 block_input ();
26193
26194 x = 0;
26195 for (i = 0; i < row->used[area];)
26196 {
26197 if (row->glyphs[area][i].overlaps_vertically_p)
26198 {
26199 int start = i, start_x = x;
26200
26201 do
26202 {
26203 x += row->glyphs[area][i].pixel_width;
26204 ++i;
26205 }
26206 while (i < row->used[area]
26207 && row->glyphs[area][i].overlaps_vertically_p);
26208
26209 draw_glyphs (w, start_x, row, area,
26210 start, i,
26211 DRAW_NORMAL_TEXT, overlaps);
26212 }
26213 else
26214 {
26215 x += row->glyphs[area][i].pixel_width;
26216 ++i;
26217 }
26218 }
26219
26220 unblock_input ();
26221 }
26222
26223
26224 /* EXPORT:
26225 Draw the cursor glyph of window W in glyph row ROW. See the
26226 comment of draw_glyphs for the meaning of HL. */
26227
26228 void
26229 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26230 enum draw_glyphs_face hl)
26231 {
26232 /* If cursor hpos is out of bounds, don't draw garbage. This can
26233 happen in mini-buffer windows when switching between echo area
26234 glyphs and mini-buffer. */
26235 if ((row->reversed_p
26236 ? (w->phys_cursor.hpos >= 0)
26237 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26238 {
26239 int on_p = w->phys_cursor_on_p;
26240 int x1;
26241 int hpos = w->phys_cursor.hpos;
26242
26243 /* When the window is hscrolled, cursor hpos can legitimately be
26244 out of bounds, but we draw the cursor at the corresponding
26245 window margin in that case. */
26246 if (!row->reversed_p && hpos < 0)
26247 hpos = 0;
26248 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26249 hpos = row->used[TEXT_AREA] - 1;
26250
26251 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26252 hl, 0);
26253 w->phys_cursor_on_p = on_p;
26254
26255 if (hl == DRAW_CURSOR)
26256 w->phys_cursor_width = x1 - w->phys_cursor.x;
26257 /* When we erase the cursor, and ROW is overlapped by other
26258 rows, make sure that these overlapping parts of other rows
26259 are redrawn. */
26260 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26261 {
26262 w->phys_cursor_width = x1 - w->phys_cursor.x;
26263
26264 if (row > w->current_matrix->rows
26265 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26266 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26267 OVERLAPS_ERASED_CURSOR);
26268
26269 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26270 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26271 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26272 OVERLAPS_ERASED_CURSOR);
26273 }
26274 }
26275 }
26276
26277
26278 /* EXPORT:
26279 Erase the image of a cursor of window W from the screen. */
26280
26281 void
26282 erase_phys_cursor (struct window *w)
26283 {
26284 struct frame *f = XFRAME (w->frame);
26285 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26286 int hpos = w->phys_cursor.hpos;
26287 int vpos = w->phys_cursor.vpos;
26288 int mouse_face_here_p = 0;
26289 struct glyph_matrix *active_glyphs = w->current_matrix;
26290 struct glyph_row *cursor_row;
26291 struct glyph *cursor_glyph;
26292 enum draw_glyphs_face hl;
26293
26294 /* No cursor displayed or row invalidated => nothing to do on the
26295 screen. */
26296 if (w->phys_cursor_type == NO_CURSOR)
26297 goto mark_cursor_off;
26298
26299 /* VPOS >= active_glyphs->nrows means that window has been resized.
26300 Don't bother to erase the cursor. */
26301 if (vpos >= active_glyphs->nrows)
26302 goto mark_cursor_off;
26303
26304 /* If row containing cursor is marked invalid, there is nothing we
26305 can do. */
26306 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26307 if (!cursor_row->enabled_p)
26308 goto mark_cursor_off;
26309
26310 /* If line spacing is > 0, old cursor may only be partially visible in
26311 window after split-window. So adjust visible height. */
26312 cursor_row->visible_height = min (cursor_row->visible_height,
26313 window_text_bottom_y (w) - cursor_row->y);
26314
26315 /* If row is completely invisible, don't attempt to delete a cursor which
26316 isn't there. This can happen if cursor is at top of a window, and
26317 we switch to a buffer with a header line in that window. */
26318 if (cursor_row->visible_height <= 0)
26319 goto mark_cursor_off;
26320
26321 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26322 if (cursor_row->cursor_in_fringe_p)
26323 {
26324 cursor_row->cursor_in_fringe_p = 0;
26325 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26326 goto mark_cursor_off;
26327 }
26328
26329 /* This can happen when the new row is shorter than the old one.
26330 In this case, either draw_glyphs or clear_end_of_line
26331 should have cleared the cursor. Note that we wouldn't be
26332 able to erase the cursor in this case because we don't have a
26333 cursor glyph at hand. */
26334 if ((cursor_row->reversed_p
26335 ? (w->phys_cursor.hpos < 0)
26336 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26337 goto mark_cursor_off;
26338
26339 /* When the window is hscrolled, cursor hpos can legitimately be out
26340 of bounds, but we draw the cursor at the corresponding window
26341 margin in that case. */
26342 if (!cursor_row->reversed_p && hpos < 0)
26343 hpos = 0;
26344 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26345 hpos = cursor_row->used[TEXT_AREA] - 1;
26346
26347 /* If the cursor is in the mouse face area, redisplay that when
26348 we clear the cursor. */
26349 if (! NILP (hlinfo->mouse_face_window)
26350 && coords_in_mouse_face_p (w, hpos, vpos)
26351 /* Don't redraw the cursor's spot in mouse face if it is at the
26352 end of a line (on a newline). The cursor appears there, but
26353 mouse highlighting does not. */
26354 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26355 mouse_face_here_p = 1;
26356
26357 /* Maybe clear the display under the cursor. */
26358 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26359 {
26360 int x, y, left_x;
26361 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26362 int width;
26363
26364 cursor_glyph = get_phys_cursor_glyph (w);
26365 if (cursor_glyph == NULL)
26366 goto mark_cursor_off;
26367
26368 width = cursor_glyph->pixel_width;
26369 left_x = window_box_left_offset (w, TEXT_AREA);
26370 x = w->phys_cursor.x;
26371 if (x < left_x)
26372 width -= left_x - x;
26373 width = min (width, window_box_width (w, TEXT_AREA) - x);
26374 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26375 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26376
26377 if (width > 0)
26378 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26379 }
26380
26381 /* Erase the cursor by redrawing the character underneath it. */
26382 if (mouse_face_here_p)
26383 hl = DRAW_MOUSE_FACE;
26384 else
26385 hl = DRAW_NORMAL_TEXT;
26386 draw_phys_cursor_glyph (w, cursor_row, hl);
26387
26388 mark_cursor_off:
26389 w->phys_cursor_on_p = 0;
26390 w->phys_cursor_type = NO_CURSOR;
26391 }
26392
26393
26394 /* EXPORT:
26395 Display or clear cursor of window W. If ON is zero, clear the
26396 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26397 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26398
26399 void
26400 display_and_set_cursor (struct window *w, bool on,
26401 int hpos, int vpos, int x, int y)
26402 {
26403 struct frame *f = XFRAME (w->frame);
26404 int new_cursor_type;
26405 int new_cursor_width;
26406 int active_cursor;
26407 struct glyph_row *glyph_row;
26408 struct glyph *glyph;
26409
26410 /* This is pointless on invisible frames, and dangerous on garbaged
26411 windows and frames; in the latter case, the frame or window may
26412 be in the midst of changing its size, and x and y may be off the
26413 window. */
26414 if (! FRAME_VISIBLE_P (f)
26415 || FRAME_GARBAGED_P (f)
26416 || vpos >= w->current_matrix->nrows
26417 || hpos >= w->current_matrix->matrix_w)
26418 return;
26419
26420 /* If cursor is off and we want it off, return quickly. */
26421 if (!on && !w->phys_cursor_on_p)
26422 return;
26423
26424 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26425 /* If cursor row is not enabled, we don't really know where to
26426 display the cursor. */
26427 if (!glyph_row->enabled_p)
26428 {
26429 w->phys_cursor_on_p = 0;
26430 return;
26431 }
26432
26433 glyph = NULL;
26434 if (!glyph_row->exact_window_width_line_p
26435 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26436 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26437
26438 eassert (input_blocked_p ());
26439
26440 /* Set new_cursor_type to the cursor we want to be displayed. */
26441 new_cursor_type = get_window_cursor_type (w, glyph,
26442 &new_cursor_width, &active_cursor);
26443
26444 /* If cursor is currently being shown and we don't want it to be or
26445 it is in the wrong place, or the cursor type is not what we want,
26446 erase it. */
26447 if (w->phys_cursor_on_p
26448 && (!on
26449 || w->phys_cursor.x != x
26450 || w->phys_cursor.y != y
26451 || new_cursor_type != w->phys_cursor_type
26452 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26453 && new_cursor_width != w->phys_cursor_width)))
26454 erase_phys_cursor (w);
26455
26456 /* Don't check phys_cursor_on_p here because that flag is only set
26457 to zero in some cases where we know that the cursor has been
26458 completely erased, to avoid the extra work of erasing the cursor
26459 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26460 still not be visible, or it has only been partly erased. */
26461 if (on)
26462 {
26463 w->phys_cursor_ascent = glyph_row->ascent;
26464 w->phys_cursor_height = glyph_row->height;
26465
26466 /* Set phys_cursor_.* before x_draw_.* is called because some
26467 of them may need the information. */
26468 w->phys_cursor.x = x;
26469 w->phys_cursor.y = glyph_row->y;
26470 w->phys_cursor.hpos = hpos;
26471 w->phys_cursor.vpos = vpos;
26472 }
26473
26474 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26475 new_cursor_type, new_cursor_width,
26476 on, active_cursor);
26477 }
26478
26479
26480 /* Switch the display of W's cursor on or off, according to the value
26481 of ON. */
26482
26483 static void
26484 update_window_cursor (struct window *w, bool on)
26485 {
26486 /* Don't update cursor in windows whose frame is in the process
26487 of being deleted. */
26488 if (w->current_matrix)
26489 {
26490 int hpos = w->phys_cursor.hpos;
26491 int vpos = w->phys_cursor.vpos;
26492 struct glyph_row *row;
26493
26494 if (vpos >= w->current_matrix->nrows
26495 || hpos >= w->current_matrix->matrix_w)
26496 return;
26497
26498 row = MATRIX_ROW (w->current_matrix, vpos);
26499
26500 /* When the window is hscrolled, cursor hpos can legitimately be
26501 out of bounds, but we draw the cursor at the corresponding
26502 window margin in that case. */
26503 if (!row->reversed_p && hpos < 0)
26504 hpos = 0;
26505 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26506 hpos = row->used[TEXT_AREA] - 1;
26507
26508 block_input ();
26509 display_and_set_cursor (w, on, hpos, vpos,
26510 w->phys_cursor.x, w->phys_cursor.y);
26511 unblock_input ();
26512 }
26513 }
26514
26515
26516 /* Call update_window_cursor with parameter ON_P on all leaf windows
26517 in the window tree rooted at W. */
26518
26519 static void
26520 update_cursor_in_window_tree (struct window *w, bool on_p)
26521 {
26522 while (w)
26523 {
26524 if (WINDOWP (w->contents))
26525 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26526 else
26527 update_window_cursor (w, on_p);
26528
26529 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26530 }
26531 }
26532
26533
26534 /* EXPORT:
26535 Display the cursor on window W, or clear it, according to ON_P.
26536 Don't change the cursor's position. */
26537
26538 void
26539 x_update_cursor (struct frame *f, bool on_p)
26540 {
26541 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26542 }
26543
26544
26545 /* EXPORT:
26546 Clear the cursor of window W to background color, and mark the
26547 cursor as not shown. This is used when the text where the cursor
26548 is about to be rewritten. */
26549
26550 void
26551 x_clear_cursor (struct window *w)
26552 {
26553 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26554 update_window_cursor (w, 0);
26555 }
26556
26557 #endif /* HAVE_WINDOW_SYSTEM */
26558
26559 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26560 and MSDOS. */
26561 static void
26562 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26563 int start_hpos, int end_hpos,
26564 enum draw_glyphs_face draw)
26565 {
26566 #ifdef HAVE_WINDOW_SYSTEM
26567 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26568 {
26569 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26570 return;
26571 }
26572 #endif
26573 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26574 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26575 #endif
26576 }
26577
26578 /* Display the active region described by mouse_face_* according to DRAW. */
26579
26580 static void
26581 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26582 {
26583 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26584 struct frame *f = XFRAME (WINDOW_FRAME (w));
26585
26586 if (/* If window is in the process of being destroyed, don't bother
26587 to do anything. */
26588 w->current_matrix != NULL
26589 /* Don't update mouse highlight if hidden */
26590 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26591 /* Recognize when we are called to operate on rows that don't exist
26592 anymore. This can happen when a window is split. */
26593 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26594 {
26595 int phys_cursor_on_p = w->phys_cursor_on_p;
26596 struct glyph_row *row, *first, *last;
26597
26598 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26599 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26600
26601 for (row = first; row <= last && row->enabled_p; ++row)
26602 {
26603 int start_hpos, end_hpos, start_x;
26604
26605 /* For all but the first row, the highlight starts at column 0. */
26606 if (row == first)
26607 {
26608 /* R2L rows have BEG and END in reversed order, but the
26609 screen drawing geometry is always left to right. So
26610 we need to mirror the beginning and end of the
26611 highlighted area in R2L rows. */
26612 if (!row->reversed_p)
26613 {
26614 start_hpos = hlinfo->mouse_face_beg_col;
26615 start_x = hlinfo->mouse_face_beg_x;
26616 }
26617 else if (row == last)
26618 {
26619 start_hpos = hlinfo->mouse_face_end_col;
26620 start_x = hlinfo->mouse_face_end_x;
26621 }
26622 else
26623 {
26624 start_hpos = 0;
26625 start_x = 0;
26626 }
26627 }
26628 else if (row->reversed_p && row == last)
26629 {
26630 start_hpos = hlinfo->mouse_face_end_col;
26631 start_x = hlinfo->mouse_face_end_x;
26632 }
26633 else
26634 {
26635 start_hpos = 0;
26636 start_x = 0;
26637 }
26638
26639 if (row == last)
26640 {
26641 if (!row->reversed_p)
26642 end_hpos = hlinfo->mouse_face_end_col;
26643 else if (row == first)
26644 end_hpos = hlinfo->mouse_face_beg_col;
26645 else
26646 {
26647 end_hpos = row->used[TEXT_AREA];
26648 if (draw == DRAW_NORMAL_TEXT)
26649 row->fill_line_p = 1; /* Clear to end of line */
26650 }
26651 }
26652 else if (row->reversed_p && row == first)
26653 end_hpos = hlinfo->mouse_face_beg_col;
26654 else
26655 {
26656 end_hpos = row->used[TEXT_AREA];
26657 if (draw == DRAW_NORMAL_TEXT)
26658 row->fill_line_p = 1; /* Clear to end of line */
26659 }
26660
26661 if (end_hpos > start_hpos)
26662 {
26663 draw_row_with_mouse_face (w, start_x, row,
26664 start_hpos, end_hpos, draw);
26665
26666 row->mouse_face_p
26667 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26668 }
26669 }
26670
26671 #ifdef HAVE_WINDOW_SYSTEM
26672 /* When we've written over the cursor, arrange for it to
26673 be displayed again. */
26674 if (FRAME_WINDOW_P (f)
26675 && phys_cursor_on_p && !w->phys_cursor_on_p)
26676 {
26677 int hpos = w->phys_cursor.hpos;
26678
26679 /* When the window is hscrolled, cursor hpos can legitimately be
26680 out of bounds, but we draw the cursor at the corresponding
26681 window margin in that case. */
26682 if (!row->reversed_p && hpos < 0)
26683 hpos = 0;
26684 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26685 hpos = row->used[TEXT_AREA] - 1;
26686
26687 block_input ();
26688 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26689 w->phys_cursor.x, w->phys_cursor.y);
26690 unblock_input ();
26691 }
26692 #endif /* HAVE_WINDOW_SYSTEM */
26693 }
26694
26695 #ifdef HAVE_WINDOW_SYSTEM
26696 /* Change the mouse cursor. */
26697 if (FRAME_WINDOW_P (f))
26698 {
26699 if (draw == DRAW_NORMAL_TEXT
26700 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26701 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26702 else if (draw == DRAW_MOUSE_FACE)
26703 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26704 else
26705 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26706 }
26707 #endif /* HAVE_WINDOW_SYSTEM */
26708 }
26709
26710 /* EXPORT:
26711 Clear out the mouse-highlighted active region.
26712 Redraw it un-highlighted first. Value is non-zero if mouse
26713 face was actually drawn unhighlighted. */
26714
26715 int
26716 clear_mouse_face (Mouse_HLInfo *hlinfo)
26717 {
26718 int cleared = 0;
26719
26720 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26721 {
26722 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26723 cleared = 1;
26724 }
26725
26726 reset_mouse_highlight (hlinfo);
26727 return cleared;
26728 }
26729
26730 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26731 within the mouse face on that window. */
26732 static int
26733 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26734 {
26735 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26736
26737 /* Quickly resolve the easy cases. */
26738 if (!(WINDOWP (hlinfo->mouse_face_window)
26739 && XWINDOW (hlinfo->mouse_face_window) == w))
26740 return 0;
26741 if (vpos < hlinfo->mouse_face_beg_row
26742 || vpos > hlinfo->mouse_face_end_row)
26743 return 0;
26744 if (vpos > hlinfo->mouse_face_beg_row
26745 && vpos < hlinfo->mouse_face_end_row)
26746 return 1;
26747
26748 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26749 {
26750 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26751 {
26752 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26753 return 1;
26754 }
26755 else if ((vpos == hlinfo->mouse_face_beg_row
26756 && hpos >= hlinfo->mouse_face_beg_col)
26757 || (vpos == hlinfo->mouse_face_end_row
26758 && hpos < hlinfo->mouse_face_end_col))
26759 return 1;
26760 }
26761 else
26762 {
26763 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26764 {
26765 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26766 return 1;
26767 }
26768 else if ((vpos == hlinfo->mouse_face_beg_row
26769 && hpos <= hlinfo->mouse_face_beg_col)
26770 || (vpos == hlinfo->mouse_face_end_row
26771 && hpos > hlinfo->mouse_face_end_col))
26772 return 1;
26773 }
26774 return 0;
26775 }
26776
26777
26778 /* EXPORT:
26779 Non-zero if physical cursor of window W is within mouse face. */
26780
26781 int
26782 cursor_in_mouse_face_p (struct window *w)
26783 {
26784 int hpos = w->phys_cursor.hpos;
26785 int vpos = w->phys_cursor.vpos;
26786 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26787
26788 /* When the window is hscrolled, cursor hpos can legitimately be out
26789 of bounds, but we draw the cursor at the corresponding window
26790 margin in that case. */
26791 if (!row->reversed_p && hpos < 0)
26792 hpos = 0;
26793 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26794 hpos = row->used[TEXT_AREA] - 1;
26795
26796 return coords_in_mouse_face_p (w, hpos, vpos);
26797 }
26798
26799
26800 \f
26801 /* Find the glyph rows START_ROW and END_ROW of window W that display
26802 characters between buffer positions START_CHARPOS and END_CHARPOS
26803 (excluding END_CHARPOS). DISP_STRING is a display string that
26804 covers these buffer positions. This is similar to
26805 row_containing_pos, but is more accurate when bidi reordering makes
26806 buffer positions change non-linearly with glyph rows. */
26807 static void
26808 rows_from_pos_range (struct window *w,
26809 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26810 Lisp_Object disp_string,
26811 struct glyph_row **start, struct glyph_row **end)
26812 {
26813 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26814 int last_y = window_text_bottom_y (w);
26815 struct glyph_row *row;
26816
26817 *start = NULL;
26818 *end = NULL;
26819
26820 while (!first->enabled_p
26821 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26822 first++;
26823
26824 /* Find the START row. */
26825 for (row = first;
26826 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26827 row++)
26828 {
26829 /* A row can potentially be the START row if the range of the
26830 characters it displays intersects the range
26831 [START_CHARPOS..END_CHARPOS). */
26832 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26833 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26834 /* See the commentary in row_containing_pos, for the
26835 explanation of the complicated way to check whether
26836 some position is beyond the end of the characters
26837 displayed by a row. */
26838 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26839 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26840 && !row->ends_at_zv_p
26841 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26842 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26843 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26844 && !row->ends_at_zv_p
26845 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26846 {
26847 /* Found a candidate row. Now make sure at least one of the
26848 glyphs it displays has a charpos from the range
26849 [START_CHARPOS..END_CHARPOS).
26850
26851 This is not obvious because bidi reordering could make
26852 buffer positions of a row be 1,2,3,102,101,100, and if we
26853 want to highlight characters in [50..60), we don't want
26854 this row, even though [50..60) does intersect [1..103),
26855 the range of character positions given by the row's start
26856 and end positions. */
26857 struct glyph *g = row->glyphs[TEXT_AREA];
26858 struct glyph *e = g + row->used[TEXT_AREA];
26859
26860 while (g < e)
26861 {
26862 if (((BUFFERP (g->object) || INTEGERP (g->object))
26863 && start_charpos <= g->charpos && g->charpos < end_charpos)
26864 /* A glyph that comes from DISP_STRING is by
26865 definition to be highlighted. */
26866 || EQ (g->object, disp_string))
26867 *start = row;
26868 g++;
26869 }
26870 if (*start)
26871 break;
26872 }
26873 }
26874
26875 /* Find the END row. */
26876 if (!*start
26877 /* If the last row is partially visible, start looking for END
26878 from that row, instead of starting from FIRST. */
26879 && !(row->enabled_p
26880 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26881 row = first;
26882 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26883 {
26884 struct glyph_row *next = row + 1;
26885 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26886
26887 if (!next->enabled_p
26888 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26889 /* The first row >= START whose range of displayed characters
26890 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26891 is the row END + 1. */
26892 || (start_charpos < next_start
26893 && end_charpos < next_start)
26894 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26895 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26896 && !next->ends_at_zv_p
26897 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26898 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26899 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26900 && !next->ends_at_zv_p
26901 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26902 {
26903 *end = row;
26904 break;
26905 }
26906 else
26907 {
26908 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26909 but none of the characters it displays are in the range, it is
26910 also END + 1. */
26911 struct glyph *g = next->glyphs[TEXT_AREA];
26912 struct glyph *s = g;
26913 struct glyph *e = g + next->used[TEXT_AREA];
26914
26915 while (g < e)
26916 {
26917 if (((BUFFERP (g->object) || INTEGERP (g->object))
26918 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26919 /* If the buffer position of the first glyph in
26920 the row is equal to END_CHARPOS, it means
26921 the last character to be highlighted is the
26922 newline of ROW, and we must consider NEXT as
26923 END, not END+1. */
26924 || (((!next->reversed_p && g == s)
26925 || (next->reversed_p && g == e - 1))
26926 && (g->charpos == end_charpos
26927 /* Special case for when NEXT is an
26928 empty line at ZV. */
26929 || (g->charpos == -1
26930 && !row->ends_at_zv_p
26931 && next_start == end_charpos)))))
26932 /* A glyph that comes from DISP_STRING is by
26933 definition to be highlighted. */
26934 || EQ (g->object, disp_string))
26935 break;
26936 g++;
26937 }
26938 if (g == e)
26939 {
26940 *end = row;
26941 break;
26942 }
26943 /* The first row that ends at ZV must be the last to be
26944 highlighted. */
26945 else if (next->ends_at_zv_p)
26946 {
26947 *end = next;
26948 break;
26949 }
26950 }
26951 }
26952 }
26953
26954 /* This function sets the mouse_face_* elements of HLINFO, assuming
26955 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26956 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26957 for the overlay or run of text properties specifying the mouse
26958 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26959 before-string and after-string that must also be highlighted.
26960 DISP_STRING, if non-nil, is a display string that may cover some
26961 or all of the highlighted text. */
26962
26963 static void
26964 mouse_face_from_buffer_pos (Lisp_Object window,
26965 Mouse_HLInfo *hlinfo,
26966 ptrdiff_t mouse_charpos,
26967 ptrdiff_t start_charpos,
26968 ptrdiff_t end_charpos,
26969 Lisp_Object before_string,
26970 Lisp_Object after_string,
26971 Lisp_Object disp_string)
26972 {
26973 struct window *w = XWINDOW (window);
26974 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26975 struct glyph_row *r1, *r2;
26976 struct glyph *glyph, *end;
26977 ptrdiff_t ignore, pos;
26978 int x;
26979
26980 eassert (NILP (disp_string) || STRINGP (disp_string));
26981 eassert (NILP (before_string) || STRINGP (before_string));
26982 eassert (NILP (after_string) || STRINGP (after_string));
26983
26984 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26985 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26986 if (r1 == NULL)
26987 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
26988 /* If the before-string or display-string contains newlines,
26989 rows_from_pos_range skips to its last row. Move back. */
26990 if (!NILP (before_string) || !NILP (disp_string))
26991 {
26992 struct glyph_row *prev;
26993 while ((prev = r1 - 1, prev >= first)
26994 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26995 && prev->used[TEXT_AREA] > 0)
26996 {
26997 struct glyph *beg = prev->glyphs[TEXT_AREA];
26998 glyph = beg + prev->used[TEXT_AREA];
26999 while (--glyph >= beg && INTEGERP (glyph->object));
27000 if (glyph < beg
27001 || !(EQ (glyph->object, before_string)
27002 || EQ (glyph->object, disp_string)))
27003 break;
27004 r1 = prev;
27005 }
27006 }
27007 if (r2 == NULL)
27008 {
27009 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27010 hlinfo->mouse_face_past_end = 1;
27011 }
27012 else if (!NILP (after_string))
27013 {
27014 /* If the after-string has newlines, advance to its last row. */
27015 struct glyph_row *next;
27016 struct glyph_row *last
27017 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27018
27019 for (next = r2 + 1;
27020 next <= last
27021 && next->used[TEXT_AREA] > 0
27022 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27023 ++next)
27024 r2 = next;
27025 }
27026 /* The rest of the display engine assumes that mouse_face_beg_row is
27027 either above mouse_face_end_row or identical to it. But with
27028 bidi-reordered continued lines, the row for START_CHARPOS could
27029 be below the row for END_CHARPOS. If so, swap the rows and store
27030 them in correct order. */
27031 if (r1->y > r2->y)
27032 {
27033 struct glyph_row *tem = r2;
27034
27035 r2 = r1;
27036 r1 = tem;
27037 }
27038
27039 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27040 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27041
27042 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27043 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27044 could be anywhere in the row and in any order. The strategy
27045 below is to find the leftmost and the rightmost glyph that
27046 belongs to either of these 3 strings, or whose position is
27047 between START_CHARPOS and END_CHARPOS, and highlight all the
27048 glyphs between those two. This may cover more than just the text
27049 between START_CHARPOS and END_CHARPOS if the range of characters
27050 strides the bidi level boundary, e.g. if the beginning is in R2L
27051 text while the end is in L2R text or vice versa. */
27052 if (!r1->reversed_p)
27053 {
27054 /* This row is in a left to right paragraph. Scan it left to
27055 right. */
27056 glyph = r1->glyphs[TEXT_AREA];
27057 end = glyph + r1->used[TEXT_AREA];
27058 x = r1->x;
27059
27060 /* Skip truncation glyphs at the start of the glyph row. */
27061 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27062 for (; glyph < end
27063 && INTEGERP (glyph->object)
27064 && glyph->charpos < 0;
27065 ++glyph)
27066 x += glyph->pixel_width;
27067
27068 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27069 or DISP_STRING, and the first glyph from buffer whose
27070 position is between START_CHARPOS and END_CHARPOS. */
27071 for (; glyph < end
27072 && !INTEGERP (glyph->object)
27073 && !EQ (glyph->object, disp_string)
27074 && !(BUFFERP (glyph->object)
27075 && (glyph->charpos >= start_charpos
27076 && glyph->charpos < end_charpos));
27077 ++glyph)
27078 {
27079 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27080 are present at buffer positions between START_CHARPOS and
27081 END_CHARPOS, or if they come from an overlay. */
27082 if (EQ (glyph->object, before_string))
27083 {
27084 pos = string_buffer_position (before_string,
27085 start_charpos);
27086 /* If pos == 0, it means before_string came from an
27087 overlay, not from a buffer position. */
27088 if (!pos || (pos >= start_charpos && pos < end_charpos))
27089 break;
27090 }
27091 else if (EQ (glyph->object, after_string))
27092 {
27093 pos = string_buffer_position (after_string, end_charpos);
27094 if (!pos || (pos >= start_charpos && pos < end_charpos))
27095 break;
27096 }
27097 x += glyph->pixel_width;
27098 }
27099 hlinfo->mouse_face_beg_x = x;
27100 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27101 }
27102 else
27103 {
27104 /* This row is in a right to left paragraph. Scan it right to
27105 left. */
27106 struct glyph *g;
27107
27108 end = r1->glyphs[TEXT_AREA] - 1;
27109 glyph = end + r1->used[TEXT_AREA];
27110
27111 /* Skip truncation glyphs at the start of the glyph row. */
27112 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27113 for (; glyph > end
27114 && INTEGERP (glyph->object)
27115 && glyph->charpos < 0;
27116 --glyph)
27117 ;
27118
27119 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27120 or DISP_STRING, and the first glyph from buffer whose
27121 position is between START_CHARPOS and END_CHARPOS. */
27122 for (; glyph > end
27123 && !INTEGERP (glyph->object)
27124 && !EQ (glyph->object, disp_string)
27125 && !(BUFFERP (glyph->object)
27126 && (glyph->charpos >= start_charpos
27127 && glyph->charpos < end_charpos));
27128 --glyph)
27129 {
27130 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27131 are present at buffer positions between START_CHARPOS and
27132 END_CHARPOS, or if they come from an overlay. */
27133 if (EQ (glyph->object, before_string))
27134 {
27135 pos = string_buffer_position (before_string, start_charpos);
27136 /* If pos == 0, it means before_string came from an
27137 overlay, not from a buffer position. */
27138 if (!pos || (pos >= start_charpos && pos < end_charpos))
27139 break;
27140 }
27141 else if (EQ (glyph->object, after_string))
27142 {
27143 pos = string_buffer_position (after_string, end_charpos);
27144 if (!pos || (pos >= start_charpos && pos < end_charpos))
27145 break;
27146 }
27147 }
27148
27149 glyph++; /* first glyph to the right of the highlighted area */
27150 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27151 x += g->pixel_width;
27152 hlinfo->mouse_face_beg_x = x;
27153 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27154 }
27155
27156 /* If the highlight ends in a different row, compute GLYPH and END
27157 for the end row. Otherwise, reuse the values computed above for
27158 the row where the highlight begins. */
27159 if (r2 != r1)
27160 {
27161 if (!r2->reversed_p)
27162 {
27163 glyph = r2->glyphs[TEXT_AREA];
27164 end = glyph + r2->used[TEXT_AREA];
27165 x = r2->x;
27166 }
27167 else
27168 {
27169 end = r2->glyphs[TEXT_AREA] - 1;
27170 glyph = end + r2->used[TEXT_AREA];
27171 }
27172 }
27173
27174 if (!r2->reversed_p)
27175 {
27176 /* Skip truncation and continuation glyphs near the end of the
27177 row, and also blanks and stretch glyphs inserted by
27178 extend_face_to_end_of_line. */
27179 while (end > glyph
27180 && INTEGERP ((end - 1)->object))
27181 --end;
27182 /* Scan the rest of the glyph row from the end, looking for the
27183 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27184 DISP_STRING, or whose position is between START_CHARPOS
27185 and END_CHARPOS */
27186 for (--end;
27187 end > glyph
27188 && !INTEGERP (end->object)
27189 && !EQ (end->object, disp_string)
27190 && !(BUFFERP (end->object)
27191 && (end->charpos >= start_charpos
27192 && end->charpos < end_charpos));
27193 --end)
27194 {
27195 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27196 are present at buffer positions between START_CHARPOS and
27197 END_CHARPOS, or if they come from an overlay. */
27198 if (EQ (end->object, before_string))
27199 {
27200 pos = string_buffer_position (before_string, start_charpos);
27201 if (!pos || (pos >= start_charpos && pos < end_charpos))
27202 break;
27203 }
27204 else if (EQ (end->object, after_string))
27205 {
27206 pos = string_buffer_position (after_string, end_charpos);
27207 if (!pos || (pos >= start_charpos && pos < end_charpos))
27208 break;
27209 }
27210 }
27211 /* Find the X coordinate of the last glyph to be highlighted. */
27212 for (; glyph <= end; ++glyph)
27213 x += glyph->pixel_width;
27214
27215 hlinfo->mouse_face_end_x = x;
27216 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27217 }
27218 else
27219 {
27220 /* Skip truncation and continuation glyphs near the end of the
27221 row, and also blanks and stretch glyphs inserted by
27222 extend_face_to_end_of_line. */
27223 x = r2->x;
27224 end++;
27225 while (end < glyph
27226 && INTEGERP (end->object))
27227 {
27228 x += end->pixel_width;
27229 ++end;
27230 }
27231 /* Scan the rest of the glyph row from the end, looking for the
27232 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27233 DISP_STRING, or whose position is between START_CHARPOS
27234 and END_CHARPOS */
27235 for ( ;
27236 end < glyph
27237 && !INTEGERP (end->object)
27238 && !EQ (end->object, disp_string)
27239 && !(BUFFERP (end->object)
27240 && (end->charpos >= start_charpos
27241 && end->charpos < end_charpos));
27242 ++end)
27243 {
27244 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27245 are present at buffer positions between START_CHARPOS and
27246 END_CHARPOS, or if they come from an overlay. */
27247 if (EQ (end->object, before_string))
27248 {
27249 pos = string_buffer_position (before_string, start_charpos);
27250 if (!pos || (pos >= start_charpos && pos < end_charpos))
27251 break;
27252 }
27253 else if (EQ (end->object, after_string))
27254 {
27255 pos = string_buffer_position (after_string, end_charpos);
27256 if (!pos || (pos >= start_charpos && pos < end_charpos))
27257 break;
27258 }
27259 x += end->pixel_width;
27260 }
27261 /* If we exited the above loop because we arrived at the last
27262 glyph of the row, and its buffer position is still not in
27263 range, it means the last character in range is the preceding
27264 newline. Bump the end column and x values to get past the
27265 last glyph. */
27266 if (end == glyph
27267 && BUFFERP (end->object)
27268 && (end->charpos < start_charpos
27269 || end->charpos >= end_charpos))
27270 {
27271 x += end->pixel_width;
27272 ++end;
27273 }
27274 hlinfo->mouse_face_end_x = x;
27275 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27276 }
27277
27278 hlinfo->mouse_face_window = window;
27279 hlinfo->mouse_face_face_id
27280 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27281 mouse_charpos + 1,
27282 !hlinfo->mouse_face_hidden, -1);
27283 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27284 }
27285
27286 /* The following function is not used anymore (replaced with
27287 mouse_face_from_string_pos), but I leave it here for the time
27288 being, in case someone would. */
27289
27290 #if 0 /* not used */
27291
27292 /* Find the position of the glyph for position POS in OBJECT in
27293 window W's current matrix, and return in *X, *Y the pixel
27294 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27295
27296 RIGHT_P non-zero means return the position of the right edge of the
27297 glyph, RIGHT_P zero means return the left edge position.
27298
27299 If no glyph for POS exists in the matrix, return the position of
27300 the glyph with the next smaller position that is in the matrix, if
27301 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27302 exists in the matrix, return the position of the glyph with the
27303 next larger position in OBJECT.
27304
27305 Value is non-zero if a glyph was found. */
27306
27307 static int
27308 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27309 int *hpos, int *vpos, int *x, int *y, int right_p)
27310 {
27311 int yb = window_text_bottom_y (w);
27312 struct glyph_row *r;
27313 struct glyph *best_glyph = NULL;
27314 struct glyph_row *best_row = NULL;
27315 int best_x = 0;
27316
27317 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27318 r->enabled_p && r->y < yb;
27319 ++r)
27320 {
27321 struct glyph *g = r->glyphs[TEXT_AREA];
27322 struct glyph *e = g + r->used[TEXT_AREA];
27323 int gx;
27324
27325 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27326 if (EQ (g->object, object))
27327 {
27328 if (g->charpos == pos)
27329 {
27330 best_glyph = g;
27331 best_x = gx;
27332 best_row = r;
27333 goto found;
27334 }
27335 else if (best_glyph == NULL
27336 || ((eabs (g->charpos - pos)
27337 < eabs (best_glyph->charpos - pos))
27338 && (right_p
27339 ? g->charpos < pos
27340 : g->charpos > pos)))
27341 {
27342 best_glyph = g;
27343 best_x = gx;
27344 best_row = r;
27345 }
27346 }
27347 }
27348
27349 found:
27350
27351 if (best_glyph)
27352 {
27353 *x = best_x;
27354 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27355
27356 if (right_p)
27357 {
27358 *x += best_glyph->pixel_width;
27359 ++*hpos;
27360 }
27361
27362 *y = best_row->y;
27363 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27364 }
27365
27366 return best_glyph != NULL;
27367 }
27368 #endif /* not used */
27369
27370 /* Find the positions of the first and the last glyphs in window W's
27371 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27372 (assumed to be a string), and return in HLINFO's mouse_face_*
27373 members the pixel and column/row coordinates of those glyphs. */
27374
27375 static void
27376 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27377 Lisp_Object object,
27378 ptrdiff_t startpos, ptrdiff_t endpos)
27379 {
27380 int yb = window_text_bottom_y (w);
27381 struct glyph_row *r;
27382 struct glyph *g, *e;
27383 int gx;
27384 int found = 0;
27385
27386 /* Find the glyph row with at least one position in the range
27387 [STARTPOS..ENDPOS], and the first glyph in that row whose
27388 position belongs to that range. */
27389 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27390 r->enabled_p && r->y < yb;
27391 ++r)
27392 {
27393 if (!r->reversed_p)
27394 {
27395 g = r->glyphs[TEXT_AREA];
27396 e = g + r->used[TEXT_AREA];
27397 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27398 if (EQ (g->object, object)
27399 && startpos <= g->charpos && g->charpos <= endpos)
27400 {
27401 hlinfo->mouse_face_beg_row
27402 = MATRIX_ROW_VPOS (r, w->current_matrix);
27403 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27404 hlinfo->mouse_face_beg_x = gx;
27405 found = 1;
27406 break;
27407 }
27408 }
27409 else
27410 {
27411 struct glyph *g1;
27412
27413 e = r->glyphs[TEXT_AREA];
27414 g = e + r->used[TEXT_AREA];
27415 for ( ; g > e; --g)
27416 if (EQ ((g-1)->object, object)
27417 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27418 {
27419 hlinfo->mouse_face_beg_row
27420 = MATRIX_ROW_VPOS (r, w->current_matrix);
27421 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27422 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27423 gx += g1->pixel_width;
27424 hlinfo->mouse_face_beg_x = gx;
27425 found = 1;
27426 break;
27427 }
27428 }
27429 if (found)
27430 break;
27431 }
27432
27433 if (!found)
27434 return;
27435
27436 /* Starting with the next row, look for the first row which does NOT
27437 include any glyphs whose positions are in the range. */
27438 for (++r; r->enabled_p && r->y < yb; ++r)
27439 {
27440 g = r->glyphs[TEXT_AREA];
27441 e = g + r->used[TEXT_AREA];
27442 found = 0;
27443 for ( ; g < e; ++g)
27444 if (EQ (g->object, object)
27445 && startpos <= g->charpos && g->charpos <= endpos)
27446 {
27447 found = 1;
27448 break;
27449 }
27450 if (!found)
27451 break;
27452 }
27453
27454 /* The highlighted region ends on the previous row. */
27455 r--;
27456
27457 /* Set the end row. */
27458 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27459
27460 /* Compute and set the end column and the end column's horizontal
27461 pixel coordinate. */
27462 if (!r->reversed_p)
27463 {
27464 g = r->glyphs[TEXT_AREA];
27465 e = g + r->used[TEXT_AREA];
27466 for ( ; e > g; --e)
27467 if (EQ ((e-1)->object, object)
27468 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27469 break;
27470 hlinfo->mouse_face_end_col = e - g;
27471
27472 for (gx = r->x; g < e; ++g)
27473 gx += g->pixel_width;
27474 hlinfo->mouse_face_end_x = gx;
27475 }
27476 else
27477 {
27478 e = r->glyphs[TEXT_AREA];
27479 g = e + r->used[TEXT_AREA];
27480 for (gx = r->x ; e < g; ++e)
27481 {
27482 if (EQ (e->object, object)
27483 && startpos <= e->charpos && e->charpos <= endpos)
27484 break;
27485 gx += e->pixel_width;
27486 }
27487 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27488 hlinfo->mouse_face_end_x = gx;
27489 }
27490 }
27491
27492 #ifdef HAVE_WINDOW_SYSTEM
27493
27494 /* See if position X, Y is within a hot-spot of an image. */
27495
27496 static int
27497 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27498 {
27499 if (!CONSP (hot_spot))
27500 return 0;
27501
27502 if (EQ (XCAR (hot_spot), Qrect))
27503 {
27504 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27505 Lisp_Object rect = XCDR (hot_spot);
27506 Lisp_Object tem;
27507 if (!CONSP (rect))
27508 return 0;
27509 if (!CONSP (XCAR (rect)))
27510 return 0;
27511 if (!CONSP (XCDR (rect)))
27512 return 0;
27513 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27514 return 0;
27515 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27516 return 0;
27517 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27518 return 0;
27519 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27520 return 0;
27521 return 1;
27522 }
27523 else if (EQ (XCAR (hot_spot), Qcircle))
27524 {
27525 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27526 Lisp_Object circ = XCDR (hot_spot);
27527 Lisp_Object lr, lx0, ly0;
27528 if (CONSP (circ)
27529 && CONSP (XCAR (circ))
27530 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27531 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27532 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27533 {
27534 double r = XFLOATINT (lr);
27535 double dx = XINT (lx0) - x;
27536 double dy = XINT (ly0) - y;
27537 return (dx * dx + dy * dy <= r * r);
27538 }
27539 }
27540 else if (EQ (XCAR (hot_spot), Qpoly))
27541 {
27542 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27543 if (VECTORP (XCDR (hot_spot)))
27544 {
27545 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27546 Lisp_Object *poly = v->contents;
27547 ptrdiff_t n = v->header.size;
27548 ptrdiff_t i;
27549 int inside = 0;
27550 Lisp_Object lx, ly;
27551 int x0, y0;
27552
27553 /* Need an even number of coordinates, and at least 3 edges. */
27554 if (n < 6 || n & 1)
27555 return 0;
27556
27557 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27558 If count is odd, we are inside polygon. Pixels on edges
27559 may or may not be included depending on actual geometry of the
27560 polygon. */
27561 if ((lx = poly[n-2], !INTEGERP (lx))
27562 || (ly = poly[n-1], !INTEGERP (lx)))
27563 return 0;
27564 x0 = XINT (lx), y0 = XINT (ly);
27565 for (i = 0; i < n; i += 2)
27566 {
27567 int x1 = x0, y1 = y0;
27568 if ((lx = poly[i], !INTEGERP (lx))
27569 || (ly = poly[i+1], !INTEGERP (ly)))
27570 return 0;
27571 x0 = XINT (lx), y0 = XINT (ly);
27572
27573 /* Does this segment cross the X line? */
27574 if (x0 >= x)
27575 {
27576 if (x1 >= x)
27577 continue;
27578 }
27579 else if (x1 < x)
27580 continue;
27581 if (y > y0 && y > y1)
27582 continue;
27583 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27584 inside = !inside;
27585 }
27586 return inside;
27587 }
27588 }
27589 return 0;
27590 }
27591
27592 Lisp_Object
27593 find_hot_spot (Lisp_Object map, int x, int y)
27594 {
27595 while (CONSP (map))
27596 {
27597 if (CONSP (XCAR (map))
27598 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27599 return XCAR (map);
27600 map = XCDR (map);
27601 }
27602
27603 return Qnil;
27604 }
27605
27606 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27607 3, 3, 0,
27608 doc: /* Lookup in image map MAP coordinates X and Y.
27609 An image map is an alist where each element has the format (AREA ID PLIST).
27610 An AREA is specified as either a rectangle, a circle, or a polygon:
27611 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27612 pixel coordinates of the upper left and bottom right corners.
27613 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27614 and the radius of the circle; r may be a float or integer.
27615 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27616 vector describes one corner in the polygon.
27617 Returns the alist element for the first matching AREA in MAP. */)
27618 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27619 {
27620 if (NILP (map))
27621 return Qnil;
27622
27623 CHECK_NUMBER (x);
27624 CHECK_NUMBER (y);
27625
27626 return find_hot_spot (map,
27627 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27628 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27629 }
27630
27631
27632 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27633 static void
27634 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27635 {
27636 /* Do not change cursor shape while dragging mouse. */
27637 if (!NILP (do_mouse_tracking))
27638 return;
27639
27640 if (!NILP (pointer))
27641 {
27642 if (EQ (pointer, Qarrow))
27643 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27644 else if (EQ (pointer, Qhand))
27645 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27646 else if (EQ (pointer, Qtext))
27647 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27648 else if (EQ (pointer, intern ("hdrag")))
27649 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27650 #ifdef HAVE_X_WINDOWS
27651 else if (EQ (pointer, intern ("vdrag")))
27652 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27653 #endif
27654 else if (EQ (pointer, intern ("hourglass")))
27655 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27656 else if (EQ (pointer, Qmodeline))
27657 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27658 else
27659 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27660 }
27661
27662 if (cursor != No_Cursor)
27663 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27664 }
27665
27666 #endif /* HAVE_WINDOW_SYSTEM */
27667
27668 /* Take proper action when mouse has moved to the mode or header line
27669 or marginal area AREA of window W, x-position X and y-position Y.
27670 X is relative to the start of the text display area of W, so the
27671 width of bitmap areas and scroll bars must be subtracted to get a
27672 position relative to the start of the mode line. */
27673
27674 static void
27675 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27676 enum window_part area)
27677 {
27678 struct window *w = XWINDOW (window);
27679 struct frame *f = XFRAME (w->frame);
27680 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27681 #ifdef HAVE_WINDOW_SYSTEM
27682 Display_Info *dpyinfo;
27683 #endif
27684 Cursor cursor = No_Cursor;
27685 Lisp_Object pointer = Qnil;
27686 int dx, dy, width, height;
27687 ptrdiff_t charpos;
27688 Lisp_Object string, object = Qnil;
27689 Lisp_Object pos IF_LINT (= Qnil), help;
27690
27691 Lisp_Object mouse_face;
27692 int original_x_pixel = x;
27693 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27694 struct glyph_row *row IF_LINT (= 0);
27695
27696 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27697 {
27698 int x0;
27699 struct glyph *end;
27700
27701 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27702 returns them in row/column units! */
27703 string = mode_line_string (w, area, &x, &y, &charpos,
27704 &object, &dx, &dy, &width, &height);
27705
27706 row = (area == ON_MODE_LINE
27707 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27708 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27709
27710 /* Find the glyph under the mouse pointer. */
27711 if (row->mode_line_p && row->enabled_p)
27712 {
27713 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27714 end = glyph + row->used[TEXT_AREA];
27715
27716 for (x0 = original_x_pixel;
27717 glyph < end && x0 >= glyph->pixel_width;
27718 ++glyph)
27719 x0 -= glyph->pixel_width;
27720
27721 if (glyph >= end)
27722 glyph = NULL;
27723 }
27724 }
27725 else
27726 {
27727 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27728 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27729 returns them in row/column units! */
27730 string = marginal_area_string (w, area, &x, &y, &charpos,
27731 &object, &dx, &dy, &width, &height);
27732 }
27733
27734 help = Qnil;
27735
27736 #ifdef HAVE_WINDOW_SYSTEM
27737 if (IMAGEP (object))
27738 {
27739 Lisp_Object image_map, hotspot;
27740 if ((image_map = Fplist_get (XCDR (object), QCmap),
27741 !NILP (image_map))
27742 && (hotspot = find_hot_spot (image_map, dx, dy),
27743 CONSP (hotspot))
27744 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27745 {
27746 Lisp_Object plist;
27747
27748 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27749 If so, we could look for mouse-enter, mouse-leave
27750 properties in PLIST (and do something...). */
27751 hotspot = XCDR (hotspot);
27752 if (CONSP (hotspot)
27753 && (plist = XCAR (hotspot), CONSP (plist)))
27754 {
27755 pointer = Fplist_get (plist, Qpointer);
27756 if (NILP (pointer))
27757 pointer = Qhand;
27758 help = Fplist_get (plist, Qhelp_echo);
27759 if (!NILP (help))
27760 {
27761 help_echo_string = help;
27762 XSETWINDOW (help_echo_window, w);
27763 help_echo_object = w->contents;
27764 help_echo_pos = charpos;
27765 }
27766 }
27767 }
27768 if (NILP (pointer))
27769 pointer = Fplist_get (XCDR (object), QCpointer);
27770 }
27771 #endif /* HAVE_WINDOW_SYSTEM */
27772
27773 if (STRINGP (string))
27774 pos = make_number (charpos);
27775
27776 /* Set the help text and mouse pointer. If the mouse is on a part
27777 of the mode line without any text (e.g. past the right edge of
27778 the mode line text), use the default help text and pointer. */
27779 if (STRINGP (string) || area == ON_MODE_LINE)
27780 {
27781 /* Arrange to display the help by setting the global variables
27782 help_echo_string, help_echo_object, and help_echo_pos. */
27783 if (NILP (help))
27784 {
27785 if (STRINGP (string))
27786 help = Fget_text_property (pos, Qhelp_echo, string);
27787
27788 if (!NILP (help))
27789 {
27790 help_echo_string = help;
27791 XSETWINDOW (help_echo_window, w);
27792 help_echo_object = string;
27793 help_echo_pos = charpos;
27794 }
27795 else if (area == ON_MODE_LINE)
27796 {
27797 Lisp_Object default_help
27798 = buffer_local_value_1 (Qmode_line_default_help_echo,
27799 w->contents);
27800
27801 if (STRINGP (default_help))
27802 {
27803 help_echo_string = default_help;
27804 XSETWINDOW (help_echo_window, w);
27805 help_echo_object = Qnil;
27806 help_echo_pos = -1;
27807 }
27808 }
27809 }
27810
27811 #ifdef HAVE_WINDOW_SYSTEM
27812 /* Change the mouse pointer according to what is under it. */
27813 if (FRAME_WINDOW_P (f))
27814 {
27815 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27816 if (STRINGP (string))
27817 {
27818 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27819
27820 if (NILP (pointer))
27821 pointer = Fget_text_property (pos, Qpointer, string);
27822
27823 /* Change the mouse pointer according to what is under X/Y. */
27824 if (NILP (pointer)
27825 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27826 {
27827 Lisp_Object map;
27828 map = Fget_text_property (pos, Qlocal_map, string);
27829 if (!KEYMAPP (map))
27830 map = Fget_text_property (pos, Qkeymap, string);
27831 if (!KEYMAPP (map))
27832 cursor = dpyinfo->vertical_scroll_bar_cursor;
27833 }
27834 }
27835 else
27836 /* Default mode-line pointer. */
27837 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27838 }
27839 #endif
27840 }
27841
27842 /* Change the mouse face according to what is under X/Y. */
27843 if (STRINGP (string))
27844 {
27845 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27846 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27847 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27848 && glyph)
27849 {
27850 Lisp_Object b, e;
27851
27852 struct glyph * tmp_glyph;
27853
27854 int gpos;
27855 int gseq_length;
27856 int total_pixel_width;
27857 ptrdiff_t begpos, endpos, ignore;
27858
27859 int vpos, hpos;
27860
27861 b = Fprevious_single_property_change (make_number (charpos + 1),
27862 Qmouse_face, string, Qnil);
27863 if (NILP (b))
27864 begpos = 0;
27865 else
27866 begpos = XINT (b);
27867
27868 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27869 if (NILP (e))
27870 endpos = SCHARS (string);
27871 else
27872 endpos = XINT (e);
27873
27874 /* Calculate the glyph position GPOS of GLYPH in the
27875 displayed string, relative to the beginning of the
27876 highlighted part of the string.
27877
27878 Note: GPOS is different from CHARPOS. CHARPOS is the
27879 position of GLYPH in the internal string object. A mode
27880 line string format has structures which are converted to
27881 a flattened string by the Emacs Lisp interpreter. The
27882 internal string is an element of those structures. The
27883 displayed string is the flattened string. */
27884 tmp_glyph = row_start_glyph;
27885 while (tmp_glyph < glyph
27886 && (!(EQ (tmp_glyph->object, glyph->object)
27887 && begpos <= tmp_glyph->charpos
27888 && tmp_glyph->charpos < endpos)))
27889 tmp_glyph++;
27890 gpos = glyph - tmp_glyph;
27891
27892 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27893 the highlighted part of the displayed string to which
27894 GLYPH belongs. Note: GSEQ_LENGTH is different from
27895 SCHARS (STRING), because the latter returns the length of
27896 the internal string. */
27897 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27898 tmp_glyph > glyph
27899 && (!(EQ (tmp_glyph->object, glyph->object)
27900 && begpos <= tmp_glyph->charpos
27901 && tmp_glyph->charpos < endpos));
27902 tmp_glyph--)
27903 ;
27904 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27905
27906 /* Calculate the total pixel width of all the glyphs between
27907 the beginning of the highlighted area and GLYPH. */
27908 total_pixel_width = 0;
27909 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27910 total_pixel_width += tmp_glyph->pixel_width;
27911
27912 /* Pre calculation of re-rendering position. Note: X is in
27913 column units here, after the call to mode_line_string or
27914 marginal_area_string. */
27915 hpos = x - gpos;
27916 vpos = (area == ON_MODE_LINE
27917 ? (w->current_matrix)->nrows - 1
27918 : 0);
27919
27920 /* If GLYPH's position is included in the region that is
27921 already drawn in mouse face, we have nothing to do. */
27922 if ( EQ (window, hlinfo->mouse_face_window)
27923 && (!row->reversed_p
27924 ? (hlinfo->mouse_face_beg_col <= hpos
27925 && hpos < hlinfo->mouse_face_end_col)
27926 /* In R2L rows we swap BEG and END, see below. */
27927 : (hlinfo->mouse_face_end_col <= hpos
27928 && hpos < hlinfo->mouse_face_beg_col))
27929 && hlinfo->mouse_face_beg_row == vpos )
27930 return;
27931
27932 if (clear_mouse_face (hlinfo))
27933 cursor = No_Cursor;
27934
27935 if (!row->reversed_p)
27936 {
27937 hlinfo->mouse_face_beg_col = hpos;
27938 hlinfo->mouse_face_beg_x = original_x_pixel
27939 - (total_pixel_width + dx);
27940 hlinfo->mouse_face_end_col = hpos + gseq_length;
27941 hlinfo->mouse_face_end_x = 0;
27942 }
27943 else
27944 {
27945 /* In R2L rows, show_mouse_face expects BEG and END
27946 coordinates to be swapped. */
27947 hlinfo->mouse_face_end_col = hpos;
27948 hlinfo->mouse_face_end_x = original_x_pixel
27949 - (total_pixel_width + dx);
27950 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27951 hlinfo->mouse_face_beg_x = 0;
27952 }
27953
27954 hlinfo->mouse_face_beg_row = vpos;
27955 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27956 hlinfo->mouse_face_past_end = 0;
27957 hlinfo->mouse_face_window = window;
27958
27959 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27960 charpos,
27961 0, 0, 0,
27962 &ignore,
27963 glyph->face_id,
27964 1);
27965 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27966
27967 if (NILP (pointer))
27968 pointer = Qhand;
27969 }
27970 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27971 clear_mouse_face (hlinfo);
27972 }
27973 #ifdef HAVE_WINDOW_SYSTEM
27974 if (FRAME_WINDOW_P (f))
27975 define_frame_cursor1 (f, cursor, pointer);
27976 #endif
27977 }
27978
27979
27980 /* EXPORT:
27981 Take proper action when the mouse has moved to position X, Y on
27982 frame F with regards to highlighting portions of display that have
27983 mouse-face properties. Also de-highlight portions of display where
27984 the mouse was before, set the mouse pointer shape as appropriate
27985 for the mouse coordinates, and activate help echo (tooltips).
27986 X and Y can be negative or out of range. */
27987
27988 void
27989 note_mouse_highlight (struct frame *f, int x, int y)
27990 {
27991 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27992 enum window_part part = ON_NOTHING;
27993 Lisp_Object window;
27994 struct window *w;
27995 Cursor cursor = No_Cursor;
27996 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27997 struct buffer *b;
27998
27999 /* When a menu is active, don't highlight because this looks odd. */
28000 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28001 if (popup_activated ())
28002 return;
28003 #endif
28004
28005 if (!f->glyphs_initialized_p
28006 || f->pointer_invisible)
28007 return;
28008
28009 hlinfo->mouse_face_mouse_x = x;
28010 hlinfo->mouse_face_mouse_y = y;
28011 hlinfo->mouse_face_mouse_frame = f;
28012
28013 if (hlinfo->mouse_face_defer)
28014 return;
28015
28016 /* Which window is that in? */
28017 window = window_from_coordinates (f, x, y, &part, 1);
28018
28019 /* If displaying active text in another window, clear that. */
28020 if (! EQ (window, hlinfo->mouse_face_window)
28021 /* Also clear if we move out of text area in same window. */
28022 || (!NILP (hlinfo->mouse_face_window)
28023 && !NILP (window)
28024 && part != ON_TEXT
28025 && part != ON_MODE_LINE
28026 && part != ON_HEADER_LINE))
28027 clear_mouse_face (hlinfo);
28028
28029 /* Not on a window -> return. */
28030 if (!WINDOWP (window))
28031 return;
28032
28033 /* Reset help_echo_string. It will get recomputed below. */
28034 help_echo_string = Qnil;
28035
28036 /* Convert to window-relative pixel coordinates. */
28037 w = XWINDOW (window);
28038 frame_to_window_pixel_xy (w, &x, &y);
28039
28040 #ifdef HAVE_WINDOW_SYSTEM
28041 /* Handle tool-bar window differently since it doesn't display a
28042 buffer. */
28043 if (EQ (window, f->tool_bar_window))
28044 {
28045 note_tool_bar_highlight (f, x, y);
28046 return;
28047 }
28048 #endif
28049
28050 /* Mouse is on the mode, header line or margin? */
28051 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28052 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28053 {
28054 note_mode_line_or_margin_highlight (window, x, y, part);
28055 return;
28056 }
28057
28058 #ifdef HAVE_WINDOW_SYSTEM
28059 if (part == ON_VERTICAL_BORDER)
28060 {
28061 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28062 help_echo_string = build_string ("drag-mouse-1: resize");
28063 }
28064 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28065 || part == ON_SCROLL_BAR)
28066 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28067 else
28068 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28069 #endif
28070
28071 /* Are we in a window whose display is up to date?
28072 And verify the buffer's text has not changed. */
28073 b = XBUFFER (w->contents);
28074 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28075 {
28076 int hpos, vpos, dx, dy, area = LAST_AREA;
28077 ptrdiff_t pos;
28078 struct glyph *glyph;
28079 Lisp_Object object;
28080 Lisp_Object mouse_face = Qnil, position;
28081 Lisp_Object *overlay_vec = NULL;
28082 ptrdiff_t i, noverlays;
28083 struct buffer *obuf;
28084 ptrdiff_t obegv, ozv;
28085 int same_region;
28086
28087 /* Find the glyph under X/Y. */
28088 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28089
28090 #ifdef HAVE_WINDOW_SYSTEM
28091 /* Look for :pointer property on image. */
28092 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28093 {
28094 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28095 if (img != NULL && IMAGEP (img->spec))
28096 {
28097 Lisp_Object image_map, hotspot;
28098 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28099 !NILP (image_map))
28100 && (hotspot = find_hot_spot (image_map,
28101 glyph->slice.img.x + dx,
28102 glyph->slice.img.y + dy),
28103 CONSP (hotspot))
28104 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28105 {
28106 Lisp_Object plist;
28107
28108 /* Could check XCAR (hotspot) to see if we enter/leave
28109 this hot-spot.
28110 If so, we could look for mouse-enter, mouse-leave
28111 properties in PLIST (and do something...). */
28112 hotspot = XCDR (hotspot);
28113 if (CONSP (hotspot)
28114 && (plist = XCAR (hotspot), CONSP (plist)))
28115 {
28116 pointer = Fplist_get (plist, Qpointer);
28117 if (NILP (pointer))
28118 pointer = Qhand;
28119 help_echo_string = Fplist_get (plist, Qhelp_echo);
28120 if (!NILP (help_echo_string))
28121 {
28122 help_echo_window = window;
28123 help_echo_object = glyph->object;
28124 help_echo_pos = glyph->charpos;
28125 }
28126 }
28127 }
28128 if (NILP (pointer))
28129 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28130 }
28131 }
28132 #endif /* HAVE_WINDOW_SYSTEM */
28133
28134 /* Clear mouse face if X/Y not over text. */
28135 if (glyph == NULL
28136 || area != TEXT_AREA
28137 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28138 /* Glyph's OBJECT is an integer for glyphs inserted by the
28139 display engine for its internal purposes, like truncation
28140 and continuation glyphs and blanks beyond the end of
28141 line's text on text terminals. If we are over such a
28142 glyph, we are not over any text. */
28143 || INTEGERP (glyph->object)
28144 /* R2L rows have a stretch glyph at their front, which
28145 stands for no text, whereas L2R rows have no glyphs at
28146 all beyond the end of text. Treat such stretch glyphs
28147 like we do with NULL glyphs in L2R rows. */
28148 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28149 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28150 && glyph->type == STRETCH_GLYPH
28151 && glyph->avoid_cursor_p))
28152 {
28153 if (clear_mouse_face (hlinfo))
28154 cursor = No_Cursor;
28155 #ifdef HAVE_WINDOW_SYSTEM
28156 if (FRAME_WINDOW_P (f) && NILP (pointer))
28157 {
28158 if (area != TEXT_AREA)
28159 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28160 else
28161 pointer = Vvoid_text_area_pointer;
28162 }
28163 #endif
28164 goto set_cursor;
28165 }
28166
28167 pos = glyph->charpos;
28168 object = glyph->object;
28169 if (!STRINGP (object) && !BUFFERP (object))
28170 goto set_cursor;
28171
28172 /* If we get an out-of-range value, return now; avoid an error. */
28173 if (BUFFERP (object) && pos > BUF_Z (b))
28174 goto set_cursor;
28175
28176 /* Make the window's buffer temporarily current for
28177 overlays_at and compute_char_face. */
28178 obuf = current_buffer;
28179 current_buffer = b;
28180 obegv = BEGV;
28181 ozv = ZV;
28182 BEGV = BEG;
28183 ZV = Z;
28184
28185 /* Is this char mouse-active or does it have help-echo? */
28186 position = make_number (pos);
28187
28188 if (BUFFERP (object))
28189 {
28190 /* Put all the overlays we want in a vector in overlay_vec. */
28191 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28192 /* Sort overlays into increasing priority order. */
28193 noverlays = sort_overlays (overlay_vec, noverlays, w);
28194 }
28195 else
28196 noverlays = 0;
28197
28198 if (NILP (Vmouse_highlight))
28199 {
28200 clear_mouse_face (hlinfo);
28201 goto check_help_echo;
28202 }
28203
28204 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28205
28206 if (same_region)
28207 cursor = No_Cursor;
28208
28209 /* Check mouse-face highlighting. */
28210 if (! same_region
28211 /* If there exists an overlay with mouse-face overlapping
28212 the one we are currently highlighting, we have to
28213 check if we enter the overlapping overlay, and then
28214 highlight only that. */
28215 || (OVERLAYP (hlinfo->mouse_face_overlay)
28216 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28217 {
28218 /* Find the highest priority overlay with a mouse-face. */
28219 Lisp_Object overlay = Qnil;
28220 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28221 {
28222 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28223 if (!NILP (mouse_face))
28224 overlay = overlay_vec[i];
28225 }
28226
28227 /* If we're highlighting the same overlay as before, there's
28228 no need to do that again. */
28229 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28230 goto check_help_echo;
28231 hlinfo->mouse_face_overlay = overlay;
28232
28233 /* Clear the display of the old active region, if any. */
28234 if (clear_mouse_face (hlinfo))
28235 cursor = No_Cursor;
28236
28237 /* If no overlay applies, get a text property. */
28238 if (NILP (overlay))
28239 mouse_face = Fget_text_property (position, Qmouse_face, object);
28240
28241 /* Next, compute the bounds of the mouse highlighting and
28242 display it. */
28243 if (!NILP (mouse_face) && STRINGP (object))
28244 {
28245 /* The mouse-highlighting comes from a display string
28246 with a mouse-face. */
28247 Lisp_Object s, e;
28248 ptrdiff_t ignore;
28249
28250 s = Fprevious_single_property_change
28251 (make_number (pos + 1), Qmouse_face, object, Qnil);
28252 e = Fnext_single_property_change
28253 (position, Qmouse_face, object, Qnil);
28254 if (NILP (s))
28255 s = make_number (0);
28256 if (NILP (e))
28257 e = make_number (SCHARS (object) - 1);
28258 mouse_face_from_string_pos (w, hlinfo, object,
28259 XINT (s), XINT (e));
28260 hlinfo->mouse_face_past_end = 0;
28261 hlinfo->mouse_face_window = window;
28262 hlinfo->mouse_face_face_id
28263 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28264 glyph->face_id, 1);
28265 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28266 cursor = No_Cursor;
28267 }
28268 else
28269 {
28270 /* The mouse-highlighting, if any, comes from an overlay
28271 or text property in the buffer. */
28272 Lisp_Object buffer IF_LINT (= Qnil);
28273 Lisp_Object disp_string IF_LINT (= Qnil);
28274
28275 if (STRINGP (object))
28276 {
28277 /* If we are on a display string with no mouse-face,
28278 check if the text under it has one. */
28279 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28280 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28281 pos = string_buffer_position (object, start);
28282 if (pos > 0)
28283 {
28284 mouse_face = get_char_property_and_overlay
28285 (make_number (pos), Qmouse_face, w->contents, &overlay);
28286 buffer = w->contents;
28287 disp_string = object;
28288 }
28289 }
28290 else
28291 {
28292 buffer = object;
28293 disp_string = Qnil;
28294 }
28295
28296 if (!NILP (mouse_face))
28297 {
28298 Lisp_Object before, after;
28299 Lisp_Object before_string, after_string;
28300 /* To correctly find the limits of mouse highlight
28301 in a bidi-reordered buffer, we must not use the
28302 optimization of limiting the search in
28303 previous-single-property-change and
28304 next-single-property-change, because
28305 rows_from_pos_range needs the real start and end
28306 positions to DTRT in this case. That's because
28307 the first row visible in a window does not
28308 necessarily display the character whose position
28309 is the smallest. */
28310 Lisp_Object lim1 =
28311 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28312 ? Fmarker_position (w->start)
28313 : Qnil;
28314 Lisp_Object lim2 =
28315 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28316 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28317 : Qnil;
28318
28319 if (NILP (overlay))
28320 {
28321 /* Handle the text property case. */
28322 before = Fprevious_single_property_change
28323 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28324 after = Fnext_single_property_change
28325 (make_number (pos), Qmouse_face, buffer, lim2);
28326 before_string = after_string = Qnil;
28327 }
28328 else
28329 {
28330 /* Handle the overlay case. */
28331 before = Foverlay_start (overlay);
28332 after = Foverlay_end (overlay);
28333 before_string = Foverlay_get (overlay, Qbefore_string);
28334 after_string = Foverlay_get (overlay, Qafter_string);
28335
28336 if (!STRINGP (before_string)) before_string = Qnil;
28337 if (!STRINGP (after_string)) after_string = Qnil;
28338 }
28339
28340 mouse_face_from_buffer_pos (window, hlinfo, pos,
28341 NILP (before)
28342 ? 1
28343 : XFASTINT (before),
28344 NILP (after)
28345 ? BUF_Z (XBUFFER (buffer))
28346 : XFASTINT (after),
28347 before_string, after_string,
28348 disp_string);
28349 cursor = No_Cursor;
28350 }
28351 }
28352 }
28353
28354 check_help_echo:
28355
28356 /* Look for a `help-echo' property. */
28357 if (NILP (help_echo_string)) {
28358 Lisp_Object help, overlay;
28359
28360 /* Check overlays first. */
28361 help = overlay = Qnil;
28362 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28363 {
28364 overlay = overlay_vec[i];
28365 help = Foverlay_get (overlay, Qhelp_echo);
28366 }
28367
28368 if (!NILP (help))
28369 {
28370 help_echo_string = help;
28371 help_echo_window = window;
28372 help_echo_object = overlay;
28373 help_echo_pos = pos;
28374 }
28375 else
28376 {
28377 Lisp_Object obj = glyph->object;
28378 ptrdiff_t charpos = glyph->charpos;
28379
28380 /* Try text properties. */
28381 if (STRINGP (obj)
28382 && charpos >= 0
28383 && charpos < SCHARS (obj))
28384 {
28385 help = Fget_text_property (make_number (charpos),
28386 Qhelp_echo, obj);
28387 if (NILP (help))
28388 {
28389 /* If the string itself doesn't specify a help-echo,
28390 see if the buffer text ``under'' it does. */
28391 struct glyph_row *r
28392 = MATRIX_ROW (w->current_matrix, vpos);
28393 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28394 ptrdiff_t p = string_buffer_position (obj, start);
28395 if (p > 0)
28396 {
28397 help = Fget_char_property (make_number (p),
28398 Qhelp_echo, w->contents);
28399 if (!NILP (help))
28400 {
28401 charpos = p;
28402 obj = w->contents;
28403 }
28404 }
28405 }
28406 }
28407 else if (BUFFERP (obj)
28408 && charpos >= BEGV
28409 && charpos < ZV)
28410 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28411 obj);
28412
28413 if (!NILP (help))
28414 {
28415 help_echo_string = help;
28416 help_echo_window = window;
28417 help_echo_object = obj;
28418 help_echo_pos = charpos;
28419 }
28420 }
28421 }
28422
28423 #ifdef HAVE_WINDOW_SYSTEM
28424 /* Look for a `pointer' property. */
28425 if (FRAME_WINDOW_P (f) && NILP (pointer))
28426 {
28427 /* Check overlays first. */
28428 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28429 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28430
28431 if (NILP (pointer))
28432 {
28433 Lisp_Object obj = glyph->object;
28434 ptrdiff_t charpos = glyph->charpos;
28435
28436 /* Try text properties. */
28437 if (STRINGP (obj)
28438 && charpos >= 0
28439 && charpos < SCHARS (obj))
28440 {
28441 pointer = Fget_text_property (make_number (charpos),
28442 Qpointer, obj);
28443 if (NILP (pointer))
28444 {
28445 /* If the string itself doesn't specify a pointer,
28446 see if the buffer text ``under'' it does. */
28447 struct glyph_row *r
28448 = MATRIX_ROW (w->current_matrix, vpos);
28449 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28450 ptrdiff_t p = string_buffer_position (obj, start);
28451 if (p > 0)
28452 pointer = Fget_char_property (make_number (p),
28453 Qpointer, w->contents);
28454 }
28455 }
28456 else if (BUFFERP (obj)
28457 && charpos >= BEGV
28458 && charpos < ZV)
28459 pointer = Fget_text_property (make_number (charpos),
28460 Qpointer, obj);
28461 }
28462 }
28463 #endif /* HAVE_WINDOW_SYSTEM */
28464
28465 BEGV = obegv;
28466 ZV = ozv;
28467 current_buffer = obuf;
28468 }
28469
28470 set_cursor:
28471
28472 #ifdef HAVE_WINDOW_SYSTEM
28473 if (FRAME_WINDOW_P (f))
28474 define_frame_cursor1 (f, cursor, pointer);
28475 #else
28476 /* This is here to prevent a compiler error, about "label at end of
28477 compound statement". */
28478 return;
28479 #endif
28480 }
28481
28482
28483 /* EXPORT for RIF:
28484 Clear any mouse-face on window W. This function is part of the
28485 redisplay interface, and is called from try_window_id and similar
28486 functions to ensure the mouse-highlight is off. */
28487
28488 void
28489 x_clear_window_mouse_face (struct window *w)
28490 {
28491 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28492 Lisp_Object window;
28493
28494 block_input ();
28495 XSETWINDOW (window, w);
28496 if (EQ (window, hlinfo->mouse_face_window))
28497 clear_mouse_face (hlinfo);
28498 unblock_input ();
28499 }
28500
28501
28502 /* EXPORT:
28503 Just discard the mouse face information for frame F, if any.
28504 This is used when the size of F is changed. */
28505
28506 void
28507 cancel_mouse_face (struct frame *f)
28508 {
28509 Lisp_Object window;
28510 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28511
28512 window = hlinfo->mouse_face_window;
28513 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28514 reset_mouse_highlight (hlinfo);
28515 }
28516
28517
28518 \f
28519 /***********************************************************************
28520 Exposure Events
28521 ***********************************************************************/
28522
28523 #ifdef HAVE_WINDOW_SYSTEM
28524
28525 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28526 which intersects rectangle R. R is in window-relative coordinates. */
28527
28528 static void
28529 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28530 enum glyph_row_area area)
28531 {
28532 struct glyph *first = row->glyphs[area];
28533 struct glyph *end = row->glyphs[area] + row->used[area];
28534 struct glyph *last;
28535 int first_x, start_x, x;
28536
28537 if (area == TEXT_AREA && row->fill_line_p)
28538 /* If row extends face to end of line write the whole line. */
28539 draw_glyphs (w, 0, row, area,
28540 0, row->used[area],
28541 DRAW_NORMAL_TEXT, 0);
28542 else
28543 {
28544 /* Set START_X to the window-relative start position for drawing glyphs of
28545 AREA. The first glyph of the text area can be partially visible.
28546 The first glyphs of other areas cannot. */
28547 start_x = window_box_left_offset (w, area);
28548 x = start_x;
28549 if (area == TEXT_AREA)
28550 x += row->x;
28551
28552 /* Find the first glyph that must be redrawn. */
28553 while (first < end
28554 && x + first->pixel_width < r->x)
28555 {
28556 x += first->pixel_width;
28557 ++first;
28558 }
28559
28560 /* Find the last one. */
28561 last = first;
28562 first_x = x;
28563 while (last < end
28564 && x < r->x + r->width)
28565 {
28566 x += last->pixel_width;
28567 ++last;
28568 }
28569
28570 /* Repaint. */
28571 if (last > first)
28572 draw_glyphs (w, first_x - start_x, row, area,
28573 first - row->glyphs[area], last - row->glyphs[area],
28574 DRAW_NORMAL_TEXT, 0);
28575 }
28576 }
28577
28578
28579 /* Redraw the parts of the glyph row ROW on window W intersecting
28580 rectangle R. R is in window-relative coordinates. Value is
28581 non-zero if mouse-face was overwritten. */
28582
28583 static int
28584 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28585 {
28586 eassert (row->enabled_p);
28587
28588 if (row->mode_line_p || w->pseudo_window_p)
28589 draw_glyphs (w, 0, row, TEXT_AREA,
28590 0, row->used[TEXT_AREA],
28591 DRAW_NORMAL_TEXT, 0);
28592 else
28593 {
28594 if (row->used[LEFT_MARGIN_AREA])
28595 expose_area (w, row, r, LEFT_MARGIN_AREA);
28596 if (row->used[TEXT_AREA])
28597 expose_area (w, row, r, TEXT_AREA);
28598 if (row->used[RIGHT_MARGIN_AREA])
28599 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28600 draw_row_fringe_bitmaps (w, row);
28601 }
28602
28603 return row->mouse_face_p;
28604 }
28605
28606
28607 /* Redraw those parts of glyphs rows during expose event handling that
28608 overlap other rows. Redrawing of an exposed line writes over parts
28609 of lines overlapping that exposed line; this function fixes that.
28610
28611 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28612 row in W's current matrix that is exposed and overlaps other rows.
28613 LAST_OVERLAPPING_ROW is the last such row. */
28614
28615 static void
28616 expose_overlaps (struct window *w,
28617 struct glyph_row *first_overlapping_row,
28618 struct glyph_row *last_overlapping_row,
28619 XRectangle *r)
28620 {
28621 struct glyph_row *row;
28622
28623 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28624 if (row->overlapping_p)
28625 {
28626 eassert (row->enabled_p && !row->mode_line_p);
28627
28628 row->clip = r;
28629 if (row->used[LEFT_MARGIN_AREA])
28630 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28631
28632 if (row->used[TEXT_AREA])
28633 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28634
28635 if (row->used[RIGHT_MARGIN_AREA])
28636 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28637 row->clip = NULL;
28638 }
28639 }
28640
28641
28642 /* Return non-zero if W's cursor intersects rectangle R. */
28643
28644 static int
28645 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28646 {
28647 XRectangle cr, result;
28648 struct glyph *cursor_glyph;
28649 struct glyph_row *row;
28650
28651 if (w->phys_cursor.vpos >= 0
28652 && w->phys_cursor.vpos < w->current_matrix->nrows
28653 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28654 row->enabled_p)
28655 && row->cursor_in_fringe_p)
28656 {
28657 /* Cursor is in the fringe. */
28658 cr.x = window_box_right_offset (w,
28659 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28660 ? RIGHT_MARGIN_AREA
28661 : TEXT_AREA));
28662 cr.y = row->y;
28663 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28664 cr.height = row->height;
28665 return x_intersect_rectangles (&cr, r, &result);
28666 }
28667
28668 cursor_glyph = get_phys_cursor_glyph (w);
28669 if (cursor_glyph)
28670 {
28671 /* r is relative to W's box, but w->phys_cursor.x is relative
28672 to left edge of W's TEXT area. Adjust it. */
28673 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28674 cr.y = w->phys_cursor.y;
28675 cr.width = cursor_glyph->pixel_width;
28676 cr.height = w->phys_cursor_height;
28677 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28678 I assume the effect is the same -- and this is portable. */
28679 return x_intersect_rectangles (&cr, r, &result);
28680 }
28681 /* If we don't understand the format, pretend we're not in the hot-spot. */
28682 return 0;
28683 }
28684
28685
28686 /* EXPORT:
28687 Draw a vertical window border to the right of window W if W doesn't
28688 have vertical scroll bars. */
28689
28690 void
28691 x_draw_vertical_border (struct window *w)
28692 {
28693 struct frame *f = XFRAME (WINDOW_FRAME (w));
28694
28695 /* We could do better, if we knew what type of scroll-bar the adjacent
28696 windows (on either side) have... But we don't :-(
28697 However, I think this works ok. ++KFS 2003-04-25 */
28698
28699 /* Redraw borders between horizontally adjacent windows. Don't
28700 do it for frames with vertical scroll bars because either the
28701 right scroll bar of a window, or the left scroll bar of its
28702 neighbor will suffice as a border. */
28703 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28704 return;
28705
28706 /* Note: It is necessary to redraw both the left and the right
28707 borders, for when only this single window W is being
28708 redisplayed. */
28709 if (!WINDOW_RIGHTMOST_P (w)
28710 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28711 {
28712 int x0, x1, y0, y1;
28713
28714 window_box_edges (w, &x0, &y0, &x1, &y1);
28715 y1 -= 1;
28716
28717 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28718 x1 -= 1;
28719
28720 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28721 }
28722 if (!WINDOW_LEFTMOST_P (w)
28723 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28724 {
28725 int x0, x1, y0, y1;
28726
28727 window_box_edges (w, &x0, &y0, &x1, &y1);
28728 y1 -= 1;
28729
28730 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28731 x0 -= 1;
28732
28733 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28734 }
28735 }
28736
28737
28738 /* Redraw the part of window W intersection rectangle FR. Pixel
28739 coordinates in FR are frame-relative. Call this function with
28740 input blocked. Value is non-zero if the exposure overwrites
28741 mouse-face. */
28742
28743 static int
28744 expose_window (struct window *w, XRectangle *fr)
28745 {
28746 struct frame *f = XFRAME (w->frame);
28747 XRectangle wr, r;
28748 int mouse_face_overwritten_p = 0;
28749
28750 /* If window is not yet fully initialized, do nothing. This can
28751 happen when toolkit scroll bars are used and a window is split.
28752 Reconfiguring the scroll bar will generate an expose for a newly
28753 created window. */
28754 if (w->current_matrix == NULL)
28755 return 0;
28756
28757 /* When we're currently updating the window, display and current
28758 matrix usually don't agree. Arrange for a thorough display
28759 later. */
28760 if (w->must_be_updated_p)
28761 {
28762 SET_FRAME_GARBAGED (f);
28763 return 0;
28764 }
28765
28766 /* Frame-relative pixel rectangle of W. */
28767 wr.x = WINDOW_LEFT_EDGE_X (w);
28768 wr.y = WINDOW_TOP_EDGE_Y (w);
28769 wr.width = WINDOW_TOTAL_WIDTH (w);
28770 wr.height = WINDOW_TOTAL_HEIGHT (w);
28771
28772 if (x_intersect_rectangles (fr, &wr, &r))
28773 {
28774 int yb = window_text_bottom_y (w);
28775 struct glyph_row *row;
28776 int cursor_cleared_p, phys_cursor_on_p;
28777 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28778
28779 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28780 r.x, r.y, r.width, r.height));
28781
28782 /* Convert to window coordinates. */
28783 r.x -= WINDOW_LEFT_EDGE_X (w);
28784 r.y -= WINDOW_TOP_EDGE_Y (w);
28785
28786 /* Turn off the cursor. */
28787 if (!w->pseudo_window_p
28788 && phys_cursor_in_rect_p (w, &r))
28789 {
28790 x_clear_cursor (w);
28791 cursor_cleared_p = 1;
28792 }
28793 else
28794 cursor_cleared_p = 0;
28795
28796 /* If the row containing the cursor extends face to end of line,
28797 then expose_area might overwrite the cursor outside the
28798 rectangle and thus notice_overwritten_cursor might clear
28799 w->phys_cursor_on_p. We remember the original value and
28800 check later if it is changed. */
28801 phys_cursor_on_p = w->phys_cursor_on_p;
28802
28803 /* Update lines intersecting rectangle R. */
28804 first_overlapping_row = last_overlapping_row = NULL;
28805 for (row = w->current_matrix->rows;
28806 row->enabled_p;
28807 ++row)
28808 {
28809 int y0 = row->y;
28810 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28811
28812 if ((y0 >= r.y && y0 < r.y + r.height)
28813 || (y1 > r.y && y1 < r.y + r.height)
28814 || (r.y >= y0 && r.y < y1)
28815 || (r.y + r.height > y0 && r.y + r.height < y1))
28816 {
28817 /* A header line may be overlapping, but there is no need
28818 to fix overlapping areas for them. KFS 2005-02-12 */
28819 if (row->overlapping_p && !row->mode_line_p)
28820 {
28821 if (first_overlapping_row == NULL)
28822 first_overlapping_row = row;
28823 last_overlapping_row = row;
28824 }
28825
28826 row->clip = fr;
28827 if (expose_line (w, row, &r))
28828 mouse_face_overwritten_p = 1;
28829 row->clip = NULL;
28830 }
28831 else if (row->overlapping_p)
28832 {
28833 /* We must redraw a row overlapping the exposed area. */
28834 if (y0 < r.y
28835 ? y0 + row->phys_height > r.y
28836 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28837 {
28838 if (first_overlapping_row == NULL)
28839 first_overlapping_row = row;
28840 last_overlapping_row = row;
28841 }
28842 }
28843
28844 if (y1 >= yb)
28845 break;
28846 }
28847
28848 /* Display the mode line if there is one. */
28849 if (WINDOW_WANTS_MODELINE_P (w)
28850 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28851 row->enabled_p)
28852 && row->y < r.y + r.height)
28853 {
28854 if (expose_line (w, row, &r))
28855 mouse_face_overwritten_p = 1;
28856 }
28857
28858 if (!w->pseudo_window_p)
28859 {
28860 /* Fix the display of overlapping rows. */
28861 if (first_overlapping_row)
28862 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28863 fr);
28864
28865 /* Draw border between windows. */
28866 x_draw_vertical_border (w);
28867
28868 /* Turn the cursor on again. */
28869 if (cursor_cleared_p
28870 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28871 update_window_cursor (w, 1);
28872 }
28873 }
28874
28875 return mouse_face_overwritten_p;
28876 }
28877
28878
28879
28880 /* Redraw (parts) of all windows in the window tree rooted at W that
28881 intersect R. R contains frame pixel coordinates. Value is
28882 non-zero if the exposure overwrites mouse-face. */
28883
28884 static int
28885 expose_window_tree (struct window *w, XRectangle *r)
28886 {
28887 struct frame *f = XFRAME (w->frame);
28888 int mouse_face_overwritten_p = 0;
28889
28890 while (w && !FRAME_GARBAGED_P (f))
28891 {
28892 if (WINDOWP (w->contents))
28893 mouse_face_overwritten_p
28894 |= expose_window_tree (XWINDOW (w->contents), r);
28895 else
28896 mouse_face_overwritten_p |= expose_window (w, r);
28897
28898 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28899 }
28900
28901 return mouse_face_overwritten_p;
28902 }
28903
28904
28905 /* EXPORT:
28906 Redisplay an exposed area of frame F. X and Y are the upper-left
28907 corner of the exposed rectangle. W and H are width and height of
28908 the exposed area. All are pixel values. W or H zero means redraw
28909 the entire frame. */
28910
28911 void
28912 expose_frame (struct frame *f, int x, int y, int w, int h)
28913 {
28914 XRectangle r;
28915 int mouse_face_overwritten_p = 0;
28916
28917 TRACE ((stderr, "expose_frame "));
28918
28919 /* No need to redraw if frame will be redrawn soon. */
28920 if (FRAME_GARBAGED_P (f))
28921 {
28922 TRACE ((stderr, " garbaged\n"));
28923 return;
28924 }
28925
28926 /* If basic faces haven't been realized yet, there is no point in
28927 trying to redraw anything. This can happen when we get an expose
28928 event while Emacs is starting, e.g. by moving another window. */
28929 if (FRAME_FACE_CACHE (f) == NULL
28930 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28931 {
28932 TRACE ((stderr, " no faces\n"));
28933 return;
28934 }
28935
28936 if (w == 0 || h == 0)
28937 {
28938 r.x = r.y = 0;
28939 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28940 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28941 }
28942 else
28943 {
28944 r.x = x;
28945 r.y = y;
28946 r.width = w;
28947 r.height = h;
28948 }
28949
28950 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28951 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28952
28953 if (WINDOWP (f->tool_bar_window))
28954 mouse_face_overwritten_p
28955 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28956
28957 #ifdef HAVE_X_WINDOWS
28958 #ifndef MSDOS
28959 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28960 if (WINDOWP (f->menu_bar_window))
28961 mouse_face_overwritten_p
28962 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28963 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28964 #endif
28965 #endif
28966
28967 /* Some window managers support a focus-follows-mouse style with
28968 delayed raising of frames. Imagine a partially obscured frame,
28969 and moving the mouse into partially obscured mouse-face on that
28970 frame. The visible part of the mouse-face will be highlighted,
28971 then the WM raises the obscured frame. With at least one WM, KDE
28972 2.1, Emacs is not getting any event for the raising of the frame
28973 (even tried with SubstructureRedirectMask), only Expose events.
28974 These expose events will draw text normally, i.e. not
28975 highlighted. Which means we must redo the highlight here.
28976 Subsume it under ``we love X''. --gerd 2001-08-15 */
28977 /* Included in Windows version because Windows most likely does not
28978 do the right thing if any third party tool offers
28979 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28980 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28981 {
28982 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28983 if (f == hlinfo->mouse_face_mouse_frame)
28984 {
28985 int mouse_x = hlinfo->mouse_face_mouse_x;
28986 int mouse_y = hlinfo->mouse_face_mouse_y;
28987 clear_mouse_face (hlinfo);
28988 note_mouse_highlight (f, mouse_x, mouse_y);
28989 }
28990 }
28991 }
28992
28993
28994 /* EXPORT:
28995 Determine the intersection of two rectangles R1 and R2. Return
28996 the intersection in *RESULT. Value is non-zero if RESULT is not
28997 empty. */
28998
28999 int
29000 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29001 {
29002 XRectangle *left, *right;
29003 XRectangle *upper, *lower;
29004 int intersection_p = 0;
29005
29006 /* Rearrange so that R1 is the left-most rectangle. */
29007 if (r1->x < r2->x)
29008 left = r1, right = r2;
29009 else
29010 left = r2, right = r1;
29011
29012 /* X0 of the intersection is right.x0, if this is inside R1,
29013 otherwise there is no intersection. */
29014 if (right->x <= left->x + left->width)
29015 {
29016 result->x = right->x;
29017
29018 /* The right end of the intersection is the minimum of
29019 the right ends of left and right. */
29020 result->width = (min (left->x + left->width, right->x + right->width)
29021 - result->x);
29022
29023 /* Same game for Y. */
29024 if (r1->y < r2->y)
29025 upper = r1, lower = r2;
29026 else
29027 upper = r2, lower = r1;
29028
29029 /* The upper end of the intersection is lower.y0, if this is inside
29030 of upper. Otherwise, there is no intersection. */
29031 if (lower->y <= upper->y + upper->height)
29032 {
29033 result->y = lower->y;
29034
29035 /* The lower end of the intersection is the minimum of the lower
29036 ends of upper and lower. */
29037 result->height = (min (lower->y + lower->height,
29038 upper->y + upper->height)
29039 - result->y);
29040 intersection_p = 1;
29041 }
29042 }
29043
29044 return intersection_p;
29045 }
29046
29047 #endif /* HAVE_WINDOW_SYSTEM */
29048
29049 \f
29050 /***********************************************************************
29051 Initialization
29052 ***********************************************************************/
29053
29054 void
29055 syms_of_xdisp (void)
29056 {
29057 Vwith_echo_area_save_vector = Qnil;
29058 staticpro (&Vwith_echo_area_save_vector);
29059
29060 Vmessage_stack = Qnil;
29061 staticpro (&Vmessage_stack);
29062
29063 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29064 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29065
29066 message_dolog_marker1 = Fmake_marker ();
29067 staticpro (&message_dolog_marker1);
29068 message_dolog_marker2 = Fmake_marker ();
29069 staticpro (&message_dolog_marker2);
29070 message_dolog_marker3 = Fmake_marker ();
29071 staticpro (&message_dolog_marker3);
29072
29073 #ifdef GLYPH_DEBUG
29074 defsubr (&Sdump_frame_glyph_matrix);
29075 defsubr (&Sdump_glyph_matrix);
29076 defsubr (&Sdump_glyph_row);
29077 defsubr (&Sdump_tool_bar_row);
29078 defsubr (&Strace_redisplay);
29079 defsubr (&Strace_to_stderr);
29080 #endif
29081 #ifdef HAVE_WINDOW_SYSTEM
29082 defsubr (&Stool_bar_lines_needed);
29083 defsubr (&Slookup_image_map);
29084 #endif
29085 defsubr (&Sline_pixel_height);
29086 defsubr (&Sformat_mode_line);
29087 defsubr (&Sinvisible_p);
29088 defsubr (&Scurrent_bidi_paragraph_direction);
29089 defsubr (&Smove_point_visually);
29090
29091 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29092 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29093 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29094 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29095 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29096 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29097 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29098 DEFSYM (Qeval, "eval");
29099 DEFSYM (QCdata, ":data");
29100 DEFSYM (Qdisplay, "display");
29101 DEFSYM (Qspace_width, "space-width");
29102 DEFSYM (Qraise, "raise");
29103 DEFSYM (Qslice, "slice");
29104 DEFSYM (Qspace, "space");
29105 DEFSYM (Qmargin, "margin");
29106 DEFSYM (Qpointer, "pointer");
29107 DEFSYM (Qleft_margin, "left-margin");
29108 DEFSYM (Qright_margin, "right-margin");
29109 DEFSYM (Qcenter, "center");
29110 DEFSYM (Qline_height, "line-height");
29111 DEFSYM (QCalign_to, ":align-to");
29112 DEFSYM (QCrelative_width, ":relative-width");
29113 DEFSYM (QCrelative_height, ":relative-height");
29114 DEFSYM (QCeval, ":eval");
29115 DEFSYM (QCpropertize, ":propertize");
29116 DEFSYM (QCfile, ":file");
29117 DEFSYM (Qfontified, "fontified");
29118 DEFSYM (Qfontification_functions, "fontification-functions");
29119 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29120 DEFSYM (Qescape_glyph, "escape-glyph");
29121 DEFSYM (Qnobreak_space, "nobreak-space");
29122 DEFSYM (Qimage, "image");
29123 DEFSYM (Qtext, "text");
29124 DEFSYM (Qboth, "both");
29125 DEFSYM (Qboth_horiz, "both-horiz");
29126 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29127 DEFSYM (QCmap, ":map");
29128 DEFSYM (QCpointer, ":pointer");
29129 DEFSYM (Qrect, "rect");
29130 DEFSYM (Qcircle, "circle");
29131 DEFSYM (Qpoly, "poly");
29132 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29133 DEFSYM (Qgrow_only, "grow-only");
29134 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29135 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29136 DEFSYM (Qposition, "position");
29137 DEFSYM (Qbuffer_position, "buffer-position");
29138 DEFSYM (Qobject, "object");
29139 DEFSYM (Qbar, "bar");
29140 DEFSYM (Qhbar, "hbar");
29141 DEFSYM (Qbox, "box");
29142 DEFSYM (Qhollow, "hollow");
29143 DEFSYM (Qhand, "hand");
29144 DEFSYM (Qarrow, "arrow");
29145 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29146
29147 list_of_error = list1 (list2 (intern_c_string ("error"),
29148 intern_c_string ("void-variable")));
29149 staticpro (&list_of_error);
29150
29151 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29152 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29153 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29154 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29155
29156 echo_buffer[0] = echo_buffer[1] = Qnil;
29157 staticpro (&echo_buffer[0]);
29158 staticpro (&echo_buffer[1]);
29159
29160 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29161 staticpro (&echo_area_buffer[0]);
29162 staticpro (&echo_area_buffer[1]);
29163
29164 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29165 staticpro (&Vmessages_buffer_name);
29166
29167 mode_line_proptrans_alist = Qnil;
29168 staticpro (&mode_line_proptrans_alist);
29169 mode_line_string_list = Qnil;
29170 staticpro (&mode_line_string_list);
29171 mode_line_string_face = Qnil;
29172 staticpro (&mode_line_string_face);
29173 mode_line_string_face_prop = Qnil;
29174 staticpro (&mode_line_string_face_prop);
29175 Vmode_line_unwind_vector = Qnil;
29176 staticpro (&Vmode_line_unwind_vector);
29177
29178 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29179
29180 help_echo_string = Qnil;
29181 staticpro (&help_echo_string);
29182 help_echo_object = Qnil;
29183 staticpro (&help_echo_object);
29184 help_echo_window = Qnil;
29185 staticpro (&help_echo_window);
29186 previous_help_echo_string = Qnil;
29187 staticpro (&previous_help_echo_string);
29188 help_echo_pos = -1;
29189
29190 DEFSYM (Qright_to_left, "right-to-left");
29191 DEFSYM (Qleft_to_right, "left-to-right");
29192
29193 #ifdef HAVE_WINDOW_SYSTEM
29194 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29195 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29196 For example, if a block cursor is over a tab, it will be drawn as
29197 wide as that tab on the display. */);
29198 x_stretch_cursor_p = 0;
29199 #endif
29200
29201 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29202 doc: /* Non-nil means highlight trailing whitespace.
29203 The face used for trailing whitespace is `trailing-whitespace'. */);
29204 Vshow_trailing_whitespace = Qnil;
29205
29206 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29207 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29208 If the value is t, Emacs highlights non-ASCII chars which have the
29209 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29210 or `escape-glyph' face respectively.
29211
29212 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29213 U+2011 (non-breaking hyphen) are affected.
29214
29215 Any other non-nil value means to display these characters as a escape
29216 glyph followed by an ordinary space or hyphen.
29217
29218 A value of nil means no special handling of these characters. */);
29219 Vnobreak_char_display = Qt;
29220
29221 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29222 doc: /* The pointer shape to show in void text areas.
29223 A value of nil means to show the text pointer. Other options are `arrow',
29224 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29225 Vvoid_text_area_pointer = Qarrow;
29226
29227 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29228 doc: /* Non-nil means don't actually do any redisplay.
29229 This is used for internal purposes. */);
29230 Vinhibit_redisplay = Qnil;
29231
29232 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29233 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29234 Vglobal_mode_string = Qnil;
29235
29236 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29237 doc: /* Marker for where to display an arrow on top of the buffer text.
29238 This must be the beginning of a line in order to work.
29239 See also `overlay-arrow-string'. */);
29240 Voverlay_arrow_position = Qnil;
29241
29242 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29243 doc: /* String to display as an arrow in non-window frames.
29244 See also `overlay-arrow-position'. */);
29245 Voverlay_arrow_string = build_pure_c_string ("=>");
29246
29247 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29248 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29249 The symbols on this list are examined during redisplay to determine
29250 where to display overlay arrows. */);
29251 Voverlay_arrow_variable_list
29252 = list1 (intern_c_string ("overlay-arrow-position"));
29253
29254 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29255 doc: /* The number of lines to try scrolling a window by when point moves out.
29256 If that fails to bring point back on frame, point is centered instead.
29257 If this is zero, point is always centered after it moves off frame.
29258 If you want scrolling to always be a line at a time, you should set
29259 `scroll-conservatively' to a large value rather than set this to 1. */);
29260
29261 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29262 doc: /* Scroll up to this many lines, to bring point back on screen.
29263 If point moves off-screen, redisplay will scroll by up to
29264 `scroll-conservatively' lines in order to bring point just barely
29265 onto the screen again. If that cannot be done, then redisplay
29266 recenters point as usual.
29267
29268 If the value is greater than 100, redisplay will never recenter point,
29269 but will always scroll just enough text to bring point into view, even
29270 if you move far away.
29271
29272 A value of zero means always recenter point if it moves off screen. */);
29273 scroll_conservatively = 0;
29274
29275 DEFVAR_INT ("scroll-margin", scroll_margin,
29276 doc: /* Number of lines of margin at the top and bottom of a window.
29277 Recenter the window whenever point gets within this many lines
29278 of the top or bottom of the window. */);
29279 scroll_margin = 0;
29280
29281 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29282 doc: /* Pixels per inch value for non-window system displays.
29283 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29284 Vdisplay_pixels_per_inch = make_float (72.0);
29285
29286 #ifdef GLYPH_DEBUG
29287 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29288 #endif
29289
29290 DEFVAR_LISP ("truncate-partial-width-windows",
29291 Vtruncate_partial_width_windows,
29292 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29293 For an integer value, truncate lines in each window narrower than the
29294 full frame width, provided the window width is less than that integer;
29295 otherwise, respect the value of `truncate-lines'.
29296
29297 For any other non-nil value, truncate lines in all windows that do
29298 not span the full frame width.
29299
29300 A value of nil means to respect the value of `truncate-lines'.
29301
29302 If `word-wrap' is enabled, you might want to reduce this. */);
29303 Vtruncate_partial_width_windows = make_number (50);
29304
29305 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29306 doc: /* Maximum buffer size for which line number should be displayed.
29307 If the buffer is bigger than this, the line number does not appear
29308 in the mode line. A value of nil means no limit. */);
29309 Vline_number_display_limit = Qnil;
29310
29311 DEFVAR_INT ("line-number-display-limit-width",
29312 line_number_display_limit_width,
29313 doc: /* Maximum line width (in characters) for line number display.
29314 If the average length of the lines near point is bigger than this, then the
29315 line number may be omitted from the mode line. */);
29316 line_number_display_limit_width = 200;
29317
29318 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29319 doc: /* Non-nil means highlight region even in nonselected windows. */);
29320 highlight_nonselected_windows = 0;
29321
29322 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29323 doc: /* Non-nil if more than one frame is visible on this display.
29324 Minibuffer-only frames don't count, but iconified frames do.
29325 This variable is not guaranteed to be accurate except while processing
29326 `frame-title-format' and `icon-title-format'. */);
29327
29328 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29329 doc: /* Template for displaying the title bar of visible frames.
29330 \(Assuming the window manager supports this feature.)
29331
29332 This variable has the same structure as `mode-line-format', except that
29333 the %c and %l constructs are ignored. It is used only on frames for
29334 which no explicit name has been set \(see `modify-frame-parameters'). */);
29335
29336 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29337 doc: /* Template for displaying the title bar of an iconified frame.
29338 \(Assuming the window manager supports this feature.)
29339 This variable has the same structure as `mode-line-format' (which see),
29340 and is used only on frames for which no explicit name has been set
29341 \(see `modify-frame-parameters'). */);
29342 Vicon_title_format
29343 = Vframe_title_format
29344 = listn (CONSTYPE_PURE, 3,
29345 intern_c_string ("multiple-frames"),
29346 build_pure_c_string ("%b"),
29347 listn (CONSTYPE_PURE, 4,
29348 empty_unibyte_string,
29349 intern_c_string ("invocation-name"),
29350 build_pure_c_string ("@"),
29351 intern_c_string ("system-name")));
29352
29353 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29354 doc: /* Maximum number of lines to keep in the message log buffer.
29355 If nil, disable message logging. If t, log messages but don't truncate
29356 the buffer when it becomes large. */);
29357 Vmessage_log_max = make_number (1000);
29358
29359 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29360 doc: /* Functions called before redisplay, if window sizes have changed.
29361 The value should be a list of functions that take one argument.
29362 Just before redisplay, for each frame, if any of its windows have changed
29363 size since the last redisplay, or have been split or deleted,
29364 all the functions in the list are called, with the frame as argument. */);
29365 Vwindow_size_change_functions = Qnil;
29366
29367 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29368 doc: /* List of functions to call before redisplaying a window with scrolling.
29369 Each function is called with two arguments, the window and its new
29370 display-start position. Note that these functions are also called by
29371 `set-window-buffer'. Also note that the value of `window-end' is not
29372 valid when these functions are called.
29373
29374 Warning: Do not use this feature to alter the way the window
29375 is scrolled. It is not designed for that, and such use probably won't
29376 work. */);
29377 Vwindow_scroll_functions = Qnil;
29378
29379 DEFVAR_LISP ("window-text-change-functions",
29380 Vwindow_text_change_functions,
29381 doc: /* Functions to call in redisplay when text in the window might change. */);
29382 Vwindow_text_change_functions = Qnil;
29383
29384 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29385 doc: /* Functions called when redisplay of a window reaches the end trigger.
29386 Each function is called with two arguments, the window and the end trigger value.
29387 See `set-window-redisplay-end-trigger'. */);
29388 Vredisplay_end_trigger_functions = Qnil;
29389
29390 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29391 doc: /* Non-nil means autoselect window with mouse pointer.
29392 If nil, do not autoselect windows.
29393 A positive number means delay autoselection by that many seconds: a
29394 window is autoselected only after the mouse has remained in that
29395 window for the duration of the delay.
29396 A negative number has a similar effect, but causes windows to be
29397 autoselected only after the mouse has stopped moving. \(Because of
29398 the way Emacs compares mouse events, you will occasionally wait twice
29399 that time before the window gets selected.\)
29400 Any other value means to autoselect window instantaneously when the
29401 mouse pointer enters it.
29402
29403 Autoselection selects the minibuffer only if it is active, and never
29404 unselects the minibuffer if it is active.
29405
29406 When customizing this variable make sure that the actual value of
29407 `focus-follows-mouse' matches the behavior of your window manager. */);
29408 Vmouse_autoselect_window = Qnil;
29409
29410 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29411 doc: /* Non-nil means automatically resize tool-bars.
29412 This dynamically changes the tool-bar's height to the minimum height
29413 that is needed to make all tool-bar items visible.
29414 If value is `grow-only', the tool-bar's height is only increased
29415 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29416 Vauto_resize_tool_bars = Qt;
29417
29418 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29419 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29420 auto_raise_tool_bar_buttons_p = 1;
29421
29422 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29423 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29424 make_cursor_line_fully_visible_p = 1;
29425
29426 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29427 doc: /* Border below tool-bar in pixels.
29428 If an integer, use it as the height of the border.
29429 If it is one of `internal-border-width' or `border-width', use the
29430 value of the corresponding frame parameter.
29431 Otherwise, no border is added below the tool-bar. */);
29432 Vtool_bar_border = Qinternal_border_width;
29433
29434 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29435 doc: /* Margin around tool-bar buttons in pixels.
29436 If an integer, use that for both horizontal and vertical margins.
29437 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29438 HORZ specifying the horizontal margin, and VERT specifying the
29439 vertical margin. */);
29440 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29441
29442 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29443 doc: /* Relief thickness of tool-bar buttons. */);
29444 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29445
29446 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29447 doc: /* Tool bar style to use.
29448 It can be one of
29449 image - show images only
29450 text - show text only
29451 both - show both, text below image
29452 both-horiz - show text to the right of the image
29453 text-image-horiz - show text to the left of the image
29454 any other - use system default or image if no system default.
29455
29456 This variable only affects the GTK+ toolkit version of Emacs. */);
29457 Vtool_bar_style = Qnil;
29458
29459 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29460 doc: /* Maximum number of characters a label can have to be shown.
29461 The tool bar style must also show labels for this to have any effect, see
29462 `tool-bar-style'. */);
29463 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29464
29465 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29466 doc: /* List of functions to call to fontify regions of text.
29467 Each function is called with one argument POS. Functions must
29468 fontify a region starting at POS in the current buffer, and give
29469 fontified regions the property `fontified'. */);
29470 Vfontification_functions = Qnil;
29471 Fmake_variable_buffer_local (Qfontification_functions);
29472
29473 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29474 unibyte_display_via_language_environment,
29475 doc: /* Non-nil means display unibyte text according to language environment.
29476 Specifically, this means that raw bytes in the range 160-255 decimal
29477 are displayed by converting them to the equivalent multibyte characters
29478 according to the current language environment. As a result, they are
29479 displayed according to the current fontset.
29480
29481 Note that this variable affects only how these bytes are displayed,
29482 but does not change the fact they are interpreted as raw bytes. */);
29483 unibyte_display_via_language_environment = 0;
29484
29485 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29486 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29487 If a float, it specifies a fraction of the mini-window frame's height.
29488 If an integer, it specifies a number of lines. */);
29489 Vmax_mini_window_height = make_float (0.25);
29490
29491 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29492 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29493 A value of nil means don't automatically resize mini-windows.
29494 A value of t means resize them to fit the text displayed in them.
29495 A value of `grow-only', the default, means let mini-windows grow only;
29496 they return to their normal size when the minibuffer is closed, or the
29497 echo area becomes empty. */);
29498 Vresize_mini_windows = Qgrow_only;
29499
29500 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29501 doc: /* Alist specifying how to blink the cursor off.
29502 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29503 `cursor-type' frame-parameter or variable equals ON-STATE,
29504 comparing using `equal', Emacs uses OFF-STATE to specify
29505 how to blink it off. ON-STATE and OFF-STATE are values for
29506 the `cursor-type' frame parameter.
29507
29508 If a frame's ON-STATE has no entry in this list,
29509 the frame's other specifications determine how to blink the cursor off. */);
29510 Vblink_cursor_alist = Qnil;
29511
29512 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29513 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29514 If non-nil, windows are automatically scrolled horizontally to make
29515 point visible. */);
29516 automatic_hscrolling_p = 1;
29517 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29518
29519 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29520 doc: /* How many columns away from the window edge point is allowed to get
29521 before automatic hscrolling will horizontally scroll the window. */);
29522 hscroll_margin = 5;
29523
29524 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29525 doc: /* How many columns to scroll the window when point gets too close to the edge.
29526 When point is less than `hscroll-margin' columns from the window
29527 edge, automatic hscrolling will scroll the window by the amount of columns
29528 determined by this variable. If its value is a positive integer, scroll that
29529 many columns. If it's a positive floating-point number, it specifies the
29530 fraction of the window's width to scroll. If it's nil or zero, point will be
29531 centered horizontally after the scroll. Any other value, including negative
29532 numbers, are treated as if the value were zero.
29533
29534 Automatic hscrolling always moves point outside the scroll margin, so if
29535 point was more than scroll step columns inside the margin, the window will
29536 scroll more than the value given by the scroll step.
29537
29538 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29539 and `scroll-right' overrides this variable's effect. */);
29540 Vhscroll_step = make_number (0);
29541
29542 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29543 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29544 Bind this around calls to `message' to let it take effect. */);
29545 message_truncate_lines = 0;
29546
29547 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29548 doc: /* Normal hook run to update the menu bar definitions.
29549 Redisplay runs this hook before it redisplays the menu bar.
29550 This is used to update submenus such as Buffers,
29551 whose contents depend on various data. */);
29552 Vmenu_bar_update_hook = Qnil;
29553
29554 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29555 doc: /* Frame for which we are updating a menu.
29556 The enable predicate for a menu binding should check this variable. */);
29557 Vmenu_updating_frame = Qnil;
29558
29559 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29560 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29561 inhibit_menubar_update = 0;
29562
29563 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29564 doc: /* Prefix prepended to all continuation lines at display time.
29565 The value may be a string, an image, or a stretch-glyph; it is
29566 interpreted in the same way as the value of a `display' text property.
29567
29568 This variable is overridden by any `wrap-prefix' text or overlay
29569 property.
29570
29571 To add a prefix to non-continuation lines, use `line-prefix'. */);
29572 Vwrap_prefix = Qnil;
29573 DEFSYM (Qwrap_prefix, "wrap-prefix");
29574 Fmake_variable_buffer_local (Qwrap_prefix);
29575
29576 DEFVAR_LISP ("line-prefix", Vline_prefix,
29577 doc: /* Prefix prepended to all non-continuation lines at display time.
29578 The value may be a string, an image, or a stretch-glyph; it is
29579 interpreted in the same way as the value of a `display' text property.
29580
29581 This variable is overridden by any `line-prefix' text or overlay
29582 property.
29583
29584 To add a prefix to continuation lines, use `wrap-prefix'. */);
29585 Vline_prefix = Qnil;
29586 DEFSYM (Qline_prefix, "line-prefix");
29587 Fmake_variable_buffer_local (Qline_prefix);
29588
29589 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29590 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29591 inhibit_eval_during_redisplay = 0;
29592
29593 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29594 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29595 inhibit_free_realized_faces = 0;
29596
29597 #ifdef GLYPH_DEBUG
29598 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29599 doc: /* Inhibit try_window_id display optimization. */);
29600 inhibit_try_window_id = 0;
29601
29602 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29603 doc: /* Inhibit try_window_reusing display optimization. */);
29604 inhibit_try_window_reusing = 0;
29605
29606 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29607 doc: /* Inhibit try_cursor_movement display optimization. */);
29608 inhibit_try_cursor_movement = 0;
29609 #endif /* GLYPH_DEBUG */
29610
29611 DEFVAR_INT ("overline-margin", overline_margin,
29612 doc: /* Space between overline and text, in pixels.
29613 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29614 margin to the character height. */);
29615 overline_margin = 2;
29616
29617 DEFVAR_INT ("underline-minimum-offset",
29618 underline_minimum_offset,
29619 doc: /* Minimum distance between baseline and underline.
29620 This can improve legibility of underlined text at small font sizes,
29621 particularly when using variable `x-use-underline-position-properties'
29622 with fonts that specify an UNDERLINE_POSITION relatively close to the
29623 baseline. The default value is 1. */);
29624 underline_minimum_offset = 1;
29625
29626 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29627 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29628 This feature only works when on a window system that can change
29629 cursor shapes. */);
29630 display_hourglass_p = 1;
29631
29632 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29633 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29634 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29635
29636 #ifdef HAVE_WINDOW_SYSTEM
29637 hourglass_atimer = NULL;
29638 hourglass_shown_p = 0;
29639 #endif /* HAVE_WINDOW_SYSTEM */
29640
29641 DEFSYM (Qglyphless_char, "glyphless-char");
29642 DEFSYM (Qhex_code, "hex-code");
29643 DEFSYM (Qempty_box, "empty-box");
29644 DEFSYM (Qthin_space, "thin-space");
29645 DEFSYM (Qzero_width, "zero-width");
29646
29647 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29648 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29649
29650 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29651 doc: /* Char-table defining glyphless characters.
29652 Each element, if non-nil, should be one of the following:
29653 an ASCII acronym string: display this string in a box
29654 `hex-code': display the hexadecimal code of a character in a box
29655 `empty-box': display as an empty box
29656 `thin-space': display as 1-pixel width space
29657 `zero-width': don't display
29658 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29659 display method for graphical terminals and text terminals respectively.
29660 GRAPHICAL and TEXT should each have one of the values listed above.
29661
29662 The char-table has one extra slot to control the display of a character for
29663 which no font is found. This slot only takes effect on graphical terminals.
29664 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29665 `thin-space'. The default is `empty-box'. */);
29666 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29667 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29668 Qempty_box);
29669
29670 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29671 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29672 Vdebug_on_message = Qnil;
29673 }
29674
29675
29676 /* Initialize this module when Emacs starts. */
29677
29678 void
29679 init_xdisp (void)
29680 {
29681 CHARPOS (this_line_start_pos) = 0;
29682
29683 if (!noninteractive)
29684 {
29685 struct window *m = XWINDOW (minibuf_window);
29686 Lisp_Object frame = m->frame;
29687 struct frame *f = XFRAME (frame);
29688 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29689 struct window *r = XWINDOW (root);
29690 int i;
29691
29692 echo_area_window = minibuf_window;
29693
29694 r->top_line = FRAME_TOP_MARGIN (f);
29695 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29696 r->total_cols = FRAME_COLS (f);
29697
29698 m->top_line = FRAME_LINES (f) - 1;
29699 m->total_lines = 1;
29700 m->total_cols = FRAME_COLS (f);
29701
29702 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29703 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29704 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29705
29706 /* The default ellipsis glyphs `...'. */
29707 for (i = 0; i < 3; ++i)
29708 default_invis_vector[i] = make_number ('.');
29709 }
29710
29711 {
29712 /* Allocate the buffer for frame titles.
29713 Also used for `format-mode-line'. */
29714 int size = 100;
29715 mode_line_noprop_buf = xmalloc (size);
29716 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29717 mode_line_noprop_ptr = mode_line_noprop_buf;
29718 mode_line_target = MODE_LINE_DISPLAY;
29719 }
29720
29721 help_echo_showing_p = 0;
29722 }
29723
29724 #ifdef HAVE_WINDOW_SYSTEM
29725
29726 /* Platform-independent portion of hourglass implementation. */
29727
29728 /* Cancel a currently active hourglass timer, and start a new one. */
29729 void
29730 start_hourglass (void)
29731 {
29732 struct timespec delay;
29733
29734 cancel_hourglass ();
29735
29736 if (INTEGERP (Vhourglass_delay)
29737 && XINT (Vhourglass_delay) > 0)
29738 delay = make_timespec (min (XINT (Vhourglass_delay),
29739 TYPE_MAXIMUM (time_t)),
29740 0);
29741 else if (FLOATP (Vhourglass_delay)
29742 && XFLOAT_DATA (Vhourglass_delay) > 0)
29743 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29744 else
29745 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29746
29747 #ifdef HAVE_NTGUI
29748 {
29749 extern void w32_note_current_window (void);
29750 w32_note_current_window ();
29751 }
29752 #endif /* HAVE_NTGUI */
29753
29754 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29755 show_hourglass, NULL);
29756 }
29757
29758
29759 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29760 shown. */
29761 void
29762 cancel_hourglass (void)
29763 {
29764 if (hourglass_atimer)
29765 {
29766 cancel_atimer (hourglass_atimer);
29767 hourglass_atimer = NULL;
29768 }
29769
29770 if (hourglass_shown_p)
29771 hide_hourglass ();
29772 }
29773
29774 #endif /* HAVE_WINDOW_SYSTEM */