Cleanup frame flushing.
[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 {
10771 redraw_frame (f);
10772 f->force_flush_display_p = 1;
10773 }
10774 clear_current_matrices (f);
10775 changed_count++;
10776 f->garbaged = 0;
10777 f->resized_p = 0;
10778 }
10779 }
10780
10781 frame_garbaged = 0;
10782 if (changed_count)
10783 ++windows_or_buffers_changed;
10784 }
10785 }
10786
10787
10788 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10789 is non-zero update selected_frame. Value is non-zero if the
10790 mini-windows height has been changed. */
10791
10792 static int
10793 echo_area_display (int update_frame_p)
10794 {
10795 Lisp_Object mini_window;
10796 struct window *w;
10797 struct frame *f;
10798 int window_height_changed_p = 0;
10799 struct frame *sf = SELECTED_FRAME ();
10800
10801 mini_window = FRAME_MINIBUF_WINDOW (sf);
10802 w = XWINDOW (mini_window);
10803 f = XFRAME (WINDOW_FRAME (w));
10804
10805 /* Don't display if frame is invisible or not yet initialized. */
10806 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10807 return 0;
10808
10809 #ifdef HAVE_WINDOW_SYSTEM
10810 /* When Emacs starts, selected_frame may be the initial terminal
10811 frame. If we let this through, a message would be displayed on
10812 the terminal. */
10813 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10814 return 0;
10815 #endif /* HAVE_WINDOW_SYSTEM */
10816
10817 /* Redraw garbaged frames. */
10818 clear_garbaged_frames ();
10819
10820 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10821 {
10822 echo_area_window = mini_window;
10823 window_height_changed_p = display_echo_area (w);
10824 w->must_be_updated_p = 1;
10825
10826 /* Update the display, unless called from redisplay_internal.
10827 Also don't update the screen during redisplay itself. The
10828 update will happen at the end of redisplay, and an update
10829 here could cause confusion. */
10830 if (update_frame_p && !redisplaying_p)
10831 {
10832 int n = 0;
10833
10834 /* If the display update has been interrupted by pending
10835 input, update mode lines in the frame. Due to the
10836 pending input, it might have been that redisplay hasn't
10837 been called, so that mode lines above the echo area are
10838 garbaged. This looks odd, so we prevent it here. */
10839 if (!display_completed)
10840 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10841
10842 if (window_height_changed_p
10843 /* Don't do this if Emacs is shutting down. Redisplay
10844 needs to run hooks. */
10845 && !NILP (Vrun_hooks))
10846 {
10847 /* Must update other windows. Likewise as in other
10848 cases, don't let this update be interrupted by
10849 pending input. */
10850 ptrdiff_t count = SPECPDL_INDEX ();
10851 specbind (Qredisplay_dont_pause, Qt);
10852 windows_or_buffers_changed = 1;
10853 redisplay_internal ();
10854 unbind_to (count, Qnil);
10855 }
10856 else if (FRAME_WINDOW_P (f) && n == 0)
10857 {
10858 /* Window configuration is the same as before.
10859 Can do with a display update of the echo area,
10860 unless we displayed some mode lines. */
10861 update_single_window (w, 1);
10862 flush_frame (f);
10863 }
10864 else
10865 update_frame (f, 1, 1);
10866
10867 /* If cursor is in the echo area, make sure that the next
10868 redisplay displays the minibuffer, so that the cursor will
10869 be replaced with what the minibuffer wants. */
10870 if (cursor_in_echo_area)
10871 ++windows_or_buffers_changed;
10872 }
10873 }
10874 else if (!EQ (mini_window, selected_window))
10875 windows_or_buffers_changed++;
10876
10877 /* Last displayed message is now the current message. */
10878 echo_area_buffer[1] = echo_area_buffer[0];
10879 /* Inform read_char that we're not echoing. */
10880 echo_message_buffer = Qnil;
10881
10882 /* Prevent redisplay optimization in redisplay_internal by resetting
10883 this_line_start_pos. This is done because the mini-buffer now
10884 displays the message instead of its buffer text. */
10885 if (EQ (mini_window, selected_window))
10886 CHARPOS (this_line_start_pos) = 0;
10887
10888 return window_height_changed_p;
10889 }
10890
10891 /* Nonzero if the current window's buffer is shown in more than one
10892 window and was modified since last redisplay. */
10893
10894 static int
10895 buffer_shared_and_changed (void)
10896 {
10897 return (buffer_window_count (current_buffer) > 1
10898 && UNCHANGED_MODIFIED < MODIFF);
10899 }
10900
10901 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10902 is enabled and mark of W's buffer was changed since last W's update. */
10903
10904 static int
10905 window_buffer_changed (struct window *w)
10906 {
10907 struct buffer *b = XBUFFER (w->contents);
10908
10909 eassert (BUFFER_LIVE_P (b));
10910
10911 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10912 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10913 != (w->region_showing != 0)));
10914 }
10915
10916 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10917
10918 static int
10919 mode_line_update_needed (struct window *w)
10920 {
10921 return (w->column_number_displayed != -1
10922 && !(PT == w->last_point && !window_outdated (w))
10923 && (w->column_number_displayed != current_column ()));
10924 }
10925
10926 /* Nonzero if window start of W is frozen and may not be changed during
10927 redisplay. */
10928
10929 static bool
10930 window_frozen_p (struct window *w)
10931 {
10932 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10933 {
10934 Lisp_Object window;
10935
10936 XSETWINDOW (window, w);
10937 if (MINI_WINDOW_P (w))
10938 return 0;
10939 else if (EQ (window, selected_window))
10940 return 0;
10941 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10942 && EQ (window, Vminibuf_scroll_window))
10943 /* This special window can't be frozen too. */
10944 return 0;
10945 else
10946 return 1;
10947 }
10948 return 0;
10949 }
10950
10951 /***********************************************************************
10952 Mode Lines and Frame Titles
10953 ***********************************************************************/
10954
10955 /* A buffer for constructing non-propertized mode-line strings and
10956 frame titles in it; allocated from the heap in init_xdisp and
10957 resized as needed in store_mode_line_noprop_char. */
10958
10959 static char *mode_line_noprop_buf;
10960
10961 /* The buffer's end, and a current output position in it. */
10962
10963 static char *mode_line_noprop_buf_end;
10964 static char *mode_line_noprop_ptr;
10965
10966 #define MODE_LINE_NOPROP_LEN(start) \
10967 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10968
10969 static enum {
10970 MODE_LINE_DISPLAY = 0,
10971 MODE_LINE_TITLE,
10972 MODE_LINE_NOPROP,
10973 MODE_LINE_STRING
10974 } mode_line_target;
10975
10976 /* Alist that caches the results of :propertize.
10977 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10978 static Lisp_Object mode_line_proptrans_alist;
10979
10980 /* List of strings making up the mode-line. */
10981 static Lisp_Object mode_line_string_list;
10982
10983 /* Base face property when building propertized mode line string. */
10984 static Lisp_Object mode_line_string_face;
10985 static Lisp_Object mode_line_string_face_prop;
10986
10987
10988 /* Unwind data for mode line strings */
10989
10990 static Lisp_Object Vmode_line_unwind_vector;
10991
10992 static Lisp_Object
10993 format_mode_line_unwind_data (struct frame *target_frame,
10994 struct buffer *obuf,
10995 Lisp_Object owin,
10996 int save_proptrans)
10997 {
10998 Lisp_Object vector, tmp;
10999
11000 /* Reduce consing by keeping one vector in
11001 Vwith_echo_area_save_vector. */
11002 vector = Vmode_line_unwind_vector;
11003 Vmode_line_unwind_vector = Qnil;
11004
11005 if (NILP (vector))
11006 vector = Fmake_vector (make_number (10), Qnil);
11007
11008 ASET (vector, 0, make_number (mode_line_target));
11009 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11010 ASET (vector, 2, mode_line_string_list);
11011 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11012 ASET (vector, 4, mode_line_string_face);
11013 ASET (vector, 5, mode_line_string_face_prop);
11014
11015 if (obuf)
11016 XSETBUFFER (tmp, obuf);
11017 else
11018 tmp = Qnil;
11019 ASET (vector, 6, tmp);
11020 ASET (vector, 7, owin);
11021 if (target_frame)
11022 {
11023 /* Similarly to `with-selected-window', if the operation selects
11024 a window on another frame, we must restore that frame's
11025 selected window, and (for a tty) the top-frame. */
11026 ASET (vector, 8, target_frame->selected_window);
11027 if (FRAME_TERMCAP_P (target_frame))
11028 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11029 }
11030
11031 return vector;
11032 }
11033
11034 static void
11035 unwind_format_mode_line (Lisp_Object vector)
11036 {
11037 Lisp_Object old_window = AREF (vector, 7);
11038 Lisp_Object target_frame_window = AREF (vector, 8);
11039 Lisp_Object old_top_frame = AREF (vector, 9);
11040
11041 mode_line_target = XINT (AREF (vector, 0));
11042 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11043 mode_line_string_list = AREF (vector, 2);
11044 if (! EQ (AREF (vector, 3), Qt))
11045 mode_line_proptrans_alist = AREF (vector, 3);
11046 mode_line_string_face = AREF (vector, 4);
11047 mode_line_string_face_prop = AREF (vector, 5);
11048
11049 /* Select window before buffer, since it may change the buffer. */
11050 if (!NILP (old_window))
11051 {
11052 /* If the operation that we are unwinding had selected a window
11053 on a different frame, reset its frame-selected-window. For a
11054 text terminal, reset its top-frame if necessary. */
11055 if (!NILP (target_frame_window))
11056 {
11057 Lisp_Object frame
11058 = WINDOW_FRAME (XWINDOW (target_frame_window));
11059
11060 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11061 Fselect_window (target_frame_window, Qt);
11062
11063 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11064 Fselect_frame (old_top_frame, Qt);
11065 }
11066
11067 Fselect_window (old_window, Qt);
11068 }
11069
11070 if (!NILP (AREF (vector, 6)))
11071 {
11072 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11073 ASET (vector, 6, Qnil);
11074 }
11075
11076 Vmode_line_unwind_vector = vector;
11077 }
11078
11079
11080 /* Store a single character C for the frame title in mode_line_noprop_buf.
11081 Re-allocate mode_line_noprop_buf if necessary. */
11082
11083 static void
11084 store_mode_line_noprop_char (char c)
11085 {
11086 /* If output position has reached the end of the allocated buffer,
11087 increase the buffer's size. */
11088 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11089 {
11090 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11091 ptrdiff_t size = len;
11092 mode_line_noprop_buf =
11093 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11094 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11095 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11096 }
11097
11098 *mode_line_noprop_ptr++ = c;
11099 }
11100
11101
11102 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11103 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11104 characters that yield more columns than PRECISION; PRECISION <= 0
11105 means copy the whole string. Pad with spaces until FIELD_WIDTH
11106 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11107 pad. Called from display_mode_element when it is used to build a
11108 frame title. */
11109
11110 static int
11111 store_mode_line_noprop (const char *string, int field_width, int precision)
11112 {
11113 const unsigned char *str = (const unsigned char *) string;
11114 int n = 0;
11115 ptrdiff_t dummy, nbytes;
11116
11117 /* Copy at most PRECISION chars from STR. */
11118 nbytes = strlen (string);
11119 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11120 while (nbytes--)
11121 store_mode_line_noprop_char (*str++);
11122
11123 /* Fill up with spaces until FIELD_WIDTH reached. */
11124 while (field_width > 0
11125 && n < field_width)
11126 {
11127 store_mode_line_noprop_char (' ');
11128 ++n;
11129 }
11130
11131 return n;
11132 }
11133
11134 /***********************************************************************
11135 Frame Titles
11136 ***********************************************************************/
11137
11138 #ifdef HAVE_WINDOW_SYSTEM
11139
11140 /* Set the title of FRAME, if it has changed. The title format is
11141 Vicon_title_format if FRAME is iconified, otherwise it is
11142 frame_title_format. */
11143
11144 static void
11145 x_consider_frame_title (Lisp_Object frame)
11146 {
11147 struct frame *f = XFRAME (frame);
11148
11149 if (FRAME_WINDOW_P (f)
11150 || FRAME_MINIBUF_ONLY_P (f)
11151 || f->explicit_name)
11152 {
11153 /* Do we have more than one visible frame on this X display? */
11154 Lisp_Object tail, other_frame, fmt;
11155 ptrdiff_t title_start;
11156 char *title;
11157 ptrdiff_t len;
11158 struct it it;
11159 ptrdiff_t count = SPECPDL_INDEX ();
11160
11161 FOR_EACH_FRAME (tail, other_frame)
11162 {
11163 struct frame *tf = XFRAME (other_frame);
11164
11165 if (tf != f
11166 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11167 && !FRAME_MINIBUF_ONLY_P (tf)
11168 && !EQ (other_frame, tip_frame)
11169 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11170 break;
11171 }
11172
11173 /* Set global variable indicating that multiple frames exist. */
11174 multiple_frames = CONSP (tail);
11175
11176 /* Switch to the buffer of selected window of the frame. Set up
11177 mode_line_target so that display_mode_element will output into
11178 mode_line_noprop_buf; then display the title. */
11179 record_unwind_protect (unwind_format_mode_line,
11180 format_mode_line_unwind_data
11181 (f, current_buffer, selected_window, 0));
11182
11183 Fselect_window (f->selected_window, Qt);
11184 set_buffer_internal_1
11185 (XBUFFER (XWINDOW (f->selected_window)->contents));
11186 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11187
11188 mode_line_target = MODE_LINE_TITLE;
11189 title_start = MODE_LINE_NOPROP_LEN (0);
11190 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11191 NULL, DEFAULT_FACE_ID);
11192 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11193 len = MODE_LINE_NOPROP_LEN (title_start);
11194 title = mode_line_noprop_buf + title_start;
11195 unbind_to (count, Qnil);
11196
11197 /* Set the title only if it's changed. This avoids consing in
11198 the common case where it hasn't. (If it turns out that we've
11199 already wasted too much time by walking through the list with
11200 display_mode_element, then we might need to optimize at a
11201 higher level than this.) */
11202 if (! STRINGP (f->name)
11203 || SBYTES (f->name) != len
11204 || memcmp (title, SDATA (f->name), len) != 0)
11205 x_implicitly_set_name (f, make_string (title, len), Qnil);
11206 }
11207 }
11208
11209 #endif /* not HAVE_WINDOW_SYSTEM */
11210
11211 \f
11212 /***********************************************************************
11213 Menu Bars
11214 ***********************************************************************/
11215
11216
11217 /* Prepare for redisplay by updating menu-bar item lists when
11218 appropriate. This can call eval. */
11219
11220 void
11221 prepare_menu_bars (void)
11222 {
11223 int all_windows;
11224 struct gcpro gcpro1, gcpro2;
11225 struct frame *f;
11226 Lisp_Object tooltip_frame;
11227
11228 #ifdef HAVE_WINDOW_SYSTEM
11229 tooltip_frame = tip_frame;
11230 #else
11231 tooltip_frame = Qnil;
11232 #endif
11233
11234 /* Update all frame titles based on their buffer names, etc. We do
11235 this before the menu bars so that the buffer-menu will show the
11236 up-to-date frame titles. */
11237 #ifdef HAVE_WINDOW_SYSTEM
11238 if (windows_or_buffers_changed || update_mode_lines)
11239 {
11240 Lisp_Object tail, frame;
11241
11242 FOR_EACH_FRAME (tail, frame)
11243 {
11244 f = XFRAME (frame);
11245 if (!EQ (frame, tooltip_frame)
11246 && (FRAME_ICONIFIED_P (f)
11247 || FRAME_VISIBLE_P (f) == 1
11248 /* Exclude TTY frames that are obscured because they
11249 are not the top frame on their console. This is
11250 because x_consider_frame_title actually switches
11251 to the frame, which for TTY frames means it is
11252 marked as garbaged, and will be completely
11253 redrawn on the next redisplay cycle. This causes
11254 TTY frames to be completely redrawn, when there
11255 are more than one of them, even though nothing
11256 should be changed on display. */
11257 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11258 x_consider_frame_title (frame);
11259 }
11260 }
11261 #endif /* HAVE_WINDOW_SYSTEM */
11262
11263 /* Update the menu bar item lists, if appropriate. This has to be
11264 done before any actual redisplay or generation of display lines. */
11265 all_windows = (update_mode_lines
11266 || buffer_shared_and_changed ()
11267 || windows_or_buffers_changed);
11268 if (all_windows)
11269 {
11270 Lisp_Object tail, frame;
11271 ptrdiff_t count = SPECPDL_INDEX ();
11272 /* 1 means that update_menu_bar has run its hooks
11273 so any further calls to update_menu_bar shouldn't do so again. */
11274 int menu_bar_hooks_run = 0;
11275
11276 record_unwind_save_match_data ();
11277
11278 FOR_EACH_FRAME (tail, frame)
11279 {
11280 f = XFRAME (frame);
11281
11282 /* Ignore tooltip frame. */
11283 if (EQ (frame, tooltip_frame))
11284 continue;
11285
11286 /* If a window on this frame changed size, report that to
11287 the user and clear the size-change flag. */
11288 if (FRAME_WINDOW_SIZES_CHANGED (f))
11289 {
11290 Lisp_Object functions;
11291
11292 /* Clear flag first in case we get an error below. */
11293 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11294 functions = Vwindow_size_change_functions;
11295 GCPRO2 (tail, functions);
11296
11297 while (CONSP (functions))
11298 {
11299 if (!EQ (XCAR (functions), Qt))
11300 call1 (XCAR (functions), frame);
11301 functions = XCDR (functions);
11302 }
11303 UNGCPRO;
11304 }
11305
11306 GCPRO1 (tail);
11307 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11308 #ifdef HAVE_WINDOW_SYSTEM
11309 update_tool_bar (f, 0);
11310 #endif
11311 #ifdef HAVE_NS
11312 if (windows_or_buffers_changed
11313 && FRAME_NS_P (f))
11314 ns_set_doc_edited
11315 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11316 #endif
11317 UNGCPRO;
11318 }
11319
11320 unbind_to (count, Qnil);
11321 }
11322 else
11323 {
11324 struct frame *sf = SELECTED_FRAME ();
11325 update_menu_bar (sf, 1, 0);
11326 #ifdef HAVE_WINDOW_SYSTEM
11327 update_tool_bar (sf, 1);
11328 #endif
11329 }
11330 }
11331
11332
11333 /* Update the menu bar item list for frame F. This has to be done
11334 before we start to fill in any display lines, because it can call
11335 eval.
11336
11337 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11338
11339 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11340 already ran the menu bar hooks for this redisplay, so there
11341 is no need to run them again. The return value is the
11342 updated value of this flag, to pass to the next call. */
11343
11344 static int
11345 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11346 {
11347 Lisp_Object window;
11348 register struct window *w;
11349
11350 /* If called recursively during a menu update, do nothing. This can
11351 happen when, for instance, an activate-menubar-hook causes a
11352 redisplay. */
11353 if (inhibit_menubar_update)
11354 return hooks_run;
11355
11356 window = FRAME_SELECTED_WINDOW (f);
11357 w = XWINDOW (window);
11358
11359 if (FRAME_WINDOW_P (f)
11360 ?
11361 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11362 || defined (HAVE_NS) || defined (USE_GTK)
11363 FRAME_EXTERNAL_MENU_BAR (f)
11364 #else
11365 FRAME_MENU_BAR_LINES (f) > 0
11366 #endif
11367 : FRAME_MENU_BAR_LINES (f) > 0)
11368 {
11369 /* If the user has switched buffers or windows, we need to
11370 recompute to reflect the new bindings. But we'll
11371 recompute when update_mode_lines is set too; that means
11372 that people can use force-mode-line-update to request
11373 that the menu bar be recomputed. The adverse effect on
11374 the rest of the redisplay algorithm is about the same as
11375 windows_or_buffers_changed anyway. */
11376 if (windows_or_buffers_changed
11377 /* This used to test w->update_mode_line, but we believe
11378 there is no need to recompute the menu in that case. */
11379 || update_mode_lines
11380 || window_buffer_changed (w))
11381 {
11382 struct buffer *prev = current_buffer;
11383 ptrdiff_t count = SPECPDL_INDEX ();
11384
11385 specbind (Qinhibit_menubar_update, Qt);
11386
11387 set_buffer_internal_1 (XBUFFER (w->contents));
11388 if (save_match_data)
11389 record_unwind_save_match_data ();
11390 if (NILP (Voverriding_local_map_menu_flag))
11391 {
11392 specbind (Qoverriding_terminal_local_map, Qnil);
11393 specbind (Qoverriding_local_map, Qnil);
11394 }
11395
11396 if (!hooks_run)
11397 {
11398 /* Run the Lucid hook. */
11399 safe_run_hooks (Qactivate_menubar_hook);
11400
11401 /* If it has changed current-menubar from previous value,
11402 really recompute the menu-bar from the value. */
11403 if (! NILP (Vlucid_menu_bar_dirty_flag))
11404 call0 (Qrecompute_lucid_menubar);
11405
11406 safe_run_hooks (Qmenu_bar_update_hook);
11407
11408 hooks_run = 1;
11409 }
11410
11411 XSETFRAME (Vmenu_updating_frame, f);
11412 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11413
11414 /* Redisplay the menu bar in case we changed it. */
11415 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11416 || defined (HAVE_NS) || defined (USE_GTK)
11417 if (FRAME_WINDOW_P (f))
11418 {
11419 #if defined (HAVE_NS)
11420 /* All frames on Mac OS share the same menubar. So only
11421 the selected frame should be allowed to set it. */
11422 if (f == SELECTED_FRAME ())
11423 #endif
11424 set_frame_menubar (f, 0, 0);
11425 }
11426 else
11427 /* On a terminal screen, the menu bar is an ordinary screen
11428 line, and this makes it get updated. */
11429 w->update_mode_line = 1;
11430 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11431 /* In the non-toolkit version, the menu bar is an ordinary screen
11432 line, and this makes it get updated. */
11433 w->update_mode_line = 1;
11434 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11435
11436 unbind_to (count, Qnil);
11437 set_buffer_internal_1 (prev);
11438 }
11439 }
11440
11441 return hooks_run;
11442 }
11443
11444 /***********************************************************************
11445 Tool-bars
11446 ***********************************************************************/
11447
11448 #ifdef HAVE_WINDOW_SYSTEM
11449
11450 /* Where the mouse was last time we reported a mouse event. */
11451
11452 struct frame *last_mouse_frame;
11453
11454 /* Tool-bar item index of the item on which a mouse button was pressed
11455 or -1. */
11456
11457 int last_tool_bar_item;
11458
11459 /* Select `frame' temporarily without running all the code in
11460 do_switch_frame.
11461 FIXME: Maybe do_switch_frame should be trimmed down similarly
11462 when `norecord' is set. */
11463 static void
11464 fast_set_selected_frame (Lisp_Object frame)
11465 {
11466 if (!EQ (selected_frame, frame))
11467 {
11468 selected_frame = frame;
11469 selected_window = XFRAME (frame)->selected_window;
11470 }
11471 }
11472
11473 /* Update the tool-bar item list for frame F. This has to be done
11474 before we start to fill in any display lines. Called from
11475 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11476 and restore it here. */
11477
11478 static void
11479 update_tool_bar (struct frame *f, int save_match_data)
11480 {
11481 #if defined (USE_GTK) || defined (HAVE_NS)
11482 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11483 #else
11484 int do_update = WINDOWP (f->tool_bar_window)
11485 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11486 #endif
11487
11488 if (do_update)
11489 {
11490 Lisp_Object window;
11491 struct window *w;
11492
11493 window = FRAME_SELECTED_WINDOW (f);
11494 w = XWINDOW (window);
11495
11496 /* If the user has switched buffers or windows, we need to
11497 recompute to reflect the new bindings. But we'll
11498 recompute when update_mode_lines is set too; that means
11499 that people can use force-mode-line-update to request
11500 that the menu bar be recomputed. The adverse effect on
11501 the rest of the redisplay algorithm is about the same as
11502 windows_or_buffers_changed anyway. */
11503 if (windows_or_buffers_changed
11504 || w->update_mode_line
11505 || update_mode_lines
11506 || window_buffer_changed (w))
11507 {
11508 struct buffer *prev = current_buffer;
11509 ptrdiff_t count = SPECPDL_INDEX ();
11510 Lisp_Object frame, new_tool_bar;
11511 int new_n_tool_bar;
11512 struct gcpro gcpro1;
11513
11514 /* Set current_buffer to the buffer of the selected
11515 window of the frame, so that we get the right local
11516 keymaps. */
11517 set_buffer_internal_1 (XBUFFER (w->contents));
11518
11519 /* Save match data, if we must. */
11520 if (save_match_data)
11521 record_unwind_save_match_data ();
11522
11523 /* Make sure that we don't accidentally use bogus keymaps. */
11524 if (NILP (Voverriding_local_map_menu_flag))
11525 {
11526 specbind (Qoverriding_terminal_local_map, Qnil);
11527 specbind (Qoverriding_local_map, Qnil);
11528 }
11529
11530 GCPRO1 (new_tool_bar);
11531
11532 /* We must temporarily set the selected frame to this frame
11533 before calling tool_bar_items, because the calculation of
11534 the tool-bar keymap uses the selected frame (see
11535 `tool-bar-make-keymap' in tool-bar.el). */
11536 eassert (EQ (selected_window,
11537 /* Since we only explicitly preserve selected_frame,
11538 check that selected_window would be redundant. */
11539 XFRAME (selected_frame)->selected_window));
11540 record_unwind_protect (fast_set_selected_frame, selected_frame);
11541 XSETFRAME (frame, f);
11542 fast_set_selected_frame (frame);
11543
11544 /* Build desired tool-bar items from keymaps. */
11545 new_tool_bar
11546 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11547 &new_n_tool_bar);
11548
11549 /* Redisplay the tool-bar if we changed it. */
11550 if (new_n_tool_bar != f->n_tool_bar_items
11551 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11552 {
11553 /* Redisplay that happens asynchronously due to an expose event
11554 may access f->tool_bar_items. Make sure we update both
11555 variables within BLOCK_INPUT so no such event interrupts. */
11556 block_input ();
11557 fset_tool_bar_items (f, new_tool_bar);
11558 f->n_tool_bar_items = new_n_tool_bar;
11559 w->update_mode_line = 1;
11560 unblock_input ();
11561 }
11562
11563 UNGCPRO;
11564
11565 unbind_to (count, Qnil);
11566 set_buffer_internal_1 (prev);
11567 }
11568 }
11569 }
11570
11571
11572 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11573 F's desired tool-bar contents. F->tool_bar_items must have
11574 been set up previously by calling prepare_menu_bars. */
11575
11576 static void
11577 build_desired_tool_bar_string (struct frame *f)
11578 {
11579 int i, size, size_needed;
11580 struct gcpro gcpro1, gcpro2, gcpro3;
11581 Lisp_Object image, plist, props;
11582
11583 image = plist = props = Qnil;
11584 GCPRO3 (image, plist, props);
11585
11586 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11587 Otherwise, make a new string. */
11588
11589 /* The size of the string we might be able to reuse. */
11590 size = (STRINGP (f->desired_tool_bar_string)
11591 ? SCHARS (f->desired_tool_bar_string)
11592 : 0);
11593
11594 /* We need one space in the string for each image. */
11595 size_needed = f->n_tool_bar_items;
11596
11597 /* Reuse f->desired_tool_bar_string, if possible. */
11598 if (size < size_needed || NILP (f->desired_tool_bar_string))
11599 fset_desired_tool_bar_string
11600 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11601 else
11602 {
11603 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11604 Fremove_text_properties (make_number (0), make_number (size),
11605 props, f->desired_tool_bar_string);
11606 }
11607
11608 /* Put a `display' property on the string for the images to display,
11609 put a `menu_item' property on tool-bar items with a value that
11610 is the index of the item in F's tool-bar item vector. */
11611 for (i = 0; i < f->n_tool_bar_items; ++i)
11612 {
11613 #define PROP(IDX) \
11614 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11615
11616 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11617 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11618 int hmargin, vmargin, relief, idx, end;
11619
11620 /* If image is a vector, choose the image according to the
11621 button state. */
11622 image = PROP (TOOL_BAR_ITEM_IMAGES);
11623 if (VECTORP (image))
11624 {
11625 if (enabled_p)
11626 idx = (selected_p
11627 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11628 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11629 else
11630 idx = (selected_p
11631 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11632 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11633
11634 eassert (ASIZE (image) >= idx);
11635 image = AREF (image, idx);
11636 }
11637 else
11638 idx = -1;
11639
11640 /* Ignore invalid image specifications. */
11641 if (!valid_image_p (image))
11642 continue;
11643
11644 /* Display the tool-bar button pressed, or depressed. */
11645 plist = Fcopy_sequence (XCDR (image));
11646
11647 /* Compute margin and relief to draw. */
11648 relief = (tool_bar_button_relief >= 0
11649 ? tool_bar_button_relief
11650 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11651 hmargin = vmargin = relief;
11652
11653 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11654 INT_MAX - max (hmargin, vmargin)))
11655 {
11656 hmargin += XFASTINT (Vtool_bar_button_margin);
11657 vmargin += XFASTINT (Vtool_bar_button_margin);
11658 }
11659 else if (CONSP (Vtool_bar_button_margin))
11660 {
11661 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11662 INT_MAX - hmargin))
11663 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11664
11665 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11666 INT_MAX - vmargin))
11667 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11668 }
11669
11670 if (auto_raise_tool_bar_buttons_p)
11671 {
11672 /* Add a `:relief' property to the image spec if the item is
11673 selected. */
11674 if (selected_p)
11675 {
11676 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11677 hmargin -= relief;
11678 vmargin -= relief;
11679 }
11680 }
11681 else
11682 {
11683 /* If image is selected, display it pressed, i.e. with a
11684 negative relief. If it's not selected, display it with a
11685 raised relief. */
11686 plist = Fplist_put (plist, QCrelief,
11687 (selected_p
11688 ? make_number (-relief)
11689 : make_number (relief)));
11690 hmargin -= relief;
11691 vmargin -= relief;
11692 }
11693
11694 /* Put a margin around the image. */
11695 if (hmargin || vmargin)
11696 {
11697 if (hmargin == vmargin)
11698 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11699 else
11700 plist = Fplist_put (plist, QCmargin,
11701 Fcons (make_number (hmargin),
11702 make_number (vmargin)));
11703 }
11704
11705 /* If button is not enabled, and we don't have special images
11706 for the disabled state, make the image appear disabled by
11707 applying an appropriate algorithm to it. */
11708 if (!enabled_p && idx < 0)
11709 plist = Fplist_put (plist, QCconversion, Qdisabled);
11710
11711 /* Put a `display' text property on the string for the image to
11712 display. Put a `menu-item' property on the string that gives
11713 the start of this item's properties in the tool-bar items
11714 vector. */
11715 image = Fcons (Qimage, plist);
11716 props = list4 (Qdisplay, image,
11717 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11718
11719 /* Let the last image hide all remaining spaces in the tool bar
11720 string. The string can be longer than needed when we reuse a
11721 previous string. */
11722 if (i + 1 == f->n_tool_bar_items)
11723 end = SCHARS (f->desired_tool_bar_string);
11724 else
11725 end = i + 1;
11726 Fadd_text_properties (make_number (i), make_number (end),
11727 props, f->desired_tool_bar_string);
11728 #undef PROP
11729 }
11730
11731 UNGCPRO;
11732 }
11733
11734
11735 /* Display one line of the tool-bar of frame IT->f.
11736
11737 HEIGHT specifies the desired height of the tool-bar line.
11738 If the actual height of the glyph row is less than HEIGHT, the
11739 row's height is increased to HEIGHT, and the icons are centered
11740 vertically in the new height.
11741
11742 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11743 count a final empty row in case the tool-bar width exactly matches
11744 the window width.
11745 */
11746
11747 static void
11748 display_tool_bar_line (struct it *it, int height)
11749 {
11750 struct glyph_row *row = it->glyph_row;
11751 int max_x = it->last_visible_x;
11752 struct glyph *last;
11753
11754 prepare_desired_row (row);
11755 row->y = it->current_y;
11756
11757 /* Note that this isn't made use of if the face hasn't a box,
11758 so there's no need to check the face here. */
11759 it->start_of_box_run_p = 1;
11760
11761 while (it->current_x < max_x)
11762 {
11763 int x, n_glyphs_before, i, nglyphs;
11764 struct it it_before;
11765
11766 /* Get the next display element. */
11767 if (!get_next_display_element (it))
11768 {
11769 /* Don't count empty row if we are counting needed tool-bar lines. */
11770 if (height < 0 && !it->hpos)
11771 return;
11772 break;
11773 }
11774
11775 /* Produce glyphs. */
11776 n_glyphs_before = row->used[TEXT_AREA];
11777 it_before = *it;
11778
11779 PRODUCE_GLYPHS (it);
11780
11781 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11782 i = 0;
11783 x = it_before.current_x;
11784 while (i < nglyphs)
11785 {
11786 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11787
11788 if (x + glyph->pixel_width > max_x)
11789 {
11790 /* Glyph doesn't fit on line. Backtrack. */
11791 row->used[TEXT_AREA] = n_glyphs_before;
11792 *it = it_before;
11793 /* If this is the only glyph on this line, it will never fit on the
11794 tool-bar, so skip it. But ensure there is at least one glyph,
11795 so we don't accidentally disable the tool-bar. */
11796 if (n_glyphs_before == 0
11797 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11798 break;
11799 goto out;
11800 }
11801
11802 ++it->hpos;
11803 x += glyph->pixel_width;
11804 ++i;
11805 }
11806
11807 /* Stop at line end. */
11808 if (ITERATOR_AT_END_OF_LINE_P (it))
11809 break;
11810
11811 set_iterator_to_next (it, 1);
11812 }
11813
11814 out:;
11815
11816 row->displays_text_p = row->used[TEXT_AREA] != 0;
11817
11818 /* Use default face for the border below the tool bar.
11819
11820 FIXME: When auto-resize-tool-bars is grow-only, there is
11821 no additional border below the possibly empty tool-bar lines.
11822 So to make the extra empty lines look "normal", we have to
11823 use the tool-bar face for the border too. */
11824 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11825 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11826 it->face_id = DEFAULT_FACE_ID;
11827
11828 extend_face_to_end_of_line (it);
11829 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11830 last->right_box_line_p = 1;
11831 if (last == row->glyphs[TEXT_AREA])
11832 last->left_box_line_p = 1;
11833
11834 /* Make line the desired height and center it vertically. */
11835 if ((height -= it->max_ascent + it->max_descent) > 0)
11836 {
11837 /* Don't add more than one line height. */
11838 height %= FRAME_LINE_HEIGHT (it->f);
11839 it->max_ascent += height / 2;
11840 it->max_descent += (height + 1) / 2;
11841 }
11842
11843 compute_line_metrics (it);
11844
11845 /* If line is empty, make it occupy the rest of the tool-bar. */
11846 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11847 {
11848 row->height = row->phys_height = it->last_visible_y - row->y;
11849 row->visible_height = row->height;
11850 row->ascent = row->phys_ascent = 0;
11851 row->extra_line_spacing = 0;
11852 }
11853
11854 row->full_width_p = 1;
11855 row->continued_p = 0;
11856 row->truncated_on_left_p = 0;
11857 row->truncated_on_right_p = 0;
11858
11859 it->current_x = it->hpos = 0;
11860 it->current_y += row->height;
11861 ++it->vpos;
11862 ++it->glyph_row;
11863 }
11864
11865
11866 /* Max tool-bar height. */
11867
11868 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11869 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11870
11871 /* Value is the number of screen lines needed to make all tool-bar
11872 items of frame F visible. The number of actual rows needed is
11873 returned in *N_ROWS if non-NULL. */
11874
11875 static int
11876 tool_bar_lines_needed (struct frame *f, int *n_rows)
11877 {
11878 struct window *w = XWINDOW (f->tool_bar_window);
11879 struct it it;
11880 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11881 the desired matrix, so use (unused) mode-line row as temporary row to
11882 avoid destroying the first tool-bar row. */
11883 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11884
11885 /* Initialize an iterator for iteration over
11886 F->desired_tool_bar_string in the tool-bar window of frame F. */
11887 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11888 it.first_visible_x = 0;
11889 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11890 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11891 it.paragraph_embedding = L2R;
11892
11893 while (!ITERATOR_AT_END_P (&it))
11894 {
11895 clear_glyph_row (temp_row);
11896 it.glyph_row = temp_row;
11897 display_tool_bar_line (&it, -1);
11898 }
11899 clear_glyph_row (temp_row);
11900
11901 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11902 if (n_rows)
11903 *n_rows = it.vpos > 0 ? it.vpos : -1;
11904
11905 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11906 }
11907
11908
11909 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11910 0, 1, 0,
11911 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11912 If FRAME is nil or omitted, use the selected frame. */)
11913 (Lisp_Object frame)
11914 {
11915 struct frame *f = decode_any_frame (frame);
11916 struct window *w;
11917 int nlines = 0;
11918
11919 if (WINDOWP (f->tool_bar_window)
11920 && (w = XWINDOW (f->tool_bar_window),
11921 WINDOW_TOTAL_LINES (w) > 0))
11922 {
11923 update_tool_bar (f, 1);
11924 if (f->n_tool_bar_items)
11925 {
11926 build_desired_tool_bar_string (f);
11927 nlines = tool_bar_lines_needed (f, NULL);
11928 }
11929 }
11930
11931 return make_number (nlines);
11932 }
11933
11934
11935 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11936 height should be changed. */
11937
11938 static int
11939 redisplay_tool_bar (struct frame *f)
11940 {
11941 struct window *w;
11942 struct it it;
11943 struct glyph_row *row;
11944
11945 #if defined (USE_GTK) || defined (HAVE_NS)
11946 if (FRAME_EXTERNAL_TOOL_BAR (f))
11947 update_frame_tool_bar (f);
11948 return 0;
11949 #endif
11950
11951 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11952 do anything. This means you must start with tool-bar-lines
11953 non-zero to get the auto-sizing effect. Or in other words, you
11954 can turn off tool-bars by specifying tool-bar-lines zero. */
11955 if (!WINDOWP (f->tool_bar_window)
11956 || (w = XWINDOW (f->tool_bar_window),
11957 WINDOW_TOTAL_LINES (w) == 0))
11958 return 0;
11959
11960 /* Set up an iterator for the tool-bar window. */
11961 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11962 it.first_visible_x = 0;
11963 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11964 row = it.glyph_row;
11965
11966 /* Build a string that represents the contents of the tool-bar. */
11967 build_desired_tool_bar_string (f);
11968 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11969 /* FIXME: This should be controlled by a user option. But it
11970 doesn't make sense to have an R2L tool bar if the menu bar cannot
11971 be drawn also R2L, and making the menu bar R2L is tricky due
11972 toolkit-specific code that implements it. If an R2L tool bar is
11973 ever supported, display_tool_bar_line should also be augmented to
11974 call unproduce_glyphs like display_line and display_string
11975 do. */
11976 it.paragraph_embedding = L2R;
11977
11978 if (f->n_tool_bar_rows == 0)
11979 {
11980 int nlines;
11981
11982 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11983 nlines != WINDOW_TOTAL_LINES (w)))
11984 {
11985 Lisp_Object frame;
11986 int old_height = WINDOW_TOTAL_LINES (w);
11987
11988 XSETFRAME (frame, f);
11989 Fmodify_frame_parameters (frame,
11990 list1 (Fcons (Qtool_bar_lines,
11991 make_number (nlines))));
11992 if (WINDOW_TOTAL_LINES (w) != old_height)
11993 {
11994 clear_glyph_matrix (w->desired_matrix);
11995 f->fonts_changed = 1;
11996 return 1;
11997 }
11998 }
11999 }
12000
12001 /* Display as many lines as needed to display all tool-bar items. */
12002
12003 if (f->n_tool_bar_rows > 0)
12004 {
12005 int border, rows, height, extra;
12006
12007 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12008 border = XINT (Vtool_bar_border);
12009 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12010 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12011 else if (EQ (Vtool_bar_border, Qborder_width))
12012 border = f->border_width;
12013 else
12014 border = 0;
12015 if (border < 0)
12016 border = 0;
12017
12018 rows = f->n_tool_bar_rows;
12019 height = max (1, (it.last_visible_y - border) / rows);
12020 extra = it.last_visible_y - border - height * rows;
12021
12022 while (it.current_y < it.last_visible_y)
12023 {
12024 int h = 0;
12025 if (extra > 0 && rows-- > 0)
12026 {
12027 h = (extra + rows - 1) / rows;
12028 extra -= h;
12029 }
12030 display_tool_bar_line (&it, height + h);
12031 }
12032 }
12033 else
12034 {
12035 while (it.current_y < it.last_visible_y)
12036 display_tool_bar_line (&it, 0);
12037 }
12038
12039 /* It doesn't make much sense to try scrolling in the tool-bar
12040 window, so don't do it. */
12041 w->desired_matrix->no_scrolling_p = 1;
12042 w->must_be_updated_p = 1;
12043
12044 if (!NILP (Vauto_resize_tool_bars))
12045 {
12046 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12047 int change_height_p = 0;
12048
12049 /* If we couldn't display everything, change the tool-bar's
12050 height if there is room for more. */
12051 if (IT_STRING_CHARPOS (it) < it.end_charpos
12052 && it.current_y < max_tool_bar_height)
12053 change_height_p = 1;
12054
12055 row = it.glyph_row - 1;
12056
12057 /* If there are blank lines at the end, except for a partially
12058 visible blank line at the end that is smaller than
12059 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12060 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12061 && row->height >= FRAME_LINE_HEIGHT (f))
12062 change_height_p = 1;
12063
12064 /* If row displays tool-bar items, but is partially visible,
12065 change the tool-bar's height. */
12066 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12067 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12068 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12069 change_height_p = 1;
12070
12071 /* Resize windows as needed by changing the `tool-bar-lines'
12072 frame parameter. */
12073 if (change_height_p)
12074 {
12075 Lisp_Object frame;
12076 int old_height = WINDOW_TOTAL_LINES (w);
12077 int nrows;
12078 int nlines = tool_bar_lines_needed (f, &nrows);
12079
12080 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12081 && !f->minimize_tool_bar_window_p)
12082 ? (nlines > old_height)
12083 : (nlines != old_height));
12084 f->minimize_tool_bar_window_p = 0;
12085
12086 if (change_height_p)
12087 {
12088 XSETFRAME (frame, f);
12089 Fmodify_frame_parameters (frame,
12090 list1 (Fcons (Qtool_bar_lines,
12091 make_number (nlines))));
12092 if (WINDOW_TOTAL_LINES (w) != old_height)
12093 {
12094 clear_glyph_matrix (w->desired_matrix);
12095 f->n_tool_bar_rows = nrows;
12096 f->fonts_changed = 1;
12097 return 1;
12098 }
12099 }
12100 }
12101 }
12102
12103 f->minimize_tool_bar_window_p = 0;
12104 return 0;
12105 }
12106
12107
12108 /* Get information about the tool-bar item which is displayed in GLYPH
12109 on frame F. Return in *PROP_IDX the index where tool-bar item
12110 properties start in F->tool_bar_items. Value is zero if
12111 GLYPH doesn't display a tool-bar item. */
12112
12113 static int
12114 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12115 {
12116 Lisp_Object prop;
12117 int success_p;
12118 int charpos;
12119
12120 /* This function can be called asynchronously, which means we must
12121 exclude any possibility that Fget_text_property signals an
12122 error. */
12123 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12124 charpos = max (0, charpos);
12125
12126 /* Get the text property `menu-item' at pos. The value of that
12127 property is the start index of this item's properties in
12128 F->tool_bar_items. */
12129 prop = Fget_text_property (make_number (charpos),
12130 Qmenu_item, f->current_tool_bar_string);
12131 if (INTEGERP (prop))
12132 {
12133 *prop_idx = XINT (prop);
12134 success_p = 1;
12135 }
12136 else
12137 success_p = 0;
12138
12139 return success_p;
12140 }
12141
12142 \f
12143 /* Get information about the tool-bar item at position X/Y on frame F.
12144 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12145 the current matrix of the tool-bar window of F, or NULL if not
12146 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12147 item in F->tool_bar_items. Value is
12148
12149 -1 if X/Y is not on a tool-bar item
12150 0 if X/Y is on the same item that was highlighted before.
12151 1 otherwise. */
12152
12153 static int
12154 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12155 int *hpos, int *vpos, int *prop_idx)
12156 {
12157 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12158 struct window *w = XWINDOW (f->tool_bar_window);
12159 int area;
12160
12161 /* Find the glyph under X/Y. */
12162 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12163 if (*glyph == NULL)
12164 return -1;
12165
12166 /* Get the start of this tool-bar item's properties in
12167 f->tool_bar_items. */
12168 if (!tool_bar_item_info (f, *glyph, prop_idx))
12169 return -1;
12170
12171 /* Is mouse on the highlighted item? */
12172 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12173 && *vpos >= hlinfo->mouse_face_beg_row
12174 && *vpos <= hlinfo->mouse_face_end_row
12175 && (*vpos > hlinfo->mouse_face_beg_row
12176 || *hpos >= hlinfo->mouse_face_beg_col)
12177 && (*vpos < hlinfo->mouse_face_end_row
12178 || *hpos < hlinfo->mouse_face_end_col
12179 || hlinfo->mouse_face_past_end))
12180 return 0;
12181
12182 return 1;
12183 }
12184
12185
12186 /* EXPORT:
12187 Handle mouse button event on the tool-bar of frame F, at
12188 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12189 0 for button release. MODIFIERS is event modifiers for button
12190 release. */
12191
12192 void
12193 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12194 int modifiers)
12195 {
12196 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12197 struct window *w = XWINDOW (f->tool_bar_window);
12198 int hpos, vpos, prop_idx;
12199 struct glyph *glyph;
12200 Lisp_Object enabled_p;
12201 int ts;
12202
12203 /* If not on the highlighted tool-bar item, and mouse-highlight is
12204 non-nil, return. This is so we generate the tool-bar button
12205 click only when the mouse button is released on the same item as
12206 where it was pressed. However, when mouse-highlight is disabled,
12207 generate the click when the button is released regardless of the
12208 highlight, since tool-bar items are not highlighted in that
12209 case. */
12210 frame_to_window_pixel_xy (w, &x, &y);
12211 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12212 if (ts == -1
12213 || (ts != 0 && !NILP (Vmouse_highlight)))
12214 return;
12215
12216 /* When mouse-highlight is off, generate the click for the item
12217 where the button was pressed, disregarding where it was
12218 released. */
12219 if (NILP (Vmouse_highlight) && !down_p)
12220 prop_idx = last_tool_bar_item;
12221
12222 /* If item is disabled, do nothing. */
12223 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12224 if (NILP (enabled_p))
12225 return;
12226
12227 if (down_p)
12228 {
12229 /* Show item in pressed state. */
12230 if (!NILP (Vmouse_highlight))
12231 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12232 last_tool_bar_item = prop_idx;
12233 }
12234 else
12235 {
12236 Lisp_Object key, frame;
12237 struct input_event event;
12238 EVENT_INIT (event);
12239
12240 /* Show item in released state. */
12241 if (!NILP (Vmouse_highlight))
12242 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12243
12244 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12245
12246 XSETFRAME (frame, f);
12247 event.kind = TOOL_BAR_EVENT;
12248 event.frame_or_window = frame;
12249 event.arg = frame;
12250 kbd_buffer_store_event (&event);
12251
12252 event.kind = TOOL_BAR_EVENT;
12253 event.frame_or_window = frame;
12254 event.arg = key;
12255 event.modifiers = modifiers;
12256 kbd_buffer_store_event (&event);
12257 last_tool_bar_item = -1;
12258 }
12259 }
12260
12261
12262 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12263 tool-bar window-relative coordinates X/Y. Called from
12264 note_mouse_highlight. */
12265
12266 static void
12267 note_tool_bar_highlight (struct frame *f, int x, int y)
12268 {
12269 Lisp_Object window = f->tool_bar_window;
12270 struct window *w = XWINDOW (window);
12271 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12272 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12273 int hpos, vpos;
12274 struct glyph *glyph;
12275 struct glyph_row *row;
12276 int i;
12277 Lisp_Object enabled_p;
12278 int prop_idx;
12279 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12280 int mouse_down_p, rc;
12281
12282 /* Function note_mouse_highlight is called with negative X/Y
12283 values when mouse moves outside of the frame. */
12284 if (x <= 0 || y <= 0)
12285 {
12286 clear_mouse_face (hlinfo);
12287 return;
12288 }
12289
12290 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12291 if (rc < 0)
12292 {
12293 /* Not on tool-bar item. */
12294 clear_mouse_face (hlinfo);
12295 return;
12296 }
12297 else if (rc == 0)
12298 /* On same tool-bar item as before. */
12299 goto set_help_echo;
12300
12301 clear_mouse_face (hlinfo);
12302
12303 /* Mouse is down, but on different tool-bar item? */
12304 mouse_down_p = (dpyinfo->grabbed
12305 && f == last_mouse_frame
12306 && FRAME_LIVE_P (f));
12307 if (mouse_down_p
12308 && last_tool_bar_item != prop_idx)
12309 return;
12310
12311 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12312
12313 /* If tool-bar item is not enabled, don't highlight it. */
12314 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12315 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12316 {
12317 /* Compute the x-position of the glyph. In front and past the
12318 image is a space. We include this in the highlighted area. */
12319 row = MATRIX_ROW (w->current_matrix, vpos);
12320 for (i = x = 0; i < hpos; ++i)
12321 x += row->glyphs[TEXT_AREA][i].pixel_width;
12322
12323 /* Record this as the current active region. */
12324 hlinfo->mouse_face_beg_col = hpos;
12325 hlinfo->mouse_face_beg_row = vpos;
12326 hlinfo->mouse_face_beg_x = x;
12327 hlinfo->mouse_face_past_end = 0;
12328
12329 hlinfo->mouse_face_end_col = hpos + 1;
12330 hlinfo->mouse_face_end_row = vpos;
12331 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12332 hlinfo->mouse_face_window = window;
12333 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12334
12335 /* Display it as active. */
12336 show_mouse_face (hlinfo, draw);
12337 }
12338
12339 set_help_echo:
12340
12341 /* Set help_echo_string to a help string to display for this tool-bar item.
12342 XTread_socket does the rest. */
12343 help_echo_object = help_echo_window = Qnil;
12344 help_echo_pos = -1;
12345 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12346 if (NILP (help_echo_string))
12347 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12348 }
12349
12350 #endif /* HAVE_WINDOW_SYSTEM */
12351
12352
12353 \f
12354 /************************************************************************
12355 Horizontal scrolling
12356 ************************************************************************/
12357
12358 static int hscroll_window_tree (Lisp_Object);
12359 static int hscroll_windows (Lisp_Object);
12360
12361 /* For all leaf windows in the window tree rooted at WINDOW, set their
12362 hscroll value so that PT is (i) visible in the window, and (ii) so
12363 that it is not within a certain margin at the window's left and
12364 right border. Value is non-zero if any window's hscroll has been
12365 changed. */
12366
12367 static int
12368 hscroll_window_tree (Lisp_Object window)
12369 {
12370 int hscrolled_p = 0;
12371 int hscroll_relative_p = FLOATP (Vhscroll_step);
12372 int hscroll_step_abs = 0;
12373 double hscroll_step_rel = 0;
12374
12375 if (hscroll_relative_p)
12376 {
12377 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12378 if (hscroll_step_rel < 0)
12379 {
12380 hscroll_relative_p = 0;
12381 hscroll_step_abs = 0;
12382 }
12383 }
12384 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12385 {
12386 hscroll_step_abs = XINT (Vhscroll_step);
12387 if (hscroll_step_abs < 0)
12388 hscroll_step_abs = 0;
12389 }
12390 else
12391 hscroll_step_abs = 0;
12392
12393 while (WINDOWP (window))
12394 {
12395 struct window *w = XWINDOW (window);
12396
12397 if (WINDOWP (w->contents))
12398 hscrolled_p |= hscroll_window_tree (w->contents);
12399 else if (w->cursor.vpos >= 0)
12400 {
12401 int h_margin;
12402 int text_area_width;
12403 struct glyph_row *current_cursor_row
12404 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12405 struct glyph_row *desired_cursor_row
12406 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12407 struct glyph_row *cursor_row
12408 = (desired_cursor_row->enabled_p
12409 ? desired_cursor_row
12410 : current_cursor_row);
12411 int row_r2l_p = cursor_row->reversed_p;
12412
12413 text_area_width = window_box_width (w, TEXT_AREA);
12414
12415 /* Scroll when cursor is inside this scroll margin. */
12416 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12417
12418 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12419 /* For left-to-right rows, hscroll when cursor is either
12420 (i) inside the right hscroll margin, or (ii) if it is
12421 inside the left margin and the window is already
12422 hscrolled. */
12423 && ((!row_r2l_p
12424 && ((w->hscroll
12425 && w->cursor.x <= h_margin)
12426 || (cursor_row->enabled_p
12427 && cursor_row->truncated_on_right_p
12428 && (w->cursor.x >= text_area_width - h_margin))))
12429 /* For right-to-left rows, the logic is similar,
12430 except that rules for scrolling to left and right
12431 are reversed. E.g., if cursor.x <= h_margin, we
12432 need to hscroll "to the right" unconditionally,
12433 and that will scroll the screen to the left so as
12434 to reveal the next portion of the row. */
12435 || (row_r2l_p
12436 && ((cursor_row->enabled_p
12437 /* FIXME: It is confusing to set the
12438 truncated_on_right_p flag when R2L rows
12439 are actually truncated on the left. */
12440 && cursor_row->truncated_on_right_p
12441 && w->cursor.x <= h_margin)
12442 || (w->hscroll
12443 && (w->cursor.x >= text_area_width - h_margin))))))
12444 {
12445 struct it it;
12446 ptrdiff_t hscroll;
12447 struct buffer *saved_current_buffer;
12448 ptrdiff_t pt;
12449 int wanted_x;
12450
12451 /* Find point in a display of infinite width. */
12452 saved_current_buffer = current_buffer;
12453 current_buffer = XBUFFER (w->contents);
12454
12455 if (w == XWINDOW (selected_window))
12456 pt = PT;
12457 else
12458 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12459
12460 /* Move iterator to pt starting at cursor_row->start in
12461 a line with infinite width. */
12462 init_to_row_start (&it, w, cursor_row);
12463 it.last_visible_x = INFINITY;
12464 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12465 current_buffer = saved_current_buffer;
12466
12467 /* Position cursor in window. */
12468 if (!hscroll_relative_p && hscroll_step_abs == 0)
12469 hscroll = max (0, (it.current_x
12470 - (ITERATOR_AT_END_OF_LINE_P (&it)
12471 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12472 : (text_area_width / 2))))
12473 / FRAME_COLUMN_WIDTH (it.f);
12474 else if ((!row_r2l_p
12475 && w->cursor.x >= text_area_width - h_margin)
12476 || (row_r2l_p && w->cursor.x <= h_margin))
12477 {
12478 if (hscroll_relative_p)
12479 wanted_x = text_area_width * (1 - hscroll_step_rel)
12480 - h_margin;
12481 else
12482 wanted_x = text_area_width
12483 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12484 - h_margin;
12485 hscroll
12486 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12487 }
12488 else
12489 {
12490 if (hscroll_relative_p)
12491 wanted_x = text_area_width * hscroll_step_rel
12492 + h_margin;
12493 else
12494 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12495 + h_margin;
12496 hscroll
12497 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12498 }
12499 hscroll = max (hscroll, w->min_hscroll);
12500
12501 /* Don't prevent redisplay optimizations if hscroll
12502 hasn't changed, as it will unnecessarily slow down
12503 redisplay. */
12504 if (w->hscroll != hscroll)
12505 {
12506 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12507 w->hscroll = hscroll;
12508 hscrolled_p = 1;
12509 }
12510 }
12511 }
12512
12513 window = w->next;
12514 }
12515
12516 /* Value is non-zero if hscroll of any leaf window has been changed. */
12517 return hscrolled_p;
12518 }
12519
12520
12521 /* Set hscroll so that cursor is visible and not inside horizontal
12522 scroll margins for all windows in the tree rooted at WINDOW. See
12523 also hscroll_window_tree above. Value is non-zero if any window's
12524 hscroll has been changed. If it has, desired matrices on the frame
12525 of WINDOW are cleared. */
12526
12527 static int
12528 hscroll_windows (Lisp_Object window)
12529 {
12530 int hscrolled_p = hscroll_window_tree (window);
12531 if (hscrolled_p)
12532 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12533 return hscrolled_p;
12534 }
12535
12536
12537 \f
12538 /************************************************************************
12539 Redisplay
12540 ************************************************************************/
12541
12542 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12543 to a non-zero value. This is sometimes handy to have in a debugger
12544 session. */
12545
12546 #ifdef GLYPH_DEBUG
12547
12548 /* First and last unchanged row for try_window_id. */
12549
12550 static int debug_first_unchanged_at_end_vpos;
12551 static int debug_last_unchanged_at_beg_vpos;
12552
12553 /* Delta vpos and y. */
12554
12555 static int debug_dvpos, debug_dy;
12556
12557 /* Delta in characters and bytes for try_window_id. */
12558
12559 static ptrdiff_t debug_delta, debug_delta_bytes;
12560
12561 /* Values of window_end_pos and window_end_vpos at the end of
12562 try_window_id. */
12563
12564 static ptrdiff_t debug_end_vpos;
12565
12566 /* Append a string to W->desired_matrix->method. FMT is a printf
12567 format string. If trace_redisplay_p is non-zero also printf the
12568 resulting string to stderr. */
12569
12570 static void debug_method_add (struct window *, char const *, ...)
12571 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12572
12573 static void
12574 debug_method_add (struct window *w, char const *fmt, ...)
12575 {
12576 void *ptr = w;
12577 char *method = w->desired_matrix->method;
12578 int len = strlen (method);
12579 int size = sizeof w->desired_matrix->method;
12580 int remaining = size - len - 1;
12581 va_list ap;
12582
12583 if (len && remaining)
12584 {
12585 method[len] = '|';
12586 --remaining, ++len;
12587 }
12588
12589 va_start (ap, fmt);
12590 vsnprintf (method + len, remaining + 1, fmt, ap);
12591 va_end (ap);
12592
12593 if (trace_redisplay_p)
12594 fprintf (stderr, "%p (%s): %s\n",
12595 ptr,
12596 ((BUFFERP (w->contents)
12597 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12598 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12599 : "no buffer"),
12600 method + len);
12601 }
12602
12603 #endif /* GLYPH_DEBUG */
12604
12605
12606 /* Value is non-zero if all changes in window W, which displays
12607 current_buffer, are in the text between START and END. START is a
12608 buffer position, END is given as a distance from Z. Used in
12609 redisplay_internal for display optimization. */
12610
12611 static int
12612 text_outside_line_unchanged_p (struct window *w,
12613 ptrdiff_t start, ptrdiff_t end)
12614 {
12615 int unchanged_p = 1;
12616
12617 /* If text or overlays have changed, see where. */
12618 if (window_outdated (w))
12619 {
12620 /* Gap in the line? */
12621 if (GPT < start || Z - GPT < end)
12622 unchanged_p = 0;
12623
12624 /* Changes start in front of the line, or end after it? */
12625 if (unchanged_p
12626 && (BEG_UNCHANGED < start - 1
12627 || END_UNCHANGED < end))
12628 unchanged_p = 0;
12629
12630 /* If selective display, can't optimize if changes start at the
12631 beginning of the line. */
12632 if (unchanged_p
12633 && INTEGERP (BVAR (current_buffer, selective_display))
12634 && XINT (BVAR (current_buffer, selective_display)) > 0
12635 && (BEG_UNCHANGED < start || GPT <= start))
12636 unchanged_p = 0;
12637
12638 /* If there are overlays at the start or end of the line, these
12639 may have overlay strings with newlines in them. A change at
12640 START, for instance, may actually concern the display of such
12641 overlay strings as well, and they are displayed on different
12642 lines. So, quickly rule out this case. (For the future, it
12643 might be desirable to implement something more telling than
12644 just BEG/END_UNCHANGED.) */
12645 if (unchanged_p)
12646 {
12647 if (BEG + BEG_UNCHANGED == start
12648 && overlay_touches_p (start))
12649 unchanged_p = 0;
12650 if (END_UNCHANGED == end
12651 && overlay_touches_p (Z - end))
12652 unchanged_p = 0;
12653 }
12654
12655 /* Under bidi reordering, adding or deleting a character in the
12656 beginning of a paragraph, before the first strong directional
12657 character, can change the base direction of the paragraph (unless
12658 the buffer specifies a fixed paragraph direction), which will
12659 require to redisplay the whole paragraph. It might be worthwhile
12660 to find the paragraph limits and widen the range of redisplayed
12661 lines to that, but for now just give up this optimization. */
12662 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12663 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12664 unchanged_p = 0;
12665 }
12666
12667 return unchanged_p;
12668 }
12669
12670
12671 /* Do a frame update, taking possible shortcuts into account. This is
12672 the main external entry point for redisplay.
12673
12674 If the last redisplay displayed an echo area message and that message
12675 is no longer requested, we clear the echo area or bring back the
12676 mini-buffer if that is in use. */
12677
12678 void
12679 redisplay (void)
12680 {
12681 redisplay_internal ();
12682 }
12683
12684
12685 static Lisp_Object
12686 overlay_arrow_string_or_property (Lisp_Object var)
12687 {
12688 Lisp_Object val;
12689
12690 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12691 return val;
12692
12693 return Voverlay_arrow_string;
12694 }
12695
12696 /* Return 1 if there are any overlay-arrows in current_buffer. */
12697 static int
12698 overlay_arrow_in_current_buffer_p (void)
12699 {
12700 Lisp_Object vlist;
12701
12702 for (vlist = Voverlay_arrow_variable_list;
12703 CONSP (vlist);
12704 vlist = XCDR (vlist))
12705 {
12706 Lisp_Object var = XCAR (vlist);
12707 Lisp_Object val;
12708
12709 if (!SYMBOLP (var))
12710 continue;
12711 val = find_symbol_value (var);
12712 if (MARKERP (val)
12713 && current_buffer == XMARKER (val)->buffer)
12714 return 1;
12715 }
12716 return 0;
12717 }
12718
12719
12720 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12721 has changed. */
12722
12723 static int
12724 overlay_arrows_changed_p (void)
12725 {
12726 Lisp_Object vlist;
12727
12728 for (vlist = Voverlay_arrow_variable_list;
12729 CONSP (vlist);
12730 vlist = XCDR (vlist))
12731 {
12732 Lisp_Object var = XCAR (vlist);
12733 Lisp_Object val, pstr;
12734
12735 if (!SYMBOLP (var))
12736 continue;
12737 val = find_symbol_value (var);
12738 if (!MARKERP (val))
12739 continue;
12740 if (! EQ (COERCE_MARKER (val),
12741 Fget (var, Qlast_arrow_position))
12742 || ! (pstr = overlay_arrow_string_or_property (var),
12743 EQ (pstr, Fget (var, Qlast_arrow_string))))
12744 return 1;
12745 }
12746 return 0;
12747 }
12748
12749 /* Mark overlay arrows to be updated on next redisplay. */
12750
12751 static void
12752 update_overlay_arrows (int up_to_date)
12753 {
12754 Lisp_Object vlist;
12755
12756 for (vlist = Voverlay_arrow_variable_list;
12757 CONSP (vlist);
12758 vlist = XCDR (vlist))
12759 {
12760 Lisp_Object var = XCAR (vlist);
12761
12762 if (!SYMBOLP (var))
12763 continue;
12764
12765 if (up_to_date > 0)
12766 {
12767 Lisp_Object val = find_symbol_value (var);
12768 Fput (var, Qlast_arrow_position,
12769 COERCE_MARKER (val));
12770 Fput (var, Qlast_arrow_string,
12771 overlay_arrow_string_or_property (var));
12772 }
12773 else if (up_to_date < 0
12774 || !NILP (Fget (var, Qlast_arrow_position)))
12775 {
12776 Fput (var, Qlast_arrow_position, Qt);
12777 Fput (var, Qlast_arrow_string, Qt);
12778 }
12779 }
12780 }
12781
12782
12783 /* Return overlay arrow string to display at row.
12784 Return integer (bitmap number) for arrow bitmap in left fringe.
12785 Return nil if no overlay arrow. */
12786
12787 static Lisp_Object
12788 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12789 {
12790 Lisp_Object vlist;
12791
12792 for (vlist = Voverlay_arrow_variable_list;
12793 CONSP (vlist);
12794 vlist = XCDR (vlist))
12795 {
12796 Lisp_Object var = XCAR (vlist);
12797 Lisp_Object val;
12798
12799 if (!SYMBOLP (var))
12800 continue;
12801
12802 val = find_symbol_value (var);
12803
12804 if (MARKERP (val)
12805 && current_buffer == XMARKER (val)->buffer
12806 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12807 {
12808 if (FRAME_WINDOW_P (it->f)
12809 /* FIXME: if ROW->reversed_p is set, this should test
12810 the right fringe, not the left one. */
12811 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12812 {
12813 #ifdef HAVE_WINDOW_SYSTEM
12814 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12815 {
12816 int fringe_bitmap;
12817 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12818 return make_number (fringe_bitmap);
12819 }
12820 #endif
12821 return make_number (-1); /* Use default arrow bitmap. */
12822 }
12823 return overlay_arrow_string_or_property (var);
12824 }
12825 }
12826
12827 return Qnil;
12828 }
12829
12830 /* Return 1 if point moved out of or into a composition. Otherwise
12831 return 0. PREV_BUF and PREV_PT are the last point buffer and
12832 position. BUF and PT are the current point buffer and position. */
12833
12834 static int
12835 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12836 struct buffer *buf, ptrdiff_t pt)
12837 {
12838 ptrdiff_t start, end;
12839 Lisp_Object prop;
12840 Lisp_Object buffer;
12841
12842 XSETBUFFER (buffer, buf);
12843 /* Check a composition at the last point if point moved within the
12844 same buffer. */
12845 if (prev_buf == buf)
12846 {
12847 if (prev_pt == pt)
12848 /* Point didn't move. */
12849 return 0;
12850
12851 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12852 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12853 && composition_valid_p (start, end, prop)
12854 && start < prev_pt && end > prev_pt)
12855 /* The last point was within the composition. Return 1 iff
12856 point moved out of the composition. */
12857 return (pt <= start || pt >= end);
12858 }
12859
12860 /* Check a composition at the current point. */
12861 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12862 && find_composition (pt, -1, &start, &end, &prop, buffer)
12863 && composition_valid_p (start, end, prop)
12864 && start < pt && end > pt);
12865 }
12866
12867 /* Reconsider the clip changes of buffer which is displayed in W. */
12868
12869 static void
12870 reconsider_clip_changes (struct window *w)
12871 {
12872 struct buffer *b = XBUFFER (w->contents);
12873
12874 if (b->clip_changed
12875 && w->window_end_valid
12876 && w->current_matrix->buffer == b
12877 && w->current_matrix->zv == BUF_ZV (b)
12878 && w->current_matrix->begv == BUF_BEGV (b))
12879 b->clip_changed = 0;
12880
12881 /* If display wasn't paused, and W is not a tool bar window, see if
12882 point has been moved into or out of a composition. In that case,
12883 we set b->clip_changed to 1 to force updating the screen. If
12884 b->clip_changed has already been set to 1, we can skip this
12885 check. */
12886 if (!b->clip_changed && w->window_end_valid)
12887 {
12888 ptrdiff_t pt = (w == XWINDOW (selected_window)
12889 ? PT : marker_position (w->pointm));
12890
12891 if ((w->current_matrix->buffer != b || pt != w->last_point)
12892 && check_point_in_composition (w->current_matrix->buffer,
12893 w->last_point, b, pt))
12894 b->clip_changed = 1;
12895 }
12896 }
12897
12898 #define STOP_POLLING \
12899 do { if (! polling_stopped_here) stop_polling (); \
12900 polling_stopped_here = 1; } while (0)
12901
12902 #define RESUME_POLLING \
12903 do { if (polling_stopped_here) start_polling (); \
12904 polling_stopped_here = 0; } while (0)
12905
12906
12907 /* Perhaps in the future avoid recentering windows if it
12908 is not necessary; currently that causes some problems. */
12909
12910 static void
12911 redisplay_internal (void)
12912 {
12913 struct window *w = XWINDOW (selected_window);
12914 struct window *sw;
12915 struct frame *fr;
12916 int pending;
12917 bool must_finish = 0, match_p;
12918 struct text_pos tlbufpos, tlendpos;
12919 int number_of_visible_frames;
12920 ptrdiff_t count;
12921 struct frame *sf;
12922 int polling_stopped_here = 0;
12923 Lisp_Object tail, frame;
12924
12925 /* Non-zero means redisplay has to consider all windows on all
12926 frames. Zero means, only selected_window is considered. */
12927 int consider_all_windows_p;
12928
12929 /* Non-zero means redisplay has to redisplay the miniwindow. */
12930 int update_miniwindow_p = 0;
12931
12932 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12933
12934 /* No redisplay if running in batch mode or frame is not yet fully
12935 initialized, or redisplay is explicitly turned off by setting
12936 Vinhibit_redisplay. */
12937 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12938 || !NILP (Vinhibit_redisplay))
12939 return;
12940
12941 /* Don't examine these until after testing Vinhibit_redisplay.
12942 When Emacs is shutting down, perhaps because its connection to
12943 X has dropped, we should not look at them at all. */
12944 fr = XFRAME (w->frame);
12945 sf = SELECTED_FRAME ();
12946
12947 if (!fr->glyphs_initialized_p)
12948 return;
12949
12950 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12951 if (popup_activated ())
12952 return;
12953 #endif
12954
12955 /* I don't think this happens but let's be paranoid. */
12956 if (redisplaying_p)
12957 return;
12958
12959 /* Record a function that clears redisplaying_p
12960 when we leave this function. */
12961 count = SPECPDL_INDEX ();
12962 record_unwind_protect_void (unwind_redisplay);
12963 redisplaying_p = 1;
12964 specbind (Qinhibit_free_realized_faces, Qnil);
12965
12966 /* Record this function, so it appears on the profiler's backtraces. */
12967 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12968
12969 FOR_EACH_FRAME (tail, frame)
12970 XFRAME (frame)->already_hscrolled_p = 0;
12971
12972 retry:
12973 /* Remember the currently selected window. */
12974 sw = w;
12975
12976 pending = 0;
12977 last_escape_glyph_frame = NULL;
12978 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12979 last_glyphless_glyph_frame = NULL;
12980 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12981
12982 /* If face_change_count is non-zero, init_iterator will free all
12983 realized faces, which includes the faces referenced from current
12984 matrices. So, we can't reuse current matrices in this case. */
12985 if (face_change_count)
12986 ++windows_or_buffers_changed;
12987
12988 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12989 && FRAME_TTY (sf)->previous_frame != sf)
12990 {
12991 /* Since frames on a single ASCII terminal share the same
12992 display area, displaying a different frame means redisplay
12993 the whole thing. */
12994 windows_or_buffers_changed++;
12995 SET_FRAME_GARBAGED (sf);
12996 #ifndef DOS_NT
12997 set_tty_color_mode (FRAME_TTY (sf), sf);
12998 #endif
12999 FRAME_TTY (sf)->previous_frame = sf;
13000 }
13001
13002 /* Set the visible flags for all frames. Do this before checking for
13003 resized or garbaged frames; they want to know if their frames are
13004 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13005 number_of_visible_frames = 0;
13006
13007 FOR_EACH_FRAME (tail, frame)
13008 {
13009 struct frame *f = XFRAME (frame);
13010
13011 if (FRAME_VISIBLE_P (f))
13012 {
13013 ++number_of_visible_frames;
13014 /* Adjust matrices for visible frames only. */
13015 if (f->fonts_changed)
13016 {
13017 adjust_frame_glyphs (f);
13018 f->fonts_changed = 0;
13019 }
13020 /* If cursor type has been changed on the frame
13021 other than selected, consider all frames. */
13022 if (f != sf && f->cursor_type_changed)
13023 update_mode_lines++;
13024 }
13025 clear_desired_matrices (f);
13026 }
13027
13028 /* Notice any pending interrupt request to change frame size. */
13029 do_pending_window_change (1);
13030
13031 /* do_pending_window_change could change the selected_window due to
13032 frame resizing which makes the selected window too small. */
13033 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13034 sw = w;
13035
13036 /* Clear frames marked as garbaged. */
13037 clear_garbaged_frames ();
13038
13039 /* Build menubar and tool-bar items. */
13040 if (NILP (Vmemory_full))
13041 prepare_menu_bars ();
13042
13043 if (windows_or_buffers_changed)
13044 update_mode_lines++;
13045
13046 reconsider_clip_changes (w);
13047
13048 /* In most cases selected window displays current buffer. */
13049 match_p = XBUFFER (w->contents) == current_buffer;
13050 if (match_p)
13051 {
13052 ptrdiff_t count1;
13053
13054 /* Detect case that we need to write or remove a star in the mode line. */
13055 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13056 {
13057 w->update_mode_line = 1;
13058 if (buffer_shared_and_changed ())
13059 update_mode_lines++;
13060 }
13061
13062 /* Avoid invocation of point motion hooks by `current_column' below. */
13063 count1 = SPECPDL_INDEX ();
13064 specbind (Qinhibit_point_motion_hooks, Qt);
13065
13066 if (mode_line_update_needed (w))
13067 w->update_mode_line = 1;
13068
13069 unbind_to (count1, Qnil);
13070 }
13071
13072 consider_all_windows_p = (update_mode_lines
13073 || buffer_shared_and_changed ());
13074
13075 /* If specs for an arrow have changed, do thorough redisplay
13076 to ensure we remove any arrow that should no longer exist. */
13077 if (overlay_arrows_changed_p ())
13078 consider_all_windows_p = windows_or_buffers_changed = 1;
13079
13080 /* Normally the message* functions will have already displayed and
13081 updated the echo area, but the frame may have been trashed, or
13082 the update may have been preempted, so display the echo area
13083 again here. Checking message_cleared_p captures the case that
13084 the echo area should be cleared. */
13085 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13086 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13087 || (message_cleared_p
13088 && minibuf_level == 0
13089 /* If the mini-window is currently selected, this means the
13090 echo-area doesn't show through. */
13091 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13092 {
13093 int window_height_changed_p = echo_area_display (0);
13094
13095 if (message_cleared_p)
13096 update_miniwindow_p = 1;
13097
13098 must_finish = 1;
13099
13100 /* If we don't display the current message, don't clear the
13101 message_cleared_p flag, because, if we did, we wouldn't clear
13102 the echo area in the next redisplay which doesn't preserve
13103 the echo area. */
13104 if (!display_last_displayed_message_p)
13105 message_cleared_p = 0;
13106
13107 if (window_height_changed_p)
13108 {
13109 consider_all_windows_p = 1;
13110 ++update_mode_lines;
13111 ++windows_or_buffers_changed;
13112
13113 /* If window configuration was changed, frames may have been
13114 marked garbaged. Clear them or we will experience
13115 surprises wrt scrolling. */
13116 clear_garbaged_frames ();
13117 }
13118 }
13119 else if (EQ (selected_window, minibuf_window)
13120 && (current_buffer->clip_changed || window_outdated (w))
13121 && resize_mini_window (w, 0))
13122 {
13123 /* Resized active mini-window to fit the size of what it is
13124 showing if its contents might have changed. */
13125 must_finish = 1;
13126 /* FIXME: this causes all frames to be updated, which seems unnecessary
13127 since only the current frame needs to be considered. This function
13128 needs to be rewritten with two variables, consider_all_windows and
13129 consider_all_frames. */
13130 consider_all_windows_p = 1;
13131 ++windows_or_buffers_changed;
13132 ++update_mode_lines;
13133
13134 /* If window configuration was changed, frames may have been
13135 marked garbaged. Clear them or we will experience
13136 surprises wrt scrolling. */
13137 clear_garbaged_frames ();
13138 }
13139
13140 /* If showing the region, and mark has changed, we must redisplay
13141 the whole window. The assignment to this_line_start_pos prevents
13142 the optimization directly below this if-statement. */
13143 if (((!NILP (Vtransient_mark_mode)
13144 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13145 != (w->region_showing > 0))
13146 || (w->region_showing
13147 && w->region_showing
13148 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13149 CHARPOS (this_line_start_pos) = 0;
13150
13151 /* Optimize the case that only the line containing the cursor in the
13152 selected window has changed. Variables starting with this_ are
13153 set in display_line and record information about the line
13154 containing the cursor. */
13155 tlbufpos = this_line_start_pos;
13156 tlendpos = this_line_end_pos;
13157 if (!consider_all_windows_p
13158 && CHARPOS (tlbufpos) > 0
13159 && !w->update_mode_line
13160 && !current_buffer->clip_changed
13161 && !current_buffer->prevent_redisplay_optimizations_p
13162 && FRAME_VISIBLE_P (XFRAME (w->frame))
13163 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13164 && !XFRAME (w->frame)->cursor_type_changed
13165 /* Make sure recorded data applies to current buffer, etc. */
13166 && this_line_buffer == current_buffer
13167 && match_p
13168 && !w->force_start
13169 && !w->optional_new_start
13170 /* Point must be on the line that we have info recorded about. */
13171 && PT >= CHARPOS (tlbufpos)
13172 && PT <= Z - CHARPOS (tlendpos)
13173 /* All text outside that line, including its final newline,
13174 must be unchanged. */
13175 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13176 CHARPOS (tlendpos)))
13177 {
13178 if (CHARPOS (tlbufpos) > BEGV
13179 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13180 && (CHARPOS (tlbufpos) == ZV
13181 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13182 /* Former continuation line has disappeared by becoming empty. */
13183 goto cancel;
13184 else if (window_outdated (w) || MINI_WINDOW_P (w))
13185 {
13186 /* We have to handle the case of continuation around a
13187 wide-column character (see the comment in indent.c around
13188 line 1340).
13189
13190 For instance, in the following case:
13191
13192 -------- Insert --------
13193 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13194 J_I_ ==> J_I_ `^^' are cursors.
13195 ^^ ^^
13196 -------- --------
13197
13198 As we have to redraw the line above, we cannot use this
13199 optimization. */
13200
13201 struct it it;
13202 int line_height_before = this_line_pixel_height;
13203
13204 /* Note that start_display will handle the case that the
13205 line starting at tlbufpos is a continuation line. */
13206 start_display (&it, w, tlbufpos);
13207
13208 /* Implementation note: It this still necessary? */
13209 if (it.current_x != this_line_start_x)
13210 goto cancel;
13211
13212 TRACE ((stderr, "trying display optimization 1\n"));
13213 w->cursor.vpos = -1;
13214 overlay_arrow_seen = 0;
13215 it.vpos = this_line_vpos;
13216 it.current_y = this_line_y;
13217 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13218 display_line (&it);
13219
13220 /* If line contains point, is not continued,
13221 and ends at same distance from eob as before, we win. */
13222 if (w->cursor.vpos >= 0
13223 /* Line is not continued, otherwise this_line_start_pos
13224 would have been set to 0 in display_line. */
13225 && CHARPOS (this_line_start_pos)
13226 /* Line ends as before. */
13227 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13228 /* Line has same height as before. Otherwise other lines
13229 would have to be shifted up or down. */
13230 && this_line_pixel_height == line_height_before)
13231 {
13232 /* If this is not the window's last line, we must adjust
13233 the charstarts of the lines below. */
13234 if (it.current_y < it.last_visible_y)
13235 {
13236 struct glyph_row *row
13237 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13238 ptrdiff_t delta, delta_bytes;
13239
13240 /* We used to distinguish between two cases here,
13241 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13242 when the line ends in a newline or the end of the
13243 buffer's accessible portion. But both cases did
13244 the same, so they were collapsed. */
13245 delta = (Z
13246 - CHARPOS (tlendpos)
13247 - MATRIX_ROW_START_CHARPOS (row));
13248 delta_bytes = (Z_BYTE
13249 - BYTEPOS (tlendpos)
13250 - MATRIX_ROW_START_BYTEPOS (row));
13251
13252 increment_matrix_positions (w->current_matrix,
13253 this_line_vpos + 1,
13254 w->current_matrix->nrows,
13255 delta, delta_bytes);
13256 }
13257
13258 /* If this row displays text now but previously didn't,
13259 or vice versa, w->window_end_vpos may have to be
13260 adjusted. */
13261 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13262 {
13263 if (w->window_end_vpos < this_line_vpos)
13264 w->window_end_vpos = this_line_vpos;
13265 }
13266 else if (w->window_end_vpos == this_line_vpos
13267 && this_line_vpos > 0)
13268 w->window_end_vpos = this_line_vpos - 1;
13269 w->window_end_valid = 0;
13270
13271 /* Update hint: No need to try to scroll in update_window. */
13272 w->desired_matrix->no_scrolling_p = 1;
13273
13274 #ifdef GLYPH_DEBUG
13275 *w->desired_matrix->method = 0;
13276 debug_method_add (w, "optimization 1");
13277 #endif
13278 #ifdef HAVE_WINDOW_SYSTEM
13279 update_window_fringes (w, 0);
13280 #endif
13281 goto update;
13282 }
13283 else
13284 goto cancel;
13285 }
13286 else if (/* Cursor position hasn't changed. */
13287 PT == w->last_point
13288 /* Make sure the cursor was last displayed
13289 in this window. Otherwise we have to reposition it. */
13290 && 0 <= w->cursor.vpos
13291 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13292 {
13293 if (!must_finish)
13294 {
13295 do_pending_window_change (1);
13296 /* If selected_window changed, redisplay again. */
13297 if (WINDOWP (selected_window)
13298 && (w = XWINDOW (selected_window)) != sw)
13299 goto retry;
13300
13301 /* We used to always goto end_of_redisplay here, but this
13302 isn't enough if we have a blinking cursor. */
13303 if (w->cursor_off_p == w->last_cursor_off_p)
13304 goto end_of_redisplay;
13305 }
13306 goto update;
13307 }
13308 /* If highlighting the region, or if the cursor is in the echo area,
13309 then we can't just move the cursor. */
13310 else if (! (!NILP (Vtransient_mark_mode)
13311 && !NILP (BVAR (current_buffer, mark_active)))
13312 && (EQ (selected_window,
13313 BVAR (current_buffer, last_selected_window))
13314 || highlight_nonselected_windows)
13315 && !w->region_showing
13316 && NILP (Vshow_trailing_whitespace)
13317 && !cursor_in_echo_area)
13318 {
13319 struct it it;
13320 struct glyph_row *row;
13321
13322 /* Skip from tlbufpos to PT and see where it is. Note that
13323 PT may be in invisible text. If so, we will end at the
13324 next visible position. */
13325 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13326 NULL, DEFAULT_FACE_ID);
13327 it.current_x = this_line_start_x;
13328 it.current_y = this_line_y;
13329 it.vpos = this_line_vpos;
13330
13331 /* The call to move_it_to stops in front of PT, but
13332 moves over before-strings. */
13333 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13334
13335 if (it.vpos == this_line_vpos
13336 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13337 row->enabled_p))
13338 {
13339 eassert (this_line_vpos == it.vpos);
13340 eassert (this_line_y == it.current_y);
13341 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13342 #ifdef GLYPH_DEBUG
13343 *w->desired_matrix->method = 0;
13344 debug_method_add (w, "optimization 3");
13345 #endif
13346 goto update;
13347 }
13348 else
13349 goto cancel;
13350 }
13351
13352 cancel:
13353 /* Text changed drastically or point moved off of line. */
13354 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13355 }
13356
13357 CHARPOS (this_line_start_pos) = 0;
13358 consider_all_windows_p |= buffer_shared_and_changed ();
13359 ++clear_face_cache_count;
13360 #ifdef HAVE_WINDOW_SYSTEM
13361 ++clear_image_cache_count;
13362 #endif
13363
13364 /* Build desired matrices, and update the display. If
13365 consider_all_windows_p is non-zero, do it for all windows on all
13366 frames. Otherwise do it for selected_window, only. */
13367
13368 if (consider_all_windows_p)
13369 {
13370 FOR_EACH_FRAME (tail, frame)
13371 XFRAME (frame)->updated_p = 0;
13372
13373 FOR_EACH_FRAME (tail, frame)
13374 {
13375 struct frame *f = XFRAME (frame);
13376
13377 /* We don't have to do anything for unselected terminal
13378 frames. */
13379 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13380 && !EQ (FRAME_TTY (f)->top_frame, frame))
13381 continue;
13382
13383 retry_frame:
13384
13385 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13386 {
13387 /* Mark all the scroll bars to be removed; we'll redeem
13388 the ones we want when we redisplay their windows. */
13389 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13390 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13391
13392 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13393 redisplay_windows (FRAME_ROOT_WINDOW (f));
13394
13395 /* The X error handler may have deleted that frame. */
13396 if (!FRAME_LIVE_P (f))
13397 continue;
13398
13399 /* Any scroll bars which redisplay_windows should have
13400 nuked should now go away. */
13401 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13402 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13403
13404 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13405 {
13406 /* If fonts changed on visible frame, display again. */
13407 if (f->fonts_changed)
13408 {
13409 adjust_frame_glyphs (f);
13410 f->fonts_changed = 0;
13411 goto retry_frame;
13412 }
13413
13414 /* See if we have to hscroll. */
13415 if (!f->already_hscrolled_p)
13416 {
13417 f->already_hscrolled_p = 1;
13418 if (hscroll_windows (f->root_window))
13419 goto retry_frame;
13420 }
13421
13422 /* Prevent various kinds of signals during display
13423 update. stdio is not robust about handling
13424 signals, which can cause an apparent I/O
13425 error. */
13426 if (interrupt_input)
13427 unrequest_sigio ();
13428 STOP_POLLING;
13429
13430 /* Update the display. */
13431 set_window_update_flags (XWINDOW (f->root_window), 1);
13432 pending |= update_frame (f, 0, 0);
13433 f->cursor_type_changed = 0;
13434 f->updated_p = 1;
13435 }
13436 }
13437 }
13438
13439 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13440
13441 if (!pending)
13442 {
13443 /* Do the mark_window_display_accurate after all windows have
13444 been redisplayed because this call resets flags in buffers
13445 which are needed for proper redisplay. */
13446 FOR_EACH_FRAME (tail, frame)
13447 {
13448 struct frame *f = XFRAME (frame);
13449 if (f->updated_p)
13450 {
13451 mark_window_display_accurate (f->root_window, 1);
13452 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13453 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13454 }
13455 }
13456 }
13457 }
13458 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13459 {
13460 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13461 struct frame *mini_frame;
13462
13463 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13464 /* Use list_of_error, not Qerror, so that
13465 we catch only errors and don't run the debugger. */
13466 internal_condition_case_1 (redisplay_window_1, selected_window,
13467 list_of_error,
13468 redisplay_window_error);
13469 if (update_miniwindow_p)
13470 internal_condition_case_1 (redisplay_window_1, mini_window,
13471 list_of_error,
13472 redisplay_window_error);
13473
13474 /* Compare desired and current matrices, perform output. */
13475
13476 update:
13477 /* If fonts changed, display again. */
13478 if (sf->fonts_changed)
13479 goto retry;
13480
13481 /* Prevent various kinds of signals during display update.
13482 stdio is not robust about handling signals,
13483 which can cause an apparent I/O error. */
13484 if (interrupt_input)
13485 unrequest_sigio ();
13486 STOP_POLLING;
13487
13488 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13489 {
13490 if (hscroll_windows (selected_window))
13491 goto retry;
13492
13493 XWINDOW (selected_window)->must_be_updated_p = 1;
13494 pending = update_frame (sf, 0, 0);
13495 sf->cursor_type_changed = 0;
13496 }
13497
13498 /* We may have called echo_area_display at the top of this
13499 function. If the echo area is on another frame, that may
13500 have put text on a frame other than the selected one, so the
13501 above call to update_frame would not have caught it. Catch
13502 it here. */
13503 mini_window = FRAME_MINIBUF_WINDOW (sf);
13504 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13505
13506 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13507 {
13508 XWINDOW (mini_window)->must_be_updated_p = 1;
13509 pending |= update_frame (mini_frame, 0, 0);
13510 mini_frame->cursor_type_changed = 0;
13511 if (!pending && hscroll_windows (mini_window))
13512 goto retry;
13513 }
13514 }
13515
13516 /* If display was paused because of pending input, make sure we do a
13517 thorough update the next time. */
13518 if (pending)
13519 {
13520 /* Prevent the optimization at the beginning of
13521 redisplay_internal that tries a single-line update of the
13522 line containing the cursor in the selected window. */
13523 CHARPOS (this_line_start_pos) = 0;
13524
13525 /* Let the overlay arrow be updated the next time. */
13526 update_overlay_arrows (0);
13527
13528 /* If we pause after scrolling, some rows in the current
13529 matrices of some windows are not valid. */
13530 if (!WINDOW_FULL_WIDTH_P (w)
13531 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13532 update_mode_lines = 1;
13533 }
13534 else
13535 {
13536 if (!consider_all_windows_p)
13537 {
13538 /* This has already been done above if
13539 consider_all_windows_p is set. */
13540 mark_window_display_accurate_1 (w, 1);
13541
13542 /* Say overlay arrows are up to date. */
13543 update_overlay_arrows (1);
13544
13545 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13546 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13547 }
13548
13549 update_mode_lines = 0;
13550 windows_or_buffers_changed = 0;
13551 }
13552
13553 /* Start SIGIO interrupts coming again. Having them off during the
13554 code above makes it less likely one will discard output, but not
13555 impossible, since there might be stuff in the system buffer here.
13556 But it is much hairier to try to do anything about that. */
13557 if (interrupt_input)
13558 request_sigio ();
13559 RESUME_POLLING;
13560
13561 /* If a frame has become visible which was not before, redisplay
13562 again, so that we display it. Expose events for such a frame
13563 (which it gets when becoming visible) don't call the parts of
13564 redisplay constructing glyphs, so simply exposing a frame won't
13565 display anything in this case. So, we have to display these
13566 frames here explicitly. */
13567 if (!pending)
13568 {
13569 int new_count = 0;
13570
13571 FOR_EACH_FRAME (tail, frame)
13572 {
13573 int this_is_visible = 0;
13574
13575 if (XFRAME (frame)->visible)
13576 this_is_visible = 1;
13577
13578 if (this_is_visible)
13579 new_count++;
13580 }
13581
13582 if (new_count != number_of_visible_frames)
13583 windows_or_buffers_changed++;
13584 }
13585
13586 /* Change frame size now if a change is pending. */
13587 do_pending_window_change (1);
13588
13589 /* If we just did a pending size change, or have additional
13590 visible frames, or selected_window changed, redisplay again. */
13591 if ((windows_or_buffers_changed && !pending)
13592 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13593 goto retry;
13594
13595 /* Clear the face and image caches.
13596
13597 We used to do this only if consider_all_windows_p. But the cache
13598 needs to be cleared if a timer creates images in the current
13599 buffer (e.g. the test case in Bug#6230). */
13600
13601 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13602 {
13603 clear_face_cache (0);
13604 clear_face_cache_count = 0;
13605 }
13606
13607 #ifdef HAVE_WINDOW_SYSTEM
13608 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13609 {
13610 clear_image_caches (Qnil);
13611 clear_image_cache_count = 0;
13612 }
13613 #endif /* HAVE_WINDOW_SYSTEM */
13614
13615 end_of_redisplay:
13616 unbind_to (count, Qnil);
13617 RESUME_POLLING;
13618 }
13619
13620
13621 /* Redisplay, but leave alone any recent echo area message unless
13622 another message has been requested in its place.
13623
13624 This is useful in situations where you need to redisplay but no
13625 user action has occurred, making it inappropriate for the message
13626 area to be cleared. See tracking_off and
13627 wait_reading_process_output for examples of these situations.
13628
13629 FROM_WHERE is an integer saying from where this function was
13630 called. This is useful for debugging. */
13631
13632 void
13633 redisplay_preserve_echo_area (int from_where)
13634 {
13635 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13636
13637 if (!NILP (echo_area_buffer[1]))
13638 {
13639 /* We have a previously displayed message, but no current
13640 message. Redisplay the previous message. */
13641 display_last_displayed_message_p = 1;
13642 redisplay_internal ();
13643 display_last_displayed_message_p = 0;
13644 }
13645 else
13646 redisplay_internal ();
13647
13648 flush_frame (SELECTED_FRAME ());
13649 }
13650
13651
13652 /* Function registered with record_unwind_protect in redisplay_internal. */
13653
13654 static void
13655 unwind_redisplay (void)
13656 {
13657 redisplaying_p = 0;
13658 }
13659
13660
13661 /* Mark the display of leaf window W as accurate or inaccurate.
13662 If ACCURATE_P is non-zero mark display of W as accurate. If
13663 ACCURATE_P is zero, arrange for W to be redisplayed the next
13664 time redisplay_internal is called. */
13665
13666 static void
13667 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13668 {
13669 struct buffer *b = XBUFFER (w->contents);
13670
13671 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13672 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13673 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13674
13675 if (accurate_p)
13676 {
13677 b->clip_changed = 0;
13678 b->prevent_redisplay_optimizations_p = 0;
13679
13680 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13681 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13682 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13683 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13684
13685 w->current_matrix->buffer = b;
13686 w->current_matrix->begv = BUF_BEGV (b);
13687 w->current_matrix->zv = BUF_ZV (b);
13688
13689 w->last_cursor_vpos = w->cursor.vpos;
13690 w->last_cursor_off_p = w->cursor_off_p;
13691
13692 if (w == XWINDOW (selected_window))
13693 w->last_point = BUF_PT (b);
13694 else
13695 w->last_point = marker_position (w->pointm);
13696
13697 w->window_end_valid = 1;
13698 w->update_mode_line = 0;
13699 }
13700 }
13701
13702
13703 /* Mark the display of windows in the window tree rooted at WINDOW as
13704 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13705 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13706 be redisplayed the next time redisplay_internal is called. */
13707
13708 void
13709 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13710 {
13711 struct window *w;
13712
13713 for (; !NILP (window); window = w->next)
13714 {
13715 w = XWINDOW (window);
13716 if (WINDOWP (w->contents))
13717 mark_window_display_accurate (w->contents, accurate_p);
13718 else
13719 mark_window_display_accurate_1 (w, accurate_p);
13720 }
13721
13722 if (accurate_p)
13723 update_overlay_arrows (1);
13724 else
13725 /* Force a thorough redisplay the next time by setting
13726 last_arrow_position and last_arrow_string to t, which is
13727 unequal to any useful value of Voverlay_arrow_... */
13728 update_overlay_arrows (-1);
13729 }
13730
13731
13732 /* Return value in display table DP (Lisp_Char_Table *) for character
13733 C. Since a display table doesn't have any parent, we don't have to
13734 follow parent. Do not call this function directly but use the
13735 macro DISP_CHAR_VECTOR. */
13736
13737 Lisp_Object
13738 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13739 {
13740 Lisp_Object val;
13741
13742 if (ASCII_CHAR_P (c))
13743 {
13744 val = dp->ascii;
13745 if (SUB_CHAR_TABLE_P (val))
13746 val = XSUB_CHAR_TABLE (val)->contents[c];
13747 }
13748 else
13749 {
13750 Lisp_Object table;
13751
13752 XSETCHAR_TABLE (table, dp);
13753 val = char_table_ref (table, c);
13754 }
13755 if (NILP (val))
13756 val = dp->defalt;
13757 return val;
13758 }
13759
13760
13761 \f
13762 /***********************************************************************
13763 Window Redisplay
13764 ***********************************************************************/
13765
13766 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13767
13768 static void
13769 redisplay_windows (Lisp_Object window)
13770 {
13771 while (!NILP (window))
13772 {
13773 struct window *w = XWINDOW (window);
13774
13775 if (WINDOWP (w->contents))
13776 redisplay_windows (w->contents);
13777 else if (BUFFERP (w->contents))
13778 {
13779 displayed_buffer = XBUFFER (w->contents);
13780 /* Use list_of_error, not Qerror, so that
13781 we catch only errors and don't run the debugger. */
13782 internal_condition_case_1 (redisplay_window_0, window,
13783 list_of_error,
13784 redisplay_window_error);
13785 }
13786
13787 window = w->next;
13788 }
13789 }
13790
13791 static Lisp_Object
13792 redisplay_window_error (Lisp_Object ignore)
13793 {
13794 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13795 return Qnil;
13796 }
13797
13798 static Lisp_Object
13799 redisplay_window_0 (Lisp_Object window)
13800 {
13801 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13802 redisplay_window (window, 0);
13803 return Qnil;
13804 }
13805
13806 static Lisp_Object
13807 redisplay_window_1 (Lisp_Object window)
13808 {
13809 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13810 redisplay_window (window, 1);
13811 return Qnil;
13812 }
13813 \f
13814
13815 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13816 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13817 which positions recorded in ROW differ from current buffer
13818 positions.
13819
13820 Return 0 if cursor is not on this row, 1 otherwise. */
13821
13822 static int
13823 set_cursor_from_row (struct window *w, struct glyph_row *row,
13824 struct glyph_matrix *matrix,
13825 ptrdiff_t delta, ptrdiff_t delta_bytes,
13826 int dy, int dvpos)
13827 {
13828 struct glyph *glyph = row->glyphs[TEXT_AREA];
13829 struct glyph *end = glyph + row->used[TEXT_AREA];
13830 struct glyph *cursor = NULL;
13831 /* The last known character position in row. */
13832 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13833 int x = row->x;
13834 ptrdiff_t pt_old = PT - delta;
13835 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13836 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13837 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13838 /* A glyph beyond the edge of TEXT_AREA which we should never
13839 touch. */
13840 struct glyph *glyphs_end = end;
13841 /* Non-zero means we've found a match for cursor position, but that
13842 glyph has the avoid_cursor_p flag set. */
13843 int match_with_avoid_cursor = 0;
13844 /* Non-zero means we've seen at least one glyph that came from a
13845 display string. */
13846 int string_seen = 0;
13847 /* Largest and smallest buffer positions seen so far during scan of
13848 glyph row. */
13849 ptrdiff_t bpos_max = pos_before;
13850 ptrdiff_t bpos_min = pos_after;
13851 /* Last buffer position covered by an overlay string with an integer
13852 `cursor' property. */
13853 ptrdiff_t bpos_covered = 0;
13854 /* Non-zero means the display string on which to display the cursor
13855 comes from a text property, not from an overlay. */
13856 int string_from_text_prop = 0;
13857
13858 /* Don't even try doing anything if called for a mode-line or
13859 header-line row, since the rest of the code isn't prepared to
13860 deal with such calamities. */
13861 eassert (!row->mode_line_p);
13862 if (row->mode_line_p)
13863 return 0;
13864
13865 /* Skip over glyphs not having an object at the start and the end of
13866 the row. These are special glyphs like truncation marks on
13867 terminal frames. */
13868 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13869 {
13870 if (!row->reversed_p)
13871 {
13872 while (glyph < end
13873 && INTEGERP (glyph->object)
13874 && glyph->charpos < 0)
13875 {
13876 x += glyph->pixel_width;
13877 ++glyph;
13878 }
13879 while (end > glyph
13880 && INTEGERP ((end - 1)->object)
13881 /* CHARPOS is zero for blanks and stretch glyphs
13882 inserted by extend_face_to_end_of_line. */
13883 && (end - 1)->charpos <= 0)
13884 --end;
13885 glyph_before = glyph - 1;
13886 glyph_after = end;
13887 }
13888 else
13889 {
13890 struct glyph *g;
13891
13892 /* If the glyph row is reversed, we need to process it from back
13893 to front, so swap the edge pointers. */
13894 glyphs_end = end = glyph - 1;
13895 glyph += row->used[TEXT_AREA] - 1;
13896
13897 while (glyph > end + 1
13898 && INTEGERP (glyph->object)
13899 && glyph->charpos < 0)
13900 {
13901 --glyph;
13902 x -= glyph->pixel_width;
13903 }
13904 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13905 --glyph;
13906 /* By default, in reversed rows we put the cursor on the
13907 rightmost (first in the reading order) glyph. */
13908 for (g = end + 1; g < glyph; g++)
13909 x += g->pixel_width;
13910 while (end < glyph
13911 && INTEGERP ((end + 1)->object)
13912 && (end + 1)->charpos <= 0)
13913 ++end;
13914 glyph_before = glyph + 1;
13915 glyph_after = end;
13916 }
13917 }
13918 else if (row->reversed_p)
13919 {
13920 /* In R2L rows that don't display text, put the cursor on the
13921 rightmost glyph. Case in point: an empty last line that is
13922 part of an R2L paragraph. */
13923 cursor = end - 1;
13924 /* Avoid placing the cursor on the last glyph of the row, where
13925 on terminal frames we hold the vertical border between
13926 adjacent windows. */
13927 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13928 && !WINDOW_RIGHTMOST_P (w)
13929 && cursor == row->glyphs[LAST_AREA] - 1)
13930 cursor--;
13931 x = -1; /* will be computed below, at label compute_x */
13932 }
13933
13934 /* Step 1: Try to find the glyph whose character position
13935 corresponds to point. If that's not possible, find 2 glyphs
13936 whose character positions are the closest to point, one before
13937 point, the other after it. */
13938 if (!row->reversed_p)
13939 while (/* not marched to end of glyph row */
13940 glyph < end
13941 /* glyph was not inserted by redisplay for internal purposes */
13942 && !INTEGERP (glyph->object))
13943 {
13944 if (BUFFERP (glyph->object))
13945 {
13946 ptrdiff_t dpos = glyph->charpos - pt_old;
13947
13948 if (glyph->charpos > bpos_max)
13949 bpos_max = glyph->charpos;
13950 if (glyph->charpos < bpos_min)
13951 bpos_min = glyph->charpos;
13952 if (!glyph->avoid_cursor_p)
13953 {
13954 /* If we hit point, we've found the glyph on which to
13955 display the cursor. */
13956 if (dpos == 0)
13957 {
13958 match_with_avoid_cursor = 0;
13959 break;
13960 }
13961 /* See if we've found a better approximation to
13962 POS_BEFORE or to POS_AFTER. */
13963 if (0 > dpos && dpos > pos_before - pt_old)
13964 {
13965 pos_before = glyph->charpos;
13966 glyph_before = glyph;
13967 }
13968 else if (0 < dpos && dpos < pos_after - pt_old)
13969 {
13970 pos_after = glyph->charpos;
13971 glyph_after = glyph;
13972 }
13973 }
13974 else if (dpos == 0)
13975 match_with_avoid_cursor = 1;
13976 }
13977 else if (STRINGP (glyph->object))
13978 {
13979 Lisp_Object chprop;
13980 ptrdiff_t glyph_pos = glyph->charpos;
13981
13982 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13983 glyph->object);
13984 if (!NILP (chprop))
13985 {
13986 /* If the string came from a `display' text property,
13987 look up the buffer position of that property and
13988 use that position to update bpos_max, as if we
13989 actually saw such a position in one of the row's
13990 glyphs. This helps with supporting integer values
13991 of `cursor' property on the display string in
13992 situations where most or all of the row's buffer
13993 text is completely covered by display properties,
13994 so that no glyph with valid buffer positions is
13995 ever seen in the row. */
13996 ptrdiff_t prop_pos =
13997 string_buffer_position_lim (glyph->object, pos_before,
13998 pos_after, 0);
13999
14000 if (prop_pos >= pos_before)
14001 bpos_max = prop_pos - 1;
14002 }
14003 if (INTEGERP (chprop))
14004 {
14005 bpos_covered = bpos_max + XINT (chprop);
14006 /* If the `cursor' property covers buffer positions up
14007 to and including point, we should display cursor on
14008 this glyph. Note that, if a `cursor' property on one
14009 of the string's characters has an integer value, we
14010 will break out of the loop below _before_ we get to
14011 the position match above. IOW, integer values of
14012 the `cursor' property override the "exact match for
14013 point" strategy of positioning the cursor. */
14014 /* Implementation note: bpos_max == pt_old when, e.g.,
14015 we are in an empty line, where bpos_max is set to
14016 MATRIX_ROW_START_CHARPOS, see above. */
14017 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14018 {
14019 cursor = glyph;
14020 break;
14021 }
14022 }
14023
14024 string_seen = 1;
14025 }
14026 x += glyph->pixel_width;
14027 ++glyph;
14028 }
14029 else if (glyph > end) /* row is reversed */
14030 while (!INTEGERP (glyph->object))
14031 {
14032 if (BUFFERP (glyph->object))
14033 {
14034 ptrdiff_t dpos = glyph->charpos - pt_old;
14035
14036 if (glyph->charpos > bpos_max)
14037 bpos_max = glyph->charpos;
14038 if (glyph->charpos < bpos_min)
14039 bpos_min = glyph->charpos;
14040 if (!glyph->avoid_cursor_p)
14041 {
14042 if (dpos == 0)
14043 {
14044 match_with_avoid_cursor = 0;
14045 break;
14046 }
14047 if (0 > dpos && dpos > pos_before - pt_old)
14048 {
14049 pos_before = glyph->charpos;
14050 glyph_before = glyph;
14051 }
14052 else if (0 < dpos && dpos < pos_after - pt_old)
14053 {
14054 pos_after = glyph->charpos;
14055 glyph_after = glyph;
14056 }
14057 }
14058 else if (dpos == 0)
14059 match_with_avoid_cursor = 1;
14060 }
14061 else if (STRINGP (glyph->object))
14062 {
14063 Lisp_Object chprop;
14064 ptrdiff_t glyph_pos = glyph->charpos;
14065
14066 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14067 glyph->object);
14068 if (!NILP (chprop))
14069 {
14070 ptrdiff_t prop_pos =
14071 string_buffer_position_lim (glyph->object, pos_before,
14072 pos_after, 0);
14073
14074 if (prop_pos >= pos_before)
14075 bpos_max = prop_pos - 1;
14076 }
14077 if (INTEGERP (chprop))
14078 {
14079 bpos_covered = bpos_max + XINT (chprop);
14080 /* If the `cursor' property covers buffer positions up
14081 to and including point, we should display cursor on
14082 this glyph. */
14083 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14084 {
14085 cursor = glyph;
14086 break;
14087 }
14088 }
14089 string_seen = 1;
14090 }
14091 --glyph;
14092 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14093 {
14094 x--; /* can't use any pixel_width */
14095 break;
14096 }
14097 x -= glyph->pixel_width;
14098 }
14099
14100 /* Step 2: If we didn't find an exact match for point, we need to
14101 look for a proper place to put the cursor among glyphs between
14102 GLYPH_BEFORE and GLYPH_AFTER. */
14103 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14104 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14105 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14106 {
14107 /* An empty line has a single glyph whose OBJECT is zero and
14108 whose CHARPOS is the position of a newline on that line.
14109 Note that on a TTY, there are more glyphs after that, which
14110 were produced by extend_face_to_end_of_line, but their
14111 CHARPOS is zero or negative. */
14112 int empty_line_p =
14113 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14114 && INTEGERP (glyph->object) && glyph->charpos > 0
14115 /* On a TTY, continued and truncated rows also have a glyph at
14116 their end whose OBJECT is zero and whose CHARPOS is
14117 positive (the continuation and truncation glyphs), but such
14118 rows are obviously not "empty". */
14119 && !(row->continued_p || row->truncated_on_right_p);
14120
14121 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14122 {
14123 ptrdiff_t ellipsis_pos;
14124
14125 /* Scan back over the ellipsis glyphs. */
14126 if (!row->reversed_p)
14127 {
14128 ellipsis_pos = (glyph - 1)->charpos;
14129 while (glyph > row->glyphs[TEXT_AREA]
14130 && (glyph - 1)->charpos == ellipsis_pos)
14131 glyph--, x -= glyph->pixel_width;
14132 /* That loop always goes one position too far, including
14133 the glyph before the ellipsis. So scan forward over
14134 that one. */
14135 x += glyph->pixel_width;
14136 glyph++;
14137 }
14138 else /* row is reversed */
14139 {
14140 ellipsis_pos = (glyph + 1)->charpos;
14141 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14142 && (glyph + 1)->charpos == ellipsis_pos)
14143 glyph++, x += glyph->pixel_width;
14144 x -= glyph->pixel_width;
14145 glyph--;
14146 }
14147 }
14148 else if (match_with_avoid_cursor)
14149 {
14150 cursor = glyph_after;
14151 x = -1;
14152 }
14153 else if (string_seen)
14154 {
14155 int incr = row->reversed_p ? -1 : +1;
14156
14157 /* Need to find the glyph that came out of a string which is
14158 present at point. That glyph is somewhere between
14159 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14160 positioned between POS_BEFORE and POS_AFTER in the
14161 buffer. */
14162 struct glyph *start, *stop;
14163 ptrdiff_t pos = pos_before;
14164
14165 x = -1;
14166
14167 /* If the row ends in a newline from a display string,
14168 reordering could have moved the glyphs belonging to the
14169 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14170 in this case we extend the search to the last glyph in
14171 the row that was not inserted by redisplay. */
14172 if (row->ends_in_newline_from_string_p)
14173 {
14174 glyph_after = end;
14175 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14176 }
14177
14178 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14179 correspond to POS_BEFORE and POS_AFTER, respectively. We
14180 need START and STOP in the order that corresponds to the
14181 row's direction as given by its reversed_p flag. If the
14182 directionality of characters between POS_BEFORE and
14183 POS_AFTER is the opposite of the row's base direction,
14184 these characters will have been reordered for display,
14185 and we need to reverse START and STOP. */
14186 if (!row->reversed_p)
14187 {
14188 start = min (glyph_before, glyph_after);
14189 stop = max (glyph_before, glyph_after);
14190 }
14191 else
14192 {
14193 start = max (glyph_before, glyph_after);
14194 stop = min (glyph_before, glyph_after);
14195 }
14196 for (glyph = start + incr;
14197 row->reversed_p ? glyph > stop : glyph < stop; )
14198 {
14199
14200 /* Any glyphs that come from the buffer are here because
14201 of bidi reordering. Skip them, and only pay
14202 attention to glyphs that came from some string. */
14203 if (STRINGP (glyph->object))
14204 {
14205 Lisp_Object str;
14206 ptrdiff_t tem;
14207 /* If the display property covers the newline, we
14208 need to search for it one position farther. */
14209 ptrdiff_t lim = pos_after
14210 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14211
14212 string_from_text_prop = 0;
14213 str = glyph->object;
14214 tem = string_buffer_position_lim (str, pos, lim, 0);
14215 if (tem == 0 /* from overlay */
14216 || pos <= tem)
14217 {
14218 /* If the string from which this glyph came is
14219 found in the buffer at point, or at position
14220 that is closer to point than pos_after, then
14221 we've found the glyph we've been looking for.
14222 If it comes from an overlay (tem == 0), and
14223 it has the `cursor' property on one of its
14224 glyphs, record that glyph as a candidate for
14225 displaying the cursor. (As in the
14226 unidirectional version, we will display the
14227 cursor on the last candidate we find.) */
14228 if (tem == 0
14229 || tem == pt_old
14230 || (tem - pt_old > 0 && tem < pos_after))
14231 {
14232 /* The glyphs from this string could have
14233 been reordered. Find the one with the
14234 smallest string position. Or there could
14235 be a character in the string with the
14236 `cursor' property, which means display
14237 cursor on that character's glyph. */
14238 ptrdiff_t strpos = glyph->charpos;
14239
14240 if (tem)
14241 {
14242 cursor = glyph;
14243 string_from_text_prop = 1;
14244 }
14245 for ( ;
14246 (row->reversed_p ? glyph > stop : glyph < stop)
14247 && EQ (glyph->object, str);
14248 glyph += incr)
14249 {
14250 Lisp_Object cprop;
14251 ptrdiff_t gpos = glyph->charpos;
14252
14253 cprop = Fget_char_property (make_number (gpos),
14254 Qcursor,
14255 glyph->object);
14256 if (!NILP (cprop))
14257 {
14258 cursor = glyph;
14259 break;
14260 }
14261 if (tem && glyph->charpos < strpos)
14262 {
14263 strpos = glyph->charpos;
14264 cursor = glyph;
14265 }
14266 }
14267
14268 if (tem == pt_old
14269 || (tem - pt_old > 0 && tem < pos_after))
14270 goto compute_x;
14271 }
14272 if (tem)
14273 pos = tem + 1; /* don't find previous instances */
14274 }
14275 /* This string is not what we want; skip all of the
14276 glyphs that came from it. */
14277 while ((row->reversed_p ? glyph > stop : glyph < stop)
14278 && EQ (glyph->object, str))
14279 glyph += incr;
14280 }
14281 else
14282 glyph += incr;
14283 }
14284
14285 /* If we reached the end of the line, and END was from a string,
14286 the cursor is not on this line. */
14287 if (cursor == NULL
14288 && (row->reversed_p ? glyph <= end : glyph >= end)
14289 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14290 && STRINGP (end->object)
14291 && row->continued_p)
14292 return 0;
14293 }
14294 /* A truncated row may not include PT among its character positions.
14295 Setting the cursor inside the scroll margin will trigger
14296 recalculation of hscroll in hscroll_window_tree. But if a
14297 display string covers point, defer to the string-handling
14298 code below to figure this out. */
14299 else if (row->truncated_on_left_p && pt_old < bpos_min)
14300 {
14301 cursor = glyph_before;
14302 x = -1;
14303 }
14304 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14305 /* Zero-width characters produce no glyphs. */
14306 || (!empty_line_p
14307 && (row->reversed_p
14308 ? glyph_after > glyphs_end
14309 : glyph_after < glyphs_end)))
14310 {
14311 cursor = glyph_after;
14312 x = -1;
14313 }
14314 }
14315
14316 compute_x:
14317 if (cursor != NULL)
14318 glyph = cursor;
14319 else if (glyph == glyphs_end
14320 && pos_before == pos_after
14321 && STRINGP ((row->reversed_p
14322 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14323 : row->glyphs[TEXT_AREA])->object))
14324 {
14325 /* If all the glyphs of this row came from strings, put the
14326 cursor on the first glyph of the row. This avoids having the
14327 cursor outside of the text area in this very rare and hard
14328 use case. */
14329 glyph =
14330 row->reversed_p
14331 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14332 : row->glyphs[TEXT_AREA];
14333 }
14334 if (x < 0)
14335 {
14336 struct glyph *g;
14337
14338 /* Need to compute x that corresponds to GLYPH. */
14339 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14340 {
14341 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14342 emacs_abort ();
14343 x += g->pixel_width;
14344 }
14345 }
14346
14347 /* ROW could be part of a continued line, which, under bidi
14348 reordering, might have other rows whose start and end charpos
14349 occlude point. Only set w->cursor if we found a better
14350 approximation to the cursor position than we have from previously
14351 examined candidate rows belonging to the same continued line. */
14352 if (/* we already have a candidate row */
14353 w->cursor.vpos >= 0
14354 /* that candidate is not the row we are processing */
14355 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14356 /* Make sure cursor.vpos specifies a row whose start and end
14357 charpos occlude point, and it is valid candidate for being a
14358 cursor-row. This is because some callers of this function
14359 leave cursor.vpos at the row where the cursor was displayed
14360 during the last redisplay cycle. */
14361 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14362 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14363 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14364 {
14365 struct glyph *g1 =
14366 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14367
14368 /* Don't consider glyphs that are outside TEXT_AREA. */
14369 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14370 return 0;
14371 /* Keep the candidate whose buffer position is the closest to
14372 point or has the `cursor' property. */
14373 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14374 w->cursor.hpos >= 0
14375 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14376 && ((BUFFERP (g1->object)
14377 && (g1->charpos == pt_old /* an exact match always wins */
14378 || (BUFFERP (glyph->object)
14379 && eabs (g1->charpos - pt_old)
14380 < eabs (glyph->charpos - pt_old))))
14381 /* previous candidate is a glyph from a string that has
14382 a non-nil `cursor' property */
14383 || (STRINGP (g1->object)
14384 && (!NILP (Fget_char_property (make_number (g1->charpos),
14385 Qcursor, g1->object))
14386 /* previous candidate is from the same display
14387 string as this one, and the display string
14388 came from a text property */
14389 || (EQ (g1->object, glyph->object)
14390 && string_from_text_prop)
14391 /* this candidate is from newline and its
14392 position is not an exact match */
14393 || (INTEGERP (glyph->object)
14394 && glyph->charpos != pt_old)))))
14395 return 0;
14396 /* If this candidate gives an exact match, use that. */
14397 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14398 /* If this candidate is a glyph created for the
14399 terminating newline of a line, and point is on that
14400 newline, it wins because it's an exact match. */
14401 || (!row->continued_p
14402 && INTEGERP (glyph->object)
14403 && glyph->charpos == 0
14404 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14405 /* Otherwise, keep the candidate that comes from a row
14406 spanning less buffer positions. This may win when one or
14407 both candidate positions are on glyphs that came from
14408 display strings, for which we cannot compare buffer
14409 positions. */
14410 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14411 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14412 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14413 return 0;
14414 }
14415 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14416 w->cursor.x = x;
14417 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14418 w->cursor.y = row->y + dy;
14419
14420 if (w == XWINDOW (selected_window))
14421 {
14422 if (!row->continued_p
14423 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14424 && row->x == 0)
14425 {
14426 this_line_buffer = XBUFFER (w->contents);
14427
14428 CHARPOS (this_line_start_pos)
14429 = MATRIX_ROW_START_CHARPOS (row) + delta;
14430 BYTEPOS (this_line_start_pos)
14431 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14432
14433 CHARPOS (this_line_end_pos)
14434 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14435 BYTEPOS (this_line_end_pos)
14436 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14437
14438 this_line_y = w->cursor.y;
14439 this_line_pixel_height = row->height;
14440 this_line_vpos = w->cursor.vpos;
14441 this_line_start_x = row->x;
14442 }
14443 else
14444 CHARPOS (this_line_start_pos) = 0;
14445 }
14446
14447 return 1;
14448 }
14449
14450
14451 /* Run window scroll functions, if any, for WINDOW with new window
14452 start STARTP. Sets the window start of WINDOW to that position.
14453
14454 We assume that the window's buffer is really current. */
14455
14456 static struct text_pos
14457 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14458 {
14459 struct window *w = XWINDOW (window);
14460 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14461
14462 eassert (current_buffer == XBUFFER (w->contents));
14463
14464 if (!NILP (Vwindow_scroll_functions))
14465 {
14466 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14467 make_number (CHARPOS (startp)));
14468 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14469 /* In case the hook functions switch buffers. */
14470 set_buffer_internal (XBUFFER (w->contents));
14471 }
14472
14473 return startp;
14474 }
14475
14476
14477 /* Make sure the line containing the cursor is fully visible.
14478 A value of 1 means there is nothing to be done.
14479 (Either the line is fully visible, or it cannot be made so,
14480 or we cannot tell.)
14481
14482 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14483 is higher than window.
14484
14485 A value of 0 means the caller should do scrolling
14486 as if point had gone off the screen. */
14487
14488 static int
14489 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14490 {
14491 struct glyph_matrix *matrix;
14492 struct glyph_row *row;
14493 int window_height;
14494
14495 if (!make_cursor_line_fully_visible_p)
14496 return 1;
14497
14498 /* It's not always possible to find the cursor, e.g, when a window
14499 is full of overlay strings. Don't do anything in that case. */
14500 if (w->cursor.vpos < 0)
14501 return 1;
14502
14503 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14504 row = MATRIX_ROW (matrix, w->cursor.vpos);
14505
14506 /* If the cursor row is not partially visible, there's nothing to do. */
14507 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14508 return 1;
14509
14510 /* If the row the cursor is in is taller than the window's height,
14511 it's not clear what to do, so do nothing. */
14512 window_height = window_box_height (w);
14513 if (row->height >= window_height)
14514 {
14515 if (!force_p || MINI_WINDOW_P (w)
14516 || w->vscroll || w->cursor.vpos == 0)
14517 return 1;
14518 }
14519 return 0;
14520 }
14521
14522
14523 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14524 non-zero means only WINDOW is redisplayed in redisplay_internal.
14525 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14526 in redisplay_window to bring a partially visible line into view in
14527 the case that only the cursor has moved.
14528
14529 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14530 last screen line's vertical height extends past the end of the screen.
14531
14532 Value is
14533
14534 1 if scrolling succeeded
14535
14536 0 if scrolling didn't find point.
14537
14538 -1 if new fonts have been loaded so that we must interrupt
14539 redisplay, adjust glyph matrices, and try again. */
14540
14541 enum
14542 {
14543 SCROLLING_SUCCESS,
14544 SCROLLING_FAILED,
14545 SCROLLING_NEED_LARGER_MATRICES
14546 };
14547
14548 /* If scroll-conservatively is more than this, never recenter.
14549
14550 If you change this, don't forget to update the doc string of
14551 `scroll-conservatively' and the Emacs manual. */
14552 #define SCROLL_LIMIT 100
14553
14554 static int
14555 try_scrolling (Lisp_Object window, int just_this_one_p,
14556 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14557 int temp_scroll_step, int last_line_misfit)
14558 {
14559 struct window *w = XWINDOW (window);
14560 struct frame *f = XFRAME (w->frame);
14561 struct text_pos pos, startp;
14562 struct it it;
14563 int this_scroll_margin, scroll_max, rc, height;
14564 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14565 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14566 Lisp_Object aggressive;
14567 /* We will never try scrolling more than this number of lines. */
14568 int scroll_limit = SCROLL_LIMIT;
14569 int frame_line_height = default_line_pixel_height (w);
14570 int window_total_lines
14571 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14572
14573 #ifdef GLYPH_DEBUG
14574 debug_method_add (w, "try_scrolling");
14575 #endif
14576
14577 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14578
14579 /* Compute scroll margin height in pixels. We scroll when point is
14580 within this distance from the top or bottom of the window. */
14581 if (scroll_margin > 0)
14582 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14583 * frame_line_height;
14584 else
14585 this_scroll_margin = 0;
14586
14587 /* Force arg_scroll_conservatively to have a reasonable value, to
14588 avoid scrolling too far away with slow move_it_* functions. Note
14589 that the user can supply scroll-conservatively equal to
14590 `most-positive-fixnum', which can be larger than INT_MAX. */
14591 if (arg_scroll_conservatively > scroll_limit)
14592 {
14593 arg_scroll_conservatively = scroll_limit + 1;
14594 scroll_max = scroll_limit * frame_line_height;
14595 }
14596 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14597 /* Compute how much we should try to scroll maximally to bring
14598 point into view. */
14599 scroll_max = (max (scroll_step,
14600 max (arg_scroll_conservatively, temp_scroll_step))
14601 * frame_line_height);
14602 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14603 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14604 /* We're trying to scroll because of aggressive scrolling but no
14605 scroll_step is set. Choose an arbitrary one. */
14606 scroll_max = 10 * frame_line_height;
14607 else
14608 scroll_max = 0;
14609
14610 too_near_end:
14611
14612 /* Decide whether to scroll down. */
14613 if (PT > CHARPOS (startp))
14614 {
14615 int scroll_margin_y;
14616
14617 /* Compute the pixel ypos of the scroll margin, then move IT to
14618 either that ypos or PT, whichever comes first. */
14619 start_display (&it, w, startp);
14620 scroll_margin_y = it.last_visible_y - this_scroll_margin
14621 - frame_line_height * extra_scroll_margin_lines;
14622 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14623 (MOVE_TO_POS | MOVE_TO_Y));
14624
14625 if (PT > CHARPOS (it.current.pos))
14626 {
14627 int y0 = line_bottom_y (&it);
14628 /* Compute how many pixels below window bottom to stop searching
14629 for PT. This avoids costly search for PT that is far away if
14630 the user limited scrolling by a small number of lines, but
14631 always finds PT if scroll_conservatively is set to a large
14632 number, such as most-positive-fixnum. */
14633 int slack = max (scroll_max, 10 * frame_line_height);
14634 int y_to_move = it.last_visible_y + slack;
14635
14636 /* Compute the distance from the scroll margin to PT or to
14637 the scroll limit, whichever comes first. This should
14638 include the height of the cursor line, to make that line
14639 fully visible. */
14640 move_it_to (&it, PT, -1, y_to_move,
14641 -1, MOVE_TO_POS | MOVE_TO_Y);
14642 dy = line_bottom_y (&it) - y0;
14643
14644 if (dy > scroll_max)
14645 return SCROLLING_FAILED;
14646
14647 if (dy > 0)
14648 scroll_down_p = 1;
14649 }
14650 }
14651
14652 if (scroll_down_p)
14653 {
14654 /* Point is in or below the bottom scroll margin, so move the
14655 window start down. If scrolling conservatively, move it just
14656 enough down to make point visible. If scroll_step is set,
14657 move it down by scroll_step. */
14658 if (arg_scroll_conservatively)
14659 amount_to_scroll
14660 = min (max (dy, frame_line_height),
14661 frame_line_height * arg_scroll_conservatively);
14662 else if (scroll_step || temp_scroll_step)
14663 amount_to_scroll = scroll_max;
14664 else
14665 {
14666 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14667 height = WINDOW_BOX_TEXT_HEIGHT (w);
14668 if (NUMBERP (aggressive))
14669 {
14670 double float_amount = XFLOATINT (aggressive) * height;
14671 int aggressive_scroll = float_amount;
14672 if (aggressive_scroll == 0 && float_amount > 0)
14673 aggressive_scroll = 1;
14674 /* Don't let point enter the scroll margin near top of
14675 the window. This could happen if the value of
14676 scroll_up_aggressively is too large and there are
14677 non-zero margins, because scroll_up_aggressively
14678 means put point that fraction of window height
14679 _from_the_bottom_margin_. */
14680 if (aggressive_scroll + 2*this_scroll_margin > height)
14681 aggressive_scroll = height - 2*this_scroll_margin;
14682 amount_to_scroll = dy + aggressive_scroll;
14683 }
14684 }
14685
14686 if (amount_to_scroll <= 0)
14687 return SCROLLING_FAILED;
14688
14689 start_display (&it, w, startp);
14690 if (arg_scroll_conservatively <= scroll_limit)
14691 move_it_vertically (&it, amount_to_scroll);
14692 else
14693 {
14694 /* Extra precision for users who set scroll-conservatively
14695 to a large number: make sure the amount we scroll
14696 the window start is never less than amount_to_scroll,
14697 which was computed as distance from window bottom to
14698 point. This matters when lines at window top and lines
14699 below window bottom have different height. */
14700 struct it it1;
14701 void *it1data = NULL;
14702 /* We use a temporary it1 because line_bottom_y can modify
14703 its argument, if it moves one line down; see there. */
14704 int start_y;
14705
14706 SAVE_IT (it1, it, it1data);
14707 start_y = line_bottom_y (&it1);
14708 do {
14709 RESTORE_IT (&it, &it, it1data);
14710 move_it_by_lines (&it, 1);
14711 SAVE_IT (it1, it, it1data);
14712 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14713 }
14714
14715 /* If STARTP is unchanged, move it down another screen line. */
14716 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14717 move_it_by_lines (&it, 1);
14718 startp = it.current.pos;
14719 }
14720 else
14721 {
14722 struct text_pos scroll_margin_pos = startp;
14723 int y_offset = 0;
14724
14725 /* See if point is inside the scroll margin at the top of the
14726 window. */
14727 if (this_scroll_margin)
14728 {
14729 int y_start;
14730
14731 start_display (&it, w, startp);
14732 y_start = it.current_y;
14733 move_it_vertically (&it, this_scroll_margin);
14734 scroll_margin_pos = it.current.pos;
14735 /* If we didn't move enough before hitting ZV, request
14736 additional amount of scroll, to move point out of the
14737 scroll margin. */
14738 if (IT_CHARPOS (it) == ZV
14739 && it.current_y - y_start < this_scroll_margin)
14740 y_offset = this_scroll_margin - (it.current_y - y_start);
14741 }
14742
14743 if (PT < CHARPOS (scroll_margin_pos))
14744 {
14745 /* Point is in the scroll margin at the top of the window or
14746 above what is displayed in the window. */
14747 int y0, y_to_move;
14748
14749 /* Compute the vertical distance from PT to the scroll
14750 margin position. Move as far as scroll_max allows, or
14751 one screenful, or 10 screen lines, whichever is largest.
14752 Give up if distance is greater than scroll_max or if we
14753 didn't reach the scroll margin position. */
14754 SET_TEXT_POS (pos, PT, PT_BYTE);
14755 start_display (&it, w, pos);
14756 y0 = it.current_y;
14757 y_to_move = max (it.last_visible_y,
14758 max (scroll_max, 10 * frame_line_height));
14759 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14760 y_to_move, -1,
14761 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14762 dy = it.current_y - y0;
14763 if (dy > scroll_max
14764 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14765 return SCROLLING_FAILED;
14766
14767 /* Additional scroll for when ZV was too close to point. */
14768 dy += y_offset;
14769
14770 /* Compute new window start. */
14771 start_display (&it, w, startp);
14772
14773 if (arg_scroll_conservatively)
14774 amount_to_scroll = max (dy, frame_line_height *
14775 max (scroll_step, temp_scroll_step));
14776 else if (scroll_step || temp_scroll_step)
14777 amount_to_scroll = scroll_max;
14778 else
14779 {
14780 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14781 height = WINDOW_BOX_TEXT_HEIGHT (w);
14782 if (NUMBERP (aggressive))
14783 {
14784 double float_amount = XFLOATINT (aggressive) * height;
14785 int aggressive_scroll = float_amount;
14786 if (aggressive_scroll == 0 && float_amount > 0)
14787 aggressive_scroll = 1;
14788 /* Don't let point enter the scroll margin near
14789 bottom of the window, if the value of
14790 scroll_down_aggressively happens to be too
14791 large. */
14792 if (aggressive_scroll + 2*this_scroll_margin > height)
14793 aggressive_scroll = height - 2*this_scroll_margin;
14794 amount_to_scroll = dy + aggressive_scroll;
14795 }
14796 }
14797
14798 if (amount_to_scroll <= 0)
14799 return SCROLLING_FAILED;
14800
14801 move_it_vertically_backward (&it, amount_to_scroll);
14802 startp = it.current.pos;
14803 }
14804 }
14805
14806 /* Run window scroll functions. */
14807 startp = run_window_scroll_functions (window, startp);
14808
14809 /* Display the window. Give up if new fonts are loaded, or if point
14810 doesn't appear. */
14811 if (!try_window (window, startp, 0))
14812 rc = SCROLLING_NEED_LARGER_MATRICES;
14813 else if (w->cursor.vpos < 0)
14814 {
14815 clear_glyph_matrix (w->desired_matrix);
14816 rc = SCROLLING_FAILED;
14817 }
14818 else
14819 {
14820 /* Maybe forget recorded base line for line number display. */
14821 if (!just_this_one_p
14822 || current_buffer->clip_changed
14823 || BEG_UNCHANGED < CHARPOS (startp))
14824 w->base_line_number = 0;
14825
14826 /* If cursor ends up on a partially visible line,
14827 treat that as being off the bottom of the screen. */
14828 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14829 /* It's possible that the cursor is on the first line of the
14830 buffer, which is partially obscured due to a vscroll
14831 (Bug#7537). In that case, avoid looping forever . */
14832 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14833 {
14834 clear_glyph_matrix (w->desired_matrix);
14835 ++extra_scroll_margin_lines;
14836 goto too_near_end;
14837 }
14838 rc = SCROLLING_SUCCESS;
14839 }
14840
14841 return rc;
14842 }
14843
14844
14845 /* Compute a suitable window start for window W if display of W starts
14846 on a continuation line. Value is non-zero if a new window start
14847 was computed.
14848
14849 The new window start will be computed, based on W's width, starting
14850 from the start of the continued line. It is the start of the
14851 screen line with the minimum distance from the old start W->start. */
14852
14853 static int
14854 compute_window_start_on_continuation_line (struct window *w)
14855 {
14856 struct text_pos pos, start_pos;
14857 int window_start_changed_p = 0;
14858
14859 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14860
14861 /* If window start is on a continuation line... Window start may be
14862 < BEGV in case there's invisible text at the start of the
14863 buffer (M-x rmail, for example). */
14864 if (CHARPOS (start_pos) > BEGV
14865 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14866 {
14867 struct it it;
14868 struct glyph_row *row;
14869
14870 /* Handle the case that the window start is out of range. */
14871 if (CHARPOS (start_pos) < BEGV)
14872 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14873 else if (CHARPOS (start_pos) > ZV)
14874 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14875
14876 /* Find the start of the continued line. This should be fast
14877 because find_newline is fast (newline cache). */
14878 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14879 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14880 row, DEFAULT_FACE_ID);
14881 reseat_at_previous_visible_line_start (&it);
14882
14883 /* If the line start is "too far" away from the window start,
14884 say it takes too much time to compute a new window start. */
14885 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14886 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14887 {
14888 int min_distance, distance;
14889
14890 /* Move forward by display lines to find the new window
14891 start. If window width was enlarged, the new start can
14892 be expected to be > the old start. If window width was
14893 decreased, the new window start will be < the old start.
14894 So, we're looking for the display line start with the
14895 minimum distance from the old window start. */
14896 pos = it.current.pos;
14897 min_distance = INFINITY;
14898 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14899 distance < min_distance)
14900 {
14901 min_distance = distance;
14902 pos = it.current.pos;
14903 if (it.line_wrap == WORD_WRAP)
14904 {
14905 /* Under WORD_WRAP, move_it_by_lines is likely to
14906 overshoot and stop not at the first, but the
14907 second character from the left margin. So in
14908 that case, we need a more tight control on the X
14909 coordinate of the iterator than move_it_by_lines
14910 promises in its contract. The method is to first
14911 go to the last (rightmost) visible character of a
14912 line, then move to the leftmost character on the
14913 next line in a separate call. */
14914 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
14915 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14916 move_it_to (&it, ZV, 0,
14917 it.current_y + it.max_ascent + it.max_descent, -1,
14918 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14919 }
14920 else
14921 move_it_by_lines (&it, 1);
14922 }
14923
14924 /* Set the window start there. */
14925 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14926 window_start_changed_p = 1;
14927 }
14928 }
14929
14930 return window_start_changed_p;
14931 }
14932
14933
14934 /* Try cursor movement in case text has not changed in window WINDOW,
14935 with window start STARTP. Value is
14936
14937 CURSOR_MOVEMENT_SUCCESS if successful
14938
14939 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14940
14941 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14942 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14943 we want to scroll as if scroll-step were set to 1. See the code.
14944
14945 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14946 which case we have to abort this redisplay, and adjust matrices
14947 first. */
14948
14949 enum
14950 {
14951 CURSOR_MOVEMENT_SUCCESS,
14952 CURSOR_MOVEMENT_CANNOT_BE_USED,
14953 CURSOR_MOVEMENT_MUST_SCROLL,
14954 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14955 };
14956
14957 static int
14958 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14959 {
14960 struct window *w = XWINDOW (window);
14961 struct frame *f = XFRAME (w->frame);
14962 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14963
14964 #ifdef GLYPH_DEBUG
14965 if (inhibit_try_cursor_movement)
14966 return rc;
14967 #endif
14968
14969 /* Previously, there was a check for Lisp integer in the
14970 if-statement below. Now, this field is converted to
14971 ptrdiff_t, thus zero means invalid position in a buffer. */
14972 eassert (w->last_point > 0);
14973 /* Likewise there was a check whether window_end_vpos is nil or larger
14974 than the window. Now window_end_vpos is int and so never nil, but
14975 let's leave eassert to check whether it fits in the window. */
14976 eassert (w->window_end_vpos < w->current_matrix->nrows);
14977
14978 /* Handle case where text has not changed, only point, and it has
14979 not moved off the frame. */
14980 if (/* Point may be in this window. */
14981 PT >= CHARPOS (startp)
14982 /* Selective display hasn't changed. */
14983 && !current_buffer->clip_changed
14984 /* Function force-mode-line-update is used to force a thorough
14985 redisplay. It sets either windows_or_buffers_changed or
14986 update_mode_lines. So don't take a shortcut here for these
14987 cases. */
14988 && !update_mode_lines
14989 && !windows_or_buffers_changed
14990 && !f->cursor_type_changed
14991 /* Can't use this case if highlighting a region. When a
14992 region exists, cursor movement has to do more than just
14993 set the cursor. */
14994 && markpos_of_region () < 0
14995 && !w->region_showing
14996 && NILP (Vshow_trailing_whitespace)
14997 /* This code is not used for mini-buffer for the sake of the case
14998 of redisplaying to replace an echo area message; since in
14999 that case the mini-buffer contents per se are usually
15000 unchanged. This code is of no real use in the mini-buffer
15001 since the handling of this_line_start_pos, etc., in redisplay
15002 handles the same cases. */
15003 && !EQ (window, minibuf_window)
15004 && (FRAME_WINDOW_P (f)
15005 || !overlay_arrow_in_current_buffer_p ()))
15006 {
15007 int this_scroll_margin, top_scroll_margin;
15008 struct glyph_row *row = NULL;
15009 int frame_line_height = default_line_pixel_height (w);
15010 int window_total_lines
15011 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15012
15013 #ifdef GLYPH_DEBUG
15014 debug_method_add (w, "cursor movement");
15015 #endif
15016
15017 /* Scroll if point within this distance from the top or bottom
15018 of the window. This is a pixel value. */
15019 if (scroll_margin > 0)
15020 {
15021 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15022 this_scroll_margin *= frame_line_height;
15023 }
15024 else
15025 this_scroll_margin = 0;
15026
15027 top_scroll_margin = this_scroll_margin;
15028 if (WINDOW_WANTS_HEADER_LINE_P (w))
15029 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15030
15031 /* Start with the row the cursor was displayed during the last
15032 not paused redisplay. Give up if that row is not valid. */
15033 if (w->last_cursor_vpos < 0
15034 || w->last_cursor_vpos >= w->current_matrix->nrows)
15035 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15036 else
15037 {
15038 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15039 if (row->mode_line_p)
15040 ++row;
15041 if (!row->enabled_p)
15042 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15043 }
15044
15045 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15046 {
15047 int scroll_p = 0, must_scroll = 0;
15048 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15049
15050 if (PT > w->last_point)
15051 {
15052 /* Point has moved forward. */
15053 while (MATRIX_ROW_END_CHARPOS (row) < PT
15054 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15055 {
15056 eassert (row->enabled_p);
15057 ++row;
15058 }
15059
15060 /* If the end position of a row equals the start
15061 position of the next row, and PT is at that position,
15062 we would rather display cursor in the next line. */
15063 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15064 && MATRIX_ROW_END_CHARPOS (row) == PT
15065 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15066 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15067 && !cursor_row_p (row))
15068 ++row;
15069
15070 /* If within the scroll margin, scroll. Note that
15071 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15072 the next line would be drawn, and that
15073 this_scroll_margin can be zero. */
15074 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15075 || PT > MATRIX_ROW_END_CHARPOS (row)
15076 /* Line is completely visible last line in window
15077 and PT is to be set in the next line. */
15078 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15079 && PT == MATRIX_ROW_END_CHARPOS (row)
15080 && !row->ends_at_zv_p
15081 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15082 scroll_p = 1;
15083 }
15084 else if (PT < w->last_point)
15085 {
15086 /* Cursor has to be moved backward. Note that PT >=
15087 CHARPOS (startp) because of the outer if-statement. */
15088 while (!row->mode_line_p
15089 && (MATRIX_ROW_START_CHARPOS (row) > PT
15090 || (MATRIX_ROW_START_CHARPOS (row) == PT
15091 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15092 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15093 row > w->current_matrix->rows
15094 && (row-1)->ends_in_newline_from_string_p))))
15095 && (row->y > top_scroll_margin
15096 || CHARPOS (startp) == BEGV))
15097 {
15098 eassert (row->enabled_p);
15099 --row;
15100 }
15101
15102 /* Consider the following case: Window starts at BEGV,
15103 there is invisible, intangible text at BEGV, so that
15104 display starts at some point START > BEGV. It can
15105 happen that we are called with PT somewhere between
15106 BEGV and START. Try to handle that case. */
15107 if (row < w->current_matrix->rows
15108 || row->mode_line_p)
15109 {
15110 row = w->current_matrix->rows;
15111 if (row->mode_line_p)
15112 ++row;
15113 }
15114
15115 /* Due to newlines in overlay strings, we may have to
15116 skip forward over overlay strings. */
15117 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15118 && MATRIX_ROW_END_CHARPOS (row) == PT
15119 && !cursor_row_p (row))
15120 ++row;
15121
15122 /* If within the scroll margin, scroll. */
15123 if (row->y < top_scroll_margin
15124 && CHARPOS (startp) != BEGV)
15125 scroll_p = 1;
15126 }
15127 else
15128 {
15129 /* Cursor did not move. So don't scroll even if cursor line
15130 is partially visible, as it was so before. */
15131 rc = CURSOR_MOVEMENT_SUCCESS;
15132 }
15133
15134 if (PT < MATRIX_ROW_START_CHARPOS (row)
15135 || PT > MATRIX_ROW_END_CHARPOS (row))
15136 {
15137 /* if PT is not in the glyph row, give up. */
15138 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15139 must_scroll = 1;
15140 }
15141 else if (rc != CURSOR_MOVEMENT_SUCCESS
15142 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15143 {
15144 struct glyph_row *row1;
15145
15146 /* If rows are bidi-reordered and point moved, back up
15147 until we find a row that does not belong to a
15148 continuation line. This is because we must consider
15149 all rows of a continued line as candidates for the
15150 new cursor positioning, since row start and end
15151 positions change non-linearly with vertical position
15152 in such rows. */
15153 /* FIXME: Revisit this when glyph ``spilling'' in
15154 continuation lines' rows is implemented for
15155 bidi-reordered rows. */
15156 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15157 MATRIX_ROW_CONTINUATION_LINE_P (row);
15158 --row)
15159 {
15160 /* If we hit the beginning of the displayed portion
15161 without finding the first row of a continued
15162 line, give up. */
15163 if (row <= row1)
15164 {
15165 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15166 break;
15167 }
15168 eassert (row->enabled_p);
15169 }
15170 }
15171 if (must_scroll)
15172 ;
15173 else if (rc != CURSOR_MOVEMENT_SUCCESS
15174 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15175 /* Make sure this isn't a header line by any chance, since
15176 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15177 && !row->mode_line_p
15178 && make_cursor_line_fully_visible_p)
15179 {
15180 if (PT == MATRIX_ROW_END_CHARPOS (row)
15181 && !row->ends_at_zv_p
15182 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15183 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15184 else if (row->height > window_box_height (w))
15185 {
15186 /* If we end up in a partially visible line, let's
15187 make it fully visible, except when it's taller
15188 than the window, in which case we can't do much
15189 about it. */
15190 *scroll_step = 1;
15191 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15192 }
15193 else
15194 {
15195 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15196 if (!cursor_row_fully_visible_p (w, 0, 1))
15197 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15198 else
15199 rc = CURSOR_MOVEMENT_SUCCESS;
15200 }
15201 }
15202 else if (scroll_p)
15203 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15204 else if (rc != CURSOR_MOVEMENT_SUCCESS
15205 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15206 {
15207 /* With bidi-reordered rows, there could be more than
15208 one candidate row whose start and end positions
15209 occlude point. We need to let set_cursor_from_row
15210 find the best candidate. */
15211 /* FIXME: Revisit this when glyph ``spilling'' in
15212 continuation lines' rows is implemented for
15213 bidi-reordered rows. */
15214 int rv = 0;
15215
15216 do
15217 {
15218 int at_zv_p = 0, exact_match_p = 0;
15219
15220 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15221 && PT <= MATRIX_ROW_END_CHARPOS (row)
15222 && cursor_row_p (row))
15223 rv |= set_cursor_from_row (w, row, w->current_matrix,
15224 0, 0, 0, 0);
15225 /* As soon as we've found the exact match for point,
15226 or the first suitable row whose ends_at_zv_p flag
15227 is set, we are done. */
15228 at_zv_p =
15229 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15230 if (rv && !at_zv_p
15231 && w->cursor.hpos >= 0
15232 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15233 w->cursor.vpos))
15234 {
15235 struct glyph_row *candidate =
15236 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15237 struct glyph *g =
15238 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15239 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15240
15241 exact_match_p =
15242 (BUFFERP (g->object) && g->charpos == PT)
15243 || (INTEGERP (g->object)
15244 && (g->charpos == PT
15245 || (g->charpos == 0 && endpos - 1 == PT)));
15246 }
15247 if (rv && (at_zv_p || exact_match_p))
15248 {
15249 rc = CURSOR_MOVEMENT_SUCCESS;
15250 break;
15251 }
15252 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15253 break;
15254 ++row;
15255 }
15256 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15257 || row->continued_p)
15258 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15259 || (MATRIX_ROW_START_CHARPOS (row) == PT
15260 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15261 /* If we didn't find any candidate rows, or exited the
15262 loop before all the candidates were examined, signal
15263 to the caller that this method failed. */
15264 if (rc != CURSOR_MOVEMENT_SUCCESS
15265 && !(rv
15266 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15267 && !row->continued_p))
15268 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15269 else if (rv)
15270 rc = CURSOR_MOVEMENT_SUCCESS;
15271 }
15272 else
15273 {
15274 do
15275 {
15276 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15277 {
15278 rc = CURSOR_MOVEMENT_SUCCESS;
15279 break;
15280 }
15281 ++row;
15282 }
15283 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15284 && MATRIX_ROW_START_CHARPOS (row) == PT
15285 && cursor_row_p (row));
15286 }
15287 }
15288 }
15289
15290 return rc;
15291 }
15292
15293 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15294 static
15295 #endif
15296 void
15297 set_vertical_scroll_bar (struct window *w)
15298 {
15299 ptrdiff_t start, end, whole;
15300
15301 /* Calculate the start and end positions for the current window.
15302 At some point, it would be nice to choose between scrollbars
15303 which reflect the whole buffer size, with special markers
15304 indicating narrowing, and scrollbars which reflect only the
15305 visible region.
15306
15307 Note that mini-buffers sometimes aren't displaying any text. */
15308 if (!MINI_WINDOW_P (w)
15309 || (w == XWINDOW (minibuf_window)
15310 && NILP (echo_area_buffer[0])))
15311 {
15312 struct buffer *buf = XBUFFER (w->contents);
15313 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15314 start = marker_position (w->start) - BUF_BEGV (buf);
15315 /* I don't think this is guaranteed to be right. For the
15316 moment, we'll pretend it is. */
15317 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15318
15319 if (end < start)
15320 end = start;
15321 if (whole < (end - start))
15322 whole = end - start;
15323 }
15324 else
15325 start = end = whole = 0;
15326
15327 /* Indicate what this scroll bar ought to be displaying now. */
15328 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15329 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15330 (w, end - start, whole, start);
15331 }
15332
15333
15334 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15335 selected_window is redisplayed.
15336
15337 We can return without actually redisplaying the window if fonts has been
15338 changed on window's frame. In that case, redisplay_internal will retry. */
15339
15340 static void
15341 redisplay_window (Lisp_Object window, int just_this_one_p)
15342 {
15343 struct window *w = XWINDOW (window);
15344 struct frame *f = XFRAME (w->frame);
15345 struct buffer *buffer = XBUFFER (w->contents);
15346 struct buffer *old = current_buffer;
15347 struct text_pos lpoint, opoint, startp;
15348 int update_mode_line;
15349 int tem;
15350 struct it it;
15351 /* Record it now because it's overwritten. */
15352 int current_matrix_up_to_date_p = 0;
15353 int used_current_matrix_p = 0;
15354 /* This is less strict than current_matrix_up_to_date_p.
15355 It indicates that the buffer contents and narrowing are unchanged. */
15356 int buffer_unchanged_p = 0;
15357 int temp_scroll_step = 0;
15358 ptrdiff_t count = SPECPDL_INDEX ();
15359 int rc;
15360 int centering_position = -1;
15361 int last_line_misfit = 0;
15362 ptrdiff_t beg_unchanged, end_unchanged;
15363 int frame_line_height;
15364
15365 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15366 opoint = lpoint;
15367
15368 #ifdef GLYPH_DEBUG
15369 *w->desired_matrix->method = 0;
15370 #endif
15371
15372 /* Make sure that both W's markers are valid. */
15373 eassert (XMARKER (w->start)->buffer == buffer);
15374 eassert (XMARKER (w->pointm)->buffer == buffer);
15375
15376 restart:
15377 reconsider_clip_changes (w);
15378 frame_line_height = default_line_pixel_height (w);
15379
15380 /* Has the mode line to be updated? */
15381 update_mode_line = (w->update_mode_line
15382 || update_mode_lines
15383 || buffer->clip_changed
15384 || buffer->prevent_redisplay_optimizations_p);
15385
15386 if (MINI_WINDOW_P (w))
15387 {
15388 if (w == XWINDOW (echo_area_window)
15389 && !NILP (echo_area_buffer[0]))
15390 {
15391 if (update_mode_line)
15392 /* We may have to update a tty frame's menu bar or a
15393 tool-bar. Example `M-x C-h C-h C-g'. */
15394 goto finish_menu_bars;
15395 else
15396 /* We've already displayed the echo area glyphs in this window. */
15397 goto finish_scroll_bars;
15398 }
15399 else if ((w != XWINDOW (minibuf_window)
15400 || minibuf_level == 0)
15401 /* When buffer is nonempty, redisplay window normally. */
15402 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15403 /* Quail displays non-mini buffers in minibuffer window.
15404 In that case, redisplay the window normally. */
15405 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15406 {
15407 /* W is a mini-buffer window, but it's not active, so clear
15408 it. */
15409 int yb = window_text_bottom_y (w);
15410 struct glyph_row *row;
15411 int y;
15412
15413 for (y = 0, row = w->desired_matrix->rows;
15414 y < yb;
15415 y += row->height, ++row)
15416 blank_row (w, row, y);
15417 goto finish_scroll_bars;
15418 }
15419
15420 clear_glyph_matrix (w->desired_matrix);
15421 }
15422
15423 /* Otherwise set up data on this window; select its buffer and point
15424 value. */
15425 /* Really select the buffer, for the sake of buffer-local
15426 variables. */
15427 set_buffer_internal_1 (XBUFFER (w->contents));
15428
15429 current_matrix_up_to_date_p
15430 = (w->window_end_valid
15431 && !current_buffer->clip_changed
15432 && !current_buffer->prevent_redisplay_optimizations_p
15433 && !window_outdated (w));
15434
15435 /* Run the window-bottom-change-functions
15436 if it is possible that the text on the screen has changed
15437 (either due to modification of the text, or any other reason). */
15438 if (!current_matrix_up_to_date_p
15439 && !NILP (Vwindow_text_change_functions))
15440 {
15441 safe_run_hooks (Qwindow_text_change_functions);
15442 goto restart;
15443 }
15444
15445 beg_unchanged = BEG_UNCHANGED;
15446 end_unchanged = END_UNCHANGED;
15447
15448 SET_TEXT_POS (opoint, PT, PT_BYTE);
15449
15450 specbind (Qinhibit_point_motion_hooks, Qt);
15451
15452 buffer_unchanged_p
15453 = (w->window_end_valid
15454 && !current_buffer->clip_changed
15455 && !window_outdated (w));
15456
15457 /* When windows_or_buffers_changed is non-zero, we can't rely
15458 on the window end being valid, so set it to zero there. */
15459 if (windows_or_buffers_changed)
15460 {
15461 /* If window starts on a continuation line, maybe adjust the
15462 window start in case the window's width changed. */
15463 if (XMARKER (w->start)->buffer == current_buffer)
15464 compute_window_start_on_continuation_line (w);
15465
15466 w->window_end_valid = 0;
15467 /* If so, we also can't rely on current matrix
15468 and should not fool try_cursor_movement below. */
15469 current_matrix_up_to_date_p = 0;
15470 }
15471
15472 /* Some sanity checks. */
15473 CHECK_WINDOW_END (w);
15474 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15475 emacs_abort ();
15476 if (BYTEPOS (opoint) < CHARPOS (opoint))
15477 emacs_abort ();
15478
15479 if (mode_line_update_needed (w))
15480 update_mode_line = 1;
15481
15482 /* Point refers normally to the selected window. For any other
15483 window, set up appropriate value. */
15484 if (!EQ (window, selected_window))
15485 {
15486 ptrdiff_t new_pt = marker_position (w->pointm);
15487 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15488 if (new_pt < BEGV)
15489 {
15490 new_pt = BEGV;
15491 new_pt_byte = BEGV_BYTE;
15492 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15493 }
15494 else if (new_pt > (ZV - 1))
15495 {
15496 new_pt = ZV;
15497 new_pt_byte = ZV_BYTE;
15498 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15499 }
15500
15501 /* We don't use SET_PT so that the point-motion hooks don't run. */
15502 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15503 }
15504
15505 /* If any of the character widths specified in the display table
15506 have changed, invalidate the width run cache. It's true that
15507 this may be a bit late to catch such changes, but the rest of
15508 redisplay goes (non-fatally) haywire when the display table is
15509 changed, so why should we worry about doing any better? */
15510 if (current_buffer->width_run_cache)
15511 {
15512 struct Lisp_Char_Table *disptab = buffer_display_table ();
15513
15514 if (! disptab_matches_widthtab
15515 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15516 {
15517 invalidate_region_cache (current_buffer,
15518 current_buffer->width_run_cache,
15519 BEG, Z);
15520 recompute_width_table (current_buffer, disptab);
15521 }
15522 }
15523
15524 /* If window-start is screwed up, choose a new one. */
15525 if (XMARKER (w->start)->buffer != current_buffer)
15526 goto recenter;
15527
15528 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15529
15530 /* If someone specified a new starting point but did not insist,
15531 check whether it can be used. */
15532 if (w->optional_new_start
15533 && CHARPOS (startp) >= BEGV
15534 && CHARPOS (startp) <= ZV)
15535 {
15536 w->optional_new_start = 0;
15537 start_display (&it, w, startp);
15538 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15539 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15540 if (IT_CHARPOS (it) == PT)
15541 w->force_start = 1;
15542 /* IT may overshoot PT if text at PT is invisible. */
15543 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15544 w->force_start = 1;
15545 }
15546
15547 force_start:
15548
15549 /* Handle case where place to start displaying has been specified,
15550 unless the specified location is outside the accessible range. */
15551 if (w->force_start || window_frozen_p (w))
15552 {
15553 /* We set this later on if we have to adjust point. */
15554 int new_vpos = -1;
15555
15556 w->force_start = 0;
15557 w->vscroll = 0;
15558 w->window_end_valid = 0;
15559
15560 /* Forget any recorded base line for line number display. */
15561 if (!buffer_unchanged_p)
15562 w->base_line_number = 0;
15563
15564 /* Redisplay the mode line. Select the buffer properly for that.
15565 Also, run the hook window-scroll-functions
15566 because we have scrolled. */
15567 /* Note, we do this after clearing force_start because
15568 if there's an error, it is better to forget about force_start
15569 than to get into an infinite loop calling the hook functions
15570 and having them get more errors. */
15571 if (!update_mode_line
15572 || ! NILP (Vwindow_scroll_functions))
15573 {
15574 update_mode_line = 1;
15575 w->update_mode_line = 1;
15576 startp = run_window_scroll_functions (window, startp);
15577 }
15578
15579 if (CHARPOS (startp) < BEGV)
15580 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15581 else if (CHARPOS (startp) > ZV)
15582 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15583
15584 /* Redisplay, then check if cursor has been set during the
15585 redisplay. Give up if new fonts were loaded. */
15586 /* We used to issue a CHECK_MARGINS argument to try_window here,
15587 but this causes scrolling to fail when point begins inside
15588 the scroll margin (bug#148) -- cyd */
15589 if (!try_window (window, startp, 0))
15590 {
15591 w->force_start = 1;
15592 clear_glyph_matrix (w->desired_matrix);
15593 goto need_larger_matrices;
15594 }
15595
15596 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15597 {
15598 /* If point does not appear, try to move point so it does
15599 appear. The desired matrix has been built above, so we
15600 can use it here. */
15601 new_vpos = window_box_height (w) / 2;
15602 }
15603
15604 if (!cursor_row_fully_visible_p (w, 0, 0))
15605 {
15606 /* Point does appear, but on a line partly visible at end of window.
15607 Move it back to a fully-visible line. */
15608 new_vpos = window_box_height (w);
15609 }
15610 else if (w->cursor.vpos >=0)
15611 {
15612 /* Some people insist on not letting point enter the scroll
15613 margin, even though this part handles windows that didn't
15614 scroll at all. */
15615 int window_total_lines
15616 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15617 int margin = min (scroll_margin, window_total_lines / 4);
15618 int pixel_margin = margin * frame_line_height;
15619 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15620
15621 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15622 below, which finds the row to move point to, advances by
15623 the Y coordinate of the _next_ row, see the definition of
15624 MATRIX_ROW_BOTTOM_Y. */
15625 if (w->cursor.vpos < margin + header_line)
15626 {
15627 w->cursor.vpos = -1;
15628 clear_glyph_matrix (w->desired_matrix);
15629 goto try_to_scroll;
15630 }
15631 else
15632 {
15633 int window_height = window_box_height (w);
15634
15635 if (header_line)
15636 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15637 if (w->cursor.y >= window_height - pixel_margin)
15638 {
15639 w->cursor.vpos = -1;
15640 clear_glyph_matrix (w->desired_matrix);
15641 goto try_to_scroll;
15642 }
15643 }
15644 }
15645
15646 /* If we need to move point for either of the above reasons,
15647 now actually do it. */
15648 if (new_vpos >= 0)
15649 {
15650 struct glyph_row *row;
15651
15652 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15653 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15654 ++row;
15655
15656 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15657 MATRIX_ROW_START_BYTEPOS (row));
15658
15659 if (w != XWINDOW (selected_window))
15660 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15661 else if (current_buffer == old)
15662 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15663
15664 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15665
15666 /* If we are highlighting the region, then we just changed
15667 the region, so redisplay to show it. */
15668 if (markpos_of_region () >= 0)
15669 {
15670 clear_glyph_matrix (w->desired_matrix);
15671 if (!try_window (window, startp, 0))
15672 goto need_larger_matrices;
15673 }
15674 }
15675
15676 #ifdef GLYPH_DEBUG
15677 debug_method_add (w, "forced window start");
15678 #endif
15679 goto done;
15680 }
15681
15682 /* Handle case where text has not changed, only point, and it has
15683 not moved off the frame, and we are not retrying after hscroll.
15684 (current_matrix_up_to_date_p is nonzero when retrying.) */
15685 if (current_matrix_up_to_date_p
15686 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15687 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15688 {
15689 switch (rc)
15690 {
15691 case CURSOR_MOVEMENT_SUCCESS:
15692 used_current_matrix_p = 1;
15693 goto done;
15694
15695 case CURSOR_MOVEMENT_MUST_SCROLL:
15696 goto try_to_scroll;
15697
15698 default:
15699 emacs_abort ();
15700 }
15701 }
15702 /* If current starting point was originally the beginning of a line
15703 but no longer is, find a new starting point. */
15704 else if (w->start_at_line_beg
15705 && !(CHARPOS (startp) <= BEGV
15706 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15707 {
15708 #ifdef GLYPH_DEBUG
15709 debug_method_add (w, "recenter 1");
15710 #endif
15711 goto recenter;
15712 }
15713
15714 /* Try scrolling with try_window_id. Value is > 0 if update has
15715 been done, it is -1 if we know that the same window start will
15716 not work. It is 0 if unsuccessful for some other reason. */
15717 else if ((tem = try_window_id (w)) != 0)
15718 {
15719 #ifdef GLYPH_DEBUG
15720 debug_method_add (w, "try_window_id %d", tem);
15721 #endif
15722
15723 if (f->fonts_changed)
15724 goto need_larger_matrices;
15725 if (tem > 0)
15726 goto done;
15727
15728 /* Otherwise try_window_id has returned -1 which means that we
15729 don't want the alternative below this comment to execute. */
15730 }
15731 else if (CHARPOS (startp) >= BEGV
15732 && CHARPOS (startp) <= ZV
15733 && PT >= CHARPOS (startp)
15734 && (CHARPOS (startp) < ZV
15735 /* Avoid starting at end of buffer. */
15736 || CHARPOS (startp) == BEGV
15737 || !window_outdated (w)))
15738 {
15739 int d1, d2, d3, d4, d5, d6;
15740
15741 /* If first window line is a continuation line, and window start
15742 is inside the modified region, but the first change is before
15743 current window start, we must select a new window start.
15744
15745 However, if this is the result of a down-mouse event (e.g. by
15746 extending the mouse-drag-overlay), we don't want to select a
15747 new window start, since that would change the position under
15748 the mouse, resulting in an unwanted mouse-movement rather
15749 than a simple mouse-click. */
15750 if (!w->start_at_line_beg
15751 && NILP (do_mouse_tracking)
15752 && CHARPOS (startp) > BEGV
15753 && CHARPOS (startp) > BEG + beg_unchanged
15754 && CHARPOS (startp) <= Z - end_unchanged
15755 /* Even if w->start_at_line_beg is nil, a new window may
15756 start at a line_beg, since that's how set_buffer_window
15757 sets it. So, we need to check the return value of
15758 compute_window_start_on_continuation_line. (See also
15759 bug#197). */
15760 && XMARKER (w->start)->buffer == current_buffer
15761 && compute_window_start_on_continuation_line (w)
15762 /* It doesn't make sense to force the window start like we
15763 do at label force_start if it is already known that point
15764 will not be visible in the resulting window, because
15765 doing so will move point from its correct position
15766 instead of scrolling the window to bring point into view.
15767 See bug#9324. */
15768 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15769 {
15770 w->force_start = 1;
15771 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15772 goto force_start;
15773 }
15774
15775 #ifdef GLYPH_DEBUG
15776 debug_method_add (w, "same window start");
15777 #endif
15778
15779 /* Try to redisplay starting at same place as before.
15780 If point has not moved off frame, accept the results. */
15781 if (!current_matrix_up_to_date_p
15782 /* Don't use try_window_reusing_current_matrix in this case
15783 because a window scroll function can have changed the
15784 buffer. */
15785 || !NILP (Vwindow_scroll_functions)
15786 || MINI_WINDOW_P (w)
15787 || !(used_current_matrix_p
15788 = try_window_reusing_current_matrix (w)))
15789 {
15790 IF_DEBUG (debug_method_add (w, "1"));
15791 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15792 /* -1 means we need to scroll.
15793 0 means we need new matrices, but fonts_changed
15794 is set in that case, so we will detect it below. */
15795 goto try_to_scroll;
15796 }
15797
15798 if (f->fonts_changed)
15799 goto need_larger_matrices;
15800
15801 if (w->cursor.vpos >= 0)
15802 {
15803 if (!just_this_one_p
15804 || current_buffer->clip_changed
15805 || BEG_UNCHANGED < CHARPOS (startp))
15806 /* Forget any recorded base line for line number display. */
15807 w->base_line_number = 0;
15808
15809 if (!cursor_row_fully_visible_p (w, 1, 0))
15810 {
15811 clear_glyph_matrix (w->desired_matrix);
15812 last_line_misfit = 1;
15813 }
15814 /* Drop through and scroll. */
15815 else
15816 goto done;
15817 }
15818 else
15819 clear_glyph_matrix (w->desired_matrix);
15820 }
15821
15822 try_to_scroll:
15823
15824 /* Redisplay the mode line. Select the buffer properly for that. */
15825 if (!update_mode_line)
15826 {
15827 update_mode_line = 1;
15828 w->update_mode_line = 1;
15829 }
15830
15831 /* Try to scroll by specified few lines. */
15832 if ((scroll_conservatively
15833 || emacs_scroll_step
15834 || temp_scroll_step
15835 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15836 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15837 && CHARPOS (startp) >= BEGV
15838 && CHARPOS (startp) <= ZV)
15839 {
15840 /* The function returns -1 if new fonts were loaded, 1 if
15841 successful, 0 if not successful. */
15842 int ss = try_scrolling (window, just_this_one_p,
15843 scroll_conservatively,
15844 emacs_scroll_step,
15845 temp_scroll_step, last_line_misfit);
15846 switch (ss)
15847 {
15848 case SCROLLING_SUCCESS:
15849 goto done;
15850
15851 case SCROLLING_NEED_LARGER_MATRICES:
15852 goto need_larger_matrices;
15853
15854 case SCROLLING_FAILED:
15855 break;
15856
15857 default:
15858 emacs_abort ();
15859 }
15860 }
15861
15862 /* Finally, just choose a place to start which positions point
15863 according to user preferences. */
15864
15865 recenter:
15866
15867 #ifdef GLYPH_DEBUG
15868 debug_method_add (w, "recenter");
15869 #endif
15870
15871 /* Forget any previously recorded base line for line number display. */
15872 if (!buffer_unchanged_p)
15873 w->base_line_number = 0;
15874
15875 /* Determine the window start relative to point. */
15876 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15877 it.current_y = it.last_visible_y;
15878 if (centering_position < 0)
15879 {
15880 int window_total_lines
15881 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15882 int margin =
15883 scroll_margin > 0
15884 ? min (scroll_margin, window_total_lines / 4)
15885 : 0;
15886 ptrdiff_t margin_pos = CHARPOS (startp);
15887 Lisp_Object aggressive;
15888 int scrolling_up;
15889
15890 /* If there is a scroll margin at the top of the window, find
15891 its character position. */
15892 if (margin
15893 /* Cannot call start_display if startp is not in the
15894 accessible region of the buffer. This can happen when we
15895 have just switched to a different buffer and/or changed
15896 its restriction. In that case, startp is initialized to
15897 the character position 1 (BEGV) because we did not yet
15898 have chance to display the buffer even once. */
15899 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15900 {
15901 struct it it1;
15902 void *it1data = NULL;
15903
15904 SAVE_IT (it1, it, it1data);
15905 start_display (&it1, w, startp);
15906 move_it_vertically (&it1, margin * frame_line_height);
15907 margin_pos = IT_CHARPOS (it1);
15908 RESTORE_IT (&it, &it, it1data);
15909 }
15910 scrolling_up = PT > margin_pos;
15911 aggressive =
15912 scrolling_up
15913 ? BVAR (current_buffer, scroll_up_aggressively)
15914 : BVAR (current_buffer, scroll_down_aggressively);
15915
15916 if (!MINI_WINDOW_P (w)
15917 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15918 {
15919 int pt_offset = 0;
15920
15921 /* Setting scroll-conservatively overrides
15922 scroll-*-aggressively. */
15923 if (!scroll_conservatively && NUMBERP (aggressive))
15924 {
15925 double float_amount = XFLOATINT (aggressive);
15926
15927 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15928 if (pt_offset == 0 && float_amount > 0)
15929 pt_offset = 1;
15930 if (pt_offset && margin > 0)
15931 margin -= 1;
15932 }
15933 /* Compute how much to move the window start backward from
15934 point so that point will be displayed where the user
15935 wants it. */
15936 if (scrolling_up)
15937 {
15938 centering_position = it.last_visible_y;
15939 if (pt_offset)
15940 centering_position -= pt_offset;
15941 centering_position -=
15942 frame_line_height * (1 + margin + (last_line_misfit != 0))
15943 + WINDOW_HEADER_LINE_HEIGHT (w);
15944 /* Don't let point enter the scroll margin near top of
15945 the window. */
15946 if (centering_position < margin * frame_line_height)
15947 centering_position = margin * frame_line_height;
15948 }
15949 else
15950 centering_position = margin * frame_line_height + pt_offset;
15951 }
15952 else
15953 /* Set the window start half the height of the window backward
15954 from point. */
15955 centering_position = window_box_height (w) / 2;
15956 }
15957 move_it_vertically_backward (&it, centering_position);
15958
15959 eassert (IT_CHARPOS (it) >= BEGV);
15960
15961 /* The function move_it_vertically_backward may move over more
15962 than the specified y-distance. If it->w is small, e.g. a
15963 mini-buffer window, we may end up in front of the window's
15964 display area. Start displaying at the start of the line
15965 containing PT in this case. */
15966 if (it.current_y <= 0)
15967 {
15968 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15969 move_it_vertically_backward (&it, 0);
15970 it.current_y = 0;
15971 }
15972
15973 it.current_x = it.hpos = 0;
15974
15975 /* Set the window start position here explicitly, to avoid an
15976 infinite loop in case the functions in window-scroll-functions
15977 get errors. */
15978 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15979
15980 /* Run scroll hooks. */
15981 startp = run_window_scroll_functions (window, it.current.pos);
15982
15983 /* Redisplay the window. */
15984 if (!current_matrix_up_to_date_p
15985 || windows_or_buffers_changed
15986 || f->cursor_type_changed
15987 /* Don't use try_window_reusing_current_matrix in this case
15988 because it can have changed the buffer. */
15989 || !NILP (Vwindow_scroll_functions)
15990 || !just_this_one_p
15991 || MINI_WINDOW_P (w)
15992 || !(used_current_matrix_p
15993 = try_window_reusing_current_matrix (w)))
15994 try_window (window, startp, 0);
15995
15996 /* If new fonts have been loaded (due to fontsets), give up. We
15997 have to start a new redisplay since we need to re-adjust glyph
15998 matrices. */
15999 if (f->fonts_changed)
16000 goto need_larger_matrices;
16001
16002 /* If cursor did not appear assume that the middle of the window is
16003 in the first line of the window. Do it again with the next line.
16004 (Imagine a window of height 100, displaying two lines of height
16005 60. Moving back 50 from it->last_visible_y will end in the first
16006 line.) */
16007 if (w->cursor.vpos < 0)
16008 {
16009 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16010 {
16011 clear_glyph_matrix (w->desired_matrix);
16012 move_it_by_lines (&it, 1);
16013 try_window (window, it.current.pos, 0);
16014 }
16015 else if (PT < IT_CHARPOS (it))
16016 {
16017 clear_glyph_matrix (w->desired_matrix);
16018 move_it_by_lines (&it, -1);
16019 try_window (window, it.current.pos, 0);
16020 }
16021 else
16022 {
16023 /* Not much we can do about it. */
16024 }
16025 }
16026
16027 /* Consider the following case: Window starts at BEGV, there is
16028 invisible, intangible text at BEGV, so that display starts at
16029 some point START > BEGV. It can happen that we are called with
16030 PT somewhere between BEGV and START. Try to handle that case. */
16031 if (w->cursor.vpos < 0)
16032 {
16033 struct glyph_row *row = w->current_matrix->rows;
16034 if (row->mode_line_p)
16035 ++row;
16036 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16037 }
16038
16039 if (!cursor_row_fully_visible_p (w, 0, 0))
16040 {
16041 /* If vscroll is enabled, disable it and try again. */
16042 if (w->vscroll)
16043 {
16044 w->vscroll = 0;
16045 clear_glyph_matrix (w->desired_matrix);
16046 goto recenter;
16047 }
16048
16049 /* Users who set scroll-conservatively to a large number want
16050 point just above/below the scroll margin. If we ended up
16051 with point's row partially visible, move the window start to
16052 make that row fully visible and out of the margin. */
16053 if (scroll_conservatively > SCROLL_LIMIT)
16054 {
16055 int window_total_lines
16056 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16057 int margin =
16058 scroll_margin > 0
16059 ? min (scroll_margin, window_total_lines / 4)
16060 : 0;
16061 int move_down = w->cursor.vpos >= window_total_lines / 2;
16062
16063 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16064 clear_glyph_matrix (w->desired_matrix);
16065 if (1 == try_window (window, it.current.pos,
16066 TRY_WINDOW_CHECK_MARGINS))
16067 goto done;
16068 }
16069
16070 /* If centering point failed to make the whole line visible,
16071 put point at the top instead. That has to make the whole line
16072 visible, if it can be done. */
16073 if (centering_position == 0)
16074 goto done;
16075
16076 clear_glyph_matrix (w->desired_matrix);
16077 centering_position = 0;
16078 goto recenter;
16079 }
16080
16081 done:
16082
16083 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16084 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16085 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16086
16087 /* Display the mode line, if we must. */
16088 if ((update_mode_line
16089 /* If window not full width, must redo its mode line
16090 if (a) the window to its side is being redone and
16091 (b) we do a frame-based redisplay. This is a consequence
16092 of how inverted lines are drawn in frame-based redisplay. */
16093 || (!just_this_one_p
16094 && !FRAME_WINDOW_P (f)
16095 && !WINDOW_FULL_WIDTH_P (w))
16096 /* Line number to display. */
16097 || w->base_line_pos > 0
16098 /* Column number is displayed and different from the one displayed. */
16099 || (w->column_number_displayed != -1
16100 && (w->column_number_displayed != current_column ())))
16101 /* This means that the window has a mode line. */
16102 && (WINDOW_WANTS_MODELINE_P (w)
16103 || WINDOW_WANTS_HEADER_LINE_P (w)))
16104 {
16105 display_mode_lines (w);
16106
16107 /* If mode line height has changed, arrange for a thorough
16108 immediate redisplay using the correct mode line height. */
16109 if (WINDOW_WANTS_MODELINE_P (w)
16110 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16111 {
16112 f->fonts_changed = 1;
16113 w->mode_line_height = -1;
16114 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16115 = DESIRED_MODE_LINE_HEIGHT (w);
16116 }
16117
16118 /* If header line height has changed, arrange for a thorough
16119 immediate redisplay using the correct header line height. */
16120 if (WINDOW_WANTS_HEADER_LINE_P (w)
16121 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16122 {
16123 f->fonts_changed = 1;
16124 w->header_line_height = -1;
16125 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16126 = DESIRED_HEADER_LINE_HEIGHT (w);
16127 }
16128
16129 if (f->fonts_changed)
16130 goto need_larger_matrices;
16131 }
16132
16133 if (!line_number_displayed && w->base_line_pos != -1)
16134 {
16135 w->base_line_pos = 0;
16136 w->base_line_number = 0;
16137 }
16138
16139 finish_menu_bars:
16140
16141 /* When we reach a frame's selected window, redo the frame's menu bar. */
16142 if (update_mode_line
16143 && EQ (FRAME_SELECTED_WINDOW (f), window))
16144 {
16145 int redisplay_menu_p = 0;
16146
16147 if (FRAME_WINDOW_P (f))
16148 {
16149 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16150 || defined (HAVE_NS) || defined (USE_GTK)
16151 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16152 #else
16153 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16154 #endif
16155 }
16156 else
16157 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16158
16159 if (redisplay_menu_p)
16160 display_menu_bar (w);
16161
16162 #ifdef HAVE_WINDOW_SYSTEM
16163 if (FRAME_WINDOW_P (f))
16164 {
16165 #if defined (USE_GTK) || defined (HAVE_NS)
16166 if (FRAME_EXTERNAL_TOOL_BAR (f))
16167 redisplay_tool_bar (f);
16168 #else
16169 if (WINDOWP (f->tool_bar_window)
16170 && (FRAME_TOOL_BAR_LINES (f) > 0
16171 || !NILP (Vauto_resize_tool_bars))
16172 && redisplay_tool_bar (f))
16173 ignore_mouse_drag_p = 1;
16174 #endif
16175 }
16176 #endif
16177 }
16178
16179 #ifdef HAVE_WINDOW_SYSTEM
16180 if (FRAME_WINDOW_P (f)
16181 && update_window_fringes (w, (just_this_one_p
16182 || (!used_current_matrix_p && !overlay_arrow_seen)
16183 || w->pseudo_window_p)))
16184 {
16185 update_begin (f);
16186 block_input ();
16187 if (draw_window_fringes (w, 1))
16188 x_draw_vertical_border (w);
16189 unblock_input ();
16190 update_end (f);
16191 }
16192 #endif /* HAVE_WINDOW_SYSTEM */
16193
16194 /* We go to this label, with fonts_changed set, if it is
16195 necessary to try again using larger glyph matrices.
16196 We have to redeem the scroll bar even in this case,
16197 because the loop in redisplay_internal expects that. */
16198 need_larger_matrices:
16199 ;
16200 finish_scroll_bars:
16201
16202 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16203 {
16204 /* Set the thumb's position and size. */
16205 set_vertical_scroll_bar (w);
16206
16207 /* Note that we actually used the scroll bar attached to this
16208 window, so it shouldn't be deleted at the end of redisplay. */
16209 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16210 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16211 }
16212
16213 /* Restore current_buffer and value of point in it. The window
16214 update may have changed the buffer, so first make sure `opoint'
16215 is still valid (Bug#6177). */
16216 if (CHARPOS (opoint) < BEGV)
16217 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16218 else if (CHARPOS (opoint) > ZV)
16219 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16220 else
16221 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16222
16223 set_buffer_internal_1 (old);
16224 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16225 shorter. This can be caused by log truncation in *Messages*. */
16226 if (CHARPOS (lpoint) <= ZV)
16227 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16228
16229 unbind_to (count, Qnil);
16230 }
16231
16232
16233 /* Build the complete desired matrix of WINDOW with a window start
16234 buffer position POS.
16235
16236 Value is 1 if successful. It is zero if fonts were loaded during
16237 redisplay which makes re-adjusting glyph matrices necessary, and -1
16238 if point would appear in the scroll margins.
16239 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16240 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16241 set in FLAGS.) */
16242
16243 int
16244 try_window (Lisp_Object window, struct text_pos pos, int flags)
16245 {
16246 struct window *w = XWINDOW (window);
16247 struct it it;
16248 struct glyph_row *last_text_row = NULL;
16249 struct frame *f = XFRAME (w->frame);
16250 int frame_line_height = default_line_pixel_height (w);
16251
16252 /* Make POS the new window start. */
16253 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16254
16255 /* Mark cursor position as unknown. No overlay arrow seen. */
16256 w->cursor.vpos = -1;
16257 overlay_arrow_seen = 0;
16258
16259 /* Initialize iterator and info to start at POS. */
16260 start_display (&it, w, pos);
16261
16262 /* Display all lines of W. */
16263 while (it.current_y < it.last_visible_y)
16264 {
16265 if (display_line (&it))
16266 last_text_row = it.glyph_row - 1;
16267 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16268 return 0;
16269 }
16270
16271 /* Don't let the cursor end in the scroll margins. */
16272 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16273 && !MINI_WINDOW_P (w))
16274 {
16275 int this_scroll_margin;
16276 int window_total_lines
16277 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16278
16279 if (scroll_margin > 0)
16280 {
16281 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16282 this_scroll_margin *= frame_line_height;
16283 }
16284 else
16285 this_scroll_margin = 0;
16286
16287 if ((w->cursor.y >= 0 /* not vscrolled */
16288 && w->cursor.y < this_scroll_margin
16289 && CHARPOS (pos) > BEGV
16290 && IT_CHARPOS (it) < ZV)
16291 /* rms: considering make_cursor_line_fully_visible_p here
16292 seems to give wrong results. We don't want to recenter
16293 when the last line is partly visible, we want to allow
16294 that case to be handled in the usual way. */
16295 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16296 {
16297 w->cursor.vpos = -1;
16298 clear_glyph_matrix (w->desired_matrix);
16299 return -1;
16300 }
16301 }
16302
16303 /* If bottom moved off end of frame, change mode line percentage. */
16304 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16305 w->update_mode_line = 1;
16306
16307 /* Set window_end_pos to the offset of the last character displayed
16308 on the window from the end of current_buffer. Set
16309 window_end_vpos to its row number. */
16310 if (last_text_row)
16311 {
16312 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16313 adjust_window_ends (w, last_text_row, 0);
16314 eassert
16315 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16316 w->window_end_vpos)));
16317 }
16318 else
16319 {
16320 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16321 w->window_end_pos = Z - ZV;
16322 w->window_end_vpos = 0;
16323 }
16324
16325 /* But that is not valid info until redisplay finishes. */
16326 w->window_end_valid = 0;
16327 return 1;
16328 }
16329
16330
16331 \f
16332 /************************************************************************
16333 Window redisplay reusing current matrix when buffer has not changed
16334 ************************************************************************/
16335
16336 /* Try redisplay of window W showing an unchanged buffer with a
16337 different window start than the last time it was displayed by
16338 reusing its current matrix. Value is non-zero if successful.
16339 W->start is the new window start. */
16340
16341 static int
16342 try_window_reusing_current_matrix (struct window *w)
16343 {
16344 struct frame *f = XFRAME (w->frame);
16345 struct glyph_row *bottom_row;
16346 struct it it;
16347 struct run run;
16348 struct text_pos start, new_start;
16349 int nrows_scrolled, i;
16350 struct glyph_row *last_text_row;
16351 struct glyph_row *last_reused_text_row;
16352 struct glyph_row *start_row;
16353 int start_vpos, min_y, max_y;
16354
16355 #ifdef GLYPH_DEBUG
16356 if (inhibit_try_window_reusing)
16357 return 0;
16358 #endif
16359
16360 if (/* This function doesn't handle terminal frames. */
16361 !FRAME_WINDOW_P (f)
16362 /* Don't try to reuse the display if windows have been split
16363 or such. */
16364 || windows_or_buffers_changed
16365 || f->cursor_type_changed)
16366 return 0;
16367
16368 /* Can't do this if region may have changed. */
16369 if (markpos_of_region () >= 0
16370 || w->region_showing
16371 || !NILP (Vshow_trailing_whitespace))
16372 return 0;
16373
16374 /* If top-line visibility has changed, give up. */
16375 if (WINDOW_WANTS_HEADER_LINE_P (w)
16376 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16377 return 0;
16378
16379 /* Give up if old or new display is scrolled vertically. We could
16380 make this function handle this, but right now it doesn't. */
16381 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16382 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16383 return 0;
16384
16385 /* The variable new_start now holds the new window start. The old
16386 start `start' can be determined from the current matrix. */
16387 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16388 start = start_row->minpos;
16389 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16390
16391 /* Clear the desired matrix for the display below. */
16392 clear_glyph_matrix (w->desired_matrix);
16393
16394 if (CHARPOS (new_start) <= CHARPOS (start))
16395 {
16396 /* Don't use this method if the display starts with an ellipsis
16397 displayed for invisible text. It's not easy to handle that case
16398 below, and it's certainly not worth the effort since this is
16399 not a frequent case. */
16400 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16401 return 0;
16402
16403 IF_DEBUG (debug_method_add (w, "twu1"));
16404
16405 /* Display up to a row that can be reused. The variable
16406 last_text_row is set to the last row displayed that displays
16407 text. Note that it.vpos == 0 if or if not there is a
16408 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16409 start_display (&it, w, new_start);
16410 w->cursor.vpos = -1;
16411 last_text_row = last_reused_text_row = NULL;
16412
16413 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16414 {
16415 /* If we have reached into the characters in the START row,
16416 that means the line boundaries have changed. So we
16417 can't start copying with the row START. Maybe it will
16418 work to start copying with the following row. */
16419 while (IT_CHARPOS (it) > CHARPOS (start))
16420 {
16421 /* Advance to the next row as the "start". */
16422 start_row++;
16423 start = start_row->minpos;
16424 /* If there are no more rows to try, or just one, give up. */
16425 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16426 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16427 || CHARPOS (start) == ZV)
16428 {
16429 clear_glyph_matrix (w->desired_matrix);
16430 return 0;
16431 }
16432
16433 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16434 }
16435 /* If we have reached alignment, we can copy the rest of the
16436 rows. */
16437 if (IT_CHARPOS (it) == CHARPOS (start)
16438 /* Don't accept "alignment" inside a display vector,
16439 since start_row could have started in the middle of
16440 that same display vector (thus their character
16441 positions match), and we have no way of telling if
16442 that is the case. */
16443 && it.current.dpvec_index < 0)
16444 break;
16445
16446 if (display_line (&it))
16447 last_text_row = it.glyph_row - 1;
16448
16449 }
16450
16451 /* A value of current_y < last_visible_y means that we stopped
16452 at the previous window start, which in turn means that we
16453 have at least one reusable row. */
16454 if (it.current_y < it.last_visible_y)
16455 {
16456 struct glyph_row *row;
16457
16458 /* IT.vpos always starts from 0; it counts text lines. */
16459 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16460
16461 /* Find PT if not already found in the lines displayed. */
16462 if (w->cursor.vpos < 0)
16463 {
16464 int dy = it.current_y - start_row->y;
16465
16466 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16467 row = row_containing_pos (w, PT, row, NULL, dy);
16468 if (row)
16469 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16470 dy, nrows_scrolled);
16471 else
16472 {
16473 clear_glyph_matrix (w->desired_matrix);
16474 return 0;
16475 }
16476 }
16477
16478 /* Scroll the display. Do it before the current matrix is
16479 changed. The problem here is that update has not yet
16480 run, i.e. part of the current matrix is not up to date.
16481 scroll_run_hook will clear the cursor, and use the
16482 current matrix to get the height of the row the cursor is
16483 in. */
16484 run.current_y = start_row->y;
16485 run.desired_y = it.current_y;
16486 run.height = it.last_visible_y - it.current_y;
16487
16488 if (run.height > 0 && run.current_y != run.desired_y)
16489 {
16490 update_begin (f);
16491 FRAME_RIF (f)->update_window_begin_hook (w);
16492 FRAME_RIF (f)->clear_window_mouse_face (w);
16493 FRAME_RIF (f)->scroll_run_hook (w, &run);
16494 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16495 update_end (f);
16496 }
16497
16498 /* Shift current matrix down by nrows_scrolled lines. */
16499 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16500 rotate_matrix (w->current_matrix,
16501 start_vpos,
16502 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16503 nrows_scrolled);
16504
16505 /* Disable lines that must be updated. */
16506 for (i = 0; i < nrows_scrolled; ++i)
16507 (start_row + i)->enabled_p = 0;
16508
16509 /* Re-compute Y positions. */
16510 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16511 max_y = it.last_visible_y;
16512 for (row = start_row + nrows_scrolled;
16513 row < bottom_row;
16514 ++row)
16515 {
16516 row->y = it.current_y;
16517 row->visible_height = row->height;
16518
16519 if (row->y < min_y)
16520 row->visible_height -= min_y - row->y;
16521 if (row->y + row->height > max_y)
16522 row->visible_height -= row->y + row->height - max_y;
16523 if (row->fringe_bitmap_periodic_p)
16524 row->redraw_fringe_bitmaps_p = 1;
16525
16526 it.current_y += row->height;
16527
16528 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16529 last_reused_text_row = row;
16530 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16531 break;
16532 }
16533
16534 /* Disable lines in the current matrix which are now
16535 below the window. */
16536 for (++row; row < bottom_row; ++row)
16537 row->enabled_p = row->mode_line_p = 0;
16538 }
16539
16540 /* Update window_end_pos etc.; last_reused_text_row is the last
16541 reused row from the current matrix containing text, if any.
16542 The value of last_text_row is the last displayed line
16543 containing text. */
16544 if (last_reused_text_row)
16545 adjust_window_ends (w, last_reused_text_row, 1);
16546 else if (last_text_row)
16547 adjust_window_ends (w, last_text_row, 0);
16548 else
16549 {
16550 /* This window must be completely empty. */
16551 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16552 w->window_end_pos = Z - ZV;
16553 w->window_end_vpos = 0;
16554 }
16555 w->window_end_valid = 0;
16556
16557 /* Update hint: don't try scrolling again in update_window. */
16558 w->desired_matrix->no_scrolling_p = 1;
16559
16560 #ifdef GLYPH_DEBUG
16561 debug_method_add (w, "try_window_reusing_current_matrix 1");
16562 #endif
16563 return 1;
16564 }
16565 else if (CHARPOS (new_start) > CHARPOS (start))
16566 {
16567 struct glyph_row *pt_row, *row;
16568 struct glyph_row *first_reusable_row;
16569 struct glyph_row *first_row_to_display;
16570 int dy;
16571 int yb = window_text_bottom_y (w);
16572
16573 /* Find the row starting at new_start, if there is one. Don't
16574 reuse a partially visible line at the end. */
16575 first_reusable_row = start_row;
16576 while (first_reusable_row->enabled_p
16577 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16578 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16579 < CHARPOS (new_start)))
16580 ++first_reusable_row;
16581
16582 /* Give up if there is no row to reuse. */
16583 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16584 || !first_reusable_row->enabled_p
16585 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16586 != CHARPOS (new_start)))
16587 return 0;
16588
16589 /* We can reuse fully visible rows beginning with
16590 first_reusable_row to the end of the window. Set
16591 first_row_to_display to the first row that cannot be reused.
16592 Set pt_row to the row containing point, if there is any. */
16593 pt_row = NULL;
16594 for (first_row_to_display = first_reusable_row;
16595 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16596 ++first_row_to_display)
16597 {
16598 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16599 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16600 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16601 && first_row_to_display->ends_at_zv_p
16602 && pt_row == NULL)))
16603 pt_row = first_row_to_display;
16604 }
16605
16606 /* Start displaying at the start of first_row_to_display. */
16607 eassert (first_row_to_display->y < yb);
16608 init_to_row_start (&it, w, first_row_to_display);
16609
16610 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16611 - start_vpos);
16612 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16613 - nrows_scrolled);
16614 it.current_y = (first_row_to_display->y - first_reusable_row->y
16615 + WINDOW_HEADER_LINE_HEIGHT (w));
16616
16617 /* Display lines beginning with first_row_to_display in the
16618 desired matrix. Set last_text_row to the last row displayed
16619 that displays text. */
16620 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16621 if (pt_row == NULL)
16622 w->cursor.vpos = -1;
16623 last_text_row = NULL;
16624 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16625 if (display_line (&it))
16626 last_text_row = it.glyph_row - 1;
16627
16628 /* If point is in a reused row, adjust y and vpos of the cursor
16629 position. */
16630 if (pt_row)
16631 {
16632 w->cursor.vpos -= nrows_scrolled;
16633 w->cursor.y -= first_reusable_row->y - start_row->y;
16634 }
16635
16636 /* Give up if point isn't in a row displayed or reused. (This
16637 also handles the case where w->cursor.vpos < nrows_scrolled
16638 after the calls to display_line, which can happen with scroll
16639 margins. See bug#1295.) */
16640 if (w->cursor.vpos < 0)
16641 {
16642 clear_glyph_matrix (w->desired_matrix);
16643 return 0;
16644 }
16645
16646 /* Scroll the display. */
16647 run.current_y = first_reusable_row->y;
16648 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16649 run.height = it.last_visible_y - run.current_y;
16650 dy = run.current_y - run.desired_y;
16651
16652 if (run.height)
16653 {
16654 update_begin (f);
16655 FRAME_RIF (f)->update_window_begin_hook (w);
16656 FRAME_RIF (f)->clear_window_mouse_face (w);
16657 FRAME_RIF (f)->scroll_run_hook (w, &run);
16658 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16659 update_end (f);
16660 }
16661
16662 /* Adjust Y positions of reused rows. */
16663 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16664 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16665 max_y = it.last_visible_y;
16666 for (row = first_reusable_row; row < first_row_to_display; ++row)
16667 {
16668 row->y -= dy;
16669 row->visible_height = row->height;
16670 if (row->y < min_y)
16671 row->visible_height -= min_y - row->y;
16672 if (row->y + row->height > max_y)
16673 row->visible_height -= row->y + row->height - max_y;
16674 if (row->fringe_bitmap_periodic_p)
16675 row->redraw_fringe_bitmaps_p = 1;
16676 }
16677
16678 /* Scroll the current matrix. */
16679 eassert (nrows_scrolled > 0);
16680 rotate_matrix (w->current_matrix,
16681 start_vpos,
16682 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16683 -nrows_scrolled);
16684
16685 /* Disable rows not reused. */
16686 for (row -= nrows_scrolled; row < bottom_row; ++row)
16687 row->enabled_p = 0;
16688
16689 /* Point may have moved to a different line, so we cannot assume that
16690 the previous cursor position is valid; locate the correct row. */
16691 if (pt_row)
16692 {
16693 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16694 row < bottom_row
16695 && PT >= MATRIX_ROW_END_CHARPOS (row)
16696 && !row->ends_at_zv_p;
16697 row++)
16698 {
16699 w->cursor.vpos++;
16700 w->cursor.y = row->y;
16701 }
16702 if (row < bottom_row)
16703 {
16704 /* Can't simply scan the row for point with
16705 bidi-reordered glyph rows. Let set_cursor_from_row
16706 figure out where to put the cursor, and if it fails,
16707 give up. */
16708 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16709 {
16710 if (!set_cursor_from_row (w, row, w->current_matrix,
16711 0, 0, 0, 0))
16712 {
16713 clear_glyph_matrix (w->desired_matrix);
16714 return 0;
16715 }
16716 }
16717 else
16718 {
16719 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16720 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16721
16722 for (; glyph < end
16723 && (!BUFFERP (glyph->object)
16724 || glyph->charpos < PT);
16725 glyph++)
16726 {
16727 w->cursor.hpos++;
16728 w->cursor.x += glyph->pixel_width;
16729 }
16730 }
16731 }
16732 }
16733
16734 /* Adjust window end. A null value of last_text_row means that
16735 the window end is in reused rows which in turn means that
16736 only its vpos can have changed. */
16737 if (last_text_row)
16738 adjust_window_ends (w, last_text_row, 0);
16739 else
16740 w->window_end_vpos -= nrows_scrolled;
16741
16742 w->window_end_valid = 0;
16743 w->desired_matrix->no_scrolling_p = 1;
16744
16745 #ifdef GLYPH_DEBUG
16746 debug_method_add (w, "try_window_reusing_current_matrix 2");
16747 #endif
16748 return 1;
16749 }
16750
16751 return 0;
16752 }
16753
16754
16755 \f
16756 /************************************************************************
16757 Window redisplay reusing current matrix when buffer has changed
16758 ************************************************************************/
16759
16760 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16761 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16762 ptrdiff_t *, ptrdiff_t *);
16763 static struct glyph_row *
16764 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16765 struct glyph_row *);
16766
16767
16768 /* Return the last row in MATRIX displaying text. If row START is
16769 non-null, start searching with that row. IT gives the dimensions
16770 of the display. Value is null if matrix is empty; otherwise it is
16771 a pointer to the row found. */
16772
16773 static struct glyph_row *
16774 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16775 struct glyph_row *start)
16776 {
16777 struct glyph_row *row, *row_found;
16778
16779 /* Set row_found to the last row in IT->w's current matrix
16780 displaying text. The loop looks funny but think of partially
16781 visible lines. */
16782 row_found = NULL;
16783 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16784 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16785 {
16786 eassert (row->enabled_p);
16787 row_found = row;
16788 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16789 break;
16790 ++row;
16791 }
16792
16793 return row_found;
16794 }
16795
16796
16797 /* Return the last row in the current matrix of W that is not affected
16798 by changes at the start of current_buffer that occurred since W's
16799 current matrix was built. Value is null if no such row exists.
16800
16801 BEG_UNCHANGED us the number of characters unchanged at the start of
16802 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16803 first changed character in current_buffer. Characters at positions <
16804 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16805 when the current matrix was built. */
16806
16807 static struct glyph_row *
16808 find_last_unchanged_at_beg_row (struct window *w)
16809 {
16810 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16811 struct glyph_row *row;
16812 struct glyph_row *row_found = NULL;
16813 int yb = window_text_bottom_y (w);
16814
16815 /* Find the last row displaying unchanged text. */
16816 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16817 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16818 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16819 ++row)
16820 {
16821 if (/* If row ends before first_changed_pos, it is unchanged,
16822 except in some case. */
16823 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16824 /* When row ends in ZV and we write at ZV it is not
16825 unchanged. */
16826 && !row->ends_at_zv_p
16827 /* When first_changed_pos is the end of a continued line,
16828 row is not unchanged because it may be no longer
16829 continued. */
16830 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16831 && (row->continued_p
16832 || row->exact_window_width_line_p))
16833 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16834 needs to be recomputed, so don't consider this row as
16835 unchanged. This happens when the last line was
16836 bidi-reordered and was killed immediately before this
16837 redisplay cycle. In that case, ROW->end stores the
16838 buffer position of the first visual-order character of
16839 the killed text, which is now beyond ZV. */
16840 && CHARPOS (row->end.pos) <= ZV)
16841 row_found = row;
16842
16843 /* Stop if last visible row. */
16844 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16845 break;
16846 }
16847
16848 return row_found;
16849 }
16850
16851
16852 /* Find the first glyph row in the current matrix of W that is not
16853 affected by changes at the end of current_buffer since the
16854 time W's current matrix was built.
16855
16856 Return in *DELTA the number of chars by which buffer positions in
16857 unchanged text at the end of current_buffer must be adjusted.
16858
16859 Return in *DELTA_BYTES the corresponding number of bytes.
16860
16861 Value is null if no such row exists, i.e. all rows are affected by
16862 changes. */
16863
16864 static struct glyph_row *
16865 find_first_unchanged_at_end_row (struct window *w,
16866 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16867 {
16868 struct glyph_row *row;
16869 struct glyph_row *row_found = NULL;
16870
16871 *delta = *delta_bytes = 0;
16872
16873 /* Display must not have been paused, otherwise the current matrix
16874 is not up to date. */
16875 eassert (w->window_end_valid);
16876
16877 /* A value of window_end_pos >= END_UNCHANGED means that the window
16878 end is in the range of changed text. If so, there is no
16879 unchanged row at the end of W's current matrix. */
16880 if (w->window_end_pos >= END_UNCHANGED)
16881 return NULL;
16882
16883 /* Set row to the last row in W's current matrix displaying text. */
16884 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
16885
16886 /* If matrix is entirely empty, no unchanged row exists. */
16887 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16888 {
16889 /* The value of row is the last glyph row in the matrix having a
16890 meaningful buffer position in it. The end position of row
16891 corresponds to window_end_pos. This allows us to translate
16892 buffer positions in the current matrix to current buffer
16893 positions for characters not in changed text. */
16894 ptrdiff_t Z_old =
16895 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
16896 ptrdiff_t Z_BYTE_old =
16897 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16898 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16899 struct glyph_row *first_text_row
16900 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16901
16902 *delta = Z - Z_old;
16903 *delta_bytes = Z_BYTE - Z_BYTE_old;
16904
16905 /* Set last_unchanged_pos to the buffer position of the last
16906 character in the buffer that has not been changed. Z is the
16907 index + 1 of the last character in current_buffer, i.e. by
16908 subtracting END_UNCHANGED we get the index of the last
16909 unchanged character, and we have to add BEG to get its buffer
16910 position. */
16911 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16912 last_unchanged_pos_old = last_unchanged_pos - *delta;
16913
16914 /* Search backward from ROW for a row displaying a line that
16915 starts at a minimum position >= last_unchanged_pos_old. */
16916 for (; row > first_text_row; --row)
16917 {
16918 /* This used to abort, but it can happen.
16919 It is ok to just stop the search instead here. KFS. */
16920 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16921 break;
16922
16923 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16924 row_found = row;
16925 }
16926 }
16927
16928 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16929
16930 return row_found;
16931 }
16932
16933
16934 /* Make sure that glyph rows in the current matrix of window W
16935 reference the same glyph memory as corresponding rows in the
16936 frame's frame matrix. This function is called after scrolling W's
16937 current matrix on a terminal frame in try_window_id and
16938 try_window_reusing_current_matrix. */
16939
16940 static void
16941 sync_frame_with_window_matrix_rows (struct window *w)
16942 {
16943 struct frame *f = XFRAME (w->frame);
16944 struct glyph_row *window_row, *window_row_end, *frame_row;
16945
16946 /* Preconditions: W must be a leaf window and full-width. Its frame
16947 must have a frame matrix. */
16948 eassert (BUFFERP (w->contents));
16949 eassert (WINDOW_FULL_WIDTH_P (w));
16950 eassert (!FRAME_WINDOW_P (f));
16951
16952 /* If W is a full-width window, glyph pointers in W's current matrix
16953 have, by definition, to be the same as glyph pointers in the
16954 corresponding frame matrix. Note that frame matrices have no
16955 marginal areas (see build_frame_matrix). */
16956 window_row = w->current_matrix->rows;
16957 window_row_end = window_row + w->current_matrix->nrows;
16958 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16959 while (window_row < window_row_end)
16960 {
16961 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16962 struct glyph *end = window_row->glyphs[LAST_AREA];
16963
16964 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16965 frame_row->glyphs[TEXT_AREA] = start;
16966 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16967 frame_row->glyphs[LAST_AREA] = end;
16968
16969 /* Disable frame rows whose corresponding window rows have
16970 been disabled in try_window_id. */
16971 if (!window_row->enabled_p)
16972 frame_row->enabled_p = 0;
16973
16974 ++window_row, ++frame_row;
16975 }
16976 }
16977
16978
16979 /* Find the glyph row in window W containing CHARPOS. Consider all
16980 rows between START and END (not inclusive). END null means search
16981 all rows to the end of the display area of W. Value is the row
16982 containing CHARPOS or null. */
16983
16984 struct glyph_row *
16985 row_containing_pos (struct window *w, ptrdiff_t charpos,
16986 struct glyph_row *start, struct glyph_row *end, int dy)
16987 {
16988 struct glyph_row *row = start;
16989 struct glyph_row *best_row = NULL;
16990 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16991 int last_y;
16992
16993 /* If we happen to start on a header-line, skip that. */
16994 if (row->mode_line_p)
16995 ++row;
16996
16997 if ((end && row >= end) || !row->enabled_p)
16998 return NULL;
16999
17000 last_y = window_text_bottom_y (w) - dy;
17001
17002 while (1)
17003 {
17004 /* Give up if we have gone too far. */
17005 if (end && row >= end)
17006 return NULL;
17007 /* This formerly returned if they were equal.
17008 I think that both quantities are of a "last plus one" type;
17009 if so, when they are equal, the row is within the screen. -- rms. */
17010 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17011 return NULL;
17012
17013 /* If it is in this row, return this row. */
17014 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17015 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17016 /* The end position of a row equals the start
17017 position of the next row. If CHARPOS is there, we
17018 would rather consider it displayed in the next
17019 line, except when this line ends in ZV. */
17020 && !row_for_charpos_p (row, charpos)))
17021 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17022 {
17023 struct glyph *g;
17024
17025 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17026 || (!best_row && !row->continued_p))
17027 return row;
17028 /* In bidi-reordered rows, there could be several rows whose
17029 edges surround CHARPOS, all of these rows belonging to
17030 the same continued line. We need to find the row which
17031 fits CHARPOS the best. */
17032 for (g = row->glyphs[TEXT_AREA];
17033 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17034 g++)
17035 {
17036 if (!STRINGP (g->object))
17037 {
17038 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17039 {
17040 mindif = eabs (g->charpos - charpos);
17041 best_row = row;
17042 /* Exact match always wins. */
17043 if (mindif == 0)
17044 return best_row;
17045 }
17046 }
17047 }
17048 }
17049 else if (best_row && !row->continued_p)
17050 return best_row;
17051 ++row;
17052 }
17053 }
17054
17055
17056 /* Try to redisplay window W by reusing its existing display. W's
17057 current matrix must be up to date when this function is called,
17058 i.e. window_end_valid must be nonzero.
17059
17060 Value is
17061
17062 1 if display has been updated
17063 0 if otherwise unsuccessful
17064 -1 if redisplay with same window start is known not to succeed
17065
17066 The following steps are performed:
17067
17068 1. Find the last row in the current matrix of W that is not
17069 affected by changes at the start of current_buffer. If no such row
17070 is found, give up.
17071
17072 2. Find the first row in W's current matrix that is not affected by
17073 changes at the end of current_buffer. Maybe there is no such row.
17074
17075 3. Display lines beginning with the row + 1 found in step 1 to the
17076 row found in step 2 or, if step 2 didn't find a row, to the end of
17077 the window.
17078
17079 4. If cursor is not known to appear on the window, give up.
17080
17081 5. If display stopped at the row found in step 2, scroll the
17082 display and current matrix as needed.
17083
17084 6. Maybe display some lines at the end of W, if we must. This can
17085 happen under various circumstances, like a partially visible line
17086 becoming fully visible, or because newly displayed lines are displayed
17087 in smaller font sizes.
17088
17089 7. Update W's window end information. */
17090
17091 static int
17092 try_window_id (struct window *w)
17093 {
17094 struct frame *f = XFRAME (w->frame);
17095 struct glyph_matrix *current_matrix = w->current_matrix;
17096 struct glyph_matrix *desired_matrix = w->desired_matrix;
17097 struct glyph_row *last_unchanged_at_beg_row;
17098 struct glyph_row *first_unchanged_at_end_row;
17099 struct glyph_row *row;
17100 struct glyph_row *bottom_row;
17101 int bottom_vpos;
17102 struct it it;
17103 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17104 int dvpos, dy;
17105 struct text_pos start_pos;
17106 struct run run;
17107 int first_unchanged_at_end_vpos = 0;
17108 struct glyph_row *last_text_row, *last_text_row_at_end;
17109 struct text_pos start;
17110 ptrdiff_t first_changed_charpos, last_changed_charpos;
17111
17112 #ifdef GLYPH_DEBUG
17113 if (inhibit_try_window_id)
17114 return 0;
17115 #endif
17116
17117 /* This is handy for debugging. */
17118 #if 0
17119 #define GIVE_UP(X) \
17120 do { \
17121 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17122 return 0; \
17123 } while (0)
17124 #else
17125 #define GIVE_UP(X) return 0
17126 #endif
17127
17128 SET_TEXT_POS_FROM_MARKER (start, w->start);
17129
17130 /* Don't use this for mini-windows because these can show
17131 messages and mini-buffers, and we don't handle that here. */
17132 if (MINI_WINDOW_P (w))
17133 GIVE_UP (1);
17134
17135 /* This flag is used to prevent redisplay optimizations. */
17136 if (windows_or_buffers_changed || f->cursor_type_changed)
17137 GIVE_UP (2);
17138
17139 /* Verify that narrowing has not changed.
17140 Also verify that we were not told to prevent redisplay optimizations.
17141 It would be nice to further
17142 reduce the number of cases where this prevents try_window_id. */
17143 if (current_buffer->clip_changed
17144 || current_buffer->prevent_redisplay_optimizations_p)
17145 GIVE_UP (3);
17146
17147 /* Window must either use window-based redisplay or be full width. */
17148 if (!FRAME_WINDOW_P (f)
17149 && (!FRAME_LINE_INS_DEL_OK (f)
17150 || !WINDOW_FULL_WIDTH_P (w)))
17151 GIVE_UP (4);
17152
17153 /* Give up if point is known NOT to appear in W. */
17154 if (PT < CHARPOS (start))
17155 GIVE_UP (5);
17156
17157 /* Another way to prevent redisplay optimizations. */
17158 if (w->last_modified == 0)
17159 GIVE_UP (6);
17160
17161 /* Verify that window is not hscrolled. */
17162 if (w->hscroll != 0)
17163 GIVE_UP (7);
17164
17165 /* Verify that display wasn't paused. */
17166 if (!w->window_end_valid)
17167 GIVE_UP (8);
17168
17169 /* Can't use this if highlighting a region because a cursor movement
17170 will do more than just set the cursor. */
17171 if (markpos_of_region () >= 0)
17172 GIVE_UP (9);
17173
17174 /* Likewise if highlighting trailing whitespace. */
17175 if (!NILP (Vshow_trailing_whitespace))
17176 GIVE_UP (11);
17177
17178 /* Likewise if showing a region. */
17179 if (w->region_showing)
17180 GIVE_UP (10);
17181
17182 /* Can't use this if overlay arrow position and/or string have
17183 changed. */
17184 if (overlay_arrows_changed_p ())
17185 GIVE_UP (12);
17186
17187 /* When word-wrap is on, adding a space to the first word of a
17188 wrapped line can change the wrap position, altering the line
17189 above it. It might be worthwhile to handle this more
17190 intelligently, but for now just redisplay from scratch. */
17191 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17192 GIVE_UP (21);
17193
17194 /* Under bidi reordering, adding or deleting a character in the
17195 beginning of a paragraph, before the first strong directional
17196 character, can change the base direction of the paragraph (unless
17197 the buffer specifies a fixed paragraph direction), which will
17198 require to redisplay the whole paragraph. It might be worthwhile
17199 to find the paragraph limits and widen the range of redisplayed
17200 lines to that, but for now just give up this optimization and
17201 redisplay from scratch. */
17202 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17203 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17204 GIVE_UP (22);
17205
17206 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17207 only if buffer has really changed. The reason is that the gap is
17208 initially at Z for freshly visited files. The code below would
17209 set end_unchanged to 0 in that case. */
17210 if (MODIFF > SAVE_MODIFF
17211 /* This seems to happen sometimes after saving a buffer. */
17212 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17213 {
17214 if (GPT - BEG < BEG_UNCHANGED)
17215 BEG_UNCHANGED = GPT - BEG;
17216 if (Z - GPT < END_UNCHANGED)
17217 END_UNCHANGED = Z - GPT;
17218 }
17219
17220 /* The position of the first and last character that has been changed. */
17221 first_changed_charpos = BEG + BEG_UNCHANGED;
17222 last_changed_charpos = Z - END_UNCHANGED;
17223
17224 /* If window starts after a line end, and the last change is in
17225 front of that newline, then changes don't affect the display.
17226 This case happens with stealth-fontification. Note that although
17227 the display is unchanged, glyph positions in the matrix have to
17228 be adjusted, of course. */
17229 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17230 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17231 && ((last_changed_charpos < CHARPOS (start)
17232 && CHARPOS (start) == BEGV)
17233 || (last_changed_charpos < CHARPOS (start) - 1
17234 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17235 {
17236 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17237 struct glyph_row *r0;
17238
17239 /* Compute how many chars/bytes have been added to or removed
17240 from the buffer. */
17241 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17242 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17243 Z_delta = Z - Z_old;
17244 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17245
17246 /* Give up if PT is not in the window. Note that it already has
17247 been checked at the start of try_window_id that PT is not in
17248 front of the window start. */
17249 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17250 GIVE_UP (13);
17251
17252 /* If window start is unchanged, we can reuse the whole matrix
17253 as is, after adjusting glyph positions. No need to compute
17254 the window end again, since its offset from Z hasn't changed. */
17255 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17256 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17257 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17258 /* PT must not be in a partially visible line. */
17259 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17260 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17261 {
17262 /* Adjust positions in the glyph matrix. */
17263 if (Z_delta || Z_delta_bytes)
17264 {
17265 struct glyph_row *r1
17266 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17267 increment_matrix_positions (w->current_matrix,
17268 MATRIX_ROW_VPOS (r0, current_matrix),
17269 MATRIX_ROW_VPOS (r1, current_matrix),
17270 Z_delta, Z_delta_bytes);
17271 }
17272
17273 /* Set the cursor. */
17274 row = row_containing_pos (w, PT, r0, NULL, 0);
17275 if (row)
17276 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17277 else
17278 emacs_abort ();
17279 return 1;
17280 }
17281 }
17282
17283 /* Handle the case that changes are all below what is displayed in
17284 the window, and that PT is in the window. This shortcut cannot
17285 be taken if ZV is visible in the window, and text has been added
17286 there that is visible in the window. */
17287 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17288 /* ZV is not visible in the window, or there are no
17289 changes at ZV, actually. */
17290 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17291 || first_changed_charpos == last_changed_charpos))
17292 {
17293 struct glyph_row *r0;
17294
17295 /* Give up if PT is not in the window. Note that it already has
17296 been checked at the start of try_window_id that PT is not in
17297 front of the window start. */
17298 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17299 GIVE_UP (14);
17300
17301 /* If window start is unchanged, we can reuse the whole matrix
17302 as is, without changing glyph positions since no text has
17303 been added/removed in front of the window end. */
17304 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17305 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17306 /* PT must not be in a partially visible line. */
17307 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17308 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17309 {
17310 /* We have to compute the window end anew since text
17311 could have been added/removed after it. */
17312 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17313 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17314
17315 /* Set the cursor. */
17316 row = row_containing_pos (w, PT, r0, NULL, 0);
17317 if (row)
17318 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17319 else
17320 emacs_abort ();
17321 return 2;
17322 }
17323 }
17324
17325 /* Give up if window start is in the changed area.
17326
17327 The condition used to read
17328
17329 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17330
17331 but why that was tested escapes me at the moment. */
17332 if (CHARPOS (start) >= first_changed_charpos
17333 && CHARPOS (start) <= last_changed_charpos)
17334 GIVE_UP (15);
17335
17336 /* Check that window start agrees with the start of the first glyph
17337 row in its current matrix. Check this after we know the window
17338 start is not in changed text, otherwise positions would not be
17339 comparable. */
17340 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17341 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17342 GIVE_UP (16);
17343
17344 /* Give up if the window ends in strings. Overlay strings
17345 at the end are difficult to handle, so don't try. */
17346 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17347 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17348 GIVE_UP (20);
17349
17350 /* Compute the position at which we have to start displaying new
17351 lines. Some of the lines at the top of the window might be
17352 reusable because they are not displaying changed text. Find the
17353 last row in W's current matrix not affected by changes at the
17354 start of current_buffer. Value is null if changes start in the
17355 first line of window. */
17356 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17357 if (last_unchanged_at_beg_row)
17358 {
17359 /* Avoid starting to display in the middle of a character, a TAB
17360 for instance. This is easier than to set up the iterator
17361 exactly, and it's not a frequent case, so the additional
17362 effort wouldn't really pay off. */
17363 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17364 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17365 && last_unchanged_at_beg_row > w->current_matrix->rows)
17366 --last_unchanged_at_beg_row;
17367
17368 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17369 GIVE_UP (17);
17370
17371 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17372 GIVE_UP (18);
17373 start_pos = it.current.pos;
17374
17375 /* Start displaying new lines in the desired matrix at the same
17376 vpos we would use in the current matrix, i.e. below
17377 last_unchanged_at_beg_row. */
17378 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17379 current_matrix);
17380 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17381 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17382
17383 eassert (it.hpos == 0 && it.current_x == 0);
17384 }
17385 else
17386 {
17387 /* There are no reusable lines at the start of the window.
17388 Start displaying in the first text line. */
17389 start_display (&it, w, start);
17390 it.vpos = it.first_vpos;
17391 start_pos = it.current.pos;
17392 }
17393
17394 /* Find the first row that is not affected by changes at the end of
17395 the buffer. Value will be null if there is no unchanged row, in
17396 which case we must redisplay to the end of the window. delta
17397 will be set to the value by which buffer positions beginning with
17398 first_unchanged_at_end_row have to be adjusted due to text
17399 changes. */
17400 first_unchanged_at_end_row
17401 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17402 IF_DEBUG (debug_delta = delta);
17403 IF_DEBUG (debug_delta_bytes = delta_bytes);
17404
17405 /* Set stop_pos to the buffer position up to which we will have to
17406 display new lines. If first_unchanged_at_end_row != NULL, this
17407 is the buffer position of the start of the line displayed in that
17408 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17409 that we don't stop at a buffer position. */
17410 stop_pos = 0;
17411 if (first_unchanged_at_end_row)
17412 {
17413 eassert (last_unchanged_at_beg_row == NULL
17414 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17415
17416 /* If this is a continuation line, move forward to the next one
17417 that isn't. Changes in lines above affect this line.
17418 Caution: this may move first_unchanged_at_end_row to a row
17419 not displaying text. */
17420 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17421 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17422 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17423 < it.last_visible_y))
17424 ++first_unchanged_at_end_row;
17425
17426 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17427 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17428 >= it.last_visible_y))
17429 first_unchanged_at_end_row = NULL;
17430 else
17431 {
17432 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17433 + delta);
17434 first_unchanged_at_end_vpos
17435 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17436 eassert (stop_pos >= Z - END_UNCHANGED);
17437 }
17438 }
17439 else if (last_unchanged_at_beg_row == NULL)
17440 GIVE_UP (19);
17441
17442
17443 #ifdef GLYPH_DEBUG
17444
17445 /* Either there is no unchanged row at the end, or the one we have
17446 now displays text. This is a necessary condition for the window
17447 end pos calculation at the end of this function. */
17448 eassert (first_unchanged_at_end_row == NULL
17449 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17450
17451 debug_last_unchanged_at_beg_vpos
17452 = (last_unchanged_at_beg_row
17453 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17454 : -1);
17455 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17456
17457 #endif /* GLYPH_DEBUG */
17458
17459
17460 /* Display new lines. Set last_text_row to the last new line
17461 displayed which has text on it, i.e. might end up as being the
17462 line where the window_end_vpos is. */
17463 w->cursor.vpos = -1;
17464 last_text_row = NULL;
17465 overlay_arrow_seen = 0;
17466 while (it.current_y < it.last_visible_y
17467 && !f->fonts_changed
17468 && (first_unchanged_at_end_row == NULL
17469 || IT_CHARPOS (it) < stop_pos))
17470 {
17471 if (display_line (&it))
17472 last_text_row = it.glyph_row - 1;
17473 }
17474
17475 if (f->fonts_changed)
17476 return -1;
17477
17478
17479 /* Compute differences in buffer positions, y-positions etc. for
17480 lines reused at the bottom of the window. Compute what we can
17481 scroll. */
17482 if (first_unchanged_at_end_row
17483 /* No lines reused because we displayed everything up to the
17484 bottom of the window. */
17485 && it.current_y < it.last_visible_y)
17486 {
17487 dvpos = (it.vpos
17488 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17489 current_matrix));
17490 dy = it.current_y - first_unchanged_at_end_row->y;
17491 run.current_y = first_unchanged_at_end_row->y;
17492 run.desired_y = run.current_y + dy;
17493 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17494 }
17495 else
17496 {
17497 delta = delta_bytes = dvpos = dy
17498 = run.current_y = run.desired_y = run.height = 0;
17499 first_unchanged_at_end_row = NULL;
17500 }
17501 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17502
17503
17504 /* Find the cursor if not already found. We have to decide whether
17505 PT will appear on this window (it sometimes doesn't, but this is
17506 not a very frequent case.) This decision has to be made before
17507 the current matrix is altered. A value of cursor.vpos < 0 means
17508 that PT is either in one of the lines beginning at
17509 first_unchanged_at_end_row or below the window. Don't care for
17510 lines that might be displayed later at the window end; as
17511 mentioned, this is not a frequent case. */
17512 if (w->cursor.vpos < 0)
17513 {
17514 /* Cursor in unchanged rows at the top? */
17515 if (PT < CHARPOS (start_pos)
17516 && last_unchanged_at_beg_row)
17517 {
17518 row = row_containing_pos (w, PT,
17519 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17520 last_unchanged_at_beg_row + 1, 0);
17521 if (row)
17522 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17523 }
17524
17525 /* Start from first_unchanged_at_end_row looking for PT. */
17526 else if (first_unchanged_at_end_row)
17527 {
17528 row = row_containing_pos (w, PT - delta,
17529 first_unchanged_at_end_row, NULL, 0);
17530 if (row)
17531 set_cursor_from_row (w, row, w->current_matrix, delta,
17532 delta_bytes, dy, dvpos);
17533 }
17534
17535 /* Give up if cursor was not found. */
17536 if (w->cursor.vpos < 0)
17537 {
17538 clear_glyph_matrix (w->desired_matrix);
17539 return -1;
17540 }
17541 }
17542
17543 /* Don't let the cursor end in the scroll margins. */
17544 {
17545 int this_scroll_margin, cursor_height;
17546 int frame_line_height = default_line_pixel_height (w);
17547 int window_total_lines
17548 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17549
17550 this_scroll_margin =
17551 max (0, min (scroll_margin, window_total_lines / 4));
17552 this_scroll_margin *= frame_line_height;
17553 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17554
17555 if ((w->cursor.y < this_scroll_margin
17556 && CHARPOS (start) > BEGV)
17557 /* Old redisplay didn't take scroll margin into account at the bottom,
17558 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17559 || (w->cursor.y + (make_cursor_line_fully_visible_p
17560 ? cursor_height + this_scroll_margin
17561 : 1)) > it.last_visible_y)
17562 {
17563 w->cursor.vpos = -1;
17564 clear_glyph_matrix (w->desired_matrix);
17565 return -1;
17566 }
17567 }
17568
17569 /* Scroll the display. Do it before changing the current matrix so
17570 that xterm.c doesn't get confused about where the cursor glyph is
17571 found. */
17572 if (dy && run.height)
17573 {
17574 update_begin (f);
17575
17576 if (FRAME_WINDOW_P (f))
17577 {
17578 FRAME_RIF (f)->update_window_begin_hook (w);
17579 FRAME_RIF (f)->clear_window_mouse_face (w);
17580 FRAME_RIF (f)->scroll_run_hook (w, &run);
17581 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17582 }
17583 else
17584 {
17585 /* Terminal frame. In this case, dvpos gives the number of
17586 lines to scroll by; dvpos < 0 means scroll up. */
17587 int from_vpos
17588 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17589 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17590 int end = (WINDOW_TOP_EDGE_LINE (w)
17591 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17592 + window_internal_height (w));
17593
17594 #if defined (HAVE_GPM) || defined (MSDOS)
17595 x_clear_window_mouse_face (w);
17596 #endif
17597 /* Perform the operation on the screen. */
17598 if (dvpos > 0)
17599 {
17600 /* Scroll last_unchanged_at_beg_row to the end of the
17601 window down dvpos lines. */
17602 set_terminal_window (f, end);
17603
17604 /* On dumb terminals delete dvpos lines at the end
17605 before inserting dvpos empty lines. */
17606 if (!FRAME_SCROLL_REGION_OK (f))
17607 ins_del_lines (f, end - dvpos, -dvpos);
17608
17609 /* Insert dvpos empty lines in front of
17610 last_unchanged_at_beg_row. */
17611 ins_del_lines (f, from, dvpos);
17612 }
17613 else if (dvpos < 0)
17614 {
17615 /* Scroll up last_unchanged_at_beg_vpos to the end of
17616 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17617 set_terminal_window (f, end);
17618
17619 /* Delete dvpos lines in front of
17620 last_unchanged_at_beg_vpos. ins_del_lines will set
17621 the cursor to the given vpos and emit |dvpos| delete
17622 line sequences. */
17623 ins_del_lines (f, from + dvpos, dvpos);
17624
17625 /* On a dumb terminal insert dvpos empty lines at the
17626 end. */
17627 if (!FRAME_SCROLL_REGION_OK (f))
17628 ins_del_lines (f, end + dvpos, -dvpos);
17629 }
17630
17631 set_terminal_window (f, 0);
17632 }
17633
17634 update_end (f);
17635 }
17636
17637 /* Shift reused rows of the current matrix to the right position.
17638 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17639 text. */
17640 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17641 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17642 if (dvpos < 0)
17643 {
17644 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17645 bottom_vpos, dvpos);
17646 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17647 bottom_vpos);
17648 }
17649 else if (dvpos > 0)
17650 {
17651 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17652 bottom_vpos, dvpos);
17653 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17654 first_unchanged_at_end_vpos + dvpos);
17655 }
17656
17657 /* For frame-based redisplay, make sure that current frame and window
17658 matrix are in sync with respect to glyph memory. */
17659 if (!FRAME_WINDOW_P (f))
17660 sync_frame_with_window_matrix_rows (w);
17661
17662 /* Adjust buffer positions in reused rows. */
17663 if (delta || delta_bytes)
17664 increment_matrix_positions (current_matrix,
17665 first_unchanged_at_end_vpos + dvpos,
17666 bottom_vpos, delta, delta_bytes);
17667
17668 /* Adjust Y positions. */
17669 if (dy)
17670 shift_glyph_matrix (w, current_matrix,
17671 first_unchanged_at_end_vpos + dvpos,
17672 bottom_vpos, dy);
17673
17674 if (first_unchanged_at_end_row)
17675 {
17676 first_unchanged_at_end_row += dvpos;
17677 if (first_unchanged_at_end_row->y >= it.last_visible_y
17678 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17679 first_unchanged_at_end_row = NULL;
17680 }
17681
17682 /* If scrolling up, there may be some lines to display at the end of
17683 the window. */
17684 last_text_row_at_end = NULL;
17685 if (dy < 0)
17686 {
17687 /* Scrolling up can leave for example a partially visible line
17688 at the end of the window to be redisplayed. */
17689 /* Set last_row to the glyph row in the current matrix where the
17690 window end line is found. It has been moved up or down in
17691 the matrix by dvpos. */
17692 int last_vpos = w->window_end_vpos + dvpos;
17693 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17694
17695 /* If last_row is the window end line, it should display text. */
17696 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17697
17698 /* If window end line was partially visible before, begin
17699 displaying at that line. Otherwise begin displaying with the
17700 line following it. */
17701 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17702 {
17703 init_to_row_start (&it, w, last_row);
17704 it.vpos = last_vpos;
17705 it.current_y = last_row->y;
17706 }
17707 else
17708 {
17709 init_to_row_end (&it, w, last_row);
17710 it.vpos = 1 + last_vpos;
17711 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17712 ++last_row;
17713 }
17714
17715 /* We may start in a continuation line. If so, we have to
17716 get the right continuation_lines_width and current_x. */
17717 it.continuation_lines_width = last_row->continuation_lines_width;
17718 it.hpos = it.current_x = 0;
17719
17720 /* Display the rest of the lines at the window end. */
17721 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17722 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17723 {
17724 /* Is it always sure that the display agrees with lines in
17725 the current matrix? I don't think so, so we mark rows
17726 displayed invalid in the current matrix by setting their
17727 enabled_p flag to zero. */
17728 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17729 if (display_line (&it))
17730 last_text_row_at_end = it.glyph_row - 1;
17731 }
17732 }
17733
17734 /* Update window_end_pos and window_end_vpos. */
17735 if (first_unchanged_at_end_row && !last_text_row_at_end)
17736 {
17737 /* Window end line if one of the preserved rows from the current
17738 matrix. Set row to the last row displaying text in current
17739 matrix starting at first_unchanged_at_end_row, after
17740 scrolling. */
17741 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17742 row = find_last_row_displaying_text (w->current_matrix, &it,
17743 first_unchanged_at_end_row);
17744 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17745 adjust_window_ends (w, row, 1);
17746 eassert (w->window_end_bytepos >= 0);
17747 IF_DEBUG (debug_method_add (w, "A"));
17748 }
17749 else if (last_text_row_at_end)
17750 {
17751 adjust_window_ends (w, last_text_row_at_end, 0);
17752 eassert (w->window_end_bytepos >= 0);
17753 IF_DEBUG (debug_method_add (w, "B"));
17754 }
17755 else if (last_text_row)
17756 {
17757 /* We have displayed either to the end of the window or at the
17758 end of the window, i.e. the last row with text is to be found
17759 in the desired matrix. */
17760 adjust_window_ends (w, last_text_row, 0);
17761 eassert (w->window_end_bytepos >= 0);
17762 }
17763 else if (first_unchanged_at_end_row == NULL
17764 && last_text_row == NULL
17765 && last_text_row_at_end == NULL)
17766 {
17767 /* Displayed to end of window, but no line containing text was
17768 displayed. Lines were deleted at the end of the window. */
17769 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17770 int vpos = w->window_end_vpos;
17771 struct glyph_row *current_row = current_matrix->rows + vpos;
17772 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17773
17774 for (row = NULL;
17775 row == NULL && vpos >= first_vpos;
17776 --vpos, --current_row, --desired_row)
17777 {
17778 if (desired_row->enabled_p)
17779 {
17780 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17781 row = desired_row;
17782 }
17783 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17784 row = current_row;
17785 }
17786
17787 eassert (row != NULL);
17788 w->window_end_vpos = vpos + 1;
17789 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17790 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17791 eassert (w->window_end_bytepos >= 0);
17792 IF_DEBUG (debug_method_add (w, "C"));
17793 }
17794 else
17795 emacs_abort ();
17796
17797 IF_DEBUG (debug_end_pos = w->window_end_pos;
17798 debug_end_vpos = w->window_end_vpos);
17799
17800 /* Record that display has not been completed. */
17801 w->window_end_valid = 0;
17802 w->desired_matrix->no_scrolling_p = 1;
17803 return 3;
17804
17805 #undef GIVE_UP
17806 }
17807
17808
17809 \f
17810 /***********************************************************************
17811 More debugging support
17812 ***********************************************************************/
17813
17814 #ifdef GLYPH_DEBUG
17815
17816 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17817 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17818 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17819
17820
17821 /* Dump the contents of glyph matrix MATRIX on stderr.
17822
17823 GLYPHS 0 means don't show glyph contents.
17824 GLYPHS 1 means show glyphs in short form
17825 GLYPHS > 1 means show glyphs in long form. */
17826
17827 void
17828 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17829 {
17830 int i;
17831 for (i = 0; i < matrix->nrows; ++i)
17832 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17833 }
17834
17835
17836 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17837 the glyph row and area where the glyph comes from. */
17838
17839 void
17840 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17841 {
17842 if (glyph->type == CHAR_GLYPH
17843 || glyph->type == GLYPHLESS_GLYPH)
17844 {
17845 fprintf (stderr,
17846 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17847 glyph - row->glyphs[TEXT_AREA],
17848 (glyph->type == CHAR_GLYPH
17849 ? 'C'
17850 : 'G'),
17851 glyph->charpos,
17852 (BUFFERP (glyph->object)
17853 ? 'B'
17854 : (STRINGP (glyph->object)
17855 ? 'S'
17856 : (INTEGERP (glyph->object)
17857 ? '0'
17858 : '-'))),
17859 glyph->pixel_width,
17860 glyph->u.ch,
17861 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17862 ? glyph->u.ch
17863 : '.'),
17864 glyph->face_id,
17865 glyph->left_box_line_p,
17866 glyph->right_box_line_p);
17867 }
17868 else if (glyph->type == STRETCH_GLYPH)
17869 {
17870 fprintf (stderr,
17871 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17872 glyph - row->glyphs[TEXT_AREA],
17873 'S',
17874 glyph->charpos,
17875 (BUFFERP (glyph->object)
17876 ? 'B'
17877 : (STRINGP (glyph->object)
17878 ? 'S'
17879 : (INTEGERP (glyph->object)
17880 ? '0'
17881 : '-'))),
17882 glyph->pixel_width,
17883 0,
17884 ' ',
17885 glyph->face_id,
17886 glyph->left_box_line_p,
17887 glyph->right_box_line_p);
17888 }
17889 else if (glyph->type == IMAGE_GLYPH)
17890 {
17891 fprintf (stderr,
17892 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17893 glyph - row->glyphs[TEXT_AREA],
17894 'I',
17895 glyph->charpos,
17896 (BUFFERP (glyph->object)
17897 ? 'B'
17898 : (STRINGP (glyph->object)
17899 ? 'S'
17900 : (INTEGERP (glyph->object)
17901 ? '0'
17902 : '-'))),
17903 glyph->pixel_width,
17904 glyph->u.img_id,
17905 '.',
17906 glyph->face_id,
17907 glyph->left_box_line_p,
17908 glyph->right_box_line_p);
17909 }
17910 else if (glyph->type == COMPOSITE_GLYPH)
17911 {
17912 fprintf (stderr,
17913 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17914 glyph - row->glyphs[TEXT_AREA],
17915 '+',
17916 glyph->charpos,
17917 (BUFFERP (glyph->object)
17918 ? 'B'
17919 : (STRINGP (glyph->object)
17920 ? 'S'
17921 : (INTEGERP (glyph->object)
17922 ? '0'
17923 : '-'))),
17924 glyph->pixel_width,
17925 glyph->u.cmp.id);
17926 if (glyph->u.cmp.automatic)
17927 fprintf (stderr,
17928 "[%d-%d]",
17929 glyph->slice.cmp.from, glyph->slice.cmp.to);
17930 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17931 glyph->face_id,
17932 glyph->left_box_line_p,
17933 glyph->right_box_line_p);
17934 }
17935 }
17936
17937
17938 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17939 GLYPHS 0 means don't show glyph contents.
17940 GLYPHS 1 means show glyphs in short form
17941 GLYPHS > 1 means show glyphs in long form. */
17942
17943 void
17944 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17945 {
17946 if (glyphs != 1)
17947 {
17948 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17949 fprintf (stderr, "==============================================================================\n");
17950
17951 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17952 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17953 vpos,
17954 MATRIX_ROW_START_CHARPOS (row),
17955 MATRIX_ROW_END_CHARPOS (row),
17956 row->used[TEXT_AREA],
17957 row->contains_overlapping_glyphs_p,
17958 row->enabled_p,
17959 row->truncated_on_left_p,
17960 row->truncated_on_right_p,
17961 row->continued_p,
17962 MATRIX_ROW_CONTINUATION_LINE_P (row),
17963 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17964 row->ends_at_zv_p,
17965 row->fill_line_p,
17966 row->ends_in_middle_of_char_p,
17967 row->starts_in_middle_of_char_p,
17968 row->mouse_face_p,
17969 row->x,
17970 row->y,
17971 row->pixel_width,
17972 row->height,
17973 row->visible_height,
17974 row->ascent,
17975 row->phys_ascent);
17976 /* The next 3 lines should align to "Start" in the header. */
17977 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17978 row->end.overlay_string_index,
17979 row->continuation_lines_width);
17980 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17981 CHARPOS (row->start.string_pos),
17982 CHARPOS (row->end.string_pos));
17983 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17984 row->end.dpvec_index);
17985 }
17986
17987 if (glyphs > 1)
17988 {
17989 int area;
17990
17991 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17992 {
17993 struct glyph *glyph = row->glyphs[area];
17994 struct glyph *glyph_end = glyph + row->used[area];
17995
17996 /* Glyph for a line end in text. */
17997 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17998 ++glyph_end;
17999
18000 if (glyph < glyph_end)
18001 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18002
18003 for (; glyph < glyph_end; ++glyph)
18004 dump_glyph (row, glyph, area);
18005 }
18006 }
18007 else if (glyphs == 1)
18008 {
18009 int area;
18010
18011 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18012 {
18013 char *s = alloca (row->used[area] + 4);
18014 int i;
18015
18016 for (i = 0; i < row->used[area]; ++i)
18017 {
18018 struct glyph *glyph = row->glyphs[area] + i;
18019 if (i == row->used[area] - 1
18020 && area == TEXT_AREA
18021 && INTEGERP (glyph->object)
18022 && glyph->type == CHAR_GLYPH
18023 && glyph->u.ch == ' ')
18024 {
18025 strcpy (&s[i], "[\\n]");
18026 i += 4;
18027 }
18028 else if (glyph->type == CHAR_GLYPH
18029 && glyph->u.ch < 0x80
18030 && glyph->u.ch >= ' ')
18031 s[i] = glyph->u.ch;
18032 else
18033 s[i] = '.';
18034 }
18035
18036 s[i] = '\0';
18037 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18038 }
18039 }
18040 }
18041
18042
18043 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18044 Sdump_glyph_matrix, 0, 1, "p",
18045 doc: /* Dump the current matrix of the selected window to stderr.
18046 Shows contents of glyph row structures. With non-nil
18047 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18048 glyphs in short form, otherwise show glyphs in long form. */)
18049 (Lisp_Object glyphs)
18050 {
18051 struct window *w = XWINDOW (selected_window);
18052 struct buffer *buffer = XBUFFER (w->contents);
18053
18054 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18055 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18056 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18057 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18058 fprintf (stderr, "=============================================\n");
18059 dump_glyph_matrix (w->current_matrix,
18060 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18061 return Qnil;
18062 }
18063
18064
18065 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18066 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18067 (void)
18068 {
18069 struct frame *f = XFRAME (selected_frame);
18070 dump_glyph_matrix (f->current_matrix, 1);
18071 return Qnil;
18072 }
18073
18074
18075 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18076 doc: /* Dump glyph row ROW to stderr.
18077 GLYPH 0 means don't dump glyphs.
18078 GLYPH 1 means dump glyphs in short form.
18079 GLYPH > 1 or omitted means dump glyphs in long form. */)
18080 (Lisp_Object row, Lisp_Object glyphs)
18081 {
18082 struct glyph_matrix *matrix;
18083 EMACS_INT vpos;
18084
18085 CHECK_NUMBER (row);
18086 matrix = XWINDOW (selected_window)->current_matrix;
18087 vpos = XINT (row);
18088 if (vpos >= 0 && vpos < matrix->nrows)
18089 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18090 vpos,
18091 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18092 return Qnil;
18093 }
18094
18095
18096 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18097 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18098 GLYPH 0 means don't dump glyphs.
18099 GLYPH 1 means dump glyphs in short form.
18100 GLYPH > 1 or omitted means dump glyphs in long form. */)
18101 (Lisp_Object row, Lisp_Object glyphs)
18102 {
18103 struct frame *sf = SELECTED_FRAME ();
18104 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18105 EMACS_INT vpos;
18106
18107 CHECK_NUMBER (row);
18108 vpos = XINT (row);
18109 if (vpos >= 0 && vpos < m->nrows)
18110 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18111 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18112 return Qnil;
18113 }
18114
18115
18116 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18117 doc: /* Toggle tracing of redisplay.
18118 With ARG, turn tracing on if and only if ARG is positive. */)
18119 (Lisp_Object arg)
18120 {
18121 if (NILP (arg))
18122 trace_redisplay_p = !trace_redisplay_p;
18123 else
18124 {
18125 arg = Fprefix_numeric_value (arg);
18126 trace_redisplay_p = XINT (arg) > 0;
18127 }
18128
18129 return Qnil;
18130 }
18131
18132
18133 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18134 doc: /* Like `format', but print result to stderr.
18135 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18136 (ptrdiff_t nargs, Lisp_Object *args)
18137 {
18138 Lisp_Object s = Fformat (nargs, args);
18139 fprintf (stderr, "%s", SDATA (s));
18140 return Qnil;
18141 }
18142
18143 #endif /* GLYPH_DEBUG */
18144
18145
18146 \f
18147 /***********************************************************************
18148 Building Desired Matrix Rows
18149 ***********************************************************************/
18150
18151 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18152 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18153
18154 static struct glyph_row *
18155 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18156 {
18157 struct frame *f = XFRAME (WINDOW_FRAME (w));
18158 struct buffer *buffer = XBUFFER (w->contents);
18159 struct buffer *old = current_buffer;
18160 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18161 int arrow_len = SCHARS (overlay_arrow_string);
18162 const unsigned char *arrow_end = arrow_string + arrow_len;
18163 const unsigned char *p;
18164 struct it it;
18165 bool multibyte_p;
18166 int n_glyphs_before;
18167
18168 set_buffer_temp (buffer);
18169 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18170 it.glyph_row->used[TEXT_AREA] = 0;
18171 SET_TEXT_POS (it.position, 0, 0);
18172
18173 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18174 p = arrow_string;
18175 while (p < arrow_end)
18176 {
18177 Lisp_Object face, ilisp;
18178
18179 /* Get the next character. */
18180 if (multibyte_p)
18181 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18182 else
18183 {
18184 it.c = it.char_to_display = *p, it.len = 1;
18185 if (! ASCII_CHAR_P (it.c))
18186 it.char_to_display = BYTE8_TO_CHAR (it.c);
18187 }
18188 p += it.len;
18189
18190 /* Get its face. */
18191 ilisp = make_number (p - arrow_string);
18192 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18193 it.face_id = compute_char_face (f, it.char_to_display, face);
18194
18195 /* Compute its width, get its glyphs. */
18196 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18197 SET_TEXT_POS (it.position, -1, -1);
18198 PRODUCE_GLYPHS (&it);
18199
18200 /* If this character doesn't fit any more in the line, we have
18201 to remove some glyphs. */
18202 if (it.current_x > it.last_visible_x)
18203 {
18204 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18205 break;
18206 }
18207 }
18208
18209 set_buffer_temp (old);
18210 return it.glyph_row;
18211 }
18212
18213
18214 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18215 glyphs to insert is determined by produce_special_glyphs. */
18216
18217 static void
18218 insert_left_trunc_glyphs (struct it *it)
18219 {
18220 struct it truncate_it;
18221 struct glyph *from, *end, *to, *toend;
18222
18223 eassert (!FRAME_WINDOW_P (it->f)
18224 || (!it->glyph_row->reversed_p
18225 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18226 || (it->glyph_row->reversed_p
18227 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18228
18229 /* Get the truncation glyphs. */
18230 truncate_it = *it;
18231 truncate_it.current_x = 0;
18232 truncate_it.face_id = DEFAULT_FACE_ID;
18233 truncate_it.glyph_row = &scratch_glyph_row;
18234 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18235 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18236 truncate_it.object = make_number (0);
18237 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18238
18239 /* Overwrite glyphs from IT with truncation glyphs. */
18240 if (!it->glyph_row->reversed_p)
18241 {
18242 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18243
18244 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18245 end = from + tused;
18246 to = it->glyph_row->glyphs[TEXT_AREA];
18247 toend = to + it->glyph_row->used[TEXT_AREA];
18248 if (FRAME_WINDOW_P (it->f))
18249 {
18250 /* On GUI frames, when variable-size fonts are displayed,
18251 the truncation glyphs may need more pixels than the row's
18252 glyphs they overwrite. We overwrite more glyphs to free
18253 enough screen real estate, and enlarge the stretch glyph
18254 on the right (see display_line), if there is one, to
18255 preserve the screen position of the truncation glyphs on
18256 the right. */
18257 int w = 0;
18258 struct glyph *g = to;
18259 short used;
18260
18261 /* The first glyph could be partially visible, in which case
18262 it->glyph_row->x will be negative. But we want the left
18263 truncation glyphs to be aligned at the left margin of the
18264 window, so we override the x coordinate at which the row
18265 will begin. */
18266 it->glyph_row->x = 0;
18267 while (g < toend && w < it->truncation_pixel_width)
18268 {
18269 w += g->pixel_width;
18270 ++g;
18271 }
18272 if (g - to - tused > 0)
18273 {
18274 memmove (to + tused, g, (toend - g) * sizeof(*g));
18275 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18276 }
18277 used = it->glyph_row->used[TEXT_AREA];
18278 if (it->glyph_row->truncated_on_right_p
18279 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18280 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18281 == STRETCH_GLYPH)
18282 {
18283 int extra = w - it->truncation_pixel_width;
18284
18285 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18286 }
18287 }
18288
18289 while (from < end)
18290 *to++ = *from++;
18291
18292 /* There may be padding glyphs left over. Overwrite them too. */
18293 if (!FRAME_WINDOW_P (it->f))
18294 {
18295 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18296 {
18297 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18298 while (from < end)
18299 *to++ = *from++;
18300 }
18301 }
18302
18303 if (to > toend)
18304 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18305 }
18306 else
18307 {
18308 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18309
18310 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18311 that back to front. */
18312 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18313 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18314 toend = it->glyph_row->glyphs[TEXT_AREA];
18315 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18316 if (FRAME_WINDOW_P (it->f))
18317 {
18318 int w = 0;
18319 struct glyph *g = to;
18320
18321 while (g >= toend && w < it->truncation_pixel_width)
18322 {
18323 w += g->pixel_width;
18324 --g;
18325 }
18326 if (to - g - tused > 0)
18327 to = g + tused;
18328 if (it->glyph_row->truncated_on_right_p
18329 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18330 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18331 {
18332 int extra = w - it->truncation_pixel_width;
18333
18334 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18335 }
18336 }
18337
18338 while (from >= end && to >= toend)
18339 *to-- = *from--;
18340 if (!FRAME_WINDOW_P (it->f))
18341 {
18342 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18343 {
18344 from =
18345 truncate_it.glyph_row->glyphs[TEXT_AREA]
18346 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18347 while (from >= end && to >= toend)
18348 *to-- = *from--;
18349 }
18350 }
18351 if (from >= end)
18352 {
18353 /* Need to free some room before prepending additional
18354 glyphs. */
18355 int move_by = from - end + 1;
18356 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18357 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18358
18359 for ( ; g >= g0; g--)
18360 g[move_by] = *g;
18361 while (from >= end)
18362 *to-- = *from--;
18363 it->glyph_row->used[TEXT_AREA] += move_by;
18364 }
18365 }
18366 }
18367
18368 /* Compute the hash code for ROW. */
18369 unsigned
18370 row_hash (struct glyph_row *row)
18371 {
18372 int area, k;
18373 unsigned hashval = 0;
18374
18375 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18376 for (k = 0; k < row->used[area]; ++k)
18377 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18378 + row->glyphs[area][k].u.val
18379 + row->glyphs[area][k].face_id
18380 + row->glyphs[area][k].padding_p
18381 + (row->glyphs[area][k].type << 2));
18382
18383 return hashval;
18384 }
18385
18386 /* Compute the pixel height and width of IT->glyph_row.
18387
18388 Most of the time, ascent and height of a display line will be equal
18389 to the max_ascent and max_height values of the display iterator
18390 structure. This is not the case if
18391
18392 1. We hit ZV without displaying anything. In this case, max_ascent
18393 and max_height will be zero.
18394
18395 2. We have some glyphs that don't contribute to the line height.
18396 (The glyph row flag contributes_to_line_height_p is for future
18397 pixmap extensions).
18398
18399 The first case is easily covered by using default values because in
18400 these cases, the line height does not really matter, except that it
18401 must not be zero. */
18402
18403 static void
18404 compute_line_metrics (struct it *it)
18405 {
18406 struct glyph_row *row = it->glyph_row;
18407
18408 if (FRAME_WINDOW_P (it->f))
18409 {
18410 int i, min_y, max_y;
18411
18412 /* The line may consist of one space only, that was added to
18413 place the cursor on it. If so, the row's height hasn't been
18414 computed yet. */
18415 if (row->height == 0)
18416 {
18417 if (it->max_ascent + it->max_descent == 0)
18418 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18419 row->ascent = it->max_ascent;
18420 row->height = it->max_ascent + it->max_descent;
18421 row->phys_ascent = it->max_phys_ascent;
18422 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18423 row->extra_line_spacing = it->max_extra_line_spacing;
18424 }
18425
18426 /* Compute the width of this line. */
18427 row->pixel_width = row->x;
18428 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18429 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18430
18431 eassert (row->pixel_width >= 0);
18432 eassert (row->ascent >= 0 && row->height > 0);
18433
18434 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18435 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18436
18437 /* If first line's physical ascent is larger than its logical
18438 ascent, use the physical ascent, and make the row taller.
18439 This makes accented characters fully visible. */
18440 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18441 && row->phys_ascent > row->ascent)
18442 {
18443 row->height += row->phys_ascent - row->ascent;
18444 row->ascent = row->phys_ascent;
18445 }
18446
18447 /* Compute how much of the line is visible. */
18448 row->visible_height = row->height;
18449
18450 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18451 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18452
18453 if (row->y < min_y)
18454 row->visible_height -= min_y - row->y;
18455 if (row->y + row->height > max_y)
18456 row->visible_height -= row->y + row->height - max_y;
18457 }
18458 else
18459 {
18460 row->pixel_width = row->used[TEXT_AREA];
18461 if (row->continued_p)
18462 row->pixel_width -= it->continuation_pixel_width;
18463 else if (row->truncated_on_right_p)
18464 row->pixel_width -= it->truncation_pixel_width;
18465 row->ascent = row->phys_ascent = 0;
18466 row->height = row->phys_height = row->visible_height = 1;
18467 row->extra_line_spacing = 0;
18468 }
18469
18470 /* Compute a hash code for this row. */
18471 row->hash = row_hash (row);
18472
18473 it->max_ascent = it->max_descent = 0;
18474 it->max_phys_ascent = it->max_phys_descent = 0;
18475 }
18476
18477
18478 /* Append one space to the glyph row of iterator IT if doing a
18479 window-based redisplay. The space has the same face as
18480 IT->face_id. Value is non-zero if a space was added.
18481
18482 This function is called to make sure that there is always one glyph
18483 at the end of a glyph row that the cursor can be set on under
18484 window-systems. (If there weren't such a glyph we would not know
18485 how wide and tall a box cursor should be displayed).
18486
18487 At the same time this space let's a nicely handle clearing to the
18488 end of the line if the row ends in italic text. */
18489
18490 static int
18491 append_space_for_newline (struct it *it, int default_face_p)
18492 {
18493 if (FRAME_WINDOW_P (it->f))
18494 {
18495 int n = it->glyph_row->used[TEXT_AREA];
18496
18497 if (it->glyph_row->glyphs[TEXT_AREA] + n
18498 < it->glyph_row->glyphs[1 + TEXT_AREA])
18499 {
18500 /* Save some values that must not be changed.
18501 Must save IT->c and IT->len because otherwise
18502 ITERATOR_AT_END_P wouldn't work anymore after
18503 append_space_for_newline has been called. */
18504 enum display_element_type saved_what = it->what;
18505 int saved_c = it->c, saved_len = it->len;
18506 int saved_char_to_display = it->char_to_display;
18507 int saved_x = it->current_x;
18508 int saved_face_id = it->face_id;
18509 int saved_box_end = it->end_of_box_run_p;
18510 struct text_pos saved_pos;
18511 Lisp_Object saved_object;
18512 struct face *face;
18513
18514 saved_object = it->object;
18515 saved_pos = it->position;
18516
18517 it->what = IT_CHARACTER;
18518 memset (&it->position, 0, sizeof it->position);
18519 it->object = make_number (0);
18520 it->c = it->char_to_display = ' ';
18521 it->len = 1;
18522
18523 /* If the default face was remapped, be sure to use the
18524 remapped face for the appended newline. */
18525 if (default_face_p)
18526 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18527 else if (it->face_before_selective_p)
18528 it->face_id = it->saved_face_id;
18529 face = FACE_FROM_ID (it->f, it->face_id);
18530 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18531 /* In R2L rows, we will prepend a stretch glyph that will
18532 have the end_of_box_run_p flag set for it, so there's no
18533 need for the appended newline glyph to have that flag
18534 set. */
18535 if (it->glyph_row->reversed_p
18536 /* But if the appended newline glyph goes all the way to
18537 the end of the row, there will be no stretch glyph,
18538 so leave the box flag set. */
18539 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18540 it->end_of_box_run_p = 0;
18541
18542 PRODUCE_GLYPHS (it);
18543
18544 it->override_ascent = -1;
18545 it->constrain_row_ascent_descent_p = 0;
18546 it->current_x = saved_x;
18547 it->object = saved_object;
18548 it->position = saved_pos;
18549 it->what = saved_what;
18550 it->face_id = saved_face_id;
18551 it->len = saved_len;
18552 it->c = saved_c;
18553 it->char_to_display = saved_char_to_display;
18554 it->end_of_box_run_p = saved_box_end;
18555 return 1;
18556 }
18557 }
18558
18559 return 0;
18560 }
18561
18562
18563 /* Extend the face of the last glyph in the text area of IT->glyph_row
18564 to the end of the display line. Called from display_line. If the
18565 glyph row is empty, add a space glyph to it so that we know the
18566 face to draw. Set the glyph row flag fill_line_p. If the glyph
18567 row is R2L, prepend a stretch glyph to cover the empty space to the
18568 left of the leftmost glyph. */
18569
18570 static void
18571 extend_face_to_end_of_line (struct it *it)
18572 {
18573 struct face *face, *default_face;
18574 struct frame *f = it->f;
18575
18576 /* If line is already filled, do nothing. Non window-system frames
18577 get a grace of one more ``pixel'' because their characters are
18578 1-``pixel'' wide, so they hit the equality too early. This grace
18579 is needed only for R2L rows that are not continued, to produce
18580 one extra blank where we could display the cursor. */
18581 if (it->current_x >= it->last_visible_x
18582 + (!FRAME_WINDOW_P (f)
18583 && it->glyph_row->reversed_p
18584 && !it->glyph_row->continued_p))
18585 return;
18586
18587 /* The default face, possibly remapped. */
18588 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18589
18590 /* Face extension extends the background and box of IT->face_id
18591 to the end of the line. If the background equals the background
18592 of the frame, we don't have to do anything. */
18593 if (it->face_before_selective_p)
18594 face = FACE_FROM_ID (f, it->saved_face_id);
18595 else
18596 face = FACE_FROM_ID (f, it->face_id);
18597
18598 if (FRAME_WINDOW_P (f)
18599 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18600 && face->box == FACE_NO_BOX
18601 && face->background == FRAME_BACKGROUND_PIXEL (f)
18602 && !face->stipple
18603 && !it->glyph_row->reversed_p)
18604 return;
18605
18606 /* Set the glyph row flag indicating that the face of the last glyph
18607 in the text area has to be drawn to the end of the text area. */
18608 it->glyph_row->fill_line_p = 1;
18609
18610 /* If current character of IT is not ASCII, make sure we have the
18611 ASCII face. This will be automatically undone the next time
18612 get_next_display_element returns a multibyte character. Note
18613 that the character will always be single byte in unibyte
18614 text. */
18615 if (!ASCII_CHAR_P (it->c))
18616 {
18617 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18618 }
18619
18620 if (FRAME_WINDOW_P (f))
18621 {
18622 /* If the row is empty, add a space with the current face of IT,
18623 so that we know which face to draw. */
18624 if (it->glyph_row->used[TEXT_AREA] == 0)
18625 {
18626 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18627 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18628 it->glyph_row->used[TEXT_AREA] = 1;
18629 }
18630 #ifdef HAVE_WINDOW_SYSTEM
18631 if (it->glyph_row->reversed_p)
18632 {
18633 /* Prepend a stretch glyph to the row, such that the
18634 rightmost glyph will be drawn flushed all the way to the
18635 right margin of the window. The stretch glyph that will
18636 occupy the empty space, if any, to the left of the
18637 glyphs. */
18638 struct font *font = face->font ? face->font : FRAME_FONT (f);
18639 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18640 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18641 struct glyph *g;
18642 int row_width, stretch_ascent, stretch_width;
18643 struct text_pos saved_pos;
18644 int saved_face_id, saved_avoid_cursor, saved_box_start;
18645
18646 for (row_width = 0, g = row_start; g < row_end; g++)
18647 row_width += g->pixel_width;
18648 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18649 if (stretch_width > 0)
18650 {
18651 stretch_ascent =
18652 (((it->ascent + it->descent)
18653 * FONT_BASE (font)) / FONT_HEIGHT (font));
18654 saved_pos = it->position;
18655 memset (&it->position, 0, sizeof it->position);
18656 saved_avoid_cursor = it->avoid_cursor_p;
18657 it->avoid_cursor_p = 1;
18658 saved_face_id = it->face_id;
18659 saved_box_start = it->start_of_box_run_p;
18660 /* The last row's stretch glyph should get the default
18661 face, to avoid painting the rest of the window with
18662 the region face, if the region ends at ZV. */
18663 if (it->glyph_row->ends_at_zv_p)
18664 it->face_id = default_face->id;
18665 else
18666 it->face_id = face->id;
18667 it->start_of_box_run_p = 0;
18668 append_stretch_glyph (it, make_number (0), stretch_width,
18669 it->ascent + it->descent, stretch_ascent);
18670 it->position = saved_pos;
18671 it->avoid_cursor_p = saved_avoid_cursor;
18672 it->face_id = saved_face_id;
18673 it->start_of_box_run_p = saved_box_start;
18674 }
18675 }
18676 #endif /* HAVE_WINDOW_SYSTEM */
18677 }
18678 else
18679 {
18680 /* Save some values that must not be changed. */
18681 int saved_x = it->current_x;
18682 struct text_pos saved_pos;
18683 Lisp_Object saved_object;
18684 enum display_element_type saved_what = it->what;
18685 int saved_face_id = it->face_id;
18686
18687 saved_object = it->object;
18688 saved_pos = it->position;
18689
18690 it->what = IT_CHARACTER;
18691 memset (&it->position, 0, sizeof it->position);
18692 it->object = make_number (0);
18693 it->c = it->char_to_display = ' ';
18694 it->len = 1;
18695 /* The last row's blank glyphs should get the default face, to
18696 avoid painting the rest of the window with the region face,
18697 if the region ends at ZV. */
18698 if (it->glyph_row->ends_at_zv_p)
18699 it->face_id = default_face->id;
18700 else
18701 it->face_id = face->id;
18702
18703 PRODUCE_GLYPHS (it);
18704
18705 while (it->current_x <= it->last_visible_x)
18706 PRODUCE_GLYPHS (it);
18707
18708 /* Don't count these blanks really. It would let us insert a left
18709 truncation glyph below and make us set the cursor on them, maybe. */
18710 it->current_x = saved_x;
18711 it->object = saved_object;
18712 it->position = saved_pos;
18713 it->what = saved_what;
18714 it->face_id = saved_face_id;
18715 }
18716 }
18717
18718
18719 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18720 trailing whitespace. */
18721
18722 static int
18723 trailing_whitespace_p (ptrdiff_t charpos)
18724 {
18725 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18726 int c = 0;
18727
18728 while (bytepos < ZV_BYTE
18729 && (c = FETCH_CHAR (bytepos),
18730 c == ' ' || c == '\t'))
18731 ++bytepos;
18732
18733 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18734 {
18735 if (bytepos != PT_BYTE)
18736 return 1;
18737 }
18738 return 0;
18739 }
18740
18741
18742 /* Highlight trailing whitespace, if any, in ROW. */
18743
18744 static void
18745 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18746 {
18747 int used = row->used[TEXT_AREA];
18748
18749 if (used)
18750 {
18751 struct glyph *start = row->glyphs[TEXT_AREA];
18752 struct glyph *glyph = start + used - 1;
18753
18754 if (row->reversed_p)
18755 {
18756 /* Right-to-left rows need to be processed in the opposite
18757 direction, so swap the edge pointers. */
18758 glyph = start;
18759 start = row->glyphs[TEXT_AREA] + used - 1;
18760 }
18761
18762 /* Skip over glyphs inserted to display the cursor at the
18763 end of a line, for extending the face of the last glyph
18764 to the end of the line on terminals, and for truncation
18765 and continuation glyphs. */
18766 if (!row->reversed_p)
18767 {
18768 while (glyph >= start
18769 && glyph->type == CHAR_GLYPH
18770 && INTEGERP (glyph->object))
18771 --glyph;
18772 }
18773 else
18774 {
18775 while (glyph <= start
18776 && glyph->type == CHAR_GLYPH
18777 && INTEGERP (glyph->object))
18778 ++glyph;
18779 }
18780
18781 /* If last glyph is a space or stretch, and it's trailing
18782 whitespace, set the face of all trailing whitespace glyphs in
18783 IT->glyph_row to `trailing-whitespace'. */
18784 if ((row->reversed_p ? glyph <= start : glyph >= start)
18785 && BUFFERP (glyph->object)
18786 && (glyph->type == STRETCH_GLYPH
18787 || (glyph->type == CHAR_GLYPH
18788 && glyph->u.ch == ' '))
18789 && trailing_whitespace_p (glyph->charpos))
18790 {
18791 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18792 if (face_id < 0)
18793 return;
18794
18795 if (!row->reversed_p)
18796 {
18797 while (glyph >= start
18798 && BUFFERP (glyph->object)
18799 && (glyph->type == STRETCH_GLYPH
18800 || (glyph->type == CHAR_GLYPH
18801 && glyph->u.ch == ' ')))
18802 (glyph--)->face_id = face_id;
18803 }
18804 else
18805 {
18806 while (glyph <= start
18807 && BUFFERP (glyph->object)
18808 && (glyph->type == STRETCH_GLYPH
18809 || (glyph->type == CHAR_GLYPH
18810 && glyph->u.ch == ' ')))
18811 (glyph++)->face_id = face_id;
18812 }
18813 }
18814 }
18815 }
18816
18817
18818 /* Value is non-zero if glyph row ROW should be
18819 considered to hold the buffer position CHARPOS. */
18820
18821 static int
18822 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18823 {
18824 int result = 1;
18825
18826 if (charpos == CHARPOS (row->end.pos)
18827 || charpos == MATRIX_ROW_END_CHARPOS (row))
18828 {
18829 /* Suppose the row ends on a string.
18830 Unless the row is continued, that means it ends on a newline
18831 in the string. If it's anything other than a display string
18832 (e.g., a before-string from an overlay), we don't want the
18833 cursor there. (This heuristic seems to give the optimal
18834 behavior for the various types of multi-line strings.)
18835 One exception: if the string has `cursor' property on one of
18836 its characters, we _do_ want the cursor there. */
18837 if (CHARPOS (row->end.string_pos) >= 0)
18838 {
18839 if (row->continued_p)
18840 result = 1;
18841 else
18842 {
18843 /* Check for `display' property. */
18844 struct glyph *beg = row->glyphs[TEXT_AREA];
18845 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18846 struct glyph *glyph;
18847
18848 result = 0;
18849 for (glyph = end; glyph >= beg; --glyph)
18850 if (STRINGP (glyph->object))
18851 {
18852 Lisp_Object prop
18853 = Fget_char_property (make_number (charpos),
18854 Qdisplay, Qnil);
18855 result =
18856 (!NILP (prop)
18857 && display_prop_string_p (prop, glyph->object));
18858 /* If there's a `cursor' property on one of the
18859 string's characters, this row is a cursor row,
18860 even though this is not a display string. */
18861 if (!result)
18862 {
18863 Lisp_Object s = glyph->object;
18864
18865 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18866 {
18867 ptrdiff_t gpos = glyph->charpos;
18868
18869 if (!NILP (Fget_char_property (make_number (gpos),
18870 Qcursor, s)))
18871 {
18872 result = 1;
18873 break;
18874 }
18875 }
18876 }
18877 break;
18878 }
18879 }
18880 }
18881 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18882 {
18883 /* If the row ends in middle of a real character,
18884 and the line is continued, we want the cursor here.
18885 That's because CHARPOS (ROW->end.pos) would equal
18886 PT if PT is before the character. */
18887 if (!row->ends_in_ellipsis_p)
18888 result = row->continued_p;
18889 else
18890 /* If the row ends in an ellipsis, then
18891 CHARPOS (ROW->end.pos) will equal point after the
18892 invisible text. We want that position to be displayed
18893 after the ellipsis. */
18894 result = 0;
18895 }
18896 /* If the row ends at ZV, display the cursor at the end of that
18897 row instead of at the start of the row below. */
18898 else if (row->ends_at_zv_p)
18899 result = 1;
18900 else
18901 result = 0;
18902 }
18903
18904 return result;
18905 }
18906
18907 /* Value is non-zero if glyph row ROW should be
18908 used to hold the cursor. */
18909
18910 static int
18911 cursor_row_p (struct glyph_row *row)
18912 {
18913 return row_for_charpos_p (row, PT);
18914 }
18915
18916 \f
18917
18918 /* Push the property PROP so that it will be rendered at the current
18919 position in IT. Return 1 if PROP was successfully pushed, 0
18920 otherwise. Called from handle_line_prefix to handle the
18921 `line-prefix' and `wrap-prefix' properties. */
18922
18923 static int
18924 push_prefix_prop (struct it *it, Lisp_Object prop)
18925 {
18926 struct text_pos pos =
18927 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18928
18929 eassert (it->method == GET_FROM_BUFFER
18930 || it->method == GET_FROM_DISPLAY_VECTOR
18931 || it->method == GET_FROM_STRING);
18932
18933 /* We need to save the current buffer/string position, so it will be
18934 restored by pop_it, because iterate_out_of_display_property
18935 depends on that being set correctly, but some situations leave
18936 it->position not yet set when this function is called. */
18937 push_it (it, &pos);
18938
18939 if (STRINGP (prop))
18940 {
18941 if (SCHARS (prop) == 0)
18942 {
18943 pop_it (it);
18944 return 0;
18945 }
18946
18947 it->string = prop;
18948 it->string_from_prefix_prop_p = 1;
18949 it->multibyte_p = STRING_MULTIBYTE (it->string);
18950 it->current.overlay_string_index = -1;
18951 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18952 it->end_charpos = it->string_nchars = SCHARS (it->string);
18953 it->method = GET_FROM_STRING;
18954 it->stop_charpos = 0;
18955 it->prev_stop = 0;
18956 it->base_level_stop = 0;
18957
18958 /* Force paragraph direction to be that of the parent
18959 buffer/string. */
18960 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18961 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18962 else
18963 it->paragraph_embedding = L2R;
18964
18965 /* Set up the bidi iterator for this display string. */
18966 if (it->bidi_p)
18967 {
18968 it->bidi_it.string.lstring = it->string;
18969 it->bidi_it.string.s = NULL;
18970 it->bidi_it.string.schars = it->end_charpos;
18971 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18972 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18973 it->bidi_it.string.unibyte = !it->multibyte_p;
18974 it->bidi_it.w = it->w;
18975 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18976 }
18977 }
18978 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18979 {
18980 it->method = GET_FROM_STRETCH;
18981 it->object = prop;
18982 }
18983 #ifdef HAVE_WINDOW_SYSTEM
18984 else if (IMAGEP (prop))
18985 {
18986 it->what = IT_IMAGE;
18987 it->image_id = lookup_image (it->f, prop);
18988 it->method = GET_FROM_IMAGE;
18989 }
18990 #endif /* HAVE_WINDOW_SYSTEM */
18991 else
18992 {
18993 pop_it (it); /* bogus display property, give up */
18994 return 0;
18995 }
18996
18997 return 1;
18998 }
18999
19000 /* Return the character-property PROP at the current position in IT. */
19001
19002 static Lisp_Object
19003 get_it_property (struct it *it, Lisp_Object prop)
19004 {
19005 Lisp_Object position, object = it->object;
19006
19007 if (STRINGP (object))
19008 position = make_number (IT_STRING_CHARPOS (*it));
19009 else if (BUFFERP (object))
19010 {
19011 position = make_number (IT_CHARPOS (*it));
19012 object = it->window;
19013 }
19014 else
19015 return Qnil;
19016
19017 return Fget_char_property (position, prop, object);
19018 }
19019
19020 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19021
19022 static void
19023 handle_line_prefix (struct it *it)
19024 {
19025 Lisp_Object prefix;
19026
19027 if (it->continuation_lines_width > 0)
19028 {
19029 prefix = get_it_property (it, Qwrap_prefix);
19030 if (NILP (prefix))
19031 prefix = Vwrap_prefix;
19032 }
19033 else
19034 {
19035 prefix = get_it_property (it, Qline_prefix);
19036 if (NILP (prefix))
19037 prefix = Vline_prefix;
19038 }
19039 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19040 {
19041 /* If the prefix is wider than the window, and we try to wrap
19042 it, it would acquire its own wrap prefix, and so on till the
19043 iterator stack overflows. So, don't wrap the prefix. */
19044 it->line_wrap = TRUNCATE;
19045 it->avoid_cursor_p = 1;
19046 }
19047 }
19048
19049 \f
19050
19051 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19052 only for R2L lines from display_line and display_string, when they
19053 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19054 the line/string needs to be continued on the next glyph row. */
19055 static void
19056 unproduce_glyphs (struct it *it, int n)
19057 {
19058 struct glyph *glyph, *end;
19059
19060 eassert (it->glyph_row);
19061 eassert (it->glyph_row->reversed_p);
19062 eassert (it->area == TEXT_AREA);
19063 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19064
19065 if (n > it->glyph_row->used[TEXT_AREA])
19066 n = it->glyph_row->used[TEXT_AREA];
19067 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19068 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19069 for ( ; glyph < end; glyph++)
19070 glyph[-n] = *glyph;
19071 }
19072
19073 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19074 and ROW->maxpos. */
19075 static void
19076 find_row_edges (struct it *it, struct glyph_row *row,
19077 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19078 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19079 {
19080 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19081 lines' rows is implemented for bidi-reordered rows. */
19082
19083 /* ROW->minpos is the value of min_pos, the minimal buffer position
19084 we have in ROW, or ROW->start.pos if that is smaller. */
19085 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19086 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19087 else
19088 /* We didn't find buffer positions smaller than ROW->start, or
19089 didn't find _any_ valid buffer positions in any of the glyphs,
19090 so we must trust the iterator's computed positions. */
19091 row->minpos = row->start.pos;
19092 if (max_pos <= 0)
19093 {
19094 max_pos = CHARPOS (it->current.pos);
19095 max_bpos = BYTEPOS (it->current.pos);
19096 }
19097
19098 /* Here are the various use-cases for ending the row, and the
19099 corresponding values for ROW->maxpos:
19100
19101 Line ends in a newline from buffer eol_pos + 1
19102 Line is continued from buffer max_pos + 1
19103 Line is truncated on right it->current.pos
19104 Line ends in a newline from string max_pos + 1(*)
19105 (*) + 1 only when line ends in a forward scan
19106 Line is continued from string max_pos
19107 Line is continued from display vector max_pos
19108 Line is entirely from a string min_pos == max_pos
19109 Line is entirely from a display vector min_pos == max_pos
19110 Line that ends at ZV ZV
19111
19112 If you discover other use-cases, please add them here as
19113 appropriate. */
19114 if (row->ends_at_zv_p)
19115 row->maxpos = it->current.pos;
19116 else if (row->used[TEXT_AREA])
19117 {
19118 int seen_this_string = 0;
19119 struct glyph_row *r1 = row - 1;
19120
19121 /* Did we see the same display string on the previous row? */
19122 if (STRINGP (it->object)
19123 /* this is not the first row */
19124 && row > it->w->desired_matrix->rows
19125 /* previous row is not the header line */
19126 && !r1->mode_line_p
19127 /* previous row also ends in a newline from a string */
19128 && r1->ends_in_newline_from_string_p)
19129 {
19130 struct glyph *start, *end;
19131
19132 /* Search for the last glyph of the previous row that came
19133 from buffer or string. Depending on whether the row is
19134 L2R or R2L, we need to process it front to back or the
19135 other way round. */
19136 if (!r1->reversed_p)
19137 {
19138 start = r1->glyphs[TEXT_AREA];
19139 end = start + r1->used[TEXT_AREA];
19140 /* Glyphs inserted by redisplay have an integer (zero)
19141 as their object. */
19142 while (end > start
19143 && INTEGERP ((end - 1)->object)
19144 && (end - 1)->charpos <= 0)
19145 --end;
19146 if (end > start)
19147 {
19148 if (EQ ((end - 1)->object, it->object))
19149 seen_this_string = 1;
19150 }
19151 else
19152 /* If all the glyphs of the previous row were inserted
19153 by redisplay, it means the previous row was
19154 produced from a single newline, which is only
19155 possible if that newline came from the same string
19156 as the one which produced this ROW. */
19157 seen_this_string = 1;
19158 }
19159 else
19160 {
19161 end = r1->glyphs[TEXT_AREA] - 1;
19162 start = end + r1->used[TEXT_AREA];
19163 while (end < start
19164 && INTEGERP ((end + 1)->object)
19165 && (end + 1)->charpos <= 0)
19166 ++end;
19167 if (end < start)
19168 {
19169 if (EQ ((end + 1)->object, it->object))
19170 seen_this_string = 1;
19171 }
19172 else
19173 seen_this_string = 1;
19174 }
19175 }
19176 /* Take note of each display string that covers a newline only
19177 once, the first time we see it. This is for when a display
19178 string includes more than one newline in it. */
19179 if (row->ends_in_newline_from_string_p && !seen_this_string)
19180 {
19181 /* If we were scanning the buffer forward when we displayed
19182 the string, we want to account for at least one buffer
19183 position that belongs to this row (position covered by
19184 the display string), so that cursor positioning will
19185 consider this row as a candidate when point is at the end
19186 of the visual line represented by this row. This is not
19187 required when scanning back, because max_pos will already
19188 have a much larger value. */
19189 if (CHARPOS (row->end.pos) > max_pos)
19190 INC_BOTH (max_pos, max_bpos);
19191 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19192 }
19193 else if (CHARPOS (it->eol_pos) > 0)
19194 SET_TEXT_POS (row->maxpos,
19195 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19196 else if (row->continued_p)
19197 {
19198 /* If max_pos is different from IT's current position, it
19199 means IT->method does not belong to the display element
19200 at max_pos. However, it also means that the display
19201 element at max_pos was displayed in its entirety on this
19202 line, which is equivalent to saying that the next line
19203 starts at the next buffer position. */
19204 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19205 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19206 else
19207 {
19208 INC_BOTH (max_pos, max_bpos);
19209 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19210 }
19211 }
19212 else if (row->truncated_on_right_p)
19213 /* display_line already called reseat_at_next_visible_line_start,
19214 which puts the iterator at the beginning of the next line, in
19215 the logical order. */
19216 row->maxpos = it->current.pos;
19217 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19218 /* A line that is entirely from a string/image/stretch... */
19219 row->maxpos = row->minpos;
19220 else
19221 emacs_abort ();
19222 }
19223 else
19224 row->maxpos = it->current.pos;
19225 }
19226
19227 /* Construct the glyph row IT->glyph_row in the desired matrix of
19228 IT->w from text at the current position of IT. See dispextern.h
19229 for an overview of struct it. Value is non-zero if
19230 IT->glyph_row displays text, as opposed to a line displaying ZV
19231 only. */
19232
19233 static int
19234 display_line (struct it *it)
19235 {
19236 struct glyph_row *row = it->glyph_row;
19237 Lisp_Object overlay_arrow_string;
19238 struct it wrap_it;
19239 void *wrap_data = NULL;
19240 int may_wrap = 0, wrap_x IF_LINT (= 0);
19241 int wrap_row_used = -1;
19242 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19243 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19244 int wrap_row_extra_line_spacing IF_LINT (= 0);
19245 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19246 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19247 int cvpos;
19248 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19249 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19250
19251 /* We always start displaying at hpos zero even if hscrolled. */
19252 eassert (it->hpos == 0 && it->current_x == 0);
19253
19254 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19255 >= it->w->desired_matrix->nrows)
19256 {
19257 it->w->nrows_scale_factor++;
19258 it->f->fonts_changed = 1;
19259 return 0;
19260 }
19261
19262 /* Is IT->w showing the region? */
19263 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19264
19265 /* Clear the result glyph row and enable it. */
19266 prepare_desired_row (row);
19267
19268 row->y = it->current_y;
19269 row->start = it->start;
19270 row->continuation_lines_width = it->continuation_lines_width;
19271 row->displays_text_p = 1;
19272 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19273 it->starts_in_middle_of_char_p = 0;
19274
19275 /* Arrange the overlays nicely for our purposes. Usually, we call
19276 display_line on only one line at a time, in which case this
19277 can't really hurt too much, or we call it on lines which appear
19278 one after another in the buffer, in which case all calls to
19279 recenter_overlay_lists but the first will be pretty cheap. */
19280 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19281
19282 /* Move over display elements that are not visible because we are
19283 hscrolled. This may stop at an x-position < IT->first_visible_x
19284 if the first glyph is partially visible or if we hit a line end. */
19285 if (it->current_x < it->first_visible_x)
19286 {
19287 enum move_it_result move_result;
19288
19289 this_line_min_pos = row->start.pos;
19290 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19291 MOVE_TO_POS | MOVE_TO_X);
19292 /* If we are under a large hscroll, move_it_in_display_line_to
19293 could hit the end of the line without reaching
19294 it->first_visible_x. Pretend that we did reach it. This is
19295 especially important on a TTY, where we will call
19296 extend_face_to_end_of_line, which needs to know how many
19297 blank glyphs to produce. */
19298 if (it->current_x < it->first_visible_x
19299 && (move_result == MOVE_NEWLINE_OR_CR
19300 || move_result == MOVE_POS_MATCH_OR_ZV))
19301 it->current_x = it->first_visible_x;
19302
19303 /* Record the smallest positions seen while we moved over
19304 display elements that are not visible. This is needed by
19305 redisplay_internal for optimizing the case where the cursor
19306 stays inside the same line. The rest of this function only
19307 considers positions that are actually displayed, so
19308 RECORD_MAX_MIN_POS will not otherwise record positions that
19309 are hscrolled to the left of the left edge of the window. */
19310 min_pos = CHARPOS (this_line_min_pos);
19311 min_bpos = BYTEPOS (this_line_min_pos);
19312 }
19313 else
19314 {
19315 /* We only do this when not calling `move_it_in_display_line_to'
19316 above, because move_it_in_display_line_to calls
19317 handle_line_prefix itself. */
19318 handle_line_prefix (it);
19319 }
19320
19321 /* Get the initial row height. This is either the height of the
19322 text hscrolled, if there is any, or zero. */
19323 row->ascent = it->max_ascent;
19324 row->height = it->max_ascent + it->max_descent;
19325 row->phys_ascent = it->max_phys_ascent;
19326 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19327 row->extra_line_spacing = it->max_extra_line_spacing;
19328
19329 /* Utility macro to record max and min buffer positions seen until now. */
19330 #define RECORD_MAX_MIN_POS(IT) \
19331 do \
19332 { \
19333 int composition_p = !STRINGP ((IT)->string) \
19334 && ((IT)->what == IT_COMPOSITION); \
19335 ptrdiff_t current_pos = \
19336 composition_p ? (IT)->cmp_it.charpos \
19337 : IT_CHARPOS (*(IT)); \
19338 ptrdiff_t current_bpos = \
19339 composition_p ? CHAR_TO_BYTE (current_pos) \
19340 : IT_BYTEPOS (*(IT)); \
19341 if (current_pos < min_pos) \
19342 { \
19343 min_pos = current_pos; \
19344 min_bpos = current_bpos; \
19345 } \
19346 if (IT_CHARPOS (*it) > max_pos) \
19347 { \
19348 max_pos = IT_CHARPOS (*it); \
19349 max_bpos = IT_BYTEPOS (*it); \
19350 } \
19351 } \
19352 while (0)
19353
19354 /* Loop generating characters. The loop is left with IT on the next
19355 character to display. */
19356 while (1)
19357 {
19358 int n_glyphs_before, hpos_before, x_before;
19359 int x, nglyphs;
19360 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19361
19362 /* Retrieve the next thing to display. Value is zero if end of
19363 buffer reached. */
19364 if (!get_next_display_element (it))
19365 {
19366 /* Maybe add a space at the end of this line that is used to
19367 display the cursor there under X. Set the charpos of the
19368 first glyph of blank lines not corresponding to any text
19369 to -1. */
19370 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19371 row->exact_window_width_line_p = 1;
19372 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19373 || row->used[TEXT_AREA] == 0)
19374 {
19375 row->glyphs[TEXT_AREA]->charpos = -1;
19376 row->displays_text_p = 0;
19377
19378 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19379 && (!MINI_WINDOW_P (it->w)
19380 || (minibuf_level && EQ (it->window, minibuf_window))))
19381 row->indicate_empty_line_p = 1;
19382 }
19383
19384 it->continuation_lines_width = 0;
19385 row->ends_at_zv_p = 1;
19386 /* A row that displays right-to-left text must always have
19387 its last face extended all the way to the end of line,
19388 even if this row ends in ZV, because we still write to
19389 the screen left to right. We also need to extend the
19390 last face if the default face is remapped to some
19391 different face, otherwise the functions that clear
19392 portions of the screen will clear with the default face's
19393 background color. */
19394 if (row->reversed_p
19395 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19396 extend_face_to_end_of_line (it);
19397 break;
19398 }
19399
19400 /* Now, get the metrics of what we want to display. This also
19401 generates glyphs in `row' (which is IT->glyph_row). */
19402 n_glyphs_before = row->used[TEXT_AREA];
19403 x = it->current_x;
19404
19405 /* Remember the line height so far in case the next element doesn't
19406 fit on the line. */
19407 if (it->line_wrap != TRUNCATE)
19408 {
19409 ascent = it->max_ascent;
19410 descent = it->max_descent;
19411 phys_ascent = it->max_phys_ascent;
19412 phys_descent = it->max_phys_descent;
19413
19414 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19415 {
19416 if (IT_DISPLAYING_WHITESPACE (it))
19417 may_wrap = 1;
19418 else if (may_wrap)
19419 {
19420 SAVE_IT (wrap_it, *it, wrap_data);
19421 wrap_x = x;
19422 wrap_row_used = row->used[TEXT_AREA];
19423 wrap_row_ascent = row->ascent;
19424 wrap_row_height = row->height;
19425 wrap_row_phys_ascent = row->phys_ascent;
19426 wrap_row_phys_height = row->phys_height;
19427 wrap_row_extra_line_spacing = row->extra_line_spacing;
19428 wrap_row_min_pos = min_pos;
19429 wrap_row_min_bpos = min_bpos;
19430 wrap_row_max_pos = max_pos;
19431 wrap_row_max_bpos = max_bpos;
19432 may_wrap = 0;
19433 }
19434 }
19435 }
19436
19437 PRODUCE_GLYPHS (it);
19438
19439 /* If this display element was in marginal areas, continue with
19440 the next one. */
19441 if (it->area != TEXT_AREA)
19442 {
19443 row->ascent = max (row->ascent, it->max_ascent);
19444 row->height = max (row->height, it->max_ascent + it->max_descent);
19445 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19446 row->phys_height = max (row->phys_height,
19447 it->max_phys_ascent + it->max_phys_descent);
19448 row->extra_line_spacing = max (row->extra_line_spacing,
19449 it->max_extra_line_spacing);
19450 set_iterator_to_next (it, 1);
19451 continue;
19452 }
19453
19454 /* Does the display element fit on the line? If we truncate
19455 lines, we should draw past the right edge of the window. If
19456 we don't truncate, we want to stop so that we can display the
19457 continuation glyph before the right margin. If lines are
19458 continued, there are two possible strategies for characters
19459 resulting in more than 1 glyph (e.g. tabs): Display as many
19460 glyphs as possible in this line and leave the rest for the
19461 continuation line, or display the whole element in the next
19462 line. Original redisplay did the former, so we do it also. */
19463 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19464 hpos_before = it->hpos;
19465 x_before = x;
19466
19467 if (/* Not a newline. */
19468 nglyphs > 0
19469 /* Glyphs produced fit entirely in the line. */
19470 && it->current_x < it->last_visible_x)
19471 {
19472 it->hpos += nglyphs;
19473 row->ascent = max (row->ascent, it->max_ascent);
19474 row->height = max (row->height, it->max_ascent + it->max_descent);
19475 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19476 row->phys_height = max (row->phys_height,
19477 it->max_phys_ascent + it->max_phys_descent);
19478 row->extra_line_spacing = max (row->extra_line_spacing,
19479 it->max_extra_line_spacing);
19480 if (it->current_x - it->pixel_width < it->first_visible_x)
19481 row->x = x - it->first_visible_x;
19482 /* Record the maximum and minimum buffer positions seen so
19483 far in glyphs that will be displayed by this row. */
19484 if (it->bidi_p)
19485 RECORD_MAX_MIN_POS (it);
19486 }
19487 else
19488 {
19489 int i, new_x;
19490 struct glyph *glyph;
19491
19492 for (i = 0; i < nglyphs; ++i, x = new_x)
19493 {
19494 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19495 new_x = x + glyph->pixel_width;
19496
19497 if (/* Lines are continued. */
19498 it->line_wrap != TRUNCATE
19499 && (/* Glyph doesn't fit on the line. */
19500 new_x > it->last_visible_x
19501 /* Or it fits exactly on a window system frame. */
19502 || (new_x == it->last_visible_x
19503 && FRAME_WINDOW_P (it->f)
19504 && (row->reversed_p
19505 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19506 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19507 {
19508 /* End of a continued line. */
19509
19510 if (it->hpos == 0
19511 || (new_x == it->last_visible_x
19512 && FRAME_WINDOW_P (it->f)
19513 && (row->reversed_p
19514 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19515 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19516 {
19517 /* Current glyph is the only one on the line or
19518 fits exactly on the line. We must continue
19519 the line because we can't draw the cursor
19520 after the glyph. */
19521 row->continued_p = 1;
19522 it->current_x = new_x;
19523 it->continuation_lines_width += new_x;
19524 ++it->hpos;
19525 if (i == nglyphs - 1)
19526 {
19527 /* If line-wrap is on, check if a previous
19528 wrap point was found. */
19529 if (wrap_row_used > 0
19530 /* Even if there is a previous wrap
19531 point, continue the line here as
19532 usual, if (i) the previous character
19533 was a space or tab AND (ii) the
19534 current character is not. */
19535 && (!may_wrap
19536 || IT_DISPLAYING_WHITESPACE (it)))
19537 goto back_to_wrap;
19538
19539 /* Record the maximum and minimum buffer
19540 positions seen so far in glyphs that will be
19541 displayed by this row. */
19542 if (it->bidi_p)
19543 RECORD_MAX_MIN_POS (it);
19544 set_iterator_to_next (it, 1);
19545 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19546 {
19547 if (!get_next_display_element (it))
19548 {
19549 row->exact_window_width_line_p = 1;
19550 it->continuation_lines_width = 0;
19551 row->continued_p = 0;
19552 row->ends_at_zv_p = 1;
19553 }
19554 else if (ITERATOR_AT_END_OF_LINE_P (it))
19555 {
19556 row->continued_p = 0;
19557 row->exact_window_width_line_p = 1;
19558 }
19559 }
19560 }
19561 else if (it->bidi_p)
19562 RECORD_MAX_MIN_POS (it);
19563 }
19564 else if (CHAR_GLYPH_PADDING_P (*glyph)
19565 && !FRAME_WINDOW_P (it->f))
19566 {
19567 /* A padding glyph that doesn't fit on this line.
19568 This means the whole character doesn't fit
19569 on the line. */
19570 if (row->reversed_p)
19571 unproduce_glyphs (it, row->used[TEXT_AREA]
19572 - n_glyphs_before);
19573 row->used[TEXT_AREA] = n_glyphs_before;
19574
19575 /* Fill the rest of the row with continuation
19576 glyphs like in 20.x. */
19577 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19578 < row->glyphs[1 + TEXT_AREA])
19579 produce_special_glyphs (it, IT_CONTINUATION);
19580
19581 row->continued_p = 1;
19582 it->current_x = x_before;
19583 it->continuation_lines_width += x_before;
19584
19585 /* Restore the height to what it was before the
19586 element not fitting on the line. */
19587 it->max_ascent = ascent;
19588 it->max_descent = descent;
19589 it->max_phys_ascent = phys_ascent;
19590 it->max_phys_descent = phys_descent;
19591 }
19592 else if (wrap_row_used > 0)
19593 {
19594 back_to_wrap:
19595 if (row->reversed_p)
19596 unproduce_glyphs (it,
19597 row->used[TEXT_AREA] - wrap_row_used);
19598 RESTORE_IT (it, &wrap_it, wrap_data);
19599 it->continuation_lines_width += wrap_x;
19600 row->used[TEXT_AREA] = wrap_row_used;
19601 row->ascent = wrap_row_ascent;
19602 row->height = wrap_row_height;
19603 row->phys_ascent = wrap_row_phys_ascent;
19604 row->phys_height = wrap_row_phys_height;
19605 row->extra_line_spacing = wrap_row_extra_line_spacing;
19606 min_pos = wrap_row_min_pos;
19607 min_bpos = wrap_row_min_bpos;
19608 max_pos = wrap_row_max_pos;
19609 max_bpos = wrap_row_max_bpos;
19610 row->continued_p = 1;
19611 row->ends_at_zv_p = 0;
19612 row->exact_window_width_line_p = 0;
19613 it->continuation_lines_width += x;
19614
19615 /* Make sure that a non-default face is extended
19616 up to the right margin of the window. */
19617 extend_face_to_end_of_line (it);
19618 }
19619 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19620 {
19621 /* A TAB that extends past the right edge of the
19622 window. This produces a single glyph on
19623 window system frames. We leave the glyph in
19624 this row and let it fill the row, but don't
19625 consume the TAB. */
19626 if ((row->reversed_p
19627 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19628 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19629 produce_special_glyphs (it, IT_CONTINUATION);
19630 it->continuation_lines_width += it->last_visible_x;
19631 row->ends_in_middle_of_char_p = 1;
19632 row->continued_p = 1;
19633 glyph->pixel_width = it->last_visible_x - x;
19634 it->starts_in_middle_of_char_p = 1;
19635 }
19636 else
19637 {
19638 /* Something other than a TAB that draws past
19639 the right edge of the window. Restore
19640 positions to values before the element. */
19641 if (row->reversed_p)
19642 unproduce_glyphs (it, row->used[TEXT_AREA]
19643 - (n_glyphs_before + i));
19644 row->used[TEXT_AREA] = n_glyphs_before + i;
19645
19646 /* Display continuation glyphs. */
19647 it->current_x = x_before;
19648 it->continuation_lines_width += x;
19649 if (!FRAME_WINDOW_P (it->f)
19650 || (row->reversed_p
19651 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19652 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19653 produce_special_glyphs (it, IT_CONTINUATION);
19654 row->continued_p = 1;
19655
19656 extend_face_to_end_of_line (it);
19657
19658 if (nglyphs > 1 && i > 0)
19659 {
19660 row->ends_in_middle_of_char_p = 1;
19661 it->starts_in_middle_of_char_p = 1;
19662 }
19663
19664 /* Restore the height to what it was before the
19665 element not fitting on the line. */
19666 it->max_ascent = ascent;
19667 it->max_descent = descent;
19668 it->max_phys_ascent = phys_ascent;
19669 it->max_phys_descent = phys_descent;
19670 }
19671
19672 break;
19673 }
19674 else if (new_x > it->first_visible_x)
19675 {
19676 /* Increment number of glyphs actually displayed. */
19677 ++it->hpos;
19678
19679 /* Record the maximum and minimum buffer positions
19680 seen so far in glyphs that will be displayed by
19681 this row. */
19682 if (it->bidi_p)
19683 RECORD_MAX_MIN_POS (it);
19684
19685 if (x < it->first_visible_x)
19686 /* Glyph is partially visible, i.e. row starts at
19687 negative X position. */
19688 row->x = x - it->first_visible_x;
19689 }
19690 else
19691 {
19692 /* Glyph is completely off the left margin of the
19693 window. This should not happen because of the
19694 move_it_in_display_line at the start of this
19695 function, unless the text display area of the
19696 window is empty. */
19697 eassert (it->first_visible_x <= it->last_visible_x);
19698 }
19699 }
19700 /* Even if this display element produced no glyphs at all,
19701 we want to record its position. */
19702 if (it->bidi_p && nglyphs == 0)
19703 RECORD_MAX_MIN_POS (it);
19704
19705 row->ascent = max (row->ascent, it->max_ascent);
19706 row->height = max (row->height, it->max_ascent + it->max_descent);
19707 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19708 row->phys_height = max (row->phys_height,
19709 it->max_phys_ascent + it->max_phys_descent);
19710 row->extra_line_spacing = max (row->extra_line_spacing,
19711 it->max_extra_line_spacing);
19712
19713 /* End of this display line if row is continued. */
19714 if (row->continued_p || row->ends_at_zv_p)
19715 break;
19716 }
19717
19718 at_end_of_line:
19719 /* Is this a line end? If yes, we're also done, after making
19720 sure that a non-default face is extended up to the right
19721 margin of the window. */
19722 if (ITERATOR_AT_END_OF_LINE_P (it))
19723 {
19724 int used_before = row->used[TEXT_AREA];
19725
19726 row->ends_in_newline_from_string_p = STRINGP (it->object);
19727
19728 /* Add a space at the end of the line that is used to
19729 display the cursor there. */
19730 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19731 append_space_for_newline (it, 0);
19732
19733 /* Extend the face to the end of the line. */
19734 extend_face_to_end_of_line (it);
19735
19736 /* Make sure we have the position. */
19737 if (used_before == 0)
19738 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19739
19740 /* Record the position of the newline, for use in
19741 find_row_edges. */
19742 it->eol_pos = it->current.pos;
19743
19744 /* Consume the line end. This skips over invisible lines. */
19745 set_iterator_to_next (it, 1);
19746 it->continuation_lines_width = 0;
19747 break;
19748 }
19749
19750 /* Proceed with next display element. Note that this skips
19751 over lines invisible because of selective display. */
19752 set_iterator_to_next (it, 1);
19753
19754 /* If we truncate lines, we are done when the last displayed
19755 glyphs reach past the right margin of the window. */
19756 if (it->line_wrap == TRUNCATE
19757 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19758 ? (it->current_x >= it->last_visible_x)
19759 : (it->current_x > it->last_visible_x)))
19760 {
19761 /* Maybe add truncation glyphs. */
19762 if (!FRAME_WINDOW_P (it->f)
19763 || (row->reversed_p
19764 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19765 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19766 {
19767 int i, n;
19768
19769 if (!row->reversed_p)
19770 {
19771 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19772 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19773 break;
19774 }
19775 else
19776 {
19777 for (i = 0; i < row->used[TEXT_AREA]; i++)
19778 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19779 break;
19780 /* Remove any padding glyphs at the front of ROW, to
19781 make room for the truncation glyphs we will be
19782 adding below. The loop below always inserts at
19783 least one truncation glyph, so also remove the
19784 last glyph added to ROW. */
19785 unproduce_glyphs (it, i + 1);
19786 /* Adjust i for the loop below. */
19787 i = row->used[TEXT_AREA] - (i + 1);
19788 }
19789
19790 it->current_x = x_before;
19791 if (!FRAME_WINDOW_P (it->f))
19792 {
19793 for (n = row->used[TEXT_AREA]; i < n; ++i)
19794 {
19795 row->used[TEXT_AREA] = i;
19796 produce_special_glyphs (it, IT_TRUNCATION);
19797 }
19798 }
19799 else
19800 {
19801 row->used[TEXT_AREA] = i;
19802 produce_special_glyphs (it, IT_TRUNCATION);
19803 }
19804 }
19805 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19806 {
19807 /* Don't truncate if we can overflow newline into fringe. */
19808 if (!get_next_display_element (it))
19809 {
19810 it->continuation_lines_width = 0;
19811 row->ends_at_zv_p = 1;
19812 row->exact_window_width_line_p = 1;
19813 break;
19814 }
19815 if (ITERATOR_AT_END_OF_LINE_P (it))
19816 {
19817 row->exact_window_width_line_p = 1;
19818 goto at_end_of_line;
19819 }
19820 it->current_x = x_before;
19821 }
19822
19823 row->truncated_on_right_p = 1;
19824 it->continuation_lines_width = 0;
19825 reseat_at_next_visible_line_start (it, 0);
19826 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19827 it->hpos = hpos_before;
19828 break;
19829 }
19830 }
19831
19832 if (wrap_data)
19833 bidi_unshelve_cache (wrap_data, 1);
19834
19835 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19836 at the left window margin. */
19837 if (it->first_visible_x
19838 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19839 {
19840 if (!FRAME_WINDOW_P (it->f)
19841 || (row->reversed_p
19842 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19843 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19844 insert_left_trunc_glyphs (it);
19845 row->truncated_on_left_p = 1;
19846 }
19847
19848 /* Remember the position at which this line ends.
19849
19850 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19851 cannot be before the call to find_row_edges below, since that is
19852 where these positions are determined. */
19853 row->end = it->current;
19854 if (!it->bidi_p)
19855 {
19856 row->minpos = row->start.pos;
19857 row->maxpos = row->end.pos;
19858 }
19859 else
19860 {
19861 /* ROW->minpos and ROW->maxpos must be the smallest and
19862 `1 + the largest' buffer positions in ROW. But if ROW was
19863 bidi-reordered, these two positions can be anywhere in the
19864 row, so we must determine them now. */
19865 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19866 }
19867
19868 /* If the start of this line is the overlay arrow-position, then
19869 mark this glyph row as the one containing the overlay arrow.
19870 This is clearly a mess with variable size fonts. It would be
19871 better to let it be displayed like cursors under X. */
19872 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19873 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19874 !NILP (overlay_arrow_string)))
19875 {
19876 /* Overlay arrow in window redisplay is a fringe bitmap. */
19877 if (STRINGP (overlay_arrow_string))
19878 {
19879 struct glyph_row *arrow_row
19880 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19881 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19882 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19883 struct glyph *p = row->glyphs[TEXT_AREA];
19884 struct glyph *p2, *end;
19885
19886 /* Copy the arrow glyphs. */
19887 while (glyph < arrow_end)
19888 *p++ = *glyph++;
19889
19890 /* Throw away padding glyphs. */
19891 p2 = p;
19892 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19893 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19894 ++p2;
19895 if (p2 > p)
19896 {
19897 while (p2 < end)
19898 *p++ = *p2++;
19899 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19900 }
19901 }
19902 else
19903 {
19904 eassert (INTEGERP (overlay_arrow_string));
19905 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19906 }
19907 overlay_arrow_seen = 1;
19908 }
19909
19910 /* Highlight trailing whitespace. */
19911 if (!NILP (Vshow_trailing_whitespace))
19912 highlight_trailing_whitespace (it->f, it->glyph_row);
19913
19914 /* Compute pixel dimensions of this line. */
19915 compute_line_metrics (it);
19916
19917 /* Implementation note: No changes in the glyphs of ROW or in their
19918 faces can be done past this point, because compute_line_metrics
19919 computes ROW's hash value and stores it within the glyph_row
19920 structure. */
19921
19922 /* Record whether this row ends inside an ellipsis. */
19923 row->ends_in_ellipsis_p
19924 = (it->method == GET_FROM_DISPLAY_VECTOR
19925 && it->ellipsis_p);
19926
19927 /* Save fringe bitmaps in this row. */
19928 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19929 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19930 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19931 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19932
19933 it->left_user_fringe_bitmap = 0;
19934 it->left_user_fringe_face_id = 0;
19935 it->right_user_fringe_bitmap = 0;
19936 it->right_user_fringe_face_id = 0;
19937
19938 /* Maybe set the cursor. */
19939 cvpos = it->w->cursor.vpos;
19940 if ((cvpos < 0
19941 /* In bidi-reordered rows, keep checking for proper cursor
19942 position even if one has been found already, because buffer
19943 positions in such rows change non-linearly with ROW->VPOS,
19944 when a line is continued. One exception: when we are at ZV,
19945 display cursor on the first suitable glyph row, since all
19946 the empty rows after that also have their position set to ZV. */
19947 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19948 lines' rows is implemented for bidi-reordered rows. */
19949 || (it->bidi_p
19950 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19951 && PT >= MATRIX_ROW_START_CHARPOS (row)
19952 && PT <= MATRIX_ROW_END_CHARPOS (row)
19953 && cursor_row_p (row))
19954 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19955
19956 /* Prepare for the next line. This line starts horizontally at (X
19957 HPOS) = (0 0). Vertical positions are incremented. As a
19958 convenience for the caller, IT->glyph_row is set to the next
19959 row to be used. */
19960 it->current_x = it->hpos = 0;
19961 it->current_y += row->height;
19962 SET_TEXT_POS (it->eol_pos, 0, 0);
19963 ++it->vpos;
19964 ++it->glyph_row;
19965 /* The next row should by default use the same value of the
19966 reversed_p flag as this one. set_iterator_to_next decides when
19967 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19968 the flag accordingly. */
19969 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19970 it->glyph_row->reversed_p = row->reversed_p;
19971 it->start = row->end;
19972 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19973
19974 #undef RECORD_MAX_MIN_POS
19975 }
19976
19977 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19978 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19979 doc: /* Return paragraph direction at point in BUFFER.
19980 Value is either `left-to-right' or `right-to-left'.
19981 If BUFFER is omitted or nil, it defaults to the current buffer.
19982
19983 Paragraph direction determines how the text in the paragraph is displayed.
19984 In left-to-right paragraphs, text begins at the left margin of the window
19985 and the reading direction is generally left to right. In right-to-left
19986 paragraphs, text begins at the right margin and is read from right to left.
19987
19988 See also `bidi-paragraph-direction'. */)
19989 (Lisp_Object buffer)
19990 {
19991 struct buffer *buf = current_buffer;
19992 struct buffer *old = buf;
19993
19994 if (! NILP (buffer))
19995 {
19996 CHECK_BUFFER (buffer);
19997 buf = XBUFFER (buffer);
19998 }
19999
20000 if (NILP (BVAR (buf, bidi_display_reordering))
20001 || NILP (BVAR (buf, enable_multibyte_characters))
20002 /* When we are loading loadup.el, the character property tables
20003 needed for bidi iteration are not yet available. */
20004 || !NILP (Vpurify_flag))
20005 return Qleft_to_right;
20006 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20007 return BVAR (buf, bidi_paragraph_direction);
20008 else
20009 {
20010 /* Determine the direction from buffer text. We could try to
20011 use current_matrix if it is up to date, but this seems fast
20012 enough as it is. */
20013 struct bidi_it itb;
20014 ptrdiff_t pos = BUF_PT (buf);
20015 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20016 int c;
20017 void *itb_data = bidi_shelve_cache ();
20018
20019 set_buffer_temp (buf);
20020 /* bidi_paragraph_init finds the base direction of the paragraph
20021 by searching forward from paragraph start. We need the base
20022 direction of the current or _previous_ paragraph, so we need
20023 to make sure we are within that paragraph. To that end, find
20024 the previous non-empty line. */
20025 if (pos >= ZV && pos > BEGV)
20026 DEC_BOTH (pos, bytepos);
20027 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20028 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20029 {
20030 while ((c = FETCH_BYTE (bytepos)) == '\n'
20031 || c == ' ' || c == '\t' || c == '\f')
20032 {
20033 if (bytepos <= BEGV_BYTE)
20034 break;
20035 bytepos--;
20036 pos--;
20037 }
20038 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20039 bytepos--;
20040 }
20041 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20042 itb.paragraph_dir = NEUTRAL_DIR;
20043 itb.string.s = NULL;
20044 itb.string.lstring = Qnil;
20045 itb.string.bufpos = 0;
20046 itb.string.unibyte = 0;
20047 /* We have no window to use here for ignoring window-specific
20048 overlays. Using NULL for window pointer will cause
20049 compute_display_string_pos to use the current buffer. */
20050 itb.w = NULL;
20051 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20052 bidi_unshelve_cache (itb_data, 0);
20053 set_buffer_temp (old);
20054 switch (itb.paragraph_dir)
20055 {
20056 case L2R:
20057 return Qleft_to_right;
20058 break;
20059 case R2L:
20060 return Qright_to_left;
20061 break;
20062 default:
20063 emacs_abort ();
20064 }
20065 }
20066 }
20067
20068 DEFUN ("move-point-visually", Fmove_point_visually,
20069 Smove_point_visually, 1, 1, 0,
20070 doc: /* Move point in the visual order in the specified DIRECTION.
20071 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20072 left.
20073
20074 Value is the new character position of point. */)
20075 (Lisp_Object direction)
20076 {
20077 struct window *w = XWINDOW (selected_window);
20078 struct buffer *b = XBUFFER (w->contents);
20079 struct glyph_row *row;
20080 int dir;
20081 Lisp_Object paragraph_dir;
20082
20083 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20084 (!(ROW)->continued_p \
20085 && INTEGERP ((GLYPH)->object) \
20086 && (GLYPH)->type == CHAR_GLYPH \
20087 && (GLYPH)->u.ch == ' ' \
20088 && (GLYPH)->charpos >= 0 \
20089 && !(GLYPH)->avoid_cursor_p)
20090
20091 CHECK_NUMBER (direction);
20092 dir = XINT (direction);
20093 if (dir > 0)
20094 dir = 1;
20095 else
20096 dir = -1;
20097
20098 /* If current matrix is up-to-date, we can use the information
20099 recorded in the glyphs, at least as long as the goal is on the
20100 screen. */
20101 if (w->window_end_valid
20102 && !windows_or_buffers_changed
20103 && b
20104 && !b->clip_changed
20105 && !b->prevent_redisplay_optimizations_p
20106 && !window_outdated (w)
20107 && w->cursor.vpos >= 0
20108 && w->cursor.vpos < w->current_matrix->nrows
20109 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20110 {
20111 struct glyph *g = row->glyphs[TEXT_AREA];
20112 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20113 struct glyph *gpt = g + w->cursor.hpos;
20114
20115 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20116 {
20117 if (BUFFERP (g->object) && g->charpos != PT)
20118 {
20119 SET_PT (g->charpos);
20120 w->cursor.vpos = -1;
20121 return make_number (PT);
20122 }
20123 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20124 {
20125 ptrdiff_t new_pos;
20126
20127 if (BUFFERP (gpt->object))
20128 {
20129 new_pos = PT;
20130 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20131 new_pos += (row->reversed_p ? -dir : dir);
20132 else
20133 new_pos -= (row->reversed_p ? -dir : dir);;
20134 }
20135 else if (BUFFERP (g->object))
20136 new_pos = g->charpos;
20137 else
20138 break;
20139 SET_PT (new_pos);
20140 w->cursor.vpos = -1;
20141 return make_number (PT);
20142 }
20143 else if (ROW_GLYPH_NEWLINE_P (row, g))
20144 {
20145 /* Glyphs inserted at the end of a non-empty line for
20146 positioning the cursor have zero charpos, so we must
20147 deduce the value of point by other means. */
20148 if (g->charpos > 0)
20149 SET_PT (g->charpos);
20150 else if (row->ends_at_zv_p && PT != ZV)
20151 SET_PT (ZV);
20152 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20153 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20154 else
20155 break;
20156 w->cursor.vpos = -1;
20157 return make_number (PT);
20158 }
20159 }
20160 if (g == e || INTEGERP (g->object))
20161 {
20162 if (row->truncated_on_left_p || row->truncated_on_right_p)
20163 goto simulate_display;
20164 if (!row->reversed_p)
20165 row += dir;
20166 else
20167 row -= dir;
20168 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20169 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20170 goto simulate_display;
20171
20172 if (dir > 0)
20173 {
20174 if (row->reversed_p && !row->continued_p)
20175 {
20176 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20177 w->cursor.vpos = -1;
20178 return make_number (PT);
20179 }
20180 g = row->glyphs[TEXT_AREA];
20181 e = g + row->used[TEXT_AREA];
20182 for ( ; g < e; g++)
20183 {
20184 if (BUFFERP (g->object)
20185 /* Empty lines have only one glyph, which stands
20186 for the newline, and whose charpos is the
20187 buffer position of the newline. */
20188 || ROW_GLYPH_NEWLINE_P (row, g)
20189 /* When the buffer ends in a newline, the line at
20190 EOB also has one glyph, but its charpos is -1. */
20191 || (row->ends_at_zv_p
20192 && !row->reversed_p
20193 && INTEGERP (g->object)
20194 && g->type == CHAR_GLYPH
20195 && g->u.ch == ' '))
20196 {
20197 if (g->charpos > 0)
20198 SET_PT (g->charpos);
20199 else if (!row->reversed_p
20200 && row->ends_at_zv_p
20201 && PT != ZV)
20202 SET_PT (ZV);
20203 else
20204 continue;
20205 w->cursor.vpos = -1;
20206 return make_number (PT);
20207 }
20208 }
20209 }
20210 else
20211 {
20212 if (!row->reversed_p && !row->continued_p)
20213 {
20214 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20215 w->cursor.vpos = -1;
20216 return make_number (PT);
20217 }
20218 e = row->glyphs[TEXT_AREA];
20219 g = e + row->used[TEXT_AREA] - 1;
20220 for ( ; g >= e; g--)
20221 {
20222 if (BUFFERP (g->object)
20223 || (ROW_GLYPH_NEWLINE_P (row, g)
20224 && g->charpos > 0)
20225 /* Empty R2L lines on GUI frames have the buffer
20226 position of the newline stored in the stretch
20227 glyph. */
20228 || g->type == STRETCH_GLYPH
20229 || (row->ends_at_zv_p
20230 && row->reversed_p
20231 && INTEGERP (g->object)
20232 && g->type == CHAR_GLYPH
20233 && g->u.ch == ' '))
20234 {
20235 if (g->charpos > 0)
20236 SET_PT (g->charpos);
20237 else if (row->reversed_p
20238 && row->ends_at_zv_p
20239 && PT != ZV)
20240 SET_PT (ZV);
20241 else
20242 continue;
20243 w->cursor.vpos = -1;
20244 return make_number (PT);
20245 }
20246 }
20247 }
20248 }
20249 }
20250
20251 simulate_display:
20252
20253 /* If we wind up here, we failed to move by using the glyphs, so we
20254 need to simulate display instead. */
20255
20256 if (b)
20257 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20258 else
20259 paragraph_dir = Qleft_to_right;
20260 if (EQ (paragraph_dir, Qright_to_left))
20261 dir = -dir;
20262 if (PT <= BEGV && dir < 0)
20263 xsignal0 (Qbeginning_of_buffer);
20264 else if (PT >= ZV && dir > 0)
20265 xsignal0 (Qend_of_buffer);
20266 else
20267 {
20268 struct text_pos pt;
20269 struct it it;
20270 int pt_x, target_x, pixel_width, pt_vpos;
20271 bool at_eol_p;
20272 bool overshoot_expected = false;
20273 bool target_is_eol_p = false;
20274
20275 /* Setup the arena. */
20276 SET_TEXT_POS (pt, PT, PT_BYTE);
20277 start_display (&it, w, pt);
20278
20279 if (it.cmp_it.id < 0
20280 && it.method == GET_FROM_STRING
20281 && it.area == TEXT_AREA
20282 && it.string_from_display_prop_p
20283 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20284 overshoot_expected = true;
20285
20286 /* Find the X coordinate of point. We start from the beginning
20287 of this or previous line to make sure we are before point in
20288 the logical order (since the move_it_* functions can only
20289 move forward). */
20290 reseat_at_previous_visible_line_start (&it);
20291 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20292 if (IT_CHARPOS (it) != PT)
20293 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20294 -1, -1, -1, MOVE_TO_POS);
20295 pt_x = it.current_x;
20296 pt_vpos = it.vpos;
20297 if (dir > 0 || overshoot_expected)
20298 {
20299 struct glyph_row *row = it.glyph_row;
20300
20301 /* When point is at beginning of line, we don't have
20302 information about the glyph there loaded into struct
20303 it. Calling get_next_display_element fixes that. */
20304 if (pt_x == 0)
20305 get_next_display_element (&it);
20306 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20307 it.glyph_row = NULL;
20308 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20309 it.glyph_row = row;
20310 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20311 it, lest it will become out of sync with it's buffer
20312 position. */
20313 it.current_x = pt_x;
20314 }
20315 else
20316 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20317 pixel_width = it.pixel_width;
20318 if (overshoot_expected && at_eol_p)
20319 pixel_width = 0;
20320 else if (pixel_width <= 0)
20321 pixel_width = 1;
20322
20323 /* If there's a display string at point, we are actually at the
20324 glyph to the left of point, so we need to correct the X
20325 coordinate. */
20326 if (overshoot_expected)
20327 pt_x += pixel_width;
20328
20329 /* Compute target X coordinate, either to the left or to the
20330 right of point. On TTY frames, all characters have the same
20331 pixel width of 1, so we can use that. On GUI frames we don't
20332 have an easy way of getting at the pixel width of the
20333 character to the left of point, so we use a different method
20334 of getting to that place. */
20335 if (dir > 0)
20336 target_x = pt_x + pixel_width;
20337 else
20338 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20339
20340 /* Target X coordinate could be one line above or below the line
20341 of point, in which case we need to adjust the target X
20342 coordinate. Also, if moving to the left, we need to begin at
20343 the left edge of the point's screen line. */
20344 if (dir < 0)
20345 {
20346 if (pt_x > 0)
20347 {
20348 start_display (&it, w, pt);
20349 reseat_at_previous_visible_line_start (&it);
20350 it.current_x = it.current_y = it.hpos = 0;
20351 if (pt_vpos != 0)
20352 move_it_by_lines (&it, pt_vpos);
20353 }
20354 else
20355 {
20356 move_it_by_lines (&it, -1);
20357 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20358 target_is_eol_p = true;
20359 }
20360 }
20361 else
20362 {
20363 if (at_eol_p
20364 || (target_x >= it.last_visible_x
20365 && it.line_wrap != TRUNCATE))
20366 {
20367 if (pt_x > 0)
20368 move_it_by_lines (&it, 0);
20369 move_it_by_lines (&it, 1);
20370 target_x = 0;
20371 }
20372 }
20373
20374 /* Move to the target X coordinate. */
20375 #ifdef HAVE_WINDOW_SYSTEM
20376 /* On GUI frames, as we don't know the X coordinate of the
20377 character to the left of point, moving point to the left
20378 requires walking, one grapheme cluster at a time, until we
20379 find ourself at a place immediately to the left of the
20380 character at point. */
20381 if (FRAME_WINDOW_P (it.f) && dir < 0)
20382 {
20383 struct text_pos new_pos = it.current.pos;
20384 enum move_it_result rc = MOVE_X_REACHED;
20385
20386 while (it.current_x + it.pixel_width <= target_x
20387 && rc == MOVE_X_REACHED)
20388 {
20389 int new_x = it.current_x + it.pixel_width;
20390
20391 new_pos = it.current.pos;
20392 if (new_x == it.current_x)
20393 new_x++;
20394 rc = move_it_in_display_line_to (&it, ZV, new_x,
20395 MOVE_TO_POS | MOVE_TO_X);
20396 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20397 break;
20398 }
20399 /* If we ended up on a composed character inside
20400 bidi-reordered text (e.g., Hebrew text with diacritics),
20401 the iterator gives us the buffer position of the last (in
20402 logical order) character of the composed grapheme cluster,
20403 which is not what we want. So we cheat: we compute the
20404 character position of the character that follows (in the
20405 logical order) the one where the above loop stopped. That
20406 character will appear on display to the left of point. */
20407 if (it.bidi_p
20408 && it.bidi_it.scan_dir == -1
20409 && new_pos.charpos - IT_CHARPOS (it) > 1)
20410 {
20411 new_pos.charpos = IT_CHARPOS (it) + 1;
20412 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20413 }
20414 it.current.pos = new_pos;
20415 }
20416 else
20417 #endif
20418 if (it.current_x != target_x)
20419 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20420
20421 /* When lines are truncated, the above loop will stop at the
20422 window edge. But we want to get to the end of line, even if
20423 it is beyond the window edge; automatic hscroll will then
20424 scroll the window to show point as appropriate. */
20425 if (target_is_eol_p && it.line_wrap == TRUNCATE
20426 && get_next_display_element (&it))
20427 {
20428 struct text_pos new_pos = it.current.pos;
20429
20430 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20431 {
20432 set_iterator_to_next (&it, 0);
20433 if (it.method == GET_FROM_BUFFER)
20434 new_pos = it.current.pos;
20435 if (!get_next_display_element (&it))
20436 break;
20437 }
20438
20439 it.current.pos = new_pos;
20440 }
20441
20442 /* If we ended up in a display string that covers point, move to
20443 buffer position to the right in the visual order. */
20444 if (dir > 0)
20445 {
20446 while (IT_CHARPOS (it) == PT)
20447 {
20448 set_iterator_to_next (&it, 0);
20449 if (!get_next_display_element (&it))
20450 break;
20451 }
20452 }
20453
20454 /* Move point to that position. */
20455 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20456 }
20457
20458 return make_number (PT);
20459
20460 #undef ROW_GLYPH_NEWLINE_P
20461 }
20462
20463 \f
20464 /***********************************************************************
20465 Menu Bar
20466 ***********************************************************************/
20467
20468 /* Redisplay the menu bar in the frame for window W.
20469
20470 The menu bar of X frames that don't have X toolkit support is
20471 displayed in a special window W->frame->menu_bar_window.
20472
20473 The menu bar of terminal frames is treated specially as far as
20474 glyph matrices are concerned. Menu bar lines are not part of
20475 windows, so the update is done directly on the frame matrix rows
20476 for the menu bar. */
20477
20478 static void
20479 display_menu_bar (struct window *w)
20480 {
20481 struct frame *f = XFRAME (WINDOW_FRAME (w));
20482 struct it it;
20483 Lisp_Object items;
20484 int i;
20485
20486 /* Don't do all this for graphical frames. */
20487 #ifdef HAVE_NTGUI
20488 if (FRAME_W32_P (f))
20489 return;
20490 #endif
20491 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20492 if (FRAME_X_P (f))
20493 return;
20494 #endif
20495
20496 #ifdef HAVE_NS
20497 if (FRAME_NS_P (f))
20498 return;
20499 #endif /* HAVE_NS */
20500
20501 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20502 eassert (!FRAME_WINDOW_P (f));
20503 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20504 it.first_visible_x = 0;
20505 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20506 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20507 if (FRAME_WINDOW_P (f))
20508 {
20509 /* Menu bar lines are displayed in the desired matrix of the
20510 dummy window menu_bar_window. */
20511 struct window *menu_w;
20512 menu_w = XWINDOW (f->menu_bar_window);
20513 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20514 MENU_FACE_ID);
20515 it.first_visible_x = 0;
20516 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20517 }
20518 else
20519 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20520 {
20521 /* This is a TTY frame, i.e. character hpos/vpos are used as
20522 pixel x/y. */
20523 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20524 MENU_FACE_ID);
20525 it.first_visible_x = 0;
20526 it.last_visible_x = FRAME_COLS (f);
20527 }
20528
20529 /* FIXME: This should be controlled by a user option. See the
20530 comments in redisplay_tool_bar and display_mode_line about
20531 this. */
20532 it.paragraph_embedding = L2R;
20533
20534 /* Clear all rows of the menu bar. */
20535 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20536 {
20537 struct glyph_row *row = it.glyph_row + i;
20538 clear_glyph_row (row);
20539 row->enabled_p = 1;
20540 row->full_width_p = 1;
20541 }
20542
20543 /* Display all items of the menu bar. */
20544 items = FRAME_MENU_BAR_ITEMS (it.f);
20545 for (i = 0; i < ASIZE (items); i += 4)
20546 {
20547 Lisp_Object string;
20548
20549 /* Stop at nil string. */
20550 string = AREF (items, i + 1);
20551 if (NILP (string))
20552 break;
20553
20554 /* Remember where item was displayed. */
20555 ASET (items, i + 3, make_number (it.hpos));
20556
20557 /* Display the item, pad with one space. */
20558 if (it.current_x < it.last_visible_x)
20559 display_string (NULL, string, Qnil, 0, 0, &it,
20560 SCHARS (string) + 1, 0, 0, -1);
20561 }
20562
20563 /* Fill out the line with spaces. */
20564 if (it.current_x < it.last_visible_x)
20565 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20566
20567 /* Compute the total height of the lines. */
20568 compute_line_metrics (&it);
20569 }
20570
20571
20572 \f
20573 /***********************************************************************
20574 Mode Line
20575 ***********************************************************************/
20576
20577 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20578 FORCE is non-zero, redisplay mode lines unconditionally.
20579 Otherwise, redisplay only mode lines that are garbaged. Value is
20580 the number of windows whose mode lines were redisplayed. */
20581
20582 static int
20583 redisplay_mode_lines (Lisp_Object window, int force)
20584 {
20585 int nwindows = 0;
20586
20587 while (!NILP (window))
20588 {
20589 struct window *w = XWINDOW (window);
20590
20591 if (WINDOWP (w->contents))
20592 nwindows += redisplay_mode_lines (w->contents, force);
20593 else if (force
20594 || FRAME_GARBAGED_P (XFRAME (w->frame))
20595 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20596 {
20597 struct text_pos lpoint;
20598 struct buffer *old = current_buffer;
20599
20600 /* Set the window's buffer for the mode line display. */
20601 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20602 set_buffer_internal_1 (XBUFFER (w->contents));
20603
20604 /* Point refers normally to the selected window. For any
20605 other window, set up appropriate value. */
20606 if (!EQ (window, selected_window))
20607 {
20608 struct text_pos pt;
20609
20610 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20611 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20612 }
20613
20614 /* Display mode lines. */
20615 clear_glyph_matrix (w->desired_matrix);
20616 if (display_mode_lines (w))
20617 {
20618 ++nwindows;
20619 w->must_be_updated_p = 1;
20620 }
20621
20622 /* Restore old settings. */
20623 set_buffer_internal_1 (old);
20624 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20625 }
20626
20627 window = w->next;
20628 }
20629
20630 return nwindows;
20631 }
20632
20633
20634 /* Display the mode and/or header line of window W. Value is the
20635 sum number of mode lines and header lines displayed. */
20636
20637 static int
20638 display_mode_lines (struct window *w)
20639 {
20640 Lisp_Object old_selected_window = selected_window;
20641 Lisp_Object old_selected_frame = selected_frame;
20642 Lisp_Object new_frame = w->frame;
20643 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20644 int n = 0;
20645
20646 selected_frame = new_frame;
20647 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20648 or window's point, then we'd need select_window_1 here as well. */
20649 XSETWINDOW (selected_window, w);
20650 XFRAME (new_frame)->selected_window = selected_window;
20651
20652 /* These will be set while the mode line specs are processed. */
20653 line_number_displayed = 0;
20654 w->column_number_displayed = -1;
20655
20656 if (WINDOW_WANTS_MODELINE_P (w))
20657 {
20658 struct window *sel_w = XWINDOW (old_selected_window);
20659
20660 /* Select mode line face based on the real selected window. */
20661 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20662 BVAR (current_buffer, mode_line_format));
20663 ++n;
20664 }
20665
20666 if (WINDOW_WANTS_HEADER_LINE_P (w))
20667 {
20668 display_mode_line (w, HEADER_LINE_FACE_ID,
20669 BVAR (current_buffer, header_line_format));
20670 ++n;
20671 }
20672
20673 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20674 selected_frame = old_selected_frame;
20675 selected_window = old_selected_window;
20676 return n;
20677 }
20678
20679
20680 /* Display mode or header line of window W. FACE_ID specifies which
20681 line to display; it is either MODE_LINE_FACE_ID or
20682 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20683 display. Value is the pixel height of the mode/header line
20684 displayed. */
20685
20686 static int
20687 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20688 {
20689 struct it it;
20690 struct face *face;
20691 ptrdiff_t count = SPECPDL_INDEX ();
20692
20693 init_iterator (&it, w, -1, -1, NULL, face_id);
20694 /* Don't extend on a previously drawn mode-line.
20695 This may happen if called from pos_visible_p. */
20696 it.glyph_row->enabled_p = 0;
20697 prepare_desired_row (it.glyph_row);
20698
20699 it.glyph_row->mode_line_p = 1;
20700
20701 /* FIXME: This should be controlled by a user option. But
20702 supporting such an option is not trivial, since the mode line is
20703 made up of many separate strings. */
20704 it.paragraph_embedding = L2R;
20705
20706 record_unwind_protect (unwind_format_mode_line,
20707 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20708
20709 mode_line_target = MODE_LINE_DISPLAY;
20710
20711 /* Temporarily make frame's keyboard the current kboard so that
20712 kboard-local variables in the mode_line_format will get the right
20713 values. */
20714 push_kboard (FRAME_KBOARD (it.f));
20715 record_unwind_save_match_data ();
20716 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20717 pop_kboard ();
20718
20719 unbind_to (count, Qnil);
20720
20721 /* Fill up with spaces. */
20722 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20723
20724 compute_line_metrics (&it);
20725 it.glyph_row->full_width_p = 1;
20726 it.glyph_row->continued_p = 0;
20727 it.glyph_row->truncated_on_left_p = 0;
20728 it.glyph_row->truncated_on_right_p = 0;
20729
20730 /* Make a 3D mode-line have a shadow at its right end. */
20731 face = FACE_FROM_ID (it.f, face_id);
20732 extend_face_to_end_of_line (&it);
20733 if (face->box != FACE_NO_BOX)
20734 {
20735 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20736 + it.glyph_row->used[TEXT_AREA] - 1);
20737 last->right_box_line_p = 1;
20738 }
20739
20740 return it.glyph_row->height;
20741 }
20742
20743 /* Move element ELT in LIST to the front of LIST.
20744 Return the updated list. */
20745
20746 static Lisp_Object
20747 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20748 {
20749 register Lisp_Object tail, prev;
20750 register Lisp_Object tem;
20751
20752 tail = list;
20753 prev = Qnil;
20754 while (CONSP (tail))
20755 {
20756 tem = XCAR (tail);
20757
20758 if (EQ (elt, tem))
20759 {
20760 /* Splice out the link TAIL. */
20761 if (NILP (prev))
20762 list = XCDR (tail);
20763 else
20764 Fsetcdr (prev, XCDR (tail));
20765
20766 /* Now make it the first. */
20767 Fsetcdr (tail, list);
20768 return tail;
20769 }
20770 else
20771 prev = tail;
20772 tail = XCDR (tail);
20773 QUIT;
20774 }
20775
20776 /* Not found--return unchanged LIST. */
20777 return list;
20778 }
20779
20780 /* Contribute ELT to the mode line for window IT->w. How it
20781 translates into text depends on its data type.
20782
20783 IT describes the display environment in which we display, as usual.
20784
20785 DEPTH is the depth in recursion. It is used to prevent
20786 infinite recursion here.
20787
20788 FIELD_WIDTH is the number of characters the display of ELT should
20789 occupy in the mode line, and PRECISION is the maximum number of
20790 characters to display from ELT's representation. See
20791 display_string for details.
20792
20793 Returns the hpos of the end of the text generated by ELT.
20794
20795 PROPS is a property list to add to any string we encounter.
20796
20797 If RISKY is nonzero, remove (disregard) any properties in any string
20798 we encounter, and ignore :eval and :propertize.
20799
20800 The global variable `mode_line_target' determines whether the
20801 output is passed to `store_mode_line_noprop',
20802 `store_mode_line_string', or `display_string'. */
20803
20804 static int
20805 display_mode_element (struct it *it, int depth, int field_width, int precision,
20806 Lisp_Object elt, Lisp_Object props, int risky)
20807 {
20808 int n = 0, field, prec;
20809 int literal = 0;
20810
20811 tail_recurse:
20812 if (depth > 100)
20813 elt = build_string ("*too-deep*");
20814
20815 depth++;
20816
20817 switch (XTYPE (elt))
20818 {
20819 case Lisp_String:
20820 {
20821 /* A string: output it and check for %-constructs within it. */
20822 unsigned char c;
20823 ptrdiff_t offset = 0;
20824
20825 if (SCHARS (elt) > 0
20826 && (!NILP (props) || risky))
20827 {
20828 Lisp_Object oprops, aelt;
20829 oprops = Ftext_properties_at (make_number (0), elt);
20830
20831 /* If the starting string's properties are not what
20832 we want, translate the string. Also, if the string
20833 is risky, do that anyway. */
20834
20835 if (NILP (Fequal (props, oprops)) || risky)
20836 {
20837 /* If the starting string has properties,
20838 merge the specified ones onto the existing ones. */
20839 if (! NILP (oprops) && !risky)
20840 {
20841 Lisp_Object tem;
20842
20843 oprops = Fcopy_sequence (oprops);
20844 tem = props;
20845 while (CONSP (tem))
20846 {
20847 oprops = Fplist_put (oprops, XCAR (tem),
20848 XCAR (XCDR (tem)));
20849 tem = XCDR (XCDR (tem));
20850 }
20851 props = oprops;
20852 }
20853
20854 aelt = Fassoc (elt, mode_line_proptrans_alist);
20855 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20856 {
20857 /* AELT is what we want. Move it to the front
20858 without consing. */
20859 elt = XCAR (aelt);
20860 mode_line_proptrans_alist
20861 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20862 }
20863 else
20864 {
20865 Lisp_Object tem;
20866
20867 /* If AELT has the wrong props, it is useless.
20868 so get rid of it. */
20869 if (! NILP (aelt))
20870 mode_line_proptrans_alist
20871 = Fdelq (aelt, mode_line_proptrans_alist);
20872
20873 elt = Fcopy_sequence (elt);
20874 Fset_text_properties (make_number (0), Flength (elt),
20875 props, elt);
20876 /* Add this item to mode_line_proptrans_alist. */
20877 mode_line_proptrans_alist
20878 = Fcons (Fcons (elt, props),
20879 mode_line_proptrans_alist);
20880 /* Truncate mode_line_proptrans_alist
20881 to at most 50 elements. */
20882 tem = Fnthcdr (make_number (50),
20883 mode_line_proptrans_alist);
20884 if (! NILP (tem))
20885 XSETCDR (tem, Qnil);
20886 }
20887 }
20888 }
20889
20890 offset = 0;
20891
20892 if (literal)
20893 {
20894 prec = precision - n;
20895 switch (mode_line_target)
20896 {
20897 case MODE_LINE_NOPROP:
20898 case MODE_LINE_TITLE:
20899 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20900 break;
20901 case MODE_LINE_STRING:
20902 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20903 break;
20904 case MODE_LINE_DISPLAY:
20905 n += display_string (NULL, elt, Qnil, 0, 0, it,
20906 0, prec, 0, STRING_MULTIBYTE (elt));
20907 break;
20908 }
20909
20910 break;
20911 }
20912
20913 /* Handle the non-literal case. */
20914
20915 while ((precision <= 0 || n < precision)
20916 && SREF (elt, offset) != 0
20917 && (mode_line_target != MODE_LINE_DISPLAY
20918 || it->current_x < it->last_visible_x))
20919 {
20920 ptrdiff_t last_offset = offset;
20921
20922 /* Advance to end of string or next format specifier. */
20923 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20924 ;
20925
20926 if (offset - 1 != last_offset)
20927 {
20928 ptrdiff_t nchars, nbytes;
20929
20930 /* Output to end of string or up to '%'. Field width
20931 is length of string. Don't output more than
20932 PRECISION allows us. */
20933 offset--;
20934
20935 prec = c_string_width (SDATA (elt) + last_offset,
20936 offset - last_offset, precision - n,
20937 &nchars, &nbytes);
20938
20939 switch (mode_line_target)
20940 {
20941 case MODE_LINE_NOPROP:
20942 case MODE_LINE_TITLE:
20943 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20944 break;
20945 case MODE_LINE_STRING:
20946 {
20947 ptrdiff_t bytepos = last_offset;
20948 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20949 ptrdiff_t endpos = (precision <= 0
20950 ? string_byte_to_char (elt, offset)
20951 : charpos + nchars);
20952
20953 n += store_mode_line_string (NULL,
20954 Fsubstring (elt, make_number (charpos),
20955 make_number (endpos)),
20956 0, 0, 0, Qnil);
20957 }
20958 break;
20959 case MODE_LINE_DISPLAY:
20960 {
20961 ptrdiff_t bytepos = last_offset;
20962 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20963
20964 if (precision <= 0)
20965 nchars = string_byte_to_char (elt, offset) - charpos;
20966 n += display_string (NULL, elt, Qnil, 0, charpos,
20967 it, 0, nchars, 0,
20968 STRING_MULTIBYTE (elt));
20969 }
20970 break;
20971 }
20972 }
20973 else /* c == '%' */
20974 {
20975 ptrdiff_t percent_position = offset;
20976
20977 /* Get the specified minimum width. Zero means
20978 don't pad. */
20979 field = 0;
20980 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20981 field = field * 10 + c - '0';
20982
20983 /* Don't pad beyond the total padding allowed. */
20984 if (field_width - n > 0 && field > field_width - n)
20985 field = field_width - n;
20986
20987 /* Note that either PRECISION <= 0 or N < PRECISION. */
20988 prec = precision - n;
20989
20990 if (c == 'M')
20991 n += display_mode_element (it, depth, field, prec,
20992 Vglobal_mode_string, props,
20993 risky);
20994 else if (c != 0)
20995 {
20996 bool multibyte;
20997 ptrdiff_t bytepos, charpos;
20998 const char *spec;
20999 Lisp_Object string;
21000
21001 bytepos = percent_position;
21002 charpos = (STRING_MULTIBYTE (elt)
21003 ? string_byte_to_char (elt, bytepos)
21004 : bytepos);
21005 spec = decode_mode_spec (it->w, c, field, &string);
21006 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21007
21008 switch (mode_line_target)
21009 {
21010 case MODE_LINE_NOPROP:
21011 case MODE_LINE_TITLE:
21012 n += store_mode_line_noprop (spec, field, prec);
21013 break;
21014 case MODE_LINE_STRING:
21015 {
21016 Lisp_Object tem = build_string (spec);
21017 props = Ftext_properties_at (make_number (charpos), elt);
21018 /* Should only keep face property in props */
21019 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21020 }
21021 break;
21022 case MODE_LINE_DISPLAY:
21023 {
21024 int nglyphs_before, nwritten;
21025
21026 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21027 nwritten = display_string (spec, string, elt,
21028 charpos, 0, it,
21029 field, prec, 0,
21030 multibyte);
21031
21032 /* Assign to the glyphs written above the
21033 string where the `%x' came from, position
21034 of the `%'. */
21035 if (nwritten > 0)
21036 {
21037 struct glyph *glyph
21038 = (it->glyph_row->glyphs[TEXT_AREA]
21039 + nglyphs_before);
21040 int i;
21041
21042 for (i = 0; i < nwritten; ++i)
21043 {
21044 glyph[i].object = elt;
21045 glyph[i].charpos = charpos;
21046 }
21047
21048 n += nwritten;
21049 }
21050 }
21051 break;
21052 }
21053 }
21054 else /* c == 0 */
21055 break;
21056 }
21057 }
21058 }
21059 break;
21060
21061 case Lisp_Symbol:
21062 /* A symbol: process the value of the symbol recursively
21063 as if it appeared here directly. Avoid error if symbol void.
21064 Special case: if value of symbol is a string, output the string
21065 literally. */
21066 {
21067 register Lisp_Object tem;
21068
21069 /* If the variable is not marked as risky to set
21070 then its contents are risky to use. */
21071 if (NILP (Fget (elt, Qrisky_local_variable)))
21072 risky = 1;
21073
21074 tem = Fboundp (elt);
21075 if (!NILP (tem))
21076 {
21077 tem = Fsymbol_value (elt);
21078 /* If value is a string, output that string literally:
21079 don't check for % within it. */
21080 if (STRINGP (tem))
21081 literal = 1;
21082
21083 if (!EQ (tem, elt))
21084 {
21085 /* Give up right away for nil or t. */
21086 elt = tem;
21087 goto tail_recurse;
21088 }
21089 }
21090 }
21091 break;
21092
21093 case Lisp_Cons:
21094 {
21095 register Lisp_Object car, tem;
21096
21097 /* A cons cell: five distinct cases.
21098 If first element is :eval or :propertize, do something special.
21099 If first element is a string or a cons, process all the elements
21100 and effectively concatenate them.
21101 If first element is a negative number, truncate displaying cdr to
21102 at most that many characters. If positive, pad (with spaces)
21103 to at least that many characters.
21104 If first element is a symbol, process the cadr or caddr recursively
21105 according to whether the symbol's value is non-nil or nil. */
21106 car = XCAR (elt);
21107 if (EQ (car, QCeval))
21108 {
21109 /* An element of the form (:eval FORM) means evaluate FORM
21110 and use the result as mode line elements. */
21111
21112 if (risky)
21113 break;
21114
21115 if (CONSP (XCDR (elt)))
21116 {
21117 Lisp_Object spec;
21118 spec = safe_eval (XCAR (XCDR (elt)));
21119 n += display_mode_element (it, depth, field_width - n,
21120 precision - n, spec, props,
21121 risky);
21122 }
21123 }
21124 else if (EQ (car, QCpropertize))
21125 {
21126 /* An element of the form (:propertize ELT PROPS...)
21127 means display ELT but applying properties PROPS. */
21128
21129 if (risky)
21130 break;
21131
21132 if (CONSP (XCDR (elt)))
21133 n += display_mode_element (it, depth, field_width - n,
21134 precision - n, XCAR (XCDR (elt)),
21135 XCDR (XCDR (elt)), risky);
21136 }
21137 else if (SYMBOLP (car))
21138 {
21139 tem = Fboundp (car);
21140 elt = XCDR (elt);
21141 if (!CONSP (elt))
21142 goto invalid;
21143 /* elt is now the cdr, and we know it is a cons cell.
21144 Use its car if CAR has a non-nil value. */
21145 if (!NILP (tem))
21146 {
21147 tem = Fsymbol_value (car);
21148 if (!NILP (tem))
21149 {
21150 elt = XCAR (elt);
21151 goto tail_recurse;
21152 }
21153 }
21154 /* Symbol's value is nil (or symbol is unbound)
21155 Get the cddr of the original list
21156 and if possible find the caddr and use that. */
21157 elt = XCDR (elt);
21158 if (NILP (elt))
21159 break;
21160 else if (!CONSP (elt))
21161 goto invalid;
21162 elt = XCAR (elt);
21163 goto tail_recurse;
21164 }
21165 else if (INTEGERP (car))
21166 {
21167 register int lim = XINT (car);
21168 elt = XCDR (elt);
21169 if (lim < 0)
21170 {
21171 /* Negative int means reduce maximum width. */
21172 if (precision <= 0)
21173 precision = -lim;
21174 else
21175 precision = min (precision, -lim);
21176 }
21177 else if (lim > 0)
21178 {
21179 /* Padding specified. Don't let it be more than
21180 current maximum. */
21181 if (precision > 0)
21182 lim = min (precision, lim);
21183
21184 /* If that's more padding than already wanted, queue it.
21185 But don't reduce padding already specified even if
21186 that is beyond the current truncation point. */
21187 field_width = max (lim, field_width);
21188 }
21189 goto tail_recurse;
21190 }
21191 else if (STRINGP (car) || CONSP (car))
21192 {
21193 Lisp_Object halftail = elt;
21194 int len = 0;
21195
21196 while (CONSP (elt)
21197 && (precision <= 0 || n < precision))
21198 {
21199 n += display_mode_element (it, depth,
21200 /* Do padding only after the last
21201 element in the list. */
21202 (! CONSP (XCDR (elt))
21203 ? field_width - n
21204 : 0),
21205 precision - n, XCAR (elt),
21206 props, risky);
21207 elt = XCDR (elt);
21208 len++;
21209 if ((len & 1) == 0)
21210 halftail = XCDR (halftail);
21211 /* Check for cycle. */
21212 if (EQ (halftail, elt))
21213 break;
21214 }
21215 }
21216 }
21217 break;
21218
21219 default:
21220 invalid:
21221 elt = build_string ("*invalid*");
21222 goto tail_recurse;
21223 }
21224
21225 /* Pad to FIELD_WIDTH. */
21226 if (field_width > 0 && n < field_width)
21227 {
21228 switch (mode_line_target)
21229 {
21230 case MODE_LINE_NOPROP:
21231 case MODE_LINE_TITLE:
21232 n += store_mode_line_noprop ("", field_width - n, 0);
21233 break;
21234 case MODE_LINE_STRING:
21235 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21236 break;
21237 case MODE_LINE_DISPLAY:
21238 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21239 0, 0, 0);
21240 break;
21241 }
21242 }
21243
21244 return n;
21245 }
21246
21247 /* Store a mode-line string element in mode_line_string_list.
21248
21249 If STRING is non-null, display that C string. Otherwise, the Lisp
21250 string LISP_STRING is displayed.
21251
21252 FIELD_WIDTH is the minimum number of output glyphs to produce.
21253 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21254 with spaces. FIELD_WIDTH <= 0 means don't pad.
21255
21256 PRECISION is the maximum number of characters to output from
21257 STRING. PRECISION <= 0 means don't truncate the string.
21258
21259 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21260 properties to the string.
21261
21262 PROPS are the properties to add to the string.
21263 The mode_line_string_face face property is always added to the string.
21264 */
21265
21266 static int
21267 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21268 int field_width, int precision, Lisp_Object props)
21269 {
21270 ptrdiff_t len;
21271 int n = 0;
21272
21273 if (string != NULL)
21274 {
21275 len = strlen (string);
21276 if (precision > 0 && len > precision)
21277 len = precision;
21278 lisp_string = make_string (string, len);
21279 if (NILP (props))
21280 props = mode_line_string_face_prop;
21281 else if (!NILP (mode_line_string_face))
21282 {
21283 Lisp_Object face = Fplist_get (props, Qface);
21284 props = Fcopy_sequence (props);
21285 if (NILP (face))
21286 face = mode_line_string_face;
21287 else
21288 face = list2 (face, mode_line_string_face);
21289 props = Fplist_put (props, Qface, face);
21290 }
21291 Fadd_text_properties (make_number (0), make_number (len),
21292 props, lisp_string);
21293 }
21294 else
21295 {
21296 len = XFASTINT (Flength (lisp_string));
21297 if (precision > 0 && len > precision)
21298 {
21299 len = precision;
21300 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21301 precision = -1;
21302 }
21303 if (!NILP (mode_line_string_face))
21304 {
21305 Lisp_Object face;
21306 if (NILP (props))
21307 props = Ftext_properties_at (make_number (0), lisp_string);
21308 face = Fplist_get (props, Qface);
21309 if (NILP (face))
21310 face = mode_line_string_face;
21311 else
21312 face = list2 (face, mode_line_string_face);
21313 props = list2 (Qface, face);
21314 if (copy_string)
21315 lisp_string = Fcopy_sequence (lisp_string);
21316 }
21317 if (!NILP (props))
21318 Fadd_text_properties (make_number (0), make_number (len),
21319 props, lisp_string);
21320 }
21321
21322 if (len > 0)
21323 {
21324 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21325 n += len;
21326 }
21327
21328 if (field_width > len)
21329 {
21330 field_width -= len;
21331 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21332 if (!NILP (props))
21333 Fadd_text_properties (make_number (0), make_number (field_width),
21334 props, lisp_string);
21335 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21336 n += field_width;
21337 }
21338
21339 return n;
21340 }
21341
21342
21343 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21344 1, 4, 0,
21345 doc: /* Format a string out of a mode line format specification.
21346 First arg FORMAT specifies the mode line format (see `mode-line-format'
21347 for details) to use.
21348
21349 By default, the format is evaluated for the currently selected window.
21350
21351 Optional second arg FACE specifies the face property to put on all
21352 characters for which no face is specified. The value nil means the
21353 default face. The value t means whatever face the window's mode line
21354 currently uses (either `mode-line' or `mode-line-inactive',
21355 depending on whether the window is the selected window or not).
21356 An integer value means the value string has no text
21357 properties.
21358
21359 Optional third and fourth args WINDOW and BUFFER specify the window
21360 and buffer to use as the context for the formatting (defaults
21361 are the selected window and the WINDOW's buffer). */)
21362 (Lisp_Object format, Lisp_Object face,
21363 Lisp_Object window, Lisp_Object buffer)
21364 {
21365 struct it it;
21366 int len;
21367 struct window *w;
21368 struct buffer *old_buffer = NULL;
21369 int face_id;
21370 int no_props = INTEGERP (face);
21371 ptrdiff_t count = SPECPDL_INDEX ();
21372 Lisp_Object str;
21373 int string_start = 0;
21374
21375 w = decode_any_window (window);
21376 XSETWINDOW (window, w);
21377
21378 if (NILP (buffer))
21379 buffer = w->contents;
21380 CHECK_BUFFER (buffer);
21381
21382 /* Make formatting the modeline a non-op when noninteractive, otherwise
21383 there will be problems later caused by a partially initialized frame. */
21384 if (NILP (format) || noninteractive)
21385 return empty_unibyte_string;
21386
21387 if (no_props)
21388 face = Qnil;
21389
21390 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21391 : EQ (face, Qt) ? (EQ (window, selected_window)
21392 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21393 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21394 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21395 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21396 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21397 : DEFAULT_FACE_ID;
21398
21399 old_buffer = current_buffer;
21400
21401 /* Save things including mode_line_proptrans_alist,
21402 and set that to nil so that we don't alter the outer value. */
21403 record_unwind_protect (unwind_format_mode_line,
21404 format_mode_line_unwind_data
21405 (XFRAME (WINDOW_FRAME (w)),
21406 old_buffer, selected_window, 1));
21407 mode_line_proptrans_alist = Qnil;
21408
21409 Fselect_window (window, Qt);
21410 set_buffer_internal_1 (XBUFFER (buffer));
21411
21412 init_iterator (&it, w, -1, -1, NULL, face_id);
21413
21414 if (no_props)
21415 {
21416 mode_line_target = MODE_LINE_NOPROP;
21417 mode_line_string_face_prop = Qnil;
21418 mode_line_string_list = Qnil;
21419 string_start = MODE_LINE_NOPROP_LEN (0);
21420 }
21421 else
21422 {
21423 mode_line_target = MODE_LINE_STRING;
21424 mode_line_string_list = Qnil;
21425 mode_line_string_face = face;
21426 mode_line_string_face_prop
21427 = NILP (face) ? Qnil : list2 (Qface, face);
21428 }
21429
21430 push_kboard (FRAME_KBOARD (it.f));
21431 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21432 pop_kboard ();
21433
21434 if (no_props)
21435 {
21436 len = MODE_LINE_NOPROP_LEN (string_start);
21437 str = make_string (mode_line_noprop_buf + string_start, len);
21438 }
21439 else
21440 {
21441 mode_line_string_list = Fnreverse (mode_line_string_list);
21442 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21443 empty_unibyte_string);
21444 }
21445
21446 unbind_to (count, Qnil);
21447 return str;
21448 }
21449
21450 /* Write a null-terminated, right justified decimal representation of
21451 the positive integer D to BUF using a minimal field width WIDTH. */
21452
21453 static void
21454 pint2str (register char *buf, register int width, register ptrdiff_t d)
21455 {
21456 register char *p = buf;
21457
21458 if (d <= 0)
21459 *p++ = '0';
21460 else
21461 {
21462 while (d > 0)
21463 {
21464 *p++ = d % 10 + '0';
21465 d /= 10;
21466 }
21467 }
21468
21469 for (width -= (int) (p - buf); width > 0; --width)
21470 *p++ = ' ';
21471 *p-- = '\0';
21472 while (p > buf)
21473 {
21474 d = *buf;
21475 *buf++ = *p;
21476 *p-- = d;
21477 }
21478 }
21479
21480 /* Write a null-terminated, right justified decimal and "human
21481 readable" representation of the nonnegative integer D to BUF using
21482 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21483
21484 static const char power_letter[] =
21485 {
21486 0, /* no letter */
21487 'k', /* kilo */
21488 'M', /* mega */
21489 'G', /* giga */
21490 'T', /* tera */
21491 'P', /* peta */
21492 'E', /* exa */
21493 'Z', /* zetta */
21494 'Y' /* yotta */
21495 };
21496
21497 static void
21498 pint2hrstr (char *buf, int width, ptrdiff_t d)
21499 {
21500 /* We aim to represent the nonnegative integer D as
21501 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21502 ptrdiff_t quotient = d;
21503 int remainder = 0;
21504 /* -1 means: do not use TENTHS. */
21505 int tenths = -1;
21506 int exponent = 0;
21507
21508 /* Length of QUOTIENT.TENTHS as a string. */
21509 int length;
21510
21511 char * psuffix;
21512 char * p;
21513
21514 if (quotient >= 1000)
21515 {
21516 /* Scale to the appropriate EXPONENT. */
21517 do
21518 {
21519 remainder = quotient % 1000;
21520 quotient /= 1000;
21521 exponent++;
21522 }
21523 while (quotient >= 1000);
21524
21525 /* Round to nearest and decide whether to use TENTHS or not. */
21526 if (quotient <= 9)
21527 {
21528 tenths = remainder / 100;
21529 if (remainder % 100 >= 50)
21530 {
21531 if (tenths < 9)
21532 tenths++;
21533 else
21534 {
21535 quotient++;
21536 if (quotient == 10)
21537 tenths = -1;
21538 else
21539 tenths = 0;
21540 }
21541 }
21542 }
21543 else
21544 if (remainder >= 500)
21545 {
21546 if (quotient < 999)
21547 quotient++;
21548 else
21549 {
21550 quotient = 1;
21551 exponent++;
21552 tenths = 0;
21553 }
21554 }
21555 }
21556
21557 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21558 if (tenths == -1 && quotient <= 99)
21559 if (quotient <= 9)
21560 length = 1;
21561 else
21562 length = 2;
21563 else
21564 length = 3;
21565 p = psuffix = buf + max (width, length);
21566
21567 /* Print EXPONENT. */
21568 *psuffix++ = power_letter[exponent];
21569 *psuffix = '\0';
21570
21571 /* Print TENTHS. */
21572 if (tenths >= 0)
21573 {
21574 *--p = '0' + tenths;
21575 *--p = '.';
21576 }
21577
21578 /* Print QUOTIENT. */
21579 do
21580 {
21581 int digit = quotient % 10;
21582 *--p = '0' + digit;
21583 }
21584 while ((quotient /= 10) != 0);
21585
21586 /* Print leading spaces. */
21587 while (buf < p)
21588 *--p = ' ';
21589 }
21590
21591 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21592 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21593 type of CODING_SYSTEM. Return updated pointer into BUF. */
21594
21595 static unsigned char invalid_eol_type[] = "(*invalid*)";
21596
21597 static char *
21598 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21599 {
21600 Lisp_Object val;
21601 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21602 const unsigned char *eol_str;
21603 int eol_str_len;
21604 /* The EOL conversion we are using. */
21605 Lisp_Object eoltype;
21606
21607 val = CODING_SYSTEM_SPEC (coding_system);
21608 eoltype = Qnil;
21609
21610 if (!VECTORP (val)) /* Not yet decided. */
21611 {
21612 *buf++ = multibyte ? '-' : ' ';
21613 if (eol_flag)
21614 eoltype = eol_mnemonic_undecided;
21615 /* Don't mention EOL conversion if it isn't decided. */
21616 }
21617 else
21618 {
21619 Lisp_Object attrs;
21620 Lisp_Object eolvalue;
21621
21622 attrs = AREF (val, 0);
21623 eolvalue = AREF (val, 2);
21624
21625 *buf++ = multibyte
21626 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21627 : ' ';
21628
21629 if (eol_flag)
21630 {
21631 /* The EOL conversion that is normal on this system. */
21632
21633 if (NILP (eolvalue)) /* Not yet decided. */
21634 eoltype = eol_mnemonic_undecided;
21635 else if (VECTORP (eolvalue)) /* Not yet decided. */
21636 eoltype = eol_mnemonic_undecided;
21637 else /* eolvalue is Qunix, Qdos, or Qmac. */
21638 eoltype = (EQ (eolvalue, Qunix)
21639 ? eol_mnemonic_unix
21640 : (EQ (eolvalue, Qdos) == 1
21641 ? eol_mnemonic_dos : eol_mnemonic_mac));
21642 }
21643 }
21644
21645 if (eol_flag)
21646 {
21647 /* Mention the EOL conversion if it is not the usual one. */
21648 if (STRINGP (eoltype))
21649 {
21650 eol_str = SDATA (eoltype);
21651 eol_str_len = SBYTES (eoltype);
21652 }
21653 else if (CHARACTERP (eoltype))
21654 {
21655 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21656 int c = XFASTINT (eoltype);
21657 eol_str_len = CHAR_STRING (c, tmp);
21658 eol_str = tmp;
21659 }
21660 else
21661 {
21662 eol_str = invalid_eol_type;
21663 eol_str_len = sizeof (invalid_eol_type) - 1;
21664 }
21665 memcpy (buf, eol_str, eol_str_len);
21666 buf += eol_str_len;
21667 }
21668
21669 return buf;
21670 }
21671
21672 /* Return a string for the output of a mode line %-spec for window W,
21673 generated by character C. FIELD_WIDTH > 0 means pad the string
21674 returned with spaces to that value. Return a Lisp string in
21675 *STRING if the resulting string is taken from that Lisp string.
21676
21677 Note we operate on the current buffer for most purposes. */
21678
21679 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21680
21681 static const char *
21682 decode_mode_spec (struct window *w, register int c, int field_width,
21683 Lisp_Object *string)
21684 {
21685 Lisp_Object obj;
21686 struct frame *f = XFRAME (WINDOW_FRAME (w));
21687 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21688 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21689 produce strings from numerical values, so limit preposterously
21690 large values of FIELD_WIDTH to avoid overrunning the buffer's
21691 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21692 bytes plus the terminating null. */
21693 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21694 struct buffer *b = current_buffer;
21695
21696 obj = Qnil;
21697 *string = Qnil;
21698
21699 switch (c)
21700 {
21701 case '*':
21702 if (!NILP (BVAR (b, read_only)))
21703 return "%";
21704 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21705 return "*";
21706 return "-";
21707
21708 case '+':
21709 /* This differs from %* only for a modified read-only buffer. */
21710 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21711 return "*";
21712 if (!NILP (BVAR (b, read_only)))
21713 return "%";
21714 return "-";
21715
21716 case '&':
21717 /* This differs from %* in ignoring read-only-ness. */
21718 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21719 return "*";
21720 return "-";
21721
21722 case '%':
21723 return "%";
21724
21725 case '[':
21726 {
21727 int i;
21728 char *p;
21729
21730 if (command_loop_level > 5)
21731 return "[[[... ";
21732 p = decode_mode_spec_buf;
21733 for (i = 0; i < command_loop_level; i++)
21734 *p++ = '[';
21735 *p = 0;
21736 return decode_mode_spec_buf;
21737 }
21738
21739 case ']':
21740 {
21741 int i;
21742 char *p;
21743
21744 if (command_loop_level > 5)
21745 return " ...]]]";
21746 p = decode_mode_spec_buf;
21747 for (i = 0; i < command_loop_level; i++)
21748 *p++ = ']';
21749 *p = 0;
21750 return decode_mode_spec_buf;
21751 }
21752
21753 case '-':
21754 {
21755 register int i;
21756
21757 /* Let lots_of_dashes be a string of infinite length. */
21758 if (mode_line_target == MODE_LINE_NOPROP
21759 || mode_line_target == MODE_LINE_STRING)
21760 return "--";
21761 if (field_width <= 0
21762 || field_width > sizeof (lots_of_dashes))
21763 {
21764 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21765 decode_mode_spec_buf[i] = '-';
21766 decode_mode_spec_buf[i] = '\0';
21767 return decode_mode_spec_buf;
21768 }
21769 else
21770 return lots_of_dashes;
21771 }
21772
21773 case 'b':
21774 obj = BVAR (b, name);
21775 break;
21776
21777 case 'c':
21778 /* %c and %l are ignored in `frame-title-format'.
21779 (In redisplay_internal, the frame title is drawn _before_ the
21780 windows are updated, so the stuff which depends on actual
21781 window contents (such as %l) may fail to render properly, or
21782 even crash emacs.) */
21783 if (mode_line_target == MODE_LINE_TITLE)
21784 return "";
21785 else
21786 {
21787 ptrdiff_t col = current_column ();
21788 w->column_number_displayed = col;
21789 pint2str (decode_mode_spec_buf, width, col);
21790 return decode_mode_spec_buf;
21791 }
21792
21793 case 'e':
21794 #ifndef SYSTEM_MALLOC
21795 {
21796 if (NILP (Vmemory_full))
21797 return "";
21798 else
21799 return "!MEM FULL! ";
21800 }
21801 #else
21802 return "";
21803 #endif
21804
21805 case 'F':
21806 /* %F displays the frame name. */
21807 if (!NILP (f->title))
21808 return SSDATA (f->title);
21809 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21810 return SSDATA (f->name);
21811 return "Emacs";
21812
21813 case 'f':
21814 obj = BVAR (b, filename);
21815 break;
21816
21817 case 'i':
21818 {
21819 ptrdiff_t size = ZV - BEGV;
21820 pint2str (decode_mode_spec_buf, width, size);
21821 return decode_mode_spec_buf;
21822 }
21823
21824 case 'I':
21825 {
21826 ptrdiff_t size = ZV - BEGV;
21827 pint2hrstr (decode_mode_spec_buf, width, size);
21828 return decode_mode_spec_buf;
21829 }
21830
21831 case 'l':
21832 {
21833 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21834 ptrdiff_t topline, nlines, height;
21835 ptrdiff_t junk;
21836
21837 /* %c and %l are ignored in `frame-title-format'. */
21838 if (mode_line_target == MODE_LINE_TITLE)
21839 return "";
21840
21841 startpos = marker_position (w->start);
21842 startpos_byte = marker_byte_position (w->start);
21843 height = WINDOW_TOTAL_LINES (w);
21844
21845 /* If we decided that this buffer isn't suitable for line numbers,
21846 don't forget that too fast. */
21847 if (w->base_line_pos == -1)
21848 goto no_value;
21849
21850 /* If the buffer is very big, don't waste time. */
21851 if (INTEGERP (Vline_number_display_limit)
21852 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21853 {
21854 w->base_line_pos = 0;
21855 w->base_line_number = 0;
21856 goto no_value;
21857 }
21858
21859 if (w->base_line_number > 0
21860 && w->base_line_pos > 0
21861 && w->base_line_pos <= startpos)
21862 {
21863 line = w->base_line_number;
21864 linepos = w->base_line_pos;
21865 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21866 }
21867 else
21868 {
21869 line = 1;
21870 linepos = BUF_BEGV (b);
21871 linepos_byte = BUF_BEGV_BYTE (b);
21872 }
21873
21874 /* Count lines from base line to window start position. */
21875 nlines = display_count_lines (linepos_byte,
21876 startpos_byte,
21877 startpos, &junk);
21878
21879 topline = nlines + line;
21880
21881 /* Determine a new base line, if the old one is too close
21882 or too far away, or if we did not have one.
21883 "Too close" means it's plausible a scroll-down would
21884 go back past it. */
21885 if (startpos == BUF_BEGV (b))
21886 {
21887 w->base_line_number = topline;
21888 w->base_line_pos = BUF_BEGV (b);
21889 }
21890 else if (nlines < height + 25 || nlines > height * 3 + 50
21891 || linepos == BUF_BEGV (b))
21892 {
21893 ptrdiff_t limit = BUF_BEGV (b);
21894 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21895 ptrdiff_t position;
21896 ptrdiff_t distance =
21897 (height * 2 + 30) * line_number_display_limit_width;
21898
21899 if (startpos - distance > limit)
21900 {
21901 limit = startpos - distance;
21902 limit_byte = CHAR_TO_BYTE (limit);
21903 }
21904
21905 nlines = display_count_lines (startpos_byte,
21906 limit_byte,
21907 - (height * 2 + 30),
21908 &position);
21909 /* If we couldn't find the lines we wanted within
21910 line_number_display_limit_width chars per line,
21911 give up on line numbers for this window. */
21912 if (position == limit_byte && limit == startpos - distance)
21913 {
21914 w->base_line_pos = -1;
21915 w->base_line_number = 0;
21916 goto no_value;
21917 }
21918
21919 w->base_line_number = topline - nlines;
21920 w->base_line_pos = BYTE_TO_CHAR (position);
21921 }
21922
21923 /* Now count lines from the start pos to point. */
21924 nlines = display_count_lines (startpos_byte,
21925 PT_BYTE, PT, &junk);
21926
21927 /* Record that we did display the line number. */
21928 line_number_displayed = 1;
21929
21930 /* Make the string to show. */
21931 pint2str (decode_mode_spec_buf, width, topline + nlines);
21932 return decode_mode_spec_buf;
21933 no_value:
21934 {
21935 char* p = decode_mode_spec_buf;
21936 int pad = width - 2;
21937 while (pad-- > 0)
21938 *p++ = ' ';
21939 *p++ = '?';
21940 *p++ = '?';
21941 *p = '\0';
21942 return decode_mode_spec_buf;
21943 }
21944 }
21945 break;
21946
21947 case 'm':
21948 obj = BVAR (b, mode_name);
21949 break;
21950
21951 case 'n':
21952 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21953 return " Narrow";
21954 break;
21955
21956 case 'p':
21957 {
21958 ptrdiff_t pos = marker_position (w->start);
21959 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21960
21961 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
21962 {
21963 if (pos <= BUF_BEGV (b))
21964 return "All";
21965 else
21966 return "Bottom";
21967 }
21968 else if (pos <= BUF_BEGV (b))
21969 return "Top";
21970 else
21971 {
21972 if (total > 1000000)
21973 /* Do it differently for a large value, to avoid overflow. */
21974 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21975 else
21976 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21977 /* We can't normally display a 3-digit number,
21978 so get us a 2-digit number that is close. */
21979 if (total == 100)
21980 total = 99;
21981 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21982 return decode_mode_spec_buf;
21983 }
21984 }
21985
21986 /* Display percentage of size above the bottom of the screen. */
21987 case 'P':
21988 {
21989 ptrdiff_t toppos = marker_position (w->start);
21990 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
21991 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21992
21993 if (botpos >= BUF_ZV (b))
21994 {
21995 if (toppos <= BUF_BEGV (b))
21996 return "All";
21997 else
21998 return "Bottom";
21999 }
22000 else
22001 {
22002 if (total > 1000000)
22003 /* Do it differently for a large value, to avoid overflow. */
22004 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22005 else
22006 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22007 /* We can't normally display a 3-digit number,
22008 so get us a 2-digit number that is close. */
22009 if (total == 100)
22010 total = 99;
22011 if (toppos <= BUF_BEGV (b))
22012 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22013 else
22014 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22015 return decode_mode_spec_buf;
22016 }
22017 }
22018
22019 case 's':
22020 /* status of process */
22021 obj = Fget_buffer_process (Fcurrent_buffer ());
22022 if (NILP (obj))
22023 return "no process";
22024 #ifndef MSDOS
22025 obj = Fsymbol_name (Fprocess_status (obj));
22026 #endif
22027 break;
22028
22029 case '@':
22030 {
22031 ptrdiff_t count = inhibit_garbage_collection ();
22032 Lisp_Object val = call1 (intern ("file-remote-p"),
22033 BVAR (current_buffer, directory));
22034 unbind_to (count, Qnil);
22035
22036 if (NILP (val))
22037 return "-";
22038 else
22039 return "@";
22040 }
22041
22042 case 'z':
22043 /* coding-system (not including end-of-line format) */
22044 case 'Z':
22045 /* coding-system (including end-of-line type) */
22046 {
22047 int eol_flag = (c == 'Z');
22048 char *p = decode_mode_spec_buf;
22049
22050 if (! FRAME_WINDOW_P (f))
22051 {
22052 /* No need to mention EOL here--the terminal never needs
22053 to do EOL conversion. */
22054 p = decode_mode_spec_coding (CODING_ID_NAME
22055 (FRAME_KEYBOARD_CODING (f)->id),
22056 p, 0);
22057 p = decode_mode_spec_coding (CODING_ID_NAME
22058 (FRAME_TERMINAL_CODING (f)->id),
22059 p, 0);
22060 }
22061 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22062 p, eol_flag);
22063
22064 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22065 #ifdef subprocesses
22066 obj = Fget_buffer_process (Fcurrent_buffer ());
22067 if (PROCESSP (obj))
22068 {
22069 p = decode_mode_spec_coding
22070 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22071 p = decode_mode_spec_coding
22072 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22073 }
22074 #endif /* subprocesses */
22075 #endif /* 0 */
22076 *p = 0;
22077 return decode_mode_spec_buf;
22078 }
22079 }
22080
22081 if (STRINGP (obj))
22082 {
22083 *string = obj;
22084 return SSDATA (obj);
22085 }
22086 else
22087 return "";
22088 }
22089
22090
22091 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22092 means count lines back from START_BYTE. But don't go beyond
22093 LIMIT_BYTE. Return the number of lines thus found (always
22094 nonnegative).
22095
22096 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22097 either the position COUNT lines after/before START_BYTE, if we
22098 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22099 COUNT lines. */
22100
22101 static ptrdiff_t
22102 display_count_lines (ptrdiff_t start_byte,
22103 ptrdiff_t limit_byte, ptrdiff_t count,
22104 ptrdiff_t *byte_pos_ptr)
22105 {
22106 register unsigned char *cursor;
22107 unsigned char *base;
22108
22109 register ptrdiff_t ceiling;
22110 register unsigned char *ceiling_addr;
22111 ptrdiff_t orig_count = count;
22112
22113 /* If we are not in selective display mode,
22114 check only for newlines. */
22115 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22116 && !INTEGERP (BVAR (current_buffer, selective_display)));
22117
22118 if (count > 0)
22119 {
22120 while (start_byte < limit_byte)
22121 {
22122 ceiling = BUFFER_CEILING_OF (start_byte);
22123 ceiling = min (limit_byte - 1, ceiling);
22124 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22125 base = (cursor = BYTE_POS_ADDR (start_byte));
22126
22127 do
22128 {
22129 if (selective_display)
22130 {
22131 while (*cursor != '\n' && *cursor != 015
22132 && ++cursor != ceiling_addr)
22133 continue;
22134 if (cursor == ceiling_addr)
22135 break;
22136 }
22137 else
22138 {
22139 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22140 if (! cursor)
22141 break;
22142 }
22143
22144 cursor++;
22145
22146 if (--count == 0)
22147 {
22148 start_byte += cursor - base;
22149 *byte_pos_ptr = start_byte;
22150 return orig_count;
22151 }
22152 }
22153 while (cursor < ceiling_addr);
22154
22155 start_byte += ceiling_addr - base;
22156 }
22157 }
22158 else
22159 {
22160 while (start_byte > limit_byte)
22161 {
22162 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22163 ceiling = max (limit_byte, ceiling);
22164 ceiling_addr = BYTE_POS_ADDR (ceiling);
22165 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22166 while (1)
22167 {
22168 if (selective_display)
22169 {
22170 while (--cursor >= ceiling_addr
22171 && *cursor != '\n' && *cursor != 015)
22172 continue;
22173 if (cursor < ceiling_addr)
22174 break;
22175 }
22176 else
22177 {
22178 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22179 if (! cursor)
22180 break;
22181 }
22182
22183 if (++count == 0)
22184 {
22185 start_byte += cursor - base + 1;
22186 *byte_pos_ptr = start_byte;
22187 /* When scanning backwards, we should
22188 not count the newline posterior to which we stop. */
22189 return - orig_count - 1;
22190 }
22191 }
22192 start_byte += ceiling_addr - base;
22193 }
22194 }
22195
22196 *byte_pos_ptr = limit_byte;
22197
22198 if (count < 0)
22199 return - orig_count + count;
22200 return orig_count - count;
22201
22202 }
22203
22204
22205 \f
22206 /***********************************************************************
22207 Displaying strings
22208 ***********************************************************************/
22209
22210 /* Display a NUL-terminated string, starting with index START.
22211
22212 If STRING is non-null, display that C string. Otherwise, the Lisp
22213 string LISP_STRING is displayed. There's a case that STRING is
22214 non-null and LISP_STRING is not nil. It means STRING is a string
22215 data of LISP_STRING. In that case, we display LISP_STRING while
22216 ignoring its text properties.
22217
22218 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22219 FACE_STRING. Display STRING or LISP_STRING with the face at
22220 FACE_STRING_POS in FACE_STRING:
22221
22222 Display the string in the environment given by IT, but use the
22223 standard display table, temporarily.
22224
22225 FIELD_WIDTH is the minimum number of output glyphs to produce.
22226 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22227 with spaces. If STRING has more characters, more than FIELD_WIDTH
22228 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22229
22230 PRECISION is the maximum number of characters to output from
22231 STRING. PRECISION < 0 means don't truncate the string.
22232
22233 This is roughly equivalent to printf format specifiers:
22234
22235 FIELD_WIDTH PRECISION PRINTF
22236 ----------------------------------------
22237 -1 -1 %s
22238 -1 10 %.10s
22239 10 -1 %10s
22240 20 10 %20.10s
22241
22242 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22243 display them, and < 0 means obey the current buffer's value of
22244 enable_multibyte_characters.
22245
22246 Value is the number of columns displayed. */
22247
22248 static int
22249 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22250 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22251 int field_width, int precision, int max_x, int multibyte)
22252 {
22253 int hpos_at_start = it->hpos;
22254 int saved_face_id = it->face_id;
22255 struct glyph_row *row = it->glyph_row;
22256 ptrdiff_t it_charpos;
22257
22258 /* Initialize the iterator IT for iteration over STRING beginning
22259 with index START. */
22260 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22261 precision, field_width, multibyte);
22262 if (string && STRINGP (lisp_string))
22263 /* LISP_STRING is the one returned by decode_mode_spec. We should
22264 ignore its text properties. */
22265 it->stop_charpos = it->end_charpos;
22266
22267 /* If displaying STRING, set up the face of the iterator from
22268 FACE_STRING, if that's given. */
22269 if (STRINGP (face_string))
22270 {
22271 ptrdiff_t endptr;
22272 struct face *face;
22273
22274 it->face_id
22275 = face_at_string_position (it->w, face_string, face_string_pos,
22276 0, it->region_beg_charpos,
22277 it->region_end_charpos,
22278 &endptr, it->base_face_id, 0);
22279 face = FACE_FROM_ID (it->f, it->face_id);
22280 it->face_box_p = face->box != FACE_NO_BOX;
22281 }
22282
22283 /* Set max_x to the maximum allowed X position. Don't let it go
22284 beyond the right edge of the window. */
22285 if (max_x <= 0)
22286 max_x = it->last_visible_x;
22287 else
22288 max_x = min (max_x, it->last_visible_x);
22289
22290 /* Skip over display elements that are not visible. because IT->w is
22291 hscrolled. */
22292 if (it->current_x < it->first_visible_x)
22293 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22294 MOVE_TO_POS | MOVE_TO_X);
22295
22296 row->ascent = it->max_ascent;
22297 row->height = it->max_ascent + it->max_descent;
22298 row->phys_ascent = it->max_phys_ascent;
22299 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22300 row->extra_line_spacing = it->max_extra_line_spacing;
22301
22302 if (STRINGP (it->string))
22303 it_charpos = IT_STRING_CHARPOS (*it);
22304 else
22305 it_charpos = IT_CHARPOS (*it);
22306
22307 /* This condition is for the case that we are called with current_x
22308 past last_visible_x. */
22309 while (it->current_x < max_x)
22310 {
22311 int x_before, x, n_glyphs_before, i, nglyphs;
22312
22313 /* Get the next display element. */
22314 if (!get_next_display_element (it))
22315 break;
22316
22317 /* Produce glyphs. */
22318 x_before = it->current_x;
22319 n_glyphs_before = row->used[TEXT_AREA];
22320 PRODUCE_GLYPHS (it);
22321
22322 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22323 i = 0;
22324 x = x_before;
22325 while (i < nglyphs)
22326 {
22327 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22328
22329 if (it->line_wrap != TRUNCATE
22330 && x + glyph->pixel_width > max_x)
22331 {
22332 /* End of continued line or max_x reached. */
22333 if (CHAR_GLYPH_PADDING_P (*glyph))
22334 {
22335 /* A wide character is unbreakable. */
22336 if (row->reversed_p)
22337 unproduce_glyphs (it, row->used[TEXT_AREA]
22338 - n_glyphs_before);
22339 row->used[TEXT_AREA] = n_glyphs_before;
22340 it->current_x = x_before;
22341 }
22342 else
22343 {
22344 if (row->reversed_p)
22345 unproduce_glyphs (it, row->used[TEXT_AREA]
22346 - (n_glyphs_before + i));
22347 row->used[TEXT_AREA] = n_glyphs_before + i;
22348 it->current_x = x;
22349 }
22350 break;
22351 }
22352 else if (x + glyph->pixel_width >= it->first_visible_x)
22353 {
22354 /* Glyph is at least partially visible. */
22355 ++it->hpos;
22356 if (x < it->first_visible_x)
22357 row->x = x - it->first_visible_x;
22358 }
22359 else
22360 {
22361 /* Glyph is off the left margin of the display area.
22362 Should not happen. */
22363 emacs_abort ();
22364 }
22365
22366 row->ascent = max (row->ascent, it->max_ascent);
22367 row->height = max (row->height, it->max_ascent + it->max_descent);
22368 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22369 row->phys_height = max (row->phys_height,
22370 it->max_phys_ascent + it->max_phys_descent);
22371 row->extra_line_spacing = max (row->extra_line_spacing,
22372 it->max_extra_line_spacing);
22373 x += glyph->pixel_width;
22374 ++i;
22375 }
22376
22377 /* Stop if max_x reached. */
22378 if (i < nglyphs)
22379 break;
22380
22381 /* Stop at line ends. */
22382 if (ITERATOR_AT_END_OF_LINE_P (it))
22383 {
22384 it->continuation_lines_width = 0;
22385 break;
22386 }
22387
22388 set_iterator_to_next (it, 1);
22389 if (STRINGP (it->string))
22390 it_charpos = IT_STRING_CHARPOS (*it);
22391 else
22392 it_charpos = IT_CHARPOS (*it);
22393
22394 /* Stop if truncating at the right edge. */
22395 if (it->line_wrap == TRUNCATE
22396 && it->current_x >= it->last_visible_x)
22397 {
22398 /* Add truncation mark, but don't do it if the line is
22399 truncated at a padding space. */
22400 if (it_charpos < it->string_nchars)
22401 {
22402 if (!FRAME_WINDOW_P (it->f))
22403 {
22404 int ii, n;
22405
22406 if (it->current_x > it->last_visible_x)
22407 {
22408 if (!row->reversed_p)
22409 {
22410 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22411 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22412 break;
22413 }
22414 else
22415 {
22416 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22417 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22418 break;
22419 unproduce_glyphs (it, ii + 1);
22420 ii = row->used[TEXT_AREA] - (ii + 1);
22421 }
22422 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22423 {
22424 row->used[TEXT_AREA] = ii;
22425 produce_special_glyphs (it, IT_TRUNCATION);
22426 }
22427 }
22428 produce_special_glyphs (it, IT_TRUNCATION);
22429 }
22430 row->truncated_on_right_p = 1;
22431 }
22432 break;
22433 }
22434 }
22435
22436 /* Maybe insert a truncation at the left. */
22437 if (it->first_visible_x
22438 && it_charpos > 0)
22439 {
22440 if (!FRAME_WINDOW_P (it->f)
22441 || (row->reversed_p
22442 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22443 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22444 insert_left_trunc_glyphs (it);
22445 row->truncated_on_left_p = 1;
22446 }
22447
22448 it->face_id = saved_face_id;
22449
22450 /* Value is number of columns displayed. */
22451 return it->hpos - hpos_at_start;
22452 }
22453
22454
22455 \f
22456 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22457 appears as an element of LIST or as the car of an element of LIST.
22458 If PROPVAL is a list, compare each element against LIST in that
22459 way, and return 1/2 if any element of PROPVAL is found in LIST.
22460 Otherwise return 0. This function cannot quit.
22461 The return value is 2 if the text is invisible but with an ellipsis
22462 and 1 if it's invisible and without an ellipsis. */
22463
22464 int
22465 invisible_p (register Lisp_Object propval, Lisp_Object list)
22466 {
22467 register Lisp_Object tail, proptail;
22468
22469 for (tail = list; CONSP (tail); tail = XCDR (tail))
22470 {
22471 register Lisp_Object tem;
22472 tem = XCAR (tail);
22473 if (EQ (propval, tem))
22474 return 1;
22475 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22476 return NILP (XCDR (tem)) ? 1 : 2;
22477 }
22478
22479 if (CONSP (propval))
22480 {
22481 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22482 {
22483 Lisp_Object propelt;
22484 propelt = XCAR (proptail);
22485 for (tail = list; CONSP (tail); tail = XCDR (tail))
22486 {
22487 register Lisp_Object tem;
22488 tem = XCAR (tail);
22489 if (EQ (propelt, tem))
22490 return 1;
22491 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22492 return NILP (XCDR (tem)) ? 1 : 2;
22493 }
22494 }
22495 }
22496
22497 return 0;
22498 }
22499
22500 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22501 doc: /* Non-nil if the property makes the text invisible.
22502 POS-OR-PROP can be a marker or number, in which case it is taken to be
22503 a position in the current buffer and the value of the `invisible' property
22504 is checked; or it can be some other value, which is then presumed to be the
22505 value of the `invisible' property of the text of interest.
22506 The non-nil value returned can be t for truly invisible text or something
22507 else if the text is replaced by an ellipsis. */)
22508 (Lisp_Object pos_or_prop)
22509 {
22510 Lisp_Object prop
22511 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22512 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22513 : pos_or_prop);
22514 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22515 return (invis == 0 ? Qnil
22516 : invis == 1 ? Qt
22517 : make_number (invis));
22518 }
22519
22520 /* Calculate a width or height in pixels from a specification using
22521 the following elements:
22522
22523 SPEC ::=
22524 NUM - a (fractional) multiple of the default font width/height
22525 (NUM) - specifies exactly NUM pixels
22526 UNIT - a fixed number of pixels, see below.
22527 ELEMENT - size of a display element in pixels, see below.
22528 (NUM . SPEC) - equals NUM * SPEC
22529 (+ SPEC SPEC ...) - add pixel values
22530 (- SPEC SPEC ...) - subtract pixel values
22531 (- SPEC) - negate pixel value
22532
22533 NUM ::=
22534 INT or FLOAT - a number constant
22535 SYMBOL - use symbol's (buffer local) variable binding.
22536
22537 UNIT ::=
22538 in - pixels per inch *)
22539 mm - pixels per 1/1000 meter *)
22540 cm - pixels per 1/100 meter *)
22541 width - width of current font in pixels.
22542 height - height of current font in pixels.
22543
22544 *) using the ratio(s) defined in display-pixels-per-inch.
22545
22546 ELEMENT ::=
22547
22548 left-fringe - left fringe width in pixels
22549 right-fringe - right fringe width in pixels
22550
22551 left-margin - left margin width in pixels
22552 right-margin - right margin width in pixels
22553
22554 scroll-bar - scroll-bar area width in pixels
22555
22556 Examples:
22557
22558 Pixels corresponding to 5 inches:
22559 (5 . in)
22560
22561 Total width of non-text areas on left side of window (if scroll-bar is on left):
22562 '(space :width (+ left-fringe left-margin scroll-bar))
22563
22564 Align to first text column (in header line):
22565 '(space :align-to 0)
22566
22567 Align to middle of text area minus half the width of variable `my-image'
22568 containing a loaded image:
22569 '(space :align-to (0.5 . (- text my-image)))
22570
22571 Width of left margin minus width of 1 character in the default font:
22572 '(space :width (- left-margin 1))
22573
22574 Width of left margin minus width of 2 characters in the current font:
22575 '(space :width (- left-margin (2 . width)))
22576
22577 Center 1 character over left-margin (in header line):
22578 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22579
22580 Different ways to express width of left fringe plus left margin minus one pixel:
22581 '(space :width (- (+ left-fringe left-margin) (1)))
22582 '(space :width (+ left-fringe left-margin (- (1))))
22583 '(space :width (+ left-fringe left-margin (-1)))
22584
22585 */
22586
22587 static int
22588 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22589 struct font *font, int width_p, int *align_to)
22590 {
22591 double pixels;
22592
22593 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22594 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22595
22596 if (NILP (prop))
22597 return OK_PIXELS (0);
22598
22599 eassert (FRAME_LIVE_P (it->f));
22600
22601 if (SYMBOLP (prop))
22602 {
22603 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22604 {
22605 char *unit = SSDATA (SYMBOL_NAME (prop));
22606
22607 if (unit[0] == 'i' && unit[1] == 'n')
22608 pixels = 1.0;
22609 else if (unit[0] == 'm' && unit[1] == 'm')
22610 pixels = 25.4;
22611 else if (unit[0] == 'c' && unit[1] == 'm')
22612 pixels = 2.54;
22613 else
22614 pixels = 0;
22615 if (pixels > 0)
22616 {
22617 double ppi = (width_p ? FRAME_RES_X (it->f)
22618 : FRAME_RES_Y (it->f));
22619
22620 if (ppi > 0)
22621 return OK_PIXELS (ppi / pixels);
22622 return 0;
22623 }
22624 }
22625
22626 #ifdef HAVE_WINDOW_SYSTEM
22627 if (EQ (prop, Qheight))
22628 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22629 if (EQ (prop, Qwidth))
22630 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22631 #else
22632 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22633 return OK_PIXELS (1);
22634 #endif
22635
22636 if (EQ (prop, Qtext))
22637 return OK_PIXELS (width_p
22638 ? window_box_width (it->w, TEXT_AREA)
22639 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22640
22641 if (align_to && *align_to < 0)
22642 {
22643 *res = 0;
22644 if (EQ (prop, Qleft))
22645 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22646 if (EQ (prop, Qright))
22647 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22648 if (EQ (prop, Qcenter))
22649 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22650 + window_box_width (it->w, TEXT_AREA) / 2);
22651 if (EQ (prop, Qleft_fringe))
22652 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22653 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22654 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22655 if (EQ (prop, Qright_fringe))
22656 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22657 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22658 : window_box_right_offset (it->w, TEXT_AREA));
22659 if (EQ (prop, Qleft_margin))
22660 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22661 if (EQ (prop, Qright_margin))
22662 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22663 if (EQ (prop, Qscroll_bar))
22664 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22665 ? 0
22666 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22667 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22668 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22669 : 0)));
22670 }
22671 else
22672 {
22673 if (EQ (prop, Qleft_fringe))
22674 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22675 if (EQ (prop, Qright_fringe))
22676 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22677 if (EQ (prop, Qleft_margin))
22678 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22679 if (EQ (prop, Qright_margin))
22680 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22681 if (EQ (prop, Qscroll_bar))
22682 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22683 }
22684
22685 prop = buffer_local_value_1 (prop, it->w->contents);
22686 if (EQ (prop, Qunbound))
22687 prop = Qnil;
22688 }
22689
22690 if (INTEGERP (prop) || FLOATP (prop))
22691 {
22692 int base_unit = (width_p
22693 ? FRAME_COLUMN_WIDTH (it->f)
22694 : FRAME_LINE_HEIGHT (it->f));
22695 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22696 }
22697
22698 if (CONSP (prop))
22699 {
22700 Lisp_Object car = XCAR (prop);
22701 Lisp_Object cdr = XCDR (prop);
22702
22703 if (SYMBOLP (car))
22704 {
22705 #ifdef HAVE_WINDOW_SYSTEM
22706 if (FRAME_WINDOW_P (it->f)
22707 && valid_image_p (prop))
22708 {
22709 ptrdiff_t id = lookup_image (it->f, prop);
22710 struct image *img = IMAGE_FROM_ID (it->f, id);
22711
22712 return OK_PIXELS (width_p ? img->width : img->height);
22713 }
22714 #endif
22715 if (EQ (car, Qplus) || EQ (car, Qminus))
22716 {
22717 int first = 1;
22718 double px;
22719
22720 pixels = 0;
22721 while (CONSP (cdr))
22722 {
22723 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22724 font, width_p, align_to))
22725 return 0;
22726 if (first)
22727 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22728 else
22729 pixels += px;
22730 cdr = XCDR (cdr);
22731 }
22732 if (EQ (car, Qminus))
22733 pixels = -pixels;
22734 return OK_PIXELS (pixels);
22735 }
22736
22737 car = buffer_local_value_1 (car, it->w->contents);
22738 if (EQ (car, Qunbound))
22739 car = Qnil;
22740 }
22741
22742 if (INTEGERP (car) || FLOATP (car))
22743 {
22744 double fact;
22745 pixels = XFLOATINT (car);
22746 if (NILP (cdr))
22747 return OK_PIXELS (pixels);
22748 if (calc_pixel_width_or_height (&fact, it, cdr,
22749 font, width_p, align_to))
22750 return OK_PIXELS (pixels * fact);
22751 return 0;
22752 }
22753
22754 return 0;
22755 }
22756
22757 return 0;
22758 }
22759
22760 \f
22761 /***********************************************************************
22762 Glyph Display
22763 ***********************************************************************/
22764
22765 #ifdef HAVE_WINDOW_SYSTEM
22766
22767 #ifdef GLYPH_DEBUG
22768
22769 void
22770 dump_glyph_string (struct glyph_string *s)
22771 {
22772 fprintf (stderr, "glyph string\n");
22773 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22774 s->x, s->y, s->width, s->height);
22775 fprintf (stderr, " ybase = %d\n", s->ybase);
22776 fprintf (stderr, " hl = %d\n", s->hl);
22777 fprintf (stderr, " left overhang = %d, right = %d\n",
22778 s->left_overhang, s->right_overhang);
22779 fprintf (stderr, " nchars = %d\n", s->nchars);
22780 fprintf (stderr, " extends to end of line = %d\n",
22781 s->extends_to_end_of_line_p);
22782 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22783 fprintf (stderr, " bg width = %d\n", s->background_width);
22784 }
22785
22786 #endif /* GLYPH_DEBUG */
22787
22788 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22789 of XChar2b structures for S; it can't be allocated in
22790 init_glyph_string because it must be allocated via `alloca'. W
22791 is the window on which S is drawn. ROW and AREA are the glyph row
22792 and area within the row from which S is constructed. START is the
22793 index of the first glyph structure covered by S. HL is a
22794 face-override for drawing S. */
22795
22796 #ifdef HAVE_NTGUI
22797 #define OPTIONAL_HDC(hdc) HDC hdc,
22798 #define DECLARE_HDC(hdc) HDC hdc;
22799 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22800 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22801 #endif
22802
22803 #ifndef OPTIONAL_HDC
22804 #define OPTIONAL_HDC(hdc)
22805 #define DECLARE_HDC(hdc)
22806 #define ALLOCATE_HDC(hdc, f)
22807 #define RELEASE_HDC(hdc, f)
22808 #endif
22809
22810 static void
22811 init_glyph_string (struct glyph_string *s,
22812 OPTIONAL_HDC (hdc)
22813 XChar2b *char2b, struct window *w, struct glyph_row *row,
22814 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22815 {
22816 memset (s, 0, sizeof *s);
22817 s->w = w;
22818 s->f = XFRAME (w->frame);
22819 #ifdef HAVE_NTGUI
22820 s->hdc = hdc;
22821 #endif
22822 s->display = FRAME_X_DISPLAY (s->f);
22823 s->window = FRAME_X_WINDOW (s->f);
22824 s->char2b = char2b;
22825 s->hl = hl;
22826 s->row = row;
22827 s->area = area;
22828 s->first_glyph = row->glyphs[area] + start;
22829 s->height = row->height;
22830 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22831 s->ybase = s->y + row->ascent;
22832 }
22833
22834
22835 /* Append the list of glyph strings with head H and tail T to the list
22836 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22837
22838 static void
22839 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22840 struct glyph_string *h, struct glyph_string *t)
22841 {
22842 if (h)
22843 {
22844 if (*head)
22845 (*tail)->next = h;
22846 else
22847 *head = h;
22848 h->prev = *tail;
22849 *tail = t;
22850 }
22851 }
22852
22853
22854 /* Prepend the list of glyph strings with head H and tail T to the
22855 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22856 result. */
22857
22858 static void
22859 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22860 struct glyph_string *h, struct glyph_string *t)
22861 {
22862 if (h)
22863 {
22864 if (*head)
22865 (*head)->prev = t;
22866 else
22867 *tail = t;
22868 t->next = *head;
22869 *head = h;
22870 }
22871 }
22872
22873
22874 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22875 Set *HEAD and *TAIL to the resulting list. */
22876
22877 static void
22878 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22879 struct glyph_string *s)
22880 {
22881 s->next = s->prev = NULL;
22882 append_glyph_string_lists (head, tail, s, s);
22883 }
22884
22885
22886 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22887 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22888 make sure that X resources for the face returned are allocated.
22889 Value is a pointer to a realized face that is ready for display if
22890 DISPLAY_P is non-zero. */
22891
22892 static struct face *
22893 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22894 XChar2b *char2b, int display_p)
22895 {
22896 struct face *face = FACE_FROM_ID (f, face_id);
22897 unsigned code = 0;
22898
22899 if (face->font)
22900 {
22901 code = face->font->driver->encode_char (face->font, c);
22902
22903 if (code == FONT_INVALID_CODE)
22904 code = 0;
22905 }
22906 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22907
22908 /* Make sure X resources of the face are allocated. */
22909 #ifdef HAVE_X_WINDOWS
22910 if (display_p)
22911 #endif
22912 {
22913 eassert (face != NULL);
22914 PREPARE_FACE_FOR_DISPLAY (f, face);
22915 }
22916
22917 return face;
22918 }
22919
22920
22921 /* Get face and two-byte form of character glyph GLYPH on frame F.
22922 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22923 a pointer to a realized face that is ready for display. */
22924
22925 static struct face *
22926 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22927 XChar2b *char2b, int *two_byte_p)
22928 {
22929 struct face *face;
22930 unsigned code = 0;
22931
22932 eassert (glyph->type == CHAR_GLYPH);
22933 face = FACE_FROM_ID (f, glyph->face_id);
22934
22935 /* Make sure X resources of the face are allocated. */
22936 eassert (face != NULL);
22937 PREPARE_FACE_FOR_DISPLAY (f, face);
22938
22939 if (two_byte_p)
22940 *two_byte_p = 0;
22941
22942 if (face->font)
22943 {
22944 if (CHAR_BYTE8_P (glyph->u.ch))
22945 code = CHAR_TO_BYTE8 (glyph->u.ch);
22946 else
22947 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22948
22949 if (code == FONT_INVALID_CODE)
22950 code = 0;
22951 }
22952
22953 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22954 return face;
22955 }
22956
22957
22958 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22959 Return 1 if FONT has a glyph for C, otherwise return 0. */
22960
22961 static int
22962 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22963 {
22964 unsigned code;
22965
22966 if (CHAR_BYTE8_P (c))
22967 code = CHAR_TO_BYTE8 (c);
22968 else
22969 code = font->driver->encode_char (font, c);
22970
22971 if (code == FONT_INVALID_CODE)
22972 return 0;
22973 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22974 return 1;
22975 }
22976
22977
22978 /* Fill glyph string S with composition components specified by S->cmp.
22979
22980 BASE_FACE is the base face of the composition.
22981 S->cmp_from is the index of the first component for S.
22982
22983 OVERLAPS non-zero means S should draw the foreground only, and use
22984 its physical height for clipping. See also draw_glyphs.
22985
22986 Value is the index of a component not in S. */
22987
22988 static int
22989 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22990 int overlaps)
22991 {
22992 int i;
22993 /* For all glyphs of this composition, starting at the offset
22994 S->cmp_from, until we reach the end of the definition or encounter a
22995 glyph that requires the different face, add it to S. */
22996 struct face *face;
22997
22998 eassert (s);
22999
23000 s->for_overlaps = overlaps;
23001 s->face = NULL;
23002 s->font = NULL;
23003 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23004 {
23005 int c = COMPOSITION_GLYPH (s->cmp, i);
23006
23007 /* TAB in a composition means display glyphs with padding space
23008 on the left or right. */
23009 if (c != '\t')
23010 {
23011 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23012 -1, Qnil);
23013
23014 face = get_char_face_and_encoding (s->f, c, face_id,
23015 s->char2b + i, 1);
23016 if (face)
23017 {
23018 if (! s->face)
23019 {
23020 s->face = face;
23021 s->font = s->face->font;
23022 }
23023 else if (s->face != face)
23024 break;
23025 }
23026 }
23027 ++s->nchars;
23028 }
23029 s->cmp_to = i;
23030
23031 if (s->face == NULL)
23032 {
23033 s->face = base_face->ascii_face;
23034 s->font = s->face->font;
23035 }
23036
23037 /* All glyph strings for the same composition has the same width,
23038 i.e. the width set for the first component of the composition. */
23039 s->width = s->first_glyph->pixel_width;
23040
23041 /* If the specified font could not be loaded, use the frame's
23042 default font, but record the fact that we couldn't load it in
23043 the glyph string so that we can draw rectangles for the
23044 characters of the glyph string. */
23045 if (s->font == NULL)
23046 {
23047 s->font_not_found_p = 1;
23048 s->font = FRAME_FONT (s->f);
23049 }
23050
23051 /* Adjust base line for subscript/superscript text. */
23052 s->ybase += s->first_glyph->voffset;
23053
23054 /* This glyph string must always be drawn with 16-bit functions. */
23055 s->two_byte_p = 1;
23056
23057 return s->cmp_to;
23058 }
23059
23060 static int
23061 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23062 int start, int end, int overlaps)
23063 {
23064 struct glyph *glyph, *last;
23065 Lisp_Object lgstring;
23066 int i;
23067
23068 s->for_overlaps = overlaps;
23069 glyph = s->row->glyphs[s->area] + start;
23070 last = s->row->glyphs[s->area] + end;
23071 s->cmp_id = glyph->u.cmp.id;
23072 s->cmp_from = glyph->slice.cmp.from;
23073 s->cmp_to = glyph->slice.cmp.to + 1;
23074 s->face = FACE_FROM_ID (s->f, face_id);
23075 lgstring = composition_gstring_from_id (s->cmp_id);
23076 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23077 glyph++;
23078 while (glyph < last
23079 && glyph->u.cmp.automatic
23080 && glyph->u.cmp.id == s->cmp_id
23081 && s->cmp_to == glyph->slice.cmp.from)
23082 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23083
23084 for (i = s->cmp_from; i < s->cmp_to; i++)
23085 {
23086 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23087 unsigned code = LGLYPH_CODE (lglyph);
23088
23089 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23090 }
23091 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23092 return glyph - s->row->glyphs[s->area];
23093 }
23094
23095
23096 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23097 See the comment of fill_glyph_string for arguments.
23098 Value is the index of the first glyph not in S. */
23099
23100
23101 static int
23102 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23103 int start, int end, int overlaps)
23104 {
23105 struct glyph *glyph, *last;
23106 int voffset;
23107
23108 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23109 s->for_overlaps = overlaps;
23110 glyph = s->row->glyphs[s->area] + start;
23111 last = s->row->glyphs[s->area] + end;
23112 voffset = glyph->voffset;
23113 s->face = FACE_FROM_ID (s->f, face_id);
23114 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23115 s->nchars = 1;
23116 s->width = glyph->pixel_width;
23117 glyph++;
23118 while (glyph < last
23119 && glyph->type == GLYPHLESS_GLYPH
23120 && glyph->voffset == voffset
23121 && glyph->face_id == face_id)
23122 {
23123 s->nchars++;
23124 s->width += glyph->pixel_width;
23125 glyph++;
23126 }
23127 s->ybase += voffset;
23128 return glyph - s->row->glyphs[s->area];
23129 }
23130
23131
23132 /* Fill glyph string S from a sequence of character glyphs.
23133
23134 FACE_ID is the face id of the string. START is the index of the
23135 first glyph to consider, END is the index of the last + 1.
23136 OVERLAPS non-zero means S should draw the foreground only, and use
23137 its physical height for clipping. See also draw_glyphs.
23138
23139 Value is the index of the first glyph not in S. */
23140
23141 static int
23142 fill_glyph_string (struct glyph_string *s, int face_id,
23143 int start, int end, int overlaps)
23144 {
23145 struct glyph *glyph, *last;
23146 int voffset;
23147 int glyph_not_available_p;
23148
23149 eassert (s->f == XFRAME (s->w->frame));
23150 eassert (s->nchars == 0);
23151 eassert (start >= 0 && end > start);
23152
23153 s->for_overlaps = overlaps;
23154 glyph = s->row->glyphs[s->area] + start;
23155 last = s->row->glyphs[s->area] + end;
23156 voffset = glyph->voffset;
23157 s->padding_p = glyph->padding_p;
23158 glyph_not_available_p = glyph->glyph_not_available_p;
23159
23160 while (glyph < last
23161 && glyph->type == CHAR_GLYPH
23162 && glyph->voffset == voffset
23163 /* Same face id implies same font, nowadays. */
23164 && glyph->face_id == face_id
23165 && glyph->glyph_not_available_p == glyph_not_available_p)
23166 {
23167 int two_byte_p;
23168
23169 s->face = get_glyph_face_and_encoding (s->f, glyph,
23170 s->char2b + s->nchars,
23171 &two_byte_p);
23172 s->two_byte_p = two_byte_p;
23173 ++s->nchars;
23174 eassert (s->nchars <= end - start);
23175 s->width += glyph->pixel_width;
23176 if (glyph++->padding_p != s->padding_p)
23177 break;
23178 }
23179
23180 s->font = s->face->font;
23181
23182 /* If the specified font could not be loaded, use the frame's font,
23183 but record the fact that we couldn't load it in
23184 S->font_not_found_p so that we can draw rectangles for the
23185 characters of the glyph string. */
23186 if (s->font == NULL || glyph_not_available_p)
23187 {
23188 s->font_not_found_p = 1;
23189 s->font = FRAME_FONT (s->f);
23190 }
23191
23192 /* Adjust base line for subscript/superscript text. */
23193 s->ybase += voffset;
23194
23195 eassert (s->face && s->face->gc);
23196 return glyph - s->row->glyphs[s->area];
23197 }
23198
23199
23200 /* Fill glyph string S from image glyph S->first_glyph. */
23201
23202 static void
23203 fill_image_glyph_string (struct glyph_string *s)
23204 {
23205 eassert (s->first_glyph->type == IMAGE_GLYPH);
23206 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23207 eassert (s->img);
23208 s->slice = s->first_glyph->slice.img;
23209 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23210 s->font = s->face->font;
23211 s->width = s->first_glyph->pixel_width;
23212
23213 /* Adjust base line for subscript/superscript text. */
23214 s->ybase += s->first_glyph->voffset;
23215 }
23216
23217
23218 /* Fill glyph string S from a sequence of stretch glyphs.
23219
23220 START is the index of the first glyph to consider,
23221 END is the index of the last + 1.
23222
23223 Value is the index of the first glyph not in S. */
23224
23225 static int
23226 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23227 {
23228 struct glyph *glyph, *last;
23229 int voffset, face_id;
23230
23231 eassert (s->first_glyph->type == STRETCH_GLYPH);
23232
23233 glyph = s->row->glyphs[s->area] + start;
23234 last = s->row->glyphs[s->area] + end;
23235 face_id = glyph->face_id;
23236 s->face = FACE_FROM_ID (s->f, face_id);
23237 s->font = s->face->font;
23238 s->width = glyph->pixel_width;
23239 s->nchars = 1;
23240 voffset = glyph->voffset;
23241
23242 for (++glyph;
23243 (glyph < last
23244 && glyph->type == STRETCH_GLYPH
23245 && glyph->voffset == voffset
23246 && glyph->face_id == face_id);
23247 ++glyph)
23248 s->width += glyph->pixel_width;
23249
23250 /* Adjust base line for subscript/superscript text. */
23251 s->ybase += voffset;
23252
23253 /* The case that face->gc == 0 is handled when drawing the glyph
23254 string by calling PREPARE_FACE_FOR_DISPLAY. */
23255 eassert (s->face);
23256 return glyph - s->row->glyphs[s->area];
23257 }
23258
23259 static struct font_metrics *
23260 get_per_char_metric (struct font *font, XChar2b *char2b)
23261 {
23262 static struct font_metrics metrics;
23263 unsigned code;
23264
23265 if (! font)
23266 return NULL;
23267 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23268 if (code == FONT_INVALID_CODE)
23269 return NULL;
23270 font->driver->text_extents (font, &code, 1, &metrics);
23271 return &metrics;
23272 }
23273
23274 /* EXPORT for RIF:
23275 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23276 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23277 assumed to be zero. */
23278
23279 void
23280 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23281 {
23282 *left = *right = 0;
23283
23284 if (glyph->type == CHAR_GLYPH)
23285 {
23286 struct face *face;
23287 XChar2b char2b;
23288 struct font_metrics *pcm;
23289
23290 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23291 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23292 {
23293 if (pcm->rbearing > pcm->width)
23294 *right = pcm->rbearing - pcm->width;
23295 if (pcm->lbearing < 0)
23296 *left = -pcm->lbearing;
23297 }
23298 }
23299 else if (glyph->type == COMPOSITE_GLYPH)
23300 {
23301 if (! glyph->u.cmp.automatic)
23302 {
23303 struct composition *cmp = composition_table[glyph->u.cmp.id];
23304
23305 if (cmp->rbearing > cmp->pixel_width)
23306 *right = cmp->rbearing - cmp->pixel_width;
23307 if (cmp->lbearing < 0)
23308 *left = - cmp->lbearing;
23309 }
23310 else
23311 {
23312 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23313 struct font_metrics metrics;
23314
23315 composition_gstring_width (gstring, glyph->slice.cmp.from,
23316 glyph->slice.cmp.to + 1, &metrics);
23317 if (metrics.rbearing > metrics.width)
23318 *right = metrics.rbearing - metrics.width;
23319 if (metrics.lbearing < 0)
23320 *left = - metrics.lbearing;
23321 }
23322 }
23323 }
23324
23325
23326 /* Return the index of the first glyph preceding glyph string S that
23327 is overwritten by S because of S's left overhang. Value is -1
23328 if no glyphs are overwritten. */
23329
23330 static int
23331 left_overwritten (struct glyph_string *s)
23332 {
23333 int k;
23334
23335 if (s->left_overhang)
23336 {
23337 int x = 0, i;
23338 struct glyph *glyphs = s->row->glyphs[s->area];
23339 int first = s->first_glyph - glyphs;
23340
23341 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23342 x -= glyphs[i].pixel_width;
23343
23344 k = i + 1;
23345 }
23346 else
23347 k = -1;
23348
23349 return k;
23350 }
23351
23352
23353 /* Return the index of the first glyph preceding glyph string S that
23354 is overwriting S because of its right overhang. Value is -1 if no
23355 glyph in front of S overwrites S. */
23356
23357 static int
23358 left_overwriting (struct glyph_string *s)
23359 {
23360 int i, k, x;
23361 struct glyph *glyphs = s->row->glyphs[s->area];
23362 int first = s->first_glyph - glyphs;
23363
23364 k = -1;
23365 x = 0;
23366 for (i = first - 1; i >= 0; --i)
23367 {
23368 int left, right;
23369 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23370 if (x + right > 0)
23371 k = i;
23372 x -= glyphs[i].pixel_width;
23373 }
23374
23375 return k;
23376 }
23377
23378
23379 /* Return the index of the last glyph following glyph string S that is
23380 overwritten by S because of S's right overhang. Value is -1 if
23381 no such glyph is found. */
23382
23383 static int
23384 right_overwritten (struct glyph_string *s)
23385 {
23386 int k = -1;
23387
23388 if (s->right_overhang)
23389 {
23390 int x = 0, i;
23391 struct glyph *glyphs = s->row->glyphs[s->area];
23392 int first = (s->first_glyph - glyphs
23393 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23394 int end = s->row->used[s->area];
23395
23396 for (i = first; i < end && s->right_overhang > x; ++i)
23397 x += glyphs[i].pixel_width;
23398
23399 k = i;
23400 }
23401
23402 return k;
23403 }
23404
23405
23406 /* Return the index of the last glyph following glyph string S that
23407 overwrites S because of its left overhang. Value is negative
23408 if no such glyph is found. */
23409
23410 static int
23411 right_overwriting (struct glyph_string *s)
23412 {
23413 int i, k, x;
23414 int end = s->row->used[s->area];
23415 struct glyph *glyphs = s->row->glyphs[s->area];
23416 int first = (s->first_glyph - glyphs
23417 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23418
23419 k = -1;
23420 x = 0;
23421 for (i = first; i < end; ++i)
23422 {
23423 int left, right;
23424 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23425 if (x - left < 0)
23426 k = i;
23427 x += glyphs[i].pixel_width;
23428 }
23429
23430 return k;
23431 }
23432
23433
23434 /* Set background width of glyph string S. START is the index of the
23435 first glyph following S. LAST_X is the right-most x-position + 1
23436 in the drawing area. */
23437
23438 static void
23439 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23440 {
23441 /* If the face of this glyph string has to be drawn to the end of
23442 the drawing area, set S->extends_to_end_of_line_p. */
23443
23444 if (start == s->row->used[s->area]
23445 && s->area == TEXT_AREA
23446 && ((s->row->fill_line_p
23447 && (s->hl == DRAW_NORMAL_TEXT
23448 || s->hl == DRAW_IMAGE_RAISED
23449 || s->hl == DRAW_IMAGE_SUNKEN))
23450 || s->hl == DRAW_MOUSE_FACE))
23451 s->extends_to_end_of_line_p = 1;
23452
23453 /* If S extends its face to the end of the line, set its
23454 background_width to the distance to the right edge of the drawing
23455 area. */
23456 if (s->extends_to_end_of_line_p)
23457 s->background_width = last_x - s->x + 1;
23458 else
23459 s->background_width = s->width;
23460 }
23461
23462
23463 /* Compute overhangs and x-positions for glyph string S and its
23464 predecessors, or successors. X is the starting x-position for S.
23465 BACKWARD_P non-zero means process predecessors. */
23466
23467 static void
23468 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23469 {
23470 if (backward_p)
23471 {
23472 while (s)
23473 {
23474 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23475 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23476 x -= s->width;
23477 s->x = x;
23478 s = s->prev;
23479 }
23480 }
23481 else
23482 {
23483 while (s)
23484 {
23485 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23486 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23487 s->x = x;
23488 x += s->width;
23489 s = s->next;
23490 }
23491 }
23492 }
23493
23494
23495
23496 /* The following macros are only called from draw_glyphs below.
23497 They reference the following parameters of that function directly:
23498 `w', `row', `area', and `overlap_p'
23499 as well as the following local variables:
23500 `s', `f', and `hdc' (in W32) */
23501
23502 #ifdef HAVE_NTGUI
23503 /* On W32, silently add local `hdc' variable to argument list of
23504 init_glyph_string. */
23505 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23506 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23507 #else
23508 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23509 init_glyph_string (s, char2b, w, row, area, start, hl)
23510 #endif
23511
23512 /* Add a glyph string for a stretch glyph to the list of strings
23513 between HEAD and TAIL. START is the index of the stretch glyph in
23514 row area AREA of glyph row ROW. END is the index of the last glyph
23515 in that glyph row area. X is the current output position assigned
23516 to the new glyph string constructed. HL overrides that face of the
23517 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23518 is the right-most x-position of the drawing area. */
23519
23520 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23521 and below -- keep them on one line. */
23522 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23523 do \
23524 { \
23525 s = alloca (sizeof *s); \
23526 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23527 START = fill_stretch_glyph_string (s, START, END); \
23528 append_glyph_string (&HEAD, &TAIL, s); \
23529 s->x = (X); \
23530 } \
23531 while (0)
23532
23533
23534 /* Add a glyph string for an image glyph to the list of strings
23535 between HEAD and TAIL. START is the index of the image glyph in
23536 row area AREA of glyph row ROW. END is the index of the last glyph
23537 in that glyph row area. X is the current output position assigned
23538 to the new glyph string constructed. HL overrides that face of the
23539 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23540 is the right-most x-position of the drawing area. */
23541
23542 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23543 do \
23544 { \
23545 s = alloca (sizeof *s); \
23546 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23547 fill_image_glyph_string (s); \
23548 append_glyph_string (&HEAD, &TAIL, s); \
23549 ++START; \
23550 s->x = (X); \
23551 } \
23552 while (0)
23553
23554
23555 /* Add a glyph string for a sequence of character glyphs to the list
23556 of strings between HEAD and TAIL. START is the index of the first
23557 glyph in row area AREA of glyph row ROW that is part of the new
23558 glyph string. END is the index of the last glyph in that glyph row
23559 area. X is the current output position assigned to the new glyph
23560 string constructed. HL overrides that face of the glyph; e.g. it
23561 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23562 right-most x-position of the drawing area. */
23563
23564 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23565 do \
23566 { \
23567 int face_id; \
23568 XChar2b *char2b; \
23569 \
23570 face_id = (row)->glyphs[area][START].face_id; \
23571 \
23572 s = alloca (sizeof *s); \
23573 char2b = alloca ((END - START) * sizeof *char2b); \
23574 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23575 append_glyph_string (&HEAD, &TAIL, s); \
23576 s->x = (X); \
23577 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23578 } \
23579 while (0)
23580
23581
23582 /* Add a glyph string for a composite sequence to the list of strings
23583 between HEAD and TAIL. START is the index of the first glyph in
23584 row area AREA of glyph row ROW that is part of the new glyph
23585 string. END is the index of the last glyph in that glyph row area.
23586 X is the current output position assigned to the new glyph string
23587 constructed. HL overrides that face of the glyph; e.g. it is
23588 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23589 x-position of the drawing area. */
23590
23591 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23592 do { \
23593 int face_id = (row)->glyphs[area][START].face_id; \
23594 struct face *base_face = FACE_FROM_ID (f, face_id); \
23595 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23596 struct composition *cmp = composition_table[cmp_id]; \
23597 XChar2b *char2b; \
23598 struct glyph_string *first_s = NULL; \
23599 int n; \
23600 \
23601 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23602 \
23603 /* Make glyph_strings for each glyph sequence that is drawable by \
23604 the same face, and append them to HEAD/TAIL. */ \
23605 for (n = 0; n < cmp->glyph_len;) \
23606 { \
23607 s = alloca (sizeof *s); \
23608 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23609 append_glyph_string (&(HEAD), &(TAIL), s); \
23610 s->cmp = cmp; \
23611 s->cmp_from = n; \
23612 s->x = (X); \
23613 if (n == 0) \
23614 first_s = s; \
23615 n = fill_composite_glyph_string (s, base_face, overlaps); \
23616 } \
23617 \
23618 ++START; \
23619 s = first_s; \
23620 } while (0)
23621
23622
23623 /* Add a glyph string for a glyph-string sequence to the list of strings
23624 between HEAD and TAIL. */
23625
23626 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23627 do { \
23628 int face_id; \
23629 XChar2b *char2b; \
23630 Lisp_Object gstring; \
23631 \
23632 face_id = (row)->glyphs[area][START].face_id; \
23633 gstring = (composition_gstring_from_id \
23634 ((row)->glyphs[area][START].u.cmp.id)); \
23635 s = alloca (sizeof *s); \
23636 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23637 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23638 append_glyph_string (&(HEAD), &(TAIL), s); \
23639 s->x = (X); \
23640 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23641 } while (0)
23642
23643
23644 /* Add a glyph string for a sequence of glyphless character's glyphs
23645 to the list of strings between HEAD and TAIL. The meanings of
23646 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23647
23648 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23649 do \
23650 { \
23651 int face_id; \
23652 \
23653 face_id = (row)->glyphs[area][START].face_id; \
23654 \
23655 s = alloca (sizeof *s); \
23656 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23657 append_glyph_string (&HEAD, &TAIL, s); \
23658 s->x = (X); \
23659 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23660 overlaps); \
23661 } \
23662 while (0)
23663
23664
23665 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23666 of AREA of glyph row ROW on window W between indices START and END.
23667 HL overrides the face for drawing glyph strings, e.g. it is
23668 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23669 x-positions of the drawing area.
23670
23671 This is an ugly monster macro construct because we must use alloca
23672 to allocate glyph strings (because draw_glyphs can be called
23673 asynchronously). */
23674
23675 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23676 do \
23677 { \
23678 HEAD = TAIL = NULL; \
23679 while (START < END) \
23680 { \
23681 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23682 switch (first_glyph->type) \
23683 { \
23684 case CHAR_GLYPH: \
23685 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23686 HL, X, LAST_X); \
23687 break; \
23688 \
23689 case COMPOSITE_GLYPH: \
23690 if (first_glyph->u.cmp.automatic) \
23691 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23692 HL, X, LAST_X); \
23693 else \
23694 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23695 HL, X, LAST_X); \
23696 break; \
23697 \
23698 case STRETCH_GLYPH: \
23699 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23700 HL, X, LAST_X); \
23701 break; \
23702 \
23703 case IMAGE_GLYPH: \
23704 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23705 HL, X, LAST_X); \
23706 break; \
23707 \
23708 case GLYPHLESS_GLYPH: \
23709 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23710 HL, X, LAST_X); \
23711 break; \
23712 \
23713 default: \
23714 emacs_abort (); \
23715 } \
23716 \
23717 if (s) \
23718 { \
23719 set_glyph_string_background_width (s, START, LAST_X); \
23720 (X) += s->width; \
23721 } \
23722 } \
23723 } while (0)
23724
23725
23726 /* Draw glyphs between START and END in AREA of ROW on window W,
23727 starting at x-position X. X is relative to AREA in W. HL is a
23728 face-override with the following meaning:
23729
23730 DRAW_NORMAL_TEXT draw normally
23731 DRAW_CURSOR draw in cursor face
23732 DRAW_MOUSE_FACE draw in mouse face.
23733 DRAW_INVERSE_VIDEO draw in mode line face
23734 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23735 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23736
23737 If OVERLAPS is non-zero, draw only the foreground of characters and
23738 clip to the physical height of ROW. Non-zero value also defines
23739 the overlapping part to be drawn:
23740
23741 OVERLAPS_PRED overlap with preceding rows
23742 OVERLAPS_SUCC overlap with succeeding rows
23743 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23744 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23745
23746 Value is the x-position reached, relative to AREA of W. */
23747
23748 static int
23749 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23750 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23751 enum draw_glyphs_face hl, int overlaps)
23752 {
23753 struct glyph_string *head, *tail;
23754 struct glyph_string *s;
23755 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23756 int i, j, x_reached, last_x, area_left = 0;
23757 struct frame *f = XFRAME (WINDOW_FRAME (w));
23758 DECLARE_HDC (hdc);
23759
23760 ALLOCATE_HDC (hdc, f);
23761
23762 /* Let's rather be paranoid than getting a SEGV. */
23763 end = min (end, row->used[area]);
23764 start = clip_to_bounds (0, start, end);
23765
23766 /* Translate X to frame coordinates. Set last_x to the right
23767 end of the drawing area. */
23768 if (row->full_width_p)
23769 {
23770 /* X is relative to the left edge of W, without scroll bars
23771 or fringes. */
23772 area_left = WINDOW_LEFT_EDGE_X (w);
23773 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23774 }
23775 else
23776 {
23777 area_left = window_box_left (w, area);
23778 last_x = area_left + window_box_width (w, area);
23779 }
23780 x += area_left;
23781
23782 /* Build a doubly-linked list of glyph_string structures between
23783 head and tail from what we have to draw. Note that the macro
23784 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23785 the reason we use a separate variable `i'. */
23786 i = start;
23787 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23788 if (tail)
23789 x_reached = tail->x + tail->background_width;
23790 else
23791 x_reached = x;
23792
23793 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23794 the row, redraw some glyphs in front or following the glyph
23795 strings built above. */
23796 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23797 {
23798 struct glyph_string *h, *t;
23799 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23800 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23801 int check_mouse_face = 0;
23802 int dummy_x = 0;
23803
23804 /* If mouse highlighting is on, we may need to draw adjacent
23805 glyphs using mouse-face highlighting. */
23806 if (area == TEXT_AREA && row->mouse_face_p
23807 && hlinfo->mouse_face_beg_row >= 0
23808 && hlinfo->mouse_face_end_row >= 0)
23809 {
23810 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23811
23812 if (row_vpos >= hlinfo->mouse_face_beg_row
23813 && row_vpos <= hlinfo->mouse_face_end_row)
23814 {
23815 check_mouse_face = 1;
23816 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23817 ? hlinfo->mouse_face_beg_col : 0;
23818 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23819 ? hlinfo->mouse_face_end_col
23820 : row->used[TEXT_AREA];
23821 }
23822 }
23823
23824 /* Compute overhangs for all glyph strings. */
23825 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23826 for (s = head; s; s = s->next)
23827 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23828
23829 /* Prepend glyph strings for glyphs in front of the first glyph
23830 string that are overwritten because of the first glyph
23831 string's left overhang. The background of all strings
23832 prepended must be drawn because the first glyph string
23833 draws over it. */
23834 i = left_overwritten (head);
23835 if (i >= 0)
23836 {
23837 enum draw_glyphs_face overlap_hl;
23838
23839 /* If this row contains mouse highlighting, attempt to draw
23840 the overlapped glyphs with the correct highlight. This
23841 code fails if the overlap encompasses more than one glyph
23842 and mouse-highlight spans only some of these glyphs.
23843 However, making it work perfectly involves a lot more
23844 code, and I don't know if the pathological case occurs in
23845 practice, so we'll stick to this for now. --- cyd */
23846 if (check_mouse_face
23847 && mouse_beg_col < start && mouse_end_col > i)
23848 overlap_hl = DRAW_MOUSE_FACE;
23849 else
23850 overlap_hl = DRAW_NORMAL_TEXT;
23851
23852 j = i;
23853 BUILD_GLYPH_STRINGS (j, start, h, t,
23854 overlap_hl, dummy_x, last_x);
23855 start = i;
23856 compute_overhangs_and_x (t, head->x, 1);
23857 prepend_glyph_string_lists (&head, &tail, h, t);
23858 clip_head = head;
23859 }
23860
23861 /* Prepend glyph strings for glyphs in front of the first glyph
23862 string that overwrite that glyph string because of their
23863 right overhang. For these strings, only the foreground must
23864 be drawn, because it draws over the glyph string at `head'.
23865 The background must not be drawn because this would overwrite
23866 right overhangs of preceding glyphs for which no glyph
23867 strings exist. */
23868 i = left_overwriting (head);
23869 if (i >= 0)
23870 {
23871 enum draw_glyphs_face overlap_hl;
23872
23873 if (check_mouse_face
23874 && mouse_beg_col < start && mouse_end_col > i)
23875 overlap_hl = DRAW_MOUSE_FACE;
23876 else
23877 overlap_hl = DRAW_NORMAL_TEXT;
23878
23879 clip_head = head;
23880 BUILD_GLYPH_STRINGS (i, start, h, t,
23881 overlap_hl, dummy_x, last_x);
23882 for (s = h; s; s = s->next)
23883 s->background_filled_p = 1;
23884 compute_overhangs_and_x (t, head->x, 1);
23885 prepend_glyph_string_lists (&head, &tail, h, t);
23886 }
23887
23888 /* Append glyphs strings for glyphs following the last glyph
23889 string tail that are overwritten by tail. The background of
23890 these strings has to be drawn because tail's foreground draws
23891 over it. */
23892 i = right_overwritten (tail);
23893 if (i >= 0)
23894 {
23895 enum draw_glyphs_face overlap_hl;
23896
23897 if (check_mouse_face
23898 && mouse_beg_col < i && mouse_end_col > end)
23899 overlap_hl = DRAW_MOUSE_FACE;
23900 else
23901 overlap_hl = DRAW_NORMAL_TEXT;
23902
23903 BUILD_GLYPH_STRINGS (end, i, h, t,
23904 overlap_hl, x, last_x);
23905 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23906 we don't have `end = i;' here. */
23907 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23908 append_glyph_string_lists (&head, &tail, h, t);
23909 clip_tail = tail;
23910 }
23911
23912 /* Append glyph strings for glyphs following the last glyph
23913 string tail that overwrite tail. The foreground of such
23914 glyphs has to be drawn because it writes into the background
23915 of tail. The background must not be drawn because it could
23916 paint over the foreground of following glyphs. */
23917 i = right_overwriting (tail);
23918 if (i >= 0)
23919 {
23920 enum draw_glyphs_face overlap_hl;
23921 if (check_mouse_face
23922 && mouse_beg_col < i && mouse_end_col > end)
23923 overlap_hl = DRAW_MOUSE_FACE;
23924 else
23925 overlap_hl = DRAW_NORMAL_TEXT;
23926
23927 clip_tail = tail;
23928 i++; /* We must include the Ith glyph. */
23929 BUILD_GLYPH_STRINGS (end, i, h, t,
23930 overlap_hl, x, last_x);
23931 for (s = h; s; s = s->next)
23932 s->background_filled_p = 1;
23933 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23934 append_glyph_string_lists (&head, &tail, h, t);
23935 }
23936 if (clip_head || clip_tail)
23937 for (s = head; s; s = s->next)
23938 {
23939 s->clip_head = clip_head;
23940 s->clip_tail = clip_tail;
23941 }
23942 }
23943
23944 /* Draw all strings. */
23945 for (s = head; s; s = s->next)
23946 FRAME_RIF (f)->draw_glyph_string (s);
23947
23948 #ifndef HAVE_NS
23949 /* When focus a sole frame and move horizontally, this sets on_p to 0
23950 causing a failure to erase prev cursor position. */
23951 if (area == TEXT_AREA
23952 && !row->full_width_p
23953 /* When drawing overlapping rows, only the glyph strings'
23954 foreground is drawn, which doesn't erase a cursor
23955 completely. */
23956 && !overlaps)
23957 {
23958 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23959 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23960 : (tail ? tail->x + tail->background_width : x));
23961 x0 -= area_left;
23962 x1 -= area_left;
23963
23964 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23965 row->y, MATRIX_ROW_BOTTOM_Y (row));
23966 }
23967 #endif
23968
23969 /* Value is the x-position up to which drawn, relative to AREA of W.
23970 This doesn't include parts drawn because of overhangs. */
23971 if (row->full_width_p)
23972 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23973 else
23974 x_reached -= area_left;
23975
23976 RELEASE_HDC (hdc, f);
23977
23978 return x_reached;
23979 }
23980
23981 /* Expand row matrix if too narrow. Don't expand if area
23982 is not present. */
23983
23984 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23985 { \
23986 if (!it->f->fonts_changed \
23987 && (it->glyph_row->glyphs[area] \
23988 < it->glyph_row->glyphs[area + 1])) \
23989 { \
23990 it->w->ncols_scale_factor++; \
23991 it->f->fonts_changed = 1; \
23992 } \
23993 }
23994
23995 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23996 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23997
23998 static void
23999 append_glyph (struct it *it)
24000 {
24001 struct glyph *glyph;
24002 enum glyph_row_area area = it->area;
24003
24004 eassert (it->glyph_row);
24005 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24006
24007 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24008 if (glyph < it->glyph_row->glyphs[area + 1])
24009 {
24010 /* If the glyph row is reversed, we need to prepend the glyph
24011 rather than append it. */
24012 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24013 {
24014 struct glyph *g;
24015
24016 /* Make room for the additional glyph. */
24017 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24018 g[1] = *g;
24019 glyph = it->glyph_row->glyphs[area];
24020 }
24021 glyph->charpos = CHARPOS (it->position);
24022 glyph->object = it->object;
24023 if (it->pixel_width > 0)
24024 {
24025 glyph->pixel_width = it->pixel_width;
24026 glyph->padding_p = 0;
24027 }
24028 else
24029 {
24030 /* Assure at least 1-pixel width. Otherwise, cursor can't
24031 be displayed correctly. */
24032 glyph->pixel_width = 1;
24033 glyph->padding_p = 1;
24034 }
24035 glyph->ascent = it->ascent;
24036 glyph->descent = it->descent;
24037 glyph->voffset = it->voffset;
24038 glyph->type = CHAR_GLYPH;
24039 glyph->avoid_cursor_p = it->avoid_cursor_p;
24040 glyph->multibyte_p = it->multibyte_p;
24041 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24042 {
24043 /* In R2L rows, the left and the right box edges need to be
24044 drawn in reverse direction. */
24045 glyph->right_box_line_p = it->start_of_box_run_p;
24046 glyph->left_box_line_p = it->end_of_box_run_p;
24047 }
24048 else
24049 {
24050 glyph->left_box_line_p = it->start_of_box_run_p;
24051 glyph->right_box_line_p = it->end_of_box_run_p;
24052 }
24053 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24054 || it->phys_descent > it->descent);
24055 glyph->glyph_not_available_p = it->glyph_not_available_p;
24056 glyph->face_id = it->face_id;
24057 glyph->u.ch = it->char_to_display;
24058 glyph->slice.img = null_glyph_slice;
24059 glyph->font_type = FONT_TYPE_UNKNOWN;
24060 if (it->bidi_p)
24061 {
24062 glyph->resolved_level = it->bidi_it.resolved_level;
24063 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24064 emacs_abort ();
24065 glyph->bidi_type = it->bidi_it.type;
24066 }
24067 else
24068 {
24069 glyph->resolved_level = 0;
24070 glyph->bidi_type = UNKNOWN_BT;
24071 }
24072 ++it->glyph_row->used[area];
24073 }
24074 else
24075 IT_EXPAND_MATRIX_WIDTH (it, area);
24076 }
24077
24078 /* Store one glyph for the composition IT->cmp_it.id in
24079 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24080 non-null. */
24081
24082 static void
24083 append_composite_glyph (struct it *it)
24084 {
24085 struct glyph *glyph;
24086 enum glyph_row_area area = it->area;
24087
24088 eassert (it->glyph_row);
24089
24090 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24091 if (glyph < it->glyph_row->glyphs[area + 1])
24092 {
24093 /* If the glyph row is reversed, we need to prepend the glyph
24094 rather than append it. */
24095 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24096 {
24097 struct glyph *g;
24098
24099 /* Make room for the new glyph. */
24100 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24101 g[1] = *g;
24102 glyph = it->glyph_row->glyphs[it->area];
24103 }
24104 glyph->charpos = it->cmp_it.charpos;
24105 glyph->object = it->object;
24106 glyph->pixel_width = it->pixel_width;
24107 glyph->ascent = it->ascent;
24108 glyph->descent = it->descent;
24109 glyph->voffset = it->voffset;
24110 glyph->type = COMPOSITE_GLYPH;
24111 if (it->cmp_it.ch < 0)
24112 {
24113 glyph->u.cmp.automatic = 0;
24114 glyph->u.cmp.id = it->cmp_it.id;
24115 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24116 }
24117 else
24118 {
24119 glyph->u.cmp.automatic = 1;
24120 glyph->u.cmp.id = it->cmp_it.id;
24121 glyph->slice.cmp.from = it->cmp_it.from;
24122 glyph->slice.cmp.to = it->cmp_it.to - 1;
24123 }
24124 glyph->avoid_cursor_p = it->avoid_cursor_p;
24125 glyph->multibyte_p = it->multibyte_p;
24126 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24127 {
24128 /* In R2L rows, the left and the right box edges need to be
24129 drawn in reverse direction. */
24130 glyph->right_box_line_p = it->start_of_box_run_p;
24131 glyph->left_box_line_p = it->end_of_box_run_p;
24132 }
24133 else
24134 {
24135 glyph->left_box_line_p = it->start_of_box_run_p;
24136 glyph->right_box_line_p = it->end_of_box_run_p;
24137 }
24138 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24139 || it->phys_descent > it->descent);
24140 glyph->padding_p = 0;
24141 glyph->glyph_not_available_p = 0;
24142 glyph->face_id = it->face_id;
24143 glyph->font_type = FONT_TYPE_UNKNOWN;
24144 if (it->bidi_p)
24145 {
24146 glyph->resolved_level = it->bidi_it.resolved_level;
24147 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24148 emacs_abort ();
24149 glyph->bidi_type = it->bidi_it.type;
24150 }
24151 ++it->glyph_row->used[area];
24152 }
24153 else
24154 IT_EXPAND_MATRIX_WIDTH (it, area);
24155 }
24156
24157
24158 /* Change IT->ascent and IT->height according to the setting of
24159 IT->voffset. */
24160
24161 static void
24162 take_vertical_position_into_account (struct it *it)
24163 {
24164 if (it->voffset)
24165 {
24166 if (it->voffset < 0)
24167 /* Increase the ascent so that we can display the text higher
24168 in the line. */
24169 it->ascent -= it->voffset;
24170 else
24171 /* Increase the descent so that we can display the text lower
24172 in the line. */
24173 it->descent += it->voffset;
24174 }
24175 }
24176
24177
24178 /* Produce glyphs/get display metrics for the image IT is loaded with.
24179 See the description of struct display_iterator in dispextern.h for
24180 an overview of struct display_iterator. */
24181
24182 static void
24183 produce_image_glyph (struct it *it)
24184 {
24185 struct image *img;
24186 struct face *face;
24187 int glyph_ascent, crop;
24188 struct glyph_slice slice;
24189
24190 eassert (it->what == IT_IMAGE);
24191
24192 face = FACE_FROM_ID (it->f, it->face_id);
24193 eassert (face);
24194 /* Make sure X resources of the face is loaded. */
24195 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24196
24197 if (it->image_id < 0)
24198 {
24199 /* Fringe bitmap. */
24200 it->ascent = it->phys_ascent = 0;
24201 it->descent = it->phys_descent = 0;
24202 it->pixel_width = 0;
24203 it->nglyphs = 0;
24204 return;
24205 }
24206
24207 img = IMAGE_FROM_ID (it->f, it->image_id);
24208 eassert (img);
24209 /* Make sure X resources of the image is loaded. */
24210 prepare_image_for_display (it->f, img);
24211
24212 slice.x = slice.y = 0;
24213 slice.width = img->width;
24214 slice.height = img->height;
24215
24216 if (INTEGERP (it->slice.x))
24217 slice.x = XINT (it->slice.x);
24218 else if (FLOATP (it->slice.x))
24219 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24220
24221 if (INTEGERP (it->slice.y))
24222 slice.y = XINT (it->slice.y);
24223 else if (FLOATP (it->slice.y))
24224 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24225
24226 if (INTEGERP (it->slice.width))
24227 slice.width = XINT (it->slice.width);
24228 else if (FLOATP (it->slice.width))
24229 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24230
24231 if (INTEGERP (it->slice.height))
24232 slice.height = XINT (it->slice.height);
24233 else if (FLOATP (it->slice.height))
24234 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24235
24236 if (slice.x >= img->width)
24237 slice.x = img->width;
24238 if (slice.y >= img->height)
24239 slice.y = img->height;
24240 if (slice.x + slice.width >= img->width)
24241 slice.width = img->width - slice.x;
24242 if (slice.y + slice.height > img->height)
24243 slice.height = img->height - slice.y;
24244
24245 if (slice.width == 0 || slice.height == 0)
24246 return;
24247
24248 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24249
24250 it->descent = slice.height - glyph_ascent;
24251 if (slice.y == 0)
24252 it->descent += img->vmargin;
24253 if (slice.y + slice.height == img->height)
24254 it->descent += img->vmargin;
24255 it->phys_descent = it->descent;
24256
24257 it->pixel_width = slice.width;
24258 if (slice.x == 0)
24259 it->pixel_width += img->hmargin;
24260 if (slice.x + slice.width == img->width)
24261 it->pixel_width += img->hmargin;
24262
24263 /* It's quite possible for images to have an ascent greater than
24264 their height, so don't get confused in that case. */
24265 if (it->descent < 0)
24266 it->descent = 0;
24267
24268 it->nglyphs = 1;
24269
24270 if (face->box != FACE_NO_BOX)
24271 {
24272 if (face->box_line_width > 0)
24273 {
24274 if (slice.y == 0)
24275 it->ascent += face->box_line_width;
24276 if (slice.y + slice.height == img->height)
24277 it->descent += face->box_line_width;
24278 }
24279
24280 if (it->start_of_box_run_p && slice.x == 0)
24281 it->pixel_width += eabs (face->box_line_width);
24282 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24283 it->pixel_width += eabs (face->box_line_width);
24284 }
24285
24286 take_vertical_position_into_account (it);
24287
24288 /* Automatically crop wide image glyphs at right edge so we can
24289 draw the cursor on same display row. */
24290 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24291 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24292 {
24293 it->pixel_width -= crop;
24294 slice.width -= crop;
24295 }
24296
24297 if (it->glyph_row)
24298 {
24299 struct glyph *glyph;
24300 enum glyph_row_area area = it->area;
24301
24302 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24303 if (glyph < it->glyph_row->glyphs[area + 1])
24304 {
24305 glyph->charpos = CHARPOS (it->position);
24306 glyph->object = it->object;
24307 glyph->pixel_width = it->pixel_width;
24308 glyph->ascent = glyph_ascent;
24309 glyph->descent = it->descent;
24310 glyph->voffset = it->voffset;
24311 glyph->type = IMAGE_GLYPH;
24312 glyph->avoid_cursor_p = it->avoid_cursor_p;
24313 glyph->multibyte_p = it->multibyte_p;
24314 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24315 {
24316 /* In R2L rows, the left and the right box edges need to be
24317 drawn in reverse direction. */
24318 glyph->right_box_line_p = it->start_of_box_run_p;
24319 glyph->left_box_line_p = it->end_of_box_run_p;
24320 }
24321 else
24322 {
24323 glyph->left_box_line_p = it->start_of_box_run_p;
24324 glyph->right_box_line_p = it->end_of_box_run_p;
24325 }
24326 glyph->overlaps_vertically_p = 0;
24327 glyph->padding_p = 0;
24328 glyph->glyph_not_available_p = 0;
24329 glyph->face_id = it->face_id;
24330 glyph->u.img_id = img->id;
24331 glyph->slice.img = slice;
24332 glyph->font_type = FONT_TYPE_UNKNOWN;
24333 if (it->bidi_p)
24334 {
24335 glyph->resolved_level = it->bidi_it.resolved_level;
24336 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24337 emacs_abort ();
24338 glyph->bidi_type = it->bidi_it.type;
24339 }
24340 ++it->glyph_row->used[area];
24341 }
24342 else
24343 IT_EXPAND_MATRIX_WIDTH (it, area);
24344 }
24345 }
24346
24347
24348 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24349 of the glyph, WIDTH and HEIGHT are the width and height of the
24350 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24351
24352 static void
24353 append_stretch_glyph (struct it *it, Lisp_Object object,
24354 int width, int height, int ascent)
24355 {
24356 struct glyph *glyph;
24357 enum glyph_row_area area = it->area;
24358
24359 eassert (ascent >= 0 && ascent <= height);
24360
24361 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24362 if (glyph < it->glyph_row->glyphs[area + 1])
24363 {
24364 /* If the glyph row is reversed, we need to prepend the glyph
24365 rather than append it. */
24366 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24367 {
24368 struct glyph *g;
24369
24370 /* Make room for the additional glyph. */
24371 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24372 g[1] = *g;
24373 glyph = it->glyph_row->glyphs[area];
24374 }
24375 glyph->charpos = CHARPOS (it->position);
24376 glyph->object = object;
24377 glyph->pixel_width = width;
24378 glyph->ascent = ascent;
24379 glyph->descent = height - ascent;
24380 glyph->voffset = it->voffset;
24381 glyph->type = STRETCH_GLYPH;
24382 glyph->avoid_cursor_p = it->avoid_cursor_p;
24383 glyph->multibyte_p = it->multibyte_p;
24384 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24385 {
24386 /* In R2L rows, the left and the right box edges need to be
24387 drawn in reverse direction. */
24388 glyph->right_box_line_p = it->start_of_box_run_p;
24389 glyph->left_box_line_p = it->end_of_box_run_p;
24390 }
24391 else
24392 {
24393 glyph->left_box_line_p = it->start_of_box_run_p;
24394 glyph->right_box_line_p = it->end_of_box_run_p;
24395 }
24396 glyph->overlaps_vertically_p = 0;
24397 glyph->padding_p = 0;
24398 glyph->glyph_not_available_p = 0;
24399 glyph->face_id = it->face_id;
24400 glyph->u.stretch.ascent = ascent;
24401 glyph->u.stretch.height = height;
24402 glyph->slice.img = null_glyph_slice;
24403 glyph->font_type = FONT_TYPE_UNKNOWN;
24404 if (it->bidi_p)
24405 {
24406 glyph->resolved_level = it->bidi_it.resolved_level;
24407 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24408 emacs_abort ();
24409 glyph->bidi_type = it->bidi_it.type;
24410 }
24411 else
24412 {
24413 glyph->resolved_level = 0;
24414 glyph->bidi_type = UNKNOWN_BT;
24415 }
24416 ++it->glyph_row->used[area];
24417 }
24418 else
24419 IT_EXPAND_MATRIX_WIDTH (it, area);
24420 }
24421
24422 #endif /* HAVE_WINDOW_SYSTEM */
24423
24424 /* Produce a stretch glyph for iterator IT. IT->object is the value
24425 of the glyph property displayed. The value must be a list
24426 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24427 being recognized:
24428
24429 1. `:width WIDTH' specifies that the space should be WIDTH *
24430 canonical char width wide. WIDTH may be an integer or floating
24431 point number.
24432
24433 2. `:relative-width FACTOR' specifies that the width of the stretch
24434 should be computed from the width of the first character having the
24435 `glyph' property, and should be FACTOR times that width.
24436
24437 3. `:align-to HPOS' specifies that the space should be wide enough
24438 to reach HPOS, a value in canonical character units.
24439
24440 Exactly one of the above pairs must be present.
24441
24442 4. `:height HEIGHT' specifies that the height of the stretch produced
24443 should be HEIGHT, measured in canonical character units.
24444
24445 5. `:relative-height FACTOR' specifies that the height of the
24446 stretch should be FACTOR times the height of the characters having
24447 the glyph property.
24448
24449 Either none or exactly one of 4 or 5 must be present.
24450
24451 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24452 of the stretch should be used for the ascent of the stretch.
24453 ASCENT must be in the range 0 <= ASCENT <= 100. */
24454
24455 void
24456 produce_stretch_glyph (struct it *it)
24457 {
24458 /* (space :width WIDTH :height HEIGHT ...) */
24459 Lisp_Object prop, plist;
24460 int width = 0, height = 0, align_to = -1;
24461 int zero_width_ok_p = 0;
24462 double tem;
24463 struct font *font = NULL;
24464
24465 #ifdef HAVE_WINDOW_SYSTEM
24466 int ascent = 0;
24467 int zero_height_ok_p = 0;
24468
24469 if (FRAME_WINDOW_P (it->f))
24470 {
24471 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24472 font = face->font ? face->font : FRAME_FONT (it->f);
24473 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24474 }
24475 #endif
24476
24477 /* List should start with `space'. */
24478 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24479 plist = XCDR (it->object);
24480
24481 /* Compute the width of the stretch. */
24482 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24483 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24484 {
24485 /* Absolute width `:width WIDTH' specified and valid. */
24486 zero_width_ok_p = 1;
24487 width = (int)tem;
24488 }
24489 #ifdef HAVE_WINDOW_SYSTEM
24490 else if (FRAME_WINDOW_P (it->f)
24491 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24492 {
24493 /* Relative width `:relative-width FACTOR' specified and valid.
24494 Compute the width of the characters having the `glyph'
24495 property. */
24496 struct it it2;
24497 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24498
24499 it2 = *it;
24500 if (it->multibyte_p)
24501 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24502 else
24503 {
24504 it2.c = it2.char_to_display = *p, it2.len = 1;
24505 if (! ASCII_CHAR_P (it2.c))
24506 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24507 }
24508
24509 it2.glyph_row = NULL;
24510 it2.what = IT_CHARACTER;
24511 x_produce_glyphs (&it2);
24512 width = NUMVAL (prop) * it2.pixel_width;
24513 }
24514 #endif /* HAVE_WINDOW_SYSTEM */
24515 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24516 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24517 {
24518 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24519 align_to = (align_to < 0
24520 ? 0
24521 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24522 else if (align_to < 0)
24523 align_to = window_box_left_offset (it->w, TEXT_AREA);
24524 width = max (0, (int)tem + align_to - it->current_x);
24525 zero_width_ok_p = 1;
24526 }
24527 else
24528 /* Nothing specified -> width defaults to canonical char width. */
24529 width = FRAME_COLUMN_WIDTH (it->f);
24530
24531 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24532 width = 1;
24533
24534 #ifdef HAVE_WINDOW_SYSTEM
24535 /* Compute height. */
24536 if (FRAME_WINDOW_P (it->f))
24537 {
24538 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24539 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24540 {
24541 height = (int)tem;
24542 zero_height_ok_p = 1;
24543 }
24544 else if (prop = Fplist_get (plist, QCrelative_height),
24545 NUMVAL (prop) > 0)
24546 height = FONT_HEIGHT (font) * NUMVAL (prop);
24547 else
24548 height = FONT_HEIGHT (font);
24549
24550 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24551 height = 1;
24552
24553 /* Compute percentage of height used for ascent. If
24554 `:ascent ASCENT' is present and valid, use that. Otherwise,
24555 derive the ascent from the font in use. */
24556 if (prop = Fplist_get (plist, QCascent),
24557 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24558 ascent = height * NUMVAL (prop) / 100.0;
24559 else if (!NILP (prop)
24560 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24561 ascent = min (max (0, (int)tem), height);
24562 else
24563 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24564 }
24565 else
24566 #endif /* HAVE_WINDOW_SYSTEM */
24567 height = 1;
24568
24569 if (width > 0 && it->line_wrap != TRUNCATE
24570 && it->current_x + width > it->last_visible_x)
24571 {
24572 width = it->last_visible_x - it->current_x;
24573 #ifdef HAVE_WINDOW_SYSTEM
24574 /* Subtract one more pixel from the stretch width, but only on
24575 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24576 width -= FRAME_WINDOW_P (it->f);
24577 #endif
24578 }
24579
24580 if (width > 0 && height > 0 && it->glyph_row)
24581 {
24582 Lisp_Object o_object = it->object;
24583 Lisp_Object object = it->stack[it->sp - 1].string;
24584 int n = width;
24585
24586 if (!STRINGP (object))
24587 object = it->w->contents;
24588 #ifdef HAVE_WINDOW_SYSTEM
24589 if (FRAME_WINDOW_P (it->f))
24590 append_stretch_glyph (it, object, width, height, ascent);
24591 else
24592 #endif
24593 {
24594 it->object = object;
24595 it->char_to_display = ' ';
24596 it->pixel_width = it->len = 1;
24597 while (n--)
24598 tty_append_glyph (it);
24599 it->object = o_object;
24600 }
24601 }
24602
24603 it->pixel_width = width;
24604 #ifdef HAVE_WINDOW_SYSTEM
24605 if (FRAME_WINDOW_P (it->f))
24606 {
24607 it->ascent = it->phys_ascent = ascent;
24608 it->descent = it->phys_descent = height - it->ascent;
24609 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24610 take_vertical_position_into_account (it);
24611 }
24612 else
24613 #endif
24614 it->nglyphs = width;
24615 }
24616
24617 /* Get information about special display element WHAT in an
24618 environment described by IT. WHAT is one of IT_TRUNCATION or
24619 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24620 non-null glyph_row member. This function ensures that fields like
24621 face_id, c, len of IT are left untouched. */
24622
24623 static void
24624 produce_special_glyphs (struct it *it, enum display_element_type what)
24625 {
24626 struct it temp_it;
24627 Lisp_Object gc;
24628 GLYPH glyph;
24629
24630 temp_it = *it;
24631 temp_it.object = make_number (0);
24632 memset (&temp_it.current, 0, sizeof temp_it.current);
24633
24634 if (what == IT_CONTINUATION)
24635 {
24636 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24637 if (it->bidi_it.paragraph_dir == R2L)
24638 SET_GLYPH_FROM_CHAR (glyph, '/');
24639 else
24640 SET_GLYPH_FROM_CHAR (glyph, '\\');
24641 if (it->dp
24642 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24643 {
24644 /* FIXME: Should we mirror GC for R2L lines? */
24645 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24646 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24647 }
24648 }
24649 else if (what == IT_TRUNCATION)
24650 {
24651 /* Truncation glyph. */
24652 SET_GLYPH_FROM_CHAR (glyph, '$');
24653 if (it->dp
24654 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24655 {
24656 /* FIXME: Should we mirror GC for R2L lines? */
24657 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24658 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24659 }
24660 }
24661 else
24662 emacs_abort ();
24663
24664 #ifdef HAVE_WINDOW_SYSTEM
24665 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24666 is turned off, we precede the truncation/continuation glyphs by a
24667 stretch glyph whose width is computed such that these special
24668 glyphs are aligned at the window margin, even when very different
24669 fonts are used in different glyph rows. */
24670 if (FRAME_WINDOW_P (temp_it.f)
24671 /* init_iterator calls this with it->glyph_row == NULL, and it
24672 wants only the pixel width of the truncation/continuation
24673 glyphs. */
24674 && temp_it.glyph_row
24675 /* insert_left_trunc_glyphs calls us at the beginning of the
24676 row, and it has its own calculation of the stretch glyph
24677 width. */
24678 && temp_it.glyph_row->used[TEXT_AREA] > 0
24679 && (temp_it.glyph_row->reversed_p
24680 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24681 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24682 {
24683 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24684
24685 if (stretch_width > 0)
24686 {
24687 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24688 struct font *font =
24689 face->font ? face->font : FRAME_FONT (temp_it.f);
24690 int stretch_ascent =
24691 (((temp_it.ascent + temp_it.descent)
24692 * FONT_BASE (font)) / FONT_HEIGHT (font));
24693
24694 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24695 temp_it.ascent + temp_it.descent,
24696 stretch_ascent);
24697 }
24698 }
24699 #endif
24700
24701 temp_it.dp = NULL;
24702 temp_it.what = IT_CHARACTER;
24703 temp_it.len = 1;
24704 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24705 temp_it.face_id = GLYPH_FACE (glyph);
24706 temp_it.len = CHAR_BYTES (temp_it.c);
24707
24708 PRODUCE_GLYPHS (&temp_it);
24709 it->pixel_width = temp_it.pixel_width;
24710 it->nglyphs = temp_it.pixel_width;
24711 }
24712
24713 #ifdef HAVE_WINDOW_SYSTEM
24714
24715 /* Calculate line-height and line-spacing properties.
24716 An integer value specifies explicit pixel value.
24717 A float value specifies relative value to current face height.
24718 A cons (float . face-name) specifies relative value to
24719 height of specified face font.
24720
24721 Returns height in pixels, or nil. */
24722
24723
24724 static Lisp_Object
24725 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24726 int boff, int override)
24727 {
24728 Lisp_Object face_name = Qnil;
24729 int ascent, descent, height;
24730
24731 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24732 return val;
24733
24734 if (CONSP (val))
24735 {
24736 face_name = XCAR (val);
24737 val = XCDR (val);
24738 if (!NUMBERP (val))
24739 val = make_number (1);
24740 if (NILP (face_name))
24741 {
24742 height = it->ascent + it->descent;
24743 goto scale;
24744 }
24745 }
24746
24747 if (NILP (face_name))
24748 {
24749 font = FRAME_FONT (it->f);
24750 boff = FRAME_BASELINE_OFFSET (it->f);
24751 }
24752 else if (EQ (face_name, Qt))
24753 {
24754 override = 0;
24755 }
24756 else
24757 {
24758 int face_id;
24759 struct face *face;
24760
24761 face_id = lookup_named_face (it->f, face_name, 0);
24762 if (face_id < 0)
24763 return make_number (-1);
24764
24765 face = FACE_FROM_ID (it->f, face_id);
24766 font = face->font;
24767 if (font == NULL)
24768 return make_number (-1);
24769 boff = font->baseline_offset;
24770 if (font->vertical_centering)
24771 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24772 }
24773
24774 ascent = FONT_BASE (font) + boff;
24775 descent = FONT_DESCENT (font) - boff;
24776
24777 if (override)
24778 {
24779 it->override_ascent = ascent;
24780 it->override_descent = descent;
24781 it->override_boff = boff;
24782 }
24783
24784 height = ascent + descent;
24785
24786 scale:
24787 if (FLOATP (val))
24788 height = (int)(XFLOAT_DATA (val) * height);
24789 else if (INTEGERP (val))
24790 height *= XINT (val);
24791
24792 return make_number (height);
24793 }
24794
24795
24796 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24797 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24798 and only if this is for a character for which no font was found.
24799
24800 If the display method (it->glyphless_method) is
24801 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24802 length of the acronym or the hexadecimal string, UPPER_XOFF and
24803 UPPER_YOFF are pixel offsets for the upper part of the string,
24804 LOWER_XOFF and LOWER_YOFF are for the lower part.
24805
24806 For the other display methods, LEN through LOWER_YOFF are zero. */
24807
24808 static void
24809 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24810 short upper_xoff, short upper_yoff,
24811 short lower_xoff, short lower_yoff)
24812 {
24813 struct glyph *glyph;
24814 enum glyph_row_area area = it->area;
24815
24816 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24817 if (glyph < it->glyph_row->glyphs[area + 1])
24818 {
24819 /* If the glyph row is reversed, we need to prepend the glyph
24820 rather than append it. */
24821 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24822 {
24823 struct glyph *g;
24824
24825 /* Make room for the additional glyph. */
24826 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24827 g[1] = *g;
24828 glyph = it->glyph_row->glyphs[area];
24829 }
24830 glyph->charpos = CHARPOS (it->position);
24831 glyph->object = it->object;
24832 glyph->pixel_width = it->pixel_width;
24833 glyph->ascent = it->ascent;
24834 glyph->descent = it->descent;
24835 glyph->voffset = it->voffset;
24836 glyph->type = GLYPHLESS_GLYPH;
24837 glyph->u.glyphless.method = it->glyphless_method;
24838 glyph->u.glyphless.for_no_font = for_no_font;
24839 glyph->u.glyphless.len = len;
24840 glyph->u.glyphless.ch = it->c;
24841 glyph->slice.glyphless.upper_xoff = upper_xoff;
24842 glyph->slice.glyphless.upper_yoff = upper_yoff;
24843 glyph->slice.glyphless.lower_xoff = lower_xoff;
24844 glyph->slice.glyphless.lower_yoff = lower_yoff;
24845 glyph->avoid_cursor_p = it->avoid_cursor_p;
24846 glyph->multibyte_p = it->multibyte_p;
24847 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24848 {
24849 /* In R2L rows, the left and the right box edges need to be
24850 drawn in reverse direction. */
24851 glyph->right_box_line_p = it->start_of_box_run_p;
24852 glyph->left_box_line_p = it->end_of_box_run_p;
24853 }
24854 else
24855 {
24856 glyph->left_box_line_p = it->start_of_box_run_p;
24857 glyph->right_box_line_p = it->end_of_box_run_p;
24858 }
24859 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24860 || it->phys_descent > it->descent);
24861 glyph->padding_p = 0;
24862 glyph->glyph_not_available_p = 0;
24863 glyph->face_id = face_id;
24864 glyph->font_type = FONT_TYPE_UNKNOWN;
24865 if (it->bidi_p)
24866 {
24867 glyph->resolved_level = it->bidi_it.resolved_level;
24868 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24869 emacs_abort ();
24870 glyph->bidi_type = it->bidi_it.type;
24871 }
24872 ++it->glyph_row->used[area];
24873 }
24874 else
24875 IT_EXPAND_MATRIX_WIDTH (it, area);
24876 }
24877
24878
24879 /* Produce a glyph for a glyphless character for iterator IT.
24880 IT->glyphless_method specifies which method to use for displaying
24881 the character. See the description of enum
24882 glyphless_display_method in dispextern.h for the detail.
24883
24884 FOR_NO_FONT is nonzero if and only if this is for a character for
24885 which no font was found. ACRONYM, if non-nil, is an acronym string
24886 for the character. */
24887
24888 static void
24889 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24890 {
24891 int face_id;
24892 struct face *face;
24893 struct font *font;
24894 int base_width, base_height, width, height;
24895 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24896 int len;
24897
24898 /* Get the metrics of the base font. We always refer to the current
24899 ASCII face. */
24900 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24901 font = face->font ? face->font : FRAME_FONT (it->f);
24902 it->ascent = FONT_BASE (font) + font->baseline_offset;
24903 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24904 base_height = it->ascent + it->descent;
24905 base_width = font->average_width;
24906
24907 face_id = merge_glyphless_glyph_face (it);
24908
24909 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24910 {
24911 it->pixel_width = THIN_SPACE_WIDTH;
24912 len = 0;
24913 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24914 }
24915 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24916 {
24917 width = CHAR_WIDTH (it->c);
24918 if (width == 0)
24919 width = 1;
24920 else if (width > 4)
24921 width = 4;
24922 it->pixel_width = base_width * width;
24923 len = 0;
24924 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24925 }
24926 else
24927 {
24928 char buf[7];
24929 const char *str;
24930 unsigned int code[6];
24931 int upper_len;
24932 int ascent, descent;
24933 struct font_metrics metrics_upper, metrics_lower;
24934
24935 face = FACE_FROM_ID (it->f, face_id);
24936 font = face->font ? face->font : FRAME_FONT (it->f);
24937 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24938
24939 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24940 {
24941 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24942 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24943 if (CONSP (acronym))
24944 acronym = XCAR (acronym);
24945 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24946 }
24947 else
24948 {
24949 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24950 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24951 str = buf;
24952 }
24953 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24954 code[len] = font->driver->encode_char (font, str[len]);
24955 upper_len = (len + 1) / 2;
24956 font->driver->text_extents (font, code, upper_len,
24957 &metrics_upper);
24958 font->driver->text_extents (font, code + upper_len, len - upper_len,
24959 &metrics_lower);
24960
24961
24962
24963 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24964 width = max (metrics_upper.width, metrics_lower.width) + 4;
24965 upper_xoff = upper_yoff = 2; /* the typical case */
24966 if (base_width >= width)
24967 {
24968 /* Align the upper to the left, the lower to the right. */
24969 it->pixel_width = base_width;
24970 lower_xoff = base_width - 2 - metrics_lower.width;
24971 }
24972 else
24973 {
24974 /* Center the shorter one. */
24975 it->pixel_width = width;
24976 if (metrics_upper.width >= metrics_lower.width)
24977 lower_xoff = (width - metrics_lower.width) / 2;
24978 else
24979 {
24980 /* FIXME: This code doesn't look right. It formerly was
24981 missing the "lower_xoff = 0;", which couldn't have
24982 been right since it left lower_xoff uninitialized. */
24983 lower_xoff = 0;
24984 upper_xoff = (width - metrics_upper.width) / 2;
24985 }
24986 }
24987
24988 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24989 top, bottom, and between upper and lower strings. */
24990 height = (metrics_upper.ascent + metrics_upper.descent
24991 + metrics_lower.ascent + metrics_lower.descent) + 5;
24992 /* Center vertically.
24993 H:base_height, D:base_descent
24994 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24995
24996 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24997 descent = D - H/2 + h/2;
24998 lower_yoff = descent - 2 - ld;
24999 upper_yoff = lower_yoff - la - 1 - ud; */
25000 ascent = - (it->descent - (base_height + height + 1) / 2);
25001 descent = it->descent - (base_height - height) / 2;
25002 lower_yoff = descent - 2 - metrics_lower.descent;
25003 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25004 - metrics_upper.descent);
25005 /* Don't make the height shorter than the base height. */
25006 if (height > base_height)
25007 {
25008 it->ascent = ascent;
25009 it->descent = descent;
25010 }
25011 }
25012
25013 it->phys_ascent = it->ascent;
25014 it->phys_descent = it->descent;
25015 if (it->glyph_row)
25016 append_glyphless_glyph (it, face_id, for_no_font, len,
25017 upper_xoff, upper_yoff,
25018 lower_xoff, lower_yoff);
25019 it->nglyphs = 1;
25020 take_vertical_position_into_account (it);
25021 }
25022
25023
25024 /* RIF:
25025 Produce glyphs/get display metrics for the display element IT is
25026 loaded with. See the description of struct it in dispextern.h
25027 for an overview of struct it. */
25028
25029 void
25030 x_produce_glyphs (struct it *it)
25031 {
25032 int extra_line_spacing = it->extra_line_spacing;
25033
25034 it->glyph_not_available_p = 0;
25035
25036 if (it->what == IT_CHARACTER)
25037 {
25038 XChar2b char2b;
25039 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25040 struct font *font = face->font;
25041 struct font_metrics *pcm = NULL;
25042 int boff; /* baseline offset */
25043
25044 if (font == NULL)
25045 {
25046 /* When no suitable font is found, display this character by
25047 the method specified in the first extra slot of
25048 Vglyphless_char_display. */
25049 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25050
25051 eassert (it->what == IT_GLYPHLESS);
25052 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25053 goto done;
25054 }
25055
25056 boff = font->baseline_offset;
25057 if (font->vertical_centering)
25058 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25059
25060 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25061 {
25062 int stretched_p;
25063
25064 it->nglyphs = 1;
25065
25066 if (it->override_ascent >= 0)
25067 {
25068 it->ascent = it->override_ascent;
25069 it->descent = it->override_descent;
25070 boff = it->override_boff;
25071 }
25072 else
25073 {
25074 it->ascent = FONT_BASE (font) + boff;
25075 it->descent = FONT_DESCENT (font) - boff;
25076 }
25077
25078 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25079 {
25080 pcm = get_per_char_metric (font, &char2b);
25081 if (pcm->width == 0
25082 && pcm->rbearing == 0 && pcm->lbearing == 0)
25083 pcm = NULL;
25084 }
25085
25086 if (pcm)
25087 {
25088 it->phys_ascent = pcm->ascent + boff;
25089 it->phys_descent = pcm->descent - boff;
25090 it->pixel_width = pcm->width;
25091 }
25092 else
25093 {
25094 it->glyph_not_available_p = 1;
25095 it->phys_ascent = it->ascent;
25096 it->phys_descent = it->descent;
25097 it->pixel_width = font->space_width;
25098 }
25099
25100 if (it->constrain_row_ascent_descent_p)
25101 {
25102 if (it->descent > it->max_descent)
25103 {
25104 it->ascent += it->descent - it->max_descent;
25105 it->descent = it->max_descent;
25106 }
25107 if (it->ascent > it->max_ascent)
25108 {
25109 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25110 it->ascent = it->max_ascent;
25111 }
25112 it->phys_ascent = min (it->phys_ascent, it->ascent);
25113 it->phys_descent = min (it->phys_descent, it->descent);
25114 extra_line_spacing = 0;
25115 }
25116
25117 /* If this is a space inside a region of text with
25118 `space-width' property, change its width. */
25119 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25120 if (stretched_p)
25121 it->pixel_width *= XFLOATINT (it->space_width);
25122
25123 /* If face has a box, add the box thickness to the character
25124 height. If character has a box line to the left and/or
25125 right, add the box line width to the character's width. */
25126 if (face->box != FACE_NO_BOX)
25127 {
25128 int thick = face->box_line_width;
25129
25130 if (thick > 0)
25131 {
25132 it->ascent += thick;
25133 it->descent += thick;
25134 }
25135 else
25136 thick = -thick;
25137
25138 if (it->start_of_box_run_p)
25139 it->pixel_width += thick;
25140 if (it->end_of_box_run_p)
25141 it->pixel_width += thick;
25142 }
25143
25144 /* If face has an overline, add the height of the overline
25145 (1 pixel) and a 1 pixel margin to the character height. */
25146 if (face->overline_p)
25147 it->ascent += overline_margin;
25148
25149 if (it->constrain_row_ascent_descent_p)
25150 {
25151 if (it->ascent > it->max_ascent)
25152 it->ascent = it->max_ascent;
25153 if (it->descent > it->max_descent)
25154 it->descent = it->max_descent;
25155 }
25156
25157 take_vertical_position_into_account (it);
25158
25159 /* If we have to actually produce glyphs, do it. */
25160 if (it->glyph_row)
25161 {
25162 if (stretched_p)
25163 {
25164 /* Translate a space with a `space-width' property
25165 into a stretch glyph. */
25166 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25167 / FONT_HEIGHT (font));
25168 append_stretch_glyph (it, it->object, it->pixel_width,
25169 it->ascent + it->descent, ascent);
25170 }
25171 else
25172 append_glyph (it);
25173
25174 /* If characters with lbearing or rbearing are displayed
25175 in this line, record that fact in a flag of the
25176 glyph row. This is used to optimize X output code. */
25177 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25178 it->glyph_row->contains_overlapping_glyphs_p = 1;
25179 }
25180 if (! stretched_p && it->pixel_width == 0)
25181 /* We assure that all visible glyphs have at least 1-pixel
25182 width. */
25183 it->pixel_width = 1;
25184 }
25185 else if (it->char_to_display == '\n')
25186 {
25187 /* A newline has no width, but we need the height of the
25188 line. But if previous part of the line sets a height,
25189 don't increase that height */
25190
25191 Lisp_Object height;
25192 Lisp_Object total_height = Qnil;
25193
25194 it->override_ascent = -1;
25195 it->pixel_width = 0;
25196 it->nglyphs = 0;
25197
25198 height = get_it_property (it, Qline_height);
25199 /* Split (line-height total-height) list */
25200 if (CONSP (height)
25201 && CONSP (XCDR (height))
25202 && NILP (XCDR (XCDR (height))))
25203 {
25204 total_height = XCAR (XCDR (height));
25205 height = XCAR (height);
25206 }
25207 height = calc_line_height_property (it, height, font, boff, 1);
25208
25209 if (it->override_ascent >= 0)
25210 {
25211 it->ascent = it->override_ascent;
25212 it->descent = it->override_descent;
25213 boff = it->override_boff;
25214 }
25215 else
25216 {
25217 it->ascent = FONT_BASE (font) + boff;
25218 it->descent = FONT_DESCENT (font) - boff;
25219 }
25220
25221 if (EQ (height, Qt))
25222 {
25223 if (it->descent > it->max_descent)
25224 {
25225 it->ascent += it->descent - it->max_descent;
25226 it->descent = it->max_descent;
25227 }
25228 if (it->ascent > it->max_ascent)
25229 {
25230 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25231 it->ascent = it->max_ascent;
25232 }
25233 it->phys_ascent = min (it->phys_ascent, it->ascent);
25234 it->phys_descent = min (it->phys_descent, it->descent);
25235 it->constrain_row_ascent_descent_p = 1;
25236 extra_line_spacing = 0;
25237 }
25238 else
25239 {
25240 Lisp_Object spacing;
25241
25242 it->phys_ascent = it->ascent;
25243 it->phys_descent = it->descent;
25244
25245 if ((it->max_ascent > 0 || it->max_descent > 0)
25246 && face->box != FACE_NO_BOX
25247 && face->box_line_width > 0)
25248 {
25249 it->ascent += face->box_line_width;
25250 it->descent += face->box_line_width;
25251 }
25252 if (!NILP (height)
25253 && XINT (height) > it->ascent + it->descent)
25254 it->ascent = XINT (height) - it->descent;
25255
25256 if (!NILP (total_height))
25257 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25258 else
25259 {
25260 spacing = get_it_property (it, Qline_spacing);
25261 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25262 }
25263 if (INTEGERP (spacing))
25264 {
25265 extra_line_spacing = XINT (spacing);
25266 if (!NILP (total_height))
25267 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25268 }
25269 }
25270 }
25271 else /* i.e. (it->char_to_display == '\t') */
25272 {
25273 if (font->space_width > 0)
25274 {
25275 int tab_width = it->tab_width * font->space_width;
25276 int x = it->current_x + it->continuation_lines_width;
25277 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25278
25279 /* If the distance from the current position to the next tab
25280 stop is less than a space character width, use the
25281 tab stop after that. */
25282 if (next_tab_x - x < font->space_width)
25283 next_tab_x += tab_width;
25284
25285 it->pixel_width = next_tab_x - x;
25286 it->nglyphs = 1;
25287 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25288 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25289
25290 if (it->glyph_row)
25291 {
25292 append_stretch_glyph (it, it->object, it->pixel_width,
25293 it->ascent + it->descent, it->ascent);
25294 }
25295 }
25296 else
25297 {
25298 it->pixel_width = 0;
25299 it->nglyphs = 1;
25300 }
25301 }
25302 }
25303 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25304 {
25305 /* A static composition.
25306
25307 Note: A composition is represented as one glyph in the
25308 glyph matrix. There are no padding glyphs.
25309
25310 Important note: pixel_width, ascent, and descent are the
25311 values of what is drawn by draw_glyphs (i.e. the values of
25312 the overall glyphs composed). */
25313 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25314 int boff; /* baseline offset */
25315 struct composition *cmp = composition_table[it->cmp_it.id];
25316 int glyph_len = cmp->glyph_len;
25317 struct font *font = face->font;
25318
25319 it->nglyphs = 1;
25320
25321 /* If we have not yet calculated pixel size data of glyphs of
25322 the composition for the current face font, calculate them
25323 now. Theoretically, we have to check all fonts for the
25324 glyphs, but that requires much time and memory space. So,
25325 here we check only the font of the first glyph. This may
25326 lead to incorrect display, but it's very rare, and C-l
25327 (recenter-top-bottom) can correct the display anyway. */
25328 if (! cmp->font || cmp->font != font)
25329 {
25330 /* Ascent and descent of the font of the first character
25331 of this composition (adjusted by baseline offset).
25332 Ascent and descent of overall glyphs should not be less
25333 than these, respectively. */
25334 int font_ascent, font_descent, font_height;
25335 /* Bounding box of the overall glyphs. */
25336 int leftmost, rightmost, lowest, highest;
25337 int lbearing, rbearing;
25338 int i, width, ascent, descent;
25339 int left_padded = 0, right_padded = 0;
25340 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25341 XChar2b char2b;
25342 struct font_metrics *pcm;
25343 int font_not_found_p;
25344 ptrdiff_t pos;
25345
25346 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25347 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25348 break;
25349 if (glyph_len < cmp->glyph_len)
25350 right_padded = 1;
25351 for (i = 0; i < glyph_len; i++)
25352 {
25353 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25354 break;
25355 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25356 }
25357 if (i > 0)
25358 left_padded = 1;
25359
25360 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25361 : IT_CHARPOS (*it));
25362 /* If no suitable font is found, use the default font. */
25363 font_not_found_p = font == NULL;
25364 if (font_not_found_p)
25365 {
25366 face = face->ascii_face;
25367 font = face->font;
25368 }
25369 boff = font->baseline_offset;
25370 if (font->vertical_centering)
25371 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25372 font_ascent = FONT_BASE (font) + boff;
25373 font_descent = FONT_DESCENT (font) - boff;
25374 font_height = FONT_HEIGHT (font);
25375
25376 cmp->font = font;
25377
25378 pcm = NULL;
25379 if (! font_not_found_p)
25380 {
25381 get_char_face_and_encoding (it->f, c, it->face_id,
25382 &char2b, 0);
25383 pcm = get_per_char_metric (font, &char2b);
25384 }
25385
25386 /* Initialize the bounding box. */
25387 if (pcm)
25388 {
25389 width = cmp->glyph_len > 0 ? pcm->width : 0;
25390 ascent = pcm->ascent;
25391 descent = pcm->descent;
25392 lbearing = pcm->lbearing;
25393 rbearing = pcm->rbearing;
25394 }
25395 else
25396 {
25397 width = cmp->glyph_len > 0 ? font->space_width : 0;
25398 ascent = FONT_BASE (font);
25399 descent = FONT_DESCENT (font);
25400 lbearing = 0;
25401 rbearing = width;
25402 }
25403
25404 rightmost = width;
25405 leftmost = 0;
25406 lowest = - descent + boff;
25407 highest = ascent + boff;
25408
25409 if (! font_not_found_p
25410 && font->default_ascent
25411 && CHAR_TABLE_P (Vuse_default_ascent)
25412 && !NILP (Faref (Vuse_default_ascent,
25413 make_number (it->char_to_display))))
25414 highest = font->default_ascent + boff;
25415
25416 /* Draw the first glyph at the normal position. It may be
25417 shifted to right later if some other glyphs are drawn
25418 at the left. */
25419 cmp->offsets[i * 2] = 0;
25420 cmp->offsets[i * 2 + 1] = boff;
25421 cmp->lbearing = lbearing;
25422 cmp->rbearing = rbearing;
25423
25424 /* Set cmp->offsets for the remaining glyphs. */
25425 for (i++; i < glyph_len; i++)
25426 {
25427 int left, right, btm, top;
25428 int ch = COMPOSITION_GLYPH (cmp, i);
25429 int face_id;
25430 struct face *this_face;
25431
25432 if (ch == '\t')
25433 ch = ' ';
25434 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25435 this_face = FACE_FROM_ID (it->f, face_id);
25436 font = this_face->font;
25437
25438 if (font == NULL)
25439 pcm = NULL;
25440 else
25441 {
25442 get_char_face_and_encoding (it->f, ch, face_id,
25443 &char2b, 0);
25444 pcm = get_per_char_metric (font, &char2b);
25445 }
25446 if (! pcm)
25447 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25448 else
25449 {
25450 width = pcm->width;
25451 ascent = pcm->ascent;
25452 descent = pcm->descent;
25453 lbearing = pcm->lbearing;
25454 rbearing = pcm->rbearing;
25455 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25456 {
25457 /* Relative composition with or without
25458 alternate chars. */
25459 left = (leftmost + rightmost - width) / 2;
25460 btm = - descent + boff;
25461 if (font->relative_compose
25462 && (! CHAR_TABLE_P (Vignore_relative_composition)
25463 || NILP (Faref (Vignore_relative_composition,
25464 make_number (ch)))))
25465 {
25466
25467 if (- descent >= font->relative_compose)
25468 /* One extra pixel between two glyphs. */
25469 btm = highest + 1;
25470 else if (ascent <= 0)
25471 /* One extra pixel between two glyphs. */
25472 btm = lowest - 1 - ascent - descent;
25473 }
25474 }
25475 else
25476 {
25477 /* A composition rule is specified by an integer
25478 value that encodes global and new reference
25479 points (GREF and NREF). GREF and NREF are
25480 specified by numbers as below:
25481
25482 0---1---2 -- ascent
25483 | |
25484 | |
25485 | |
25486 9--10--11 -- center
25487 | |
25488 ---3---4---5--- baseline
25489 | |
25490 6---7---8 -- descent
25491 */
25492 int rule = COMPOSITION_RULE (cmp, i);
25493 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25494
25495 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25496 grefx = gref % 3, nrefx = nref % 3;
25497 grefy = gref / 3, nrefy = nref / 3;
25498 if (xoff)
25499 xoff = font_height * (xoff - 128) / 256;
25500 if (yoff)
25501 yoff = font_height * (yoff - 128) / 256;
25502
25503 left = (leftmost
25504 + grefx * (rightmost - leftmost) / 2
25505 - nrefx * width / 2
25506 + xoff);
25507
25508 btm = ((grefy == 0 ? highest
25509 : grefy == 1 ? 0
25510 : grefy == 2 ? lowest
25511 : (highest + lowest) / 2)
25512 - (nrefy == 0 ? ascent + descent
25513 : nrefy == 1 ? descent - boff
25514 : nrefy == 2 ? 0
25515 : (ascent + descent) / 2)
25516 + yoff);
25517 }
25518
25519 cmp->offsets[i * 2] = left;
25520 cmp->offsets[i * 2 + 1] = btm + descent;
25521
25522 /* Update the bounding box of the overall glyphs. */
25523 if (width > 0)
25524 {
25525 right = left + width;
25526 if (left < leftmost)
25527 leftmost = left;
25528 if (right > rightmost)
25529 rightmost = right;
25530 }
25531 top = btm + descent + ascent;
25532 if (top > highest)
25533 highest = top;
25534 if (btm < lowest)
25535 lowest = btm;
25536
25537 if (cmp->lbearing > left + lbearing)
25538 cmp->lbearing = left + lbearing;
25539 if (cmp->rbearing < left + rbearing)
25540 cmp->rbearing = left + rbearing;
25541 }
25542 }
25543
25544 /* If there are glyphs whose x-offsets are negative,
25545 shift all glyphs to the right and make all x-offsets
25546 non-negative. */
25547 if (leftmost < 0)
25548 {
25549 for (i = 0; i < cmp->glyph_len; i++)
25550 cmp->offsets[i * 2] -= leftmost;
25551 rightmost -= leftmost;
25552 cmp->lbearing -= leftmost;
25553 cmp->rbearing -= leftmost;
25554 }
25555
25556 if (left_padded && cmp->lbearing < 0)
25557 {
25558 for (i = 0; i < cmp->glyph_len; i++)
25559 cmp->offsets[i * 2] -= cmp->lbearing;
25560 rightmost -= cmp->lbearing;
25561 cmp->rbearing -= cmp->lbearing;
25562 cmp->lbearing = 0;
25563 }
25564 if (right_padded && rightmost < cmp->rbearing)
25565 {
25566 rightmost = cmp->rbearing;
25567 }
25568
25569 cmp->pixel_width = rightmost;
25570 cmp->ascent = highest;
25571 cmp->descent = - lowest;
25572 if (cmp->ascent < font_ascent)
25573 cmp->ascent = font_ascent;
25574 if (cmp->descent < font_descent)
25575 cmp->descent = font_descent;
25576 }
25577
25578 if (it->glyph_row
25579 && (cmp->lbearing < 0
25580 || cmp->rbearing > cmp->pixel_width))
25581 it->glyph_row->contains_overlapping_glyphs_p = 1;
25582
25583 it->pixel_width = cmp->pixel_width;
25584 it->ascent = it->phys_ascent = cmp->ascent;
25585 it->descent = it->phys_descent = cmp->descent;
25586 if (face->box != FACE_NO_BOX)
25587 {
25588 int thick = face->box_line_width;
25589
25590 if (thick > 0)
25591 {
25592 it->ascent += thick;
25593 it->descent += thick;
25594 }
25595 else
25596 thick = - thick;
25597
25598 if (it->start_of_box_run_p)
25599 it->pixel_width += thick;
25600 if (it->end_of_box_run_p)
25601 it->pixel_width += thick;
25602 }
25603
25604 /* If face has an overline, add the height of the overline
25605 (1 pixel) and a 1 pixel margin to the character height. */
25606 if (face->overline_p)
25607 it->ascent += overline_margin;
25608
25609 take_vertical_position_into_account (it);
25610 if (it->ascent < 0)
25611 it->ascent = 0;
25612 if (it->descent < 0)
25613 it->descent = 0;
25614
25615 if (it->glyph_row && cmp->glyph_len > 0)
25616 append_composite_glyph (it);
25617 }
25618 else if (it->what == IT_COMPOSITION)
25619 {
25620 /* A dynamic (automatic) composition. */
25621 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25622 Lisp_Object gstring;
25623 struct font_metrics metrics;
25624
25625 it->nglyphs = 1;
25626
25627 gstring = composition_gstring_from_id (it->cmp_it.id);
25628 it->pixel_width
25629 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25630 &metrics);
25631 if (it->glyph_row
25632 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25633 it->glyph_row->contains_overlapping_glyphs_p = 1;
25634 it->ascent = it->phys_ascent = metrics.ascent;
25635 it->descent = it->phys_descent = metrics.descent;
25636 if (face->box != FACE_NO_BOX)
25637 {
25638 int thick = face->box_line_width;
25639
25640 if (thick > 0)
25641 {
25642 it->ascent += thick;
25643 it->descent += thick;
25644 }
25645 else
25646 thick = - thick;
25647
25648 if (it->start_of_box_run_p)
25649 it->pixel_width += thick;
25650 if (it->end_of_box_run_p)
25651 it->pixel_width += thick;
25652 }
25653 /* If face has an overline, add the height of the overline
25654 (1 pixel) and a 1 pixel margin to the character height. */
25655 if (face->overline_p)
25656 it->ascent += overline_margin;
25657 take_vertical_position_into_account (it);
25658 if (it->ascent < 0)
25659 it->ascent = 0;
25660 if (it->descent < 0)
25661 it->descent = 0;
25662
25663 if (it->glyph_row)
25664 append_composite_glyph (it);
25665 }
25666 else if (it->what == IT_GLYPHLESS)
25667 produce_glyphless_glyph (it, 0, Qnil);
25668 else if (it->what == IT_IMAGE)
25669 produce_image_glyph (it);
25670 else if (it->what == IT_STRETCH)
25671 produce_stretch_glyph (it);
25672
25673 done:
25674 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25675 because this isn't true for images with `:ascent 100'. */
25676 eassert (it->ascent >= 0 && it->descent >= 0);
25677 if (it->area == TEXT_AREA)
25678 it->current_x += it->pixel_width;
25679
25680 if (extra_line_spacing > 0)
25681 {
25682 it->descent += extra_line_spacing;
25683 if (extra_line_spacing > it->max_extra_line_spacing)
25684 it->max_extra_line_spacing = extra_line_spacing;
25685 }
25686
25687 it->max_ascent = max (it->max_ascent, it->ascent);
25688 it->max_descent = max (it->max_descent, it->descent);
25689 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25690 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25691 }
25692
25693 /* EXPORT for RIF:
25694 Output LEN glyphs starting at START at the nominal cursor position.
25695 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
25696 being updated, and UPDATED_AREA is the area of that row being updated. */
25697
25698 void
25699 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
25700 struct glyph *start, enum glyph_row_area updated_area, int len)
25701 {
25702 int x, hpos, chpos = w->phys_cursor.hpos;
25703
25704 eassert (updated_row);
25705 /* When the window is hscrolled, cursor hpos can legitimately be out
25706 of bounds, but we draw the cursor at the corresponding window
25707 margin in that case. */
25708 if (!updated_row->reversed_p && chpos < 0)
25709 chpos = 0;
25710 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25711 chpos = updated_row->used[TEXT_AREA] - 1;
25712
25713 block_input ();
25714
25715 /* Write glyphs. */
25716
25717 hpos = start - updated_row->glyphs[updated_area];
25718 x = draw_glyphs (w, w->output_cursor.x,
25719 updated_row, updated_area,
25720 hpos, hpos + len,
25721 DRAW_NORMAL_TEXT, 0);
25722
25723 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25724 if (updated_area == TEXT_AREA
25725 && w->phys_cursor_on_p
25726 && w->phys_cursor.vpos == w->output_cursor.vpos
25727 && chpos >= hpos
25728 && chpos < hpos + len)
25729 w->phys_cursor_on_p = 0;
25730
25731 unblock_input ();
25732
25733 /* Advance the output cursor. */
25734 w->output_cursor.hpos += len;
25735 w->output_cursor.x = x;
25736 }
25737
25738
25739 /* EXPORT for RIF:
25740 Insert LEN glyphs from START at the nominal cursor position. */
25741
25742 void
25743 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
25744 struct glyph *start, enum glyph_row_area updated_area, int len)
25745 {
25746 struct frame *f;
25747 int line_height, shift_by_width, shifted_region_width;
25748 struct glyph_row *row;
25749 struct glyph *glyph;
25750 int frame_x, frame_y;
25751 ptrdiff_t hpos;
25752
25753 eassert (updated_row);
25754 block_input ();
25755 f = XFRAME (WINDOW_FRAME (w));
25756
25757 /* Get the height of the line we are in. */
25758 row = updated_row;
25759 line_height = row->height;
25760
25761 /* Get the width of the glyphs to insert. */
25762 shift_by_width = 0;
25763 for (glyph = start; glyph < start + len; ++glyph)
25764 shift_by_width += glyph->pixel_width;
25765
25766 /* Get the width of the region to shift right. */
25767 shifted_region_width = (window_box_width (w, updated_area)
25768 - w->output_cursor.x
25769 - shift_by_width);
25770
25771 /* Shift right. */
25772 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
25773 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
25774
25775 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25776 line_height, shift_by_width);
25777
25778 /* Write the glyphs. */
25779 hpos = start - row->glyphs[updated_area];
25780 draw_glyphs (w, w->output_cursor.x, row, updated_area,
25781 hpos, hpos + len,
25782 DRAW_NORMAL_TEXT, 0);
25783
25784 /* Advance the output cursor. */
25785 w->output_cursor.hpos += len;
25786 w->output_cursor.x += shift_by_width;
25787 unblock_input ();
25788 }
25789
25790
25791 /* EXPORT for RIF:
25792 Erase the current text line from the nominal cursor position
25793 (inclusive) to pixel column TO_X (exclusive). The idea is that
25794 everything from TO_X onward is already erased.
25795
25796 TO_X is a pixel position relative to UPDATED_AREA of currently
25797 updated window W. TO_X == -1 means clear to the end of this area. */
25798
25799 void
25800 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
25801 enum glyph_row_area updated_area, int to_x)
25802 {
25803 struct frame *f;
25804 int max_x, min_y, max_y;
25805 int from_x, from_y, to_y;
25806
25807 eassert (updated_row);
25808 f = XFRAME (w->frame);
25809
25810 if (updated_row->full_width_p)
25811 max_x = WINDOW_TOTAL_WIDTH (w);
25812 else
25813 max_x = window_box_width (w, updated_area);
25814 max_y = window_text_bottom_y (w);
25815
25816 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25817 of window. For TO_X > 0, truncate to end of drawing area. */
25818 if (to_x == 0)
25819 return;
25820 else if (to_x < 0)
25821 to_x = max_x;
25822 else
25823 to_x = min (to_x, max_x);
25824
25825 to_y = min (max_y, w->output_cursor.y + updated_row->height);
25826
25827 /* Notice if the cursor will be cleared by this operation. */
25828 if (!updated_row->full_width_p)
25829 notice_overwritten_cursor (w, updated_area,
25830 w->output_cursor.x, -1,
25831 updated_row->y,
25832 MATRIX_ROW_BOTTOM_Y (updated_row));
25833
25834 from_x = w->output_cursor.x;
25835
25836 /* Translate to frame coordinates. */
25837 if (updated_row->full_width_p)
25838 {
25839 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25840 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25841 }
25842 else
25843 {
25844 int area_left = window_box_left (w, updated_area);
25845 from_x += area_left;
25846 to_x += area_left;
25847 }
25848
25849 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25850 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
25851 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25852
25853 /* Prevent inadvertently clearing to end of the X window. */
25854 if (to_x > from_x && to_y > from_y)
25855 {
25856 block_input ();
25857 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25858 to_x - from_x, to_y - from_y);
25859 unblock_input ();
25860 }
25861 }
25862
25863 #endif /* HAVE_WINDOW_SYSTEM */
25864
25865
25866 \f
25867 /***********************************************************************
25868 Cursor types
25869 ***********************************************************************/
25870
25871 /* Value is the internal representation of the specified cursor type
25872 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25873 of the bar cursor. */
25874
25875 static enum text_cursor_kinds
25876 get_specified_cursor_type (Lisp_Object arg, int *width)
25877 {
25878 enum text_cursor_kinds type;
25879
25880 if (NILP (arg))
25881 return NO_CURSOR;
25882
25883 if (EQ (arg, Qbox))
25884 return FILLED_BOX_CURSOR;
25885
25886 if (EQ (arg, Qhollow))
25887 return HOLLOW_BOX_CURSOR;
25888
25889 if (EQ (arg, Qbar))
25890 {
25891 *width = 2;
25892 return BAR_CURSOR;
25893 }
25894
25895 if (CONSP (arg)
25896 && EQ (XCAR (arg), Qbar)
25897 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25898 {
25899 *width = XINT (XCDR (arg));
25900 return BAR_CURSOR;
25901 }
25902
25903 if (EQ (arg, Qhbar))
25904 {
25905 *width = 2;
25906 return HBAR_CURSOR;
25907 }
25908
25909 if (CONSP (arg)
25910 && EQ (XCAR (arg), Qhbar)
25911 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25912 {
25913 *width = XINT (XCDR (arg));
25914 return HBAR_CURSOR;
25915 }
25916
25917 /* Treat anything unknown as "hollow box cursor".
25918 It was bad to signal an error; people have trouble fixing
25919 .Xdefaults with Emacs, when it has something bad in it. */
25920 type = HOLLOW_BOX_CURSOR;
25921
25922 return type;
25923 }
25924
25925 /* Set the default cursor types for specified frame. */
25926 void
25927 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25928 {
25929 int width = 1;
25930 Lisp_Object tem;
25931
25932 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25933 FRAME_CURSOR_WIDTH (f) = width;
25934
25935 /* By default, set up the blink-off state depending on the on-state. */
25936
25937 tem = Fassoc (arg, Vblink_cursor_alist);
25938 if (!NILP (tem))
25939 {
25940 FRAME_BLINK_OFF_CURSOR (f)
25941 = get_specified_cursor_type (XCDR (tem), &width);
25942 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25943 }
25944 else
25945 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25946
25947 /* Make sure the cursor gets redrawn. */
25948 f->cursor_type_changed = 1;
25949 }
25950
25951
25952 #ifdef HAVE_WINDOW_SYSTEM
25953
25954 /* Return the cursor we want to be displayed in window W. Return
25955 width of bar/hbar cursor through WIDTH arg. Return with
25956 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25957 (i.e. if the `system caret' should track this cursor).
25958
25959 In a mini-buffer window, we want the cursor only to appear if we
25960 are reading input from this window. For the selected window, we
25961 want the cursor type given by the frame parameter or buffer local
25962 setting of cursor-type. If explicitly marked off, draw no cursor.
25963 In all other cases, we want a hollow box cursor. */
25964
25965 static enum text_cursor_kinds
25966 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25967 int *active_cursor)
25968 {
25969 struct frame *f = XFRAME (w->frame);
25970 struct buffer *b = XBUFFER (w->contents);
25971 int cursor_type = DEFAULT_CURSOR;
25972 Lisp_Object alt_cursor;
25973 int non_selected = 0;
25974
25975 *active_cursor = 1;
25976
25977 /* Echo area */
25978 if (cursor_in_echo_area
25979 && FRAME_HAS_MINIBUF_P (f)
25980 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25981 {
25982 if (w == XWINDOW (echo_area_window))
25983 {
25984 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25985 {
25986 *width = FRAME_CURSOR_WIDTH (f);
25987 return FRAME_DESIRED_CURSOR (f);
25988 }
25989 else
25990 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25991 }
25992
25993 *active_cursor = 0;
25994 non_selected = 1;
25995 }
25996
25997 /* Detect a nonselected window or nonselected frame. */
25998 else if (w != XWINDOW (f->selected_window)
25999 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26000 {
26001 *active_cursor = 0;
26002
26003 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26004 return NO_CURSOR;
26005
26006 non_selected = 1;
26007 }
26008
26009 /* Never display a cursor in a window in which cursor-type is nil. */
26010 if (NILP (BVAR (b, cursor_type)))
26011 return NO_CURSOR;
26012
26013 /* Get the normal cursor type for this window. */
26014 if (EQ (BVAR (b, cursor_type), Qt))
26015 {
26016 cursor_type = FRAME_DESIRED_CURSOR (f);
26017 *width = FRAME_CURSOR_WIDTH (f);
26018 }
26019 else
26020 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26021
26022 /* Use cursor-in-non-selected-windows instead
26023 for non-selected window or frame. */
26024 if (non_selected)
26025 {
26026 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26027 if (!EQ (Qt, alt_cursor))
26028 return get_specified_cursor_type (alt_cursor, width);
26029 /* t means modify the normal cursor type. */
26030 if (cursor_type == FILLED_BOX_CURSOR)
26031 cursor_type = HOLLOW_BOX_CURSOR;
26032 else if (cursor_type == BAR_CURSOR && *width > 1)
26033 --*width;
26034 return cursor_type;
26035 }
26036
26037 /* Use normal cursor if not blinked off. */
26038 if (!w->cursor_off_p)
26039 {
26040 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26041 {
26042 if (cursor_type == FILLED_BOX_CURSOR)
26043 {
26044 /* Using a block cursor on large images can be very annoying.
26045 So use a hollow cursor for "large" images.
26046 If image is not transparent (no mask), also use hollow cursor. */
26047 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26048 if (img != NULL && IMAGEP (img->spec))
26049 {
26050 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26051 where N = size of default frame font size.
26052 This should cover most of the "tiny" icons people may use. */
26053 if (!img->mask
26054 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26055 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26056 cursor_type = HOLLOW_BOX_CURSOR;
26057 }
26058 }
26059 else if (cursor_type != NO_CURSOR)
26060 {
26061 /* Display current only supports BOX and HOLLOW cursors for images.
26062 So for now, unconditionally use a HOLLOW cursor when cursor is
26063 not a solid box cursor. */
26064 cursor_type = HOLLOW_BOX_CURSOR;
26065 }
26066 }
26067 return cursor_type;
26068 }
26069
26070 /* Cursor is blinked off, so determine how to "toggle" it. */
26071
26072 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26073 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26074 return get_specified_cursor_type (XCDR (alt_cursor), width);
26075
26076 /* Then see if frame has specified a specific blink off cursor type. */
26077 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26078 {
26079 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26080 return FRAME_BLINK_OFF_CURSOR (f);
26081 }
26082
26083 #if 0
26084 /* Some people liked having a permanently visible blinking cursor,
26085 while others had very strong opinions against it. So it was
26086 decided to remove it. KFS 2003-09-03 */
26087
26088 /* Finally perform built-in cursor blinking:
26089 filled box <-> hollow box
26090 wide [h]bar <-> narrow [h]bar
26091 narrow [h]bar <-> no cursor
26092 other type <-> no cursor */
26093
26094 if (cursor_type == FILLED_BOX_CURSOR)
26095 return HOLLOW_BOX_CURSOR;
26096
26097 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26098 {
26099 *width = 1;
26100 return cursor_type;
26101 }
26102 #endif
26103
26104 return NO_CURSOR;
26105 }
26106
26107
26108 /* Notice when the text cursor of window W has been completely
26109 overwritten by a drawing operation that outputs glyphs in AREA
26110 starting at X0 and ending at X1 in the line starting at Y0 and
26111 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26112 the rest of the line after X0 has been written. Y coordinates
26113 are window-relative. */
26114
26115 static void
26116 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26117 int x0, int x1, int y0, int y1)
26118 {
26119 int cx0, cx1, cy0, cy1;
26120 struct glyph_row *row;
26121
26122 if (!w->phys_cursor_on_p)
26123 return;
26124 if (area != TEXT_AREA)
26125 return;
26126
26127 if (w->phys_cursor.vpos < 0
26128 || w->phys_cursor.vpos >= w->current_matrix->nrows
26129 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26130 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26131 return;
26132
26133 if (row->cursor_in_fringe_p)
26134 {
26135 row->cursor_in_fringe_p = 0;
26136 draw_fringe_bitmap (w, row, row->reversed_p);
26137 w->phys_cursor_on_p = 0;
26138 return;
26139 }
26140
26141 cx0 = w->phys_cursor.x;
26142 cx1 = cx0 + w->phys_cursor_width;
26143 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26144 return;
26145
26146 /* The cursor image will be completely removed from the
26147 screen if the output area intersects the cursor area in
26148 y-direction. When we draw in [y0 y1[, and some part of
26149 the cursor is at y < y0, that part must have been drawn
26150 before. When scrolling, the cursor is erased before
26151 actually scrolling, so we don't come here. When not
26152 scrolling, the rows above the old cursor row must have
26153 changed, and in this case these rows must have written
26154 over the cursor image.
26155
26156 Likewise if part of the cursor is below y1, with the
26157 exception of the cursor being in the first blank row at
26158 the buffer and window end because update_text_area
26159 doesn't draw that row. (Except when it does, but
26160 that's handled in update_text_area.) */
26161
26162 cy0 = w->phys_cursor.y;
26163 cy1 = cy0 + w->phys_cursor_height;
26164 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26165 return;
26166
26167 w->phys_cursor_on_p = 0;
26168 }
26169
26170 #endif /* HAVE_WINDOW_SYSTEM */
26171
26172 \f
26173 /************************************************************************
26174 Mouse Face
26175 ************************************************************************/
26176
26177 #ifdef HAVE_WINDOW_SYSTEM
26178
26179 /* EXPORT for RIF:
26180 Fix the display of area AREA of overlapping row ROW in window W
26181 with respect to the overlapping part OVERLAPS. */
26182
26183 void
26184 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26185 enum glyph_row_area area, int overlaps)
26186 {
26187 int i, x;
26188
26189 block_input ();
26190
26191 x = 0;
26192 for (i = 0; i < row->used[area];)
26193 {
26194 if (row->glyphs[area][i].overlaps_vertically_p)
26195 {
26196 int start = i, start_x = x;
26197
26198 do
26199 {
26200 x += row->glyphs[area][i].pixel_width;
26201 ++i;
26202 }
26203 while (i < row->used[area]
26204 && row->glyphs[area][i].overlaps_vertically_p);
26205
26206 draw_glyphs (w, start_x, row, area,
26207 start, i,
26208 DRAW_NORMAL_TEXT, overlaps);
26209 }
26210 else
26211 {
26212 x += row->glyphs[area][i].pixel_width;
26213 ++i;
26214 }
26215 }
26216
26217 unblock_input ();
26218 }
26219
26220
26221 /* EXPORT:
26222 Draw the cursor glyph of window W in glyph row ROW. See the
26223 comment of draw_glyphs for the meaning of HL. */
26224
26225 void
26226 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26227 enum draw_glyphs_face hl)
26228 {
26229 /* If cursor hpos is out of bounds, don't draw garbage. This can
26230 happen in mini-buffer windows when switching between echo area
26231 glyphs and mini-buffer. */
26232 if ((row->reversed_p
26233 ? (w->phys_cursor.hpos >= 0)
26234 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26235 {
26236 int on_p = w->phys_cursor_on_p;
26237 int x1;
26238 int hpos = w->phys_cursor.hpos;
26239
26240 /* When the window is hscrolled, cursor hpos can legitimately be
26241 out of bounds, but we draw the cursor at the corresponding
26242 window margin in that case. */
26243 if (!row->reversed_p && hpos < 0)
26244 hpos = 0;
26245 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26246 hpos = row->used[TEXT_AREA] - 1;
26247
26248 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26249 hl, 0);
26250 w->phys_cursor_on_p = on_p;
26251
26252 if (hl == DRAW_CURSOR)
26253 w->phys_cursor_width = x1 - w->phys_cursor.x;
26254 /* When we erase the cursor, and ROW is overlapped by other
26255 rows, make sure that these overlapping parts of other rows
26256 are redrawn. */
26257 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26258 {
26259 w->phys_cursor_width = x1 - w->phys_cursor.x;
26260
26261 if (row > w->current_matrix->rows
26262 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26263 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26264 OVERLAPS_ERASED_CURSOR);
26265
26266 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26267 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26268 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26269 OVERLAPS_ERASED_CURSOR);
26270 }
26271 }
26272 }
26273
26274
26275 /* EXPORT:
26276 Erase the image of a cursor of window W from the screen. */
26277
26278 void
26279 erase_phys_cursor (struct window *w)
26280 {
26281 struct frame *f = XFRAME (w->frame);
26282 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26283 int hpos = w->phys_cursor.hpos;
26284 int vpos = w->phys_cursor.vpos;
26285 int mouse_face_here_p = 0;
26286 struct glyph_matrix *active_glyphs = w->current_matrix;
26287 struct glyph_row *cursor_row;
26288 struct glyph *cursor_glyph;
26289 enum draw_glyphs_face hl;
26290
26291 /* No cursor displayed or row invalidated => nothing to do on the
26292 screen. */
26293 if (w->phys_cursor_type == NO_CURSOR)
26294 goto mark_cursor_off;
26295
26296 /* VPOS >= active_glyphs->nrows means that window has been resized.
26297 Don't bother to erase the cursor. */
26298 if (vpos >= active_glyphs->nrows)
26299 goto mark_cursor_off;
26300
26301 /* If row containing cursor is marked invalid, there is nothing we
26302 can do. */
26303 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26304 if (!cursor_row->enabled_p)
26305 goto mark_cursor_off;
26306
26307 /* If line spacing is > 0, old cursor may only be partially visible in
26308 window after split-window. So adjust visible height. */
26309 cursor_row->visible_height = min (cursor_row->visible_height,
26310 window_text_bottom_y (w) - cursor_row->y);
26311
26312 /* If row is completely invisible, don't attempt to delete a cursor which
26313 isn't there. This can happen if cursor is at top of a window, and
26314 we switch to a buffer with a header line in that window. */
26315 if (cursor_row->visible_height <= 0)
26316 goto mark_cursor_off;
26317
26318 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26319 if (cursor_row->cursor_in_fringe_p)
26320 {
26321 cursor_row->cursor_in_fringe_p = 0;
26322 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26323 goto mark_cursor_off;
26324 }
26325
26326 /* This can happen when the new row is shorter than the old one.
26327 In this case, either draw_glyphs or clear_end_of_line
26328 should have cleared the cursor. Note that we wouldn't be
26329 able to erase the cursor in this case because we don't have a
26330 cursor glyph at hand. */
26331 if ((cursor_row->reversed_p
26332 ? (w->phys_cursor.hpos < 0)
26333 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26334 goto mark_cursor_off;
26335
26336 /* When the window is hscrolled, cursor hpos can legitimately be out
26337 of bounds, but we draw the cursor at the corresponding window
26338 margin in that case. */
26339 if (!cursor_row->reversed_p && hpos < 0)
26340 hpos = 0;
26341 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26342 hpos = cursor_row->used[TEXT_AREA] - 1;
26343
26344 /* If the cursor is in the mouse face area, redisplay that when
26345 we clear the cursor. */
26346 if (! NILP (hlinfo->mouse_face_window)
26347 && coords_in_mouse_face_p (w, hpos, vpos)
26348 /* Don't redraw the cursor's spot in mouse face if it is at the
26349 end of a line (on a newline). The cursor appears there, but
26350 mouse highlighting does not. */
26351 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26352 mouse_face_here_p = 1;
26353
26354 /* Maybe clear the display under the cursor. */
26355 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26356 {
26357 int x, y, left_x;
26358 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26359 int width;
26360
26361 cursor_glyph = get_phys_cursor_glyph (w);
26362 if (cursor_glyph == NULL)
26363 goto mark_cursor_off;
26364
26365 width = cursor_glyph->pixel_width;
26366 left_x = window_box_left_offset (w, TEXT_AREA);
26367 x = w->phys_cursor.x;
26368 if (x < left_x)
26369 width -= left_x - x;
26370 width = min (width, window_box_width (w, TEXT_AREA) - x);
26371 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26372 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26373
26374 if (width > 0)
26375 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26376 }
26377
26378 /* Erase the cursor by redrawing the character underneath it. */
26379 if (mouse_face_here_p)
26380 hl = DRAW_MOUSE_FACE;
26381 else
26382 hl = DRAW_NORMAL_TEXT;
26383 draw_phys_cursor_glyph (w, cursor_row, hl);
26384
26385 mark_cursor_off:
26386 w->phys_cursor_on_p = 0;
26387 w->phys_cursor_type = NO_CURSOR;
26388 }
26389
26390
26391 /* EXPORT:
26392 Display or clear cursor of window W. If ON is zero, clear the
26393 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26394 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26395
26396 void
26397 display_and_set_cursor (struct window *w, bool on,
26398 int hpos, int vpos, int x, int y)
26399 {
26400 struct frame *f = XFRAME (w->frame);
26401 int new_cursor_type;
26402 int new_cursor_width;
26403 int active_cursor;
26404 struct glyph_row *glyph_row;
26405 struct glyph *glyph;
26406
26407 /* This is pointless on invisible frames, and dangerous on garbaged
26408 windows and frames; in the latter case, the frame or window may
26409 be in the midst of changing its size, and x and y may be off the
26410 window. */
26411 if (! FRAME_VISIBLE_P (f)
26412 || FRAME_GARBAGED_P (f)
26413 || vpos >= w->current_matrix->nrows
26414 || hpos >= w->current_matrix->matrix_w)
26415 return;
26416
26417 /* If cursor is off and we want it off, return quickly. */
26418 if (!on && !w->phys_cursor_on_p)
26419 return;
26420
26421 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26422 /* If cursor row is not enabled, we don't really know where to
26423 display the cursor. */
26424 if (!glyph_row->enabled_p)
26425 {
26426 w->phys_cursor_on_p = 0;
26427 return;
26428 }
26429
26430 glyph = NULL;
26431 if (!glyph_row->exact_window_width_line_p
26432 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26433 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26434
26435 eassert (input_blocked_p ());
26436
26437 /* Set new_cursor_type to the cursor we want to be displayed. */
26438 new_cursor_type = get_window_cursor_type (w, glyph,
26439 &new_cursor_width, &active_cursor);
26440
26441 /* If cursor is currently being shown and we don't want it to be or
26442 it is in the wrong place, or the cursor type is not what we want,
26443 erase it. */
26444 if (w->phys_cursor_on_p
26445 && (!on
26446 || w->phys_cursor.x != x
26447 || w->phys_cursor.y != y
26448 || new_cursor_type != w->phys_cursor_type
26449 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26450 && new_cursor_width != w->phys_cursor_width)))
26451 erase_phys_cursor (w);
26452
26453 /* Don't check phys_cursor_on_p here because that flag is only set
26454 to zero in some cases where we know that the cursor has been
26455 completely erased, to avoid the extra work of erasing the cursor
26456 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26457 still not be visible, or it has only been partly erased. */
26458 if (on)
26459 {
26460 w->phys_cursor_ascent = glyph_row->ascent;
26461 w->phys_cursor_height = glyph_row->height;
26462
26463 /* Set phys_cursor_.* before x_draw_.* is called because some
26464 of them may need the information. */
26465 w->phys_cursor.x = x;
26466 w->phys_cursor.y = glyph_row->y;
26467 w->phys_cursor.hpos = hpos;
26468 w->phys_cursor.vpos = vpos;
26469 }
26470
26471 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26472 new_cursor_type, new_cursor_width,
26473 on, active_cursor);
26474 }
26475
26476
26477 /* Switch the display of W's cursor on or off, according to the value
26478 of ON. */
26479
26480 static void
26481 update_window_cursor (struct window *w, bool on)
26482 {
26483 /* Don't update cursor in windows whose frame is in the process
26484 of being deleted. */
26485 if (w->current_matrix)
26486 {
26487 int hpos = w->phys_cursor.hpos;
26488 int vpos = w->phys_cursor.vpos;
26489 struct glyph_row *row;
26490
26491 if (vpos >= w->current_matrix->nrows
26492 || hpos >= w->current_matrix->matrix_w)
26493 return;
26494
26495 row = MATRIX_ROW (w->current_matrix, vpos);
26496
26497 /* When the window is hscrolled, cursor hpos can legitimately be
26498 out of bounds, but we draw the cursor at the corresponding
26499 window margin in that case. */
26500 if (!row->reversed_p && hpos < 0)
26501 hpos = 0;
26502 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26503 hpos = row->used[TEXT_AREA] - 1;
26504
26505 block_input ();
26506 display_and_set_cursor (w, on, hpos, vpos,
26507 w->phys_cursor.x, w->phys_cursor.y);
26508 unblock_input ();
26509 }
26510 }
26511
26512
26513 /* Call update_window_cursor with parameter ON_P on all leaf windows
26514 in the window tree rooted at W. */
26515
26516 static void
26517 update_cursor_in_window_tree (struct window *w, bool on_p)
26518 {
26519 while (w)
26520 {
26521 if (WINDOWP (w->contents))
26522 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26523 else
26524 update_window_cursor (w, on_p);
26525
26526 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26527 }
26528 }
26529
26530
26531 /* EXPORT:
26532 Display the cursor on window W, or clear it, according to ON_P.
26533 Don't change the cursor's position. */
26534
26535 void
26536 x_update_cursor (struct frame *f, bool on_p)
26537 {
26538 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26539 }
26540
26541
26542 /* EXPORT:
26543 Clear the cursor of window W to background color, and mark the
26544 cursor as not shown. This is used when the text where the cursor
26545 is about to be rewritten. */
26546
26547 void
26548 x_clear_cursor (struct window *w)
26549 {
26550 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26551 update_window_cursor (w, 0);
26552 }
26553
26554 #endif /* HAVE_WINDOW_SYSTEM */
26555
26556 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26557 and MSDOS. */
26558 static void
26559 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26560 int start_hpos, int end_hpos,
26561 enum draw_glyphs_face draw)
26562 {
26563 #ifdef HAVE_WINDOW_SYSTEM
26564 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26565 {
26566 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26567 return;
26568 }
26569 #endif
26570 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26571 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26572 #endif
26573 }
26574
26575 /* Display the active region described by mouse_face_* according to DRAW. */
26576
26577 static void
26578 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26579 {
26580 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26581 struct frame *f = XFRAME (WINDOW_FRAME (w));
26582
26583 if (/* If window is in the process of being destroyed, don't bother
26584 to do anything. */
26585 w->current_matrix != NULL
26586 /* Don't update mouse highlight if hidden */
26587 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26588 /* Recognize when we are called to operate on rows that don't exist
26589 anymore. This can happen when a window is split. */
26590 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26591 {
26592 int phys_cursor_on_p = w->phys_cursor_on_p;
26593 struct glyph_row *row, *first, *last;
26594
26595 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26596 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26597
26598 for (row = first; row <= last && row->enabled_p; ++row)
26599 {
26600 int start_hpos, end_hpos, start_x;
26601
26602 /* For all but the first row, the highlight starts at column 0. */
26603 if (row == first)
26604 {
26605 /* R2L rows have BEG and END in reversed order, but the
26606 screen drawing geometry is always left to right. So
26607 we need to mirror the beginning and end of the
26608 highlighted area in R2L rows. */
26609 if (!row->reversed_p)
26610 {
26611 start_hpos = hlinfo->mouse_face_beg_col;
26612 start_x = hlinfo->mouse_face_beg_x;
26613 }
26614 else if (row == last)
26615 {
26616 start_hpos = hlinfo->mouse_face_end_col;
26617 start_x = hlinfo->mouse_face_end_x;
26618 }
26619 else
26620 {
26621 start_hpos = 0;
26622 start_x = 0;
26623 }
26624 }
26625 else if (row->reversed_p && row == last)
26626 {
26627 start_hpos = hlinfo->mouse_face_end_col;
26628 start_x = hlinfo->mouse_face_end_x;
26629 }
26630 else
26631 {
26632 start_hpos = 0;
26633 start_x = 0;
26634 }
26635
26636 if (row == last)
26637 {
26638 if (!row->reversed_p)
26639 end_hpos = hlinfo->mouse_face_end_col;
26640 else if (row == first)
26641 end_hpos = hlinfo->mouse_face_beg_col;
26642 else
26643 {
26644 end_hpos = row->used[TEXT_AREA];
26645 if (draw == DRAW_NORMAL_TEXT)
26646 row->fill_line_p = 1; /* Clear to end of line */
26647 }
26648 }
26649 else if (row->reversed_p && row == first)
26650 end_hpos = hlinfo->mouse_face_beg_col;
26651 else
26652 {
26653 end_hpos = row->used[TEXT_AREA];
26654 if (draw == DRAW_NORMAL_TEXT)
26655 row->fill_line_p = 1; /* Clear to end of line */
26656 }
26657
26658 if (end_hpos > start_hpos)
26659 {
26660 draw_row_with_mouse_face (w, start_x, row,
26661 start_hpos, end_hpos, draw);
26662
26663 row->mouse_face_p
26664 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26665 }
26666 }
26667
26668 #ifdef HAVE_WINDOW_SYSTEM
26669 /* When we've written over the cursor, arrange for it to
26670 be displayed again. */
26671 if (FRAME_WINDOW_P (f)
26672 && phys_cursor_on_p && !w->phys_cursor_on_p)
26673 {
26674 int hpos = w->phys_cursor.hpos;
26675
26676 /* When the window is hscrolled, cursor hpos can legitimately be
26677 out of bounds, but we draw the cursor at the corresponding
26678 window margin in that case. */
26679 if (!row->reversed_p && hpos < 0)
26680 hpos = 0;
26681 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26682 hpos = row->used[TEXT_AREA] - 1;
26683
26684 block_input ();
26685 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26686 w->phys_cursor.x, w->phys_cursor.y);
26687 unblock_input ();
26688 }
26689 #endif /* HAVE_WINDOW_SYSTEM */
26690 }
26691
26692 #ifdef HAVE_WINDOW_SYSTEM
26693 /* Change the mouse cursor. */
26694 if (FRAME_WINDOW_P (f))
26695 {
26696 if (draw == DRAW_NORMAL_TEXT
26697 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26698 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26699 else if (draw == DRAW_MOUSE_FACE)
26700 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26701 else
26702 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26703 }
26704 #endif /* HAVE_WINDOW_SYSTEM */
26705 }
26706
26707 /* EXPORT:
26708 Clear out the mouse-highlighted active region.
26709 Redraw it un-highlighted first. Value is non-zero if mouse
26710 face was actually drawn unhighlighted. */
26711
26712 int
26713 clear_mouse_face (Mouse_HLInfo *hlinfo)
26714 {
26715 int cleared = 0;
26716
26717 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26718 {
26719 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26720 cleared = 1;
26721 }
26722
26723 reset_mouse_highlight (hlinfo);
26724 return cleared;
26725 }
26726
26727 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26728 within the mouse face on that window. */
26729 static int
26730 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26731 {
26732 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26733
26734 /* Quickly resolve the easy cases. */
26735 if (!(WINDOWP (hlinfo->mouse_face_window)
26736 && XWINDOW (hlinfo->mouse_face_window) == w))
26737 return 0;
26738 if (vpos < hlinfo->mouse_face_beg_row
26739 || vpos > hlinfo->mouse_face_end_row)
26740 return 0;
26741 if (vpos > hlinfo->mouse_face_beg_row
26742 && vpos < hlinfo->mouse_face_end_row)
26743 return 1;
26744
26745 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26746 {
26747 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26748 {
26749 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26750 return 1;
26751 }
26752 else if ((vpos == hlinfo->mouse_face_beg_row
26753 && hpos >= hlinfo->mouse_face_beg_col)
26754 || (vpos == hlinfo->mouse_face_end_row
26755 && hpos < hlinfo->mouse_face_end_col))
26756 return 1;
26757 }
26758 else
26759 {
26760 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26761 {
26762 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26763 return 1;
26764 }
26765 else if ((vpos == hlinfo->mouse_face_beg_row
26766 && hpos <= hlinfo->mouse_face_beg_col)
26767 || (vpos == hlinfo->mouse_face_end_row
26768 && hpos > hlinfo->mouse_face_end_col))
26769 return 1;
26770 }
26771 return 0;
26772 }
26773
26774
26775 /* EXPORT:
26776 Non-zero if physical cursor of window W is within mouse face. */
26777
26778 int
26779 cursor_in_mouse_face_p (struct window *w)
26780 {
26781 int hpos = w->phys_cursor.hpos;
26782 int vpos = w->phys_cursor.vpos;
26783 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26784
26785 /* When the window is hscrolled, cursor hpos can legitimately be out
26786 of bounds, but we draw the cursor at the corresponding window
26787 margin in that case. */
26788 if (!row->reversed_p && hpos < 0)
26789 hpos = 0;
26790 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26791 hpos = row->used[TEXT_AREA] - 1;
26792
26793 return coords_in_mouse_face_p (w, hpos, vpos);
26794 }
26795
26796
26797 \f
26798 /* Find the glyph rows START_ROW and END_ROW of window W that display
26799 characters between buffer positions START_CHARPOS and END_CHARPOS
26800 (excluding END_CHARPOS). DISP_STRING is a display string that
26801 covers these buffer positions. This is similar to
26802 row_containing_pos, but is more accurate when bidi reordering makes
26803 buffer positions change non-linearly with glyph rows. */
26804 static void
26805 rows_from_pos_range (struct window *w,
26806 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26807 Lisp_Object disp_string,
26808 struct glyph_row **start, struct glyph_row **end)
26809 {
26810 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26811 int last_y = window_text_bottom_y (w);
26812 struct glyph_row *row;
26813
26814 *start = NULL;
26815 *end = NULL;
26816
26817 while (!first->enabled_p
26818 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26819 first++;
26820
26821 /* Find the START row. */
26822 for (row = first;
26823 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26824 row++)
26825 {
26826 /* A row can potentially be the START row if the range of the
26827 characters it displays intersects the range
26828 [START_CHARPOS..END_CHARPOS). */
26829 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26830 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26831 /* See the commentary in row_containing_pos, for the
26832 explanation of the complicated way to check whether
26833 some position is beyond the end of the characters
26834 displayed by a row. */
26835 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26836 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26837 && !row->ends_at_zv_p
26838 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26839 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26840 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26841 && !row->ends_at_zv_p
26842 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26843 {
26844 /* Found a candidate row. Now make sure at least one of the
26845 glyphs it displays has a charpos from the range
26846 [START_CHARPOS..END_CHARPOS).
26847
26848 This is not obvious because bidi reordering could make
26849 buffer positions of a row be 1,2,3,102,101,100, and if we
26850 want to highlight characters in [50..60), we don't want
26851 this row, even though [50..60) does intersect [1..103),
26852 the range of character positions given by the row's start
26853 and end positions. */
26854 struct glyph *g = row->glyphs[TEXT_AREA];
26855 struct glyph *e = g + row->used[TEXT_AREA];
26856
26857 while (g < e)
26858 {
26859 if (((BUFFERP (g->object) || INTEGERP (g->object))
26860 && start_charpos <= g->charpos && g->charpos < end_charpos)
26861 /* A glyph that comes from DISP_STRING is by
26862 definition to be highlighted. */
26863 || EQ (g->object, disp_string))
26864 *start = row;
26865 g++;
26866 }
26867 if (*start)
26868 break;
26869 }
26870 }
26871
26872 /* Find the END row. */
26873 if (!*start
26874 /* If the last row is partially visible, start looking for END
26875 from that row, instead of starting from FIRST. */
26876 && !(row->enabled_p
26877 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26878 row = first;
26879 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26880 {
26881 struct glyph_row *next = row + 1;
26882 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26883
26884 if (!next->enabled_p
26885 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26886 /* The first row >= START whose range of displayed characters
26887 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26888 is the row END + 1. */
26889 || (start_charpos < next_start
26890 && end_charpos < next_start)
26891 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26892 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26893 && !next->ends_at_zv_p
26894 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26895 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26896 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26897 && !next->ends_at_zv_p
26898 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26899 {
26900 *end = row;
26901 break;
26902 }
26903 else
26904 {
26905 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26906 but none of the characters it displays are in the range, it is
26907 also END + 1. */
26908 struct glyph *g = next->glyphs[TEXT_AREA];
26909 struct glyph *s = g;
26910 struct glyph *e = g + next->used[TEXT_AREA];
26911
26912 while (g < e)
26913 {
26914 if (((BUFFERP (g->object) || INTEGERP (g->object))
26915 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26916 /* If the buffer position of the first glyph in
26917 the row is equal to END_CHARPOS, it means
26918 the last character to be highlighted is the
26919 newline of ROW, and we must consider NEXT as
26920 END, not END+1. */
26921 || (((!next->reversed_p && g == s)
26922 || (next->reversed_p && g == e - 1))
26923 && (g->charpos == end_charpos
26924 /* Special case for when NEXT is an
26925 empty line at ZV. */
26926 || (g->charpos == -1
26927 && !row->ends_at_zv_p
26928 && next_start == end_charpos)))))
26929 /* A glyph that comes from DISP_STRING is by
26930 definition to be highlighted. */
26931 || EQ (g->object, disp_string))
26932 break;
26933 g++;
26934 }
26935 if (g == e)
26936 {
26937 *end = row;
26938 break;
26939 }
26940 /* The first row that ends at ZV must be the last to be
26941 highlighted. */
26942 else if (next->ends_at_zv_p)
26943 {
26944 *end = next;
26945 break;
26946 }
26947 }
26948 }
26949 }
26950
26951 /* This function sets the mouse_face_* elements of HLINFO, assuming
26952 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26953 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26954 for the overlay or run of text properties specifying the mouse
26955 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26956 before-string and after-string that must also be highlighted.
26957 DISP_STRING, if non-nil, is a display string that may cover some
26958 or all of the highlighted text. */
26959
26960 static void
26961 mouse_face_from_buffer_pos (Lisp_Object window,
26962 Mouse_HLInfo *hlinfo,
26963 ptrdiff_t mouse_charpos,
26964 ptrdiff_t start_charpos,
26965 ptrdiff_t end_charpos,
26966 Lisp_Object before_string,
26967 Lisp_Object after_string,
26968 Lisp_Object disp_string)
26969 {
26970 struct window *w = XWINDOW (window);
26971 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26972 struct glyph_row *r1, *r2;
26973 struct glyph *glyph, *end;
26974 ptrdiff_t ignore, pos;
26975 int x;
26976
26977 eassert (NILP (disp_string) || STRINGP (disp_string));
26978 eassert (NILP (before_string) || STRINGP (before_string));
26979 eassert (NILP (after_string) || STRINGP (after_string));
26980
26981 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26982 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26983 if (r1 == NULL)
26984 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
26985 /* If the before-string or display-string contains newlines,
26986 rows_from_pos_range skips to its last row. Move back. */
26987 if (!NILP (before_string) || !NILP (disp_string))
26988 {
26989 struct glyph_row *prev;
26990 while ((prev = r1 - 1, prev >= first)
26991 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26992 && prev->used[TEXT_AREA] > 0)
26993 {
26994 struct glyph *beg = prev->glyphs[TEXT_AREA];
26995 glyph = beg + prev->used[TEXT_AREA];
26996 while (--glyph >= beg && INTEGERP (glyph->object));
26997 if (glyph < beg
26998 || !(EQ (glyph->object, before_string)
26999 || EQ (glyph->object, disp_string)))
27000 break;
27001 r1 = prev;
27002 }
27003 }
27004 if (r2 == NULL)
27005 {
27006 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27007 hlinfo->mouse_face_past_end = 1;
27008 }
27009 else if (!NILP (after_string))
27010 {
27011 /* If the after-string has newlines, advance to its last row. */
27012 struct glyph_row *next;
27013 struct glyph_row *last
27014 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27015
27016 for (next = r2 + 1;
27017 next <= last
27018 && next->used[TEXT_AREA] > 0
27019 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27020 ++next)
27021 r2 = next;
27022 }
27023 /* The rest of the display engine assumes that mouse_face_beg_row is
27024 either above mouse_face_end_row or identical to it. But with
27025 bidi-reordered continued lines, the row for START_CHARPOS could
27026 be below the row for END_CHARPOS. If so, swap the rows and store
27027 them in correct order. */
27028 if (r1->y > r2->y)
27029 {
27030 struct glyph_row *tem = r2;
27031
27032 r2 = r1;
27033 r1 = tem;
27034 }
27035
27036 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27037 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27038
27039 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27040 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27041 could be anywhere in the row and in any order. The strategy
27042 below is to find the leftmost and the rightmost glyph that
27043 belongs to either of these 3 strings, or whose position is
27044 between START_CHARPOS and END_CHARPOS, and highlight all the
27045 glyphs between those two. This may cover more than just the text
27046 between START_CHARPOS and END_CHARPOS if the range of characters
27047 strides the bidi level boundary, e.g. if the beginning is in R2L
27048 text while the end is in L2R text or vice versa. */
27049 if (!r1->reversed_p)
27050 {
27051 /* This row is in a left to right paragraph. Scan it left to
27052 right. */
27053 glyph = r1->glyphs[TEXT_AREA];
27054 end = glyph + r1->used[TEXT_AREA];
27055 x = r1->x;
27056
27057 /* Skip truncation glyphs at the start of the glyph row. */
27058 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27059 for (; glyph < end
27060 && INTEGERP (glyph->object)
27061 && glyph->charpos < 0;
27062 ++glyph)
27063 x += glyph->pixel_width;
27064
27065 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27066 or DISP_STRING, and the first glyph from buffer whose
27067 position is between START_CHARPOS and END_CHARPOS. */
27068 for (; glyph < end
27069 && !INTEGERP (glyph->object)
27070 && !EQ (glyph->object, disp_string)
27071 && !(BUFFERP (glyph->object)
27072 && (glyph->charpos >= start_charpos
27073 && glyph->charpos < end_charpos));
27074 ++glyph)
27075 {
27076 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27077 are present at buffer positions between START_CHARPOS and
27078 END_CHARPOS, or if they come from an overlay. */
27079 if (EQ (glyph->object, before_string))
27080 {
27081 pos = string_buffer_position (before_string,
27082 start_charpos);
27083 /* If pos == 0, it means before_string came from an
27084 overlay, not from a buffer position. */
27085 if (!pos || (pos >= start_charpos && pos < end_charpos))
27086 break;
27087 }
27088 else if (EQ (glyph->object, after_string))
27089 {
27090 pos = string_buffer_position (after_string, end_charpos);
27091 if (!pos || (pos >= start_charpos && pos < end_charpos))
27092 break;
27093 }
27094 x += glyph->pixel_width;
27095 }
27096 hlinfo->mouse_face_beg_x = x;
27097 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27098 }
27099 else
27100 {
27101 /* This row is in a right to left paragraph. Scan it right to
27102 left. */
27103 struct glyph *g;
27104
27105 end = r1->glyphs[TEXT_AREA] - 1;
27106 glyph = end + r1->used[TEXT_AREA];
27107
27108 /* Skip truncation glyphs at the start of the glyph row. */
27109 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27110 for (; glyph > end
27111 && INTEGERP (glyph->object)
27112 && glyph->charpos < 0;
27113 --glyph)
27114 ;
27115
27116 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27117 or DISP_STRING, and the first glyph from buffer whose
27118 position is between START_CHARPOS and END_CHARPOS. */
27119 for (; glyph > end
27120 && !INTEGERP (glyph->object)
27121 && !EQ (glyph->object, disp_string)
27122 && !(BUFFERP (glyph->object)
27123 && (glyph->charpos >= start_charpos
27124 && glyph->charpos < end_charpos));
27125 --glyph)
27126 {
27127 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27128 are present at buffer positions between START_CHARPOS and
27129 END_CHARPOS, or if they come from an overlay. */
27130 if (EQ (glyph->object, before_string))
27131 {
27132 pos = string_buffer_position (before_string, start_charpos);
27133 /* If pos == 0, it means before_string came from an
27134 overlay, not from a buffer position. */
27135 if (!pos || (pos >= start_charpos && pos < end_charpos))
27136 break;
27137 }
27138 else if (EQ (glyph->object, after_string))
27139 {
27140 pos = string_buffer_position (after_string, end_charpos);
27141 if (!pos || (pos >= start_charpos && pos < end_charpos))
27142 break;
27143 }
27144 }
27145
27146 glyph++; /* first glyph to the right of the highlighted area */
27147 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27148 x += g->pixel_width;
27149 hlinfo->mouse_face_beg_x = x;
27150 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27151 }
27152
27153 /* If the highlight ends in a different row, compute GLYPH and END
27154 for the end row. Otherwise, reuse the values computed above for
27155 the row where the highlight begins. */
27156 if (r2 != r1)
27157 {
27158 if (!r2->reversed_p)
27159 {
27160 glyph = r2->glyphs[TEXT_AREA];
27161 end = glyph + r2->used[TEXT_AREA];
27162 x = r2->x;
27163 }
27164 else
27165 {
27166 end = r2->glyphs[TEXT_AREA] - 1;
27167 glyph = end + r2->used[TEXT_AREA];
27168 }
27169 }
27170
27171 if (!r2->reversed_p)
27172 {
27173 /* Skip truncation and continuation glyphs near the end of the
27174 row, and also blanks and stretch glyphs inserted by
27175 extend_face_to_end_of_line. */
27176 while (end > glyph
27177 && INTEGERP ((end - 1)->object))
27178 --end;
27179 /* Scan the rest of the glyph row from the end, looking for the
27180 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27181 DISP_STRING, or whose position is between START_CHARPOS
27182 and END_CHARPOS */
27183 for (--end;
27184 end > glyph
27185 && !INTEGERP (end->object)
27186 && !EQ (end->object, disp_string)
27187 && !(BUFFERP (end->object)
27188 && (end->charpos >= start_charpos
27189 && end->charpos < end_charpos));
27190 --end)
27191 {
27192 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27193 are present at buffer positions between START_CHARPOS and
27194 END_CHARPOS, or if they come from an overlay. */
27195 if (EQ (end->object, before_string))
27196 {
27197 pos = string_buffer_position (before_string, start_charpos);
27198 if (!pos || (pos >= start_charpos && pos < end_charpos))
27199 break;
27200 }
27201 else if (EQ (end->object, after_string))
27202 {
27203 pos = string_buffer_position (after_string, end_charpos);
27204 if (!pos || (pos >= start_charpos && pos < end_charpos))
27205 break;
27206 }
27207 }
27208 /* Find the X coordinate of the last glyph to be highlighted. */
27209 for (; glyph <= end; ++glyph)
27210 x += glyph->pixel_width;
27211
27212 hlinfo->mouse_face_end_x = x;
27213 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27214 }
27215 else
27216 {
27217 /* Skip truncation and continuation glyphs near the end of the
27218 row, and also blanks and stretch glyphs inserted by
27219 extend_face_to_end_of_line. */
27220 x = r2->x;
27221 end++;
27222 while (end < glyph
27223 && INTEGERP (end->object))
27224 {
27225 x += end->pixel_width;
27226 ++end;
27227 }
27228 /* Scan the rest of the glyph row from the end, looking for the
27229 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27230 DISP_STRING, or whose position is between START_CHARPOS
27231 and END_CHARPOS */
27232 for ( ;
27233 end < glyph
27234 && !INTEGERP (end->object)
27235 && !EQ (end->object, disp_string)
27236 && !(BUFFERP (end->object)
27237 && (end->charpos >= start_charpos
27238 && end->charpos < end_charpos));
27239 ++end)
27240 {
27241 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27242 are present at buffer positions between START_CHARPOS and
27243 END_CHARPOS, or if they come from an overlay. */
27244 if (EQ (end->object, before_string))
27245 {
27246 pos = string_buffer_position (before_string, start_charpos);
27247 if (!pos || (pos >= start_charpos && pos < end_charpos))
27248 break;
27249 }
27250 else if (EQ (end->object, after_string))
27251 {
27252 pos = string_buffer_position (after_string, end_charpos);
27253 if (!pos || (pos >= start_charpos && pos < end_charpos))
27254 break;
27255 }
27256 x += end->pixel_width;
27257 }
27258 /* If we exited the above loop because we arrived at the last
27259 glyph of the row, and its buffer position is still not in
27260 range, it means the last character in range is the preceding
27261 newline. Bump the end column and x values to get past the
27262 last glyph. */
27263 if (end == glyph
27264 && BUFFERP (end->object)
27265 && (end->charpos < start_charpos
27266 || end->charpos >= end_charpos))
27267 {
27268 x += end->pixel_width;
27269 ++end;
27270 }
27271 hlinfo->mouse_face_end_x = x;
27272 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27273 }
27274
27275 hlinfo->mouse_face_window = window;
27276 hlinfo->mouse_face_face_id
27277 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27278 mouse_charpos + 1,
27279 !hlinfo->mouse_face_hidden, -1);
27280 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27281 }
27282
27283 /* The following function is not used anymore (replaced with
27284 mouse_face_from_string_pos), but I leave it here for the time
27285 being, in case someone would. */
27286
27287 #if 0 /* not used */
27288
27289 /* Find the position of the glyph for position POS in OBJECT in
27290 window W's current matrix, and return in *X, *Y the pixel
27291 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27292
27293 RIGHT_P non-zero means return the position of the right edge of the
27294 glyph, RIGHT_P zero means return the left edge position.
27295
27296 If no glyph for POS exists in the matrix, return the position of
27297 the glyph with the next smaller position that is in the matrix, if
27298 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27299 exists in the matrix, return the position of the glyph with the
27300 next larger position in OBJECT.
27301
27302 Value is non-zero if a glyph was found. */
27303
27304 static int
27305 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27306 int *hpos, int *vpos, int *x, int *y, int right_p)
27307 {
27308 int yb = window_text_bottom_y (w);
27309 struct glyph_row *r;
27310 struct glyph *best_glyph = NULL;
27311 struct glyph_row *best_row = NULL;
27312 int best_x = 0;
27313
27314 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27315 r->enabled_p && r->y < yb;
27316 ++r)
27317 {
27318 struct glyph *g = r->glyphs[TEXT_AREA];
27319 struct glyph *e = g + r->used[TEXT_AREA];
27320 int gx;
27321
27322 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27323 if (EQ (g->object, object))
27324 {
27325 if (g->charpos == pos)
27326 {
27327 best_glyph = g;
27328 best_x = gx;
27329 best_row = r;
27330 goto found;
27331 }
27332 else if (best_glyph == NULL
27333 || ((eabs (g->charpos - pos)
27334 < eabs (best_glyph->charpos - pos))
27335 && (right_p
27336 ? g->charpos < pos
27337 : g->charpos > pos)))
27338 {
27339 best_glyph = g;
27340 best_x = gx;
27341 best_row = r;
27342 }
27343 }
27344 }
27345
27346 found:
27347
27348 if (best_glyph)
27349 {
27350 *x = best_x;
27351 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27352
27353 if (right_p)
27354 {
27355 *x += best_glyph->pixel_width;
27356 ++*hpos;
27357 }
27358
27359 *y = best_row->y;
27360 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27361 }
27362
27363 return best_glyph != NULL;
27364 }
27365 #endif /* not used */
27366
27367 /* Find the positions of the first and the last glyphs in window W's
27368 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27369 (assumed to be a string), and return in HLINFO's mouse_face_*
27370 members the pixel and column/row coordinates of those glyphs. */
27371
27372 static void
27373 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27374 Lisp_Object object,
27375 ptrdiff_t startpos, ptrdiff_t endpos)
27376 {
27377 int yb = window_text_bottom_y (w);
27378 struct glyph_row *r;
27379 struct glyph *g, *e;
27380 int gx;
27381 int found = 0;
27382
27383 /* Find the glyph row with at least one position in the range
27384 [STARTPOS..ENDPOS], and the first glyph in that row whose
27385 position belongs to that range. */
27386 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27387 r->enabled_p && r->y < yb;
27388 ++r)
27389 {
27390 if (!r->reversed_p)
27391 {
27392 g = r->glyphs[TEXT_AREA];
27393 e = g + r->used[TEXT_AREA];
27394 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27395 if (EQ (g->object, object)
27396 && startpos <= g->charpos && g->charpos <= endpos)
27397 {
27398 hlinfo->mouse_face_beg_row
27399 = MATRIX_ROW_VPOS (r, w->current_matrix);
27400 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27401 hlinfo->mouse_face_beg_x = gx;
27402 found = 1;
27403 break;
27404 }
27405 }
27406 else
27407 {
27408 struct glyph *g1;
27409
27410 e = r->glyphs[TEXT_AREA];
27411 g = e + r->used[TEXT_AREA];
27412 for ( ; g > e; --g)
27413 if (EQ ((g-1)->object, object)
27414 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27415 {
27416 hlinfo->mouse_face_beg_row
27417 = MATRIX_ROW_VPOS (r, w->current_matrix);
27418 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27419 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27420 gx += g1->pixel_width;
27421 hlinfo->mouse_face_beg_x = gx;
27422 found = 1;
27423 break;
27424 }
27425 }
27426 if (found)
27427 break;
27428 }
27429
27430 if (!found)
27431 return;
27432
27433 /* Starting with the next row, look for the first row which does NOT
27434 include any glyphs whose positions are in the range. */
27435 for (++r; r->enabled_p && r->y < yb; ++r)
27436 {
27437 g = r->glyphs[TEXT_AREA];
27438 e = g + r->used[TEXT_AREA];
27439 found = 0;
27440 for ( ; g < e; ++g)
27441 if (EQ (g->object, object)
27442 && startpos <= g->charpos && g->charpos <= endpos)
27443 {
27444 found = 1;
27445 break;
27446 }
27447 if (!found)
27448 break;
27449 }
27450
27451 /* The highlighted region ends on the previous row. */
27452 r--;
27453
27454 /* Set the end row. */
27455 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27456
27457 /* Compute and set the end column and the end column's horizontal
27458 pixel coordinate. */
27459 if (!r->reversed_p)
27460 {
27461 g = r->glyphs[TEXT_AREA];
27462 e = g + r->used[TEXT_AREA];
27463 for ( ; e > g; --e)
27464 if (EQ ((e-1)->object, object)
27465 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27466 break;
27467 hlinfo->mouse_face_end_col = e - g;
27468
27469 for (gx = r->x; g < e; ++g)
27470 gx += g->pixel_width;
27471 hlinfo->mouse_face_end_x = gx;
27472 }
27473 else
27474 {
27475 e = r->glyphs[TEXT_AREA];
27476 g = e + r->used[TEXT_AREA];
27477 for (gx = r->x ; e < g; ++e)
27478 {
27479 if (EQ (e->object, object)
27480 && startpos <= e->charpos && e->charpos <= endpos)
27481 break;
27482 gx += e->pixel_width;
27483 }
27484 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27485 hlinfo->mouse_face_end_x = gx;
27486 }
27487 }
27488
27489 #ifdef HAVE_WINDOW_SYSTEM
27490
27491 /* See if position X, Y is within a hot-spot of an image. */
27492
27493 static int
27494 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27495 {
27496 if (!CONSP (hot_spot))
27497 return 0;
27498
27499 if (EQ (XCAR (hot_spot), Qrect))
27500 {
27501 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27502 Lisp_Object rect = XCDR (hot_spot);
27503 Lisp_Object tem;
27504 if (!CONSP (rect))
27505 return 0;
27506 if (!CONSP (XCAR (rect)))
27507 return 0;
27508 if (!CONSP (XCDR (rect)))
27509 return 0;
27510 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27511 return 0;
27512 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27513 return 0;
27514 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27515 return 0;
27516 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27517 return 0;
27518 return 1;
27519 }
27520 else if (EQ (XCAR (hot_spot), Qcircle))
27521 {
27522 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27523 Lisp_Object circ = XCDR (hot_spot);
27524 Lisp_Object lr, lx0, ly0;
27525 if (CONSP (circ)
27526 && CONSP (XCAR (circ))
27527 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27528 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27529 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27530 {
27531 double r = XFLOATINT (lr);
27532 double dx = XINT (lx0) - x;
27533 double dy = XINT (ly0) - y;
27534 return (dx * dx + dy * dy <= r * r);
27535 }
27536 }
27537 else if (EQ (XCAR (hot_spot), Qpoly))
27538 {
27539 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27540 if (VECTORP (XCDR (hot_spot)))
27541 {
27542 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27543 Lisp_Object *poly = v->contents;
27544 ptrdiff_t n = v->header.size;
27545 ptrdiff_t i;
27546 int inside = 0;
27547 Lisp_Object lx, ly;
27548 int x0, y0;
27549
27550 /* Need an even number of coordinates, and at least 3 edges. */
27551 if (n < 6 || n & 1)
27552 return 0;
27553
27554 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27555 If count is odd, we are inside polygon. Pixels on edges
27556 may or may not be included depending on actual geometry of the
27557 polygon. */
27558 if ((lx = poly[n-2], !INTEGERP (lx))
27559 || (ly = poly[n-1], !INTEGERP (lx)))
27560 return 0;
27561 x0 = XINT (lx), y0 = XINT (ly);
27562 for (i = 0; i < n; i += 2)
27563 {
27564 int x1 = x0, y1 = y0;
27565 if ((lx = poly[i], !INTEGERP (lx))
27566 || (ly = poly[i+1], !INTEGERP (ly)))
27567 return 0;
27568 x0 = XINT (lx), y0 = XINT (ly);
27569
27570 /* Does this segment cross the X line? */
27571 if (x0 >= x)
27572 {
27573 if (x1 >= x)
27574 continue;
27575 }
27576 else if (x1 < x)
27577 continue;
27578 if (y > y0 && y > y1)
27579 continue;
27580 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27581 inside = !inside;
27582 }
27583 return inside;
27584 }
27585 }
27586 return 0;
27587 }
27588
27589 Lisp_Object
27590 find_hot_spot (Lisp_Object map, int x, int y)
27591 {
27592 while (CONSP (map))
27593 {
27594 if (CONSP (XCAR (map))
27595 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27596 return XCAR (map);
27597 map = XCDR (map);
27598 }
27599
27600 return Qnil;
27601 }
27602
27603 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27604 3, 3, 0,
27605 doc: /* Lookup in image map MAP coordinates X and Y.
27606 An image map is an alist where each element has the format (AREA ID PLIST).
27607 An AREA is specified as either a rectangle, a circle, or a polygon:
27608 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27609 pixel coordinates of the upper left and bottom right corners.
27610 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27611 and the radius of the circle; r may be a float or integer.
27612 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27613 vector describes one corner in the polygon.
27614 Returns the alist element for the first matching AREA in MAP. */)
27615 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27616 {
27617 if (NILP (map))
27618 return Qnil;
27619
27620 CHECK_NUMBER (x);
27621 CHECK_NUMBER (y);
27622
27623 return find_hot_spot (map,
27624 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27625 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27626 }
27627
27628
27629 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27630 static void
27631 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27632 {
27633 /* Do not change cursor shape while dragging mouse. */
27634 if (!NILP (do_mouse_tracking))
27635 return;
27636
27637 if (!NILP (pointer))
27638 {
27639 if (EQ (pointer, Qarrow))
27640 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27641 else if (EQ (pointer, Qhand))
27642 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27643 else if (EQ (pointer, Qtext))
27644 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27645 else if (EQ (pointer, intern ("hdrag")))
27646 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27647 #ifdef HAVE_X_WINDOWS
27648 else if (EQ (pointer, intern ("vdrag")))
27649 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27650 #endif
27651 else if (EQ (pointer, intern ("hourglass")))
27652 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27653 else if (EQ (pointer, Qmodeline))
27654 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27655 else
27656 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27657 }
27658
27659 if (cursor != No_Cursor)
27660 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27661 }
27662
27663 #endif /* HAVE_WINDOW_SYSTEM */
27664
27665 /* Take proper action when mouse has moved to the mode or header line
27666 or marginal area AREA of window W, x-position X and y-position Y.
27667 X is relative to the start of the text display area of W, so the
27668 width of bitmap areas and scroll bars must be subtracted to get a
27669 position relative to the start of the mode line. */
27670
27671 static void
27672 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27673 enum window_part area)
27674 {
27675 struct window *w = XWINDOW (window);
27676 struct frame *f = XFRAME (w->frame);
27677 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27678 #ifdef HAVE_WINDOW_SYSTEM
27679 Display_Info *dpyinfo;
27680 #endif
27681 Cursor cursor = No_Cursor;
27682 Lisp_Object pointer = Qnil;
27683 int dx, dy, width, height;
27684 ptrdiff_t charpos;
27685 Lisp_Object string, object = Qnil;
27686 Lisp_Object pos IF_LINT (= Qnil), help;
27687
27688 Lisp_Object mouse_face;
27689 int original_x_pixel = x;
27690 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27691 struct glyph_row *row IF_LINT (= 0);
27692
27693 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27694 {
27695 int x0;
27696 struct glyph *end;
27697
27698 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27699 returns them in row/column units! */
27700 string = mode_line_string (w, area, &x, &y, &charpos,
27701 &object, &dx, &dy, &width, &height);
27702
27703 row = (area == ON_MODE_LINE
27704 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27705 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27706
27707 /* Find the glyph under the mouse pointer. */
27708 if (row->mode_line_p && row->enabled_p)
27709 {
27710 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27711 end = glyph + row->used[TEXT_AREA];
27712
27713 for (x0 = original_x_pixel;
27714 glyph < end && x0 >= glyph->pixel_width;
27715 ++glyph)
27716 x0 -= glyph->pixel_width;
27717
27718 if (glyph >= end)
27719 glyph = NULL;
27720 }
27721 }
27722 else
27723 {
27724 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27725 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27726 returns them in row/column units! */
27727 string = marginal_area_string (w, area, &x, &y, &charpos,
27728 &object, &dx, &dy, &width, &height);
27729 }
27730
27731 help = Qnil;
27732
27733 #ifdef HAVE_WINDOW_SYSTEM
27734 if (IMAGEP (object))
27735 {
27736 Lisp_Object image_map, hotspot;
27737 if ((image_map = Fplist_get (XCDR (object), QCmap),
27738 !NILP (image_map))
27739 && (hotspot = find_hot_spot (image_map, dx, dy),
27740 CONSP (hotspot))
27741 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27742 {
27743 Lisp_Object plist;
27744
27745 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27746 If so, we could look for mouse-enter, mouse-leave
27747 properties in PLIST (and do something...). */
27748 hotspot = XCDR (hotspot);
27749 if (CONSP (hotspot)
27750 && (plist = XCAR (hotspot), CONSP (plist)))
27751 {
27752 pointer = Fplist_get (plist, Qpointer);
27753 if (NILP (pointer))
27754 pointer = Qhand;
27755 help = Fplist_get (plist, Qhelp_echo);
27756 if (!NILP (help))
27757 {
27758 help_echo_string = help;
27759 XSETWINDOW (help_echo_window, w);
27760 help_echo_object = w->contents;
27761 help_echo_pos = charpos;
27762 }
27763 }
27764 }
27765 if (NILP (pointer))
27766 pointer = Fplist_get (XCDR (object), QCpointer);
27767 }
27768 #endif /* HAVE_WINDOW_SYSTEM */
27769
27770 if (STRINGP (string))
27771 pos = make_number (charpos);
27772
27773 /* Set the help text and mouse pointer. If the mouse is on a part
27774 of the mode line without any text (e.g. past the right edge of
27775 the mode line text), use the default help text and pointer. */
27776 if (STRINGP (string) || area == ON_MODE_LINE)
27777 {
27778 /* Arrange to display the help by setting the global variables
27779 help_echo_string, help_echo_object, and help_echo_pos. */
27780 if (NILP (help))
27781 {
27782 if (STRINGP (string))
27783 help = Fget_text_property (pos, Qhelp_echo, string);
27784
27785 if (!NILP (help))
27786 {
27787 help_echo_string = help;
27788 XSETWINDOW (help_echo_window, w);
27789 help_echo_object = string;
27790 help_echo_pos = charpos;
27791 }
27792 else if (area == ON_MODE_LINE)
27793 {
27794 Lisp_Object default_help
27795 = buffer_local_value_1 (Qmode_line_default_help_echo,
27796 w->contents);
27797
27798 if (STRINGP (default_help))
27799 {
27800 help_echo_string = default_help;
27801 XSETWINDOW (help_echo_window, w);
27802 help_echo_object = Qnil;
27803 help_echo_pos = -1;
27804 }
27805 }
27806 }
27807
27808 #ifdef HAVE_WINDOW_SYSTEM
27809 /* Change the mouse pointer according to what is under it. */
27810 if (FRAME_WINDOW_P (f))
27811 {
27812 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27813 if (STRINGP (string))
27814 {
27815 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27816
27817 if (NILP (pointer))
27818 pointer = Fget_text_property (pos, Qpointer, string);
27819
27820 /* Change the mouse pointer according to what is under X/Y. */
27821 if (NILP (pointer)
27822 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27823 {
27824 Lisp_Object map;
27825 map = Fget_text_property (pos, Qlocal_map, string);
27826 if (!KEYMAPP (map))
27827 map = Fget_text_property (pos, Qkeymap, string);
27828 if (!KEYMAPP (map))
27829 cursor = dpyinfo->vertical_scroll_bar_cursor;
27830 }
27831 }
27832 else
27833 /* Default mode-line pointer. */
27834 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27835 }
27836 #endif
27837 }
27838
27839 /* Change the mouse face according to what is under X/Y. */
27840 if (STRINGP (string))
27841 {
27842 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27843 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27844 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27845 && glyph)
27846 {
27847 Lisp_Object b, e;
27848
27849 struct glyph * tmp_glyph;
27850
27851 int gpos;
27852 int gseq_length;
27853 int total_pixel_width;
27854 ptrdiff_t begpos, endpos, ignore;
27855
27856 int vpos, hpos;
27857
27858 b = Fprevious_single_property_change (make_number (charpos + 1),
27859 Qmouse_face, string, Qnil);
27860 if (NILP (b))
27861 begpos = 0;
27862 else
27863 begpos = XINT (b);
27864
27865 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27866 if (NILP (e))
27867 endpos = SCHARS (string);
27868 else
27869 endpos = XINT (e);
27870
27871 /* Calculate the glyph position GPOS of GLYPH in the
27872 displayed string, relative to the beginning of the
27873 highlighted part of the string.
27874
27875 Note: GPOS is different from CHARPOS. CHARPOS is the
27876 position of GLYPH in the internal string object. A mode
27877 line string format has structures which are converted to
27878 a flattened string by the Emacs Lisp interpreter. The
27879 internal string is an element of those structures. The
27880 displayed string is the flattened string. */
27881 tmp_glyph = row_start_glyph;
27882 while (tmp_glyph < glyph
27883 && (!(EQ (tmp_glyph->object, glyph->object)
27884 && begpos <= tmp_glyph->charpos
27885 && tmp_glyph->charpos < endpos)))
27886 tmp_glyph++;
27887 gpos = glyph - tmp_glyph;
27888
27889 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27890 the highlighted part of the displayed string to which
27891 GLYPH belongs. Note: GSEQ_LENGTH is different from
27892 SCHARS (STRING), because the latter returns the length of
27893 the internal string. */
27894 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27895 tmp_glyph > glyph
27896 && (!(EQ (tmp_glyph->object, glyph->object)
27897 && begpos <= tmp_glyph->charpos
27898 && tmp_glyph->charpos < endpos));
27899 tmp_glyph--)
27900 ;
27901 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27902
27903 /* Calculate the total pixel width of all the glyphs between
27904 the beginning of the highlighted area and GLYPH. */
27905 total_pixel_width = 0;
27906 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27907 total_pixel_width += tmp_glyph->pixel_width;
27908
27909 /* Pre calculation of re-rendering position. Note: X is in
27910 column units here, after the call to mode_line_string or
27911 marginal_area_string. */
27912 hpos = x - gpos;
27913 vpos = (area == ON_MODE_LINE
27914 ? (w->current_matrix)->nrows - 1
27915 : 0);
27916
27917 /* If GLYPH's position is included in the region that is
27918 already drawn in mouse face, we have nothing to do. */
27919 if ( EQ (window, hlinfo->mouse_face_window)
27920 && (!row->reversed_p
27921 ? (hlinfo->mouse_face_beg_col <= hpos
27922 && hpos < hlinfo->mouse_face_end_col)
27923 /* In R2L rows we swap BEG and END, see below. */
27924 : (hlinfo->mouse_face_end_col <= hpos
27925 && hpos < hlinfo->mouse_face_beg_col))
27926 && hlinfo->mouse_face_beg_row == vpos )
27927 return;
27928
27929 if (clear_mouse_face (hlinfo))
27930 cursor = No_Cursor;
27931
27932 if (!row->reversed_p)
27933 {
27934 hlinfo->mouse_face_beg_col = hpos;
27935 hlinfo->mouse_face_beg_x = original_x_pixel
27936 - (total_pixel_width + dx);
27937 hlinfo->mouse_face_end_col = hpos + gseq_length;
27938 hlinfo->mouse_face_end_x = 0;
27939 }
27940 else
27941 {
27942 /* In R2L rows, show_mouse_face expects BEG and END
27943 coordinates to be swapped. */
27944 hlinfo->mouse_face_end_col = hpos;
27945 hlinfo->mouse_face_end_x = original_x_pixel
27946 - (total_pixel_width + dx);
27947 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27948 hlinfo->mouse_face_beg_x = 0;
27949 }
27950
27951 hlinfo->mouse_face_beg_row = vpos;
27952 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27953 hlinfo->mouse_face_past_end = 0;
27954 hlinfo->mouse_face_window = window;
27955
27956 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27957 charpos,
27958 0, 0, 0,
27959 &ignore,
27960 glyph->face_id,
27961 1);
27962 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27963
27964 if (NILP (pointer))
27965 pointer = Qhand;
27966 }
27967 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27968 clear_mouse_face (hlinfo);
27969 }
27970 #ifdef HAVE_WINDOW_SYSTEM
27971 if (FRAME_WINDOW_P (f))
27972 define_frame_cursor1 (f, cursor, pointer);
27973 #endif
27974 }
27975
27976
27977 /* EXPORT:
27978 Take proper action when the mouse has moved to position X, Y on
27979 frame F with regards to highlighting portions of display that have
27980 mouse-face properties. Also de-highlight portions of display where
27981 the mouse was before, set the mouse pointer shape as appropriate
27982 for the mouse coordinates, and activate help echo (tooltips).
27983 X and Y can be negative or out of range. */
27984
27985 void
27986 note_mouse_highlight (struct frame *f, int x, int y)
27987 {
27988 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27989 enum window_part part = ON_NOTHING;
27990 Lisp_Object window;
27991 struct window *w;
27992 Cursor cursor = No_Cursor;
27993 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27994 struct buffer *b;
27995
27996 /* When a menu is active, don't highlight because this looks odd. */
27997 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27998 if (popup_activated ())
27999 return;
28000 #endif
28001
28002 if (!f->glyphs_initialized_p
28003 || f->pointer_invisible)
28004 return;
28005
28006 hlinfo->mouse_face_mouse_x = x;
28007 hlinfo->mouse_face_mouse_y = y;
28008 hlinfo->mouse_face_mouse_frame = f;
28009
28010 if (hlinfo->mouse_face_defer)
28011 return;
28012
28013 /* Which window is that in? */
28014 window = window_from_coordinates (f, x, y, &part, 1);
28015
28016 /* If displaying active text in another window, clear that. */
28017 if (! EQ (window, hlinfo->mouse_face_window)
28018 /* Also clear if we move out of text area in same window. */
28019 || (!NILP (hlinfo->mouse_face_window)
28020 && !NILP (window)
28021 && part != ON_TEXT
28022 && part != ON_MODE_LINE
28023 && part != ON_HEADER_LINE))
28024 clear_mouse_face (hlinfo);
28025
28026 /* Not on a window -> return. */
28027 if (!WINDOWP (window))
28028 return;
28029
28030 /* Reset help_echo_string. It will get recomputed below. */
28031 help_echo_string = Qnil;
28032
28033 /* Convert to window-relative pixel coordinates. */
28034 w = XWINDOW (window);
28035 frame_to_window_pixel_xy (w, &x, &y);
28036
28037 #ifdef HAVE_WINDOW_SYSTEM
28038 /* Handle tool-bar window differently since it doesn't display a
28039 buffer. */
28040 if (EQ (window, f->tool_bar_window))
28041 {
28042 note_tool_bar_highlight (f, x, y);
28043 return;
28044 }
28045 #endif
28046
28047 /* Mouse is on the mode, header line or margin? */
28048 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28049 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28050 {
28051 note_mode_line_or_margin_highlight (window, x, y, part);
28052 return;
28053 }
28054
28055 #ifdef HAVE_WINDOW_SYSTEM
28056 if (part == ON_VERTICAL_BORDER)
28057 {
28058 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28059 help_echo_string = build_string ("drag-mouse-1: resize");
28060 }
28061 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28062 || part == ON_SCROLL_BAR)
28063 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28064 else
28065 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28066 #endif
28067
28068 /* Are we in a window whose display is up to date?
28069 And verify the buffer's text has not changed. */
28070 b = XBUFFER (w->contents);
28071 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28072 {
28073 int hpos, vpos, dx, dy, area = LAST_AREA;
28074 ptrdiff_t pos;
28075 struct glyph *glyph;
28076 Lisp_Object object;
28077 Lisp_Object mouse_face = Qnil, position;
28078 Lisp_Object *overlay_vec = NULL;
28079 ptrdiff_t i, noverlays;
28080 struct buffer *obuf;
28081 ptrdiff_t obegv, ozv;
28082 int same_region;
28083
28084 /* Find the glyph under X/Y. */
28085 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28086
28087 #ifdef HAVE_WINDOW_SYSTEM
28088 /* Look for :pointer property on image. */
28089 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28090 {
28091 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28092 if (img != NULL && IMAGEP (img->spec))
28093 {
28094 Lisp_Object image_map, hotspot;
28095 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28096 !NILP (image_map))
28097 && (hotspot = find_hot_spot (image_map,
28098 glyph->slice.img.x + dx,
28099 glyph->slice.img.y + dy),
28100 CONSP (hotspot))
28101 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28102 {
28103 Lisp_Object plist;
28104
28105 /* Could check XCAR (hotspot) to see if we enter/leave
28106 this hot-spot.
28107 If so, we could look for mouse-enter, mouse-leave
28108 properties in PLIST (and do something...). */
28109 hotspot = XCDR (hotspot);
28110 if (CONSP (hotspot)
28111 && (plist = XCAR (hotspot), CONSP (plist)))
28112 {
28113 pointer = Fplist_get (plist, Qpointer);
28114 if (NILP (pointer))
28115 pointer = Qhand;
28116 help_echo_string = Fplist_get (plist, Qhelp_echo);
28117 if (!NILP (help_echo_string))
28118 {
28119 help_echo_window = window;
28120 help_echo_object = glyph->object;
28121 help_echo_pos = glyph->charpos;
28122 }
28123 }
28124 }
28125 if (NILP (pointer))
28126 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28127 }
28128 }
28129 #endif /* HAVE_WINDOW_SYSTEM */
28130
28131 /* Clear mouse face if X/Y not over text. */
28132 if (glyph == NULL
28133 || area != TEXT_AREA
28134 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28135 /* Glyph's OBJECT is an integer for glyphs inserted by the
28136 display engine for its internal purposes, like truncation
28137 and continuation glyphs and blanks beyond the end of
28138 line's text on text terminals. If we are over such a
28139 glyph, we are not over any text. */
28140 || INTEGERP (glyph->object)
28141 /* R2L rows have a stretch glyph at their front, which
28142 stands for no text, whereas L2R rows have no glyphs at
28143 all beyond the end of text. Treat such stretch glyphs
28144 like we do with NULL glyphs in L2R rows. */
28145 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28146 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28147 && glyph->type == STRETCH_GLYPH
28148 && glyph->avoid_cursor_p))
28149 {
28150 if (clear_mouse_face (hlinfo))
28151 cursor = No_Cursor;
28152 #ifdef HAVE_WINDOW_SYSTEM
28153 if (FRAME_WINDOW_P (f) && NILP (pointer))
28154 {
28155 if (area != TEXT_AREA)
28156 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28157 else
28158 pointer = Vvoid_text_area_pointer;
28159 }
28160 #endif
28161 goto set_cursor;
28162 }
28163
28164 pos = glyph->charpos;
28165 object = glyph->object;
28166 if (!STRINGP (object) && !BUFFERP (object))
28167 goto set_cursor;
28168
28169 /* If we get an out-of-range value, return now; avoid an error. */
28170 if (BUFFERP (object) && pos > BUF_Z (b))
28171 goto set_cursor;
28172
28173 /* Make the window's buffer temporarily current for
28174 overlays_at and compute_char_face. */
28175 obuf = current_buffer;
28176 current_buffer = b;
28177 obegv = BEGV;
28178 ozv = ZV;
28179 BEGV = BEG;
28180 ZV = Z;
28181
28182 /* Is this char mouse-active or does it have help-echo? */
28183 position = make_number (pos);
28184
28185 if (BUFFERP (object))
28186 {
28187 /* Put all the overlays we want in a vector in overlay_vec. */
28188 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28189 /* Sort overlays into increasing priority order. */
28190 noverlays = sort_overlays (overlay_vec, noverlays, w);
28191 }
28192 else
28193 noverlays = 0;
28194
28195 if (NILP (Vmouse_highlight))
28196 {
28197 clear_mouse_face (hlinfo);
28198 goto check_help_echo;
28199 }
28200
28201 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28202
28203 if (same_region)
28204 cursor = No_Cursor;
28205
28206 /* Check mouse-face highlighting. */
28207 if (! same_region
28208 /* If there exists an overlay with mouse-face overlapping
28209 the one we are currently highlighting, we have to
28210 check if we enter the overlapping overlay, and then
28211 highlight only that. */
28212 || (OVERLAYP (hlinfo->mouse_face_overlay)
28213 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28214 {
28215 /* Find the highest priority overlay with a mouse-face. */
28216 Lisp_Object overlay = Qnil;
28217 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28218 {
28219 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28220 if (!NILP (mouse_face))
28221 overlay = overlay_vec[i];
28222 }
28223
28224 /* If we're highlighting the same overlay as before, there's
28225 no need to do that again. */
28226 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28227 goto check_help_echo;
28228 hlinfo->mouse_face_overlay = overlay;
28229
28230 /* Clear the display of the old active region, if any. */
28231 if (clear_mouse_face (hlinfo))
28232 cursor = No_Cursor;
28233
28234 /* If no overlay applies, get a text property. */
28235 if (NILP (overlay))
28236 mouse_face = Fget_text_property (position, Qmouse_face, object);
28237
28238 /* Next, compute the bounds of the mouse highlighting and
28239 display it. */
28240 if (!NILP (mouse_face) && STRINGP (object))
28241 {
28242 /* The mouse-highlighting comes from a display string
28243 with a mouse-face. */
28244 Lisp_Object s, e;
28245 ptrdiff_t ignore;
28246
28247 s = Fprevious_single_property_change
28248 (make_number (pos + 1), Qmouse_face, object, Qnil);
28249 e = Fnext_single_property_change
28250 (position, Qmouse_face, object, Qnil);
28251 if (NILP (s))
28252 s = make_number (0);
28253 if (NILP (e))
28254 e = make_number (SCHARS (object) - 1);
28255 mouse_face_from_string_pos (w, hlinfo, object,
28256 XINT (s), XINT (e));
28257 hlinfo->mouse_face_past_end = 0;
28258 hlinfo->mouse_face_window = window;
28259 hlinfo->mouse_face_face_id
28260 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28261 glyph->face_id, 1);
28262 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28263 cursor = No_Cursor;
28264 }
28265 else
28266 {
28267 /* The mouse-highlighting, if any, comes from an overlay
28268 or text property in the buffer. */
28269 Lisp_Object buffer IF_LINT (= Qnil);
28270 Lisp_Object disp_string IF_LINT (= Qnil);
28271
28272 if (STRINGP (object))
28273 {
28274 /* If we are on a display string with no mouse-face,
28275 check if the text under it has one. */
28276 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28277 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28278 pos = string_buffer_position (object, start);
28279 if (pos > 0)
28280 {
28281 mouse_face = get_char_property_and_overlay
28282 (make_number (pos), Qmouse_face, w->contents, &overlay);
28283 buffer = w->contents;
28284 disp_string = object;
28285 }
28286 }
28287 else
28288 {
28289 buffer = object;
28290 disp_string = Qnil;
28291 }
28292
28293 if (!NILP (mouse_face))
28294 {
28295 Lisp_Object before, after;
28296 Lisp_Object before_string, after_string;
28297 /* To correctly find the limits of mouse highlight
28298 in a bidi-reordered buffer, we must not use the
28299 optimization of limiting the search in
28300 previous-single-property-change and
28301 next-single-property-change, because
28302 rows_from_pos_range needs the real start and end
28303 positions to DTRT in this case. That's because
28304 the first row visible in a window does not
28305 necessarily display the character whose position
28306 is the smallest. */
28307 Lisp_Object lim1 =
28308 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28309 ? Fmarker_position (w->start)
28310 : Qnil;
28311 Lisp_Object lim2 =
28312 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28313 ? make_number (BUF_Z (XBUFFER (buffer)) - w->window_end_pos)
28314 : Qnil;
28315
28316 if (NILP (overlay))
28317 {
28318 /* Handle the text property case. */
28319 before = Fprevious_single_property_change
28320 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28321 after = Fnext_single_property_change
28322 (make_number (pos), Qmouse_face, buffer, lim2);
28323 before_string = after_string = Qnil;
28324 }
28325 else
28326 {
28327 /* Handle the overlay case. */
28328 before = Foverlay_start (overlay);
28329 after = Foverlay_end (overlay);
28330 before_string = Foverlay_get (overlay, Qbefore_string);
28331 after_string = Foverlay_get (overlay, Qafter_string);
28332
28333 if (!STRINGP (before_string)) before_string = Qnil;
28334 if (!STRINGP (after_string)) after_string = Qnil;
28335 }
28336
28337 mouse_face_from_buffer_pos (window, hlinfo, pos,
28338 NILP (before)
28339 ? 1
28340 : XFASTINT (before),
28341 NILP (after)
28342 ? BUF_Z (XBUFFER (buffer))
28343 : XFASTINT (after),
28344 before_string, after_string,
28345 disp_string);
28346 cursor = No_Cursor;
28347 }
28348 }
28349 }
28350
28351 check_help_echo:
28352
28353 /* Look for a `help-echo' property. */
28354 if (NILP (help_echo_string)) {
28355 Lisp_Object help, overlay;
28356
28357 /* Check overlays first. */
28358 help = overlay = Qnil;
28359 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28360 {
28361 overlay = overlay_vec[i];
28362 help = Foverlay_get (overlay, Qhelp_echo);
28363 }
28364
28365 if (!NILP (help))
28366 {
28367 help_echo_string = help;
28368 help_echo_window = window;
28369 help_echo_object = overlay;
28370 help_echo_pos = pos;
28371 }
28372 else
28373 {
28374 Lisp_Object obj = glyph->object;
28375 ptrdiff_t charpos = glyph->charpos;
28376
28377 /* Try text properties. */
28378 if (STRINGP (obj)
28379 && charpos >= 0
28380 && charpos < SCHARS (obj))
28381 {
28382 help = Fget_text_property (make_number (charpos),
28383 Qhelp_echo, obj);
28384 if (NILP (help))
28385 {
28386 /* If the string itself doesn't specify a help-echo,
28387 see if the buffer text ``under'' it does. */
28388 struct glyph_row *r
28389 = MATRIX_ROW (w->current_matrix, vpos);
28390 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28391 ptrdiff_t p = string_buffer_position (obj, start);
28392 if (p > 0)
28393 {
28394 help = Fget_char_property (make_number (p),
28395 Qhelp_echo, w->contents);
28396 if (!NILP (help))
28397 {
28398 charpos = p;
28399 obj = w->contents;
28400 }
28401 }
28402 }
28403 }
28404 else if (BUFFERP (obj)
28405 && charpos >= BEGV
28406 && charpos < ZV)
28407 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28408 obj);
28409
28410 if (!NILP (help))
28411 {
28412 help_echo_string = help;
28413 help_echo_window = window;
28414 help_echo_object = obj;
28415 help_echo_pos = charpos;
28416 }
28417 }
28418 }
28419
28420 #ifdef HAVE_WINDOW_SYSTEM
28421 /* Look for a `pointer' property. */
28422 if (FRAME_WINDOW_P (f) && NILP (pointer))
28423 {
28424 /* Check overlays first. */
28425 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28426 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28427
28428 if (NILP (pointer))
28429 {
28430 Lisp_Object obj = glyph->object;
28431 ptrdiff_t charpos = glyph->charpos;
28432
28433 /* Try text properties. */
28434 if (STRINGP (obj)
28435 && charpos >= 0
28436 && charpos < SCHARS (obj))
28437 {
28438 pointer = Fget_text_property (make_number (charpos),
28439 Qpointer, obj);
28440 if (NILP (pointer))
28441 {
28442 /* If the string itself doesn't specify a pointer,
28443 see if the buffer text ``under'' it does. */
28444 struct glyph_row *r
28445 = MATRIX_ROW (w->current_matrix, vpos);
28446 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28447 ptrdiff_t p = string_buffer_position (obj, start);
28448 if (p > 0)
28449 pointer = Fget_char_property (make_number (p),
28450 Qpointer, w->contents);
28451 }
28452 }
28453 else if (BUFFERP (obj)
28454 && charpos >= BEGV
28455 && charpos < ZV)
28456 pointer = Fget_text_property (make_number (charpos),
28457 Qpointer, obj);
28458 }
28459 }
28460 #endif /* HAVE_WINDOW_SYSTEM */
28461
28462 BEGV = obegv;
28463 ZV = ozv;
28464 current_buffer = obuf;
28465 }
28466
28467 set_cursor:
28468
28469 #ifdef HAVE_WINDOW_SYSTEM
28470 if (FRAME_WINDOW_P (f))
28471 define_frame_cursor1 (f, cursor, pointer);
28472 #else
28473 /* This is here to prevent a compiler error, about "label at end of
28474 compound statement". */
28475 return;
28476 #endif
28477 }
28478
28479
28480 /* EXPORT for RIF:
28481 Clear any mouse-face on window W. This function is part of the
28482 redisplay interface, and is called from try_window_id and similar
28483 functions to ensure the mouse-highlight is off. */
28484
28485 void
28486 x_clear_window_mouse_face (struct window *w)
28487 {
28488 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28489 Lisp_Object window;
28490
28491 block_input ();
28492 XSETWINDOW (window, w);
28493 if (EQ (window, hlinfo->mouse_face_window))
28494 clear_mouse_face (hlinfo);
28495 unblock_input ();
28496 }
28497
28498
28499 /* EXPORT:
28500 Just discard the mouse face information for frame F, if any.
28501 This is used when the size of F is changed. */
28502
28503 void
28504 cancel_mouse_face (struct frame *f)
28505 {
28506 Lisp_Object window;
28507 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28508
28509 window = hlinfo->mouse_face_window;
28510 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28511 reset_mouse_highlight (hlinfo);
28512 }
28513
28514
28515 \f
28516 /***********************************************************************
28517 Exposure Events
28518 ***********************************************************************/
28519
28520 #ifdef HAVE_WINDOW_SYSTEM
28521
28522 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28523 which intersects rectangle R. R is in window-relative coordinates. */
28524
28525 static void
28526 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28527 enum glyph_row_area area)
28528 {
28529 struct glyph *first = row->glyphs[area];
28530 struct glyph *end = row->glyphs[area] + row->used[area];
28531 struct glyph *last;
28532 int first_x, start_x, x;
28533
28534 if (area == TEXT_AREA && row->fill_line_p)
28535 /* If row extends face to end of line write the whole line. */
28536 draw_glyphs (w, 0, row, area,
28537 0, row->used[area],
28538 DRAW_NORMAL_TEXT, 0);
28539 else
28540 {
28541 /* Set START_X to the window-relative start position for drawing glyphs of
28542 AREA. The first glyph of the text area can be partially visible.
28543 The first glyphs of other areas cannot. */
28544 start_x = window_box_left_offset (w, area);
28545 x = start_x;
28546 if (area == TEXT_AREA)
28547 x += row->x;
28548
28549 /* Find the first glyph that must be redrawn. */
28550 while (first < end
28551 && x + first->pixel_width < r->x)
28552 {
28553 x += first->pixel_width;
28554 ++first;
28555 }
28556
28557 /* Find the last one. */
28558 last = first;
28559 first_x = x;
28560 while (last < end
28561 && x < r->x + r->width)
28562 {
28563 x += last->pixel_width;
28564 ++last;
28565 }
28566
28567 /* Repaint. */
28568 if (last > first)
28569 draw_glyphs (w, first_x - start_x, row, area,
28570 first - row->glyphs[area], last - row->glyphs[area],
28571 DRAW_NORMAL_TEXT, 0);
28572 }
28573 }
28574
28575
28576 /* Redraw the parts of the glyph row ROW on window W intersecting
28577 rectangle R. R is in window-relative coordinates. Value is
28578 non-zero if mouse-face was overwritten. */
28579
28580 static int
28581 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28582 {
28583 eassert (row->enabled_p);
28584
28585 if (row->mode_line_p || w->pseudo_window_p)
28586 draw_glyphs (w, 0, row, TEXT_AREA,
28587 0, row->used[TEXT_AREA],
28588 DRAW_NORMAL_TEXT, 0);
28589 else
28590 {
28591 if (row->used[LEFT_MARGIN_AREA])
28592 expose_area (w, row, r, LEFT_MARGIN_AREA);
28593 if (row->used[TEXT_AREA])
28594 expose_area (w, row, r, TEXT_AREA);
28595 if (row->used[RIGHT_MARGIN_AREA])
28596 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28597 draw_row_fringe_bitmaps (w, row);
28598 }
28599
28600 return row->mouse_face_p;
28601 }
28602
28603
28604 /* Redraw those parts of glyphs rows during expose event handling that
28605 overlap other rows. Redrawing of an exposed line writes over parts
28606 of lines overlapping that exposed line; this function fixes that.
28607
28608 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28609 row in W's current matrix that is exposed and overlaps other rows.
28610 LAST_OVERLAPPING_ROW is the last such row. */
28611
28612 static void
28613 expose_overlaps (struct window *w,
28614 struct glyph_row *first_overlapping_row,
28615 struct glyph_row *last_overlapping_row,
28616 XRectangle *r)
28617 {
28618 struct glyph_row *row;
28619
28620 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28621 if (row->overlapping_p)
28622 {
28623 eassert (row->enabled_p && !row->mode_line_p);
28624
28625 row->clip = r;
28626 if (row->used[LEFT_MARGIN_AREA])
28627 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28628
28629 if (row->used[TEXT_AREA])
28630 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28631
28632 if (row->used[RIGHT_MARGIN_AREA])
28633 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28634 row->clip = NULL;
28635 }
28636 }
28637
28638
28639 /* Return non-zero if W's cursor intersects rectangle R. */
28640
28641 static int
28642 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28643 {
28644 XRectangle cr, result;
28645 struct glyph *cursor_glyph;
28646 struct glyph_row *row;
28647
28648 if (w->phys_cursor.vpos >= 0
28649 && w->phys_cursor.vpos < w->current_matrix->nrows
28650 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28651 row->enabled_p)
28652 && row->cursor_in_fringe_p)
28653 {
28654 /* Cursor is in the fringe. */
28655 cr.x = window_box_right_offset (w,
28656 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28657 ? RIGHT_MARGIN_AREA
28658 : TEXT_AREA));
28659 cr.y = row->y;
28660 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28661 cr.height = row->height;
28662 return x_intersect_rectangles (&cr, r, &result);
28663 }
28664
28665 cursor_glyph = get_phys_cursor_glyph (w);
28666 if (cursor_glyph)
28667 {
28668 /* r is relative to W's box, but w->phys_cursor.x is relative
28669 to left edge of W's TEXT area. Adjust it. */
28670 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28671 cr.y = w->phys_cursor.y;
28672 cr.width = cursor_glyph->pixel_width;
28673 cr.height = w->phys_cursor_height;
28674 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28675 I assume the effect is the same -- and this is portable. */
28676 return x_intersect_rectangles (&cr, r, &result);
28677 }
28678 /* If we don't understand the format, pretend we're not in the hot-spot. */
28679 return 0;
28680 }
28681
28682
28683 /* EXPORT:
28684 Draw a vertical window border to the right of window W if W doesn't
28685 have vertical scroll bars. */
28686
28687 void
28688 x_draw_vertical_border (struct window *w)
28689 {
28690 struct frame *f = XFRAME (WINDOW_FRAME (w));
28691
28692 /* We could do better, if we knew what type of scroll-bar the adjacent
28693 windows (on either side) have... But we don't :-(
28694 However, I think this works ok. ++KFS 2003-04-25 */
28695
28696 /* Redraw borders between horizontally adjacent windows. Don't
28697 do it for frames with vertical scroll bars because either the
28698 right scroll bar of a window, or the left scroll bar of its
28699 neighbor will suffice as a border. */
28700 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28701 return;
28702
28703 /* Note: It is necessary to redraw both the left and the right
28704 borders, for when only this single window W is being
28705 redisplayed. */
28706 if (!WINDOW_RIGHTMOST_P (w)
28707 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28708 {
28709 int x0, x1, y0, y1;
28710
28711 window_box_edges (w, &x0, &y0, &x1, &y1);
28712 y1 -= 1;
28713
28714 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28715 x1 -= 1;
28716
28717 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28718 }
28719 if (!WINDOW_LEFTMOST_P (w)
28720 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28721 {
28722 int x0, x1, y0, y1;
28723
28724 window_box_edges (w, &x0, &y0, &x1, &y1);
28725 y1 -= 1;
28726
28727 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28728 x0 -= 1;
28729
28730 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28731 }
28732 }
28733
28734
28735 /* Redraw the part of window W intersection rectangle FR. Pixel
28736 coordinates in FR are frame-relative. Call this function with
28737 input blocked. Value is non-zero if the exposure overwrites
28738 mouse-face. */
28739
28740 static int
28741 expose_window (struct window *w, XRectangle *fr)
28742 {
28743 struct frame *f = XFRAME (w->frame);
28744 XRectangle wr, r;
28745 int mouse_face_overwritten_p = 0;
28746
28747 /* If window is not yet fully initialized, do nothing. This can
28748 happen when toolkit scroll bars are used and a window is split.
28749 Reconfiguring the scroll bar will generate an expose for a newly
28750 created window. */
28751 if (w->current_matrix == NULL)
28752 return 0;
28753
28754 /* When we're currently updating the window, display and current
28755 matrix usually don't agree. Arrange for a thorough display
28756 later. */
28757 if (w->must_be_updated_p)
28758 {
28759 SET_FRAME_GARBAGED (f);
28760 return 0;
28761 }
28762
28763 /* Frame-relative pixel rectangle of W. */
28764 wr.x = WINDOW_LEFT_EDGE_X (w);
28765 wr.y = WINDOW_TOP_EDGE_Y (w);
28766 wr.width = WINDOW_TOTAL_WIDTH (w);
28767 wr.height = WINDOW_TOTAL_HEIGHT (w);
28768
28769 if (x_intersect_rectangles (fr, &wr, &r))
28770 {
28771 int yb = window_text_bottom_y (w);
28772 struct glyph_row *row;
28773 int cursor_cleared_p, phys_cursor_on_p;
28774 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28775
28776 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28777 r.x, r.y, r.width, r.height));
28778
28779 /* Convert to window coordinates. */
28780 r.x -= WINDOW_LEFT_EDGE_X (w);
28781 r.y -= WINDOW_TOP_EDGE_Y (w);
28782
28783 /* Turn off the cursor. */
28784 if (!w->pseudo_window_p
28785 && phys_cursor_in_rect_p (w, &r))
28786 {
28787 x_clear_cursor (w);
28788 cursor_cleared_p = 1;
28789 }
28790 else
28791 cursor_cleared_p = 0;
28792
28793 /* If the row containing the cursor extends face to end of line,
28794 then expose_area might overwrite the cursor outside the
28795 rectangle and thus notice_overwritten_cursor might clear
28796 w->phys_cursor_on_p. We remember the original value and
28797 check later if it is changed. */
28798 phys_cursor_on_p = w->phys_cursor_on_p;
28799
28800 /* Update lines intersecting rectangle R. */
28801 first_overlapping_row = last_overlapping_row = NULL;
28802 for (row = w->current_matrix->rows;
28803 row->enabled_p;
28804 ++row)
28805 {
28806 int y0 = row->y;
28807 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28808
28809 if ((y0 >= r.y && y0 < r.y + r.height)
28810 || (y1 > r.y && y1 < r.y + r.height)
28811 || (r.y >= y0 && r.y < y1)
28812 || (r.y + r.height > y0 && r.y + r.height < y1))
28813 {
28814 /* A header line may be overlapping, but there is no need
28815 to fix overlapping areas for them. KFS 2005-02-12 */
28816 if (row->overlapping_p && !row->mode_line_p)
28817 {
28818 if (first_overlapping_row == NULL)
28819 first_overlapping_row = row;
28820 last_overlapping_row = row;
28821 }
28822
28823 row->clip = fr;
28824 if (expose_line (w, row, &r))
28825 mouse_face_overwritten_p = 1;
28826 row->clip = NULL;
28827 }
28828 else if (row->overlapping_p)
28829 {
28830 /* We must redraw a row overlapping the exposed area. */
28831 if (y0 < r.y
28832 ? y0 + row->phys_height > r.y
28833 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28834 {
28835 if (first_overlapping_row == NULL)
28836 first_overlapping_row = row;
28837 last_overlapping_row = row;
28838 }
28839 }
28840
28841 if (y1 >= yb)
28842 break;
28843 }
28844
28845 /* Display the mode line if there is one. */
28846 if (WINDOW_WANTS_MODELINE_P (w)
28847 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28848 row->enabled_p)
28849 && row->y < r.y + r.height)
28850 {
28851 if (expose_line (w, row, &r))
28852 mouse_face_overwritten_p = 1;
28853 }
28854
28855 if (!w->pseudo_window_p)
28856 {
28857 /* Fix the display of overlapping rows. */
28858 if (first_overlapping_row)
28859 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28860 fr);
28861
28862 /* Draw border between windows. */
28863 x_draw_vertical_border (w);
28864
28865 /* Turn the cursor on again. */
28866 if (cursor_cleared_p
28867 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28868 update_window_cursor (w, 1);
28869 }
28870 }
28871
28872 return mouse_face_overwritten_p;
28873 }
28874
28875
28876
28877 /* Redraw (parts) of all windows in the window tree rooted at W that
28878 intersect R. R contains frame pixel coordinates. Value is
28879 non-zero if the exposure overwrites mouse-face. */
28880
28881 static int
28882 expose_window_tree (struct window *w, XRectangle *r)
28883 {
28884 struct frame *f = XFRAME (w->frame);
28885 int mouse_face_overwritten_p = 0;
28886
28887 while (w && !FRAME_GARBAGED_P (f))
28888 {
28889 if (WINDOWP (w->contents))
28890 mouse_face_overwritten_p
28891 |= expose_window_tree (XWINDOW (w->contents), r);
28892 else
28893 mouse_face_overwritten_p |= expose_window (w, r);
28894
28895 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28896 }
28897
28898 return mouse_face_overwritten_p;
28899 }
28900
28901
28902 /* EXPORT:
28903 Redisplay an exposed area of frame F. X and Y are the upper-left
28904 corner of the exposed rectangle. W and H are width and height of
28905 the exposed area. All are pixel values. W or H zero means redraw
28906 the entire frame. */
28907
28908 void
28909 expose_frame (struct frame *f, int x, int y, int w, int h)
28910 {
28911 XRectangle r;
28912 int mouse_face_overwritten_p = 0;
28913
28914 TRACE ((stderr, "expose_frame "));
28915
28916 /* No need to redraw if frame will be redrawn soon. */
28917 if (FRAME_GARBAGED_P (f))
28918 {
28919 TRACE ((stderr, " garbaged\n"));
28920 return;
28921 }
28922
28923 /* If basic faces haven't been realized yet, there is no point in
28924 trying to redraw anything. This can happen when we get an expose
28925 event while Emacs is starting, e.g. by moving another window. */
28926 if (FRAME_FACE_CACHE (f) == NULL
28927 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28928 {
28929 TRACE ((stderr, " no faces\n"));
28930 return;
28931 }
28932
28933 if (w == 0 || h == 0)
28934 {
28935 r.x = r.y = 0;
28936 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28937 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28938 }
28939 else
28940 {
28941 r.x = x;
28942 r.y = y;
28943 r.width = w;
28944 r.height = h;
28945 }
28946
28947 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28948 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28949
28950 if (WINDOWP (f->tool_bar_window))
28951 mouse_face_overwritten_p
28952 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28953
28954 #ifdef HAVE_X_WINDOWS
28955 #ifndef MSDOS
28956 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28957 if (WINDOWP (f->menu_bar_window))
28958 mouse_face_overwritten_p
28959 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28960 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28961 #endif
28962 #endif
28963
28964 /* Some window managers support a focus-follows-mouse style with
28965 delayed raising of frames. Imagine a partially obscured frame,
28966 and moving the mouse into partially obscured mouse-face on that
28967 frame. The visible part of the mouse-face will be highlighted,
28968 then the WM raises the obscured frame. With at least one WM, KDE
28969 2.1, Emacs is not getting any event for the raising of the frame
28970 (even tried with SubstructureRedirectMask), only Expose events.
28971 These expose events will draw text normally, i.e. not
28972 highlighted. Which means we must redo the highlight here.
28973 Subsume it under ``we love X''. --gerd 2001-08-15 */
28974 /* Included in Windows version because Windows most likely does not
28975 do the right thing if any third party tool offers
28976 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28977 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28978 {
28979 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28980 if (f == hlinfo->mouse_face_mouse_frame)
28981 {
28982 int mouse_x = hlinfo->mouse_face_mouse_x;
28983 int mouse_y = hlinfo->mouse_face_mouse_y;
28984 clear_mouse_face (hlinfo);
28985 note_mouse_highlight (f, mouse_x, mouse_y);
28986 }
28987 }
28988 }
28989
28990
28991 /* EXPORT:
28992 Determine the intersection of two rectangles R1 and R2. Return
28993 the intersection in *RESULT. Value is non-zero if RESULT is not
28994 empty. */
28995
28996 int
28997 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28998 {
28999 XRectangle *left, *right;
29000 XRectangle *upper, *lower;
29001 int intersection_p = 0;
29002
29003 /* Rearrange so that R1 is the left-most rectangle. */
29004 if (r1->x < r2->x)
29005 left = r1, right = r2;
29006 else
29007 left = r2, right = r1;
29008
29009 /* X0 of the intersection is right.x0, if this is inside R1,
29010 otherwise there is no intersection. */
29011 if (right->x <= left->x + left->width)
29012 {
29013 result->x = right->x;
29014
29015 /* The right end of the intersection is the minimum of
29016 the right ends of left and right. */
29017 result->width = (min (left->x + left->width, right->x + right->width)
29018 - result->x);
29019
29020 /* Same game for Y. */
29021 if (r1->y < r2->y)
29022 upper = r1, lower = r2;
29023 else
29024 upper = r2, lower = r1;
29025
29026 /* The upper end of the intersection is lower.y0, if this is inside
29027 of upper. Otherwise, there is no intersection. */
29028 if (lower->y <= upper->y + upper->height)
29029 {
29030 result->y = lower->y;
29031
29032 /* The lower end of the intersection is the minimum of the lower
29033 ends of upper and lower. */
29034 result->height = (min (lower->y + lower->height,
29035 upper->y + upper->height)
29036 - result->y);
29037 intersection_p = 1;
29038 }
29039 }
29040
29041 return intersection_p;
29042 }
29043
29044 #endif /* HAVE_WINDOW_SYSTEM */
29045
29046 \f
29047 /***********************************************************************
29048 Initialization
29049 ***********************************************************************/
29050
29051 void
29052 syms_of_xdisp (void)
29053 {
29054 Vwith_echo_area_save_vector = Qnil;
29055 staticpro (&Vwith_echo_area_save_vector);
29056
29057 Vmessage_stack = Qnil;
29058 staticpro (&Vmessage_stack);
29059
29060 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29061 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29062
29063 message_dolog_marker1 = Fmake_marker ();
29064 staticpro (&message_dolog_marker1);
29065 message_dolog_marker2 = Fmake_marker ();
29066 staticpro (&message_dolog_marker2);
29067 message_dolog_marker3 = Fmake_marker ();
29068 staticpro (&message_dolog_marker3);
29069
29070 #ifdef GLYPH_DEBUG
29071 defsubr (&Sdump_frame_glyph_matrix);
29072 defsubr (&Sdump_glyph_matrix);
29073 defsubr (&Sdump_glyph_row);
29074 defsubr (&Sdump_tool_bar_row);
29075 defsubr (&Strace_redisplay);
29076 defsubr (&Strace_to_stderr);
29077 #endif
29078 #ifdef HAVE_WINDOW_SYSTEM
29079 defsubr (&Stool_bar_lines_needed);
29080 defsubr (&Slookup_image_map);
29081 #endif
29082 defsubr (&Sline_pixel_height);
29083 defsubr (&Sformat_mode_line);
29084 defsubr (&Sinvisible_p);
29085 defsubr (&Scurrent_bidi_paragraph_direction);
29086 defsubr (&Smove_point_visually);
29087
29088 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29089 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29090 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29091 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29092 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29093 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29094 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29095 DEFSYM (Qeval, "eval");
29096 DEFSYM (QCdata, ":data");
29097 DEFSYM (Qdisplay, "display");
29098 DEFSYM (Qspace_width, "space-width");
29099 DEFSYM (Qraise, "raise");
29100 DEFSYM (Qslice, "slice");
29101 DEFSYM (Qspace, "space");
29102 DEFSYM (Qmargin, "margin");
29103 DEFSYM (Qpointer, "pointer");
29104 DEFSYM (Qleft_margin, "left-margin");
29105 DEFSYM (Qright_margin, "right-margin");
29106 DEFSYM (Qcenter, "center");
29107 DEFSYM (Qline_height, "line-height");
29108 DEFSYM (QCalign_to, ":align-to");
29109 DEFSYM (QCrelative_width, ":relative-width");
29110 DEFSYM (QCrelative_height, ":relative-height");
29111 DEFSYM (QCeval, ":eval");
29112 DEFSYM (QCpropertize, ":propertize");
29113 DEFSYM (QCfile, ":file");
29114 DEFSYM (Qfontified, "fontified");
29115 DEFSYM (Qfontification_functions, "fontification-functions");
29116 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29117 DEFSYM (Qescape_glyph, "escape-glyph");
29118 DEFSYM (Qnobreak_space, "nobreak-space");
29119 DEFSYM (Qimage, "image");
29120 DEFSYM (Qtext, "text");
29121 DEFSYM (Qboth, "both");
29122 DEFSYM (Qboth_horiz, "both-horiz");
29123 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29124 DEFSYM (QCmap, ":map");
29125 DEFSYM (QCpointer, ":pointer");
29126 DEFSYM (Qrect, "rect");
29127 DEFSYM (Qcircle, "circle");
29128 DEFSYM (Qpoly, "poly");
29129 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29130 DEFSYM (Qgrow_only, "grow-only");
29131 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29132 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29133 DEFSYM (Qposition, "position");
29134 DEFSYM (Qbuffer_position, "buffer-position");
29135 DEFSYM (Qobject, "object");
29136 DEFSYM (Qbar, "bar");
29137 DEFSYM (Qhbar, "hbar");
29138 DEFSYM (Qbox, "box");
29139 DEFSYM (Qhollow, "hollow");
29140 DEFSYM (Qhand, "hand");
29141 DEFSYM (Qarrow, "arrow");
29142 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29143
29144 list_of_error = list1 (list2 (intern_c_string ("error"),
29145 intern_c_string ("void-variable")));
29146 staticpro (&list_of_error);
29147
29148 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29149 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29150 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29151 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29152
29153 echo_buffer[0] = echo_buffer[1] = Qnil;
29154 staticpro (&echo_buffer[0]);
29155 staticpro (&echo_buffer[1]);
29156
29157 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29158 staticpro (&echo_area_buffer[0]);
29159 staticpro (&echo_area_buffer[1]);
29160
29161 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29162 staticpro (&Vmessages_buffer_name);
29163
29164 mode_line_proptrans_alist = Qnil;
29165 staticpro (&mode_line_proptrans_alist);
29166 mode_line_string_list = Qnil;
29167 staticpro (&mode_line_string_list);
29168 mode_line_string_face = Qnil;
29169 staticpro (&mode_line_string_face);
29170 mode_line_string_face_prop = Qnil;
29171 staticpro (&mode_line_string_face_prop);
29172 Vmode_line_unwind_vector = Qnil;
29173 staticpro (&Vmode_line_unwind_vector);
29174
29175 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29176
29177 help_echo_string = Qnil;
29178 staticpro (&help_echo_string);
29179 help_echo_object = Qnil;
29180 staticpro (&help_echo_object);
29181 help_echo_window = Qnil;
29182 staticpro (&help_echo_window);
29183 previous_help_echo_string = Qnil;
29184 staticpro (&previous_help_echo_string);
29185 help_echo_pos = -1;
29186
29187 DEFSYM (Qright_to_left, "right-to-left");
29188 DEFSYM (Qleft_to_right, "left-to-right");
29189
29190 #ifdef HAVE_WINDOW_SYSTEM
29191 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29192 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29193 For example, if a block cursor is over a tab, it will be drawn as
29194 wide as that tab on the display. */);
29195 x_stretch_cursor_p = 0;
29196 #endif
29197
29198 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29199 doc: /* Non-nil means highlight trailing whitespace.
29200 The face used for trailing whitespace is `trailing-whitespace'. */);
29201 Vshow_trailing_whitespace = Qnil;
29202
29203 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29204 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29205 If the value is t, Emacs highlights non-ASCII chars which have the
29206 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29207 or `escape-glyph' face respectively.
29208
29209 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29210 U+2011 (non-breaking hyphen) are affected.
29211
29212 Any other non-nil value means to display these characters as a escape
29213 glyph followed by an ordinary space or hyphen.
29214
29215 A value of nil means no special handling of these characters. */);
29216 Vnobreak_char_display = Qt;
29217
29218 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29219 doc: /* The pointer shape to show in void text areas.
29220 A value of nil means to show the text pointer. Other options are `arrow',
29221 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29222 Vvoid_text_area_pointer = Qarrow;
29223
29224 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29225 doc: /* Non-nil means don't actually do any redisplay.
29226 This is used for internal purposes. */);
29227 Vinhibit_redisplay = Qnil;
29228
29229 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29230 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29231 Vglobal_mode_string = Qnil;
29232
29233 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29234 doc: /* Marker for where to display an arrow on top of the buffer text.
29235 This must be the beginning of a line in order to work.
29236 See also `overlay-arrow-string'. */);
29237 Voverlay_arrow_position = Qnil;
29238
29239 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29240 doc: /* String to display as an arrow in non-window frames.
29241 See also `overlay-arrow-position'. */);
29242 Voverlay_arrow_string = build_pure_c_string ("=>");
29243
29244 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29245 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29246 The symbols on this list are examined during redisplay to determine
29247 where to display overlay arrows. */);
29248 Voverlay_arrow_variable_list
29249 = list1 (intern_c_string ("overlay-arrow-position"));
29250
29251 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29252 doc: /* The number of lines to try scrolling a window by when point moves out.
29253 If that fails to bring point back on frame, point is centered instead.
29254 If this is zero, point is always centered after it moves off frame.
29255 If you want scrolling to always be a line at a time, you should set
29256 `scroll-conservatively' to a large value rather than set this to 1. */);
29257
29258 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29259 doc: /* Scroll up to this many lines, to bring point back on screen.
29260 If point moves off-screen, redisplay will scroll by up to
29261 `scroll-conservatively' lines in order to bring point just barely
29262 onto the screen again. If that cannot be done, then redisplay
29263 recenters point as usual.
29264
29265 If the value is greater than 100, redisplay will never recenter point,
29266 but will always scroll just enough text to bring point into view, even
29267 if you move far away.
29268
29269 A value of zero means always recenter point if it moves off screen. */);
29270 scroll_conservatively = 0;
29271
29272 DEFVAR_INT ("scroll-margin", scroll_margin,
29273 doc: /* Number of lines of margin at the top and bottom of a window.
29274 Recenter the window whenever point gets within this many lines
29275 of the top or bottom of the window. */);
29276 scroll_margin = 0;
29277
29278 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29279 doc: /* Pixels per inch value for non-window system displays.
29280 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29281 Vdisplay_pixels_per_inch = make_float (72.0);
29282
29283 #ifdef GLYPH_DEBUG
29284 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29285 #endif
29286
29287 DEFVAR_LISP ("truncate-partial-width-windows",
29288 Vtruncate_partial_width_windows,
29289 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29290 For an integer value, truncate lines in each window narrower than the
29291 full frame width, provided the window width is less than that integer;
29292 otherwise, respect the value of `truncate-lines'.
29293
29294 For any other non-nil value, truncate lines in all windows that do
29295 not span the full frame width.
29296
29297 A value of nil means to respect the value of `truncate-lines'.
29298
29299 If `word-wrap' is enabled, you might want to reduce this. */);
29300 Vtruncate_partial_width_windows = make_number (50);
29301
29302 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29303 doc: /* Maximum buffer size for which line number should be displayed.
29304 If the buffer is bigger than this, the line number does not appear
29305 in the mode line. A value of nil means no limit. */);
29306 Vline_number_display_limit = Qnil;
29307
29308 DEFVAR_INT ("line-number-display-limit-width",
29309 line_number_display_limit_width,
29310 doc: /* Maximum line width (in characters) for line number display.
29311 If the average length of the lines near point is bigger than this, then the
29312 line number may be omitted from the mode line. */);
29313 line_number_display_limit_width = 200;
29314
29315 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29316 doc: /* Non-nil means highlight region even in nonselected windows. */);
29317 highlight_nonselected_windows = 0;
29318
29319 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29320 doc: /* Non-nil if more than one frame is visible on this display.
29321 Minibuffer-only frames don't count, but iconified frames do.
29322 This variable is not guaranteed to be accurate except while processing
29323 `frame-title-format' and `icon-title-format'. */);
29324
29325 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29326 doc: /* Template for displaying the title bar of visible frames.
29327 \(Assuming the window manager supports this feature.)
29328
29329 This variable has the same structure as `mode-line-format', except that
29330 the %c and %l constructs are ignored. It is used only on frames for
29331 which no explicit name has been set \(see `modify-frame-parameters'). */);
29332
29333 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29334 doc: /* Template for displaying the title bar of an iconified frame.
29335 \(Assuming the window manager supports this feature.)
29336 This variable has the same structure as `mode-line-format' (which see),
29337 and is used only on frames for which no explicit name has been set
29338 \(see `modify-frame-parameters'). */);
29339 Vicon_title_format
29340 = Vframe_title_format
29341 = listn (CONSTYPE_PURE, 3,
29342 intern_c_string ("multiple-frames"),
29343 build_pure_c_string ("%b"),
29344 listn (CONSTYPE_PURE, 4,
29345 empty_unibyte_string,
29346 intern_c_string ("invocation-name"),
29347 build_pure_c_string ("@"),
29348 intern_c_string ("system-name")));
29349
29350 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29351 doc: /* Maximum number of lines to keep in the message log buffer.
29352 If nil, disable message logging. If t, log messages but don't truncate
29353 the buffer when it becomes large. */);
29354 Vmessage_log_max = make_number (1000);
29355
29356 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29357 doc: /* Functions called before redisplay, if window sizes have changed.
29358 The value should be a list of functions that take one argument.
29359 Just before redisplay, for each frame, if any of its windows have changed
29360 size since the last redisplay, or have been split or deleted,
29361 all the functions in the list are called, with the frame as argument. */);
29362 Vwindow_size_change_functions = Qnil;
29363
29364 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29365 doc: /* List of functions to call before redisplaying a window with scrolling.
29366 Each function is called with two arguments, the window and its new
29367 display-start position. Note that these functions are also called by
29368 `set-window-buffer'. Also note that the value of `window-end' is not
29369 valid when these functions are called.
29370
29371 Warning: Do not use this feature to alter the way the window
29372 is scrolled. It is not designed for that, and such use probably won't
29373 work. */);
29374 Vwindow_scroll_functions = Qnil;
29375
29376 DEFVAR_LISP ("window-text-change-functions",
29377 Vwindow_text_change_functions,
29378 doc: /* Functions to call in redisplay when text in the window might change. */);
29379 Vwindow_text_change_functions = Qnil;
29380
29381 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29382 doc: /* Functions called when redisplay of a window reaches the end trigger.
29383 Each function is called with two arguments, the window and the end trigger value.
29384 See `set-window-redisplay-end-trigger'. */);
29385 Vredisplay_end_trigger_functions = Qnil;
29386
29387 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29388 doc: /* Non-nil means autoselect window with mouse pointer.
29389 If nil, do not autoselect windows.
29390 A positive number means delay autoselection by that many seconds: a
29391 window is autoselected only after the mouse has remained in that
29392 window for the duration of the delay.
29393 A negative number has a similar effect, but causes windows to be
29394 autoselected only after the mouse has stopped moving. \(Because of
29395 the way Emacs compares mouse events, you will occasionally wait twice
29396 that time before the window gets selected.\)
29397 Any other value means to autoselect window instantaneously when the
29398 mouse pointer enters it.
29399
29400 Autoselection selects the minibuffer only if it is active, and never
29401 unselects the minibuffer if it is active.
29402
29403 When customizing this variable make sure that the actual value of
29404 `focus-follows-mouse' matches the behavior of your window manager. */);
29405 Vmouse_autoselect_window = Qnil;
29406
29407 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29408 doc: /* Non-nil means automatically resize tool-bars.
29409 This dynamically changes the tool-bar's height to the minimum height
29410 that is needed to make all tool-bar items visible.
29411 If value is `grow-only', the tool-bar's height is only increased
29412 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29413 Vauto_resize_tool_bars = Qt;
29414
29415 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29416 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29417 auto_raise_tool_bar_buttons_p = 1;
29418
29419 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29420 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29421 make_cursor_line_fully_visible_p = 1;
29422
29423 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29424 doc: /* Border below tool-bar in pixels.
29425 If an integer, use it as the height of the border.
29426 If it is one of `internal-border-width' or `border-width', use the
29427 value of the corresponding frame parameter.
29428 Otherwise, no border is added below the tool-bar. */);
29429 Vtool_bar_border = Qinternal_border_width;
29430
29431 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29432 doc: /* Margin around tool-bar buttons in pixels.
29433 If an integer, use that for both horizontal and vertical margins.
29434 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29435 HORZ specifying the horizontal margin, and VERT specifying the
29436 vertical margin. */);
29437 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29438
29439 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29440 doc: /* Relief thickness of tool-bar buttons. */);
29441 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29442
29443 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29444 doc: /* Tool bar style to use.
29445 It can be one of
29446 image - show images only
29447 text - show text only
29448 both - show both, text below image
29449 both-horiz - show text to the right of the image
29450 text-image-horiz - show text to the left of the image
29451 any other - use system default or image if no system default.
29452
29453 This variable only affects the GTK+ toolkit version of Emacs. */);
29454 Vtool_bar_style = Qnil;
29455
29456 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29457 doc: /* Maximum number of characters a label can have to be shown.
29458 The tool bar style must also show labels for this to have any effect, see
29459 `tool-bar-style'. */);
29460 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29461
29462 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29463 doc: /* List of functions to call to fontify regions of text.
29464 Each function is called with one argument POS. Functions must
29465 fontify a region starting at POS in the current buffer, and give
29466 fontified regions the property `fontified'. */);
29467 Vfontification_functions = Qnil;
29468 Fmake_variable_buffer_local (Qfontification_functions);
29469
29470 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29471 unibyte_display_via_language_environment,
29472 doc: /* Non-nil means display unibyte text according to language environment.
29473 Specifically, this means that raw bytes in the range 160-255 decimal
29474 are displayed by converting them to the equivalent multibyte characters
29475 according to the current language environment. As a result, they are
29476 displayed according to the current fontset.
29477
29478 Note that this variable affects only how these bytes are displayed,
29479 but does not change the fact they are interpreted as raw bytes. */);
29480 unibyte_display_via_language_environment = 0;
29481
29482 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29483 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29484 If a float, it specifies a fraction of the mini-window frame's height.
29485 If an integer, it specifies a number of lines. */);
29486 Vmax_mini_window_height = make_float (0.25);
29487
29488 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29489 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29490 A value of nil means don't automatically resize mini-windows.
29491 A value of t means resize them to fit the text displayed in them.
29492 A value of `grow-only', the default, means let mini-windows grow only;
29493 they return to their normal size when the minibuffer is closed, or the
29494 echo area becomes empty. */);
29495 Vresize_mini_windows = Qgrow_only;
29496
29497 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29498 doc: /* Alist specifying how to blink the cursor off.
29499 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29500 `cursor-type' frame-parameter or variable equals ON-STATE,
29501 comparing using `equal', Emacs uses OFF-STATE to specify
29502 how to blink it off. ON-STATE and OFF-STATE are values for
29503 the `cursor-type' frame parameter.
29504
29505 If a frame's ON-STATE has no entry in this list,
29506 the frame's other specifications determine how to blink the cursor off. */);
29507 Vblink_cursor_alist = Qnil;
29508
29509 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29510 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29511 If non-nil, windows are automatically scrolled horizontally to make
29512 point visible. */);
29513 automatic_hscrolling_p = 1;
29514 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29515
29516 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29517 doc: /* How many columns away from the window edge point is allowed to get
29518 before automatic hscrolling will horizontally scroll the window. */);
29519 hscroll_margin = 5;
29520
29521 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29522 doc: /* How many columns to scroll the window when point gets too close to the edge.
29523 When point is less than `hscroll-margin' columns from the window
29524 edge, automatic hscrolling will scroll the window by the amount of columns
29525 determined by this variable. If its value is a positive integer, scroll that
29526 many columns. If it's a positive floating-point number, it specifies the
29527 fraction of the window's width to scroll. If it's nil or zero, point will be
29528 centered horizontally after the scroll. Any other value, including negative
29529 numbers, are treated as if the value were zero.
29530
29531 Automatic hscrolling always moves point outside the scroll margin, so if
29532 point was more than scroll step columns inside the margin, the window will
29533 scroll more than the value given by the scroll step.
29534
29535 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29536 and `scroll-right' overrides this variable's effect. */);
29537 Vhscroll_step = make_number (0);
29538
29539 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29540 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29541 Bind this around calls to `message' to let it take effect. */);
29542 message_truncate_lines = 0;
29543
29544 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29545 doc: /* Normal hook run to update the menu bar definitions.
29546 Redisplay runs this hook before it redisplays the menu bar.
29547 This is used to update submenus such as Buffers,
29548 whose contents depend on various data. */);
29549 Vmenu_bar_update_hook = Qnil;
29550
29551 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29552 doc: /* Frame for which we are updating a menu.
29553 The enable predicate for a menu binding should check this variable. */);
29554 Vmenu_updating_frame = Qnil;
29555
29556 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29557 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29558 inhibit_menubar_update = 0;
29559
29560 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29561 doc: /* Prefix prepended to all continuation lines at display time.
29562 The value may be a string, an image, or a stretch-glyph; it is
29563 interpreted in the same way as the value of a `display' text property.
29564
29565 This variable is overridden by any `wrap-prefix' text or overlay
29566 property.
29567
29568 To add a prefix to non-continuation lines, use `line-prefix'. */);
29569 Vwrap_prefix = Qnil;
29570 DEFSYM (Qwrap_prefix, "wrap-prefix");
29571 Fmake_variable_buffer_local (Qwrap_prefix);
29572
29573 DEFVAR_LISP ("line-prefix", Vline_prefix,
29574 doc: /* Prefix prepended to all non-continuation lines at display time.
29575 The value may be a string, an image, or a stretch-glyph; it is
29576 interpreted in the same way as the value of a `display' text property.
29577
29578 This variable is overridden by any `line-prefix' text or overlay
29579 property.
29580
29581 To add a prefix to continuation lines, use `wrap-prefix'. */);
29582 Vline_prefix = Qnil;
29583 DEFSYM (Qline_prefix, "line-prefix");
29584 Fmake_variable_buffer_local (Qline_prefix);
29585
29586 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29587 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29588 inhibit_eval_during_redisplay = 0;
29589
29590 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29591 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29592 inhibit_free_realized_faces = 0;
29593
29594 #ifdef GLYPH_DEBUG
29595 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29596 doc: /* Inhibit try_window_id display optimization. */);
29597 inhibit_try_window_id = 0;
29598
29599 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29600 doc: /* Inhibit try_window_reusing display optimization. */);
29601 inhibit_try_window_reusing = 0;
29602
29603 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29604 doc: /* Inhibit try_cursor_movement display optimization. */);
29605 inhibit_try_cursor_movement = 0;
29606 #endif /* GLYPH_DEBUG */
29607
29608 DEFVAR_INT ("overline-margin", overline_margin,
29609 doc: /* Space between overline and text, in pixels.
29610 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29611 margin to the character height. */);
29612 overline_margin = 2;
29613
29614 DEFVAR_INT ("underline-minimum-offset",
29615 underline_minimum_offset,
29616 doc: /* Minimum distance between baseline and underline.
29617 This can improve legibility of underlined text at small font sizes,
29618 particularly when using variable `x-use-underline-position-properties'
29619 with fonts that specify an UNDERLINE_POSITION relatively close to the
29620 baseline. The default value is 1. */);
29621 underline_minimum_offset = 1;
29622
29623 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29624 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29625 This feature only works when on a window system that can change
29626 cursor shapes. */);
29627 display_hourglass_p = 1;
29628
29629 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29630 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29631 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29632
29633 #ifdef HAVE_WINDOW_SYSTEM
29634 hourglass_atimer = NULL;
29635 hourglass_shown_p = 0;
29636 #endif /* HAVE_WINDOW_SYSTEM */
29637
29638 DEFSYM (Qglyphless_char, "glyphless-char");
29639 DEFSYM (Qhex_code, "hex-code");
29640 DEFSYM (Qempty_box, "empty-box");
29641 DEFSYM (Qthin_space, "thin-space");
29642 DEFSYM (Qzero_width, "zero-width");
29643
29644 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29645 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29646
29647 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29648 doc: /* Char-table defining glyphless characters.
29649 Each element, if non-nil, should be one of the following:
29650 an ASCII acronym string: display this string in a box
29651 `hex-code': display the hexadecimal code of a character in a box
29652 `empty-box': display as an empty box
29653 `thin-space': display as 1-pixel width space
29654 `zero-width': don't display
29655 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29656 display method for graphical terminals and text terminals respectively.
29657 GRAPHICAL and TEXT should each have one of the values listed above.
29658
29659 The char-table has one extra slot to control the display of a character for
29660 which no font is found. This slot only takes effect on graphical terminals.
29661 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29662 `thin-space'. The default is `empty-box'. */);
29663 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29664 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29665 Qempty_box);
29666
29667 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29668 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29669 Vdebug_on_message = Qnil;
29670 }
29671
29672
29673 /* Initialize this module when Emacs starts. */
29674
29675 void
29676 init_xdisp (void)
29677 {
29678 CHARPOS (this_line_start_pos) = 0;
29679
29680 if (!noninteractive)
29681 {
29682 struct window *m = XWINDOW (minibuf_window);
29683 Lisp_Object frame = m->frame;
29684 struct frame *f = XFRAME (frame);
29685 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29686 struct window *r = XWINDOW (root);
29687 int i;
29688
29689 echo_area_window = minibuf_window;
29690
29691 r->top_line = FRAME_TOP_MARGIN (f);
29692 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29693 r->total_cols = FRAME_COLS (f);
29694
29695 m->top_line = FRAME_LINES (f) - 1;
29696 m->total_lines = 1;
29697 m->total_cols = FRAME_COLS (f);
29698
29699 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29700 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29701 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29702
29703 /* The default ellipsis glyphs `...'. */
29704 for (i = 0; i < 3; ++i)
29705 default_invis_vector[i] = make_number ('.');
29706 }
29707
29708 {
29709 /* Allocate the buffer for frame titles.
29710 Also used for `format-mode-line'. */
29711 int size = 100;
29712 mode_line_noprop_buf = xmalloc (size);
29713 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29714 mode_line_noprop_ptr = mode_line_noprop_buf;
29715 mode_line_target = MODE_LINE_DISPLAY;
29716 }
29717
29718 help_echo_showing_p = 0;
29719 }
29720
29721 #ifdef HAVE_WINDOW_SYSTEM
29722
29723 /* Platform-independent portion of hourglass implementation. */
29724
29725 /* Cancel a currently active hourglass timer, and start a new one. */
29726 void
29727 start_hourglass (void)
29728 {
29729 struct timespec delay;
29730
29731 cancel_hourglass ();
29732
29733 if (INTEGERP (Vhourglass_delay)
29734 && XINT (Vhourglass_delay) > 0)
29735 delay = make_timespec (min (XINT (Vhourglass_delay),
29736 TYPE_MAXIMUM (time_t)),
29737 0);
29738 else if (FLOATP (Vhourglass_delay)
29739 && XFLOAT_DATA (Vhourglass_delay) > 0)
29740 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
29741 else
29742 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
29743
29744 #ifdef HAVE_NTGUI
29745 {
29746 extern void w32_note_current_window (void);
29747 w32_note_current_window ();
29748 }
29749 #endif /* HAVE_NTGUI */
29750
29751 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29752 show_hourglass, NULL);
29753 }
29754
29755
29756 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29757 shown. */
29758 void
29759 cancel_hourglass (void)
29760 {
29761 if (hourglass_atimer)
29762 {
29763 cancel_atimer (hourglass_atimer);
29764 hourglass_atimer = NULL;
29765 }
29766
29767 if (hourglass_shown_p)
29768 hide_hourglass ();
29769 }
29770
29771 #endif /* HAVE_WINDOW_SYSTEM */