Review and drop old frame resize hack.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
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 after display_mode_line if %l was used and it displayed a
516 line number. */
517
518 static int line_number_displayed;
519
520 /* The name of the *Messages* buffer, a string. */
521
522 static Lisp_Object Vmessages_buffer_name;
523
524 /* Current, index 0, and last displayed echo area message. Either
525 buffers from echo_buffers, or nil to indicate no message. */
526
527 Lisp_Object echo_area_buffer[2];
528
529 /* The buffers referenced from echo_area_buffer. */
530
531 static Lisp_Object echo_buffer[2];
532
533 /* A vector saved used in with_area_buffer to reduce consing. */
534
535 static Lisp_Object Vwith_echo_area_save_vector;
536
537 /* Non-zero means display_echo_area should display the last echo area
538 message again. Set by redisplay_preserve_echo_area. */
539
540 static int display_last_displayed_message_p;
541
542 /* Nonzero if echo area is being used by print; zero if being used by
543 message. */
544
545 static int message_buf_print;
546
547 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
548
549 static Lisp_Object Qinhibit_menubar_update;
550 static Lisp_Object Qmessage_truncate_lines;
551
552 /* Set to 1 in clear_message to make redisplay_internal aware
553 of an emptied echo area. */
554
555 static int message_cleared_p;
556
557 /* A scratch glyph row with contents used for generating truncation
558 glyphs. Also used in direct_output_for_insert. */
559
560 #define MAX_SCRATCH_GLYPHS 100
561 static struct glyph_row scratch_glyph_row;
562 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
563
564 /* Ascent and height of the last line processed by move_it_to. */
565
566 static int last_height;
567
568 /* Non-zero if there's a help-echo in the echo area. */
569
570 int help_echo_showing_p;
571
572 /* The maximum distance to look ahead for text properties. Values
573 that are too small let us call compute_char_face and similar
574 functions too often which is expensive. Values that are too large
575 let us call compute_char_face and alike too often because we
576 might not be interested in text properties that far away. */
577
578 #define TEXT_PROP_DISTANCE_LIMIT 100
579
580 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
581 iterator state and later restore it. This is needed because the
582 bidi iterator on bidi.c keeps a stacked cache of its states, which
583 is really a singleton. When we use scratch iterator objects to
584 move around the buffer, we can cause the bidi cache to be pushed or
585 popped, and therefore we need to restore the cache state when we
586 return to the original iterator. */
587 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
588 do { \
589 if (CACHE) \
590 bidi_unshelve_cache (CACHE, 1); \
591 ITCOPY = ITORIG; \
592 CACHE = bidi_shelve_cache (); \
593 } while (0)
594
595 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
596 do { \
597 if (pITORIG != pITCOPY) \
598 *(pITORIG) = *(pITCOPY); \
599 bidi_unshelve_cache (CACHE, 0); \
600 CACHE = NULL; \
601 } while (0)
602
603 #ifdef GLYPH_DEBUG
604
605 /* Non-zero means print traces of redisplay if compiled with
606 GLYPH_DEBUG defined. */
607
608 int trace_redisplay_p;
609
610 #endif /* GLYPH_DEBUG */
611
612 #ifdef DEBUG_TRACE_MOVE
613 /* Non-zero means trace with TRACE_MOVE to stderr. */
614 int trace_move;
615
616 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
617 #else
618 #define TRACE_MOVE(x) (void) 0
619 #endif
620
621 static Lisp_Object Qauto_hscroll_mode;
622
623 /* Buffer being redisplayed -- for redisplay_window_error. */
624
625 static struct buffer *displayed_buffer;
626
627 /* Value returned from text property handlers (see below). */
628
629 enum prop_handled
630 {
631 HANDLED_NORMALLY,
632 HANDLED_RECOMPUTE_PROPS,
633 HANDLED_OVERLAY_STRING_CONSUMED,
634 HANDLED_RETURN
635 };
636
637 /* A description of text properties that redisplay is interested
638 in. */
639
640 struct props
641 {
642 /* The name of the property. */
643 Lisp_Object *name;
644
645 /* A unique index for the property. */
646 enum prop_idx idx;
647
648 /* A handler function called to set up iterator IT from the property
649 at IT's current position. Value is used to steer handle_stop. */
650 enum prop_handled (*handler) (struct it *it);
651 };
652
653 static enum prop_handled handle_face_prop (struct it *);
654 static enum prop_handled handle_invisible_prop (struct it *);
655 static enum prop_handled handle_display_prop (struct it *);
656 static enum prop_handled handle_composition_prop (struct it *);
657 static enum prop_handled handle_overlay_change (struct it *);
658 static enum prop_handled handle_fontified_prop (struct it *);
659
660 /* Properties handled by iterators. */
661
662 static struct props it_props[] =
663 {
664 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
665 /* Handle `face' before `display' because some sub-properties of
666 `display' need to know the face. */
667 {&Qface, FACE_PROP_IDX, handle_face_prop},
668 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
669 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
670 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
671 {NULL, 0, NULL}
672 };
673
674 /* Value is the position described by X. If X is a marker, value is
675 the marker_position of X. Otherwise, value is X. */
676
677 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
678
679 /* Enumeration returned by some move_it_.* functions internally. */
680
681 enum move_it_result
682 {
683 /* Not used. Undefined value. */
684 MOVE_UNDEFINED,
685
686 /* Move ended at the requested buffer position or ZV. */
687 MOVE_POS_MATCH_OR_ZV,
688
689 /* Move ended at the requested X pixel position. */
690 MOVE_X_REACHED,
691
692 /* Move within a line ended at the end of a line that must be
693 continued. */
694 MOVE_LINE_CONTINUED,
695
696 /* Move within a line ended at the end of a line that would
697 be displayed truncated. */
698 MOVE_LINE_TRUNCATED,
699
700 /* Move within a line ended at a line end. */
701 MOVE_NEWLINE_OR_CR
702 };
703
704 /* This counter is used to clear the face cache every once in a while
705 in redisplay_internal. It is incremented for each redisplay.
706 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
707 cleared. */
708
709 #define CLEAR_FACE_CACHE_COUNT 500
710 static int clear_face_cache_count;
711
712 /* Similarly for the image cache. */
713
714 #ifdef HAVE_WINDOW_SYSTEM
715 #define CLEAR_IMAGE_CACHE_COUNT 101
716 static int clear_image_cache_count;
717
718 /* Null glyph slice */
719 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
720 #endif
721
722 /* True while redisplay_internal is in progress. */
723
724 bool redisplaying_p;
725
726 static Lisp_Object Qinhibit_free_realized_faces;
727 static Lisp_Object Qmode_line_default_help_echo;
728
729 /* If a string, XTread_socket generates an event to display that string.
730 (The display is done in read_char.) */
731
732 Lisp_Object help_echo_string;
733 Lisp_Object help_echo_window;
734 Lisp_Object help_echo_object;
735 ptrdiff_t help_echo_pos;
736
737 /* Temporary variable for XTread_socket. */
738
739 Lisp_Object previous_help_echo_string;
740
741 /* Platform-independent portion of hourglass implementation. */
742
743 #ifdef HAVE_WINDOW_SYSTEM
744
745 /* Non-zero means an hourglass cursor is currently shown. */
746 int hourglass_shown_p;
747
748 /* If non-null, an asynchronous timer that, when it expires, displays
749 an hourglass cursor on all frames. */
750 struct atimer *hourglass_atimer;
751
752 #endif /* HAVE_WINDOW_SYSTEM */
753
754 /* Name of the face used to display glyphless characters. */
755 Lisp_Object Qglyphless_char;
756
757 /* Symbol for the purpose of Vglyphless_char_display. */
758 static Lisp_Object Qglyphless_char_display;
759
760 /* Method symbols for Vglyphless_char_display. */
761 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
762
763 /* Default number of seconds to wait before displaying an hourglass
764 cursor. */
765 #define DEFAULT_HOURGLASS_DELAY 1
766
767 #ifdef HAVE_WINDOW_SYSTEM
768
769 /* Default pixel width of `thin-space' display method. */
770 #define THIN_SPACE_WIDTH 1
771
772 #endif /* HAVE_WINDOW_SYSTEM */
773
774 /* Function prototypes. */
775
776 static void setup_for_ellipsis (struct it *, int);
777 static void set_iterator_to_next (struct it *, int);
778 static void mark_window_display_accurate_1 (struct window *, int);
779 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
780 static int display_prop_string_p (Lisp_Object, Lisp_Object);
781 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
782 static int cursor_row_p (struct glyph_row *);
783 static int redisplay_mode_lines (Lisp_Object, int);
784 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
785
786 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
787
788 static void handle_line_prefix (struct it *);
789
790 static void pint2str (char *, int, ptrdiff_t);
791 static void pint2hrstr (char *, int, ptrdiff_t);
792 static struct text_pos run_window_scroll_functions (Lisp_Object,
793 struct text_pos);
794 static int text_outside_line_unchanged_p (struct window *,
795 ptrdiff_t, ptrdiff_t);
796 static void store_mode_line_noprop_char (char);
797 static int store_mode_line_noprop (const char *, int, int);
798 static void handle_stop (struct it *);
799 static void handle_stop_backwards (struct it *, ptrdiff_t);
800 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
801 static void ensure_echo_area_buffers (void);
802 static void unwind_with_echo_area_buffer (Lisp_Object);
803 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
804 static int with_echo_area_buffer (struct window *, int,
805 int (*) (ptrdiff_t, Lisp_Object),
806 ptrdiff_t, Lisp_Object);
807 static void clear_garbaged_frames (void);
808 static int current_message_1 (ptrdiff_t, Lisp_Object);
809 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
810 static void set_message (Lisp_Object);
811 static int set_message_1 (ptrdiff_t, Lisp_Object);
812 static int display_echo_area (struct window *);
813 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
814 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
815 static void unwind_redisplay (void);
816 static int string_char_and_length (const unsigned char *, int *);
817 static struct text_pos display_prop_end (struct it *, Lisp_Object,
818 struct text_pos);
819 static int compute_window_start_on_continuation_line (struct window *);
820 static void insert_left_trunc_glyphs (struct it *);
821 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
822 Lisp_Object);
823 static void extend_face_to_end_of_line (struct it *);
824 static int append_space_for_newline (struct it *, int);
825 static int cursor_row_fully_visible_p (struct window *, int, int);
826 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
827 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
828 static int trailing_whitespace_p (ptrdiff_t);
829 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
830 static void push_it (struct it *, struct text_pos *);
831 static void iterate_out_of_display_property (struct it *);
832 static void pop_it (struct it *);
833 static void sync_frame_with_window_matrix_rows (struct window *);
834 static void redisplay_internal (void);
835 static int echo_area_display (int);
836 static void redisplay_windows (Lisp_Object);
837 static void redisplay_window (Lisp_Object, int);
838 static Lisp_Object redisplay_window_error (Lisp_Object);
839 static Lisp_Object redisplay_window_0 (Lisp_Object);
840 static Lisp_Object redisplay_window_1 (Lisp_Object);
841 static int set_cursor_from_row (struct window *, struct glyph_row *,
842 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
843 int, int);
844 static int update_menu_bar (struct frame *, int, int);
845 static int try_window_reusing_current_matrix (struct window *);
846 static int try_window_id (struct window *);
847 static int display_line (struct it *);
848 static int display_mode_lines (struct window *);
849 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
850 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
851 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
852 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
853 static void display_menu_bar (struct window *);
854 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
855 ptrdiff_t *);
856 static int display_string (const char *, Lisp_Object, Lisp_Object,
857 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
858 static void compute_line_metrics (struct it *);
859 static void run_redisplay_end_trigger_hook (struct it *);
860 static int get_overlay_strings (struct it *, ptrdiff_t);
861 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
862 static void next_overlay_string (struct it *);
863 static void reseat (struct it *, struct text_pos, int);
864 static void reseat_1 (struct it *, struct text_pos, int);
865 static void back_to_previous_visible_line_start (struct it *);
866 static void reseat_at_next_visible_line_start (struct it *, int);
867 static int next_element_from_ellipsis (struct it *);
868 static int next_element_from_display_vector (struct it *);
869 static int next_element_from_string (struct it *);
870 static int next_element_from_c_string (struct it *);
871 static int next_element_from_buffer (struct it *);
872 static int next_element_from_composition (struct it *);
873 static int next_element_from_image (struct it *);
874 static int next_element_from_stretch (struct it *);
875 static void load_overlay_strings (struct it *, ptrdiff_t);
876 static int init_from_display_pos (struct it *, struct window *,
877 struct display_pos *);
878 static void reseat_to_string (struct it *, const char *,
879 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
880 static int get_next_display_element (struct it *);
881 static enum move_it_result
882 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
883 enum move_operation_enum);
884 static void get_visually_first_element (struct it *);
885 static void init_to_row_start (struct it *, struct window *,
886 struct glyph_row *);
887 static int init_to_row_end (struct it *, struct window *,
888 struct glyph_row *);
889 static void back_to_previous_line_start (struct it *);
890 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
891 static struct text_pos string_pos_nchars_ahead (struct text_pos,
892 Lisp_Object, ptrdiff_t);
893 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
894 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
895 static ptrdiff_t number_of_chars (const char *, bool);
896 static void compute_stop_pos (struct it *);
897 static void compute_string_pos (struct text_pos *, struct text_pos,
898 Lisp_Object);
899 static int face_before_or_after_it_pos (struct it *, int);
900 static ptrdiff_t next_overlay_change (ptrdiff_t);
901 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
902 Lisp_Object, struct text_pos *, ptrdiff_t, int);
903 static int handle_single_display_spec (struct it *, Lisp_Object,
904 Lisp_Object, Lisp_Object,
905 struct text_pos *, ptrdiff_t, int, int);
906 static int underlying_face_id (struct it *);
907 static int in_ellipses_for_invisible_text_p (struct display_pos *,
908 struct window *);
909
910 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
911 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
912
913 #ifdef HAVE_WINDOW_SYSTEM
914
915 static void x_consider_frame_title (Lisp_Object);
916 static int tool_bar_lines_needed (struct frame *, int *);
917 static void update_tool_bar (struct frame *, int);
918 static void build_desired_tool_bar_string (struct frame *f);
919 static int redisplay_tool_bar (struct frame *);
920 static void display_tool_bar_line (struct it *, int);
921 static void notice_overwritten_cursor (struct window *,
922 enum glyph_row_area,
923 int, int, int, int);
924 static void append_stretch_glyph (struct it *, Lisp_Object,
925 int, int, int);
926
927
928 #endif /* HAVE_WINDOW_SYSTEM */
929
930 static void produce_special_glyphs (struct it *, enum display_element_type);
931 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
932 static int coords_in_mouse_face_p (struct window *, int, int);
933
934
935 \f
936 /***********************************************************************
937 Window display dimensions
938 ***********************************************************************/
939
940 /* Return the bottom boundary y-position for text lines in window W.
941 This is the first y position at which a line cannot start.
942 It is relative to the top of the window.
943
944 This is the height of W minus the height of a mode line, if any. */
945
946 int
947 window_text_bottom_y (struct window *w)
948 {
949 int height = WINDOW_TOTAL_HEIGHT (w);
950
951 if (WINDOW_WANTS_MODELINE_P (w))
952 height -= CURRENT_MODE_LINE_HEIGHT (w);
953 return height;
954 }
955
956 /* Return the pixel width of display area AREA of window W.
957 ANY_AREA means return the total width of W, not including
958 fringes to the left and right of the window. */
959
960 int
961 window_box_width (struct window *w, enum glyph_row_area area)
962 {
963 int cols = w->total_cols;
964 int pixels = 0;
965
966 if (!w->pseudo_window_p)
967 {
968 cols -= WINDOW_SCROLL_BAR_COLS (w);
969
970 if (area == TEXT_AREA)
971 {
972 cols -= max (0, w->left_margin_cols);
973 cols -= max (0, w->right_margin_cols);
974 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
975 }
976 else if (area == LEFT_MARGIN_AREA)
977 {
978 cols = max (0, w->left_margin_cols);
979 pixels = 0;
980 }
981 else if (area == RIGHT_MARGIN_AREA)
982 {
983 cols = max (0, w->right_margin_cols);
984 pixels = 0;
985 }
986 }
987
988 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
989 }
990
991
992 /* Return the pixel height of the display area of window W, not
993 including mode lines of W, if any. */
994
995 int
996 window_box_height (struct window *w)
997 {
998 struct frame *f = XFRAME (w->frame);
999 int height = WINDOW_TOTAL_HEIGHT (w);
1000
1001 eassert (height >= 0);
1002
1003 /* Note: the code below that determines the mode-line/header-line
1004 height is essentially the same as that contained in the macro
1005 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1006 the appropriate glyph row has its `mode_line_p' flag set,
1007 and if it doesn't, uses estimate_mode_line_height instead. */
1008
1009 if (WINDOW_WANTS_MODELINE_P (w))
1010 {
1011 struct glyph_row *ml_row
1012 = (w->current_matrix && w->current_matrix->rows
1013 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1014 : 0);
1015 if (ml_row && ml_row->mode_line_p)
1016 height -= ml_row->height;
1017 else
1018 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1019 }
1020
1021 if (WINDOW_WANTS_HEADER_LINE_P (w))
1022 {
1023 struct glyph_row *hl_row
1024 = (w->current_matrix && w->current_matrix->rows
1025 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1026 : 0);
1027 if (hl_row && hl_row->mode_line_p)
1028 height -= hl_row->height;
1029 else
1030 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1031 }
1032
1033 /* With a very small font and a mode-line that's taller than
1034 default, we might end up with a negative height. */
1035 return max (0, height);
1036 }
1037
1038 /* Return the window-relative coordinate of the left edge of display
1039 area AREA of window W. ANY_AREA means return the left edge of the
1040 whole window, to the right of the left fringe of W. */
1041
1042 int
1043 window_box_left_offset (struct window *w, enum glyph_row_area area)
1044 {
1045 int x;
1046
1047 if (w->pseudo_window_p)
1048 return 0;
1049
1050 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1051
1052 if (area == TEXT_AREA)
1053 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1054 + window_box_width (w, LEFT_MARGIN_AREA));
1055 else if (area == RIGHT_MARGIN_AREA)
1056 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1057 + window_box_width (w, LEFT_MARGIN_AREA)
1058 + window_box_width (w, TEXT_AREA)
1059 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1060 ? 0
1061 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1062 else if (area == LEFT_MARGIN_AREA
1063 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1064 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1065
1066 return x;
1067 }
1068
1069
1070 /* Return the window-relative coordinate of the right edge of display
1071 area AREA of window W. ANY_AREA means return the right edge of the
1072 whole window, to the left of the right fringe of W. */
1073
1074 int
1075 window_box_right_offset (struct window *w, enum glyph_row_area area)
1076 {
1077 return window_box_left_offset (w, area) + window_box_width (w, area);
1078 }
1079
1080 /* Return the frame-relative coordinate of the left edge of display
1081 area AREA of window W. ANY_AREA means return the left edge of the
1082 whole window, to the right of the left fringe of W. */
1083
1084 int
1085 window_box_left (struct window *w, enum glyph_row_area area)
1086 {
1087 struct frame *f = XFRAME (w->frame);
1088 int x;
1089
1090 if (w->pseudo_window_p)
1091 return FRAME_INTERNAL_BORDER_WIDTH (f);
1092
1093 x = (WINDOW_LEFT_EDGE_X (w)
1094 + window_box_left_offset (w, area));
1095
1096 return x;
1097 }
1098
1099
1100 /* Return the frame-relative coordinate of the right edge of display
1101 area AREA of window W. ANY_AREA means return the right edge of the
1102 whole window, to the left of the right fringe of W. */
1103
1104 int
1105 window_box_right (struct window *w, enum glyph_row_area area)
1106 {
1107 return window_box_left (w, area) + window_box_width (w, area);
1108 }
1109
1110 /* Get the bounding box of the display area AREA of window W, without
1111 mode lines, in frame-relative coordinates. ANY_AREA means the
1112 whole window, not including the left and right fringes of
1113 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1114 coordinates of the upper-left corner of the box. Return in
1115 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1116
1117 void
1118 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1119 int *box_y, int *box_width, int *box_height)
1120 {
1121 if (box_width)
1122 *box_width = window_box_width (w, area);
1123 if (box_height)
1124 *box_height = window_box_height (w);
1125 if (box_x)
1126 *box_x = window_box_left (w, area);
1127 if (box_y)
1128 {
1129 *box_y = WINDOW_TOP_EDGE_Y (w);
1130 if (WINDOW_WANTS_HEADER_LINE_P (w))
1131 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1132 }
1133 }
1134
1135 #ifdef HAVE_WINDOW_SYSTEM
1136
1137 /* Get the bounding box of the display area AREA of window W, without
1138 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1139 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1140 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1141 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1142 box. */
1143
1144 static void
1145 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1146 int *bottom_right_x, int *bottom_right_y)
1147 {
1148 window_box (w, ANY_AREA, top_left_x, top_left_y,
1149 bottom_right_x, bottom_right_y);
1150 *bottom_right_x += *top_left_x;
1151 *bottom_right_y += *top_left_y;
1152 }
1153
1154 #endif /* HAVE_WINDOW_SYSTEM */
1155
1156 /***********************************************************************
1157 Utilities
1158 ***********************************************************************/
1159
1160 /* Return the bottom y-position of the line the iterator IT is in.
1161 This can modify IT's settings. */
1162
1163 int
1164 line_bottom_y (struct it *it)
1165 {
1166 int line_height = it->max_ascent + it->max_descent;
1167 int line_top_y = it->current_y;
1168
1169 if (line_height == 0)
1170 {
1171 if (last_height)
1172 line_height = last_height;
1173 else if (IT_CHARPOS (*it) < ZV)
1174 {
1175 move_it_by_lines (it, 1);
1176 line_height = (it->max_ascent || it->max_descent
1177 ? it->max_ascent + it->max_descent
1178 : last_height);
1179 }
1180 else
1181 {
1182 struct glyph_row *row = it->glyph_row;
1183
1184 /* Use the default character height. */
1185 it->glyph_row = NULL;
1186 it->what = IT_CHARACTER;
1187 it->c = ' ';
1188 it->len = 1;
1189 PRODUCE_GLYPHS (it);
1190 line_height = it->ascent + it->descent;
1191 it->glyph_row = row;
1192 }
1193 }
1194
1195 return line_top_y + line_height;
1196 }
1197
1198 DEFUN ("line-pixel-height", Fline_pixel_height,
1199 Sline_pixel_height, 0, 0, 0,
1200 doc: /* Return height in pixels of text line in the selected window.
1201
1202 Value is the height in pixels of the line at point. */)
1203 (void)
1204 {
1205 struct it it;
1206 struct text_pos pt;
1207 struct window *w = XWINDOW (selected_window);
1208
1209 SET_TEXT_POS (pt, PT, PT_BYTE);
1210 start_display (&it, w, pt);
1211 it.vpos = it.current_y = 0;
1212 last_height = 0;
1213 return make_number (line_bottom_y (&it));
1214 }
1215
1216 /* Return the default pixel height of text lines in window W. The
1217 value is the canonical height of the W frame's default font, plus
1218 any extra space required by the line-spacing variable or frame
1219 parameter.
1220
1221 Implementation note: this ignores any line-spacing text properties
1222 put on the newline characters. This is because those properties
1223 only affect the _screen_ line ending in the newline (i.e., in a
1224 continued line, only the last screen line will be affected), which
1225 means only a small number of lines in a buffer can ever use this
1226 feature. Since this function is used to compute the default pixel
1227 equivalent of text lines in a window, we can safely ignore those
1228 few lines. For the same reasons, we ignore the line-height
1229 properties. */
1230 int
1231 default_line_pixel_height (struct window *w)
1232 {
1233 struct frame *f = WINDOW_XFRAME (w);
1234 int height = FRAME_LINE_HEIGHT (f);
1235
1236 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1237 {
1238 struct buffer *b = XBUFFER (w->contents);
1239 Lisp_Object val = BVAR (b, extra_line_spacing);
1240
1241 if (NILP (val))
1242 val = BVAR (&buffer_defaults, extra_line_spacing);
1243 if (!NILP (val))
1244 {
1245 if (RANGED_INTEGERP (0, val, INT_MAX))
1246 height += XFASTINT (val);
1247 else if (FLOATP (val))
1248 {
1249 int addon = XFLOAT_DATA (val) * height + 0.5;
1250
1251 if (addon >= 0)
1252 height += addon;
1253 }
1254 }
1255 else
1256 height += f->extra_line_spacing;
1257 }
1258
1259 return height;
1260 }
1261
1262 /* Subroutine of pos_visible_p below. Extracts a display string, if
1263 any, from the display spec given as its argument. */
1264 static Lisp_Object
1265 string_from_display_spec (Lisp_Object spec)
1266 {
1267 if (CONSP (spec))
1268 {
1269 while (CONSP (spec))
1270 {
1271 if (STRINGP (XCAR (spec)))
1272 return XCAR (spec);
1273 spec = XCDR (spec);
1274 }
1275 }
1276 else if (VECTORP (spec))
1277 {
1278 ptrdiff_t i;
1279
1280 for (i = 0; i < ASIZE (spec); i++)
1281 {
1282 if (STRINGP (AREF (spec, i)))
1283 return AREF (spec, i);
1284 }
1285 return Qnil;
1286 }
1287
1288 return spec;
1289 }
1290
1291
1292 /* Limit insanely large values of W->hscroll on frame F to the largest
1293 value that will still prevent first_visible_x and last_visible_x of
1294 'struct it' from overflowing an int. */
1295 static int
1296 window_hscroll_limited (struct window *w, struct frame *f)
1297 {
1298 ptrdiff_t window_hscroll = w->hscroll;
1299 int window_text_width = window_box_width (w, TEXT_AREA);
1300 int colwidth = FRAME_COLUMN_WIDTH (f);
1301
1302 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1303 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1304
1305 return window_hscroll;
1306 }
1307
1308 /* Return 1 if position CHARPOS is visible in window W.
1309 CHARPOS < 0 means return info about WINDOW_END position.
1310 If visible, set *X and *Y to pixel coordinates of top left corner.
1311 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1312 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1313
1314 int
1315 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1316 int *rtop, int *rbot, int *rowh, int *vpos)
1317 {
1318 struct it it;
1319 void *itdata = bidi_shelve_cache ();
1320 struct text_pos top;
1321 int visible_p = 0;
1322 struct buffer *old_buffer = NULL;
1323
1324 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1325 return visible_p;
1326
1327 if (XBUFFER (w->contents) != current_buffer)
1328 {
1329 old_buffer = current_buffer;
1330 set_buffer_internal_1 (XBUFFER (w->contents));
1331 }
1332
1333 SET_TEXT_POS_FROM_MARKER (top, w->start);
1334 /* Scrolling a minibuffer window via scroll bar when the echo area
1335 shows long text sometimes resets the minibuffer contents behind
1336 our backs. */
1337 if (CHARPOS (top) > ZV)
1338 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1339
1340 /* Compute exact mode line heights. */
1341 if (WINDOW_WANTS_MODELINE_P (w))
1342 w->mode_line_height
1343 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1344 BVAR (current_buffer, mode_line_format));
1345
1346 if (WINDOW_WANTS_HEADER_LINE_P (w))
1347 w->header_line_height
1348 = display_mode_line (w, HEADER_LINE_FACE_ID,
1349 BVAR (current_buffer, header_line_format));
1350
1351 start_display (&it, w, top);
1352 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1353 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1354
1355 if (charpos >= 0
1356 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1357 && IT_CHARPOS (it) >= charpos)
1358 /* When scanning backwards under bidi iteration, move_it_to
1359 stops at or _before_ CHARPOS, because it stops at or to
1360 the _right_ of the character at CHARPOS. */
1361 || (it.bidi_p && it.bidi_it.scan_dir == -1
1362 && IT_CHARPOS (it) <= charpos)))
1363 {
1364 /* We have reached CHARPOS, or passed it. How the call to
1365 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1366 or covered by a display property, move_it_to stops at the end
1367 of the invisible text, to the right of CHARPOS. (ii) If
1368 CHARPOS is in a display vector, move_it_to stops on its last
1369 glyph. */
1370 int top_x = it.current_x;
1371 int top_y = it.current_y;
1372 /* Calling line_bottom_y may change it.method, it.position, etc. */
1373 enum it_method it_method = it.method;
1374 int bottom_y = (last_height = 0, line_bottom_y (&it));
1375 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1376
1377 if (top_y < window_top_y)
1378 visible_p = bottom_y > window_top_y;
1379 else if (top_y < it.last_visible_y)
1380 visible_p = 1;
1381 if (bottom_y >= it.last_visible_y
1382 && it.bidi_p && it.bidi_it.scan_dir == -1
1383 && IT_CHARPOS (it) < charpos)
1384 {
1385 /* When the last line of the window is scanned backwards
1386 under bidi iteration, we could be duped into thinking
1387 that we have passed CHARPOS, when in fact move_it_to
1388 simply stopped short of CHARPOS because it reached
1389 last_visible_y. To see if that's what happened, we call
1390 move_it_to again with a slightly larger vertical limit,
1391 and see if it actually moved vertically; if it did, we
1392 didn't really reach CHARPOS, which is beyond window end. */
1393 struct it save_it = it;
1394 /* Why 10? because we don't know how many canonical lines
1395 will the height of the next line(s) be. So we guess. */
1396 int ten_more_lines = 10 * default_line_pixel_height (w);
1397
1398 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1399 MOVE_TO_POS | MOVE_TO_Y);
1400 if (it.current_y > top_y)
1401 visible_p = 0;
1402
1403 it = save_it;
1404 }
1405 if (visible_p)
1406 {
1407 if (it_method == GET_FROM_DISPLAY_VECTOR)
1408 {
1409 /* We stopped on the last glyph of a display vector.
1410 Try and recompute. Hack alert! */
1411 if (charpos < 2 || top.charpos >= charpos)
1412 top_x = it.glyph_row->x;
1413 else
1414 {
1415 struct it it2, it2_prev;
1416 /* The idea is to get to the previous buffer
1417 position, consume the character there, and use
1418 the pixel coordinates we get after that. But if
1419 the previous buffer position is also displayed
1420 from a display vector, we need to consume all of
1421 the glyphs from that display vector. */
1422 start_display (&it2, w, top);
1423 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1424 /* If we didn't get to CHARPOS - 1, there's some
1425 replacing display property at that position, and
1426 we stopped after it. That is exactly the place
1427 whose coordinates we want. */
1428 if (IT_CHARPOS (it2) != charpos - 1)
1429 it2_prev = it2;
1430 else
1431 {
1432 /* Iterate until we get out of the display
1433 vector that displays the character at
1434 CHARPOS - 1. */
1435 do {
1436 get_next_display_element (&it2);
1437 PRODUCE_GLYPHS (&it2);
1438 it2_prev = it2;
1439 set_iterator_to_next (&it2, 1);
1440 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1441 && IT_CHARPOS (it2) < charpos);
1442 }
1443 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1444 || it2_prev.current_x > it2_prev.last_visible_x)
1445 top_x = it.glyph_row->x;
1446 else
1447 {
1448 top_x = it2_prev.current_x;
1449 top_y = it2_prev.current_y;
1450 }
1451 }
1452 }
1453 else if (IT_CHARPOS (it) != charpos)
1454 {
1455 Lisp_Object cpos = make_number (charpos);
1456 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1457 Lisp_Object string = string_from_display_spec (spec);
1458 struct text_pos tpos;
1459 int replacing_spec_p;
1460 bool newline_in_string
1461 = (STRINGP (string)
1462 && memchr (SDATA (string), '\n', SBYTES (string)));
1463
1464 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1465 replacing_spec_p
1466 = (!NILP (spec)
1467 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1468 charpos, FRAME_WINDOW_P (it.f)));
1469 /* The tricky code below is needed because there's a
1470 discrepancy between move_it_to and how we set cursor
1471 when PT is at the beginning of a portion of text
1472 covered by a display property or an overlay with a
1473 display property, or the display line ends in a
1474 newline from a display string. move_it_to will stop
1475 _after_ such display strings, whereas
1476 set_cursor_from_row conspires with cursor_row_p to
1477 place the cursor on the first glyph produced from the
1478 display string. */
1479
1480 /* We have overshoot PT because it is covered by a
1481 display property that replaces the text it covers.
1482 If the string includes embedded newlines, we are also
1483 in the wrong display line. Backtrack to the correct
1484 line, where the display property begins. */
1485 if (replacing_spec_p)
1486 {
1487 Lisp_Object startpos, endpos;
1488 EMACS_INT start, end;
1489 struct it it3;
1490 int it3_moved;
1491
1492 /* Find the first and the last buffer positions
1493 covered by the display string. */
1494 endpos =
1495 Fnext_single_char_property_change (cpos, Qdisplay,
1496 Qnil, Qnil);
1497 startpos =
1498 Fprevious_single_char_property_change (endpos, Qdisplay,
1499 Qnil, Qnil);
1500 start = XFASTINT (startpos);
1501 end = XFASTINT (endpos);
1502 /* Move to the last buffer position before the
1503 display property. */
1504 start_display (&it3, w, top);
1505 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1506 /* Move forward one more line if the position before
1507 the display string is a newline or if it is the
1508 rightmost character on a line that is
1509 continued or word-wrapped. */
1510 if (it3.method == GET_FROM_BUFFER
1511 && (it3.c == '\n'
1512 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1513 move_it_by_lines (&it3, 1);
1514 else if (move_it_in_display_line_to (&it3, -1,
1515 it3.current_x
1516 + it3.pixel_width,
1517 MOVE_TO_X)
1518 == MOVE_LINE_CONTINUED)
1519 {
1520 move_it_by_lines (&it3, 1);
1521 /* When we are under word-wrap, the #$@%!
1522 move_it_by_lines moves 2 lines, so we need to
1523 fix that up. */
1524 if (it3.line_wrap == WORD_WRAP)
1525 move_it_by_lines (&it3, -1);
1526 }
1527
1528 /* Record the vertical coordinate of the display
1529 line where we wound up. */
1530 top_y = it3.current_y;
1531 if (it3.bidi_p)
1532 {
1533 /* When characters are reordered for display,
1534 the character displayed to the left of the
1535 display string could be _after_ the display
1536 property in the logical order. Use the
1537 smallest vertical position of these two. */
1538 start_display (&it3, w, top);
1539 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1540 if (it3.current_y < top_y)
1541 top_y = it3.current_y;
1542 }
1543 /* Move from the top of the window to the beginning
1544 of the display line where the display string
1545 begins. */
1546 start_display (&it3, w, top);
1547 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1548 /* If it3_moved stays zero after the 'while' loop
1549 below, that means we already were at a newline
1550 before the loop (e.g., the display string begins
1551 with a newline), so we don't need to (and cannot)
1552 inspect the glyphs of it3.glyph_row, because
1553 PRODUCE_GLYPHS will not produce anything for a
1554 newline, and thus it3.glyph_row stays at its
1555 stale content it got at top of the window. */
1556 it3_moved = 0;
1557 /* Finally, advance the iterator until we hit the
1558 first display element whose character position is
1559 CHARPOS, or until the first newline from the
1560 display string, which signals the end of the
1561 display line. */
1562 while (get_next_display_element (&it3))
1563 {
1564 PRODUCE_GLYPHS (&it3);
1565 if (IT_CHARPOS (it3) == charpos
1566 || ITERATOR_AT_END_OF_LINE_P (&it3))
1567 break;
1568 it3_moved = 1;
1569 set_iterator_to_next (&it3, 0);
1570 }
1571 top_x = it3.current_x - it3.pixel_width;
1572 /* Normally, we would exit the above loop because we
1573 found the display element whose character
1574 position is CHARPOS. For the contingency that we
1575 didn't, and stopped at the first newline from the
1576 display string, move back over the glyphs
1577 produced from the string, until we find the
1578 rightmost glyph not from the string. */
1579 if (it3_moved
1580 && newline_in_string
1581 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1582 {
1583 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1584 + it3.glyph_row->used[TEXT_AREA];
1585
1586 while (EQ ((g - 1)->object, string))
1587 {
1588 --g;
1589 top_x -= g->pixel_width;
1590 }
1591 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1592 + it3.glyph_row->used[TEXT_AREA]);
1593 }
1594 }
1595 }
1596
1597 *x = top_x;
1598 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1599 *rtop = max (0, window_top_y - top_y);
1600 *rbot = max (0, bottom_y - it.last_visible_y);
1601 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1602 - max (top_y, window_top_y)));
1603 *vpos = it.vpos;
1604 }
1605 }
1606 else
1607 {
1608 /* We were asked to provide info about WINDOW_END. */
1609 struct it it2;
1610 void *it2data = NULL;
1611
1612 SAVE_IT (it2, it, it2data);
1613 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1614 move_it_by_lines (&it, 1);
1615 if (charpos < IT_CHARPOS (it)
1616 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1617 {
1618 visible_p = 1;
1619 RESTORE_IT (&it2, &it2, it2data);
1620 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1621 *x = it2.current_x;
1622 *y = it2.current_y + it2.max_ascent - it2.ascent;
1623 *rtop = max (0, -it2.current_y);
1624 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1625 - it.last_visible_y));
1626 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1627 it.last_visible_y)
1628 - max (it2.current_y,
1629 WINDOW_HEADER_LINE_HEIGHT (w))));
1630 *vpos = it2.vpos;
1631 }
1632 else
1633 bidi_unshelve_cache (it2data, 1);
1634 }
1635 bidi_unshelve_cache (itdata, 0);
1636
1637 if (old_buffer)
1638 set_buffer_internal_1 (old_buffer);
1639
1640 if (visible_p && w->hscroll > 0)
1641 *x -=
1642 window_hscroll_limited (w, WINDOW_XFRAME (w))
1643 * WINDOW_FRAME_COLUMN_WIDTH (w);
1644
1645 #if 0
1646 /* Debugging code. */
1647 if (visible_p)
1648 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1649 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1650 else
1651 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1652 #endif
1653
1654 return visible_p;
1655 }
1656
1657
1658 /* Return the next character from STR. Return in *LEN the length of
1659 the character. This is like STRING_CHAR_AND_LENGTH but never
1660 returns an invalid character. If we find one, we return a `?', but
1661 with the length of the invalid character. */
1662
1663 static int
1664 string_char_and_length (const unsigned char *str, int *len)
1665 {
1666 int c;
1667
1668 c = STRING_CHAR_AND_LENGTH (str, *len);
1669 if (!CHAR_VALID_P (c))
1670 /* We may not change the length here because other places in Emacs
1671 don't use this function, i.e. they silently accept invalid
1672 characters. */
1673 c = '?';
1674
1675 return c;
1676 }
1677
1678
1679
1680 /* Given a position POS containing a valid character and byte position
1681 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1682
1683 static struct text_pos
1684 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1685 {
1686 eassert (STRINGP (string) && nchars >= 0);
1687
1688 if (STRING_MULTIBYTE (string))
1689 {
1690 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1691 int len;
1692
1693 while (nchars--)
1694 {
1695 string_char_and_length (p, &len);
1696 p += len;
1697 CHARPOS (pos) += 1;
1698 BYTEPOS (pos) += len;
1699 }
1700 }
1701 else
1702 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1703
1704 return pos;
1705 }
1706
1707
1708 /* Value is the text position, i.e. character and byte position,
1709 for character position CHARPOS in STRING. */
1710
1711 static struct text_pos
1712 string_pos (ptrdiff_t charpos, Lisp_Object string)
1713 {
1714 struct text_pos pos;
1715 eassert (STRINGP (string));
1716 eassert (charpos >= 0);
1717 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1718 return pos;
1719 }
1720
1721
1722 /* Value is a text position, i.e. character and byte position, for
1723 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1724 means recognize multibyte characters. */
1725
1726 static struct text_pos
1727 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1728 {
1729 struct text_pos pos;
1730
1731 eassert (s != NULL);
1732 eassert (charpos >= 0);
1733
1734 if (multibyte_p)
1735 {
1736 int len;
1737
1738 SET_TEXT_POS (pos, 0, 0);
1739 while (charpos--)
1740 {
1741 string_char_and_length ((const unsigned char *) s, &len);
1742 s += len;
1743 CHARPOS (pos) += 1;
1744 BYTEPOS (pos) += len;
1745 }
1746 }
1747 else
1748 SET_TEXT_POS (pos, charpos, charpos);
1749
1750 return pos;
1751 }
1752
1753
1754 /* Value is the number of characters in C string S. MULTIBYTE_P
1755 non-zero means recognize multibyte characters. */
1756
1757 static ptrdiff_t
1758 number_of_chars (const char *s, bool multibyte_p)
1759 {
1760 ptrdiff_t nchars;
1761
1762 if (multibyte_p)
1763 {
1764 ptrdiff_t rest = strlen (s);
1765 int len;
1766 const unsigned char *p = (const unsigned char *) s;
1767
1768 for (nchars = 0; rest > 0; ++nchars)
1769 {
1770 string_char_and_length (p, &len);
1771 rest -= len, p += len;
1772 }
1773 }
1774 else
1775 nchars = strlen (s);
1776
1777 return nchars;
1778 }
1779
1780
1781 /* Compute byte position NEWPOS->bytepos corresponding to
1782 NEWPOS->charpos. POS is a known position in string STRING.
1783 NEWPOS->charpos must be >= POS.charpos. */
1784
1785 static void
1786 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1787 {
1788 eassert (STRINGP (string));
1789 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1790
1791 if (STRING_MULTIBYTE (string))
1792 *newpos = string_pos_nchars_ahead (pos, string,
1793 CHARPOS (*newpos) - CHARPOS (pos));
1794 else
1795 BYTEPOS (*newpos) = CHARPOS (*newpos);
1796 }
1797
1798 /* EXPORT:
1799 Return an estimation of the pixel height of mode or header lines on
1800 frame F. FACE_ID specifies what line's height to estimate. */
1801
1802 int
1803 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1804 {
1805 #ifdef HAVE_WINDOW_SYSTEM
1806 if (FRAME_WINDOW_P (f))
1807 {
1808 int height = FONT_HEIGHT (FRAME_FONT (f));
1809
1810 /* This function is called so early when Emacs starts that the face
1811 cache and mode line face are not yet initialized. */
1812 if (FRAME_FACE_CACHE (f))
1813 {
1814 struct face *face = FACE_FROM_ID (f, face_id);
1815 if (face)
1816 {
1817 if (face->font)
1818 height = FONT_HEIGHT (face->font);
1819 if (face->box_line_width > 0)
1820 height += 2 * face->box_line_width;
1821 }
1822 }
1823
1824 return height;
1825 }
1826 #endif
1827
1828 return 1;
1829 }
1830
1831 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1832 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1833 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1834 not force the value into range. */
1835
1836 void
1837 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1838 int *x, int *y, NativeRectangle *bounds, int noclip)
1839 {
1840
1841 #ifdef HAVE_WINDOW_SYSTEM
1842 if (FRAME_WINDOW_P (f))
1843 {
1844 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1845 even for negative values. */
1846 if (pix_x < 0)
1847 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1848 if (pix_y < 0)
1849 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1850
1851 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1852 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1853
1854 if (bounds)
1855 STORE_NATIVE_RECT (*bounds,
1856 FRAME_COL_TO_PIXEL_X (f, pix_x),
1857 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1858 FRAME_COLUMN_WIDTH (f) - 1,
1859 FRAME_LINE_HEIGHT (f) - 1);
1860
1861 if (!noclip)
1862 {
1863 if (pix_x < 0)
1864 pix_x = 0;
1865 else if (pix_x > FRAME_TOTAL_COLS (f))
1866 pix_x = FRAME_TOTAL_COLS (f);
1867
1868 if (pix_y < 0)
1869 pix_y = 0;
1870 else if (pix_y > FRAME_LINES (f))
1871 pix_y = FRAME_LINES (f);
1872 }
1873 }
1874 #endif
1875
1876 *x = pix_x;
1877 *y = pix_y;
1878 }
1879
1880
1881 /* Find the glyph under window-relative coordinates X/Y in window W.
1882 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1883 strings. Return in *HPOS and *VPOS the row and column number of
1884 the glyph found. Return in *AREA the glyph area containing X.
1885 Value is a pointer to the glyph found or null if X/Y is not on
1886 text, or we can't tell because W's current matrix is not up to
1887 date. */
1888
1889 static
1890 struct glyph *
1891 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1892 int *dx, int *dy, int *area)
1893 {
1894 struct glyph *glyph, *end;
1895 struct glyph_row *row = NULL;
1896 int x0, i;
1897
1898 /* Find row containing Y. Give up if some row is not enabled. */
1899 for (i = 0; i < w->current_matrix->nrows; ++i)
1900 {
1901 row = MATRIX_ROW (w->current_matrix, i);
1902 if (!row->enabled_p)
1903 return NULL;
1904 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1905 break;
1906 }
1907
1908 *vpos = i;
1909 *hpos = 0;
1910
1911 /* Give up if Y is not in the window. */
1912 if (i == w->current_matrix->nrows)
1913 return NULL;
1914
1915 /* Get the glyph area containing X. */
1916 if (w->pseudo_window_p)
1917 {
1918 *area = TEXT_AREA;
1919 x0 = 0;
1920 }
1921 else
1922 {
1923 if (x < window_box_left_offset (w, TEXT_AREA))
1924 {
1925 *area = LEFT_MARGIN_AREA;
1926 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1927 }
1928 else if (x < window_box_right_offset (w, TEXT_AREA))
1929 {
1930 *area = TEXT_AREA;
1931 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1932 }
1933 else
1934 {
1935 *area = RIGHT_MARGIN_AREA;
1936 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1937 }
1938 }
1939
1940 /* Find glyph containing X. */
1941 glyph = row->glyphs[*area];
1942 end = glyph + row->used[*area];
1943 x -= x0;
1944 while (glyph < end && x >= glyph->pixel_width)
1945 {
1946 x -= glyph->pixel_width;
1947 ++glyph;
1948 }
1949
1950 if (glyph == end)
1951 return NULL;
1952
1953 if (dx)
1954 {
1955 *dx = x;
1956 *dy = y - (row->y + row->ascent - glyph->ascent);
1957 }
1958
1959 *hpos = glyph - row->glyphs[*area];
1960 return glyph;
1961 }
1962
1963 /* Convert frame-relative x/y to coordinates relative to window W.
1964 Takes pseudo-windows into account. */
1965
1966 static void
1967 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1968 {
1969 if (w->pseudo_window_p)
1970 {
1971 /* A pseudo-window is always full-width, and starts at the
1972 left edge of the frame, plus a frame border. */
1973 struct frame *f = XFRAME (w->frame);
1974 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1975 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1976 }
1977 else
1978 {
1979 *x -= WINDOW_LEFT_EDGE_X (w);
1980 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1981 }
1982 }
1983
1984 #ifdef HAVE_WINDOW_SYSTEM
1985
1986 /* EXPORT:
1987 Return in RECTS[] at most N clipping rectangles for glyph string S.
1988 Return the number of stored rectangles. */
1989
1990 int
1991 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1992 {
1993 XRectangle r;
1994
1995 if (n <= 0)
1996 return 0;
1997
1998 if (s->row->full_width_p)
1999 {
2000 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2001 r.x = WINDOW_LEFT_EDGE_X (s->w);
2002 r.width = WINDOW_TOTAL_WIDTH (s->w);
2003
2004 /* Unless displaying a mode or menu bar line, which are always
2005 fully visible, clip to the visible part of the row. */
2006 if (s->w->pseudo_window_p)
2007 r.height = s->row->visible_height;
2008 else
2009 r.height = s->height;
2010 }
2011 else
2012 {
2013 /* This is a text line that may be partially visible. */
2014 r.x = window_box_left (s->w, s->area);
2015 r.width = window_box_width (s->w, s->area);
2016 r.height = s->row->visible_height;
2017 }
2018
2019 if (s->clip_head)
2020 if (r.x < s->clip_head->x)
2021 {
2022 if (r.width >= s->clip_head->x - r.x)
2023 r.width -= s->clip_head->x - r.x;
2024 else
2025 r.width = 0;
2026 r.x = s->clip_head->x;
2027 }
2028 if (s->clip_tail)
2029 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2030 {
2031 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2032 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2033 else
2034 r.width = 0;
2035 }
2036
2037 /* If S draws overlapping rows, it's sufficient to use the top and
2038 bottom of the window for clipping because this glyph string
2039 intentionally draws over other lines. */
2040 if (s->for_overlaps)
2041 {
2042 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2043 r.height = window_text_bottom_y (s->w) - r.y;
2044
2045 /* Alas, the above simple strategy does not work for the
2046 environments with anti-aliased text: if the same text is
2047 drawn onto the same place multiple times, it gets thicker.
2048 If the overlap we are processing is for the erased cursor, we
2049 take the intersection with the rectangle of the cursor. */
2050 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2051 {
2052 XRectangle rc, r_save = r;
2053
2054 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2055 rc.y = s->w->phys_cursor.y;
2056 rc.width = s->w->phys_cursor_width;
2057 rc.height = s->w->phys_cursor_height;
2058
2059 x_intersect_rectangles (&r_save, &rc, &r);
2060 }
2061 }
2062 else
2063 {
2064 /* Don't use S->y for clipping because it doesn't take partially
2065 visible lines into account. For example, it can be negative for
2066 partially visible lines at the top of a window. */
2067 if (!s->row->full_width_p
2068 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2069 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2070 else
2071 r.y = max (0, s->row->y);
2072 }
2073
2074 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2075
2076 /* If drawing the cursor, don't let glyph draw outside its
2077 advertised boundaries. Cleartype does this under some circumstances. */
2078 if (s->hl == DRAW_CURSOR)
2079 {
2080 struct glyph *glyph = s->first_glyph;
2081 int height, max_y;
2082
2083 if (s->x > r.x)
2084 {
2085 r.width -= s->x - r.x;
2086 r.x = s->x;
2087 }
2088 r.width = min (r.width, glyph->pixel_width);
2089
2090 /* If r.y is below window bottom, ensure that we still see a cursor. */
2091 height = min (glyph->ascent + glyph->descent,
2092 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2093 max_y = window_text_bottom_y (s->w) - height;
2094 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2095 if (s->ybase - glyph->ascent > max_y)
2096 {
2097 r.y = max_y;
2098 r.height = height;
2099 }
2100 else
2101 {
2102 /* Don't draw cursor glyph taller than our actual glyph. */
2103 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2104 if (height < r.height)
2105 {
2106 max_y = r.y + r.height;
2107 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2108 r.height = min (max_y - r.y, height);
2109 }
2110 }
2111 }
2112
2113 if (s->row->clip)
2114 {
2115 XRectangle r_save = r;
2116
2117 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2118 r.width = 0;
2119 }
2120
2121 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2122 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2123 {
2124 #ifdef CONVERT_FROM_XRECT
2125 CONVERT_FROM_XRECT (r, *rects);
2126 #else
2127 *rects = r;
2128 #endif
2129 return 1;
2130 }
2131 else
2132 {
2133 /* If we are processing overlapping and allowed to return
2134 multiple clipping rectangles, we exclude the row of the glyph
2135 string from the clipping rectangle. This is to avoid drawing
2136 the same text on the environment with anti-aliasing. */
2137 #ifdef CONVERT_FROM_XRECT
2138 XRectangle rs[2];
2139 #else
2140 XRectangle *rs = rects;
2141 #endif
2142 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2143
2144 if (s->for_overlaps & OVERLAPS_PRED)
2145 {
2146 rs[i] = r;
2147 if (r.y + r.height > row_y)
2148 {
2149 if (r.y < row_y)
2150 rs[i].height = row_y - r.y;
2151 else
2152 rs[i].height = 0;
2153 }
2154 i++;
2155 }
2156 if (s->for_overlaps & OVERLAPS_SUCC)
2157 {
2158 rs[i] = r;
2159 if (r.y < row_y + s->row->visible_height)
2160 {
2161 if (r.y + r.height > row_y + s->row->visible_height)
2162 {
2163 rs[i].y = row_y + s->row->visible_height;
2164 rs[i].height = r.y + r.height - rs[i].y;
2165 }
2166 else
2167 rs[i].height = 0;
2168 }
2169 i++;
2170 }
2171
2172 n = i;
2173 #ifdef CONVERT_FROM_XRECT
2174 for (i = 0; i < n; i++)
2175 CONVERT_FROM_XRECT (rs[i], rects[i]);
2176 #endif
2177 return n;
2178 }
2179 }
2180
2181 /* EXPORT:
2182 Return in *NR the clipping rectangle for glyph string S. */
2183
2184 void
2185 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2186 {
2187 get_glyph_string_clip_rects (s, nr, 1);
2188 }
2189
2190
2191 /* EXPORT:
2192 Return the position and height of the phys cursor in window W.
2193 Set w->phys_cursor_width to width of phys cursor.
2194 */
2195
2196 void
2197 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2198 struct glyph *glyph, int *xp, int *yp, int *heightp)
2199 {
2200 struct frame *f = XFRAME (WINDOW_FRAME (w));
2201 int x, y, wd, h, h0, y0;
2202
2203 /* Compute the width of the rectangle to draw. If on a stretch
2204 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2205 rectangle as wide as the glyph, but use a canonical character
2206 width instead. */
2207 wd = glyph->pixel_width - 1;
2208 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2209 wd++; /* Why? */
2210 #endif
2211
2212 x = w->phys_cursor.x;
2213 if (x < 0)
2214 {
2215 wd += x;
2216 x = 0;
2217 }
2218
2219 if (glyph->type == STRETCH_GLYPH
2220 && !x_stretch_cursor_p)
2221 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2222 w->phys_cursor_width = wd;
2223
2224 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2225
2226 /* If y is below window bottom, ensure that we still see a cursor. */
2227 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2228
2229 h = max (h0, glyph->ascent + glyph->descent);
2230 h0 = min (h0, glyph->ascent + glyph->descent);
2231
2232 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2233 if (y < y0)
2234 {
2235 h = max (h - (y0 - y) + 1, h0);
2236 y = y0 - 1;
2237 }
2238 else
2239 {
2240 y0 = window_text_bottom_y (w) - h0;
2241 if (y > y0)
2242 {
2243 h += y - y0;
2244 y = y0;
2245 }
2246 }
2247
2248 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2249 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2250 *heightp = h;
2251 }
2252
2253 /*
2254 * Remember which glyph the mouse is over.
2255 */
2256
2257 void
2258 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2259 {
2260 Lisp_Object window;
2261 struct window *w;
2262 struct glyph_row *r, *gr, *end_row;
2263 enum window_part part;
2264 enum glyph_row_area area;
2265 int x, y, width, height;
2266
2267 /* Try to determine frame pixel position and size of the glyph under
2268 frame pixel coordinates X/Y on frame F. */
2269
2270 if (!f->glyphs_initialized_p
2271 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2272 NILP (window)))
2273 {
2274 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2275 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2276 goto virtual_glyph;
2277 }
2278
2279 w = XWINDOW (window);
2280 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2281 height = WINDOW_FRAME_LINE_HEIGHT (w);
2282
2283 x = window_relative_x_coord (w, part, gx);
2284 y = gy - WINDOW_TOP_EDGE_Y (w);
2285
2286 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2287 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2288
2289 if (w->pseudo_window_p)
2290 {
2291 area = TEXT_AREA;
2292 part = ON_MODE_LINE; /* Don't adjust margin. */
2293 goto text_glyph;
2294 }
2295
2296 switch (part)
2297 {
2298 case ON_LEFT_MARGIN:
2299 area = LEFT_MARGIN_AREA;
2300 goto text_glyph;
2301
2302 case ON_RIGHT_MARGIN:
2303 area = RIGHT_MARGIN_AREA;
2304 goto text_glyph;
2305
2306 case ON_HEADER_LINE:
2307 case ON_MODE_LINE:
2308 gr = (part == ON_HEADER_LINE
2309 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2310 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2311 gy = gr->y;
2312 area = TEXT_AREA;
2313 goto text_glyph_row_found;
2314
2315 case ON_TEXT:
2316 area = TEXT_AREA;
2317
2318 text_glyph:
2319 gr = 0; gy = 0;
2320 for (; r <= end_row && r->enabled_p; ++r)
2321 if (r->y + r->height > y)
2322 {
2323 gr = r; gy = r->y;
2324 break;
2325 }
2326
2327 text_glyph_row_found:
2328 if (gr && gy <= y)
2329 {
2330 struct glyph *g = gr->glyphs[area];
2331 struct glyph *end = g + gr->used[area];
2332
2333 height = gr->height;
2334 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2335 if (gx + g->pixel_width > x)
2336 break;
2337
2338 if (g < end)
2339 {
2340 if (g->type == IMAGE_GLYPH)
2341 {
2342 /* Don't remember when mouse is over image, as
2343 image may have hot-spots. */
2344 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2345 return;
2346 }
2347 width = g->pixel_width;
2348 }
2349 else
2350 {
2351 /* Use nominal char spacing at end of line. */
2352 x -= gx;
2353 gx += (x / width) * width;
2354 }
2355
2356 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2357 gx += window_box_left_offset (w, area);
2358 }
2359 else
2360 {
2361 /* Use nominal line height at end of window. */
2362 gx = (x / width) * width;
2363 y -= gy;
2364 gy += (y / height) * height;
2365 }
2366 break;
2367
2368 case ON_LEFT_FRINGE:
2369 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2370 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2371 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2372 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2373 goto row_glyph;
2374
2375 case ON_RIGHT_FRINGE:
2376 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2377 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2378 : window_box_right_offset (w, TEXT_AREA));
2379 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2380 goto row_glyph;
2381
2382 case ON_SCROLL_BAR:
2383 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2384 ? 0
2385 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2386 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2387 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2388 : 0)));
2389 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2390
2391 row_glyph:
2392 gr = 0, gy = 0;
2393 for (; r <= end_row && r->enabled_p; ++r)
2394 if (r->y + r->height > y)
2395 {
2396 gr = r; gy = r->y;
2397 break;
2398 }
2399
2400 if (gr && gy <= y)
2401 height = gr->height;
2402 else
2403 {
2404 /* Use nominal line height at end of window. */
2405 y -= gy;
2406 gy += (y / height) * height;
2407 }
2408 break;
2409
2410 default:
2411 ;
2412 virtual_glyph:
2413 /* If there is no glyph under the mouse, then we divide the screen
2414 into a grid of the smallest glyph in the frame, and use that
2415 as our "glyph". */
2416
2417 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2418 round down even for negative values. */
2419 if (gx < 0)
2420 gx -= width - 1;
2421 if (gy < 0)
2422 gy -= height - 1;
2423
2424 gx = (gx / width) * width;
2425 gy = (gy / height) * height;
2426
2427 goto store_rect;
2428 }
2429
2430 gx += WINDOW_LEFT_EDGE_X (w);
2431 gy += WINDOW_TOP_EDGE_Y (w);
2432
2433 store_rect:
2434 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2435
2436 /* Visible feedback for debugging. */
2437 #if 0
2438 #if HAVE_X_WINDOWS
2439 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2440 f->output_data.x->normal_gc,
2441 gx, gy, width, height);
2442 #endif
2443 #endif
2444 }
2445
2446
2447 #endif /* HAVE_WINDOW_SYSTEM */
2448
2449 static void
2450 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2451 {
2452 eassert (w);
2453 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2454 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2455 w->window_end_vpos
2456 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2457 }
2458
2459 /***********************************************************************
2460 Lisp form evaluation
2461 ***********************************************************************/
2462
2463 /* Error handler for safe_eval and safe_call. */
2464
2465 static Lisp_Object
2466 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2467 {
2468 add_to_log ("Error during redisplay: %S signaled %S",
2469 Flist (nargs, args), arg);
2470 return Qnil;
2471 }
2472
2473 /* Call function FUNC with the rest of NARGS - 1 arguments
2474 following. Return the result, or nil if something went
2475 wrong. Prevent redisplay during the evaluation. */
2476
2477 Lisp_Object
2478 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2479 {
2480 Lisp_Object val;
2481
2482 if (inhibit_eval_during_redisplay)
2483 val = Qnil;
2484 else
2485 {
2486 va_list ap;
2487 ptrdiff_t i;
2488 ptrdiff_t count = SPECPDL_INDEX ();
2489 struct gcpro gcpro1;
2490 Lisp_Object *args = alloca (nargs * word_size);
2491
2492 args[0] = func;
2493 va_start (ap, func);
2494 for (i = 1; i < nargs; i++)
2495 args[i] = va_arg (ap, Lisp_Object);
2496 va_end (ap);
2497
2498 GCPRO1 (args[0]);
2499 gcpro1.nvars = nargs;
2500 specbind (Qinhibit_redisplay, Qt);
2501 /* Use Qt to ensure debugger does not run,
2502 so there is no possibility of wanting to redisplay. */
2503 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2504 safe_eval_handler);
2505 UNGCPRO;
2506 val = unbind_to (count, val);
2507 }
2508
2509 return val;
2510 }
2511
2512
2513 /* Call function FN with one argument ARG.
2514 Return the result, or nil if something went wrong. */
2515
2516 Lisp_Object
2517 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2518 {
2519 return safe_call (2, fn, arg);
2520 }
2521
2522 static Lisp_Object Qeval;
2523
2524 Lisp_Object
2525 safe_eval (Lisp_Object sexpr)
2526 {
2527 return safe_call1 (Qeval, sexpr);
2528 }
2529
2530 /* Call function FN with two arguments ARG1 and ARG2.
2531 Return the result, or nil if something went wrong. */
2532
2533 Lisp_Object
2534 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2535 {
2536 return safe_call (3, fn, arg1, arg2);
2537 }
2538
2539
2540 \f
2541 /***********************************************************************
2542 Debugging
2543 ***********************************************************************/
2544
2545 #if 0
2546
2547 /* Define CHECK_IT to perform sanity checks on iterators.
2548 This is for debugging. It is too slow to do unconditionally. */
2549
2550 static void
2551 check_it (struct it *it)
2552 {
2553 if (it->method == GET_FROM_STRING)
2554 {
2555 eassert (STRINGP (it->string));
2556 eassert (IT_STRING_CHARPOS (*it) >= 0);
2557 }
2558 else
2559 {
2560 eassert (IT_STRING_CHARPOS (*it) < 0);
2561 if (it->method == GET_FROM_BUFFER)
2562 {
2563 /* Check that character and byte positions agree. */
2564 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2565 }
2566 }
2567
2568 if (it->dpvec)
2569 eassert (it->current.dpvec_index >= 0);
2570 else
2571 eassert (it->current.dpvec_index < 0);
2572 }
2573
2574 #define CHECK_IT(IT) check_it ((IT))
2575
2576 #else /* not 0 */
2577
2578 #define CHECK_IT(IT) (void) 0
2579
2580 #endif /* not 0 */
2581
2582
2583 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2584
2585 /* Check that the window end of window W is what we expect it
2586 to be---the last row in the current matrix displaying text. */
2587
2588 static void
2589 check_window_end (struct window *w)
2590 {
2591 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2592 {
2593 struct glyph_row *row;
2594 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2595 !row->enabled_p
2596 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2597 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2598 }
2599 }
2600
2601 #define CHECK_WINDOW_END(W) check_window_end ((W))
2602
2603 #else
2604
2605 #define CHECK_WINDOW_END(W) (void) 0
2606
2607 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2608
2609 /* Return mark position if current buffer has the region of non-zero length,
2610 or -1 otherwise. */
2611
2612 static ptrdiff_t
2613 markpos_of_region (void)
2614 {
2615 if (!NILP (Vtransient_mark_mode)
2616 && !NILP (BVAR (current_buffer, mark_active))
2617 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2618 {
2619 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2620
2621 if (markpos != PT)
2622 return markpos;
2623 }
2624 return -1;
2625 }
2626
2627 /***********************************************************************
2628 Iterator initialization
2629 ***********************************************************************/
2630
2631 /* Initialize IT for displaying current_buffer in window W, starting
2632 at character position CHARPOS. CHARPOS < 0 means that no buffer
2633 position is specified which is useful when the iterator is assigned
2634 a position later. BYTEPOS is the byte position corresponding to
2635 CHARPOS.
2636
2637 If ROW is not null, calls to produce_glyphs with IT as parameter
2638 will produce glyphs in that row.
2639
2640 BASE_FACE_ID is the id of a base face to use. It must be one of
2641 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2642 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2643 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2644
2645 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2646 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2647 will be initialized to use the corresponding mode line glyph row of
2648 the desired matrix of W. */
2649
2650 void
2651 init_iterator (struct it *it, struct window *w,
2652 ptrdiff_t charpos, ptrdiff_t bytepos,
2653 struct glyph_row *row, enum face_id base_face_id)
2654 {
2655 ptrdiff_t markpos;
2656 enum face_id remapped_base_face_id = base_face_id;
2657
2658 /* Some precondition checks. */
2659 eassert (w != NULL && it != NULL);
2660 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2661 && charpos <= ZV));
2662
2663 /* If face attributes have been changed since the last redisplay,
2664 free realized faces now because they depend on face definitions
2665 that might have changed. Don't free faces while there might be
2666 desired matrices pending which reference these faces. */
2667 if (face_change_count && !inhibit_free_realized_faces)
2668 {
2669 face_change_count = 0;
2670 free_all_realized_faces (Qnil);
2671 }
2672
2673 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2674 if (! NILP (Vface_remapping_alist))
2675 remapped_base_face_id
2676 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2677
2678 /* Use one of the mode line rows of W's desired matrix if
2679 appropriate. */
2680 if (row == NULL)
2681 {
2682 if (base_face_id == MODE_LINE_FACE_ID
2683 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2684 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2685 else if (base_face_id == HEADER_LINE_FACE_ID)
2686 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2687 }
2688
2689 /* Clear IT. */
2690 memset (it, 0, sizeof *it);
2691 it->current.overlay_string_index = -1;
2692 it->current.dpvec_index = -1;
2693 it->base_face_id = remapped_base_face_id;
2694 it->string = Qnil;
2695 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2696 it->paragraph_embedding = L2R;
2697 it->bidi_it.string.lstring = Qnil;
2698 it->bidi_it.string.s = NULL;
2699 it->bidi_it.string.bufpos = 0;
2700 it->bidi_it.w = w;
2701
2702 /* The window in which we iterate over current_buffer: */
2703 XSETWINDOW (it->window, w);
2704 it->w = w;
2705 it->f = XFRAME (w->frame);
2706
2707 it->cmp_it.id = -1;
2708
2709 /* Extra space between lines (on window systems only). */
2710 if (base_face_id == DEFAULT_FACE_ID
2711 && FRAME_WINDOW_P (it->f))
2712 {
2713 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2714 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2715 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2716 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2717 * FRAME_LINE_HEIGHT (it->f));
2718 else if (it->f->extra_line_spacing > 0)
2719 it->extra_line_spacing = it->f->extra_line_spacing;
2720 it->max_extra_line_spacing = 0;
2721 }
2722
2723 /* If realized faces have been removed, e.g. because of face
2724 attribute changes of named faces, recompute them. When running
2725 in batch mode, the face cache of the initial frame is null. If
2726 we happen to get called, make a dummy face cache. */
2727 if (FRAME_FACE_CACHE (it->f) == NULL)
2728 init_frame_faces (it->f);
2729 if (FRAME_FACE_CACHE (it->f)->used == 0)
2730 recompute_basic_faces (it->f);
2731
2732 /* Current value of the `slice', `space-width', and 'height' properties. */
2733 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2734 it->space_width = Qnil;
2735 it->font_height = Qnil;
2736 it->override_ascent = -1;
2737
2738 /* Are control characters displayed as `^C'? */
2739 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2740
2741 /* -1 means everything between a CR and the following line end
2742 is invisible. >0 means lines indented more than this value are
2743 invisible. */
2744 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2745 ? (clip_to_bounds
2746 (-1, XINT (BVAR (current_buffer, selective_display)),
2747 PTRDIFF_MAX))
2748 : (!NILP (BVAR (current_buffer, selective_display))
2749 ? -1 : 0));
2750 it->selective_display_ellipsis_p
2751 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2752
2753 /* Display table to use. */
2754 it->dp = window_display_table (w);
2755
2756 /* Are multibyte characters enabled in current_buffer? */
2757 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2758
2759 /* If visible region is of non-zero length, set IT->region_beg_charpos
2760 and IT->region_end_charpos to the start and end of a visible region
2761 in window IT->w. Set both to -1 to indicate no region. */
2762 markpos = markpos_of_region ();
2763 if (markpos >= 0
2764 /* Maybe highlight only in selected window. */
2765 && (/* Either show region everywhere. */
2766 highlight_nonselected_windows
2767 /* Or show region in the selected window. */
2768 || w == XWINDOW (selected_window)
2769 /* Or show the region if we are in the mini-buffer and W is
2770 the window the mini-buffer refers to. */
2771 || (MINI_WINDOW_P (XWINDOW (selected_window))
2772 && WINDOWP (minibuf_selected_window)
2773 && w == XWINDOW (minibuf_selected_window))))
2774 {
2775 it->region_beg_charpos = min (PT, markpos);
2776 it->region_end_charpos = max (PT, markpos);
2777 }
2778 else
2779 it->region_beg_charpos = it->region_end_charpos = -1;
2780
2781 /* Get the position at which the redisplay_end_trigger hook should
2782 be run, if it is to be run at all. */
2783 if (MARKERP (w->redisplay_end_trigger)
2784 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2785 it->redisplay_end_trigger_charpos
2786 = marker_position (w->redisplay_end_trigger);
2787 else if (INTEGERP (w->redisplay_end_trigger))
2788 it->redisplay_end_trigger_charpos =
2789 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2790
2791 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2792
2793 /* Are lines in the display truncated? */
2794 if (base_face_id != DEFAULT_FACE_ID
2795 || it->w->hscroll
2796 || (! WINDOW_FULL_WIDTH_P (it->w)
2797 && ((!NILP (Vtruncate_partial_width_windows)
2798 && !INTEGERP (Vtruncate_partial_width_windows))
2799 || (INTEGERP (Vtruncate_partial_width_windows)
2800 && (WINDOW_TOTAL_COLS (it->w)
2801 < XINT (Vtruncate_partial_width_windows))))))
2802 it->line_wrap = TRUNCATE;
2803 else if (NILP (BVAR (current_buffer, truncate_lines)))
2804 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2805 ? WINDOW_WRAP : WORD_WRAP;
2806 else
2807 it->line_wrap = TRUNCATE;
2808
2809 /* Get dimensions of truncation and continuation glyphs. These are
2810 displayed as fringe bitmaps under X, but we need them for such
2811 frames when the fringes are turned off. But leave the dimensions
2812 zero for tooltip frames, as these glyphs look ugly there and also
2813 sabotage calculations of tooltip dimensions in x-show-tip. */
2814 #ifdef HAVE_WINDOW_SYSTEM
2815 if (!(FRAME_WINDOW_P (it->f)
2816 && FRAMEP (tip_frame)
2817 && it->f == XFRAME (tip_frame)))
2818 #endif
2819 {
2820 if (it->line_wrap == TRUNCATE)
2821 {
2822 /* We will need the truncation glyph. */
2823 eassert (it->glyph_row == NULL);
2824 produce_special_glyphs (it, IT_TRUNCATION);
2825 it->truncation_pixel_width = it->pixel_width;
2826 }
2827 else
2828 {
2829 /* We will need the continuation glyph. */
2830 eassert (it->glyph_row == NULL);
2831 produce_special_glyphs (it, IT_CONTINUATION);
2832 it->continuation_pixel_width = it->pixel_width;
2833 }
2834 }
2835
2836 /* Reset these values to zero because the produce_special_glyphs
2837 above has changed them. */
2838 it->pixel_width = it->ascent = it->descent = 0;
2839 it->phys_ascent = it->phys_descent = 0;
2840
2841 /* Set this after getting the dimensions of truncation and
2842 continuation glyphs, so that we don't produce glyphs when calling
2843 produce_special_glyphs, above. */
2844 it->glyph_row = row;
2845 it->area = TEXT_AREA;
2846
2847 /* Forget any previous info about this row being reversed. */
2848 if (it->glyph_row)
2849 it->glyph_row->reversed_p = 0;
2850
2851 /* Get the dimensions of the display area. The display area
2852 consists of the visible window area plus a horizontally scrolled
2853 part to the left of the window. All x-values are relative to the
2854 start of this total display area. */
2855 if (base_face_id != DEFAULT_FACE_ID)
2856 {
2857 /* Mode lines, menu bar in terminal frames. */
2858 it->first_visible_x = 0;
2859 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2860 }
2861 else
2862 {
2863 it->first_visible_x =
2864 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2865 it->last_visible_x = (it->first_visible_x
2866 + window_box_width (w, TEXT_AREA));
2867
2868 /* If we truncate lines, leave room for the truncation glyph(s) at
2869 the right margin. Otherwise, leave room for the continuation
2870 glyph(s). Done only if the window has no fringes. Since we
2871 don't know at this point whether there will be any R2L lines in
2872 the window, we reserve space for truncation/continuation glyphs
2873 even if only one of the fringes is absent. */
2874 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2875 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2876 {
2877 if (it->line_wrap == TRUNCATE)
2878 it->last_visible_x -= it->truncation_pixel_width;
2879 else
2880 it->last_visible_x -= it->continuation_pixel_width;
2881 }
2882
2883 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2884 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2885 }
2886
2887 /* Leave room for a border glyph. */
2888 if (!FRAME_WINDOW_P (it->f)
2889 && !WINDOW_RIGHTMOST_P (it->w))
2890 it->last_visible_x -= 1;
2891
2892 it->last_visible_y = window_text_bottom_y (w);
2893
2894 /* For mode lines and alike, arrange for the first glyph having a
2895 left box line if the face specifies a box. */
2896 if (base_face_id != DEFAULT_FACE_ID)
2897 {
2898 struct face *face;
2899
2900 it->face_id = remapped_base_face_id;
2901
2902 /* If we have a boxed mode line, make the first character appear
2903 with a left box line. */
2904 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2905 if (face->box != FACE_NO_BOX)
2906 it->start_of_box_run_p = 1;
2907 }
2908
2909 /* If a buffer position was specified, set the iterator there,
2910 getting overlays and face properties from that position. */
2911 if (charpos >= BUF_BEG (current_buffer))
2912 {
2913 it->end_charpos = ZV;
2914 eassert (charpos == BYTE_TO_CHAR (bytepos));
2915 IT_CHARPOS (*it) = charpos;
2916 IT_BYTEPOS (*it) = bytepos;
2917
2918 /* We will rely on `reseat' to set this up properly, via
2919 handle_face_prop. */
2920 it->face_id = it->base_face_id;
2921
2922 it->start = it->current;
2923 /* Do we need to reorder bidirectional text? Not if this is a
2924 unibyte buffer: by definition, none of the single-byte
2925 characters are strong R2L, so no reordering is needed. And
2926 bidi.c doesn't support unibyte buffers anyway. Also, don't
2927 reorder while we are loading loadup.el, since the tables of
2928 character properties needed for reordering are not yet
2929 available. */
2930 it->bidi_p =
2931 NILP (Vpurify_flag)
2932 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2933 && it->multibyte_p;
2934
2935 /* If we are to reorder bidirectional text, init the bidi
2936 iterator. */
2937 if (it->bidi_p)
2938 {
2939 /* Note the paragraph direction that this buffer wants to
2940 use. */
2941 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2942 Qleft_to_right))
2943 it->paragraph_embedding = L2R;
2944 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2945 Qright_to_left))
2946 it->paragraph_embedding = R2L;
2947 else
2948 it->paragraph_embedding = NEUTRAL_DIR;
2949 bidi_unshelve_cache (NULL, 0);
2950 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2951 &it->bidi_it);
2952 }
2953
2954 /* Compute faces etc. */
2955 reseat (it, it->current.pos, 1);
2956 }
2957
2958 CHECK_IT (it);
2959 }
2960
2961
2962 /* Initialize IT for the display of window W with window start POS. */
2963
2964 void
2965 start_display (struct it *it, struct window *w, struct text_pos pos)
2966 {
2967 struct glyph_row *row;
2968 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2969
2970 row = w->desired_matrix->rows + first_vpos;
2971 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2972 it->first_vpos = first_vpos;
2973
2974 /* Don't reseat to previous visible line start if current start
2975 position is in a string or image. */
2976 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2977 {
2978 int start_at_line_beg_p;
2979 int first_y = it->current_y;
2980
2981 /* If window start is not at a line start, skip forward to POS to
2982 get the correct continuation lines width. */
2983 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2984 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2985 if (!start_at_line_beg_p)
2986 {
2987 int new_x;
2988
2989 reseat_at_previous_visible_line_start (it);
2990 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2991
2992 new_x = it->current_x + it->pixel_width;
2993
2994 /* If lines are continued, this line may end in the middle
2995 of a multi-glyph character (e.g. a control character
2996 displayed as \003, or in the middle of an overlay
2997 string). In this case move_it_to above will not have
2998 taken us to the start of the continuation line but to the
2999 end of the continued line. */
3000 if (it->current_x > 0
3001 && it->line_wrap != TRUNCATE /* Lines are continued. */
3002 && (/* And glyph doesn't fit on the line. */
3003 new_x > it->last_visible_x
3004 /* Or it fits exactly and we're on a window
3005 system frame. */
3006 || (new_x == it->last_visible_x
3007 && FRAME_WINDOW_P (it->f)
3008 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3009 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3010 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3011 {
3012 if ((it->current.dpvec_index >= 0
3013 || it->current.overlay_string_index >= 0)
3014 /* If we are on a newline from a display vector or
3015 overlay string, then we are already at the end of
3016 a screen line; no need to go to the next line in
3017 that case, as this line is not really continued.
3018 (If we do go to the next line, C-e will not DTRT.) */
3019 && it->c != '\n')
3020 {
3021 set_iterator_to_next (it, 1);
3022 move_it_in_display_line_to (it, -1, -1, 0);
3023 }
3024
3025 it->continuation_lines_width += it->current_x;
3026 }
3027 /* If the character at POS is displayed via a display
3028 vector, move_it_to above stops at the final glyph of
3029 IT->dpvec. To make the caller redisplay that character
3030 again (a.k.a. start at POS), we need to reset the
3031 dpvec_index to the beginning of IT->dpvec. */
3032 else if (it->current.dpvec_index >= 0)
3033 it->current.dpvec_index = 0;
3034
3035 /* We're starting a new display line, not affected by the
3036 height of the continued line, so clear the appropriate
3037 fields in the iterator structure. */
3038 it->max_ascent = it->max_descent = 0;
3039 it->max_phys_ascent = it->max_phys_descent = 0;
3040
3041 it->current_y = first_y;
3042 it->vpos = 0;
3043 it->current_x = it->hpos = 0;
3044 }
3045 }
3046 }
3047
3048
3049 /* Return 1 if POS is a position in ellipses displayed for invisible
3050 text. W is the window we display, for text property lookup. */
3051
3052 static int
3053 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3054 {
3055 Lisp_Object prop, window;
3056 int ellipses_p = 0;
3057 ptrdiff_t charpos = CHARPOS (pos->pos);
3058
3059 /* If POS specifies a position in a display vector, this might
3060 be for an ellipsis displayed for invisible text. We won't
3061 get the iterator set up for delivering that ellipsis unless
3062 we make sure that it gets aware of the invisible text. */
3063 if (pos->dpvec_index >= 0
3064 && pos->overlay_string_index < 0
3065 && CHARPOS (pos->string_pos) < 0
3066 && charpos > BEGV
3067 && (XSETWINDOW (window, w),
3068 prop = Fget_char_property (make_number (charpos),
3069 Qinvisible, window),
3070 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3071 {
3072 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3073 window);
3074 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3075 }
3076
3077 return ellipses_p;
3078 }
3079
3080
3081 /* Initialize IT for stepping through current_buffer in window W,
3082 starting at position POS that includes overlay string and display
3083 vector/ control character translation position information. Value
3084 is zero if there are overlay strings with newlines at POS. */
3085
3086 static int
3087 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3088 {
3089 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3090 int i, overlay_strings_with_newlines = 0;
3091
3092 /* If POS specifies a position in a display vector, this might
3093 be for an ellipsis displayed for invisible text. We won't
3094 get the iterator set up for delivering that ellipsis unless
3095 we make sure that it gets aware of the invisible text. */
3096 if (in_ellipses_for_invisible_text_p (pos, w))
3097 {
3098 --charpos;
3099 bytepos = 0;
3100 }
3101
3102 /* Keep in mind: the call to reseat in init_iterator skips invisible
3103 text, so we might end up at a position different from POS. This
3104 is only a problem when POS is a row start after a newline and an
3105 overlay starts there with an after-string, and the overlay has an
3106 invisible property. Since we don't skip invisible text in
3107 display_line and elsewhere immediately after consuming the
3108 newline before the row start, such a POS will not be in a string,
3109 but the call to init_iterator below will move us to the
3110 after-string. */
3111 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3112
3113 /* This only scans the current chunk -- it should scan all chunks.
3114 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3115 to 16 in 22.1 to make this a lesser problem. */
3116 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3117 {
3118 const char *s = SSDATA (it->overlay_strings[i]);
3119 const char *e = s + SBYTES (it->overlay_strings[i]);
3120
3121 while (s < e && *s != '\n')
3122 ++s;
3123
3124 if (s < e)
3125 {
3126 overlay_strings_with_newlines = 1;
3127 break;
3128 }
3129 }
3130
3131 /* If position is within an overlay string, set up IT to the right
3132 overlay string. */
3133 if (pos->overlay_string_index >= 0)
3134 {
3135 int relative_index;
3136
3137 /* If the first overlay string happens to have a `display'
3138 property for an image, the iterator will be set up for that
3139 image, and we have to undo that setup first before we can
3140 correct the overlay string index. */
3141 if (it->method == GET_FROM_IMAGE)
3142 pop_it (it);
3143
3144 /* We already have the first chunk of overlay strings in
3145 IT->overlay_strings. Load more until the one for
3146 pos->overlay_string_index is in IT->overlay_strings. */
3147 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3148 {
3149 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3150 it->current.overlay_string_index = 0;
3151 while (n--)
3152 {
3153 load_overlay_strings (it, 0);
3154 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3155 }
3156 }
3157
3158 it->current.overlay_string_index = pos->overlay_string_index;
3159 relative_index = (it->current.overlay_string_index
3160 % OVERLAY_STRING_CHUNK_SIZE);
3161 it->string = it->overlay_strings[relative_index];
3162 eassert (STRINGP (it->string));
3163 it->current.string_pos = pos->string_pos;
3164 it->method = GET_FROM_STRING;
3165 it->end_charpos = SCHARS (it->string);
3166 /* Set up the bidi iterator for this overlay string. */
3167 if (it->bidi_p)
3168 {
3169 it->bidi_it.string.lstring = it->string;
3170 it->bidi_it.string.s = NULL;
3171 it->bidi_it.string.schars = SCHARS (it->string);
3172 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3173 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3174 it->bidi_it.string.unibyte = !it->multibyte_p;
3175 it->bidi_it.w = it->w;
3176 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3177 FRAME_WINDOW_P (it->f), &it->bidi_it);
3178
3179 /* Synchronize the state of the bidi iterator with
3180 pos->string_pos. For any string position other than
3181 zero, this will be done automagically when we resume
3182 iteration over the string and get_visually_first_element
3183 is called. But if string_pos is zero, and the string is
3184 to be reordered for display, we need to resync manually,
3185 since it could be that the iteration state recorded in
3186 pos ended at string_pos of 0 moving backwards in string. */
3187 if (CHARPOS (pos->string_pos) == 0)
3188 {
3189 get_visually_first_element (it);
3190 if (IT_STRING_CHARPOS (*it) != 0)
3191 do {
3192 /* Paranoia. */
3193 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3194 bidi_move_to_visually_next (&it->bidi_it);
3195 } while (it->bidi_it.charpos != 0);
3196 }
3197 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3198 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3199 }
3200 }
3201
3202 if (CHARPOS (pos->string_pos) >= 0)
3203 {
3204 /* Recorded position is not in an overlay string, but in another
3205 string. This can only be a string from a `display' property.
3206 IT should already be filled with that string. */
3207 it->current.string_pos = pos->string_pos;
3208 eassert (STRINGP (it->string));
3209 if (it->bidi_p)
3210 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3211 FRAME_WINDOW_P (it->f), &it->bidi_it);
3212 }
3213
3214 /* Restore position in display vector translations, control
3215 character translations or ellipses. */
3216 if (pos->dpvec_index >= 0)
3217 {
3218 if (it->dpvec == NULL)
3219 get_next_display_element (it);
3220 eassert (it->dpvec && it->current.dpvec_index == 0);
3221 it->current.dpvec_index = pos->dpvec_index;
3222 }
3223
3224 CHECK_IT (it);
3225 return !overlay_strings_with_newlines;
3226 }
3227
3228
3229 /* Initialize IT for stepping through current_buffer in window W
3230 starting at ROW->start. */
3231
3232 static void
3233 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3234 {
3235 init_from_display_pos (it, w, &row->start);
3236 it->start = row->start;
3237 it->continuation_lines_width = row->continuation_lines_width;
3238 CHECK_IT (it);
3239 }
3240
3241
3242 /* Initialize IT for stepping through current_buffer in window W
3243 starting in the line following ROW, i.e. starting at ROW->end.
3244 Value is zero if there are overlay strings with newlines at ROW's
3245 end position. */
3246
3247 static int
3248 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3249 {
3250 int success = 0;
3251
3252 if (init_from_display_pos (it, w, &row->end))
3253 {
3254 if (row->continued_p)
3255 it->continuation_lines_width
3256 = row->continuation_lines_width + row->pixel_width;
3257 CHECK_IT (it);
3258 success = 1;
3259 }
3260
3261 return success;
3262 }
3263
3264
3265
3266 \f
3267 /***********************************************************************
3268 Text properties
3269 ***********************************************************************/
3270
3271 /* Called when IT reaches IT->stop_charpos. Handle text property and
3272 overlay changes. Set IT->stop_charpos to the next position where
3273 to stop. */
3274
3275 static void
3276 handle_stop (struct it *it)
3277 {
3278 enum prop_handled handled;
3279 int handle_overlay_change_p;
3280 struct props *p;
3281
3282 it->dpvec = NULL;
3283 it->current.dpvec_index = -1;
3284 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3285 it->ignore_overlay_strings_at_pos_p = 0;
3286 it->ellipsis_p = 0;
3287
3288 /* Use face of preceding text for ellipsis (if invisible) */
3289 if (it->selective_display_ellipsis_p)
3290 it->saved_face_id = it->face_id;
3291
3292 do
3293 {
3294 handled = HANDLED_NORMALLY;
3295
3296 /* Call text property handlers. */
3297 for (p = it_props; p->handler; ++p)
3298 {
3299 handled = p->handler (it);
3300
3301 if (handled == HANDLED_RECOMPUTE_PROPS)
3302 break;
3303 else if (handled == HANDLED_RETURN)
3304 {
3305 /* We still want to show before and after strings from
3306 overlays even if the actual buffer text is replaced. */
3307 if (!handle_overlay_change_p
3308 || it->sp > 1
3309 /* Don't call get_overlay_strings_1 if we already
3310 have overlay strings loaded, because doing so
3311 will load them again and push the iterator state
3312 onto the stack one more time, which is not
3313 expected by the rest of the code that processes
3314 overlay strings. */
3315 || (it->current.overlay_string_index < 0
3316 ? !get_overlay_strings_1 (it, 0, 0)
3317 : 0))
3318 {
3319 if (it->ellipsis_p)
3320 setup_for_ellipsis (it, 0);
3321 /* When handling a display spec, we might load an
3322 empty string. In that case, discard it here. We
3323 used to discard it in handle_single_display_spec,
3324 but that causes get_overlay_strings_1, above, to
3325 ignore overlay strings that we must check. */
3326 if (STRINGP (it->string) && !SCHARS (it->string))
3327 pop_it (it);
3328 return;
3329 }
3330 else if (STRINGP (it->string) && !SCHARS (it->string))
3331 pop_it (it);
3332 else
3333 {
3334 it->ignore_overlay_strings_at_pos_p = 1;
3335 it->string_from_display_prop_p = 0;
3336 it->from_disp_prop_p = 0;
3337 handle_overlay_change_p = 0;
3338 }
3339 handled = HANDLED_RECOMPUTE_PROPS;
3340 break;
3341 }
3342 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3343 handle_overlay_change_p = 0;
3344 }
3345
3346 if (handled != HANDLED_RECOMPUTE_PROPS)
3347 {
3348 /* Don't check for overlay strings below when set to deliver
3349 characters from a display vector. */
3350 if (it->method == GET_FROM_DISPLAY_VECTOR)
3351 handle_overlay_change_p = 0;
3352
3353 /* Handle overlay changes.
3354 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3355 if it finds overlays. */
3356 if (handle_overlay_change_p)
3357 handled = handle_overlay_change (it);
3358 }
3359
3360 if (it->ellipsis_p)
3361 {
3362 setup_for_ellipsis (it, 0);
3363 break;
3364 }
3365 }
3366 while (handled == HANDLED_RECOMPUTE_PROPS);
3367
3368 /* Determine where to stop next. */
3369 if (handled == HANDLED_NORMALLY)
3370 compute_stop_pos (it);
3371 }
3372
3373
3374 /* Compute IT->stop_charpos from text property and overlay change
3375 information for IT's current position. */
3376
3377 static void
3378 compute_stop_pos (struct it *it)
3379 {
3380 register INTERVAL iv, next_iv;
3381 Lisp_Object object, limit, position;
3382 ptrdiff_t charpos, bytepos;
3383
3384 if (STRINGP (it->string))
3385 {
3386 /* Strings are usually short, so don't limit the search for
3387 properties. */
3388 it->stop_charpos = it->end_charpos;
3389 object = it->string;
3390 limit = Qnil;
3391 charpos = IT_STRING_CHARPOS (*it);
3392 bytepos = IT_STRING_BYTEPOS (*it);
3393 }
3394 else
3395 {
3396 ptrdiff_t pos;
3397
3398 /* If end_charpos is out of range for some reason, such as a
3399 misbehaving display function, rationalize it (Bug#5984). */
3400 if (it->end_charpos > ZV)
3401 it->end_charpos = ZV;
3402 it->stop_charpos = it->end_charpos;
3403
3404 /* If next overlay change is in front of the current stop pos
3405 (which is IT->end_charpos), stop there. Note: value of
3406 next_overlay_change is point-max if no overlay change
3407 follows. */
3408 charpos = IT_CHARPOS (*it);
3409 bytepos = IT_BYTEPOS (*it);
3410 pos = next_overlay_change (charpos);
3411 if (pos < it->stop_charpos)
3412 it->stop_charpos = pos;
3413
3414 /* If showing the region, we have to stop at the region
3415 start or end because the face might change there. */
3416 if (it->region_beg_charpos > 0)
3417 {
3418 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3419 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3420 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3421 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3422 }
3423
3424 /* Set up variables for computing the stop position from text
3425 property changes. */
3426 XSETBUFFER (object, current_buffer);
3427 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3428 }
3429
3430 /* Get the interval containing IT's position. Value is a null
3431 interval if there isn't such an interval. */
3432 position = make_number (charpos);
3433 iv = validate_interval_range (object, &position, &position, 0);
3434 if (iv)
3435 {
3436 Lisp_Object values_here[LAST_PROP_IDX];
3437 struct props *p;
3438
3439 /* Get properties here. */
3440 for (p = it_props; p->handler; ++p)
3441 values_here[p->idx] = textget (iv->plist, *p->name);
3442
3443 /* Look for an interval following iv that has different
3444 properties. */
3445 for (next_iv = next_interval (iv);
3446 (next_iv
3447 && (NILP (limit)
3448 || XFASTINT (limit) > next_iv->position));
3449 next_iv = next_interval (next_iv))
3450 {
3451 for (p = it_props; p->handler; ++p)
3452 {
3453 Lisp_Object new_value;
3454
3455 new_value = textget (next_iv->plist, *p->name);
3456 if (!EQ (values_here[p->idx], new_value))
3457 break;
3458 }
3459
3460 if (p->handler)
3461 break;
3462 }
3463
3464 if (next_iv)
3465 {
3466 if (INTEGERP (limit)
3467 && next_iv->position >= XFASTINT (limit))
3468 /* No text property change up to limit. */
3469 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3470 else
3471 /* Text properties change in next_iv. */
3472 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3473 }
3474 }
3475
3476 if (it->cmp_it.id < 0)
3477 {
3478 ptrdiff_t stoppos = it->end_charpos;
3479
3480 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3481 stoppos = -1;
3482 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3483 stoppos, it->string);
3484 }
3485
3486 eassert (STRINGP (it->string)
3487 || (it->stop_charpos >= BEGV
3488 && it->stop_charpos >= IT_CHARPOS (*it)));
3489 }
3490
3491
3492 /* Return the position of the next overlay change after POS in
3493 current_buffer. Value is point-max if no overlay change
3494 follows. This is like `next-overlay-change' but doesn't use
3495 xmalloc. */
3496
3497 static ptrdiff_t
3498 next_overlay_change (ptrdiff_t pos)
3499 {
3500 ptrdiff_t i, noverlays;
3501 ptrdiff_t endpos;
3502 Lisp_Object *overlays;
3503
3504 /* Get all overlays at the given position. */
3505 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3506
3507 /* If any of these overlays ends before endpos,
3508 use its ending point instead. */
3509 for (i = 0; i < noverlays; ++i)
3510 {
3511 Lisp_Object oend;
3512 ptrdiff_t oendpos;
3513
3514 oend = OVERLAY_END (overlays[i]);
3515 oendpos = OVERLAY_POSITION (oend);
3516 endpos = min (endpos, oendpos);
3517 }
3518
3519 return endpos;
3520 }
3521
3522 /* How many characters forward to search for a display property or
3523 display string. Searching too far forward makes the bidi display
3524 sluggish, especially in small windows. */
3525 #define MAX_DISP_SCAN 250
3526
3527 /* Return the character position of a display string at or after
3528 position specified by POSITION. If no display string exists at or
3529 after POSITION, return ZV. A display string is either an overlay
3530 with `display' property whose value is a string, or a `display'
3531 text property whose value is a string. STRING is data about the
3532 string to iterate; if STRING->lstring is nil, we are iterating a
3533 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3534 on a GUI frame. DISP_PROP is set to zero if we searched
3535 MAX_DISP_SCAN characters forward without finding any display
3536 strings, non-zero otherwise. It is set to 2 if the display string
3537 uses any kind of `(space ...)' spec that will produce a stretch of
3538 white space in the text area. */
3539 ptrdiff_t
3540 compute_display_string_pos (struct text_pos *position,
3541 struct bidi_string_data *string,
3542 struct window *w,
3543 int frame_window_p, int *disp_prop)
3544 {
3545 /* OBJECT = nil means current buffer. */
3546 Lisp_Object object, object1;
3547 Lisp_Object pos, spec, limpos;
3548 int string_p = (string && (STRINGP (string->lstring) || string->s));
3549 ptrdiff_t eob = string_p ? string->schars : ZV;
3550 ptrdiff_t begb = string_p ? 0 : BEGV;
3551 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3552 ptrdiff_t lim =
3553 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3554 struct text_pos tpos;
3555 int rv = 0;
3556
3557 if (string && STRINGP (string->lstring))
3558 object1 = object = string->lstring;
3559 else if (w && !string_p)
3560 {
3561 XSETWINDOW (object, w);
3562 object1 = Qnil;
3563 }
3564 else
3565 object1 = object = Qnil;
3566
3567 *disp_prop = 1;
3568
3569 if (charpos >= eob
3570 /* We don't support display properties whose values are strings
3571 that have display string properties. */
3572 || string->from_disp_str
3573 /* C strings cannot have display properties. */
3574 || (string->s && !STRINGP (object)))
3575 {
3576 *disp_prop = 0;
3577 return eob;
3578 }
3579
3580 /* If the character at CHARPOS is where the display string begins,
3581 return CHARPOS. */
3582 pos = make_number (charpos);
3583 if (STRINGP (object))
3584 bufpos = string->bufpos;
3585 else
3586 bufpos = charpos;
3587 tpos = *position;
3588 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3589 && (charpos <= begb
3590 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3591 object),
3592 spec))
3593 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3594 frame_window_p)))
3595 {
3596 if (rv == 2)
3597 *disp_prop = 2;
3598 return charpos;
3599 }
3600
3601 /* Look forward for the first character with a `display' property
3602 that will replace the underlying text when displayed. */
3603 limpos = make_number (lim);
3604 do {
3605 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3606 CHARPOS (tpos) = XFASTINT (pos);
3607 if (CHARPOS (tpos) >= lim)
3608 {
3609 *disp_prop = 0;
3610 break;
3611 }
3612 if (STRINGP (object))
3613 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3614 else
3615 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3616 spec = Fget_char_property (pos, Qdisplay, object);
3617 if (!STRINGP (object))
3618 bufpos = CHARPOS (tpos);
3619 } while (NILP (spec)
3620 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3621 bufpos, frame_window_p)));
3622 if (rv == 2)
3623 *disp_prop = 2;
3624
3625 return CHARPOS (tpos);
3626 }
3627
3628 /* Return the character position of the end of the display string that
3629 started at CHARPOS. If there's no display string at CHARPOS,
3630 return -1. A display string is either an overlay with `display'
3631 property whose value is a string or a `display' text property whose
3632 value is a string. */
3633 ptrdiff_t
3634 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3635 {
3636 /* OBJECT = nil means current buffer. */
3637 Lisp_Object object =
3638 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3639 Lisp_Object pos = make_number (charpos);
3640 ptrdiff_t eob =
3641 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3642
3643 if (charpos >= eob || (string->s && !STRINGP (object)))
3644 return eob;
3645
3646 /* It could happen that the display property or overlay was removed
3647 since we found it in compute_display_string_pos above. One way
3648 this can happen is if JIT font-lock was called (through
3649 handle_fontified_prop), and jit-lock-functions remove text
3650 properties or overlays from the portion of buffer that includes
3651 CHARPOS. Muse mode is known to do that, for example. In this
3652 case, we return -1 to the caller, to signal that no display
3653 string is actually present at CHARPOS. See bidi_fetch_char for
3654 how this is handled.
3655
3656 An alternative would be to never look for display properties past
3657 it->stop_charpos. But neither compute_display_string_pos nor
3658 bidi_fetch_char that calls it know or care where the next
3659 stop_charpos is. */
3660 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3661 return -1;
3662
3663 /* Look forward for the first character where the `display' property
3664 changes. */
3665 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3666
3667 return XFASTINT (pos);
3668 }
3669
3670
3671 \f
3672 /***********************************************************************
3673 Fontification
3674 ***********************************************************************/
3675
3676 /* Handle changes in the `fontified' property of the current buffer by
3677 calling hook functions from Qfontification_functions to fontify
3678 regions of text. */
3679
3680 static enum prop_handled
3681 handle_fontified_prop (struct it *it)
3682 {
3683 Lisp_Object prop, pos;
3684 enum prop_handled handled = HANDLED_NORMALLY;
3685
3686 if (!NILP (Vmemory_full))
3687 return handled;
3688
3689 /* Get the value of the `fontified' property at IT's current buffer
3690 position. (The `fontified' property doesn't have a special
3691 meaning in strings.) If the value is nil, call functions from
3692 Qfontification_functions. */
3693 if (!STRINGP (it->string)
3694 && it->s == NULL
3695 && !NILP (Vfontification_functions)
3696 && !NILP (Vrun_hooks)
3697 && (pos = make_number (IT_CHARPOS (*it)),
3698 prop = Fget_char_property (pos, Qfontified, Qnil),
3699 /* Ignore the special cased nil value always present at EOB since
3700 no amount of fontifying will be able to change it. */
3701 NILP (prop) && IT_CHARPOS (*it) < Z))
3702 {
3703 ptrdiff_t count = SPECPDL_INDEX ();
3704 Lisp_Object val;
3705 struct buffer *obuf = current_buffer;
3706 int begv = BEGV, zv = ZV;
3707 int old_clip_changed = current_buffer->clip_changed;
3708
3709 val = Vfontification_functions;
3710 specbind (Qfontification_functions, Qnil);
3711
3712 eassert (it->end_charpos == ZV);
3713
3714 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3715 safe_call1 (val, pos);
3716 else
3717 {
3718 Lisp_Object fns, fn;
3719 struct gcpro gcpro1, gcpro2;
3720
3721 fns = Qnil;
3722 GCPRO2 (val, fns);
3723
3724 for (; CONSP (val); val = XCDR (val))
3725 {
3726 fn = XCAR (val);
3727
3728 if (EQ (fn, Qt))
3729 {
3730 /* A value of t indicates this hook has a local
3731 binding; it means to run the global binding too.
3732 In a global value, t should not occur. If it
3733 does, we must ignore it to avoid an endless
3734 loop. */
3735 for (fns = Fdefault_value (Qfontification_functions);
3736 CONSP (fns);
3737 fns = XCDR (fns))
3738 {
3739 fn = XCAR (fns);
3740 if (!EQ (fn, Qt))
3741 safe_call1 (fn, pos);
3742 }
3743 }
3744 else
3745 safe_call1 (fn, pos);
3746 }
3747
3748 UNGCPRO;
3749 }
3750
3751 unbind_to (count, Qnil);
3752
3753 /* Fontification functions routinely call `save-restriction'.
3754 Normally, this tags clip_changed, which can confuse redisplay
3755 (see discussion in Bug#6671). Since we don't perform any
3756 special handling of fontification changes in the case where
3757 `save-restriction' isn't called, there's no point doing so in
3758 this case either. So, if the buffer's restrictions are
3759 actually left unchanged, reset clip_changed. */
3760 if (obuf == current_buffer)
3761 {
3762 if (begv == BEGV && zv == ZV)
3763 current_buffer->clip_changed = old_clip_changed;
3764 }
3765 /* There isn't much we can reasonably do to protect against
3766 misbehaving fontification, but here's a fig leaf. */
3767 else if (BUFFER_LIVE_P (obuf))
3768 set_buffer_internal_1 (obuf);
3769
3770 /* The fontification code may have added/removed text.
3771 It could do even a lot worse, but let's at least protect against
3772 the most obvious case where only the text past `pos' gets changed',
3773 as is/was done in grep.el where some escapes sequences are turned
3774 into face properties (bug#7876). */
3775 it->end_charpos = ZV;
3776
3777 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3778 something. This avoids an endless loop if they failed to
3779 fontify the text for which reason ever. */
3780 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3781 handled = HANDLED_RECOMPUTE_PROPS;
3782 }
3783
3784 return handled;
3785 }
3786
3787
3788 \f
3789 /***********************************************************************
3790 Faces
3791 ***********************************************************************/
3792
3793 /* Set up iterator IT from face properties at its current position.
3794 Called from handle_stop. */
3795
3796 static enum prop_handled
3797 handle_face_prop (struct it *it)
3798 {
3799 int new_face_id;
3800 ptrdiff_t next_stop;
3801
3802 if (!STRINGP (it->string))
3803 {
3804 new_face_id
3805 = face_at_buffer_position (it->w,
3806 IT_CHARPOS (*it),
3807 it->region_beg_charpos,
3808 it->region_end_charpos,
3809 &next_stop,
3810 (IT_CHARPOS (*it)
3811 + TEXT_PROP_DISTANCE_LIMIT),
3812 0, it->base_face_id);
3813
3814 /* Is this a start of a run of characters with box face?
3815 Caveat: this can be called for a freshly initialized
3816 iterator; face_id is -1 in this case. We know that the new
3817 face will not change until limit, i.e. if the new face has a
3818 box, all characters up to limit will have one. But, as
3819 usual, we don't know whether limit is really the end. */
3820 if (new_face_id != it->face_id)
3821 {
3822 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3823 /* If it->face_id is -1, old_face below will be NULL, see
3824 the definition of FACE_FROM_ID. This will happen if this
3825 is the initial call that gets the face. */
3826 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3827
3828 /* If the value of face_id of the iterator is -1, we have to
3829 look in front of IT's position and see whether there is a
3830 face there that's different from new_face_id. */
3831 if (!old_face && IT_CHARPOS (*it) > BEG)
3832 {
3833 int prev_face_id = face_before_it_pos (it);
3834
3835 old_face = FACE_FROM_ID (it->f, prev_face_id);
3836 }
3837
3838 /* If the new face has a box, but the old face does not,
3839 this is the start of a run of characters with box face,
3840 i.e. this character has a shadow on the left side. */
3841 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3842 && (old_face == NULL || !old_face->box));
3843 it->face_box_p = new_face->box != FACE_NO_BOX;
3844 }
3845 }
3846 else
3847 {
3848 int base_face_id;
3849 ptrdiff_t bufpos;
3850 int i;
3851 Lisp_Object from_overlay
3852 = (it->current.overlay_string_index >= 0
3853 ? it->string_overlays[it->current.overlay_string_index
3854 % OVERLAY_STRING_CHUNK_SIZE]
3855 : Qnil);
3856
3857 /* See if we got to this string directly or indirectly from
3858 an overlay property. That includes the before-string or
3859 after-string of an overlay, strings in display properties
3860 provided by an overlay, their text properties, etc.
3861
3862 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3863 if (! NILP (from_overlay))
3864 for (i = it->sp - 1; i >= 0; i--)
3865 {
3866 if (it->stack[i].current.overlay_string_index >= 0)
3867 from_overlay
3868 = it->string_overlays[it->stack[i].current.overlay_string_index
3869 % OVERLAY_STRING_CHUNK_SIZE];
3870 else if (! NILP (it->stack[i].from_overlay))
3871 from_overlay = it->stack[i].from_overlay;
3872
3873 if (!NILP (from_overlay))
3874 break;
3875 }
3876
3877 if (! NILP (from_overlay))
3878 {
3879 bufpos = IT_CHARPOS (*it);
3880 /* For a string from an overlay, the base face depends
3881 only on text properties and ignores overlays. */
3882 base_face_id
3883 = face_for_overlay_string (it->w,
3884 IT_CHARPOS (*it),
3885 it->region_beg_charpos,
3886 it->region_end_charpos,
3887 &next_stop,
3888 (IT_CHARPOS (*it)
3889 + TEXT_PROP_DISTANCE_LIMIT),
3890 0,
3891 from_overlay);
3892 }
3893 else
3894 {
3895 bufpos = 0;
3896
3897 /* For strings from a `display' property, use the face at
3898 IT's current buffer position as the base face to merge
3899 with, so that overlay strings appear in the same face as
3900 surrounding text, unless they specify their own faces.
3901 For strings from wrap-prefix and line-prefix properties,
3902 use the default face, possibly remapped via
3903 Vface_remapping_alist. */
3904 base_face_id = it->string_from_prefix_prop_p
3905 ? (!NILP (Vface_remapping_alist)
3906 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3907 : DEFAULT_FACE_ID)
3908 : underlying_face_id (it);
3909 }
3910
3911 new_face_id = face_at_string_position (it->w,
3912 it->string,
3913 IT_STRING_CHARPOS (*it),
3914 bufpos,
3915 it->region_beg_charpos,
3916 it->region_end_charpos,
3917 &next_stop,
3918 base_face_id, 0);
3919
3920 /* Is this a start of a run of characters with box? Caveat:
3921 this can be called for a freshly allocated iterator; face_id
3922 is -1 is this case. We know that the new face will not
3923 change until the next check pos, i.e. if the new face has a
3924 box, all characters up to that position will have a
3925 box. But, as usual, we don't know whether that position
3926 is really the end. */
3927 if (new_face_id != it->face_id)
3928 {
3929 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3930 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3931
3932 /* If new face has a box but old face hasn't, this is the
3933 start of a run of characters with box, i.e. it has a
3934 shadow on the left side. */
3935 it->start_of_box_run_p
3936 = new_face->box && (old_face == NULL || !old_face->box);
3937 it->face_box_p = new_face->box != FACE_NO_BOX;
3938 }
3939 }
3940
3941 it->face_id = new_face_id;
3942 return HANDLED_NORMALLY;
3943 }
3944
3945
3946 /* Return the ID of the face ``underlying'' IT's current position,
3947 which is in a string. If the iterator is associated with a
3948 buffer, return the face at IT's current buffer position.
3949 Otherwise, use the iterator's base_face_id. */
3950
3951 static int
3952 underlying_face_id (struct it *it)
3953 {
3954 int face_id = it->base_face_id, i;
3955
3956 eassert (STRINGP (it->string));
3957
3958 for (i = it->sp - 1; i >= 0; --i)
3959 if (NILP (it->stack[i].string))
3960 face_id = it->stack[i].face_id;
3961
3962 return face_id;
3963 }
3964
3965
3966 /* Compute the face one character before or after the current position
3967 of IT, in the visual order. BEFORE_P non-zero means get the face
3968 in front (to the left in L2R paragraphs, to the right in R2L
3969 paragraphs) of IT's screen position. Value is the ID of the face. */
3970
3971 static int
3972 face_before_or_after_it_pos (struct it *it, int before_p)
3973 {
3974 int face_id, limit;
3975 ptrdiff_t next_check_charpos;
3976 struct it it_copy;
3977 void *it_copy_data = NULL;
3978
3979 eassert (it->s == NULL);
3980
3981 if (STRINGP (it->string))
3982 {
3983 ptrdiff_t bufpos, charpos;
3984 int base_face_id;
3985
3986 /* No face change past the end of the string (for the case
3987 we are padding with spaces). No face change before the
3988 string start. */
3989 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3990 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3991 return it->face_id;
3992
3993 if (!it->bidi_p)
3994 {
3995 /* Set charpos to the position before or after IT's current
3996 position, in the logical order, which in the non-bidi
3997 case is the same as the visual order. */
3998 if (before_p)
3999 charpos = IT_STRING_CHARPOS (*it) - 1;
4000 else if (it->what == IT_COMPOSITION)
4001 /* For composition, we must check the character after the
4002 composition. */
4003 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4004 else
4005 charpos = IT_STRING_CHARPOS (*it) + 1;
4006 }
4007 else
4008 {
4009 if (before_p)
4010 {
4011 /* With bidi iteration, the character before the current
4012 in the visual order cannot be found by simple
4013 iteration, because "reverse" reordering is not
4014 supported. Instead, we need to use the move_it_*
4015 family of functions. */
4016 /* Ignore face changes before the first visible
4017 character on this display line. */
4018 if (it->current_x <= it->first_visible_x)
4019 return it->face_id;
4020 SAVE_IT (it_copy, *it, it_copy_data);
4021 /* Implementation note: Since move_it_in_display_line
4022 works in the iterator geometry, and thinks the first
4023 character is always the leftmost, even in R2L lines,
4024 we don't need to distinguish between the R2L and L2R
4025 cases here. */
4026 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4027 it_copy.current_x - 1, MOVE_TO_X);
4028 charpos = IT_STRING_CHARPOS (it_copy);
4029 RESTORE_IT (it, it, it_copy_data);
4030 }
4031 else
4032 {
4033 /* Set charpos to the string position of the character
4034 that comes after IT's current position in the visual
4035 order. */
4036 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4037
4038 it_copy = *it;
4039 while (n--)
4040 bidi_move_to_visually_next (&it_copy.bidi_it);
4041
4042 charpos = it_copy.bidi_it.charpos;
4043 }
4044 }
4045 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4046
4047 if (it->current.overlay_string_index >= 0)
4048 bufpos = IT_CHARPOS (*it);
4049 else
4050 bufpos = 0;
4051
4052 base_face_id = underlying_face_id (it);
4053
4054 /* Get the face for ASCII, or unibyte. */
4055 face_id = face_at_string_position (it->w,
4056 it->string,
4057 charpos,
4058 bufpos,
4059 it->region_beg_charpos,
4060 it->region_end_charpos,
4061 &next_check_charpos,
4062 base_face_id, 0);
4063
4064 /* Correct the face for charsets different from ASCII. Do it
4065 for the multibyte case only. The face returned above is
4066 suitable for unibyte text if IT->string is unibyte. */
4067 if (STRING_MULTIBYTE (it->string))
4068 {
4069 struct text_pos pos1 = string_pos (charpos, it->string);
4070 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4071 int c, len;
4072 struct face *face = FACE_FROM_ID (it->f, face_id);
4073
4074 c = string_char_and_length (p, &len);
4075 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4076 }
4077 }
4078 else
4079 {
4080 struct text_pos pos;
4081
4082 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4083 || (IT_CHARPOS (*it) <= BEGV && before_p))
4084 return it->face_id;
4085
4086 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4087 pos = it->current.pos;
4088
4089 if (!it->bidi_p)
4090 {
4091 if (before_p)
4092 DEC_TEXT_POS (pos, it->multibyte_p);
4093 else
4094 {
4095 if (it->what == IT_COMPOSITION)
4096 {
4097 /* For composition, we must check the position after
4098 the composition. */
4099 pos.charpos += it->cmp_it.nchars;
4100 pos.bytepos += it->len;
4101 }
4102 else
4103 INC_TEXT_POS (pos, it->multibyte_p);
4104 }
4105 }
4106 else
4107 {
4108 if (before_p)
4109 {
4110 /* With bidi iteration, the character before the current
4111 in the visual order cannot be found by simple
4112 iteration, because "reverse" reordering is not
4113 supported. Instead, we need to use the move_it_*
4114 family of functions. */
4115 /* Ignore face changes before the first visible
4116 character on this display line. */
4117 if (it->current_x <= it->first_visible_x)
4118 return it->face_id;
4119 SAVE_IT (it_copy, *it, it_copy_data);
4120 /* Implementation note: Since move_it_in_display_line
4121 works in the iterator geometry, and thinks the first
4122 character is always the leftmost, even in R2L lines,
4123 we don't need to distinguish between the R2L and L2R
4124 cases here. */
4125 move_it_in_display_line (&it_copy, ZV,
4126 it_copy.current_x - 1, MOVE_TO_X);
4127 pos = it_copy.current.pos;
4128 RESTORE_IT (it, it, it_copy_data);
4129 }
4130 else
4131 {
4132 /* Set charpos to the buffer position of the character
4133 that comes after IT's current position in the visual
4134 order. */
4135 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4136
4137 it_copy = *it;
4138 while (n--)
4139 bidi_move_to_visually_next (&it_copy.bidi_it);
4140
4141 SET_TEXT_POS (pos,
4142 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4143 }
4144 }
4145 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4146
4147 /* Determine face for CHARSET_ASCII, or unibyte. */
4148 face_id = face_at_buffer_position (it->w,
4149 CHARPOS (pos),
4150 it->region_beg_charpos,
4151 it->region_end_charpos,
4152 &next_check_charpos,
4153 limit, 0, -1);
4154
4155 /* Correct the face for charsets different from ASCII. Do it
4156 for the multibyte case only. The face returned above is
4157 suitable for unibyte text if current_buffer is unibyte. */
4158 if (it->multibyte_p)
4159 {
4160 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4161 struct face *face = FACE_FROM_ID (it->f, face_id);
4162 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4163 }
4164 }
4165
4166 return face_id;
4167 }
4168
4169
4170 \f
4171 /***********************************************************************
4172 Invisible text
4173 ***********************************************************************/
4174
4175 /* Set up iterator IT from invisible properties at its current
4176 position. Called from handle_stop. */
4177
4178 static enum prop_handled
4179 handle_invisible_prop (struct it *it)
4180 {
4181 enum prop_handled handled = HANDLED_NORMALLY;
4182 int invis_p;
4183 Lisp_Object prop;
4184
4185 if (STRINGP (it->string))
4186 {
4187 Lisp_Object end_charpos, limit, charpos;
4188
4189 /* Get the value of the invisible text property at the
4190 current position. Value will be nil if there is no such
4191 property. */
4192 charpos = make_number (IT_STRING_CHARPOS (*it));
4193 prop = Fget_text_property (charpos, Qinvisible, it->string);
4194 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4195
4196 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4197 {
4198 /* Record whether we have to display an ellipsis for the
4199 invisible text. */
4200 int display_ellipsis_p = (invis_p == 2);
4201 ptrdiff_t len, endpos;
4202
4203 handled = HANDLED_RECOMPUTE_PROPS;
4204
4205 /* Get the position at which the next visible text can be
4206 found in IT->string, if any. */
4207 endpos = len = SCHARS (it->string);
4208 XSETINT (limit, len);
4209 do
4210 {
4211 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4212 it->string, limit);
4213 if (INTEGERP (end_charpos))
4214 {
4215 endpos = XFASTINT (end_charpos);
4216 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4217 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4218 if (invis_p == 2)
4219 display_ellipsis_p = 1;
4220 }
4221 }
4222 while (invis_p && endpos < len);
4223
4224 if (display_ellipsis_p)
4225 it->ellipsis_p = 1;
4226
4227 if (endpos < len)
4228 {
4229 /* Text at END_CHARPOS is visible. Move IT there. */
4230 struct text_pos old;
4231 ptrdiff_t oldpos;
4232
4233 old = it->current.string_pos;
4234 oldpos = CHARPOS (old);
4235 if (it->bidi_p)
4236 {
4237 if (it->bidi_it.first_elt
4238 && it->bidi_it.charpos < SCHARS (it->string))
4239 bidi_paragraph_init (it->paragraph_embedding,
4240 &it->bidi_it, 1);
4241 /* Bidi-iterate out of the invisible text. */
4242 do
4243 {
4244 bidi_move_to_visually_next (&it->bidi_it);
4245 }
4246 while (oldpos <= it->bidi_it.charpos
4247 && it->bidi_it.charpos < endpos);
4248
4249 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4250 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4251 if (IT_CHARPOS (*it) >= endpos)
4252 it->prev_stop = endpos;
4253 }
4254 else
4255 {
4256 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4257 compute_string_pos (&it->current.string_pos, old, it->string);
4258 }
4259 }
4260 else
4261 {
4262 /* The rest of the string is invisible. If this is an
4263 overlay string, proceed with the next overlay string
4264 or whatever comes and return a character from there. */
4265 if (it->current.overlay_string_index >= 0
4266 && !display_ellipsis_p)
4267 {
4268 next_overlay_string (it);
4269 /* Don't check for overlay strings when we just
4270 finished processing them. */
4271 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4272 }
4273 else
4274 {
4275 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4276 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4277 }
4278 }
4279 }
4280 }
4281 else
4282 {
4283 ptrdiff_t newpos, next_stop, start_charpos, tem;
4284 Lisp_Object pos, overlay;
4285
4286 /* First of all, is there invisible text at this position? */
4287 tem = start_charpos = IT_CHARPOS (*it);
4288 pos = make_number (tem);
4289 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4290 &overlay);
4291 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4292
4293 /* If we are on invisible text, skip over it. */
4294 if (invis_p && start_charpos < it->end_charpos)
4295 {
4296 /* Record whether we have to display an ellipsis for the
4297 invisible text. */
4298 int display_ellipsis_p = invis_p == 2;
4299
4300 handled = HANDLED_RECOMPUTE_PROPS;
4301
4302 /* Loop skipping over invisible text. The loop is left at
4303 ZV or with IT on the first char being visible again. */
4304 do
4305 {
4306 /* Try to skip some invisible text. Return value is the
4307 position reached which can be equal to where we start
4308 if there is nothing invisible there. This skips both
4309 over invisible text properties and overlays with
4310 invisible property. */
4311 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4312
4313 /* If we skipped nothing at all we weren't at invisible
4314 text in the first place. If everything to the end of
4315 the buffer was skipped, end the loop. */
4316 if (newpos == tem || newpos >= ZV)
4317 invis_p = 0;
4318 else
4319 {
4320 /* We skipped some characters but not necessarily
4321 all there are. Check if we ended up on visible
4322 text. Fget_char_property returns the property of
4323 the char before the given position, i.e. if we
4324 get invis_p = 0, this means that the char at
4325 newpos is visible. */
4326 pos = make_number (newpos);
4327 prop = Fget_char_property (pos, Qinvisible, it->window);
4328 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4329 }
4330
4331 /* If we ended up on invisible text, proceed to
4332 skip starting with next_stop. */
4333 if (invis_p)
4334 tem = next_stop;
4335
4336 /* If there are adjacent invisible texts, don't lose the
4337 second one's ellipsis. */
4338 if (invis_p == 2)
4339 display_ellipsis_p = 1;
4340 }
4341 while (invis_p);
4342
4343 /* The position newpos is now either ZV or on visible text. */
4344 if (it->bidi_p)
4345 {
4346 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4347 int on_newline =
4348 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4349 int after_newline =
4350 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4351
4352 /* If the invisible text ends on a newline or on a
4353 character after a newline, we can avoid the costly,
4354 character by character, bidi iteration to NEWPOS, and
4355 instead simply reseat the iterator there. That's
4356 because all bidi reordering information is tossed at
4357 the newline. This is a big win for modes that hide
4358 complete lines, like Outline, Org, etc. */
4359 if (on_newline || after_newline)
4360 {
4361 struct text_pos tpos;
4362 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4363
4364 SET_TEXT_POS (tpos, newpos, bpos);
4365 reseat_1 (it, tpos, 0);
4366 /* If we reseat on a newline/ZV, we need to prep the
4367 bidi iterator for advancing to the next character
4368 after the newline/EOB, keeping the current paragraph
4369 direction (so that PRODUCE_GLYPHS does TRT wrt
4370 prepending/appending glyphs to a glyph row). */
4371 if (on_newline)
4372 {
4373 it->bidi_it.first_elt = 0;
4374 it->bidi_it.paragraph_dir = pdir;
4375 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4376 it->bidi_it.nchars = 1;
4377 it->bidi_it.ch_len = 1;
4378 }
4379 }
4380 else /* Must use the slow method. */
4381 {
4382 /* With bidi iteration, the region of invisible text
4383 could start and/or end in the middle of a
4384 non-base embedding level. Therefore, we need to
4385 skip invisible text using the bidi iterator,
4386 starting at IT's current position, until we find
4387 ourselves outside of the invisible text.
4388 Skipping invisible text _after_ bidi iteration
4389 avoids affecting the visual order of the
4390 displayed text when invisible properties are
4391 added or removed. */
4392 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4393 {
4394 /* If we were `reseat'ed to a new paragraph,
4395 determine the paragraph base direction. We
4396 need to do it now because
4397 next_element_from_buffer may not have a
4398 chance to do it, if we are going to skip any
4399 text at the beginning, which resets the
4400 FIRST_ELT flag. */
4401 bidi_paragraph_init (it->paragraph_embedding,
4402 &it->bidi_it, 1);
4403 }
4404 do
4405 {
4406 bidi_move_to_visually_next (&it->bidi_it);
4407 }
4408 while (it->stop_charpos <= it->bidi_it.charpos
4409 && it->bidi_it.charpos < newpos);
4410 IT_CHARPOS (*it) = it->bidi_it.charpos;
4411 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4412 /* If we overstepped NEWPOS, record its position in
4413 the iterator, so that we skip invisible text if
4414 later the bidi iteration lands us in the
4415 invisible region again. */
4416 if (IT_CHARPOS (*it) >= newpos)
4417 it->prev_stop = newpos;
4418 }
4419 }
4420 else
4421 {
4422 IT_CHARPOS (*it) = newpos;
4423 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4424 }
4425
4426 /* If there are before-strings at the start of invisible
4427 text, and the text is invisible because of a text
4428 property, arrange to show before-strings because 20.x did
4429 it that way. (If the text is invisible because of an
4430 overlay property instead of a text property, this is
4431 already handled in the overlay code.) */
4432 if (NILP (overlay)
4433 && get_overlay_strings (it, it->stop_charpos))
4434 {
4435 handled = HANDLED_RECOMPUTE_PROPS;
4436 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4437 }
4438 else if (display_ellipsis_p)
4439 {
4440 /* Make sure that the glyphs of the ellipsis will get
4441 correct `charpos' values. If we would not update
4442 it->position here, the glyphs would belong to the
4443 last visible character _before_ the invisible
4444 text, which confuses `set_cursor_from_row'.
4445
4446 We use the last invisible position instead of the
4447 first because this way the cursor is always drawn on
4448 the first "." of the ellipsis, whenever PT is inside
4449 the invisible text. Otherwise the cursor would be
4450 placed _after_ the ellipsis when the point is after the
4451 first invisible character. */
4452 if (!STRINGP (it->object))
4453 {
4454 it->position.charpos = newpos - 1;
4455 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4456 }
4457 it->ellipsis_p = 1;
4458 /* Let the ellipsis display before
4459 considering any properties of the following char.
4460 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4461 handled = HANDLED_RETURN;
4462 }
4463 }
4464 }
4465
4466 return handled;
4467 }
4468
4469
4470 /* Make iterator IT return `...' next.
4471 Replaces LEN characters from buffer. */
4472
4473 static void
4474 setup_for_ellipsis (struct it *it, int len)
4475 {
4476 /* Use the display table definition for `...'. Invalid glyphs
4477 will be handled by the method returning elements from dpvec. */
4478 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4479 {
4480 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4481 it->dpvec = v->contents;
4482 it->dpend = v->contents + v->header.size;
4483 }
4484 else
4485 {
4486 /* Default `...'. */
4487 it->dpvec = default_invis_vector;
4488 it->dpend = default_invis_vector + 3;
4489 }
4490
4491 it->dpvec_char_len = len;
4492 it->current.dpvec_index = 0;
4493 it->dpvec_face_id = -1;
4494
4495 /* Remember the current face id in case glyphs specify faces.
4496 IT's face is restored in set_iterator_to_next.
4497 saved_face_id was set to preceding char's face in handle_stop. */
4498 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4499 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4500
4501 it->method = GET_FROM_DISPLAY_VECTOR;
4502 it->ellipsis_p = 1;
4503 }
4504
4505
4506 \f
4507 /***********************************************************************
4508 'display' property
4509 ***********************************************************************/
4510
4511 /* Set up iterator IT from `display' property at its current position.
4512 Called from handle_stop.
4513 We return HANDLED_RETURN if some part of the display property
4514 overrides the display of the buffer text itself.
4515 Otherwise we return HANDLED_NORMALLY. */
4516
4517 static enum prop_handled
4518 handle_display_prop (struct it *it)
4519 {
4520 Lisp_Object propval, object, overlay;
4521 struct text_pos *position;
4522 ptrdiff_t bufpos;
4523 /* Nonzero if some property replaces the display of the text itself. */
4524 int display_replaced_p = 0;
4525
4526 if (STRINGP (it->string))
4527 {
4528 object = it->string;
4529 position = &it->current.string_pos;
4530 bufpos = CHARPOS (it->current.pos);
4531 }
4532 else
4533 {
4534 XSETWINDOW (object, it->w);
4535 position = &it->current.pos;
4536 bufpos = CHARPOS (*position);
4537 }
4538
4539 /* Reset those iterator values set from display property values. */
4540 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4541 it->space_width = Qnil;
4542 it->font_height = Qnil;
4543 it->voffset = 0;
4544
4545 /* We don't support recursive `display' properties, i.e. string
4546 values that have a string `display' property, that have a string
4547 `display' property etc. */
4548 if (!it->string_from_display_prop_p)
4549 it->area = TEXT_AREA;
4550
4551 propval = get_char_property_and_overlay (make_number (position->charpos),
4552 Qdisplay, object, &overlay);
4553 if (NILP (propval))
4554 return HANDLED_NORMALLY;
4555 /* Now OVERLAY is the overlay that gave us this property, or nil
4556 if it was a text property. */
4557
4558 if (!STRINGP (it->string))
4559 object = it->w->contents;
4560
4561 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4562 position, bufpos,
4563 FRAME_WINDOW_P (it->f));
4564
4565 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4566 }
4567
4568 /* Subroutine of handle_display_prop. Returns non-zero if the display
4569 specification in SPEC is a replacing specification, i.e. it would
4570 replace the text covered by `display' property with something else,
4571 such as an image or a display string. If SPEC includes any kind or
4572 `(space ...) specification, the value is 2; this is used by
4573 compute_display_string_pos, which see.
4574
4575 See handle_single_display_spec for documentation of arguments.
4576 frame_window_p is non-zero if the window being redisplayed is on a
4577 GUI frame; this argument is used only if IT is NULL, see below.
4578
4579 IT can be NULL, if this is called by the bidi reordering code
4580 through compute_display_string_pos, which see. In that case, this
4581 function only examines SPEC, but does not otherwise "handle" it, in
4582 the sense that it doesn't set up members of IT from the display
4583 spec. */
4584 static int
4585 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4586 Lisp_Object overlay, struct text_pos *position,
4587 ptrdiff_t bufpos, int frame_window_p)
4588 {
4589 int replacing_p = 0;
4590 int rv;
4591
4592 if (CONSP (spec)
4593 /* Simple specifications. */
4594 && !EQ (XCAR (spec), Qimage)
4595 && !EQ (XCAR (spec), Qspace)
4596 && !EQ (XCAR (spec), Qwhen)
4597 && !EQ (XCAR (spec), Qslice)
4598 && !EQ (XCAR (spec), Qspace_width)
4599 && !EQ (XCAR (spec), Qheight)
4600 && !EQ (XCAR (spec), Qraise)
4601 /* Marginal area specifications. */
4602 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4603 && !EQ (XCAR (spec), Qleft_fringe)
4604 && !EQ (XCAR (spec), Qright_fringe)
4605 && !NILP (XCAR (spec)))
4606 {
4607 for (; CONSP (spec); spec = XCDR (spec))
4608 {
4609 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4610 overlay, position, bufpos,
4611 replacing_p, frame_window_p)))
4612 {
4613 replacing_p = rv;
4614 /* If some text in a string is replaced, `position' no
4615 longer points to the position of `object'. */
4616 if (!it || STRINGP (object))
4617 break;
4618 }
4619 }
4620 }
4621 else if (VECTORP (spec))
4622 {
4623 ptrdiff_t i;
4624 for (i = 0; i < ASIZE (spec); ++i)
4625 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4626 overlay, position, bufpos,
4627 replacing_p, frame_window_p)))
4628 {
4629 replacing_p = rv;
4630 /* If some text in a string is replaced, `position' no
4631 longer points to the position of `object'. */
4632 if (!it || STRINGP (object))
4633 break;
4634 }
4635 }
4636 else
4637 {
4638 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4639 position, bufpos, 0,
4640 frame_window_p)))
4641 replacing_p = rv;
4642 }
4643
4644 return replacing_p;
4645 }
4646
4647 /* Value is the position of the end of the `display' property starting
4648 at START_POS in OBJECT. */
4649
4650 static struct text_pos
4651 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4652 {
4653 Lisp_Object end;
4654 struct text_pos end_pos;
4655
4656 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4657 Qdisplay, object, Qnil);
4658 CHARPOS (end_pos) = XFASTINT (end);
4659 if (STRINGP (object))
4660 compute_string_pos (&end_pos, start_pos, it->string);
4661 else
4662 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4663
4664 return end_pos;
4665 }
4666
4667
4668 /* Set up IT from a single `display' property specification SPEC. OBJECT
4669 is the object in which the `display' property was found. *POSITION
4670 is the position in OBJECT at which the `display' property was found.
4671 BUFPOS is the buffer position of OBJECT (different from POSITION if
4672 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4673 previously saw a display specification which already replaced text
4674 display with something else, for example an image; we ignore such
4675 properties after the first one has been processed.
4676
4677 OVERLAY is the overlay this `display' property came from,
4678 or nil if it was a text property.
4679
4680 If SPEC is a `space' or `image' specification, and in some other
4681 cases too, set *POSITION to the position where the `display'
4682 property ends.
4683
4684 If IT is NULL, only examine the property specification in SPEC, but
4685 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4686 is intended to be displayed in a window on a GUI frame.
4687
4688 Value is non-zero if something was found which replaces the display
4689 of buffer or string text. */
4690
4691 static int
4692 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4693 Lisp_Object overlay, struct text_pos *position,
4694 ptrdiff_t bufpos, int display_replaced_p,
4695 int frame_window_p)
4696 {
4697 Lisp_Object form;
4698 Lisp_Object location, value;
4699 struct text_pos start_pos = *position;
4700 int valid_p;
4701
4702 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4703 If the result is non-nil, use VALUE instead of SPEC. */
4704 form = Qt;
4705 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4706 {
4707 spec = XCDR (spec);
4708 if (!CONSP (spec))
4709 return 0;
4710 form = XCAR (spec);
4711 spec = XCDR (spec);
4712 }
4713
4714 if (!NILP (form) && !EQ (form, Qt))
4715 {
4716 ptrdiff_t count = SPECPDL_INDEX ();
4717 struct gcpro gcpro1;
4718
4719 /* Bind `object' to the object having the `display' property, a
4720 buffer or string. Bind `position' to the position in the
4721 object where the property was found, and `buffer-position'
4722 to the current position in the buffer. */
4723
4724 if (NILP (object))
4725 XSETBUFFER (object, current_buffer);
4726 specbind (Qobject, object);
4727 specbind (Qposition, make_number (CHARPOS (*position)));
4728 specbind (Qbuffer_position, make_number (bufpos));
4729 GCPRO1 (form);
4730 form = safe_eval (form);
4731 UNGCPRO;
4732 unbind_to (count, Qnil);
4733 }
4734
4735 if (NILP (form))
4736 return 0;
4737
4738 /* Handle `(height HEIGHT)' specifications. */
4739 if (CONSP (spec)
4740 && EQ (XCAR (spec), Qheight)
4741 && CONSP (XCDR (spec)))
4742 {
4743 if (it)
4744 {
4745 if (!FRAME_WINDOW_P (it->f))
4746 return 0;
4747
4748 it->font_height = XCAR (XCDR (spec));
4749 if (!NILP (it->font_height))
4750 {
4751 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4752 int new_height = -1;
4753
4754 if (CONSP (it->font_height)
4755 && (EQ (XCAR (it->font_height), Qplus)
4756 || EQ (XCAR (it->font_height), Qminus))
4757 && CONSP (XCDR (it->font_height))
4758 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4759 {
4760 /* `(+ N)' or `(- N)' where N is an integer. */
4761 int steps = XINT (XCAR (XCDR (it->font_height)));
4762 if (EQ (XCAR (it->font_height), Qplus))
4763 steps = - steps;
4764 it->face_id = smaller_face (it->f, it->face_id, steps);
4765 }
4766 else if (FUNCTIONP (it->font_height))
4767 {
4768 /* Call function with current height as argument.
4769 Value is the new height. */
4770 Lisp_Object height;
4771 height = safe_call1 (it->font_height,
4772 face->lface[LFACE_HEIGHT_INDEX]);
4773 if (NUMBERP (height))
4774 new_height = XFLOATINT (height);
4775 }
4776 else if (NUMBERP (it->font_height))
4777 {
4778 /* Value is a multiple of the canonical char height. */
4779 struct face *f;
4780
4781 f = FACE_FROM_ID (it->f,
4782 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4783 new_height = (XFLOATINT (it->font_height)
4784 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4785 }
4786 else
4787 {
4788 /* Evaluate IT->font_height with `height' bound to the
4789 current specified height to get the new height. */
4790 ptrdiff_t count = SPECPDL_INDEX ();
4791
4792 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4793 value = safe_eval (it->font_height);
4794 unbind_to (count, Qnil);
4795
4796 if (NUMBERP (value))
4797 new_height = XFLOATINT (value);
4798 }
4799
4800 if (new_height > 0)
4801 it->face_id = face_with_height (it->f, it->face_id, new_height);
4802 }
4803 }
4804
4805 return 0;
4806 }
4807
4808 /* Handle `(space-width WIDTH)'. */
4809 if (CONSP (spec)
4810 && EQ (XCAR (spec), Qspace_width)
4811 && CONSP (XCDR (spec)))
4812 {
4813 if (it)
4814 {
4815 if (!FRAME_WINDOW_P (it->f))
4816 return 0;
4817
4818 value = XCAR (XCDR (spec));
4819 if (NUMBERP (value) && XFLOATINT (value) > 0)
4820 it->space_width = value;
4821 }
4822
4823 return 0;
4824 }
4825
4826 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4827 if (CONSP (spec)
4828 && EQ (XCAR (spec), Qslice))
4829 {
4830 Lisp_Object tem;
4831
4832 if (it)
4833 {
4834 if (!FRAME_WINDOW_P (it->f))
4835 return 0;
4836
4837 if (tem = XCDR (spec), CONSP (tem))
4838 {
4839 it->slice.x = XCAR (tem);
4840 if (tem = XCDR (tem), CONSP (tem))
4841 {
4842 it->slice.y = XCAR (tem);
4843 if (tem = XCDR (tem), CONSP (tem))
4844 {
4845 it->slice.width = XCAR (tem);
4846 if (tem = XCDR (tem), CONSP (tem))
4847 it->slice.height = XCAR (tem);
4848 }
4849 }
4850 }
4851 }
4852
4853 return 0;
4854 }
4855
4856 /* Handle `(raise FACTOR)'. */
4857 if (CONSP (spec)
4858 && EQ (XCAR (spec), Qraise)
4859 && CONSP (XCDR (spec)))
4860 {
4861 if (it)
4862 {
4863 if (!FRAME_WINDOW_P (it->f))
4864 return 0;
4865
4866 #ifdef HAVE_WINDOW_SYSTEM
4867 value = XCAR (XCDR (spec));
4868 if (NUMBERP (value))
4869 {
4870 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4871 it->voffset = - (XFLOATINT (value)
4872 * (FONT_HEIGHT (face->font)));
4873 }
4874 #endif /* HAVE_WINDOW_SYSTEM */
4875 }
4876
4877 return 0;
4878 }
4879
4880 /* Don't handle the other kinds of display specifications
4881 inside a string that we got from a `display' property. */
4882 if (it && it->string_from_display_prop_p)
4883 return 0;
4884
4885 /* Characters having this form of property are not displayed, so
4886 we have to find the end of the property. */
4887 if (it)
4888 {
4889 start_pos = *position;
4890 *position = display_prop_end (it, object, start_pos);
4891 }
4892 value = Qnil;
4893
4894 /* Stop the scan at that end position--we assume that all
4895 text properties change there. */
4896 if (it)
4897 it->stop_charpos = position->charpos;
4898
4899 /* Handle `(left-fringe BITMAP [FACE])'
4900 and `(right-fringe BITMAP [FACE])'. */
4901 if (CONSP (spec)
4902 && (EQ (XCAR (spec), Qleft_fringe)
4903 || EQ (XCAR (spec), Qright_fringe))
4904 && CONSP (XCDR (spec)))
4905 {
4906 int fringe_bitmap;
4907
4908 if (it)
4909 {
4910 if (!FRAME_WINDOW_P (it->f))
4911 /* If we return here, POSITION has been advanced
4912 across the text with this property. */
4913 {
4914 /* Synchronize the bidi iterator with POSITION. This is
4915 needed because we are not going to push the iterator
4916 on behalf of this display property, so there will be
4917 no pop_it call to do this synchronization for us. */
4918 if (it->bidi_p)
4919 {
4920 it->position = *position;
4921 iterate_out_of_display_property (it);
4922 *position = it->position;
4923 }
4924 return 1;
4925 }
4926 }
4927 else if (!frame_window_p)
4928 return 1;
4929
4930 #ifdef HAVE_WINDOW_SYSTEM
4931 value = XCAR (XCDR (spec));
4932 if (!SYMBOLP (value)
4933 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4934 /* If we return here, POSITION has been advanced
4935 across the text with this property. */
4936 {
4937 if (it && it->bidi_p)
4938 {
4939 it->position = *position;
4940 iterate_out_of_display_property (it);
4941 *position = it->position;
4942 }
4943 return 1;
4944 }
4945
4946 if (it)
4947 {
4948 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4949
4950 if (CONSP (XCDR (XCDR (spec))))
4951 {
4952 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4953 int face_id2 = lookup_derived_face (it->f, face_name,
4954 FRINGE_FACE_ID, 0);
4955 if (face_id2 >= 0)
4956 face_id = face_id2;
4957 }
4958
4959 /* Save current settings of IT so that we can restore them
4960 when we are finished with the glyph property value. */
4961 push_it (it, position);
4962
4963 it->area = TEXT_AREA;
4964 it->what = IT_IMAGE;
4965 it->image_id = -1; /* no image */
4966 it->position = start_pos;
4967 it->object = NILP (object) ? it->w->contents : object;
4968 it->method = GET_FROM_IMAGE;
4969 it->from_overlay = Qnil;
4970 it->face_id = face_id;
4971 it->from_disp_prop_p = 1;
4972
4973 /* Say that we haven't consumed the characters with
4974 `display' property yet. The call to pop_it in
4975 set_iterator_to_next will clean this up. */
4976 *position = start_pos;
4977
4978 if (EQ (XCAR (spec), Qleft_fringe))
4979 {
4980 it->left_user_fringe_bitmap = fringe_bitmap;
4981 it->left_user_fringe_face_id = face_id;
4982 }
4983 else
4984 {
4985 it->right_user_fringe_bitmap = fringe_bitmap;
4986 it->right_user_fringe_face_id = face_id;
4987 }
4988 }
4989 #endif /* HAVE_WINDOW_SYSTEM */
4990 return 1;
4991 }
4992
4993 /* Prepare to handle `((margin left-margin) ...)',
4994 `((margin right-margin) ...)' and `((margin nil) ...)'
4995 prefixes for display specifications. */
4996 location = Qunbound;
4997 if (CONSP (spec) && CONSP (XCAR (spec)))
4998 {
4999 Lisp_Object tem;
5000
5001 value = XCDR (spec);
5002 if (CONSP (value))
5003 value = XCAR (value);
5004
5005 tem = XCAR (spec);
5006 if (EQ (XCAR (tem), Qmargin)
5007 && (tem = XCDR (tem),
5008 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5009 (NILP (tem)
5010 || EQ (tem, Qleft_margin)
5011 || EQ (tem, Qright_margin))))
5012 location = tem;
5013 }
5014
5015 if (EQ (location, Qunbound))
5016 {
5017 location = Qnil;
5018 value = spec;
5019 }
5020
5021 /* After this point, VALUE is the property after any
5022 margin prefix has been stripped. It must be a string,
5023 an image specification, or `(space ...)'.
5024
5025 LOCATION specifies where to display: `left-margin',
5026 `right-margin' or nil. */
5027
5028 valid_p = (STRINGP (value)
5029 #ifdef HAVE_WINDOW_SYSTEM
5030 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5031 && valid_image_p (value))
5032 #endif /* not HAVE_WINDOW_SYSTEM */
5033 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5034
5035 if (valid_p && !display_replaced_p)
5036 {
5037 int retval = 1;
5038
5039 if (!it)
5040 {
5041 /* Callers need to know whether the display spec is any kind
5042 of `(space ...)' spec that is about to affect text-area
5043 display. */
5044 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5045 retval = 2;
5046 return retval;
5047 }
5048
5049 /* Save current settings of IT so that we can restore them
5050 when we are finished with the glyph property value. */
5051 push_it (it, position);
5052 it->from_overlay = overlay;
5053 it->from_disp_prop_p = 1;
5054
5055 if (NILP (location))
5056 it->area = TEXT_AREA;
5057 else if (EQ (location, Qleft_margin))
5058 it->area = LEFT_MARGIN_AREA;
5059 else
5060 it->area = RIGHT_MARGIN_AREA;
5061
5062 if (STRINGP (value))
5063 {
5064 it->string = value;
5065 it->multibyte_p = STRING_MULTIBYTE (it->string);
5066 it->current.overlay_string_index = -1;
5067 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5068 it->end_charpos = it->string_nchars = SCHARS (it->string);
5069 it->method = GET_FROM_STRING;
5070 it->stop_charpos = 0;
5071 it->prev_stop = 0;
5072 it->base_level_stop = 0;
5073 it->string_from_display_prop_p = 1;
5074 /* Say that we haven't consumed the characters with
5075 `display' property yet. The call to pop_it in
5076 set_iterator_to_next will clean this up. */
5077 if (BUFFERP (object))
5078 *position = start_pos;
5079
5080 /* Force paragraph direction to be that of the parent
5081 object. If the parent object's paragraph direction is
5082 not yet determined, default to L2R. */
5083 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5084 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5085 else
5086 it->paragraph_embedding = L2R;
5087
5088 /* Set up the bidi iterator for this display string. */
5089 if (it->bidi_p)
5090 {
5091 it->bidi_it.string.lstring = it->string;
5092 it->bidi_it.string.s = NULL;
5093 it->bidi_it.string.schars = it->end_charpos;
5094 it->bidi_it.string.bufpos = bufpos;
5095 it->bidi_it.string.from_disp_str = 1;
5096 it->bidi_it.string.unibyte = !it->multibyte_p;
5097 it->bidi_it.w = it->w;
5098 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5099 }
5100 }
5101 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5102 {
5103 it->method = GET_FROM_STRETCH;
5104 it->object = value;
5105 *position = it->position = start_pos;
5106 retval = 1 + (it->area == TEXT_AREA);
5107 }
5108 #ifdef HAVE_WINDOW_SYSTEM
5109 else
5110 {
5111 it->what = IT_IMAGE;
5112 it->image_id = lookup_image (it->f, value);
5113 it->position = start_pos;
5114 it->object = NILP (object) ? it->w->contents : object;
5115 it->method = GET_FROM_IMAGE;
5116
5117 /* Say that we haven't consumed the characters with
5118 `display' property yet. The call to pop_it in
5119 set_iterator_to_next will clean this up. */
5120 *position = start_pos;
5121 }
5122 #endif /* HAVE_WINDOW_SYSTEM */
5123
5124 return retval;
5125 }
5126
5127 /* Invalid property or property not supported. Restore
5128 POSITION to what it was before. */
5129 *position = start_pos;
5130 return 0;
5131 }
5132
5133 /* Check if PROP is a display property value whose text should be
5134 treated as intangible. OVERLAY is the overlay from which PROP
5135 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5136 specify the buffer position covered by PROP. */
5137
5138 int
5139 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5140 ptrdiff_t charpos, ptrdiff_t bytepos)
5141 {
5142 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5143 struct text_pos position;
5144
5145 SET_TEXT_POS (position, charpos, bytepos);
5146 return handle_display_spec (NULL, prop, Qnil, overlay,
5147 &position, charpos, frame_window_p);
5148 }
5149
5150
5151 /* Return 1 if PROP is a display sub-property value containing STRING.
5152
5153 Implementation note: this and the following function are really
5154 special cases of handle_display_spec and
5155 handle_single_display_spec, and should ideally use the same code.
5156 Until they do, these two pairs must be consistent and must be
5157 modified in sync. */
5158
5159 static int
5160 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5161 {
5162 if (EQ (string, prop))
5163 return 1;
5164
5165 /* Skip over `when FORM'. */
5166 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5167 {
5168 prop = XCDR (prop);
5169 if (!CONSP (prop))
5170 return 0;
5171 /* Actually, the condition following `when' should be eval'ed,
5172 like handle_single_display_spec does, and we should return
5173 zero if it evaluates to nil. However, this function is
5174 called only when the buffer was already displayed and some
5175 glyph in the glyph matrix was found to come from a display
5176 string. Therefore, the condition was already evaluated, and
5177 the result was non-nil, otherwise the display string wouldn't
5178 have been displayed and we would have never been called for
5179 this property. Thus, we can skip the evaluation and assume
5180 its result is non-nil. */
5181 prop = XCDR (prop);
5182 }
5183
5184 if (CONSP (prop))
5185 /* Skip over `margin LOCATION'. */
5186 if (EQ (XCAR (prop), Qmargin))
5187 {
5188 prop = XCDR (prop);
5189 if (!CONSP (prop))
5190 return 0;
5191
5192 prop = XCDR (prop);
5193 if (!CONSP (prop))
5194 return 0;
5195 }
5196
5197 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5198 }
5199
5200
5201 /* Return 1 if STRING appears in the `display' property PROP. */
5202
5203 static int
5204 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5205 {
5206 if (CONSP (prop)
5207 && !EQ (XCAR (prop), Qwhen)
5208 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5209 {
5210 /* A list of sub-properties. */
5211 while (CONSP (prop))
5212 {
5213 if (single_display_spec_string_p (XCAR (prop), string))
5214 return 1;
5215 prop = XCDR (prop);
5216 }
5217 }
5218 else if (VECTORP (prop))
5219 {
5220 /* A vector of sub-properties. */
5221 ptrdiff_t i;
5222 for (i = 0; i < ASIZE (prop); ++i)
5223 if (single_display_spec_string_p (AREF (prop, i), string))
5224 return 1;
5225 }
5226 else
5227 return single_display_spec_string_p (prop, string);
5228
5229 return 0;
5230 }
5231
5232 /* Look for STRING in overlays and text properties in the current
5233 buffer, between character positions FROM and TO (excluding TO).
5234 BACK_P non-zero means look back (in this case, TO is supposed to be
5235 less than FROM).
5236 Value is the first character position where STRING was found, or
5237 zero if it wasn't found before hitting TO.
5238
5239 This function may only use code that doesn't eval because it is
5240 called asynchronously from note_mouse_highlight. */
5241
5242 static ptrdiff_t
5243 string_buffer_position_lim (Lisp_Object string,
5244 ptrdiff_t from, ptrdiff_t to, int back_p)
5245 {
5246 Lisp_Object limit, prop, pos;
5247 int found = 0;
5248
5249 pos = make_number (max (from, BEGV));
5250
5251 if (!back_p) /* looking forward */
5252 {
5253 limit = make_number (min (to, ZV));
5254 while (!found && !EQ (pos, limit))
5255 {
5256 prop = Fget_char_property (pos, Qdisplay, Qnil);
5257 if (!NILP (prop) && display_prop_string_p (prop, string))
5258 found = 1;
5259 else
5260 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5261 limit);
5262 }
5263 }
5264 else /* looking back */
5265 {
5266 limit = make_number (max (to, BEGV));
5267 while (!found && !EQ (pos, limit))
5268 {
5269 prop = Fget_char_property (pos, Qdisplay, Qnil);
5270 if (!NILP (prop) && display_prop_string_p (prop, string))
5271 found = 1;
5272 else
5273 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5274 limit);
5275 }
5276 }
5277
5278 return found ? XINT (pos) : 0;
5279 }
5280
5281 /* Determine which buffer position in current buffer STRING comes from.
5282 AROUND_CHARPOS is an approximate position where it could come from.
5283 Value is the buffer position or 0 if it couldn't be determined.
5284
5285 This function is necessary because we don't record buffer positions
5286 in glyphs generated from strings (to keep struct glyph small).
5287 This function may only use code that doesn't eval because it is
5288 called asynchronously from note_mouse_highlight. */
5289
5290 static ptrdiff_t
5291 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5292 {
5293 const int MAX_DISTANCE = 1000;
5294 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5295 around_charpos + MAX_DISTANCE,
5296 0);
5297
5298 if (!found)
5299 found = string_buffer_position_lim (string, around_charpos,
5300 around_charpos - MAX_DISTANCE, 1);
5301 return found;
5302 }
5303
5304
5305 \f
5306 /***********************************************************************
5307 `composition' property
5308 ***********************************************************************/
5309
5310 /* Set up iterator IT from `composition' property at its current
5311 position. Called from handle_stop. */
5312
5313 static enum prop_handled
5314 handle_composition_prop (struct it *it)
5315 {
5316 Lisp_Object prop, string;
5317 ptrdiff_t pos, pos_byte, start, end;
5318
5319 if (STRINGP (it->string))
5320 {
5321 unsigned char *s;
5322
5323 pos = IT_STRING_CHARPOS (*it);
5324 pos_byte = IT_STRING_BYTEPOS (*it);
5325 string = it->string;
5326 s = SDATA (string) + pos_byte;
5327 it->c = STRING_CHAR (s);
5328 }
5329 else
5330 {
5331 pos = IT_CHARPOS (*it);
5332 pos_byte = IT_BYTEPOS (*it);
5333 string = Qnil;
5334 it->c = FETCH_CHAR (pos_byte);
5335 }
5336
5337 /* If there's a valid composition and point is not inside of the
5338 composition (in the case that the composition is from the current
5339 buffer), draw a glyph composed from the composition components. */
5340 if (find_composition (pos, -1, &start, &end, &prop, string)
5341 && composition_valid_p (start, end, prop)
5342 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5343 {
5344 if (start < pos)
5345 /* As we can't handle this situation (perhaps font-lock added
5346 a new composition), we just return here hoping that next
5347 redisplay will detect this composition much earlier. */
5348 return HANDLED_NORMALLY;
5349 if (start != pos)
5350 {
5351 if (STRINGP (it->string))
5352 pos_byte = string_char_to_byte (it->string, start);
5353 else
5354 pos_byte = CHAR_TO_BYTE (start);
5355 }
5356 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5357 prop, string);
5358
5359 if (it->cmp_it.id >= 0)
5360 {
5361 it->cmp_it.ch = -1;
5362 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5363 it->cmp_it.nglyphs = -1;
5364 }
5365 }
5366
5367 return HANDLED_NORMALLY;
5368 }
5369
5370
5371 \f
5372 /***********************************************************************
5373 Overlay strings
5374 ***********************************************************************/
5375
5376 /* The following structure is used to record overlay strings for
5377 later sorting in load_overlay_strings. */
5378
5379 struct overlay_entry
5380 {
5381 Lisp_Object overlay;
5382 Lisp_Object string;
5383 EMACS_INT priority;
5384 int after_string_p;
5385 };
5386
5387
5388 /* Set up iterator IT from overlay strings at its current position.
5389 Called from handle_stop. */
5390
5391 static enum prop_handled
5392 handle_overlay_change (struct it *it)
5393 {
5394 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5395 return HANDLED_RECOMPUTE_PROPS;
5396 else
5397 return HANDLED_NORMALLY;
5398 }
5399
5400
5401 /* Set up the next overlay string for delivery by IT, if there is an
5402 overlay string to deliver. Called by set_iterator_to_next when the
5403 end of the current overlay string is reached. If there are more
5404 overlay strings to display, IT->string and
5405 IT->current.overlay_string_index are set appropriately here.
5406 Otherwise IT->string is set to nil. */
5407
5408 static void
5409 next_overlay_string (struct it *it)
5410 {
5411 ++it->current.overlay_string_index;
5412 if (it->current.overlay_string_index == it->n_overlay_strings)
5413 {
5414 /* No more overlay strings. Restore IT's settings to what
5415 they were before overlay strings were processed, and
5416 continue to deliver from current_buffer. */
5417
5418 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5419 pop_it (it);
5420 eassert (it->sp > 0
5421 || (NILP (it->string)
5422 && it->method == GET_FROM_BUFFER
5423 && it->stop_charpos >= BEGV
5424 && it->stop_charpos <= it->end_charpos));
5425 it->current.overlay_string_index = -1;
5426 it->n_overlay_strings = 0;
5427 it->overlay_strings_charpos = -1;
5428 /* If there's an empty display string on the stack, pop the
5429 stack, to resync the bidi iterator with IT's position. Such
5430 empty strings are pushed onto the stack in
5431 get_overlay_strings_1. */
5432 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5433 pop_it (it);
5434
5435 /* If we're at the end of the buffer, record that we have
5436 processed the overlay strings there already, so that
5437 next_element_from_buffer doesn't try it again. */
5438 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5439 it->overlay_strings_at_end_processed_p = 1;
5440 }
5441 else
5442 {
5443 /* There are more overlay strings to process. If
5444 IT->current.overlay_string_index has advanced to a position
5445 where we must load IT->overlay_strings with more strings, do
5446 it. We must load at the IT->overlay_strings_charpos where
5447 IT->n_overlay_strings was originally computed; when invisible
5448 text is present, this might not be IT_CHARPOS (Bug#7016). */
5449 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5450
5451 if (it->current.overlay_string_index && i == 0)
5452 load_overlay_strings (it, it->overlay_strings_charpos);
5453
5454 /* Initialize IT to deliver display elements from the overlay
5455 string. */
5456 it->string = it->overlay_strings[i];
5457 it->multibyte_p = STRING_MULTIBYTE (it->string);
5458 SET_TEXT_POS (it->current.string_pos, 0, 0);
5459 it->method = GET_FROM_STRING;
5460 it->stop_charpos = 0;
5461 it->end_charpos = SCHARS (it->string);
5462 if (it->cmp_it.stop_pos >= 0)
5463 it->cmp_it.stop_pos = 0;
5464 it->prev_stop = 0;
5465 it->base_level_stop = 0;
5466
5467 /* Set up the bidi iterator for this overlay string. */
5468 if (it->bidi_p)
5469 {
5470 it->bidi_it.string.lstring = it->string;
5471 it->bidi_it.string.s = NULL;
5472 it->bidi_it.string.schars = SCHARS (it->string);
5473 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5474 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5475 it->bidi_it.string.unibyte = !it->multibyte_p;
5476 it->bidi_it.w = it->w;
5477 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5478 }
5479 }
5480
5481 CHECK_IT (it);
5482 }
5483
5484
5485 /* Compare two overlay_entry structures E1 and E2. Used as a
5486 comparison function for qsort in load_overlay_strings. Overlay
5487 strings for the same position are sorted so that
5488
5489 1. All after-strings come in front of before-strings, except
5490 when they come from the same overlay.
5491
5492 2. Within after-strings, strings are sorted so that overlay strings
5493 from overlays with higher priorities come first.
5494
5495 2. Within before-strings, strings are sorted so that overlay
5496 strings from overlays with higher priorities come last.
5497
5498 Value is analogous to strcmp. */
5499
5500
5501 static int
5502 compare_overlay_entries (const void *e1, const void *e2)
5503 {
5504 struct overlay_entry const *entry1 = e1;
5505 struct overlay_entry const *entry2 = e2;
5506 int result;
5507
5508 if (entry1->after_string_p != entry2->after_string_p)
5509 {
5510 /* Let after-strings appear in front of before-strings if
5511 they come from different overlays. */
5512 if (EQ (entry1->overlay, entry2->overlay))
5513 result = entry1->after_string_p ? 1 : -1;
5514 else
5515 result = entry1->after_string_p ? -1 : 1;
5516 }
5517 else if (entry1->priority != entry2->priority)
5518 {
5519 if (entry1->after_string_p)
5520 /* After-strings sorted in order of decreasing priority. */
5521 result = entry2->priority < entry1->priority ? -1 : 1;
5522 else
5523 /* Before-strings sorted in order of increasing priority. */
5524 result = entry1->priority < entry2->priority ? -1 : 1;
5525 }
5526 else
5527 result = 0;
5528
5529 return result;
5530 }
5531
5532
5533 /* Load the vector IT->overlay_strings with overlay strings from IT's
5534 current buffer position, or from CHARPOS if that is > 0. Set
5535 IT->n_overlays to the total number of overlay strings found.
5536
5537 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5538 a time. On entry into load_overlay_strings,
5539 IT->current.overlay_string_index gives the number of overlay
5540 strings that have already been loaded by previous calls to this
5541 function.
5542
5543 IT->add_overlay_start contains an additional overlay start
5544 position to consider for taking overlay strings from, if non-zero.
5545 This position comes into play when the overlay has an `invisible'
5546 property, and both before and after-strings. When we've skipped to
5547 the end of the overlay, because of its `invisible' property, we
5548 nevertheless want its before-string to appear.
5549 IT->add_overlay_start will contain the overlay start position
5550 in this case.
5551
5552 Overlay strings are sorted so that after-string strings come in
5553 front of before-string strings. Within before and after-strings,
5554 strings are sorted by overlay priority. See also function
5555 compare_overlay_entries. */
5556
5557 static void
5558 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5559 {
5560 Lisp_Object overlay, window, str, invisible;
5561 struct Lisp_Overlay *ov;
5562 ptrdiff_t start, end;
5563 ptrdiff_t size = 20;
5564 ptrdiff_t n = 0, i, j;
5565 int invis_p;
5566 struct overlay_entry *entries = alloca (size * sizeof *entries);
5567 USE_SAFE_ALLOCA;
5568
5569 if (charpos <= 0)
5570 charpos = IT_CHARPOS (*it);
5571
5572 /* Append the overlay string STRING of overlay OVERLAY to vector
5573 `entries' which has size `size' and currently contains `n'
5574 elements. AFTER_P non-zero means STRING is an after-string of
5575 OVERLAY. */
5576 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5577 do \
5578 { \
5579 Lisp_Object priority; \
5580 \
5581 if (n == size) \
5582 { \
5583 struct overlay_entry *old = entries; \
5584 SAFE_NALLOCA (entries, 2, size); \
5585 memcpy (entries, old, size * sizeof *entries); \
5586 size *= 2; \
5587 } \
5588 \
5589 entries[n].string = (STRING); \
5590 entries[n].overlay = (OVERLAY); \
5591 priority = Foverlay_get ((OVERLAY), Qpriority); \
5592 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5593 entries[n].after_string_p = (AFTER_P); \
5594 ++n; \
5595 } \
5596 while (0)
5597
5598 /* Process overlay before the overlay center. */
5599 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5600 {
5601 XSETMISC (overlay, ov);
5602 eassert (OVERLAYP (overlay));
5603 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5604 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5605
5606 if (end < charpos)
5607 break;
5608
5609 /* Skip this overlay if it doesn't start or end at IT's current
5610 position. */
5611 if (end != charpos && start != charpos)
5612 continue;
5613
5614 /* Skip this overlay if it doesn't apply to IT->w. */
5615 window = Foverlay_get (overlay, Qwindow);
5616 if (WINDOWP (window) && XWINDOW (window) != it->w)
5617 continue;
5618
5619 /* If the text ``under'' the overlay is invisible, both before-
5620 and after-strings from this overlay are visible; start and
5621 end position are indistinguishable. */
5622 invisible = Foverlay_get (overlay, Qinvisible);
5623 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5624
5625 /* If overlay has a non-empty before-string, record it. */
5626 if ((start == charpos || (end == charpos && invis_p))
5627 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5628 && SCHARS (str))
5629 RECORD_OVERLAY_STRING (overlay, str, 0);
5630
5631 /* If overlay has a non-empty after-string, record it. */
5632 if ((end == charpos || (start == charpos && invis_p))
5633 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5634 && SCHARS (str))
5635 RECORD_OVERLAY_STRING (overlay, str, 1);
5636 }
5637
5638 /* Process overlays after the overlay center. */
5639 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5640 {
5641 XSETMISC (overlay, ov);
5642 eassert (OVERLAYP (overlay));
5643 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5644 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5645
5646 if (start > charpos)
5647 break;
5648
5649 /* Skip this overlay if it doesn't start or end at IT's current
5650 position. */
5651 if (end != charpos && start != charpos)
5652 continue;
5653
5654 /* Skip this overlay if it doesn't apply to IT->w. */
5655 window = Foverlay_get (overlay, Qwindow);
5656 if (WINDOWP (window) && XWINDOW (window) != it->w)
5657 continue;
5658
5659 /* If the text ``under'' the overlay is invisible, it has a zero
5660 dimension, and both before- and after-strings apply. */
5661 invisible = Foverlay_get (overlay, Qinvisible);
5662 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5663
5664 /* If overlay has a non-empty before-string, record it. */
5665 if ((start == charpos || (end == charpos && invis_p))
5666 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5667 && SCHARS (str))
5668 RECORD_OVERLAY_STRING (overlay, str, 0);
5669
5670 /* If overlay has a non-empty after-string, record it. */
5671 if ((end == charpos || (start == charpos && invis_p))
5672 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5673 && SCHARS (str))
5674 RECORD_OVERLAY_STRING (overlay, str, 1);
5675 }
5676
5677 #undef RECORD_OVERLAY_STRING
5678
5679 /* Sort entries. */
5680 if (n > 1)
5681 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5682
5683 /* Record number of overlay strings, and where we computed it. */
5684 it->n_overlay_strings = n;
5685 it->overlay_strings_charpos = charpos;
5686
5687 /* IT->current.overlay_string_index is the number of overlay strings
5688 that have already been consumed by IT. Copy some of the
5689 remaining overlay strings to IT->overlay_strings. */
5690 i = 0;
5691 j = it->current.overlay_string_index;
5692 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5693 {
5694 it->overlay_strings[i] = entries[j].string;
5695 it->string_overlays[i++] = entries[j++].overlay;
5696 }
5697
5698 CHECK_IT (it);
5699 SAFE_FREE ();
5700 }
5701
5702
5703 /* Get the first chunk of overlay strings at IT's current buffer
5704 position, or at CHARPOS if that is > 0. Value is non-zero if at
5705 least one overlay string was found. */
5706
5707 static int
5708 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5709 {
5710 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5711 process. This fills IT->overlay_strings with strings, and sets
5712 IT->n_overlay_strings to the total number of strings to process.
5713 IT->pos.overlay_string_index has to be set temporarily to zero
5714 because load_overlay_strings needs this; it must be set to -1
5715 when no overlay strings are found because a zero value would
5716 indicate a position in the first overlay string. */
5717 it->current.overlay_string_index = 0;
5718 load_overlay_strings (it, charpos);
5719
5720 /* If we found overlay strings, set up IT to deliver display
5721 elements from the first one. Otherwise set up IT to deliver
5722 from current_buffer. */
5723 if (it->n_overlay_strings)
5724 {
5725 /* Make sure we know settings in current_buffer, so that we can
5726 restore meaningful values when we're done with the overlay
5727 strings. */
5728 if (compute_stop_p)
5729 compute_stop_pos (it);
5730 eassert (it->face_id >= 0);
5731
5732 /* Save IT's settings. They are restored after all overlay
5733 strings have been processed. */
5734 eassert (!compute_stop_p || it->sp == 0);
5735
5736 /* When called from handle_stop, there might be an empty display
5737 string loaded. In that case, don't bother saving it. But
5738 don't use this optimization with the bidi iterator, since we
5739 need the corresponding pop_it call to resync the bidi
5740 iterator's position with IT's position, after we are done
5741 with the overlay strings. (The corresponding call to pop_it
5742 in case of an empty display string is in
5743 next_overlay_string.) */
5744 if (!(!it->bidi_p
5745 && STRINGP (it->string) && !SCHARS (it->string)))
5746 push_it (it, NULL);
5747
5748 /* Set up IT to deliver display elements from the first overlay
5749 string. */
5750 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5751 it->string = it->overlay_strings[0];
5752 it->from_overlay = Qnil;
5753 it->stop_charpos = 0;
5754 eassert (STRINGP (it->string));
5755 it->end_charpos = SCHARS (it->string);
5756 it->prev_stop = 0;
5757 it->base_level_stop = 0;
5758 it->multibyte_p = STRING_MULTIBYTE (it->string);
5759 it->method = GET_FROM_STRING;
5760 it->from_disp_prop_p = 0;
5761
5762 /* Force paragraph direction to be that of the parent
5763 buffer. */
5764 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5765 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5766 else
5767 it->paragraph_embedding = L2R;
5768
5769 /* Set up the bidi iterator for this overlay string. */
5770 if (it->bidi_p)
5771 {
5772 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5773
5774 it->bidi_it.string.lstring = it->string;
5775 it->bidi_it.string.s = NULL;
5776 it->bidi_it.string.schars = SCHARS (it->string);
5777 it->bidi_it.string.bufpos = pos;
5778 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5779 it->bidi_it.string.unibyte = !it->multibyte_p;
5780 it->bidi_it.w = it->w;
5781 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5782 }
5783 return 1;
5784 }
5785
5786 it->current.overlay_string_index = -1;
5787 return 0;
5788 }
5789
5790 static int
5791 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5792 {
5793 it->string = Qnil;
5794 it->method = GET_FROM_BUFFER;
5795
5796 (void) get_overlay_strings_1 (it, charpos, 1);
5797
5798 CHECK_IT (it);
5799
5800 /* Value is non-zero if we found at least one overlay string. */
5801 return STRINGP (it->string);
5802 }
5803
5804
5805 \f
5806 /***********************************************************************
5807 Saving and restoring state
5808 ***********************************************************************/
5809
5810 /* Save current settings of IT on IT->stack. Called, for example,
5811 before setting up IT for an overlay string, to be able to restore
5812 IT's settings to what they were after the overlay string has been
5813 processed. If POSITION is non-NULL, it is the position to save on
5814 the stack instead of IT->position. */
5815
5816 static void
5817 push_it (struct it *it, struct text_pos *position)
5818 {
5819 struct iterator_stack_entry *p;
5820
5821 eassert (it->sp < IT_STACK_SIZE);
5822 p = it->stack + it->sp;
5823
5824 p->stop_charpos = it->stop_charpos;
5825 p->prev_stop = it->prev_stop;
5826 p->base_level_stop = it->base_level_stop;
5827 p->cmp_it = it->cmp_it;
5828 eassert (it->face_id >= 0);
5829 p->face_id = it->face_id;
5830 p->string = it->string;
5831 p->method = it->method;
5832 p->from_overlay = it->from_overlay;
5833 switch (p->method)
5834 {
5835 case GET_FROM_IMAGE:
5836 p->u.image.object = it->object;
5837 p->u.image.image_id = it->image_id;
5838 p->u.image.slice = it->slice;
5839 break;
5840 case GET_FROM_STRETCH:
5841 p->u.stretch.object = it->object;
5842 break;
5843 }
5844 p->position = position ? *position : it->position;
5845 p->current = it->current;
5846 p->end_charpos = it->end_charpos;
5847 p->string_nchars = it->string_nchars;
5848 p->area = it->area;
5849 p->multibyte_p = it->multibyte_p;
5850 p->avoid_cursor_p = it->avoid_cursor_p;
5851 p->space_width = it->space_width;
5852 p->font_height = it->font_height;
5853 p->voffset = it->voffset;
5854 p->string_from_display_prop_p = it->string_from_display_prop_p;
5855 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5856 p->display_ellipsis_p = 0;
5857 p->line_wrap = it->line_wrap;
5858 p->bidi_p = it->bidi_p;
5859 p->paragraph_embedding = it->paragraph_embedding;
5860 p->from_disp_prop_p = it->from_disp_prop_p;
5861 ++it->sp;
5862
5863 /* Save the state of the bidi iterator as well. */
5864 if (it->bidi_p)
5865 bidi_push_it (&it->bidi_it);
5866 }
5867
5868 static void
5869 iterate_out_of_display_property (struct it *it)
5870 {
5871 int buffer_p = !STRINGP (it->string);
5872 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5873 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5874
5875 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5876
5877 /* Maybe initialize paragraph direction. If we are at the beginning
5878 of a new paragraph, next_element_from_buffer may not have a
5879 chance to do that. */
5880 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5881 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5882 /* prev_stop can be zero, so check against BEGV as well. */
5883 while (it->bidi_it.charpos >= bob
5884 && it->prev_stop <= it->bidi_it.charpos
5885 && it->bidi_it.charpos < CHARPOS (it->position)
5886 && it->bidi_it.charpos < eob)
5887 bidi_move_to_visually_next (&it->bidi_it);
5888 /* Record the stop_pos we just crossed, for when we cross it
5889 back, maybe. */
5890 if (it->bidi_it.charpos > CHARPOS (it->position))
5891 it->prev_stop = CHARPOS (it->position);
5892 /* If we ended up not where pop_it put us, resync IT's
5893 positional members with the bidi iterator. */
5894 if (it->bidi_it.charpos != CHARPOS (it->position))
5895 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5896 if (buffer_p)
5897 it->current.pos = it->position;
5898 else
5899 it->current.string_pos = it->position;
5900 }
5901
5902 /* Restore IT's settings from IT->stack. Called, for example, when no
5903 more overlay strings must be processed, and we return to delivering
5904 display elements from a buffer, or when the end of a string from a
5905 `display' property is reached and we return to delivering display
5906 elements from an overlay string, or from a buffer. */
5907
5908 static void
5909 pop_it (struct it *it)
5910 {
5911 struct iterator_stack_entry *p;
5912 int from_display_prop = it->from_disp_prop_p;
5913
5914 eassert (it->sp > 0);
5915 --it->sp;
5916 p = it->stack + it->sp;
5917 it->stop_charpos = p->stop_charpos;
5918 it->prev_stop = p->prev_stop;
5919 it->base_level_stop = p->base_level_stop;
5920 it->cmp_it = p->cmp_it;
5921 it->face_id = p->face_id;
5922 it->current = p->current;
5923 it->position = p->position;
5924 it->string = p->string;
5925 it->from_overlay = p->from_overlay;
5926 if (NILP (it->string))
5927 SET_TEXT_POS (it->current.string_pos, -1, -1);
5928 it->method = p->method;
5929 switch (it->method)
5930 {
5931 case GET_FROM_IMAGE:
5932 it->image_id = p->u.image.image_id;
5933 it->object = p->u.image.object;
5934 it->slice = p->u.image.slice;
5935 break;
5936 case GET_FROM_STRETCH:
5937 it->object = p->u.stretch.object;
5938 break;
5939 case GET_FROM_BUFFER:
5940 it->object = it->w->contents;
5941 break;
5942 case GET_FROM_STRING:
5943 it->object = it->string;
5944 break;
5945 case GET_FROM_DISPLAY_VECTOR:
5946 if (it->s)
5947 it->method = GET_FROM_C_STRING;
5948 else if (STRINGP (it->string))
5949 it->method = GET_FROM_STRING;
5950 else
5951 {
5952 it->method = GET_FROM_BUFFER;
5953 it->object = it->w->contents;
5954 }
5955 }
5956 it->end_charpos = p->end_charpos;
5957 it->string_nchars = p->string_nchars;
5958 it->area = p->area;
5959 it->multibyte_p = p->multibyte_p;
5960 it->avoid_cursor_p = p->avoid_cursor_p;
5961 it->space_width = p->space_width;
5962 it->font_height = p->font_height;
5963 it->voffset = p->voffset;
5964 it->string_from_display_prop_p = p->string_from_display_prop_p;
5965 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5966 it->line_wrap = p->line_wrap;
5967 it->bidi_p = p->bidi_p;
5968 it->paragraph_embedding = p->paragraph_embedding;
5969 it->from_disp_prop_p = p->from_disp_prop_p;
5970 if (it->bidi_p)
5971 {
5972 bidi_pop_it (&it->bidi_it);
5973 /* Bidi-iterate until we get out of the portion of text, if any,
5974 covered by a `display' text property or by an overlay with
5975 `display' property. (We cannot just jump there, because the
5976 internal coherency of the bidi iterator state can not be
5977 preserved across such jumps.) We also must determine the
5978 paragraph base direction if the overlay we just processed is
5979 at the beginning of a new paragraph. */
5980 if (from_display_prop
5981 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5982 iterate_out_of_display_property (it);
5983
5984 eassert ((BUFFERP (it->object)
5985 && IT_CHARPOS (*it) == it->bidi_it.charpos
5986 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5987 || (STRINGP (it->object)
5988 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5989 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5990 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5991 }
5992 }
5993
5994
5995 \f
5996 /***********************************************************************
5997 Moving over lines
5998 ***********************************************************************/
5999
6000 /* Set IT's current position to the previous line start. */
6001
6002 static void
6003 back_to_previous_line_start (struct it *it)
6004 {
6005 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6006
6007 DEC_BOTH (cp, bp);
6008 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6009 }
6010
6011
6012 /* Move IT to the next line start.
6013
6014 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6015 we skipped over part of the text (as opposed to moving the iterator
6016 continuously over the text). Otherwise, don't change the value
6017 of *SKIPPED_P.
6018
6019 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6020 iterator on the newline, if it was found.
6021
6022 Newlines may come from buffer text, overlay strings, or strings
6023 displayed via the `display' property. That's the reason we can't
6024 simply use find_newline_no_quit.
6025
6026 Note that this function may not skip over invisible text that is so
6027 because of text properties and immediately follows a newline. If
6028 it would, function reseat_at_next_visible_line_start, when called
6029 from set_iterator_to_next, would effectively make invisible
6030 characters following a newline part of the wrong glyph row, which
6031 leads to wrong cursor motion. */
6032
6033 static int
6034 forward_to_next_line_start (struct it *it, int *skipped_p,
6035 struct bidi_it *bidi_it_prev)
6036 {
6037 ptrdiff_t old_selective;
6038 int newline_found_p, n;
6039 const int MAX_NEWLINE_DISTANCE = 500;
6040
6041 /* If already on a newline, just consume it to avoid unintended
6042 skipping over invisible text below. */
6043 if (it->what == IT_CHARACTER
6044 && it->c == '\n'
6045 && CHARPOS (it->position) == IT_CHARPOS (*it))
6046 {
6047 if (it->bidi_p && bidi_it_prev)
6048 *bidi_it_prev = it->bidi_it;
6049 set_iterator_to_next (it, 0);
6050 it->c = 0;
6051 return 1;
6052 }
6053
6054 /* Don't handle selective display in the following. It's (a)
6055 unnecessary because it's done by the caller, and (b) leads to an
6056 infinite recursion because next_element_from_ellipsis indirectly
6057 calls this function. */
6058 old_selective = it->selective;
6059 it->selective = 0;
6060
6061 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6062 from buffer text. */
6063 for (n = newline_found_p = 0;
6064 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6065 n += STRINGP (it->string) ? 0 : 1)
6066 {
6067 if (!get_next_display_element (it))
6068 return 0;
6069 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6070 if (newline_found_p && it->bidi_p && bidi_it_prev)
6071 *bidi_it_prev = it->bidi_it;
6072 set_iterator_to_next (it, 0);
6073 }
6074
6075 /* If we didn't find a newline near enough, see if we can use a
6076 short-cut. */
6077 if (!newline_found_p)
6078 {
6079 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6080 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6081 1, &bytepos);
6082 Lisp_Object pos;
6083
6084 eassert (!STRINGP (it->string));
6085
6086 /* If there isn't any `display' property in sight, and no
6087 overlays, we can just use the position of the newline in
6088 buffer text. */
6089 if (it->stop_charpos >= limit
6090 || ((pos = Fnext_single_property_change (make_number (start),
6091 Qdisplay, Qnil,
6092 make_number (limit)),
6093 NILP (pos))
6094 && next_overlay_change (start) == ZV))
6095 {
6096 if (!it->bidi_p)
6097 {
6098 IT_CHARPOS (*it) = limit;
6099 IT_BYTEPOS (*it) = bytepos;
6100 }
6101 else
6102 {
6103 struct bidi_it bprev;
6104
6105 /* Help bidi.c avoid expensive searches for display
6106 properties and overlays, by telling it that there are
6107 none up to `limit'. */
6108 if (it->bidi_it.disp_pos < limit)
6109 {
6110 it->bidi_it.disp_pos = limit;
6111 it->bidi_it.disp_prop = 0;
6112 }
6113 do {
6114 bprev = it->bidi_it;
6115 bidi_move_to_visually_next (&it->bidi_it);
6116 } while (it->bidi_it.charpos != limit);
6117 IT_CHARPOS (*it) = limit;
6118 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6119 if (bidi_it_prev)
6120 *bidi_it_prev = bprev;
6121 }
6122 *skipped_p = newline_found_p = 1;
6123 }
6124 else
6125 {
6126 while (get_next_display_element (it)
6127 && !newline_found_p)
6128 {
6129 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6130 if (newline_found_p && it->bidi_p && bidi_it_prev)
6131 *bidi_it_prev = it->bidi_it;
6132 set_iterator_to_next (it, 0);
6133 }
6134 }
6135 }
6136
6137 it->selective = old_selective;
6138 return newline_found_p;
6139 }
6140
6141
6142 /* Set IT's current position to the previous visible line start. Skip
6143 invisible text that is so either due to text properties or due to
6144 selective display. Caution: this does not change IT->current_x and
6145 IT->hpos. */
6146
6147 static void
6148 back_to_previous_visible_line_start (struct it *it)
6149 {
6150 while (IT_CHARPOS (*it) > BEGV)
6151 {
6152 back_to_previous_line_start (it);
6153
6154 if (IT_CHARPOS (*it) <= BEGV)
6155 break;
6156
6157 /* If selective > 0, then lines indented more than its value are
6158 invisible. */
6159 if (it->selective > 0
6160 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6161 it->selective))
6162 continue;
6163
6164 /* Check the newline before point for invisibility. */
6165 {
6166 Lisp_Object prop;
6167 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6168 Qinvisible, it->window);
6169 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6170 continue;
6171 }
6172
6173 if (IT_CHARPOS (*it) <= BEGV)
6174 break;
6175
6176 {
6177 struct it it2;
6178 void *it2data = NULL;
6179 ptrdiff_t pos;
6180 ptrdiff_t beg, end;
6181 Lisp_Object val, overlay;
6182
6183 SAVE_IT (it2, *it, it2data);
6184
6185 /* If newline is part of a composition, continue from start of composition */
6186 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6187 && beg < IT_CHARPOS (*it))
6188 goto replaced;
6189
6190 /* If newline is replaced by a display property, find start of overlay
6191 or interval and continue search from that point. */
6192 pos = --IT_CHARPOS (it2);
6193 --IT_BYTEPOS (it2);
6194 it2.sp = 0;
6195 bidi_unshelve_cache (NULL, 0);
6196 it2.string_from_display_prop_p = 0;
6197 it2.from_disp_prop_p = 0;
6198 if (handle_display_prop (&it2) == HANDLED_RETURN
6199 && !NILP (val = get_char_property_and_overlay
6200 (make_number (pos), Qdisplay, Qnil, &overlay))
6201 && (OVERLAYP (overlay)
6202 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6203 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6204 {
6205 RESTORE_IT (it, it, it2data);
6206 goto replaced;
6207 }
6208
6209 /* Newline is not replaced by anything -- so we are done. */
6210 RESTORE_IT (it, it, it2data);
6211 break;
6212
6213 replaced:
6214 if (beg < BEGV)
6215 beg = BEGV;
6216 IT_CHARPOS (*it) = beg;
6217 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6218 }
6219 }
6220
6221 it->continuation_lines_width = 0;
6222
6223 eassert (IT_CHARPOS (*it) >= BEGV);
6224 eassert (IT_CHARPOS (*it) == BEGV
6225 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6226 CHECK_IT (it);
6227 }
6228
6229
6230 /* Reseat iterator IT at the previous visible line start. Skip
6231 invisible text that is so either due to text properties or due to
6232 selective display. At the end, update IT's overlay information,
6233 face information etc. */
6234
6235 void
6236 reseat_at_previous_visible_line_start (struct it *it)
6237 {
6238 back_to_previous_visible_line_start (it);
6239 reseat (it, it->current.pos, 1);
6240 CHECK_IT (it);
6241 }
6242
6243
6244 /* Reseat iterator IT on the next visible line start in the current
6245 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6246 preceding the line start. Skip over invisible text that is so
6247 because of selective display. Compute faces, overlays etc at the
6248 new position. Note that this function does not skip over text that
6249 is invisible because of text properties. */
6250
6251 static void
6252 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6253 {
6254 int newline_found_p, skipped_p = 0;
6255 struct bidi_it bidi_it_prev;
6256
6257 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6258
6259 /* Skip over lines that are invisible because they are indented
6260 more than the value of IT->selective. */
6261 if (it->selective > 0)
6262 while (IT_CHARPOS (*it) < ZV
6263 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6264 it->selective))
6265 {
6266 eassert (IT_BYTEPOS (*it) == BEGV
6267 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6268 newline_found_p =
6269 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6270 }
6271
6272 /* Position on the newline if that's what's requested. */
6273 if (on_newline_p && newline_found_p)
6274 {
6275 if (STRINGP (it->string))
6276 {
6277 if (IT_STRING_CHARPOS (*it) > 0)
6278 {
6279 if (!it->bidi_p)
6280 {
6281 --IT_STRING_CHARPOS (*it);
6282 --IT_STRING_BYTEPOS (*it);
6283 }
6284 else
6285 {
6286 /* We need to restore the bidi iterator to the state
6287 it had on the newline, and resync the IT's
6288 position with that. */
6289 it->bidi_it = bidi_it_prev;
6290 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6291 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6292 }
6293 }
6294 }
6295 else if (IT_CHARPOS (*it) > BEGV)
6296 {
6297 if (!it->bidi_p)
6298 {
6299 --IT_CHARPOS (*it);
6300 --IT_BYTEPOS (*it);
6301 }
6302 else
6303 {
6304 /* We need to restore the bidi iterator to the state it
6305 had on the newline and resync IT with that. */
6306 it->bidi_it = bidi_it_prev;
6307 IT_CHARPOS (*it) = it->bidi_it.charpos;
6308 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6309 }
6310 reseat (it, it->current.pos, 0);
6311 }
6312 }
6313 else if (skipped_p)
6314 reseat (it, it->current.pos, 0);
6315
6316 CHECK_IT (it);
6317 }
6318
6319
6320 \f
6321 /***********************************************************************
6322 Changing an iterator's position
6323 ***********************************************************************/
6324
6325 /* Change IT's current position to POS in current_buffer. If FORCE_P
6326 is non-zero, always check for text properties at the new position.
6327 Otherwise, text properties are only looked up if POS >=
6328 IT->check_charpos of a property. */
6329
6330 static void
6331 reseat (struct it *it, struct text_pos pos, int force_p)
6332 {
6333 ptrdiff_t original_pos = IT_CHARPOS (*it);
6334
6335 reseat_1 (it, pos, 0);
6336
6337 /* Determine where to check text properties. Avoid doing it
6338 where possible because text property lookup is very expensive. */
6339 if (force_p
6340 || CHARPOS (pos) > it->stop_charpos
6341 || CHARPOS (pos) < original_pos)
6342 {
6343 if (it->bidi_p)
6344 {
6345 /* For bidi iteration, we need to prime prev_stop and
6346 base_level_stop with our best estimations. */
6347 /* Implementation note: Of course, POS is not necessarily a
6348 stop position, so assigning prev_pos to it is a lie; we
6349 should have called compute_stop_backwards. However, if
6350 the current buffer does not include any R2L characters,
6351 that call would be a waste of cycles, because the
6352 iterator will never move back, and thus never cross this
6353 "fake" stop position. So we delay that backward search
6354 until the time we really need it, in next_element_from_buffer. */
6355 if (CHARPOS (pos) != it->prev_stop)
6356 it->prev_stop = CHARPOS (pos);
6357 if (CHARPOS (pos) < it->base_level_stop)
6358 it->base_level_stop = 0; /* meaning it's unknown */
6359 handle_stop (it);
6360 }
6361 else
6362 {
6363 handle_stop (it);
6364 it->prev_stop = it->base_level_stop = 0;
6365 }
6366
6367 }
6368
6369 CHECK_IT (it);
6370 }
6371
6372
6373 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6374 IT->stop_pos to POS, also. */
6375
6376 static void
6377 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6378 {
6379 /* Don't call this function when scanning a C string. */
6380 eassert (it->s == NULL);
6381
6382 /* POS must be a reasonable value. */
6383 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6384
6385 it->current.pos = it->position = pos;
6386 it->end_charpos = ZV;
6387 it->dpvec = NULL;
6388 it->current.dpvec_index = -1;
6389 it->current.overlay_string_index = -1;
6390 IT_STRING_CHARPOS (*it) = -1;
6391 IT_STRING_BYTEPOS (*it) = -1;
6392 it->string = Qnil;
6393 it->method = GET_FROM_BUFFER;
6394 it->object = it->w->contents;
6395 it->area = TEXT_AREA;
6396 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6397 it->sp = 0;
6398 it->string_from_display_prop_p = 0;
6399 it->string_from_prefix_prop_p = 0;
6400
6401 it->from_disp_prop_p = 0;
6402 it->face_before_selective_p = 0;
6403 if (it->bidi_p)
6404 {
6405 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6406 &it->bidi_it);
6407 bidi_unshelve_cache (NULL, 0);
6408 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6409 it->bidi_it.string.s = NULL;
6410 it->bidi_it.string.lstring = Qnil;
6411 it->bidi_it.string.bufpos = 0;
6412 it->bidi_it.string.unibyte = 0;
6413 it->bidi_it.w = it->w;
6414 }
6415
6416 if (set_stop_p)
6417 {
6418 it->stop_charpos = CHARPOS (pos);
6419 it->base_level_stop = CHARPOS (pos);
6420 }
6421 /* This make the information stored in it->cmp_it invalidate. */
6422 it->cmp_it.id = -1;
6423 }
6424
6425
6426 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6427 If S is non-null, it is a C string to iterate over. Otherwise,
6428 STRING gives a Lisp string to iterate over.
6429
6430 If PRECISION > 0, don't return more then PRECISION number of
6431 characters from the string.
6432
6433 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6434 characters have been returned. FIELD_WIDTH < 0 means an infinite
6435 field width.
6436
6437 MULTIBYTE = 0 means disable processing of multibyte characters,
6438 MULTIBYTE > 0 means enable it,
6439 MULTIBYTE < 0 means use IT->multibyte_p.
6440
6441 IT must be initialized via a prior call to init_iterator before
6442 calling this function. */
6443
6444 static void
6445 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6446 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6447 int multibyte)
6448 {
6449 /* No region in strings. */
6450 it->region_beg_charpos = it->region_end_charpos = -1;
6451
6452 /* No text property checks performed by default, but see below. */
6453 it->stop_charpos = -1;
6454
6455 /* Set iterator position and end position. */
6456 memset (&it->current, 0, sizeof it->current);
6457 it->current.overlay_string_index = -1;
6458 it->current.dpvec_index = -1;
6459 eassert (charpos >= 0);
6460
6461 /* If STRING is specified, use its multibyteness, otherwise use the
6462 setting of MULTIBYTE, if specified. */
6463 if (multibyte >= 0)
6464 it->multibyte_p = multibyte > 0;
6465
6466 /* Bidirectional reordering of strings is controlled by the default
6467 value of bidi-display-reordering. Don't try to reorder while
6468 loading loadup.el, as the necessary character property tables are
6469 not yet available. */
6470 it->bidi_p =
6471 NILP (Vpurify_flag)
6472 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6473
6474 if (s == NULL)
6475 {
6476 eassert (STRINGP (string));
6477 it->string = string;
6478 it->s = NULL;
6479 it->end_charpos = it->string_nchars = SCHARS (string);
6480 it->method = GET_FROM_STRING;
6481 it->current.string_pos = string_pos (charpos, string);
6482
6483 if (it->bidi_p)
6484 {
6485 it->bidi_it.string.lstring = string;
6486 it->bidi_it.string.s = NULL;
6487 it->bidi_it.string.schars = it->end_charpos;
6488 it->bidi_it.string.bufpos = 0;
6489 it->bidi_it.string.from_disp_str = 0;
6490 it->bidi_it.string.unibyte = !it->multibyte_p;
6491 it->bidi_it.w = it->w;
6492 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6493 FRAME_WINDOW_P (it->f), &it->bidi_it);
6494 }
6495 }
6496 else
6497 {
6498 it->s = (const unsigned char *) s;
6499 it->string = Qnil;
6500
6501 /* Note that we use IT->current.pos, not it->current.string_pos,
6502 for displaying C strings. */
6503 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6504 if (it->multibyte_p)
6505 {
6506 it->current.pos = c_string_pos (charpos, s, 1);
6507 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6508 }
6509 else
6510 {
6511 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6512 it->end_charpos = it->string_nchars = strlen (s);
6513 }
6514
6515 if (it->bidi_p)
6516 {
6517 it->bidi_it.string.lstring = Qnil;
6518 it->bidi_it.string.s = (const unsigned char *) s;
6519 it->bidi_it.string.schars = it->end_charpos;
6520 it->bidi_it.string.bufpos = 0;
6521 it->bidi_it.string.from_disp_str = 0;
6522 it->bidi_it.string.unibyte = !it->multibyte_p;
6523 it->bidi_it.w = it->w;
6524 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6525 &it->bidi_it);
6526 }
6527 it->method = GET_FROM_C_STRING;
6528 }
6529
6530 /* PRECISION > 0 means don't return more than PRECISION characters
6531 from the string. */
6532 if (precision > 0 && it->end_charpos - charpos > precision)
6533 {
6534 it->end_charpos = it->string_nchars = charpos + precision;
6535 if (it->bidi_p)
6536 it->bidi_it.string.schars = it->end_charpos;
6537 }
6538
6539 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6540 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6541 FIELD_WIDTH < 0 means infinite field width. This is useful for
6542 padding with `-' at the end of a mode line. */
6543 if (field_width < 0)
6544 field_width = INFINITY;
6545 /* Implementation note: We deliberately don't enlarge
6546 it->bidi_it.string.schars here to fit it->end_charpos, because
6547 the bidi iterator cannot produce characters out of thin air. */
6548 if (field_width > it->end_charpos - charpos)
6549 it->end_charpos = charpos + field_width;
6550
6551 /* Use the standard display table for displaying strings. */
6552 if (DISP_TABLE_P (Vstandard_display_table))
6553 it->dp = XCHAR_TABLE (Vstandard_display_table);
6554
6555 it->stop_charpos = charpos;
6556 it->prev_stop = charpos;
6557 it->base_level_stop = 0;
6558 if (it->bidi_p)
6559 {
6560 it->bidi_it.first_elt = 1;
6561 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6562 it->bidi_it.disp_pos = -1;
6563 }
6564 if (s == NULL && it->multibyte_p)
6565 {
6566 ptrdiff_t endpos = SCHARS (it->string);
6567 if (endpos > it->end_charpos)
6568 endpos = it->end_charpos;
6569 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6570 it->string);
6571 }
6572 CHECK_IT (it);
6573 }
6574
6575
6576 \f
6577 /***********************************************************************
6578 Iteration
6579 ***********************************************************************/
6580
6581 /* Map enum it_method value to corresponding next_element_from_* function. */
6582
6583 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6584 {
6585 next_element_from_buffer,
6586 next_element_from_display_vector,
6587 next_element_from_string,
6588 next_element_from_c_string,
6589 next_element_from_image,
6590 next_element_from_stretch
6591 };
6592
6593 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6594
6595
6596 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6597 (possibly with the following characters). */
6598
6599 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6600 ((IT)->cmp_it.id >= 0 \
6601 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6602 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6603 END_CHARPOS, (IT)->w, \
6604 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6605 (IT)->string)))
6606
6607
6608 /* Lookup the char-table Vglyphless_char_display for character C (-1
6609 if we want information for no-font case), and return the display
6610 method symbol. By side-effect, update it->what and
6611 it->glyphless_method. This function is called from
6612 get_next_display_element for each character element, and from
6613 x_produce_glyphs when no suitable font was found. */
6614
6615 Lisp_Object
6616 lookup_glyphless_char_display (int c, struct it *it)
6617 {
6618 Lisp_Object glyphless_method = Qnil;
6619
6620 if (CHAR_TABLE_P (Vglyphless_char_display)
6621 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6622 {
6623 if (c >= 0)
6624 {
6625 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6626 if (CONSP (glyphless_method))
6627 glyphless_method = FRAME_WINDOW_P (it->f)
6628 ? XCAR (glyphless_method)
6629 : XCDR (glyphless_method);
6630 }
6631 else
6632 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6633 }
6634
6635 retry:
6636 if (NILP (glyphless_method))
6637 {
6638 if (c >= 0)
6639 /* The default is to display the character by a proper font. */
6640 return Qnil;
6641 /* The default for the no-font case is to display an empty box. */
6642 glyphless_method = Qempty_box;
6643 }
6644 if (EQ (glyphless_method, Qzero_width))
6645 {
6646 if (c >= 0)
6647 return glyphless_method;
6648 /* This method can't be used for the no-font case. */
6649 glyphless_method = Qempty_box;
6650 }
6651 if (EQ (glyphless_method, Qthin_space))
6652 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6653 else if (EQ (glyphless_method, Qempty_box))
6654 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6655 else if (EQ (glyphless_method, Qhex_code))
6656 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6657 else if (STRINGP (glyphless_method))
6658 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6659 else
6660 {
6661 /* Invalid value. We use the default method. */
6662 glyphless_method = Qnil;
6663 goto retry;
6664 }
6665 it->what = IT_GLYPHLESS;
6666 return glyphless_method;
6667 }
6668
6669 /* Merge escape glyph face and cache the result. */
6670
6671 static struct frame *last_escape_glyph_frame = NULL;
6672 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6673 static int last_escape_glyph_merged_face_id = 0;
6674
6675 static int
6676 merge_escape_glyph_face (struct it *it)
6677 {
6678 int face_id;
6679
6680 if (it->f == last_escape_glyph_frame
6681 && it->face_id == last_escape_glyph_face_id)
6682 face_id = last_escape_glyph_merged_face_id;
6683 else
6684 {
6685 /* Merge the `escape-glyph' face into the current face. */
6686 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6687 last_escape_glyph_frame = it->f;
6688 last_escape_glyph_face_id = it->face_id;
6689 last_escape_glyph_merged_face_id = face_id;
6690 }
6691 return face_id;
6692 }
6693
6694 /* Likewise for glyphless glyph face. */
6695
6696 static struct frame *last_glyphless_glyph_frame = NULL;
6697 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6698 static int last_glyphless_glyph_merged_face_id = 0;
6699
6700 int
6701 merge_glyphless_glyph_face (struct it *it)
6702 {
6703 int face_id;
6704
6705 if (it->f == last_glyphless_glyph_frame
6706 && it->face_id == last_glyphless_glyph_face_id)
6707 face_id = last_glyphless_glyph_merged_face_id;
6708 else
6709 {
6710 /* Merge the `glyphless-char' face into the current face. */
6711 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6712 last_glyphless_glyph_frame = it->f;
6713 last_glyphless_glyph_face_id = it->face_id;
6714 last_glyphless_glyph_merged_face_id = face_id;
6715 }
6716 return face_id;
6717 }
6718
6719 /* Load IT's display element fields with information about the next
6720 display element from the current position of IT. Value is zero if
6721 end of buffer (or C string) is reached. */
6722
6723 static int
6724 get_next_display_element (struct it *it)
6725 {
6726 /* Non-zero means that we found a display element. Zero means that
6727 we hit the end of what we iterate over. Performance note: the
6728 function pointer `method' used here turns out to be faster than
6729 using a sequence of if-statements. */
6730 int success_p;
6731
6732 get_next:
6733 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6734
6735 if (it->what == IT_CHARACTER)
6736 {
6737 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6738 and only if (a) the resolved directionality of that character
6739 is R..." */
6740 /* FIXME: Do we need an exception for characters from display
6741 tables? */
6742 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6743 it->c = bidi_mirror_char (it->c);
6744 /* Map via display table or translate control characters.
6745 IT->c, IT->len etc. have been set to the next character by
6746 the function call above. If we have a display table, and it
6747 contains an entry for IT->c, translate it. Don't do this if
6748 IT->c itself comes from a display table, otherwise we could
6749 end up in an infinite recursion. (An alternative could be to
6750 count the recursion depth of this function and signal an
6751 error when a certain maximum depth is reached.) Is it worth
6752 it? */
6753 if (success_p && it->dpvec == NULL)
6754 {
6755 Lisp_Object dv;
6756 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6757 int nonascii_space_p = 0;
6758 int nonascii_hyphen_p = 0;
6759 int c = it->c; /* This is the character to display. */
6760
6761 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6762 {
6763 eassert (SINGLE_BYTE_CHAR_P (c));
6764 if (unibyte_display_via_language_environment)
6765 {
6766 c = DECODE_CHAR (unibyte, c);
6767 if (c < 0)
6768 c = BYTE8_TO_CHAR (it->c);
6769 }
6770 else
6771 c = BYTE8_TO_CHAR (it->c);
6772 }
6773
6774 if (it->dp
6775 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6776 VECTORP (dv)))
6777 {
6778 struct Lisp_Vector *v = XVECTOR (dv);
6779
6780 /* Return the first character from the display table
6781 entry, if not empty. If empty, don't display the
6782 current character. */
6783 if (v->header.size)
6784 {
6785 it->dpvec_char_len = it->len;
6786 it->dpvec = v->contents;
6787 it->dpend = v->contents + v->header.size;
6788 it->current.dpvec_index = 0;
6789 it->dpvec_face_id = -1;
6790 it->saved_face_id = it->face_id;
6791 it->method = GET_FROM_DISPLAY_VECTOR;
6792 it->ellipsis_p = 0;
6793 }
6794 else
6795 {
6796 set_iterator_to_next (it, 0);
6797 }
6798 goto get_next;
6799 }
6800
6801 if (! NILP (lookup_glyphless_char_display (c, it)))
6802 {
6803 if (it->what == IT_GLYPHLESS)
6804 goto done;
6805 /* Don't display this character. */
6806 set_iterator_to_next (it, 0);
6807 goto get_next;
6808 }
6809
6810 /* If `nobreak-char-display' is non-nil, we display
6811 non-ASCII spaces and hyphens specially. */
6812 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6813 {
6814 if (c == 0xA0)
6815 nonascii_space_p = 1;
6816 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6817 nonascii_hyphen_p = 1;
6818 }
6819
6820 /* Translate control characters into `\003' or `^C' form.
6821 Control characters coming from a display table entry are
6822 currently not translated because we use IT->dpvec to hold
6823 the translation. This could easily be changed but I
6824 don't believe that it is worth doing.
6825
6826 The characters handled by `nobreak-char-display' must be
6827 translated too.
6828
6829 Non-printable characters and raw-byte characters are also
6830 translated to octal form. */
6831 if (((c < ' ' || c == 127) /* ASCII control chars */
6832 ? (it->area != TEXT_AREA
6833 /* In mode line, treat \n, \t like other crl chars. */
6834 || (c != '\t'
6835 && it->glyph_row
6836 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6837 || (c != '\n' && c != '\t'))
6838 : (nonascii_space_p
6839 || nonascii_hyphen_p
6840 || CHAR_BYTE8_P (c)
6841 || ! CHAR_PRINTABLE_P (c))))
6842 {
6843 /* C is a control character, non-ASCII space/hyphen,
6844 raw-byte, or a non-printable character which must be
6845 displayed either as '\003' or as `^C' where the '\\'
6846 and '^' can be defined in the display table. Fill
6847 IT->ctl_chars with glyphs for what we have to
6848 display. Then, set IT->dpvec to these glyphs. */
6849 Lisp_Object gc;
6850 int ctl_len;
6851 int face_id;
6852 int lface_id = 0;
6853 int escape_glyph;
6854
6855 /* Handle control characters with ^. */
6856
6857 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6858 {
6859 int g;
6860
6861 g = '^'; /* default glyph for Control */
6862 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6863 if (it->dp
6864 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6865 {
6866 g = GLYPH_CODE_CHAR (gc);
6867 lface_id = GLYPH_CODE_FACE (gc);
6868 }
6869
6870 face_id = (lface_id
6871 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6872 : merge_escape_glyph_face (it));
6873
6874 XSETINT (it->ctl_chars[0], g);
6875 XSETINT (it->ctl_chars[1], c ^ 0100);
6876 ctl_len = 2;
6877 goto display_control;
6878 }
6879
6880 /* Handle non-ascii space in the mode where it only gets
6881 highlighting. */
6882
6883 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6884 {
6885 /* Merge `nobreak-space' into the current face. */
6886 face_id = merge_faces (it->f, Qnobreak_space, 0,
6887 it->face_id);
6888 XSETINT (it->ctl_chars[0], ' ');
6889 ctl_len = 1;
6890 goto display_control;
6891 }
6892
6893 /* Handle sequences that start with the "escape glyph". */
6894
6895 /* the default escape glyph is \. */
6896 escape_glyph = '\\';
6897
6898 if (it->dp
6899 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6900 {
6901 escape_glyph = GLYPH_CODE_CHAR (gc);
6902 lface_id = GLYPH_CODE_FACE (gc);
6903 }
6904
6905 face_id = (lface_id
6906 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6907 : merge_escape_glyph_face (it));
6908
6909 /* Draw non-ASCII hyphen with just highlighting: */
6910
6911 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6912 {
6913 XSETINT (it->ctl_chars[0], '-');
6914 ctl_len = 1;
6915 goto display_control;
6916 }
6917
6918 /* Draw non-ASCII space/hyphen with escape glyph: */
6919
6920 if (nonascii_space_p || nonascii_hyphen_p)
6921 {
6922 XSETINT (it->ctl_chars[0], escape_glyph);
6923 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6924 ctl_len = 2;
6925 goto display_control;
6926 }
6927
6928 {
6929 char str[10];
6930 int len, i;
6931
6932 if (CHAR_BYTE8_P (c))
6933 /* Display \200 instead of \17777600. */
6934 c = CHAR_TO_BYTE8 (c);
6935 len = sprintf (str, "%03o", c);
6936
6937 XSETINT (it->ctl_chars[0], escape_glyph);
6938 for (i = 0; i < len; i++)
6939 XSETINT (it->ctl_chars[i + 1], str[i]);
6940 ctl_len = len + 1;
6941 }
6942
6943 display_control:
6944 /* Set up IT->dpvec and return first character from it. */
6945 it->dpvec_char_len = it->len;
6946 it->dpvec = it->ctl_chars;
6947 it->dpend = it->dpvec + ctl_len;
6948 it->current.dpvec_index = 0;
6949 it->dpvec_face_id = face_id;
6950 it->saved_face_id = it->face_id;
6951 it->method = GET_FROM_DISPLAY_VECTOR;
6952 it->ellipsis_p = 0;
6953 goto get_next;
6954 }
6955 it->char_to_display = c;
6956 }
6957 else if (success_p)
6958 {
6959 it->char_to_display = it->c;
6960 }
6961 }
6962
6963 /* Adjust face id for a multibyte character. There are no multibyte
6964 character in unibyte text. */
6965 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6966 && it->multibyte_p
6967 && success_p
6968 && FRAME_WINDOW_P (it->f))
6969 {
6970 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6971
6972 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6973 {
6974 /* Automatic composition with glyph-string. */
6975 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6976
6977 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6978 }
6979 else
6980 {
6981 ptrdiff_t pos = (it->s ? -1
6982 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6983 : IT_CHARPOS (*it));
6984 int c;
6985
6986 if (it->what == IT_CHARACTER)
6987 c = it->char_to_display;
6988 else
6989 {
6990 struct composition *cmp = composition_table[it->cmp_it.id];
6991 int i;
6992
6993 c = ' ';
6994 for (i = 0; i < cmp->glyph_len; i++)
6995 /* TAB in a composition means display glyphs with
6996 padding space on the left or right. */
6997 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6998 break;
6999 }
7000 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7001 }
7002 }
7003
7004 done:
7005 /* Is this character the last one of a run of characters with
7006 box? If yes, set IT->end_of_box_run_p to 1. */
7007 if (it->face_box_p
7008 && it->s == NULL)
7009 {
7010 if (it->method == GET_FROM_STRING && it->sp)
7011 {
7012 int face_id = underlying_face_id (it);
7013 struct face *face = FACE_FROM_ID (it->f, face_id);
7014
7015 if (face)
7016 {
7017 if (face->box == FACE_NO_BOX)
7018 {
7019 /* If the box comes from face properties in a
7020 display string, check faces in that string. */
7021 int string_face_id = face_after_it_pos (it);
7022 it->end_of_box_run_p
7023 = (FACE_FROM_ID (it->f, string_face_id)->box
7024 == FACE_NO_BOX);
7025 }
7026 /* Otherwise, the box comes from the underlying face.
7027 If this is the last string character displayed, check
7028 the next buffer location. */
7029 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7030 && (it->current.overlay_string_index
7031 == it->n_overlay_strings - 1))
7032 {
7033 ptrdiff_t ignore;
7034 int next_face_id;
7035 struct text_pos pos = it->current.pos;
7036 INC_TEXT_POS (pos, it->multibyte_p);
7037
7038 next_face_id = face_at_buffer_position
7039 (it->w, CHARPOS (pos), it->region_beg_charpos,
7040 it->region_end_charpos, &ignore,
7041 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7042 -1);
7043 it->end_of_box_run_p
7044 = (FACE_FROM_ID (it->f, next_face_id)->box
7045 == FACE_NO_BOX);
7046 }
7047 }
7048 }
7049 /* next_element_from_display_vector sets this flag according to
7050 faces of the display vector glyphs, see there. */
7051 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7052 {
7053 int face_id = face_after_it_pos (it);
7054 it->end_of_box_run_p
7055 = (face_id != it->face_id
7056 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7057 }
7058 }
7059 /* If we reached the end of the object we've been iterating (e.g., a
7060 display string or an overlay string), and there's something on
7061 IT->stack, proceed with what's on the stack. It doesn't make
7062 sense to return zero if there's unprocessed stuff on the stack,
7063 because otherwise that stuff will never be displayed. */
7064 if (!success_p && it->sp > 0)
7065 {
7066 set_iterator_to_next (it, 0);
7067 success_p = get_next_display_element (it);
7068 }
7069
7070 /* Value is 0 if end of buffer or string reached. */
7071 return success_p;
7072 }
7073
7074
7075 /* Move IT to the next display element.
7076
7077 RESEAT_P non-zero means if called on a newline in buffer text,
7078 skip to the next visible line start.
7079
7080 Functions get_next_display_element and set_iterator_to_next are
7081 separate because I find this arrangement easier to handle than a
7082 get_next_display_element function that also increments IT's
7083 position. The way it is we can first look at an iterator's current
7084 display element, decide whether it fits on a line, and if it does,
7085 increment the iterator position. The other way around we probably
7086 would either need a flag indicating whether the iterator has to be
7087 incremented the next time, or we would have to implement a
7088 decrement position function which would not be easy to write. */
7089
7090 void
7091 set_iterator_to_next (struct it *it, int reseat_p)
7092 {
7093 /* Reset flags indicating start and end of a sequence of characters
7094 with box. Reset them at the start of this function because
7095 moving the iterator to a new position might set them. */
7096 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7097
7098 switch (it->method)
7099 {
7100 case GET_FROM_BUFFER:
7101 /* The current display element of IT is a character from
7102 current_buffer. Advance in the buffer, and maybe skip over
7103 invisible lines that are so because of selective display. */
7104 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7105 reseat_at_next_visible_line_start (it, 0);
7106 else if (it->cmp_it.id >= 0)
7107 {
7108 /* We are currently getting glyphs from a composition. */
7109 int i;
7110
7111 if (! it->bidi_p)
7112 {
7113 IT_CHARPOS (*it) += it->cmp_it.nchars;
7114 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7115 if (it->cmp_it.to < it->cmp_it.nglyphs)
7116 {
7117 it->cmp_it.from = it->cmp_it.to;
7118 }
7119 else
7120 {
7121 it->cmp_it.id = -1;
7122 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7123 IT_BYTEPOS (*it),
7124 it->end_charpos, Qnil);
7125 }
7126 }
7127 else if (! it->cmp_it.reversed_p)
7128 {
7129 /* Composition created while scanning forward. */
7130 /* Update IT's char/byte positions to point to the first
7131 character of the next grapheme cluster, or to the
7132 character visually after the current composition. */
7133 for (i = 0; i < it->cmp_it.nchars; i++)
7134 bidi_move_to_visually_next (&it->bidi_it);
7135 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7136 IT_CHARPOS (*it) = it->bidi_it.charpos;
7137
7138 if (it->cmp_it.to < it->cmp_it.nglyphs)
7139 {
7140 /* Proceed to the next grapheme cluster. */
7141 it->cmp_it.from = it->cmp_it.to;
7142 }
7143 else
7144 {
7145 /* No more grapheme clusters in this composition.
7146 Find the next stop position. */
7147 ptrdiff_t stop = it->end_charpos;
7148 if (it->bidi_it.scan_dir < 0)
7149 /* Now we are scanning backward and don't know
7150 where to stop. */
7151 stop = -1;
7152 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7153 IT_BYTEPOS (*it), stop, Qnil);
7154 }
7155 }
7156 else
7157 {
7158 /* Composition created while scanning backward. */
7159 /* Update IT's char/byte positions to point to the last
7160 character of the previous grapheme cluster, or the
7161 character visually after the current composition. */
7162 for (i = 0; i < it->cmp_it.nchars; i++)
7163 bidi_move_to_visually_next (&it->bidi_it);
7164 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7165 IT_CHARPOS (*it) = it->bidi_it.charpos;
7166 if (it->cmp_it.from > 0)
7167 {
7168 /* Proceed to the previous grapheme cluster. */
7169 it->cmp_it.to = it->cmp_it.from;
7170 }
7171 else
7172 {
7173 /* No more grapheme clusters in this composition.
7174 Find the next stop position. */
7175 ptrdiff_t stop = it->end_charpos;
7176 if (it->bidi_it.scan_dir < 0)
7177 /* Now we are scanning backward and don't know
7178 where to stop. */
7179 stop = -1;
7180 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7181 IT_BYTEPOS (*it), stop, Qnil);
7182 }
7183 }
7184 }
7185 else
7186 {
7187 eassert (it->len != 0);
7188
7189 if (!it->bidi_p)
7190 {
7191 IT_BYTEPOS (*it) += it->len;
7192 IT_CHARPOS (*it) += 1;
7193 }
7194 else
7195 {
7196 int prev_scan_dir = it->bidi_it.scan_dir;
7197 /* If this is a new paragraph, determine its base
7198 direction (a.k.a. its base embedding level). */
7199 if (it->bidi_it.new_paragraph)
7200 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7201 bidi_move_to_visually_next (&it->bidi_it);
7202 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7203 IT_CHARPOS (*it) = it->bidi_it.charpos;
7204 if (prev_scan_dir != it->bidi_it.scan_dir)
7205 {
7206 /* As the scan direction was changed, we must
7207 re-compute the stop position for composition. */
7208 ptrdiff_t stop = it->end_charpos;
7209 if (it->bidi_it.scan_dir < 0)
7210 stop = -1;
7211 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7212 IT_BYTEPOS (*it), stop, Qnil);
7213 }
7214 }
7215 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7216 }
7217 break;
7218
7219 case GET_FROM_C_STRING:
7220 /* Current display element of IT is from a C string. */
7221 if (!it->bidi_p
7222 /* If the string position is beyond string's end, it means
7223 next_element_from_c_string is padding the string with
7224 blanks, in which case we bypass the bidi iterator,
7225 because it cannot deal with such virtual characters. */
7226 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7227 {
7228 IT_BYTEPOS (*it) += it->len;
7229 IT_CHARPOS (*it) += 1;
7230 }
7231 else
7232 {
7233 bidi_move_to_visually_next (&it->bidi_it);
7234 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7235 IT_CHARPOS (*it) = it->bidi_it.charpos;
7236 }
7237 break;
7238
7239 case GET_FROM_DISPLAY_VECTOR:
7240 /* Current display element of IT is from a display table entry.
7241 Advance in the display table definition. Reset it to null if
7242 end reached, and continue with characters from buffers/
7243 strings. */
7244 ++it->current.dpvec_index;
7245
7246 /* Restore face of the iterator to what they were before the
7247 display vector entry (these entries may contain faces). */
7248 it->face_id = it->saved_face_id;
7249
7250 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7251 {
7252 int recheck_faces = it->ellipsis_p;
7253
7254 if (it->s)
7255 it->method = GET_FROM_C_STRING;
7256 else if (STRINGP (it->string))
7257 it->method = GET_FROM_STRING;
7258 else
7259 {
7260 it->method = GET_FROM_BUFFER;
7261 it->object = it->w->contents;
7262 }
7263
7264 it->dpvec = NULL;
7265 it->current.dpvec_index = -1;
7266
7267 /* Skip over characters which were displayed via IT->dpvec. */
7268 if (it->dpvec_char_len < 0)
7269 reseat_at_next_visible_line_start (it, 1);
7270 else if (it->dpvec_char_len > 0)
7271 {
7272 if (it->method == GET_FROM_STRING
7273 && it->current.overlay_string_index >= 0
7274 && it->n_overlay_strings > 0)
7275 it->ignore_overlay_strings_at_pos_p = 1;
7276 it->len = it->dpvec_char_len;
7277 set_iterator_to_next (it, reseat_p);
7278 }
7279
7280 /* Maybe recheck faces after display vector */
7281 if (recheck_faces)
7282 it->stop_charpos = IT_CHARPOS (*it);
7283 }
7284 break;
7285
7286 case GET_FROM_STRING:
7287 /* Current display element is a character from a Lisp string. */
7288 eassert (it->s == NULL && STRINGP (it->string));
7289 /* Don't advance past string end. These conditions are true
7290 when set_iterator_to_next is called at the end of
7291 get_next_display_element, in which case the Lisp string is
7292 already exhausted, and all we want is pop the iterator
7293 stack. */
7294 if (it->current.overlay_string_index >= 0)
7295 {
7296 /* This is an overlay string, so there's no padding with
7297 spaces, and the number of characters in the string is
7298 where the string ends. */
7299 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7300 goto consider_string_end;
7301 }
7302 else
7303 {
7304 /* Not an overlay string. There could be padding, so test
7305 against it->end_charpos . */
7306 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7307 goto consider_string_end;
7308 }
7309 if (it->cmp_it.id >= 0)
7310 {
7311 int i;
7312
7313 if (! it->bidi_p)
7314 {
7315 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7316 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7317 if (it->cmp_it.to < it->cmp_it.nglyphs)
7318 it->cmp_it.from = it->cmp_it.to;
7319 else
7320 {
7321 it->cmp_it.id = -1;
7322 composition_compute_stop_pos (&it->cmp_it,
7323 IT_STRING_CHARPOS (*it),
7324 IT_STRING_BYTEPOS (*it),
7325 it->end_charpos, it->string);
7326 }
7327 }
7328 else if (! it->cmp_it.reversed_p)
7329 {
7330 for (i = 0; i < it->cmp_it.nchars; i++)
7331 bidi_move_to_visually_next (&it->bidi_it);
7332 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7333 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7334
7335 if (it->cmp_it.to < it->cmp_it.nglyphs)
7336 it->cmp_it.from = it->cmp_it.to;
7337 else
7338 {
7339 ptrdiff_t stop = it->end_charpos;
7340 if (it->bidi_it.scan_dir < 0)
7341 stop = -1;
7342 composition_compute_stop_pos (&it->cmp_it,
7343 IT_STRING_CHARPOS (*it),
7344 IT_STRING_BYTEPOS (*it), stop,
7345 it->string);
7346 }
7347 }
7348 else
7349 {
7350 for (i = 0; i < it->cmp_it.nchars; i++)
7351 bidi_move_to_visually_next (&it->bidi_it);
7352 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7353 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7354 if (it->cmp_it.from > 0)
7355 it->cmp_it.to = it->cmp_it.from;
7356 else
7357 {
7358 ptrdiff_t stop = it->end_charpos;
7359 if (it->bidi_it.scan_dir < 0)
7360 stop = -1;
7361 composition_compute_stop_pos (&it->cmp_it,
7362 IT_STRING_CHARPOS (*it),
7363 IT_STRING_BYTEPOS (*it), stop,
7364 it->string);
7365 }
7366 }
7367 }
7368 else
7369 {
7370 if (!it->bidi_p
7371 /* If the string position is beyond string's end, it
7372 means next_element_from_string is padding the string
7373 with blanks, in which case we bypass the bidi
7374 iterator, because it cannot deal with such virtual
7375 characters. */
7376 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7377 {
7378 IT_STRING_BYTEPOS (*it) += it->len;
7379 IT_STRING_CHARPOS (*it) += 1;
7380 }
7381 else
7382 {
7383 int prev_scan_dir = it->bidi_it.scan_dir;
7384
7385 bidi_move_to_visually_next (&it->bidi_it);
7386 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7387 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7388 if (prev_scan_dir != it->bidi_it.scan_dir)
7389 {
7390 ptrdiff_t stop = it->end_charpos;
7391
7392 if (it->bidi_it.scan_dir < 0)
7393 stop = -1;
7394 composition_compute_stop_pos (&it->cmp_it,
7395 IT_STRING_CHARPOS (*it),
7396 IT_STRING_BYTEPOS (*it), stop,
7397 it->string);
7398 }
7399 }
7400 }
7401
7402 consider_string_end:
7403
7404 if (it->current.overlay_string_index >= 0)
7405 {
7406 /* IT->string is an overlay string. Advance to the
7407 next, if there is one. */
7408 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7409 {
7410 it->ellipsis_p = 0;
7411 next_overlay_string (it);
7412 if (it->ellipsis_p)
7413 setup_for_ellipsis (it, 0);
7414 }
7415 }
7416 else
7417 {
7418 /* IT->string is not an overlay string. If we reached
7419 its end, and there is something on IT->stack, proceed
7420 with what is on the stack. This can be either another
7421 string, this time an overlay string, or a buffer. */
7422 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7423 && it->sp > 0)
7424 {
7425 pop_it (it);
7426 if (it->method == GET_FROM_STRING)
7427 goto consider_string_end;
7428 }
7429 }
7430 break;
7431
7432 case GET_FROM_IMAGE:
7433 case GET_FROM_STRETCH:
7434 /* The position etc with which we have to proceed are on
7435 the stack. The position may be at the end of a string,
7436 if the `display' property takes up the whole string. */
7437 eassert (it->sp > 0);
7438 pop_it (it);
7439 if (it->method == GET_FROM_STRING)
7440 goto consider_string_end;
7441 break;
7442
7443 default:
7444 /* There are no other methods defined, so this should be a bug. */
7445 emacs_abort ();
7446 }
7447
7448 eassert (it->method != GET_FROM_STRING
7449 || (STRINGP (it->string)
7450 && IT_STRING_CHARPOS (*it) >= 0));
7451 }
7452
7453 /* Load IT's display element fields with information about the next
7454 display element which comes from a display table entry or from the
7455 result of translating a control character to one of the forms `^C'
7456 or `\003'.
7457
7458 IT->dpvec holds the glyphs to return as characters.
7459 IT->saved_face_id holds the face id before the display vector--it
7460 is restored into IT->face_id in set_iterator_to_next. */
7461
7462 static int
7463 next_element_from_display_vector (struct it *it)
7464 {
7465 Lisp_Object gc;
7466 int prev_face_id = it->face_id;
7467 int next_face_id;
7468
7469 /* Precondition. */
7470 eassert (it->dpvec && it->current.dpvec_index >= 0);
7471
7472 it->face_id = it->saved_face_id;
7473
7474 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7475 That seemed totally bogus - so I changed it... */
7476 gc = it->dpvec[it->current.dpvec_index];
7477
7478 if (GLYPH_CODE_P (gc))
7479 {
7480 struct face *this_face, *prev_face, *next_face;
7481
7482 it->c = GLYPH_CODE_CHAR (gc);
7483 it->len = CHAR_BYTES (it->c);
7484
7485 /* The entry may contain a face id to use. Such a face id is
7486 the id of a Lisp face, not a realized face. A face id of
7487 zero means no face is specified. */
7488 if (it->dpvec_face_id >= 0)
7489 it->face_id = it->dpvec_face_id;
7490 else
7491 {
7492 int lface_id = GLYPH_CODE_FACE (gc);
7493 if (lface_id > 0)
7494 it->face_id = merge_faces (it->f, Qt, lface_id,
7495 it->saved_face_id);
7496 }
7497
7498 /* Glyphs in the display vector could have the box face, so we
7499 need to set the related flags in the iterator, as
7500 appropriate. */
7501 this_face = FACE_FROM_ID (it->f, it->face_id);
7502 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7503
7504 /* Is this character the first character of a box-face run? */
7505 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7506 && (!prev_face
7507 || prev_face->box == FACE_NO_BOX));
7508
7509 /* For the last character of the box-face run, we need to look
7510 either at the next glyph from the display vector, or at the
7511 face we saw before the display vector. */
7512 next_face_id = it->saved_face_id;
7513 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7514 {
7515 if (it->dpvec_face_id >= 0)
7516 next_face_id = it->dpvec_face_id;
7517 else
7518 {
7519 int lface_id =
7520 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7521
7522 if (lface_id > 0)
7523 next_face_id = merge_faces (it->f, Qt, lface_id,
7524 it->saved_face_id);
7525 }
7526 }
7527 next_face = FACE_FROM_ID (it->f, next_face_id);
7528 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7529 && (!next_face
7530 || next_face->box == FACE_NO_BOX));
7531 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7532 }
7533 else
7534 /* Display table entry is invalid. Return a space. */
7535 it->c = ' ', it->len = 1;
7536
7537 /* Don't change position and object of the iterator here. They are
7538 still the values of the character that had this display table
7539 entry or was translated, and that's what we want. */
7540 it->what = IT_CHARACTER;
7541 return 1;
7542 }
7543
7544 /* Get the first element of string/buffer in the visual order, after
7545 being reseated to a new position in a string or a buffer. */
7546 static void
7547 get_visually_first_element (struct it *it)
7548 {
7549 int string_p = STRINGP (it->string) || it->s;
7550 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7551 ptrdiff_t bob = (string_p ? 0 : BEGV);
7552
7553 if (STRINGP (it->string))
7554 {
7555 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7556 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7557 }
7558 else
7559 {
7560 it->bidi_it.charpos = IT_CHARPOS (*it);
7561 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7562 }
7563
7564 if (it->bidi_it.charpos == eob)
7565 {
7566 /* Nothing to do, but reset the FIRST_ELT flag, like
7567 bidi_paragraph_init does, because we are not going to
7568 call it. */
7569 it->bidi_it.first_elt = 0;
7570 }
7571 else if (it->bidi_it.charpos == bob
7572 || (!string_p
7573 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7574 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7575 {
7576 /* If we are at the beginning of a line/string, we can produce
7577 the next element right away. */
7578 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7579 bidi_move_to_visually_next (&it->bidi_it);
7580 }
7581 else
7582 {
7583 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7584
7585 /* We need to prime the bidi iterator starting at the line's or
7586 string's beginning, before we will be able to produce the
7587 next element. */
7588 if (string_p)
7589 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7590 else
7591 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7592 IT_BYTEPOS (*it), -1,
7593 &it->bidi_it.bytepos);
7594 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7595 do
7596 {
7597 /* Now return to buffer/string position where we were asked
7598 to get the next display element, and produce that. */
7599 bidi_move_to_visually_next (&it->bidi_it);
7600 }
7601 while (it->bidi_it.bytepos != orig_bytepos
7602 && it->bidi_it.charpos < eob);
7603 }
7604
7605 /* Adjust IT's position information to where we ended up. */
7606 if (STRINGP (it->string))
7607 {
7608 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7609 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7610 }
7611 else
7612 {
7613 IT_CHARPOS (*it) = it->bidi_it.charpos;
7614 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7615 }
7616
7617 if (STRINGP (it->string) || !it->s)
7618 {
7619 ptrdiff_t stop, charpos, bytepos;
7620
7621 if (STRINGP (it->string))
7622 {
7623 eassert (!it->s);
7624 stop = SCHARS (it->string);
7625 if (stop > it->end_charpos)
7626 stop = it->end_charpos;
7627 charpos = IT_STRING_CHARPOS (*it);
7628 bytepos = IT_STRING_BYTEPOS (*it);
7629 }
7630 else
7631 {
7632 stop = it->end_charpos;
7633 charpos = IT_CHARPOS (*it);
7634 bytepos = IT_BYTEPOS (*it);
7635 }
7636 if (it->bidi_it.scan_dir < 0)
7637 stop = -1;
7638 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7639 it->string);
7640 }
7641 }
7642
7643 /* Load IT with the next display element from Lisp string IT->string.
7644 IT->current.string_pos is the current position within the string.
7645 If IT->current.overlay_string_index >= 0, the Lisp string is an
7646 overlay string. */
7647
7648 static int
7649 next_element_from_string (struct it *it)
7650 {
7651 struct text_pos position;
7652
7653 eassert (STRINGP (it->string));
7654 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7655 eassert (IT_STRING_CHARPOS (*it) >= 0);
7656 position = it->current.string_pos;
7657
7658 /* With bidi reordering, the character to display might not be the
7659 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7660 that we were reseat()ed to a new string, whose paragraph
7661 direction is not known. */
7662 if (it->bidi_p && it->bidi_it.first_elt)
7663 {
7664 get_visually_first_element (it);
7665 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7666 }
7667
7668 /* Time to check for invisible text? */
7669 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7670 {
7671 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7672 {
7673 if (!(!it->bidi_p
7674 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7675 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7676 {
7677 /* With bidi non-linear iteration, we could find
7678 ourselves far beyond the last computed stop_charpos,
7679 with several other stop positions in between that we
7680 missed. Scan them all now, in buffer's logical
7681 order, until we find and handle the last stop_charpos
7682 that precedes our current position. */
7683 handle_stop_backwards (it, it->stop_charpos);
7684 return GET_NEXT_DISPLAY_ELEMENT (it);
7685 }
7686 else
7687 {
7688 if (it->bidi_p)
7689 {
7690 /* Take note of the stop position we just moved
7691 across, for when we will move back across it. */
7692 it->prev_stop = it->stop_charpos;
7693 /* If we are at base paragraph embedding level, take
7694 note of the last stop position seen at this
7695 level. */
7696 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7697 it->base_level_stop = it->stop_charpos;
7698 }
7699 handle_stop (it);
7700
7701 /* Since a handler may have changed IT->method, we must
7702 recurse here. */
7703 return GET_NEXT_DISPLAY_ELEMENT (it);
7704 }
7705 }
7706 else if (it->bidi_p
7707 /* If we are before prev_stop, we may have overstepped
7708 on our way backwards a stop_pos, and if so, we need
7709 to handle that stop_pos. */
7710 && IT_STRING_CHARPOS (*it) < it->prev_stop
7711 /* We can sometimes back up for reasons that have nothing
7712 to do with bidi reordering. E.g., compositions. The
7713 code below is only needed when we are above the base
7714 embedding level, so test for that explicitly. */
7715 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7716 {
7717 /* If we lost track of base_level_stop, we have no better
7718 place for handle_stop_backwards to start from than string
7719 beginning. This happens, e.g., when we were reseated to
7720 the previous screenful of text by vertical-motion. */
7721 if (it->base_level_stop <= 0
7722 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7723 it->base_level_stop = 0;
7724 handle_stop_backwards (it, it->base_level_stop);
7725 return GET_NEXT_DISPLAY_ELEMENT (it);
7726 }
7727 }
7728
7729 if (it->current.overlay_string_index >= 0)
7730 {
7731 /* Get the next character from an overlay string. In overlay
7732 strings, there is no field width or padding with spaces to
7733 do. */
7734 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7735 {
7736 it->what = IT_EOB;
7737 return 0;
7738 }
7739 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7740 IT_STRING_BYTEPOS (*it),
7741 it->bidi_it.scan_dir < 0
7742 ? -1
7743 : SCHARS (it->string))
7744 && next_element_from_composition (it))
7745 {
7746 return 1;
7747 }
7748 else if (STRING_MULTIBYTE (it->string))
7749 {
7750 const unsigned char *s = (SDATA (it->string)
7751 + IT_STRING_BYTEPOS (*it));
7752 it->c = string_char_and_length (s, &it->len);
7753 }
7754 else
7755 {
7756 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7757 it->len = 1;
7758 }
7759 }
7760 else
7761 {
7762 /* Get the next character from a Lisp string that is not an
7763 overlay string. Such strings come from the mode line, for
7764 example. We may have to pad with spaces, or truncate the
7765 string. See also next_element_from_c_string. */
7766 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7767 {
7768 it->what = IT_EOB;
7769 return 0;
7770 }
7771 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7772 {
7773 /* Pad with spaces. */
7774 it->c = ' ', it->len = 1;
7775 CHARPOS (position) = BYTEPOS (position) = -1;
7776 }
7777 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7778 IT_STRING_BYTEPOS (*it),
7779 it->bidi_it.scan_dir < 0
7780 ? -1
7781 : it->string_nchars)
7782 && next_element_from_composition (it))
7783 {
7784 return 1;
7785 }
7786 else if (STRING_MULTIBYTE (it->string))
7787 {
7788 const unsigned char *s = (SDATA (it->string)
7789 + IT_STRING_BYTEPOS (*it));
7790 it->c = string_char_and_length (s, &it->len);
7791 }
7792 else
7793 {
7794 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7795 it->len = 1;
7796 }
7797 }
7798
7799 /* Record what we have and where it came from. */
7800 it->what = IT_CHARACTER;
7801 it->object = it->string;
7802 it->position = position;
7803 return 1;
7804 }
7805
7806
7807 /* Load IT with next display element from C string IT->s.
7808 IT->string_nchars is the maximum number of characters to return
7809 from the string. IT->end_charpos may be greater than
7810 IT->string_nchars when this function is called, in which case we
7811 may have to return padding spaces. Value is zero if end of string
7812 reached, including padding spaces. */
7813
7814 static int
7815 next_element_from_c_string (struct it *it)
7816 {
7817 int success_p = 1;
7818
7819 eassert (it->s);
7820 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7821 it->what = IT_CHARACTER;
7822 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7823 it->object = Qnil;
7824
7825 /* With bidi reordering, the character to display might not be the
7826 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7827 we were reseated to a new string, whose paragraph direction is
7828 not known. */
7829 if (it->bidi_p && it->bidi_it.first_elt)
7830 get_visually_first_element (it);
7831
7832 /* IT's position can be greater than IT->string_nchars in case a
7833 field width or precision has been specified when the iterator was
7834 initialized. */
7835 if (IT_CHARPOS (*it) >= it->end_charpos)
7836 {
7837 /* End of the game. */
7838 it->what = IT_EOB;
7839 success_p = 0;
7840 }
7841 else if (IT_CHARPOS (*it) >= it->string_nchars)
7842 {
7843 /* Pad with spaces. */
7844 it->c = ' ', it->len = 1;
7845 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7846 }
7847 else if (it->multibyte_p)
7848 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7849 else
7850 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7851
7852 return success_p;
7853 }
7854
7855
7856 /* Set up IT to return characters from an ellipsis, if appropriate.
7857 The definition of the ellipsis glyphs may come from a display table
7858 entry. This function fills IT with the first glyph from the
7859 ellipsis if an ellipsis is to be displayed. */
7860
7861 static int
7862 next_element_from_ellipsis (struct it *it)
7863 {
7864 if (it->selective_display_ellipsis_p)
7865 setup_for_ellipsis (it, it->len);
7866 else
7867 {
7868 /* The face at the current position may be different from the
7869 face we find after the invisible text. Remember what it
7870 was in IT->saved_face_id, and signal that it's there by
7871 setting face_before_selective_p. */
7872 it->saved_face_id = it->face_id;
7873 it->method = GET_FROM_BUFFER;
7874 it->object = it->w->contents;
7875 reseat_at_next_visible_line_start (it, 1);
7876 it->face_before_selective_p = 1;
7877 }
7878
7879 return GET_NEXT_DISPLAY_ELEMENT (it);
7880 }
7881
7882
7883 /* Deliver an image display element. The iterator IT is already
7884 filled with image information (done in handle_display_prop). Value
7885 is always 1. */
7886
7887
7888 static int
7889 next_element_from_image (struct it *it)
7890 {
7891 it->what = IT_IMAGE;
7892 it->ignore_overlay_strings_at_pos_p = 0;
7893 return 1;
7894 }
7895
7896
7897 /* Fill iterator IT with next display element from a stretch glyph
7898 property. IT->object is the value of the text property. Value is
7899 always 1. */
7900
7901 static int
7902 next_element_from_stretch (struct it *it)
7903 {
7904 it->what = IT_STRETCH;
7905 return 1;
7906 }
7907
7908 /* Scan backwards from IT's current position until we find a stop
7909 position, or until BEGV. This is called when we find ourself
7910 before both the last known prev_stop and base_level_stop while
7911 reordering bidirectional text. */
7912
7913 static void
7914 compute_stop_pos_backwards (struct it *it)
7915 {
7916 const int SCAN_BACK_LIMIT = 1000;
7917 struct text_pos pos;
7918 struct display_pos save_current = it->current;
7919 struct text_pos save_position = it->position;
7920 ptrdiff_t charpos = IT_CHARPOS (*it);
7921 ptrdiff_t where_we_are = charpos;
7922 ptrdiff_t save_stop_pos = it->stop_charpos;
7923 ptrdiff_t save_end_pos = it->end_charpos;
7924
7925 eassert (NILP (it->string) && !it->s);
7926 eassert (it->bidi_p);
7927 it->bidi_p = 0;
7928 do
7929 {
7930 it->end_charpos = min (charpos + 1, ZV);
7931 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7932 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7933 reseat_1 (it, pos, 0);
7934 compute_stop_pos (it);
7935 /* We must advance forward, right? */
7936 if (it->stop_charpos <= charpos)
7937 emacs_abort ();
7938 }
7939 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7940
7941 if (it->stop_charpos <= where_we_are)
7942 it->prev_stop = it->stop_charpos;
7943 else
7944 it->prev_stop = BEGV;
7945 it->bidi_p = 1;
7946 it->current = save_current;
7947 it->position = save_position;
7948 it->stop_charpos = save_stop_pos;
7949 it->end_charpos = save_end_pos;
7950 }
7951
7952 /* Scan forward from CHARPOS in the current buffer/string, until we
7953 find a stop position > current IT's position. Then handle the stop
7954 position before that. This is called when we bump into a stop
7955 position while reordering bidirectional text. CHARPOS should be
7956 the last previously processed stop_pos (or BEGV/0, if none were
7957 processed yet) whose position is less that IT's current
7958 position. */
7959
7960 static void
7961 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7962 {
7963 int bufp = !STRINGP (it->string);
7964 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7965 struct display_pos save_current = it->current;
7966 struct text_pos save_position = it->position;
7967 struct text_pos pos1;
7968 ptrdiff_t next_stop;
7969
7970 /* Scan in strict logical order. */
7971 eassert (it->bidi_p);
7972 it->bidi_p = 0;
7973 do
7974 {
7975 it->prev_stop = charpos;
7976 if (bufp)
7977 {
7978 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7979 reseat_1 (it, pos1, 0);
7980 }
7981 else
7982 it->current.string_pos = string_pos (charpos, it->string);
7983 compute_stop_pos (it);
7984 /* We must advance forward, right? */
7985 if (it->stop_charpos <= it->prev_stop)
7986 emacs_abort ();
7987 charpos = it->stop_charpos;
7988 }
7989 while (charpos <= where_we_are);
7990
7991 it->bidi_p = 1;
7992 it->current = save_current;
7993 it->position = save_position;
7994 next_stop = it->stop_charpos;
7995 it->stop_charpos = it->prev_stop;
7996 handle_stop (it);
7997 it->stop_charpos = next_stop;
7998 }
7999
8000 /* Load IT with the next display element from current_buffer. Value
8001 is zero if end of buffer reached. IT->stop_charpos is the next
8002 position at which to stop and check for text properties or buffer
8003 end. */
8004
8005 static int
8006 next_element_from_buffer (struct it *it)
8007 {
8008 int success_p = 1;
8009
8010 eassert (IT_CHARPOS (*it) >= BEGV);
8011 eassert (NILP (it->string) && !it->s);
8012 eassert (!it->bidi_p
8013 || (EQ (it->bidi_it.string.lstring, Qnil)
8014 && it->bidi_it.string.s == NULL));
8015
8016 /* With bidi reordering, the character to display might not be the
8017 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8018 we were reseat()ed to a new buffer position, which is potentially
8019 a different paragraph. */
8020 if (it->bidi_p && it->bidi_it.first_elt)
8021 {
8022 get_visually_first_element (it);
8023 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8024 }
8025
8026 if (IT_CHARPOS (*it) >= it->stop_charpos)
8027 {
8028 if (IT_CHARPOS (*it) >= it->end_charpos)
8029 {
8030 int overlay_strings_follow_p;
8031
8032 /* End of the game, except when overlay strings follow that
8033 haven't been returned yet. */
8034 if (it->overlay_strings_at_end_processed_p)
8035 overlay_strings_follow_p = 0;
8036 else
8037 {
8038 it->overlay_strings_at_end_processed_p = 1;
8039 overlay_strings_follow_p = get_overlay_strings (it, 0);
8040 }
8041
8042 if (overlay_strings_follow_p)
8043 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8044 else
8045 {
8046 it->what = IT_EOB;
8047 it->position = it->current.pos;
8048 success_p = 0;
8049 }
8050 }
8051 else if (!(!it->bidi_p
8052 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8053 || IT_CHARPOS (*it) == it->stop_charpos))
8054 {
8055 /* With bidi non-linear iteration, we could find ourselves
8056 far beyond the last computed stop_charpos, with several
8057 other stop positions in between that we missed. Scan
8058 them all now, in buffer's logical order, until we find
8059 and handle the last stop_charpos that precedes our
8060 current position. */
8061 handle_stop_backwards (it, it->stop_charpos);
8062 return GET_NEXT_DISPLAY_ELEMENT (it);
8063 }
8064 else
8065 {
8066 if (it->bidi_p)
8067 {
8068 /* Take note of the stop position we just moved across,
8069 for when we will move back across it. */
8070 it->prev_stop = it->stop_charpos;
8071 /* If we are at base paragraph embedding level, take
8072 note of the last stop position seen at this
8073 level. */
8074 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8075 it->base_level_stop = it->stop_charpos;
8076 }
8077 handle_stop (it);
8078 return GET_NEXT_DISPLAY_ELEMENT (it);
8079 }
8080 }
8081 else if (it->bidi_p
8082 /* If we are before prev_stop, we may have overstepped on
8083 our way backwards a stop_pos, and if so, we need to
8084 handle that stop_pos. */
8085 && IT_CHARPOS (*it) < it->prev_stop
8086 /* We can sometimes back up for reasons that have nothing
8087 to do with bidi reordering. E.g., compositions. The
8088 code below is only needed when we are above the base
8089 embedding level, so test for that explicitly. */
8090 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8091 {
8092 if (it->base_level_stop <= 0
8093 || IT_CHARPOS (*it) < it->base_level_stop)
8094 {
8095 /* If we lost track of base_level_stop, we need to find
8096 prev_stop by looking backwards. This happens, e.g., when
8097 we were reseated to the previous screenful of text by
8098 vertical-motion. */
8099 it->base_level_stop = BEGV;
8100 compute_stop_pos_backwards (it);
8101 handle_stop_backwards (it, it->prev_stop);
8102 }
8103 else
8104 handle_stop_backwards (it, it->base_level_stop);
8105 return GET_NEXT_DISPLAY_ELEMENT (it);
8106 }
8107 else
8108 {
8109 /* No face changes, overlays etc. in sight, so just return a
8110 character from current_buffer. */
8111 unsigned char *p;
8112 ptrdiff_t stop;
8113
8114 /* Maybe run the redisplay end trigger hook. Performance note:
8115 This doesn't seem to cost measurable time. */
8116 if (it->redisplay_end_trigger_charpos
8117 && it->glyph_row
8118 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8119 run_redisplay_end_trigger_hook (it);
8120
8121 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8122 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8123 stop)
8124 && next_element_from_composition (it))
8125 {
8126 return 1;
8127 }
8128
8129 /* Get the next character, maybe multibyte. */
8130 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8131 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8132 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8133 else
8134 it->c = *p, it->len = 1;
8135
8136 /* Record what we have and where it came from. */
8137 it->what = IT_CHARACTER;
8138 it->object = it->w->contents;
8139 it->position = it->current.pos;
8140
8141 /* Normally we return the character found above, except when we
8142 really want to return an ellipsis for selective display. */
8143 if (it->selective)
8144 {
8145 if (it->c == '\n')
8146 {
8147 /* A value of selective > 0 means hide lines indented more
8148 than that number of columns. */
8149 if (it->selective > 0
8150 && IT_CHARPOS (*it) + 1 < ZV
8151 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8152 IT_BYTEPOS (*it) + 1,
8153 it->selective))
8154 {
8155 success_p = next_element_from_ellipsis (it);
8156 it->dpvec_char_len = -1;
8157 }
8158 }
8159 else if (it->c == '\r' && it->selective == -1)
8160 {
8161 /* A value of selective == -1 means that everything from the
8162 CR to the end of the line is invisible, with maybe an
8163 ellipsis displayed for it. */
8164 success_p = next_element_from_ellipsis (it);
8165 it->dpvec_char_len = -1;
8166 }
8167 }
8168 }
8169
8170 /* Value is zero if end of buffer reached. */
8171 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8172 return success_p;
8173 }
8174
8175
8176 /* Run the redisplay end trigger hook for IT. */
8177
8178 static void
8179 run_redisplay_end_trigger_hook (struct it *it)
8180 {
8181 Lisp_Object args[3];
8182
8183 /* IT->glyph_row should be non-null, i.e. we should be actually
8184 displaying something, or otherwise we should not run the hook. */
8185 eassert (it->glyph_row);
8186
8187 /* Set up hook arguments. */
8188 args[0] = Qredisplay_end_trigger_functions;
8189 args[1] = it->window;
8190 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8191 it->redisplay_end_trigger_charpos = 0;
8192
8193 /* Since we are *trying* to run these functions, don't try to run
8194 them again, even if they get an error. */
8195 wset_redisplay_end_trigger (it->w, Qnil);
8196 Frun_hook_with_args (3, args);
8197
8198 /* Notice if it changed the face of the character we are on. */
8199 handle_face_prop (it);
8200 }
8201
8202
8203 /* Deliver a composition display element. Unlike the other
8204 next_element_from_XXX, this function is not registered in the array
8205 get_next_element[]. It is called from next_element_from_buffer and
8206 next_element_from_string when necessary. */
8207
8208 static int
8209 next_element_from_composition (struct it *it)
8210 {
8211 it->what = IT_COMPOSITION;
8212 it->len = it->cmp_it.nbytes;
8213 if (STRINGP (it->string))
8214 {
8215 if (it->c < 0)
8216 {
8217 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8218 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8219 return 0;
8220 }
8221 it->position = it->current.string_pos;
8222 it->object = it->string;
8223 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8224 IT_STRING_BYTEPOS (*it), it->string);
8225 }
8226 else
8227 {
8228 if (it->c < 0)
8229 {
8230 IT_CHARPOS (*it) += it->cmp_it.nchars;
8231 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8232 if (it->bidi_p)
8233 {
8234 if (it->bidi_it.new_paragraph)
8235 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8236 /* Resync the bidi iterator with IT's new position.
8237 FIXME: this doesn't support bidirectional text. */
8238 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8239 bidi_move_to_visually_next (&it->bidi_it);
8240 }
8241 return 0;
8242 }
8243 it->position = it->current.pos;
8244 it->object = it->w->contents;
8245 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8246 IT_BYTEPOS (*it), Qnil);
8247 }
8248 return 1;
8249 }
8250
8251
8252 \f
8253 /***********************************************************************
8254 Moving an iterator without producing glyphs
8255 ***********************************************************************/
8256
8257 /* Check if iterator is at a position corresponding to a valid buffer
8258 position after some move_it_ call. */
8259
8260 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8261 ((it)->method == GET_FROM_STRING \
8262 ? IT_STRING_CHARPOS (*it) == 0 \
8263 : 1)
8264
8265
8266 /* Move iterator IT to a specified buffer or X position within one
8267 line on the display without producing glyphs.
8268
8269 OP should be a bit mask including some or all of these bits:
8270 MOVE_TO_X: Stop upon reaching x-position TO_X.
8271 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8272 Regardless of OP's value, stop upon reaching the end of the display line.
8273
8274 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8275 This means, in particular, that TO_X includes window's horizontal
8276 scroll amount.
8277
8278 The return value has several possible values that
8279 say what condition caused the scan to stop:
8280
8281 MOVE_POS_MATCH_OR_ZV
8282 - when TO_POS or ZV was reached.
8283
8284 MOVE_X_REACHED
8285 -when TO_X was reached before TO_POS or ZV were reached.
8286
8287 MOVE_LINE_CONTINUED
8288 - when we reached the end of the display area and the line must
8289 be continued.
8290
8291 MOVE_LINE_TRUNCATED
8292 - when we reached the end of the display area and the line is
8293 truncated.
8294
8295 MOVE_NEWLINE_OR_CR
8296 - when we stopped at a line end, i.e. a newline or a CR and selective
8297 display is on. */
8298
8299 static enum move_it_result
8300 move_it_in_display_line_to (struct it *it,
8301 ptrdiff_t to_charpos, int to_x,
8302 enum move_operation_enum op)
8303 {
8304 enum move_it_result result = MOVE_UNDEFINED;
8305 struct glyph_row *saved_glyph_row;
8306 struct it wrap_it, atpos_it, atx_it, ppos_it;
8307 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8308 void *ppos_data = NULL;
8309 int may_wrap = 0;
8310 enum it_method prev_method = it->method;
8311 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8312 int saw_smaller_pos = prev_pos < to_charpos;
8313
8314 /* Don't produce glyphs in produce_glyphs. */
8315 saved_glyph_row = it->glyph_row;
8316 it->glyph_row = NULL;
8317
8318 /* Use wrap_it to save a copy of IT wherever a word wrap could
8319 occur. Use atpos_it to save a copy of IT at the desired buffer
8320 position, if found, so that we can scan ahead and check if the
8321 word later overshoots the window edge. Use atx_it similarly, for
8322 pixel positions. */
8323 wrap_it.sp = -1;
8324 atpos_it.sp = -1;
8325 atx_it.sp = -1;
8326
8327 /* Use ppos_it under bidi reordering to save a copy of IT for the
8328 position > CHARPOS that is the closest to CHARPOS. We restore
8329 that position in IT when we have scanned the entire display line
8330 without finding a match for CHARPOS and all the character
8331 positions are greater than CHARPOS. */
8332 if (it->bidi_p)
8333 {
8334 SAVE_IT (ppos_it, *it, ppos_data);
8335 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8336 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8337 SAVE_IT (ppos_it, *it, ppos_data);
8338 }
8339
8340 #define BUFFER_POS_REACHED_P() \
8341 ((op & MOVE_TO_POS) != 0 \
8342 && BUFFERP (it->object) \
8343 && (IT_CHARPOS (*it) == to_charpos \
8344 || ((!it->bidi_p \
8345 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8346 && IT_CHARPOS (*it) > to_charpos) \
8347 || (it->what == IT_COMPOSITION \
8348 && ((IT_CHARPOS (*it) > to_charpos \
8349 && to_charpos >= it->cmp_it.charpos) \
8350 || (IT_CHARPOS (*it) < to_charpos \
8351 && to_charpos <= it->cmp_it.charpos)))) \
8352 && (it->method == GET_FROM_BUFFER \
8353 || (it->method == GET_FROM_DISPLAY_VECTOR \
8354 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8355
8356 /* If there's a line-/wrap-prefix, handle it. */
8357 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8358 && it->current_y < it->last_visible_y)
8359 handle_line_prefix (it);
8360
8361 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8362 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8363
8364 while (1)
8365 {
8366 int x, i, ascent = 0, descent = 0;
8367
8368 /* Utility macro to reset an iterator with x, ascent, and descent. */
8369 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8370 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8371 (IT)->max_descent = descent)
8372
8373 /* Stop if we move beyond TO_CHARPOS (after an image or a
8374 display string or stretch glyph). */
8375 if ((op & MOVE_TO_POS) != 0
8376 && BUFFERP (it->object)
8377 && it->method == GET_FROM_BUFFER
8378 && (((!it->bidi_p
8379 /* When the iterator is at base embedding level, we
8380 are guaranteed that characters are delivered for
8381 display in strictly increasing order of their
8382 buffer positions. */
8383 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8384 && IT_CHARPOS (*it) > to_charpos)
8385 || (it->bidi_p
8386 && (prev_method == GET_FROM_IMAGE
8387 || prev_method == GET_FROM_STRETCH
8388 || prev_method == GET_FROM_STRING)
8389 /* Passed TO_CHARPOS from left to right. */
8390 && ((prev_pos < to_charpos
8391 && IT_CHARPOS (*it) > to_charpos)
8392 /* Passed TO_CHARPOS from right to left. */
8393 || (prev_pos > to_charpos
8394 && IT_CHARPOS (*it) < to_charpos)))))
8395 {
8396 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8397 {
8398 result = MOVE_POS_MATCH_OR_ZV;
8399 break;
8400 }
8401 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8402 /* If wrap_it is valid, the current position might be in a
8403 word that is wrapped. So, save the iterator in
8404 atpos_it and continue to see if wrapping happens. */
8405 SAVE_IT (atpos_it, *it, atpos_data);
8406 }
8407
8408 /* Stop when ZV reached.
8409 We used to stop here when TO_CHARPOS reached as well, but that is
8410 too soon if this glyph does not fit on this line. So we handle it
8411 explicitly below. */
8412 if (!get_next_display_element (it))
8413 {
8414 result = MOVE_POS_MATCH_OR_ZV;
8415 break;
8416 }
8417
8418 if (it->line_wrap == TRUNCATE)
8419 {
8420 if (BUFFER_POS_REACHED_P ())
8421 {
8422 result = MOVE_POS_MATCH_OR_ZV;
8423 break;
8424 }
8425 }
8426 else
8427 {
8428 if (it->line_wrap == WORD_WRAP)
8429 {
8430 if (IT_DISPLAYING_WHITESPACE (it))
8431 may_wrap = 1;
8432 else if (may_wrap)
8433 {
8434 /* We have reached a glyph that follows one or more
8435 whitespace characters. If the position is
8436 already found, we are done. */
8437 if (atpos_it.sp >= 0)
8438 {
8439 RESTORE_IT (it, &atpos_it, atpos_data);
8440 result = MOVE_POS_MATCH_OR_ZV;
8441 goto done;
8442 }
8443 if (atx_it.sp >= 0)
8444 {
8445 RESTORE_IT (it, &atx_it, atx_data);
8446 result = MOVE_X_REACHED;
8447 goto done;
8448 }
8449 /* Otherwise, we can wrap here. */
8450 SAVE_IT (wrap_it, *it, wrap_data);
8451 may_wrap = 0;
8452 }
8453 }
8454 }
8455
8456 /* Remember the line height for the current line, in case
8457 the next element doesn't fit on the line. */
8458 ascent = it->max_ascent;
8459 descent = it->max_descent;
8460
8461 /* The call to produce_glyphs will get the metrics of the
8462 display element IT is loaded with. Record the x-position
8463 before this display element, in case it doesn't fit on the
8464 line. */
8465 x = it->current_x;
8466
8467 PRODUCE_GLYPHS (it);
8468
8469 if (it->area != TEXT_AREA)
8470 {
8471 prev_method = it->method;
8472 if (it->method == GET_FROM_BUFFER)
8473 prev_pos = IT_CHARPOS (*it);
8474 set_iterator_to_next (it, 1);
8475 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8476 SET_TEXT_POS (this_line_min_pos,
8477 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8478 if (it->bidi_p
8479 && (op & MOVE_TO_POS)
8480 && IT_CHARPOS (*it) > to_charpos
8481 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8482 SAVE_IT (ppos_it, *it, ppos_data);
8483 continue;
8484 }
8485
8486 /* The number of glyphs we get back in IT->nglyphs will normally
8487 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8488 character on a terminal frame, or (iii) a line end. For the
8489 second case, IT->nglyphs - 1 padding glyphs will be present.
8490 (On X frames, there is only one glyph produced for a
8491 composite character.)
8492
8493 The behavior implemented below means, for continuation lines,
8494 that as many spaces of a TAB as fit on the current line are
8495 displayed there. For terminal frames, as many glyphs of a
8496 multi-glyph character are displayed in the current line, too.
8497 This is what the old redisplay code did, and we keep it that
8498 way. Under X, the whole shape of a complex character must
8499 fit on the line or it will be completely displayed in the
8500 next line.
8501
8502 Note that both for tabs and padding glyphs, all glyphs have
8503 the same width. */
8504 if (it->nglyphs)
8505 {
8506 /* More than one glyph or glyph doesn't fit on line. All
8507 glyphs have the same width. */
8508 int single_glyph_width = it->pixel_width / it->nglyphs;
8509 int new_x;
8510 int x_before_this_char = x;
8511 int hpos_before_this_char = it->hpos;
8512
8513 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8514 {
8515 new_x = x + single_glyph_width;
8516
8517 /* We want to leave anything reaching TO_X to the caller. */
8518 if ((op & MOVE_TO_X) && new_x > to_x)
8519 {
8520 if (BUFFER_POS_REACHED_P ())
8521 {
8522 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8523 goto buffer_pos_reached;
8524 if (atpos_it.sp < 0)
8525 {
8526 SAVE_IT (atpos_it, *it, atpos_data);
8527 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8528 }
8529 }
8530 else
8531 {
8532 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8533 {
8534 it->current_x = x;
8535 result = MOVE_X_REACHED;
8536 break;
8537 }
8538 if (atx_it.sp < 0)
8539 {
8540 SAVE_IT (atx_it, *it, atx_data);
8541 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8542 }
8543 }
8544 }
8545
8546 if (/* Lines are continued. */
8547 it->line_wrap != TRUNCATE
8548 && (/* And glyph doesn't fit on the line. */
8549 new_x > it->last_visible_x
8550 /* Or it fits exactly and we're on a window
8551 system frame. */
8552 || (new_x == it->last_visible_x
8553 && FRAME_WINDOW_P (it->f)
8554 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8555 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8556 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8557 {
8558 if (/* IT->hpos == 0 means the very first glyph
8559 doesn't fit on the line, e.g. a wide image. */
8560 it->hpos == 0
8561 || (new_x == it->last_visible_x
8562 && FRAME_WINDOW_P (it->f)))
8563 {
8564 ++it->hpos;
8565 it->current_x = new_x;
8566
8567 /* The character's last glyph just barely fits
8568 in this row. */
8569 if (i == it->nglyphs - 1)
8570 {
8571 /* If this is the destination position,
8572 return a position *before* it in this row,
8573 now that we know it fits in this row. */
8574 if (BUFFER_POS_REACHED_P ())
8575 {
8576 if (it->line_wrap != WORD_WRAP
8577 || wrap_it.sp < 0)
8578 {
8579 it->hpos = hpos_before_this_char;
8580 it->current_x = x_before_this_char;
8581 result = MOVE_POS_MATCH_OR_ZV;
8582 break;
8583 }
8584 if (it->line_wrap == WORD_WRAP
8585 && atpos_it.sp < 0)
8586 {
8587 SAVE_IT (atpos_it, *it, atpos_data);
8588 atpos_it.current_x = x_before_this_char;
8589 atpos_it.hpos = hpos_before_this_char;
8590 }
8591 }
8592
8593 prev_method = it->method;
8594 if (it->method == GET_FROM_BUFFER)
8595 prev_pos = IT_CHARPOS (*it);
8596 set_iterator_to_next (it, 1);
8597 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8598 SET_TEXT_POS (this_line_min_pos,
8599 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8600 /* On graphical terminals, newlines may
8601 "overflow" into the fringe if
8602 overflow-newline-into-fringe is non-nil.
8603 On text terminals, and on graphical
8604 terminals with no right margin, newlines
8605 may overflow into the last glyph on the
8606 display line.*/
8607 if (!FRAME_WINDOW_P (it->f)
8608 || ((it->bidi_p
8609 && it->bidi_it.paragraph_dir == R2L)
8610 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8611 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8612 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8613 {
8614 if (!get_next_display_element (it))
8615 {
8616 result = MOVE_POS_MATCH_OR_ZV;
8617 break;
8618 }
8619 if (BUFFER_POS_REACHED_P ())
8620 {
8621 if (ITERATOR_AT_END_OF_LINE_P (it))
8622 result = MOVE_POS_MATCH_OR_ZV;
8623 else
8624 result = MOVE_LINE_CONTINUED;
8625 break;
8626 }
8627 if (ITERATOR_AT_END_OF_LINE_P (it)
8628 && (it->line_wrap != WORD_WRAP
8629 || wrap_it.sp < 0))
8630 {
8631 result = MOVE_NEWLINE_OR_CR;
8632 break;
8633 }
8634 }
8635 }
8636 }
8637 else
8638 IT_RESET_X_ASCENT_DESCENT (it);
8639
8640 if (wrap_it.sp >= 0)
8641 {
8642 RESTORE_IT (it, &wrap_it, wrap_data);
8643 atpos_it.sp = -1;
8644 atx_it.sp = -1;
8645 }
8646
8647 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8648 IT_CHARPOS (*it)));
8649 result = MOVE_LINE_CONTINUED;
8650 break;
8651 }
8652
8653 if (BUFFER_POS_REACHED_P ())
8654 {
8655 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8656 goto buffer_pos_reached;
8657 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8658 {
8659 SAVE_IT (atpos_it, *it, atpos_data);
8660 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8661 }
8662 }
8663
8664 if (new_x > it->first_visible_x)
8665 {
8666 /* Glyph is visible. Increment number of glyphs that
8667 would be displayed. */
8668 ++it->hpos;
8669 }
8670 }
8671
8672 if (result != MOVE_UNDEFINED)
8673 break;
8674 }
8675 else if (BUFFER_POS_REACHED_P ())
8676 {
8677 buffer_pos_reached:
8678 IT_RESET_X_ASCENT_DESCENT (it);
8679 result = MOVE_POS_MATCH_OR_ZV;
8680 break;
8681 }
8682 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8683 {
8684 /* Stop when TO_X specified and reached. This check is
8685 necessary here because of lines consisting of a line end,
8686 only. The line end will not produce any glyphs and we
8687 would never get MOVE_X_REACHED. */
8688 eassert (it->nglyphs == 0);
8689 result = MOVE_X_REACHED;
8690 break;
8691 }
8692
8693 /* Is this a line end? If yes, we're done. */
8694 if (ITERATOR_AT_END_OF_LINE_P (it))
8695 {
8696 /* If we are past TO_CHARPOS, but never saw any character
8697 positions smaller than TO_CHARPOS, return
8698 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8699 did. */
8700 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8701 {
8702 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8703 {
8704 if (IT_CHARPOS (ppos_it) < ZV)
8705 {
8706 RESTORE_IT (it, &ppos_it, ppos_data);
8707 result = MOVE_POS_MATCH_OR_ZV;
8708 }
8709 else
8710 goto buffer_pos_reached;
8711 }
8712 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8713 && IT_CHARPOS (*it) > to_charpos)
8714 goto buffer_pos_reached;
8715 else
8716 result = MOVE_NEWLINE_OR_CR;
8717 }
8718 else
8719 result = MOVE_NEWLINE_OR_CR;
8720 break;
8721 }
8722
8723 prev_method = it->method;
8724 if (it->method == GET_FROM_BUFFER)
8725 prev_pos = IT_CHARPOS (*it);
8726 /* The current display element has been consumed. Advance
8727 to the next. */
8728 set_iterator_to_next (it, 1);
8729 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8730 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8731 if (IT_CHARPOS (*it) < to_charpos)
8732 saw_smaller_pos = 1;
8733 if (it->bidi_p
8734 && (op & MOVE_TO_POS)
8735 && IT_CHARPOS (*it) >= to_charpos
8736 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8737 SAVE_IT (ppos_it, *it, ppos_data);
8738
8739 /* Stop if lines are truncated and IT's current x-position is
8740 past the right edge of the window now. */
8741 if (it->line_wrap == TRUNCATE
8742 && it->current_x >= it->last_visible_x)
8743 {
8744 if (!FRAME_WINDOW_P (it->f)
8745 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8746 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8747 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8748 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8749 {
8750 int at_eob_p = 0;
8751
8752 if ((at_eob_p = !get_next_display_element (it))
8753 || BUFFER_POS_REACHED_P ()
8754 /* If we are past TO_CHARPOS, but never saw any
8755 character positions smaller than TO_CHARPOS,
8756 return MOVE_POS_MATCH_OR_ZV, like the
8757 unidirectional display did. */
8758 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8759 && !saw_smaller_pos
8760 && IT_CHARPOS (*it) > to_charpos))
8761 {
8762 if (it->bidi_p
8763 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8764 RESTORE_IT (it, &ppos_it, ppos_data);
8765 result = MOVE_POS_MATCH_OR_ZV;
8766 break;
8767 }
8768 if (ITERATOR_AT_END_OF_LINE_P (it))
8769 {
8770 result = MOVE_NEWLINE_OR_CR;
8771 break;
8772 }
8773 }
8774 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8775 && !saw_smaller_pos
8776 && IT_CHARPOS (*it) > to_charpos)
8777 {
8778 if (IT_CHARPOS (ppos_it) < ZV)
8779 RESTORE_IT (it, &ppos_it, ppos_data);
8780 result = MOVE_POS_MATCH_OR_ZV;
8781 break;
8782 }
8783 result = MOVE_LINE_TRUNCATED;
8784 break;
8785 }
8786 #undef IT_RESET_X_ASCENT_DESCENT
8787 }
8788
8789 #undef BUFFER_POS_REACHED_P
8790
8791 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8792 restore the saved iterator. */
8793 if (atpos_it.sp >= 0)
8794 RESTORE_IT (it, &atpos_it, atpos_data);
8795 else if (atx_it.sp >= 0)
8796 RESTORE_IT (it, &atx_it, atx_data);
8797
8798 done:
8799
8800 if (atpos_data)
8801 bidi_unshelve_cache (atpos_data, 1);
8802 if (atx_data)
8803 bidi_unshelve_cache (atx_data, 1);
8804 if (wrap_data)
8805 bidi_unshelve_cache (wrap_data, 1);
8806 if (ppos_data)
8807 bidi_unshelve_cache (ppos_data, 1);
8808
8809 /* Restore the iterator settings altered at the beginning of this
8810 function. */
8811 it->glyph_row = saved_glyph_row;
8812 return result;
8813 }
8814
8815 /* For external use. */
8816 void
8817 move_it_in_display_line (struct it *it,
8818 ptrdiff_t to_charpos, int to_x,
8819 enum move_operation_enum op)
8820 {
8821 if (it->line_wrap == WORD_WRAP
8822 && (op & MOVE_TO_X))
8823 {
8824 struct it save_it;
8825 void *save_data = NULL;
8826 int skip;
8827
8828 SAVE_IT (save_it, *it, save_data);
8829 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8830 /* When word-wrap is on, TO_X may lie past the end
8831 of a wrapped line. Then it->current is the
8832 character on the next line, so backtrack to the
8833 space before the wrap point. */
8834 if (skip == MOVE_LINE_CONTINUED)
8835 {
8836 int prev_x = max (it->current_x - 1, 0);
8837 RESTORE_IT (it, &save_it, save_data);
8838 move_it_in_display_line_to
8839 (it, -1, prev_x, MOVE_TO_X);
8840 }
8841 else
8842 bidi_unshelve_cache (save_data, 1);
8843 }
8844 else
8845 move_it_in_display_line_to (it, to_charpos, to_x, op);
8846 }
8847
8848
8849 /* Move IT forward until it satisfies one or more of the criteria in
8850 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8851
8852 OP is a bit-mask that specifies where to stop, and in particular,
8853 which of those four position arguments makes a difference. See the
8854 description of enum move_operation_enum.
8855
8856 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8857 screen line, this function will set IT to the next position that is
8858 displayed to the right of TO_CHARPOS on the screen. */
8859
8860 void
8861 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8862 {
8863 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8864 int line_height, line_start_x = 0, reached = 0;
8865 void *backup_data = NULL;
8866
8867 for (;;)
8868 {
8869 if (op & MOVE_TO_VPOS)
8870 {
8871 /* If no TO_CHARPOS and no TO_X specified, stop at the
8872 start of the line TO_VPOS. */
8873 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8874 {
8875 if (it->vpos == to_vpos)
8876 {
8877 reached = 1;
8878 break;
8879 }
8880 else
8881 skip = move_it_in_display_line_to (it, -1, -1, 0);
8882 }
8883 else
8884 {
8885 /* TO_VPOS >= 0 means stop at TO_X in the line at
8886 TO_VPOS, or at TO_POS, whichever comes first. */
8887 if (it->vpos == to_vpos)
8888 {
8889 reached = 2;
8890 break;
8891 }
8892
8893 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8894
8895 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8896 {
8897 reached = 3;
8898 break;
8899 }
8900 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8901 {
8902 /* We have reached TO_X but not in the line we want. */
8903 skip = move_it_in_display_line_to (it, to_charpos,
8904 -1, MOVE_TO_POS);
8905 if (skip == MOVE_POS_MATCH_OR_ZV)
8906 {
8907 reached = 4;
8908 break;
8909 }
8910 }
8911 }
8912 }
8913 else if (op & MOVE_TO_Y)
8914 {
8915 struct it it_backup;
8916
8917 if (it->line_wrap == WORD_WRAP)
8918 SAVE_IT (it_backup, *it, backup_data);
8919
8920 /* TO_Y specified means stop at TO_X in the line containing
8921 TO_Y---or at TO_CHARPOS if this is reached first. The
8922 problem is that we can't really tell whether the line
8923 contains TO_Y before we have completely scanned it, and
8924 this may skip past TO_X. What we do is to first scan to
8925 TO_X.
8926
8927 If TO_X is not specified, use a TO_X of zero. The reason
8928 is to make the outcome of this function more predictable.
8929 If we didn't use TO_X == 0, we would stop at the end of
8930 the line which is probably not what a caller would expect
8931 to happen. */
8932 skip = move_it_in_display_line_to
8933 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8934 (MOVE_TO_X | (op & MOVE_TO_POS)));
8935
8936 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8937 if (skip == MOVE_POS_MATCH_OR_ZV)
8938 reached = 5;
8939 else if (skip == MOVE_X_REACHED)
8940 {
8941 /* If TO_X was reached, we want to know whether TO_Y is
8942 in the line. We know this is the case if the already
8943 scanned glyphs make the line tall enough. Otherwise,
8944 we must check by scanning the rest of the line. */
8945 line_height = it->max_ascent + it->max_descent;
8946 if (to_y >= it->current_y
8947 && to_y < it->current_y + line_height)
8948 {
8949 reached = 6;
8950 break;
8951 }
8952 SAVE_IT (it_backup, *it, backup_data);
8953 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8954 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8955 op & MOVE_TO_POS);
8956 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8957 line_height = it->max_ascent + it->max_descent;
8958 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8959
8960 if (to_y >= it->current_y
8961 && to_y < it->current_y + line_height)
8962 {
8963 /* If TO_Y is in this line and TO_X was reached
8964 above, we scanned too far. We have to restore
8965 IT's settings to the ones before skipping. But
8966 keep the more accurate values of max_ascent and
8967 max_descent we've found while skipping the rest
8968 of the line, for the sake of callers, such as
8969 pos_visible_p, that need to know the line
8970 height. */
8971 int max_ascent = it->max_ascent;
8972 int max_descent = it->max_descent;
8973
8974 RESTORE_IT (it, &it_backup, backup_data);
8975 it->max_ascent = max_ascent;
8976 it->max_descent = max_descent;
8977 reached = 6;
8978 }
8979 else
8980 {
8981 skip = skip2;
8982 if (skip == MOVE_POS_MATCH_OR_ZV)
8983 reached = 7;
8984 }
8985 }
8986 else
8987 {
8988 /* Check whether TO_Y is in this line. */
8989 line_height = it->max_ascent + it->max_descent;
8990 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8991
8992 if (to_y >= it->current_y
8993 && to_y < it->current_y + line_height)
8994 {
8995 /* When word-wrap is on, TO_X may lie past the end
8996 of a wrapped line. Then it->current is the
8997 character on the next line, so backtrack to the
8998 space before the wrap point. */
8999 if (skip == MOVE_LINE_CONTINUED
9000 && it->line_wrap == WORD_WRAP)
9001 {
9002 int prev_x = max (it->current_x - 1, 0);
9003 RESTORE_IT (it, &it_backup, backup_data);
9004 skip = move_it_in_display_line_to
9005 (it, -1, prev_x, MOVE_TO_X);
9006 }
9007 reached = 6;
9008 }
9009 }
9010
9011 if (reached)
9012 break;
9013 }
9014 else if (BUFFERP (it->object)
9015 && (it->method == GET_FROM_BUFFER
9016 || it->method == GET_FROM_STRETCH)
9017 && IT_CHARPOS (*it) >= to_charpos
9018 /* Under bidi iteration, a call to set_iterator_to_next
9019 can scan far beyond to_charpos if the initial
9020 portion of the next line needs to be reordered. In
9021 that case, give move_it_in_display_line_to another
9022 chance below. */
9023 && !(it->bidi_p
9024 && it->bidi_it.scan_dir == -1))
9025 skip = MOVE_POS_MATCH_OR_ZV;
9026 else
9027 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9028
9029 switch (skip)
9030 {
9031 case MOVE_POS_MATCH_OR_ZV:
9032 reached = 8;
9033 goto out;
9034
9035 case MOVE_NEWLINE_OR_CR:
9036 set_iterator_to_next (it, 1);
9037 it->continuation_lines_width = 0;
9038 break;
9039
9040 case MOVE_LINE_TRUNCATED:
9041 it->continuation_lines_width = 0;
9042 reseat_at_next_visible_line_start (it, 0);
9043 if ((op & MOVE_TO_POS) != 0
9044 && IT_CHARPOS (*it) > to_charpos)
9045 {
9046 reached = 9;
9047 goto out;
9048 }
9049 break;
9050
9051 case MOVE_LINE_CONTINUED:
9052 /* For continued lines ending in a tab, some of the glyphs
9053 associated with the tab are displayed on the current
9054 line. Since it->current_x does not include these glyphs,
9055 we use it->last_visible_x instead. */
9056 if (it->c == '\t')
9057 {
9058 it->continuation_lines_width += it->last_visible_x;
9059 /* When moving by vpos, ensure that the iterator really
9060 advances to the next line (bug#847, bug#969). Fixme:
9061 do we need to do this in other circumstances? */
9062 if (it->current_x != it->last_visible_x
9063 && (op & MOVE_TO_VPOS)
9064 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9065 {
9066 line_start_x = it->current_x + it->pixel_width
9067 - it->last_visible_x;
9068 set_iterator_to_next (it, 0);
9069 }
9070 }
9071 else
9072 it->continuation_lines_width += it->current_x;
9073 break;
9074
9075 default:
9076 emacs_abort ();
9077 }
9078
9079 /* Reset/increment for the next run. */
9080 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9081 it->current_x = line_start_x;
9082 line_start_x = 0;
9083 it->hpos = 0;
9084 it->current_y += it->max_ascent + it->max_descent;
9085 ++it->vpos;
9086 last_height = it->max_ascent + it->max_descent;
9087 it->max_ascent = it->max_descent = 0;
9088 }
9089
9090 out:
9091
9092 /* On text terminals, we may stop at the end of a line in the middle
9093 of a multi-character glyph. If the glyph itself is continued,
9094 i.e. it is actually displayed on the next line, don't treat this
9095 stopping point as valid; move to the next line instead (unless
9096 that brings us offscreen). */
9097 if (!FRAME_WINDOW_P (it->f)
9098 && op & MOVE_TO_POS
9099 && IT_CHARPOS (*it) == to_charpos
9100 && it->what == IT_CHARACTER
9101 && it->nglyphs > 1
9102 && it->line_wrap == WINDOW_WRAP
9103 && it->current_x == it->last_visible_x - 1
9104 && it->c != '\n'
9105 && it->c != '\t'
9106 && it->vpos < it->w->window_end_vpos)
9107 {
9108 it->continuation_lines_width += it->current_x;
9109 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9110 it->current_y += it->max_ascent + it->max_descent;
9111 ++it->vpos;
9112 last_height = it->max_ascent + it->max_descent;
9113 }
9114
9115 if (backup_data)
9116 bidi_unshelve_cache (backup_data, 1);
9117
9118 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9119 }
9120
9121
9122 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9123
9124 If DY > 0, move IT backward at least that many pixels. DY = 0
9125 means move IT backward to the preceding line start or BEGV. This
9126 function may move over more than DY pixels if IT->current_y - DY
9127 ends up in the middle of a line; in this case IT->current_y will be
9128 set to the top of the line moved to. */
9129
9130 void
9131 move_it_vertically_backward (struct it *it, int dy)
9132 {
9133 int nlines, h;
9134 struct it it2, it3;
9135 void *it2data = NULL, *it3data = NULL;
9136 ptrdiff_t start_pos;
9137 int nchars_per_row
9138 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9139 ptrdiff_t pos_limit;
9140
9141 move_further_back:
9142 eassert (dy >= 0);
9143
9144 start_pos = IT_CHARPOS (*it);
9145
9146 /* Estimate how many newlines we must move back. */
9147 nlines = max (1, dy / default_line_pixel_height (it->w));
9148 if (it->line_wrap == TRUNCATE)
9149 pos_limit = BEGV;
9150 else
9151 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9152
9153 /* Set the iterator's position that many lines back. But don't go
9154 back more than NLINES full screen lines -- this wins a day with
9155 buffers which have very long lines. */
9156 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9157 back_to_previous_visible_line_start (it);
9158
9159 /* Reseat the iterator here. When moving backward, we don't want
9160 reseat to skip forward over invisible text, set up the iterator
9161 to deliver from overlay strings at the new position etc. So,
9162 use reseat_1 here. */
9163 reseat_1 (it, it->current.pos, 1);
9164
9165 /* We are now surely at a line start. */
9166 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9167 reordering is in effect. */
9168 it->continuation_lines_width = 0;
9169
9170 /* Move forward and see what y-distance we moved. First move to the
9171 start of the next line so that we get its height. We need this
9172 height to be able to tell whether we reached the specified
9173 y-distance. */
9174 SAVE_IT (it2, *it, it2data);
9175 it2.max_ascent = it2.max_descent = 0;
9176 do
9177 {
9178 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9179 MOVE_TO_POS | MOVE_TO_VPOS);
9180 }
9181 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9182 /* If we are in a display string which starts at START_POS,
9183 and that display string includes a newline, and we are
9184 right after that newline (i.e. at the beginning of a
9185 display line), exit the loop, because otherwise we will
9186 infloop, since move_it_to will see that it is already at
9187 START_POS and will not move. */
9188 || (it2.method == GET_FROM_STRING
9189 && IT_CHARPOS (it2) == start_pos
9190 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9191 eassert (IT_CHARPOS (*it) >= BEGV);
9192 SAVE_IT (it3, it2, it3data);
9193
9194 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9195 eassert (IT_CHARPOS (*it) >= BEGV);
9196 /* H is the actual vertical distance from the position in *IT
9197 and the starting position. */
9198 h = it2.current_y - it->current_y;
9199 /* NLINES is the distance in number of lines. */
9200 nlines = it2.vpos - it->vpos;
9201
9202 /* Correct IT's y and vpos position
9203 so that they are relative to the starting point. */
9204 it->vpos -= nlines;
9205 it->current_y -= h;
9206
9207 if (dy == 0)
9208 {
9209 /* DY == 0 means move to the start of the screen line. The
9210 value of nlines is > 0 if continuation lines were involved,
9211 or if the original IT position was at start of a line. */
9212 RESTORE_IT (it, it, it2data);
9213 if (nlines > 0)
9214 move_it_by_lines (it, nlines);
9215 /* The above code moves us to some position NLINES down,
9216 usually to its first glyph (leftmost in an L2R line), but
9217 that's not necessarily the start of the line, under bidi
9218 reordering. We want to get to the character position
9219 that is immediately after the newline of the previous
9220 line. */
9221 if (it->bidi_p
9222 && !it->continuation_lines_width
9223 && !STRINGP (it->string)
9224 && IT_CHARPOS (*it) > BEGV
9225 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9226 {
9227 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9228
9229 DEC_BOTH (cp, bp);
9230 cp = find_newline_no_quit (cp, bp, -1, NULL);
9231 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9232 }
9233 bidi_unshelve_cache (it3data, 1);
9234 }
9235 else
9236 {
9237 /* The y-position we try to reach, relative to *IT.
9238 Note that H has been subtracted in front of the if-statement. */
9239 int target_y = it->current_y + h - dy;
9240 int y0 = it3.current_y;
9241 int y1;
9242 int line_height;
9243
9244 RESTORE_IT (&it3, &it3, it3data);
9245 y1 = line_bottom_y (&it3);
9246 line_height = y1 - y0;
9247 RESTORE_IT (it, it, it2data);
9248 /* If we did not reach target_y, try to move further backward if
9249 we can. If we moved too far backward, try to move forward. */
9250 if (target_y < it->current_y
9251 /* This is heuristic. In a window that's 3 lines high, with
9252 a line height of 13 pixels each, recentering with point
9253 on the bottom line will try to move -39/2 = 19 pixels
9254 backward. Try to avoid moving into the first line. */
9255 && (it->current_y - target_y
9256 > min (window_box_height (it->w), line_height * 2 / 3))
9257 && IT_CHARPOS (*it) > BEGV)
9258 {
9259 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9260 target_y - it->current_y));
9261 dy = it->current_y - target_y;
9262 goto move_further_back;
9263 }
9264 else if (target_y >= it->current_y + line_height
9265 && IT_CHARPOS (*it) < ZV)
9266 {
9267 /* Should move forward by at least one line, maybe more.
9268
9269 Note: Calling move_it_by_lines can be expensive on
9270 terminal frames, where compute_motion is used (via
9271 vmotion) to do the job, when there are very long lines
9272 and truncate-lines is nil. That's the reason for
9273 treating terminal frames specially here. */
9274
9275 if (!FRAME_WINDOW_P (it->f))
9276 move_it_vertically (it, target_y - (it->current_y + line_height));
9277 else
9278 {
9279 do
9280 {
9281 move_it_by_lines (it, 1);
9282 }
9283 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9284 }
9285 }
9286 }
9287 }
9288
9289
9290 /* Move IT by a specified amount of pixel lines DY. DY negative means
9291 move backwards. DY = 0 means move to start of screen line. At the
9292 end, IT will be on the start of a screen line. */
9293
9294 void
9295 move_it_vertically (struct it *it, int dy)
9296 {
9297 if (dy <= 0)
9298 move_it_vertically_backward (it, -dy);
9299 else
9300 {
9301 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9302 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9303 MOVE_TO_POS | MOVE_TO_Y);
9304 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9305
9306 /* If buffer ends in ZV without a newline, move to the start of
9307 the line to satisfy the post-condition. */
9308 if (IT_CHARPOS (*it) == ZV
9309 && ZV > BEGV
9310 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9311 move_it_by_lines (it, 0);
9312 }
9313 }
9314
9315
9316 /* Move iterator IT past the end of the text line it is in. */
9317
9318 void
9319 move_it_past_eol (struct it *it)
9320 {
9321 enum move_it_result rc;
9322
9323 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9324 if (rc == MOVE_NEWLINE_OR_CR)
9325 set_iterator_to_next (it, 0);
9326 }
9327
9328
9329 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9330 negative means move up. DVPOS == 0 means move to the start of the
9331 screen line.
9332
9333 Optimization idea: If we would know that IT->f doesn't use
9334 a face with proportional font, we could be faster for
9335 truncate-lines nil. */
9336
9337 void
9338 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9339 {
9340
9341 /* The commented-out optimization uses vmotion on terminals. This
9342 gives bad results, because elements like it->what, on which
9343 callers such as pos_visible_p rely, aren't updated. */
9344 /* struct position pos;
9345 if (!FRAME_WINDOW_P (it->f))
9346 {
9347 struct text_pos textpos;
9348
9349 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9350 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9351 reseat (it, textpos, 1);
9352 it->vpos += pos.vpos;
9353 it->current_y += pos.vpos;
9354 }
9355 else */
9356
9357 if (dvpos == 0)
9358 {
9359 /* DVPOS == 0 means move to the start of the screen line. */
9360 move_it_vertically_backward (it, 0);
9361 /* Let next call to line_bottom_y calculate real line height */
9362 last_height = 0;
9363 }
9364 else if (dvpos > 0)
9365 {
9366 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9367 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9368 {
9369 /* Only move to the next buffer position if we ended up in a
9370 string from display property, not in an overlay string
9371 (before-string or after-string). That is because the
9372 latter don't conceal the underlying buffer position, so
9373 we can ask to move the iterator to the exact position we
9374 are interested in. Note that, even if we are already at
9375 IT_CHARPOS (*it), the call below is not a no-op, as it
9376 will detect that we are at the end of the string, pop the
9377 iterator, and compute it->current_x and it->hpos
9378 correctly. */
9379 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9380 -1, -1, -1, MOVE_TO_POS);
9381 }
9382 }
9383 else
9384 {
9385 struct it it2;
9386 void *it2data = NULL;
9387 ptrdiff_t start_charpos, i;
9388 int nchars_per_row
9389 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9390 ptrdiff_t pos_limit;
9391
9392 /* Start at the beginning of the screen line containing IT's
9393 position. This may actually move vertically backwards,
9394 in case of overlays, so adjust dvpos accordingly. */
9395 dvpos += it->vpos;
9396 move_it_vertically_backward (it, 0);
9397 dvpos -= it->vpos;
9398
9399 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9400 screen lines, and reseat the iterator there. */
9401 start_charpos = IT_CHARPOS (*it);
9402 if (it->line_wrap == TRUNCATE)
9403 pos_limit = BEGV;
9404 else
9405 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9406 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9407 back_to_previous_visible_line_start (it);
9408 reseat (it, it->current.pos, 1);
9409
9410 /* Move further back if we end up in a string or an image. */
9411 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9412 {
9413 /* First try to move to start of display line. */
9414 dvpos += it->vpos;
9415 move_it_vertically_backward (it, 0);
9416 dvpos -= it->vpos;
9417 if (IT_POS_VALID_AFTER_MOVE_P (it))
9418 break;
9419 /* If start of line is still in string or image,
9420 move further back. */
9421 back_to_previous_visible_line_start (it);
9422 reseat (it, it->current.pos, 1);
9423 dvpos--;
9424 }
9425
9426 it->current_x = it->hpos = 0;
9427
9428 /* Above call may have moved too far if continuation lines
9429 are involved. Scan forward and see if it did. */
9430 SAVE_IT (it2, *it, it2data);
9431 it2.vpos = it2.current_y = 0;
9432 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9433 it->vpos -= it2.vpos;
9434 it->current_y -= it2.current_y;
9435 it->current_x = it->hpos = 0;
9436
9437 /* If we moved too far back, move IT some lines forward. */
9438 if (it2.vpos > -dvpos)
9439 {
9440 int delta = it2.vpos + dvpos;
9441
9442 RESTORE_IT (&it2, &it2, it2data);
9443 SAVE_IT (it2, *it, it2data);
9444 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9445 /* Move back again if we got too far ahead. */
9446 if (IT_CHARPOS (*it) >= start_charpos)
9447 RESTORE_IT (it, &it2, it2data);
9448 else
9449 bidi_unshelve_cache (it2data, 1);
9450 }
9451 else
9452 RESTORE_IT (it, it, it2data);
9453 }
9454 }
9455
9456 /* Return 1 if IT points into the middle of a display vector. */
9457
9458 int
9459 in_display_vector_p (struct it *it)
9460 {
9461 return (it->method == GET_FROM_DISPLAY_VECTOR
9462 && it->current.dpvec_index > 0
9463 && it->dpvec + it->current.dpvec_index != it->dpend);
9464 }
9465
9466 \f
9467 /***********************************************************************
9468 Messages
9469 ***********************************************************************/
9470
9471
9472 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9473 to *Messages*. */
9474
9475 void
9476 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9477 {
9478 Lisp_Object args[3];
9479 Lisp_Object msg, fmt;
9480 char *buffer;
9481 ptrdiff_t len;
9482 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9483 USE_SAFE_ALLOCA;
9484
9485 fmt = msg = Qnil;
9486 GCPRO4 (fmt, msg, arg1, arg2);
9487
9488 args[0] = fmt = build_string (format);
9489 args[1] = arg1;
9490 args[2] = arg2;
9491 msg = Fformat (3, args);
9492
9493 len = SBYTES (msg) + 1;
9494 buffer = SAFE_ALLOCA (len);
9495 memcpy (buffer, SDATA (msg), len);
9496
9497 message_dolog (buffer, len - 1, 1, 0);
9498 SAFE_FREE ();
9499
9500 UNGCPRO;
9501 }
9502
9503
9504 /* Output a newline in the *Messages* buffer if "needs" one. */
9505
9506 void
9507 message_log_maybe_newline (void)
9508 {
9509 if (message_log_need_newline)
9510 message_dolog ("", 0, 1, 0);
9511 }
9512
9513
9514 /* Add a string M of length NBYTES to the message log, optionally
9515 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9516 true, means interpret the contents of M as multibyte. This
9517 function calls low-level routines in order to bypass text property
9518 hooks, etc. which might not be safe to run.
9519
9520 This may GC (insert may run before/after change hooks),
9521 so the buffer M must NOT point to a Lisp string. */
9522
9523 void
9524 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9525 {
9526 const unsigned char *msg = (const unsigned char *) m;
9527
9528 if (!NILP (Vmemory_full))
9529 return;
9530
9531 if (!NILP (Vmessage_log_max))
9532 {
9533 struct buffer *oldbuf;
9534 Lisp_Object oldpoint, oldbegv, oldzv;
9535 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9536 ptrdiff_t point_at_end = 0;
9537 ptrdiff_t zv_at_end = 0;
9538 Lisp_Object old_deactivate_mark;
9539 bool shown;
9540 struct gcpro gcpro1;
9541
9542 old_deactivate_mark = Vdeactivate_mark;
9543 oldbuf = current_buffer;
9544 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9545 bset_undo_list (current_buffer, Qt);
9546
9547 oldpoint = message_dolog_marker1;
9548 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9549 oldbegv = message_dolog_marker2;
9550 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9551 oldzv = message_dolog_marker3;
9552 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9553 GCPRO1 (old_deactivate_mark);
9554
9555 if (PT == Z)
9556 point_at_end = 1;
9557 if (ZV == Z)
9558 zv_at_end = 1;
9559
9560 BEGV = BEG;
9561 BEGV_BYTE = BEG_BYTE;
9562 ZV = Z;
9563 ZV_BYTE = Z_BYTE;
9564 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9565
9566 /* Insert the string--maybe converting multibyte to single byte
9567 or vice versa, so that all the text fits the buffer. */
9568 if (multibyte
9569 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9570 {
9571 ptrdiff_t i;
9572 int c, char_bytes;
9573 char work[1];
9574
9575 /* Convert a multibyte string to single-byte
9576 for the *Message* buffer. */
9577 for (i = 0; i < nbytes; i += char_bytes)
9578 {
9579 c = string_char_and_length (msg + i, &char_bytes);
9580 work[0] = (ASCII_CHAR_P (c)
9581 ? c
9582 : multibyte_char_to_unibyte (c));
9583 insert_1_both (work, 1, 1, 1, 0, 0);
9584 }
9585 }
9586 else if (! multibyte
9587 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9588 {
9589 ptrdiff_t i;
9590 int c, char_bytes;
9591 unsigned char str[MAX_MULTIBYTE_LENGTH];
9592 /* Convert a single-byte string to multibyte
9593 for the *Message* buffer. */
9594 for (i = 0; i < nbytes; i++)
9595 {
9596 c = msg[i];
9597 MAKE_CHAR_MULTIBYTE (c);
9598 char_bytes = CHAR_STRING (c, str);
9599 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9600 }
9601 }
9602 else if (nbytes)
9603 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9604
9605 if (nlflag)
9606 {
9607 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9608 printmax_t dups;
9609
9610 insert_1_both ("\n", 1, 1, 1, 0, 0);
9611
9612 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9613 this_bol = PT;
9614 this_bol_byte = PT_BYTE;
9615
9616 /* See if this line duplicates the previous one.
9617 If so, combine duplicates. */
9618 if (this_bol > BEG)
9619 {
9620 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9621 prev_bol = PT;
9622 prev_bol_byte = PT_BYTE;
9623
9624 dups = message_log_check_duplicate (prev_bol_byte,
9625 this_bol_byte);
9626 if (dups)
9627 {
9628 del_range_both (prev_bol, prev_bol_byte,
9629 this_bol, this_bol_byte, 0);
9630 if (dups > 1)
9631 {
9632 char dupstr[sizeof " [ times]"
9633 + INT_STRLEN_BOUND (printmax_t)];
9634
9635 /* If you change this format, don't forget to also
9636 change message_log_check_duplicate. */
9637 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9638 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9639 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9640 }
9641 }
9642 }
9643
9644 /* If we have more than the desired maximum number of lines
9645 in the *Messages* buffer now, delete the oldest ones.
9646 This is safe because we don't have undo in this buffer. */
9647
9648 if (NATNUMP (Vmessage_log_max))
9649 {
9650 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9651 -XFASTINT (Vmessage_log_max) - 1, 0);
9652 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9653 }
9654 }
9655 BEGV = marker_position (oldbegv);
9656 BEGV_BYTE = marker_byte_position (oldbegv);
9657
9658 if (zv_at_end)
9659 {
9660 ZV = Z;
9661 ZV_BYTE = Z_BYTE;
9662 }
9663 else
9664 {
9665 ZV = marker_position (oldzv);
9666 ZV_BYTE = marker_byte_position (oldzv);
9667 }
9668
9669 if (point_at_end)
9670 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9671 else
9672 /* We can't do Fgoto_char (oldpoint) because it will run some
9673 Lisp code. */
9674 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9675 marker_byte_position (oldpoint));
9676
9677 UNGCPRO;
9678 unchain_marker (XMARKER (oldpoint));
9679 unchain_marker (XMARKER (oldbegv));
9680 unchain_marker (XMARKER (oldzv));
9681
9682 shown = buffer_window_count (current_buffer) > 0;
9683 set_buffer_internal (oldbuf);
9684 /* We called insert_1_both above with its 5th argument (PREPARE)
9685 zero, which prevents insert_1_both from calling
9686 prepare_to_modify_buffer, which in turns prevents us from
9687 incrementing windows_or_buffers_changed even if *Messages* is
9688 shown in some window. So we must manually incrementing
9689 windows_or_buffers_changed here to make up for that. */
9690 if (shown)
9691 windows_or_buffers_changed++;
9692 else
9693 windows_or_buffers_changed = old_windows_or_buffers_changed;
9694 message_log_need_newline = !nlflag;
9695 Vdeactivate_mark = old_deactivate_mark;
9696 }
9697 }
9698
9699
9700 /* We are at the end of the buffer after just having inserted a newline.
9701 (Note: We depend on the fact we won't be crossing the gap.)
9702 Check to see if the most recent message looks a lot like the previous one.
9703 Return 0 if different, 1 if the new one should just replace it, or a
9704 value N > 1 if we should also append " [N times]". */
9705
9706 static intmax_t
9707 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9708 {
9709 ptrdiff_t i;
9710 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9711 int seen_dots = 0;
9712 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9713 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9714
9715 for (i = 0; i < len; i++)
9716 {
9717 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9718 seen_dots = 1;
9719 if (p1[i] != p2[i])
9720 return seen_dots;
9721 }
9722 p1 += len;
9723 if (*p1 == '\n')
9724 return 2;
9725 if (*p1++ == ' ' && *p1++ == '[')
9726 {
9727 char *pend;
9728 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9729 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9730 return n + 1;
9731 }
9732 return 0;
9733 }
9734 \f
9735
9736 /* Display an echo area message M with a specified length of NBYTES
9737 bytes. The string may include null characters. If M is not a
9738 string, clear out any existing message, and let the mini-buffer
9739 text show through.
9740
9741 This function cancels echoing. */
9742
9743 void
9744 message3 (Lisp_Object m)
9745 {
9746 struct gcpro gcpro1;
9747
9748 GCPRO1 (m);
9749 clear_message (1,1);
9750 cancel_echoing ();
9751
9752 /* First flush out any partial line written with print. */
9753 message_log_maybe_newline ();
9754 if (STRINGP (m))
9755 {
9756 ptrdiff_t nbytes = SBYTES (m);
9757 bool multibyte = STRING_MULTIBYTE (m);
9758 USE_SAFE_ALLOCA;
9759 char *buffer = SAFE_ALLOCA (nbytes);
9760 memcpy (buffer, SDATA (m), nbytes);
9761 message_dolog (buffer, nbytes, 1, multibyte);
9762 SAFE_FREE ();
9763 }
9764 message3_nolog (m);
9765
9766 UNGCPRO;
9767 }
9768
9769
9770 /* The non-logging version of message3.
9771 This does not cancel echoing, because it is used for echoing.
9772 Perhaps we need to make a separate function for echoing
9773 and make this cancel echoing. */
9774
9775 void
9776 message3_nolog (Lisp_Object m)
9777 {
9778 struct frame *sf = SELECTED_FRAME ();
9779
9780 if (FRAME_INITIAL_P (sf))
9781 {
9782 if (noninteractive_need_newline)
9783 putc ('\n', stderr);
9784 noninteractive_need_newline = 0;
9785 if (STRINGP (m))
9786 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9787 if (cursor_in_echo_area == 0)
9788 fprintf (stderr, "\n");
9789 fflush (stderr);
9790 }
9791 /* Error messages get reported properly by cmd_error, so this must be just an
9792 informative message; if the frame hasn't really been initialized yet, just
9793 toss it. */
9794 else if (INTERACTIVE && sf->glyphs_initialized_p)
9795 {
9796 /* Get the frame containing the mini-buffer
9797 that the selected frame is using. */
9798 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9799 Lisp_Object frame = XWINDOW (mini_window)->frame;
9800 struct frame *f = XFRAME (frame);
9801
9802 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9803 Fmake_frame_visible (frame);
9804
9805 if (STRINGP (m) && SCHARS (m) > 0)
9806 {
9807 set_message (m);
9808 if (minibuffer_auto_raise)
9809 Fraise_frame (frame);
9810 /* Assume we are not echoing.
9811 (If we are, echo_now will override this.) */
9812 echo_message_buffer = Qnil;
9813 }
9814 else
9815 clear_message (1, 1);
9816
9817 do_pending_window_change (0);
9818 echo_area_display (1);
9819 do_pending_window_change (0);
9820 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9821 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9822 }
9823 }
9824
9825
9826 /* Display a null-terminated echo area message M. If M is 0, clear
9827 out any existing message, and let the mini-buffer text show through.
9828
9829 The buffer M must continue to exist until after the echo area gets
9830 cleared or some other message gets displayed there. Do not pass
9831 text that is stored in a Lisp string. Do not pass text in a buffer
9832 that was alloca'd. */
9833
9834 void
9835 message1 (const char *m)
9836 {
9837 message3 (m ? build_unibyte_string (m) : Qnil);
9838 }
9839
9840
9841 /* The non-logging counterpart of message1. */
9842
9843 void
9844 message1_nolog (const char *m)
9845 {
9846 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9847 }
9848
9849 /* Display a message M which contains a single %s
9850 which gets replaced with STRING. */
9851
9852 void
9853 message_with_string (const char *m, Lisp_Object string, int log)
9854 {
9855 CHECK_STRING (string);
9856
9857 if (noninteractive)
9858 {
9859 if (m)
9860 {
9861 if (noninteractive_need_newline)
9862 putc ('\n', stderr);
9863 noninteractive_need_newline = 0;
9864 fprintf (stderr, m, SDATA (string));
9865 if (!cursor_in_echo_area)
9866 fprintf (stderr, "\n");
9867 fflush (stderr);
9868 }
9869 }
9870 else if (INTERACTIVE)
9871 {
9872 /* The frame whose minibuffer we're going to display the message on.
9873 It may be larger than the selected frame, so we need
9874 to use its buffer, not the selected frame's buffer. */
9875 Lisp_Object mini_window;
9876 struct frame *f, *sf = SELECTED_FRAME ();
9877
9878 /* Get the frame containing the minibuffer
9879 that the selected frame is using. */
9880 mini_window = FRAME_MINIBUF_WINDOW (sf);
9881 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9882
9883 /* Error messages get reported properly by cmd_error, so this must be
9884 just an informative message; if the frame hasn't really been
9885 initialized yet, just toss it. */
9886 if (f->glyphs_initialized_p)
9887 {
9888 Lisp_Object args[2], msg;
9889 struct gcpro gcpro1, gcpro2;
9890
9891 args[0] = build_string (m);
9892 args[1] = msg = string;
9893 GCPRO2 (args[0], msg);
9894 gcpro1.nvars = 2;
9895
9896 msg = Fformat (2, args);
9897
9898 if (log)
9899 message3 (msg);
9900 else
9901 message3_nolog (msg);
9902
9903 UNGCPRO;
9904
9905 /* Print should start at the beginning of the message
9906 buffer next time. */
9907 message_buf_print = 0;
9908 }
9909 }
9910 }
9911
9912
9913 /* Dump an informative message to the minibuf. If M is 0, clear out
9914 any existing message, and let the mini-buffer text show through. */
9915
9916 static void
9917 vmessage (const char *m, va_list ap)
9918 {
9919 if (noninteractive)
9920 {
9921 if (m)
9922 {
9923 if (noninteractive_need_newline)
9924 putc ('\n', stderr);
9925 noninteractive_need_newline = 0;
9926 vfprintf (stderr, m, ap);
9927 if (cursor_in_echo_area == 0)
9928 fprintf (stderr, "\n");
9929 fflush (stderr);
9930 }
9931 }
9932 else if (INTERACTIVE)
9933 {
9934 /* The frame whose mini-buffer we're going to display the message
9935 on. It may be larger than the selected frame, so we need to
9936 use its buffer, not the selected frame's buffer. */
9937 Lisp_Object mini_window;
9938 struct frame *f, *sf = SELECTED_FRAME ();
9939
9940 /* Get the frame containing the mini-buffer
9941 that the selected frame is using. */
9942 mini_window = FRAME_MINIBUF_WINDOW (sf);
9943 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9944
9945 /* Error messages get reported properly by cmd_error, so this must be
9946 just an informative message; if the frame hasn't really been
9947 initialized yet, just toss it. */
9948 if (f->glyphs_initialized_p)
9949 {
9950 if (m)
9951 {
9952 ptrdiff_t len;
9953 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9954 char *message_buf = alloca (maxsize + 1);
9955
9956 len = doprnt (message_buf, maxsize, m, 0, ap);
9957
9958 message3 (make_string (message_buf, len));
9959 }
9960 else
9961 message1 (0);
9962
9963 /* Print should start at the beginning of the message
9964 buffer next time. */
9965 message_buf_print = 0;
9966 }
9967 }
9968 }
9969
9970 void
9971 message (const char *m, ...)
9972 {
9973 va_list ap;
9974 va_start (ap, m);
9975 vmessage (m, ap);
9976 va_end (ap);
9977 }
9978
9979
9980 #if 0
9981 /* The non-logging version of message. */
9982
9983 void
9984 message_nolog (const char *m, ...)
9985 {
9986 Lisp_Object old_log_max;
9987 va_list ap;
9988 va_start (ap, m);
9989 old_log_max = Vmessage_log_max;
9990 Vmessage_log_max = Qnil;
9991 vmessage (m, ap);
9992 Vmessage_log_max = old_log_max;
9993 va_end (ap);
9994 }
9995 #endif
9996
9997
9998 /* Display the current message in the current mini-buffer. This is
9999 only called from error handlers in process.c, and is not time
10000 critical. */
10001
10002 void
10003 update_echo_area (void)
10004 {
10005 if (!NILP (echo_area_buffer[0]))
10006 {
10007 Lisp_Object string;
10008 string = Fcurrent_message ();
10009 message3 (string);
10010 }
10011 }
10012
10013
10014 /* Make sure echo area buffers in `echo_buffers' are live.
10015 If they aren't, make new ones. */
10016
10017 static void
10018 ensure_echo_area_buffers (void)
10019 {
10020 int i;
10021
10022 for (i = 0; i < 2; ++i)
10023 if (!BUFFERP (echo_buffer[i])
10024 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10025 {
10026 char name[30];
10027 Lisp_Object old_buffer;
10028 int j;
10029
10030 old_buffer = echo_buffer[i];
10031 echo_buffer[i] = Fget_buffer_create
10032 (make_formatted_string (name, " *Echo Area %d*", i));
10033 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10034 /* to force word wrap in echo area -
10035 it was decided to postpone this*/
10036 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10037
10038 for (j = 0; j < 2; ++j)
10039 if (EQ (old_buffer, echo_area_buffer[j]))
10040 echo_area_buffer[j] = echo_buffer[i];
10041 }
10042 }
10043
10044
10045 /* Call FN with args A1..A2 with either the current or last displayed
10046 echo_area_buffer as current buffer.
10047
10048 WHICH zero means use the current message buffer
10049 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10050 from echo_buffer[] and clear it.
10051
10052 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10053 suitable buffer from echo_buffer[] and clear it.
10054
10055 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10056 that the current message becomes the last displayed one, make
10057 choose a suitable buffer for echo_area_buffer[0], and clear it.
10058
10059 Value is what FN returns. */
10060
10061 static int
10062 with_echo_area_buffer (struct window *w, int which,
10063 int (*fn) (ptrdiff_t, Lisp_Object),
10064 ptrdiff_t a1, Lisp_Object a2)
10065 {
10066 Lisp_Object buffer;
10067 int this_one, the_other, clear_buffer_p, rc;
10068 ptrdiff_t count = SPECPDL_INDEX ();
10069
10070 /* If buffers aren't live, make new ones. */
10071 ensure_echo_area_buffers ();
10072
10073 clear_buffer_p = 0;
10074
10075 if (which == 0)
10076 this_one = 0, the_other = 1;
10077 else if (which > 0)
10078 this_one = 1, the_other = 0;
10079 else
10080 {
10081 this_one = 0, the_other = 1;
10082 clear_buffer_p = 1;
10083
10084 /* We need a fresh one in case the current echo buffer equals
10085 the one containing the last displayed echo area message. */
10086 if (!NILP (echo_area_buffer[this_one])
10087 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10088 echo_area_buffer[this_one] = Qnil;
10089 }
10090
10091 /* Choose a suitable buffer from echo_buffer[] is we don't
10092 have one. */
10093 if (NILP (echo_area_buffer[this_one]))
10094 {
10095 echo_area_buffer[this_one]
10096 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10097 ? echo_buffer[the_other]
10098 : echo_buffer[this_one]);
10099 clear_buffer_p = 1;
10100 }
10101
10102 buffer = echo_area_buffer[this_one];
10103
10104 /* Don't get confused by reusing the buffer used for echoing
10105 for a different purpose. */
10106 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10107 cancel_echoing ();
10108
10109 record_unwind_protect (unwind_with_echo_area_buffer,
10110 with_echo_area_buffer_unwind_data (w));
10111
10112 /* Make the echo area buffer current. Note that for display
10113 purposes, it is not necessary that the displayed window's buffer
10114 == current_buffer, except for text property lookup. So, let's
10115 only set that buffer temporarily here without doing a full
10116 Fset_window_buffer. We must also change w->pointm, though,
10117 because otherwise an assertions in unshow_buffer fails, and Emacs
10118 aborts. */
10119 set_buffer_internal_1 (XBUFFER (buffer));
10120 if (w)
10121 {
10122 wset_buffer (w, buffer);
10123 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10124 }
10125
10126 bset_undo_list (current_buffer, Qt);
10127 bset_read_only (current_buffer, Qnil);
10128 specbind (Qinhibit_read_only, Qt);
10129 specbind (Qinhibit_modification_hooks, Qt);
10130
10131 if (clear_buffer_p && Z > BEG)
10132 del_range (BEG, Z);
10133
10134 eassert (BEGV >= BEG);
10135 eassert (ZV <= Z && ZV >= BEGV);
10136
10137 rc = fn (a1, a2);
10138
10139 eassert (BEGV >= BEG);
10140 eassert (ZV <= Z && ZV >= BEGV);
10141
10142 unbind_to (count, Qnil);
10143 return rc;
10144 }
10145
10146
10147 /* Save state that should be preserved around the call to the function
10148 FN called in with_echo_area_buffer. */
10149
10150 static Lisp_Object
10151 with_echo_area_buffer_unwind_data (struct window *w)
10152 {
10153 int i = 0;
10154 Lisp_Object vector, tmp;
10155
10156 /* Reduce consing by keeping one vector in
10157 Vwith_echo_area_save_vector. */
10158 vector = Vwith_echo_area_save_vector;
10159 Vwith_echo_area_save_vector = Qnil;
10160
10161 if (NILP (vector))
10162 vector = Fmake_vector (make_number (9), Qnil);
10163
10164 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10165 ASET (vector, i, Vdeactivate_mark); ++i;
10166 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10167
10168 if (w)
10169 {
10170 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10171 ASET (vector, i, w->contents); ++i;
10172 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10173 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10174 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10175 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10176 }
10177 else
10178 {
10179 int end = i + 6;
10180 for (; i < end; ++i)
10181 ASET (vector, i, Qnil);
10182 }
10183
10184 eassert (i == ASIZE (vector));
10185 return vector;
10186 }
10187
10188
10189 /* Restore global state from VECTOR which was created by
10190 with_echo_area_buffer_unwind_data. */
10191
10192 static void
10193 unwind_with_echo_area_buffer (Lisp_Object vector)
10194 {
10195 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10196 Vdeactivate_mark = AREF (vector, 1);
10197 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10198
10199 if (WINDOWP (AREF (vector, 3)))
10200 {
10201 struct window *w;
10202 Lisp_Object buffer;
10203
10204 w = XWINDOW (AREF (vector, 3));
10205 buffer = AREF (vector, 4);
10206
10207 wset_buffer (w, buffer);
10208 set_marker_both (w->pointm, buffer,
10209 XFASTINT (AREF (vector, 5)),
10210 XFASTINT (AREF (vector, 6)));
10211 set_marker_both (w->start, buffer,
10212 XFASTINT (AREF (vector, 7)),
10213 XFASTINT (AREF (vector, 8)));
10214 }
10215
10216 Vwith_echo_area_save_vector = vector;
10217 }
10218
10219
10220 /* Set up the echo area for use by print functions. MULTIBYTE_P
10221 non-zero means we will print multibyte. */
10222
10223 void
10224 setup_echo_area_for_printing (int multibyte_p)
10225 {
10226 /* If we can't find an echo area any more, exit. */
10227 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10228 Fkill_emacs (Qnil);
10229
10230 ensure_echo_area_buffers ();
10231
10232 if (!message_buf_print)
10233 {
10234 /* A message has been output since the last time we printed.
10235 Choose a fresh echo area buffer. */
10236 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10237 echo_area_buffer[0] = echo_buffer[1];
10238 else
10239 echo_area_buffer[0] = echo_buffer[0];
10240
10241 /* Switch to that buffer and clear it. */
10242 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10243 bset_truncate_lines (current_buffer, Qnil);
10244
10245 if (Z > BEG)
10246 {
10247 ptrdiff_t count = SPECPDL_INDEX ();
10248 specbind (Qinhibit_read_only, Qt);
10249 /* Note that undo recording is always disabled. */
10250 del_range (BEG, Z);
10251 unbind_to (count, Qnil);
10252 }
10253 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10254
10255 /* Set up the buffer for the multibyteness we need. */
10256 if (multibyte_p
10257 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10258 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10259
10260 /* Raise the frame containing the echo area. */
10261 if (minibuffer_auto_raise)
10262 {
10263 struct frame *sf = SELECTED_FRAME ();
10264 Lisp_Object mini_window;
10265 mini_window = FRAME_MINIBUF_WINDOW (sf);
10266 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10267 }
10268
10269 message_log_maybe_newline ();
10270 message_buf_print = 1;
10271 }
10272 else
10273 {
10274 if (NILP (echo_area_buffer[0]))
10275 {
10276 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10277 echo_area_buffer[0] = echo_buffer[1];
10278 else
10279 echo_area_buffer[0] = echo_buffer[0];
10280 }
10281
10282 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10283 {
10284 /* Someone switched buffers between print requests. */
10285 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10286 bset_truncate_lines (current_buffer, Qnil);
10287 }
10288 }
10289 }
10290
10291
10292 /* Display an echo area message in window W. Value is non-zero if W's
10293 height is changed. If display_last_displayed_message_p is
10294 non-zero, display the message that was last displayed, otherwise
10295 display the current message. */
10296
10297 static int
10298 display_echo_area (struct window *w)
10299 {
10300 int i, no_message_p, window_height_changed_p;
10301
10302 /* Temporarily disable garbage collections while displaying the echo
10303 area. This is done because a GC can print a message itself.
10304 That message would modify the echo area buffer's contents while a
10305 redisplay of the buffer is going on, and seriously confuse
10306 redisplay. */
10307 ptrdiff_t count = inhibit_garbage_collection ();
10308
10309 /* If there is no message, we must call display_echo_area_1
10310 nevertheless because it resizes the window. But we will have to
10311 reset the echo_area_buffer in question to nil at the end because
10312 with_echo_area_buffer will sets it to an empty buffer. */
10313 i = display_last_displayed_message_p ? 1 : 0;
10314 no_message_p = NILP (echo_area_buffer[i]);
10315
10316 window_height_changed_p
10317 = with_echo_area_buffer (w, display_last_displayed_message_p,
10318 display_echo_area_1,
10319 (intptr_t) w, Qnil);
10320
10321 if (no_message_p)
10322 echo_area_buffer[i] = Qnil;
10323
10324 unbind_to (count, Qnil);
10325 return window_height_changed_p;
10326 }
10327
10328
10329 /* Helper for display_echo_area. Display the current buffer which
10330 contains the current echo area message in window W, a mini-window,
10331 a pointer to which is passed in A1. A2..A4 are currently not used.
10332 Change the height of W so that all of the message is displayed.
10333 Value is non-zero if height of W was changed. */
10334
10335 static int
10336 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10337 {
10338 intptr_t i1 = a1;
10339 struct window *w = (struct window *) i1;
10340 Lisp_Object window;
10341 struct text_pos start;
10342 int window_height_changed_p = 0;
10343
10344 /* Do this before displaying, so that we have a large enough glyph
10345 matrix for the display. If we can't get enough space for the
10346 whole text, display the last N lines. That works by setting w->start. */
10347 window_height_changed_p = resize_mini_window (w, 0);
10348
10349 /* Use the starting position chosen by resize_mini_window. */
10350 SET_TEXT_POS_FROM_MARKER (start, w->start);
10351
10352 /* Display. */
10353 clear_glyph_matrix (w->desired_matrix);
10354 XSETWINDOW (window, w);
10355 try_window (window, start, 0);
10356
10357 return window_height_changed_p;
10358 }
10359
10360
10361 /* Resize the echo area window to exactly the size needed for the
10362 currently displayed message, if there is one. If a mini-buffer
10363 is active, don't shrink it. */
10364
10365 void
10366 resize_echo_area_exactly (void)
10367 {
10368 if (BUFFERP (echo_area_buffer[0])
10369 && WINDOWP (echo_area_window))
10370 {
10371 struct window *w = XWINDOW (echo_area_window);
10372 int resized_p;
10373 Lisp_Object resize_exactly;
10374
10375 if (minibuf_level == 0)
10376 resize_exactly = Qt;
10377 else
10378 resize_exactly = Qnil;
10379
10380 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10381 (intptr_t) w, resize_exactly);
10382 if (resized_p)
10383 {
10384 ++windows_or_buffers_changed;
10385 ++update_mode_lines;
10386 redisplay_internal ();
10387 }
10388 }
10389 }
10390
10391
10392 /* Callback function for with_echo_area_buffer, when used from
10393 resize_echo_area_exactly. A1 contains a pointer to the window to
10394 resize, EXACTLY non-nil means resize the mini-window exactly to the
10395 size of the text displayed. A3 and A4 are not used. Value is what
10396 resize_mini_window returns. */
10397
10398 static int
10399 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10400 {
10401 intptr_t i1 = a1;
10402 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10403 }
10404
10405
10406 /* Resize mini-window W to fit the size of its contents. EXACT_P
10407 means size the window exactly to the size needed. Otherwise, it's
10408 only enlarged until W's buffer is empty.
10409
10410 Set W->start to the right place to begin display. If the whole
10411 contents fit, start at the beginning. Otherwise, start so as
10412 to make the end of the contents appear. This is particularly
10413 important for y-or-n-p, but seems desirable generally.
10414
10415 Value is non-zero if the window height has been changed. */
10416
10417 int
10418 resize_mini_window (struct window *w, int exact_p)
10419 {
10420 struct frame *f = XFRAME (w->frame);
10421 int window_height_changed_p = 0;
10422
10423 eassert (MINI_WINDOW_P (w));
10424
10425 /* By default, start display at the beginning. */
10426 set_marker_both (w->start, w->contents,
10427 BUF_BEGV (XBUFFER (w->contents)),
10428 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10429
10430 /* Don't resize windows while redisplaying a window; it would
10431 confuse redisplay functions when the size of the window they are
10432 displaying changes from under them. Such a resizing can happen,
10433 for instance, when which-func prints a long message while
10434 we are running fontification-functions. We're running these
10435 functions with safe_call which binds inhibit-redisplay to t. */
10436 if (!NILP (Vinhibit_redisplay))
10437 return 0;
10438
10439 /* Nil means don't try to resize. */
10440 if (NILP (Vresize_mini_windows)
10441 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10442 return 0;
10443
10444 if (!FRAME_MINIBUF_ONLY_P (f))
10445 {
10446 struct it it;
10447 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10448 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10449 int height;
10450 EMACS_INT max_height;
10451 int unit = FRAME_LINE_HEIGHT (f);
10452 struct text_pos start;
10453 struct buffer *old_current_buffer = NULL;
10454
10455 if (current_buffer != XBUFFER (w->contents))
10456 {
10457 old_current_buffer = current_buffer;
10458 set_buffer_internal (XBUFFER (w->contents));
10459 }
10460
10461 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10462
10463 /* Compute the max. number of lines specified by the user. */
10464 if (FLOATP (Vmax_mini_window_height))
10465 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10466 else if (INTEGERP (Vmax_mini_window_height))
10467 max_height = XINT (Vmax_mini_window_height);
10468 else
10469 max_height = total_height / 4;
10470
10471 /* Correct that max. height if it's bogus. */
10472 max_height = clip_to_bounds (1, max_height, total_height);
10473
10474 /* Find out the height of the text in the window. */
10475 if (it.line_wrap == TRUNCATE)
10476 height = 1;
10477 else
10478 {
10479 last_height = 0;
10480 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10481 if (it.max_ascent == 0 && it.max_descent == 0)
10482 height = it.current_y + last_height;
10483 else
10484 height = it.current_y + it.max_ascent + it.max_descent;
10485 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10486 height = (height + unit - 1) / unit;
10487 }
10488
10489 /* Compute a suitable window start. */
10490 if (height > max_height)
10491 {
10492 height = max_height;
10493 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10494 move_it_vertically_backward (&it, (height - 1) * unit);
10495 start = it.current.pos;
10496 }
10497 else
10498 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10499 SET_MARKER_FROM_TEXT_POS (w->start, start);
10500
10501 if (EQ (Vresize_mini_windows, Qgrow_only))
10502 {
10503 /* Let it grow only, until we display an empty message, in which
10504 case the window shrinks again. */
10505 if (height > WINDOW_TOTAL_LINES (w))
10506 {
10507 int old_height = WINDOW_TOTAL_LINES (w);
10508
10509 FRAME_WINDOWS_FROZEN (f) = 1;
10510 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10511 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10512 }
10513 else if (height < WINDOW_TOTAL_LINES (w)
10514 && (exact_p || BEGV == ZV))
10515 {
10516 int old_height = WINDOW_TOTAL_LINES (w);
10517
10518 FRAME_WINDOWS_FROZEN (f) = 0;
10519 shrink_mini_window (w);
10520 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10521 }
10522 }
10523 else
10524 {
10525 /* Always resize to exact size needed. */
10526 if (height > WINDOW_TOTAL_LINES (w))
10527 {
10528 int old_height = WINDOW_TOTAL_LINES (w);
10529
10530 FRAME_WINDOWS_FROZEN (f) = 1;
10531 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10532 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10533 }
10534 else if (height < WINDOW_TOTAL_LINES (w))
10535 {
10536 int old_height = WINDOW_TOTAL_LINES (w);
10537
10538 FRAME_WINDOWS_FROZEN (f) = 0;
10539 shrink_mini_window (w);
10540
10541 if (height)
10542 {
10543 FRAME_WINDOWS_FROZEN (f) = 1;
10544 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10545 }
10546
10547 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10548 }
10549 }
10550
10551 if (old_current_buffer)
10552 set_buffer_internal (old_current_buffer);
10553 }
10554
10555 return window_height_changed_p;
10556 }
10557
10558
10559 /* Value is the current message, a string, or nil if there is no
10560 current message. */
10561
10562 Lisp_Object
10563 current_message (void)
10564 {
10565 Lisp_Object msg;
10566
10567 if (!BUFFERP (echo_area_buffer[0]))
10568 msg = Qnil;
10569 else
10570 {
10571 with_echo_area_buffer (0, 0, current_message_1,
10572 (intptr_t) &msg, Qnil);
10573 if (NILP (msg))
10574 echo_area_buffer[0] = Qnil;
10575 }
10576
10577 return msg;
10578 }
10579
10580
10581 static int
10582 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10583 {
10584 intptr_t i1 = a1;
10585 Lisp_Object *msg = (Lisp_Object *) i1;
10586
10587 if (Z > BEG)
10588 *msg = make_buffer_string (BEG, Z, 1);
10589 else
10590 *msg = Qnil;
10591 return 0;
10592 }
10593
10594
10595 /* Push the current message on Vmessage_stack for later restoration
10596 by restore_message. Value is non-zero if the current message isn't
10597 empty. This is a relatively infrequent operation, so it's not
10598 worth optimizing. */
10599
10600 bool
10601 push_message (void)
10602 {
10603 Lisp_Object msg = current_message ();
10604 Vmessage_stack = Fcons (msg, Vmessage_stack);
10605 return STRINGP (msg);
10606 }
10607
10608
10609 /* Restore message display from the top of Vmessage_stack. */
10610
10611 void
10612 restore_message (void)
10613 {
10614 eassert (CONSP (Vmessage_stack));
10615 message3_nolog (XCAR (Vmessage_stack));
10616 }
10617
10618
10619 /* Handler for unwind-protect calling pop_message. */
10620
10621 void
10622 pop_message_unwind (void)
10623 {
10624 /* Pop the top-most entry off Vmessage_stack. */
10625 eassert (CONSP (Vmessage_stack));
10626 Vmessage_stack = XCDR (Vmessage_stack);
10627 }
10628
10629
10630 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10631 exits. If the stack is not empty, we have a missing pop_message
10632 somewhere. */
10633
10634 void
10635 check_message_stack (void)
10636 {
10637 if (!NILP (Vmessage_stack))
10638 emacs_abort ();
10639 }
10640
10641
10642 /* Truncate to NCHARS what will be displayed in the echo area the next
10643 time we display it---but don't redisplay it now. */
10644
10645 void
10646 truncate_echo_area (ptrdiff_t nchars)
10647 {
10648 if (nchars == 0)
10649 echo_area_buffer[0] = Qnil;
10650 else if (!noninteractive
10651 && INTERACTIVE
10652 && !NILP (echo_area_buffer[0]))
10653 {
10654 struct frame *sf = SELECTED_FRAME ();
10655 /* Error messages get reported properly by cmd_error, so this must be
10656 just an informative message; if the frame hasn't really been
10657 initialized yet, just toss it. */
10658 if (sf->glyphs_initialized_p)
10659 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10660 }
10661 }
10662
10663
10664 /* Helper function for truncate_echo_area. Truncate the current
10665 message to at most NCHARS characters. */
10666
10667 static int
10668 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10669 {
10670 if (BEG + nchars < Z)
10671 del_range (BEG + nchars, Z);
10672 if (Z == BEG)
10673 echo_area_buffer[0] = Qnil;
10674 return 0;
10675 }
10676
10677 /* Set the current message to STRING. */
10678
10679 static void
10680 set_message (Lisp_Object string)
10681 {
10682 eassert (STRINGP (string));
10683
10684 message_enable_multibyte = STRING_MULTIBYTE (string);
10685
10686 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10687 message_buf_print = 0;
10688 help_echo_showing_p = 0;
10689
10690 if (STRINGP (Vdebug_on_message)
10691 && STRINGP (string)
10692 && fast_string_match (Vdebug_on_message, string) >= 0)
10693 call_debugger (list2 (Qerror, string));
10694 }
10695
10696
10697 /* Helper function for set_message. First argument is ignored and second
10698 argument has the same meaning as for set_message.
10699 This function is called with the echo area buffer being current. */
10700
10701 static int
10702 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10703 {
10704 eassert (STRINGP (string));
10705
10706 /* Change multibyteness of the echo buffer appropriately. */
10707 if (message_enable_multibyte
10708 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10709 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10710
10711 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10712 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10713 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10714
10715 /* Insert new message at BEG. */
10716 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10717
10718 /* This function takes care of single/multibyte conversion.
10719 We just have to ensure that the echo area buffer has the right
10720 setting of enable_multibyte_characters. */
10721 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10722
10723 return 0;
10724 }
10725
10726
10727 /* Clear messages. CURRENT_P non-zero means clear the current
10728 message. LAST_DISPLAYED_P non-zero means clear the message
10729 last displayed. */
10730
10731 void
10732 clear_message (int current_p, int last_displayed_p)
10733 {
10734 if (current_p)
10735 {
10736 echo_area_buffer[0] = Qnil;
10737 message_cleared_p = 1;
10738 }
10739
10740 if (last_displayed_p)
10741 echo_area_buffer[1] = Qnil;
10742
10743 message_buf_print = 0;
10744 }
10745
10746 /* Clear garbaged frames.
10747
10748 This function is used where the old redisplay called
10749 redraw_garbaged_frames which in turn called redraw_frame which in
10750 turn called clear_frame. The call to clear_frame was a source of
10751 flickering. I believe a clear_frame is not necessary. It should
10752 suffice in the new redisplay to invalidate all current matrices,
10753 and ensure a complete redisplay of all windows. */
10754
10755 static void
10756 clear_garbaged_frames (void)
10757 {
10758 if (frame_garbaged)
10759 {
10760 Lisp_Object tail, frame;
10761 int changed_count = 0;
10762
10763 FOR_EACH_FRAME (tail, frame)
10764 {
10765 struct frame *f = XFRAME (frame);
10766
10767 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10768 {
10769 if (f->resized_p)
10770 redraw_frame (f);
10771 else
10772 clear_current_matrices (f);
10773 changed_count++;
10774 f->garbaged = 0;
10775 f->resized_p = 0;
10776 }
10777 }
10778
10779 frame_garbaged = 0;
10780 if (changed_count)
10781 ++windows_or_buffers_changed;
10782 }
10783 }
10784
10785
10786 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10787 is non-zero update selected_frame. Value is non-zero if the
10788 mini-windows height has been changed. */
10789
10790 static int
10791 echo_area_display (int update_frame_p)
10792 {
10793 Lisp_Object mini_window;
10794 struct window *w;
10795 struct frame *f;
10796 int window_height_changed_p = 0;
10797 struct frame *sf = SELECTED_FRAME ();
10798
10799 mini_window = FRAME_MINIBUF_WINDOW (sf);
10800 w = XWINDOW (mini_window);
10801 f = XFRAME (WINDOW_FRAME (w));
10802
10803 /* Don't display if frame is invisible or not yet initialized. */
10804 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10805 return 0;
10806
10807 #ifdef HAVE_WINDOW_SYSTEM
10808 /* When Emacs starts, selected_frame may be the initial terminal
10809 frame. If we let this through, a message would be displayed on
10810 the terminal. */
10811 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10812 return 0;
10813 #endif /* HAVE_WINDOW_SYSTEM */
10814
10815 /* Redraw garbaged frames. */
10816 clear_garbaged_frames ();
10817
10818 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10819 {
10820 echo_area_window = mini_window;
10821 window_height_changed_p = display_echo_area (w);
10822 w->must_be_updated_p = 1;
10823
10824 /* Update the display, unless called from redisplay_internal.
10825 Also don't update the screen during redisplay itself. The
10826 update will happen at the end of redisplay, and an update
10827 here could cause confusion. */
10828 if (update_frame_p && !redisplaying_p)
10829 {
10830 int n = 0;
10831
10832 /* If the display update has been interrupted by pending
10833 input, update mode lines in the frame. Due to the
10834 pending input, it might have been that redisplay hasn't
10835 been called, so that mode lines above the echo area are
10836 garbaged. This looks odd, so we prevent it here. */
10837 if (!display_completed)
10838 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10839
10840 if (window_height_changed_p
10841 /* Don't do this if Emacs is shutting down. Redisplay
10842 needs to run hooks. */
10843 && !NILP (Vrun_hooks))
10844 {
10845 /* Must update other windows. Likewise as in other
10846 cases, don't let this update be interrupted by
10847 pending input. */
10848 ptrdiff_t count = SPECPDL_INDEX ();
10849 specbind (Qredisplay_dont_pause, Qt);
10850 windows_or_buffers_changed = 1;
10851 redisplay_internal ();
10852 unbind_to (count, Qnil);
10853 }
10854 else if (FRAME_WINDOW_P (f) && n == 0)
10855 {
10856 /* Window configuration is the same as before.
10857 Can do with a display update of the echo area,
10858 unless we displayed some mode lines. */
10859 update_single_window (w, 1);
10860 flush_frame (f);
10861 }
10862 else
10863 update_frame (f, 1, 1);
10864
10865 /* If cursor is in the echo area, make sure that the next
10866 redisplay displays the minibuffer, so that the cursor will
10867 be replaced with what the minibuffer wants. */
10868 if (cursor_in_echo_area)
10869 ++windows_or_buffers_changed;
10870 }
10871 }
10872 else if (!EQ (mini_window, selected_window))
10873 windows_or_buffers_changed++;
10874
10875 /* Last displayed message is now the current message. */
10876 echo_area_buffer[1] = echo_area_buffer[0];
10877 /* Inform read_char that we're not echoing. */
10878 echo_message_buffer = Qnil;
10879
10880 /* Prevent redisplay optimization in redisplay_internal by resetting
10881 this_line_start_pos. This is done because the mini-buffer now
10882 displays the message instead of its buffer text. */
10883 if (EQ (mini_window, selected_window))
10884 CHARPOS (this_line_start_pos) = 0;
10885
10886 return window_height_changed_p;
10887 }
10888
10889 /* Nonzero if the current window's buffer is shown in more than one
10890 window and was modified since last redisplay. */
10891
10892 static int
10893 buffer_shared_and_changed (void)
10894 {
10895 return (buffer_window_count (current_buffer) > 1
10896 && UNCHANGED_MODIFIED < MODIFF);
10897 }
10898
10899 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10900 is enabled and mark of W's buffer was changed since last W's update. */
10901
10902 static int
10903 window_buffer_changed (struct window *w)
10904 {
10905 struct buffer *b = XBUFFER (w->contents);
10906
10907 eassert (BUFFER_LIVE_P (b));
10908
10909 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10910 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10911 != (w->region_showing != 0)));
10912 }
10913
10914 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10915
10916 static int
10917 mode_line_update_needed (struct window *w)
10918 {
10919 return (w->column_number_displayed != -1
10920 && !(PT == w->last_point && !window_outdated (w))
10921 && (w->column_number_displayed != current_column ()));
10922 }
10923
10924 /* Nonzero if window start of W is frozen and may not be changed during
10925 redisplay. */
10926
10927 static bool
10928 window_frozen_p (struct window *w)
10929 {
10930 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10931 {
10932 Lisp_Object window;
10933
10934 XSETWINDOW (window, w);
10935 if (MINI_WINDOW_P (w))
10936 return 0;
10937 else if (EQ (window, selected_window))
10938 return 0;
10939 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10940 && EQ (window, Vminibuf_scroll_window))
10941 /* This special window can't be frozen too. */
10942 return 0;
10943 else
10944 return 1;
10945 }
10946 return 0;
10947 }
10948
10949 /***********************************************************************
10950 Mode Lines and Frame Titles
10951 ***********************************************************************/
10952
10953 /* A buffer for constructing non-propertized mode-line strings and
10954 frame titles in it; allocated from the heap in init_xdisp and
10955 resized as needed in store_mode_line_noprop_char. */
10956
10957 static char *mode_line_noprop_buf;
10958
10959 /* The buffer's end, and a current output position in it. */
10960
10961 static char *mode_line_noprop_buf_end;
10962 static char *mode_line_noprop_ptr;
10963
10964 #define MODE_LINE_NOPROP_LEN(start) \
10965 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10966
10967 static enum {
10968 MODE_LINE_DISPLAY = 0,
10969 MODE_LINE_TITLE,
10970 MODE_LINE_NOPROP,
10971 MODE_LINE_STRING
10972 } mode_line_target;
10973
10974 /* Alist that caches the results of :propertize.
10975 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10976 static Lisp_Object mode_line_proptrans_alist;
10977
10978 /* List of strings making up the mode-line. */
10979 static Lisp_Object mode_line_string_list;
10980
10981 /* Base face property when building propertized mode line string. */
10982 static Lisp_Object mode_line_string_face;
10983 static Lisp_Object mode_line_string_face_prop;
10984
10985
10986 /* Unwind data for mode line strings */
10987
10988 static Lisp_Object Vmode_line_unwind_vector;
10989
10990 static Lisp_Object
10991 format_mode_line_unwind_data (struct frame *target_frame,
10992 struct buffer *obuf,
10993 Lisp_Object owin,
10994 int save_proptrans)
10995 {
10996 Lisp_Object vector, tmp;
10997
10998 /* Reduce consing by keeping one vector in
10999 Vwith_echo_area_save_vector. */
11000 vector = Vmode_line_unwind_vector;
11001 Vmode_line_unwind_vector = Qnil;
11002
11003 if (NILP (vector))
11004 vector = Fmake_vector (make_number (10), Qnil);
11005
11006 ASET (vector, 0, make_number (mode_line_target));
11007 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11008 ASET (vector, 2, mode_line_string_list);
11009 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11010 ASET (vector, 4, mode_line_string_face);
11011 ASET (vector, 5, mode_line_string_face_prop);
11012
11013 if (obuf)
11014 XSETBUFFER (tmp, obuf);
11015 else
11016 tmp = Qnil;
11017 ASET (vector, 6, tmp);
11018 ASET (vector, 7, owin);
11019 if (target_frame)
11020 {
11021 /* Similarly to `with-selected-window', if the operation selects
11022 a window on another frame, we must restore that frame's
11023 selected window, and (for a tty) the top-frame. */
11024 ASET (vector, 8, target_frame->selected_window);
11025 if (FRAME_TERMCAP_P (target_frame))
11026 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11027 }
11028
11029 return vector;
11030 }
11031
11032 static void
11033 unwind_format_mode_line (Lisp_Object vector)
11034 {
11035 Lisp_Object old_window = AREF (vector, 7);
11036 Lisp_Object target_frame_window = AREF (vector, 8);
11037 Lisp_Object old_top_frame = AREF (vector, 9);
11038
11039 mode_line_target = XINT (AREF (vector, 0));
11040 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11041 mode_line_string_list = AREF (vector, 2);
11042 if (! EQ (AREF (vector, 3), Qt))
11043 mode_line_proptrans_alist = AREF (vector, 3);
11044 mode_line_string_face = AREF (vector, 4);
11045 mode_line_string_face_prop = AREF (vector, 5);
11046
11047 /* Select window before buffer, since it may change the buffer. */
11048 if (!NILP (old_window))
11049 {
11050 /* If the operation that we are unwinding had selected a window
11051 on a different frame, reset its frame-selected-window. For a
11052 text terminal, reset its top-frame if necessary. */
11053 if (!NILP (target_frame_window))
11054 {
11055 Lisp_Object frame
11056 = WINDOW_FRAME (XWINDOW (target_frame_window));
11057
11058 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11059 Fselect_window (target_frame_window, Qt);
11060
11061 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11062 Fselect_frame (old_top_frame, Qt);
11063 }
11064
11065 Fselect_window (old_window, Qt);
11066 }
11067
11068 if (!NILP (AREF (vector, 6)))
11069 {
11070 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11071 ASET (vector, 6, Qnil);
11072 }
11073
11074 Vmode_line_unwind_vector = vector;
11075 }
11076
11077
11078 /* Store a single character C for the frame title in mode_line_noprop_buf.
11079 Re-allocate mode_line_noprop_buf if necessary. */
11080
11081 static void
11082 store_mode_line_noprop_char (char c)
11083 {
11084 /* If output position has reached the end of the allocated buffer,
11085 increase the buffer's size. */
11086 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11087 {
11088 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11089 ptrdiff_t size = len;
11090 mode_line_noprop_buf =
11091 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11092 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11093 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11094 }
11095
11096 *mode_line_noprop_ptr++ = c;
11097 }
11098
11099
11100 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11101 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11102 characters that yield more columns than PRECISION; PRECISION <= 0
11103 means copy the whole string. Pad with spaces until FIELD_WIDTH
11104 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11105 pad. Called from display_mode_element when it is used to build a
11106 frame title. */
11107
11108 static int
11109 store_mode_line_noprop (const char *string, int field_width, int precision)
11110 {
11111 const unsigned char *str = (const unsigned char *) string;
11112 int n = 0;
11113 ptrdiff_t dummy, nbytes;
11114
11115 /* Copy at most PRECISION chars from STR. */
11116 nbytes = strlen (string);
11117 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11118 while (nbytes--)
11119 store_mode_line_noprop_char (*str++);
11120
11121 /* Fill up with spaces until FIELD_WIDTH reached. */
11122 while (field_width > 0
11123 && n < field_width)
11124 {
11125 store_mode_line_noprop_char (' ');
11126 ++n;
11127 }
11128
11129 return n;
11130 }
11131
11132 /***********************************************************************
11133 Frame Titles
11134 ***********************************************************************/
11135
11136 #ifdef HAVE_WINDOW_SYSTEM
11137
11138 /* Set the title of FRAME, if it has changed. The title format is
11139 Vicon_title_format if FRAME is iconified, otherwise it is
11140 frame_title_format. */
11141
11142 static void
11143 x_consider_frame_title (Lisp_Object frame)
11144 {
11145 struct frame *f = XFRAME (frame);
11146
11147 if (FRAME_WINDOW_P (f)
11148 || FRAME_MINIBUF_ONLY_P (f)
11149 || f->explicit_name)
11150 {
11151 /* Do we have more than one visible frame on this X display? */
11152 Lisp_Object tail, other_frame, fmt;
11153 ptrdiff_t title_start;
11154 char *title;
11155 ptrdiff_t len;
11156 struct it it;
11157 ptrdiff_t count = SPECPDL_INDEX ();
11158
11159 FOR_EACH_FRAME (tail, other_frame)
11160 {
11161 struct frame *tf = XFRAME (other_frame);
11162
11163 if (tf != f
11164 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11165 && !FRAME_MINIBUF_ONLY_P (tf)
11166 && !EQ (other_frame, tip_frame)
11167 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11168 break;
11169 }
11170
11171 /* Set global variable indicating that multiple frames exist. */
11172 multiple_frames = CONSP (tail);
11173
11174 /* Switch to the buffer of selected window of the frame. Set up
11175 mode_line_target so that display_mode_element will output into
11176 mode_line_noprop_buf; then display the title. */
11177 record_unwind_protect (unwind_format_mode_line,
11178 format_mode_line_unwind_data
11179 (f, current_buffer, selected_window, 0));
11180
11181 Fselect_window (f->selected_window, Qt);
11182 set_buffer_internal_1
11183 (XBUFFER (XWINDOW (f->selected_window)->contents));
11184 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11185
11186 mode_line_target = MODE_LINE_TITLE;
11187 title_start = MODE_LINE_NOPROP_LEN (0);
11188 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11189 NULL, DEFAULT_FACE_ID);
11190 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11191 len = MODE_LINE_NOPROP_LEN (title_start);
11192 title = mode_line_noprop_buf + title_start;
11193 unbind_to (count, Qnil);
11194
11195 /* Set the title only if it's changed. This avoids consing in
11196 the common case where it hasn't. (If it turns out that we've
11197 already wasted too much time by walking through the list with
11198 display_mode_element, then we might need to optimize at a
11199 higher level than this.) */
11200 if (! STRINGP (f->name)
11201 || SBYTES (f->name) != len
11202 || memcmp (title, SDATA (f->name), len) != 0)
11203 x_implicitly_set_name (f, make_string (title, len), Qnil);
11204 }
11205 }
11206
11207 #endif /* not HAVE_WINDOW_SYSTEM */
11208
11209 \f
11210 /***********************************************************************
11211 Menu Bars
11212 ***********************************************************************/
11213
11214
11215 /* Prepare for redisplay by updating menu-bar item lists when
11216 appropriate. This can call eval. */
11217
11218 void
11219 prepare_menu_bars (void)
11220 {
11221 int all_windows;
11222 struct gcpro gcpro1, gcpro2;
11223 struct frame *f;
11224 Lisp_Object tooltip_frame;
11225
11226 #ifdef HAVE_WINDOW_SYSTEM
11227 tooltip_frame = tip_frame;
11228 #else
11229 tooltip_frame = Qnil;
11230 #endif
11231
11232 /* Update all frame titles based on their buffer names, etc. We do
11233 this before the menu bars so that the buffer-menu will show the
11234 up-to-date frame titles. */
11235 #ifdef HAVE_WINDOW_SYSTEM
11236 if (windows_or_buffers_changed || update_mode_lines)
11237 {
11238 Lisp_Object tail, frame;
11239
11240 FOR_EACH_FRAME (tail, frame)
11241 {
11242 f = XFRAME (frame);
11243 if (!EQ (frame, tooltip_frame)
11244 && (FRAME_ICONIFIED_P (f)
11245 || FRAME_VISIBLE_P (f) == 1
11246 /* Exclude TTY frames that are obscured because they
11247 are not the top frame on their console. This is
11248 because x_consider_frame_title actually switches
11249 to the frame, which for TTY frames means it is
11250 marked as garbaged, and will be completely
11251 redrawn on the next redisplay cycle. This causes
11252 TTY frames to be completely redrawn, when there
11253 are more than one of them, even though nothing
11254 should be changed on display. */
11255 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11256 x_consider_frame_title (frame);
11257 }
11258 }
11259 #endif /* HAVE_WINDOW_SYSTEM */
11260
11261 /* Update the menu bar item lists, if appropriate. This has to be
11262 done before any actual redisplay or generation of display lines. */
11263 all_windows = (update_mode_lines
11264 || buffer_shared_and_changed ()
11265 || windows_or_buffers_changed);
11266 if (all_windows)
11267 {
11268 Lisp_Object tail, frame;
11269 ptrdiff_t count = SPECPDL_INDEX ();
11270 /* 1 means that update_menu_bar has run its hooks
11271 so any further calls to update_menu_bar shouldn't do so again. */
11272 int menu_bar_hooks_run = 0;
11273
11274 record_unwind_save_match_data ();
11275
11276 FOR_EACH_FRAME (tail, frame)
11277 {
11278 f = XFRAME (frame);
11279
11280 /* Ignore tooltip frame. */
11281 if (EQ (frame, tooltip_frame))
11282 continue;
11283
11284 /* If a window on this frame changed size, report that to
11285 the user and clear the size-change flag. */
11286 if (FRAME_WINDOW_SIZES_CHANGED (f))
11287 {
11288 Lisp_Object functions;
11289
11290 /* Clear flag first in case we get an error below. */
11291 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11292 functions = Vwindow_size_change_functions;
11293 GCPRO2 (tail, functions);
11294
11295 while (CONSP (functions))
11296 {
11297 if (!EQ (XCAR (functions), Qt))
11298 call1 (XCAR (functions), frame);
11299 functions = XCDR (functions);
11300 }
11301 UNGCPRO;
11302 }
11303
11304 GCPRO1 (tail);
11305 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11306 #ifdef HAVE_WINDOW_SYSTEM
11307 update_tool_bar (f, 0);
11308 #endif
11309 #ifdef HAVE_NS
11310 if (windows_or_buffers_changed
11311 && FRAME_NS_P (f))
11312 ns_set_doc_edited
11313 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11314 #endif
11315 UNGCPRO;
11316 }
11317
11318 unbind_to (count, Qnil);
11319 }
11320 else
11321 {
11322 struct frame *sf = SELECTED_FRAME ();
11323 update_menu_bar (sf, 1, 0);
11324 #ifdef HAVE_WINDOW_SYSTEM
11325 update_tool_bar (sf, 1);
11326 #endif
11327 }
11328 }
11329
11330
11331 /* Update the menu bar item list for frame F. This has to be done
11332 before we start to fill in any display lines, because it can call
11333 eval.
11334
11335 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11336
11337 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11338 already ran the menu bar hooks for this redisplay, so there
11339 is no need to run them again. The return value is the
11340 updated value of this flag, to pass to the next call. */
11341
11342 static int
11343 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11344 {
11345 Lisp_Object window;
11346 register struct window *w;
11347
11348 /* If called recursively during a menu update, do nothing. This can
11349 happen when, for instance, an activate-menubar-hook causes a
11350 redisplay. */
11351 if (inhibit_menubar_update)
11352 return hooks_run;
11353
11354 window = FRAME_SELECTED_WINDOW (f);
11355 w = XWINDOW (window);
11356
11357 if (FRAME_WINDOW_P (f)
11358 ?
11359 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11360 || defined (HAVE_NS) || defined (USE_GTK)
11361 FRAME_EXTERNAL_MENU_BAR (f)
11362 #else
11363 FRAME_MENU_BAR_LINES (f) > 0
11364 #endif
11365 : FRAME_MENU_BAR_LINES (f) > 0)
11366 {
11367 /* If the user has switched buffers or windows, we need to
11368 recompute to reflect the new bindings. But we'll
11369 recompute when update_mode_lines is set too; that means
11370 that people can use force-mode-line-update to request
11371 that the menu bar be recomputed. The adverse effect on
11372 the rest of the redisplay algorithm is about the same as
11373 windows_or_buffers_changed anyway. */
11374 if (windows_or_buffers_changed
11375 /* This used to test w->update_mode_line, but we believe
11376 there is no need to recompute the menu in that case. */
11377 || update_mode_lines
11378 || window_buffer_changed (w))
11379 {
11380 struct buffer *prev = current_buffer;
11381 ptrdiff_t count = SPECPDL_INDEX ();
11382
11383 specbind (Qinhibit_menubar_update, Qt);
11384
11385 set_buffer_internal_1 (XBUFFER (w->contents));
11386 if (save_match_data)
11387 record_unwind_save_match_data ();
11388 if (NILP (Voverriding_local_map_menu_flag))
11389 {
11390 specbind (Qoverriding_terminal_local_map, Qnil);
11391 specbind (Qoverriding_local_map, Qnil);
11392 }
11393
11394 if (!hooks_run)
11395 {
11396 /* Run the Lucid hook. */
11397 safe_run_hooks (Qactivate_menubar_hook);
11398
11399 /* If it has changed current-menubar from previous value,
11400 really recompute the menu-bar from the value. */
11401 if (! NILP (Vlucid_menu_bar_dirty_flag))
11402 call0 (Qrecompute_lucid_menubar);
11403
11404 safe_run_hooks (Qmenu_bar_update_hook);
11405
11406 hooks_run = 1;
11407 }
11408
11409 XSETFRAME (Vmenu_updating_frame, f);
11410 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11411
11412 /* Redisplay the menu bar in case we changed it. */
11413 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11414 || defined (HAVE_NS) || defined (USE_GTK)
11415 if (FRAME_WINDOW_P (f))
11416 {
11417 #if defined (HAVE_NS)
11418 /* All frames on Mac OS share the same menubar. So only
11419 the selected frame should be allowed to set it. */
11420 if (f == SELECTED_FRAME ())
11421 #endif
11422 set_frame_menubar (f, 0, 0);
11423 }
11424 else
11425 /* On a terminal screen, the menu bar is an ordinary screen
11426 line, and this makes it get updated. */
11427 w->update_mode_line = 1;
11428 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11429 /* In the non-toolkit version, the menu bar is an ordinary screen
11430 line, and this makes it get updated. */
11431 w->update_mode_line = 1;
11432 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11433
11434 unbind_to (count, Qnil);
11435 set_buffer_internal_1 (prev);
11436 }
11437 }
11438
11439 return hooks_run;
11440 }
11441
11442 /***********************************************************************
11443 Tool-bars
11444 ***********************************************************************/
11445
11446 #ifdef HAVE_WINDOW_SYSTEM
11447
11448 /* Where the mouse was last time we reported a mouse event. */
11449
11450 struct frame *last_mouse_frame;
11451
11452 /* Tool-bar item index of the item on which a mouse button was pressed
11453 or -1. */
11454
11455 int last_tool_bar_item;
11456
11457 /* Select `frame' temporarily without running all the code in
11458 do_switch_frame.
11459 FIXME: Maybe do_switch_frame should be trimmed down similarly
11460 when `norecord' is set. */
11461 static void
11462 fast_set_selected_frame (Lisp_Object frame)
11463 {
11464 if (!EQ (selected_frame, frame))
11465 {
11466 selected_frame = frame;
11467 selected_window = XFRAME (frame)->selected_window;
11468 }
11469 }
11470
11471 /* Update the tool-bar item list for frame F. This has to be done
11472 before we start to fill in any display lines. Called from
11473 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11474 and restore it here. */
11475
11476 static void
11477 update_tool_bar (struct frame *f, int save_match_data)
11478 {
11479 #if defined (USE_GTK) || defined (HAVE_NS)
11480 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11481 #else
11482 int do_update = WINDOWP (f->tool_bar_window)
11483 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11484 #endif
11485
11486 if (do_update)
11487 {
11488 Lisp_Object window;
11489 struct window *w;
11490
11491 window = FRAME_SELECTED_WINDOW (f);
11492 w = XWINDOW (window);
11493
11494 /* If the user has switched buffers or windows, we need to
11495 recompute to reflect the new bindings. But we'll
11496 recompute when update_mode_lines is set too; that means
11497 that people can use force-mode-line-update to request
11498 that the menu bar be recomputed. The adverse effect on
11499 the rest of the redisplay algorithm is about the same as
11500 windows_or_buffers_changed anyway. */
11501 if (windows_or_buffers_changed
11502 || w->update_mode_line
11503 || update_mode_lines
11504 || window_buffer_changed (w))
11505 {
11506 struct buffer *prev = current_buffer;
11507 ptrdiff_t count = SPECPDL_INDEX ();
11508 Lisp_Object frame, new_tool_bar;
11509 int new_n_tool_bar;
11510 struct gcpro gcpro1;
11511
11512 /* Set current_buffer to the buffer of the selected
11513 window of the frame, so that we get the right local
11514 keymaps. */
11515 set_buffer_internal_1 (XBUFFER (w->contents));
11516
11517 /* Save match data, if we must. */
11518 if (save_match_data)
11519 record_unwind_save_match_data ();
11520
11521 /* Make sure that we don't accidentally use bogus keymaps. */
11522 if (NILP (Voverriding_local_map_menu_flag))
11523 {
11524 specbind (Qoverriding_terminal_local_map, Qnil);
11525 specbind (Qoverriding_local_map, Qnil);
11526 }
11527
11528 GCPRO1 (new_tool_bar);
11529
11530 /* We must temporarily set the selected frame to this frame
11531 before calling tool_bar_items, because the calculation of
11532 the tool-bar keymap uses the selected frame (see
11533 `tool-bar-make-keymap' in tool-bar.el). */
11534 eassert (EQ (selected_window,
11535 /* Since we only explicitly preserve selected_frame,
11536 check that selected_window would be redundant. */
11537 XFRAME (selected_frame)->selected_window));
11538 record_unwind_protect (fast_set_selected_frame, selected_frame);
11539 XSETFRAME (frame, f);
11540 fast_set_selected_frame (frame);
11541
11542 /* Build desired tool-bar items from keymaps. */
11543 new_tool_bar
11544 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11545 &new_n_tool_bar);
11546
11547 /* Redisplay the tool-bar if we changed it. */
11548 if (new_n_tool_bar != f->n_tool_bar_items
11549 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11550 {
11551 /* Redisplay that happens asynchronously due to an expose event
11552 may access f->tool_bar_items. Make sure we update both
11553 variables within BLOCK_INPUT so no such event interrupts. */
11554 block_input ();
11555 fset_tool_bar_items (f, new_tool_bar);
11556 f->n_tool_bar_items = new_n_tool_bar;
11557 w->update_mode_line = 1;
11558 unblock_input ();
11559 }
11560
11561 UNGCPRO;
11562
11563 unbind_to (count, Qnil);
11564 set_buffer_internal_1 (prev);
11565 }
11566 }
11567 }
11568
11569
11570 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11571 F's desired tool-bar contents. F->tool_bar_items must have
11572 been set up previously by calling prepare_menu_bars. */
11573
11574 static void
11575 build_desired_tool_bar_string (struct frame *f)
11576 {
11577 int i, size, size_needed;
11578 struct gcpro gcpro1, gcpro2, gcpro3;
11579 Lisp_Object image, plist, props;
11580
11581 image = plist = props = Qnil;
11582 GCPRO3 (image, plist, props);
11583
11584 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11585 Otherwise, make a new string. */
11586
11587 /* The size of the string we might be able to reuse. */
11588 size = (STRINGP (f->desired_tool_bar_string)
11589 ? SCHARS (f->desired_tool_bar_string)
11590 : 0);
11591
11592 /* We need one space in the string for each image. */
11593 size_needed = f->n_tool_bar_items;
11594
11595 /* Reuse f->desired_tool_bar_string, if possible. */
11596 if (size < size_needed || NILP (f->desired_tool_bar_string))
11597 fset_desired_tool_bar_string
11598 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11599 else
11600 {
11601 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11602 Fremove_text_properties (make_number (0), make_number (size),
11603 props, f->desired_tool_bar_string);
11604 }
11605
11606 /* Put a `display' property on the string for the images to display,
11607 put a `menu_item' property on tool-bar items with a value that
11608 is the index of the item in F's tool-bar item vector. */
11609 for (i = 0; i < f->n_tool_bar_items; ++i)
11610 {
11611 #define PROP(IDX) \
11612 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11613
11614 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11615 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11616 int hmargin, vmargin, relief, idx, end;
11617
11618 /* If image is a vector, choose the image according to the
11619 button state. */
11620 image = PROP (TOOL_BAR_ITEM_IMAGES);
11621 if (VECTORP (image))
11622 {
11623 if (enabled_p)
11624 idx = (selected_p
11625 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11626 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11627 else
11628 idx = (selected_p
11629 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11630 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11631
11632 eassert (ASIZE (image) >= idx);
11633 image = AREF (image, idx);
11634 }
11635 else
11636 idx = -1;
11637
11638 /* Ignore invalid image specifications. */
11639 if (!valid_image_p (image))
11640 continue;
11641
11642 /* Display the tool-bar button pressed, or depressed. */
11643 plist = Fcopy_sequence (XCDR (image));
11644
11645 /* Compute margin and relief to draw. */
11646 relief = (tool_bar_button_relief >= 0
11647 ? tool_bar_button_relief
11648 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11649 hmargin = vmargin = relief;
11650
11651 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11652 INT_MAX - max (hmargin, vmargin)))
11653 {
11654 hmargin += XFASTINT (Vtool_bar_button_margin);
11655 vmargin += XFASTINT (Vtool_bar_button_margin);
11656 }
11657 else if (CONSP (Vtool_bar_button_margin))
11658 {
11659 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11660 INT_MAX - hmargin))
11661 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11662
11663 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11664 INT_MAX - vmargin))
11665 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11666 }
11667
11668 if (auto_raise_tool_bar_buttons_p)
11669 {
11670 /* Add a `:relief' property to the image spec if the item is
11671 selected. */
11672 if (selected_p)
11673 {
11674 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11675 hmargin -= relief;
11676 vmargin -= relief;
11677 }
11678 }
11679 else
11680 {
11681 /* If image is selected, display it pressed, i.e. with a
11682 negative relief. If it's not selected, display it with a
11683 raised relief. */
11684 plist = Fplist_put (plist, QCrelief,
11685 (selected_p
11686 ? make_number (-relief)
11687 : make_number (relief)));
11688 hmargin -= relief;
11689 vmargin -= relief;
11690 }
11691
11692 /* Put a margin around the image. */
11693 if (hmargin || vmargin)
11694 {
11695 if (hmargin == vmargin)
11696 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11697 else
11698 plist = Fplist_put (plist, QCmargin,
11699 Fcons (make_number (hmargin),
11700 make_number (vmargin)));
11701 }
11702
11703 /* If button is not enabled, and we don't have special images
11704 for the disabled state, make the image appear disabled by
11705 applying an appropriate algorithm to it. */
11706 if (!enabled_p && idx < 0)
11707 plist = Fplist_put (plist, QCconversion, Qdisabled);
11708
11709 /* Put a `display' text property on the string for the image to
11710 display. Put a `menu-item' property on the string that gives
11711 the start of this item's properties in the tool-bar items
11712 vector. */
11713 image = Fcons (Qimage, plist);
11714 props = list4 (Qdisplay, image,
11715 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11716
11717 /* Let the last image hide all remaining spaces in the tool bar
11718 string. The string can be longer than needed when we reuse a
11719 previous string. */
11720 if (i + 1 == f->n_tool_bar_items)
11721 end = SCHARS (f->desired_tool_bar_string);
11722 else
11723 end = i + 1;
11724 Fadd_text_properties (make_number (i), make_number (end),
11725 props, f->desired_tool_bar_string);
11726 #undef PROP
11727 }
11728
11729 UNGCPRO;
11730 }
11731
11732
11733 /* Display one line of the tool-bar of frame IT->f.
11734
11735 HEIGHT specifies the desired height of the tool-bar line.
11736 If the actual height of the glyph row is less than HEIGHT, the
11737 row's height is increased to HEIGHT, and the icons are centered
11738 vertically in the new height.
11739
11740 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11741 count a final empty row in case the tool-bar width exactly matches
11742 the window width.
11743 */
11744
11745 static void
11746 display_tool_bar_line (struct it *it, int height)
11747 {
11748 struct glyph_row *row = it->glyph_row;
11749 int max_x = it->last_visible_x;
11750 struct glyph *last;
11751
11752 prepare_desired_row (row);
11753 row->y = it->current_y;
11754
11755 /* Note that this isn't made use of if the face hasn't a box,
11756 so there's no need to check the face here. */
11757 it->start_of_box_run_p = 1;
11758
11759 while (it->current_x < max_x)
11760 {
11761 int x, n_glyphs_before, i, nglyphs;
11762 struct it it_before;
11763
11764 /* Get the next display element. */
11765 if (!get_next_display_element (it))
11766 {
11767 /* Don't count empty row if we are counting needed tool-bar lines. */
11768 if (height < 0 && !it->hpos)
11769 return;
11770 break;
11771 }
11772
11773 /* Produce glyphs. */
11774 n_glyphs_before = row->used[TEXT_AREA];
11775 it_before = *it;
11776
11777 PRODUCE_GLYPHS (it);
11778
11779 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11780 i = 0;
11781 x = it_before.current_x;
11782 while (i < nglyphs)
11783 {
11784 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11785
11786 if (x + glyph->pixel_width > max_x)
11787 {
11788 /* Glyph doesn't fit on line. Backtrack. */
11789 row->used[TEXT_AREA] = n_glyphs_before;
11790 *it = it_before;
11791 /* If this is the only glyph on this line, it will never fit on the
11792 tool-bar, so skip it. But ensure there is at least one glyph,
11793 so we don't accidentally disable the tool-bar. */
11794 if (n_glyphs_before == 0
11795 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11796 break;
11797 goto out;
11798 }
11799
11800 ++it->hpos;
11801 x += glyph->pixel_width;
11802 ++i;
11803 }
11804
11805 /* Stop at line end. */
11806 if (ITERATOR_AT_END_OF_LINE_P (it))
11807 break;
11808
11809 set_iterator_to_next (it, 1);
11810 }
11811
11812 out:;
11813
11814 row->displays_text_p = row->used[TEXT_AREA] != 0;
11815
11816 /* Use default face for the border below the tool bar.
11817
11818 FIXME: When auto-resize-tool-bars is grow-only, there is
11819 no additional border below the possibly empty tool-bar lines.
11820 So to make the extra empty lines look "normal", we have to
11821 use the tool-bar face for the border too. */
11822 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11823 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11824 it->face_id = DEFAULT_FACE_ID;
11825
11826 extend_face_to_end_of_line (it);
11827 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11828 last->right_box_line_p = 1;
11829 if (last == row->glyphs[TEXT_AREA])
11830 last->left_box_line_p = 1;
11831
11832 /* Make line the desired height and center it vertically. */
11833 if ((height -= it->max_ascent + it->max_descent) > 0)
11834 {
11835 /* Don't add more than one line height. */
11836 height %= FRAME_LINE_HEIGHT (it->f);
11837 it->max_ascent += height / 2;
11838 it->max_descent += (height + 1) / 2;
11839 }
11840
11841 compute_line_metrics (it);
11842
11843 /* If line is empty, make it occupy the rest of the tool-bar. */
11844 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11845 {
11846 row->height = row->phys_height = it->last_visible_y - row->y;
11847 row->visible_height = row->height;
11848 row->ascent = row->phys_ascent = 0;
11849 row->extra_line_spacing = 0;
11850 }
11851
11852 row->full_width_p = 1;
11853 row->continued_p = 0;
11854 row->truncated_on_left_p = 0;
11855 row->truncated_on_right_p = 0;
11856
11857 it->current_x = it->hpos = 0;
11858 it->current_y += row->height;
11859 ++it->vpos;
11860 ++it->glyph_row;
11861 }
11862
11863
11864 /* Max tool-bar height. */
11865
11866 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11867 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11868
11869 /* Value is the number of screen lines needed to make all tool-bar
11870 items of frame F visible. The number of actual rows needed is
11871 returned in *N_ROWS if non-NULL. */
11872
11873 static int
11874 tool_bar_lines_needed (struct frame *f, int *n_rows)
11875 {
11876 struct window *w = XWINDOW (f->tool_bar_window);
11877 struct it it;
11878 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11879 the desired matrix, so use (unused) mode-line row as temporary row to
11880 avoid destroying the first tool-bar row. */
11881 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11882
11883 /* Initialize an iterator for iteration over
11884 F->desired_tool_bar_string in the tool-bar window of frame F. */
11885 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11886 it.first_visible_x = 0;
11887 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11888 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11889 it.paragraph_embedding = L2R;
11890
11891 while (!ITERATOR_AT_END_P (&it))
11892 {
11893 clear_glyph_row (temp_row);
11894 it.glyph_row = temp_row;
11895 display_tool_bar_line (&it, -1);
11896 }
11897 clear_glyph_row (temp_row);
11898
11899 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11900 if (n_rows)
11901 *n_rows = it.vpos > 0 ? it.vpos : -1;
11902
11903 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11904 }
11905
11906
11907 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11908 0, 1, 0,
11909 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11910 If FRAME is nil or omitted, use the selected frame. */)
11911 (Lisp_Object frame)
11912 {
11913 struct frame *f = decode_any_frame (frame);
11914 struct window *w;
11915 int nlines = 0;
11916
11917 if (WINDOWP (f->tool_bar_window)
11918 && (w = XWINDOW (f->tool_bar_window),
11919 WINDOW_TOTAL_LINES (w) > 0))
11920 {
11921 update_tool_bar (f, 1);
11922 if (f->n_tool_bar_items)
11923 {
11924 build_desired_tool_bar_string (f);
11925 nlines = tool_bar_lines_needed (f, NULL);
11926 }
11927 }
11928
11929 return make_number (nlines);
11930 }
11931
11932
11933 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11934 height should be changed. */
11935
11936 static int
11937 redisplay_tool_bar (struct frame *f)
11938 {
11939 struct window *w;
11940 struct it it;
11941 struct glyph_row *row;
11942
11943 #if defined (USE_GTK) || defined (HAVE_NS)
11944 if (FRAME_EXTERNAL_TOOL_BAR (f))
11945 update_frame_tool_bar (f);
11946 return 0;
11947 #endif
11948
11949 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11950 do anything. This means you must start with tool-bar-lines
11951 non-zero to get the auto-sizing effect. Or in other words, you
11952 can turn off tool-bars by specifying tool-bar-lines zero. */
11953 if (!WINDOWP (f->tool_bar_window)
11954 || (w = XWINDOW (f->tool_bar_window),
11955 WINDOW_TOTAL_LINES (w) == 0))
11956 return 0;
11957
11958 /* Set up an iterator for the tool-bar window. */
11959 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11960 it.first_visible_x = 0;
11961 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11962 row = it.glyph_row;
11963
11964 /* Build a string that represents the contents of the tool-bar. */
11965 build_desired_tool_bar_string (f);
11966 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11967 /* FIXME: This should be controlled by a user option. But it
11968 doesn't make sense to have an R2L tool bar if the menu bar cannot
11969 be drawn also R2L, and making the menu bar R2L is tricky due
11970 toolkit-specific code that implements it. If an R2L tool bar is
11971 ever supported, display_tool_bar_line should also be augmented to
11972 call unproduce_glyphs like display_line and display_string
11973 do. */
11974 it.paragraph_embedding = L2R;
11975
11976 if (f->n_tool_bar_rows == 0)
11977 {
11978 int nlines;
11979
11980 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11981 nlines != WINDOW_TOTAL_LINES (w)))
11982 {
11983 Lisp_Object frame;
11984 int old_height = WINDOW_TOTAL_LINES (w);
11985
11986 XSETFRAME (frame, f);
11987 Fmodify_frame_parameters (frame,
11988 list1 (Fcons (Qtool_bar_lines,
11989 make_number (nlines))));
11990 if (WINDOW_TOTAL_LINES (w) != old_height)
11991 {
11992 clear_glyph_matrix (w->desired_matrix);
11993 f->fonts_changed = 1;
11994 return 1;
11995 }
11996 }
11997 }
11998
11999 /* Display as many lines as needed to display all tool-bar items. */
12000
12001 if (f->n_tool_bar_rows > 0)
12002 {
12003 int border, rows, height, extra;
12004
12005 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12006 border = XINT (Vtool_bar_border);
12007 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12008 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12009 else if (EQ (Vtool_bar_border, Qborder_width))
12010 border = f->border_width;
12011 else
12012 border = 0;
12013 if (border < 0)
12014 border = 0;
12015
12016 rows = f->n_tool_bar_rows;
12017 height = max (1, (it.last_visible_y - border) / rows);
12018 extra = it.last_visible_y - border - height * rows;
12019
12020 while (it.current_y < it.last_visible_y)
12021 {
12022 int h = 0;
12023 if (extra > 0 && rows-- > 0)
12024 {
12025 h = (extra + rows - 1) / rows;
12026 extra -= h;
12027 }
12028 display_tool_bar_line (&it, height + h);
12029 }
12030 }
12031 else
12032 {
12033 while (it.current_y < it.last_visible_y)
12034 display_tool_bar_line (&it, 0);
12035 }
12036
12037 /* It doesn't make much sense to try scrolling in the tool-bar
12038 window, so don't do it. */
12039 w->desired_matrix->no_scrolling_p = 1;
12040 w->must_be_updated_p = 1;
12041
12042 if (!NILP (Vauto_resize_tool_bars))
12043 {
12044 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12045 int change_height_p = 0;
12046
12047 /* If we couldn't display everything, change the tool-bar's
12048 height if there is room for more. */
12049 if (IT_STRING_CHARPOS (it) < it.end_charpos
12050 && it.current_y < max_tool_bar_height)
12051 change_height_p = 1;
12052
12053 row = it.glyph_row - 1;
12054
12055 /* If there are blank lines at the end, except for a partially
12056 visible blank line at the end that is smaller than
12057 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12058 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12059 && row->height >= FRAME_LINE_HEIGHT (f))
12060 change_height_p = 1;
12061
12062 /* If row displays tool-bar items, but is partially visible,
12063 change the tool-bar's height. */
12064 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12065 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12066 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12067 change_height_p = 1;
12068
12069 /* Resize windows as needed by changing the `tool-bar-lines'
12070 frame parameter. */
12071 if (change_height_p)
12072 {
12073 Lisp_Object frame;
12074 int old_height = WINDOW_TOTAL_LINES (w);
12075 int nrows;
12076 int nlines = tool_bar_lines_needed (f, &nrows);
12077
12078 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12079 && !f->minimize_tool_bar_window_p)
12080 ? (nlines > old_height)
12081 : (nlines != old_height));
12082 f->minimize_tool_bar_window_p = 0;
12083
12084 if (change_height_p)
12085 {
12086 XSETFRAME (frame, f);
12087 Fmodify_frame_parameters (frame,
12088 list1 (Fcons (Qtool_bar_lines,
12089 make_number (nlines))));
12090 if (WINDOW_TOTAL_LINES (w) != old_height)
12091 {
12092 clear_glyph_matrix (w->desired_matrix);
12093 f->n_tool_bar_rows = nrows;
12094 f->fonts_changed = 1;
12095 return 1;
12096 }
12097 }
12098 }
12099 }
12100
12101 f->minimize_tool_bar_window_p = 0;
12102 return 0;
12103 }
12104
12105
12106 /* Get information about the tool-bar item which is displayed in GLYPH
12107 on frame F. Return in *PROP_IDX the index where tool-bar item
12108 properties start in F->tool_bar_items. Value is zero if
12109 GLYPH doesn't display a tool-bar item. */
12110
12111 static int
12112 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12113 {
12114 Lisp_Object prop;
12115 int success_p;
12116 int charpos;
12117
12118 /* This function can be called asynchronously, which means we must
12119 exclude any possibility that Fget_text_property signals an
12120 error. */
12121 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12122 charpos = max (0, charpos);
12123
12124 /* Get the text property `menu-item' at pos. The value of that
12125 property is the start index of this item's properties in
12126 F->tool_bar_items. */
12127 prop = Fget_text_property (make_number (charpos),
12128 Qmenu_item, f->current_tool_bar_string);
12129 if (INTEGERP (prop))
12130 {
12131 *prop_idx = XINT (prop);
12132 success_p = 1;
12133 }
12134 else
12135 success_p = 0;
12136
12137 return success_p;
12138 }
12139
12140 \f
12141 /* Get information about the tool-bar item at position X/Y on frame F.
12142 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12143 the current matrix of the tool-bar window of F, or NULL if not
12144 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12145 item in F->tool_bar_items. Value is
12146
12147 -1 if X/Y is not on a tool-bar item
12148 0 if X/Y is on the same item that was highlighted before.
12149 1 otherwise. */
12150
12151 static int
12152 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12153 int *hpos, int *vpos, int *prop_idx)
12154 {
12155 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12156 struct window *w = XWINDOW (f->tool_bar_window);
12157 int area;
12158
12159 /* Find the glyph under X/Y. */
12160 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12161 if (*glyph == NULL)
12162 return -1;
12163
12164 /* Get the start of this tool-bar item's properties in
12165 f->tool_bar_items. */
12166 if (!tool_bar_item_info (f, *glyph, prop_idx))
12167 return -1;
12168
12169 /* Is mouse on the highlighted item? */
12170 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12171 && *vpos >= hlinfo->mouse_face_beg_row
12172 && *vpos <= hlinfo->mouse_face_end_row
12173 && (*vpos > hlinfo->mouse_face_beg_row
12174 || *hpos >= hlinfo->mouse_face_beg_col)
12175 && (*vpos < hlinfo->mouse_face_end_row
12176 || *hpos < hlinfo->mouse_face_end_col
12177 || hlinfo->mouse_face_past_end))
12178 return 0;
12179
12180 return 1;
12181 }
12182
12183
12184 /* EXPORT:
12185 Handle mouse button event on the tool-bar of frame F, at
12186 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12187 0 for button release. MODIFIERS is event modifiers for button
12188 release. */
12189
12190 void
12191 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12192 int modifiers)
12193 {
12194 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12195 struct window *w = XWINDOW (f->tool_bar_window);
12196 int hpos, vpos, prop_idx;
12197 struct glyph *glyph;
12198 Lisp_Object enabled_p;
12199 int ts;
12200
12201 /* If not on the highlighted tool-bar item, and mouse-highlight is
12202 non-nil, return. This is so we generate the tool-bar button
12203 click only when the mouse button is released on the same item as
12204 where it was pressed. However, when mouse-highlight is disabled,
12205 generate the click when the button is released regardless of the
12206 highlight, since tool-bar items are not highlighted in that
12207 case. */
12208 frame_to_window_pixel_xy (w, &x, &y);
12209 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12210 if (ts == -1
12211 || (ts != 0 && !NILP (Vmouse_highlight)))
12212 return;
12213
12214 /* When mouse-highlight is off, generate the click for the item
12215 where the button was pressed, disregarding where it was
12216 released. */
12217 if (NILP (Vmouse_highlight) && !down_p)
12218 prop_idx = last_tool_bar_item;
12219
12220 /* If item is disabled, do nothing. */
12221 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12222 if (NILP (enabled_p))
12223 return;
12224
12225 if (down_p)
12226 {
12227 /* Show item in pressed state. */
12228 if (!NILP (Vmouse_highlight))
12229 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12230 last_tool_bar_item = prop_idx;
12231 }
12232 else
12233 {
12234 Lisp_Object key, frame;
12235 struct input_event event;
12236 EVENT_INIT (event);
12237
12238 /* Show item in released state. */
12239 if (!NILP (Vmouse_highlight))
12240 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12241
12242 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12243
12244 XSETFRAME (frame, f);
12245 event.kind = TOOL_BAR_EVENT;
12246 event.frame_or_window = frame;
12247 event.arg = frame;
12248 kbd_buffer_store_event (&event);
12249
12250 event.kind = TOOL_BAR_EVENT;
12251 event.frame_or_window = frame;
12252 event.arg = key;
12253 event.modifiers = modifiers;
12254 kbd_buffer_store_event (&event);
12255 last_tool_bar_item = -1;
12256 }
12257 }
12258
12259
12260 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12261 tool-bar window-relative coordinates X/Y. Called from
12262 note_mouse_highlight. */
12263
12264 static void
12265 note_tool_bar_highlight (struct frame *f, int x, int y)
12266 {
12267 Lisp_Object window = f->tool_bar_window;
12268 struct window *w = XWINDOW (window);
12269 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12270 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12271 int hpos, vpos;
12272 struct glyph *glyph;
12273 struct glyph_row *row;
12274 int i;
12275 Lisp_Object enabled_p;
12276 int prop_idx;
12277 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12278 int mouse_down_p, rc;
12279
12280 /* Function note_mouse_highlight is called with negative X/Y
12281 values when mouse moves outside of the frame. */
12282 if (x <= 0 || y <= 0)
12283 {
12284 clear_mouse_face (hlinfo);
12285 return;
12286 }
12287
12288 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12289 if (rc < 0)
12290 {
12291 /* Not on tool-bar item. */
12292 clear_mouse_face (hlinfo);
12293 return;
12294 }
12295 else if (rc == 0)
12296 /* On same tool-bar item as before. */
12297 goto set_help_echo;
12298
12299 clear_mouse_face (hlinfo);
12300
12301 /* Mouse is down, but on different tool-bar item? */
12302 mouse_down_p = (dpyinfo->grabbed
12303 && f == last_mouse_frame
12304 && FRAME_LIVE_P (f));
12305 if (mouse_down_p
12306 && last_tool_bar_item != prop_idx)
12307 return;
12308
12309 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12310
12311 /* If tool-bar item is not enabled, don't highlight it. */
12312 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12313 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12314 {
12315 /* Compute the x-position of the glyph. In front and past the
12316 image is a space. We include this in the highlighted area. */
12317 row = MATRIX_ROW (w->current_matrix, vpos);
12318 for (i = x = 0; i < hpos; ++i)
12319 x += row->glyphs[TEXT_AREA][i].pixel_width;
12320
12321 /* Record this as the current active region. */
12322 hlinfo->mouse_face_beg_col = hpos;
12323 hlinfo->mouse_face_beg_row = vpos;
12324 hlinfo->mouse_face_beg_x = x;
12325 hlinfo->mouse_face_past_end = 0;
12326
12327 hlinfo->mouse_face_end_col = hpos + 1;
12328 hlinfo->mouse_face_end_row = vpos;
12329 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12330 hlinfo->mouse_face_window = window;
12331 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12332
12333 /* Display it as active. */
12334 show_mouse_face (hlinfo, draw);
12335 }
12336
12337 set_help_echo:
12338
12339 /* Set help_echo_string to a help string to display for this tool-bar item.
12340 XTread_socket does the rest. */
12341 help_echo_object = help_echo_window = Qnil;
12342 help_echo_pos = -1;
12343 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12344 if (NILP (help_echo_string))
12345 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12346 }
12347
12348 #endif /* HAVE_WINDOW_SYSTEM */
12349
12350
12351 \f
12352 /************************************************************************
12353 Horizontal scrolling
12354 ************************************************************************/
12355
12356 static int hscroll_window_tree (Lisp_Object);
12357 static int hscroll_windows (Lisp_Object);
12358
12359 /* For all leaf windows in the window tree rooted at WINDOW, set their
12360 hscroll value so that PT is (i) visible in the window, and (ii) so
12361 that it is not within a certain margin at the window's left and
12362 right border. Value is non-zero if any window's hscroll has been
12363 changed. */
12364
12365 static int
12366 hscroll_window_tree (Lisp_Object window)
12367 {
12368 int hscrolled_p = 0;
12369 int hscroll_relative_p = FLOATP (Vhscroll_step);
12370 int hscroll_step_abs = 0;
12371 double hscroll_step_rel = 0;
12372
12373 if (hscroll_relative_p)
12374 {
12375 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12376 if (hscroll_step_rel < 0)
12377 {
12378 hscroll_relative_p = 0;
12379 hscroll_step_abs = 0;
12380 }
12381 }
12382 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12383 {
12384 hscroll_step_abs = XINT (Vhscroll_step);
12385 if (hscroll_step_abs < 0)
12386 hscroll_step_abs = 0;
12387 }
12388 else
12389 hscroll_step_abs = 0;
12390
12391 while (WINDOWP (window))
12392 {
12393 struct window *w = XWINDOW (window);
12394
12395 if (WINDOWP (w->contents))
12396 hscrolled_p |= hscroll_window_tree (w->contents);
12397 else if (w->cursor.vpos >= 0)
12398 {
12399 int h_margin;
12400 int text_area_width;
12401 struct glyph_row *current_cursor_row
12402 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12403 struct glyph_row *desired_cursor_row
12404 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12405 struct glyph_row *cursor_row
12406 = (desired_cursor_row->enabled_p
12407 ? desired_cursor_row
12408 : current_cursor_row);
12409 int row_r2l_p = cursor_row->reversed_p;
12410
12411 text_area_width = window_box_width (w, TEXT_AREA);
12412
12413 /* Scroll when cursor is inside this scroll margin. */
12414 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12415
12416 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12417 /* For left-to-right rows, hscroll when cursor is either
12418 (i) inside the right hscroll margin, or (ii) if it is
12419 inside the left margin and the window is already
12420 hscrolled. */
12421 && ((!row_r2l_p
12422 && ((w->hscroll
12423 && w->cursor.x <= h_margin)
12424 || (cursor_row->enabled_p
12425 && cursor_row->truncated_on_right_p
12426 && (w->cursor.x >= text_area_width - h_margin))))
12427 /* For right-to-left rows, the logic is similar,
12428 except that rules for scrolling to left and right
12429 are reversed. E.g., if cursor.x <= h_margin, we
12430 need to hscroll "to the right" unconditionally,
12431 and that will scroll the screen to the left so as
12432 to reveal the next portion of the row. */
12433 || (row_r2l_p
12434 && ((cursor_row->enabled_p
12435 /* FIXME: It is confusing to set the
12436 truncated_on_right_p flag when R2L rows
12437 are actually truncated on the left. */
12438 && cursor_row->truncated_on_right_p
12439 && w->cursor.x <= h_margin)
12440 || (w->hscroll
12441 && (w->cursor.x >= text_area_width - h_margin))))))
12442 {
12443 struct it it;
12444 ptrdiff_t hscroll;
12445 struct buffer *saved_current_buffer;
12446 ptrdiff_t pt;
12447 int wanted_x;
12448
12449 /* Find point in a display of infinite width. */
12450 saved_current_buffer = current_buffer;
12451 current_buffer = XBUFFER (w->contents);
12452
12453 if (w == XWINDOW (selected_window))
12454 pt = PT;
12455 else
12456 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12457
12458 /* Move iterator to pt starting at cursor_row->start in
12459 a line with infinite width. */
12460 init_to_row_start (&it, w, cursor_row);
12461 it.last_visible_x = INFINITY;
12462 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12463 current_buffer = saved_current_buffer;
12464
12465 /* Position cursor in window. */
12466 if (!hscroll_relative_p && hscroll_step_abs == 0)
12467 hscroll = max (0, (it.current_x
12468 - (ITERATOR_AT_END_OF_LINE_P (&it)
12469 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12470 : (text_area_width / 2))))
12471 / FRAME_COLUMN_WIDTH (it.f);
12472 else if ((!row_r2l_p
12473 && w->cursor.x >= text_area_width - h_margin)
12474 || (row_r2l_p && w->cursor.x <= h_margin))
12475 {
12476 if (hscroll_relative_p)
12477 wanted_x = text_area_width * (1 - hscroll_step_rel)
12478 - h_margin;
12479 else
12480 wanted_x = text_area_width
12481 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12482 - h_margin;
12483 hscroll
12484 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12485 }
12486 else
12487 {
12488 if (hscroll_relative_p)
12489 wanted_x = text_area_width * hscroll_step_rel
12490 + h_margin;
12491 else
12492 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12493 + h_margin;
12494 hscroll
12495 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12496 }
12497 hscroll = max (hscroll, w->min_hscroll);
12498
12499 /* Don't prevent redisplay optimizations if hscroll
12500 hasn't changed, as it will unnecessarily slow down
12501 redisplay. */
12502 if (w->hscroll != hscroll)
12503 {
12504 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12505 w->hscroll = hscroll;
12506 hscrolled_p = 1;
12507 }
12508 }
12509 }
12510
12511 window = w->next;
12512 }
12513
12514 /* Value is non-zero if hscroll of any leaf window has been changed. */
12515 return hscrolled_p;
12516 }
12517
12518
12519 /* Set hscroll so that cursor is visible and not inside horizontal
12520 scroll margins for all windows in the tree rooted at WINDOW. See
12521 also hscroll_window_tree above. Value is non-zero if any window's
12522 hscroll has been changed. If it has, desired matrices on the frame
12523 of WINDOW are cleared. */
12524
12525 static int
12526 hscroll_windows (Lisp_Object window)
12527 {
12528 int hscrolled_p = hscroll_window_tree (window);
12529 if (hscrolled_p)
12530 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12531 return hscrolled_p;
12532 }
12533
12534
12535 \f
12536 /************************************************************************
12537 Redisplay
12538 ************************************************************************/
12539
12540 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12541 to a non-zero value. This is sometimes handy to have in a debugger
12542 session. */
12543
12544 #ifdef GLYPH_DEBUG
12545
12546 /* First and last unchanged row for try_window_id. */
12547
12548 static int debug_first_unchanged_at_end_vpos;
12549 static int debug_last_unchanged_at_beg_vpos;
12550
12551 /* Delta vpos and y. */
12552
12553 static int debug_dvpos, debug_dy;
12554
12555 /* Delta in characters and bytes for try_window_id. */
12556
12557 static ptrdiff_t debug_delta, debug_delta_bytes;
12558
12559 /* Values of window_end_pos and window_end_vpos at the end of
12560 try_window_id. */
12561
12562 static ptrdiff_t debug_end_vpos;
12563
12564 /* Append a string to W->desired_matrix->method. FMT is a printf
12565 format string. If trace_redisplay_p is non-zero also printf the
12566 resulting string to stderr. */
12567
12568 static void debug_method_add (struct window *, char const *, ...)
12569 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12570
12571 static void
12572 debug_method_add (struct window *w, char const *fmt, ...)
12573 {
12574 void *ptr = w;
12575 char *method = w->desired_matrix->method;
12576 int len = strlen (method);
12577 int size = sizeof w->desired_matrix->method;
12578 int remaining = size - len - 1;
12579 va_list ap;
12580
12581 if (len && remaining)
12582 {
12583 method[len] = '|';
12584 --remaining, ++len;
12585 }
12586
12587 va_start (ap, fmt);
12588 vsnprintf (method + len, remaining + 1, fmt, ap);
12589 va_end (ap);
12590
12591 if (trace_redisplay_p)
12592 fprintf (stderr, "%p (%s): %s\n",
12593 ptr,
12594 ((BUFFERP (w->contents)
12595 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12596 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12597 : "no buffer"),
12598 method + len);
12599 }
12600
12601 #endif /* GLYPH_DEBUG */
12602
12603
12604 /* Value is non-zero if all changes in window W, which displays
12605 current_buffer, are in the text between START and END. START is a
12606 buffer position, END is given as a distance from Z. Used in
12607 redisplay_internal for display optimization. */
12608
12609 static int
12610 text_outside_line_unchanged_p (struct window *w,
12611 ptrdiff_t start, ptrdiff_t end)
12612 {
12613 int unchanged_p = 1;
12614
12615 /* If text or overlays have changed, see where. */
12616 if (window_outdated (w))
12617 {
12618 /* Gap in the line? */
12619 if (GPT < start || Z - GPT < end)
12620 unchanged_p = 0;
12621
12622 /* Changes start in front of the line, or end after it? */
12623 if (unchanged_p
12624 && (BEG_UNCHANGED < start - 1
12625 || END_UNCHANGED < end))
12626 unchanged_p = 0;
12627
12628 /* If selective display, can't optimize if changes start at the
12629 beginning of the line. */
12630 if (unchanged_p
12631 && INTEGERP (BVAR (current_buffer, selective_display))
12632 && XINT (BVAR (current_buffer, selective_display)) > 0
12633 && (BEG_UNCHANGED < start || GPT <= start))
12634 unchanged_p = 0;
12635
12636 /* If there are overlays at the start or end of the line, these
12637 may have overlay strings with newlines in them. A change at
12638 START, for instance, may actually concern the display of such
12639 overlay strings as well, and they are displayed on different
12640 lines. So, quickly rule out this case. (For the future, it
12641 might be desirable to implement something more telling than
12642 just BEG/END_UNCHANGED.) */
12643 if (unchanged_p)
12644 {
12645 if (BEG + BEG_UNCHANGED == start
12646 && overlay_touches_p (start))
12647 unchanged_p = 0;
12648 if (END_UNCHANGED == end
12649 && overlay_touches_p (Z - end))
12650 unchanged_p = 0;
12651 }
12652
12653 /* Under bidi reordering, adding or deleting a character in the
12654 beginning of a paragraph, before the first strong directional
12655 character, can change the base direction of the paragraph (unless
12656 the buffer specifies a fixed paragraph direction), which will
12657 require to redisplay the whole paragraph. It might be worthwhile
12658 to find the paragraph limits and widen the range of redisplayed
12659 lines to that, but for now just give up this optimization. */
12660 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12661 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12662 unchanged_p = 0;
12663 }
12664
12665 return unchanged_p;
12666 }
12667
12668
12669 /* Do a frame update, taking possible shortcuts into account. This is
12670 the main external entry point for redisplay.
12671
12672 If the last redisplay displayed an echo area message and that message
12673 is no longer requested, we clear the echo area or bring back the
12674 mini-buffer if that is in use. */
12675
12676 void
12677 redisplay (void)
12678 {
12679 redisplay_internal ();
12680 }
12681
12682
12683 static Lisp_Object
12684 overlay_arrow_string_or_property (Lisp_Object var)
12685 {
12686 Lisp_Object val;
12687
12688 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12689 return val;
12690
12691 return Voverlay_arrow_string;
12692 }
12693
12694 /* Return 1 if there are any overlay-arrows in current_buffer. */
12695 static int
12696 overlay_arrow_in_current_buffer_p (void)
12697 {
12698 Lisp_Object vlist;
12699
12700 for (vlist = Voverlay_arrow_variable_list;
12701 CONSP (vlist);
12702 vlist = XCDR (vlist))
12703 {
12704 Lisp_Object var = XCAR (vlist);
12705 Lisp_Object val;
12706
12707 if (!SYMBOLP (var))
12708 continue;
12709 val = find_symbol_value (var);
12710 if (MARKERP (val)
12711 && current_buffer == XMARKER (val)->buffer)
12712 return 1;
12713 }
12714 return 0;
12715 }
12716
12717
12718 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12719 has changed. */
12720
12721 static int
12722 overlay_arrows_changed_p (void)
12723 {
12724 Lisp_Object vlist;
12725
12726 for (vlist = Voverlay_arrow_variable_list;
12727 CONSP (vlist);
12728 vlist = XCDR (vlist))
12729 {
12730 Lisp_Object var = XCAR (vlist);
12731 Lisp_Object val, pstr;
12732
12733 if (!SYMBOLP (var))
12734 continue;
12735 val = find_symbol_value (var);
12736 if (!MARKERP (val))
12737 continue;
12738 if (! EQ (COERCE_MARKER (val),
12739 Fget (var, Qlast_arrow_position))
12740 || ! (pstr = overlay_arrow_string_or_property (var),
12741 EQ (pstr, Fget (var, Qlast_arrow_string))))
12742 return 1;
12743 }
12744 return 0;
12745 }
12746
12747 /* Mark overlay arrows to be updated on next redisplay. */
12748
12749 static void
12750 update_overlay_arrows (int up_to_date)
12751 {
12752 Lisp_Object vlist;
12753
12754 for (vlist = Voverlay_arrow_variable_list;
12755 CONSP (vlist);
12756 vlist = XCDR (vlist))
12757 {
12758 Lisp_Object var = XCAR (vlist);
12759
12760 if (!SYMBOLP (var))
12761 continue;
12762
12763 if (up_to_date > 0)
12764 {
12765 Lisp_Object val = find_symbol_value (var);
12766 Fput (var, Qlast_arrow_position,
12767 COERCE_MARKER (val));
12768 Fput (var, Qlast_arrow_string,
12769 overlay_arrow_string_or_property (var));
12770 }
12771 else if (up_to_date < 0
12772 || !NILP (Fget (var, Qlast_arrow_position)))
12773 {
12774 Fput (var, Qlast_arrow_position, Qt);
12775 Fput (var, Qlast_arrow_string, Qt);
12776 }
12777 }
12778 }
12779
12780
12781 /* Return overlay arrow string to display at row.
12782 Return integer (bitmap number) for arrow bitmap in left fringe.
12783 Return nil if no overlay arrow. */
12784
12785 static Lisp_Object
12786 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12787 {
12788 Lisp_Object vlist;
12789
12790 for (vlist = Voverlay_arrow_variable_list;
12791 CONSP (vlist);
12792 vlist = XCDR (vlist))
12793 {
12794 Lisp_Object var = XCAR (vlist);
12795 Lisp_Object val;
12796
12797 if (!SYMBOLP (var))
12798 continue;
12799
12800 val = find_symbol_value (var);
12801
12802 if (MARKERP (val)
12803 && current_buffer == XMARKER (val)->buffer
12804 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12805 {
12806 if (FRAME_WINDOW_P (it->f)
12807 /* FIXME: if ROW->reversed_p is set, this should test
12808 the right fringe, not the left one. */
12809 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12810 {
12811 #ifdef HAVE_WINDOW_SYSTEM
12812 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12813 {
12814 int fringe_bitmap;
12815 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12816 return make_number (fringe_bitmap);
12817 }
12818 #endif
12819 return make_number (-1); /* Use default arrow bitmap. */
12820 }
12821 return overlay_arrow_string_or_property (var);
12822 }
12823 }
12824
12825 return Qnil;
12826 }
12827
12828 /* Return 1 if point moved out of or into a composition. Otherwise
12829 return 0. PREV_BUF and PREV_PT are the last point buffer and
12830 position. BUF and PT are the current point buffer and position. */
12831
12832 static int
12833 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12834 struct buffer *buf, ptrdiff_t pt)
12835 {
12836 ptrdiff_t start, end;
12837 Lisp_Object prop;
12838 Lisp_Object buffer;
12839
12840 XSETBUFFER (buffer, buf);
12841 /* Check a composition at the last point if point moved within the
12842 same buffer. */
12843 if (prev_buf == buf)
12844 {
12845 if (prev_pt == pt)
12846 /* Point didn't move. */
12847 return 0;
12848
12849 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12850 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12851 && composition_valid_p (start, end, prop)
12852 && start < prev_pt && end > prev_pt)
12853 /* The last point was within the composition. Return 1 iff
12854 point moved out of the composition. */
12855 return (pt <= start || pt >= end);
12856 }
12857
12858 /* Check a composition at the current point. */
12859 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12860 && find_composition (pt, -1, &start, &end, &prop, buffer)
12861 && composition_valid_p (start, end, prop)
12862 && start < pt && end > pt);
12863 }
12864
12865 /* Reconsider the clip changes of buffer which is displayed in W. */
12866
12867 static void
12868 reconsider_clip_changes (struct window *w)
12869 {
12870 struct buffer *b = XBUFFER (w->contents);
12871
12872 if (b->clip_changed
12873 && w->window_end_valid
12874 && w->current_matrix->buffer == b
12875 && w->current_matrix->zv == BUF_ZV (b)
12876 && w->current_matrix->begv == BUF_BEGV (b))
12877 b->clip_changed = 0;
12878
12879 /* If display wasn't paused, and W is not a tool bar window, see if
12880 point has been moved into or out of a composition. In that case,
12881 we set b->clip_changed to 1 to force updating the screen. If
12882 b->clip_changed has already been set to 1, we can skip this
12883 check. */
12884 if (!b->clip_changed && w->window_end_valid)
12885 {
12886 ptrdiff_t pt = (w == XWINDOW (selected_window)
12887 ? PT : marker_position (w->pointm));
12888
12889 if ((w->current_matrix->buffer != b || pt != w->last_point)
12890 && check_point_in_composition (w->current_matrix->buffer,
12891 w->last_point, b, pt))
12892 b->clip_changed = 1;
12893 }
12894 }
12895
12896 #define STOP_POLLING \
12897 do { if (! polling_stopped_here) stop_polling (); \
12898 polling_stopped_here = 1; } while (0)
12899
12900 #define RESUME_POLLING \
12901 do { if (polling_stopped_here) start_polling (); \
12902 polling_stopped_here = 0; } while (0)
12903
12904
12905 /* Perhaps in the future avoid recentering windows if it
12906 is not necessary; currently that causes some problems. */
12907
12908 static void
12909 redisplay_internal (void)
12910 {
12911 struct window *w = XWINDOW (selected_window);
12912 struct window *sw;
12913 struct frame *fr;
12914 int pending;
12915 bool must_finish = 0, match_p;
12916 struct text_pos tlbufpos, tlendpos;
12917 int number_of_visible_frames;
12918 ptrdiff_t count;
12919 struct frame *sf;
12920 int polling_stopped_here = 0;
12921 Lisp_Object tail, frame;
12922
12923 /* Non-zero means redisplay has to consider all windows on all
12924 frames. Zero means, only selected_window is considered. */
12925 int consider_all_windows_p;
12926
12927 /* Non-zero means redisplay has to redisplay the miniwindow. */
12928 int update_miniwindow_p = 0;
12929
12930 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12931
12932 /* No redisplay if running in batch mode or frame is not yet fully
12933 initialized, or redisplay is explicitly turned off by setting
12934 Vinhibit_redisplay. */
12935 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12936 || !NILP (Vinhibit_redisplay))
12937 return;
12938
12939 /* Don't examine these until after testing Vinhibit_redisplay.
12940 When Emacs is shutting down, perhaps because its connection to
12941 X has dropped, we should not look at them at all. */
12942 fr = XFRAME (w->frame);
12943 sf = SELECTED_FRAME ();
12944
12945 if (!fr->glyphs_initialized_p)
12946 return;
12947
12948 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12949 if (popup_activated ())
12950 return;
12951 #endif
12952
12953 /* I don't think this happens but let's be paranoid. */
12954 if (redisplaying_p)
12955 return;
12956
12957 /* Record a function that clears redisplaying_p
12958 when we leave this function. */
12959 count = SPECPDL_INDEX ();
12960 record_unwind_protect_void (unwind_redisplay);
12961 redisplaying_p = 1;
12962 specbind (Qinhibit_free_realized_faces, Qnil);
12963
12964 /* Record this function, so it appears on the profiler's backtraces. */
12965 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12966
12967 FOR_EACH_FRAME (tail, frame)
12968 XFRAME (frame)->already_hscrolled_p = 0;
12969
12970 retry:
12971 /* Remember the currently selected window. */
12972 sw = w;
12973
12974 pending = 0;
12975 last_escape_glyph_frame = NULL;
12976 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12977 last_glyphless_glyph_frame = NULL;
12978 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12979
12980 /* If face_change_count is non-zero, init_iterator will free all
12981 realized faces, which includes the faces referenced from current
12982 matrices. So, we can't reuse current matrices in this case. */
12983 if (face_change_count)
12984 ++windows_or_buffers_changed;
12985
12986 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12987 && FRAME_TTY (sf)->previous_frame != sf)
12988 {
12989 /* Since frames on a single ASCII terminal share the same
12990 display area, displaying a different frame means redisplay
12991 the whole thing. */
12992 windows_or_buffers_changed++;
12993 SET_FRAME_GARBAGED (sf);
12994 #ifndef DOS_NT
12995 set_tty_color_mode (FRAME_TTY (sf), sf);
12996 #endif
12997 FRAME_TTY (sf)->previous_frame = sf;
12998 }
12999
13000 /* Set the visible flags for all frames. Do this before checking for
13001 resized or garbaged frames; they want to know if their frames are
13002 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13003 number_of_visible_frames = 0;
13004
13005 FOR_EACH_FRAME (tail, frame)
13006 {
13007 struct frame *f = XFRAME (frame);
13008
13009 if (FRAME_VISIBLE_P (f))
13010 {
13011 ++number_of_visible_frames;
13012 /* Adjust matrices for visible frames only. */
13013 if (f->fonts_changed)
13014 {
13015 adjust_frame_glyphs (f);
13016 f->fonts_changed = 0;
13017 }
13018 /* If cursor type has been changed on the frame
13019 other than selected, consider all frames. */
13020 if (f != sf && f->cursor_type_changed)
13021 update_mode_lines++;
13022 }
13023 clear_desired_matrices (f);
13024 }
13025
13026 /* Notice any pending interrupt request to change frame size. */
13027 do_pending_window_change (1);
13028
13029 /* do_pending_window_change could change the selected_window due to
13030 frame resizing which makes the selected window too small. */
13031 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13032 sw = w;
13033
13034 /* Clear frames marked as garbaged. */
13035 clear_garbaged_frames ();
13036
13037 /* Build menubar and tool-bar items. */
13038 if (NILP (Vmemory_full))
13039 prepare_menu_bars ();
13040
13041 if (windows_or_buffers_changed)
13042 update_mode_lines++;
13043
13044 reconsider_clip_changes (w);
13045
13046 /* In most cases selected window displays current buffer. */
13047 match_p = XBUFFER (w->contents) == current_buffer;
13048 if (match_p)
13049 {
13050 ptrdiff_t count1;
13051
13052 /* Detect case that we need to write or remove a star in the mode line. */
13053 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13054 {
13055 w->update_mode_line = 1;
13056 if (buffer_shared_and_changed ())
13057 update_mode_lines++;
13058 }
13059
13060 /* Avoid invocation of point motion hooks by `current_column' below. */
13061 count1 = SPECPDL_INDEX ();
13062 specbind (Qinhibit_point_motion_hooks, Qt);
13063
13064 if (mode_line_update_needed (w))
13065 w->update_mode_line = 1;
13066
13067 unbind_to (count1, Qnil);
13068 }
13069
13070 consider_all_windows_p = (update_mode_lines
13071 || buffer_shared_and_changed ());
13072
13073 /* If specs for an arrow have changed, do thorough redisplay
13074 to ensure we remove any arrow that should no longer exist. */
13075 if (overlay_arrows_changed_p ())
13076 consider_all_windows_p = windows_or_buffers_changed = 1;
13077
13078 /* Normally the message* functions will have already displayed and
13079 updated the echo area, but the frame may have been trashed, or
13080 the update may have been preempted, so display the echo area
13081 again here. Checking message_cleared_p captures the case that
13082 the echo area should be cleared. */
13083 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13084 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13085 || (message_cleared_p
13086 && minibuf_level == 0
13087 /* If the mini-window is currently selected, this means the
13088 echo-area doesn't show through. */
13089 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13090 {
13091 int window_height_changed_p = echo_area_display (0);
13092
13093 if (message_cleared_p)
13094 update_miniwindow_p = 1;
13095
13096 must_finish = 1;
13097
13098 /* If we don't display the current message, don't clear the
13099 message_cleared_p flag, because, if we did, we wouldn't clear
13100 the echo area in the next redisplay which doesn't preserve
13101 the echo area. */
13102 if (!display_last_displayed_message_p)
13103 message_cleared_p = 0;
13104
13105 if (window_height_changed_p)
13106 {
13107 consider_all_windows_p = 1;
13108 ++update_mode_lines;
13109 ++windows_or_buffers_changed;
13110
13111 /* If window configuration was changed, frames may have been
13112 marked garbaged. Clear them or we will experience
13113 surprises wrt scrolling. */
13114 clear_garbaged_frames ();
13115 }
13116 }
13117 else if (EQ (selected_window, minibuf_window)
13118 && (current_buffer->clip_changed || window_outdated (w))
13119 && resize_mini_window (w, 0))
13120 {
13121 /* Resized active mini-window to fit the size of what it is
13122 showing if its contents might have changed. */
13123 must_finish = 1;
13124 /* FIXME: this causes all frames to be updated, which seems unnecessary
13125 since only the current frame needs to be considered. This function
13126 needs to be rewritten with two variables, consider_all_windows and
13127 consider_all_frames. */
13128 consider_all_windows_p = 1;
13129 ++windows_or_buffers_changed;
13130 ++update_mode_lines;
13131
13132 /* If window configuration was changed, frames may have been
13133 marked garbaged. Clear them or we will experience
13134 surprises wrt scrolling. */
13135 clear_garbaged_frames ();
13136 }
13137
13138 /* If showing the region, and mark has changed, we must redisplay
13139 the whole window. The assignment to this_line_start_pos prevents
13140 the optimization directly below this if-statement. */
13141 if (((!NILP (Vtransient_mark_mode)
13142 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13143 != (w->region_showing > 0))
13144 || (w->region_showing
13145 && w->region_showing
13146 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13147 CHARPOS (this_line_start_pos) = 0;
13148
13149 /* Optimize the case that only the line containing the cursor in the
13150 selected window has changed. Variables starting with this_ are
13151 set in display_line and record information about the line
13152 containing the cursor. */
13153 tlbufpos = this_line_start_pos;
13154 tlendpos = this_line_end_pos;
13155 if (!consider_all_windows_p
13156 && CHARPOS (tlbufpos) > 0
13157 && !w->update_mode_line
13158 && !current_buffer->clip_changed
13159 && !current_buffer->prevent_redisplay_optimizations_p
13160 && FRAME_VISIBLE_P (XFRAME (w->frame))
13161 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13162 && !XFRAME (w->frame)->cursor_type_changed
13163 /* Make sure recorded data applies to current buffer, etc. */
13164 && this_line_buffer == current_buffer
13165 && match_p
13166 && !w->force_start
13167 && !w->optional_new_start
13168 /* Point must be on the line that we have info recorded about. */
13169 && PT >= CHARPOS (tlbufpos)
13170 && PT <= Z - CHARPOS (tlendpos)
13171 /* All text outside that line, including its final newline,
13172 must be unchanged. */
13173 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13174 CHARPOS (tlendpos)))
13175 {
13176 if (CHARPOS (tlbufpos) > BEGV
13177 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13178 && (CHARPOS (tlbufpos) == ZV
13179 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13180 /* Former continuation line has disappeared by becoming empty. */
13181 goto cancel;
13182 else if (window_outdated (w) || MINI_WINDOW_P (w))
13183 {
13184 /* We have to handle the case of continuation around a
13185 wide-column character (see the comment in indent.c around
13186 line 1340).
13187
13188 For instance, in the following case:
13189
13190 -------- Insert --------
13191 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13192 J_I_ ==> J_I_ `^^' are cursors.
13193 ^^ ^^
13194 -------- --------
13195
13196 As we have to redraw the line above, we cannot use this
13197 optimization. */
13198
13199 struct it it;
13200 int line_height_before = this_line_pixel_height;
13201
13202 /* Note that start_display will handle the case that the
13203 line starting at tlbufpos is a continuation line. */
13204 start_display (&it, w, tlbufpos);
13205
13206 /* Implementation note: It this still necessary? */
13207 if (it.current_x != this_line_start_x)
13208 goto cancel;
13209
13210 TRACE ((stderr, "trying display optimization 1\n"));
13211 w->cursor.vpos = -1;
13212 overlay_arrow_seen = 0;
13213 it.vpos = this_line_vpos;
13214 it.current_y = this_line_y;
13215 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13216 display_line (&it);
13217
13218 /* If line contains point, is not continued,
13219 and ends at same distance from eob as before, we win. */
13220 if (w->cursor.vpos >= 0
13221 /* Line is not continued, otherwise this_line_start_pos
13222 would have been set to 0 in display_line. */
13223 && CHARPOS (this_line_start_pos)
13224 /* Line ends as before. */
13225 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13226 /* Line has same height as before. Otherwise other lines
13227 would have to be shifted up or down. */
13228 && this_line_pixel_height == line_height_before)
13229 {
13230 /* If this is not the window's last line, we must adjust
13231 the charstarts of the lines below. */
13232 if (it.current_y < it.last_visible_y)
13233 {
13234 struct glyph_row *row
13235 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13236 ptrdiff_t delta, delta_bytes;
13237
13238 /* We used to distinguish between two cases here,
13239 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13240 when the line ends in a newline or the end of the
13241 buffer's accessible portion. But both cases did
13242 the same, so they were collapsed. */
13243 delta = (Z
13244 - CHARPOS (tlendpos)
13245 - MATRIX_ROW_START_CHARPOS (row));
13246 delta_bytes = (Z_BYTE
13247 - BYTEPOS (tlendpos)
13248 - MATRIX_ROW_START_BYTEPOS (row));
13249
13250 increment_matrix_positions (w->current_matrix,
13251 this_line_vpos + 1,
13252 w->current_matrix->nrows,
13253 delta, delta_bytes);
13254 }
13255
13256 /* If this row displays text now but previously didn't,
13257 or vice versa, w->window_end_vpos may have to be
13258 adjusted. */
13259 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13260 {
13261 if (w->window_end_vpos < this_line_vpos)
13262 w->window_end_vpos = this_line_vpos;
13263 }
13264 else if (w->window_end_vpos == this_line_vpos
13265 && this_line_vpos > 0)
13266 w->window_end_vpos = this_line_vpos - 1;
13267 w->window_end_valid = 0;
13268
13269 /* Update hint: No need to try to scroll in update_window. */
13270 w->desired_matrix->no_scrolling_p = 1;
13271
13272 #ifdef GLYPH_DEBUG
13273 *w->desired_matrix->method = 0;
13274 debug_method_add (w, "optimization 1");
13275 #endif
13276 #ifdef HAVE_WINDOW_SYSTEM
13277 update_window_fringes (w, 0);
13278 #endif
13279 goto update;
13280 }
13281 else
13282 goto cancel;
13283 }
13284 else if (/* Cursor position hasn't changed. */
13285 PT == w->last_point
13286 /* Make sure the cursor was last displayed
13287 in this window. Otherwise we have to reposition it. */
13288 && 0 <= w->cursor.vpos
13289 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13290 {
13291 if (!must_finish)
13292 {
13293 do_pending_window_change (1);
13294 /* If selected_window changed, redisplay again. */
13295 if (WINDOWP (selected_window)
13296 && (w = XWINDOW (selected_window)) != sw)
13297 goto retry;
13298
13299 /* We used to always goto end_of_redisplay here, but this
13300 isn't enough if we have a blinking cursor. */
13301 if (w->cursor_off_p == w->last_cursor_off_p)
13302 goto end_of_redisplay;
13303 }
13304 goto update;
13305 }
13306 /* If highlighting the region, or if the cursor is in the echo area,
13307 then we can't just move the cursor. */
13308 else if (! (!NILP (Vtransient_mark_mode)
13309 && !NILP (BVAR (current_buffer, mark_active)))
13310 && (EQ (selected_window,
13311 BVAR (current_buffer, last_selected_window))
13312 || highlight_nonselected_windows)
13313 && !w->region_showing
13314 && NILP (Vshow_trailing_whitespace)
13315 && !cursor_in_echo_area)
13316 {
13317 struct it it;
13318 struct glyph_row *row;
13319
13320 /* Skip from tlbufpos to PT and see where it is. Note that
13321 PT may be in invisible text. If so, we will end at the
13322 next visible position. */
13323 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13324 NULL, DEFAULT_FACE_ID);
13325 it.current_x = this_line_start_x;
13326 it.current_y = this_line_y;
13327 it.vpos = this_line_vpos;
13328
13329 /* The call to move_it_to stops in front of PT, but
13330 moves over before-strings. */
13331 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13332
13333 if (it.vpos == this_line_vpos
13334 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13335 row->enabled_p))
13336 {
13337 eassert (this_line_vpos == it.vpos);
13338 eassert (this_line_y == it.current_y);
13339 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13340 #ifdef GLYPH_DEBUG
13341 *w->desired_matrix->method = 0;
13342 debug_method_add (w, "optimization 3");
13343 #endif
13344 goto update;
13345 }
13346 else
13347 goto cancel;
13348 }
13349
13350 cancel:
13351 /* Text changed drastically or point moved off of line. */
13352 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13353 }
13354
13355 CHARPOS (this_line_start_pos) = 0;
13356 consider_all_windows_p |= buffer_shared_and_changed ();
13357 ++clear_face_cache_count;
13358 #ifdef HAVE_WINDOW_SYSTEM
13359 ++clear_image_cache_count;
13360 #endif
13361
13362 /* Build desired matrices, and update the display. If
13363 consider_all_windows_p is non-zero, do it for all windows on all
13364 frames. Otherwise do it for selected_window, only. */
13365
13366 if (consider_all_windows_p)
13367 {
13368 FOR_EACH_FRAME (tail, frame)
13369 XFRAME (frame)->updated_p = 0;
13370
13371 FOR_EACH_FRAME (tail, frame)
13372 {
13373 struct frame *f = XFRAME (frame);
13374
13375 /* We don't have to do anything for unselected terminal
13376 frames. */
13377 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13378 && !EQ (FRAME_TTY (f)->top_frame, frame))
13379 continue;
13380
13381 retry_frame:
13382
13383 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13384 {
13385 /* Mark all the scroll bars to be removed; we'll redeem
13386 the ones we want when we redisplay their windows. */
13387 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13388 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13389
13390 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13391 redisplay_windows (FRAME_ROOT_WINDOW (f));
13392
13393 /* The X error handler may have deleted that frame. */
13394 if (!FRAME_LIVE_P (f))
13395 continue;
13396
13397 /* Any scroll bars which redisplay_windows should have
13398 nuked should now go away. */
13399 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13400 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13401
13402 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13403 {
13404 /* If fonts changed on visible frame, display again. */
13405 if (f->fonts_changed)
13406 {
13407 adjust_frame_glyphs (f);
13408 f->fonts_changed = 0;
13409 goto retry_frame;
13410 }
13411
13412 /* See if we have to hscroll. */
13413 if (!f->already_hscrolled_p)
13414 {
13415 f->already_hscrolled_p = 1;
13416 if (hscroll_windows (f->root_window))
13417 goto retry_frame;
13418 }
13419
13420 /* Prevent various kinds of signals during display
13421 update. stdio is not robust about handling
13422 signals, which can cause an apparent I/O
13423 error. */
13424 if (interrupt_input)
13425 unrequest_sigio ();
13426 STOP_POLLING;
13427
13428 /* Update the display. */
13429 set_window_update_flags (XWINDOW (f->root_window), 1);
13430 pending |= update_frame (f, 0, 0);
13431 f->cursor_type_changed = 0;
13432 f->updated_p = 1;
13433 }
13434 }
13435 }
13436
13437 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13438
13439 if (!pending)
13440 {
13441 /* Do the mark_window_display_accurate after all windows have
13442 been redisplayed because this call resets flags in buffers
13443 which are needed for proper redisplay. */
13444 FOR_EACH_FRAME (tail, frame)
13445 {
13446 struct frame *f = XFRAME (frame);
13447 if (f->updated_p)
13448 {
13449 mark_window_display_accurate (f->root_window, 1);
13450 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13451 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13452 }
13453 }
13454 }
13455 }
13456 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13457 {
13458 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13459 struct frame *mini_frame;
13460
13461 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13462 /* Use list_of_error, not Qerror, so that
13463 we catch only errors and don't run the debugger. */
13464 internal_condition_case_1 (redisplay_window_1, selected_window,
13465 list_of_error,
13466 redisplay_window_error);
13467 if (update_miniwindow_p)
13468 internal_condition_case_1 (redisplay_window_1, mini_window,
13469 list_of_error,
13470 redisplay_window_error);
13471
13472 /* Compare desired and current matrices, perform output. */
13473
13474 update:
13475 /* If fonts changed, display again. */
13476 if (sf->fonts_changed)
13477 goto retry;
13478
13479 /* Prevent various kinds of signals during display update.
13480 stdio is not robust about handling signals,
13481 which can cause an apparent I/O error. */
13482 if (interrupt_input)
13483 unrequest_sigio ();
13484 STOP_POLLING;
13485
13486 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13487 {
13488 if (hscroll_windows (selected_window))
13489 goto retry;
13490
13491 XWINDOW (selected_window)->must_be_updated_p = 1;
13492 pending = update_frame (sf, 0, 0);
13493 sf->cursor_type_changed = 0;
13494 }
13495
13496 /* We may have called echo_area_display at the top of this
13497 function. If the echo area is on another frame, that may
13498 have put text on a frame other than the selected one, so the
13499 above call to update_frame would not have caught it. Catch
13500 it here. */
13501 mini_window = FRAME_MINIBUF_WINDOW (sf);
13502 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13503
13504 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13505 {
13506 XWINDOW (mini_window)->must_be_updated_p = 1;
13507 pending |= update_frame (mini_frame, 0, 0);
13508 mini_frame->cursor_type_changed = 0;
13509 if (!pending && hscroll_windows (mini_window))
13510 goto retry;
13511 }
13512 }
13513
13514 /* If display was paused because of pending input, make sure we do a
13515 thorough update the next time. */
13516 if (pending)
13517 {
13518 /* Prevent the optimization at the beginning of
13519 redisplay_internal that tries a single-line update of the
13520 line containing the cursor in the selected window. */
13521 CHARPOS (this_line_start_pos) = 0;
13522
13523 /* Let the overlay arrow be updated the next time. */
13524 update_overlay_arrows (0);
13525
13526 /* If we pause after scrolling, some rows in the current
13527 matrices of some windows are not valid. */
13528 if (!WINDOW_FULL_WIDTH_P (w)
13529 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13530 update_mode_lines = 1;
13531 }
13532 else
13533 {
13534 if (!consider_all_windows_p)
13535 {
13536 /* This has already been done above if
13537 consider_all_windows_p is set. */
13538 mark_window_display_accurate_1 (w, 1);
13539
13540 /* Say overlay arrows are up to date. */
13541 update_overlay_arrows (1);
13542
13543 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13544 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13545 }
13546
13547 update_mode_lines = 0;
13548 windows_or_buffers_changed = 0;
13549 }
13550
13551 /* Start SIGIO interrupts coming again. Having them off during the
13552 code above makes it less likely one will discard output, but not
13553 impossible, since there might be stuff in the system buffer here.
13554 But it is much hairier to try to do anything about that. */
13555 if (interrupt_input)
13556 request_sigio ();
13557 RESUME_POLLING;
13558
13559 /* If a frame has become visible which was not before, redisplay
13560 again, so that we display it. Expose events for such a frame
13561 (which it gets when becoming visible) don't call the parts of
13562 redisplay constructing glyphs, so simply exposing a frame won't
13563 display anything in this case. So, we have to display these
13564 frames here explicitly. */
13565 if (!pending)
13566 {
13567 int new_count = 0;
13568
13569 FOR_EACH_FRAME (tail, frame)
13570 {
13571 int this_is_visible = 0;
13572
13573 if (XFRAME (frame)->visible)
13574 this_is_visible = 1;
13575
13576 if (this_is_visible)
13577 new_count++;
13578 }
13579
13580 if (new_count != number_of_visible_frames)
13581 windows_or_buffers_changed++;
13582 }
13583
13584 /* Change frame size now if a change is pending. */
13585 do_pending_window_change (1);
13586
13587 /* If we just did a pending size change, or have additional
13588 visible frames, or selected_window changed, redisplay again. */
13589 if ((windows_or_buffers_changed && !pending)
13590 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13591 goto retry;
13592
13593 /* Clear the face and image caches.
13594
13595 We used to do this only if consider_all_windows_p. But the cache
13596 needs to be cleared if a timer creates images in the current
13597 buffer (e.g. the test case in Bug#6230). */
13598
13599 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13600 {
13601 clear_face_cache (0);
13602 clear_face_cache_count = 0;
13603 }
13604
13605 #ifdef HAVE_WINDOW_SYSTEM
13606 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13607 {
13608 clear_image_caches (Qnil);
13609 clear_image_cache_count = 0;
13610 }
13611 #endif /* HAVE_WINDOW_SYSTEM */
13612
13613 end_of_redisplay:
13614 unbind_to (count, Qnil);
13615 RESUME_POLLING;
13616 }
13617
13618
13619 /* Redisplay, but leave alone any recent echo area message unless
13620 another message has been requested in its place.
13621
13622 This is useful in situations where you need to redisplay but no
13623 user action has occurred, making it inappropriate for the message
13624 area to be cleared. See tracking_off and
13625 wait_reading_process_output for examples of these situations.
13626
13627 FROM_WHERE is an integer saying from where this function was
13628 called. This is useful for debugging. */
13629
13630 void
13631 redisplay_preserve_echo_area (int from_where)
13632 {
13633 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13634
13635 if (!NILP (echo_area_buffer[1]))
13636 {
13637 /* We have a previously displayed message, but no current
13638 message. Redisplay the previous message. */
13639 display_last_displayed_message_p = 1;
13640 redisplay_internal ();
13641 display_last_displayed_message_p = 0;
13642 }
13643 else
13644 redisplay_internal ();
13645
13646 flush_frame (SELECTED_FRAME ());
13647 }
13648
13649
13650 /* Function registered with record_unwind_protect in redisplay_internal. */
13651
13652 static void
13653 unwind_redisplay (void)
13654 {
13655 redisplaying_p = 0;
13656 }
13657
13658
13659 /* Mark the display of leaf window W as accurate or inaccurate.
13660 If ACCURATE_P is non-zero mark display of W as accurate. If
13661 ACCURATE_P is zero, arrange for W to be redisplayed the next
13662 time redisplay_internal is called. */
13663
13664 static void
13665 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13666 {
13667 struct buffer *b = XBUFFER (w->contents);
13668
13669 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13670 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13671 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13672
13673 if (accurate_p)
13674 {
13675 b->clip_changed = 0;
13676 b->prevent_redisplay_optimizations_p = 0;
13677
13678 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13679 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13680 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13681 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13682
13683 w->current_matrix->buffer = b;
13684 w->current_matrix->begv = BUF_BEGV (b);
13685 w->current_matrix->zv = BUF_ZV (b);
13686
13687 w->last_cursor_vpos = w->cursor.vpos;
13688 w->last_cursor_off_p = w->cursor_off_p;
13689
13690 if (w == XWINDOW (selected_window))
13691 w->last_point = BUF_PT (b);
13692 else
13693 w->last_point = marker_position (w->pointm);
13694
13695 w->window_end_valid = 1;
13696 w->update_mode_line = 0;
13697 }
13698 }
13699
13700
13701 /* Mark the display of windows in the window tree rooted at WINDOW as
13702 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13703 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13704 be redisplayed the next time redisplay_internal is called. */
13705
13706 void
13707 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13708 {
13709 struct window *w;
13710
13711 for (; !NILP (window); window = w->next)
13712 {
13713 w = XWINDOW (window);
13714 if (WINDOWP (w->contents))
13715 mark_window_display_accurate (w->contents, accurate_p);
13716 else
13717 mark_window_display_accurate_1 (w, accurate_p);
13718 }
13719
13720 if (accurate_p)
13721 update_overlay_arrows (1);
13722 else
13723 /* Force a thorough redisplay the next time by setting
13724 last_arrow_position and last_arrow_string to t, which is
13725 unequal to any useful value of Voverlay_arrow_... */
13726 update_overlay_arrows (-1);
13727 }
13728
13729
13730 /* Return value in display table DP (Lisp_Char_Table *) for character
13731 C. Since a display table doesn't have any parent, we don't have to
13732 follow parent. Do not call this function directly but use the
13733 macro DISP_CHAR_VECTOR. */
13734
13735 Lisp_Object
13736 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13737 {
13738 Lisp_Object val;
13739
13740 if (ASCII_CHAR_P (c))
13741 {
13742 val = dp->ascii;
13743 if (SUB_CHAR_TABLE_P (val))
13744 val = XSUB_CHAR_TABLE (val)->contents[c];
13745 }
13746 else
13747 {
13748 Lisp_Object table;
13749
13750 XSETCHAR_TABLE (table, dp);
13751 val = char_table_ref (table, c);
13752 }
13753 if (NILP (val))
13754 val = dp->defalt;
13755 return val;
13756 }
13757
13758
13759 \f
13760 /***********************************************************************
13761 Window Redisplay
13762 ***********************************************************************/
13763
13764 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13765
13766 static void
13767 redisplay_windows (Lisp_Object window)
13768 {
13769 while (!NILP (window))
13770 {
13771 struct window *w = XWINDOW (window);
13772
13773 if (WINDOWP (w->contents))
13774 redisplay_windows (w->contents);
13775 else if (BUFFERP (w->contents))
13776 {
13777 displayed_buffer = XBUFFER (w->contents);
13778 /* Use list_of_error, not Qerror, so that
13779 we catch only errors and don't run the debugger. */
13780 internal_condition_case_1 (redisplay_window_0, window,
13781 list_of_error,
13782 redisplay_window_error);
13783 }
13784
13785 window = w->next;
13786 }
13787 }
13788
13789 static Lisp_Object
13790 redisplay_window_error (Lisp_Object ignore)
13791 {
13792 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13793 return Qnil;
13794 }
13795
13796 static Lisp_Object
13797 redisplay_window_0 (Lisp_Object window)
13798 {
13799 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13800 redisplay_window (window, 0);
13801 return Qnil;
13802 }
13803
13804 static Lisp_Object
13805 redisplay_window_1 (Lisp_Object window)
13806 {
13807 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13808 redisplay_window (window, 1);
13809 return Qnil;
13810 }
13811 \f
13812
13813 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13814 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13815 which positions recorded in ROW differ from current buffer
13816 positions.
13817
13818 Return 0 if cursor is not on this row, 1 otherwise. */
13819
13820 static int
13821 set_cursor_from_row (struct window *w, struct glyph_row *row,
13822 struct glyph_matrix *matrix,
13823 ptrdiff_t delta, ptrdiff_t delta_bytes,
13824 int dy, int dvpos)
13825 {
13826 struct glyph *glyph = row->glyphs[TEXT_AREA];
13827 struct glyph *end = glyph + row->used[TEXT_AREA];
13828 struct glyph *cursor = NULL;
13829 /* The last known character position in row. */
13830 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13831 int x = row->x;
13832 ptrdiff_t pt_old = PT - delta;
13833 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13834 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13835 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13836 /* A glyph beyond the edge of TEXT_AREA which we should never
13837 touch. */
13838 struct glyph *glyphs_end = end;
13839 /* Non-zero means we've found a match for cursor position, but that
13840 glyph has the avoid_cursor_p flag set. */
13841 int match_with_avoid_cursor = 0;
13842 /* Non-zero means we've seen at least one glyph that came from a
13843 display string. */
13844 int string_seen = 0;
13845 /* Largest and smallest buffer positions seen so far during scan of
13846 glyph row. */
13847 ptrdiff_t bpos_max = pos_before;
13848 ptrdiff_t bpos_min = pos_after;
13849 /* Last buffer position covered by an overlay string with an integer
13850 `cursor' property. */
13851 ptrdiff_t bpos_covered = 0;
13852 /* Non-zero means the display string on which to display the cursor
13853 comes from a text property, not from an overlay. */
13854 int string_from_text_prop = 0;
13855
13856 /* Don't even try doing anything if called for a mode-line or
13857 header-line row, since the rest of the code isn't prepared to
13858 deal with such calamities. */
13859 eassert (!row->mode_line_p);
13860 if (row->mode_line_p)
13861 return 0;
13862
13863 /* Skip over glyphs not having an object at the start and the end of
13864 the row. These are special glyphs like truncation marks on
13865 terminal frames. */
13866 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13867 {
13868 if (!row->reversed_p)
13869 {
13870 while (glyph < end
13871 && INTEGERP (glyph->object)
13872 && glyph->charpos < 0)
13873 {
13874 x += glyph->pixel_width;
13875 ++glyph;
13876 }
13877 while (end > glyph
13878 && INTEGERP ((end - 1)->object)
13879 /* CHARPOS is zero for blanks and stretch glyphs
13880 inserted by extend_face_to_end_of_line. */
13881 && (end - 1)->charpos <= 0)
13882 --end;
13883 glyph_before = glyph - 1;
13884 glyph_after = end;
13885 }
13886 else
13887 {
13888 struct glyph *g;
13889
13890 /* If the glyph row is reversed, we need to process it from back
13891 to front, so swap the edge pointers. */
13892 glyphs_end = end = glyph - 1;
13893 glyph += row->used[TEXT_AREA] - 1;
13894
13895 while (glyph > end + 1
13896 && INTEGERP (glyph->object)
13897 && glyph->charpos < 0)
13898 {
13899 --glyph;
13900 x -= glyph->pixel_width;
13901 }
13902 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13903 --glyph;
13904 /* By default, in reversed rows we put the cursor on the
13905 rightmost (first in the reading order) glyph. */
13906 for (g = end + 1; g < glyph; g++)
13907 x += g->pixel_width;
13908 while (end < glyph
13909 && INTEGERP ((end + 1)->object)
13910 && (end + 1)->charpos <= 0)
13911 ++end;
13912 glyph_before = glyph + 1;
13913 glyph_after = end;
13914 }
13915 }
13916 else if (row->reversed_p)
13917 {
13918 /* In R2L rows that don't display text, put the cursor on the
13919 rightmost glyph. Case in point: an empty last line that is
13920 part of an R2L paragraph. */
13921 cursor = end - 1;
13922 /* Avoid placing the cursor on the last glyph of the row, where
13923 on terminal frames we hold the vertical border between
13924 adjacent windows. */
13925 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13926 && !WINDOW_RIGHTMOST_P (w)
13927 && cursor == row->glyphs[LAST_AREA] - 1)
13928 cursor--;
13929 x = -1; /* will be computed below, at label compute_x */
13930 }
13931
13932 /* Step 1: Try to find the glyph whose character position
13933 corresponds to point. If that's not possible, find 2 glyphs
13934 whose character positions are the closest to point, one before
13935 point, the other after it. */
13936 if (!row->reversed_p)
13937 while (/* not marched to end of glyph row */
13938 glyph < end
13939 /* glyph was not inserted by redisplay for internal purposes */
13940 && !INTEGERP (glyph->object))
13941 {
13942 if (BUFFERP (glyph->object))
13943 {
13944 ptrdiff_t dpos = glyph->charpos - pt_old;
13945
13946 if (glyph->charpos > bpos_max)
13947 bpos_max = glyph->charpos;
13948 if (glyph->charpos < bpos_min)
13949 bpos_min = glyph->charpos;
13950 if (!glyph->avoid_cursor_p)
13951 {
13952 /* If we hit point, we've found the glyph on which to
13953 display the cursor. */
13954 if (dpos == 0)
13955 {
13956 match_with_avoid_cursor = 0;
13957 break;
13958 }
13959 /* See if we've found a better approximation to
13960 POS_BEFORE or to POS_AFTER. */
13961 if (0 > dpos && dpos > pos_before - pt_old)
13962 {
13963 pos_before = glyph->charpos;
13964 glyph_before = glyph;
13965 }
13966 else if (0 < dpos && dpos < pos_after - pt_old)
13967 {
13968 pos_after = glyph->charpos;
13969 glyph_after = glyph;
13970 }
13971 }
13972 else if (dpos == 0)
13973 match_with_avoid_cursor = 1;
13974 }
13975 else if (STRINGP (glyph->object))
13976 {
13977 Lisp_Object chprop;
13978 ptrdiff_t glyph_pos = glyph->charpos;
13979
13980 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13981 glyph->object);
13982 if (!NILP (chprop))
13983 {
13984 /* If the string came from a `display' text property,
13985 look up the buffer position of that property and
13986 use that position to update bpos_max, as if we
13987 actually saw such a position in one of the row's
13988 glyphs. This helps with supporting integer values
13989 of `cursor' property on the display string in
13990 situations where most or all of the row's buffer
13991 text is completely covered by display properties,
13992 so that no glyph with valid buffer positions is
13993 ever seen in the row. */
13994 ptrdiff_t prop_pos =
13995 string_buffer_position_lim (glyph->object, pos_before,
13996 pos_after, 0);
13997
13998 if (prop_pos >= pos_before)
13999 bpos_max = prop_pos - 1;
14000 }
14001 if (INTEGERP (chprop))
14002 {
14003 bpos_covered = bpos_max + XINT (chprop);
14004 /* If the `cursor' property covers buffer positions up
14005 to and including point, we should display cursor on
14006 this glyph. Note that, if a `cursor' property on one
14007 of the string's characters has an integer value, we
14008 will break out of the loop below _before_ we get to
14009 the position match above. IOW, integer values of
14010 the `cursor' property override the "exact match for
14011 point" strategy of positioning the cursor. */
14012 /* Implementation note: bpos_max == pt_old when, e.g.,
14013 we are in an empty line, where bpos_max is set to
14014 MATRIX_ROW_START_CHARPOS, see above. */
14015 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14016 {
14017 cursor = glyph;
14018 break;
14019 }
14020 }
14021
14022 string_seen = 1;
14023 }
14024 x += glyph->pixel_width;
14025 ++glyph;
14026 }
14027 else if (glyph > end) /* row is reversed */
14028 while (!INTEGERP (glyph->object))
14029 {
14030 if (BUFFERP (glyph->object))
14031 {
14032 ptrdiff_t dpos = glyph->charpos - pt_old;
14033
14034 if (glyph->charpos > bpos_max)
14035 bpos_max = glyph->charpos;
14036 if (glyph->charpos < bpos_min)
14037 bpos_min = glyph->charpos;
14038 if (!glyph->avoid_cursor_p)
14039 {
14040 if (dpos == 0)
14041 {
14042 match_with_avoid_cursor = 0;
14043 break;
14044 }
14045 if (0 > dpos && dpos > pos_before - pt_old)
14046 {
14047 pos_before = glyph->charpos;
14048 glyph_before = glyph;
14049 }
14050 else if (0 < dpos && dpos < pos_after - pt_old)
14051 {
14052 pos_after = glyph->charpos;
14053 glyph_after = glyph;
14054 }
14055 }
14056 else if (dpos == 0)
14057 match_with_avoid_cursor = 1;
14058 }
14059 else if (STRINGP (glyph->object))
14060 {
14061 Lisp_Object chprop;
14062 ptrdiff_t glyph_pos = glyph->charpos;
14063
14064 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14065 glyph->object);
14066 if (!NILP (chprop))
14067 {
14068 ptrdiff_t prop_pos =
14069 string_buffer_position_lim (glyph->object, pos_before,
14070 pos_after, 0);
14071
14072 if (prop_pos >= pos_before)
14073 bpos_max = prop_pos - 1;
14074 }
14075 if (INTEGERP (chprop))
14076 {
14077 bpos_covered = bpos_max + XINT (chprop);
14078 /* If the `cursor' property covers buffer positions up
14079 to and including point, we should display cursor on
14080 this glyph. */
14081 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14082 {
14083 cursor = glyph;
14084 break;
14085 }
14086 }
14087 string_seen = 1;
14088 }
14089 --glyph;
14090 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14091 {
14092 x--; /* can't use any pixel_width */
14093 break;
14094 }
14095 x -= glyph->pixel_width;
14096 }
14097
14098 /* Step 2: If we didn't find an exact match for point, we need to
14099 look for a proper place to put the cursor among glyphs between
14100 GLYPH_BEFORE and GLYPH_AFTER. */
14101 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14102 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14103 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14104 {
14105 /* An empty line has a single glyph whose OBJECT is zero and
14106 whose CHARPOS is the position of a newline on that line.
14107 Note that on a TTY, there are more glyphs after that, which
14108 were produced by extend_face_to_end_of_line, but their
14109 CHARPOS is zero or negative. */
14110 int empty_line_p =
14111 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14112 && INTEGERP (glyph->object) && glyph->charpos > 0
14113 /* On a TTY, continued and truncated rows also have a glyph at
14114 their end whose OBJECT is zero and whose CHARPOS is
14115 positive (the continuation and truncation glyphs), but such
14116 rows are obviously not "empty". */
14117 && !(row->continued_p || row->truncated_on_right_p);
14118
14119 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14120 {
14121 ptrdiff_t ellipsis_pos;
14122
14123 /* Scan back over the ellipsis glyphs. */
14124 if (!row->reversed_p)
14125 {
14126 ellipsis_pos = (glyph - 1)->charpos;
14127 while (glyph > row->glyphs[TEXT_AREA]
14128 && (glyph - 1)->charpos == ellipsis_pos)
14129 glyph--, x -= glyph->pixel_width;
14130 /* That loop always goes one position too far, including
14131 the glyph before the ellipsis. So scan forward over
14132 that one. */
14133 x += glyph->pixel_width;
14134 glyph++;
14135 }
14136 else /* row is reversed */
14137 {
14138 ellipsis_pos = (glyph + 1)->charpos;
14139 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14140 && (glyph + 1)->charpos == ellipsis_pos)
14141 glyph++, x += glyph->pixel_width;
14142 x -= glyph->pixel_width;
14143 glyph--;
14144 }
14145 }
14146 else if (match_with_avoid_cursor)
14147 {
14148 cursor = glyph_after;
14149 x = -1;
14150 }
14151 else if (string_seen)
14152 {
14153 int incr = row->reversed_p ? -1 : +1;
14154
14155 /* Need to find the glyph that came out of a string which is
14156 present at point. That glyph is somewhere between
14157 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14158 positioned between POS_BEFORE and POS_AFTER in the
14159 buffer. */
14160 struct glyph *start, *stop;
14161 ptrdiff_t pos = pos_before;
14162
14163 x = -1;
14164
14165 /* If the row ends in a newline from a display string,
14166 reordering could have moved the glyphs belonging to the
14167 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14168 in this case we extend the search to the last glyph in
14169 the row that was not inserted by redisplay. */
14170 if (row->ends_in_newline_from_string_p)
14171 {
14172 glyph_after = end;
14173 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14174 }
14175
14176 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14177 correspond to POS_BEFORE and POS_AFTER, respectively. We
14178 need START and STOP in the order that corresponds to the
14179 row's direction as given by its reversed_p flag. If the
14180 directionality of characters between POS_BEFORE and
14181 POS_AFTER is the opposite of the row's base direction,
14182 these characters will have been reordered for display,
14183 and we need to reverse START and STOP. */
14184 if (!row->reversed_p)
14185 {
14186 start = min (glyph_before, glyph_after);
14187 stop = max (glyph_before, glyph_after);
14188 }
14189 else
14190 {
14191 start = max (glyph_before, glyph_after);
14192 stop = min (glyph_before, glyph_after);
14193 }
14194 for (glyph = start + incr;
14195 row->reversed_p ? glyph > stop : glyph < stop; )
14196 {
14197
14198 /* Any glyphs that come from the buffer are here because
14199 of bidi reordering. Skip them, and only pay
14200 attention to glyphs that came from some string. */
14201 if (STRINGP (glyph->object))
14202 {
14203 Lisp_Object str;
14204 ptrdiff_t tem;
14205 /* If the display property covers the newline, we
14206 need to search for it one position farther. */
14207 ptrdiff_t lim = pos_after
14208 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14209
14210 string_from_text_prop = 0;
14211 str = glyph->object;
14212 tem = string_buffer_position_lim (str, pos, lim, 0);
14213 if (tem == 0 /* from overlay */
14214 || pos <= tem)
14215 {
14216 /* If the string from which this glyph came is
14217 found in the buffer at point, or at position
14218 that is closer to point than pos_after, then
14219 we've found the glyph we've been looking for.
14220 If it comes from an overlay (tem == 0), and
14221 it has the `cursor' property on one of its
14222 glyphs, record that glyph as a candidate for
14223 displaying the cursor. (As in the
14224 unidirectional version, we will display the
14225 cursor on the last candidate we find.) */
14226 if (tem == 0
14227 || tem == pt_old
14228 || (tem - pt_old > 0 && tem < pos_after))
14229 {
14230 /* The glyphs from this string could have
14231 been reordered. Find the one with the
14232 smallest string position. Or there could
14233 be a character in the string with the
14234 `cursor' property, which means display
14235 cursor on that character's glyph. */
14236 ptrdiff_t strpos = glyph->charpos;
14237
14238 if (tem)
14239 {
14240 cursor = glyph;
14241 string_from_text_prop = 1;
14242 }
14243 for ( ;
14244 (row->reversed_p ? glyph > stop : glyph < stop)
14245 && EQ (glyph->object, str);
14246 glyph += incr)
14247 {
14248 Lisp_Object cprop;
14249 ptrdiff_t gpos = glyph->charpos;
14250
14251 cprop = Fget_char_property (make_number (gpos),
14252 Qcursor,
14253 glyph->object);
14254 if (!NILP (cprop))
14255 {
14256 cursor = glyph;
14257 break;
14258 }
14259 if (tem && glyph->charpos < strpos)
14260 {
14261 strpos = glyph->charpos;
14262 cursor = glyph;
14263 }
14264 }
14265
14266 if (tem == pt_old
14267 || (tem - pt_old > 0 && tem < pos_after))
14268 goto compute_x;
14269 }
14270 if (tem)
14271 pos = tem + 1; /* don't find previous instances */
14272 }
14273 /* This string is not what we want; skip all of the
14274 glyphs that came from it. */
14275 while ((row->reversed_p ? glyph > stop : glyph < stop)
14276 && EQ (glyph->object, str))
14277 glyph += incr;
14278 }
14279 else
14280 glyph += incr;
14281 }
14282
14283 /* If we reached the end of the line, and END was from a string,
14284 the cursor is not on this line. */
14285 if (cursor == NULL
14286 && (row->reversed_p ? glyph <= end : glyph >= end)
14287 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14288 && STRINGP (end->object)
14289 && row->continued_p)
14290 return 0;
14291 }
14292 /* A truncated row may not include PT among its character positions.
14293 Setting the cursor inside the scroll margin will trigger
14294 recalculation of hscroll in hscroll_window_tree. But if a
14295 display string covers point, defer to the string-handling
14296 code below to figure this out. */
14297 else if (row->truncated_on_left_p && pt_old < bpos_min)
14298 {
14299 cursor = glyph_before;
14300 x = -1;
14301 }
14302 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14303 /* Zero-width characters produce no glyphs. */
14304 || (!empty_line_p
14305 && (row->reversed_p
14306 ? glyph_after > glyphs_end
14307 : glyph_after < glyphs_end)))
14308 {
14309 cursor = glyph_after;
14310 x = -1;
14311 }
14312 }
14313
14314 compute_x:
14315 if (cursor != NULL)
14316 glyph = cursor;
14317 else if (glyph == glyphs_end
14318 && pos_before == pos_after
14319 && STRINGP ((row->reversed_p
14320 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14321 : row->glyphs[TEXT_AREA])->object))
14322 {
14323 /* If all the glyphs of this row came from strings, put the
14324 cursor on the first glyph of the row. This avoids having the
14325 cursor outside of the text area in this very rare and hard
14326 use case. */
14327 glyph =
14328 row->reversed_p
14329 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14330 : row->glyphs[TEXT_AREA];
14331 }
14332 if (x < 0)
14333 {
14334 struct glyph *g;
14335
14336 /* Need to compute x that corresponds to GLYPH. */
14337 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14338 {
14339 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14340 emacs_abort ();
14341 x += g->pixel_width;
14342 }
14343 }
14344
14345 /* ROW could be part of a continued line, which, under bidi
14346 reordering, might have other rows whose start and end charpos
14347 occlude point. Only set w->cursor if we found a better
14348 approximation to the cursor position than we have from previously
14349 examined candidate rows belonging to the same continued line. */
14350 if (/* we already have a candidate row */
14351 w->cursor.vpos >= 0
14352 /* that candidate is not the row we are processing */
14353 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14354 /* Make sure cursor.vpos specifies a row whose start and end
14355 charpos occlude point, and it is valid candidate for being a
14356 cursor-row. This is because some callers of this function
14357 leave cursor.vpos at the row where the cursor was displayed
14358 during the last redisplay cycle. */
14359 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14360 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14361 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14362 {
14363 struct glyph *g1 =
14364 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14365
14366 /* Don't consider glyphs that are outside TEXT_AREA. */
14367 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14368 return 0;
14369 /* Keep the candidate whose buffer position is the closest to
14370 point or has the `cursor' property. */
14371 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14372 w->cursor.hpos >= 0
14373 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14374 && ((BUFFERP (g1->object)
14375 && (g1->charpos == pt_old /* an exact match always wins */
14376 || (BUFFERP (glyph->object)
14377 && eabs (g1->charpos - pt_old)
14378 < eabs (glyph->charpos - pt_old))))
14379 /* previous candidate is a glyph from a string that has
14380 a non-nil `cursor' property */
14381 || (STRINGP (g1->object)
14382 && (!NILP (Fget_char_property (make_number (g1->charpos),
14383 Qcursor, g1->object))
14384 /* previous candidate is from the same display
14385 string as this one, and the display string
14386 came from a text property */
14387 || (EQ (g1->object, glyph->object)
14388 && string_from_text_prop)
14389 /* this candidate is from newline and its
14390 position is not an exact match */
14391 || (INTEGERP (glyph->object)
14392 && glyph->charpos != pt_old)))))
14393 return 0;
14394 /* If this candidate gives an exact match, use that. */
14395 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14396 /* If this candidate is a glyph created for the
14397 terminating newline of a line, and point is on that
14398 newline, it wins because it's an exact match. */
14399 || (!row->continued_p
14400 && INTEGERP (glyph->object)
14401 && glyph->charpos == 0
14402 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14403 /* Otherwise, keep the candidate that comes from a row
14404 spanning less buffer positions. This may win when one or
14405 both candidate positions are on glyphs that came from
14406 display strings, for which we cannot compare buffer
14407 positions. */
14408 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14409 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14410 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14411 return 0;
14412 }
14413 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14414 w->cursor.x = x;
14415 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14416 w->cursor.y = row->y + dy;
14417
14418 if (w == XWINDOW (selected_window))
14419 {
14420 if (!row->continued_p
14421 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14422 && row->x == 0)
14423 {
14424 this_line_buffer = XBUFFER (w->contents);
14425
14426 CHARPOS (this_line_start_pos)
14427 = MATRIX_ROW_START_CHARPOS (row) + delta;
14428 BYTEPOS (this_line_start_pos)
14429 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14430
14431 CHARPOS (this_line_end_pos)
14432 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14433 BYTEPOS (this_line_end_pos)
14434 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14435
14436 this_line_y = w->cursor.y;
14437 this_line_pixel_height = row->height;
14438 this_line_vpos = w->cursor.vpos;
14439 this_line_start_x = row->x;
14440 }
14441 else
14442 CHARPOS (this_line_start_pos) = 0;
14443 }
14444
14445 return 1;
14446 }
14447
14448
14449 /* Run window scroll functions, if any, for WINDOW with new window
14450 start STARTP. Sets the window start of WINDOW to that position.
14451
14452 We assume that the window's buffer is really current. */
14453
14454 static struct text_pos
14455 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14456 {
14457 struct window *w = XWINDOW (window);
14458 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14459
14460 eassert (current_buffer == XBUFFER (w->contents));
14461
14462 if (!NILP (Vwindow_scroll_functions))
14463 {
14464 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14465 make_number (CHARPOS (startp)));
14466 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14467 /* In case the hook functions switch buffers. */
14468 set_buffer_internal (XBUFFER (w->contents));
14469 }
14470
14471 return startp;
14472 }
14473
14474
14475 /* Make sure the line containing the cursor is fully visible.
14476 A value of 1 means there is nothing to be done.
14477 (Either the line is fully visible, or it cannot be made so,
14478 or we cannot tell.)
14479
14480 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14481 is higher than window.
14482
14483 A value of 0 means the caller should do scrolling
14484 as if point had gone off the screen. */
14485
14486 static int
14487 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14488 {
14489 struct glyph_matrix *matrix;
14490 struct glyph_row *row;
14491 int window_height;
14492
14493 if (!make_cursor_line_fully_visible_p)
14494 return 1;
14495
14496 /* It's not always possible to find the cursor, e.g, when a window
14497 is full of overlay strings. Don't do anything in that case. */
14498 if (w->cursor.vpos < 0)
14499 return 1;
14500
14501 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14502 row = MATRIX_ROW (matrix, w->cursor.vpos);
14503
14504 /* If the cursor row is not partially visible, there's nothing to do. */
14505 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14506 return 1;
14507
14508 /* If the row the cursor is in is taller than the window's height,
14509 it's not clear what to do, so do nothing. */
14510 window_height = window_box_height (w);
14511 if (row->height >= window_height)
14512 {
14513 if (!force_p || MINI_WINDOW_P (w)
14514 || w->vscroll || w->cursor.vpos == 0)
14515 return 1;
14516 }
14517 return 0;
14518 }
14519
14520
14521 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14522 non-zero means only WINDOW is redisplayed in redisplay_internal.
14523 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14524 in redisplay_window to bring a partially visible line into view in
14525 the case that only the cursor has moved.
14526
14527 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14528 last screen line's vertical height extends past the end of the screen.
14529
14530 Value is
14531
14532 1 if scrolling succeeded
14533
14534 0 if scrolling didn't find point.
14535
14536 -1 if new fonts have been loaded so that we must interrupt
14537 redisplay, adjust glyph matrices, and try again. */
14538
14539 enum
14540 {
14541 SCROLLING_SUCCESS,
14542 SCROLLING_FAILED,
14543 SCROLLING_NEED_LARGER_MATRICES
14544 };
14545
14546 /* If scroll-conservatively is more than this, never recenter.
14547
14548 If you change this, don't forget to update the doc string of
14549 `scroll-conservatively' and the Emacs manual. */
14550 #define SCROLL_LIMIT 100
14551
14552 static int
14553 try_scrolling (Lisp_Object window, int just_this_one_p,
14554 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14555 int temp_scroll_step, int last_line_misfit)
14556 {
14557 struct window *w = XWINDOW (window);
14558 struct frame *f = XFRAME (w->frame);
14559 struct text_pos pos, startp;
14560 struct it it;
14561 int this_scroll_margin, scroll_max, rc, height;
14562 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14563 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14564 Lisp_Object aggressive;
14565 /* We will never try scrolling more than this number of lines. */
14566 int scroll_limit = SCROLL_LIMIT;
14567 int frame_line_height = default_line_pixel_height (w);
14568 int window_total_lines
14569 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14570
14571 #ifdef GLYPH_DEBUG
14572 debug_method_add (w, "try_scrolling");
14573 #endif
14574
14575 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14576
14577 /* Compute scroll margin height in pixels. We scroll when point is
14578 within this distance from the top or bottom of the window. */
14579 if (scroll_margin > 0)
14580 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14581 * frame_line_height;
14582 else
14583 this_scroll_margin = 0;
14584
14585 /* Force arg_scroll_conservatively to have a reasonable value, to
14586 avoid scrolling too far away with slow move_it_* functions. Note
14587 that the user can supply scroll-conservatively equal to
14588 `most-positive-fixnum', which can be larger than INT_MAX. */
14589 if (arg_scroll_conservatively > scroll_limit)
14590 {
14591 arg_scroll_conservatively = scroll_limit + 1;
14592 scroll_max = scroll_limit * frame_line_height;
14593 }
14594 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14595 /* Compute how much we should try to scroll maximally to bring
14596 point into view. */
14597 scroll_max = (max (scroll_step,
14598 max (arg_scroll_conservatively, temp_scroll_step))
14599 * frame_line_height);
14600 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14601 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14602 /* We're trying to scroll because of aggressive scrolling but no
14603 scroll_step is set. Choose an arbitrary one. */
14604 scroll_max = 10 * frame_line_height;
14605 else
14606 scroll_max = 0;
14607
14608 too_near_end:
14609
14610 /* Decide whether to scroll down. */
14611 if (PT > CHARPOS (startp))
14612 {
14613 int scroll_margin_y;
14614
14615 /* Compute the pixel ypos of the scroll margin, then move IT to
14616 either that ypos or PT, whichever comes first. */
14617 start_display (&it, w, startp);
14618 scroll_margin_y = it.last_visible_y - this_scroll_margin
14619 - frame_line_height * extra_scroll_margin_lines;
14620 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14621 (MOVE_TO_POS | MOVE_TO_Y));
14622
14623 if (PT > CHARPOS (it.current.pos))
14624 {
14625 int y0 = line_bottom_y (&it);
14626 /* Compute how many pixels below window bottom to stop searching
14627 for PT. This avoids costly search for PT that is far away if
14628 the user limited scrolling by a small number of lines, but
14629 always finds PT if scroll_conservatively is set to a large
14630 number, such as most-positive-fixnum. */
14631 int slack = max (scroll_max, 10 * frame_line_height);
14632 int y_to_move = it.last_visible_y + slack;
14633
14634 /* Compute the distance from the scroll margin to PT or to
14635 the scroll limit, whichever comes first. This should
14636 include the height of the cursor line, to make that line
14637 fully visible. */
14638 move_it_to (&it, PT, -1, y_to_move,
14639 -1, MOVE_TO_POS | MOVE_TO_Y);
14640 dy = line_bottom_y (&it) - y0;
14641
14642 if (dy > scroll_max)
14643 return SCROLLING_FAILED;
14644
14645 if (dy > 0)
14646 scroll_down_p = 1;
14647 }
14648 }
14649
14650 if (scroll_down_p)
14651 {
14652 /* Point is in or below the bottom scroll margin, so move the
14653 window start down. If scrolling conservatively, move it just
14654 enough down to make point visible. If scroll_step is set,
14655 move it down by scroll_step. */
14656 if (arg_scroll_conservatively)
14657 amount_to_scroll
14658 = min (max (dy, frame_line_height),
14659 frame_line_height * arg_scroll_conservatively);
14660 else if (scroll_step || temp_scroll_step)
14661 amount_to_scroll = scroll_max;
14662 else
14663 {
14664 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14665 height = WINDOW_BOX_TEXT_HEIGHT (w);
14666 if (NUMBERP (aggressive))
14667 {
14668 double float_amount = XFLOATINT (aggressive) * height;
14669 int aggressive_scroll = float_amount;
14670 if (aggressive_scroll == 0 && float_amount > 0)
14671 aggressive_scroll = 1;
14672 /* Don't let point enter the scroll margin near top of
14673 the window. This could happen if the value of
14674 scroll_up_aggressively is too large and there are
14675 non-zero margins, because scroll_up_aggressively
14676 means put point that fraction of window height
14677 _from_the_bottom_margin_. */
14678 if (aggressive_scroll + 2*this_scroll_margin > height)
14679 aggressive_scroll = height - 2*this_scroll_margin;
14680 amount_to_scroll = dy + aggressive_scroll;
14681 }
14682 }
14683
14684 if (amount_to_scroll <= 0)
14685 return SCROLLING_FAILED;
14686
14687 start_display (&it, w, startp);
14688 if (arg_scroll_conservatively <= scroll_limit)
14689 move_it_vertically (&it, amount_to_scroll);
14690 else
14691 {
14692 /* Extra precision for users who set scroll-conservatively
14693 to a large number: make sure the amount we scroll
14694 the window start is never less than amount_to_scroll,
14695 which was computed as distance from window bottom to
14696 point. This matters when lines at window top and lines
14697 below window bottom have different height. */
14698 struct it it1;
14699 void *it1data = NULL;
14700 /* We use a temporary it1 because line_bottom_y can modify
14701 its argument, if it moves one line down; see there. */
14702 int start_y;
14703
14704 SAVE_IT (it1, it, it1data);
14705 start_y = line_bottom_y (&it1);
14706 do {
14707 RESTORE_IT (&it, &it, it1data);
14708 move_it_by_lines (&it, 1);
14709 SAVE_IT (it1, it, it1data);
14710 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14711 }
14712
14713 /* If STARTP is unchanged, move it down another screen line. */
14714 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14715 move_it_by_lines (&it, 1);
14716 startp = it.current.pos;
14717 }
14718 else
14719 {
14720 struct text_pos scroll_margin_pos = startp;
14721 int y_offset = 0;
14722
14723 /* See if point is inside the scroll margin at the top of the
14724 window. */
14725 if (this_scroll_margin)
14726 {
14727 int y_start;
14728
14729 start_display (&it, w, startp);
14730 y_start = it.current_y;
14731 move_it_vertically (&it, this_scroll_margin);
14732 scroll_margin_pos = it.current.pos;
14733 /* If we didn't move enough before hitting ZV, request
14734 additional amount of scroll, to move point out of the
14735 scroll margin. */
14736 if (IT_CHARPOS (it) == ZV
14737 && it.current_y - y_start < this_scroll_margin)
14738 y_offset = this_scroll_margin - (it.current_y - y_start);
14739 }
14740
14741 if (PT < CHARPOS (scroll_margin_pos))
14742 {
14743 /* Point is in the scroll margin at the top of the window or
14744 above what is displayed in the window. */
14745 int y0, y_to_move;
14746
14747 /* Compute the vertical distance from PT to the scroll
14748 margin position. Move as far as scroll_max allows, or
14749 one screenful, or 10 screen lines, whichever is largest.
14750 Give up if distance is greater than scroll_max or if we
14751 didn't reach the scroll margin position. */
14752 SET_TEXT_POS (pos, PT, PT_BYTE);
14753 start_display (&it, w, pos);
14754 y0 = it.current_y;
14755 y_to_move = max (it.last_visible_y,
14756 max (scroll_max, 10 * frame_line_height));
14757 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14758 y_to_move, -1,
14759 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14760 dy = it.current_y - y0;
14761 if (dy > scroll_max
14762 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14763 return SCROLLING_FAILED;
14764
14765 /* Additional scroll for when ZV was too close to point. */
14766 dy += y_offset;
14767
14768 /* Compute new window start. */
14769 start_display (&it, w, startp);
14770
14771 if (arg_scroll_conservatively)
14772 amount_to_scroll = max (dy, frame_line_height *
14773 max (scroll_step, temp_scroll_step));
14774 else if (scroll_step || temp_scroll_step)
14775 amount_to_scroll = scroll_max;
14776 else
14777 {
14778 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14779 height = WINDOW_BOX_TEXT_HEIGHT (w);
14780 if (NUMBERP (aggressive))
14781 {
14782 double float_amount = XFLOATINT (aggressive) * height;
14783 int aggressive_scroll = float_amount;
14784 if (aggressive_scroll == 0 && float_amount > 0)
14785 aggressive_scroll = 1;
14786 /* Don't let point enter the scroll margin near
14787 bottom of the window, if the value of
14788 scroll_down_aggressively happens to be too
14789 large. */
14790 if (aggressive_scroll + 2*this_scroll_margin > height)
14791 aggressive_scroll = height - 2*this_scroll_margin;
14792 amount_to_scroll = dy + aggressive_scroll;
14793 }
14794 }
14795
14796 if (amount_to_scroll <= 0)
14797 return SCROLLING_FAILED;
14798
14799 move_it_vertically_backward (&it, amount_to_scroll);
14800 startp = it.current.pos;
14801 }
14802 }
14803
14804 /* Run window scroll functions. */
14805 startp = run_window_scroll_functions (window, startp);
14806
14807 /* Display the window. Give up if new fonts are loaded, or if point
14808 doesn't appear. */
14809 if (!try_window (window, startp, 0))
14810 rc = SCROLLING_NEED_LARGER_MATRICES;
14811 else if (w->cursor.vpos < 0)
14812 {
14813 clear_glyph_matrix (w->desired_matrix);
14814 rc = SCROLLING_FAILED;
14815 }
14816 else
14817 {
14818 /* Maybe forget recorded base line for line number display. */
14819 if (!just_this_one_p
14820 || current_buffer->clip_changed
14821 || BEG_UNCHANGED < CHARPOS (startp))
14822 w->base_line_number = 0;
14823
14824 /* If cursor ends up on a partially visible line,
14825 treat that as being off the bottom of the screen. */
14826 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14827 /* It's possible that the cursor is on the first line of the
14828 buffer, which is partially obscured due to a vscroll
14829 (Bug#7537). In that case, avoid looping forever . */
14830 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14831 {
14832 clear_glyph_matrix (w->desired_matrix);
14833 ++extra_scroll_margin_lines;
14834 goto too_near_end;
14835 }
14836 rc = SCROLLING_SUCCESS;
14837 }
14838
14839 return rc;
14840 }
14841
14842
14843 /* Compute a suitable window start for window W if display of W starts
14844 on a continuation line. Value is non-zero if a new window start
14845 was computed.
14846
14847 The new window start will be computed, based on W's width, starting
14848 from the start of the continued line. It is the start of the
14849 screen line with the minimum distance from the old start W->start. */
14850
14851 static int
14852 compute_window_start_on_continuation_line (struct window *w)
14853 {
14854 struct text_pos pos, start_pos;
14855 int window_start_changed_p = 0;
14856
14857 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14858
14859 /* If window start is on a continuation line... Window start may be
14860 < BEGV in case there's invisible text at the start of the
14861 buffer (M-x rmail, for example). */
14862 if (CHARPOS (start_pos) > BEGV
14863 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14864 {
14865 struct it it;
14866 struct glyph_row *row;
14867
14868 /* Handle the case that the window start is out of range. */
14869 if (CHARPOS (start_pos) < BEGV)
14870 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14871 else if (CHARPOS (start_pos) > ZV)
14872 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14873
14874 /* Find the start of the continued line. This should be fast
14875 because find_newline is fast (newline cache). */
14876 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14877 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14878 row, DEFAULT_FACE_ID);
14879 reseat_at_previous_visible_line_start (&it);
14880
14881 /* If the line start is "too far" away from the window start,
14882 say it takes too much time to compute a new window start. */
14883 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14884 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14885 {
14886 int min_distance, distance;
14887
14888 /* Move forward by display lines to find the new window
14889 start. If window width was enlarged, the new start can
14890 be expected to be > the old start. If window width was
14891 decreased, the new window start will be < the old start.
14892 So, we're looking for the display line start with the
14893 minimum distance from the old window start. */
14894 pos = it.current.pos;
14895 min_distance = INFINITY;
14896 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14897 distance < min_distance)
14898 {
14899 min_distance = distance;
14900 pos = it.current.pos;
14901 if (it.line_wrap == WORD_WRAP)
14902 {
14903 /* Under WORD_WRAP, move_it_by_lines is likely to
14904 overshoot and stop not at the first, but the
14905 second character from the left margin. So in
14906 that case, we need a more tight control on the X
14907 coordinate of the iterator than move_it_by_lines
14908 promises in its contract. The method is to first
14909 go to the last (rightmost) visible character of a
14910 line, then move to the leftmost character on the
14911 next line in a separate call. */
14912 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14913 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14914 move_it_to (&it, ZV, 0,
14915 it.current_y + it.max_ascent + it.max_descent, -1,
14916 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14917 }
14918 else
14919 move_it_by_lines (&it, 1);
14920 }
14921
14922 /* Set the window start there. */
14923 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14924 window_start_changed_p = 1;
14925 }
14926 }
14927
14928 return window_start_changed_p;
14929 }
14930
14931
14932 /* Try cursor movement in case text has not changed in window WINDOW,
14933 with window start STARTP. Value is
14934
14935 CURSOR_MOVEMENT_SUCCESS if successful
14936
14937 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14938
14939 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14940 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14941 we want to scroll as if scroll-step were set to 1. See the code.
14942
14943 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14944 which case we have to abort this redisplay, and adjust matrices
14945 first. */
14946
14947 enum
14948 {
14949 CURSOR_MOVEMENT_SUCCESS,
14950 CURSOR_MOVEMENT_CANNOT_BE_USED,
14951 CURSOR_MOVEMENT_MUST_SCROLL,
14952 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14953 };
14954
14955 static int
14956 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14957 {
14958 struct window *w = XWINDOW (window);
14959 struct frame *f = XFRAME (w->frame);
14960 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14961
14962 #ifdef GLYPH_DEBUG
14963 if (inhibit_try_cursor_movement)
14964 return rc;
14965 #endif
14966
14967 /* Previously, there was a check for Lisp integer in the
14968 if-statement below. Now, this field is converted to
14969 ptrdiff_t, thus zero means invalid position in a buffer. */
14970 eassert (w->last_point > 0);
14971 /* Likewise there was a check whether window_end_vpos is nil or larger
14972 than the window. Now window_end_vpos is int and so never nil, but
14973 let's leave eassert to check whether it fits in the window. */
14974 eassert (w->window_end_vpos < w->current_matrix->nrows);
14975
14976 /* Handle case where text has not changed, only point, and it has
14977 not moved off the frame. */
14978 if (/* Point may be in this window. */
14979 PT >= CHARPOS (startp)
14980 /* Selective display hasn't changed. */
14981 && !current_buffer->clip_changed
14982 /* Function force-mode-line-update is used to force a thorough
14983 redisplay. It sets either windows_or_buffers_changed or
14984 update_mode_lines. So don't take a shortcut here for these
14985 cases. */
14986 && !update_mode_lines
14987 && !windows_or_buffers_changed
14988 && !f->cursor_type_changed
14989 /* Can't use this case if highlighting a region. When a
14990 region exists, cursor movement has to do more than just
14991 set the cursor. */
14992 && markpos_of_region () < 0
14993 && !w->region_showing
14994 && NILP (Vshow_trailing_whitespace)
14995 /* This code is not used for mini-buffer for the sake of the case
14996 of redisplaying to replace an echo area message; since in
14997 that case the mini-buffer contents per se are usually
14998 unchanged. This code is of no real use in the mini-buffer
14999 since the handling of this_line_start_pos, etc., in redisplay
15000 handles the same cases. */
15001 && !EQ (window, minibuf_window)
15002 && (FRAME_WINDOW_P (f)
15003 || !overlay_arrow_in_current_buffer_p ()))
15004 {
15005 int this_scroll_margin, top_scroll_margin;
15006 struct glyph_row *row = NULL;
15007 int frame_line_height = default_line_pixel_height (w);
15008 int window_total_lines
15009 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15010
15011 #ifdef GLYPH_DEBUG
15012 debug_method_add (w, "cursor movement");
15013 #endif
15014
15015 /* Scroll if point within this distance from the top or bottom
15016 of the window. This is a pixel value. */
15017 if (scroll_margin > 0)
15018 {
15019 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15020 this_scroll_margin *= frame_line_height;
15021 }
15022 else
15023 this_scroll_margin = 0;
15024
15025 top_scroll_margin = this_scroll_margin;
15026 if (WINDOW_WANTS_HEADER_LINE_P (w))
15027 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15028
15029 /* Start with the row the cursor was displayed during the last
15030 not paused redisplay. Give up if that row is not valid. */
15031 if (w->last_cursor_vpos < 0
15032 || w->last_cursor_vpos >= w->current_matrix->nrows)
15033 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15034 else
15035 {
15036 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15037 if (row->mode_line_p)
15038 ++row;
15039 if (!row->enabled_p)
15040 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15041 }
15042
15043 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15044 {
15045 int scroll_p = 0, must_scroll = 0;
15046 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15047
15048 if (PT > w->last_point)
15049 {
15050 /* Point has moved forward. */
15051 while (MATRIX_ROW_END_CHARPOS (row) < PT
15052 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15053 {
15054 eassert (row->enabled_p);
15055 ++row;
15056 }
15057
15058 /* If the end position of a row equals the start
15059 position of the next row, and PT is at that position,
15060 we would rather display cursor in the next line. */
15061 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15062 && MATRIX_ROW_END_CHARPOS (row) == PT
15063 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15064 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15065 && !cursor_row_p (row))
15066 ++row;
15067
15068 /* If within the scroll margin, scroll. Note that
15069 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15070 the next line would be drawn, and that
15071 this_scroll_margin can be zero. */
15072 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15073 || PT > MATRIX_ROW_END_CHARPOS (row)
15074 /* Line is completely visible last line in window
15075 and PT is to be set in the next line. */
15076 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15077 && PT == MATRIX_ROW_END_CHARPOS (row)
15078 && !row->ends_at_zv_p
15079 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15080 scroll_p = 1;
15081 }
15082 else if (PT < w->last_point)
15083 {
15084 /* Cursor has to be moved backward. Note that PT >=
15085 CHARPOS (startp) because of the outer if-statement. */
15086 while (!row->mode_line_p
15087 && (MATRIX_ROW_START_CHARPOS (row) > PT
15088 || (MATRIX_ROW_START_CHARPOS (row) == PT
15089 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15090 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15091 row > w->current_matrix->rows
15092 && (row-1)->ends_in_newline_from_string_p))))
15093 && (row->y > top_scroll_margin
15094 || CHARPOS (startp) == BEGV))
15095 {
15096 eassert (row->enabled_p);
15097 --row;
15098 }
15099
15100 /* Consider the following case: Window starts at BEGV,
15101 there is invisible, intangible text at BEGV, so that
15102 display starts at some point START > BEGV. It can
15103 happen that we are called with PT somewhere between
15104 BEGV and START. Try to handle that case. */
15105 if (row < w->current_matrix->rows
15106 || row->mode_line_p)
15107 {
15108 row = w->current_matrix->rows;
15109 if (row->mode_line_p)
15110 ++row;
15111 }
15112
15113 /* Due to newlines in overlay strings, we may have to
15114 skip forward over overlay strings. */
15115 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15116 && MATRIX_ROW_END_CHARPOS (row) == PT
15117 && !cursor_row_p (row))
15118 ++row;
15119
15120 /* If within the scroll margin, scroll. */
15121 if (row->y < top_scroll_margin
15122 && CHARPOS (startp) != BEGV)
15123 scroll_p = 1;
15124 }
15125 else
15126 {
15127 /* Cursor did not move. So don't scroll even if cursor line
15128 is partially visible, as it was so before. */
15129 rc = CURSOR_MOVEMENT_SUCCESS;
15130 }
15131
15132 if (PT < MATRIX_ROW_START_CHARPOS (row)
15133 || PT > MATRIX_ROW_END_CHARPOS (row))
15134 {
15135 /* if PT is not in the glyph row, give up. */
15136 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15137 must_scroll = 1;
15138 }
15139 else if (rc != CURSOR_MOVEMENT_SUCCESS
15140 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15141 {
15142 struct glyph_row *row1;
15143
15144 /* If rows are bidi-reordered and point moved, back up
15145 until we find a row that does not belong to a
15146 continuation line. This is because we must consider
15147 all rows of a continued line as candidates for the
15148 new cursor positioning, since row start and end
15149 positions change non-linearly with vertical position
15150 in such rows. */
15151 /* FIXME: Revisit this when glyph ``spilling'' in
15152 continuation lines' rows is implemented for
15153 bidi-reordered rows. */
15154 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15155 MATRIX_ROW_CONTINUATION_LINE_P (row);
15156 --row)
15157 {
15158 /* If we hit the beginning of the displayed portion
15159 without finding the first row of a continued
15160 line, give up. */
15161 if (row <= row1)
15162 {
15163 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15164 break;
15165 }
15166 eassert (row->enabled_p);
15167 }
15168 }
15169 if (must_scroll)
15170 ;
15171 else if (rc != CURSOR_MOVEMENT_SUCCESS
15172 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15173 /* Make sure this isn't a header line by any chance, since
15174 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15175 && !row->mode_line_p
15176 && make_cursor_line_fully_visible_p)
15177 {
15178 if (PT == MATRIX_ROW_END_CHARPOS (row)
15179 && !row->ends_at_zv_p
15180 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15181 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15182 else if (row->height > window_box_height (w))
15183 {
15184 /* If we end up in a partially visible line, let's
15185 make it fully visible, except when it's taller
15186 than the window, in which case we can't do much
15187 about it. */
15188 *scroll_step = 1;
15189 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15190 }
15191 else
15192 {
15193 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15194 if (!cursor_row_fully_visible_p (w, 0, 1))
15195 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15196 else
15197 rc = CURSOR_MOVEMENT_SUCCESS;
15198 }
15199 }
15200 else if (scroll_p)
15201 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15202 else if (rc != CURSOR_MOVEMENT_SUCCESS
15203 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15204 {
15205 /* With bidi-reordered rows, there could be more than
15206 one candidate row whose start and end positions
15207 occlude point. We need to let set_cursor_from_row
15208 find the best candidate. */
15209 /* FIXME: Revisit this when glyph ``spilling'' in
15210 continuation lines' rows is implemented for
15211 bidi-reordered rows. */
15212 int rv = 0;
15213
15214 do
15215 {
15216 int at_zv_p = 0, exact_match_p = 0;
15217
15218 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15219 && PT <= MATRIX_ROW_END_CHARPOS (row)
15220 && cursor_row_p (row))
15221 rv |= set_cursor_from_row (w, row, w->current_matrix,
15222 0, 0, 0, 0);
15223 /* As soon as we've found the exact match for point,
15224 or the first suitable row whose ends_at_zv_p flag
15225 is set, we are done. */
15226 at_zv_p =
15227 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15228 if (rv && !at_zv_p
15229 && w->cursor.hpos >= 0
15230 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15231 w->cursor.vpos))
15232 {
15233 struct glyph_row *candidate =
15234 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15235 struct glyph *g =
15236 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15237 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15238
15239 exact_match_p =
15240 (BUFFERP (g->object) && g->charpos == PT)
15241 || (INTEGERP (g->object)
15242 && (g->charpos == PT
15243 || (g->charpos == 0 && endpos - 1 == PT)));
15244 }
15245 if (rv && (at_zv_p || exact_match_p))
15246 {
15247 rc = CURSOR_MOVEMENT_SUCCESS;
15248 break;
15249 }
15250 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15251 break;
15252 ++row;
15253 }
15254 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15255 || row->continued_p)
15256 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15257 || (MATRIX_ROW_START_CHARPOS (row) == PT
15258 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15259 /* If we didn't find any candidate rows, or exited the
15260 loop before all the candidates were examined, signal
15261 to the caller that this method failed. */
15262 if (rc != CURSOR_MOVEMENT_SUCCESS
15263 && !(rv
15264 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15265 && !row->continued_p))
15266 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15267 else if (rv)
15268 rc = CURSOR_MOVEMENT_SUCCESS;
15269 }
15270 else
15271 {
15272 do
15273 {
15274 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15275 {
15276 rc = CURSOR_MOVEMENT_SUCCESS;
15277 break;
15278 }
15279 ++row;
15280 }
15281 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15282 && MATRIX_ROW_START_CHARPOS (row) == PT
15283 && cursor_row_p (row));
15284 }
15285 }
15286 }
15287
15288 return rc;
15289 }
15290
15291 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15292 static
15293 #endif
15294 void
15295 set_vertical_scroll_bar (struct window *w)
15296 {
15297 ptrdiff_t start, end, whole;
15298
15299 /* Calculate the start and end positions for the current window.
15300 At some point, it would be nice to choose between scrollbars
15301 which reflect the whole buffer size, with special markers
15302 indicating narrowing, and scrollbars which reflect only the
15303 visible region.
15304
15305 Note that mini-buffers sometimes aren't displaying any text. */
15306 if (!MINI_WINDOW_P (w)
15307 || (w == XWINDOW (minibuf_window)
15308 && NILP (echo_area_buffer[0])))
15309 {
15310 struct buffer *buf = XBUFFER (w->contents);
15311 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15312 start = marker_position (w->start) - BUF_BEGV (buf);
15313 /* I don't think this is guaranteed to be right. For the
15314 moment, we'll pretend it is. */
15315 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15316
15317 if (end < start)
15318 end = start;
15319 if (whole < (end - start))
15320 whole = end - start;
15321 }
15322 else
15323 start = end = whole = 0;
15324
15325 /* Indicate what this scroll bar ought to be displaying now. */
15326 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15327 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15328 (w, end - start, whole, start);
15329 }
15330
15331
15332 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15333 selected_window is redisplayed.
15334
15335 We can return without actually redisplaying the window if fonts has been
15336 changed on window's frame. In that case, redisplay_internal will retry. */
15337
15338 static void
15339 redisplay_window (Lisp_Object window, int just_this_one_p)
15340 {
15341 struct window *w = XWINDOW (window);
15342 struct frame *f = XFRAME (w->frame);
15343 struct buffer *buffer = XBUFFER (w->contents);
15344 struct buffer *old = current_buffer;
15345 struct text_pos lpoint, opoint, startp;
15346 int update_mode_line;
15347 int tem;
15348 struct it it;
15349 /* Record it now because it's overwritten. */
15350 int current_matrix_up_to_date_p = 0;
15351 int used_current_matrix_p = 0;
15352 /* This is less strict than current_matrix_up_to_date_p.
15353 It indicates that the buffer contents and narrowing are unchanged. */
15354 int buffer_unchanged_p = 0;
15355 int temp_scroll_step = 0;
15356 ptrdiff_t count = SPECPDL_INDEX ();
15357 int rc;
15358 int centering_position = -1;
15359 int last_line_misfit = 0;
15360 ptrdiff_t beg_unchanged, end_unchanged;
15361 int frame_line_height;
15362
15363 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15364 opoint = lpoint;
15365
15366 #ifdef GLYPH_DEBUG
15367 *w->desired_matrix->method = 0;
15368 #endif
15369
15370 /* Make sure that both W's markers are valid. */
15371 eassert (XMARKER (w->start)->buffer == buffer);
15372 eassert (XMARKER (w->pointm)->buffer == buffer);
15373
15374 restart:
15375 reconsider_clip_changes (w);
15376 frame_line_height = default_line_pixel_height (w);
15377
15378 /* Has the mode line to be updated? */
15379 update_mode_line = (w->update_mode_line
15380 || update_mode_lines
15381 || buffer->clip_changed
15382 || buffer->prevent_redisplay_optimizations_p);
15383
15384 if (MINI_WINDOW_P (w))
15385 {
15386 if (w == XWINDOW (echo_area_window)
15387 && !NILP (echo_area_buffer[0]))
15388 {
15389 if (update_mode_line)
15390 /* We may have to update a tty frame's menu bar or a
15391 tool-bar. Example `M-x C-h C-h C-g'. */
15392 goto finish_menu_bars;
15393 else
15394 /* We've already displayed the echo area glyphs in this window. */
15395 goto finish_scroll_bars;
15396 }
15397 else if ((w != XWINDOW (minibuf_window)
15398 || minibuf_level == 0)
15399 /* When buffer is nonempty, redisplay window normally. */
15400 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15401 /* Quail displays non-mini buffers in minibuffer window.
15402 In that case, redisplay the window normally. */
15403 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15404 {
15405 /* W is a mini-buffer window, but it's not active, so clear
15406 it. */
15407 int yb = window_text_bottom_y (w);
15408 struct glyph_row *row;
15409 int y;
15410
15411 for (y = 0, row = w->desired_matrix->rows;
15412 y < yb;
15413 y += row->height, ++row)
15414 blank_row (w, row, y);
15415 goto finish_scroll_bars;
15416 }
15417
15418 clear_glyph_matrix (w->desired_matrix);
15419 }
15420
15421 /* Otherwise set up data on this window; select its buffer and point
15422 value. */
15423 /* Really select the buffer, for the sake of buffer-local
15424 variables. */
15425 set_buffer_internal_1 (XBUFFER (w->contents));
15426
15427 current_matrix_up_to_date_p
15428 = (w->window_end_valid
15429 && !current_buffer->clip_changed
15430 && !current_buffer->prevent_redisplay_optimizations_p
15431 && !window_outdated (w));
15432
15433 /* Run the window-bottom-change-functions
15434 if it is possible that the text on the screen has changed
15435 (either due to modification of the text, or any other reason). */
15436 if (!current_matrix_up_to_date_p
15437 && !NILP (Vwindow_text_change_functions))
15438 {
15439 safe_run_hooks (Qwindow_text_change_functions);
15440 goto restart;
15441 }
15442
15443 beg_unchanged = BEG_UNCHANGED;
15444 end_unchanged = END_UNCHANGED;
15445
15446 SET_TEXT_POS (opoint, PT, PT_BYTE);
15447
15448 specbind (Qinhibit_point_motion_hooks, Qt);
15449
15450 buffer_unchanged_p
15451 = (w->window_end_valid
15452 && !current_buffer->clip_changed
15453 && !window_outdated (w));
15454
15455 /* When windows_or_buffers_changed is non-zero, we can't rely
15456 on the window end being valid, so set it to zero there. */
15457 if (windows_or_buffers_changed)
15458 {
15459 /* If window starts on a continuation line, maybe adjust the
15460 window start in case the window's width changed. */
15461 if (XMARKER (w->start)->buffer == current_buffer)
15462 compute_window_start_on_continuation_line (w);
15463
15464 w->window_end_valid = 0;
15465 /* If so, we also can't rely on current matrix
15466 and should not fool try_cursor_movement below. */
15467 current_matrix_up_to_date_p = 0;
15468 }
15469
15470 /* Some sanity checks. */
15471 CHECK_WINDOW_END (w);
15472 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15473 emacs_abort ();
15474 if (BYTEPOS (opoint) < CHARPOS (opoint))
15475 emacs_abort ();
15476
15477 if (mode_line_update_needed (w))
15478 update_mode_line = 1;
15479
15480 /* Point refers normally to the selected window. For any other
15481 window, set up appropriate value. */
15482 if (!EQ (window, selected_window))
15483 {
15484 ptrdiff_t new_pt = marker_position (w->pointm);
15485 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15486 if (new_pt < BEGV)
15487 {
15488 new_pt = BEGV;
15489 new_pt_byte = BEGV_BYTE;
15490 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15491 }
15492 else if (new_pt > (ZV - 1))
15493 {
15494 new_pt = ZV;
15495 new_pt_byte = ZV_BYTE;
15496 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15497 }
15498
15499 /* We don't use SET_PT so that the point-motion hooks don't run. */
15500 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15501 }
15502
15503 /* If any of the character widths specified in the display table
15504 have changed, invalidate the width run cache. It's true that
15505 this may be a bit late to catch such changes, but the rest of
15506 redisplay goes (non-fatally) haywire when the display table is
15507 changed, so why should we worry about doing any better? */
15508 if (current_buffer->width_run_cache)
15509 {
15510 struct Lisp_Char_Table *disptab = buffer_display_table ();
15511
15512 if (! disptab_matches_widthtab
15513 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15514 {
15515 invalidate_region_cache (current_buffer,
15516 current_buffer->width_run_cache,
15517 BEG, Z);
15518 recompute_width_table (current_buffer, disptab);
15519 }
15520 }
15521
15522 /* If window-start is screwed up, choose a new one. */
15523 if (XMARKER (w->start)->buffer != current_buffer)
15524 goto recenter;
15525
15526 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15527
15528 /* If someone specified a new starting point but did not insist,
15529 check whether it can be used. */
15530 if (w->optional_new_start
15531 && CHARPOS (startp) >= BEGV
15532 && CHARPOS (startp) <= ZV)
15533 {
15534 w->optional_new_start = 0;
15535 start_display (&it, w, startp);
15536 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15537 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15538 if (IT_CHARPOS (it) == PT)
15539 w->force_start = 1;
15540 /* IT may overshoot PT if text at PT is invisible. */
15541 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15542 w->force_start = 1;
15543 }
15544
15545 force_start:
15546
15547 /* Handle case where place to start displaying has been specified,
15548 unless the specified location is outside the accessible range. */
15549 if (w->force_start || window_frozen_p (w))
15550 {
15551 /* We set this later on if we have to adjust point. */
15552 int new_vpos = -1;
15553
15554 w->force_start = 0;
15555 w->vscroll = 0;
15556 w->window_end_valid = 0;
15557
15558 /* Forget any recorded base line for line number display. */
15559 if (!buffer_unchanged_p)
15560 w->base_line_number = 0;
15561
15562 /* Redisplay the mode line. Select the buffer properly for that.
15563 Also, run the hook window-scroll-functions
15564 because we have scrolled. */
15565 /* Note, we do this after clearing force_start because
15566 if there's an error, it is better to forget about force_start
15567 than to get into an infinite loop calling the hook functions
15568 and having them get more errors. */
15569 if (!update_mode_line
15570 || ! NILP (Vwindow_scroll_functions))
15571 {
15572 update_mode_line = 1;
15573 w->update_mode_line = 1;
15574 startp = run_window_scroll_functions (window, startp);
15575 }
15576
15577 if (CHARPOS (startp) < BEGV)
15578 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15579 else if (CHARPOS (startp) > ZV)
15580 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15581
15582 /* Redisplay, then check if cursor has been set during the
15583 redisplay. Give up if new fonts were loaded. */
15584 /* We used to issue a CHECK_MARGINS argument to try_window here,
15585 but this causes scrolling to fail when point begins inside
15586 the scroll margin (bug#148) -- cyd */
15587 if (!try_window (window, startp, 0))
15588 {
15589 w->force_start = 1;
15590 clear_glyph_matrix (w->desired_matrix);
15591 goto need_larger_matrices;
15592 }
15593
15594 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15595 {
15596 /* If point does not appear, try to move point so it does
15597 appear. The desired matrix has been built above, so we
15598 can use it here. */
15599 new_vpos = window_box_height (w) / 2;
15600 }
15601
15602 if (!cursor_row_fully_visible_p (w, 0, 0))
15603 {
15604 /* Point does appear, but on a line partly visible at end of window.
15605 Move it back to a fully-visible line. */
15606 new_vpos = window_box_height (w);
15607 }
15608 else if (w->cursor.vpos >=0)
15609 {
15610 /* Some people insist on not letting point enter the scroll
15611 margin, even though this part handles windows that didn't
15612 scroll at all. */
15613 int window_total_lines
15614 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15615 int margin = min (scroll_margin, window_total_lines / 4);
15616 int pixel_margin = margin * frame_line_height;
15617 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15618
15619 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15620 below, which finds the row to move point to, advances by
15621 the Y coordinate of the _next_ row, see the definition of
15622 MATRIX_ROW_BOTTOM_Y. */
15623 if (w->cursor.vpos < margin + header_line)
15624 {
15625 w->cursor.vpos = -1;
15626 clear_glyph_matrix (w->desired_matrix);
15627 goto try_to_scroll;
15628 }
15629 else
15630 {
15631 int window_height = window_box_height (w);
15632
15633 if (header_line)
15634 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15635 if (w->cursor.y >= window_height - pixel_margin)
15636 {
15637 w->cursor.vpos = -1;
15638 clear_glyph_matrix (w->desired_matrix);
15639 goto try_to_scroll;
15640 }
15641 }
15642 }
15643
15644 /* If we need to move point for either of the above reasons,
15645 now actually do it. */
15646 if (new_vpos >= 0)
15647 {
15648 struct glyph_row *row;
15649
15650 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15651 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15652 ++row;
15653
15654 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15655 MATRIX_ROW_START_BYTEPOS (row));
15656
15657 if (w != XWINDOW (selected_window))
15658 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15659 else if (current_buffer == old)
15660 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15661
15662 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15663
15664 /* If we are highlighting the region, then we just changed
15665 the region, so redisplay to show it. */
15666 if (markpos_of_region () >= 0)
15667 {
15668 clear_glyph_matrix (w->desired_matrix);
15669 if (!try_window (window, startp, 0))
15670 goto need_larger_matrices;
15671 }
15672 }
15673
15674 #ifdef GLYPH_DEBUG
15675 debug_method_add (w, "forced window start");
15676 #endif
15677 goto done;
15678 }
15679
15680 /* Handle case where text has not changed, only point, and it has
15681 not moved off the frame, and we are not retrying after hscroll.
15682 (current_matrix_up_to_date_p is nonzero when retrying.) */
15683 if (current_matrix_up_to_date_p
15684 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15685 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15686 {
15687 switch (rc)
15688 {
15689 case CURSOR_MOVEMENT_SUCCESS:
15690 used_current_matrix_p = 1;
15691 goto done;
15692
15693 case CURSOR_MOVEMENT_MUST_SCROLL:
15694 goto try_to_scroll;
15695
15696 default:
15697 emacs_abort ();
15698 }
15699 }
15700 /* If current starting point was originally the beginning of a line
15701 but no longer is, find a new starting point. */
15702 else if (w->start_at_line_beg
15703 && !(CHARPOS (startp) <= BEGV
15704 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15705 {
15706 #ifdef GLYPH_DEBUG
15707 debug_method_add (w, "recenter 1");
15708 #endif
15709 goto recenter;
15710 }
15711
15712 /* Try scrolling with try_window_id. Value is > 0 if update has
15713 been done, it is -1 if we know that the same window start will
15714 not work. It is 0 if unsuccessful for some other reason. */
15715 else if ((tem = try_window_id (w)) != 0)
15716 {
15717 #ifdef GLYPH_DEBUG
15718 debug_method_add (w, "try_window_id %d", tem);
15719 #endif
15720
15721 if (f->fonts_changed)
15722 goto need_larger_matrices;
15723 if (tem > 0)
15724 goto done;
15725
15726 /* Otherwise try_window_id has returned -1 which means that we
15727 don't want the alternative below this comment to execute. */
15728 }
15729 else if (CHARPOS (startp) >= BEGV
15730 && CHARPOS (startp) <= ZV
15731 && PT >= CHARPOS (startp)
15732 && (CHARPOS (startp) < ZV
15733 /* Avoid starting at end of buffer. */
15734 || CHARPOS (startp) == BEGV
15735 || !window_outdated (w)))
15736 {
15737 int d1, d2, d3, d4, d5, d6;
15738
15739 /* If first window line is a continuation line, and window start
15740 is inside the modified region, but the first change is before
15741 current window start, we must select a new window start.
15742
15743 However, if this is the result of a down-mouse event (e.g. by
15744 extending the mouse-drag-overlay), we don't want to select a
15745 new window start, since that would change the position under
15746 the mouse, resulting in an unwanted mouse-movement rather
15747 than a simple mouse-click. */
15748 if (!w->start_at_line_beg
15749 && NILP (do_mouse_tracking)
15750 && CHARPOS (startp) > BEGV
15751 && CHARPOS (startp) > BEG + beg_unchanged
15752 && CHARPOS (startp) <= Z - end_unchanged
15753 /* Even if w->start_at_line_beg is nil, a new window may
15754 start at a line_beg, since that's how set_buffer_window
15755 sets it. So, we need to check the return value of
15756 compute_window_start_on_continuation_line. (See also
15757 bug#197). */
15758 && XMARKER (w->start)->buffer == current_buffer
15759 && compute_window_start_on_continuation_line (w)
15760 /* It doesn't make sense to force the window start like we
15761 do at label force_start if it is already known that point
15762 will not be visible in the resulting window, because
15763 doing so will move point from its correct position
15764 instead of scrolling the window to bring point into view.
15765 See bug#9324. */
15766 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15767 {
15768 w->force_start = 1;
15769 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15770 goto force_start;
15771 }
15772
15773 #ifdef GLYPH_DEBUG
15774 debug_method_add (w, "same window start");
15775 #endif
15776
15777 /* Try to redisplay starting at same place as before.
15778 If point has not moved off frame, accept the results. */
15779 if (!current_matrix_up_to_date_p
15780 /* Don't use try_window_reusing_current_matrix in this case
15781 because a window scroll function can have changed the
15782 buffer. */
15783 || !NILP (Vwindow_scroll_functions)
15784 || MINI_WINDOW_P (w)
15785 || !(used_current_matrix_p
15786 = try_window_reusing_current_matrix (w)))
15787 {
15788 IF_DEBUG (debug_method_add (w, "1"));
15789 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15790 /* -1 means we need to scroll.
15791 0 means we need new matrices, but fonts_changed
15792 is set in that case, so we will detect it below. */
15793 goto try_to_scroll;
15794 }
15795
15796 if (f->fonts_changed)
15797 goto need_larger_matrices;
15798
15799 if (w->cursor.vpos >= 0)
15800 {
15801 if (!just_this_one_p
15802 || current_buffer->clip_changed
15803 || BEG_UNCHANGED < CHARPOS (startp))
15804 /* Forget any recorded base line for line number display. */
15805 w->base_line_number = 0;
15806
15807 if (!cursor_row_fully_visible_p (w, 1, 0))
15808 {
15809 clear_glyph_matrix (w->desired_matrix);
15810 last_line_misfit = 1;
15811 }
15812 /* Drop through and scroll. */
15813 else
15814 goto done;
15815 }
15816 else
15817 clear_glyph_matrix (w->desired_matrix);
15818 }
15819
15820 try_to_scroll:
15821
15822 /* Redisplay the mode line. Select the buffer properly for that. */
15823 if (!update_mode_line)
15824 {
15825 update_mode_line = 1;
15826 w->update_mode_line = 1;
15827 }
15828
15829 /* Try to scroll by specified few lines. */
15830 if ((scroll_conservatively
15831 || emacs_scroll_step
15832 || temp_scroll_step
15833 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15834 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15835 && CHARPOS (startp) >= BEGV
15836 && CHARPOS (startp) <= ZV)
15837 {
15838 /* The function returns -1 if new fonts were loaded, 1 if
15839 successful, 0 if not successful. */
15840 int ss = try_scrolling (window, just_this_one_p,
15841 scroll_conservatively,
15842 emacs_scroll_step,
15843 temp_scroll_step, last_line_misfit);
15844 switch (ss)
15845 {
15846 case SCROLLING_SUCCESS:
15847 goto done;
15848
15849 case SCROLLING_NEED_LARGER_MATRICES:
15850 goto need_larger_matrices;
15851
15852 case SCROLLING_FAILED:
15853 break;
15854
15855 default:
15856 emacs_abort ();
15857 }
15858 }
15859
15860 /* Finally, just choose a place to start which positions point
15861 according to user preferences. */
15862
15863 recenter:
15864
15865 #ifdef GLYPH_DEBUG
15866 debug_method_add (w, "recenter");
15867 #endif
15868
15869 /* Forget any previously recorded base line for line number display. */
15870 if (!buffer_unchanged_p)
15871 w->base_line_number = 0;
15872
15873 /* Determine the window start relative to point. */
15874 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15875 it.current_y = it.last_visible_y;
15876 if (centering_position < 0)
15877 {
15878 int window_total_lines
15879 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15880 int margin =
15881 scroll_margin > 0
15882 ? min (scroll_margin, window_total_lines / 4)
15883 : 0;
15884 ptrdiff_t margin_pos = CHARPOS (startp);
15885 Lisp_Object aggressive;
15886 int scrolling_up;
15887
15888 /* If there is a scroll margin at the top of the window, find
15889 its character position. */
15890 if (margin
15891 /* Cannot call start_display if startp is not in the
15892 accessible region of the buffer. This can happen when we
15893 have just switched to a different buffer and/or changed
15894 its restriction. In that case, startp is initialized to
15895 the character position 1 (BEGV) because we did not yet
15896 have chance to display the buffer even once. */
15897 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15898 {
15899 struct it it1;
15900 void *it1data = NULL;
15901
15902 SAVE_IT (it1, it, it1data);
15903 start_display (&it1, w, startp);
15904 move_it_vertically (&it1, margin * frame_line_height);
15905 margin_pos = IT_CHARPOS (it1);
15906 RESTORE_IT (&it, &it, it1data);
15907 }
15908 scrolling_up = PT > margin_pos;
15909 aggressive =
15910 scrolling_up
15911 ? BVAR (current_buffer, scroll_up_aggressively)
15912 : BVAR (current_buffer, scroll_down_aggressively);
15913
15914 if (!MINI_WINDOW_P (w)
15915 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15916 {
15917 int pt_offset = 0;
15918
15919 /* Setting scroll-conservatively overrides
15920 scroll-*-aggressively. */
15921 if (!scroll_conservatively && NUMBERP (aggressive))
15922 {
15923 double float_amount = XFLOATINT (aggressive);
15924
15925 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15926 if (pt_offset == 0 && float_amount > 0)
15927 pt_offset = 1;
15928 if (pt_offset && margin > 0)
15929 margin -= 1;
15930 }
15931 /* Compute how much to move the window start backward from
15932 point so that point will be displayed where the user
15933 wants it. */
15934 if (scrolling_up)
15935 {
15936 centering_position = it.last_visible_y;
15937 if (pt_offset)
15938 centering_position -= pt_offset;
15939 centering_position -=
15940 frame_line_height * (1 + margin + (last_line_misfit != 0))
15941 + WINDOW_HEADER_LINE_HEIGHT (w);
15942 /* Don't let point enter the scroll margin near top of
15943 the window. */
15944 if (centering_position < margin * frame_line_height)
15945 centering_position = margin * frame_line_height;
15946 }
15947 else
15948 centering_position = margin * frame_line_height + pt_offset;
15949 }
15950 else
15951 /* Set the window start half the height of the window backward
15952 from point. */
15953 centering_position = window_box_height (w) / 2;
15954 }
15955 move_it_vertically_backward (&it, centering_position);
15956
15957 eassert (IT_CHARPOS (it) >= BEGV);
15958
15959 /* The function move_it_vertically_backward may move over more
15960 than the specified y-distance. If it->w is small, e.g. a
15961 mini-buffer window, we may end up in front of the window's
15962 display area. Start displaying at the start of the line
15963 containing PT in this case. */
15964 if (it.current_y <= 0)
15965 {
15966 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15967 move_it_vertically_backward (&it, 0);
15968 it.current_y = 0;
15969 }
15970
15971 it.current_x = it.hpos = 0;
15972
15973 /* Set the window start position here explicitly, to avoid an
15974 infinite loop in case the functions in window-scroll-functions
15975 get errors. */
15976 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15977
15978 /* Run scroll hooks. */
15979 startp = run_window_scroll_functions (window, it.current.pos);
15980
15981 /* Redisplay the window. */
15982 if (!current_matrix_up_to_date_p
15983 || windows_or_buffers_changed
15984 || f->cursor_type_changed
15985 /* Don't use try_window_reusing_current_matrix in this case
15986 because it can have changed the buffer. */
15987 || !NILP (Vwindow_scroll_functions)
15988 || !just_this_one_p
15989 || MINI_WINDOW_P (w)
15990 || !(used_current_matrix_p
15991 = try_window_reusing_current_matrix (w)))
15992 try_window (window, startp, 0);
15993
15994 /* If new fonts have been loaded (due to fontsets), give up. We
15995 have to start a new redisplay since we need to re-adjust glyph
15996 matrices. */
15997 if (f->fonts_changed)
15998 goto need_larger_matrices;
15999
16000 /* If cursor did not appear assume that the middle of the window is
16001 in the first line of the window. Do it again with the next line.
16002 (Imagine a window of height 100, displaying two lines of height
16003 60. Moving back 50 from it->last_visible_y will end in the first
16004 line.) */
16005 if (w->cursor.vpos < 0)
16006 {
16007 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16008 {
16009 clear_glyph_matrix (w->desired_matrix);
16010 move_it_by_lines (&it, 1);
16011 try_window (window, it.current.pos, 0);
16012 }
16013 else if (PT < IT_CHARPOS (it))
16014 {
16015 clear_glyph_matrix (w->desired_matrix);
16016 move_it_by_lines (&it, -1);
16017 try_window (window, it.current.pos, 0);
16018 }
16019 else
16020 {
16021 /* Not much we can do about it. */
16022 }
16023 }
16024
16025 /* Consider the following case: Window starts at BEGV, there is
16026 invisible, intangible text at BEGV, so that display starts at
16027 some point START > BEGV. It can happen that we are called with
16028 PT somewhere between BEGV and START. Try to handle that case. */
16029 if (w->cursor.vpos < 0)
16030 {
16031 struct glyph_row *row = w->current_matrix->rows;
16032 if (row->mode_line_p)
16033 ++row;
16034 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16035 }
16036
16037 if (!cursor_row_fully_visible_p (w, 0, 0))
16038 {
16039 /* If vscroll is enabled, disable it and try again. */
16040 if (w->vscroll)
16041 {
16042 w->vscroll = 0;
16043 clear_glyph_matrix (w->desired_matrix);
16044 goto recenter;
16045 }
16046
16047 /* Users who set scroll-conservatively to a large number want
16048 point just above/below the scroll margin. If we ended up
16049 with point's row partially visible, move the window start to
16050 make that row fully visible and out of the margin. */
16051 if (scroll_conservatively > SCROLL_LIMIT)
16052 {
16053 int window_total_lines
16054 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16055 int margin =
16056 scroll_margin > 0
16057 ? min (scroll_margin, window_total_lines / 4)
16058 : 0;
16059 int move_down = w->cursor.vpos >= window_total_lines / 2;
16060
16061 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16062 clear_glyph_matrix (w->desired_matrix);
16063 if (1 == try_window (window, it.current.pos,
16064 TRY_WINDOW_CHECK_MARGINS))
16065 goto done;
16066 }
16067
16068 /* If centering point failed to make the whole line visible,
16069 put point at the top instead. That has to make the whole line
16070 visible, if it can be done. */
16071 if (centering_position == 0)
16072 goto done;
16073
16074 clear_glyph_matrix (w->desired_matrix);
16075 centering_position = 0;
16076 goto recenter;
16077 }
16078
16079 done:
16080
16081 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16082 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16083 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16084
16085 /* Display the mode line, if we must. */
16086 if ((update_mode_line
16087 /* If window not full width, must redo its mode line
16088 if (a) the window to its side is being redone and
16089 (b) we do a frame-based redisplay. This is a consequence
16090 of how inverted lines are drawn in frame-based redisplay. */
16091 || (!just_this_one_p
16092 && !FRAME_WINDOW_P (f)
16093 && !WINDOW_FULL_WIDTH_P (w))
16094 /* Line number to display. */
16095 || w->base_line_pos > 0
16096 /* Column number is displayed and different from the one displayed. */
16097 || (w->column_number_displayed != -1
16098 && (w->column_number_displayed != current_column ())))
16099 /* This means that the window has a mode line. */
16100 && (WINDOW_WANTS_MODELINE_P (w)
16101 || WINDOW_WANTS_HEADER_LINE_P (w)))
16102 {
16103 display_mode_lines (w);
16104
16105 /* If mode line height has changed, arrange for a thorough
16106 immediate redisplay using the correct mode line height. */
16107 if (WINDOW_WANTS_MODELINE_P (w)
16108 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16109 {
16110 f->fonts_changed = 1;
16111 w->mode_line_height = -1;
16112 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16113 = DESIRED_MODE_LINE_HEIGHT (w);
16114 }
16115
16116 /* If header line height has changed, arrange for a thorough
16117 immediate redisplay using the correct header line height. */
16118 if (WINDOW_WANTS_HEADER_LINE_P (w)
16119 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16120 {
16121 f->fonts_changed = 1;
16122 w->header_line_height = -1;
16123 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16124 = DESIRED_HEADER_LINE_HEIGHT (w);
16125 }
16126
16127 if (f->fonts_changed)
16128 goto need_larger_matrices;
16129 }
16130
16131 if (!line_number_displayed && w->base_line_pos != -1)
16132 {
16133 w->base_line_pos = 0;
16134 w->base_line_number = 0;
16135 }
16136
16137 finish_menu_bars:
16138
16139 /* When we reach a frame's selected window, redo the frame's menu bar. */
16140 if (update_mode_line
16141 && EQ (FRAME_SELECTED_WINDOW (f), window))
16142 {
16143 int redisplay_menu_p = 0;
16144
16145 if (FRAME_WINDOW_P (f))
16146 {
16147 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16148 || defined (HAVE_NS) || defined (USE_GTK)
16149 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16150 #else
16151 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16152 #endif
16153 }
16154 else
16155 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16156
16157 if (redisplay_menu_p)
16158 display_menu_bar (w);
16159
16160 #ifdef HAVE_WINDOW_SYSTEM
16161 if (FRAME_WINDOW_P (f))
16162 {
16163 #if defined (USE_GTK) || defined (HAVE_NS)
16164 if (FRAME_EXTERNAL_TOOL_BAR (f))
16165 redisplay_tool_bar (f);
16166 #else
16167 if (WINDOWP (f->tool_bar_window)
16168 && (FRAME_TOOL_BAR_LINES (f) > 0
16169 || !NILP (Vauto_resize_tool_bars))
16170 && redisplay_tool_bar (f))
16171 ignore_mouse_drag_p = 1;
16172 #endif
16173 }
16174 #endif
16175 }
16176
16177 #ifdef HAVE_WINDOW_SYSTEM
16178 if (FRAME_WINDOW_P (f)
16179 && update_window_fringes (w, (just_this_one_p
16180 || (!used_current_matrix_p && !overlay_arrow_seen)
16181 || w->pseudo_window_p)))
16182 {
16183 update_begin (f);
16184 block_input ();
16185 if (draw_window_fringes (w, 1))
16186 x_draw_vertical_border (w);
16187 unblock_input ();
16188 update_end (f);
16189 }
16190 #endif /* HAVE_WINDOW_SYSTEM */
16191
16192 /* We go to this label, with fonts_changed set, if it is
16193 necessary to try again using larger glyph matrices.
16194 We have to redeem the scroll bar even in this case,
16195 because the loop in redisplay_internal expects that. */
16196 need_larger_matrices:
16197 ;
16198 finish_scroll_bars:
16199
16200 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16201 {
16202 /* Set the thumb's position and size. */
16203 set_vertical_scroll_bar (w);
16204
16205 /* Note that we actually used the scroll bar attached to this
16206 window, so it shouldn't be deleted at the end of redisplay. */
16207 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16208 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16209 }
16210
16211 /* Restore current_buffer and value of point in it. The window
16212 update may have changed the buffer, so first make sure `opoint'
16213 is still valid (Bug#6177). */
16214 if (CHARPOS (opoint) < BEGV)
16215 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16216 else if (CHARPOS (opoint) > ZV)
16217 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16218 else
16219 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16220
16221 set_buffer_internal_1 (old);
16222 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16223 shorter. This can be caused by log truncation in *Messages*. */
16224 if (CHARPOS (lpoint) <= ZV)
16225 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16226
16227 unbind_to (count, Qnil);
16228 }
16229
16230
16231 /* Build the complete desired matrix of WINDOW with a window start
16232 buffer position POS.
16233
16234 Value is 1 if successful. It is zero if fonts were loaded during
16235 redisplay which makes re-adjusting glyph matrices necessary, and -1
16236 if point would appear in the scroll margins.
16237 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16238 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16239 set in FLAGS.) */
16240
16241 int
16242 try_window (Lisp_Object window, struct text_pos pos, int flags)
16243 {
16244 struct window *w = XWINDOW (window);
16245 struct it it;
16246 struct glyph_row *last_text_row = NULL;
16247 struct frame *f = XFRAME (w->frame);
16248 int frame_line_height = default_line_pixel_height (w);
16249
16250 /* Make POS the new window start. */
16251 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16252
16253 /* Mark cursor position as unknown. No overlay arrow seen. */
16254 w->cursor.vpos = -1;
16255 overlay_arrow_seen = 0;
16256
16257 /* Initialize iterator and info to start at POS. */
16258 start_display (&it, w, pos);
16259
16260 /* Display all lines of W. */
16261 while (it.current_y < it.last_visible_y)
16262 {
16263 if (display_line (&it))
16264 last_text_row = it.glyph_row - 1;
16265 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16266 return 0;
16267 }
16268
16269 /* Don't let the cursor end in the scroll margins. */
16270 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16271 && !MINI_WINDOW_P (w))
16272 {
16273 int this_scroll_margin;
16274 int window_total_lines
16275 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16276
16277 if (scroll_margin > 0)
16278 {
16279 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16280 this_scroll_margin *= frame_line_height;
16281 }
16282 else
16283 this_scroll_margin = 0;
16284
16285 if ((w->cursor.y >= 0 /* not vscrolled */
16286 && w->cursor.y < this_scroll_margin
16287 && CHARPOS (pos) > BEGV
16288 && IT_CHARPOS (it) < ZV)
16289 /* rms: considering make_cursor_line_fully_visible_p here
16290 seems to give wrong results. We don't want to recenter
16291 when the last line is partly visible, we want to allow
16292 that case to be handled in the usual way. */
16293 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16294 {
16295 w->cursor.vpos = -1;
16296 clear_glyph_matrix (w->desired_matrix);
16297 return -1;
16298 }
16299 }
16300
16301 /* If bottom moved off end of frame, change mode line percentage. */
16302 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16303 w->update_mode_line = 1;
16304
16305 /* Set window_end_pos to the offset of the last character displayed
16306 on the window from the end of current_buffer. Set
16307 window_end_vpos to its row number. */
16308 if (last_text_row)
16309 {
16310 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16311 adjust_window_ends (w, last_text_row, 0);
16312 eassert
16313 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16314 w->window_end_vpos)));
16315 }
16316 else
16317 {
16318 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16319 w->window_end_pos = Z - ZV;
16320 w->window_end_vpos = 0;
16321 }
16322
16323 /* But that is not valid info until redisplay finishes. */
16324 w->window_end_valid = 0;
16325 return 1;
16326 }
16327
16328
16329 \f
16330 /************************************************************************
16331 Window redisplay reusing current matrix when buffer has not changed
16332 ************************************************************************/
16333
16334 /* Try redisplay of window W showing an unchanged buffer with a
16335 different window start than the last time it was displayed by
16336 reusing its current matrix. Value is non-zero if successful.
16337 W->start is the new window start. */
16338
16339 static int
16340 try_window_reusing_current_matrix (struct window *w)
16341 {
16342 struct frame *f = XFRAME (w->frame);
16343 struct glyph_row *bottom_row;
16344 struct it it;
16345 struct run run;
16346 struct text_pos start, new_start;
16347 int nrows_scrolled, i;
16348 struct glyph_row *last_text_row;
16349 struct glyph_row *last_reused_text_row;
16350 struct glyph_row *start_row;
16351 int start_vpos, min_y, max_y;
16352
16353 #ifdef GLYPH_DEBUG
16354 if (inhibit_try_window_reusing)
16355 return 0;
16356 #endif
16357
16358 if (/* This function doesn't handle terminal frames. */
16359 !FRAME_WINDOW_P (f)
16360 /* Don't try to reuse the display if windows have been split
16361 or such. */
16362 || windows_or_buffers_changed
16363 || f->cursor_type_changed)
16364 return 0;
16365
16366 /* Can't do this if region may have changed. */
16367 if (markpos_of_region () >= 0
16368 || w->region_showing
16369 || !NILP (Vshow_trailing_whitespace))
16370 return 0;
16371
16372 /* If top-line visibility has changed, give up. */
16373 if (WINDOW_WANTS_HEADER_LINE_P (w)
16374 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16375 return 0;
16376
16377 /* Give up if old or new display is scrolled vertically. We could
16378 make this function handle this, but right now it doesn't. */
16379 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16380 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16381 return 0;
16382
16383 /* The variable new_start now holds the new window start. The old
16384 start `start' can be determined from the current matrix. */
16385 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16386 start = start_row->minpos;
16387 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16388
16389 /* Clear the desired matrix for the display below. */
16390 clear_glyph_matrix (w->desired_matrix);
16391
16392 if (CHARPOS (new_start) <= CHARPOS (start))
16393 {
16394 /* Don't use this method if the display starts with an ellipsis
16395 displayed for invisible text. It's not easy to handle that case
16396 below, and it's certainly not worth the effort since this is
16397 not a frequent case. */
16398 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16399 return 0;
16400
16401 IF_DEBUG (debug_method_add (w, "twu1"));
16402
16403 /* Display up to a row that can be reused. The variable
16404 last_text_row is set to the last row displayed that displays
16405 text. Note that it.vpos == 0 if or if not there is a
16406 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16407 start_display (&it, w, new_start);
16408 w->cursor.vpos = -1;
16409 last_text_row = last_reused_text_row = NULL;
16410
16411 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16412 {
16413 /* If we have reached into the characters in the START row,
16414 that means the line boundaries have changed. So we
16415 can't start copying with the row START. Maybe it will
16416 work to start copying with the following row. */
16417 while (IT_CHARPOS (it) > CHARPOS (start))
16418 {
16419 /* Advance to the next row as the "start". */
16420 start_row++;
16421 start = start_row->minpos;
16422 /* If there are no more rows to try, or just one, give up. */
16423 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16424 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16425 || CHARPOS (start) == ZV)
16426 {
16427 clear_glyph_matrix (w->desired_matrix);
16428 return 0;
16429 }
16430
16431 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16432 }
16433 /* If we have reached alignment, we can copy the rest of the
16434 rows. */
16435 if (IT_CHARPOS (it) == CHARPOS (start)
16436 /* Don't accept "alignment" inside a display vector,
16437 since start_row could have started in the middle of
16438 that same display vector (thus their character
16439 positions match), and we have no way of telling if
16440 that is the case. */
16441 && it.current.dpvec_index < 0)
16442 break;
16443
16444 if (display_line (&it))
16445 last_text_row = it.glyph_row - 1;
16446
16447 }
16448
16449 /* A value of current_y < last_visible_y means that we stopped
16450 at the previous window start, which in turn means that we
16451 have at least one reusable row. */
16452 if (it.current_y < it.last_visible_y)
16453 {
16454 struct glyph_row *row;
16455
16456 /* IT.vpos always starts from 0; it counts text lines. */
16457 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16458
16459 /* Find PT if not already found in the lines displayed. */
16460 if (w->cursor.vpos < 0)
16461 {
16462 int dy = it.current_y - start_row->y;
16463
16464 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16465 row = row_containing_pos (w, PT, row, NULL, dy);
16466 if (row)
16467 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16468 dy, nrows_scrolled);
16469 else
16470 {
16471 clear_glyph_matrix (w->desired_matrix);
16472 return 0;
16473 }
16474 }
16475
16476 /* Scroll the display. Do it before the current matrix is
16477 changed. The problem here is that update has not yet
16478 run, i.e. part of the current matrix is not up to date.
16479 scroll_run_hook will clear the cursor, and use the
16480 current matrix to get the height of the row the cursor is
16481 in. */
16482 run.current_y = start_row->y;
16483 run.desired_y = it.current_y;
16484 run.height = it.last_visible_y - it.current_y;
16485
16486 if (run.height > 0 && run.current_y != run.desired_y)
16487 {
16488 update_begin (f);
16489 FRAME_RIF (f)->update_window_begin_hook (w);
16490 FRAME_RIF (f)->clear_window_mouse_face (w);
16491 FRAME_RIF (f)->scroll_run_hook (w, &run);
16492 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16493 update_end (f);
16494 }
16495
16496 /* Shift current matrix down by nrows_scrolled lines. */
16497 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16498 rotate_matrix (w->current_matrix,
16499 start_vpos,
16500 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16501 nrows_scrolled);
16502
16503 /* Disable lines that must be updated. */
16504 for (i = 0; i < nrows_scrolled; ++i)
16505 (start_row + i)->enabled_p = 0;
16506
16507 /* Re-compute Y positions. */
16508 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16509 max_y = it.last_visible_y;
16510 for (row = start_row + nrows_scrolled;
16511 row < bottom_row;
16512 ++row)
16513 {
16514 row->y = it.current_y;
16515 row->visible_height = row->height;
16516
16517 if (row->y < min_y)
16518 row->visible_height -= min_y - row->y;
16519 if (row->y + row->height > max_y)
16520 row->visible_height -= row->y + row->height - max_y;
16521 if (row->fringe_bitmap_periodic_p)
16522 row->redraw_fringe_bitmaps_p = 1;
16523
16524 it.current_y += row->height;
16525
16526 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16527 last_reused_text_row = row;
16528 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16529 break;
16530 }
16531
16532 /* Disable lines in the current matrix which are now
16533 below the window. */
16534 for (++row; row < bottom_row; ++row)
16535 row->enabled_p = row->mode_line_p = 0;
16536 }
16537
16538 /* Update window_end_pos etc.; last_reused_text_row is the last
16539 reused row from the current matrix containing text, if any.
16540 The value of last_text_row is the last displayed line
16541 containing text. */
16542 if (last_reused_text_row)
16543 adjust_window_ends (w, last_reused_text_row, 1);
16544 else if (last_text_row)
16545 adjust_window_ends (w, last_text_row, 0);
16546 else
16547 {
16548 /* This window must be completely empty. */
16549 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16550 w->window_end_pos = Z - ZV;
16551 w->window_end_vpos = 0;
16552 }
16553 w->window_end_valid = 0;
16554
16555 /* Update hint: don't try scrolling again in update_window. */
16556 w->desired_matrix->no_scrolling_p = 1;
16557
16558 #ifdef GLYPH_DEBUG
16559 debug_method_add (w, "try_window_reusing_current_matrix 1");
16560 #endif
16561 return 1;
16562 }
16563 else if (CHARPOS (new_start) > CHARPOS (start))
16564 {
16565 struct glyph_row *pt_row, *row;
16566 struct glyph_row *first_reusable_row;
16567 struct glyph_row *first_row_to_display;
16568 int dy;
16569 int yb = window_text_bottom_y (w);
16570
16571 /* Find the row starting at new_start, if there is one. Don't
16572 reuse a partially visible line at the end. */
16573 first_reusable_row = start_row;
16574 while (first_reusable_row->enabled_p
16575 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16576 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16577 < CHARPOS (new_start)))
16578 ++first_reusable_row;
16579
16580 /* Give up if there is no row to reuse. */
16581 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16582 || !first_reusable_row->enabled_p
16583 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16584 != CHARPOS (new_start)))
16585 return 0;
16586
16587 /* We can reuse fully visible rows beginning with
16588 first_reusable_row to the end of the window. Set
16589 first_row_to_display to the first row that cannot be reused.
16590 Set pt_row to the row containing point, if there is any. */
16591 pt_row = NULL;
16592 for (first_row_to_display = first_reusable_row;
16593 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16594 ++first_row_to_display)
16595 {
16596 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16597 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16598 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16599 && first_row_to_display->ends_at_zv_p
16600 && pt_row == NULL)))
16601 pt_row = first_row_to_display;
16602 }
16603
16604 /* Start displaying at the start of first_row_to_display. */
16605 eassert (first_row_to_display->y < yb);
16606 init_to_row_start (&it, w, first_row_to_display);
16607
16608 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16609 - start_vpos);
16610 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16611 - nrows_scrolled);
16612 it.current_y = (first_row_to_display->y - first_reusable_row->y
16613 + WINDOW_HEADER_LINE_HEIGHT (w));
16614
16615 /* Display lines beginning with first_row_to_display in the
16616 desired matrix. Set last_text_row to the last row displayed
16617 that displays text. */
16618 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16619 if (pt_row == NULL)
16620 w->cursor.vpos = -1;
16621 last_text_row = NULL;
16622 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16623 if (display_line (&it))
16624 last_text_row = it.glyph_row - 1;
16625
16626 /* If point is in a reused row, adjust y and vpos of the cursor
16627 position. */
16628 if (pt_row)
16629 {
16630 w->cursor.vpos -= nrows_scrolled;
16631 w->cursor.y -= first_reusable_row->y - start_row->y;
16632 }
16633
16634 /* Give up if point isn't in a row displayed or reused. (This
16635 also handles the case where w->cursor.vpos < nrows_scrolled
16636 after the calls to display_line, which can happen with scroll
16637 margins. See bug#1295.) */
16638 if (w->cursor.vpos < 0)
16639 {
16640 clear_glyph_matrix (w->desired_matrix);
16641 return 0;
16642 }
16643
16644 /* Scroll the display. */
16645 run.current_y = first_reusable_row->y;
16646 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16647 run.height = it.last_visible_y - run.current_y;
16648 dy = run.current_y - run.desired_y;
16649
16650 if (run.height)
16651 {
16652 update_begin (f);
16653 FRAME_RIF (f)->update_window_begin_hook (w);
16654 FRAME_RIF (f)->clear_window_mouse_face (w);
16655 FRAME_RIF (f)->scroll_run_hook (w, &run);
16656 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16657 update_end (f);
16658 }
16659
16660 /* Adjust Y positions of reused rows. */
16661 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16662 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16663 max_y = it.last_visible_y;
16664 for (row = first_reusable_row; row < first_row_to_display; ++row)
16665 {
16666 row->y -= dy;
16667 row->visible_height = row->height;
16668 if (row->y < min_y)
16669 row->visible_height -= min_y - row->y;
16670 if (row->y + row->height > max_y)
16671 row->visible_height -= row->y + row->height - max_y;
16672 if (row->fringe_bitmap_periodic_p)
16673 row->redraw_fringe_bitmaps_p = 1;
16674 }
16675
16676 /* Scroll the current matrix. */
16677 eassert (nrows_scrolled > 0);
16678 rotate_matrix (w->current_matrix,
16679 start_vpos,
16680 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16681 -nrows_scrolled);
16682
16683 /* Disable rows not reused. */
16684 for (row -= nrows_scrolled; row < bottom_row; ++row)
16685 row->enabled_p = 0;
16686
16687 /* Point may have moved to a different line, so we cannot assume that
16688 the previous cursor position is valid; locate the correct row. */
16689 if (pt_row)
16690 {
16691 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16692 row < bottom_row
16693 && PT >= MATRIX_ROW_END_CHARPOS (row)
16694 && !row->ends_at_zv_p;
16695 row++)
16696 {
16697 w->cursor.vpos++;
16698 w->cursor.y = row->y;
16699 }
16700 if (row < bottom_row)
16701 {
16702 /* Can't simply scan the row for point with
16703 bidi-reordered glyph rows. Let set_cursor_from_row
16704 figure out where to put the cursor, and if it fails,
16705 give up. */
16706 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16707 {
16708 if (!set_cursor_from_row (w, row, w->current_matrix,
16709 0, 0, 0, 0))
16710 {
16711 clear_glyph_matrix (w->desired_matrix);
16712 return 0;
16713 }
16714 }
16715 else
16716 {
16717 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16718 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16719
16720 for (; glyph < end
16721 && (!BUFFERP (glyph->object)
16722 || glyph->charpos < PT);
16723 glyph++)
16724 {
16725 w->cursor.hpos++;
16726 w->cursor.x += glyph->pixel_width;
16727 }
16728 }
16729 }
16730 }
16731
16732 /* Adjust window end. A null value of last_text_row means that
16733 the window end is in reused rows which in turn means that
16734 only its vpos can have changed. */
16735 if (last_text_row)
16736 adjust_window_ends (w, last_text_row, 0);
16737 else
16738 w->window_end_vpos -= nrows_scrolled;
16739
16740 w->window_end_valid = 0;
16741 w->desired_matrix->no_scrolling_p = 1;
16742
16743 #ifdef GLYPH_DEBUG
16744 debug_method_add (w, "try_window_reusing_current_matrix 2");
16745 #endif
16746 return 1;
16747 }
16748
16749 return 0;
16750 }
16751
16752
16753 \f
16754 /************************************************************************
16755 Window redisplay reusing current matrix when buffer has changed
16756 ************************************************************************/
16757
16758 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16759 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16760 ptrdiff_t *, ptrdiff_t *);
16761 static struct glyph_row *
16762 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16763 struct glyph_row *);
16764
16765
16766 /* Return the last row in MATRIX displaying text. If row START is
16767 non-null, start searching with that row. IT gives the dimensions
16768 of the display. Value is null if matrix is empty; otherwise it is
16769 a pointer to the row found. */
16770
16771 static struct glyph_row *
16772 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16773 struct glyph_row *start)
16774 {
16775 struct glyph_row *row, *row_found;
16776
16777 /* Set row_found to the last row in IT->w's current matrix
16778 displaying text. The loop looks funny but think of partially
16779 visible lines. */
16780 row_found = NULL;
16781 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16782 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16783 {
16784 eassert (row->enabled_p);
16785 row_found = row;
16786 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16787 break;
16788 ++row;
16789 }
16790
16791 return row_found;
16792 }
16793
16794
16795 /* Return the last row in the current matrix of W that is not affected
16796 by changes at the start of current_buffer that occurred since W's
16797 current matrix was built. Value is null if no such row exists.
16798
16799 BEG_UNCHANGED us the number of characters unchanged at the start of
16800 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16801 first changed character in current_buffer. Characters at positions <
16802 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16803 when the current matrix was built. */
16804
16805 static struct glyph_row *
16806 find_last_unchanged_at_beg_row (struct window *w)
16807 {
16808 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16809 struct glyph_row *row;
16810 struct glyph_row *row_found = NULL;
16811 int yb = window_text_bottom_y (w);
16812
16813 /* Find the last row displaying unchanged text. */
16814 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16815 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16816 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16817 ++row)
16818 {
16819 if (/* If row ends before first_changed_pos, it is unchanged,
16820 except in some case. */
16821 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16822 /* When row ends in ZV and we write at ZV it is not
16823 unchanged. */
16824 && !row->ends_at_zv_p
16825 /* When first_changed_pos is the end of a continued line,
16826 row is not unchanged because it may be no longer
16827 continued. */
16828 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16829 && (row->continued_p
16830 || row->exact_window_width_line_p))
16831 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16832 needs to be recomputed, so don't consider this row as
16833 unchanged. This happens when the last line was
16834 bidi-reordered and was killed immediately before this
16835 redisplay cycle. In that case, ROW->end stores the
16836 buffer position of the first visual-order character of
16837 the killed text, which is now beyond ZV. */
16838 && CHARPOS (row->end.pos) <= ZV)
16839 row_found = row;
16840
16841 /* Stop if last visible row. */
16842 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16843 break;
16844 }
16845
16846 return row_found;
16847 }
16848
16849
16850 /* Find the first glyph row in the current matrix of W that is not
16851 affected by changes at the end of current_buffer since the
16852 time W's current matrix was built.
16853
16854 Return in *DELTA the number of chars by which buffer positions in
16855 unchanged text at the end of current_buffer must be adjusted.
16856
16857 Return in *DELTA_BYTES the corresponding number of bytes.
16858
16859 Value is null if no such row exists, i.e. all rows are affected by
16860 changes. */
16861
16862 static struct glyph_row *
16863 find_first_unchanged_at_end_row (struct window *w,
16864 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16865 {
16866 struct glyph_row *row;
16867 struct glyph_row *row_found = NULL;
16868
16869 *delta = *delta_bytes = 0;
16870
16871 /* Display must not have been paused, otherwise the current matrix
16872 is not up to date. */
16873 eassert (w->window_end_valid);
16874
16875 /* A value of window_end_pos >= END_UNCHANGED means that the window
16876 end is in the range of changed text. If so, there is no
16877 unchanged row at the end of W's current matrix. */
16878 if (w->window_end_pos >= END_UNCHANGED)
16879 return NULL;
16880
16881 /* Set row to the last row in W's current matrix displaying text. */
16882 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16883
16884 /* If matrix is entirely empty, no unchanged row exists. */
16885 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16886 {
16887 /* The value of row is the last glyph row in the matrix having a
16888 meaningful buffer position in it. The end position of row
16889 corresponds to window_end_pos. This allows us to translate
16890 buffer positions in the current matrix to current buffer
16891 positions for characters not in changed text. */
16892 ptrdiff_t Z_old =
16893 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16894 ptrdiff_t Z_BYTE_old =
16895 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16896 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16897 struct glyph_row *first_text_row
16898 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16899
16900 *delta = Z - Z_old;
16901 *delta_bytes = Z_BYTE - Z_BYTE_old;
16902
16903 /* Set last_unchanged_pos to the buffer position of the last
16904 character in the buffer that has not been changed. Z is the
16905 index + 1 of the last character in current_buffer, i.e. by
16906 subtracting END_UNCHANGED we get the index of the last
16907 unchanged character, and we have to add BEG to get its buffer
16908 position. */
16909 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16910 last_unchanged_pos_old = last_unchanged_pos - *delta;
16911
16912 /* Search backward from ROW for a row displaying a line that
16913 starts at a minimum position >= last_unchanged_pos_old. */
16914 for (; row > first_text_row; --row)
16915 {
16916 /* This used to abort, but it can happen.
16917 It is ok to just stop the search instead here. KFS. */
16918 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16919 break;
16920
16921 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16922 row_found = row;
16923 }
16924 }
16925
16926 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16927
16928 return row_found;
16929 }
16930
16931
16932 /* Make sure that glyph rows in the current matrix of window W
16933 reference the same glyph memory as corresponding rows in the
16934 frame's frame matrix. This function is called after scrolling W's
16935 current matrix on a terminal frame in try_window_id and
16936 try_window_reusing_current_matrix. */
16937
16938 static void
16939 sync_frame_with_window_matrix_rows (struct window *w)
16940 {
16941 struct frame *f = XFRAME (w->frame);
16942 struct glyph_row *window_row, *window_row_end, *frame_row;
16943
16944 /* Preconditions: W must be a leaf window and full-width. Its frame
16945 must have a frame matrix. */
16946 eassert (BUFFERP (w->contents));
16947 eassert (WINDOW_FULL_WIDTH_P (w));
16948 eassert (!FRAME_WINDOW_P (f));
16949
16950 /* If W is a full-width window, glyph pointers in W's current matrix
16951 have, by definition, to be the same as glyph pointers in the
16952 corresponding frame matrix. Note that frame matrices have no
16953 marginal areas (see build_frame_matrix). */
16954 window_row = w->current_matrix->rows;
16955 window_row_end = window_row + w->current_matrix->nrows;
16956 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16957 while (window_row < window_row_end)
16958 {
16959 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16960 struct glyph *end = window_row->glyphs[LAST_AREA];
16961
16962 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16963 frame_row->glyphs[TEXT_AREA] = start;
16964 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16965 frame_row->glyphs[LAST_AREA] = end;
16966
16967 /* Disable frame rows whose corresponding window rows have
16968 been disabled in try_window_id. */
16969 if (!window_row->enabled_p)
16970 frame_row->enabled_p = 0;
16971
16972 ++window_row, ++frame_row;
16973 }
16974 }
16975
16976
16977 /* Find the glyph row in window W containing CHARPOS. Consider all
16978 rows between START and END (not inclusive). END null means search
16979 all rows to the end of the display area of W. Value is the row
16980 containing CHARPOS or null. */
16981
16982 struct glyph_row *
16983 row_containing_pos (struct window *w, ptrdiff_t charpos,
16984 struct glyph_row *start, struct glyph_row *end, int dy)
16985 {
16986 struct glyph_row *row = start;
16987 struct glyph_row *best_row = NULL;
16988 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16989 int last_y;
16990
16991 /* If we happen to start on a header-line, skip that. */
16992 if (row->mode_line_p)
16993 ++row;
16994
16995 if ((end && row >= end) || !row->enabled_p)
16996 return NULL;
16997
16998 last_y = window_text_bottom_y (w) - dy;
16999
17000 while (1)
17001 {
17002 /* Give up if we have gone too far. */
17003 if (end && row >= end)
17004 return NULL;
17005 /* This formerly returned if they were equal.
17006 I think that both quantities are of a "last plus one" type;
17007 if so, when they are equal, the row is within the screen. -- rms. */
17008 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17009 return NULL;
17010
17011 /* If it is in this row, return this row. */
17012 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17013 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17014 /* The end position of a row equals the start
17015 position of the next row. If CHARPOS is there, we
17016 would rather consider it displayed in the next
17017 line, except when this line ends in ZV. */
17018 && !row_for_charpos_p (row, charpos)))
17019 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17020 {
17021 struct glyph *g;
17022
17023 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17024 || (!best_row && !row->continued_p))
17025 return row;
17026 /* In bidi-reordered rows, there could be several rows whose
17027 edges surround CHARPOS, all of these rows belonging to
17028 the same continued line. We need to find the row which
17029 fits CHARPOS the best. */
17030 for (g = row->glyphs[TEXT_AREA];
17031 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17032 g++)
17033 {
17034 if (!STRINGP (g->object))
17035 {
17036 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17037 {
17038 mindif = eabs (g->charpos - charpos);
17039 best_row = row;
17040 /* Exact match always wins. */
17041 if (mindif == 0)
17042 return best_row;
17043 }
17044 }
17045 }
17046 }
17047 else if (best_row && !row->continued_p)
17048 return best_row;
17049 ++row;
17050 }
17051 }
17052
17053
17054 /* Try to redisplay window W by reusing its existing display. W's
17055 current matrix must be up to date when this function is called,
17056 i.e. window_end_valid must be nonzero.
17057
17058 Value is
17059
17060 1 if display has been updated
17061 0 if otherwise unsuccessful
17062 -1 if redisplay with same window start is known not to succeed
17063
17064 The following steps are performed:
17065
17066 1. Find the last row in the current matrix of W that is not
17067 affected by changes at the start of current_buffer. If no such row
17068 is found, give up.
17069
17070 2. Find the first row in W's current matrix that is not affected by
17071 changes at the end of current_buffer. Maybe there is no such row.
17072
17073 3. Display lines beginning with the row + 1 found in step 1 to the
17074 row found in step 2 or, if step 2 didn't find a row, to the end of
17075 the window.
17076
17077 4. If cursor is not known to appear on the window, give up.
17078
17079 5. If display stopped at the row found in step 2, scroll the
17080 display and current matrix as needed.
17081
17082 6. Maybe display some lines at the end of W, if we must. This can
17083 happen under various circumstances, like a partially visible line
17084 becoming fully visible, or because newly displayed lines are displayed
17085 in smaller font sizes.
17086
17087 7. Update W's window end information. */
17088
17089 static int
17090 try_window_id (struct window *w)
17091 {
17092 struct frame *f = XFRAME (w->frame);
17093 struct glyph_matrix *current_matrix = w->current_matrix;
17094 struct glyph_matrix *desired_matrix = w->desired_matrix;
17095 struct glyph_row *last_unchanged_at_beg_row;
17096 struct glyph_row *first_unchanged_at_end_row;
17097 struct glyph_row *row;
17098 struct glyph_row *bottom_row;
17099 int bottom_vpos;
17100 struct it it;
17101 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17102 int dvpos, dy;
17103 struct text_pos start_pos;
17104 struct run run;
17105 int first_unchanged_at_end_vpos = 0;
17106 struct glyph_row *last_text_row, *last_text_row_at_end;
17107 struct text_pos start;
17108 ptrdiff_t first_changed_charpos, last_changed_charpos;
17109
17110 #ifdef GLYPH_DEBUG
17111 if (inhibit_try_window_id)
17112 return 0;
17113 #endif
17114
17115 /* This is handy for debugging. */
17116 #if 0
17117 #define GIVE_UP(X) \
17118 do { \
17119 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17120 return 0; \
17121 } while (0)
17122 #else
17123 #define GIVE_UP(X) return 0
17124 #endif
17125
17126 SET_TEXT_POS_FROM_MARKER (start, w->start);
17127
17128 /* Don't use this for mini-windows because these can show
17129 messages and mini-buffers, and we don't handle that here. */
17130 if (MINI_WINDOW_P (w))
17131 GIVE_UP (1);
17132
17133 /* This flag is used to prevent redisplay optimizations. */
17134 if (windows_or_buffers_changed || f->cursor_type_changed)
17135 GIVE_UP (2);
17136
17137 /* Verify that narrowing has not changed.
17138 Also verify that we were not told to prevent redisplay optimizations.
17139 It would be nice to further
17140 reduce the number of cases where this prevents try_window_id. */
17141 if (current_buffer->clip_changed
17142 || current_buffer->prevent_redisplay_optimizations_p)
17143 GIVE_UP (3);
17144
17145 /* Window must either use window-based redisplay or be full width. */
17146 if (!FRAME_WINDOW_P (f)
17147 && (!FRAME_LINE_INS_DEL_OK (f)
17148 || !WINDOW_FULL_WIDTH_P (w)))
17149 GIVE_UP (4);
17150
17151 /* Give up if point is known NOT to appear in W. */
17152 if (PT < CHARPOS (start))
17153 GIVE_UP (5);
17154
17155 /* Another way to prevent redisplay optimizations. */
17156 if (w->last_modified == 0)
17157 GIVE_UP (6);
17158
17159 /* Verify that window is not hscrolled. */
17160 if (w->hscroll != 0)
17161 GIVE_UP (7);
17162
17163 /* Verify that display wasn't paused. */
17164 if (!w->window_end_valid)
17165 GIVE_UP (8);
17166
17167 /* Can't use this if highlighting a region because a cursor movement
17168 will do more than just set the cursor. */
17169 if (markpos_of_region () >= 0)
17170 GIVE_UP (9);
17171
17172 /* Likewise if highlighting trailing whitespace. */
17173 if (!NILP (Vshow_trailing_whitespace))
17174 GIVE_UP (11);
17175
17176 /* Likewise if showing a region. */
17177 if (w->region_showing)
17178 GIVE_UP (10);
17179
17180 /* Can't use this if overlay arrow position and/or string have
17181 changed. */
17182 if (overlay_arrows_changed_p ())
17183 GIVE_UP (12);
17184
17185 /* When word-wrap is on, adding a space to the first word of a
17186 wrapped line can change the wrap position, altering the line
17187 above it. It might be worthwhile to handle this more
17188 intelligently, but for now just redisplay from scratch. */
17189 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17190 GIVE_UP (21);
17191
17192 /* Under bidi reordering, adding or deleting a character in the
17193 beginning of a paragraph, before the first strong directional
17194 character, can change the base direction of the paragraph (unless
17195 the buffer specifies a fixed paragraph direction), which will
17196 require to redisplay the whole paragraph. It might be worthwhile
17197 to find the paragraph limits and widen the range of redisplayed
17198 lines to that, but for now just give up this optimization and
17199 redisplay from scratch. */
17200 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17201 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17202 GIVE_UP (22);
17203
17204 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17205 only if buffer has really changed. The reason is that the gap is
17206 initially at Z for freshly visited files. The code below would
17207 set end_unchanged to 0 in that case. */
17208 if (MODIFF > SAVE_MODIFF
17209 /* This seems to happen sometimes after saving a buffer. */
17210 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17211 {
17212 if (GPT - BEG < BEG_UNCHANGED)
17213 BEG_UNCHANGED = GPT - BEG;
17214 if (Z - GPT < END_UNCHANGED)
17215 END_UNCHANGED = Z - GPT;
17216 }
17217
17218 /* The position of the first and last character that has been changed. */
17219 first_changed_charpos = BEG + BEG_UNCHANGED;
17220 last_changed_charpos = Z - END_UNCHANGED;
17221
17222 /* If window starts after a line end, and the last change is in
17223 front of that newline, then changes don't affect the display.
17224 This case happens with stealth-fontification. Note that although
17225 the display is unchanged, glyph positions in the matrix have to
17226 be adjusted, of course. */
17227 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17228 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17229 && ((last_changed_charpos < CHARPOS (start)
17230 && CHARPOS (start) == BEGV)
17231 || (last_changed_charpos < CHARPOS (start) - 1
17232 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17233 {
17234 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17235 struct glyph_row *r0;
17236
17237 /* Compute how many chars/bytes have been added to or removed
17238 from the buffer. */
17239 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17240 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17241 Z_delta = Z - Z_old;
17242 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17243
17244 /* Give up if PT is not in the window. Note that it already has
17245 been checked at the start of try_window_id that PT is not in
17246 front of the window start. */
17247 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17248 GIVE_UP (13);
17249
17250 /* If window start is unchanged, we can reuse the whole matrix
17251 as is, after adjusting glyph positions. No need to compute
17252 the window end again, since its offset from Z hasn't changed. */
17253 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17254 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17255 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17256 /* PT must not be in a partially visible line. */
17257 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17258 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17259 {
17260 /* Adjust positions in the glyph matrix. */
17261 if (Z_delta || Z_delta_bytes)
17262 {
17263 struct glyph_row *r1
17264 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17265 increment_matrix_positions (w->current_matrix,
17266 MATRIX_ROW_VPOS (r0, current_matrix),
17267 MATRIX_ROW_VPOS (r1, current_matrix),
17268 Z_delta, Z_delta_bytes);
17269 }
17270
17271 /* Set the cursor. */
17272 row = row_containing_pos (w, PT, r0, NULL, 0);
17273 if (row)
17274 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17275 else
17276 emacs_abort ();
17277 return 1;
17278 }
17279 }
17280
17281 /* Handle the case that changes are all below what is displayed in
17282 the window, and that PT is in the window. This shortcut cannot
17283 be taken if ZV is visible in the window, and text has been added
17284 there that is visible in the window. */
17285 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17286 /* ZV is not visible in the window, or there are no
17287 changes at ZV, actually. */
17288 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17289 || first_changed_charpos == last_changed_charpos))
17290 {
17291 struct glyph_row *r0;
17292
17293 /* Give up if PT is not in the window. Note that it already has
17294 been checked at the start of try_window_id that PT is not in
17295 front of the window start. */
17296 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17297 GIVE_UP (14);
17298
17299 /* If window start is unchanged, we can reuse the whole matrix
17300 as is, without changing glyph positions since no text has
17301 been added/removed in front of the window end. */
17302 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17303 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17304 /* PT must not be in a partially visible line. */
17305 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17306 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17307 {
17308 /* We have to compute the window end anew since text
17309 could have been added/removed after it. */
17310 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17311 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17312
17313 /* Set the cursor. */
17314 row = row_containing_pos (w, PT, r0, NULL, 0);
17315 if (row)
17316 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17317 else
17318 emacs_abort ();
17319 return 2;
17320 }
17321 }
17322
17323 /* Give up if window start is in the changed area.
17324
17325 The condition used to read
17326
17327 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17328
17329 but why that was tested escapes me at the moment. */
17330 if (CHARPOS (start) >= first_changed_charpos
17331 && CHARPOS (start) <= last_changed_charpos)
17332 GIVE_UP (15);
17333
17334 /* Check that window start agrees with the start of the first glyph
17335 row in its current matrix. Check this after we know the window
17336 start is not in changed text, otherwise positions would not be
17337 comparable. */
17338 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17339 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17340 GIVE_UP (16);
17341
17342 /* Give up if the window ends in strings. Overlay strings
17343 at the end are difficult to handle, so don't try. */
17344 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17345 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17346 GIVE_UP (20);
17347
17348 /* Compute the position at which we have to start displaying new
17349 lines. Some of the lines at the top of the window might be
17350 reusable because they are not displaying changed text. Find the
17351 last row in W's current matrix not affected by changes at the
17352 start of current_buffer. Value is null if changes start in the
17353 first line of window. */
17354 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17355 if (last_unchanged_at_beg_row)
17356 {
17357 /* Avoid starting to display in the middle of a character, a TAB
17358 for instance. This is easier than to set up the iterator
17359 exactly, and it's not a frequent case, so the additional
17360 effort wouldn't really pay off. */
17361 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17362 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17363 && last_unchanged_at_beg_row > w->current_matrix->rows)
17364 --last_unchanged_at_beg_row;
17365
17366 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17367 GIVE_UP (17);
17368
17369 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17370 GIVE_UP (18);
17371 start_pos = it.current.pos;
17372
17373 /* Start displaying new lines in the desired matrix at the same
17374 vpos we would use in the current matrix, i.e. below
17375 last_unchanged_at_beg_row. */
17376 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17377 current_matrix);
17378 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17379 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17380
17381 eassert (it.hpos == 0 && it.current_x == 0);
17382 }
17383 else
17384 {
17385 /* There are no reusable lines at the start of the window.
17386 Start displaying in the first text line. */
17387 start_display (&it, w, start);
17388 it.vpos = it.first_vpos;
17389 start_pos = it.current.pos;
17390 }
17391
17392 /* Find the first row that is not affected by changes at the end of
17393 the buffer. Value will be null if there is no unchanged row, in
17394 which case we must redisplay to the end of the window. delta
17395 will be set to the value by which buffer positions beginning with
17396 first_unchanged_at_end_row have to be adjusted due to text
17397 changes. */
17398 first_unchanged_at_end_row
17399 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17400 IF_DEBUG (debug_delta = delta);
17401 IF_DEBUG (debug_delta_bytes = delta_bytes);
17402
17403 /* Set stop_pos to the buffer position up to which we will have to
17404 display new lines. If first_unchanged_at_end_row != NULL, this
17405 is the buffer position of the start of the line displayed in that
17406 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17407 that we don't stop at a buffer position. */
17408 stop_pos = 0;
17409 if (first_unchanged_at_end_row)
17410 {
17411 eassert (last_unchanged_at_beg_row == NULL
17412 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17413
17414 /* If this is a continuation line, move forward to the next one
17415 that isn't. Changes in lines above affect this line.
17416 Caution: this may move first_unchanged_at_end_row to a row
17417 not displaying text. */
17418 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17419 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17420 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17421 < it.last_visible_y))
17422 ++first_unchanged_at_end_row;
17423
17424 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17425 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17426 >= it.last_visible_y))
17427 first_unchanged_at_end_row = NULL;
17428 else
17429 {
17430 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17431 + delta);
17432 first_unchanged_at_end_vpos
17433 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17434 eassert (stop_pos >= Z - END_UNCHANGED);
17435 }
17436 }
17437 else if (last_unchanged_at_beg_row == NULL)
17438 GIVE_UP (19);
17439
17440
17441 #ifdef GLYPH_DEBUG
17442
17443 /* Either there is no unchanged row at the end, or the one we have
17444 now displays text. This is a necessary condition for the window
17445 end pos calculation at the end of this function. */
17446 eassert (first_unchanged_at_end_row == NULL
17447 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17448
17449 debug_last_unchanged_at_beg_vpos
17450 = (last_unchanged_at_beg_row
17451 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17452 : -1);
17453 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17454
17455 #endif /* GLYPH_DEBUG */
17456
17457
17458 /* Display new lines. Set last_text_row to the last new line
17459 displayed which has text on it, i.e. might end up as being the
17460 line where the window_end_vpos is. */
17461 w->cursor.vpos = -1;
17462 last_text_row = NULL;
17463 overlay_arrow_seen = 0;
17464 while (it.current_y < it.last_visible_y
17465 && !f->fonts_changed
17466 && (first_unchanged_at_end_row == NULL
17467 || IT_CHARPOS (it) < stop_pos))
17468 {
17469 if (display_line (&it))
17470 last_text_row = it.glyph_row - 1;
17471 }
17472
17473 if (f->fonts_changed)
17474 return -1;
17475
17476
17477 /* Compute differences in buffer positions, y-positions etc. for
17478 lines reused at the bottom of the window. Compute what we can
17479 scroll. */
17480 if (first_unchanged_at_end_row
17481 /* No lines reused because we displayed everything up to the
17482 bottom of the window. */
17483 && it.current_y < it.last_visible_y)
17484 {
17485 dvpos = (it.vpos
17486 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17487 current_matrix));
17488 dy = it.current_y - first_unchanged_at_end_row->y;
17489 run.current_y = first_unchanged_at_end_row->y;
17490 run.desired_y = run.current_y + dy;
17491 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17492 }
17493 else
17494 {
17495 delta = delta_bytes = dvpos = dy
17496 = run.current_y = run.desired_y = run.height = 0;
17497 first_unchanged_at_end_row = NULL;
17498 }
17499 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17500
17501
17502 /* Find the cursor if not already found. We have to decide whether
17503 PT will appear on this window (it sometimes doesn't, but this is
17504 not a very frequent case.) This decision has to be made before
17505 the current matrix is altered. A value of cursor.vpos < 0 means
17506 that PT is either in one of the lines beginning at
17507 first_unchanged_at_end_row or below the window. Don't care for
17508 lines that might be displayed later at the window end; as
17509 mentioned, this is not a frequent case. */
17510 if (w->cursor.vpos < 0)
17511 {
17512 /* Cursor in unchanged rows at the top? */
17513 if (PT < CHARPOS (start_pos)
17514 && last_unchanged_at_beg_row)
17515 {
17516 row = row_containing_pos (w, PT,
17517 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17518 last_unchanged_at_beg_row + 1, 0);
17519 if (row)
17520 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17521 }
17522
17523 /* Start from first_unchanged_at_end_row looking for PT. */
17524 else if (first_unchanged_at_end_row)
17525 {
17526 row = row_containing_pos (w, PT - delta,
17527 first_unchanged_at_end_row, NULL, 0);
17528 if (row)
17529 set_cursor_from_row (w, row, w->current_matrix, delta,
17530 delta_bytes, dy, dvpos);
17531 }
17532
17533 /* Give up if cursor was not found. */
17534 if (w->cursor.vpos < 0)
17535 {
17536 clear_glyph_matrix (w->desired_matrix);
17537 return -1;
17538 }
17539 }
17540
17541 /* Don't let the cursor end in the scroll margins. */
17542 {
17543 int this_scroll_margin, cursor_height;
17544 int frame_line_height = default_line_pixel_height (w);
17545 int window_total_lines
17546 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17547
17548 this_scroll_margin =
17549 max (0, min (scroll_margin, window_total_lines / 4));
17550 this_scroll_margin *= frame_line_height;
17551 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17552
17553 if ((w->cursor.y < this_scroll_margin
17554 && CHARPOS (start) > BEGV)
17555 /* Old redisplay didn't take scroll margin into account at the bottom,
17556 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17557 || (w->cursor.y + (make_cursor_line_fully_visible_p
17558 ? cursor_height + this_scroll_margin
17559 : 1)) > it.last_visible_y)
17560 {
17561 w->cursor.vpos = -1;
17562 clear_glyph_matrix (w->desired_matrix);
17563 return -1;
17564 }
17565 }
17566
17567 /* Scroll the display. Do it before changing the current matrix so
17568 that xterm.c doesn't get confused about where the cursor glyph is
17569 found. */
17570 if (dy && run.height)
17571 {
17572 update_begin (f);
17573
17574 if (FRAME_WINDOW_P (f))
17575 {
17576 FRAME_RIF (f)->update_window_begin_hook (w);
17577 FRAME_RIF (f)->clear_window_mouse_face (w);
17578 FRAME_RIF (f)->scroll_run_hook (w, &run);
17579 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17580 }
17581 else
17582 {
17583 /* Terminal frame. In this case, dvpos gives the number of
17584 lines to scroll by; dvpos < 0 means scroll up. */
17585 int from_vpos
17586 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17587 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17588 int end = (WINDOW_TOP_EDGE_LINE (w)
17589 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17590 + window_internal_height (w));
17591
17592 #if defined (HAVE_GPM) || defined (MSDOS)
17593 x_clear_window_mouse_face (w);
17594 #endif
17595 /* Perform the operation on the screen. */
17596 if (dvpos > 0)
17597 {
17598 /* Scroll last_unchanged_at_beg_row to the end of the
17599 window down dvpos lines. */
17600 set_terminal_window (f, end);
17601
17602 /* On dumb terminals delete dvpos lines at the end
17603 before inserting dvpos empty lines. */
17604 if (!FRAME_SCROLL_REGION_OK (f))
17605 ins_del_lines (f, end - dvpos, -dvpos);
17606
17607 /* Insert dvpos empty lines in front of
17608 last_unchanged_at_beg_row. */
17609 ins_del_lines (f, from, dvpos);
17610 }
17611 else if (dvpos < 0)
17612 {
17613 /* Scroll up last_unchanged_at_beg_vpos to the end of
17614 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17615 set_terminal_window (f, end);
17616
17617 /* Delete dvpos lines in front of
17618 last_unchanged_at_beg_vpos. ins_del_lines will set
17619 the cursor to the given vpos and emit |dvpos| delete
17620 line sequences. */
17621 ins_del_lines (f, from + dvpos, dvpos);
17622
17623 /* On a dumb terminal insert dvpos empty lines at the
17624 end. */
17625 if (!FRAME_SCROLL_REGION_OK (f))
17626 ins_del_lines (f, end + dvpos, -dvpos);
17627 }
17628
17629 set_terminal_window (f, 0);
17630 }
17631
17632 update_end (f);
17633 }
17634
17635 /* Shift reused rows of the current matrix to the right position.
17636 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17637 text. */
17638 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17639 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17640 if (dvpos < 0)
17641 {
17642 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17643 bottom_vpos, dvpos);
17644 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17645 bottom_vpos);
17646 }
17647 else if (dvpos > 0)
17648 {
17649 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17650 bottom_vpos, dvpos);
17651 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17652 first_unchanged_at_end_vpos + dvpos);
17653 }
17654
17655 /* For frame-based redisplay, make sure that current frame and window
17656 matrix are in sync with respect to glyph memory. */
17657 if (!FRAME_WINDOW_P (f))
17658 sync_frame_with_window_matrix_rows (w);
17659
17660 /* Adjust buffer positions in reused rows. */
17661 if (delta || delta_bytes)
17662 increment_matrix_positions (current_matrix,
17663 first_unchanged_at_end_vpos + dvpos,
17664 bottom_vpos, delta, delta_bytes);
17665
17666 /* Adjust Y positions. */
17667 if (dy)
17668 shift_glyph_matrix (w, current_matrix,
17669 first_unchanged_at_end_vpos + dvpos,
17670 bottom_vpos, dy);
17671
17672 if (first_unchanged_at_end_row)
17673 {
17674 first_unchanged_at_end_row += dvpos;
17675 if (first_unchanged_at_end_row->y >= it.last_visible_y
17676 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17677 first_unchanged_at_end_row = NULL;
17678 }
17679
17680 /* If scrolling up, there may be some lines to display at the end of
17681 the window. */
17682 last_text_row_at_end = NULL;
17683 if (dy < 0)
17684 {
17685 /* Scrolling up can leave for example a partially visible line
17686 at the end of the window to be redisplayed. */
17687 /* Set last_row to the glyph row in the current matrix where the
17688 window end line is found. It has been moved up or down in
17689 the matrix by dvpos. */
17690 int last_vpos = w->window_end_vpos + dvpos;
17691 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17692
17693 /* If last_row is the window end line, it should display text. */
17694 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17695
17696 /* If window end line was partially visible before, begin
17697 displaying at that line. Otherwise begin displaying with the
17698 line following it. */
17699 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17700 {
17701 init_to_row_start (&it, w, last_row);
17702 it.vpos = last_vpos;
17703 it.current_y = last_row->y;
17704 }
17705 else
17706 {
17707 init_to_row_end (&it, w, last_row);
17708 it.vpos = 1 + last_vpos;
17709 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17710 ++last_row;
17711 }
17712
17713 /* We may start in a continuation line. If so, we have to
17714 get the right continuation_lines_width and current_x. */
17715 it.continuation_lines_width = last_row->continuation_lines_width;
17716 it.hpos = it.current_x = 0;
17717
17718 /* Display the rest of the lines at the window end. */
17719 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17720 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17721 {
17722 /* Is it always sure that the display agrees with lines in
17723 the current matrix? I don't think so, so we mark rows
17724 displayed invalid in the current matrix by setting their
17725 enabled_p flag to zero. */
17726 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17727 if (display_line (&it))
17728 last_text_row_at_end = it.glyph_row - 1;
17729 }
17730 }
17731
17732 /* Update window_end_pos and window_end_vpos. */
17733 if (first_unchanged_at_end_row && !last_text_row_at_end)
17734 {
17735 /* Window end line if one of the preserved rows from the current
17736 matrix. Set row to the last row displaying text in current
17737 matrix starting at first_unchanged_at_end_row, after
17738 scrolling. */
17739 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17740 row = find_last_row_displaying_text (w->current_matrix, &it,
17741 first_unchanged_at_end_row);
17742 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17743 adjust_window_ends (w, row, 1);
17744 eassert (w->window_end_bytepos >= 0);
17745 IF_DEBUG (debug_method_add (w, "A"));
17746 }
17747 else if (last_text_row_at_end)
17748 {
17749 adjust_window_ends (w, last_text_row_at_end, 0);
17750 eassert (w->window_end_bytepos >= 0);
17751 IF_DEBUG (debug_method_add (w, "B"));
17752 }
17753 else if (last_text_row)
17754 {
17755 /* We have displayed either to the end of the window or at the
17756 end of the window, i.e. the last row with text is to be found
17757 in the desired matrix. */
17758 adjust_window_ends (w, last_text_row, 0);
17759 eassert (w->window_end_bytepos >= 0);
17760 }
17761 else if (first_unchanged_at_end_row == NULL
17762 && last_text_row == NULL
17763 && last_text_row_at_end == NULL)
17764 {
17765 /* Displayed to end of window, but no line containing text was
17766 displayed. Lines were deleted at the end of the window. */
17767 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17768 int vpos = w->window_end_vpos;
17769 struct glyph_row *current_row = current_matrix->rows + vpos;
17770 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17771
17772 for (row = NULL;
17773 row == NULL && vpos >= first_vpos;
17774 --vpos, --current_row, --desired_row)
17775 {
17776 if (desired_row->enabled_p)
17777 {
17778 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17779 row = desired_row;
17780 }
17781 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17782 row = current_row;
17783 }
17784
17785 eassert (row != NULL);
17786 w->window_end_vpos = vpos + 1;
17787 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17788 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17789 eassert (w->window_end_bytepos >= 0);
17790 IF_DEBUG (debug_method_add (w, "C"));
17791 }
17792 else
17793 emacs_abort ();
17794
17795 IF_DEBUG (debug_end_pos = w->window_end_pos;
17796 debug_end_vpos = w->window_end_vpos);
17797
17798 /* Record that display has not been completed. */
17799 w->window_end_valid = 0;
17800 w->desired_matrix->no_scrolling_p = 1;
17801 return 3;
17802
17803 #undef GIVE_UP
17804 }
17805
17806
17807 \f
17808 /***********************************************************************
17809 More debugging support
17810 ***********************************************************************/
17811
17812 #ifdef GLYPH_DEBUG
17813
17814 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17815 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17816 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17817
17818
17819 /* Dump the contents of glyph matrix MATRIX on stderr.
17820
17821 GLYPHS 0 means don't show glyph contents.
17822 GLYPHS 1 means show glyphs in short form
17823 GLYPHS > 1 means show glyphs in long form. */
17824
17825 void
17826 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17827 {
17828 int i;
17829 for (i = 0; i < matrix->nrows; ++i)
17830 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17831 }
17832
17833
17834 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17835 the glyph row and area where the glyph comes from. */
17836
17837 void
17838 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17839 {
17840 if (glyph->type == CHAR_GLYPH
17841 || glyph->type == GLYPHLESS_GLYPH)
17842 {
17843 fprintf (stderr,
17844 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17845 glyph - row->glyphs[TEXT_AREA],
17846 (glyph->type == CHAR_GLYPH
17847 ? 'C'
17848 : 'G'),
17849 glyph->charpos,
17850 (BUFFERP (glyph->object)
17851 ? 'B'
17852 : (STRINGP (glyph->object)
17853 ? 'S'
17854 : (INTEGERP (glyph->object)
17855 ? '0'
17856 : '-'))),
17857 glyph->pixel_width,
17858 glyph->u.ch,
17859 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17860 ? glyph->u.ch
17861 : '.'),
17862 glyph->face_id,
17863 glyph->left_box_line_p,
17864 glyph->right_box_line_p);
17865 }
17866 else if (glyph->type == STRETCH_GLYPH)
17867 {
17868 fprintf (stderr,
17869 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17870 glyph - row->glyphs[TEXT_AREA],
17871 'S',
17872 glyph->charpos,
17873 (BUFFERP (glyph->object)
17874 ? 'B'
17875 : (STRINGP (glyph->object)
17876 ? 'S'
17877 : (INTEGERP (glyph->object)
17878 ? '0'
17879 : '-'))),
17880 glyph->pixel_width,
17881 0,
17882 ' ',
17883 glyph->face_id,
17884 glyph->left_box_line_p,
17885 glyph->right_box_line_p);
17886 }
17887 else if (glyph->type == IMAGE_GLYPH)
17888 {
17889 fprintf (stderr,
17890 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17891 glyph - row->glyphs[TEXT_AREA],
17892 'I',
17893 glyph->charpos,
17894 (BUFFERP (glyph->object)
17895 ? 'B'
17896 : (STRINGP (glyph->object)
17897 ? 'S'
17898 : (INTEGERP (glyph->object)
17899 ? '0'
17900 : '-'))),
17901 glyph->pixel_width,
17902 glyph->u.img_id,
17903 '.',
17904 glyph->face_id,
17905 glyph->left_box_line_p,
17906 glyph->right_box_line_p);
17907 }
17908 else if (glyph->type == COMPOSITE_GLYPH)
17909 {
17910 fprintf (stderr,
17911 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17912 glyph - row->glyphs[TEXT_AREA],
17913 '+',
17914 glyph->charpos,
17915 (BUFFERP (glyph->object)
17916 ? 'B'
17917 : (STRINGP (glyph->object)
17918 ? 'S'
17919 : (INTEGERP (glyph->object)
17920 ? '0'
17921 : '-'))),
17922 glyph->pixel_width,
17923 glyph->u.cmp.id);
17924 if (glyph->u.cmp.automatic)
17925 fprintf (stderr,
17926 "[%d-%d]",
17927 glyph->slice.cmp.from, glyph->slice.cmp.to);
17928 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17929 glyph->face_id,
17930 glyph->left_box_line_p,
17931 glyph->right_box_line_p);
17932 }
17933 }
17934
17935
17936 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17937 GLYPHS 0 means don't show glyph contents.
17938 GLYPHS 1 means show glyphs in short form
17939 GLYPHS > 1 means show glyphs in long form. */
17940
17941 void
17942 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17943 {
17944 if (glyphs != 1)
17945 {
17946 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17947 fprintf (stderr, "==============================================================================\n");
17948
17949 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17950 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17951 vpos,
17952 MATRIX_ROW_START_CHARPOS (row),
17953 MATRIX_ROW_END_CHARPOS (row),
17954 row->used[TEXT_AREA],
17955 row->contains_overlapping_glyphs_p,
17956 row->enabled_p,
17957 row->truncated_on_left_p,
17958 row->truncated_on_right_p,
17959 row->continued_p,
17960 MATRIX_ROW_CONTINUATION_LINE_P (row),
17961 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17962 row->ends_at_zv_p,
17963 row->fill_line_p,
17964 row->ends_in_middle_of_char_p,
17965 row->starts_in_middle_of_char_p,
17966 row->mouse_face_p,
17967 row->x,
17968 row->y,
17969 row->pixel_width,
17970 row->height,
17971 row->visible_height,
17972 row->ascent,
17973 row->phys_ascent);
17974 /* The next 3 lines should align to "Start" in the header. */
17975 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17976 row->end.overlay_string_index,
17977 row->continuation_lines_width);
17978 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17979 CHARPOS (row->start.string_pos),
17980 CHARPOS (row->end.string_pos));
17981 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17982 row->end.dpvec_index);
17983 }
17984
17985 if (glyphs > 1)
17986 {
17987 int area;
17988
17989 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17990 {
17991 struct glyph *glyph = row->glyphs[area];
17992 struct glyph *glyph_end = glyph + row->used[area];
17993
17994 /* Glyph for a line end in text. */
17995 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17996 ++glyph_end;
17997
17998 if (glyph < glyph_end)
17999 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18000
18001 for (; glyph < glyph_end; ++glyph)
18002 dump_glyph (row, glyph, area);
18003 }
18004 }
18005 else if (glyphs == 1)
18006 {
18007 int area;
18008
18009 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18010 {
18011 char *s = alloca (row->used[area] + 4);
18012 int i;
18013
18014 for (i = 0; i < row->used[area]; ++i)
18015 {
18016 struct glyph *glyph = row->glyphs[area] + i;
18017 if (i == row->used[area] - 1
18018 && area == TEXT_AREA
18019 && INTEGERP (glyph->object)
18020 && glyph->type == CHAR_GLYPH
18021 && glyph->u.ch == ' ')
18022 {
18023 strcpy (&s[i], "[\\n]");
18024 i += 4;
18025 }
18026 else if (glyph->type == CHAR_GLYPH
18027 && glyph->u.ch < 0x80
18028 && glyph->u.ch >= ' ')
18029 s[i] = glyph->u.ch;
18030 else
18031 s[i] = '.';
18032 }
18033
18034 s[i] = '\0';
18035 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18036 }
18037 }
18038 }
18039
18040
18041 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18042 Sdump_glyph_matrix, 0, 1, "p",
18043 doc: /* Dump the current matrix of the selected window to stderr.
18044 Shows contents of glyph row structures. With non-nil
18045 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18046 glyphs in short form, otherwise show glyphs in long form. */)
18047 (Lisp_Object glyphs)
18048 {
18049 struct window *w = XWINDOW (selected_window);
18050 struct buffer *buffer = XBUFFER (w->contents);
18051
18052 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18053 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18054 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18055 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18056 fprintf (stderr, "=============================================\n");
18057 dump_glyph_matrix (w->current_matrix,
18058 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18059 return Qnil;
18060 }
18061
18062
18063 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18064 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18065 (void)
18066 {
18067 struct frame *f = XFRAME (selected_frame);
18068 dump_glyph_matrix (f->current_matrix, 1);
18069 return Qnil;
18070 }
18071
18072
18073 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18074 doc: /* Dump glyph row ROW to stderr.
18075 GLYPH 0 means don't dump glyphs.
18076 GLYPH 1 means dump glyphs in short form.
18077 GLYPH > 1 or omitted means dump glyphs in long form. */)
18078 (Lisp_Object row, Lisp_Object glyphs)
18079 {
18080 struct glyph_matrix *matrix;
18081 EMACS_INT vpos;
18082
18083 CHECK_NUMBER (row);
18084 matrix = XWINDOW (selected_window)->current_matrix;
18085 vpos = XINT (row);
18086 if (vpos >= 0 && vpos < matrix->nrows)
18087 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18088 vpos,
18089 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18090 return Qnil;
18091 }
18092
18093
18094 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18095 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18096 GLYPH 0 means don't dump glyphs.
18097 GLYPH 1 means dump glyphs in short form.
18098 GLYPH > 1 or omitted means dump glyphs in long form. */)
18099 (Lisp_Object row, Lisp_Object glyphs)
18100 {
18101 struct frame *sf = SELECTED_FRAME ();
18102 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18103 EMACS_INT vpos;
18104
18105 CHECK_NUMBER (row);
18106 vpos = XINT (row);
18107 if (vpos >= 0 && vpos < m->nrows)
18108 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18109 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18110 return Qnil;
18111 }
18112
18113
18114 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18115 doc: /* Toggle tracing of redisplay.
18116 With ARG, turn tracing on if and only if ARG is positive. */)
18117 (Lisp_Object arg)
18118 {
18119 if (NILP (arg))
18120 trace_redisplay_p = !trace_redisplay_p;
18121 else
18122 {
18123 arg = Fprefix_numeric_value (arg);
18124 trace_redisplay_p = XINT (arg) > 0;
18125 }
18126
18127 return Qnil;
18128 }
18129
18130
18131 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18132 doc: /* Like `format', but print result to stderr.
18133 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18134 (ptrdiff_t nargs, Lisp_Object *args)
18135 {
18136 Lisp_Object s = Fformat (nargs, args);
18137 fprintf (stderr, "%s", SDATA (s));
18138 return Qnil;
18139 }
18140
18141 #endif /* GLYPH_DEBUG */
18142
18143
18144 \f
18145 /***********************************************************************
18146 Building Desired Matrix Rows
18147 ***********************************************************************/
18148
18149 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18150 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18151
18152 static struct glyph_row *
18153 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18154 {
18155 struct frame *f = XFRAME (WINDOW_FRAME (w));
18156 struct buffer *buffer = XBUFFER (w->contents);
18157 struct buffer *old = current_buffer;
18158 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18159 int arrow_len = SCHARS (overlay_arrow_string);
18160 const unsigned char *arrow_end = arrow_string + arrow_len;
18161 const unsigned char *p;
18162 struct it it;
18163 bool multibyte_p;
18164 int n_glyphs_before;
18165
18166 set_buffer_temp (buffer);
18167 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18168 it.glyph_row->used[TEXT_AREA] = 0;
18169 SET_TEXT_POS (it.position, 0, 0);
18170
18171 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18172 p = arrow_string;
18173 while (p < arrow_end)
18174 {
18175 Lisp_Object face, ilisp;
18176
18177 /* Get the next character. */
18178 if (multibyte_p)
18179 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18180 else
18181 {
18182 it.c = it.char_to_display = *p, it.len = 1;
18183 if (! ASCII_CHAR_P (it.c))
18184 it.char_to_display = BYTE8_TO_CHAR (it.c);
18185 }
18186 p += it.len;
18187
18188 /* Get its face. */
18189 ilisp = make_number (p - arrow_string);
18190 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18191 it.face_id = compute_char_face (f, it.char_to_display, face);
18192
18193 /* Compute its width, get its glyphs. */
18194 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18195 SET_TEXT_POS (it.position, -1, -1);
18196 PRODUCE_GLYPHS (&it);
18197
18198 /* If this character doesn't fit any more in the line, we have
18199 to remove some glyphs. */
18200 if (it.current_x > it.last_visible_x)
18201 {
18202 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18203 break;
18204 }
18205 }
18206
18207 set_buffer_temp (old);
18208 return it.glyph_row;
18209 }
18210
18211
18212 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18213 glyphs to insert is determined by produce_special_glyphs. */
18214
18215 static void
18216 insert_left_trunc_glyphs (struct it *it)
18217 {
18218 struct it truncate_it;
18219 struct glyph *from, *end, *to, *toend;
18220
18221 eassert (!FRAME_WINDOW_P (it->f)
18222 || (!it->glyph_row->reversed_p
18223 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18224 || (it->glyph_row->reversed_p
18225 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18226
18227 /* Get the truncation glyphs. */
18228 truncate_it = *it;
18229 truncate_it.current_x = 0;
18230 truncate_it.face_id = DEFAULT_FACE_ID;
18231 truncate_it.glyph_row = &scratch_glyph_row;
18232 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18233 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18234 truncate_it.object = make_number (0);
18235 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18236
18237 /* Overwrite glyphs from IT with truncation glyphs. */
18238 if (!it->glyph_row->reversed_p)
18239 {
18240 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18241
18242 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18243 end = from + tused;
18244 to = it->glyph_row->glyphs[TEXT_AREA];
18245 toend = to + it->glyph_row->used[TEXT_AREA];
18246 if (FRAME_WINDOW_P (it->f))
18247 {
18248 /* On GUI frames, when variable-size fonts are displayed,
18249 the truncation glyphs may need more pixels than the row's
18250 glyphs they overwrite. We overwrite more glyphs to free
18251 enough screen real estate, and enlarge the stretch glyph
18252 on the right (see display_line), if there is one, to
18253 preserve the screen position of the truncation glyphs on
18254 the right. */
18255 int w = 0;
18256 struct glyph *g = to;
18257 short used;
18258
18259 /* The first glyph could be partially visible, in which case
18260 it->glyph_row->x will be negative. But we want the left
18261 truncation glyphs to be aligned at the left margin of the
18262 window, so we override the x coordinate at which the row
18263 will begin. */
18264 it->glyph_row->x = 0;
18265 while (g < toend && w < it->truncation_pixel_width)
18266 {
18267 w += g->pixel_width;
18268 ++g;
18269 }
18270 if (g - to - tused > 0)
18271 {
18272 memmove (to + tused, g, (toend - g) * sizeof(*g));
18273 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18274 }
18275 used = it->glyph_row->used[TEXT_AREA];
18276 if (it->glyph_row->truncated_on_right_p
18277 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18278 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18279 == STRETCH_GLYPH)
18280 {
18281 int extra = w - it->truncation_pixel_width;
18282
18283 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18284 }
18285 }
18286
18287 while (from < end)
18288 *to++ = *from++;
18289
18290 /* There may be padding glyphs left over. Overwrite them too. */
18291 if (!FRAME_WINDOW_P (it->f))
18292 {
18293 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18294 {
18295 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18296 while (from < end)
18297 *to++ = *from++;
18298 }
18299 }
18300
18301 if (to > toend)
18302 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18303 }
18304 else
18305 {
18306 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18307
18308 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18309 that back to front. */
18310 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18311 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18312 toend = it->glyph_row->glyphs[TEXT_AREA];
18313 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18314 if (FRAME_WINDOW_P (it->f))
18315 {
18316 int w = 0;
18317 struct glyph *g = to;
18318
18319 while (g >= toend && w < it->truncation_pixel_width)
18320 {
18321 w += g->pixel_width;
18322 --g;
18323 }
18324 if (to - g - tused > 0)
18325 to = g + tused;
18326 if (it->glyph_row->truncated_on_right_p
18327 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18328 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18329 {
18330 int extra = w - it->truncation_pixel_width;
18331
18332 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18333 }
18334 }
18335
18336 while (from >= end && to >= toend)
18337 *to-- = *from--;
18338 if (!FRAME_WINDOW_P (it->f))
18339 {
18340 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18341 {
18342 from =
18343 truncate_it.glyph_row->glyphs[TEXT_AREA]
18344 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18345 while (from >= end && to >= toend)
18346 *to-- = *from--;
18347 }
18348 }
18349 if (from >= end)
18350 {
18351 /* Need to free some room before prepending additional
18352 glyphs. */
18353 int move_by = from - end + 1;
18354 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18355 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18356
18357 for ( ; g >= g0; g--)
18358 g[move_by] = *g;
18359 while (from >= end)
18360 *to-- = *from--;
18361 it->glyph_row->used[TEXT_AREA] += move_by;
18362 }
18363 }
18364 }
18365
18366 /* Compute the hash code for ROW. */
18367 unsigned
18368 row_hash (struct glyph_row *row)
18369 {
18370 int area, k;
18371 unsigned hashval = 0;
18372
18373 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18374 for (k = 0; k < row->used[area]; ++k)
18375 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18376 + row->glyphs[area][k].u.val
18377 + row->glyphs[area][k].face_id
18378 + row->glyphs[area][k].padding_p
18379 + (row->glyphs[area][k].type << 2));
18380
18381 return hashval;
18382 }
18383
18384 /* Compute the pixel height and width of IT->glyph_row.
18385
18386 Most of the time, ascent and height of a display line will be equal
18387 to the max_ascent and max_height values of the display iterator
18388 structure. This is not the case if
18389
18390 1. We hit ZV without displaying anything. In this case, max_ascent
18391 and max_height will be zero.
18392
18393 2. We have some glyphs that don't contribute to the line height.
18394 (The glyph row flag contributes_to_line_height_p is for future
18395 pixmap extensions).
18396
18397 The first case is easily covered by using default values because in
18398 these cases, the line height does not really matter, except that it
18399 must not be zero. */
18400
18401 static void
18402 compute_line_metrics (struct it *it)
18403 {
18404 struct glyph_row *row = it->glyph_row;
18405
18406 if (FRAME_WINDOW_P (it->f))
18407 {
18408 int i, min_y, max_y;
18409
18410 /* The line may consist of one space only, that was added to
18411 place the cursor on it. If so, the row's height hasn't been
18412 computed yet. */
18413 if (row->height == 0)
18414 {
18415 if (it->max_ascent + it->max_descent == 0)
18416 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18417 row->ascent = it->max_ascent;
18418 row->height = it->max_ascent + it->max_descent;
18419 row->phys_ascent = it->max_phys_ascent;
18420 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18421 row->extra_line_spacing = it->max_extra_line_spacing;
18422 }
18423
18424 /* Compute the width of this line. */
18425 row->pixel_width = row->x;
18426 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18427 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18428
18429 eassert (row->pixel_width >= 0);
18430 eassert (row->ascent >= 0 && row->height > 0);
18431
18432 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18433 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18434
18435 /* If first line's physical ascent is larger than its logical
18436 ascent, use the physical ascent, and make the row taller.
18437 This makes accented characters fully visible. */
18438 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18439 && row->phys_ascent > row->ascent)
18440 {
18441 row->height += row->phys_ascent - row->ascent;
18442 row->ascent = row->phys_ascent;
18443 }
18444
18445 /* Compute how much of the line is visible. */
18446 row->visible_height = row->height;
18447
18448 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18449 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18450
18451 if (row->y < min_y)
18452 row->visible_height -= min_y - row->y;
18453 if (row->y + row->height > max_y)
18454 row->visible_height -= row->y + row->height - max_y;
18455 }
18456 else
18457 {
18458 row->pixel_width = row->used[TEXT_AREA];
18459 if (row->continued_p)
18460 row->pixel_width -= it->continuation_pixel_width;
18461 else if (row->truncated_on_right_p)
18462 row->pixel_width -= it->truncation_pixel_width;
18463 row->ascent = row->phys_ascent = 0;
18464 row->height = row->phys_height = row->visible_height = 1;
18465 row->extra_line_spacing = 0;
18466 }
18467
18468 /* Compute a hash code for this row. */
18469 row->hash = row_hash (row);
18470
18471 it->max_ascent = it->max_descent = 0;
18472 it->max_phys_ascent = it->max_phys_descent = 0;
18473 }
18474
18475
18476 /* Append one space to the glyph row of iterator IT if doing a
18477 window-based redisplay. The space has the same face as
18478 IT->face_id. Value is non-zero if a space was added.
18479
18480 This function is called to make sure that there is always one glyph
18481 at the end of a glyph row that the cursor can be set on under
18482 window-systems. (If there weren't such a glyph we would not know
18483 how wide and tall a box cursor should be displayed).
18484
18485 At the same time this space let's a nicely handle clearing to the
18486 end of the line if the row ends in italic text. */
18487
18488 static int
18489 append_space_for_newline (struct it *it, int default_face_p)
18490 {
18491 if (FRAME_WINDOW_P (it->f))
18492 {
18493 int n = it->glyph_row->used[TEXT_AREA];
18494
18495 if (it->glyph_row->glyphs[TEXT_AREA] + n
18496 < it->glyph_row->glyphs[1 + TEXT_AREA])
18497 {
18498 /* Save some values that must not be changed.
18499 Must save IT->c and IT->len because otherwise
18500 ITERATOR_AT_END_P wouldn't work anymore after
18501 append_space_for_newline has been called. */
18502 enum display_element_type saved_what = it->what;
18503 int saved_c = it->c, saved_len = it->len;
18504 int saved_char_to_display = it->char_to_display;
18505 int saved_x = it->current_x;
18506 int saved_face_id = it->face_id;
18507 int saved_box_end = it->end_of_box_run_p;
18508 struct text_pos saved_pos;
18509 Lisp_Object saved_object;
18510 struct face *face;
18511
18512 saved_object = it->object;
18513 saved_pos = it->position;
18514
18515 it->what = IT_CHARACTER;
18516 memset (&it->position, 0, sizeof it->position);
18517 it->object = make_number (0);
18518 it->c = it->char_to_display = ' ';
18519 it->len = 1;
18520
18521 /* If the default face was remapped, be sure to use the
18522 remapped face for the appended newline. */
18523 if (default_face_p)
18524 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18525 else if (it->face_before_selective_p)
18526 it->face_id = it->saved_face_id;
18527 face = FACE_FROM_ID (it->f, it->face_id);
18528 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18529 /* In R2L rows, we will prepend a stretch glyph that will
18530 have the end_of_box_run_p flag set for it, so there's no
18531 need for the appended newline glyph to have that flag
18532 set. */
18533 if (it->glyph_row->reversed_p
18534 /* But if the appended newline glyph goes all the way to
18535 the end of the row, there will be no stretch glyph,
18536 so leave the box flag set. */
18537 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18538 it->end_of_box_run_p = 0;
18539
18540 PRODUCE_GLYPHS (it);
18541
18542 it->override_ascent = -1;
18543 it->constrain_row_ascent_descent_p = 0;
18544 it->current_x = saved_x;
18545 it->object = saved_object;
18546 it->position = saved_pos;
18547 it->what = saved_what;
18548 it->face_id = saved_face_id;
18549 it->len = saved_len;
18550 it->c = saved_c;
18551 it->char_to_display = saved_char_to_display;
18552 it->end_of_box_run_p = saved_box_end;
18553 return 1;
18554 }
18555 }
18556
18557 return 0;
18558 }
18559
18560
18561 /* Extend the face of the last glyph in the text area of IT->glyph_row
18562 to the end of the display line. Called from display_line. If the
18563 glyph row is empty, add a space glyph to it so that we know the
18564 face to draw. Set the glyph row flag fill_line_p. If the glyph
18565 row is R2L, prepend a stretch glyph to cover the empty space to the
18566 left of the leftmost glyph. */
18567
18568 static void
18569 extend_face_to_end_of_line (struct it *it)
18570 {
18571 struct face *face, *default_face;
18572 struct frame *f = it->f;
18573
18574 /* If line is already filled, do nothing. Non window-system frames
18575 get a grace of one more ``pixel'' because their characters are
18576 1-``pixel'' wide, so they hit the equality too early. This grace
18577 is needed only for R2L rows that are not continued, to produce
18578 one extra blank where we could display the cursor. */
18579 if (it->current_x >= it->last_visible_x
18580 + (!FRAME_WINDOW_P (f)
18581 && it->glyph_row->reversed_p
18582 && !it->glyph_row->continued_p))
18583 return;
18584
18585 /* The default face, possibly remapped. */
18586 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18587
18588 /* Face extension extends the background and box of IT->face_id
18589 to the end of the line. If the background equals the background
18590 of the frame, we don't have to do anything. */
18591 if (it->face_before_selective_p)
18592 face = FACE_FROM_ID (f, it->saved_face_id);
18593 else
18594 face = FACE_FROM_ID (f, it->face_id);
18595
18596 if (FRAME_WINDOW_P (f)
18597 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18598 && face->box == FACE_NO_BOX
18599 && face->background == FRAME_BACKGROUND_PIXEL (f)
18600 && !face->stipple
18601 && !it->glyph_row->reversed_p)
18602 return;
18603
18604 /* Set the glyph row flag indicating that the face of the last glyph
18605 in the text area has to be drawn to the end of the text area. */
18606 it->glyph_row->fill_line_p = 1;
18607
18608 /* If current character of IT is not ASCII, make sure we have the
18609 ASCII face. This will be automatically undone the next time
18610 get_next_display_element returns a multibyte character. Note
18611 that the character will always be single byte in unibyte
18612 text. */
18613 if (!ASCII_CHAR_P (it->c))
18614 {
18615 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18616 }
18617
18618 if (FRAME_WINDOW_P (f))
18619 {
18620 /* If the row is empty, add a space with the current face of IT,
18621 so that we know which face to draw. */
18622 if (it->glyph_row->used[TEXT_AREA] == 0)
18623 {
18624 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18625 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18626 it->glyph_row->used[TEXT_AREA] = 1;
18627 }
18628 #ifdef HAVE_WINDOW_SYSTEM
18629 if (it->glyph_row->reversed_p)
18630 {
18631 /* Prepend a stretch glyph to the row, such that the
18632 rightmost glyph will be drawn flushed all the way to the
18633 right margin of the window. The stretch glyph that will
18634 occupy the empty space, if any, to the left of the
18635 glyphs. */
18636 struct font *font = face->font ? face->font : FRAME_FONT (f);
18637 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18638 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18639 struct glyph *g;
18640 int row_width, stretch_ascent, stretch_width;
18641 struct text_pos saved_pos;
18642 int saved_face_id, saved_avoid_cursor, saved_box_start;
18643
18644 for (row_width = 0, g = row_start; g < row_end; g++)
18645 row_width += g->pixel_width;
18646 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18647 if (stretch_width > 0)
18648 {
18649 stretch_ascent =
18650 (((it->ascent + it->descent)
18651 * FONT_BASE (font)) / FONT_HEIGHT (font));
18652 saved_pos = it->position;
18653 memset (&it->position, 0, sizeof it->position);
18654 saved_avoid_cursor = it->avoid_cursor_p;
18655 it->avoid_cursor_p = 1;
18656 saved_face_id = it->face_id;
18657 saved_box_start = it->start_of_box_run_p;
18658 /* The last row's stretch glyph should get the default
18659 face, to avoid painting the rest of the window with
18660 the region face, if the region ends at ZV. */
18661 if (it->glyph_row->ends_at_zv_p)
18662 it->face_id = default_face->id;
18663 else
18664 it->face_id = face->id;
18665 it->start_of_box_run_p = 0;
18666 append_stretch_glyph (it, make_number (0), stretch_width,
18667 it->ascent + it->descent, stretch_ascent);
18668 it->position = saved_pos;
18669 it->avoid_cursor_p = saved_avoid_cursor;
18670 it->face_id = saved_face_id;
18671 it->start_of_box_run_p = saved_box_start;
18672 }
18673 }
18674 #endif /* HAVE_WINDOW_SYSTEM */
18675 }
18676 else
18677 {
18678 /* Save some values that must not be changed. */
18679 int saved_x = it->current_x;
18680 struct text_pos saved_pos;
18681 Lisp_Object saved_object;
18682 enum display_element_type saved_what = it->what;
18683 int saved_face_id = it->face_id;
18684
18685 saved_object = it->object;
18686 saved_pos = it->position;
18687
18688 it->what = IT_CHARACTER;
18689 memset (&it->position, 0, sizeof it->position);
18690 it->object = make_number (0);
18691 it->c = it->char_to_display = ' ';
18692 it->len = 1;
18693 /* The last row's blank glyphs should get the default face, to
18694 avoid painting the rest of the window with the region face,
18695 if the region ends at ZV. */
18696 if (it->glyph_row->ends_at_zv_p)
18697 it->face_id = default_face->id;
18698 else
18699 it->face_id = face->id;
18700
18701 PRODUCE_GLYPHS (it);
18702
18703 while (it->current_x <= it->last_visible_x)
18704 PRODUCE_GLYPHS (it);
18705
18706 /* Don't count these blanks really. It would let us insert a left
18707 truncation glyph below and make us set the cursor on them, maybe. */
18708 it->current_x = saved_x;
18709 it->object = saved_object;
18710 it->position = saved_pos;
18711 it->what = saved_what;
18712 it->face_id = saved_face_id;
18713 }
18714 }
18715
18716
18717 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18718 trailing whitespace. */
18719
18720 static int
18721 trailing_whitespace_p (ptrdiff_t charpos)
18722 {
18723 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18724 int c = 0;
18725
18726 while (bytepos < ZV_BYTE
18727 && (c = FETCH_CHAR (bytepos),
18728 c == ' ' || c == '\t'))
18729 ++bytepos;
18730
18731 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18732 {
18733 if (bytepos != PT_BYTE)
18734 return 1;
18735 }
18736 return 0;
18737 }
18738
18739
18740 /* Highlight trailing whitespace, if any, in ROW. */
18741
18742 static void
18743 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18744 {
18745 int used = row->used[TEXT_AREA];
18746
18747 if (used)
18748 {
18749 struct glyph *start = row->glyphs[TEXT_AREA];
18750 struct glyph *glyph = start + used - 1;
18751
18752 if (row->reversed_p)
18753 {
18754 /* Right-to-left rows need to be processed in the opposite
18755 direction, so swap the edge pointers. */
18756 glyph = start;
18757 start = row->glyphs[TEXT_AREA] + used - 1;
18758 }
18759
18760 /* Skip over glyphs inserted to display the cursor at the
18761 end of a line, for extending the face of the last glyph
18762 to the end of the line on terminals, and for truncation
18763 and continuation glyphs. */
18764 if (!row->reversed_p)
18765 {
18766 while (glyph >= start
18767 && glyph->type == CHAR_GLYPH
18768 && INTEGERP (glyph->object))
18769 --glyph;
18770 }
18771 else
18772 {
18773 while (glyph <= start
18774 && glyph->type == CHAR_GLYPH
18775 && INTEGERP (glyph->object))
18776 ++glyph;
18777 }
18778
18779 /* If last glyph is a space or stretch, and it's trailing
18780 whitespace, set the face of all trailing whitespace glyphs in
18781 IT->glyph_row to `trailing-whitespace'. */
18782 if ((row->reversed_p ? glyph <= start : glyph >= start)
18783 && BUFFERP (glyph->object)
18784 && (glyph->type == STRETCH_GLYPH
18785 || (glyph->type == CHAR_GLYPH
18786 && glyph->u.ch == ' '))
18787 && trailing_whitespace_p (glyph->charpos))
18788 {
18789 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18790 if (face_id < 0)
18791 return;
18792
18793 if (!row->reversed_p)
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 else
18803 {
18804 while (glyph <= start
18805 && BUFFERP (glyph->object)
18806 && (glyph->type == STRETCH_GLYPH
18807 || (glyph->type == CHAR_GLYPH
18808 && glyph->u.ch == ' ')))
18809 (glyph++)->face_id = face_id;
18810 }
18811 }
18812 }
18813 }
18814
18815
18816 /* Value is non-zero if glyph row ROW should be
18817 considered to hold the buffer position CHARPOS. */
18818
18819 static int
18820 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18821 {
18822 int result = 1;
18823
18824 if (charpos == CHARPOS (row->end.pos)
18825 || charpos == MATRIX_ROW_END_CHARPOS (row))
18826 {
18827 /* Suppose the row ends on a string.
18828 Unless the row is continued, that means it ends on a newline
18829 in the string. If it's anything other than a display string
18830 (e.g., a before-string from an overlay), we don't want the
18831 cursor there. (This heuristic seems to give the optimal
18832 behavior for the various types of multi-line strings.)
18833 One exception: if the string has `cursor' property on one of
18834 its characters, we _do_ want the cursor there. */
18835 if (CHARPOS (row->end.string_pos) >= 0)
18836 {
18837 if (row->continued_p)
18838 result = 1;
18839 else
18840 {
18841 /* Check for `display' property. */
18842 struct glyph *beg = row->glyphs[TEXT_AREA];
18843 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18844 struct glyph *glyph;
18845
18846 result = 0;
18847 for (glyph = end; glyph >= beg; --glyph)
18848 if (STRINGP (glyph->object))
18849 {
18850 Lisp_Object prop
18851 = Fget_char_property (make_number (charpos),
18852 Qdisplay, Qnil);
18853 result =
18854 (!NILP (prop)
18855 && display_prop_string_p (prop, glyph->object));
18856 /* If there's a `cursor' property on one of the
18857 string's characters, this row is a cursor row,
18858 even though this is not a display string. */
18859 if (!result)
18860 {
18861 Lisp_Object s = glyph->object;
18862
18863 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18864 {
18865 ptrdiff_t gpos = glyph->charpos;
18866
18867 if (!NILP (Fget_char_property (make_number (gpos),
18868 Qcursor, s)))
18869 {
18870 result = 1;
18871 break;
18872 }
18873 }
18874 }
18875 break;
18876 }
18877 }
18878 }
18879 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18880 {
18881 /* If the row ends in middle of a real character,
18882 and the line is continued, we want the cursor here.
18883 That's because CHARPOS (ROW->end.pos) would equal
18884 PT if PT is before the character. */
18885 if (!row->ends_in_ellipsis_p)
18886 result = row->continued_p;
18887 else
18888 /* If the row ends in an ellipsis, then
18889 CHARPOS (ROW->end.pos) will equal point after the
18890 invisible text. We want that position to be displayed
18891 after the ellipsis. */
18892 result = 0;
18893 }
18894 /* If the row ends at ZV, display the cursor at the end of that
18895 row instead of at the start of the row below. */
18896 else if (row->ends_at_zv_p)
18897 result = 1;
18898 else
18899 result = 0;
18900 }
18901
18902 return result;
18903 }
18904
18905 /* Value is non-zero if glyph row ROW should be
18906 used to hold the cursor. */
18907
18908 static int
18909 cursor_row_p (struct glyph_row *row)
18910 {
18911 return row_for_charpos_p (row, PT);
18912 }
18913
18914 \f
18915
18916 /* Push the property PROP so that it will be rendered at the current
18917 position in IT. Return 1 if PROP was successfully pushed, 0
18918 otherwise. Called from handle_line_prefix to handle the
18919 `line-prefix' and `wrap-prefix' properties. */
18920
18921 static int
18922 push_prefix_prop (struct it *it, Lisp_Object prop)
18923 {
18924 struct text_pos pos =
18925 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18926
18927 eassert (it->method == GET_FROM_BUFFER
18928 || it->method == GET_FROM_DISPLAY_VECTOR
18929 || it->method == GET_FROM_STRING);
18930
18931 /* We need to save the current buffer/string position, so it will be
18932 restored by pop_it, because iterate_out_of_display_property
18933 depends on that being set correctly, but some situations leave
18934 it->position not yet set when this function is called. */
18935 push_it (it, &pos);
18936
18937 if (STRINGP (prop))
18938 {
18939 if (SCHARS (prop) == 0)
18940 {
18941 pop_it (it);
18942 return 0;
18943 }
18944
18945 it->string = prop;
18946 it->string_from_prefix_prop_p = 1;
18947 it->multibyte_p = STRING_MULTIBYTE (it->string);
18948 it->current.overlay_string_index = -1;
18949 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18950 it->end_charpos = it->string_nchars = SCHARS (it->string);
18951 it->method = GET_FROM_STRING;
18952 it->stop_charpos = 0;
18953 it->prev_stop = 0;
18954 it->base_level_stop = 0;
18955
18956 /* Force paragraph direction to be that of the parent
18957 buffer/string. */
18958 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18959 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18960 else
18961 it->paragraph_embedding = L2R;
18962
18963 /* Set up the bidi iterator for this display string. */
18964 if (it->bidi_p)
18965 {
18966 it->bidi_it.string.lstring = it->string;
18967 it->bidi_it.string.s = NULL;
18968 it->bidi_it.string.schars = it->end_charpos;
18969 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18970 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18971 it->bidi_it.string.unibyte = !it->multibyte_p;
18972 it->bidi_it.w = it->w;
18973 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18974 }
18975 }
18976 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18977 {
18978 it->method = GET_FROM_STRETCH;
18979 it->object = prop;
18980 }
18981 #ifdef HAVE_WINDOW_SYSTEM
18982 else if (IMAGEP (prop))
18983 {
18984 it->what = IT_IMAGE;
18985 it->image_id = lookup_image (it->f, prop);
18986 it->method = GET_FROM_IMAGE;
18987 }
18988 #endif /* HAVE_WINDOW_SYSTEM */
18989 else
18990 {
18991 pop_it (it); /* bogus display property, give up */
18992 return 0;
18993 }
18994
18995 return 1;
18996 }
18997
18998 /* Return the character-property PROP at the current position in IT. */
18999
19000 static Lisp_Object
19001 get_it_property (struct it *it, Lisp_Object prop)
19002 {
19003 Lisp_Object position, object = it->object;
19004
19005 if (STRINGP (object))
19006 position = make_number (IT_STRING_CHARPOS (*it));
19007 else if (BUFFERP (object))
19008 {
19009 position = make_number (IT_CHARPOS (*it));
19010 object = it->window;
19011 }
19012 else
19013 return Qnil;
19014
19015 return Fget_char_property (position, prop, object);
19016 }
19017
19018 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19019
19020 static void
19021 handle_line_prefix (struct it *it)
19022 {
19023 Lisp_Object prefix;
19024
19025 if (it->continuation_lines_width > 0)
19026 {
19027 prefix = get_it_property (it, Qwrap_prefix);
19028 if (NILP (prefix))
19029 prefix = Vwrap_prefix;
19030 }
19031 else
19032 {
19033 prefix = get_it_property (it, Qline_prefix);
19034 if (NILP (prefix))
19035 prefix = Vline_prefix;
19036 }
19037 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19038 {
19039 /* If the prefix is wider than the window, and we try to wrap
19040 it, it would acquire its own wrap prefix, and so on till the
19041 iterator stack overflows. So, don't wrap the prefix. */
19042 it->line_wrap = TRUNCATE;
19043 it->avoid_cursor_p = 1;
19044 }
19045 }
19046
19047 \f
19048
19049 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19050 only for R2L lines from display_line and display_string, when they
19051 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19052 the line/string needs to be continued on the next glyph row. */
19053 static void
19054 unproduce_glyphs (struct it *it, int n)
19055 {
19056 struct glyph *glyph, *end;
19057
19058 eassert (it->glyph_row);
19059 eassert (it->glyph_row->reversed_p);
19060 eassert (it->area == TEXT_AREA);
19061 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19062
19063 if (n > it->glyph_row->used[TEXT_AREA])
19064 n = it->glyph_row->used[TEXT_AREA];
19065 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19066 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19067 for ( ; glyph < end; glyph++)
19068 glyph[-n] = *glyph;
19069 }
19070
19071 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19072 and ROW->maxpos. */
19073 static void
19074 find_row_edges (struct it *it, struct glyph_row *row,
19075 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19076 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19077 {
19078 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19079 lines' rows is implemented for bidi-reordered rows. */
19080
19081 /* ROW->minpos is the value of min_pos, the minimal buffer position
19082 we have in ROW, or ROW->start.pos if that is smaller. */
19083 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19084 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19085 else
19086 /* We didn't find buffer positions smaller than ROW->start, or
19087 didn't find _any_ valid buffer positions in any of the glyphs,
19088 so we must trust the iterator's computed positions. */
19089 row->minpos = row->start.pos;
19090 if (max_pos <= 0)
19091 {
19092 max_pos = CHARPOS (it->current.pos);
19093 max_bpos = BYTEPOS (it->current.pos);
19094 }
19095
19096 /* Here are the various use-cases for ending the row, and the
19097 corresponding values for ROW->maxpos:
19098
19099 Line ends in a newline from buffer eol_pos + 1
19100 Line is continued from buffer max_pos + 1
19101 Line is truncated on right it->current.pos
19102 Line ends in a newline from string max_pos + 1(*)
19103 (*) + 1 only when line ends in a forward scan
19104 Line is continued from string max_pos
19105 Line is continued from display vector max_pos
19106 Line is entirely from a string min_pos == max_pos
19107 Line is entirely from a display vector min_pos == max_pos
19108 Line that ends at ZV ZV
19109
19110 If you discover other use-cases, please add them here as
19111 appropriate. */
19112 if (row->ends_at_zv_p)
19113 row->maxpos = it->current.pos;
19114 else if (row->used[TEXT_AREA])
19115 {
19116 int seen_this_string = 0;
19117 struct glyph_row *r1 = row - 1;
19118
19119 /* Did we see the same display string on the previous row? */
19120 if (STRINGP (it->object)
19121 /* this is not the first row */
19122 && row > it->w->desired_matrix->rows
19123 /* previous row is not the header line */
19124 && !r1->mode_line_p
19125 /* previous row also ends in a newline from a string */
19126 && r1->ends_in_newline_from_string_p)
19127 {
19128 struct glyph *start, *end;
19129
19130 /* Search for the last glyph of the previous row that came
19131 from buffer or string. Depending on whether the row is
19132 L2R or R2L, we need to process it front to back or the
19133 other way round. */
19134 if (!r1->reversed_p)
19135 {
19136 start = r1->glyphs[TEXT_AREA];
19137 end = start + r1->used[TEXT_AREA];
19138 /* Glyphs inserted by redisplay have an integer (zero)
19139 as their object. */
19140 while (end > start
19141 && INTEGERP ((end - 1)->object)
19142 && (end - 1)->charpos <= 0)
19143 --end;
19144 if (end > start)
19145 {
19146 if (EQ ((end - 1)->object, it->object))
19147 seen_this_string = 1;
19148 }
19149 else
19150 /* If all the glyphs of the previous row were inserted
19151 by redisplay, it means the previous row was
19152 produced from a single newline, which is only
19153 possible if that newline came from the same string
19154 as the one which produced this ROW. */
19155 seen_this_string = 1;
19156 }
19157 else
19158 {
19159 end = r1->glyphs[TEXT_AREA] - 1;
19160 start = end + r1->used[TEXT_AREA];
19161 while (end < start
19162 && INTEGERP ((end + 1)->object)
19163 && (end + 1)->charpos <= 0)
19164 ++end;
19165 if (end < start)
19166 {
19167 if (EQ ((end + 1)->object, it->object))
19168 seen_this_string = 1;
19169 }
19170 else
19171 seen_this_string = 1;
19172 }
19173 }
19174 /* Take note of each display string that covers a newline only
19175 once, the first time we see it. This is for when a display
19176 string includes more than one newline in it. */
19177 if (row->ends_in_newline_from_string_p && !seen_this_string)
19178 {
19179 /* If we were scanning the buffer forward when we displayed
19180 the string, we want to account for at least one buffer
19181 position that belongs to this row (position covered by
19182 the display string), so that cursor positioning will
19183 consider this row as a candidate when point is at the end
19184 of the visual line represented by this row. This is not
19185 required when scanning back, because max_pos will already
19186 have a much larger value. */
19187 if (CHARPOS (row->end.pos) > max_pos)
19188 INC_BOTH (max_pos, max_bpos);
19189 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19190 }
19191 else if (CHARPOS (it->eol_pos) > 0)
19192 SET_TEXT_POS (row->maxpos,
19193 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19194 else if (row->continued_p)
19195 {
19196 /* If max_pos is different from IT's current position, it
19197 means IT->method does not belong to the display element
19198 at max_pos. However, it also means that the display
19199 element at max_pos was displayed in its entirety on this
19200 line, which is equivalent to saying that the next line
19201 starts at the next buffer position. */
19202 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19203 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19204 else
19205 {
19206 INC_BOTH (max_pos, max_bpos);
19207 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19208 }
19209 }
19210 else if (row->truncated_on_right_p)
19211 /* display_line already called reseat_at_next_visible_line_start,
19212 which puts the iterator at the beginning of the next line, in
19213 the logical order. */
19214 row->maxpos = it->current.pos;
19215 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19216 /* A line that is entirely from a string/image/stretch... */
19217 row->maxpos = row->minpos;
19218 else
19219 emacs_abort ();
19220 }
19221 else
19222 row->maxpos = it->current.pos;
19223 }
19224
19225 /* Construct the glyph row IT->glyph_row in the desired matrix of
19226 IT->w from text at the current position of IT. See dispextern.h
19227 for an overview of struct it. Value is non-zero if
19228 IT->glyph_row displays text, as opposed to a line displaying ZV
19229 only. */
19230
19231 static int
19232 display_line (struct it *it)
19233 {
19234 struct glyph_row *row = it->glyph_row;
19235 Lisp_Object overlay_arrow_string;
19236 struct it wrap_it;
19237 void *wrap_data = NULL;
19238 int may_wrap = 0, wrap_x IF_LINT (= 0);
19239 int wrap_row_used = -1;
19240 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19241 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19242 int wrap_row_extra_line_spacing IF_LINT (= 0);
19243 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19244 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19245 int cvpos;
19246 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19247 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19248
19249 /* We always start displaying at hpos zero even if hscrolled. */
19250 eassert (it->hpos == 0 && it->current_x == 0);
19251
19252 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19253 >= it->w->desired_matrix->nrows)
19254 {
19255 it->w->nrows_scale_factor++;
19256 it->f->fonts_changed = 1;
19257 return 0;
19258 }
19259
19260 /* Is IT->w showing the region? */
19261 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19262
19263 /* Clear the result glyph row and enable it. */
19264 prepare_desired_row (row);
19265
19266 row->y = it->current_y;
19267 row->start = it->start;
19268 row->continuation_lines_width = it->continuation_lines_width;
19269 row->displays_text_p = 1;
19270 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19271 it->starts_in_middle_of_char_p = 0;
19272
19273 /* Arrange the overlays nicely for our purposes. Usually, we call
19274 display_line on only one line at a time, in which case this
19275 can't really hurt too much, or we call it on lines which appear
19276 one after another in the buffer, in which case all calls to
19277 recenter_overlay_lists but the first will be pretty cheap. */
19278 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19279
19280 /* Move over display elements that are not visible because we are
19281 hscrolled. This may stop at an x-position < IT->first_visible_x
19282 if the first glyph is partially visible or if we hit a line end. */
19283 if (it->current_x < it->first_visible_x)
19284 {
19285 enum move_it_result move_result;
19286
19287 this_line_min_pos = row->start.pos;
19288 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19289 MOVE_TO_POS | MOVE_TO_X);
19290 /* If we are under a large hscroll, move_it_in_display_line_to
19291 could hit the end of the line without reaching
19292 it->first_visible_x. Pretend that we did reach it. This is
19293 especially important on a TTY, where we will call
19294 extend_face_to_end_of_line, which needs to know how many
19295 blank glyphs to produce. */
19296 if (it->current_x < it->first_visible_x
19297 && (move_result == MOVE_NEWLINE_OR_CR
19298 || move_result == MOVE_POS_MATCH_OR_ZV))
19299 it->current_x = it->first_visible_x;
19300
19301 /* Record the smallest positions seen while we moved over
19302 display elements that are not visible. This is needed by
19303 redisplay_internal for optimizing the case where the cursor
19304 stays inside the same line. The rest of this function only
19305 considers positions that are actually displayed, so
19306 RECORD_MAX_MIN_POS will not otherwise record positions that
19307 are hscrolled to the left of the left edge of the window. */
19308 min_pos = CHARPOS (this_line_min_pos);
19309 min_bpos = BYTEPOS (this_line_min_pos);
19310 }
19311 else
19312 {
19313 /* We only do this when not calling `move_it_in_display_line_to'
19314 above, because move_it_in_display_line_to calls
19315 handle_line_prefix itself. */
19316 handle_line_prefix (it);
19317 }
19318
19319 /* Get the initial row height. This is either the height of the
19320 text hscrolled, if there is any, or zero. */
19321 row->ascent = it->max_ascent;
19322 row->height = it->max_ascent + it->max_descent;
19323 row->phys_ascent = it->max_phys_ascent;
19324 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19325 row->extra_line_spacing = it->max_extra_line_spacing;
19326
19327 /* Utility macro to record max and min buffer positions seen until now. */
19328 #define RECORD_MAX_MIN_POS(IT) \
19329 do \
19330 { \
19331 int composition_p = !STRINGP ((IT)->string) \
19332 && ((IT)->what == IT_COMPOSITION); \
19333 ptrdiff_t current_pos = \
19334 composition_p ? (IT)->cmp_it.charpos \
19335 : IT_CHARPOS (*(IT)); \
19336 ptrdiff_t current_bpos = \
19337 composition_p ? CHAR_TO_BYTE (current_pos) \
19338 : IT_BYTEPOS (*(IT)); \
19339 if (current_pos < min_pos) \
19340 { \
19341 min_pos = current_pos; \
19342 min_bpos = current_bpos; \
19343 } \
19344 if (IT_CHARPOS (*it) > max_pos) \
19345 { \
19346 max_pos = IT_CHARPOS (*it); \
19347 max_bpos = IT_BYTEPOS (*it); \
19348 } \
19349 } \
19350 while (0)
19351
19352 /* Loop generating characters. The loop is left with IT on the next
19353 character to display. */
19354 while (1)
19355 {
19356 int n_glyphs_before, hpos_before, x_before;
19357 int x, nglyphs;
19358 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19359
19360 /* Retrieve the next thing to display. Value is zero if end of
19361 buffer reached. */
19362 if (!get_next_display_element (it))
19363 {
19364 /* Maybe add a space at the end of this line that is used to
19365 display the cursor there under X. Set the charpos of the
19366 first glyph of blank lines not corresponding to any text
19367 to -1. */
19368 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19369 row->exact_window_width_line_p = 1;
19370 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19371 || row->used[TEXT_AREA] == 0)
19372 {
19373 row->glyphs[TEXT_AREA]->charpos = -1;
19374 row->displays_text_p = 0;
19375
19376 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19377 && (!MINI_WINDOW_P (it->w)
19378 || (minibuf_level && EQ (it->window, minibuf_window))))
19379 row->indicate_empty_line_p = 1;
19380 }
19381
19382 it->continuation_lines_width = 0;
19383 row->ends_at_zv_p = 1;
19384 /* A row that displays right-to-left text must always have
19385 its last face extended all the way to the end of line,
19386 even if this row ends in ZV, because we still write to
19387 the screen left to right. We also need to extend the
19388 last face if the default face is remapped to some
19389 different face, otherwise the functions that clear
19390 portions of the screen will clear with the default face's
19391 background color. */
19392 if (row->reversed_p
19393 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19394 extend_face_to_end_of_line (it);
19395 break;
19396 }
19397
19398 /* Now, get the metrics of what we want to display. This also
19399 generates glyphs in `row' (which is IT->glyph_row). */
19400 n_glyphs_before = row->used[TEXT_AREA];
19401 x = it->current_x;
19402
19403 /* Remember the line height so far in case the next element doesn't
19404 fit on the line. */
19405 if (it->line_wrap != TRUNCATE)
19406 {
19407 ascent = it->max_ascent;
19408 descent = it->max_descent;
19409 phys_ascent = it->max_phys_ascent;
19410 phys_descent = it->max_phys_descent;
19411
19412 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19413 {
19414 if (IT_DISPLAYING_WHITESPACE (it))
19415 may_wrap = 1;
19416 else if (may_wrap)
19417 {
19418 SAVE_IT (wrap_it, *it, wrap_data);
19419 wrap_x = x;
19420 wrap_row_used = row->used[TEXT_AREA];
19421 wrap_row_ascent = row->ascent;
19422 wrap_row_height = row->height;
19423 wrap_row_phys_ascent = row->phys_ascent;
19424 wrap_row_phys_height = row->phys_height;
19425 wrap_row_extra_line_spacing = row->extra_line_spacing;
19426 wrap_row_min_pos = min_pos;
19427 wrap_row_min_bpos = min_bpos;
19428 wrap_row_max_pos = max_pos;
19429 wrap_row_max_bpos = max_bpos;
19430 may_wrap = 0;
19431 }
19432 }
19433 }
19434
19435 PRODUCE_GLYPHS (it);
19436
19437 /* If this display element was in marginal areas, continue with
19438 the next one. */
19439 if (it->area != TEXT_AREA)
19440 {
19441 row->ascent = max (row->ascent, it->max_ascent);
19442 row->height = max (row->height, it->max_ascent + it->max_descent);
19443 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19444 row->phys_height = max (row->phys_height,
19445 it->max_phys_ascent + it->max_phys_descent);
19446 row->extra_line_spacing = max (row->extra_line_spacing,
19447 it->max_extra_line_spacing);
19448 set_iterator_to_next (it, 1);
19449 continue;
19450 }
19451
19452 /* Does the display element fit on the line? If we truncate
19453 lines, we should draw past the right edge of the window. If
19454 we don't truncate, we want to stop so that we can display the
19455 continuation glyph before the right margin. If lines are
19456 continued, there are two possible strategies for characters
19457 resulting in more than 1 glyph (e.g. tabs): Display as many
19458 glyphs as possible in this line and leave the rest for the
19459 continuation line, or display the whole element in the next
19460 line. Original redisplay did the former, so we do it also. */
19461 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19462 hpos_before = it->hpos;
19463 x_before = x;
19464
19465 if (/* Not a newline. */
19466 nglyphs > 0
19467 /* Glyphs produced fit entirely in the line. */
19468 && it->current_x < it->last_visible_x)
19469 {
19470 it->hpos += nglyphs;
19471 row->ascent = max (row->ascent, it->max_ascent);
19472 row->height = max (row->height, it->max_ascent + it->max_descent);
19473 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19474 row->phys_height = max (row->phys_height,
19475 it->max_phys_ascent + it->max_phys_descent);
19476 row->extra_line_spacing = max (row->extra_line_spacing,
19477 it->max_extra_line_spacing);
19478 if (it->current_x - it->pixel_width < it->first_visible_x)
19479 row->x = x - it->first_visible_x;
19480 /* Record the maximum and minimum buffer positions seen so
19481 far in glyphs that will be displayed by this row. */
19482 if (it->bidi_p)
19483 RECORD_MAX_MIN_POS (it);
19484 }
19485 else
19486 {
19487 int i, new_x;
19488 struct glyph *glyph;
19489
19490 for (i = 0; i < nglyphs; ++i, x = new_x)
19491 {
19492 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19493 new_x = x + glyph->pixel_width;
19494
19495 if (/* Lines are continued. */
19496 it->line_wrap != TRUNCATE
19497 && (/* Glyph doesn't fit on the line. */
19498 new_x > it->last_visible_x
19499 /* Or it fits exactly on a window system frame. */
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 /* End of a continued line. */
19507
19508 if (it->hpos == 0
19509 || (new_x == it->last_visible_x
19510 && FRAME_WINDOW_P (it->f)
19511 && (row->reversed_p
19512 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19513 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19514 {
19515 /* Current glyph is the only one on the line or
19516 fits exactly on the line. We must continue
19517 the line because we can't draw the cursor
19518 after the glyph. */
19519 row->continued_p = 1;
19520 it->current_x = new_x;
19521 it->continuation_lines_width += new_x;
19522 ++it->hpos;
19523 if (i == nglyphs - 1)
19524 {
19525 /* If line-wrap is on, check if a previous
19526 wrap point was found. */
19527 if (wrap_row_used > 0
19528 /* Even if there is a previous wrap
19529 point, continue the line here as
19530 usual, if (i) the previous character
19531 was a space or tab AND (ii) the
19532 current character is not. */
19533 && (!may_wrap
19534 || IT_DISPLAYING_WHITESPACE (it)))
19535 goto back_to_wrap;
19536
19537 /* Record the maximum and minimum buffer
19538 positions seen so far in glyphs that will be
19539 displayed by this row. */
19540 if (it->bidi_p)
19541 RECORD_MAX_MIN_POS (it);
19542 set_iterator_to_next (it, 1);
19543 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19544 {
19545 if (!get_next_display_element (it))
19546 {
19547 row->exact_window_width_line_p = 1;
19548 it->continuation_lines_width = 0;
19549 row->continued_p = 0;
19550 row->ends_at_zv_p = 1;
19551 }
19552 else if (ITERATOR_AT_END_OF_LINE_P (it))
19553 {
19554 row->continued_p = 0;
19555 row->exact_window_width_line_p = 1;
19556 }
19557 }
19558 }
19559 else if (it->bidi_p)
19560 RECORD_MAX_MIN_POS (it);
19561 }
19562 else if (CHAR_GLYPH_PADDING_P (*glyph)
19563 && !FRAME_WINDOW_P (it->f))
19564 {
19565 /* A padding glyph that doesn't fit on this line.
19566 This means the whole character doesn't fit
19567 on the line. */
19568 if (row->reversed_p)
19569 unproduce_glyphs (it, row->used[TEXT_AREA]
19570 - n_glyphs_before);
19571 row->used[TEXT_AREA] = n_glyphs_before;
19572
19573 /* Fill the rest of the row with continuation
19574 glyphs like in 20.x. */
19575 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19576 < row->glyphs[1 + TEXT_AREA])
19577 produce_special_glyphs (it, IT_CONTINUATION);
19578
19579 row->continued_p = 1;
19580 it->current_x = x_before;
19581 it->continuation_lines_width += x_before;
19582
19583 /* Restore the height to what it was before the
19584 element not fitting on the line. */
19585 it->max_ascent = ascent;
19586 it->max_descent = descent;
19587 it->max_phys_ascent = phys_ascent;
19588 it->max_phys_descent = phys_descent;
19589 }
19590 else if (wrap_row_used > 0)
19591 {
19592 back_to_wrap:
19593 if (row->reversed_p)
19594 unproduce_glyphs (it,
19595 row->used[TEXT_AREA] - wrap_row_used);
19596 RESTORE_IT (it, &wrap_it, wrap_data);
19597 it->continuation_lines_width += wrap_x;
19598 row->used[TEXT_AREA] = wrap_row_used;
19599 row->ascent = wrap_row_ascent;
19600 row->height = wrap_row_height;
19601 row->phys_ascent = wrap_row_phys_ascent;
19602 row->phys_height = wrap_row_phys_height;
19603 row->extra_line_spacing = wrap_row_extra_line_spacing;
19604 min_pos = wrap_row_min_pos;
19605 min_bpos = wrap_row_min_bpos;
19606 max_pos = wrap_row_max_pos;
19607 max_bpos = wrap_row_max_bpos;
19608 row->continued_p = 1;
19609 row->ends_at_zv_p = 0;
19610 row->exact_window_width_line_p = 0;
19611 it->continuation_lines_width += x;
19612
19613 /* Make sure that a non-default face is extended
19614 up to the right margin of the window. */
19615 extend_face_to_end_of_line (it);
19616 }
19617 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19618 {
19619 /* A TAB that extends past the right edge of the
19620 window. This produces a single glyph on
19621 window system frames. We leave the glyph in
19622 this row and let it fill the row, but don't
19623 consume the TAB. */
19624 if ((row->reversed_p
19625 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19626 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19627 produce_special_glyphs (it, IT_CONTINUATION);
19628 it->continuation_lines_width += it->last_visible_x;
19629 row->ends_in_middle_of_char_p = 1;
19630 row->continued_p = 1;
19631 glyph->pixel_width = it->last_visible_x - x;
19632 it->starts_in_middle_of_char_p = 1;
19633 }
19634 else
19635 {
19636 /* Something other than a TAB that draws past
19637 the right edge of the window. Restore
19638 positions to values before the element. */
19639 if (row->reversed_p)
19640 unproduce_glyphs (it, row->used[TEXT_AREA]
19641 - (n_glyphs_before + i));
19642 row->used[TEXT_AREA] = n_glyphs_before + i;
19643
19644 /* Display continuation glyphs. */
19645 it->current_x = x_before;
19646 it->continuation_lines_width += x;
19647 if (!FRAME_WINDOW_P (it->f)
19648 || (row->reversed_p
19649 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19650 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19651 produce_special_glyphs (it, IT_CONTINUATION);
19652 row->continued_p = 1;
19653
19654 extend_face_to_end_of_line (it);
19655
19656 if (nglyphs > 1 && i > 0)
19657 {
19658 row->ends_in_middle_of_char_p = 1;
19659 it->starts_in_middle_of_char_p = 1;
19660 }
19661
19662 /* Restore the height to what it was before the
19663 element not fitting on the line. */
19664 it->max_ascent = ascent;
19665 it->max_descent = descent;
19666 it->max_phys_ascent = phys_ascent;
19667 it->max_phys_descent = phys_descent;
19668 }
19669
19670 break;
19671 }
19672 else if (new_x > it->first_visible_x)
19673 {
19674 /* Increment number of glyphs actually displayed. */
19675 ++it->hpos;
19676
19677 /* Record the maximum and minimum buffer positions
19678 seen so far in glyphs that will be displayed by
19679 this row. */
19680 if (it->bidi_p)
19681 RECORD_MAX_MIN_POS (it);
19682
19683 if (x < it->first_visible_x)
19684 /* Glyph is partially visible, i.e. row starts at
19685 negative X position. */
19686 row->x = x - it->first_visible_x;
19687 }
19688 else
19689 {
19690 /* Glyph is completely off the left margin of the
19691 window. This should not happen because of the
19692 move_it_in_display_line at the start of this
19693 function, unless the text display area of the
19694 window is empty. */
19695 eassert (it->first_visible_x <= it->last_visible_x);
19696 }
19697 }
19698 /* Even if this display element produced no glyphs at all,
19699 we want to record its position. */
19700 if (it->bidi_p && nglyphs == 0)
19701 RECORD_MAX_MIN_POS (it);
19702
19703 row->ascent = max (row->ascent, it->max_ascent);
19704 row->height = max (row->height, it->max_ascent + it->max_descent);
19705 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19706 row->phys_height = max (row->phys_height,
19707 it->max_phys_ascent + it->max_phys_descent);
19708 row->extra_line_spacing = max (row->extra_line_spacing,
19709 it->max_extra_line_spacing);
19710
19711 /* End of this display line if row is continued. */
19712 if (row->continued_p || row->ends_at_zv_p)
19713 break;
19714 }
19715
19716 at_end_of_line:
19717 /* Is this a line end? If yes, we're also done, after making
19718 sure that a non-default face is extended up to the right
19719 margin of the window. */
19720 if (ITERATOR_AT_END_OF_LINE_P (it))
19721 {
19722 int used_before = row->used[TEXT_AREA];
19723
19724 row->ends_in_newline_from_string_p = STRINGP (it->object);
19725
19726 /* Add a space at the end of the line that is used to
19727 display the cursor there. */
19728 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19729 append_space_for_newline (it, 0);
19730
19731 /* Extend the face to the end of the line. */
19732 extend_face_to_end_of_line (it);
19733
19734 /* Make sure we have the position. */
19735 if (used_before == 0)
19736 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19737
19738 /* Record the position of the newline, for use in
19739 find_row_edges. */
19740 it->eol_pos = it->current.pos;
19741
19742 /* Consume the line end. This skips over invisible lines. */
19743 set_iterator_to_next (it, 1);
19744 it->continuation_lines_width = 0;
19745 break;
19746 }
19747
19748 /* Proceed with next display element. Note that this skips
19749 over lines invisible because of selective display. */
19750 set_iterator_to_next (it, 1);
19751
19752 /* If we truncate lines, we are done when the last displayed
19753 glyphs reach past the right margin of the window. */
19754 if (it->line_wrap == TRUNCATE
19755 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19756 ? (it->current_x >= it->last_visible_x)
19757 : (it->current_x > it->last_visible_x)))
19758 {
19759 /* Maybe add truncation glyphs. */
19760 if (!FRAME_WINDOW_P (it->f)
19761 || (row->reversed_p
19762 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19763 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19764 {
19765 int i, n;
19766
19767 if (!row->reversed_p)
19768 {
19769 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19770 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19771 break;
19772 }
19773 else
19774 {
19775 for (i = 0; i < row->used[TEXT_AREA]; i++)
19776 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19777 break;
19778 /* Remove any padding glyphs at the front of ROW, to
19779 make room for the truncation glyphs we will be
19780 adding below. The loop below always inserts at
19781 least one truncation glyph, so also remove the
19782 last glyph added to ROW. */
19783 unproduce_glyphs (it, i + 1);
19784 /* Adjust i for the loop below. */
19785 i = row->used[TEXT_AREA] - (i + 1);
19786 }
19787
19788 it->current_x = x_before;
19789 if (!FRAME_WINDOW_P (it->f))
19790 {
19791 for (n = row->used[TEXT_AREA]; i < n; ++i)
19792 {
19793 row->used[TEXT_AREA] = i;
19794 produce_special_glyphs (it, IT_TRUNCATION);
19795 }
19796 }
19797 else
19798 {
19799 row->used[TEXT_AREA] = i;
19800 produce_special_glyphs (it, IT_TRUNCATION);
19801 }
19802 }
19803 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19804 {
19805 /* Don't truncate if we can overflow newline into fringe. */
19806 if (!get_next_display_element (it))
19807 {
19808 it->continuation_lines_width = 0;
19809 row->ends_at_zv_p = 1;
19810 row->exact_window_width_line_p = 1;
19811 break;
19812 }
19813 if (ITERATOR_AT_END_OF_LINE_P (it))
19814 {
19815 row->exact_window_width_line_p = 1;
19816 goto at_end_of_line;
19817 }
19818 it->current_x = x_before;
19819 }
19820
19821 row->truncated_on_right_p = 1;
19822 it->continuation_lines_width = 0;
19823 reseat_at_next_visible_line_start (it, 0);
19824 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19825 it->hpos = hpos_before;
19826 break;
19827 }
19828 }
19829
19830 if (wrap_data)
19831 bidi_unshelve_cache (wrap_data, 1);
19832
19833 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19834 at the left window margin. */
19835 if (it->first_visible_x
19836 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19837 {
19838 if (!FRAME_WINDOW_P (it->f)
19839 || (row->reversed_p
19840 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19841 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19842 insert_left_trunc_glyphs (it);
19843 row->truncated_on_left_p = 1;
19844 }
19845
19846 /* Remember the position at which this line ends.
19847
19848 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19849 cannot be before the call to find_row_edges below, since that is
19850 where these positions are determined. */
19851 row->end = it->current;
19852 if (!it->bidi_p)
19853 {
19854 row->minpos = row->start.pos;
19855 row->maxpos = row->end.pos;
19856 }
19857 else
19858 {
19859 /* ROW->minpos and ROW->maxpos must be the smallest and
19860 `1 + the largest' buffer positions in ROW. But if ROW was
19861 bidi-reordered, these two positions can be anywhere in the
19862 row, so we must determine them now. */
19863 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19864 }
19865
19866 /* If the start of this line is the overlay arrow-position, then
19867 mark this glyph row as the one containing the overlay arrow.
19868 This is clearly a mess with variable size fonts. It would be
19869 better to let it be displayed like cursors under X. */
19870 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19871 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19872 !NILP (overlay_arrow_string)))
19873 {
19874 /* Overlay arrow in window redisplay is a fringe bitmap. */
19875 if (STRINGP (overlay_arrow_string))
19876 {
19877 struct glyph_row *arrow_row
19878 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19879 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19880 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19881 struct glyph *p = row->glyphs[TEXT_AREA];
19882 struct glyph *p2, *end;
19883
19884 /* Copy the arrow glyphs. */
19885 while (glyph < arrow_end)
19886 *p++ = *glyph++;
19887
19888 /* Throw away padding glyphs. */
19889 p2 = p;
19890 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19891 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19892 ++p2;
19893 if (p2 > p)
19894 {
19895 while (p2 < end)
19896 *p++ = *p2++;
19897 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19898 }
19899 }
19900 else
19901 {
19902 eassert (INTEGERP (overlay_arrow_string));
19903 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19904 }
19905 overlay_arrow_seen = 1;
19906 }
19907
19908 /* Highlight trailing whitespace. */
19909 if (!NILP (Vshow_trailing_whitespace))
19910 highlight_trailing_whitespace (it->f, it->glyph_row);
19911
19912 /* Compute pixel dimensions of this line. */
19913 compute_line_metrics (it);
19914
19915 /* Implementation note: No changes in the glyphs of ROW or in their
19916 faces can be done past this point, because compute_line_metrics
19917 computes ROW's hash value and stores it within the glyph_row
19918 structure. */
19919
19920 /* Record whether this row ends inside an ellipsis. */
19921 row->ends_in_ellipsis_p
19922 = (it->method == GET_FROM_DISPLAY_VECTOR
19923 && it->ellipsis_p);
19924
19925 /* Save fringe bitmaps in this row. */
19926 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19927 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19928 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19929 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19930
19931 it->left_user_fringe_bitmap = 0;
19932 it->left_user_fringe_face_id = 0;
19933 it->right_user_fringe_bitmap = 0;
19934 it->right_user_fringe_face_id = 0;
19935
19936 /* Maybe set the cursor. */
19937 cvpos = it->w->cursor.vpos;
19938 if ((cvpos < 0
19939 /* In bidi-reordered rows, keep checking for proper cursor
19940 position even if one has been found already, because buffer
19941 positions in such rows change non-linearly with ROW->VPOS,
19942 when a line is continued. One exception: when we are at ZV,
19943 display cursor on the first suitable glyph row, since all
19944 the empty rows after that also have their position set to ZV. */
19945 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19946 lines' rows is implemented for bidi-reordered rows. */
19947 || (it->bidi_p
19948 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19949 && PT >= MATRIX_ROW_START_CHARPOS (row)
19950 && PT <= MATRIX_ROW_END_CHARPOS (row)
19951 && cursor_row_p (row))
19952 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19953
19954 /* Prepare for the next line. This line starts horizontally at (X
19955 HPOS) = (0 0). Vertical positions are incremented. As a
19956 convenience for the caller, IT->glyph_row is set to the next
19957 row to be used. */
19958 it->current_x = it->hpos = 0;
19959 it->current_y += row->height;
19960 SET_TEXT_POS (it->eol_pos, 0, 0);
19961 ++it->vpos;
19962 ++it->glyph_row;
19963 /* The next row should by default use the same value of the
19964 reversed_p flag as this one. set_iterator_to_next decides when
19965 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19966 the flag accordingly. */
19967 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19968 it->glyph_row->reversed_p = row->reversed_p;
19969 it->start = row->end;
19970 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19971
19972 #undef RECORD_MAX_MIN_POS
19973 }
19974
19975 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19976 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19977 doc: /* Return paragraph direction at point in BUFFER.
19978 Value is either `left-to-right' or `right-to-left'.
19979 If BUFFER is omitted or nil, it defaults to the current buffer.
19980
19981 Paragraph direction determines how the text in the paragraph is displayed.
19982 In left-to-right paragraphs, text begins at the left margin of the window
19983 and the reading direction is generally left to right. In right-to-left
19984 paragraphs, text begins at the right margin and is read from right to left.
19985
19986 See also `bidi-paragraph-direction'. */)
19987 (Lisp_Object buffer)
19988 {
19989 struct buffer *buf = current_buffer;
19990 struct buffer *old = buf;
19991
19992 if (! NILP (buffer))
19993 {
19994 CHECK_BUFFER (buffer);
19995 buf = XBUFFER (buffer);
19996 }
19997
19998 if (NILP (BVAR (buf, bidi_display_reordering))
19999 || NILP (BVAR (buf, enable_multibyte_characters))
20000 /* When we are loading loadup.el, the character property tables
20001 needed for bidi iteration are not yet available. */
20002 || !NILP (Vpurify_flag))
20003 return Qleft_to_right;
20004 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20005 return BVAR (buf, bidi_paragraph_direction);
20006 else
20007 {
20008 /* Determine the direction from buffer text. We could try to
20009 use current_matrix if it is up to date, but this seems fast
20010 enough as it is. */
20011 struct bidi_it itb;
20012 ptrdiff_t pos = BUF_PT (buf);
20013 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20014 int c;
20015 void *itb_data = bidi_shelve_cache ();
20016
20017 set_buffer_temp (buf);
20018 /* bidi_paragraph_init finds the base direction of the paragraph
20019 by searching forward from paragraph start. We need the base
20020 direction of the current or _previous_ paragraph, so we need
20021 to make sure we are within that paragraph. To that end, find
20022 the previous non-empty line. */
20023 if (pos >= ZV && pos > BEGV)
20024 DEC_BOTH (pos, bytepos);
20025 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20026 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20027 {
20028 while ((c = FETCH_BYTE (bytepos)) == '\n'
20029 || c == ' ' || c == '\t' || c == '\f')
20030 {
20031 if (bytepos <= BEGV_BYTE)
20032 break;
20033 bytepos--;
20034 pos--;
20035 }
20036 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20037 bytepos--;
20038 }
20039 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20040 itb.paragraph_dir = NEUTRAL_DIR;
20041 itb.string.s = NULL;
20042 itb.string.lstring = Qnil;
20043 itb.string.bufpos = 0;
20044 itb.string.unibyte = 0;
20045 /* We have no window to use here for ignoring window-specific
20046 overlays. Using NULL for window pointer will cause
20047 compute_display_string_pos to use the current buffer. */
20048 itb.w = NULL;
20049 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20050 bidi_unshelve_cache (itb_data, 0);
20051 set_buffer_temp (old);
20052 switch (itb.paragraph_dir)
20053 {
20054 case L2R:
20055 return Qleft_to_right;
20056 break;
20057 case R2L:
20058 return Qright_to_left;
20059 break;
20060 default:
20061 emacs_abort ();
20062 }
20063 }
20064 }
20065
20066 DEFUN ("move-point-visually", Fmove_point_visually,
20067 Smove_point_visually, 1, 1, 0,
20068 doc: /* Move point in the visual order in the specified DIRECTION.
20069 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20070 left.
20071
20072 Value is the new character position of point. */)
20073 (Lisp_Object direction)
20074 {
20075 struct window *w = XWINDOW (selected_window);
20076 struct buffer *b = XBUFFER (w->contents);
20077 struct glyph_row *row;
20078 int dir;
20079 Lisp_Object paragraph_dir;
20080
20081 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20082 (!(ROW)->continued_p \
20083 && INTEGERP ((GLYPH)->object) \
20084 && (GLYPH)->type == CHAR_GLYPH \
20085 && (GLYPH)->u.ch == ' ' \
20086 && (GLYPH)->charpos >= 0 \
20087 && !(GLYPH)->avoid_cursor_p)
20088
20089 CHECK_NUMBER (direction);
20090 dir = XINT (direction);
20091 if (dir > 0)
20092 dir = 1;
20093 else
20094 dir = -1;
20095
20096 /* If current matrix is up-to-date, we can use the information
20097 recorded in the glyphs, at least as long as the goal is on the
20098 screen. */
20099 if (w->window_end_valid
20100 && !windows_or_buffers_changed
20101 && b
20102 && !b->clip_changed
20103 && !b->prevent_redisplay_optimizations_p
20104 && !window_outdated (w)
20105 && w->cursor.vpos >= 0
20106 && w->cursor.vpos < w->current_matrix->nrows
20107 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20108 {
20109 struct glyph *g = row->glyphs[TEXT_AREA];
20110 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20111 struct glyph *gpt = g + w->cursor.hpos;
20112
20113 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20114 {
20115 if (BUFFERP (g->object) && g->charpos != PT)
20116 {
20117 SET_PT (g->charpos);
20118 w->cursor.vpos = -1;
20119 return make_number (PT);
20120 }
20121 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20122 {
20123 ptrdiff_t new_pos;
20124
20125 if (BUFFERP (gpt->object))
20126 {
20127 new_pos = PT;
20128 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20129 new_pos += (row->reversed_p ? -dir : dir);
20130 else
20131 new_pos -= (row->reversed_p ? -dir : dir);;
20132 }
20133 else if (BUFFERP (g->object))
20134 new_pos = g->charpos;
20135 else
20136 break;
20137 SET_PT (new_pos);
20138 w->cursor.vpos = -1;
20139 return make_number (PT);
20140 }
20141 else if (ROW_GLYPH_NEWLINE_P (row, g))
20142 {
20143 /* Glyphs inserted at the end of a non-empty line for
20144 positioning the cursor have zero charpos, so we must
20145 deduce the value of point by other means. */
20146 if (g->charpos > 0)
20147 SET_PT (g->charpos);
20148 else if (row->ends_at_zv_p && PT != ZV)
20149 SET_PT (ZV);
20150 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20151 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20152 else
20153 break;
20154 w->cursor.vpos = -1;
20155 return make_number (PT);
20156 }
20157 }
20158 if (g == e || INTEGERP (g->object))
20159 {
20160 if (row->truncated_on_left_p || row->truncated_on_right_p)
20161 goto simulate_display;
20162 if (!row->reversed_p)
20163 row += dir;
20164 else
20165 row -= dir;
20166 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20167 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20168 goto simulate_display;
20169
20170 if (dir > 0)
20171 {
20172 if (row->reversed_p && !row->continued_p)
20173 {
20174 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20175 w->cursor.vpos = -1;
20176 return make_number (PT);
20177 }
20178 g = row->glyphs[TEXT_AREA];
20179 e = g + row->used[TEXT_AREA];
20180 for ( ; g < e; g++)
20181 {
20182 if (BUFFERP (g->object)
20183 /* Empty lines have only one glyph, which stands
20184 for the newline, and whose charpos is the
20185 buffer position of the newline. */
20186 || ROW_GLYPH_NEWLINE_P (row, g)
20187 /* When the buffer ends in a newline, the line at
20188 EOB also has one glyph, but its charpos is -1. */
20189 || (row->ends_at_zv_p
20190 && !row->reversed_p
20191 && INTEGERP (g->object)
20192 && g->type == CHAR_GLYPH
20193 && g->u.ch == ' '))
20194 {
20195 if (g->charpos > 0)
20196 SET_PT (g->charpos);
20197 else if (!row->reversed_p
20198 && row->ends_at_zv_p
20199 && PT != ZV)
20200 SET_PT (ZV);
20201 else
20202 continue;
20203 w->cursor.vpos = -1;
20204 return make_number (PT);
20205 }
20206 }
20207 }
20208 else
20209 {
20210 if (!row->reversed_p && !row->continued_p)
20211 {
20212 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20213 w->cursor.vpos = -1;
20214 return make_number (PT);
20215 }
20216 e = row->glyphs[TEXT_AREA];
20217 g = e + row->used[TEXT_AREA] - 1;
20218 for ( ; g >= e; g--)
20219 {
20220 if (BUFFERP (g->object)
20221 || (ROW_GLYPH_NEWLINE_P (row, g)
20222 && g->charpos > 0)
20223 /* Empty R2L lines on GUI frames have the buffer
20224 position of the newline stored in the stretch
20225 glyph. */
20226 || g->type == STRETCH_GLYPH
20227 || (row->ends_at_zv_p
20228 && row->reversed_p
20229 && INTEGERP (g->object)
20230 && g->type == CHAR_GLYPH
20231 && g->u.ch == ' '))
20232 {
20233 if (g->charpos > 0)
20234 SET_PT (g->charpos);
20235 else if (row->reversed_p
20236 && row->ends_at_zv_p
20237 && PT != ZV)
20238 SET_PT (ZV);
20239 else
20240 continue;
20241 w->cursor.vpos = -1;
20242 return make_number (PT);
20243 }
20244 }
20245 }
20246 }
20247 }
20248
20249 simulate_display:
20250
20251 /* If we wind up here, we failed to move by using the glyphs, so we
20252 need to simulate display instead. */
20253
20254 if (b)
20255 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20256 else
20257 paragraph_dir = Qleft_to_right;
20258 if (EQ (paragraph_dir, Qright_to_left))
20259 dir = -dir;
20260 if (PT <= BEGV && dir < 0)
20261 xsignal0 (Qbeginning_of_buffer);
20262 else if (PT >= ZV && dir > 0)
20263 xsignal0 (Qend_of_buffer);
20264 else
20265 {
20266 struct text_pos pt;
20267 struct it it;
20268 int pt_x, target_x, pixel_width, pt_vpos;
20269 bool at_eol_p;
20270 bool overshoot_expected = false;
20271 bool target_is_eol_p = false;
20272
20273 /* Setup the arena. */
20274 SET_TEXT_POS (pt, PT, PT_BYTE);
20275 start_display (&it, w, pt);
20276
20277 if (it.cmp_it.id < 0
20278 && it.method == GET_FROM_STRING
20279 && it.area == TEXT_AREA
20280 && it.string_from_display_prop_p
20281 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20282 overshoot_expected = true;
20283
20284 /* Find the X coordinate of point. We start from the beginning
20285 of this or previous line to make sure we are before point in
20286 the logical order (since the move_it_* functions can only
20287 move forward). */
20288 reseat_at_previous_visible_line_start (&it);
20289 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20290 if (IT_CHARPOS (it) != PT)
20291 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20292 -1, -1, -1, MOVE_TO_POS);
20293 pt_x = it.current_x;
20294 pt_vpos = it.vpos;
20295 if (dir > 0 || overshoot_expected)
20296 {
20297 struct glyph_row *row = it.glyph_row;
20298
20299 /* When point is at beginning of line, we don't have
20300 information about the glyph there loaded into struct
20301 it. Calling get_next_display_element fixes that. */
20302 if (pt_x == 0)
20303 get_next_display_element (&it);
20304 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20305 it.glyph_row = NULL;
20306 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20307 it.glyph_row = row;
20308 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20309 it, lest it will become out of sync with it's buffer
20310 position. */
20311 it.current_x = pt_x;
20312 }
20313 else
20314 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20315 pixel_width = it.pixel_width;
20316 if (overshoot_expected && at_eol_p)
20317 pixel_width = 0;
20318 else if (pixel_width <= 0)
20319 pixel_width = 1;
20320
20321 /* If there's a display string at point, we are actually at the
20322 glyph to the left of point, so we need to correct the X
20323 coordinate. */
20324 if (overshoot_expected)
20325 pt_x += pixel_width;
20326
20327 /* Compute target X coordinate, either to the left or to the
20328 right of point. On TTY frames, all characters have the same
20329 pixel width of 1, so we can use that. On GUI frames we don't
20330 have an easy way of getting at the pixel width of the
20331 character to the left of point, so we use a different method
20332 of getting to that place. */
20333 if (dir > 0)
20334 target_x = pt_x + pixel_width;
20335 else
20336 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20337
20338 /* Target X coordinate could be one line above or below the line
20339 of point, in which case we need to adjust the target X
20340 coordinate. Also, if moving to the left, we need to begin at
20341 the left edge of the point's screen line. */
20342 if (dir < 0)
20343 {
20344 if (pt_x > 0)
20345 {
20346 start_display (&it, w, pt);
20347 reseat_at_previous_visible_line_start (&it);
20348 it.current_x = it.current_y = it.hpos = 0;
20349 if (pt_vpos != 0)
20350 move_it_by_lines (&it, pt_vpos);
20351 }
20352 else
20353 {
20354 move_it_by_lines (&it, -1);
20355 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20356 target_is_eol_p = true;
20357 }
20358 }
20359 else
20360 {
20361 if (at_eol_p
20362 || (target_x >= it.last_visible_x
20363 && it.line_wrap != TRUNCATE))
20364 {
20365 if (pt_x > 0)
20366 move_it_by_lines (&it, 0);
20367 move_it_by_lines (&it, 1);
20368 target_x = 0;
20369 }
20370 }
20371
20372 /* Move to the target X coordinate. */
20373 #ifdef HAVE_WINDOW_SYSTEM
20374 /* On GUI frames, as we don't know the X coordinate of the
20375 character to the left of point, moving point to the left
20376 requires walking, one grapheme cluster at a time, until we
20377 find ourself at a place immediately to the left of the
20378 character at point. */
20379 if (FRAME_WINDOW_P (it.f) && dir < 0)
20380 {
20381 struct text_pos new_pos = it.current.pos;
20382 enum move_it_result rc = MOVE_X_REACHED;
20383
20384 while (it.current_x + it.pixel_width <= target_x
20385 && rc == MOVE_X_REACHED)
20386 {
20387 int new_x = it.current_x + it.pixel_width;
20388
20389 new_pos = it.current.pos;
20390 if (new_x == it.current_x)
20391 new_x++;
20392 rc = move_it_in_display_line_to (&it, ZV, new_x,
20393 MOVE_TO_POS | MOVE_TO_X);
20394 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20395 break;
20396 }
20397 /* If we ended up on a composed character inside
20398 bidi-reordered text (e.g., Hebrew text with diacritics),
20399 the iterator gives us the buffer position of the last (in
20400 logical order) character of the composed grapheme cluster,
20401 which is not what we want. So we cheat: we compute the
20402 character position of the character that follows (in the
20403 logical order) the one where the above loop stopped. That
20404 character will appear on display to the left of point. */
20405 if (it.bidi_p
20406 && it.bidi_it.scan_dir == -1
20407 && new_pos.charpos - IT_CHARPOS (it) > 1)
20408 {
20409 new_pos.charpos = IT_CHARPOS (it) + 1;
20410 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20411 }
20412 it.current.pos = new_pos;
20413 }
20414 else
20415 #endif
20416 if (it.current_x != target_x)
20417 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20418
20419 /* When lines are truncated, the above loop will stop at the
20420 window edge. But we want to get to the end of line, even if
20421 it is beyond the window edge; automatic hscroll will then
20422 scroll the window to show point as appropriate. */
20423 if (target_is_eol_p && it.line_wrap == TRUNCATE
20424 && get_next_display_element (&it))
20425 {
20426 struct text_pos new_pos = it.current.pos;
20427
20428 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20429 {
20430 set_iterator_to_next (&it, 0);
20431 if (it.method == GET_FROM_BUFFER)
20432 new_pos = it.current.pos;
20433 if (!get_next_display_element (&it))
20434 break;
20435 }
20436
20437 it.current.pos = new_pos;
20438 }
20439
20440 /* If we ended up in a display string that covers point, move to
20441 buffer position to the right in the visual order. */
20442 if (dir > 0)
20443 {
20444 while (IT_CHARPOS (it) == PT)
20445 {
20446 set_iterator_to_next (&it, 0);
20447 if (!get_next_display_element (&it))
20448 break;
20449 }
20450 }
20451
20452 /* Move point to that position. */
20453 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20454 }
20455
20456 return make_number (PT);
20457
20458 #undef ROW_GLYPH_NEWLINE_P
20459 }
20460
20461 \f
20462 /***********************************************************************
20463 Menu Bar
20464 ***********************************************************************/
20465
20466 /* Redisplay the menu bar in the frame for window W.
20467
20468 The menu bar of X frames that don't have X toolkit support is
20469 displayed in a special window W->frame->menu_bar_window.
20470
20471 The menu bar of terminal frames is treated specially as far as
20472 glyph matrices are concerned. Menu bar lines are not part of
20473 windows, so the update is done directly on the frame matrix rows
20474 for the menu bar. */
20475
20476 static void
20477 display_menu_bar (struct window *w)
20478 {
20479 struct frame *f = XFRAME (WINDOW_FRAME (w));
20480 struct it it;
20481 Lisp_Object items;
20482 int i;
20483
20484 /* Don't do all this for graphical frames. */
20485 #ifdef HAVE_NTGUI
20486 if (FRAME_W32_P (f))
20487 return;
20488 #endif
20489 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20490 if (FRAME_X_P (f))
20491 return;
20492 #endif
20493
20494 #ifdef HAVE_NS
20495 if (FRAME_NS_P (f))
20496 return;
20497 #endif /* HAVE_NS */
20498
20499 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20500 eassert (!FRAME_WINDOW_P (f));
20501 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20502 it.first_visible_x = 0;
20503 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20504 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20505 if (FRAME_WINDOW_P (f))
20506 {
20507 /* Menu bar lines are displayed in the desired matrix of the
20508 dummy window menu_bar_window. */
20509 struct window *menu_w;
20510 menu_w = XWINDOW (f->menu_bar_window);
20511 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20512 MENU_FACE_ID);
20513 it.first_visible_x = 0;
20514 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20515 }
20516 else
20517 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20518 {
20519 /* This is a TTY frame, i.e. character hpos/vpos are used as
20520 pixel x/y. */
20521 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20522 MENU_FACE_ID);
20523 it.first_visible_x = 0;
20524 it.last_visible_x = FRAME_COLS (f);
20525 }
20526
20527 /* FIXME: This should be controlled by a user option. See the
20528 comments in redisplay_tool_bar and display_mode_line about
20529 this. */
20530 it.paragraph_embedding = L2R;
20531
20532 /* Clear all rows of the menu bar. */
20533 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20534 {
20535 struct glyph_row *row = it.glyph_row + i;
20536 clear_glyph_row (row);
20537 row->enabled_p = 1;
20538 row->full_width_p = 1;
20539 }
20540
20541 /* Display all items of the menu bar. */
20542 items = FRAME_MENU_BAR_ITEMS (it.f);
20543 for (i = 0; i < ASIZE (items); i += 4)
20544 {
20545 Lisp_Object string;
20546
20547 /* Stop at nil string. */
20548 string = AREF (items, i + 1);
20549 if (NILP (string))
20550 break;
20551
20552 /* Remember where item was displayed. */
20553 ASET (items, i + 3, make_number (it.hpos));
20554
20555 /* Display the item, pad with one space. */
20556 if (it.current_x < it.last_visible_x)
20557 display_string (NULL, string, Qnil, 0, 0, &it,
20558 SCHARS (string) + 1, 0, 0, -1);
20559 }
20560
20561 /* Fill out the line with spaces. */
20562 if (it.current_x < it.last_visible_x)
20563 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20564
20565 /* Compute the total height of the lines. */
20566 compute_line_metrics (&it);
20567 }
20568
20569
20570 \f
20571 /***********************************************************************
20572 Mode Line
20573 ***********************************************************************/
20574
20575 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20576 FORCE is non-zero, redisplay mode lines unconditionally.
20577 Otherwise, redisplay only mode lines that are garbaged. Value is
20578 the number of windows whose mode lines were redisplayed. */
20579
20580 static int
20581 redisplay_mode_lines (Lisp_Object window, int force)
20582 {
20583 int nwindows = 0;
20584
20585 while (!NILP (window))
20586 {
20587 struct window *w = XWINDOW (window);
20588
20589 if (WINDOWP (w->contents))
20590 nwindows += redisplay_mode_lines (w->contents, force);
20591 else if (force
20592 || FRAME_GARBAGED_P (XFRAME (w->frame))
20593 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20594 {
20595 struct text_pos lpoint;
20596 struct buffer *old = current_buffer;
20597
20598 /* Set the window's buffer for the mode line display. */
20599 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20600 set_buffer_internal_1 (XBUFFER (w->contents));
20601
20602 /* Point refers normally to the selected window. For any
20603 other window, set up appropriate value. */
20604 if (!EQ (window, selected_window))
20605 {
20606 struct text_pos pt;
20607
20608 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20609 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20610 }
20611
20612 /* Display mode lines. */
20613 clear_glyph_matrix (w->desired_matrix);
20614 if (display_mode_lines (w))
20615 {
20616 ++nwindows;
20617 w->must_be_updated_p = 1;
20618 }
20619
20620 /* Restore old settings. */
20621 set_buffer_internal_1 (old);
20622 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20623 }
20624
20625 window = w->next;
20626 }
20627
20628 return nwindows;
20629 }
20630
20631
20632 /* Display the mode and/or header line of window W. Value is the
20633 sum number of mode lines and header lines displayed. */
20634
20635 static int
20636 display_mode_lines (struct window *w)
20637 {
20638 Lisp_Object old_selected_window = selected_window;
20639 Lisp_Object old_selected_frame = selected_frame;
20640 Lisp_Object new_frame = w->frame;
20641 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20642 int n = 0;
20643
20644 selected_frame = new_frame;
20645 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20646 or window's point, then we'd need select_window_1 here as well. */
20647 XSETWINDOW (selected_window, w);
20648 XFRAME (new_frame)->selected_window = selected_window;
20649
20650 /* These will be set while the mode line specs are processed. */
20651 line_number_displayed = 0;
20652 w->column_number_displayed = -1;
20653
20654 if (WINDOW_WANTS_MODELINE_P (w))
20655 {
20656 struct window *sel_w = XWINDOW (old_selected_window);
20657
20658 /* Select mode line face based on the real selected window. */
20659 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20660 BVAR (current_buffer, mode_line_format));
20661 ++n;
20662 }
20663
20664 if (WINDOW_WANTS_HEADER_LINE_P (w))
20665 {
20666 display_mode_line (w, HEADER_LINE_FACE_ID,
20667 BVAR (current_buffer, header_line_format));
20668 ++n;
20669 }
20670
20671 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20672 selected_frame = old_selected_frame;
20673 selected_window = old_selected_window;
20674 return n;
20675 }
20676
20677
20678 /* Display mode or header line of window W. FACE_ID specifies which
20679 line to display; it is either MODE_LINE_FACE_ID or
20680 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20681 display. Value is the pixel height of the mode/header line
20682 displayed. */
20683
20684 static int
20685 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20686 {
20687 struct it it;
20688 struct face *face;
20689 ptrdiff_t count = SPECPDL_INDEX ();
20690
20691 init_iterator (&it, w, -1, -1, NULL, face_id);
20692 /* Don't extend on a previously drawn mode-line.
20693 This may happen if called from pos_visible_p. */
20694 it.glyph_row->enabled_p = 0;
20695 prepare_desired_row (it.glyph_row);
20696
20697 it.glyph_row->mode_line_p = 1;
20698
20699 /* FIXME: This should be controlled by a user option. But
20700 supporting such an option is not trivial, since the mode line is
20701 made up of many separate strings. */
20702 it.paragraph_embedding = L2R;
20703
20704 record_unwind_protect (unwind_format_mode_line,
20705 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20706
20707 mode_line_target = MODE_LINE_DISPLAY;
20708
20709 /* Temporarily make frame's keyboard the current kboard so that
20710 kboard-local variables in the mode_line_format will get the right
20711 values. */
20712 push_kboard (FRAME_KBOARD (it.f));
20713 record_unwind_save_match_data ();
20714 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20715 pop_kboard ();
20716
20717 unbind_to (count, Qnil);
20718
20719 /* Fill up with spaces. */
20720 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20721
20722 compute_line_metrics (&it);
20723 it.glyph_row->full_width_p = 1;
20724 it.glyph_row->continued_p = 0;
20725 it.glyph_row->truncated_on_left_p = 0;
20726 it.glyph_row->truncated_on_right_p = 0;
20727
20728 /* Make a 3D mode-line have a shadow at its right end. */
20729 face = FACE_FROM_ID (it.f, face_id);
20730 extend_face_to_end_of_line (&it);
20731 if (face->box != FACE_NO_BOX)
20732 {
20733 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20734 + it.glyph_row->used[TEXT_AREA] - 1);
20735 last->right_box_line_p = 1;
20736 }
20737
20738 return it.glyph_row->height;
20739 }
20740
20741 /* Move element ELT in LIST to the front of LIST.
20742 Return the updated list. */
20743
20744 static Lisp_Object
20745 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20746 {
20747 register Lisp_Object tail, prev;
20748 register Lisp_Object tem;
20749
20750 tail = list;
20751 prev = Qnil;
20752 while (CONSP (tail))
20753 {
20754 tem = XCAR (tail);
20755
20756 if (EQ (elt, tem))
20757 {
20758 /* Splice out the link TAIL. */
20759 if (NILP (prev))
20760 list = XCDR (tail);
20761 else
20762 Fsetcdr (prev, XCDR (tail));
20763
20764 /* Now make it the first. */
20765 Fsetcdr (tail, list);
20766 return tail;
20767 }
20768 else
20769 prev = tail;
20770 tail = XCDR (tail);
20771 QUIT;
20772 }
20773
20774 /* Not found--return unchanged LIST. */
20775 return list;
20776 }
20777
20778 /* Contribute ELT to the mode line for window IT->w. How it
20779 translates into text depends on its data type.
20780
20781 IT describes the display environment in which we display, as usual.
20782
20783 DEPTH is the depth in recursion. It is used to prevent
20784 infinite recursion here.
20785
20786 FIELD_WIDTH is the number of characters the display of ELT should
20787 occupy in the mode line, and PRECISION is the maximum number of
20788 characters to display from ELT's representation. See
20789 display_string for details.
20790
20791 Returns the hpos of the end of the text generated by ELT.
20792
20793 PROPS is a property list to add to any string we encounter.
20794
20795 If RISKY is nonzero, remove (disregard) any properties in any string
20796 we encounter, and ignore :eval and :propertize.
20797
20798 The global variable `mode_line_target' determines whether the
20799 output is passed to `store_mode_line_noprop',
20800 `store_mode_line_string', or `display_string'. */
20801
20802 static int
20803 display_mode_element (struct it *it, int depth, int field_width, int precision,
20804 Lisp_Object elt, Lisp_Object props, int risky)
20805 {
20806 int n = 0, field, prec;
20807 int literal = 0;
20808
20809 tail_recurse:
20810 if (depth > 100)
20811 elt = build_string ("*too-deep*");
20812
20813 depth++;
20814
20815 switch (XTYPE (elt))
20816 {
20817 case Lisp_String:
20818 {
20819 /* A string: output it and check for %-constructs within it. */
20820 unsigned char c;
20821 ptrdiff_t offset = 0;
20822
20823 if (SCHARS (elt) > 0
20824 && (!NILP (props) || risky))
20825 {
20826 Lisp_Object oprops, aelt;
20827 oprops = Ftext_properties_at (make_number (0), elt);
20828
20829 /* If the starting string's properties are not what
20830 we want, translate the string. Also, if the string
20831 is risky, do that anyway. */
20832
20833 if (NILP (Fequal (props, oprops)) || risky)
20834 {
20835 /* If the starting string has properties,
20836 merge the specified ones onto the existing ones. */
20837 if (! NILP (oprops) && !risky)
20838 {
20839 Lisp_Object tem;
20840
20841 oprops = Fcopy_sequence (oprops);
20842 tem = props;
20843 while (CONSP (tem))
20844 {
20845 oprops = Fplist_put (oprops, XCAR (tem),
20846 XCAR (XCDR (tem)));
20847 tem = XCDR (XCDR (tem));
20848 }
20849 props = oprops;
20850 }
20851
20852 aelt = Fassoc (elt, mode_line_proptrans_alist);
20853 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20854 {
20855 /* AELT is what we want. Move it to the front
20856 without consing. */
20857 elt = XCAR (aelt);
20858 mode_line_proptrans_alist
20859 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20860 }
20861 else
20862 {
20863 Lisp_Object tem;
20864
20865 /* If AELT has the wrong props, it is useless.
20866 so get rid of it. */
20867 if (! NILP (aelt))
20868 mode_line_proptrans_alist
20869 = Fdelq (aelt, mode_line_proptrans_alist);
20870
20871 elt = Fcopy_sequence (elt);
20872 Fset_text_properties (make_number (0), Flength (elt),
20873 props, elt);
20874 /* Add this item to mode_line_proptrans_alist. */
20875 mode_line_proptrans_alist
20876 = Fcons (Fcons (elt, props),
20877 mode_line_proptrans_alist);
20878 /* Truncate mode_line_proptrans_alist
20879 to at most 50 elements. */
20880 tem = Fnthcdr (make_number (50),
20881 mode_line_proptrans_alist);
20882 if (! NILP (tem))
20883 XSETCDR (tem, Qnil);
20884 }
20885 }
20886 }
20887
20888 offset = 0;
20889
20890 if (literal)
20891 {
20892 prec = precision - n;
20893 switch (mode_line_target)
20894 {
20895 case MODE_LINE_NOPROP:
20896 case MODE_LINE_TITLE:
20897 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20898 break;
20899 case MODE_LINE_STRING:
20900 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20901 break;
20902 case MODE_LINE_DISPLAY:
20903 n += display_string (NULL, elt, Qnil, 0, 0, it,
20904 0, prec, 0, STRING_MULTIBYTE (elt));
20905 break;
20906 }
20907
20908 break;
20909 }
20910
20911 /* Handle the non-literal case. */
20912
20913 while ((precision <= 0 || n < precision)
20914 && SREF (elt, offset) != 0
20915 && (mode_line_target != MODE_LINE_DISPLAY
20916 || it->current_x < it->last_visible_x))
20917 {
20918 ptrdiff_t last_offset = offset;
20919
20920 /* Advance to end of string or next format specifier. */
20921 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20922 ;
20923
20924 if (offset - 1 != last_offset)
20925 {
20926 ptrdiff_t nchars, nbytes;
20927
20928 /* Output to end of string or up to '%'. Field width
20929 is length of string. Don't output more than
20930 PRECISION allows us. */
20931 offset--;
20932
20933 prec = c_string_width (SDATA (elt) + last_offset,
20934 offset - last_offset, precision - n,
20935 &nchars, &nbytes);
20936
20937 switch (mode_line_target)
20938 {
20939 case MODE_LINE_NOPROP:
20940 case MODE_LINE_TITLE:
20941 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20942 break;
20943 case MODE_LINE_STRING:
20944 {
20945 ptrdiff_t bytepos = last_offset;
20946 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20947 ptrdiff_t endpos = (precision <= 0
20948 ? string_byte_to_char (elt, offset)
20949 : charpos + nchars);
20950
20951 n += store_mode_line_string (NULL,
20952 Fsubstring (elt, make_number (charpos),
20953 make_number (endpos)),
20954 0, 0, 0, Qnil);
20955 }
20956 break;
20957 case MODE_LINE_DISPLAY:
20958 {
20959 ptrdiff_t bytepos = last_offset;
20960 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20961
20962 if (precision <= 0)
20963 nchars = string_byte_to_char (elt, offset) - charpos;
20964 n += display_string (NULL, elt, Qnil, 0, charpos,
20965 it, 0, nchars, 0,
20966 STRING_MULTIBYTE (elt));
20967 }
20968 break;
20969 }
20970 }
20971 else /* c == '%' */
20972 {
20973 ptrdiff_t percent_position = offset;
20974
20975 /* Get the specified minimum width. Zero means
20976 don't pad. */
20977 field = 0;
20978 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20979 field = field * 10 + c - '0';
20980
20981 /* Don't pad beyond the total padding allowed. */
20982 if (field_width - n > 0 && field > field_width - n)
20983 field = field_width - n;
20984
20985 /* Note that either PRECISION <= 0 or N < PRECISION. */
20986 prec = precision - n;
20987
20988 if (c == 'M')
20989 n += display_mode_element (it, depth, field, prec,
20990 Vglobal_mode_string, props,
20991 risky);
20992 else if (c != 0)
20993 {
20994 bool multibyte;
20995 ptrdiff_t bytepos, charpos;
20996 const char *spec;
20997 Lisp_Object string;
20998
20999 bytepos = percent_position;
21000 charpos = (STRING_MULTIBYTE (elt)
21001 ? string_byte_to_char (elt, bytepos)
21002 : bytepos);
21003 spec = decode_mode_spec (it->w, c, field, &string);
21004 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21005
21006 switch (mode_line_target)
21007 {
21008 case MODE_LINE_NOPROP:
21009 case MODE_LINE_TITLE:
21010 n += store_mode_line_noprop (spec, field, prec);
21011 break;
21012 case MODE_LINE_STRING:
21013 {
21014 Lisp_Object tem = build_string (spec);
21015 props = Ftext_properties_at (make_number (charpos), elt);
21016 /* Should only keep face property in props */
21017 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21018 }
21019 break;
21020 case MODE_LINE_DISPLAY:
21021 {
21022 int nglyphs_before, nwritten;
21023
21024 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21025 nwritten = display_string (spec, string, elt,
21026 charpos, 0, it,
21027 field, prec, 0,
21028 multibyte);
21029
21030 /* Assign to the glyphs written above the
21031 string where the `%x' came from, position
21032 of the `%'. */
21033 if (nwritten > 0)
21034 {
21035 struct glyph *glyph
21036 = (it->glyph_row->glyphs[TEXT_AREA]
21037 + nglyphs_before);
21038 int i;
21039
21040 for (i = 0; i < nwritten; ++i)
21041 {
21042 glyph[i].object = elt;
21043 glyph[i].charpos = charpos;
21044 }
21045
21046 n += nwritten;
21047 }
21048 }
21049 break;
21050 }
21051 }
21052 else /* c == 0 */
21053 break;
21054 }
21055 }
21056 }
21057 break;
21058
21059 case Lisp_Symbol:
21060 /* A symbol: process the value of the symbol recursively
21061 as if it appeared here directly. Avoid error if symbol void.
21062 Special case: if value of symbol is a string, output the string
21063 literally. */
21064 {
21065 register Lisp_Object tem;
21066
21067 /* If the variable is not marked as risky to set
21068 then its contents are risky to use. */
21069 if (NILP (Fget (elt, Qrisky_local_variable)))
21070 risky = 1;
21071
21072 tem = Fboundp (elt);
21073 if (!NILP (tem))
21074 {
21075 tem = Fsymbol_value (elt);
21076 /* If value is a string, output that string literally:
21077 don't check for % within it. */
21078 if (STRINGP (tem))
21079 literal = 1;
21080
21081 if (!EQ (tem, elt))
21082 {
21083 /* Give up right away for nil or t. */
21084 elt = tem;
21085 goto tail_recurse;
21086 }
21087 }
21088 }
21089 break;
21090
21091 case Lisp_Cons:
21092 {
21093 register Lisp_Object car, tem;
21094
21095 /* A cons cell: five distinct cases.
21096 If first element is :eval or :propertize, do something special.
21097 If first element is a string or a cons, process all the elements
21098 and effectively concatenate them.
21099 If first element is a negative number, truncate displaying cdr to
21100 at most that many characters. If positive, pad (with spaces)
21101 to at least that many characters.
21102 If first element is a symbol, process the cadr or caddr recursively
21103 according to whether the symbol's value is non-nil or nil. */
21104 car = XCAR (elt);
21105 if (EQ (car, QCeval))
21106 {
21107 /* An element of the form (:eval FORM) means evaluate FORM
21108 and use the result as mode line elements. */
21109
21110 if (risky)
21111 break;
21112
21113 if (CONSP (XCDR (elt)))
21114 {
21115 Lisp_Object spec;
21116 spec = safe_eval (XCAR (XCDR (elt)));
21117 n += display_mode_element (it, depth, field_width - n,
21118 precision - n, spec, props,
21119 risky);
21120 }
21121 }
21122 else if (EQ (car, QCpropertize))
21123 {
21124 /* An element of the form (:propertize ELT PROPS...)
21125 means display ELT but applying properties PROPS. */
21126
21127 if (risky)
21128 break;
21129
21130 if (CONSP (XCDR (elt)))
21131 n += display_mode_element (it, depth, field_width - n,
21132 precision - n, XCAR (XCDR (elt)),
21133 XCDR (XCDR (elt)), risky);
21134 }
21135 else if (SYMBOLP (car))
21136 {
21137 tem = Fboundp (car);
21138 elt = XCDR (elt);
21139 if (!CONSP (elt))
21140 goto invalid;
21141 /* elt is now the cdr, and we know it is a cons cell.
21142 Use its car if CAR has a non-nil value. */
21143 if (!NILP (tem))
21144 {
21145 tem = Fsymbol_value (car);
21146 if (!NILP (tem))
21147 {
21148 elt = XCAR (elt);
21149 goto tail_recurse;
21150 }
21151 }
21152 /* Symbol's value is nil (or symbol is unbound)
21153 Get the cddr of the original list
21154 and if possible find the caddr and use that. */
21155 elt = XCDR (elt);
21156 if (NILP (elt))
21157 break;
21158 else if (!CONSP (elt))
21159 goto invalid;
21160 elt = XCAR (elt);
21161 goto tail_recurse;
21162 }
21163 else if (INTEGERP (car))
21164 {
21165 register int lim = XINT (car);
21166 elt = XCDR (elt);
21167 if (lim < 0)
21168 {
21169 /* Negative int means reduce maximum width. */
21170 if (precision <= 0)
21171 precision = -lim;
21172 else
21173 precision = min (precision, -lim);
21174 }
21175 else if (lim > 0)
21176 {
21177 /* Padding specified. Don't let it be more than
21178 current maximum. */
21179 if (precision > 0)
21180 lim = min (precision, lim);
21181
21182 /* If that's more padding than already wanted, queue it.
21183 But don't reduce padding already specified even if
21184 that is beyond the current truncation point. */
21185 field_width = max (lim, field_width);
21186 }
21187 goto tail_recurse;
21188 }
21189 else if (STRINGP (car) || CONSP (car))
21190 {
21191 Lisp_Object halftail = elt;
21192 int len = 0;
21193
21194 while (CONSP (elt)
21195 && (precision <= 0 || n < precision))
21196 {
21197 n += display_mode_element (it, depth,
21198 /* Do padding only after the last
21199 element in the list. */
21200 (! CONSP (XCDR (elt))
21201 ? field_width - n
21202 : 0),
21203 precision - n, XCAR (elt),
21204 props, risky);
21205 elt = XCDR (elt);
21206 len++;
21207 if ((len & 1) == 0)
21208 halftail = XCDR (halftail);
21209 /* Check for cycle. */
21210 if (EQ (halftail, elt))
21211 break;
21212 }
21213 }
21214 }
21215 break;
21216
21217 default:
21218 invalid:
21219 elt = build_string ("*invalid*");
21220 goto tail_recurse;
21221 }
21222
21223 /* Pad to FIELD_WIDTH. */
21224 if (field_width > 0 && n < field_width)
21225 {
21226 switch (mode_line_target)
21227 {
21228 case MODE_LINE_NOPROP:
21229 case MODE_LINE_TITLE:
21230 n += store_mode_line_noprop ("", field_width - n, 0);
21231 break;
21232 case MODE_LINE_STRING:
21233 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21234 break;
21235 case MODE_LINE_DISPLAY:
21236 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21237 0, 0, 0);
21238 break;
21239 }
21240 }
21241
21242 return n;
21243 }
21244
21245 /* Store a mode-line string element in mode_line_string_list.
21246
21247 If STRING is non-null, display that C string. Otherwise, the Lisp
21248 string LISP_STRING is displayed.
21249
21250 FIELD_WIDTH is the minimum number of output glyphs to produce.
21251 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21252 with spaces. FIELD_WIDTH <= 0 means don't pad.
21253
21254 PRECISION is the maximum number of characters to output from
21255 STRING. PRECISION <= 0 means don't truncate the string.
21256
21257 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21258 properties to the string.
21259
21260 PROPS are the properties to add to the string.
21261 The mode_line_string_face face property is always added to the string.
21262 */
21263
21264 static int
21265 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21266 int field_width, int precision, Lisp_Object props)
21267 {
21268 ptrdiff_t len;
21269 int n = 0;
21270
21271 if (string != NULL)
21272 {
21273 len = strlen (string);
21274 if (precision > 0 && len > precision)
21275 len = precision;
21276 lisp_string = make_string (string, len);
21277 if (NILP (props))
21278 props = mode_line_string_face_prop;
21279 else if (!NILP (mode_line_string_face))
21280 {
21281 Lisp_Object face = Fplist_get (props, Qface);
21282 props = Fcopy_sequence (props);
21283 if (NILP (face))
21284 face = mode_line_string_face;
21285 else
21286 face = list2 (face, mode_line_string_face);
21287 props = Fplist_put (props, Qface, face);
21288 }
21289 Fadd_text_properties (make_number (0), make_number (len),
21290 props, lisp_string);
21291 }
21292 else
21293 {
21294 len = XFASTINT (Flength (lisp_string));
21295 if (precision > 0 && len > precision)
21296 {
21297 len = precision;
21298 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21299 precision = -1;
21300 }
21301 if (!NILP (mode_line_string_face))
21302 {
21303 Lisp_Object face;
21304 if (NILP (props))
21305 props = Ftext_properties_at (make_number (0), lisp_string);
21306 face = Fplist_get (props, Qface);
21307 if (NILP (face))
21308 face = mode_line_string_face;
21309 else
21310 face = list2 (face, mode_line_string_face);
21311 props = list2 (Qface, face);
21312 if (copy_string)
21313 lisp_string = Fcopy_sequence (lisp_string);
21314 }
21315 if (!NILP (props))
21316 Fadd_text_properties (make_number (0), make_number (len),
21317 props, lisp_string);
21318 }
21319
21320 if (len > 0)
21321 {
21322 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21323 n += len;
21324 }
21325
21326 if (field_width > len)
21327 {
21328 field_width -= len;
21329 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21330 if (!NILP (props))
21331 Fadd_text_properties (make_number (0), make_number (field_width),
21332 props, lisp_string);
21333 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21334 n += field_width;
21335 }
21336
21337 return n;
21338 }
21339
21340
21341 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21342 1, 4, 0,
21343 doc: /* Format a string out of a mode line format specification.
21344 First arg FORMAT specifies the mode line format (see `mode-line-format'
21345 for details) to use.
21346
21347 By default, the format is evaluated for the currently selected window.
21348
21349 Optional second arg FACE specifies the face property to put on all
21350 characters for which no face is specified. The value nil means the
21351 default face. The value t means whatever face the window's mode line
21352 currently uses (either `mode-line' or `mode-line-inactive',
21353 depending on whether the window is the selected window or not).
21354 An integer value means the value string has no text
21355 properties.
21356
21357 Optional third and fourth args WINDOW and BUFFER specify the window
21358 and buffer to use as the context for the formatting (defaults
21359 are the selected window and the WINDOW's buffer). */)
21360 (Lisp_Object format, Lisp_Object face,
21361 Lisp_Object window, Lisp_Object buffer)
21362 {
21363 struct it it;
21364 int len;
21365 struct window *w;
21366 struct buffer *old_buffer = NULL;
21367 int face_id;
21368 int no_props = INTEGERP (face);
21369 ptrdiff_t count = SPECPDL_INDEX ();
21370 Lisp_Object str;
21371 int string_start = 0;
21372
21373 w = decode_any_window (window);
21374 XSETWINDOW (window, w);
21375
21376 if (NILP (buffer))
21377 buffer = w->contents;
21378 CHECK_BUFFER (buffer);
21379
21380 /* Make formatting the modeline a non-op when noninteractive, otherwise
21381 there will be problems later caused by a partially initialized frame. */
21382 if (NILP (format) || noninteractive)
21383 return empty_unibyte_string;
21384
21385 if (no_props)
21386 face = Qnil;
21387
21388 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21389 : EQ (face, Qt) ? (EQ (window, selected_window)
21390 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21391 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21392 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21393 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21394 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21395 : DEFAULT_FACE_ID;
21396
21397 old_buffer = current_buffer;
21398
21399 /* Save things including mode_line_proptrans_alist,
21400 and set that to nil so that we don't alter the outer value. */
21401 record_unwind_protect (unwind_format_mode_line,
21402 format_mode_line_unwind_data
21403 (XFRAME (WINDOW_FRAME (w)),
21404 old_buffer, selected_window, 1));
21405 mode_line_proptrans_alist = Qnil;
21406
21407 Fselect_window (window, Qt);
21408 set_buffer_internal_1 (XBUFFER (buffer));
21409
21410 init_iterator (&it, w, -1, -1, NULL, face_id);
21411
21412 if (no_props)
21413 {
21414 mode_line_target = MODE_LINE_NOPROP;
21415 mode_line_string_face_prop = Qnil;
21416 mode_line_string_list = Qnil;
21417 string_start = MODE_LINE_NOPROP_LEN (0);
21418 }
21419 else
21420 {
21421 mode_line_target = MODE_LINE_STRING;
21422 mode_line_string_list = Qnil;
21423 mode_line_string_face = face;
21424 mode_line_string_face_prop
21425 = NILP (face) ? Qnil : list2 (Qface, face);
21426 }
21427
21428 push_kboard (FRAME_KBOARD (it.f));
21429 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21430 pop_kboard ();
21431
21432 if (no_props)
21433 {
21434 len = MODE_LINE_NOPROP_LEN (string_start);
21435 str = make_string (mode_line_noprop_buf + string_start, len);
21436 }
21437 else
21438 {
21439 mode_line_string_list = Fnreverse (mode_line_string_list);
21440 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21441 empty_unibyte_string);
21442 }
21443
21444 unbind_to (count, Qnil);
21445 return str;
21446 }
21447
21448 /* Write a null-terminated, right justified decimal representation of
21449 the positive integer D to BUF using a minimal field width WIDTH. */
21450
21451 static void
21452 pint2str (register char *buf, register int width, register ptrdiff_t d)
21453 {
21454 register char *p = buf;
21455
21456 if (d <= 0)
21457 *p++ = '0';
21458 else
21459 {
21460 while (d > 0)
21461 {
21462 *p++ = d % 10 + '0';
21463 d /= 10;
21464 }
21465 }
21466
21467 for (width -= (int) (p - buf); width > 0; --width)
21468 *p++ = ' ';
21469 *p-- = '\0';
21470 while (p > buf)
21471 {
21472 d = *buf;
21473 *buf++ = *p;
21474 *p-- = d;
21475 }
21476 }
21477
21478 /* Write a null-terminated, right justified decimal and "human
21479 readable" representation of the nonnegative integer D to BUF using
21480 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21481
21482 static const char power_letter[] =
21483 {
21484 0, /* no letter */
21485 'k', /* kilo */
21486 'M', /* mega */
21487 'G', /* giga */
21488 'T', /* tera */
21489 'P', /* peta */
21490 'E', /* exa */
21491 'Z', /* zetta */
21492 'Y' /* yotta */
21493 };
21494
21495 static void
21496 pint2hrstr (char *buf, int width, ptrdiff_t d)
21497 {
21498 /* We aim to represent the nonnegative integer D as
21499 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21500 ptrdiff_t quotient = d;
21501 int remainder = 0;
21502 /* -1 means: do not use TENTHS. */
21503 int tenths = -1;
21504 int exponent = 0;
21505
21506 /* Length of QUOTIENT.TENTHS as a string. */
21507 int length;
21508
21509 char * psuffix;
21510 char * p;
21511
21512 if (quotient >= 1000)
21513 {
21514 /* Scale to the appropriate EXPONENT. */
21515 do
21516 {
21517 remainder = quotient % 1000;
21518 quotient /= 1000;
21519 exponent++;
21520 }
21521 while (quotient >= 1000);
21522
21523 /* Round to nearest and decide whether to use TENTHS or not. */
21524 if (quotient <= 9)
21525 {
21526 tenths = remainder / 100;
21527 if (remainder % 100 >= 50)
21528 {
21529 if (tenths < 9)
21530 tenths++;
21531 else
21532 {
21533 quotient++;
21534 if (quotient == 10)
21535 tenths = -1;
21536 else
21537 tenths = 0;
21538 }
21539 }
21540 }
21541 else
21542 if (remainder >= 500)
21543 {
21544 if (quotient < 999)
21545 quotient++;
21546 else
21547 {
21548 quotient = 1;
21549 exponent++;
21550 tenths = 0;
21551 }
21552 }
21553 }
21554
21555 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21556 if (tenths == -1 && quotient <= 99)
21557 if (quotient <= 9)
21558 length = 1;
21559 else
21560 length = 2;
21561 else
21562 length = 3;
21563 p = psuffix = buf + max (width, length);
21564
21565 /* Print EXPONENT. */
21566 *psuffix++ = power_letter[exponent];
21567 *psuffix = '\0';
21568
21569 /* Print TENTHS. */
21570 if (tenths >= 0)
21571 {
21572 *--p = '0' + tenths;
21573 *--p = '.';
21574 }
21575
21576 /* Print QUOTIENT. */
21577 do
21578 {
21579 int digit = quotient % 10;
21580 *--p = '0' + digit;
21581 }
21582 while ((quotient /= 10) != 0);
21583
21584 /* Print leading spaces. */
21585 while (buf < p)
21586 *--p = ' ';
21587 }
21588
21589 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21590 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21591 type of CODING_SYSTEM. Return updated pointer into BUF. */
21592
21593 static unsigned char invalid_eol_type[] = "(*invalid*)";
21594
21595 static char *
21596 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21597 {
21598 Lisp_Object val;
21599 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21600 const unsigned char *eol_str;
21601 int eol_str_len;
21602 /* The EOL conversion we are using. */
21603 Lisp_Object eoltype;
21604
21605 val = CODING_SYSTEM_SPEC (coding_system);
21606 eoltype = Qnil;
21607
21608 if (!VECTORP (val)) /* Not yet decided. */
21609 {
21610 *buf++ = multibyte ? '-' : ' ';
21611 if (eol_flag)
21612 eoltype = eol_mnemonic_undecided;
21613 /* Don't mention EOL conversion if it isn't decided. */
21614 }
21615 else
21616 {
21617 Lisp_Object attrs;
21618 Lisp_Object eolvalue;
21619
21620 attrs = AREF (val, 0);
21621 eolvalue = AREF (val, 2);
21622
21623 *buf++ = multibyte
21624 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21625 : ' ';
21626
21627 if (eol_flag)
21628 {
21629 /* The EOL conversion that is normal on this system. */
21630
21631 if (NILP (eolvalue)) /* Not yet decided. */
21632 eoltype = eol_mnemonic_undecided;
21633 else if (VECTORP (eolvalue)) /* Not yet decided. */
21634 eoltype = eol_mnemonic_undecided;
21635 else /* eolvalue is Qunix, Qdos, or Qmac. */
21636 eoltype = (EQ (eolvalue, Qunix)
21637 ? eol_mnemonic_unix
21638 : (EQ (eolvalue, Qdos) == 1
21639 ? eol_mnemonic_dos : eol_mnemonic_mac));
21640 }
21641 }
21642
21643 if (eol_flag)
21644 {
21645 /* Mention the EOL conversion if it is not the usual one. */
21646 if (STRINGP (eoltype))
21647 {
21648 eol_str = SDATA (eoltype);
21649 eol_str_len = SBYTES (eoltype);
21650 }
21651 else if (CHARACTERP (eoltype))
21652 {
21653 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21654 int c = XFASTINT (eoltype);
21655 eol_str_len = CHAR_STRING (c, tmp);
21656 eol_str = tmp;
21657 }
21658 else
21659 {
21660 eol_str = invalid_eol_type;
21661 eol_str_len = sizeof (invalid_eol_type) - 1;
21662 }
21663 memcpy (buf, eol_str, eol_str_len);
21664 buf += eol_str_len;
21665 }
21666
21667 return buf;
21668 }
21669
21670 /* Return a string for the output of a mode line %-spec for window W,
21671 generated by character C. FIELD_WIDTH > 0 means pad the string
21672 returned with spaces to that value. Return a Lisp string in
21673 *STRING if the resulting string is taken from that Lisp string.
21674
21675 Note we operate on the current buffer for most purposes. */
21676
21677 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21678
21679 static const char *
21680 decode_mode_spec (struct window *w, register int c, int field_width,
21681 Lisp_Object *string)
21682 {
21683 Lisp_Object obj;
21684 struct frame *f = XFRAME (WINDOW_FRAME (w));
21685 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21686 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21687 produce strings from numerical values, so limit preposterously
21688 large values of FIELD_WIDTH to avoid overrunning the buffer's
21689 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21690 bytes plus the terminating null. */
21691 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21692 struct buffer *b = current_buffer;
21693
21694 obj = Qnil;
21695 *string = Qnil;
21696
21697 switch (c)
21698 {
21699 case '*':
21700 if (!NILP (BVAR (b, read_only)))
21701 return "%";
21702 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21703 return "*";
21704 return "-";
21705
21706 case '+':
21707 /* This differs from %* only for a modified read-only buffer. */
21708 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21709 return "*";
21710 if (!NILP (BVAR (b, read_only)))
21711 return "%";
21712 return "-";
21713
21714 case '&':
21715 /* This differs from %* in ignoring read-only-ness. */
21716 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21717 return "*";
21718 return "-";
21719
21720 case '%':
21721 return "%";
21722
21723 case '[':
21724 {
21725 int i;
21726 char *p;
21727
21728 if (command_loop_level > 5)
21729 return "[[[... ";
21730 p = decode_mode_spec_buf;
21731 for (i = 0; i < command_loop_level; i++)
21732 *p++ = '[';
21733 *p = 0;
21734 return decode_mode_spec_buf;
21735 }
21736
21737 case ']':
21738 {
21739 int i;
21740 char *p;
21741
21742 if (command_loop_level > 5)
21743 return " ...]]]";
21744 p = decode_mode_spec_buf;
21745 for (i = 0; i < command_loop_level; i++)
21746 *p++ = ']';
21747 *p = 0;
21748 return decode_mode_spec_buf;
21749 }
21750
21751 case '-':
21752 {
21753 register int i;
21754
21755 /* Let lots_of_dashes be a string of infinite length. */
21756 if (mode_line_target == MODE_LINE_NOPROP
21757 || mode_line_target == MODE_LINE_STRING)
21758 return "--";
21759 if (field_width <= 0
21760 || field_width > sizeof (lots_of_dashes))
21761 {
21762 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21763 decode_mode_spec_buf[i] = '-';
21764 decode_mode_spec_buf[i] = '\0';
21765 return decode_mode_spec_buf;
21766 }
21767 else
21768 return lots_of_dashes;
21769 }
21770
21771 case 'b':
21772 obj = BVAR (b, name);
21773 break;
21774
21775 case 'c':
21776 /* %c and %l are ignored in `frame-title-format'.
21777 (In redisplay_internal, the frame title is drawn _before_ the
21778 windows are updated, so the stuff which depends on actual
21779 window contents (such as %l) may fail to render properly, or
21780 even crash emacs.) */
21781 if (mode_line_target == MODE_LINE_TITLE)
21782 return "";
21783 else
21784 {
21785 ptrdiff_t col = current_column ();
21786 w->column_number_displayed = col;
21787 pint2str (decode_mode_spec_buf, width, col);
21788 return decode_mode_spec_buf;
21789 }
21790
21791 case 'e':
21792 #ifndef SYSTEM_MALLOC
21793 {
21794 if (NILP (Vmemory_full))
21795 return "";
21796 else
21797 return "!MEM FULL! ";
21798 }
21799 #else
21800 return "";
21801 #endif
21802
21803 case 'F':
21804 /* %F displays the frame name. */
21805 if (!NILP (f->title))
21806 return SSDATA (f->title);
21807 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21808 return SSDATA (f->name);
21809 return "Emacs";
21810
21811 case 'f':
21812 obj = BVAR (b, filename);
21813 break;
21814
21815 case 'i':
21816 {
21817 ptrdiff_t size = ZV - BEGV;
21818 pint2str (decode_mode_spec_buf, width, size);
21819 return decode_mode_spec_buf;
21820 }
21821
21822 case 'I':
21823 {
21824 ptrdiff_t size = ZV - BEGV;
21825 pint2hrstr (decode_mode_spec_buf, width, size);
21826 return decode_mode_spec_buf;
21827 }
21828
21829 case 'l':
21830 {
21831 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21832 ptrdiff_t topline, nlines, height;
21833 ptrdiff_t junk;
21834
21835 /* %c and %l are ignored in `frame-title-format'. */
21836 if (mode_line_target == MODE_LINE_TITLE)
21837 return "";
21838
21839 startpos = marker_position (w->start);
21840 startpos_byte = marker_byte_position (w->start);
21841 height = WINDOW_TOTAL_LINES (w);
21842
21843 /* If we decided that this buffer isn't suitable for line numbers,
21844 don't forget that too fast. */
21845 if (w->base_line_pos == -1)
21846 goto no_value;
21847
21848 /* If the buffer is very big, don't waste time. */
21849 if (INTEGERP (Vline_number_display_limit)
21850 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21851 {
21852 w->base_line_pos = 0;
21853 w->base_line_number = 0;
21854 goto no_value;
21855 }
21856
21857 if (w->base_line_number > 0
21858 && w->base_line_pos > 0
21859 && w->base_line_pos <= startpos)
21860 {
21861 line = w->base_line_number;
21862 linepos = w->base_line_pos;
21863 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21864 }
21865 else
21866 {
21867 line = 1;
21868 linepos = BUF_BEGV (b);
21869 linepos_byte = BUF_BEGV_BYTE (b);
21870 }
21871
21872 /* Count lines from base line to window start position. */
21873 nlines = display_count_lines (linepos_byte,
21874 startpos_byte,
21875 startpos, &junk);
21876
21877 topline = nlines + line;
21878
21879 /* Determine a new base line, if the old one is too close
21880 or too far away, or if we did not have one.
21881 "Too close" means it's plausible a scroll-down would
21882 go back past it. */
21883 if (startpos == BUF_BEGV (b))
21884 {
21885 w->base_line_number = topline;
21886 w->base_line_pos = BUF_BEGV (b);
21887 }
21888 else if (nlines < height + 25 || nlines > height * 3 + 50
21889 || linepos == BUF_BEGV (b))
21890 {
21891 ptrdiff_t limit = BUF_BEGV (b);
21892 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21893 ptrdiff_t position;
21894 ptrdiff_t distance =
21895 (height * 2 + 30) * line_number_display_limit_width;
21896
21897 if (startpos - distance > limit)
21898 {
21899 limit = startpos - distance;
21900 limit_byte = CHAR_TO_BYTE (limit);
21901 }
21902
21903 nlines = display_count_lines (startpos_byte,
21904 limit_byte,
21905 - (height * 2 + 30),
21906 &position);
21907 /* If we couldn't find the lines we wanted within
21908 line_number_display_limit_width chars per line,
21909 give up on line numbers for this window. */
21910 if (position == limit_byte && limit == startpos - distance)
21911 {
21912 w->base_line_pos = -1;
21913 w->base_line_number = 0;
21914 goto no_value;
21915 }
21916
21917 w->base_line_number = topline - nlines;
21918 w->base_line_pos = BYTE_TO_CHAR (position);
21919 }
21920
21921 /* Now count lines from the start pos to point. */
21922 nlines = display_count_lines (startpos_byte,
21923 PT_BYTE, PT, &junk);
21924
21925 /* Record that we did display the line number. */
21926 line_number_displayed = 1;
21927
21928 /* Make the string to show. */
21929 pint2str (decode_mode_spec_buf, width, topline + nlines);
21930 return decode_mode_spec_buf;
21931 no_value:
21932 {
21933 char* p = decode_mode_spec_buf;
21934 int pad = width - 2;
21935 while (pad-- > 0)
21936 *p++ = ' ';
21937 *p++ = '?';
21938 *p++ = '?';
21939 *p = '\0';
21940 return decode_mode_spec_buf;
21941 }
21942 }
21943 break;
21944
21945 case 'm':
21946 obj = BVAR (b, mode_name);
21947 break;
21948
21949 case 'n':
21950 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21951 return " Narrow";
21952 break;
21953
21954 case 'p':
21955 {
21956 ptrdiff_t pos = marker_position (w->start);
21957 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21958
21959 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
21960 {
21961 if (pos <= BUF_BEGV (b))
21962 return "All";
21963 else
21964 return "Bottom";
21965 }
21966 else if (pos <= BUF_BEGV (b))
21967 return "Top";
21968 else
21969 {
21970 if (total > 1000000)
21971 /* Do it differently for a large value, to avoid overflow. */
21972 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21973 else
21974 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21975 /* We can't normally display a 3-digit number,
21976 so get us a 2-digit number that is close. */
21977 if (total == 100)
21978 total = 99;
21979 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21980 return decode_mode_spec_buf;
21981 }
21982 }
21983
21984 /* Display percentage of size above the bottom of the screen. */
21985 case 'P':
21986 {
21987 ptrdiff_t toppos = marker_position (w->start);
21988 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
21989 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21990
21991 if (botpos >= BUF_ZV (b))
21992 {
21993 if (toppos <= BUF_BEGV (b))
21994 return "All";
21995 else
21996 return "Bottom";
21997 }
21998 else
21999 {
22000 if (total > 1000000)
22001 /* Do it differently for a large value, to avoid overflow. */
22002 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22003 else
22004 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22005 /* We can't normally display a 3-digit number,
22006 so get us a 2-digit number that is close. */
22007 if (total == 100)
22008 total = 99;
22009 if (toppos <= BUF_BEGV (b))
22010 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22011 else
22012 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22013 return decode_mode_spec_buf;
22014 }
22015 }
22016
22017 case 's':
22018 /* status of process */
22019 obj = Fget_buffer_process (Fcurrent_buffer ());
22020 if (NILP (obj))
22021 return "no process";
22022 #ifndef MSDOS
22023 obj = Fsymbol_name (Fprocess_status (obj));
22024 #endif
22025 break;
22026
22027 case '@':
22028 {
22029 ptrdiff_t count = inhibit_garbage_collection ();
22030 Lisp_Object val = call1 (intern ("file-remote-p"),
22031 BVAR (current_buffer, directory));
22032 unbind_to (count, Qnil);
22033
22034 if (NILP (val))
22035 return "-";
22036 else
22037 return "@";
22038 }
22039
22040 case 'z':
22041 /* coding-system (not including end-of-line format) */
22042 case 'Z':
22043 /* coding-system (including end-of-line type) */
22044 {
22045 int eol_flag = (c == 'Z');
22046 char *p = decode_mode_spec_buf;
22047
22048 if (! FRAME_WINDOW_P (f))
22049 {
22050 /* No need to mention EOL here--the terminal never needs
22051 to do EOL conversion. */
22052 p = decode_mode_spec_coding (CODING_ID_NAME
22053 (FRAME_KEYBOARD_CODING (f)->id),
22054 p, 0);
22055 p = decode_mode_spec_coding (CODING_ID_NAME
22056 (FRAME_TERMINAL_CODING (f)->id),
22057 p, 0);
22058 }
22059 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22060 p, eol_flag);
22061
22062 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22063 #ifdef subprocesses
22064 obj = Fget_buffer_process (Fcurrent_buffer ());
22065 if (PROCESSP (obj))
22066 {
22067 p = decode_mode_spec_coding
22068 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22069 p = decode_mode_spec_coding
22070 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22071 }
22072 #endif /* subprocesses */
22073 #endif /* 0 */
22074 *p = 0;
22075 return decode_mode_spec_buf;
22076 }
22077 }
22078
22079 if (STRINGP (obj))
22080 {
22081 *string = obj;
22082 return SSDATA (obj);
22083 }
22084 else
22085 return "";
22086 }
22087
22088
22089 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22090 means count lines back from START_BYTE. But don't go beyond
22091 LIMIT_BYTE. Return the number of lines thus found (always
22092 nonnegative).
22093
22094 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22095 either the position COUNT lines after/before START_BYTE, if we
22096 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22097 COUNT lines. */
22098
22099 static ptrdiff_t
22100 display_count_lines (ptrdiff_t start_byte,
22101 ptrdiff_t limit_byte, ptrdiff_t count,
22102 ptrdiff_t *byte_pos_ptr)
22103 {
22104 register unsigned char *cursor;
22105 unsigned char *base;
22106
22107 register ptrdiff_t ceiling;
22108 register unsigned char *ceiling_addr;
22109 ptrdiff_t orig_count = count;
22110
22111 /* If we are not in selective display mode,
22112 check only for newlines. */
22113 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22114 && !INTEGERP (BVAR (current_buffer, selective_display)));
22115
22116 if (count > 0)
22117 {
22118 while (start_byte < limit_byte)
22119 {
22120 ceiling = BUFFER_CEILING_OF (start_byte);
22121 ceiling = min (limit_byte - 1, ceiling);
22122 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22123 base = (cursor = BYTE_POS_ADDR (start_byte));
22124
22125 do
22126 {
22127 if (selective_display)
22128 {
22129 while (*cursor != '\n' && *cursor != 015
22130 && ++cursor != ceiling_addr)
22131 continue;
22132 if (cursor == ceiling_addr)
22133 break;
22134 }
22135 else
22136 {
22137 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22138 if (! cursor)
22139 break;
22140 }
22141
22142 cursor++;
22143
22144 if (--count == 0)
22145 {
22146 start_byte += cursor - base;
22147 *byte_pos_ptr = start_byte;
22148 return orig_count;
22149 }
22150 }
22151 while (cursor < ceiling_addr);
22152
22153 start_byte += ceiling_addr - base;
22154 }
22155 }
22156 else
22157 {
22158 while (start_byte > limit_byte)
22159 {
22160 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22161 ceiling = max (limit_byte, ceiling);
22162 ceiling_addr = BYTE_POS_ADDR (ceiling);
22163 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22164 while (1)
22165 {
22166 if (selective_display)
22167 {
22168 while (--cursor >= ceiling_addr
22169 && *cursor != '\n' && *cursor != 015)
22170 continue;
22171 if (cursor < ceiling_addr)
22172 break;
22173 }
22174 else
22175 {
22176 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22177 if (! cursor)
22178 break;
22179 }
22180
22181 if (++count == 0)
22182 {
22183 start_byte += cursor - base + 1;
22184 *byte_pos_ptr = start_byte;
22185 /* When scanning backwards, we should
22186 not count the newline posterior to which we stop. */
22187 return - orig_count - 1;
22188 }
22189 }
22190 start_byte += ceiling_addr - base;
22191 }
22192 }
22193
22194 *byte_pos_ptr = limit_byte;
22195
22196 if (count < 0)
22197 return - orig_count + count;
22198 return orig_count - count;
22199
22200 }
22201
22202
22203 \f
22204 /***********************************************************************
22205 Displaying strings
22206 ***********************************************************************/
22207
22208 /* Display a NUL-terminated string, starting with index START.
22209
22210 If STRING is non-null, display that C string. Otherwise, the Lisp
22211 string LISP_STRING is displayed. There's a case that STRING is
22212 non-null and LISP_STRING is not nil. It means STRING is a string
22213 data of LISP_STRING. In that case, we display LISP_STRING while
22214 ignoring its text properties.
22215
22216 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22217 FACE_STRING. Display STRING or LISP_STRING with the face at
22218 FACE_STRING_POS in FACE_STRING:
22219
22220 Display the string in the environment given by IT, but use the
22221 standard display table, temporarily.
22222
22223 FIELD_WIDTH is the minimum number of output glyphs to produce.
22224 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22225 with spaces. If STRING has more characters, more than FIELD_WIDTH
22226 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22227
22228 PRECISION is the maximum number of characters to output from
22229 STRING. PRECISION < 0 means don't truncate the string.
22230
22231 This is roughly equivalent to printf format specifiers:
22232
22233 FIELD_WIDTH PRECISION PRINTF
22234 ----------------------------------------
22235 -1 -1 %s
22236 -1 10 %.10s
22237 10 -1 %10s
22238 20 10 %20.10s
22239
22240 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22241 display them, and < 0 means obey the current buffer's value of
22242 enable_multibyte_characters.
22243
22244 Value is the number of columns displayed. */
22245
22246 static int
22247 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22248 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22249 int field_width, int precision, int max_x, int multibyte)
22250 {
22251 int hpos_at_start = it->hpos;
22252 int saved_face_id = it->face_id;
22253 struct glyph_row *row = it->glyph_row;
22254 ptrdiff_t it_charpos;
22255
22256 /* Initialize the iterator IT for iteration over STRING beginning
22257 with index START. */
22258 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22259 precision, field_width, multibyte);
22260 if (string && STRINGP (lisp_string))
22261 /* LISP_STRING is the one returned by decode_mode_spec. We should
22262 ignore its text properties. */
22263 it->stop_charpos = it->end_charpos;
22264
22265 /* If displaying STRING, set up the face of the iterator from
22266 FACE_STRING, if that's given. */
22267 if (STRINGP (face_string))
22268 {
22269 ptrdiff_t endptr;
22270 struct face *face;
22271
22272 it->face_id
22273 = face_at_string_position (it->w, face_string, face_string_pos,
22274 0, it->region_beg_charpos,
22275 it->region_end_charpos,
22276 &endptr, it->base_face_id, 0);
22277 face = FACE_FROM_ID (it->f, it->face_id);
22278 it->face_box_p = face->box != FACE_NO_BOX;
22279 }
22280
22281 /* Set max_x to the maximum allowed X position. Don't let it go
22282 beyond the right edge of the window. */
22283 if (max_x <= 0)
22284 max_x = it->last_visible_x;
22285 else
22286 max_x = min (max_x, it->last_visible_x);
22287
22288 /* Skip over display elements that are not visible. because IT->w is
22289 hscrolled. */
22290 if (it->current_x < it->first_visible_x)
22291 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22292 MOVE_TO_POS | MOVE_TO_X);
22293
22294 row->ascent = it->max_ascent;
22295 row->height = it->max_ascent + it->max_descent;
22296 row->phys_ascent = it->max_phys_ascent;
22297 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22298 row->extra_line_spacing = it->max_extra_line_spacing;
22299
22300 if (STRINGP (it->string))
22301 it_charpos = IT_STRING_CHARPOS (*it);
22302 else
22303 it_charpos = IT_CHARPOS (*it);
22304
22305 /* This condition is for the case that we are called with current_x
22306 past last_visible_x. */
22307 while (it->current_x < max_x)
22308 {
22309 int x_before, x, n_glyphs_before, i, nglyphs;
22310
22311 /* Get the next display element. */
22312 if (!get_next_display_element (it))
22313 break;
22314
22315 /* Produce glyphs. */
22316 x_before = it->current_x;
22317 n_glyphs_before = row->used[TEXT_AREA];
22318 PRODUCE_GLYPHS (it);
22319
22320 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22321 i = 0;
22322 x = x_before;
22323 while (i < nglyphs)
22324 {
22325 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22326
22327 if (it->line_wrap != TRUNCATE
22328 && x + glyph->pixel_width > max_x)
22329 {
22330 /* End of continued line or max_x reached. */
22331 if (CHAR_GLYPH_PADDING_P (*glyph))
22332 {
22333 /* A wide character is unbreakable. */
22334 if (row->reversed_p)
22335 unproduce_glyphs (it, row->used[TEXT_AREA]
22336 - n_glyphs_before);
22337 row->used[TEXT_AREA] = n_glyphs_before;
22338 it->current_x = x_before;
22339 }
22340 else
22341 {
22342 if (row->reversed_p)
22343 unproduce_glyphs (it, row->used[TEXT_AREA]
22344 - (n_glyphs_before + i));
22345 row->used[TEXT_AREA] = n_glyphs_before + i;
22346 it->current_x = x;
22347 }
22348 break;
22349 }
22350 else if (x + glyph->pixel_width >= it->first_visible_x)
22351 {
22352 /* Glyph is at least partially visible. */
22353 ++it->hpos;
22354 if (x < it->first_visible_x)
22355 row->x = x - it->first_visible_x;
22356 }
22357 else
22358 {
22359 /* Glyph is off the left margin of the display area.
22360 Should not happen. */
22361 emacs_abort ();
22362 }
22363
22364 row->ascent = max (row->ascent, it->max_ascent);
22365 row->height = max (row->height, it->max_ascent + it->max_descent);
22366 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22367 row->phys_height = max (row->phys_height,
22368 it->max_phys_ascent + it->max_phys_descent);
22369 row->extra_line_spacing = max (row->extra_line_spacing,
22370 it->max_extra_line_spacing);
22371 x += glyph->pixel_width;
22372 ++i;
22373 }
22374
22375 /* Stop if max_x reached. */
22376 if (i < nglyphs)
22377 break;
22378
22379 /* Stop at line ends. */
22380 if (ITERATOR_AT_END_OF_LINE_P (it))
22381 {
22382 it->continuation_lines_width = 0;
22383 break;
22384 }
22385
22386 set_iterator_to_next (it, 1);
22387 if (STRINGP (it->string))
22388 it_charpos = IT_STRING_CHARPOS (*it);
22389 else
22390 it_charpos = IT_CHARPOS (*it);
22391
22392 /* Stop if truncating at the right edge. */
22393 if (it->line_wrap == TRUNCATE
22394 && it->current_x >= it->last_visible_x)
22395 {
22396 /* Add truncation mark, but don't do it if the line is
22397 truncated at a padding space. */
22398 if (it_charpos < it->string_nchars)
22399 {
22400 if (!FRAME_WINDOW_P (it->f))
22401 {
22402 int ii, n;
22403
22404 if (it->current_x > it->last_visible_x)
22405 {
22406 if (!row->reversed_p)
22407 {
22408 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22409 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22410 break;
22411 }
22412 else
22413 {
22414 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22415 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22416 break;
22417 unproduce_glyphs (it, ii + 1);
22418 ii = row->used[TEXT_AREA] - (ii + 1);
22419 }
22420 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22421 {
22422 row->used[TEXT_AREA] = ii;
22423 produce_special_glyphs (it, IT_TRUNCATION);
22424 }
22425 }
22426 produce_special_glyphs (it, IT_TRUNCATION);
22427 }
22428 row->truncated_on_right_p = 1;
22429 }
22430 break;
22431 }
22432 }
22433
22434 /* Maybe insert a truncation at the left. */
22435 if (it->first_visible_x
22436 && it_charpos > 0)
22437 {
22438 if (!FRAME_WINDOW_P (it->f)
22439 || (row->reversed_p
22440 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22441 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22442 insert_left_trunc_glyphs (it);
22443 row->truncated_on_left_p = 1;
22444 }
22445
22446 it->face_id = saved_face_id;
22447
22448 /* Value is number of columns displayed. */
22449 return it->hpos - hpos_at_start;
22450 }
22451
22452
22453 \f
22454 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22455 appears as an element of LIST or as the car of an element of LIST.
22456 If PROPVAL is a list, compare each element against LIST in that
22457 way, and return 1/2 if any element of PROPVAL is found in LIST.
22458 Otherwise return 0. This function cannot quit.
22459 The return value is 2 if the text is invisible but with an ellipsis
22460 and 1 if it's invisible and without an ellipsis. */
22461
22462 int
22463 invisible_p (register Lisp_Object propval, Lisp_Object list)
22464 {
22465 register Lisp_Object tail, proptail;
22466
22467 for (tail = list; CONSP (tail); tail = XCDR (tail))
22468 {
22469 register Lisp_Object tem;
22470 tem = XCAR (tail);
22471 if (EQ (propval, tem))
22472 return 1;
22473 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22474 return NILP (XCDR (tem)) ? 1 : 2;
22475 }
22476
22477 if (CONSP (propval))
22478 {
22479 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22480 {
22481 Lisp_Object propelt;
22482 propelt = XCAR (proptail);
22483 for (tail = list; CONSP (tail); tail = XCDR (tail))
22484 {
22485 register Lisp_Object tem;
22486 tem = XCAR (tail);
22487 if (EQ (propelt, tem))
22488 return 1;
22489 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22490 return NILP (XCDR (tem)) ? 1 : 2;
22491 }
22492 }
22493 }
22494
22495 return 0;
22496 }
22497
22498 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22499 doc: /* Non-nil if the property makes the text invisible.
22500 POS-OR-PROP can be a marker or number, in which case it is taken to be
22501 a position in the current buffer and the value of the `invisible' property
22502 is checked; or it can be some other value, which is then presumed to be the
22503 value of the `invisible' property of the text of interest.
22504 The non-nil value returned can be t for truly invisible text or something
22505 else if the text is replaced by an ellipsis. */)
22506 (Lisp_Object pos_or_prop)
22507 {
22508 Lisp_Object prop
22509 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22510 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22511 : pos_or_prop);
22512 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22513 return (invis == 0 ? Qnil
22514 : invis == 1 ? Qt
22515 : make_number (invis));
22516 }
22517
22518 /* Calculate a width or height in pixels from a specification using
22519 the following elements:
22520
22521 SPEC ::=
22522 NUM - a (fractional) multiple of the default font width/height
22523 (NUM) - specifies exactly NUM pixels
22524 UNIT - a fixed number of pixels, see below.
22525 ELEMENT - size of a display element in pixels, see below.
22526 (NUM . SPEC) - equals NUM * SPEC
22527 (+ SPEC SPEC ...) - add pixel values
22528 (- SPEC SPEC ...) - subtract pixel values
22529 (- SPEC) - negate pixel value
22530
22531 NUM ::=
22532 INT or FLOAT - a number constant
22533 SYMBOL - use symbol's (buffer local) variable binding.
22534
22535 UNIT ::=
22536 in - pixels per inch *)
22537 mm - pixels per 1/1000 meter *)
22538 cm - pixels per 1/100 meter *)
22539 width - width of current font in pixels.
22540 height - height of current font in pixels.
22541
22542 *) using the ratio(s) defined in display-pixels-per-inch.
22543
22544 ELEMENT ::=
22545
22546 left-fringe - left fringe width in pixels
22547 right-fringe - right fringe width in pixels
22548
22549 left-margin - left margin width in pixels
22550 right-margin - right margin width in pixels
22551
22552 scroll-bar - scroll-bar area width in pixels
22553
22554 Examples:
22555
22556 Pixels corresponding to 5 inches:
22557 (5 . in)
22558
22559 Total width of non-text areas on left side of window (if scroll-bar is on left):
22560 '(space :width (+ left-fringe left-margin scroll-bar))
22561
22562 Align to first text column (in header line):
22563 '(space :align-to 0)
22564
22565 Align to middle of text area minus half the width of variable `my-image'
22566 containing a loaded image:
22567 '(space :align-to (0.5 . (- text my-image)))
22568
22569 Width of left margin minus width of 1 character in the default font:
22570 '(space :width (- left-margin 1))
22571
22572 Width of left margin minus width of 2 characters in the current font:
22573 '(space :width (- left-margin (2 . width)))
22574
22575 Center 1 character over left-margin (in header line):
22576 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22577
22578 Different ways to express width of left fringe plus left margin minus one pixel:
22579 '(space :width (- (+ left-fringe left-margin) (1)))
22580 '(space :width (+ left-fringe left-margin (- (1))))
22581 '(space :width (+ left-fringe left-margin (-1)))
22582
22583 */
22584
22585 static int
22586 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22587 struct font *font, int width_p, int *align_to)
22588 {
22589 double pixels;
22590
22591 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22592 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22593
22594 if (NILP (prop))
22595 return OK_PIXELS (0);
22596
22597 eassert (FRAME_LIVE_P (it->f));
22598
22599 if (SYMBOLP (prop))
22600 {
22601 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22602 {
22603 char *unit = SSDATA (SYMBOL_NAME (prop));
22604
22605 if (unit[0] == 'i' && unit[1] == 'n')
22606 pixels = 1.0;
22607 else if (unit[0] == 'm' && unit[1] == 'm')
22608 pixels = 25.4;
22609 else if (unit[0] == 'c' && unit[1] == 'm')
22610 pixels = 2.54;
22611 else
22612 pixels = 0;
22613 if (pixels > 0)
22614 {
22615 double ppi = (width_p ? FRAME_RES_X (it->f)
22616 : FRAME_RES_Y (it->f));
22617
22618 if (ppi > 0)
22619 return OK_PIXELS (ppi / pixels);
22620 return 0;
22621 }
22622 }
22623
22624 #ifdef HAVE_WINDOW_SYSTEM
22625 if (EQ (prop, Qheight))
22626 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22627 if (EQ (prop, Qwidth))
22628 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22629 #else
22630 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22631 return OK_PIXELS (1);
22632 #endif
22633
22634 if (EQ (prop, Qtext))
22635 return OK_PIXELS (width_p
22636 ? window_box_width (it->w, TEXT_AREA)
22637 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22638
22639 if (align_to && *align_to < 0)
22640 {
22641 *res = 0;
22642 if (EQ (prop, Qleft))
22643 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22644 if (EQ (prop, Qright))
22645 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22646 if (EQ (prop, Qcenter))
22647 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22648 + window_box_width (it->w, TEXT_AREA) / 2);
22649 if (EQ (prop, Qleft_fringe))
22650 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22651 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22652 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22653 if (EQ (prop, Qright_fringe))
22654 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22655 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22656 : window_box_right_offset (it->w, TEXT_AREA));
22657 if (EQ (prop, Qleft_margin))
22658 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22659 if (EQ (prop, Qright_margin))
22660 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22661 if (EQ (prop, Qscroll_bar))
22662 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22663 ? 0
22664 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22665 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22666 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22667 : 0)));
22668 }
22669 else
22670 {
22671 if (EQ (prop, Qleft_fringe))
22672 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22673 if (EQ (prop, Qright_fringe))
22674 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22675 if (EQ (prop, Qleft_margin))
22676 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22677 if (EQ (prop, Qright_margin))
22678 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22679 if (EQ (prop, Qscroll_bar))
22680 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22681 }
22682
22683 prop = buffer_local_value_1 (prop, it->w->contents);
22684 if (EQ (prop, Qunbound))
22685 prop = Qnil;
22686 }
22687
22688 if (INTEGERP (prop) || FLOATP (prop))
22689 {
22690 int base_unit = (width_p
22691 ? FRAME_COLUMN_WIDTH (it->f)
22692 : FRAME_LINE_HEIGHT (it->f));
22693 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22694 }
22695
22696 if (CONSP (prop))
22697 {
22698 Lisp_Object car = XCAR (prop);
22699 Lisp_Object cdr = XCDR (prop);
22700
22701 if (SYMBOLP (car))
22702 {
22703 #ifdef HAVE_WINDOW_SYSTEM
22704 if (FRAME_WINDOW_P (it->f)
22705 && valid_image_p (prop))
22706 {
22707 ptrdiff_t id = lookup_image (it->f, prop);
22708 struct image *img = IMAGE_FROM_ID (it->f, id);
22709
22710 return OK_PIXELS (width_p ? img->width : img->height);
22711 }
22712 #endif
22713 if (EQ (car, Qplus) || EQ (car, Qminus))
22714 {
22715 int first = 1;
22716 double px;
22717
22718 pixels = 0;
22719 while (CONSP (cdr))
22720 {
22721 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22722 font, width_p, align_to))
22723 return 0;
22724 if (first)
22725 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22726 else
22727 pixels += px;
22728 cdr = XCDR (cdr);
22729 }
22730 if (EQ (car, Qminus))
22731 pixels = -pixels;
22732 return OK_PIXELS (pixels);
22733 }
22734
22735 car = buffer_local_value_1 (car, it->w->contents);
22736 if (EQ (car, Qunbound))
22737 car = Qnil;
22738 }
22739
22740 if (INTEGERP (car) || FLOATP (car))
22741 {
22742 double fact;
22743 pixels = XFLOATINT (car);
22744 if (NILP (cdr))
22745 return OK_PIXELS (pixels);
22746 if (calc_pixel_width_or_height (&fact, it, cdr,
22747 font, width_p, align_to))
22748 return OK_PIXELS (pixels * fact);
22749 return 0;
22750 }
22751
22752 return 0;
22753 }
22754
22755 return 0;
22756 }
22757
22758 \f
22759 /***********************************************************************
22760 Glyph Display
22761 ***********************************************************************/
22762
22763 #ifdef HAVE_WINDOW_SYSTEM
22764
22765 #ifdef GLYPH_DEBUG
22766
22767 void
22768 dump_glyph_string (struct glyph_string *s)
22769 {
22770 fprintf (stderr, "glyph string\n");
22771 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22772 s->x, s->y, s->width, s->height);
22773 fprintf (stderr, " ybase = %d\n", s->ybase);
22774 fprintf (stderr, " hl = %d\n", s->hl);
22775 fprintf (stderr, " left overhang = %d, right = %d\n",
22776 s->left_overhang, s->right_overhang);
22777 fprintf (stderr, " nchars = %d\n", s->nchars);
22778 fprintf (stderr, " extends to end of line = %d\n",
22779 s->extends_to_end_of_line_p);
22780 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22781 fprintf (stderr, " bg width = %d\n", s->background_width);
22782 }
22783
22784 #endif /* GLYPH_DEBUG */
22785
22786 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22787 of XChar2b structures for S; it can't be allocated in
22788 init_glyph_string because it must be allocated via `alloca'. W
22789 is the window on which S is drawn. ROW and AREA are the glyph row
22790 and area within the row from which S is constructed. START is the
22791 index of the first glyph structure covered by S. HL is a
22792 face-override for drawing S. */
22793
22794 #ifdef HAVE_NTGUI
22795 #define OPTIONAL_HDC(hdc) HDC hdc,
22796 #define DECLARE_HDC(hdc) HDC hdc;
22797 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22798 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22799 #endif
22800
22801 #ifndef OPTIONAL_HDC
22802 #define OPTIONAL_HDC(hdc)
22803 #define DECLARE_HDC(hdc)
22804 #define ALLOCATE_HDC(hdc, f)
22805 #define RELEASE_HDC(hdc, f)
22806 #endif
22807
22808 static void
22809 init_glyph_string (struct glyph_string *s,
22810 OPTIONAL_HDC (hdc)
22811 XChar2b *char2b, struct window *w, struct glyph_row *row,
22812 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22813 {
22814 memset (s, 0, sizeof *s);
22815 s->w = w;
22816 s->f = XFRAME (w->frame);
22817 #ifdef HAVE_NTGUI
22818 s->hdc = hdc;
22819 #endif
22820 s->display = FRAME_X_DISPLAY (s->f);
22821 s->window = FRAME_X_WINDOW (s->f);
22822 s->char2b = char2b;
22823 s->hl = hl;
22824 s->row = row;
22825 s->area = area;
22826 s->first_glyph = row->glyphs[area] + start;
22827 s->height = row->height;
22828 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22829 s->ybase = s->y + row->ascent;
22830 }
22831
22832
22833 /* Append the list of glyph strings with head H and tail T to the list
22834 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22835
22836 static void
22837 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22838 struct glyph_string *h, struct glyph_string *t)
22839 {
22840 if (h)
22841 {
22842 if (*head)
22843 (*tail)->next = h;
22844 else
22845 *head = h;
22846 h->prev = *tail;
22847 *tail = t;
22848 }
22849 }
22850
22851
22852 /* Prepend the list of glyph strings with head H and tail T to the
22853 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22854 result. */
22855
22856 static void
22857 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22858 struct glyph_string *h, struct glyph_string *t)
22859 {
22860 if (h)
22861 {
22862 if (*head)
22863 (*head)->prev = t;
22864 else
22865 *tail = t;
22866 t->next = *head;
22867 *head = h;
22868 }
22869 }
22870
22871
22872 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22873 Set *HEAD and *TAIL to the resulting list. */
22874
22875 static void
22876 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22877 struct glyph_string *s)
22878 {
22879 s->next = s->prev = NULL;
22880 append_glyph_string_lists (head, tail, s, s);
22881 }
22882
22883
22884 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22885 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22886 make sure that X resources for the face returned are allocated.
22887 Value is a pointer to a realized face that is ready for display if
22888 DISPLAY_P is non-zero. */
22889
22890 static struct face *
22891 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22892 XChar2b *char2b, int display_p)
22893 {
22894 struct face *face = FACE_FROM_ID (f, face_id);
22895 unsigned code = 0;
22896
22897 if (face->font)
22898 {
22899 code = face->font->driver->encode_char (face->font, c);
22900
22901 if (code == FONT_INVALID_CODE)
22902 code = 0;
22903 }
22904 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22905
22906 /* Make sure X resources of the face are allocated. */
22907 #ifdef HAVE_X_WINDOWS
22908 if (display_p)
22909 #endif
22910 {
22911 eassert (face != NULL);
22912 PREPARE_FACE_FOR_DISPLAY (f, face);
22913 }
22914
22915 return face;
22916 }
22917
22918
22919 /* Get face and two-byte form of character glyph GLYPH on frame F.
22920 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22921 a pointer to a realized face that is ready for display. */
22922
22923 static struct face *
22924 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22925 XChar2b *char2b, int *two_byte_p)
22926 {
22927 struct face *face;
22928 unsigned code = 0;
22929
22930 eassert (glyph->type == CHAR_GLYPH);
22931 face = FACE_FROM_ID (f, glyph->face_id);
22932
22933 /* Make sure X resources of the face are allocated. */
22934 eassert (face != NULL);
22935 PREPARE_FACE_FOR_DISPLAY (f, face);
22936
22937 if (two_byte_p)
22938 *two_byte_p = 0;
22939
22940 if (face->font)
22941 {
22942 if (CHAR_BYTE8_P (glyph->u.ch))
22943 code = CHAR_TO_BYTE8 (glyph->u.ch);
22944 else
22945 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22946
22947 if (code == FONT_INVALID_CODE)
22948 code = 0;
22949 }
22950
22951 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22952 return face;
22953 }
22954
22955
22956 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22957 Return 1 if FONT has a glyph for C, otherwise return 0. */
22958
22959 static int
22960 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22961 {
22962 unsigned code;
22963
22964 if (CHAR_BYTE8_P (c))
22965 code = CHAR_TO_BYTE8 (c);
22966 else
22967 code = font->driver->encode_char (font, c);
22968
22969 if (code == FONT_INVALID_CODE)
22970 return 0;
22971 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22972 return 1;
22973 }
22974
22975
22976 /* Fill glyph string S with composition components specified by S->cmp.
22977
22978 BASE_FACE is the base face of the composition.
22979 S->cmp_from is the index of the first component for S.
22980
22981 OVERLAPS non-zero means S should draw the foreground only, and use
22982 its physical height for clipping. See also draw_glyphs.
22983
22984 Value is the index of a component not in S. */
22985
22986 static int
22987 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22988 int overlaps)
22989 {
22990 int i;
22991 /* For all glyphs of this composition, starting at the offset
22992 S->cmp_from, until we reach the end of the definition or encounter a
22993 glyph that requires the different face, add it to S. */
22994 struct face *face;
22995
22996 eassert (s);
22997
22998 s->for_overlaps = overlaps;
22999 s->face = NULL;
23000 s->font = NULL;
23001 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23002 {
23003 int c = COMPOSITION_GLYPH (s->cmp, i);
23004
23005 /* TAB in a composition means display glyphs with padding space
23006 on the left or right. */
23007 if (c != '\t')
23008 {
23009 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23010 -1, Qnil);
23011
23012 face = get_char_face_and_encoding (s->f, c, face_id,
23013 s->char2b + i, 1);
23014 if (face)
23015 {
23016 if (! s->face)
23017 {
23018 s->face = face;
23019 s->font = s->face->font;
23020 }
23021 else if (s->face != face)
23022 break;
23023 }
23024 }
23025 ++s->nchars;
23026 }
23027 s->cmp_to = i;
23028
23029 if (s->face == NULL)
23030 {
23031 s->face = base_face->ascii_face;
23032 s->font = s->face->font;
23033 }
23034
23035 /* All glyph strings for the same composition has the same width,
23036 i.e. the width set for the first component of the composition. */
23037 s->width = s->first_glyph->pixel_width;
23038
23039 /* If the specified font could not be loaded, use the frame's
23040 default font, but record the fact that we couldn't load it in
23041 the glyph string so that we can draw rectangles for the
23042 characters of the glyph string. */
23043 if (s->font == NULL)
23044 {
23045 s->font_not_found_p = 1;
23046 s->font = FRAME_FONT (s->f);
23047 }
23048
23049 /* Adjust base line for subscript/superscript text. */
23050 s->ybase += s->first_glyph->voffset;
23051
23052 /* This glyph string must always be drawn with 16-bit functions. */
23053 s->two_byte_p = 1;
23054
23055 return s->cmp_to;
23056 }
23057
23058 static int
23059 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23060 int start, int end, int overlaps)
23061 {
23062 struct glyph *glyph, *last;
23063 Lisp_Object lgstring;
23064 int i;
23065
23066 s->for_overlaps = overlaps;
23067 glyph = s->row->glyphs[s->area] + start;
23068 last = s->row->glyphs[s->area] + end;
23069 s->cmp_id = glyph->u.cmp.id;
23070 s->cmp_from = glyph->slice.cmp.from;
23071 s->cmp_to = glyph->slice.cmp.to + 1;
23072 s->face = FACE_FROM_ID (s->f, face_id);
23073 lgstring = composition_gstring_from_id (s->cmp_id);
23074 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23075 glyph++;
23076 while (glyph < last
23077 && glyph->u.cmp.automatic
23078 && glyph->u.cmp.id == s->cmp_id
23079 && s->cmp_to == glyph->slice.cmp.from)
23080 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23081
23082 for (i = s->cmp_from; i < s->cmp_to; i++)
23083 {
23084 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23085 unsigned code = LGLYPH_CODE (lglyph);
23086
23087 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23088 }
23089 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23090 return glyph - s->row->glyphs[s->area];
23091 }
23092
23093
23094 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23095 See the comment of fill_glyph_string for arguments.
23096 Value is the index of the first glyph not in S. */
23097
23098
23099 static int
23100 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23101 int start, int end, int overlaps)
23102 {
23103 struct glyph *glyph, *last;
23104 int voffset;
23105
23106 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23107 s->for_overlaps = overlaps;
23108 glyph = s->row->glyphs[s->area] + start;
23109 last = s->row->glyphs[s->area] + end;
23110 voffset = glyph->voffset;
23111 s->face = FACE_FROM_ID (s->f, face_id);
23112 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23113 s->nchars = 1;
23114 s->width = glyph->pixel_width;
23115 glyph++;
23116 while (glyph < last
23117 && glyph->type == GLYPHLESS_GLYPH
23118 && glyph->voffset == voffset
23119 && glyph->face_id == face_id)
23120 {
23121 s->nchars++;
23122 s->width += glyph->pixel_width;
23123 glyph++;
23124 }
23125 s->ybase += voffset;
23126 return glyph - s->row->glyphs[s->area];
23127 }
23128
23129
23130 /* Fill glyph string S from a sequence of character glyphs.
23131
23132 FACE_ID is the face id of the string. START is the index of the
23133 first glyph to consider, END is the index of the last + 1.
23134 OVERLAPS non-zero means S should draw the foreground only, and use
23135 its physical height for clipping. See also draw_glyphs.
23136
23137 Value is the index of the first glyph not in S. */
23138
23139 static int
23140 fill_glyph_string (struct glyph_string *s, int face_id,
23141 int start, int end, int overlaps)
23142 {
23143 struct glyph *glyph, *last;
23144 int voffset;
23145 int glyph_not_available_p;
23146
23147 eassert (s->f == XFRAME (s->w->frame));
23148 eassert (s->nchars == 0);
23149 eassert (start >= 0 && end > start);
23150
23151 s->for_overlaps = overlaps;
23152 glyph = s->row->glyphs[s->area] + start;
23153 last = s->row->glyphs[s->area] + end;
23154 voffset = glyph->voffset;
23155 s->padding_p = glyph->padding_p;
23156 glyph_not_available_p = glyph->glyph_not_available_p;
23157
23158 while (glyph < last
23159 && glyph->type == CHAR_GLYPH
23160 && glyph->voffset == voffset
23161 /* Same face id implies same font, nowadays. */
23162 && glyph->face_id == face_id
23163 && glyph->glyph_not_available_p == glyph_not_available_p)
23164 {
23165 int two_byte_p;
23166
23167 s->face = get_glyph_face_and_encoding (s->f, glyph,
23168 s->char2b + s->nchars,
23169 &two_byte_p);
23170 s->two_byte_p = two_byte_p;
23171 ++s->nchars;
23172 eassert (s->nchars <= end - start);
23173 s->width += glyph->pixel_width;
23174 if (glyph++->padding_p != s->padding_p)
23175 break;
23176 }
23177
23178 s->font = s->face->font;
23179
23180 /* If the specified font could not be loaded, use the frame's font,
23181 but record the fact that we couldn't load it in
23182 S->font_not_found_p so that we can draw rectangles for the
23183 characters of the glyph string. */
23184 if (s->font == NULL || glyph_not_available_p)
23185 {
23186 s->font_not_found_p = 1;
23187 s->font = FRAME_FONT (s->f);
23188 }
23189
23190 /* Adjust base line for subscript/superscript text. */
23191 s->ybase += voffset;
23192
23193 eassert (s->face && s->face->gc);
23194 return glyph - s->row->glyphs[s->area];
23195 }
23196
23197
23198 /* Fill glyph string S from image glyph S->first_glyph. */
23199
23200 static void
23201 fill_image_glyph_string (struct glyph_string *s)
23202 {
23203 eassert (s->first_glyph->type == IMAGE_GLYPH);
23204 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23205 eassert (s->img);
23206 s->slice = s->first_glyph->slice.img;
23207 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23208 s->font = s->face->font;
23209 s->width = s->first_glyph->pixel_width;
23210
23211 /* Adjust base line for subscript/superscript text. */
23212 s->ybase += s->first_glyph->voffset;
23213 }
23214
23215
23216 /* Fill glyph string S from a sequence of stretch glyphs.
23217
23218 START is the index of the first glyph to consider,
23219 END is the index of the last + 1.
23220
23221 Value is the index of the first glyph not in S. */
23222
23223 static int
23224 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23225 {
23226 struct glyph *glyph, *last;
23227 int voffset, face_id;
23228
23229 eassert (s->first_glyph->type == STRETCH_GLYPH);
23230
23231 glyph = s->row->glyphs[s->area] + start;
23232 last = s->row->glyphs[s->area] + end;
23233 face_id = glyph->face_id;
23234 s->face = FACE_FROM_ID (s->f, face_id);
23235 s->font = s->face->font;
23236 s->width = glyph->pixel_width;
23237 s->nchars = 1;
23238 voffset = glyph->voffset;
23239
23240 for (++glyph;
23241 (glyph < last
23242 && glyph->type == STRETCH_GLYPH
23243 && glyph->voffset == voffset
23244 && glyph->face_id == face_id);
23245 ++glyph)
23246 s->width += glyph->pixel_width;
23247
23248 /* Adjust base line for subscript/superscript text. */
23249 s->ybase += voffset;
23250
23251 /* The case that face->gc == 0 is handled when drawing the glyph
23252 string by calling PREPARE_FACE_FOR_DISPLAY. */
23253 eassert (s->face);
23254 return glyph - s->row->glyphs[s->area];
23255 }
23256
23257 static struct font_metrics *
23258 get_per_char_metric (struct font *font, XChar2b *char2b)
23259 {
23260 static struct font_metrics metrics;
23261 unsigned code;
23262
23263 if (! font)
23264 return NULL;
23265 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23266 if (code == FONT_INVALID_CODE)
23267 return NULL;
23268 font->driver->text_extents (font, &code, 1, &metrics);
23269 return &metrics;
23270 }
23271
23272 /* EXPORT for RIF:
23273 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23274 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23275 assumed to be zero. */
23276
23277 void
23278 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23279 {
23280 *left = *right = 0;
23281
23282 if (glyph->type == CHAR_GLYPH)
23283 {
23284 struct face *face;
23285 XChar2b char2b;
23286 struct font_metrics *pcm;
23287
23288 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23289 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23290 {
23291 if (pcm->rbearing > pcm->width)
23292 *right = pcm->rbearing - pcm->width;
23293 if (pcm->lbearing < 0)
23294 *left = -pcm->lbearing;
23295 }
23296 }
23297 else if (glyph->type == COMPOSITE_GLYPH)
23298 {
23299 if (! glyph->u.cmp.automatic)
23300 {
23301 struct composition *cmp = composition_table[glyph->u.cmp.id];
23302
23303 if (cmp->rbearing > cmp->pixel_width)
23304 *right = cmp->rbearing - cmp->pixel_width;
23305 if (cmp->lbearing < 0)
23306 *left = - cmp->lbearing;
23307 }
23308 else
23309 {
23310 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23311 struct font_metrics metrics;
23312
23313 composition_gstring_width (gstring, glyph->slice.cmp.from,
23314 glyph->slice.cmp.to + 1, &metrics);
23315 if (metrics.rbearing > metrics.width)
23316 *right = metrics.rbearing - metrics.width;
23317 if (metrics.lbearing < 0)
23318 *left = - metrics.lbearing;
23319 }
23320 }
23321 }
23322
23323
23324 /* Return the index of the first glyph preceding glyph string S that
23325 is overwritten by S because of S's left overhang. Value is -1
23326 if no glyphs are overwritten. */
23327
23328 static int
23329 left_overwritten (struct glyph_string *s)
23330 {
23331 int k;
23332
23333 if (s->left_overhang)
23334 {
23335 int x = 0, i;
23336 struct glyph *glyphs = s->row->glyphs[s->area];
23337 int first = s->first_glyph - glyphs;
23338
23339 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23340 x -= glyphs[i].pixel_width;
23341
23342 k = i + 1;
23343 }
23344 else
23345 k = -1;
23346
23347 return k;
23348 }
23349
23350
23351 /* Return the index of the first glyph preceding glyph string S that
23352 is overwriting S because of its right overhang. Value is -1 if no
23353 glyph in front of S overwrites S. */
23354
23355 static int
23356 left_overwriting (struct glyph_string *s)
23357 {
23358 int i, k, x;
23359 struct glyph *glyphs = s->row->glyphs[s->area];
23360 int first = s->first_glyph - glyphs;
23361
23362 k = -1;
23363 x = 0;
23364 for (i = first - 1; i >= 0; --i)
23365 {
23366 int left, right;
23367 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23368 if (x + right > 0)
23369 k = i;
23370 x -= glyphs[i].pixel_width;
23371 }
23372
23373 return k;
23374 }
23375
23376
23377 /* Return the index of the last glyph following glyph string S that is
23378 overwritten by S because of S's right overhang. Value is -1 if
23379 no such glyph is found. */
23380
23381 static int
23382 right_overwritten (struct glyph_string *s)
23383 {
23384 int k = -1;
23385
23386 if (s->right_overhang)
23387 {
23388 int x = 0, i;
23389 struct glyph *glyphs = s->row->glyphs[s->area];
23390 int first = (s->first_glyph - glyphs
23391 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23392 int end = s->row->used[s->area];
23393
23394 for (i = first; i < end && s->right_overhang > x; ++i)
23395 x += glyphs[i].pixel_width;
23396
23397 k = i;
23398 }
23399
23400 return k;
23401 }
23402
23403
23404 /* Return the index of the last glyph following glyph string S that
23405 overwrites S because of its left overhang. Value is negative
23406 if no such glyph is found. */
23407
23408 static int
23409 right_overwriting (struct glyph_string *s)
23410 {
23411 int i, k, x;
23412 int end = s->row->used[s->area];
23413 struct glyph *glyphs = s->row->glyphs[s->area];
23414 int first = (s->first_glyph - glyphs
23415 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23416
23417 k = -1;
23418 x = 0;
23419 for (i = first; i < end; ++i)
23420 {
23421 int left, right;
23422 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23423 if (x - left < 0)
23424 k = i;
23425 x += glyphs[i].pixel_width;
23426 }
23427
23428 return k;
23429 }
23430
23431
23432 /* Set background width of glyph string S. START is the index of the
23433 first glyph following S. LAST_X is the right-most x-position + 1
23434 in the drawing area. */
23435
23436 static void
23437 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23438 {
23439 /* If the face of this glyph string has to be drawn to the end of
23440 the drawing area, set S->extends_to_end_of_line_p. */
23441
23442 if (start == s->row->used[s->area]
23443 && s->area == TEXT_AREA
23444 && ((s->row->fill_line_p
23445 && (s->hl == DRAW_NORMAL_TEXT
23446 || s->hl == DRAW_IMAGE_RAISED
23447 || s->hl == DRAW_IMAGE_SUNKEN))
23448 || s->hl == DRAW_MOUSE_FACE))
23449 s->extends_to_end_of_line_p = 1;
23450
23451 /* If S extends its face to the end of the line, set its
23452 background_width to the distance to the right edge of the drawing
23453 area. */
23454 if (s->extends_to_end_of_line_p)
23455 s->background_width = last_x - s->x + 1;
23456 else
23457 s->background_width = s->width;
23458 }
23459
23460
23461 /* Compute overhangs and x-positions for glyph string S and its
23462 predecessors, or successors. X is the starting x-position for S.
23463 BACKWARD_P non-zero means process predecessors. */
23464
23465 static void
23466 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23467 {
23468 if (backward_p)
23469 {
23470 while (s)
23471 {
23472 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23473 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23474 x -= s->width;
23475 s->x = x;
23476 s = s->prev;
23477 }
23478 }
23479 else
23480 {
23481 while (s)
23482 {
23483 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23484 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23485 s->x = x;
23486 x += s->width;
23487 s = s->next;
23488 }
23489 }
23490 }
23491
23492
23493
23494 /* The following macros are only called from draw_glyphs below.
23495 They reference the following parameters of that function directly:
23496 `w', `row', `area', and `overlap_p'
23497 as well as the following local variables:
23498 `s', `f', and `hdc' (in W32) */
23499
23500 #ifdef HAVE_NTGUI
23501 /* On W32, silently add local `hdc' variable to argument list of
23502 init_glyph_string. */
23503 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23504 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23505 #else
23506 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23507 init_glyph_string (s, char2b, w, row, area, start, hl)
23508 #endif
23509
23510 /* Add a glyph string for a stretch glyph to the list of strings
23511 between HEAD and TAIL. START is the index of the stretch glyph in
23512 row area AREA of glyph row ROW. END is the index of the last glyph
23513 in that glyph row area. X is the current output position assigned
23514 to the new glyph string constructed. HL overrides that face of the
23515 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23516 is the right-most x-position of the drawing area. */
23517
23518 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23519 and below -- keep them on one line. */
23520 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23521 do \
23522 { \
23523 s = alloca (sizeof *s); \
23524 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23525 START = fill_stretch_glyph_string (s, START, END); \
23526 append_glyph_string (&HEAD, &TAIL, s); \
23527 s->x = (X); \
23528 } \
23529 while (0)
23530
23531
23532 /* Add a glyph string for an image glyph to the list of strings
23533 between HEAD and TAIL. START is the index of the image glyph in
23534 row area AREA of glyph row ROW. END is the index of the last glyph
23535 in that glyph row area. X is the current output position assigned
23536 to the new glyph string constructed. HL overrides that face of the
23537 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23538 is the right-most x-position of the drawing area. */
23539
23540 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23541 do \
23542 { \
23543 s = alloca (sizeof *s); \
23544 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23545 fill_image_glyph_string (s); \
23546 append_glyph_string (&HEAD, &TAIL, s); \
23547 ++START; \
23548 s->x = (X); \
23549 } \
23550 while (0)
23551
23552
23553 /* Add a glyph string for a sequence of character glyphs to the list
23554 of strings between HEAD and TAIL. START is the index of the first
23555 glyph in row area AREA of glyph row ROW that is part of the new
23556 glyph string. END is the index of the last glyph in that glyph row
23557 area. X is the current output position assigned to the new glyph
23558 string constructed. HL overrides that face of the glyph; e.g. it
23559 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23560 right-most x-position of the drawing area. */
23561
23562 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23563 do \
23564 { \
23565 int face_id; \
23566 XChar2b *char2b; \
23567 \
23568 face_id = (row)->glyphs[area][START].face_id; \
23569 \
23570 s = alloca (sizeof *s); \
23571 char2b = alloca ((END - START) * sizeof *char2b); \
23572 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23573 append_glyph_string (&HEAD, &TAIL, s); \
23574 s->x = (X); \
23575 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23576 } \
23577 while (0)
23578
23579
23580 /* Add a glyph string for a composite sequence to the list of strings
23581 between HEAD and TAIL. START is the index of the first glyph in
23582 row area AREA of glyph row ROW that is part of the new glyph
23583 string. END is the index of the last glyph in that glyph row area.
23584 X is the current output position assigned to the new glyph string
23585 constructed. HL overrides that face of the glyph; e.g. it is
23586 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23587 x-position of the drawing area. */
23588
23589 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23590 do { \
23591 int face_id = (row)->glyphs[area][START].face_id; \
23592 struct face *base_face = FACE_FROM_ID (f, face_id); \
23593 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23594 struct composition *cmp = composition_table[cmp_id]; \
23595 XChar2b *char2b; \
23596 struct glyph_string *first_s = NULL; \
23597 int n; \
23598 \
23599 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23600 \
23601 /* Make glyph_strings for each glyph sequence that is drawable by \
23602 the same face, and append them to HEAD/TAIL. */ \
23603 for (n = 0; n < cmp->glyph_len;) \
23604 { \
23605 s = alloca (sizeof *s); \
23606 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23607 append_glyph_string (&(HEAD), &(TAIL), s); \
23608 s->cmp = cmp; \
23609 s->cmp_from = n; \
23610 s->x = (X); \
23611 if (n == 0) \
23612 first_s = s; \
23613 n = fill_composite_glyph_string (s, base_face, overlaps); \
23614 } \
23615 \
23616 ++START; \
23617 s = first_s; \
23618 } while (0)
23619
23620
23621 /* Add a glyph string for a glyph-string sequence to the list of strings
23622 between HEAD and TAIL. */
23623
23624 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23625 do { \
23626 int face_id; \
23627 XChar2b *char2b; \
23628 Lisp_Object gstring; \
23629 \
23630 face_id = (row)->glyphs[area][START].face_id; \
23631 gstring = (composition_gstring_from_id \
23632 ((row)->glyphs[area][START].u.cmp.id)); \
23633 s = alloca (sizeof *s); \
23634 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23635 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23636 append_glyph_string (&(HEAD), &(TAIL), s); \
23637 s->x = (X); \
23638 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23639 } while (0)
23640
23641
23642 /* Add a glyph string for a sequence of glyphless character's glyphs
23643 to the list of strings between HEAD and TAIL. The meanings of
23644 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23645
23646 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23647 do \
23648 { \
23649 int face_id; \
23650 \
23651 face_id = (row)->glyphs[area][START].face_id; \
23652 \
23653 s = alloca (sizeof *s); \
23654 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23655 append_glyph_string (&HEAD, &TAIL, s); \
23656 s->x = (X); \
23657 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23658 overlaps); \
23659 } \
23660 while (0)
23661
23662
23663 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23664 of AREA of glyph row ROW on window W between indices START and END.
23665 HL overrides the face for drawing glyph strings, e.g. it is
23666 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23667 x-positions of the drawing area.
23668
23669 This is an ugly monster macro construct because we must use alloca
23670 to allocate glyph strings (because draw_glyphs can be called
23671 asynchronously). */
23672
23673 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23674 do \
23675 { \
23676 HEAD = TAIL = NULL; \
23677 while (START < END) \
23678 { \
23679 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23680 switch (first_glyph->type) \
23681 { \
23682 case CHAR_GLYPH: \
23683 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23684 HL, X, LAST_X); \
23685 break; \
23686 \
23687 case COMPOSITE_GLYPH: \
23688 if (first_glyph->u.cmp.automatic) \
23689 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23690 HL, X, LAST_X); \
23691 else \
23692 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23693 HL, X, LAST_X); \
23694 break; \
23695 \
23696 case STRETCH_GLYPH: \
23697 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23698 HL, X, LAST_X); \
23699 break; \
23700 \
23701 case IMAGE_GLYPH: \
23702 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23703 HL, X, LAST_X); \
23704 break; \
23705 \
23706 case GLYPHLESS_GLYPH: \
23707 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23708 HL, X, LAST_X); \
23709 break; \
23710 \
23711 default: \
23712 emacs_abort (); \
23713 } \
23714 \
23715 if (s) \
23716 { \
23717 set_glyph_string_background_width (s, START, LAST_X); \
23718 (X) += s->width; \
23719 } \
23720 } \
23721 } while (0)
23722
23723
23724 /* Draw glyphs between START and END in AREA of ROW on window W,
23725 starting at x-position X. X is relative to AREA in W. HL is a
23726 face-override with the following meaning:
23727
23728 DRAW_NORMAL_TEXT draw normally
23729 DRAW_CURSOR draw in cursor face
23730 DRAW_MOUSE_FACE draw in mouse face.
23731 DRAW_INVERSE_VIDEO draw in mode line face
23732 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23733 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23734
23735 If OVERLAPS is non-zero, draw only the foreground of characters and
23736 clip to the physical height of ROW. Non-zero value also defines
23737 the overlapping part to be drawn:
23738
23739 OVERLAPS_PRED overlap with preceding rows
23740 OVERLAPS_SUCC overlap with succeeding rows
23741 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23742 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23743
23744 Value is the x-position reached, relative to AREA of W. */
23745
23746 static int
23747 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23748 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23749 enum draw_glyphs_face hl, int overlaps)
23750 {
23751 struct glyph_string *head, *tail;
23752 struct glyph_string *s;
23753 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23754 int i, j, x_reached, last_x, area_left = 0;
23755 struct frame *f = XFRAME (WINDOW_FRAME (w));
23756 DECLARE_HDC (hdc);
23757
23758 ALLOCATE_HDC (hdc, f);
23759
23760 /* Let's rather be paranoid than getting a SEGV. */
23761 end = min (end, row->used[area]);
23762 start = clip_to_bounds (0, start, end);
23763
23764 /* Translate X to frame coordinates. Set last_x to the right
23765 end of the drawing area. */
23766 if (row->full_width_p)
23767 {
23768 /* X is relative to the left edge of W, without scroll bars
23769 or fringes. */
23770 area_left = WINDOW_LEFT_EDGE_X (w);
23771 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23772 }
23773 else
23774 {
23775 area_left = window_box_left (w, area);
23776 last_x = area_left + window_box_width (w, area);
23777 }
23778 x += area_left;
23779
23780 /* Build a doubly-linked list of glyph_string structures between
23781 head and tail from what we have to draw. Note that the macro
23782 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23783 the reason we use a separate variable `i'. */
23784 i = start;
23785 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23786 if (tail)
23787 x_reached = tail->x + tail->background_width;
23788 else
23789 x_reached = x;
23790
23791 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23792 the row, redraw some glyphs in front or following the glyph
23793 strings built above. */
23794 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23795 {
23796 struct glyph_string *h, *t;
23797 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23798 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23799 int check_mouse_face = 0;
23800 int dummy_x = 0;
23801
23802 /* If mouse highlighting is on, we may need to draw adjacent
23803 glyphs using mouse-face highlighting. */
23804 if (area == TEXT_AREA && row->mouse_face_p
23805 && hlinfo->mouse_face_beg_row >= 0
23806 && hlinfo->mouse_face_end_row >= 0)
23807 {
23808 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23809
23810 if (row_vpos >= hlinfo->mouse_face_beg_row
23811 && row_vpos <= hlinfo->mouse_face_end_row)
23812 {
23813 check_mouse_face = 1;
23814 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23815 ? hlinfo->mouse_face_beg_col : 0;
23816 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23817 ? hlinfo->mouse_face_end_col
23818 : row->used[TEXT_AREA];
23819 }
23820 }
23821
23822 /* Compute overhangs for all glyph strings. */
23823 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23824 for (s = head; s; s = s->next)
23825 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23826
23827 /* Prepend glyph strings for glyphs in front of the first glyph
23828 string that are overwritten because of the first glyph
23829 string's left overhang. The background of all strings
23830 prepended must be drawn because the first glyph string
23831 draws over it. */
23832 i = left_overwritten (head);
23833 if (i >= 0)
23834 {
23835 enum draw_glyphs_face overlap_hl;
23836
23837 /* If this row contains mouse highlighting, attempt to draw
23838 the overlapped glyphs with the correct highlight. This
23839 code fails if the overlap encompasses more than one glyph
23840 and mouse-highlight spans only some of these glyphs.
23841 However, making it work perfectly involves a lot more
23842 code, and I don't know if the pathological case occurs in
23843 practice, so we'll stick to this for now. --- cyd */
23844 if (check_mouse_face
23845 && mouse_beg_col < start && mouse_end_col > i)
23846 overlap_hl = DRAW_MOUSE_FACE;
23847 else
23848 overlap_hl = DRAW_NORMAL_TEXT;
23849
23850 j = i;
23851 BUILD_GLYPH_STRINGS (j, start, h, t,
23852 overlap_hl, dummy_x, last_x);
23853 start = i;
23854 compute_overhangs_and_x (t, head->x, 1);
23855 prepend_glyph_string_lists (&head, &tail, h, t);
23856 clip_head = head;
23857 }
23858
23859 /* Prepend glyph strings for glyphs in front of the first glyph
23860 string that overwrite that glyph string because of their
23861 right overhang. For these strings, only the foreground must
23862 be drawn, because it draws over the glyph string at `head'.
23863 The background must not be drawn because this would overwrite
23864 right overhangs of preceding glyphs for which no glyph
23865 strings exist. */
23866 i = left_overwriting (head);
23867 if (i >= 0)
23868 {
23869 enum draw_glyphs_face overlap_hl;
23870
23871 if (check_mouse_face
23872 && mouse_beg_col < start && mouse_end_col > i)
23873 overlap_hl = DRAW_MOUSE_FACE;
23874 else
23875 overlap_hl = DRAW_NORMAL_TEXT;
23876
23877 clip_head = head;
23878 BUILD_GLYPH_STRINGS (i, start, h, t,
23879 overlap_hl, dummy_x, last_x);
23880 for (s = h; s; s = s->next)
23881 s->background_filled_p = 1;
23882 compute_overhangs_and_x (t, head->x, 1);
23883 prepend_glyph_string_lists (&head, &tail, h, t);
23884 }
23885
23886 /* Append glyphs strings for glyphs following the last glyph
23887 string tail that are overwritten by tail. The background of
23888 these strings has to be drawn because tail's foreground draws
23889 over it. */
23890 i = right_overwritten (tail);
23891 if (i >= 0)
23892 {
23893 enum draw_glyphs_face overlap_hl;
23894
23895 if (check_mouse_face
23896 && mouse_beg_col < i && mouse_end_col > end)
23897 overlap_hl = DRAW_MOUSE_FACE;
23898 else
23899 overlap_hl = DRAW_NORMAL_TEXT;
23900
23901 BUILD_GLYPH_STRINGS (end, i, h, t,
23902 overlap_hl, x, last_x);
23903 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23904 we don't have `end = i;' here. */
23905 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23906 append_glyph_string_lists (&head, &tail, h, t);
23907 clip_tail = tail;
23908 }
23909
23910 /* Append glyph strings for glyphs following the last glyph
23911 string tail that overwrite tail. The foreground of such
23912 glyphs has to be drawn because it writes into the background
23913 of tail. The background must not be drawn because it could
23914 paint over the foreground of following glyphs. */
23915 i = right_overwriting (tail);
23916 if (i >= 0)
23917 {
23918 enum draw_glyphs_face overlap_hl;
23919 if (check_mouse_face
23920 && mouse_beg_col < i && mouse_end_col > end)
23921 overlap_hl = DRAW_MOUSE_FACE;
23922 else
23923 overlap_hl = DRAW_NORMAL_TEXT;
23924
23925 clip_tail = tail;
23926 i++; /* We must include the Ith glyph. */
23927 BUILD_GLYPH_STRINGS (end, i, h, t,
23928 overlap_hl, x, last_x);
23929 for (s = h; s; s = s->next)
23930 s->background_filled_p = 1;
23931 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23932 append_glyph_string_lists (&head, &tail, h, t);
23933 }
23934 if (clip_head || clip_tail)
23935 for (s = head; s; s = s->next)
23936 {
23937 s->clip_head = clip_head;
23938 s->clip_tail = clip_tail;
23939 }
23940 }
23941
23942 /* Draw all strings. */
23943 for (s = head; s; s = s->next)
23944 FRAME_RIF (f)->draw_glyph_string (s);
23945
23946 #ifndef HAVE_NS
23947 /* When focus a sole frame and move horizontally, this sets on_p to 0
23948 causing a failure to erase prev cursor position. */
23949 if (area == TEXT_AREA
23950 && !row->full_width_p
23951 /* When drawing overlapping rows, only the glyph strings'
23952 foreground is drawn, which doesn't erase a cursor
23953 completely. */
23954 && !overlaps)
23955 {
23956 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23957 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23958 : (tail ? tail->x + tail->background_width : x));
23959 x0 -= area_left;
23960 x1 -= area_left;
23961
23962 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23963 row->y, MATRIX_ROW_BOTTOM_Y (row));
23964 }
23965 #endif
23966
23967 /* Value is the x-position up to which drawn, relative to AREA of W.
23968 This doesn't include parts drawn because of overhangs. */
23969 if (row->full_width_p)
23970 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23971 else
23972 x_reached -= area_left;
23973
23974 RELEASE_HDC (hdc, f);
23975
23976 return x_reached;
23977 }
23978
23979 /* Expand row matrix if too narrow. Don't expand if area
23980 is not present. */
23981
23982 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23983 { \
23984 if (!it->f->fonts_changed \
23985 && (it->glyph_row->glyphs[area] \
23986 < it->glyph_row->glyphs[area + 1])) \
23987 { \
23988 it->w->ncols_scale_factor++; \
23989 it->f->fonts_changed = 1; \
23990 } \
23991 }
23992
23993 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23994 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23995
23996 static void
23997 append_glyph (struct it *it)
23998 {
23999 struct glyph *glyph;
24000 enum glyph_row_area area = it->area;
24001
24002 eassert (it->glyph_row);
24003 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24004
24005 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24006 if (glyph < it->glyph_row->glyphs[area + 1])
24007 {
24008 /* If the glyph row is reversed, we need to prepend the glyph
24009 rather than append it. */
24010 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24011 {
24012 struct glyph *g;
24013
24014 /* Make room for the additional glyph. */
24015 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24016 g[1] = *g;
24017 glyph = it->glyph_row->glyphs[area];
24018 }
24019 glyph->charpos = CHARPOS (it->position);
24020 glyph->object = it->object;
24021 if (it->pixel_width > 0)
24022 {
24023 glyph->pixel_width = it->pixel_width;
24024 glyph->padding_p = 0;
24025 }
24026 else
24027 {
24028 /* Assure at least 1-pixel width. Otherwise, cursor can't
24029 be displayed correctly. */
24030 glyph->pixel_width = 1;
24031 glyph->padding_p = 1;
24032 }
24033 glyph->ascent = it->ascent;
24034 glyph->descent = it->descent;
24035 glyph->voffset = it->voffset;
24036 glyph->type = CHAR_GLYPH;
24037 glyph->avoid_cursor_p = it->avoid_cursor_p;
24038 glyph->multibyte_p = it->multibyte_p;
24039 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24040 {
24041 /* In R2L rows, the left and the right box edges need to be
24042 drawn in reverse direction. */
24043 glyph->right_box_line_p = it->start_of_box_run_p;
24044 glyph->left_box_line_p = it->end_of_box_run_p;
24045 }
24046 else
24047 {
24048 glyph->left_box_line_p = it->start_of_box_run_p;
24049 glyph->right_box_line_p = it->end_of_box_run_p;
24050 }
24051 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24052 || it->phys_descent > it->descent);
24053 glyph->glyph_not_available_p = it->glyph_not_available_p;
24054 glyph->face_id = it->face_id;
24055 glyph->u.ch = it->char_to_display;
24056 glyph->slice.img = null_glyph_slice;
24057 glyph->font_type = FONT_TYPE_UNKNOWN;
24058 if (it->bidi_p)
24059 {
24060 glyph->resolved_level = it->bidi_it.resolved_level;
24061 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24062 emacs_abort ();
24063 glyph->bidi_type = it->bidi_it.type;
24064 }
24065 else
24066 {
24067 glyph->resolved_level = 0;
24068 glyph->bidi_type = UNKNOWN_BT;
24069 }
24070 ++it->glyph_row->used[area];
24071 }
24072 else
24073 IT_EXPAND_MATRIX_WIDTH (it, area);
24074 }
24075
24076 /* Store one glyph for the composition IT->cmp_it.id in
24077 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24078 non-null. */
24079
24080 static void
24081 append_composite_glyph (struct it *it)
24082 {
24083 struct glyph *glyph;
24084 enum glyph_row_area area = it->area;
24085
24086 eassert (it->glyph_row);
24087
24088 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24089 if (glyph < it->glyph_row->glyphs[area + 1])
24090 {
24091 /* If the glyph row is reversed, we need to prepend the glyph
24092 rather than append it. */
24093 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24094 {
24095 struct glyph *g;
24096
24097 /* Make room for the new glyph. */
24098 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24099 g[1] = *g;
24100 glyph = it->glyph_row->glyphs[it->area];
24101 }
24102 glyph->charpos = it->cmp_it.charpos;
24103 glyph->object = it->object;
24104 glyph->pixel_width = it->pixel_width;
24105 glyph->ascent = it->ascent;
24106 glyph->descent = it->descent;
24107 glyph->voffset = it->voffset;
24108 glyph->type = COMPOSITE_GLYPH;
24109 if (it->cmp_it.ch < 0)
24110 {
24111 glyph->u.cmp.automatic = 0;
24112 glyph->u.cmp.id = it->cmp_it.id;
24113 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24114 }
24115 else
24116 {
24117 glyph->u.cmp.automatic = 1;
24118 glyph->u.cmp.id = it->cmp_it.id;
24119 glyph->slice.cmp.from = it->cmp_it.from;
24120 glyph->slice.cmp.to = it->cmp_it.to - 1;
24121 }
24122 glyph->avoid_cursor_p = it->avoid_cursor_p;
24123 glyph->multibyte_p = it->multibyte_p;
24124 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24125 {
24126 /* In R2L rows, the left and the right box edges need to be
24127 drawn in reverse direction. */
24128 glyph->right_box_line_p = it->start_of_box_run_p;
24129 glyph->left_box_line_p = it->end_of_box_run_p;
24130 }
24131 else
24132 {
24133 glyph->left_box_line_p = it->start_of_box_run_p;
24134 glyph->right_box_line_p = it->end_of_box_run_p;
24135 }
24136 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24137 || it->phys_descent > it->descent);
24138 glyph->padding_p = 0;
24139 glyph->glyph_not_available_p = 0;
24140 glyph->face_id = it->face_id;
24141 glyph->font_type = FONT_TYPE_UNKNOWN;
24142 if (it->bidi_p)
24143 {
24144 glyph->resolved_level = it->bidi_it.resolved_level;
24145 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24146 emacs_abort ();
24147 glyph->bidi_type = it->bidi_it.type;
24148 }
24149 ++it->glyph_row->used[area];
24150 }
24151 else
24152 IT_EXPAND_MATRIX_WIDTH (it, area);
24153 }
24154
24155
24156 /* Change IT->ascent and IT->height according to the setting of
24157 IT->voffset. */
24158
24159 static void
24160 take_vertical_position_into_account (struct it *it)
24161 {
24162 if (it->voffset)
24163 {
24164 if (it->voffset < 0)
24165 /* Increase the ascent so that we can display the text higher
24166 in the line. */
24167 it->ascent -= it->voffset;
24168 else
24169 /* Increase the descent so that we can display the text lower
24170 in the line. */
24171 it->descent += it->voffset;
24172 }
24173 }
24174
24175
24176 /* Produce glyphs/get display metrics for the image IT is loaded with.
24177 See the description of struct display_iterator in dispextern.h for
24178 an overview of struct display_iterator. */
24179
24180 static void
24181 produce_image_glyph (struct it *it)
24182 {
24183 struct image *img;
24184 struct face *face;
24185 int glyph_ascent, crop;
24186 struct glyph_slice slice;
24187
24188 eassert (it->what == IT_IMAGE);
24189
24190 face = FACE_FROM_ID (it->f, it->face_id);
24191 eassert (face);
24192 /* Make sure X resources of the face is loaded. */
24193 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24194
24195 if (it->image_id < 0)
24196 {
24197 /* Fringe bitmap. */
24198 it->ascent = it->phys_ascent = 0;
24199 it->descent = it->phys_descent = 0;
24200 it->pixel_width = 0;
24201 it->nglyphs = 0;
24202 return;
24203 }
24204
24205 img = IMAGE_FROM_ID (it->f, it->image_id);
24206 eassert (img);
24207 /* Make sure X resources of the image is loaded. */
24208 prepare_image_for_display (it->f, img);
24209
24210 slice.x = slice.y = 0;
24211 slice.width = img->width;
24212 slice.height = img->height;
24213
24214 if (INTEGERP (it->slice.x))
24215 slice.x = XINT (it->slice.x);
24216 else if (FLOATP (it->slice.x))
24217 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24218
24219 if (INTEGERP (it->slice.y))
24220 slice.y = XINT (it->slice.y);
24221 else if (FLOATP (it->slice.y))
24222 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24223
24224 if (INTEGERP (it->slice.width))
24225 slice.width = XINT (it->slice.width);
24226 else if (FLOATP (it->slice.width))
24227 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24228
24229 if (INTEGERP (it->slice.height))
24230 slice.height = XINT (it->slice.height);
24231 else if (FLOATP (it->slice.height))
24232 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24233
24234 if (slice.x >= img->width)
24235 slice.x = img->width;
24236 if (slice.y >= img->height)
24237 slice.y = img->height;
24238 if (slice.x + slice.width >= img->width)
24239 slice.width = img->width - slice.x;
24240 if (slice.y + slice.height > img->height)
24241 slice.height = img->height - slice.y;
24242
24243 if (slice.width == 0 || slice.height == 0)
24244 return;
24245
24246 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24247
24248 it->descent = slice.height - glyph_ascent;
24249 if (slice.y == 0)
24250 it->descent += img->vmargin;
24251 if (slice.y + slice.height == img->height)
24252 it->descent += img->vmargin;
24253 it->phys_descent = it->descent;
24254
24255 it->pixel_width = slice.width;
24256 if (slice.x == 0)
24257 it->pixel_width += img->hmargin;
24258 if (slice.x + slice.width == img->width)
24259 it->pixel_width += img->hmargin;
24260
24261 /* It's quite possible for images to have an ascent greater than
24262 their height, so don't get confused in that case. */
24263 if (it->descent < 0)
24264 it->descent = 0;
24265
24266 it->nglyphs = 1;
24267
24268 if (face->box != FACE_NO_BOX)
24269 {
24270 if (face->box_line_width > 0)
24271 {
24272 if (slice.y == 0)
24273 it->ascent += face->box_line_width;
24274 if (slice.y + slice.height == img->height)
24275 it->descent += face->box_line_width;
24276 }
24277
24278 if (it->start_of_box_run_p && slice.x == 0)
24279 it->pixel_width += eabs (face->box_line_width);
24280 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24281 it->pixel_width += eabs (face->box_line_width);
24282 }
24283
24284 take_vertical_position_into_account (it);
24285
24286 /* Automatically crop wide image glyphs at right edge so we can
24287 draw the cursor on same display row. */
24288 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24289 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24290 {
24291 it->pixel_width -= crop;
24292 slice.width -= crop;
24293 }
24294
24295 if (it->glyph_row)
24296 {
24297 struct glyph *glyph;
24298 enum glyph_row_area area = it->area;
24299
24300 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24301 if (glyph < it->glyph_row->glyphs[area + 1])
24302 {
24303 glyph->charpos = CHARPOS (it->position);
24304 glyph->object = it->object;
24305 glyph->pixel_width = it->pixel_width;
24306 glyph->ascent = glyph_ascent;
24307 glyph->descent = it->descent;
24308 glyph->voffset = it->voffset;
24309 glyph->type = IMAGE_GLYPH;
24310 glyph->avoid_cursor_p = it->avoid_cursor_p;
24311 glyph->multibyte_p = it->multibyte_p;
24312 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24313 {
24314 /* In R2L rows, the left and the right box edges need to be
24315 drawn in reverse direction. */
24316 glyph->right_box_line_p = it->start_of_box_run_p;
24317 glyph->left_box_line_p = it->end_of_box_run_p;
24318 }
24319 else
24320 {
24321 glyph->left_box_line_p = it->start_of_box_run_p;
24322 glyph->right_box_line_p = it->end_of_box_run_p;
24323 }
24324 glyph->overlaps_vertically_p = 0;
24325 glyph->padding_p = 0;
24326 glyph->glyph_not_available_p = 0;
24327 glyph->face_id = it->face_id;
24328 glyph->u.img_id = img->id;
24329 glyph->slice.img = slice;
24330 glyph->font_type = FONT_TYPE_UNKNOWN;
24331 if (it->bidi_p)
24332 {
24333 glyph->resolved_level = it->bidi_it.resolved_level;
24334 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24335 emacs_abort ();
24336 glyph->bidi_type = it->bidi_it.type;
24337 }
24338 ++it->glyph_row->used[area];
24339 }
24340 else
24341 IT_EXPAND_MATRIX_WIDTH (it, area);
24342 }
24343 }
24344
24345
24346 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24347 of the glyph, WIDTH and HEIGHT are the width and height of the
24348 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24349
24350 static void
24351 append_stretch_glyph (struct it *it, Lisp_Object object,
24352 int width, int height, int ascent)
24353 {
24354 struct glyph *glyph;
24355 enum glyph_row_area area = it->area;
24356
24357 eassert (ascent >= 0 && ascent <= height);
24358
24359 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24360 if (glyph < it->glyph_row->glyphs[area + 1])
24361 {
24362 /* If the glyph row is reversed, we need to prepend the glyph
24363 rather than append it. */
24364 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24365 {
24366 struct glyph *g;
24367
24368 /* Make room for the additional glyph. */
24369 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24370 g[1] = *g;
24371 glyph = it->glyph_row->glyphs[area];
24372 }
24373 glyph->charpos = CHARPOS (it->position);
24374 glyph->object = object;
24375 glyph->pixel_width = width;
24376 glyph->ascent = ascent;
24377 glyph->descent = height - ascent;
24378 glyph->voffset = it->voffset;
24379 glyph->type = STRETCH_GLYPH;
24380 glyph->avoid_cursor_p = it->avoid_cursor_p;
24381 glyph->multibyte_p = it->multibyte_p;
24382 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24383 {
24384 /* In R2L rows, the left and the right box edges need to be
24385 drawn in reverse direction. */
24386 glyph->right_box_line_p = it->start_of_box_run_p;
24387 glyph->left_box_line_p = it->end_of_box_run_p;
24388 }
24389 else
24390 {
24391 glyph->left_box_line_p = it->start_of_box_run_p;
24392 glyph->right_box_line_p = it->end_of_box_run_p;
24393 }
24394 glyph->overlaps_vertically_p = 0;
24395 glyph->padding_p = 0;
24396 glyph->glyph_not_available_p = 0;
24397 glyph->face_id = it->face_id;
24398 glyph->u.stretch.ascent = ascent;
24399 glyph->u.stretch.height = height;
24400 glyph->slice.img = null_glyph_slice;
24401 glyph->font_type = FONT_TYPE_UNKNOWN;
24402 if (it->bidi_p)
24403 {
24404 glyph->resolved_level = it->bidi_it.resolved_level;
24405 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24406 emacs_abort ();
24407 glyph->bidi_type = it->bidi_it.type;
24408 }
24409 else
24410 {
24411 glyph->resolved_level = 0;
24412 glyph->bidi_type = UNKNOWN_BT;
24413 }
24414 ++it->glyph_row->used[area];
24415 }
24416 else
24417 IT_EXPAND_MATRIX_WIDTH (it, area);
24418 }
24419
24420 #endif /* HAVE_WINDOW_SYSTEM */
24421
24422 /* Produce a stretch glyph for iterator IT. IT->object is the value
24423 of the glyph property displayed. The value must be a list
24424 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24425 being recognized:
24426
24427 1. `:width WIDTH' specifies that the space should be WIDTH *
24428 canonical char width wide. WIDTH may be an integer or floating
24429 point number.
24430
24431 2. `:relative-width FACTOR' specifies that the width of the stretch
24432 should be computed from the width of the first character having the
24433 `glyph' property, and should be FACTOR times that width.
24434
24435 3. `:align-to HPOS' specifies that the space should be wide enough
24436 to reach HPOS, a value in canonical character units.
24437
24438 Exactly one of the above pairs must be present.
24439
24440 4. `:height HEIGHT' specifies that the height of the stretch produced
24441 should be HEIGHT, measured in canonical character units.
24442
24443 5. `:relative-height FACTOR' specifies that the height of the
24444 stretch should be FACTOR times the height of the characters having
24445 the glyph property.
24446
24447 Either none or exactly one of 4 or 5 must be present.
24448
24449 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24450 of the stretch should be used for the ascent of the stretch.
24451 ASCENT must be in the range 0 <= ASCENT <= 100. */
24452
24453 void
24454 produce_stretch_glyph (struct it *it)
24455 {
24456 /* (space :width WIDTH :height HEIGHT ...) */
24457 Lisp_Object prop, plist;
24458 int width = 0, height = 0, align_to = -1;
24459 int zero_width_ok_p = 0;
24460 double tem;
24461 struct font *font = NULL;
24462
24463 #ifdef HAVE_WINDOW_SYSTEM
24464 int ascent = 0;
24465 int zero_height_ok_p = 0;
24466
24467 if (FRAME_WINDOW_P (it->f))
24468 {
24469 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24470 font = face->font ? face->font : FRAME_FONT (it->f);
24471 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24472 }
24473 #endif
24474
24475 /* List should start with `space'. */
24476 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24477 plist = XCDR (it->object);
24478
24479 /* Compute the width of the stretch. */
24480 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24481 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24482 {
24483 /* Absolute width `:width WIDTH' specified and valid. */
24484 zero_width_ok_p = 1;
24485 width = (int)tem;
24486 }
24487 #ifdef HAVE_WINDOW_SYSTEM
24488 else if (FRAME_WINDOW_P (it->f)
24489 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24490 {
24491 /* Relative width `:relative-width FACTOR' specified and valid.
24492 Compute the width of the characters having the `glyph'
24493 property. */
24494 struct it it2;
24495 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24496
24497 it2 = *it;
24498 if (it->multibyte_p)
24499 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24500 else
24501 {
24502 it2.c = it2.char_to_display = *p, it2.len = 1;
24503 if (! ASCII_CHAR_P (it2.c))
24504 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24505 }
24506
24507 it2.glyph_row = NULL;
24508 it2.what = IT_CHARACTER;
24509 x_produce_glyphs (&it2);
24510 width = NUMVAL (prop) * it2.pixel_width;
24511 }
24512 #endif /* HAVE_WINDOW_SYSTEM */
24513 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24514 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24515 {
24516 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24517 align_to = (align_to < 0
24518 ? 0
24519 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24520 else if (align_to < 0)
24521 align_to = window_box_left_offset (it->w, TEXT_AREA);
24522 width = max (0, (int)tem + align_to - it->current_x);
24523 zero_width_ok_p = 1;
24524 }
24525 else
24526 /* Nothing specified -> width defaults to canonical char width. */
24527 width = FRAME_COLUMN_WIDTH (it->f);
24528
24529 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24530 width = 1;
24531
24532 #ifdef HAVE_WINDOW_SYSTEM
24533 /* Compute height. */
24534 if (FRAME_WINDOW_P (it->f))
24535 {
24536 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24537 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24538 {
24539 height = (int)tem;
24540 zero_height_ok_p = 1;
24541 }
24542 else if (prop = Fplist_get (plist, QCrelative_height),
24543 NUMVAL (prop) > 0)
24544 height = FONT_HEIGHT (font) * NUMVAL (prop);
24545 else
24546 height = FONT_HEIGHT (font);
24547
24548 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24549 height = 1;
24550
24551 /* Compute percentage of height used for ascent. If
24552 `:ascent ASCENT' is present and valid, use that. Otherwise,
24553 derive the ascent from the font in use. */
24554 if (prop = Fplist_get (plist, QCascent),
24555 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24556 ascent = height * NUMVAL (prop) / 100.0;
24557 else if (!NILP (prop)
24558 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24559 ascent = min (max (0, (int)tem), height);
24560 else
24561 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24562 }
24563 else
24564 #endif /* HAVE_WINDOW_SYSTEM */
24565 height = 1;
24566
24567 if (width > 0 && it->line_wrap != TRUNCATE
24568 && it->current_x + width > it->last_visible_x)
24569 {
24570 width = it->last_visible_x - it->current_x;
24571 #ifdef HAVE_WINDOW_SYSTEM
24572 /* Subtract one more pixel from the stretch width, but only on
24573 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24574 width -= FRAME_WINDOW_P (it->f);
24575 #endif
24576 }
24577
24578 if (width > 0 && height > 0 && it->glyph_row)
24579 {
24580 Lisp_Object o_object = it->object;
24581 Lisp_Object object = it->stack[it->sp - 1].string;
24582 int n = width;
24583
24584 if (!STRINGP (object))
24585 object = it->w->contents;
24586 #ifdef HAVE_WINDOW_SYSTEM
24587 if (FRAME_WINDOW_P (it->f))
24588 append_stretch_glyph (it, object, width, height, ascent);
24589 else
24590 #endif
24591 {
24592 it->object = object;
24593 it->char_to_display = ' ';
24594 it->pixel_width = it->len = 1;
24595 while (n--)
24596 tty_append_glyph (it);
24597 it->object = o_object;
24598 }
24599 }
24600
24601 it->pixel_width = width;
24602 #ifdef HAVE_WINDOW_SYSTEM
24603 if (FRAME_WINDOW_P (it->f))
24604 {
24605 it->ascent = it->phys_ascent = ascent;
24606 it->descent = it->phys_descent = height - it->ascent;
24607 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24608 take_vertical_position_into_account (it);
24609 }
24610 else
24611 #endif
24612 it->nglyphs = width;
24613 }
24614
24615 /* Get information about special display element WHAT in an
24616 environment described by IT. WHAT is one of IT_TRUNCATION or
24617 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24618 non-null glyph_row member. This function ensures that fields like
24619 face_id, c, len of IT are left untouched. */
24620
24621 static void
24622 produce_special_glyphs (struct it *it, enum display_element_type what)
24623 {
24624 struct it temp_it;
24625 Lisp_Object gc;
24626 GLYPH glyph;
24627
24628 temp_it = *it;
24629 temp_it.object = make_number (0);
24630 memset (&temp_it.current, 0, sizeof temp_it.current);
24631
24632 if (what == IT_CONTINUATION)
24633 {
24634 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24635 if (it->bidi_it.paragraph_dir == R2L)
24636 SET_GLYPH_FROM_CHAR (glyph, '/');
24637 else
24638 SET_GLYPH_FROM_CHAR (glyph, '\\');
24639 if (it->dp
24640 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24641 {
24642 /* FIXME: Should we mirror GC for R2L lines? */
24643 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24644 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24645 }
24646 }
24647 else if (what == IT_TRUNCATION)
24648 {
24649 /* Truncation glyph. */
24650 SET_GLYPH_FROM_CHAR (glyph, '$');
24651 if (it->dp
24652 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24653 {
24654 /* FIXME: Should we mirror GC for R2L lines? */
24655 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24656 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24657 }
24658 }
24659 else
24660 emacs_abort ();
24661
24662 #ifdef HAVE_WINDOW_SYSTEM
24663 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24664 is turned off, we precede the truncation/continuation glyphs by a
24665 stretch glyph whose width is computed such that these special
24666 glyphs are aligned at the window margin, even when very different
24667 fonts are used in different glyph rows. */
24668 if (FRAME_WINDOW_P (temp_it.f)
24669 /* init_iterator calls this with it->glyph_row == NULL, and it
24670 wants only the pixel width of the truncation/continuation
24671 glyphs. */
24672 && temp_it.glyph_row
24673 /* insert_left_trunc_glyphs calls us at the beginning of the
24674 row, and it has its own calculation of the stretch glyph
24675 width. */
24676 && temp_it.glyph_row->used[TEXT_AREA] > 0
24677 && (temp_it.glyph_row->reversed_p
24678 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24679 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24680 {
24681 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24682
24683 if (stretch_width > 0)
24684 {
24685 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24686 struct font *font =
24687 face->font ? face->font : FRAME_FONT (temp_it.f);
24688 int stretch_ascent =
24689 (((temp_it.ascent + temp_it.descent)
24690 * FONT_BASE (font)) / FONT_HEIGHT (font));
24691
24692 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24693 temp_it.ascent + temp_it.descent,
24694 stretch_ascent);
24695 }
24696 }
24697 #endif
24698
24699 temp_it.dp = NULL;
24700 temp_it.what = IT_CHARACTER;
24701 temp_it.len = 1;
24702 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24703 temp_it.face_id = GLYPH_FACE (glyph);
24704 temp_it.len = CHAR_BYTES (temp_it.c);
24705
24706 PRODUCE_GLYPHS (&temp_it);
24707 it->pixel_width = temp_it.pixel_width;
24708 it->nglyphs = temp_it.pixel_width;
24709 }
24710
24711 #ifdef HAVE_WINDOW_SYSTEM
24712
24713 /* Calculate line-height and line-spacing properties.
24714 An integer value specifies explicit pixel value.
24715 A float value specifies relative value to current face height.
24716 A cons (float . face-name) specifies relative value to
24717 height of specified face font.
24718
24719 Returns height in pixels, or nil. */
24720
24721
24722 static Lisp_Object
24723 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24724 int boff, int override)
24725 {
24726 Lisp_Object face_name = Qnil;
24727 int ascent, descent, height;
24728
24729 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24730 return val;
24731
24732 if (CONSP (val))
24733 {
24734 face_name = XCAR (val);
24735 val = XCDR (val);
24736 if (!NUMBERP (val))
24737 val = make_number (1);
24738 if (NILP (face_name))
24739 {
24740 height = it->ascent + it->descent;
24741 goto scale;
24742 }
24743 }
24744
24745 if (NILP (face_name))
24746 {
24747 font = FRAME_FONT (it->f);
24748 boff = FRAME_BASELINE_OFFSET (it->f);
24749 }
24750 else if (EQ (face_name, Qt))
24751 {
24752 override = 0;
24753 }
24754 else
24755 {
24756 int face_id;
24757 struct face *face;
24758
24759 face_id = lookup_named_face (it->f, face_name, 0);
24760 if (face_id < 0)
24761 return make_number (-1);
24762
24763 face = FACE_FROM_ID (it->f, face_id);
24764 font = face->font;
24765 if (font == NULL)
24766 return make_number (-1);
24767 boff = font->baseline_offset;
24768 if (font->vertical_centering)
24769 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24770 }
24771
24772 ascent = FONT_BASE (font) + boff;
24773 descent = FONT_DESCENT (font) - boff;
24774
24775 if (override)
24776 {
24777 it->override_ascent = ascent;
24778 it->override_descent = descent;
24779 it->override_boff = boff;
24780 }
24781
24782 height = ascent + descent;
24783
24784 scale:
24785 if (FLOATP (val))
24786 height = (int)(XFLOAT_DATA (val) * height);
24787 else if (INTEGERP (val))
24788 height *= XINT (val);
24789
24790 return make_number (height);
24791 }
24792
24793
24794 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24795 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24796 and only if this is for a character for which no font was found.
24797
24798 If the display method (it->glyphless_method) is
24799 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24800 length of the acronym or the hexadecimal string, UPPER_XOFF and
24801 UPPER_YOFF are pixel offsets for the upper part of the string,
24802 LOWER_XOFF and LOWER_YOFF are for the lower part.
24803
24804 For the other display methods, LEN through LOWER_YOFF are zero. */
24805
24806 static void
24807 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24808 short upper_xoff, short upper_yoff,
24809 short lower_xoff, short lower_yoff)
24810 {
24811 struct glyph *glyph;
24812 enum glyph_row_area area = it->area;
24813
24814 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24815 if (glyph < it->glyph_row->glyphs[area + 1])
24816 {
24817 /* If the glyph row is reversed, we need to prepend the glyph
24818 rather than append it. */
24819 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24820 {
24821 struct glyph *g;
24822
24823 /* Make room for the additional glyph. */
24824 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24825 g[1] = *g;
24826 glyph = it->glyph_row->glyphs[area];
24827 }
24828 glyph->charpos = CHARPOS (it->position);
24829 glyph->object = it->object;
24830 glyph->pixel_width = it->pixel_width;
24831 glyph->ascent = it->ascent;
24832 glyph->descent = it->descent;
24833 glyph->voffset = it->voffset;
24834 glyph->type = GLYPHLESS_GLYPH;
24835 glyph->u.glyphless.method = it->glyphless_method;
24836 glyph->u.glyphless.for_no_font = for_no_font;
24837 glyph->u.glyphless.len = len;
24838 glyph->u.glyphless.ch = it->c;
24839 glyph->slice.glyphless.upper_xoff = upper_xoff;
24840 glyph->slice.glyphless.upper_yoff = upper_yoff;
24841 glyph->slice.glyphless.lower_xoff = lower_xoff;
24842 glyph->slice.glyphless.lower_yoff = lower_yoff;
24843 glyph->avoid_cursor_p = it->avoid_cursor_p;
24844 glyph->multibyte_p = it->multibyte_p;
24845 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24846 {
24847 /* In R2L rows, the left and the right box edges need to be
24848 drawn in reverse direction. */
24849 glyph->right_box_line_p = it->start_of_box_run_p;
24850 glyph->left_box_line_p = it->end_of_box_run_p;
24851 }
24852 else
24853 {
24854 glyph->left_box_line_p = it->start_of_box_run_p;
24855 glyph->right_box_line_p = it->end_of_box_run_p;
24856 }
24857 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24858 || it->phys_descent > it->descent);
24859 glyph->padding_p = 0;
24860 glyph->glyph_not_available_p = 0;
24861 glyph->face_id = face_id;
24862 glyph->font_type = FONT_TYPE_UNKNOWN;
24863 if (it->bidi_p)
24864 {
24865 glyph->resolved_level = it->bidi_it.resolved_level;
24866 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24867 emacs_abort ();
24868 glyph->bidi_type = it->bidi_it.type;
24869 }
24870 ++it->glyph_row->used[area];
24871 }
24872 else
24873 IT_EXPAND_MATRIX_WIDTH (it, area);
24874 }
24875
24876
24877 /* Produce a glyph for a glyphless character for iterator IT.
24878 IT->glyphless_method specifies which method to use for displaying
24879 the character. See the description of enum
24880 glyphless_display_method in dispextern.h for the detail.
24881
24882 FOR_NO_FONT is nonzero if and only if this is for a character for
24883 which no font was found. ACRONYM, if non-nil, is an acronym string
24884 for the character. */
24885
24886 static void
24887 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24888 {
24889 int face_id;
24890 struct face *face;
24891 struct font *font;
24892 int base_width, base_height, width, height;
24893 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24894 int len;
24895
24896 /* Get the metrics of the base font. We always refer to the current
24897 ASCII face. */
24898 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24899 font = face->font ? face->font : FRAME_FONT (it->f);
24900 it->ascent = FONT_BASE (font) + font->baseline_offset;
24901 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24902 base_height = it->ascent + it->descent;
24903 base_width = font->average_width;
24904
24905 face_id = merge_glyphless_glyph_face (it);
24906
24907 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24908 {
24909 it->pixel_width = THIN_SPACE_WIDTH;
24910 len = 0;
24911 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24912 }
24913 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24914 {
24915 width = CHAR_WIDTH (it->c);
24916 if (width == 0)
24917 width = 1;
24918 else if (width > 4)
24919 width = 4;
24920 it->pixel_width = base_width * width;
24921 len = 0;
24922 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24923 }
24924 else
24925 {
24926 char buf[7];
24927 const char *str;
24928 unsigned int code[6];
24929 int upper_len;
24930 int ascent, descent;
24931 struct font_metrics metrics_upper, metrics_lower;
24932
24933 face = FACE_FROM_ID (it->f, face_id);
24934 font = face->font ? face->font : FRAME_FONT (it->f);
24935 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24936
24937 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24938 {
24939 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24940 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24941 if (CONSP (acronym))
24942 acronym = XCAR (acronym);
24943 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24944 }
24945 else
24946 {
24947 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24948 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24949 str = buf;
24950 }
24951 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24952 code[len] = font->driver->encode_char (font, str[len]);
24953 upper_len = (len + 1) / 2;
24954 font->driver->text_extents (font, code, upper_len,
24955 &metrics_upper);
24956 font->driver->text_extents (font, code + upper_len, len - upper_len,
24957 &metrics_lower);
24958
24959
24960
24961 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24962 width = max (metrics_upper.width, metrics_lower.width) + 4;
24963 upper_xoff = upper_yoff = 2; /* the typical case */
24964 if (base_width >= width)
24965 {
24966 /* Align the upper to the left, the lower to the right. */
24967 it->pixel_width = base_width;
24968 lower_xoff = base_width - 2 - metrics_lower.width;
24969 }
24970 else
24971 {
24972 /* Center the shorter one. */
24973 it->pixel_width = width;
24974 if (metrics_upper.width >= metrics_lower.width)
24975 lower_xoff = (width - metrics_lower.width) / 2;
24976 else
24977 {
24978 /* FIXME: This code doesn't look right. It formerly was
24979 missing the "lower_xoff = 0;", which couldn't have
24980 been right since it left lower_xoff uninitialized. */
24981 lower_xoff = 0;
24982 upper_xoff = (width - metrics_upper.width) / 2;
24983 }
24984 }
24985
24986 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24987 top, bottom, and between upper and lower strings. */
24988 height = (metrics_upper.ascent + metrics_upper.descent
24989 + metrics_lower.ascent + metrics_lower.descent) + 5;
24990 /* Center vertically.
24991 H:base_height, D:base_descent
24992 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24993
24994 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24995 descent = D - H/2 + h/2;
24996 lower_yoff = descent - 2 - ld;
24997 upper_yoff = lower_yoff - la - 1 - ud; */
24998 ascent = - (it->descent - (base_height + height + 1) / 2);
24999 descent = it->descent - (base_height - height) / 2;
25000 lower_yoff = descent - 2 - metrics_lower.descent;
25001 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25002 - metrics_upper.descent);
25003 /* Don't make the height shorter than the base height. */
25004 if (height > base_height)
25005 {
25006 it->ascent = ascent;
25007 it->descent = descent;
25008 }
25009 }
25010
25011 it->phys_ascent = it->ascent;
25012 it->phys_descent = it->descent;
25013 if (it->glyph_row)
25014 append_glyphless_glyph (it, face_id, for_no_font, len,
25015 upper_xoff, upper_yoff,
25016 lower_xoff, lower_yoff);
25017 it->nglyphs = 1;
25018 take_vertical_position_into_account (it);
25019 }
25020
25021
25022 /* RIF:
25023 Produce glyphs/get display metrics for the display element IT is
25024 loaded with. See the description of struct it in dispextern.h
25025 for an overview of struct it. */
25026
25027 void
25028 x_produce_glyphs (struct it *it)
25029 {
25030 int extra_line_spacing = it->extra_line_spacing;
25031
25032 it->glyph_not_available_p = 0;
25033
25034 if (it->what == IT_CHARACTER)
25035 {
25036 XChar2b char2b;
25037 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25038 struct font *font = face->font;
25039 struct font_metrics *pcm = NULL;
25040 int boff; /* baseline offset */
25041
25042 if (font == NULL)
25043 {
25044 /* When no suitable font is found, display this character by
25045 the method specified in the first extra slot of
25046 Vglyphless_char_display. */
25047 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25048
25049 eassert (it->what == IT_GLYPHLESS);
25050 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25051 goto done;
25052 }
25053
25054 boff = font->baseline_offset;
25055 if (font->vertical_centering)
25056 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25057
25058 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25059 {
25060 int stretched_p;
25061
25062 it->nglyphs = 1;
25063
25064 if (it->override_ascent >= 0)
25065 {
25066 it->ascent = it->override_ascent;
25067 it->descent = it->override_descent;
25068 boff = it->override_boff;
25069 }
25070 else
25071 {
25072 it->ascent = FONT_BASE (font) + boff;
25073 it->descent = FONT_DESCENT (font) - boff;
25074 }
25075
25076 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25077 {
25078 pcm = get_per_char_metric (font, &char2b);
25079 if (pcm->width == 0
25080 && pcm->rbearing == 0 && pcm->lbearing == 0)
25081 pcm = NULL;
25082 }
25083
25084 if (pcm)
25085 {
25086 it->phys_ascent = pcm->ascent + boff;
25087 it->phys_descent = pcm->descent - boff;
25088 it->pixel_width = pcm->width;
25089 }
25090 else
25091 {
25092 it->glyph_not_available_p = 1;
25093 it->phys_ascent = it->ascent;
25094 it->phys_descent = it->descent;
25095 it->pixel_width = font->space_width;
25096 }
25097
25098 if (it->constrain_row_ascent_descent_p)
25099 {
25100 if (it->descent > it->max_descent)
25101 {
25102 it->ascent += it->descent - it->max_descent;
25103 it->descent = it->max_descent;
25104 }
25105 if (it->ascent > it->max_ascent)
25106 {
25107 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25108 it->ascent = it->max_ascent;
25109 }
25110 it->phys_ascent = min (it->phys_ascent, it->ascent);
25111 it->phys_descent = min (it->phys_descent, it->descent);
25112 extra_line_spacing = 0;
25113 }
25114
25115 /* If this is a space inside a region of text with
25116 `space-width' property, change its width. */
25117 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25118 if (stretched_p)
25119 it->pixel_width *= XFLOATINT (it->space_width);
25120
25121 /* If face has a box, add the box thickness to the character
25122 height. If character has a box line to the left and/or
25123 right, add the box line width to the character's width. */
25124 if (face->box != FACE_NO_BOX)
25125 {
25126 int thick = face->box_line_width;
25127
25128 if (thick > 0)
25129 {
25130 it->ascent += thick;
25131 it->descent += thick;
25132 }
25133 else
25134 thick = -thick;
25135
25136 if (it->start_of_box_run_p)
25137 it->pixel_width += thick;
25138 if (it->end_of_box_run_p)
25139 it->pixel_width += thick;
25140 }
25141
25142 /* If face has an overline, add the height of the overline
25143 (1 pixel) and a 1 pixel margin to the character height. */
25144 if (face->overline_p)
25145 it->ascent += overline_margin;
25146
25147 if (it->constrain_row_ascent_descent_p)
25148 {
25149 if (it->ascent > it->max_ascent)
25150 it->ascent = it->max_ascent;
25151 if (it->descent > it->max_descent)
25152 it->descent = it->max_descent;
25153 }
25154
25155 take_vertical_position_into_account (it);
25156
25157 /* If we have to actually produce glyphs, do it. */
25158 if (it->glyph_row)
25159 {
25160 if (stretched_p)
25161 {
25162 /* Translate a space with a `space-width' property
25163 into a stretch glyph. */
25164 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25165 / FONT_HEIGHT (font));
25166 append_stretch_glyph (it, it->object, it->pixel_width,
25167 it->ascent + it->descent, ascent);
25168 }
25169 else
25170 append_glyph (it);
25171
25172 /* If characters with lbearing or rbearing are displayed
25173 in this line, record that fact in a flag of the
25174 glyph row. This is used to optimize X output code. */
25175 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25176 it->glyph_row->contains_overlapping_glyphs_p = 1;
25177 }
25178 if (! stretched_p && it->pixel_width == 0)
25179 /* We assure that all visible glyphs have at least 1-pixel
25180 width. */
25181 it->pixel_width = 1;
25182 }
25183 else if (it->char_to_display == '\n')
25184 {
25185 /* A newline has no width, but we need the height of the
25186 line. But if previous part of the line sets a height,
25187 don't increase that height */
25188
25189 Lisp_Object height;
25190 Lisp_Object total_height = Qnil;
25191
25192 it->override_ascent = -1;
25193 it->pixel_width = 0;
25194 it->nglyphs = 0;
25195
25196 height = get_it_property (it, Qline_height);
25197 /* Split (line-height total-height) list */
25198 if (CONSP (height)
25199 && CONSP (XCDR (height))
25200 && NILP (XCDR (XCDR (height))))
25201 {
25202 total_height = XCAR (XCDR (height));
25203 height = XCAR (height);
25204 }
25205 height = calc_line_height_property (it, height, font, boff, 1);
25206
25207 if (it->override_ascent >= 0)
25208 {
25209 it->ascent = it->override_ascent;
25210 it->descent = it->override_descent;
25211 boff = it->override_boff;
25212 }
25213 else
25214 {
25215 it->ascent = FONT_BASE (font) + boff;
25216 it->descent = FONT_DESCENT (font) - boff;
25217 }
25218
25219 if (EQ (height, Qt))
25220 {
25221 if (it->descent > it->max_descent)
25222 {
25223 it->ascent += it->descent - it->max_descent;
25224 it->descent = it->max_descent;
25225 }
25226 if (it->ascent > it->max_ascent)
25227 {
25228 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25229 it->ascent = it->max_ascent;
25230 }
25231 it->phys_ascent = min (it->phys_ascent, it->ascent);
25232 it->phys_descent = min (it->phys_descent, it->descent);
25233 it->constrain_row_ascent_descent_p = 1;
25234 extra_line_spacing = 0;
25235 }
25236 else
25237 {
25238 Lisp_Object spacing;
25239
25240 it->phys_ascent = it->ascent;
25241 it->phys_descent = it->descent;
25242
25243 if ((it->max_ascent > 0 || it->max_descent > 0)
25244 && face->box != FACE_NO_BOX
25245 && face->box_line_width > 0)
25246 {
25247 it->ascent += face->box_line_width;
25248 it->descent += face->box_line_width;
25249 }
25250 if (!NILP (height)
25251 && XINT (height) > it->ascent + it->descent)
25252 it->ascent = XINT (height) - it->descent;
25253
25254 if (!NILP (total_height))
25255 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25256 else
25257 {
25258 spacing = get_it_property (it, Qline_spacing);
25259 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25260 }
25261 if (INTEGERP (spacing))
25262 {
25263 extra_line_spacing = XINT (spacing);
25264 if (!NILP (total_height))
25265 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25266 }
25267 }
25268 }
25269 else /* i.e. (it->char_to_display == '\t') */
25270 {
25271 if (font->space_width > 0)
25272 {
25273 int tab_width = it->tab_width * font->space_width;
25274 int x = it->current_x + it->continuation_lines_width;
25275 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25276
25277 /* If the distance from the current position to the next tab
25278 stop is less than a space character width, use the
25279 tab stop after that. */
25280 if (next_tab_x - x < font->space_width)
25281 next_tab_x += tab_width;
25282
25283 it->pixel_width = next_tab_x - x;
25284 it->nglyphs = 1;
25285 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25286 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25287
25288 if (it->glyph_row)
25289 {
25290 append_stretch_glyph (it, it->object, it->pixel_width,
25291 it->ascent + it->descent, it->ascent);
25292 }
25293 }
25294 else
25295 {
25296 it->pixel_width = 0;
25297 it->nglyphs = 1;
25298 }
25299 }
25300 }
25301 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25302 {
25303 /* A static composition.
25304
25305 Note: A composition is represented as one glyph in the
25306 glyph matrix. There are no padding glyphs.
25307
25308 Important note: pixel_width, ascent, and descent are the
25309 values of what is drawn by draw_glyphs (i.e. the values of
25310 the overall glyphs composed). */
25311 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25312 int boff; /* baseline offset */
25313 struct composition *cmp = composition_table[it->cmp_it.id];
25314 int glyph_len = cmp->glyph_len;
25315 struct font *font = face->font;
25316
25317 it->nglyphs = 1;
25318
25319 /* If we have not yet calculated pixel size data of glyphs of
25320 the composition for the current face font, calculate them
25321 now. Theoretically, we have to check all fonts for the
25322 glyphs, but that requires much time and memory space. So,
25323 here we check only the font of the first glyph. This may
25324 lead to incorrect display, but it's very rare, and C-l
25325 (recenter-top-bottom) can correct the display anyway. */
25326 if (! cmp->font || cmp->font != font)
25327 {
25328 /* Ascent and descent of the font of the first character
25329 of this composition (adjusted by baseline offset).
25330 Ascent and descent of overall glyphs should not be less
25331 than these, respectively. */
25332 int font_ascent, font_descent, font_height;
25333 /* Bounding box of the overall glyphs. */
25334 int leftmost, rightmost, lowest, highest;
25335 int lbearing, rbearing;
25336 int i, width, ascent, descent;
25337 int left_padded = 0, right_padded = 0;
25338 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25339 XChar2b char2b;
25340 struct font_metrics *pcm;
25341 int font_not_found_p;
25342 ptrdiff_t pos;
25343
25344 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25345 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25346 break;
25347 if (glyph_len < cmp->glyph_len)
25348 right_padded = 1;
25349 for (i = 0; i < glyph_len; i++)
25350 {
25351 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25352 break;
25353 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25354 }
25355 if (i > 0)
25356 left_padded = 1;
25357
25358 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25359 : IT_CHARPOS (*it));
25360 /* If no suitable font is found, use the default font. */
25361 font_not_found_p = font == NULL;
25362 if (font_not_found_p)
25363 {
25364 face = face->ascii_face;
25365 font = face->font;
25366 }
25367 boff = font->baseline_offset;
25368 if (font->vertical_centering)
25369 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25370 font_ascent = FONT_BASE (font) + boff;
25371 font_descent = FONT_DESCENT (font) - boff;
25372 font_height = FONT_HEIGHT (font);
25373
25374 cmp->font = font;
25375
25376 pcm = NULL;
25377 if (! font_not_found_p)
25378 {
25379 get_char_face_and_encoding (it->f, c, it->face_id,
25380 &char2b, 0);
25381 pcm = get_per_char_metric (font, &char2b);
25382 }
25383
25384 /* Initialize the bounding box. */
25385 if (pcm)
25386 {
25387 width = cmp->glyph_len > 0 ? pcm->width : 0;
25388 ascent = pcm->ascent;
25389 descent = pcm->descent;
25390 lbearing = pcm->lbearing;
25391 rbearing = pcm->rbearing;
25392 }
25393 else
25394 {
25395 width = cmp->glyph_len > 0 ? font->space_width : 0;
25396 ascent = FONT_BASE (font);
25397 descent = FONT_DESCENT (font);
25398 lbearing = 0;
25399 rbearing = width;
25400 }
25401
25402 rightmost = width;
25403 leftmost = 0;
25404 lowest = - descent + boff;
25405 highest = ascent + boff;
25406
25407 if (! font_not_found_p
25408 && font->default_ascent
25409 && CHAR_TABLE_P (Vuse_default_ascent)
25410 && !NILP (Faref (Vuse_default_ascent,
25411 make_number (it->char_to_display))))
25412 highest = font->default_ascent + boff;
25413
25414 /* Draw the first glyph at the normal position. It may be
25415 shifted to right later if some other glyphs are drawn
25416 at the left. */
25417 cmp->offsets[i * 2] = 0;
25418 cmp->offsets[i * 2 + 1] = boff;
25419 cmp->lbearing = lbearing;
25420 cmp->rbearing = rbearing;
25421
25422 /* Set cmp->offsets for the remaining glyphs. */
25423 for (i++; i < glyph_len; i++)
25424 {
25425 int left, right, btm, top;
25426 int ch = COMPOSITION_GLYPH (cmp, i);
25427 int face_id;
25428 struct face *this_face;
25429
25430 if (ch == '\t')
25431 ch = ' ';
25432 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25433 this_face = FACE_FROM_ID (it->f, face_id);
25434 font = this_face->font;
25435
25436 if (font == NULL)
25437 pcm = NULL;
25438 else
25439 {
25440 get_char_face_and_encoding (it->f, ch, face_id,
25441 &char2b, 0);
25442 pcm = get_per_char_metric (font, &char2b);
25443 }
25444 if (! pcm)
25445 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25446 else
25447 {
25448 width = pcm->width;
25449 ascent = pcm->ascent;
25450 descent = pcm->descent;
25451 lbearing = pcm->lbearing;
25452 rbearing = pcm->rbearing;
25453 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25454 {
25455 /* Relative composition with or without
25456 alternate chars. */
25457 left = (leftmost + rightmost - width) / 2;
25458 btm = - descent + boff;
25459 if (font->relative_compose
25460 && (! CHAR_TABLE_P (Vignore_relative_composition)
25461 || NILP (Faref (Vignore_relative_composition,
25462 make_number (ch)))))
25463 {
25464
25465 if (- descent >= font->relative_compose)
25466 /* One extra pixel between two glyphs. */
25467 btm = highest + 1;
25468 else if (ascent <= 0)
25469 /* One extra pixel between two glyphs. */
25470 btm = lowest - 1 - ascent - descent;
25471 }
25472 }
25473 else
25474 {
25475 /* A composition rule is specified by an integer
25476 value that encodes global and new reference
25477 points (GREF and NREF). GREF and NREF are
25478 specified by numbers as below:
25479
25480 0---1---2 -- ascent
25481 | |
25482 | |
25483 | |
25484 9--10--11 -- center
25485 | |
25486 ---3---4---5--- baseline
25487 | |
25488 6---7---8 -- descent
25489 */
25490 int rule = COMPOSITION_RULE (cmp, i);
25491 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25492
25493 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25494 grefx = gref % 3, nrefx = nref % 3;
25495 grefy = gref / 3, nrefy = nref / 3;
25496 if (xoff)
25497 xoff = font_height * (xoff - 128) / 256;
25498 if (yoff)
25499 yoff = font_height * (yoff - 128) / 256;
25500
25501 left = (leftmost
25502 + grefx * (rightmost - leftmost) / 2
25503 - nrefx * width / 2
25504 + xoff);
25505
25506 btm = ((grefy == 0 ? highest
25507 : grefy == 1 ? 0
25508 : grefy == 2 ? lowest
25509 : (highest + lowest) / 2)
25510 - (nrefy == 0 ? ascent + descent
25511 : nrefy == 1 ? descent - boff
25512 : nrefy == 2 ? 0
25513 : (ascent + descent) / 2)
25514 + yoff);
25515 }
25516
25517 cmp->offsets[i * 2] = left;
25518 cmp->offsets[i * 2 + 1] = btm + descent;
25519
25520 /* Update the bounding box of the overall glyphs. */
25521 if (width > 0)
25522 {
25523 right = left + width;
25524 if (left < leftmost)
25525 leftmost = left;
25526 if (right > rightmost)
25527 rightmost = right;
25528 }
25529 top = btm + descent + ascent;
25530 if (top > highest)
25531 highest = top;
25532 if (btm < lowest)
25533 lowest = btm;
25534
25535 if (cmp->lbearing > left + lbearing)
25536 cmp->lbearing = left + lbearing;
25537 if (cmp->rbearing < left + rbearing)
25538 cmp->rbearing = left + rbearing;
25539 }
25540 }
25541
25542 /* If there are glyphs whose x-offsets are negative,
25543 shift all glyphs to the right and make all x-offsets
25544 non-negative. */
25545 if (leftmost < 0)
25546 {
25547 for (i = 0; i < cmp->glyph_len; i++)
25548 cmp->offsets[i * 2] -= leftmost;
25549 rightmost -= leftmost;
25550 cmp->lbearing -= leftmost;
25551 cmp->rbearing -= leftmost;
25552 }
25553
25554 if (left_padded && cmp->lbearing < 0)
25555 {
25556 for (i = 0; i < cmp->glyph_len; i++)
25557 cmp->offsets[i * 2] -= cmp->lbearing;
25558 rightmost -= cmp->lbearing;
25559 cmp->rbearing -= cmp->lbearing;
25560 cmp->lbearing = 0;
25561 }
25562 if (right_padded && rightmost < cmp->rbearing)
25563 {
25564 rightmost = cmp->rbearing;
25565 }
25566
25567 cmp->pixel_width = rightmost;
25568 cmp->ascent = highest;
25569 cmp->descent = - lowest;
25570 if (cmp->ascent < font_ascent)
25571 cmp->ascent = font_ascent;
25572 if (cmp->descent < font_descent)
25573 cmp->descent = font_descent;
25574 }
25575
25576 if (it->glyph_row
25577 && (cmp->lbearing < 0
25578 || cmp->rbearing > cmp->pixel_width))
25579 it->glyph_row->contains_overlapping_glyphs_p = 1;
25580
25581 it->pixel_width = cmp->pixel_width;
25582 it->ascent = it->phys_ascent = cmp->ascent;
25583 it->descent = it->phys_descent = cmp->descent;
25584 if (face->box != FACE_NO_BOX)
25585 {
25586 int thick = face->box_line_width;
25587
25588 if (thick > 0)
25589 {
25590 it->ascent += thick;
25591 it->descent += thick;
25592 }
25593 else
25594 thick = - thick;
25595
25596 if (it->start_of_box_run_p)
25597 it->pixel_width += thick;
25598 if (it->end_of_box_run_p)
25599 it->pixel_width += thick;
25600 }
25601
25602 /* If face has an overline, add the height of the overline
25603 (1 pixel) and a 1 pixel margin to the character height. */
25604 if (face->overline_p)
25605 it->ascent += overline_margin;
25606
25607 take_vertical_position_into_account (it);
25608 if (it->ascent < 0)
25609 it->ascent = 0;
25610 if (it->descent < 0)
25611 it->descent = 0;
25612
25613 if (it->glyph_row && cmp->glyph_len > 0)
25614 append_composite_glyph (it);
25615 }
25616 else if (it->what == IT_COMPOSITION)
25617 {
25618 /* A dynamic (automatic) composition. */
25619 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25620 Lisp_Object gstring;
25621 struct font_metrics metrics;
25622
25623 it->nglyphs = 1;
25624
25625 gstring = composition_gstring_from_id (it->cmp_it.id);
25626 it->pixel_width
25627 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25628 &metrics);
25629 if (it->glyph_row
25630 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25631 it->glyph_row->contains_overlapping_glyphs_p = 1;
25632 it->ascent = it->phys_ascent = metrics.ascent;
25633 it->descent = it->phys_descent = metrics.descent;
25634 if (face->box != FACE_NO_BOX)
25635 {
25636 int thick = face->box_line_width;
25637
25638 if (thick > 0)
25639 {
25640 it->ascent += thick;
25641 it->descent += thick;
25642 }
25643 else
25644 thick = - thick;
25645
25646 if (it->start_of_box_run_p)
25647 it->pixel_width += thick;
25648 if (it->end_of_box_run_p)
25649 it->pixel_width += thick;
25650 }
25651 /* If face has an overline, add the height of the overline
25652 (1 pixel) and a 1 pixel margin to the character height. */
25653 if (face->overline_p)
25654 it->ascent += overline_margin;
25655 take_vertical_position_into_account (it);
25656 if (it->ascent < 0)
25657 it->ascent = 0;
25658 if (it->descent < 0)
25659 it->descent = 0;
25660
25661 if (it->glyph_row)
25662 append_composite_glyph (it);
25663 }
25664 else if (it->what == IT_GLYPHLESS)
25665 produce_glyphless_glyph (it, 0, Qnil);
25666 else if (it->what == IT_IMAGE)
25667 produce_image_glyph (it);
25668 else if (it->what == IT_STRETCH)
25669 produce_stretch_glyph (it);
25670
25671 done:
25672 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25673 because this isn't true for images with `:ascent 100'. */
25674 eassert (it->ascent >= 0 && it->descent >= 0);
25675 if (it->area == TEXT_AREA)
25676 it->current_x += it->pixel_width;
25677
25678 if (extra_line_spacing > 0)
25679 {
25680 it->descent += extra_line_spacing;
25681 if (extra_line_spacing > it->max_extra_line_spacing)
25682 it->max_extra_line_spacing = extra_line_spacing;
25683 }
25684
25685 it->max_ascent = max (it->max_ascent, it->ascent);
25686 it->max_descent = max (it->max_descent, it->descent);
25687 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25688 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25689 }
25690
25691 /* EXPORT for RIF:
25692 Output LEN glyphs starting at START at the nominal cursor position.
25693 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25694 being updated, and UPDATED_AREA is the area of that row being updated. */
25695
25696 void
25697 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25698 struct glyph *start, enum glyph_row_area updated_area, int len)
25699 {
25700 int x, hpos, chpos = w->phys_cursor.hpos;
25701
25702 eassert (updated_row);
25703 /* When the window is hscrolled, cursor hpos can legitimately be out
25704 of bounds, but we draw the cursor at the corresponding window
25705 margin in that case. */
25706 if (!updated_row->reversed_p && chpos < 0)
25707 chpos = 0;
25708 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25709 chpos = updated_row->used[TEXT_AREA] - 1;
25710
25711 block_input ();
25712
25713 /* Write glyphs. */
25714
25715 hpos = start - updated_row->glyphs[updated_area];
25716 x = draw_glyphs (w, w->output_cursor.x,
25717 updated_row, updated_area,
25718 hpos, hpos + len,
25719 DRAW_NORMAL_TEXT, 0);
25720
25721 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25722 if (updated_area == TEXT_AREA
25723 && w->phys_cursor_on_p
25724 && w->phys_cursor.vpos == w->output_cursor.vpos
25725 && chpos >= hpos
25726 && chpos < hpos + len)
25727 w->phys_cursor_on_p = 0;
25728
25729 unblock_input ();
25730
25731 /* Advance the output cursor. */
25732 w->output_cursor.hpos += len;
25733 w->output_cursor.x = x;
25734 }
25735
25736
25737 /* EXPORT for RIF:
25738 Insert LEN glyphs from START at the nominal cursor position. */
25739
25740 void
25741 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25742 struct glyph *start, enum glyph_row_area updated_area, int len)
25743 {
25744 struct frame *f;
25745 int line_height, shift_by_width, shifted_region_width;
25746 struct glyph_row *row;
25747 struct glyph *glyph;
25748 int frame_x, frame_y;
25749 ptrdiff_t hpos;
25750
25751 eassert (updated_row);
25752 block_input ();
25753 f = XFRAME (WINDOW_FRAME (w));
25754
25755 /* Get the height of the line we are in. */
25756 row = updated_row;
25757 line_height = row->height;
25758
25759 /* Get the width of the glyphs to insert. */
25760 shift_by_width = 0;
25761 for (glyph = start; glyph < start + len; ++glyph)
25762 shift_by_width += glyph->pixel_width;
25763
25764 /* Get the width of the region to shift right. */
25765 shifted_region_width = (window_box_width (w, updated_area)
25766 - w->output_cursor.x
25767 - shift_by_width);
25768
25769 /* Shift right. */
25770 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25771 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25772
25773 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25774 line_height, shift_by_width);
25775
25776 /* Write the glyphs. */
25777 hpos = start - row->glyphs[updated_area];
25778 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25779 hpos, hpos + len,
25780 DRAW_NORMAL_TEXT, 0);
25781
25782 /* Advance the output cursor. */
25783 w->output_cursor.hpos += len;
25784 w->output_cursor.x += shift_by_width;
25785 unblock_input ();
25786 }
25787
25788
25789 /* EXPORT for RIF:
25790 Erase the current text line from the nominal cursor position
25791 (inclusive) to pixel column TO_X (exclusive). The idea is that
25792 everything from TO_X onward is already erased.
25793
25794 TO_X is a pixel position relative to UPDATED_AREA of currently
25795 updated window W. TO_X == -1 means clear to the end of this area. */
25796
25797 void
25798 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25799 enum glyph_row_area updated_area, int to_x)
25800 {
25801 struct frame *f;
25802 int max_x, min_y, max_y;
25803 int from_x, from_y, to_y;
25804
25805 eassert (updated_row);
25806 f = XFRAME (w->frame);
25807
25808 if (updated_row->full_width_p)
25809 max_x = WINDOW_TOTAL_WIDTH (w);
25810 else
25811 max_x = window_box_width (w, updated_area);
25812 max_y = window_text_bottom_y (w);
25813
25814 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25815 of window. For TO_X > 0, truncate to end of drawing area. */
25816 if (to_x == 0)
25817 return;
25818 else if (to_x < 0)
25819 to_x = max_x;
25820 else
25821 to_x = min (to_x, max_x);
25822
25823 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25824
25825 /* Notice if the cursor will be cleared by this operation. */
25826 if (!updated_row->full_width_p)
25827 notice_overwritten_cursor (w, updated_area,
25828 w->output_cursor.x, -1,
25829 updated_row->y,
25830 MATRIX_ROW_BOTTOM_Y (updated_row));
25831
25832 from_x = w->output_cursor.x;
25833
25834 /* Translate to frame coordinates. */
25835 if (updated_row->full_width_p)
25836 {
25837 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25838 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25839 }
25840 else
25841 {
25842 int area_left = window_box_left (w, updated_area);
25843 from_x += area_left;
25844 to_x += area_left;
25845 }
25846
25847 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25848 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
25849 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25850
25851 /* Prevent inadvertently clearing to end of the X window. */
25852 if (to_x > from_x && to_y > from_y)
25853 {
25854 block_input ();
25855 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25856 to_x - from_x, to_y - from_y);
25857 unblock_input ();
25858 }
25859 }
25860
25861 #endif /* HAVE_WINDOW_SYSTEM */
25862
25863
25864 \f
25865 /***********************************************************************
25866 Cursor types
25867 ***********************************************************************/
25868
25869 /* Value is the internal representation of the specified cursor type
25870 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25871 of the bar cursor. */
25872
25873 static enum text_cursor_kinds
25874 get_specified_cursor_type (Lisp_Object arg, int *width)
25875 {
25876 enum text_cursor_kinds type;
25877
25878 if (NILP (arg))
25879 return NO_CURSOR;
25880
25881 if (EQ (arg, Qbox))
25882 return FILLED_BOX_CURSOR;
25883
25884 if (EQ (arg, Qhollow))
25885 return HOLLOW_BOX_CURSOR;
25886
25887 if (EQ (arg, Qbar))
25888 {
25889 *width = 2;
25890 return BAR_CURSOR;
25891 }
25892
25893 if (CONSP (arg)
25894 && EQ (XCAR (arg), Qbar)
25895 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25896 {
25897 *width = XINT (XCDR (arg));
25898 return BAR_CURSOR;
25899 }
25900
25901 if (EQ (arg, Qhbar))
25902 {
25903 *width = 2;
25904 return HBAR_CURSOR;
25905 }
25906
25907 if (CONSP (arg)
25908 && EQ (XCAR (arg), Qhbar)
25909 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25910 {
25911 *width = XINT (XCDR (arg));
25912 return HBAR_CURSOR;
25913 }
25914
25915 /* Treat anything unknown as "hollow box cursor".
25916 It was bad to signal an error; people have trouble fixing
25917 .Xdefaults with Emacs, when it has something bad in it. */
25918 type = HOLLOW_BOX_CURSOR;
25919
25920 return type;
25921 }
25922
25923 /* Set the default cursor types for specified frame. */
25924 void
25925 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25926 {
25927 int width = 1;
25928 Lisp_Object tem;
25929
25930 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25931 FRAME_CURSOR_WIDTH (f) = width;
25932
25933 /* By default, set up the blink-off state depending on the on-state. */
25934
25935 tem = Fassoc (arg, Vblink_cursor_alist);
25936 if (!NILP (tem))
25937 {
25938 FRAME_BLINK_OFF_CURSOR (f)
25939 = get_specified_cursor_type (XCDR (tem), &width);
25940 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25941 }
25942 else
25943 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25944
25945 /* Make sure the cursor gets redrawn. */
25946 f->cursor_type_changed = 1;
25947 }
25948
25949
25950 #ifdef HAVE_WINDOW_SYSTEM
25951
25952 /* Return the cursor we want to be displayed in window W. Return
25953 width of bar/hbar cursor through WIDTH arg. Return with
25954 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25955 (i.e. if the `system caret' should track this cursor).
25956
25957 In a mini-buffer window, we want the cursor only to appear if we
25958 are reading input from this window. For the selected window, we
25959 want the cursor type given by the frame parameter or buffer local
25960 setting of cursor-type. If explicitly marked off, draw no cursor.
25961 In all other cases, we want a hollow box cursor. */
25962
25963 static enum text_cursor_kinds
25964 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25965 int *active_cursor)
25966 {
25967 struct frame *f = XFRAME (w->frame);
25968 struct buffer *b = XBUFFER (w->contents);
25969 int cursor_type = DEFAULT_CURSOR;
25970 Lisp_Object alt_cursor;
25971 int non_selected = 0;
25972
25973 *active_cursor = 1;
25974
25975 /* Echo area */
25976 if (cursor_in_echo_area
25977 && FRAME_HAS_MINIBUF_P (f)
25978 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25979 {
25980 if (w == XWINDOW (echo_area_window))
25981 {
25982 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25983 {
25984 *width = FRAME_CURSOR_WIDTH (f);
25985 return FRAME_DESIRED_CURSOR (f);
25986 }
25987 else
25988 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25989 }
25990
25991 *active_cursor = 0;
25992 non_selected = 1;
25993 }
25994
25995 /* Detect a nonselected window or nonselected frame. */
25996 else if (w != XWINDOW (f->selected_window)
25997 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25998 {
25999 *active_cursor = 0;
26000
26001 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26002 return NO_CURSOR;
26003
26004 non_selected = 1;
26005 }
26006
26007 /* Never display a cursor in a window in which cursor-type is nil. */
26008 if (NILP (BVAR (b, cursor_type)))
26009 return NO_CURSOR;
26010
26011 /* Get the normal cursor type for this window. */
26012 if (EQ (BVAR (b, cursor_type), Qt))
26013 {
26014 cursor_type = FRAME_DESIRED_CURSOR (f);
26015 *width = FRAME_CURSOR_WIDTH (f);
26016 }
26017 else
26018 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26019
26020 /* Use cursor-in-non-selected-windows instead
26021 for non-selected window or frame. */
26022 if (non_selected)
26023 {
26024 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26025 if (!EQ (Qt, alt_cursor))
26026 return get_specified_cursor_type (alt_cursor, width);
26027 /* t means modify the normal cursor type. */
26028 if (cursor_type == FILLED_BOX_CURSOR)
26029 cursor_type = HOLLOW_BOX_CURSOR;
26030 else if (cursor_type == BAR_CURSOR && *width > 1)
26031 --*width;
26032 return cursor_type;
26033 }
26034
26035 /* Use normal cursor if not blinked off. */
26036 if (!w->cursor_off_p)
26037 {
26038 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26039 {
26040 if (cursor_type == FILLED_BOX_CURSOR)
26041 {
26042 /* Using a block cursor on large images can be very annoying.
26043 So use a hollow cursor for "large" images.
26044 If image is not transparent (no mask), also use hollow cursor. */
26045 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26046 if (img != NULL && IMAGEP (img->spec))
26047 {
26048 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26049 where N = size of default frame font size.
26050 This should cover most of the "tiny" icons people may use. */
26051 if (!img->mask
26052 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26053 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26054 cursor_type = HOLLOW_BOX_CURSOR;
26055 }
26056 }
26057 else if (cursor_type != NO_CURSOR)
26058 {
26059 /* Display current only supports BOX and HOLLOW cursors for images.
26060 So for now, unconditionally use a HOLLOW cursor when cursor is
26061 not a solid box cursor. */
26062 cursor_type = HOLLOW_BOX_CURSOR;
26063 }
26064 }
26065 return cursor_type;
26066 }
26067
26068 /* Cursor is blinked off, so determine how to "toggle" it. */
26069
26070 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26071 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26072 return get_specified_cursor_type (XCDR (alt_cursor), width);
26073
26074 /* Then see if frame has specified a specific blink off cursor type. */
26075 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26076 {
26077 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26078 return FRAME_BLINK_OFF_CURSOR (f);
26079 }
26080
26081 #if 0
26082 /* Some people liked having a permanently visible blinking cursor,
26083 while others had very strong opinions against it. So it was
26084 decided to remove it. KFS 2003-09-03 */
26085
26086 /* Finally perform built-in cursor blinking:
26087 filled box <-> hollow box
26088 wide [h]bar <-> narrow [h]bar
26089 narrow [h]bar <-> no cursor
26090 other type <-> no cursor */
26091
26092 if (cursor_type == FILLED_BOX_CURSOR)
26093 return HOLLOW_BOX_CURSOR;
26094
26095 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26096 {
26097 *width = 1;
26098 return cursor_type;
26099 }
26100 #endif
26101
26102 return NO_CURSOR;
26103 }
26104
26105
26106 /* Notice when the text cursor of window W has been completely
26107 overwritten by a drawing operation that outputs glyphs in AREA
26108 starting at X0 and ending at X1 in the line starting at Y0 and
26109 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26110 the rest of the line after X0 has been written. Y coordinates
26111 are window-relative. */
26112
26113 static void
26114 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26115 int x0, int x1, int y0, int y1)
26116 {
26117 int cx0, cx1, cy0, cy1;
26118 struct glyph_row *row;
26119
26120 if (!w->phys_cursor_on_p)
26121 return;
26122 if (area != TEXT_AREA)
26123 return;
26124
26125 if (w->phys_cursor.vpos < 0
26126 || w->phys_cursor.vpos >= w->current_matrix->nrows
26127 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26128 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26129 return;
26130
26131 if (row->cursor_in_fringe_p)
26132 {
26133 row->cursor_in_fringe_p = 0;
26134 draw_fringe_bitmap (w, row, row->reversed_p);
26135 w->phys_cursor_on_p = 0;
26136 return;
26137 }
26138
26139 cx0 = w->phys_cursor.x;
26140 cx1 = cx0 + w->phys_cursor_width;
26141 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26142 return;
26143
26144 /* The cursor image will be completely removed from the
26145 screen if the output area intersects the cursor area in
26146 y-direction. When we draw in [y0 y1[, and some part of
26147 the cursor is at y < y0, that part must have been drawn
26148 before. When scrolling, the cursor is erased before
26149 actually scrolling, so we don't come here. When not
26150 scrolling, the rows above the old cursor row must have
26151 changed, and in this case these rows must have written
26152 over the cursor image.
26153
26154 Likewise if part of the cursor is below y1, with the
26155 exception of the cursor being in the first blank row at
26156 the buffer and window end because update_text_area
26157 doesn't draw that row. (Except when it does, but
26158 that's handled in update_text_area.) */
26159
26160 cy0 = w->phys_cursor.y;
26161 cy1 = cy0 + w->phys_cursor_height;
26162 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26163 return;
26164
26165 w->phys_cursor_on_p = 0;
26166 }
26167
26168 #endif /* HAVE_WINDOW_SYSTEM */
26169
26170 \f
26171 /************************************************************************
26172 Mouse Face
26173 ************************************************************************/
26174
26175 #ifdef HAVE_WINDOW_SYSTEM
26176
26177 /* EXPORT for RIF:
26178 Fix the display of area AREA of overlapping row ROW in window W
26179 with respect to the overlapping part OVERLAPS. */
26180
26181 void
26182 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26183 enum glyph_row_area area, int overlaps)
26184 {
26185 int i, x;
26186
26187 block_input ();
26188
26189 x = 0;
26190 for (i = 0; i < row->used[area];)
26191 {
26192 if (row->glyphs[area][i].overlaps_vertically_p)
26193 {
26194 int start = i, start_x = x;
26195
26196 do
26197 {
26198 x += row->glyphs[area][i].pixel_width;
26199 ++i;
26200 }
26201 while (i < row->used[area]
26202 && row->glyphs[area][i].overlaps_vertically_p);
26203
26204 draw_glyphs (w, start_x, row, area,
26205 start, i,
26206 DRAW_NORMAL_TEXT, overlaps);
26207 }
26208 else
26209 {
26210 x += row->glyphs[area][i].pixel_width;
26211 ++i;
26212 }
26213 }
26214
26215 unblock_input ();
26216 }
26217
26218
26219 /* EXPORT:
26220 Draw the cursor glyph of window W in glyph row ROW. See the
26221 comment of draw_glyphs for the meaning of HL. */
26222
26223 void
26224 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26225 enum draw_glyphs_face hl)
26226 {
26227 /* If cursor hpos is out of bounds, don't draw garbage. This can
26228 happen in mini-buffer windows when switching between echo area
26229 glyphs and mini-buffer. */
26230 if ((row->reversed_p
26231 ? (w->phys_cursor.hpos >= 0)
26232 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26233 {
26234 int on_p = w->phys_cursor_on_p;
26235 int x1;
26236 int hpos = w->phys_cursor.hpos;
26237
26238 /* When the window is hscrolled, cursor hpos can legitimately be
26239 out of bounds, but we draw the cursor at the corresponding
26240 window margin in that case. */
26241 if (!row->reversed_p && hpos < 0)
26242 hpos = 0;
26243 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26244 hpos = row->used[TEXT_AREA] - 1;
26245
26246 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26247 hl, 0);
26248 w->phys_cursor_on_p = on_p;
26249
26250 if (hl == DRAW_CURSOR)
26251 w->phys_cursor_width = x1 - w->phys_cursor.x;
26252 /* When we erase the cursor, and ROW is overlapped by other
26253 rows, make sure that these overlapping parts of other rows
26254 are redrawn. */
26255 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26256 {
26257 w->phys_cursor_width = x1 - w->phys_cursor.x;
26258
26259 if (row > w->current_matrix->rows
26260 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26261 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26262 OVERLAPS_ERASED_CURSOR);
26263
26264 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26265 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26266 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26267 OVERLAPS_ERASED_CURSOR);
26268 }
26269 }
26270 }
26271
26272
26273 /* EXPORT:
26274 Erase the image of a cursor of window W from the screen. */
26275
26276 void
26277 erase_phys_cursor (struct window *w)
26278 {
26279 struct frame *f = XFRAME (w->frame);
26280 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26281 int hpos = w->phys_cursor.hpos;
26282 int vpos = w->phys_cursor.vpos;
26283 int mouse_face_here_p = 0;
26284 struct glyph_matrix *active_glyphs = w->current_matrix;
26285 struct glyph_row *cursor_row;
26286 struct glyph *cursor_glyph;
26287 enum draw_glyphs_face hl;
26288
26289 /* No cursor displayed or row invalidated => nothing to do on the
26290 screen. */
26291 if (w->phys_cursor_type == NO_CURSOR)
26292 goto mark_cursor_off;
26293
26294 /* VPOS >= active_glyphs->nrows means that window has been resized.
26295 Don't bother to erase the cursor. */
26296 if (vpos >= active_glyphs->nrows)
26297 goto mark_cursor_off;
26298
26299 /* If row containing cursor is marked invalid, there is nothing we
26300 can do. */
26301 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26302 if (!cursor_row->enabled_p)
26303 goto mark_cursor_off;
26304
26305 /* If line spacing is > 0, old cursor may only be partially visible in
26306 window after split-window. So adjust visible height. */
26307 cursor_row->visible_height = min (cursor_row->visible_height,
26308 window_text_bottom_y (w) - cursor_row->y);
26309
26310 /* If row is completely invisible, don't attempt to delete a cursor which
26311 isn't there. This can happen if cursor is at top of a window, and
26312 we switch to a buffer with a header line in that window. */
26313 if (cursor_row->visible_height <= 0)
26314 goto mark_cursor_off;
26315
26316 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26317 if (cursor_row->cursor_in_fringe_p)
26318 {
26319 cursor_row->cursor_in_fringe_p = 0;
26320 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26321 goto mark_cursor_off;
26322 }
26323
26324 /* This can happen when the new row is shorter than the old one.
26325 In this case, either draw_glyphs or clear_end_of_line
26326 should have cleared the cursor. Note that we wouldn't be
26327 able to erase the cursor in this case because we don't have a
26328 cursor glyph at hand. */
26329 if ((cursor_row->reversed_p
26330 ? (w->phys_cursor.hpos < 0)
26331 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26332 goto mark_cursor_off;
26333
26334 /* When the window is hscrolled, cursor hpos can legitimately be out
26335 of bounds, but we draw the cursor at the corresponding window
26336 margin in that case. */
26337 if (!cursor_row->reversed_p && hpos < 0)
26338 hpos = 0;
26339 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26340 hpos = cursor_row->used[TEXT_AREA] - 1;
26341
26342 /* If the cursor is in the mouse face area, redisplay that when
26343 we clear the cursor. */
26344 if (! NILP (hlinfo->mouse_face_window)
26345 && coords_in_mouse_face_p (w, hpos, vpos)
26346 /* Don't redraw the cursor's spot in mouse face if it is at the
26347 end of a line (on a newline). The cursor appears there, but
26348 mouse highlighting does not. */
26349 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26350 mouse_face_here_p = 1;
26351
26352 /* Maybe clear the display under the cursor. */
26353 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26354 {
26355 int x, y, left_x;
26356 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26357 int width;
26358
26359 cursor_glyph = get_phys_cursor_glyph (w);
26360 if (cursor_glyph == NULL)
26361 goto mark_cursor_off;
26362
26363 width = cursor_glyph->pixel_width;
26364 left_x = window_box_left_offset (w, TEXT_AREA);
26365 x = w->phys_cursor.x;
26366 if (x < left_x)
26367 width -= left_x - x;
26368 width = min (width, window_box_width (w, TEXT_AREA) - x);
26369 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26370 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26371
26372 if (width > 0)
26373 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26374 }
26375
26376 /* Erase the cursor by redrawing the character underneath it. */
26377 if (mouse_face_here_p)
26378 hl = DRAW_MOUSE_FACE;
26379 else
26380 hl = DRAW_NORMAL_TEXT;
26381 draw_phys_cursor_glyph (w, cursor_row, hl);
26382
26383 mark_cursor_off:
26384 w->phys_cursor_on_p = 0;
26385 w->phys_cursor_type = NO_CURSOR;
26386 }
26387
26388
26389 /* EXPORT:
26390 Display or clear cursor of window W. If ON is zero, clear the
26391 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26392 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26393
26394 void
26395 display_and_set_cursor (struct window *w, bool on,
26396 int hpos, int vpos, int x, int y)
26397 {
26398 struct frame *f = XFRAME (w->frame);
26399 int new_cursor_type;
26400 int new_cursor_width;
26401 int active_cursor;
26402 struct glyph_row *glyph_row;
26403 struct glyph *glyph;
26404
26405 /* This is pointless on invisible frames, and dangerous on garbaged
26406 windows and frames; in the latter case, the frame or window may
26407 be in the midst of changing its size, and x and y may be off the
26408 window. */
26409 if (! FRAME_VISIBLE_P (f)
26410 || FRAME_GARBAGED_P (f)
26411 || vpos >= w->current_matrix->nrows
26412 || hpos >= w->current_matrix->matrix_w)
26413 return;
26414
26415 /* If cursor is off and we want it off, return quickly. */
26416 if (!on && !w->phys_cursor_on_p)
26417 return;
26418
26419 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26420 /* If cursor row is not enabled, we don't really know where to
26421 display the cursor. */
26422 if (!glyph_row->enabled_p)
26423 {
26424 w->phys_cursor_on_p = 0;
26425 return;
26426 }
26427
26428 glyph = NULL;
26429 if (!glyph_row->exact_window_width_line_p
26430 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26431 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26432
26433 eassert (input_blocked_p ());
26434
26435 /* Set new_cursor_type to the cursor we want to be displayed. */
26436 new_cursor_type = get_window_cursor_type (w, glyph,
26437 &new_cursor_width, &active_cursor);
26438
26439 /* If cursor is currently being shown and we don't want it to be or
26440 it is in the wrong place, or the cursor type is not what we want,
26441 erase it. */
26442 if (w->phys_cursor_on_p
26443 && (!on
26444 || w->phys_cursor.x != x
26445 || w->phys_cursor.y != y
26446 || new_cursor_type != w->phys_cursor_type
26447 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26448 && new_cursor_width != w->phys_cursor_width)))
26449 erase_phys_cursor (w);
26450
26451 /* Don't check phys_cursor_on_p here because that flag is only set
26452 to zero in some cases where we know that the cursor has been
26453 completely erased, to avoid the extra work of erasing the cursor
26454 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26455 still not be visible, or it has only been partly erased. */
26456 if (on)
26457 {
26458 w->phys_cursor_ascent = glyph_row->ascent;
26459 w->phys_cursor_height = glyph_row->height;
26460
26461 /* Set phys_cursor_.* before x_draw_.* is called because some
26462 of them may need the information. */
26463 w->phys_cursor.x = x;
26464 w->phys_cursor.y = glyph_row->y;
26465 w->phys_cursor.hpos = hpos;
26466 w->phys_cursor.vpos = vpos;
26467 }
26468
26469 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26470 new_cursor_type, new_cursor_width,
26471 on, active_cursor);
26472 }
26473
26474
26475 /* Switch the display of W's cursor on or off, according to the value
26476 of ON. */
26477
26478 static void
26479 update_window_cursor (struct window *w, bool on)
26480 {
26481 /* Don't update cursor in windows whose frame is in the process
26482 of being deleted. */
26483 if (w->current_matrix)
26484 {
26485 int hpos = w->phys_cursor.hpos;
26486 int vpos = w->phys_cursor.vpos;
26487 struct glyph_row *row;
26488
26489 if (vpos >= w->current_matrix->nrows
26490 || hpos >= w->current_matrix->matrix_w)
26491 return;
26492
26493 row = MATRIX_ROW (w->current_matrix, vpos);
26494
26495 /* When the window is hscrolled, cursor hpos can legitimately be
26496 out of bounds, but we draw the cursor at the corresponding
26497 window margin in that case. */
26498 if (!row->reversed_p && hpos < 0)
26499 hpos = 0;
26500 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26501 hpos = row->used[TEXT_AREA] - 1;
26502
26503 block_input ();
26504 display_and_set_cursor (w, on, hpos, vpos,
26505 w->phys_cursor.x, w->phys_cursor.y);
26506 unblock_input ();
26507 }
26508 }
26509
26510
26511 /* Call update_window_cursor with parameter ON_P on all leaf windows
26512 in the window tree rooted at W. */
26513
26514 static void
26515 update_cursor_in_window_tree (struct window *w, bool on_p)
26516 {
26517 while (w)
26518 {
26519 if (WINDOWP (w->contents))
26520 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26521 else
26522 update_window_cursor (w, on_p);
26523
26524 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26525 }
26526 }
26527
26528
26529 /* EXPORT:
26530 Display the cursor on window W, or clear it, according to ON_P.
26531 Don't change the cursor's position. */
26532
26533 void
26534 x_update_cursor (struct frame *f, bool on_p)
26535 {
26536 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26537 }
26538
26539
26540 /* EXPORT:
26541 Clear the cursor of window W to background color, and mark the
26542 cursor as not shown. This is used when the text where the cursor
26543 is about to be rewritten. */
26544
26545 void
26546 x_clear_cursor (struct window *w)
26547 {
26548 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26549 update_window_cursor (w, 0);
26550 }
26551
26552 #endif /* HAVE_WINDOW_SYSTEM */
26553
26554 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26555 and MSDOS. */
26556 static void
26557 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26558 int start_hpos, int end_hpos,
26559 enum draw_glyphs_face draw)
26560 {
26561 #ifdef HAVE_WINDOW_SYSTEM
26562 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26563 {
26564 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26565 return;
26566 }
26567 #endif
26568 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26569 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26570 #endif
26571 }
26572
26573 /* Display the active region described by mouse_face_* according to DRAW. */
26574
26575 static void
26576 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26577 {
26578 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26579 struct frame *f = XFRAME (WINDOW_FRAME (w));
26580
26581 if (/* If window is in the process of being destroyed, don't bother
26582 to do anything. */
26583 w->current_matrix != NULL
26584 /* Don't update mouse highlight if hidden */
26585 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26586 /* Recognize when we are called to operate on rows that don't exist
26587 anymore. This can happen when a window is split. */
26588 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26589 {
26590 int phys_cursor_on_p = w->phys_cursor_on_p;
26591 struct glyph_row *row, *first, *last;
26592
26593 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26594 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26595
26596 for (row = first; row <= last && row->enabled_p; ++row)
26597 {
26598 int start_hpos, end_hpos, start_x;
26599
26600 /* For all but the first row, the highlight starts at column 0. */
26601 if (row == first)
26602 {
26603 /* R2L rows have BEG and END in reversed order, but the
26604 screen drawing geometry is always left to right. So
26605 we need to mirror the beginning and end of the
26606 highlighted area in R2L rows. */
26607 if (!row->reversed_p)
26608 {
26609 start_hpos = hlinfo->mouse_face_beg_col;
26610 start_x = hlinfo->mouse_face_beg_x;
26611 }
26612 else if (row == last)
26613 {
26614 start_hpos = hlinfo->mouse_face_end_col;
26615 start_x = hlinfo->mouse_face_end_x;
26616 }
26617 else
26618 {
26619 start_hpos = 0;
26620 start_x = 0;
26621 }
26622 }
26623 else if (row->reversed_p && row == last)
26624 {
26625 start_hpos = hlinfo->mouse_face_end_col;
26626 start_x = hlinfo->mouse_face_end_x;
26627 }
26628 else
26629 {
26630 start_hpos = 0;
26631 start_x = 0;
26632 }
26633
26634 if (row == last)
26635 {
26636 if (!row->reversed_p)
26637 end_hpos = hlinfo->mouse_face_end_col;
26638 else if (row == first)
26639 end_hpos = hlinfo->mouse_face_beg_col;
26640 else
26641 {
26642 end_hpos = row->used[TEXT_AREA];
26643 if (draw == DRAW_NORMAL_TEXT)
26644 row->fill_line_p = 1; /* Clear to end of line */
26645 }
26646 }
26647 else if (row->reversed_p && row == first)
26648 end_hpos = hlinfo->mouse_face_beg_col;
26649 else
26650 {
26651 end_hpos = row->used[TEXT_AREA];
26652 if (draw == DRAW_NORMAL_TEXT)
26653 row->fill_line_p = 1; /* Clear to end of line */
26654 }
26655
26656 if (end_hpos > start_hpos)
26657 {
26658 draw_row_with_mouse_face (w, start_x, row,
26659 start_hpos, end_hpos, draw);
26660
26661 row->mouse_face_p
26662 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26663 }
26664 }
26665
26666 #ifdef HAVE_WINDOW_SYSTEM
26667 /* When we've written over the cursor, arrange for it to
26668 be displayed again. */
26669 if (FRAME_WINDOW_P (f)
26670 && phys_cursor_on_p && !w->phys_cursor_on_p)
26671 {
26672 int hpos = w->phys_cursor.hpos;
26673
26674 /* When the window is hscrolled, cursor hpos can legitimately be
26675 out of bounds, but we draw the cursor at the corresponding
26676 window margin in that case. */
26677 if (!row->reversed_p && hpos < 0)
26678 hpos = 0;
26679 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26680 hpos = row->used[TEXT_AREA] - 1;
26681
26682 block_input ();
26683 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26684 w->phys_cursor.x, w->phys_cursor.y);
26685 unblock_input ();
26686 }
26687 #endif /* HAVE_WINDOW_SYSTEM */
26688 }
26689
26690 #ifdef HAVE_WINDOW_SYSTEM
26691 /* Change the mouse cursor. */
26692 if (FRAME_WINDOW_P (f))
26693 {
26694 if (draw == DRAW_NORMAL_TEXT
26695 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26696 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26697 else if (draw == DRAW_MOUSE_FACE)
26698 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26699 else
26700 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26701 }
26702 #endif /* HAVE_WINDOW_SYSTEM */
26703 }
26704
26705 /* EXPORT:
26706 Clear out the mouse-highlighted active region.
26707 Redraw it un-highlighted first. Value is non-zero if mouse
26708 face was actually drawn unhighlighted. */
26709
26710 int
26711 clear_mouse_face (Mouse_HLInfo *hlinfo)
26712 {
26713 int cleared = 0;
26714
26715 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26716 {
26717 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26718 cleared = 1;
26719 }
26720
26721 reset_mouse_highlight (hlinfo);
26722 return cleared;
26723 }
26724
26725 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26726 within the mouse face on that window. */
26727 static int
26728 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26729 {
26730 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26731
26732 /* Quickly resolve the easy cases. */
26733 if (!(WINDOWP (hlinfo->mouse_face_window)
26734 && XWINDOW (hlinfo->mouse_face_window) == w))
26735 return 0;
26736 if (vpos < hlinfo->mouse_face_beg_row
26737 || vpos > hlinfo->mouse_face_end_row)
26738 return 0;
26739 if (vpos > hlinfo->mouse_face_beg_row
26740 && vpos < hlinfo->mouse_face_end_row)
26741 return 1;
26742
26743 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26744 {
26745 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26746 {
26747 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26748 return 1;
26749 }
26750 else if ((vpos == hlinfo->mouse_face_beg_row
26751 && hpos >= hlinfo->mouse_face_beg_col)
26752 || (vpos == hlinfo->mouse_face_end_row
26753 && hpos < hlinfo->mouse_face_end_col))
26754 return 1;
26755 }
26756 else
26757 {
26758 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26759 {
26760 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26761 return 1;
26762 }
26763 else if ((vpos == hlinfo->mouse_face_beg_row
26764 && hpos <= hlinfo->mouse_face_beg_col)
26765 || (vpos == hlinfo->mouse_face_end_row
26766 && hpos > hlinfo->mouse_face_end_col))
26767 return 1;
26768 }
26769 return 0;
26770 }
26771
26772
26773 /* EXPORT:
26774 Non-zero if physical cursor of window W is within mouse face. */
26775
26776 int
26777 cursor_in_mouse_face_p (struct window *w)
26778 {
26779 int hpos = w->phys_cursor.hpos;
26780 int vpos = w->phys_cursor.vpos;
26781 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26782
26783 /* When the window is hscrolled, cursor hpos can legitimately be out
26784 of bounds, but we draw the cursor at the corresponding window
26785 margin in that case. */
26786 if (!row->reversed_p && hpos < 0)
26787 hpos = 0;
26788 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26789 hpos = row->used[TEXT_AREA] - 1;
26790
26791 return coords_in_mouse_face_p (w, hpos, vpos);
26792 }
26793
26794
26795 \f
26796 /* Find the glyph rows START_ROW and END_ROW of window W that display
26797 characters between buffer positions START_CHARPOS and END_CHARPOS
26798 (excluding END_CHARPOS). DISP_STRING is a display string that
26799 covers these buffer positions. This is similar to
26800 row_containing_pos, but is more accurate when bidi reordering makes
26801 buffer positions change non-linearly with glyph rows. */
26802 static void
26803 rows_from_pos_range (struct window *w,
26804 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26805 Lisp_Object disp_string,
26806 struct glyph_row **start, struct glyph_row **end)
26807 {
26808 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26809 int last_y = window_text_bottom_y (w);
26810 struct glyph_row *row;
26811
26812 *start = NULL;
26813 *end = NULL;
26814
26815 while (!first->enabled_p
26816 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26817 first++;
26818
26819 /* Find the START row. */
26820 for (row = first;
26821 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26822 row++)
26823 {
26824 /* A row can potentially be the START row if the range of the
26825 characters it displays intersects the range
26826 [START_CHARPOS..END_CHARPOS). */
26827 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26828 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26829 /* See the commentary in row_containing_pos, for the
26830 explanation of the complicated way to check whether
26831 some position is beyond the end of the characters
26832 displayed by a row. */
26833 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26834 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26835 && !row->ends_at_zv_p
26836 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26837 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26838 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26839 && !row->ends_at_zv_p
26840 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26841 {
26842 /* Found a candidate row. Now make sure at least one of the
26843 glyphs it displays has a charpos from the range
26844 [START_CHARPOS..END_CHARPOS).
26845
26846 This is not obvious because bidi reordering could make
26847 buffer positions of a row be 1,2,3,102,101,100, and if we
26848 want to highlight characters in [50..60), we don't want
26849 this row, even though [50..60) does intersect [1..103),
26850 the range of character positions given by the row's start
26851 and end positions. */
26852 struct glyph *g = row->glyphs[TEXT_AREA];
26853 struct glyph *e = g + row->used[TEXT_AREA];
26854
26855 while (g < e)
26856 {
26857 if (((BUFFERP (g->object) || INTEGERP (g->object))
26858 && start_charpos <= g->charpos && g->charpos < end_charpos)
26859 /* A glyph that comes from DISP_STRING is by
26860 definition to be highlighted. */
26861 || EQ (g->object, disp_string))
26862 *start = row;
26863 g++;
26864 }
26865 if (*start)
26866 break;
26867 }
26868 }
26869
26870 /* Find the END row. */
26871 if (!*start
26872 /* If the last row is partially visible, start looking for END
26873 from that row, instead of starting from FIRST. */
26874 && !(row->enabled_p
26875 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26876 row = first;
26877 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26878 {
26879 struct glyph_row *next = row + 1;
26880 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26881
26882 if (!next->enabled_p
26883 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26884 /* The first row >= START whose range of displayed characters
26885 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26886 is the row END + 1. */
26887 || (start_charpos < next_start
26888 && end_charpos < next_start)
26889 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26890 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26891 && !next->ends_at_zv_p
26892 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26893 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26894 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26895 && !next->ends_at_zv_p
26896 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26897 {
26898 *end = row;
26899 break;
26900 }
26901 else
26902 {
26903 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26904 but none of the characters it displays are in the range, it is
26905 also END + 1. */
26906 struct glyph *g = next->glyphs[TEXT_AREA];
26907 struct glyph *s = g;
26908 struct glyph *e = g + next->used[TEXT_AREA];
26909
26910 while (g < e)
26911 {
26912 if (((BUFFERP (g->object) || INTEGERP (g->object))
26913 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26914 /* If the buffer position of the first glyph in
26915 the row is equal to END_CHARPOS, it means
26916 the last character to be highlighted is the
26917 newline of ROW, and we must consider NEXT as
26918 END, not END+1. */
26919 || (((!next->reversed_p && g == s)
26920 || (next->reversed_p && g == e - 1))
26921 && (g->charpos == end_charpos
26922 /* Special case for when NEXT is an
26923 empty line at ZV. */
26924 || (g->charpos == -1
26925 && !row->ends_at_zv_p
26926 && next_start == end_charpos)))))
26927 /* A glyph that comes from DISP_STRING is by
26928 definition to be highlighted. */
26929 || EQ (g->object, disp_string))
26930 break;
26931 g++;
26932 }
26933 if (g == e)
26934 {
26935 *end = row;
26936 break;
26937 }
26938 /* The first row that ends at ZV must be the last to be
26939 highlighted. */
26940 else if (next->ends_at_zv_p)
26941 {
26942 *end = next;
26943 break;
26944 }
26945 }
26946 }
26947 }
26948
26949 /* This function sets the mouse_face_* elements of HLINFO, assuming
26950 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26951 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26952 for the overlay or run of text properties specifying the mouse
26953 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26954 before-string and after-string that must also be highlighted.
26955 DISP_STRING, if non-nil, is a display string that may cover some
26956 or all of the highlighted text. */
26957
26958 static void
26959 mouse_face_from_buffer_pos (Lisp_Object window,
26960 Mouse_HLInfo *hlinfo,
26961 ptrdiff_t mouse_charpos,
26962 ptrdiff_t start_charpos,
26963 ptrdiff_t end_charpos,
26964 Lisp_Object before_string,
26965 Lisp_Object after_string,
26966 Lisp_Object disp_string)
26967 {
26968 struct window *w = XWINDOW (window);
26969 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26970 struct glyph_row *r1, *r2;
26971 struct glyph *glyph, *end;
26972 ptrdiff_t ignore, pos;
26973 int x;
26974
26975 eassert (NILP (disp_string) || STRINGP (disp_string));
26976 eassert (NILP (before_string) || STRINGP (before_string));
26977 eassert (NILP (after_string) || STRINGP (after_string));
26978
26979 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26980 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26981 if (r1 == NULL)
26982 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
26983 /* If the before-string or display-string contains newlines,
26984 rows_from_pos_range skips to its last row. Move back. */
26985 if (!NILP (before_string) || !NILP (disp_string))
26986 {
26987 struct glyph_row *prev;
26988 while ((prev = r1 - 1, prev >= first)
26989 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26990 && prev->used[TEXT_AREA] > 0)
26991 {
26992 struct glyph *beg = prev->glyphs[TEXT_AREA];
26993 glyph = beg + prev->used[TEXT_AREA];
26994 while (--glyph >= beg && INTEGERP (glyph->object));
26995 if (glyph < beg
26996 || !(EQ (glyph->object, before_string)
26997 || EQ (glyph->object, disp_string)))
26998 break;
26999 r1 = prev;
27000 }
27001 }
27002 if (r2 == NULL)
27003 {
27004 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27005 hlinfo->mouse_face_past_end = 1;
27006 }
27007 else if (!NILP (after_string))
27008 {
27009 /* If the after-string has newlines, advance to its last row. */
27010 struct glyph_row *next;
27011 struct glyph_row *last
27012 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27013
27014 for (next = r2 + 1;
27015 next <= last
27016 && next->used[TEXT_AREA] > 0
27017 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27018 ++next)
27019 r2 = next;
27020 }
27021 /* The rest of the display engine assumes that mouse_face_beg_row is
27022 either above mouse_face_end_row or identical to it. But with
27023 bidi-reordered continued lines, the row for START_CHARPOS could
27024 be below the row for END_CHARPOS. If so, swap the rows and store
27025 them in correct order. */
27026 if (r1->y > r2->y)
27027 {
27028 struct glyph_row *tem = r2;
27029
27030 r2 = r1;
27031 r1 = tem;
27032 }
27033
27034 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27035 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27036
27037 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27038 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27039 could be anywhere in the row and in any order. The strategy
27040 below is to find the leftmost and the rightmost glyph that
27041 belongs to either of these 3 strings, or whose position is
27042 between START_CHARPOS and END_CHARPOS, and highlight all the
27043 glyphs between those two. This may cover more than just the text
27044 between START_CHARPOS and END_CHARPOS if the range of characters
27045 strides the bidi level boundary, e.g. if the beginning is in R2L
27046 text while the end is in L2R text or vice versa. */
27047 if (!r1->reversed_p)
27048 {
27049 /* This row is in a left to right paragraph. Scan it left to
27050 right. */
27051 glyph = r1->glyphs[TEXT_AREA];
27052 end = glyph + r1->used[TEXT_AREA];
27053 x = r1->x;
27054
27055 /* Skip truncation glyphs at the start of the glyph row. */
27056 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27057 for (; glyph < end
27058 && INTEGERP (glyph->object)
27059 && glyph->charpos < 0;
27060 ++glyph)
27061 x += glyph->pixel_width;
27062
27063 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27064 or DISP_STRING, and the first glyph from buffer whose
27065 position is between START_CHARPOS and END_CHARPOS. */
27066 for (; glyph < end
27067 && !INTEGERP (glyph->object)
27068 && !EQ (glyph->object, disp_string)
27069 && !(BUFFERP (glyph->object)
27070 && (glyph->charpos >= start_charpos
27071 && glyph->charpos < end_charpos));
27072 ++glyph)
27073 {
27074 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27075 are present at buffer positions between START_CHARPOS and
27076 END_CHARPOS, or if they come from an overlay. */
27077 if (EQ (glyph->object, before_string))
27078 {
27079 pos = string_buffer_position (before_string,
27080 start_charpos);
27081 /* If pos == 0, it means before_string came from an
27082 overlay, not from a buffer position. */
27083 if (!pos || (pos >= start_charpos && pos < end_charpos))
27084 break;
27085 }
27086 else if (EQ (glyph->object, after_string))
27087 {
27088 pos = string_buffer_position (after_string, end_charpos);
27089 if (!pos || (pos >= start_charpos && pos < end_charpos))
27090 break;
27091 }
27092 x += glyph->pixel_width;
27093 }
27094 hlinfo->mouse_face_beg_x = x;
27095 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27096 }
27097 else
27098 {
27099 /* This row is in a right to left paragraph. Scan it right to
27100 left. */
27101 struct glyph *g;
27102
27103 end = r1->glyphs[TEXT_AREA] - 1;
27104 glyph = end + r1->used[TEXT_AREA];
27105
27106 /* Skip truncation glyphs at the start of the glyph row. */
27107 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27108 for (; glyph > end
27109 && INTEGERP (glyph->object)
27110 && glyph->charpos < 0;
27111 --glyph)
27112 ;
27113
27114 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27115 or DISP_STRING, and the first glyph from buffer whose
27116 position is between START_CHARPOS and END_CHARPOS. */
27117 for (; glyph > end
27118 && !INTEGERP (glyph->object)
27119 && !EQ (glyph->object, disp_string)
27120 && !(BUFFERP (glyph->object)
27121 && (glyph->charpos >= start_charpos
27122 && glyph->charpos < end_charpos));
27123 --glyph)
27124 {
27125 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27126 are present at buffer positions between START_CHARPOS and
27127 END_CHARPOS, or if they come from an overlay. */
27128 if (EQ (glyph->object, before_string))
27129 {
27130 pos = string_buffer_position (before_string, start_charpos);
27131 /* If pos == 0, it means before_string came from an
27132 overlay, not from a buffer position. */
27133 if (!pos || (pos >= start_charpos && pos < end_charpos))
27134 break;
27135 }
27136 else if (EQ (glyph->object, after_string))
27137 {
27138 pos = string_buffer_position (after_string, end_charpos);
27139 if (!pos || (pos >= start_charpos && pos < end_charpos))
27140 break;
27141 }
27142 }
27143
27144 glyph++; /* first glyph to the right of the highlighted area */
27145 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27146 x += g->pixel_width;
27147 hlinfo->mouse_face_beg_x = x;
27148 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27149 }
27150
27151 /* If the highlight ends in a different row, compute GLYPH and END
27152 for the end row. Otherwise, reuse the values computed above for
27153 the row where the highlight begins. */
27154 if (r2 != r1)
27155 {
27156 if (!r2->reversed_p)
27157 {
27158 glyph = r2->glyphs[TEXT_AREA];
27159 end = glyph + r2->used[TEXT_AREA];
27160 x = r2->x;
27161 }
27162 else
27163 {
27164 end = r2->glyphs[TEXT_AREA] - 1;
27165 glyph = end + r2->used[TEXT_AREA];
27166 }
27167 }
27168
27169 if (!r2->reversed_p)
27170 {
27171 /* Skip truncation and continuation glyphs near the end of the
27172 row, and also blanks and stretch glyphs inserted by
27173 extend_face_to_end_of_line. */
27174 while (end > glyph
27175 && INTEGERP ((end - 1)->object))
27176 --end;
27177 /* Scan the rest of the glyph row from the end, looking for the
27178 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27179 DISP_STRING, or whose position is between START_CHARPOS
27180 and END_CHARPOS */
27181 for (--end;
27182 end > glyph
27183 && !INTEGERP (end->object)
27184 && !EQ (end->object, disp_string)
27185 && !(BUFFERP (end->object)
27186 && (end->charpos >= start_charpos
27187 && end->charpos < end_charpos));
27188 --end)
27189 {
27190 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27191 are present at buffer positions between START_CHARPOS and
27192 END_CHARPOS, or if they come from an overlay. */
27193 if (EQ (end->object, before_string))
27194 {
27195 pos = string_buffer_position (before_string, start_charpos);
27196 if (!pos || (pos >= start_charpos && pos < end_charpos))
27197 break;
27198 }
27199 else if (EQ (end->object, after_string))
27200 {
27201 pos = string_buffer_position (after_string, end_charpos);
27202 if (!pos || (pos >= start_charpos && pos < end_charpos))
27203 break;
27204 }
27205 }
27206 /* Find the X coordinate of the last glyph to be highlighted. */
27207 for (; glyph <= end; ++glyph)
27208 x += glyph->pixel_width;
27209
27210 hlinfo->mouse_face_end_x = x;
27211 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27212 }
27213 else
27214 {
27215 /* Skip truncation and continuation glyphs near the end of the
27216 row, and also blanks and stretch glyphs inserted by
27217 extend_face_to_end_of_line. */
27218 x = r2->x;
27219 end++;
27220 while (end < glyph
27221 && INTEGERP (end->object))
27222 {
27223 x += end->pixel_width;
27224 ++end;
27225 }
27226 /* Scan the rest of the glyph row from the end, looking for the
27227 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27228 DISP_STRING, or whose position is between START_CHARPOS
27229 and END_CHARPOS */
27230 for ( ;
27231 end < glyph
27232 && !INTEGERP (end->object)
27233 && !EQ (end->object, disp_string)
27234 && !(BUFFERP (end->object)
27235 && (end->charpos >= start_charpos
27236 && end->charpos < end_charpos));
27237 ++end)
27238 {
27239 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27240 are present at buffer positions between START_CHARPOS and
27241 END_CHARPOS, or if they come from an overlay. */
27242 if (EQ (end->object, before_string))
27243 {
27244 pos = string_buffer_position (before_string, start_charpos);
27245 if (!pos || (pos >= start_charpos && pos < end_charpos))
27246 break;
27247 }
27248 else if (EQ (end->object, after_string))
27249 {
27250 pos = string_buffer_position (after_string, end_charpos);
27251 if (!pos || (pos >= start_charpos && pos < end_charpos))
27252 break;
27253 }
27254 x += end->pixel_width;
27255 }
27256 /* If we exited the above loop because we arrived at the last
27257 glyph of the row, and its buffer position is still not in
27258 range, it means the last character in range is the preceding
27259 newline. Bump the end column and x values to get past the
27260 last glyph. */
27261 if (end == glyph
27262 && BUFFERP (end->object)
27263 && (end->charpos < start_charpos
27264 || end->charpos >= end_charpos))
27265 {
27266 x += end->pixel_width;
27267 ++end;
27268 }
27269 hlinfo->mouse_face_end_x = x;
27270 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27271 }
27272
27273 hlinfo->mouse_face_window = window;
27274 hlinfo->mouse_face_face_id
27275 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27276 mouse_charpos + 1,
27277 !hlinfo->mouse_face_hidden, -1);
27278 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27279 }
27280
27281 /* The following function is not used anymore (replaced with
27282 mouse_face_from_string_pos), but I leave it here for the time
27283 being, in case someone would. */
27284
27285 #if 0 /* not used */
27286
27287 /* Find the position of the glyph for position POS in OBJECT in
27288 window W's current matrix, and return in *X, *Y the pixel
27289 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27290
27291 RIGHT_P non-zero means return the position of the right edge of the
27292 glyph, RIGHT_P zero means return the left edge position.
27293
27294 If no glyph for POS exists in the matrix, return the position of
27295 the glyph with the next smaller position that is in the matrix, if
27296 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27297 exists in the matrix, return the position of the glyph with the
27298 next larger position in OBJECT.
27299
27300 Value is non-zero if a glyph was found. */
27301
27302 static int
27303 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27304 int *hpos, int *vpos, int *x, int *y, int right_p)
27305 {
27306 int yb = window_text_bottom_y (w);
27307 struct glyph_row *r;
27308 struct glyph *best_glyph = NULL;
27309 struct glyph_row *best_row = NULL;
27310 int best_x = 0;
27311
27312 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27313 r->enabled_p && r->y < yb;
27314 ++r)
27315 {
27316 struct glyph *g = r->glyphs[TEXT_AREA];
27317 struct glyph *e = g + r->used[TEXT_AREA];
27318 int gx;
27319
27320 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27321 if (EQ (g->object, object))
27322 {
27323 if (g->charpos == pos)
27324 {
27325 best_glyph = g;
27326 best_x = gx;
27327 best_row = r;
27328 goto found;
27329 }
27330 else if (best_glyph == NULL
27331 || ((eabs (g->charpos - pos)
27332 < eabs (best_glyph->charpos - pos))
27333 && (right_p
27334 ? g->charpos < pos
27335 : g->charpos > pos)))
27336 {
27337 best_glyph = g;
27338 best_x = gx;
27339 best_row = r;
27340 }
27341 }
27342 }
27343
27344 found:
27345
27346 if (best_glyph)
27347 {
27348 *x = best_x;
27349 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27350
27351 if (right_p)
27352 {
27353 *x += best_glyph->pixel_width;
27354 ++*hpos;
27355 }
27356
27357 *y = best_row->y;
27358 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27359 }
27360
27361 return best_glyph != NULL;
27362 }
27363 #endif /* not used */
27364
27365 /* Find the positions of the first and the last glyphs in window W's
27366 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27367 (assumed to be a string), and return in HLINFO's mouse_face_*
27368 members the pixel and column/row coordinates of those glyphs. */
27369
27370 static void
27371 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27372 Lisp_Object object,
27373 ptrdiff_t startpos, ptrdiff_t endpos)
27374 {
27375 int yb = window_text_bottom_y (w);
27376 struct glyph_row *r;
27377 struct glyph *g, *e;
27378 int gx;
27379 int found = 0;
27380
27381 /* Find the glyph row with at least one position in the range
27382 [STARTPOS..ENDPOS], and the first glyph in that row whose
27383 position belongs to that range. */
27384 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27385 r->enabled_p && r->y < yb;
27386 ++r)
27387 {
27388 if (!r->reversed_p)
27389 {
27390 g = r->glyphs[TEXT_AREA];
27391 e = g + r->used[TEXT_AREA];
27392 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27393 if (EQ (g->object, object)
27394 && startpos <= g->charpos && g->charpos <= endpos)
27395 {
27396 hlinfo->mouse_face_beg_row
27397 = MATRIX_ROW_VPOS (r, w->current_matrix);
27398 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27399 hlinfo->mouse_face_beg_x = gx;
27400 found = 1;
27401 break;
27402 }
27403 }
27404 else
27405 {
27406 struct glyph *g1;
27407
27408 e = r->glyphs[TEXT_AREA];
27409 g = e + r->used[TEXT_AREA];
27410 for ( ; g > e; --g)
27411 if (EQ ((g-1)->object, object)
27412 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27413 {
27414 hlinfo->mouse_face_beg_row
27415 = MATRIX_ROW_VPOS (r, w->current_matrix);
27416 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27417 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27418 gx += g1->pixel_width;
27419 hlinfo->mouse_face_beg_x = gx;
27420 found = 1;
27421 break;
27422 }
27423 }
27424 if (found)
27425 break;
27426 }
27427
27428 if (!found)
27429 return;
27430
27431 /* Starting with the next row, look for the first row which does NOT
27432 include any glyphs whose positions are in the range. */
27433 for (++r; r->enabled_p && r->y < yb; ++r)
27434 {
27435 g = r->glyphs[TEXT_AREA];
27436 e = g + r->used[TEXT_AREA];
27437 found = 0;
27438 for ( ; g < e; ++g)
27439 if (EQ (g->object, object)
27440 && startpos <= g->charpos && g->charpos <= endpos)
27441 {
27442 found = 1;
27443 break;
27444 }
27445 if (!found)
27446 break;
27447 }
27448
27449 /* The highlighted region ends on the previous row. */
27450 r--;
27451
27452 /* Set the end row. */
27453 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27454
27455 /* Compute and set the end column and the end column's horizontal
27456 pixel coordinate. */
27457 if (!r->reversed_p)
27458 {
27459 g = r->glyphs[TEXT_AREA];
27460 e = g + r->used[TEXT_AREA];
27461 for ( ; e > g; --e)
27462 if (EQ ((e-1)->object, object)
27463 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27464 break;
27465 hlinfo->mouse_face_end_col = e - g;
27466
27467 for (gx = r->x; g < e; ++g)
27468 gx += g->pixel_width;
27469 hlinfo->mouse_face_end_x = gx;
27470 }
27471 else
27472 {
27473 e = r->glyphs[TEXT_AREA];
27474 g = e + r->used[TEXT_AREA];
27475 for (gx = r->x ; e < g; ++e)
27476 {
27477 if (EQ (e->object, object)
27478 && startpos <= e->charpos && e->charpos <= endpos)
27479 break;
27480 gx += e->pixel_width;
27481 }
27482 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27483 hlinfo->mouse_face_end_x = gx;
27484 }
27485 }
27486
27487 #ifdef HAVE_WINDOW_SYSTEM
27488
27489 /* See if position X, Y is within a hot-spot of an image. */
27490
27491 static int
27492 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27493 {
27494 if (!CONSP (hot_spot))
27495 return 0;
27496
27497 if (EQ (XCAR (hot_spot), Qrect))
27498 {
27499 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27500 Lisp_Object rect = XCDR (hot_spot);
27501 Lisp_Object tem;
27502 if (!CONSP (rect))
27503 return 0;
27504 if (!CONSP (XCAR (rect)))
27505 return 0;
27506 if (!CONSP (XCDR (rect)))
27507 return 0;
27508 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27509 return 0;
27510 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27511 return 0;
27512 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27513 return 0;
27514 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27515 return 0;
27516 return 1;
27517 }
27518 else if (EQ (XCAR (hot_spot), Qcircle))
27519 {
27520 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27521 Lisp_Object circ = XCDR (hot_spot);
27522 Lisp_Object lr, lx0, ly0;
27523 if (CONSP (circ)
27524 && CONSP (XCAR (circ))
27525 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27526 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27527 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27528 {
27529 double r = XFLOATINT (lr);
27530 double dx = XINT (lx0) - x;
27531 double dy = XINT (ly0) - y;
27532 return (dx * dx + dy * dy <= r * r);
27533 }
27534 }
27535 else if (EQ (XCAR (hot_spot), Qpoly))
27536 {
27537 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27538 if (VECTORP (XCDR (hot_spot)))
27539 {
27540 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27541 Lisp_Object *poly = v->contents;
27542 ptrdiff_t n = v->header.size;
27543 ptrdiff_t i;
27544 int inside = 0;
27545 Lisp_Object lx, ly;
27546 int x0, y0;
27547
27548 /* Need an even number of coordinates, and at least 3 edges. */
27549 if (n < 6 || n & 1)
27550 return 0;
27551
27552 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27553 If count is odd, we are inside polygon. Pixels on edges
27554 may or may not be included depending on actual geometry of the
27555 polygon. */
27556 if ((lx = poly[n-2], !INTEGERP (lx))
27557 || (ly = poly[n-1], !INTEGERP (lx)))
27558 return 0;
27559 x0 = XINT (lx), y0 = XINT (ly);
27560 for (i = 0; i < n; i += 2)
27561 {
27562 int x1 = x0, y1 = y0;
27563 if ((lx = poly[i], !INTEGERP (lx))
27564 || (ly = poly[i+1], !INTEGERP (ly)))
27565 return 0;
27566 x0 = XINT (lx), y0 = XINT (ly);
27567
27568 /* Does this segment cross the X line? */
27569 if (x0 >= x)
27570 {
27571 if (x1 >= x)
27572 continue;
27573 }
27574 else if (x1 < x)
27575 continue;
27576 if (y > y0 && y > y1)
27577 continue;
27578 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27579 inside = !inside;
27580 }
27581 return inside;
27582 }
27583 }
27584 return 0;
27585 }
27586
27587 Lisp_Object
27588 find_hot_spot (Lisp_Object map, int x, int y)
27589 {
27590 while (CONSP (map))
27591 {
27592 if (CONSP (XCAR (map))
27593 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27594 return XCAR (map);
27595 map = XCDR (map);
27596 }
27597
27598 return Qnil;
27599 }
27600
27601 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27602 3, 3, 0,
27603 doc: /* Lookup in image map MAP coordinates X and Y.
27604 An image map is an alist where each element has the format (AREA ID PLIST).
27605 An AREA is specified as either a rectangle, a circle, or a polygon:
27606 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27607 pixel coordinates of the upper left and bottom right corners.
27608 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27609 and the radius of the circle; r may be a float or integer.
27610 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27611 vector describes one corner in the polygon.
27612 Returns the alist element for the first matching AREA in MAP. */)
27613 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27614 {
27615 if (NILP (map))
27616 return Qnil;
27617
27618 CHECK_NUMBER (x);
27619 CHECK_NUMBER (y);
27620
27621 return find_hot_spot (map,
27622 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27623 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27624 }
27625
27626
27627 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27628 static void
27629 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27630 {
27631 /* Do not change cursor shape while dragging mouse. */
27632 if (!NILP (do_mouse_tracking))
27633 return;
27634
27635 if (!NILP (pointer))
27636 {
27637 if (EQ (pointer, Qarrow))
27638 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27639 else if (EQ (pointer, Qhand))
27640 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27641 else if (EQ (pointer, Qtext))
27642 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27643 else if (EQ (pointer, intern ("hdrag")))
27644 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27645 #ifdef HAVE_X_WINDOWS
27646 else if (EQ (pointer, intern ("vdrag")))
27647 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27648 #endif
27649 else if (EQ (pointer, intern ("hourglass")))
27650 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27651 else if (EQ (pointer, Qmodeline))
27652 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27653 else
27654 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27655 }
27656
27657 if (cursor != No_Cursor)
27658 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27659 }
27660
27661 #endif /* HAVE_WINDOW_SYSTEM */
27662
27663 /* Take proper action when mouse has moved to the mode or header line
27664 or marginal area AREA of window W, x-position X and y-position Y.
27665 X is relative to the start of the text display area of W, so the
27666 width of bitmap areas and scroll bars must be subtracted to get a
27667 position relative to the start of the mode line. */
27668
27669 static void
27670 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27671 enum window_part area)
27672 {
27673 struct window *w = XWINDOW (window);
27674 struct frame *f = XFRAME (w->frame);
27675 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27676 #ifdef HAVE_WINDOW_SYSTEM
27677 Display_Info *dpyinfo;
27678 #endif
27679 Cursor cursor = No_Cursor;
27680 Lisp_Object pointer = Qnil;
27681 int dx, dy, width, height;
27682 ptrdiff_t charpos;
27683 Lisp_Object string, object = Qnil;
27684 Lisp_Object pos IF_LINT (= Qnil), help;
27685
27686 Lisp_Object mouse_face;
27687 int original_x_pixel = x;
27688 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27689 struct glyph_row *row IF_LINT (= 0);
27690
27691 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27692 {
27693 int x0;
27694 struct glyph *end;
27695
27696 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27697 returns them in row/column units! */
27698 string = mode_line_string (w, area, &x, &y, &charpos,
27699 &object, &dx, &dy, &width, &height);
27700
27701 row = (area == ON_MODE_LINE
27702 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27703 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27704
27705 /* Find the glyph under the mouse pointer. */
27706 if (row->mode_line_p && row->enabled_p)
27707 {
27708 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27709 end = glyph + row->used[TEXT_AREA];
27710
27711 for (x0 = original_x_pixel;
27712 glyph < end && x0 >= glyph->pixel_width;
27713 ++glyph)
27714 x0 -= glyph->pixel_width;
27715
27716 if (glyph >= end)
27717 glyph = NULL;
27718 }
27719 }
27720 else
27721 {
27722 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27723 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27724 returns them in row/column units! */
27725 string = marginal_area_string (w, area, &x, &y, &charpos,
27726 &object, &dx, &dy, &width, &height);
27727 }
27728
27729 help = Qnil;
27730
27731 #ifdef HAVE_WINDOW_SYSTEM
27732 if (IMAGEP (object))
27733 {
27734 Lisp_Object image_map, hotspot;
27735 if ((image_map = Fplist_get (XCDR (object), QCmap),
27736 !NILP (image_map))
27737 && (hotspot = find_hot_spot (image_map, dx, dy),
27738 CONSP (hotspot))
27739 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27740 {
27741 Lisp_Object plist;
27742
27743 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27744 If so, we could look for mouse-enter, mouse-leave
27745 properties in PLIST (and do something...). */
27746 hotspot = XCDR (hotspot);
27747 if (CONSP (hotspot)
27748 && (plist = XCAR (hotspot), CONSP (plist)))
27749 {
27750 pointer = Fplist_get (plist, Qpointer);
27751 if (NILP (pointer))
27752 pointer = Qhand;
27753 help = Fplist_get (plist, Qhelp_echo);
27754 if (!NILP (help))
27755 {
27756 help_echo_string = help;
27757 XSETWINDOW (help_echo_window, w);
27758 help_echo_object = w->contents;
27759 help_echo_pos = charpos;
27760 }
27761 }
27762 }
27763 if (NILP (pointer))
27764 pointer = Fplist_get (XCDR (object), QCpointer);
27765 }
27766 #endif /* HAVE_WINDOW_SYSTEM */
27767
27768 if (STRINGP (string))
27769 pos = make_number (charpos);
27770
27771 /* Set the help text and mouse pointer. If the mouse is on a part
27772 of the mode line without any text (e.g. past the right edge of
27773 the mode line text), use the default help text and pointer. */
27774 if (STRINGP (string) || area == ON_MODE_LINE)
27775 {
27776 /* Arrange to display the help by setting the global variables
27777 help_echo_string, help_echo_object, and help_echo_pos. */
27778 if (NILP (help))
27779 {
27780 if (STRINGP (string))
27781 help = Fget_text_property (pos, Qhelp_echo, string);
27782
27783 if (!NILP (help))
27784 {
27785 help_echo_string = help;
27786 XSETWINDOW (help_echo_window, w);
27787 help_echo_object = string;
27788 help_echo_pos = charpos;
27789 }
27790 else if (area == ON_MODE_LINE)
27791 {
27792 Lisp_Object default_help
27793 = buffer_local_value_1 (Qmode_line_default_help_echo,
27794 w->contents);
27795
27796 if (STRINGP (default_help))
27797 {
27798 help_echo_string = default_help;
27799 XSETWINDOW (help_echo_window, w);
27800 help_echo_object = Qnil;
27801 help_echo_pos = -1;
27802 }
27803 }
27804 }
27805
27806 #ifdef HAVE_WINDOW_SYSTEM
27807 /* Change the mouse pointer according to what is under it. */
27808 if (FRAME_WINDOW_P (f))
27809 {
27810 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27811 if (STRINGP (string))
27812 {
27813 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27814
27815 if (NILP (pointer))
27816 pointer = Fget_text_property (pos, Qpointer, string);
27817
27818 /* Change the mouse pointer according to what is under X/Y. */
27819 if (NILP (pointer)
27820 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27821 {
27822 Lisp_Object map;
27823 map = Fget_text_property (pos, Qlocal_map, string);
27824 if (!KEYMAPP (map))
27825 map = Fget_text_property (pos, Qkeymap, string);
27826 if (!KEYMAPP (map))
27827 cursor = dpyinfo->vertical_scroll_bar_cursor;
27828 }
27829 }
27830 else
27831 /* Default mode-line pointer. */
27832 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27833 }
27834 #endif
27835 }
27836
27837 /* Change the mouse face according to what is under X/Y. */
27838 if (STRINGP (string))
27839 {
27840 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27841 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27842 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27843 && glyph)
27844 {
27845 Lisp_Object b, e;
27846
27847 struct glyph * tmp_glyph;
27848
27849 int gpos;
27850 int gseq_length;
27851 int total_pixel_width;
27852 ptrdiff_t begpos, endpos, ignore;
27853
27854 int vpos, hpos;
27855
27856 b = Fprevious_single_property_change (make_number (charpos + 1),
27857 Qmouse_face, string, Qnil);
27858 if (NILP (b))
27859 begpos = 0;
27860 else
27861 begpos = XINT (b);
27862
27863 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27864 if (NILP (e))
27865 endpos = SCHARS (string);
27866 else
27867 endpos = XINT (e);
27868
27869 /* Calculate the glyph position GPOS of GLYPH in the
27870 displayed string, relative to the beginning of the
27871 highlighted part of the string.
27872
27873 Note: GPOS is different from CHARPOS. CHARPOS is the
27874 position of GLYPH in the internal string object. A mode
27875 line string format has structures which are converted to
27876 a flattened string by the Emacs Lisp interpreter. The
27877 internal string is an element of those structures. The
27878 displayed string is the flattened string. */
27879 tmp_glyph = row_start_glyph;
27880 while (tmp_glyph < glyph
27881 && (!(EQ (tmp_glyph->object, glyph->object)
27882 && begpos <= tmp_glyph->charpos
27883 && tmp_glyph->charpos < endpos)))
27884 tmp_glyph++;
27885 gpos = glyph - tmp_glyph;
27886
27887 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27888 the highlighted part of the displayed string to which
27889 GLYPH belongs. Note: GSEQ_LENGTH is different from
27890 SCHARS (STRING), because the latter returns the length of
27891 the internal string. */
27892 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27893 tmp_glyph > glyph
27894 && (!(EQ (tmp_glyph->object, glyph->object)
27895 && begpos <= tmp_glyph->charpos
27896 && tmp_glyph->charpos < endpos));
27897 tmp_glyph--)
27898 ;
27899 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27900
27901 /* Calculate the total pixel width of all the glyphs between
27902 the beginning of the highlighted area and GLYPH. */
27903 total_pixel_width = 0;
27904 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27905 total_pixel_width += tmp_glyph->pixel_width;
27906
27907 /* Pre calculation of re-rendering position. Note: X is in
27908 column units here, after the call to mode_line_string or
27909 marginal_area_string. */
27910 hpos = x - gpos;
27911 vpos = (area == ON_MODE_LINE
27912 ? (w->current_matrix)->nrows - 1
27913 : 0);
27914
27915 /* If GLYPH's position is included in the region that is
27916 already drawn in mouse face, we have nothing to do. */
27917 if ( EQ (window, hlinfo->mouse_face_window)
27918 && (!row->reversed_p
27919 ? (hlinfo->mouse_face_beg_col <= hpos
27920 && hpos < hlinfo->mouse_face_end_col)
27921 /* In R2L rows we swap BEG and END, see below. */
27922 : (hlinfo->mouse_face_end_col <= hpos
27923 && hpos < hlinfo->mouse_face_beg_col))
27924 && hlinfo->mouse_face_beg_row == vpos )
27925 return;
27926
27927 if (clear_mouse_face (hlinfo))
27928 cursor = No_Cursor;
27929
27930 if (!row->reversed_p)
27931 {
27932 hlinfo->mouse_face_beg_col = hpos;
27933 hlinfo->mouse_face_beg_x = original_x_pixel
27934 - (total_pixel_width + dx);
27935 hlinfo->mouse_face_end_col = hpos + gseq_length;
27936 hlinfo->mouse_face_end_x = 0;
27937 }
27938 else
27939 {
27940 /* In R2L rows, show_mouse_face expects BEG and END
27941 coordinates to be swapped. */
27942 hlinfo->mouse_face_end_col = hpos;
27943 hlinfo->mouse_face_end_x = original_x_pixel
27944 - (total_pixel_width + dx);
27945 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27946 hlinfo->mouse_face_beg_x = 0;
27947 }
27948
27949 hlinfo->mouse_face_beg_row = vpos;
27950 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27951 hlinfo->mouse_face_past_end = 0;
27952 hlinfo->mouse_face_window = window;
27953
27954 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27955 charpos,
27956 0, 0, 0,
27957 &ignore,
27958 glyph->face_id,
27959 1);
27960 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27961
27962 if (NILP (pointer))
27963 pointer = Qhand;
27964 }
27965 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27966 clear_mouse_face (hlinfo);
27967 }
27968 #ifdef HAVE_WINDOW_SYSTEM
27969 if (FRAME_WINDOW_P (f))
27970 define_frame_cursor1 (f, cursor, pointer);
27971 #endif
27972 }
27973
27974
27975 /* EXPORT:
27976 Take proper action when the mouse has moved to position X, Y on
27977 frame F with regards to highlighting portions of display that have
27978 mouse-face properties. Also de-highlight portions of display where
27979 the mouse was before, set the mouse pointer shape as appropriate
27980 for the mouse coordinates, and activate help echo (tooltips).
27981 X and Y can be negative or out of range. */
27982
27983 void
27984 note_mouse_highlight (struct frame *f, int x, int y)
27985 {
27986 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27987 enum window_part part = ON_NOTHING;
27988 Lisp_Object window;
27989 struct window *w;
27990 Cursor cursor = No_Cursor;
27991 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27992 struct buffer *b;
27993
27994 /* When a menu is active, don't highlight because this looks odd. */
27995 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27996 if (popup_activated ())
27997 return;
27998 #endif
27999
28000 if (!f->glyphs_initialized_p
28001 || f->pointer_invisible)
28002 return;
28003
28004 hlinfo->mouse_face_mouse_x = x;
28005 hlinfo->mouse_face_mouse_y = y;
28006 hlinfo->mouse_face_mouse_frame = f;
28007
28008 if (hlinfo->mouse_face_defer)
28009 return;
28010
28011 /* Which window is that in? */
28012 window = window_from_coordinates (f, x, y, &part, 1);
28013
28014 /* If displaying active text in another window, clear that. */
28015 if (! EQ (window, hlinfo->mouse_face_window)
28016 /* Also clear if we move out of text area in same window. */
28017 || (!NILP (hlinfo->mouse_face_window)
28018 && !NILP (window)
28019 && part != ON_TEXT
28020 && part != ON_MODE_LINE
28021 && part != ON_HEADER_LINE))
28022 clear_mouse_face (hlinfo);
28023
28024 /* Not on a window -> return. */
28025 if (!WINDOWP (window))
28026 return;
28027
28028 /* Reset help_echo_string. It will get recomputed below. */
28029 help_echo_string = Qnil;
28030
28031 /* Convert to window-relative pixel coordinates. */
28032 w = XWINDOW (window);
28033 frame_to_window_pixel_xy (w, &x, &y);
28034
28035 #ifdef HAVE_WINDOW_SYSTEM
28036 /* Handle tool-bar window differently since it doesn't display a
28037 buffer. */
28038 if (EQ (window, f->tool_bar_window))
28039 {
28040 note_tool_bar_highlight (f, x, y);
28041 return;
28042 }
28043 #endif
28044
28045 /* Mouse is on the mode, header line or margin? */
28046 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28047 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28048 {
28049 note_mode_line_or_margin_highlight (window, x, y, part);
28050 return;
28051 }
28052
28053 #ifdef HAVE_WINDOW_SYSTEM
28054 if (part == ON_VERTICAL_BORDER)
28055 {
28056 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28057 help_echo_string = build_string ("drag-mouse-1: resize");
28058 }
28059 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28060 || part == ON_SCROLL_BAR)
28061 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28062 else
28063 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28064 #endif
28065
28066 /* Are we in a window whose display is up to date?
28067 And verify the buffer's text has not changed. */
28068 b = XBUFFER (w->contents);
28069 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28070 {
28071 int hpos, vpos, dx, dy, area = LAST_AREA;
28072 ptrdiff_t pos;
28073 struct glyph *glyph;
28074 Lisp_Object object;
28075 Lisp_Object mouse_face = Qnil, position;
28076 Lisp_Object *overlay_vec = NULL;
28077 ptrdiff_t i, noverlays;
28078 struct buffer *obuf;
28079 ptrdiff_t obegv, ozv;
28080 int same_region;
28081
28082 /* Find the glyph under X/Y. */
28083 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28084
28085 #ifdef HAVE_WINDOW_SYSTEM
28086 /* Look for :pointer property on image. */
28087 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28088 {
28089 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28090 if (img != NULL && IMAGEP (img->spec))
28091 {
28092 Lisp_Object image_map, hotspot;
28093 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28094 !NILP (image_map))
28095 && (hotspot = find_hot_spot (image_map,
28096 glyph->slice.img.x + dx,
28097 glyph->slice.img.y + dy),
28098 CONSP (hotspot))
28099 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28100 {
28101 Lisp_Object plist;
28102
28103 /* Could check XCAR (hotspot) to see if we enter/leave
28104 this hot-spot.
28105 If so, we could look for mouse-enter, mouse-leave
28106 properties in PLIST (and do something...). */
28107 hotspot = XCDR (hotspot);
28108 if (CONSP (hotspot)
28109 && (plist = XCAR (hotspot), CONSP (plist)))
28110 {
28111 pointer = Fplist_get (plist, Qpointer);
28112 if (NILP (pointer))
28113 pointer = Qhand;
28114 help_echo_string = Fplist_get (plist, Qhelp_echo);
28115 if (!NILP (help_echo_string))
28116 {
28117 help_echo_window = window;
28118 help_echo_object = glyph->object;
28119 help_echo_pos = glyph->charpos;
28120 }
28121 }
28122 }
28123 if (NILP (pointer))
28124 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28125 }
28126 }
28127 #endif /* HAVE_WINDOW_SYSTEM */
28128
28129 /* Clear mouse face if X/Y not over text. */
28130 if (glyph == NULL
28131 || area != TEXT_AREA
28132 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28133 /* Glyph's OBJECT is an integer for glyphs inserted by the
28134 display engine for its internal purposes, like truncation
28135 and continuation glyphs and blanks beyond the end of
28136 line's text on text terminals. If we are over such a
28137 glyph, we are not over any text. */
28138 || INTEGERP (glyph->object)
28139 /* R2L rows have a stretch glyph at their front, which
28140 stands for no text, whereas L2R rows have no glyphs at
28141 all beyond the end of text. Treat such stretch glyphs
28142 like we do with NULL glyphs in L2R rows. */
28143 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28144 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28145 && glyph->type == STRETCH_GLYPH
28146 && glyph->avoid_cursor_p))
28147 {
28148 if (clear_mouse_face (hlinfo))
28149 cursor = No_Cursor;
28150 #ifdef HAVE_WINDOW_SYSTEM
28151 if (FRAME_WINDOW_P (f) && NILP (pointer))
28152 {
28153 if (area != TEXT_AREA)
28154 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28155 else
28156 pointer = Vvoid_text_area_pointer;
28157 }
28158 #endif
28159 goto set_cursor;
28160 }
28161
28162 pos = glyph->charpos;
28163 object = glyph->object;
28164 if (!STRINGP (object) && !BUFFERP (object))
28165 goto set_cursor;
28166
28167 /* If we get an out-of-range value, return now; avoid an error. */
28168 if (BUFFERP (object) && pos > BUF_Z (b))
28169 goto set_cursor;
28170
28171 /* Make the window's buffer temporarily current for
28172 overlays_at and compute_char_face. */
28173 obuf = current_buffer;
28174 current_buffer = b;
28175 obegv = BEGV;
28176 ozv = ZV;
28177 BEGV = BEG;
28178 ZV = Z;
28179
28180 /* Is this char mouse-active or does it have help-echo? */
28181 position = make_number (pos);
28182
28183 if (BUFFERP (object))
28184 {
28185 /* Put all the overlays we want in a vector in overlay_vec. */
28186 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28187 /* Sort overlays into increasing priority order. */
28188 noverlays = sort_overlays (overlay_vec, noverlays, w);
28189 }
28190 else
28191 noverlays = 0;
28192
28193 if (NILP (Vmouse_highlight))
28194 {
28195 clear_mouse_face (hlinfo);
28196 goto check_help_echo;
28197 }
28198
28199 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28200
28201 if (same_region)
28202 cursor = No_Cursor;
28203
28204 /* Check mouse-face highlighting. */
28205 if (! same_region
28206 /* If there exists an overlay with mouse-face overlapping
28207 the one we are currently highlighting, we have to
28208 check if we enter the overlapping overlay, and then
28209 highlight only that. */
28210 || (OVERLAYP (hlinfo->mouse_face_overlay)
28211 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28212 {
28213 /* Find the highest priority overlay with a mouse-face. */
28214 Lisp_Object overlay = Qnil;
28215 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28216 {
28217 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28218 if (!NILP (mouse_face))
28219 overlay = overlay_vec[i];
28220 }
28221
28222 /* If we're highlighting the same overlay as before, there's
28223 no need to do that again. */
28224 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28225 goto check_help_echo;
28226 hlinfo->mouse_face_overlay = overlay;
28227
28228 /* Clear the display of the old active region, if any. */
28229 if (clear_mouse_face (hlinfo))
28230 cursor = No_Cursor;
28231
28232 /* If no overlay applies, get a text property. */
28233 if (NILP (overlay))
28234 mouse_face = Fget_text_property (position, Qmouse_face, object);
28235
28236 /* Next, compute the bounds of the mouse highlighting and
28237 display it. */
28238 if (!NILP (mouse_face) && STRINGP (object))
28239 {
28240 /* The mouse-highlighting comes from a display string
28241 with a mouse-face. */
28242 Lisp_Object s, e;
28243 ptrdiff_t ignore;
28244
28245 s = Fprevious_single_property_change
28246 (make_number (pos + 1), Qmouse_face, object, Qnil);
28247 e = Fnext_single_property_change
28248 (position, Qmouse_face, object, Qnil);
28249 if (NILP (s))
28250 s = make_number (0);
28251 if (NILP (e))
28252 e = make_number (SCHARS (object) - 1);
28253 mouse_face_from_string_pos (w, hlinfo, object,
28254 XINT (s), XINT (e));
28255 hlinfo->mouse_face_past_end = 0;
28256 hlinfo->mouse_face_window = window;
28257 hlinfo->mouse_face_face_id
28258 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28259 glyph->face_id, 1);
28260 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28261 cursor = No_Cursor;
28262 }
28263 else
28264 {
28265 /* The mouse-highlighting, if any, comes from an overlay
28266 or text property in the buffer. */
28267 Lisp_Object buffer IF_LINT (= Qnil);
28268 Lisp_Object disp_string IF_LINT (= Qnil);
28269
28270 if (STRINGP (object))
28271 {
28272 /* If we are on a display string with no mouse-face,
28273 check if the text under it has one. */
28274 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28275 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28276 pos = string_buffer_position (object, start);
28277 if (pos > 0)
28278 {
28279 mouse_face = get_char_property_and_overlay
28280 (make_number (pos), Qmouse_face, w->contents, &overlay);
28281 buffer = w->contents;
28282 disp_string = object;
28283 }
28284 }
28285 else
28286 {
28287 buffer = object;
28288 disp_string = Qnil;
28289 }
28290
28291 if (!NILP (mouse_face))
28292 {
28293 Lisp_Object before, after;
28294 Lisp_Object before_string, after_string;
28295 /* To correctly find the limits of mouse highlight
28296 in a bidi-reordered buffer, we must not use the
28297 optimization of limiting the search in
28298 previous-single-property-change and
28299 next-single-property-change, because
28300 rows_from_pos_range needs the real start and end
28301 positions to DTRT in this case. That's because
28302 the first row visible in a window does not
28303 necessarily display the character whose position
28304 is the smallest. */
28305 Lisp_Object lim1 =
28306 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28307 ? Fmarker_position (w->start)
28308 : Qnil;
28309 Lisp_Object lim2 =
28310 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28311 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28312 : Qnil;
28313
28314 if (NILP (overlay))
28315 {
28316 /* Handle the text property case. */
28317 before = Fprevious_single_property_change
28318 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28319 after = Fnext_single_property_change
28320 (make_number (pos), Qmouse_face, buffer, lim2);
28321 before_string = after_string = Qnil;
28322 }
28323 else
28324 {
28325 /* Handle the overlay case. */
28326 before = Foverlay_start (overlay);
28327 after = Foverlay_end (overlay);
28328 before_string = Foverlay_get (overlay, Qbefore_string);
28329 after_string = Foverlay_get (overlay, Qafter_string);
28330
28331 if (!STRINGP (before_string)) before_string = Qnil;
28332 if (!STRINGP (after_string)) after_string = Qnil;
28333 }
28334
28335 mouse_face_from_buffer_pos (window, hlinfo, pos,
28336 NILP (before)
28337 ? 1
28338 : XFASTINT (before),
28339 NILP (after)
28340 ? BUF_Z (XBUFFER (buffer))
28341 : XFASTINT (after),
28342 before_string, after_string,
28343 disp_string);
28344 cursor = No_Cursor;
28345 }
28346 }
28347 }
28348
28349 check_help_echo:
28350
28351 /* Look for a `help-echo' property. */
28352 if (NILP (help_echo_string)) {
28353 Lisp_Object help, overlay;
28354
28355 /* Check overlays first. */
28356 help = overlay = Qnil;
28357 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28358 {
28359 overlay = overlay_vec[i];
28360 help = Foverlay_get (overlay, Qhelp_echo);
28361 }
28362
28363 if (!NILP (help))
28364 {
28365 help_echo_string = help;
28366 help_echo_window = window;
28367 help_echo_object = overlay;
28368 help_echo_pos = pos;
28369 }
28370 else
28371 {
28372 Lisp_Object obj = glyph->object;
28373 ptrdiff_t charpos = glyph->charpos;
28374
28375 /* Try text properties. */
28376 if (STRINGP (obj)
28377 && charpos >= 0
28378 && charpos < SCHARS (obj))
28379 {
28380 help = Fget_text_property (make_number (charpos),
28381 Qhelp_echo, obj);
28382 if (NILP (help))
28383 {
28384 /* If the string itself doesn't specify a help-echo,
28385 see if the buffer text ``under'' it does. */
28386 struct glyph_row *r
28387 = MATRIX_ROW (w->current_matrix, vpos);
28388 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28389 ptrdiff_t p = string_buffer_position (obj, start);
28390 if (p > 0)
28391 {
28392 help = Fget_char_property (make_number (p),
28393 Qhelp_echo, w->contents);
28394 if (!NILP (help))
28395 {
28396 charpos = p;
28397 obj = w->contents;
28398 }
28399 }
28400 }
28401 }
28402 else if (BUFFERP (obj)
28403 && charpos >= BEGV
28404 && charpos < ZV)
28405 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28406 obj);
28407
28408 if (!NILP (help))
28409 {
28410 help_echo_string = help;
28411 help_echo_window = window;
28412 help_echo_object = obj;
28413 help_echo_pos = charpos;
28414 }
28415 }
28416 }
28417
28418 #ifdef HAVE_WINDOW_SYSTEM
28419 /* Look for a `pointer' property. */
28420 if (FRAME_WINDOW_P (f) && NILP (pointer))
28421 {
28422 /* Check overlays first. */
28423 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28424 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28425
28426 if (NILP (pointer))
28427 {
28428 Lisp_Object obj = glyph->object;
28429 ptrdiff_t charpos = glyph->charpos;
28430
28431 /* Try text properties. */
28432 if (STRINGP (obj)
28433 && charpos >= 0
28434 && charpos < SCHARS (obj))
28435 {
28436 pointer = Fget_text_property (make_number (charpos),
28437 Qpointer, obj);
28438 if (NILP (pointer))
28439 {
28440 /* If the string itself doesn't specify a pointer,
28441 see if the buffer text ``under'' it does. */
28442 struct glyph_row *r
28443 = MATRIX_ROW (w->current_matrix, vpos);
28444 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28445 ptrdiff_t p = string_buffer_position (obj, start);
28446 if (p > 0)
28447 pointer = Fget_char_property (make_number (p),
28448 Qpointer, w->contents);
28449 }
28450 }
28451 else if (BUFFERP (obj)
28452 && charpos >= BEGV
28453 && charpos < ZV)
28454 pointer = Fget_text_property (make_number (charpos),
28455 Qpointer, obj);
28456 }
28457 }
28458 #endif /* HAVE_WINDOW_SYSTEM */
28459
28460 BEGV = obegv;
28461 ZV = ozv;
28462 current_buffer = obuf;
28463 }
28464
28465 set_cursor:
28466
28467 #ifdef HAVE_WINDOW_SYSTEM
28468 if (FRAME_WINDOW_P (f))
28469 define_frame_cursor1 (f, cursor, pointer);
28470 #else
28471 /* This is here to prevent a compiler error, about "label at end of
28472 compound statement". */
28473 return;
28474 #endif
28475 }
28476
28477
28478 /* EXPORT for RIF:
28479 Clear any mouse-face on window W. This function is part of the
28480 redisplay interface, and is called from try_window_id and similar
28481 functions to ensure the mouse-highlight is off. */
28482
28483 void
28484 x_clear_window_mouse_face (struct window *w)
28485 {
28486 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28487 Lisp_Object window;
28488
28489 block_input ();
28490 XSETWINDOW (window, w);
28491 if (EQ (window, hlinfo->mouse_face_window))
28492 clear_mouse_face (hlinfo);
28493 unblock_input ();
28494 }
28495
28496
28497 /* EXPORT:
28498 Just discard the mouse face information for frame F, if any.
28499 This is used when the size of F is changed. */
28500
28501 void
28502 cancel_mouse_face (struct frame *f)
28503 {
28504 Lisp_Object window;
28505 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28506
28507 window = hlinfo->mouse_face_window;
28508 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28509 reset_mouse_highlight (hlinfo);
28510 }
28511
28512
28513 \f
28514 /***********************************************************************
28515 Exposure Events
28516 ***********************************************************************/
28517
28518 #ifdef HAVE_WINDOW_SYSTEM
28519
28520 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28521 which intersects rectangle R. R is in window-relative coordinates. */
28522
28523 static void
28524 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28525 enum glyph_row_area area)
28526 {
28527 struct glyph *first = row->glyphs[area];
28528 struct glyph *end = row->glyphs[area] + row->used[area];
28529 struct glyph *last;
28530 int first_x, start_x, x;
28531
28532 if (area == TEXT_AREA && row->fill_line_p)
28533 /* If row extends face to end of line write the whole line. */
28534 draw_glyphs (w, 0, row, area,
28535 0, row->used[area],
28536 DRAW_NORMAL_TEXT, 0);
28537 else
28538 {
28539 /* Set START_X to the window-relative start position for drawing glyphs of
28540 AREA. The first glyph of the text area can be partially visible.
28541 The first glyphs of other areas cannot. */
28542 start_x = window_box_left_offset (w, area);
28543 x = start_x;
28544 if (area == TEXT_AREA)
28545 x += row->x;
28546
28547 /* Find the first glyph that must be redrawn. */
28548 while (first < end
28549 && x + first->pixel_width < r->x)
28550 {
28551 x += first->pixel_width;
28552 ++first;
28553 }
28554
28555 /* Find the last one. */
28556 last = first;
28557 first_x = x;
28558 while (last < end
28559 && x < r->x + r->width)
28560 {
28561 x += last->pixel_width;
28562 ++last;
28563 }
28564
28565 /* Repaint. */
28566 if (last > first)
28567 draw_glyphs (w, first_x - start_x, row, area,
28568 first - row->glyphs[area], last - row->glyphs[area],
28569 DRAW_NORMAL_TEXT, 0);
28570 }
28571 }
28572
28573
28574 /* Redraw the parts of the glyph row ROW on window W intersecting
28575 rectangle R. R is in window-relative coordinates. Value is
28576 non-zero if mouse-face was overwritten. */
28577
28578 static int
28579 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28580 {
28581 eassert (row->enabled_p);
28582
28583 if (row->mode_line_p || w->pseudo_window_p)
28584 draw_glyphs (w, 0, row, TEXT_AREA,
28585 0, row->used[TEXT_AREA],
28586 DRAW_NORMAL_TEXT, 0);
28587 else
28588 {
28589 if (row->used[LEFT_MARGIN_AREA])
28590 expose_area (w, row, r, LEFT_MARGIN_AREA);
28591 if (row->used[TEXT_AREA])
28592 expose_area (w, row, r, TEXT_AREA);
28593 if (row->used[RIGHT_MARGIN_AREA])
28594 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28595 draw_row_fringe_bitmaps (w, row);
28596 }
28597
28598 return row->mouse_face_p;
28599 }
28600
28601
28602 /* Redraw those parts of glyphs rows during expose event handling that
28603 overlap other rows. Redrawing of an exposed line writes over parts
28604 of lines overlapping that exposed line; this function fixes that.
28605
28606 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28607 row in W's current matrix that is exposed and overlaps other rows.
28608 LAST_OVERLAPPING_ROW is the last such row. */
28609
28610 static void
28611 expose_overlaps (struct window *w,
28612 struct glyph_row *first_overlapping_row,
28613 struct glyph_row *last_overlapping_row,
28614 XRectangle *r)
28615 {
28616 struct glyph_row *row;
28617
28618 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28619 if (row->overlapping_p)
28620 {
28621 eassert (row->enabled_p && !row->mode_line_p);
28622
28623 row->clip = r;
28624 if (row->used[LEFT_MARGIN_AREA])
28625 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28626
28627 if (row->used[TEXT_AREA])
28628 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28629
28630 if (row->used[RIGHT_MARGIN_AREA])
28631 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28632 row->clip = NULL;
28633 }
28634 }
28635
28636
28637 /* Return non-zero if W's cursor intersects rectangle R. */
28638
28639 static int
28640 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28641 {
28642 XRectangle cr, result;
28643 struct glyph *cursor_glyph;
28644 struct glyph_row *row;
28645
28646 if (w->phys_cursor.vpos >= 0
28647 && w->phys_cursor.vpos < w->current_matrix->nrows
28648 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28649 row->enabled_p)
28650 && row->cursor_in_fringe_p)
28651 {
28652 /* Cursor is in the fringe. */
28653 cr.x = window_box_right_offset (w,
28654 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28655 ? RIGHT_MARGIN_AREA
28656 : TEXT_AREA));
28657 cr.y = row->y;
28658 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28659 cr.height = row->height;
28660 return x_intersect_rectangles (&cr, r, &result);
28661 }
28662
28663 cursor_glyph = get_phys_cursor_glyph (w);
28664 if (cursor_glyph)
28665 {
28666 /* r is relative to W's box, but w->phys_cursor.x is relative
28667 to left edge of W's TEXT area. Adjust it. */
28668 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28669 cr.y = w->phys_cursor.y;
28670 cr.width = cursor_glyph->pixel_width;
28671 cr.height = w->phys_cursor_height;
28672 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28673 I assume the effect is the same -- and this is portable. */
28674 return x_intersect_rectangles (&cr, r, &result);
28675 }
28676 /* If we don't understand the format, pretend we're not in the hot-spot. */
28677 return 0;
28678 }
28679
28680
28681 /* EXPORT:
28682 Draw a vertical window border to the right of window W if W doesn't
28683 have vertical scroll bars. */
28684
28685 void
28686 x_draw_vertical_border (struct window *w)
28687 {
28688 struct frame *f = XFRAME (WINDOW_FRAME (w));
28689
28690 /* We could do better, if we knew what type of scroll-bar the adjacent
28691 windows (on either side) have... But we don't :-(
28692 However, I think this works ok. ++KFS 2003-04-25 */
28693
28694 /* Redraw borders between horizontally adjacent windows. Don't
28695 do it for frames with vertical scroll bars because either the
28696 right scroll bar of a window, or the left scroll bar of its
28697 neighbor will suffice as a border. */
28698 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28699 return;
28700
28701 /* Note: It is necessary to redraw both the left and the right
28702 borders, for when only this single window W is being
28703 redisplayed. */
28704 if (!WINDOW_RIGHTMOST_P (w)
28705 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28706 {
28707 int x0, x1, y0, y1;
28708
28709 window_box_edges (w, &x0, &y0, &x1, &y1);
28710 y1 -= 1;
28711
28712 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28713 x1 -= 1;
28714
28715 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28716 }
28717 if (!WINDOW_LEFTMOST_P (w)
28718 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28719 {
28720 int x0, x1, y0, y1;
28721
28722 window_box_edges (w, &x0, &y0, &x1, &y1);
28723 y1 -= 1;
28724
28725 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28726 x0 -= 1;
28727
28728 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28729 }
28730 }
28731
28732
28733 /* Redraw the part of window W intersection rectangle FR. Pixel
28734 coordinates in FR are frame-relative. Call this function with
28735 input blocked. Value is non-zero if the exposure overwrites
28736 mouse-face. */
28737
28738 static int
28739 expose_window (struct window *w, XRectangle *fr)
28740 {
28741 struct frame *f = XFRAME (w->frame);
28742 XRectangle wr, r;
28743 int mouse_face_overwritten_p = 0;
28744
28745 /* If window is not yet fully initialized, do nothing. This can
28746 happen when toolkit scroll bars are used and a window is split.
28747 Reconfiguring the scroll bar will generate an expose for a newly
28748 created window. */
28749 if (w->current_matrix == NULL)
28750 return 0;
28751
28752 /* When we're currently updating the window, display and current
28753 matrix usually don't agree. Arrange for a thorough display
28754 later. */
28755 if (w->must_be_updated_p)
28756 {
28757 SET_FRAME_GARBAGED (f);
28758 return 0;
28759 }
28760
28761 /* Frame-relative pixel rectangle of W. */
28762 wr.x = WINDOW_LEFT_EDGE_X (w);
28763 wr.y = WINDOW_TOP_EDGE_Y (w);
28764 wr.width = WINDOW_TOTAL_WIDTH (w);
28765 wr.height = WINDOW_TOTAL_HEIGHT (w);
28766
28767 if (x_intersect_rectangles (fr, &wr, &r))
28768 {
28769 int yb = window_text_bottom_y (w);
28770 struct glyph_row *row;
28771 int cursor_cleared_p, phys_cursor_on_p;
28772 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28773
28774 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28775 r.x, r.y, r.width, r.height));
28776
28777 /* Convert to window coordinates. */
28778 r.x -= WINDOW_LEFT_EDGE_X (w);
28779 r.y -= WINDOW_TOP_EDGE_Y (w);
28780
28781 /* Turn off the cursor. */
28782 if (!w->pseudo_window_p
28783 && phys_cursor_in_rect_p (w, &r))
28784 {
28785 x_clear_cursor (w);
28786 cursor_cleared_p = 1;
28787 }
28788 else
28789 cursor_cleared_p = 0;
28790
28791 /* If the row containing the cursor extends face to end of line,
28792 then expose_area might overwrite the cursor outside the
28793 rectangle and thus notice_overwritten_cursor might clear
28794 w->phys_cursor_on_p. We remember the original value and
28795 check later if it is changed. */
28796 phys_cursor_on_p = w->phys_cursor_on_p;
28797
28798 /* Update lines intersecting rectangle R. */
28799 first_overlapping_row = last_overlapping_row = NULL;
28800 for (row = w->current_matrix->rows;
28801 row->enabled_p;
28802 ++row)
28803 {
28804 int y0 = row->y;
28805 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28806
28807 if ((y0 >= r.y && y0 < r.y + r.height)
28808 || (y1 > r.y && y1 < r.y + r.height)
28809 || (r.y >= y0 && r.y < y1)
28810 || (r.y + r.height > y0 && r.y + r.height < y1))
28811 {
28812 /* A header line may be overlapping, but there is no need
28813 to fix overlapping areas for them. KFS 2005-02-12 */
28814 if (row->overlapping_p && !row->mode_line_p)
28815 {
28816 if (first_overlapping_row == NULL)
28817 first_overlapping_row = row;
28818 last_overlapping_row = row;
28819 }
28820
28821 row->clip = fr;
28822 if (expose_line (w, row, &r))
28823 mouse_face_overwritten_p = 1;
28824 row->clip = NULL;
28825 }
28826 else if (row->overlapping_p)
28827 {
28828 /* We must redraw a row overlapping the exposed area. */
28829 if (y0 < r.y
28830 ? y0 + row->phys_height > r.y
28831 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28832 {
28833 if (first_overlapping_row == NULL)
28834 first_overlapping_row = row;
28835 last_overlapping_row = row;
28836 }
28837 }
28838
28839 if (y1 >= yb)
28840 break;
28841 }
28842
28843 /* Display the mode line if there is one. */
28844 if (WINDOW_WANTS_MODELINE_P (w)
28845 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28846 row->enabled_p)
28847 && row->y < r.y + r.height)
28848 {
28849 if (expose_line (w, row, &r))
28850 mouse_face_overwritten_p = 1;
28851 }
28852
28853 if (!w->pseudo_window_p)
28854 {
28855 /* Fix the display of overlapping rows. */
28856 if (first_overlapping_row)
28857 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28858 fr);
28859
28860 /* Draw border between windows. */
28861 x_draw_vertical_border (w);
28862
28863 /* Turn the cursor on again. */
28864 if (cursor_cleared_p
28865 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28866 update_window_cursor (w, 1);
28867 }
28868 }
28869
28870 return mouse_face_overwritten_p;
28871 }
28872
28873
28874
28875 /* Redraw (parts) of all windows in the window tree rooted at W that
28876 intersect R. R contains frame pixel coordinates. Value is
28877 non-zero if the exposure overwrites mouse-face. */
28878
28879 static int
28880 expose_window_tree (struct window *w, XRectangle *r)
28881 {
28882 struct frame *f = XFRAME (w->frame);
28883 int mouse_face_overwritten_p = 0;
28884
28885 while (w && !FRAME_GARBAGED_P (f))
28886 {
28887 if (WINDOWP (w->contents))
28888 mouse_face_overwritten_p
28889 |= expose_window_tree (XWINDOW (w->contents), r);
28890 else
28891 mouse_face_overwritten_p |= expose_window (w, r);
28892
28893 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28894 }
28895
28896 return mouse_face_overwritten_p;
28897 }
28898
28899
28900 /* EXPORT:
28901 Redisplay an exposed area of frame F. X and Y are the upper-left
28902 corner of the exposed rectangle. W and H are width and height of
28903 the exposed area. All are pixel values. W or H zero means redraw
28904 the entire frame. */
28905
28906 void
28907 expose_frame (struct frame *f, int x, int y, int w, int h)
28908 {
28909 XRectangle r;
28910 int mouse_face_overwritten_p = 0;
28911
28912 TRACE ((stderr, "expose_frame "));
28913
28914 /* No need to redraw if frame will be redrawn soon. */
28915 if (FRAME_GARBAGED_P (f))
28916 {
28917 TRACE ((stderr, " garbaged\n"));
28918 return;
28919 }
28920
28921 /* If basic faces haven't been realized yet, there is no point in
28922 trying to redraw anything. This can happen when we get an expose
28923 event while Emacs is starting, e.g. by moving another window. */
28924 if (FRAME_FACE_CACHE (f) == NULL
28925 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28926 {
28927 TRACE ((stderr, " no faces\n"));
28928 return;
28929 }
28930
28931 if (w == 0 || h == 0)
28932 {
28933 r.x = r.y = 0;
28934 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28935 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28936 }
28937 else
28938 {
28939 r.x = x;
28940 r.y = y;
28941 r.width = w;
28942 r.height = h;
28943 }
28944
28945 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28946 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28947
28948 if (WINDOWP (f->tool_bar_window))
28949 mouse_face_overwritten_p
28950 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28951
28952 #ifdef HAVE_X_WINDOWS
28953 #ifndef MSDOS
28954 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28955 if (WINDOWP (f->menu_bar_window))
28956 mouse_face_overwritten_p
28957 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28958 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28959 #endif
28960 #endif
28961
28962 /* Some window managers support a focus-follows-mouse style with
28963 delayed raising of frames. Imagine a partially obscured frame,
28964 and moving the mouse into partially obscured mouse-face on that
28965 frame. The visible part of the mouse-face will be highlighted,
28966 then the WM raises the obscured frame. With at least one WM, KDE
28967 2.1, Emacs is not getting any event for the raising of the frame
28968 (even tried with SubstructureRedirectMask), only Expose events.
28969 These expose events will draw text normally, i.e. not
28970 highlighted. Which means we must redo the highlight here.
28971 Subsume it under ``we love X''. --gerd 2001-08-15 */
28972 /* Included in Windows version because Windows most likely does not
28973 do the right thing if any third party tool offers
28974 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28975 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28976 {
28977 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28978 if (f == hlinfo->mouse_face_mouse_frame)
28979 {
28980 int mouse_x = hlinfo->mouse_face_mouse_x;
28981 int mouse_y = hlinfo->mouse_face_mouse_y;
28982 clear_mouse_face (hlinfo);
28983 note_mouse_highlight (f, mouse_x, mouse_y);
28984 }
28985 }
28986 }
28987
28988
28989 /* EXPORT:
28990 Determine the intersection of two rectangles R1 and R2. Return
28991 the intersection in *RESULT. Value is non-zero if RESULT is not
28992 empty. */
28993
28994 int
28995 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28996 {
28997 XRectangle *left, *right;
28998 XRectangle *upper, *lower;
28999 int intersection_p = 0;
29000
29001 /* Rearrange so that R1 is the left-most rectangle. */
29002 if (r1->x < r2->x)
29003 left = r1, right = r2;
29004 else
29005 left = r2, right = r1;
29006
29007 /* X0 of the intersection is right.x0, if this is inside R1,
29008 otherwise there is no intersection. */
29009 if (right->x <= left->x + left->width)
29010 {
29011 result->x = right->x;
29012
29013 /* The right end of the intersection is the minimum of
29014 the right ends of left and right. */
29015 result->width = (min (left->x + left->width, right->x + right->width)
29016 - result->x);
29017
29018 /* Same game for Y. */
29019 if (r1->y < r2->y)
29020 upper = r1, lower = r2;
29021 else
29022 upper = r2, lower = r1;
29023
29024 /* The upper end of the intersection is lower.y0, if this is inside
29025 of upper. Otherwise, there is no intersection. */
29026 if (lower->y <= upper->y + upper->height)
29027 {
29028 result->y = lower->y;
29029
29030 /* The lower end of the intersection is the minimum of the lower
29031 ends of upper and lower. */
29032 result->height = (min (lower->y + lower->height,
29033 upper->y + upper->height)
29034 - result->y);
29035 intersection_p = 1;
29036 }
29037 }
29038
29039 return intersection_p;
29040 }
29041
29042 #endif /* HAVE_WINDOW_SYSTEM */
29043
29044 \f
29045 /***********************************************************************
29046 Initialization
29047 ***********************************************************************/
29048
29049 void
29050 syms_of_xdisp (void)
29051 {
29052 Vwith_echo_area_save_vector = Qnil;
29053 staticpro (&Vwith_echo_area_save_vector);
29054
29055 Vmessage_stack = Qnil;
29056 staticpro (&Vmessage_stack);
29057
29058 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29059 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29060
29061 message_dolog_marker1 = Fmake_marker ();
29062 staticpro (&message_dolog_marker1);
29063 message_dolog_marker2 = Fmake_marker ();
29064 staticpro (&message_dolog_marker2);
29065 message_dolog_marker3 = Fmake_marker ();
29066 staticpro (&message_dolog_marker3);
29067
29068 #ifdef GLYPH_DEBUG
29069 defsubr (&Sdump_frame_glyph_matrix);
29070 defsubr (&Sdump_glyph_matrix);
29071 defsubr (&Sdump_glyph_row);
29072 defsubr (&Sdump_tool_bar_row);
29073 defsubr (&Strace_redisplay);
29074 defsubr (&Strace_to_stderr);
29075 #endif
29076 #ifdef HAVE_WINDOW_SYSTEM
29077 defsubr (&Stool_bar_lines_needed);
29078 defsubr (&Slookup_image_map);
29079 #endif
29080 defsubr (&Sline_pixel_height);
29081 defsubr (&Sformat_mode_line);
29082 defsubr (&Sinvisible_p);
29083 defsubr (&Scurrent_bidi_paragraph_direction);
29084 defsubr (&Smove_point_visually);
29085
29086 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29087 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29088 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29089 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29090 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29091 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29092 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29093 DEFSYM (Qeval, "eval");
29094 DEFSYM (QCdata, ":data");
29095 DEFSYM (Qdisplay, "display");
29096 DEFSYM (Qspace_width, "space-width");
29097 DEFSYM (Qraise, "raise");
29098 DEFSYM (Qslice, "slice");
29099 DEFSYM (Qspace, "space");
29100 DEFSYM (Qmargin, "margin");
29101 DEFSYM (Qpointer, "pointer");
29102 DEFSYM (Qleft_margin, "left-margin");
29103 DEFSYM (Qright_margin, "right-margin");
29104 DEFSYM (Qcenter, "center");
29105 DEFSYM (Qline_height, "line-height");
29106 DEFSYM (QCalign_to, ":align-to");
29107 DEFSYM (QCrelative_width, ":relative-width");
29108 DEFSYM (QCrelative_height, ":relative-height");
29109 DEFSYM (QCeval, ":eval");
29110 DEFSYM (QCpropertize, ":propertize");
29111 DEFSYM (QCfile, ":file");
29112 DEFSYM (Qfontified, "fontified");
29113 DEFSYM (Qfontification_functions, "fontification-functions");
29114 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29115 DEFSYM (Qescape_glyph, "escape-glyph");
29116 DEFSYM (Qnobreak_space, "nobreak-space");
29117 DEFSYM (Qimage, "image");
29118 DEFSYM (Qtext, "text");
29119 DEFSYM (Qboth, "both");
29120 DEFSYM (Qboth_horiz, "both-horiz");
29121 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29122 DEFSYM (QCmap, ":map");
29123 DEFSYM (QCpointer, ":pointer");
29124 DEFSYM (Qrect, "rect");
29125 DEFSYM (Qcircle, "circle");
29126 DEFSYM (Qpoly, "poly");
29127 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29128 DEFSYM (Qgrow_only, "grow-only");
29129 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29130 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29131 DEFSYM (Qposition, "position");
29132 DEFSYM (Qbuffer_position, "buffer-position");
29133 DEFSYM (Qobject, "object");
29134 DEFSYM (Qbar, "bar");
29135 DEFSYM (Qhbar, "hbar");
29136 DEFSYM (Qbox, "box");
29137 DEFSYM (Qhollow, "hollow");
29138 DEFSYM (Qhand, "hand");
29139 DEFSYM (Qarrow, "arrow");
29140 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29141
29142 list_of_error = list1 (list2 (intern_c_string ("error"),
29143 intern_c_string ("void-variable")));
29144 staticpro (&list_of_error);
29145
29146 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29147 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29148 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29149 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29150
29151 echo_buffer[0] = echo_buffer[1] = Qnil;
29152 staticpro (&echo_buffer[0]);
29153 staticpro (&echo_buffer[1]);
29154
29155 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29156 staticpro (&echo_area_buffer[0]);
29157 staticpro (&echo_area_buffer[1]);
29158
29159 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29160 staticpro (&Vmessages_buffer_name);
29161
29162 mode_line_proptrans_alist = Qnil;
29163 staticpro (&mode_line_proptrans_alist);
29164 mode_line_string_list = Qnil;
29165 staticpro (&mode_line_string_list);
29166 mode_line_string_face = Qnil;
29167 staticpro (&mode_line_string_face);
29168 mode_line_string_face_prop = Qnil;
29169 staticpro (&mode_line_string_face_prop);
29170 Vmode_line_unwind_vector = Qnil;
29171 staticpro (&Vmode_line_unwind_vector);
29172
29173 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29174
29175 help_echo_string = Qnil;
29176 staticpro (&help_echo_string);
29177 help_echo_object = Qnil;
29178 staticpro (&help_echo_object);
29179 help_echo_window = Qnil;
29180 staticpro (&help_echo_window);
29181 previous_help_echo_string = Qnil;
29182 staticpro (&previous_help_echo_string);
29183 help_echo_pos = -1;
29184
29185 DEFSYM (Qright_to_left, "right-to-left");
29186 DEFSYM (Qleft_to_right, "left-to-right");
29187
29188 #ifdef HAVE_WINDOW_SYSTEM
29189 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29190 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29191 For example, if a block cursor is over a tab, it will be drawn as
29192 wide as that tab on the display. */);
29193 x_stretch_cursor_p = 0;
29194 #endif
29195
29196 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29197 doc: /* Non-nil means highlight trailing whitespace.
29198 The face used for trailing whitespace is `trailing-whitespace'. */);
29199 Vshow_trailing_whitespace = Qnil;
29200
29201 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29202 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29203 If the value is t, Emacs highlights non-ASCII chars which have the
29204 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29205 or `escape-glyph' face respectively.
29206
29207 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29208 U+2011 (non-breaking hyphen) are affected.
29209
29210 Any other non-nil value means to display these characters as a escape
29211 glyph followed by an ordinary space or hyphen.
29212
29213 A value of nil means no special handling of these characters. */);
29214 Vnobreak_char_display = Qt;
29215
29216 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29217 doc: /* The pointer shape to show in void text areas.
29218 A value of nil means to show the text pointer. Other options are `arrow',
29219 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29220 Vvoid_text_area_pointer = Qarrow;
29221
29222 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29223 doc: /* Non-nil means don't actually do any redisplay.
29224 This is used for internal purposes. */);
29225 Vinhibit_redisplay = Qnil;
29226
29227 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29228 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29229 Vglobal_mode_string = Qnil;
29230
29231 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29232 doc: /* Marker for where to display an arrow on top of the buffer text.
29233 This must be the beginning of a line in order to work.
29234 See also `overlay-arrow-string'. */);
29235 Voverlay_arrow_position = Qnil;
29236
29237 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29238 doc: /* String to display as an arrow in non-window frames.
29239 See also `overlay-arrow-position'. */);
29240 Voverlay_arrow_string = build_pure_c_string ("=>");
29241
29242 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29243 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29244 The symbols on this list are examined during redisplay to determine
29245 where to display overlay arrows. */);
29246 Voverlay_arrow_variable_list
29247 = list1 (intern_c_string ("overlay-arrow-position"));
29248
29249 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29250 doc: /* The number of lines to try scrolling a window by when point moves out.
29251 If that fails to bring point back on frame, point is centered instead.
29252 If this is zero, point is always centered after it moves off frame.
29253 If you want scrolling to always be a line at a time, you should set
29254 `scroll-conservatively' to a large value rather than set this to 1. */);
29255
29256 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29257 doc: /* Scroll up to this many lines, to bring point back on screen.
29258 If point moves off-screen, redisplay will scroll by up to
29259 `scroll-conservatively' lines in order to bring point just barely
29260 onto the screen again. If that cannot be done, then redisplay
29261 recenters point as usual.
29262
29263 If the value is greater than 100, redisplay will never recenter point,
29264 but will always scroll just enough text to bring point into view, even
29265 if you move far away.
29266
29267 A value of zero means always recenter point if it moves off screen. */);
29268 scroll_conservatively = 0;
29269
29270 DEFVAR_INT ("scroll-margin", scroll_margin,
29271 doc: /* Number of lines of margin at the top and bottom of a window.
29272 Recenter the window whenever point gets within this many lines
29273 of the top or bottom of the window. */);
29274 scroll_margin = 0;
29275
29276 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29277 doc: /* Pixels per inch value for non-window system displays.
29278 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29279 Vdisplay_pixels_per_inch = make_float (72.0);
29280
29281 #ifdef GLYPH_DEBUG
29282 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29283 #endif
29284
29285 DEFVAR_LISP ("truncate-partial-width-windows",
29286 Vtruncate_partial_width_windows,
29287 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29288 For an integer value, truncate lines in each window narrower than the
29289 full frame width, provided the window width is less than that integer;
29290 otherwise, respect the value of `truncate-lines'.
29291
29292 For any other non-nil value, truncate lines in all windows that do
29293 not span the full frame width.
29294
29295 A value of nil means to respect the value of `truncate-lines'.
29296
29297 If `word-wrap' is enabled, you might want to reduce this. */);
29298 Vtruncate_partial_width_windows = make_number (50);
29299
29300 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29301 doc: /* Maximum buffer size for which line number should be displayed.
29302 If the buffer is bigger than this, the line number does not appear
29303 in the mode line. A value of nil means no limit. */);
29304 Vline_number_display_limit = Qnil;
29305
29306 DEFVAR_INT ("line-number-display-limit-width",
29307 line_number_display_limit_width,
29308 doc: /* Maximum line width (in characters) for line number display.
29309 If the average length of the lines near point is bigger than this, then the
29310 line number may be omitted from the mode line. */);
29311 line_number_display_limit_width = 200;
29312
29313 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29314 doc: /* Non-nil means highlight region even in nonselected windows. */);
29315 highlight_nonselected_windows = 0;
29316
29317 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29318 doc: /* Non-nil if more than one frame is visible on this display.
29319 Minibuffer-only frames don't count, but iconified frames do.
29320 This variable is not guaranteed to be accurate except while processing
29321 `frame-title-format' and `icon-title-format'. */);
29322
29323 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29324 doc: /* Template for displaying the title bar of visible frames.
29325 \(Assuming the window manager supports this feature.)
29326
29327 This variable has the same structure as `mode-line-format', except that
29328 the %c and %l constructs are ignored. It is used only on frames for
29329 which no explicit name has been set \(see `modify-frame-parameters'). */);
29330
29331 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29332 doc: /* Template for displaying the title bar of an iconified frame.
29333 \(Assuming the window manager supports this feature.)
29334 This variable has the same structure as `mode-line-format' (which see),
29335 and is used only on frames for which no explicit name has been set
29336 \(see `modify-frame-parameters'). */);
29337 Vicon_title_format
29338 = Vframe_title_format
29339 = listn (CONSTYPE_PURE, 3,
29340 intern_c_string ("multiple-frames"),
29341 build_pure_c_string ("%b"),
29342 listn (CONSTYPE_PURE, 4,
29343 empty_unibyte_string,
29344 intern_c_string ("invocation-name"),
29345 build_pure_c_string ("@"),
29346 intern_c_string ("system-name")));
29347
29348 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29349 doc: /* Maximum number of lines to keep in the message log buffer.
29350 If nil, disable message logging. If t, log messages but don't truncate
29351 the buffer when it becomes large. */);
29352 Vmessage_log_max = make_number (1000);
29353
29354 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29355 doc: /* Functions called before redisplay, if window sizes have changed.
29356 The value should be a list of functions that take one argument.
29357 Just before redisplay, for each frame, if any of its windows have changed
29358 size since the last redisplay, or have been split or deleted,
29359 all the functions in the list are called, with the frame as argument. */);
29360 Vwindow_size_change_functions = Qnil;
29361
29362 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29363 doc: /* List of functions to call before redisplaying a window with scrolling.
29364 Each function is called with two arguments, the window and its new
29365 display-start position. Note that these functions are also called by
29366 `set-window-buffer'. Also note that the value of `window-end' is not
29367 valid when these functions are called.
29368
29369 Warning: Do not use this feature to alter the way the window
29370 is scrolled. It is not designed for that, and such use probably won't
29371 work. */);
29372 Vwindow_scroll_functions = Qnil;
29373
29374 DEFVAR_LISP ("window-text-change-functions",
29375 Vwindow_text_change_functions,
29376 doc: /* Functions to call in redisplay when text in the window might change. */);
29377 Vwindow_text_change_functions = Qnil;
29378
29379 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29380 doc: /* Functions called when redisplay of a window reaches the end trigger.
29381 Each function is called with two arguments, the window and the end trigger value.
29382 See `set-window-redisplay-end-trigger'. */);
29383 Vredisplay_end_trigger_functions = Qnil;
29384
29385 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29386 doc: /* Non-nil means autoselect window with mouse pointer.
29387 If nil, do not autoselect windows.
29388 A positive number means delay autoselection by that many seconds: a
29389 window is autoselected only after the mouse has remained in that
29390 window for the duration of the delay.
29391 A negative number has a similar effect, but causes windows to be
29392 autoselected only after the mouse has stopped moving. \(Because of
29393 the way Emacs compares mouse events, you will occasionally wait twice
29394 that time before the window gets selected.\)
29395 Any other value means to autoselect window instantaneously when the
29396 mouse pointer enters it.
29397
29398 Autoselection selects the minibuffer only if it is active, and never
29399 unselects the minibuffer if it is active.
29400
29401 When customizing this variable make sure that the actual value of
29402 `focus-follows-mouse' matches the behavior of your window manager. */);
29403 Vmouse_autoselect_window = Qnil;
29404
29405 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29406 doc: /* Non-nil means automatically resize tool-bars.
29407 This dynamically changes the tool-bar's height to the minimum height
29408 that is needed to make all tool-bar items visible.
29409 If value is `grow-only', the tool-bar's height is only increased
29410 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29411 Vauto_resize_tool_bars = Qt;
29412
29413 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29414 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29415 auto_raise_tool_bar_buttons_p = 1;
29416
29417 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29418 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29419 make_cursor_line_fully_visible_p = 1;
29420
29421 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29422 doc: /* Border below tool-bar in pixels.
29423 If an integer, use it as the height of the border.
29424 If it is one of `internal-border-width' or `border-width', use the
29425 value of the corresponding frame parameter.
29426 Otherwise, no border is added below the tool-bar. */);
29427 Vtool_bar_border = Qinternal_border_width;
29428
29429 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29430 doc: /* Margin around tool-bar buttons in pixels.
29431 If an integer, use that for both horizontal and vertical margins.
29432 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29433 HORZ specifying the horizontal margin, and VERT specifying the
29434 vertical margin. */);
29435 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29436
29437 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29438 doc: /* Relief thickness of tool-bar buttons. */);
29439 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29440
29441 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29442 doc: /* Tool bar style to use.
29443 It can be one of
29444 image - show images only
29445 text - show text only
29446 both - show both, text below image
29447 both-horiz - show text to the right of the image
29448 text-image-horiz - show text to the left of the image
29449 any other - use system default or image if no system default.
29450
29451 This variable only affects the GTK+ toolkit version of Emacs. */);
29452 Vtool_bar_style = Qnil;
29453
29454 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29455 doc: /* Maximum number of characters a label can have to be shown.
29456 The tool bar style must also show labels for this to have any effect, see
29457 `tool-bar-style'. */);
29458 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29459
29460 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29461 doc: /* List of functions to call to fontify regions of text.
29462 Each function is called with one argument POS. Functions must
29463 fontify a region starting at POS in the current buffer, and give
29464 fontified regions the property `fontified'. */);
29465 Vfontification_functions = Qnil;
29466 Fmake_variable_buffer_local (Qfontification_functions);
29467
29468 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29469 unibyte_display_via_language_environment,
29470 doc: /* Non-nil means display unibyte text according to language environment.
29471 Specifically, this means that raw bytes in the range 160-255 decimal
29472 are displayed by converting them to the equivalent multibyte characters
29473 according to the current language environment. As a result, they are
29474 displayed according to the current fontset.
29475
29476 Note that this variable affects only how these bytes are displayed,
29477 but does not change the fact they are interpreted as raw bytes. */);
29478 unibyte_display_via_language_environment = 0;
29479
29480 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29481 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29482 If a float, it specifies a fraction of the mini-window frame's height.
29483 If an integer, it specifies a number of lines. */);
29484 Vmax_mini_window_height = make_float (0.25);
29485
29486 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29487 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29488 A value of nil means don't automatically resize mini-windows.
29489 A value of t means resize them to fit the text displayed in them.
29490 A value of `grow-only', the default, means let mini-windows grow only;
29491 they return to their normal size when the minibuffer is closed, or the
29492 echo area becomes empty. */);
29493 Vresize_mini_windows = Qgrow_only;
29494
29495 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29496 doc: /* Alist specifying how to blink the cursor off.
29497 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29498 `cursor-type' frame-parameter or variable equals ON-STATE,
29499 comparing using `equal', Emacs uses OFF-STATE to specify
29500 how to blink it off. ON-STATE and OFF-STATE are values for
29501 the `cursor-type' frame parameter.
29502
29503 If a frame's ON-STATE has no entry in this list,
29504 the frame's other specifications determine how to blink the cursor off. */);
29505 Vblink_cursor_alist = Qnil;
29506
29507 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29508 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29509 If non-nil, windows are automatically scrolled horizontally to make
29510 point visible. */);
29511 automatic_hscrolling_p = 1;
29512 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29513
29514 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29515 doc: /* How many columns away from the window edge point is allowed to get
29516 before automatic hscrolling will horizontally scroll the window. */);
29517 hscroll_margin = 5;
29518
29519 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29520 doc: /* How many columns to scroll the window when point gets too close to the edge.
29521 When point is less than `hscroll-margin' columns from the window
29522 edge, automatic hscrolling will scroll the window by the amount of columns
29523 determined by this variable. If its value is a positive integer, scroll that
29524 many columns. If it's a positive floating-point number, it specifies the
29525 fraction of the window's width to scroll. If it's nil or zero, point will be
29526 centered horizontally after the scroll. Any other value, including negative
29527 numbers, are treated as if the value were zero.
29528
29529 Automatic hscrolling always moves point outside the scroll margin, so if
29530 point was more than scroll step columns inside the margin, the window will
29531 scroll more than the value given by the scroll step.
29532
29533 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29534 and `scroll-right' overrides this variable's effect. */);
29535 Vhscroll_step = make_number (0);
29536
29537 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29538 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29539 Bind this around calls to `message' to let it take effect. */);
29540 message_truncate_lines = 0;
29541
29542 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29543 doc: /* Normal hook run to update the menu bar definitions.
29544 Redisplay runs this hook before it redisplays the menu bar.
29545 This is used to update submenus such as Buffers,
29546 whose contents depend on various data. */);
29547 Vmenu_bar_update_hook = Qnil;
29548
29549 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29550 doc: /* Frame for which we are updating a menu.
29551 The enable predicate for a menu binding should check this variable. */);
29552 Vmenu_updating_frame = Qnil;
29553
29554 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29555 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29556 inhibit_menubar_update = 0;
29557
29558 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29559 doc: /* Prefix prepended to all continuation lines at display time.
29560 The value may be a string, an image, or a stretch-glyph; it is
29561 interpreted in the same way as the value of a `display' text property.
29562
29563 This variable is overridden by any `wrap-prefix' text or overlay
29564 property.
29565
29566 To add a prefix to non-continuation lines, use `line-prefix'. */);
29567 Vwrap_prefix = Qnil;
29568 DEFSYM (Qwrap_prefix, "wrap-prefix");
29569 Fmake_variable_buffer_local (Qwrap_prefix);
29570
29571 DEFVAR_LISP ("line-prefix", Vline_prefix,
29572 doc: /* Prefix prepended to all non-continuation lines at display time.
29573 The value may be a string, an image, or a stretch-glyph; it is
29574 interpreted in the same way as the value of a `display' text property.
29575
29576 This variable is overridden by any `line-prefix' text or overlay
29577 property.
29578
29579 To add a prefix to continuation lines, use `wrap-prefix'. */);
29580 Vline_prefix = Qnil;
29581 DEFSYM (Qline_prefix, "line-prefix");
29582 Fmake_variable_buffer_local (Qline_prefix);
29583
29584 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29585 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29586 inhibit_eval_during_redisplay = 0;
29587
29588 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29589 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29590 inhibit_free_realized_faces = 0;
29591
29592 #ifdef GLYPH_DEBUG
29593 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29594 doc: /* Inhibit try_window_id display optimization. */);
29595 inhibit_try_window_id = 0;
29596
29597 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29598 doc: /* Inhibit try_window_reusing display optimization. */);
29599 inhibit_try_window_reusing = 0;
29600
29601 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29602 doc: /* Inhibit try_cursor_movement display optimization. */);
29603 inhibit_try_cursor_movement = 0;
29604 #endif /* GLYPH_DEBUG */
29605
29606 DEFVAR_INT ("overline-margin", overline_margin,
29607 doc: /* Space between overline and text, in pixels.
29608 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29609 margin to the character height. */);
29610 overline_margin = 2;
29611
29612 DEFVAR_INT ("underline-minimum-offset",
29613 underline_minimum_offset,
29614 doc: /* Minimum distance between baseline and underline.
29615 This can improve legibility of underlined text at small font sizes,
29616 particularly when using variable `x-use-underline-position-properties'
29617 with fonts that specify an UNDERLINE_POSITION relatively close to the
29618 baseline. The default value is 1. */);
29619 underline_minimum_offset = 1;
29620
29621 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29622 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29623 This feature only works when on a window system that can change
29624 cursor shapes. */);
29625 display_hourglass_p = 1;
29626
29627 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29628 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29629 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29630
29631 #ifdef HAVE_WINDOW_SYSTEM
29632 hourglass_atimer = NULL;
29633 hourglass_shown_p = 0;
29634 #endif /* HAVE_WINDOW_SYSTEM */
29635
29636 DEFSYM (Qglyphless_char, "glyphless-char");
29637 DEFSYM (Qhex_code, "hex-code");
29638 DEFSYM (Qempty_box, "empty-box");
29639 DEFSYM (Qthin_space, "thin-space");
29640 DEFSYM (Qzero_width, "zero-width");
29641
29642 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29643 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29644
29645 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29646 doc: /* Char-table defining glyphless characters.
29647 Each element, if non-nil, should be one of the following:
29648 an ASCII acronym string: display this string in a box
29649 `hex-code': display the hexadecimal code of a character in a box
29650 `empty-box': display as an empty box
29651 `thin-space': display as 1-pixel width space
29652 `zero-width': don't display
29653 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29654 display method for graphical terminals and text terminals respectively.
29655 GRAPHICAL and TEXT should each have one of the values listed above.
29656
29657 The char-table has one extra slot to control the display of a character for
29658 which no font is found. This slot only takes effect on graphical terminals.
29659 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29660 `thin-space'. The default is `empty-box'. */);
29661 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29662 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29663 Qempty_box);
29664
29665 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29666 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29667 Vdebug_on_message = Qnil;
29668 }
29669
29670
29671 /* Initialize this module when Emacs starts. */
29672
29673 void
29674 init_xdisp (void)
29675 {
29676 CHARPOS (this_line_start_pos) = 0;
29677
29678 if (!noninteractive)
29679 {
29680 struct window *m = XWINDOW (minibuf_window);
29681 Lisp_Object frame = m->frame;
29682 struct frame *f = XFRAME (frame);
29683 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29684 struct window *r = XWINDOW (root);
29685 int i;
29686
29687 echo_area_window = minibuf_window;
29688
29689 r->top_line = FRAME_TOP_MARGIN (f);
29690 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29691 r->total_cols = FRAME_COLS (f);
29692
29693 m->top_line = FRAME_LINES (f) - 1;
29694 m->total_lines = 1;
29695 m->total_cols = FRAME_COLS (f);
29696
29697 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29698 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29699 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29700
29701 /* The default ellipsis glyphs `...'. */
29702 for (i = 0; i < 3; ++i)
29703 default_invis_vector[i] = make_number ('.');
29704 }
29705
29706 {
29707 /* Allocate the buffer for frame titles.
29708 Also used for `format-mode-line'. */
29709 int size = 100;
29710 mode_line_noprop_buf = xmalloc (size);
29711 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29712 mode_line_noprop_ptr = mode_line_noprop_buf;
29713 mode_line_target = MODE_LINE_DISPLAY;
29714 }
29715
29716 help_echo_showing_p = 0;
29717 }
29718
29719 #ifdef HAVE_WINDOW_SYSTEM
29720
29721 /* Platform-independent portion of hourglass implementation. */
29722
29723 /* Cancel a currently active hourglass timer, and start a new one. */
29724 void
29725 start_hourglass (void)
29726 {
29727 struct timespec delay;
29728
29729 cancel_hourglass ();
29730
29731 if (INTEGERP (Vhourglass_delay)
29732 && XINT (Vhourglass_delay) > 0)
29733 delay = make_timespec (min (XINT (Vhourglass_delay),
29734 TYPE_MAXIMUM (time_t)),
29735 0);
29736 else if (FLOATP (Vhourglass_delay)
29737 && XFLOAT_DATA (Vhourglass_delay) > 0)
29738 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29739 else
29740 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29741
29742 #ifdef HAVE_NTGUI
29743 {
29744 extern void w32_note_current_window (void);
29745 w32_note_current_window ();
29746 }
29747 #endif /* HAVE_NTGUI */
29748
29749 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29750 show_hourglass, NULL);
29751 }
29752
29753
29754 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29755 shown. */
29756 void
29757 cancel_hourglass (void)
29758 {
29759 if (hourglass_atimer)
29760 {
29761 cancel_atimer (hourglass_atimer);
29762 hourglass_atimer = NULL;
29763 }
29764
29765 if (hourglass_shown_p)
29766 hide_hourglass ();
29767 }
29768
29769 #endif /* HAVE_WINDOW_SYSTEM */