Remove some unused items introduced during pixelwise change.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276
277 #include "lisp.h"
278 #include "atimer.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301 #ifdef HAVE_WINDOW_SYSTEM
302 #include TERM_HEADER
303 #endif /* HAVE_WINDOW_SYSTEM */
304
305 #ifndef FRAME_X_OUTPUT
306 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
307 #endif
308
309 #define INFINITY 10000000
310
311 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
312 Lisp_Object Qwindow_scroll_functions;
313 static Lisp_Object Qwindow_text_change_functions;
314 static Lisp_Object Qredisplay_end_trigger_functions;
315 Lisp_Object Qinhibit_point_motion_hooks;
316 static Lisp_Object QCeval, QCpropertize;
317 Lisp_Object QCfile, QCdata;
318 static Lisp_Object Qfontified;
319 static Lisp_Object Qgrow_only;
320 static Lisp_Object Qinhibit_eval_during_redisplay;
321 static Lisp_Object Qbuffer_position, Qposition, Qobject;
322 static Lisp_Object Qright_to_left, Qleft_to_right;
323
324 /* Cursor shapes. */
325 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
326
327 /* Pointer shapes. */
328 static Lisp_Object Qarrow, Qhand;
329 Lisp_Object Qtext;
330
331 /* Holds the list (error). */
332 static Lisp_Object list_of_error;
333
334 static Lisp_Object Qfontification_functions;
335
336 static Lisp_Object Qwrap_prefix;
337 static Lisp_Object Qline_prefix;
338 static Lisp_Object Qredisplay_internal;
339
340 /* Non-nil means don't actually do any redisplay. */
341
342 Lisp_Object Qinhibit_redisplay;
343
344 /* Names of text properties relevant for redisplay. */
345
346 Lisp_Object Qdisplay;
347
348 Lisp_Object Qspace, QCalign_to;
349 static Lisp_Object QCrelative_width, QCrelative_height;
350 Lisp_Object Qleft_margin, Qright_margin;
351 static Lisp_Object Qspace_width, Qraise;
352 static Lisp_Object Qslice;
353 Lisp_Object Qcenter;
354 static Lisp_Object Qmargin, Qpointer;
355 static Lisp_Object Qline_height;
356
357 #ifdef HAVE_WINDOW_SYSTEM
358
359 /* Test if overflow newline into fringe. Called with iterator IT
360 at or past right window margin, and with IT->current_x set. */
361
362 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
363 (!NILP (Voverflow_newline_into_fringe) \
364 && FRAME_WINDOW_P ((IT)->f) \
365 && ((IT)->bidi_it.paragraph_dir == R2L \
366 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
367 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
368 && (IT)->current_x == (IT)->last_visible_x)
369
370 #else /* !HAVE_WINDOW_SYSTEM */
371 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
372 #endif /* HAVE_WINDOW_SYSTEM */
373
374 /* Test if the display element loaded in IT, or the underlying buffer
375 or string character, is a space or a TAB character. This is used
376 to determine where word wrapping can occur. */
377
378 #define IT_DISPLAYING_WHITESPACE(it) \
379 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
380 || ((STRINGP (it->string) \
381 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
382 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
383 || (it->s \
384 && (it->s[IT_BYTEPOS (*it)] == ' ' \
385 || it->s[IT_BYTEPOS (*it)] == '\t')) \
386 || (IT_BYTEPOS (*it) < ZV_BYTE \
387 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
388 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
389
390 /* Name of the face used to highlight trailing whitespace. */
391
392 static Lisp_Object Qtrailing_whitespace;
393
394 /* Name and number of the face used to highlight escape glyphs. */
395
396 static Lisp_Object Qescape_glyph;
397
398 /* Name and number of the face used to highlight non-breaking spaces. */
399
400 static Lisp_Object Qnobreak_space;
401
402 /* The symbol `image' which is the car of the lists used to represent
403 images in Lisp. Also a tool bar style. */
404
405 Lisp_Object Qimage;
406
407 /* The image map types. */
408 Lisp_Object QCmap;
409 static Lisp_Object QCpointer;
410 static Lisp_Object Qrect, Qcircle, Qpoly;
411
412 /* Tool bar styles */
413 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
414
415 /* Non-zero means print newline to stdout before next mini-buffer
416 message. */
417
418 bool noninteractive_need_newline;
419
420 /* Non-zero means print newline to message log before next message. */
421
422 static bool message_log_need_newline;
423
424 /* Three markers that message_dolog uses.
425 It could allocate them itself, but that causes trouble
426 in handling memory-full errors. */
427 static Lisp_Object message_dolog_marker1;
428 static Lisp_Object message_dolog_marker2;
429 static Lisp_Object message_dolog_marker3;
430 \f
431 /* The buffer position of the first character appearing entirely or
432 partially on the line of the selected window which contains the
433 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
434 redisplay optimization in redisplay_internal. */
435
436 static struct text_pos this_line_start_pos;
437
438 /* Number of characters past the end of the line above, including the
439 terminating newline. */
440
441 static struct text_pos this_line_end_pos;
442
443 /* The vertical positions and the height of this line. */
444
445 static int this_line_vpos;
446 static int this_line_y;
447 static int this_line_pixel_height;
448
449 /* X position at which this display line starts. Usually zero;
450 negative if first character is partially visible. */
451
452 static int this_line_start_x;
453
454 /* The smallest character position seen by move_it_* functions as they
455 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
456 hscrolled lines, see display_line. */
457
458 static struct text_pos this_line_min_pos;
459
460 /* Buffer that this_line_.* variables are referring to. */
461
462 static struct buffer *this_line_buffer;
463
464
465 /* Values of those variables at last redisplay are stored as
466 properties on `overlay-arrow-position' symbol. However, if
467 Voverlay_arrow_position is a marker, last-arrow-position is its
468 numerical position. */
469
470 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
471
472 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
473 properties on a symbol in overlay-arrow-variable-list. */
474
475 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
476
477 Lisp_Object Qmenu_bar_update_hook;
478
479 /* Nonzero if an overlay arrow has been displayed in this window. */
480
481 static bool overlay_arrow_seen;
482
483 /* Vector containing glyphs for an ellipsis `...'. */
484
485 static Lisp_Object default_invis_vector[3];
486
487 /* This is the window where the echo area message was displayed. It
488 is always a mini-buffer window, but it may not be the same window
489 currently active as a mini-buffer. */
490
491 Lisp_Object echo_area_window;
492
493 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
494 pushes the current message and the value of
495 message_enable_multibyte on the stack, the function restore_message
496 pops the stack and displays MESSAGE again. */
497
498 static Lisp_Object Vmessage_stack;
499
500 /* Nonzero means multibyte characters were enabled when the echo area
501 message was specified. */
502
503 static bool message_enable_multibyte;
504
505 /* Nonzero if we should redraw the mode lines on the next redisplay.
506 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
507 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
508 (the number used is then only used to track down the cause for this
509 full-redisplay). */
510
511 int update_mode_lines;
512
513 /* Nonzero if window sizes or contents other than selected-window have changed
514 since last redisplay that finished.
515 If it has value REDISPLAY_SOME, then only redisplay the windows where
516 the `redisplay' bit has been set. Otherwise, redisplay all windows
517 (the number used is then only used to track down the cause for this
518 full-redisplay). */
519
520 int windows_or_buffers_changed;
521
522 /* Nonzero after display_mode_line if %l was used and it displayed a
523 line number. */
524
525 static bool line_number_displayed;
526
527 /* The name of the *Messages* buffer, a string. */
528
529 static Lisp_Object Vmessages_buffer_name;
530
531 /* Current, index 0, and last displayed echo area message. Either
532 buffers from echo_buffers, or nil to indicate no message. */
533
534 Lisp_Object echo_area_buffer[2];
535
536 /* The buffers referenced from echo_area_buffer. */
537
538 static Lisp_Object echo_buffer[2];
539
540 /* A vector saved used in with_area_buffer to reduce consing. */
541
542 static Lisp_Object Vwith_echo_area_save_vector;
543
544 /* Non-zero means display_echo_area should display the last echo area
545 message again. Set by redisplay_preserve_echo_area. */
546
547 static bool display_last_displayed_message_p;
548
549 /* Nonzero if echo area is being used by print; zero if being used by
550 message. */
551
552 static bool message_buf_print;
553
554 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
555
556 static Lisp_Object Qinhibit_menubar_update;
557 static Lisp_Object Qmessage_truncate_lines;
558
559 /* Set to 1 in clear_message to make redisplay_internal aware
560 of an emptied echo area. */
561
562 static bool message_cleared_p;
563
564 /* A scratch glyph row with contents used for generating truncation
565 glyphs. Also used in direct_output_for_insert. */
566
567 #define MAX_SCRATCH_GLYPHS 100
568 static struct glyph_row scratch_glyph_row;
569 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
570
571 /* Ascent and height of the last line processed by move_it_to. */
572
573 static int last_max_ascent, last_height;
574
575 /* Non-zero if there's a help-echo in the echo area. */
576
577 bool help_echo_showing_p;
578
579 /* The maximum distance to look ahead for text properties. Values
580 that are too small let us call compute_char_face and similar
581 functions too often which is expensive. Values that are too large
582 let us call compute_char_face and alike too often because we
583 might not be interested in text properties that far away. */
584
585 #define TEXT_PROP_DISTANCE_LIMIT 100
586
587 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
588 iterator state and later restore it. This is needed because the
589 bidi iterator on bidi.c keeps a stacked cache of its states, which
590 is really a singleton. When we use scratch iterator objects to
591 move around the buffer, we can cause the bidi cache to be pushed or
592 popped, and therefore we need to restore the cache state when we
593 return to the original iterator. */
594 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
595 do { \
596 if (CACHE) \
597 bidi_unshelve_cache (CACHE, 1); \
598 ITCOPY = ITORIG; \
599 CACHE = bidi_shelve_cache (); \
600 } while (0)
601
602 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
603 do { \
604 if (pITORIG != pITCOPY) \
605 *(pITORIG) = *(pITCOPY); \
606 bidi_unshelve_cache (CACHE, 0); \
607 CACHE = NULL; \
608 } while (0)
609
610 /* Functions to mark elements as needing redisplay. */
611 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
612
613 void
614 redisplay_other_windows (void)
615 {
616 if (!windows_or_buffers_changed)
617 windows_or_buffers_changed = REDISPLAY_SOME;
618 }
619
620 void
621 wset_redisplay (struct window *w)
622 {
623 redisplay_other_windows ();
624 w->redisplay = true;
625 }
626
627 void
628 fset_redisplay (struct frame *f)
629 {
630 redisplay_other_windows ();
631 f->redisplay = true;
632 }
633
634 void
635 bset_redisplay (struct buffer *b)
636 {
637 int count = buffer_window_count (b);
638 if (count > 0)
639 {
640 /* ... it's visible in other window than selected, */
641 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
642 redisplay_other_windows ();
643 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
644 so that if we later set windows_or_buffers_changed, this buffer will
645 not be omitted. */
646 b->text->redisplay = true;
647 }
648 }
649
650 void
651 bset_update_mode_line (struct buffer *b)
652 {
653 if (!update_mode_lines)
654 update_mode_lines = REDISPLAY_SOME;
655 b->text->redisplay = true;
656 }
657
658 #ifdef GLYPH_DEBUG
659
660 /* Non-zero means print traces of redisplay if compiled with
661 GLYPH_DEBUG defined. */
662
663 int trace_redisplay_p;
664
665 #endif /* GLYPH_DEBUG */
666
667 #ifdef DEBUG_TRACE_MOVE
668 /* Non-zero means trace with TRACE_MOVE to stderr. */
669 int trace_move;
670
671 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
672 #else
673 #define TRACE_MOVE(x) (void) 0
674 #endif
675
676 static Lisp_Object Qauto_hscroll_mode;
677
678 /* Buffer being redisplayed -- for redisplay_window_error. */
679
680 static struct buffer *displayed_buffer;
681
682 /* Value returned from text property handlers (see below). */
683
684 enum prop_handled
685 {
686 HANDLED_NORMALLY,
687 HANDLED_RECOMPUTE_PROPS,
688 HANDLED_OVERLAY_STRING_CONSUMED,
689 HANDLED_RETURN
690 };
691
692 /* A description of text properties that redisplay is interested
693 in. */
694
695 struct props
696 {
697 /* The name of the property. */
698 Lisp_Object *name;
699
700 /* A unique index for the property. */
701 enum prop_idx idx;
702
703 /* A handler function called to set up iterator IT from the property
704 at IT's current position. Value is used to steer handle_stop. */
705 enum prop_handled (*handler) (struct it *it);
706 };
707
708 static enum prop_handled handle_face_prop (struct it *);
709 static enum prop_handled handle_invisible_prop (struct it *);
710 static enum prop_handled handle_display_prop (struct it *);
711 static enum prop_handled handle_composition_prop (struct it *);
712 static enum prop_handled handle_overlay_change (struct it *);
713 static enum prop_handled handle_fontified_prop (struct it *);
714
715 /* Properties handled by iterators. */
716
717 static struct props it_props[] =
718 {
719 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
720 /* Handle `face' before `display' because some sub-properties of
721 `display' need to know the face. */
722 {&Qface, FACE_PROP_IDX, handle_face_prop},
723 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
724 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
725 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
726 {NULL, 0, NULL}
727 };
728
729 /* Value is the position described by X. If X is a marker, value is
730 the marker_position of X. Otherwise, value is X. */
731
732 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
733
734 /* Enumeration returned by some move_it_.* functions internally. */
735
736 enum move_it_result
737 {
738 /* Not used. Undefined value. */
739 MOVE_UNDEFINED,
740
741 /* Move ended at the requested buffer position or ZV. */
742 MOVE_POS_MATCH_OR_ZV,
743
744 /* Move ended at the requested X pixel position. */
745 MOVE_X_REACHED,
746
747 /* Move within a line ended at the end of a line that must be
748 continued. */
749 MOVE_LINE_CONTINUED,
750
751 /* Move within a line ended at the end of a line that would
752 be displayed truncated. */
753 MOVE_LINE_TRUNCATED,
754
755 /* Move within a line ended at a line end. */
756 MOVE_NEWLINE_OR_CR
757 };
758
759 /* This counter is used to clear the face cache every once in a while
760 in redisplay_internal. It is incremented for each redisplay.
761 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
762 cleared. */
763
764 #define CLEAR_FACE_CACHE_COUNT 500
765 static int clear_face_cache_count;
766
767 /* Similarly for the image cache. */
768
769 #ifdef HAVE_WINDOW_SYSTEM
770 #define CLEAR_IMAGE_CACHE_COUNT 101
771 static int clear_image_cache_count;
772
773 /* Null glyph slice */
774 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
775 #endif
776
777 /* True while redisplay_internal is in progress. */
778
779 bool redisplaying_p;
780
781 static Lisp_Object Qinhibit_free_realized_faces;
782 static Lisp_Object Qmode_line_default_help_echo;
783
784 /* If a string, XTread_socket generates an event to display that string.
785 (The display is done in read_char.) */
786
787 Lisp_Object help_echo_string;
788 Lisp_Object help_echo_window;
789 Lisp_Object help_echo_object;
790 ptrdiff_t help_echo_pos;
791
792 /* Temporary variable for XTread_socket. */
793
794 Lisp_Object previous_help_echo_string;
795
796 /* Platform-independent portion of hourglass implementation. */
797
798 #ifdef HAVE_WINDOW_SYSTEM
799
800 /* Non-zero means an hourglass cursor is currently shown. */
801 bool hourglass_shown_p;
802
803 /* If non-null, an asynchronous timer that, when it expires, displays
804 an hourglass cursor on all frames. */
805 struct atimer *hourglass_atimer;
806
807 #endif /* HAVE_WINDOW_SYSTEM */
808
809 /* Name of the face used to display glyphless characters. */
810 static Lisp_Object Qglyphless_char;
811
812 /* Symbol for the purpose of Vglyphless_char_display. */
813 static Lisp_Object Qglyphless_char_display;
814
815 /* Method symbols for Vglyphless_char_display. */
816 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
817
818 /* Default number of seconds to wait before displaying an hourglass
819 cursor. */
820 #define DEFAULT_HOURGLASS_DELAY 1
821
822 #ifdef HAVE_WINDOW_SYSTEM
823
824 /* Default pixel width of `thin-space' display method. */
825 #define THIN_SPACE_WIDTH 1
826
827 #endif /* HAVE_WINDOW_SYSTEM */
828
829 /* Function prototypes. */
830
831 static void setup_for_ellipsis (struct it *, int);
832 static void set_iterator_to_next (struct it *, int);
833 static void mark_window_display_accurate_1 (struct window *, int);
834 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
835 static int display_prop_string_p (Lisp_Object, Lisp_Object);
836 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
837 static int cursor_row_p (struct glyph_row *);
838 static int redisplay_mode_lines (Lisp_Object, bool);
839 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
840
841 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
842
843 static void handle_line_prefix (struct it *);
844
845 static void pint2str (char *, int, ptrdiff_t);
846 static void pint2hrstr (char *, int, ptrdiff_t);
847 static struct text_pos run_window_scroll_functions (Lisp_Object,
848 struct text_pos);
849 static int text_outside_line_unchanged_p (struct window *,
850 ptrdiff_t, ptrdiff_t);
851 static void store_mode_line_noprop_char (char);
852 static int store_mode_line_noprop (const char *, int, int);
853 static void handle_stop (struct it *);
854 static void handle_stop_backwards (struct it *, ptrdiff_t);
855 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
856 static void ensure_echo_area_buffers (void);
857 static void unwind_with_echo_area_buffer (Lisp_Object);
858 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
859 static int with_echo_area_buffer (struct window *, int,
860 int (*) (ptrdiff_t, Lisp_Object),
861 ptrdiff_t, Lisp_Object);
862 static void clear_garbaged_frames (void);
863 static int current_message_1 (ptrdiff_t, Lisp_Object);
864 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
865 static void set_message (Lisp_Object);
866 static int set_message_1 (ptrdiff_t, Lisp_Object);
867 static int display_echo_area (struct window *);
868 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
869 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
870 static void unwind_redisplay (void);
871 static int string_char_and_length (const unsigned char *, int *);
872 static struct text_pos display_prop_end (struct it *, Lisp_Object,
873 struct text_pos);
874 static int compute_window_start_on_continuation_line (struct window *);
875 static void insert_left_trunc_glyphs (struct it *);
876 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
877 Lisp_Object);
878 static void extend_face_to_end_of_line (struct it *);
879 static int append_space_for_newline (struct it *, int);
880 static int cursor_row_fully_visible_p (struct window *, int, int);
881 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
882 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
883 static int trailing_whitespace_p (ptrdiff_t);
884 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
885 static void push_it (struct it *, struct text_pos *);
886 static void iterate_out_of_display_property (struct it *);
887 static void pop_it (struct it *);
888 static void sync_frame_with_window_matrix_rows (struct window *);
889 static void redisplay_internal (void);
890 static int echo_area_display (int);
891 static void redisplay_windows (Lisp_Object);
892 static void redisplay_window (Lisp_Object, bool);
893 static Lisp_Object redisplay_window_error (Lisp_Object);
894 static Lisp_Object redisplay_window_0 (Lisp_Object);
895 static Lisp_Object redisplay_window_1 (Lisp_Object);
896 static int set_cursor_from_row (struct window *, struct glyph_row *,
897 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
898 int, int);
899 static int update_menu_bar (struct frame *, int, int);
900 static int try_window_reusing_current_matrix (struct window *);
901 static int try_window_id (struct window *);
902 static int display_line (struct it *);
903 static int display_mode_lines (struct window *);
904 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
905 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
906 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
907 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
908 static void display_menu_bar (struct window *);
909 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
910 ptrdiff_t *);
911 static int display_string (const char *, Lisp_Object, Lisp_Object,
912 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
913 static void compute_line_metrics (struct it *);
914 static void run_redisplay_end_trigger_hook (struct it *);
915 static int get_overlay_strings (struct it *, ptrdiff_t);
916 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
917 static void next_overlay_string (struct it *);
918 static void reseat (struct it *, struct text_pos, int);
919 static void reseat_1 (struct it *, struct text_pos, int);
920 static void back_to_previous_visible_line_start (struct it *);
921 static void reseat_at_next_visible_line_start (struct it *, int);
922 static int next_element_from_ellipsis (struct it *);
923 static int next_element_from_display_vector (struct it *);
924 static int next_element_from_string (struct it *);
925 static int next_element_from_c_string (struct it *);
926 static int next_element_from_buffer (struct it *);
927 static int next_element_from_composition (struct it *);
928 static int next_element_from_image (struct it *);
929 static int next_element_from_stretch (struct it *);
930 static void load_overlay_strings (struct it *, ptrdiff_t);
931 static int init_from_display_pos (struct it *, struct window *,
932 struct display_pos *);
933 static void reseat_to_string (struct it *, const char *,
934 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
935 static int get_next_display_element (struct it *);
936 static enum move_it_result
937 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
938 enum move_operation_enum);
939 static void get_visually_first_element (struct it *);
940 static void init_to_row_start (struct it *, struct window *,
941 struct glyph_row *);
942 static int init_to_row_end (struct it *, struct window *,
943 struct glyph_row *);
944 static void back_to_previous_line_start (struct it *);
945 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
946 static struct text_pos string_pos_nchars_ahead (struct text_pos,
947 Lisp_Object, ptrdiff_t);
948 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
949 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
950 static ptrdiff_t number_of_chars (const char *, bool);
951 static void compute_stop_pos (struct it *);
952 static void compute_string_pos (struct text_pos *, struct text_pos,
953 Lisp_Object);
954 static int face_before_or_after_it_pos (struct it *, int);
955 static ptrdiff_t next_overlay_change (ptrdiff_t);
956 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
957 Lisp_Object, struct text_pos *, ptrdiff_t, int);
958 static int handle_single_display_spec (struct it *, Lisp_Object,
959 Lisp_Object, Lisp_Object,
960 struct text_pos *, ptrdiff_t, int, int);
961 static int underlying_face_id (struct it *);
962 static int in_ellipses_for_invisible_text_p (struct display_pos *,
963 struct window *);
964
965 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
966 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
967
968 #ifdef HAVE_WINDOW_SYSTEM
969
970 static void x_consider_frame_title (Lisp_Object);
971 static void update_tool_bar (struct frame *, int);
972 static int redisplay_tool_bar (struct frame *);
973 static void notice_overwritten_cursor (struct window *,
974 enum glyph_row_area,
975 int, int, int, int);
976 static void append_stretch_glyph (struct it *, Lisp_Object,
977 int, int, int);
978
979
980 #endif /* HAVE_WINDOW_SYSTEM */
981
982 static void produce_special_glyphs (struct it *, enum display_element_type);
983 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
984 static int coords_in_mouse_face_p (struct window *, int, int);
985
986
987 \f
988 /***********************************************************************
989 Window display dimensions
990 ***********************************************************************/
991
992 /* Return the bottom boundary y-position for text lines in window W.
993 This is the first y position at which a line cannot start.
994 It is relative to the top of the window.
995
996 This is the height of W minus the height of a mode line, if any. */
997
998 int
999 window_text_bottom_y (struct window *w)
1000 {
1001 int height = WINDOW_PIXEL_HEIGHT (w);
1002
1003 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1004
1005 if (WINDOW_WANTS_MODELINE_P (w))
1006 height -= CURRENT_MODE_LINE_HEIGHT (w);
1007
1008 return height;
1009 }
1010
1011 /* Return the pixel width of display area AREA of window W.
1012 ANY_AREA means return the total width of W, not including
1013 fringes to the left and right of the window. */
1014
1015 int
1016 window_box_width (struct window *w, enum glyph_row_area area)
1017 {
1018 int pixels = w->pixel_width;
1019
1020 if (!w->pseudo_window_p)
1021 {
1022 pixels -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
1023 pixels -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
1024
1025 if (area == TEXT_AREA)
1026 pixels -= (WINDOW_MARGINS_WIDTH (w)
1027 + WINDOW_FRINGES_WIDTH (w));
1028 else if (area == LEFT_MARGIN_AREA)
1029 pixels = WINDOW_LEFT_MARGIN_WIDTH (w);
1030 else if (area == RIGHT_MARGIN_AREA)
1031 pixels = WINDOW_RIGHT_MARGIN_WIDTH (w);
1032 }
1033
1034 return pixels;
1035 }
1036
1037
1038 /* Return the pixel height of the display area of window W, not
1039 including mode lines of W, if any. */
1040
1041 int
1042 window_box_height (struct window *w)
1043 {
1044 struct frame *f = XFRAME (w->frame);
1045 int height = WINDOW_PIXEL_HEIGHT (w);
1046
1047 eassert (height >= 0);
1048
1049 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1050
1051 /* Note: the code below that determines the mode-line/header-line
1052 height is essentially the same as that contained in the macro
1053 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1054 the appropriate glyph row has its `mode_line_p' flag set,
1055 and if it doesn't, uses estimate_mode_line_height instead. */
1056
1057 if (WINDOW_WANTS_MODELINE_P (w))
1058 {
1059 struct glyph_row *ml_row
1060 = (w->current_matrix && w->current_matrix->rows
1061 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1062 : 0);
1063 if (ml_row && ml_row->mode_line_p)
1064 height -= ml_row->height;
1065 else
1066 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1067 }
1068
1069 if (WINDOW_WANTS_HEADER_LINE_P (w))
1070 {
1071 struct glyph_row *hl_row
1072 = (w->current_matrix && w->current_matrix->rows
1073 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1074 : 0);
1075 if (hl_row && hl_row->mode_line_p)
1076 height -= hl_row->height;
1077 else
1078 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1079 }
1080
1081 /* With a very small font and a mode-line that's taller than
1082 default, we might end up with a negative height. */
1083 return max (0, height);
1084 }
1085
1086 /* Return the window-relative coordinate of the left edge of display
1087 area AREA of window W. ANY_AREA means return the left edge of the
1088 whole window, to the right of the left fringe of W. */
1089
1090 int
1091 window_box_left_offset (struct window *w, enum glyph_row_area area)
1092 {
1093 int x;
1094
1095 if (w->pseudo_window_p)
1096 return 0;
1097
1098 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1099
1100 if (area == TEXT_AREA)
1101 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1102 + window_box_width (w, LEFT_MARGIN_AREA));
1103 else if (area == RIGHT_MARGIN_AREA)
1104 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1105 + window_box_width (w, LEFT_MARGIN_AREA)
1106 + window_box_width (w, TEXT_AREA)
1107 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1108 ? 0
1109 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1110 else if (area == LEFT_MARGIN_AREA
1111 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1112 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1113
1114 return x;
1115 }
1116
1117
1118 /* Return the window-relative coordinate of the right edge of display
1119 area AREA of window W. ANY_AREA means return the right edge of the
1120 whole window, to the left of the right fringe of W. */
1121
1122 int
1123 window_box_right_offset (struct window *w, enum glyph_row_area area)
1124 {
1125 return window_box_left_offset (w, area) + window_box_width (w, area);
1126 }
1127
1128 /* Return the frame-relative coordinate of the left edge of display
1129 area AREA of window W. ANY_AREA means return the left edge of the
1130 whole window, to the right of the left fringe of W. */
1131
1132 int
1133 window_box_left (struct window *w, enum glyph_row_area area)
1134 {
1135 struct frame *f = XFRAME (w->frame);
1136 int x;
1137
1138 if (w->pseudo_window_p)
1139 return FRAME_INTERNAL_BORDER_WIDTH (f);
1140
1141 x = (WINDOW_LEFT_EDGE_X (w)
1142 + window_box_left_offset (w, area));
1143
1144 return x;
1145 }
1146
1147
1148 /* Return the frame-relative coordinate of the right edge of display
1149 area AREA of window W. ANY_AREA means return the right edge of the
1150 whole window, to the left of the right fringe of W. */
1151
1152 int
1153 window_box_right (struct window *w, enum glyph_row_area area)
1154 {
1155 return window_box_left (w, area) + window_box_width (w, area);
1156 }
1157
1158 /* Get the bounding box of the display area AREA of window W, without
1159 mode lines, in frame-relative coordinates. ANY_AREA means the
1160 whole window, not including the left and right fringes of
1161 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1162 coordinates of the upper-left corner of the box. Return in
1163 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1164
1165 void
1166 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1167 int *box_y, int *box_width, int *box_height)
1168 {
1169 if (box_width)
1170 *box_width = window_box_width (w, area);
1171 if (box_height)
1172 *box_height = window_box_height (w);
1173 if (box_x)
1174 *box_x = window_box_left (w, area);
1175 if (box_y)
1176 {
1177 *box_y = WINDOW_TOP_EDGE_Y (w);
1178 if (WINDOW_WANTS_HEADER_LINE_P (w))
1179 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1180 }
1181 }
1182
1183 #ifdef HAVE_WINDOW_SYSTEM
1184
1185 /* Get the bounding box of the display area AREA of window W, without
1186 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1187 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1188 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1189 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1190 box. */
1191
1192 static void
1193 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1194 int *bottom_right_x, int *bottom_right_y)
1195 {
1196 window_box (w, ANY_AREA, top_left_x, top_left_y,
1197 bottom_right_x, bottom_right_y);
1198 *bottom_right_x += *top_left_x;
1199 *bottom_right_y += *top_left_y;
1200 }
1201
1202 #endif /* HAVE_WINDOW_SYSTEM */
1203
1204 /***********************************************************************
1205 Utilities
1206 ***********************************************************************/
1207
1208 /* Return the bottom y-position of the line the iterator IT is in.
1209 This can modify IT's settings. */
1210
1211 int
1212 line_bottom_y (struct it *it)
1213 {
1214 int line_height = it->max_ascent + it->max_descent;
1215 int line_top_y = it->current_y;
1216
1217 if (line_height == 0)
1218 {
1219 if (last_height)
1220 line_height = last_height;
1221 else if (IT_CHARPOS (*it) < ZV)
1222 {
1223 move_it_by_lines (it, 1);
1224 line_height = (it->max_ascent || it->max_descent
1225 ? it->max_ascent + it->max_descent
1226 : last_height);
1227 }
1228 else
1229 {
1230 struct glyph_row *row = it->glyph_row;
1231
1232 /* Use the default character height. */
1233 it->glyph_row = NULL;
1234 it->what = IT_CHARACTER;
1235 it->c = ' ';
1236 it->len = 1;
1237 PRODUCE_GLYPHS (it);
1238 line_height = it->ascent + it->descent;
1239 it->glyph_row = row;
1240 }
1241 }
1242
1243 return line_top_y + line_height;
1244 }
1245
1246 DEFUN ("line-pixel-height", Fline_pixel_height,
1247 Sline_pixel_height, 0, 0, 0,
1248 doc: /* Return height in pixels of text line in the selected window.
1249
1250 Value is the height in pixels of the line at point. */)
1251 (void)
1252 {
1253 struct it it;
1254 struct text_pos pt;
1255 struct window *w = XWINDOW (selected_window);
1256
1257 SET_TEXT_POS (pt, PT, PT_BYTE);
1258 start_display (&it, w, pt);
1259 it.vpos = it.current_y = 0;
1260 last_height = 0;
1261 return make_number (line_bottom_y (&it));
1262 }
1263
1264 /* Return the default pixel height of text lines in window W. The
1265 value is the canonical height of the W frame's default font, plus
1266 any extra space required by the line-spacing variable or frame
1267 parameter.
1268
1269 Implementation note: this ignores any line-spacing text properties
1270 put on the newline characters. This is because those properties
1271 only affect the _screen_ line ending in the newline (i.e., in a
1272 continued line, only the last screen line will be affected), which
1273 means only a small number of lines in a buffer can ever use this
1274 feature. Since this function is used to compute the default pixel
1275 equivalent of text lines in a window, we can safely ignore those
1276 few lines. For the same reasons, we ignore the line-height
1277 properties. */
1278 int
1279 default_line_pixel_height (struct window *w)
1280 {
1281 struct frame *f = WINDOW_XFRAME (w);
1282 int height = FRAME_LINE_HEIGHT (f);
1283
1284 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1285 {
1286 struct buffer *b = XBUFFER (w->contents);
1287 Lisp_Object val = BVAR (b, extra_line_spacing);
1288
1289 if (NILP (val))
1290 val = BVAR (&buffer_defaults, extra_line_spacing);
1291 if (!NILP (val))
1292 {
1293 if (RANGED_INTEGERP (0, val, INT_MAX))
1294 height += XFASTINT (val);
1295 else if (FLOATP (val))
1296 {
1297 int addon = XFLOAT_DATA (val) * height + 0.5;
1298
1299 if (addon >= 0)
1300 height += addon;
1301 }
1302 }
1303 else
1304 height += f->extra_line_spacing;
1305 }
1306
1307 return height;
1308 }
1309
1310 /* Subroutine of pos_visible_p below. Extracts a display string, if
1311 any, from the display spec given as its argument. */
1312 static Lisp_Object
1313 string_from_display_spec (Lisp_Object spec)
1314 {
1315 if (CONSP (spec))
1316 {
1317 while (CONSP (spec))
1318 {
1319 if (STRINGP (XCAR (spec)))
1320 return XCAR (spec);
1321 spec = XCDR (spec);
1322 }
1323 }
1324 else if (VECTORP (spec))
1325 {
1326 ptrdiff_t i;
1327
1328 for (i = 0; i < ASIZE (spec); i++)
1329 {
1330 if (STRINGP (AREF (spec, i)))
1331 return AREF (spec, i);
1332 }
1333 return Qnil;
1334 }
1335
1336 return spec;
1337 }
1338
1339
1340 /* Limit insanely large values of W->hscroll on frame F to the largest
1341 value that will still prevent first_visible_x and last_visible_x of
1342 'struct it' from overflowing an int. */
1343 static int
1344 window_hscroll_limited (struct window *w, struct frame *f)
1345 {
1346 ptrdiff_t window_hscroll = w->hscroll;
1347 int window_text_width = window_box_width (w, TEXT_AREA);
1348 int colwidth = FRAME_COLUMN_WIDTH (f);
1349
1350 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1351 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1352
1353 return window_hscroll;
1354 }
1355
1356 /* Return 1 if position CHARPOS is visible in window W.
1357 CHARPOS < 0 means return info about WINDOW_END position.
1358 If visible, set *X and *Y to pixel coordinates of top left corner.
1359 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1360 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1361
1362 int
1363 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1364 int *rtop, int *rbot, int *rowh, int *vpos)
1365 {
1366 struct it it;
1367 void *itdata = bidi_shelve_cache ();
1368 struct text_pos top;
1369 int visible_p = 0;
1370 struct buffer *old_buffer = NULL;
1371
1372 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1373 return visible_p;
1374
1375 if (XBUFFER (w->contents) != current_buffer)
1376 {
1377 old_buffer = current_buffer;
1378 set_buffer_internal_1 (XBUFFER (w->contents));
1379 }
1380
1381 SET_TEXT_POS_FROM_MARKER (top, w->start);
1382 /* Scrolling a minibuffer window via scroll bar when the echo area
1383 shows long text sometimes resets the minibuffer contents behind
1384 our backs. */
1385 if (CHARPOS (top) > ZV)
1386 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1387
1388 /* Compute exact mode line heights. */
1389 if (WINDOW_WANTS_MODELINE_P (w))
1390 w->mode_line_height
1391 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1392 BVAR (current_buffer, mode_line_format));
1393
1394 if (WINDOW_WANTS_HEADER_LINE_P (w))
1395 w->header_line_height
1396 = display_mode_line (w, HEADER_LINE_FACE_ID,
1397 BVAR (current_buffer, header_line_format));
1398
1399 start_display (&it, w, top);
1400 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1401 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1402
1403 if (charpos >= 0
1404 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1405 && IT_CHARPOS (it) >= charpos)
1406 /* When scanning backwards under bidi iteration, move_it_to
1407 stops at or _before_ CHARPOS, because it stops at or to
1408 the _right_ of the character at CHARPOS. */
1409 || (it.bidi_p && it.bidi_it.scan_dir == -1
1410 && IT_CHARPOS (it) <= charpos)))
1411 {
1412 /* We have reached CHARPOS, or passed it. How the call to
1413 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1414 or covered by a display property, move_it_to stops at the end
1415 of the invisible text, to the right of CHARPOS. (ii) If
1416 CHARPOS is in a display vector, move_it_to stops on its last
1417 glyph. */
1418 int top_x = it.current_x;
1419 int top_y = it.current_y;
1420 /* Calling line_bottom_y may change it.method, it.position, etc. */
1421 enum it_method it_method = it.method;
1422 int bottom_y = (last_height = 0, line_bottom_y (&it));
1423 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1424
1425 if (top_y < window_top_y)
1426 visible_p = bottom_y > window_top_y;
1427 else if (top_y < it.last_visible_y)
1428 visible_p = true;
1429 if (bottom_y >= it.last_visible_y
1430 && it.bidi_p && it.bidi_it.scan_dir == -1
1431 && IT_CHARPOS (it) < charpos)
1432 {
1433 /* When the last line of the window is scanned backwards
1434 under bidi iteration, we could be duped into thinking
1435 that we have passed CHARPOS, when in fact move_it_to
1436 simply stopped short of CHARPOS because it reached
1437 last_visible_y. To see if that's what happened, we call
1438 move_it_to again with a slightly larger vertical limit,
1439 and see if it actually moved vertically; if it did, we
1440 didn't really reach CHARPOS, which is beyond window end. */
1441 struct it save_it = it;
1442 /* Why 10? because we don't know how many canonical lines
1443 will the height of the next line(s) be. So we guess. */
1444 int ten_more_lines = 10 * default_line_pixel_height (w);
1445
1446 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1447 MOVE_TO_POS | MOVE_TO_Y);
1448 if (it.current_y > top_y)
1449 visible_p = 0;
1450
1451 it = save_it;
1452 }
1453 if (visible_p)
1454 {
1455 if (it_method == GET_FROM_DISPLAY_VECTOR)
1456 {
1457 /* We stopped on the last glyph of a display vector.
1458 Try and recompute. Hack alert! */
1459 if (charpos < 2 || top.charpos >= charpos)
1460 top_x = it.glyph_row->x;
1461 else
1462 {
1463 struct it it2, it2_prev;
1464 /* The idea is to get to the previous buffer
1465 position, consume the character there, and use
1466 the pixel coordinates we get after that. But if
1467 the previous buffer position is also displayed
1468 from a display vector, we need to consume all of
1469 the glyphs from that display vector. */
1470 start_display (&it2, w, top);
1471 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1472 /* If we didn't get to CHARPOS - 1, there's some
1473 replacing display property at that position, and
1474 we stopped after it. That is exactly the place
1475 whose coordinates we want. */
1476 if (IT_CHARPOS (it2) != charpos - 1)
1477 it2_prev = it2;
1478 else
1479 {
1480 /* Iterate until we get out of the display
1481 vector that displays the character at
1482 CHARPOS - 1. */
1483 do {
1484 get_next_display_element (&it2);
1485 PRODUCE_GLYPHS (&it2);
1486 it2_prev = it2;
1487 set_iterator_to_next (&it2, 1);
1488 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1489 && IT_CHARPOS (it2) < charpos);
1490 }
1491 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1492 || it2_prev.current_x > it2_prev.last_visible_x)
1493 top_x = it.glyph_row->x;
1494 else
1495 {
1496 top_x = it2_prev.current_x;
1497 top_y = it2_prev.current_y;
1498 }
1499 }
1500 }
1501 else if (IT_CHARPOS (it) != charpos)
1502 {
1503 Lisp_Object cpos = make_number (charpos);
1504 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1505 Lisp_Object string = string_from_display_spec (spec);
1506 struct text_pos tpos;
1507 int replacing_spec_p;
1508 bool newline_in_string
1509 = (STRINGP (string)
1510 && memchr (SDATA (string), '\n', SBYTES (string)));
1511
1512 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1513 replacing_spec_p
1514 = (!NILP (spec)
1515 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1516 charpos, FRAME_WINDOW_P (it.f)));
1517 /* The tricky code below is needed because there's a
1518 discrepancy between move_it_to and how we set cursor
1519 when PT is at the beginning of a portion of text
1520 covered by a display property or an overlay with a
1521 display property, or the display line ends in a
1522 newline from a display string. move_it_to will stop
1523 _after_ such display strings, whereas
1524 set_cursor_from_row conspires with cursor_row_p to
1525 place the cursor on the first glyph produced from the
1526 display string. */
1527
1528 /* We have overshoot PT because it is covered by a
1529 display property that replaces the text it covers.
1530 If the string includes embedded newlines, we are also
1531 in the wrong display line. Backtrack to the correct
1532 line, where the display property begins. */
1533 if (replacing_spec_p)
1534 {
1535 Lisp_Object startpos, endpos;
1536 EMACS_INT start, end;
1537 struct it it3;
1538 int it3_moved;
1539
1540 /* Find the first and the last buffer positions
1541 covered by the display string. */
1542 endpos =
1543 Fnext_single_char_property_change (cpos, Qdisplay,
1544 Qnil, Qnil);
1545 startpos =
1546 Fprevious_single_char_property_change (endpos, Qdisplay,
1547 Qnil, Qnil);
1548 start = XFASTINT (startpos);
1549 end = XFASTINT (endpos);
1550 /* Move to the last buffer position before the
1551 display property. */
1552 start_display (&it3, w, top);
1553 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1554 /* Move forward one more line if the position before
1555 the display string is a newline or if it is the
1556 rightmost character on a line that is
1557 continued or word-wrapped. */
1558 if (it3.method == GET_FROM_BUFFER
1559 && (it3.c == '\n'
1560 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1561 move_it_by_lines (&it3, 1);
1562 else if (move_it_in_display_line_to (&it3, -1,
1563 it3.current_x
1564 + it3.pixel_width,
1565 MOVE_TO_X)
1566 == MOVE_LINE_CONTINUED)
1567 {
1568 move_it_by_lines (&it3, 1);
1569 /* When we are under word-wrap, the #$@%!
1570 move_it_by_lines moves 2 lines, so we need to
1571 fix that up. */
1572 if (it3.line_wrap == WORD_WRAP)
1573 move_it_by_lines (&it3, -1);
1574 }
1575
1576 /* Record the vertical coordinate of the display
1577 line where we wound up. */
1578 top_y = it3.current_y;
1579 if (it3.bidi_p)
1580 {
1581 /* When characters are reordered for display,
1582 the character displayed to the left of the
1583 display string could be _after_ the display
1584 property in the logical order. Use the
1585 smallest vertical position of these two. */
1586 start_display (&it3, w, top);
1587 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1588 if (it3.current_y < top_y)
1589 top_y = it3.current_y;
1590 }
1591 /* Move from the top of the window to the beginning
1592 of the display line where the display string
1593 begins. */
1594 start_display (&it3, w, top);
1595 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1596 /* If it3_moved stays zero after the 'while' loop
1597 below, that means we already were at a newline
1598 before the loop (e.g., the display string begins
1599 with a newline), so we don't need to (and cannot)
1600 inspect the glyphs of it3.glyph_row, because
1601 PRODUCE_GLYPHS will not produce anything for a
1602 newline, and thus it3.glyph_row stays at its
1603 stale content it got at top of the window. */
1604 it3_moved = 0;
1605 /* Finally, advance the iterator until we hit the
1606 first display element whose character position is
1607 CHARPOS, or until the first newline from the
1608 display string, which signals the end of the
1609 display line. */
1610 while (get_next_display_element (&it3))
1611 {
1612 PRODUCE_GLYPHS (&it3);
1613 if (IT_CHARPOS (it3) == charpos
1614 || ITERATOR_AT_END_OF_LINE_P (&it3))
1615 break;
1616 it3_moved = 1;
1617 set_iterator_to_next (&it3, 0);
1618 }
1619 top_x = it3.current_x - it3.pixel_width;
1620 /* Normally, we would exit the above loop because we
1621 found the display element whose character
1622 position is CHARPOS. For the contingency that we
1623 didn't, and stopped at the first newline from the
1624 display string, move back over the glyphs
1625 produced from the string, until we find the
1626 rightmost glyph not from the string. */
1627 if (it3_moved
1628 && newline_in_string
1629 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1630 {
1631 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1632 + it3.glyph_row->used[TEXT_AREA];
1633
1634 while (EQ ((g - 1)->object, string))
1635 {
1636 --g;
1637 top_x -= g->pixel_width;
1638 }
1639 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1640 + it3.glyph_row->used[TEXT_AREA]);
1641 }
1642 }
1643 }
1644
1645 *x = top_x;
1646 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1647 *rtop = max (0, window_top_y - top_y);
1648 *rbot = max (0, bottom_y - it.last_visible_y);
1649 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1650 - max (top_y, window_top_y)));
1651 *vpos = it.vpos;
1652 }
1653 }
1654 else
1655 {
1656 /* We were asked to provide info about WINDOW_END. */
1657 struct it it2;
1658 void *it2data = NULL;
1659
1660 SAVE_IT (it2, it, it2data);
1661 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1662 move_it_by_lines (&it, 1);
1663 if (charpos < IT_CHARPOS (it)
1664 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1665 {
1666 visible_p = true;
1667 RESTORE_IT (&it2, &it2, it2data);
1668 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1669 *x = it2.current_x;
1670 *y = it2.current_y + it2.max_ascent - it2.ascent;
1671 *rtop = max (0, -it2.current_y);
1672 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1673 - it.last_visible_y));
1674 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1675 it.last_visible_y)
1676 - max (it2.current_y,
1677 WINDOW_HEADER_LINE_HEIGHT (w))));
1678 *vpos = it2.vpos;
1679 }
1680 else
1681 bidi_unshelve_cache (it2data, 1);
1682 }
1683 bidi_unshelve_cache (itdata, 0);
1684
1685 if (old_buffer)
1686 set_buffer_internal_1 (old_buffer);
1687
1688 if (visible_p && w->hscroll > 0)
1689 *x -=
1690 window_hscroll_limited (w, WINDOW_XFRAME (w))
1691 * WINDOW_FRAME_COLUMN_WIDTH (w);
1692
1693 #if 0
1694 /* Debugging code. */
1695 if (visible_p)
1696 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1697 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1698 else
1699 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1700 #endif
1701
1702 return visible_p;
1703 }
1704
1705
1706 /* Return the next character from STR. Return in *LEN the length of
1707 the character. This is like STRING_CHAR_AND_LENGTH but never
1708 returns an invalid character. If we find one, we return a `?', but
1709 with the length of the invalid character. */
1710
1711 static int
1712 string_char_and_length (const unsigned char *str, int *len)
1713 {
1714 int c;
1715
1716 c = STRING_CHAR_AND_LENGTH (str, *len);
1717 if (!CHAR_VALID_P (c))
1718 /* We may not change the length here because other places in Emacs
1719 don't use this function, i.e. they silently accept invalid
1720 characters. */
1721 c = '?';
1722
1723 return c;
1724 }
1725
1726
1727
1728 /* Given a position POS containing a valid character and byte position
1729 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1730
1731 static struct text_pos
1732 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1733 {
1734 eassert (STRINGP (string) && nchars >= 0);
1735
1736 if (STRING_MULTIBYTE (string))
1737 {
1738 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1739 int len;
1740
1741 while (nchars--)
1742 {
1743 string_char_and_length (p, &len);
1744 p += len;
1745 CHARPOS (pos) += 1;
1746 BYTEPOS (pos) += len;
1747 }
1748 }
1749 else
1750 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1751
1752 return pos;
1753 }
1754
1755
1756 /* Value is the text position, i.e. character and byte position,
1757 for character position CHARPOS in STRING. */
1758
1759 static struct text_pos
1760 string_pos (ptrdiff_t charpos, Lisp_Object string)
1761 {
1762 struct text_pos pos;
1763 eassert (STRINGP (string));
1764 eassert (charpos >= 0);
1765 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1766 return pos;
1767 }
1768
1769
1770 /* Value is a text position, i.e. character and byte position, for
1771 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1772 means recognize multibyte characters. */
1773
1774 static struct text_pos
1775 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1776 {
1777 struct text_pos pos;
1778
1779 eassert (s != NULL);
1780 eassert (charpos >= 0);
1781
1782 if (multibyte_p)
1783 {
1784 int len;
1785
1786 SET_TEXT_POS (pos, 0, 0);
1787 while (charpos--)
1788 {
1789 string_char_and_length ((const unsigned char *) s, &len);
1790 s += len;
1791 CHARPOS (pos) += 1;
1792 BYTEPOS (pos) += len;
1793 }
1794 }
1795 else
1796 SET_TEXT_POS (pos, charpos, charpos);
1797
1798 return pos;
1799 }
1800
1801
1802 /* Value is the number of characters in C string S. MULTIBYTE_P
1803 non-zero means recognize multibyte characters. */
1804
1805 static ptrdiff_t
1806 number_of_chars (const char *s, bool multibyte_p)
1807 {
1808 ptrdiff_t nchars;
1809
1810 if (multibyte_p)
1811 {
1812 ptrdiff_t rest = strlen (s);
1813 int len;
1814 const unsigned char *p = (const unsigned char *) s;
1815
1816 for (nchars = 0; rest > 0; ++nchars)
1817 {
1818 string_char_and_length (p, &len);
1819 rest -= len, p += len;
1820 }
1821 }
1822 else
1823 nchars = strlen (s);
1824
1825 return nchars;
1826 }
1827
1828
1829 /* Compute byte position NEWPOS->bytepos corresponding to
1830 NEWPOS->charpos. POS is a known position in string STRING.
1831 NEWPOS->charpos must be >= POS.charpos. */
1832
1833 static void
1834 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1835 {
1836 eassert (STRINGP (string));
1837 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1838
1839 if (STRING_MULTIBYTE (string))
1840 *newpos = string_pos_nchars_ahead (pos, string,
1841 CHARPOS (*newpos) - CHARPOS (pos));
1842 else
1843 BYTEPOS (*newpos) = CHARPOS (*newpos);
1844 }
1845
1846 /* EXPORT:
1847 Return an estimation of the pixel height of mode or header lines on
1848 frame F. FACE_ID specifies what line's height to estimate. */
1849
1850 int
1851 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1852 {
1853 #ifdef HAVE_WINDOW_SYSTEM
1854 if (FRAME_WINDOW_P (f))
1855 {
1856 int height = FONT_HEIGHT (FRAME_FONT (f));
1857
1858 /* This function is called so early when Emacs starts that the face
1859 cache and mode line face are not yet initialized. */
1860 if (FRAME_FACE_CACHE (f))
1861 {
1862 struct face *face = FACE_FROM_ID (f, face_id);
1863 if (face)
1864 {
1865 if (face->font)
1866 height = FONT_HEIGHT (face->font);
1867 if (face->box_line_width > 0)
1868 height += 2 * face->box_line_width;
1869 }
1870 }
1871
1872 return height;
1873 }
1874 #endif
1875
1876 return 1;
1877 }
1878
1879 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1880 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1881 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1882 not force the value into range. */
1883
1884 void
1885 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1886 int *x, int *y, NativeRectangle *bounds, int noclip)
1887 {
1888
1889 #ifdef HAVE_WINDOW_SYSTEM
1890 if (FRAME_WINDOW_P (f))
1891 {
1892 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1893 even for negative values. */
1894 if (pix_x < 0)
1895 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1896 if (pix_y < 0)
1897 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1898
1899 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1900 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1901
1902 if (bounds)
1903 STORE_NATIVE_RECT (*bounds,
1904 FRAME_COL_TO_PIXEL_X (f, pix_x),
1905 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1906 FRAME_COLUMN_WIDTH (f) - 1,
1907 FRAME_LINE_HEIGHT (f) - 1);
1908
1909 /* PXW: Should we clip pixelized before converting to
1910 columns/lines ? */
1911 if (!noclip)
1912 {
1913 if (pix_x < 0)
1914 pix_x = 0;
1915 else if (pix_x > FRAME_TOTAL_COLS (f))
1916 pix_x = FRAME_TOTAL_COLS (f);
1917
1918 if (pix_y < 0)
1919 pix_y = 0;
1920 else if (pix_y > FRAME_LINES (f))
1921 pix_y = FRAME_LINES (f);
1922 }
1923 }
1924 #endif
1925
1926 *x = pix_x;
1927 *y = pix_y;
1928 }
1929
1930
1931 /* Find the glyph under window-relative coordinates X/Y in window W.
1932 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1933 strings. Return in *HPOS and *VPOS the row and column number of
1934 the glyph found. Return in *AREA the glyph area containing X.
1935 Value is a pointer to the glyph found or null if X/Y is not on
1936 text, or we can't tell because W's current matrix is not up to
1937 date. */
1938
1939 static struct glyph *
1940 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1941 int *dx, int *dy, int *area)
1942 {
1943 struct glyph *glyph, *end;
1944 struct glyph_row *row = NULL;
1945 int x0, i;
1946
1947 /* Find row containing Y. Give up if some row is not enabled. */
1948 for (i = 0; i < w->current_matrix->nrows; ++i)
1949 {
1950 row = MATRIX_ROW (w->current_matrix, i);
1951 if (!row->enabled_p)
1952 return NULL;
1953 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1954 break;
1955 }
1956
1957 *vpos = i;
1958 *hpos = 0;
1959
1960 /* Give up if Y is not in the window. */
1961 if (i == w->current_matrix->nrows)
1962 return NULL;
1963
1964 /* Get the glyph area containing X. */
1965 if (w->pseudo_window_p)
1966 {
1967 *area = TEXT_AREA;
1968 x0 = 0;
1969 }
1970 else
1971 {
1972 if (x < window_box_left_offset (w, TEXT_AREA))
1973 {
1974 *area = LEFT_MARGIN_AREA;
1975 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1976 }
1977 else if (x < window_box_right_offset (w, TEXT_AREA))
1978 {
1979 *area = TEXT_AREA;
1980 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1981 }
1982 else
1983 {
1984 *area = RIGHT_MARGIN_AREA;
1985 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1986 }
1987 }
1988
1989 /* Find glyph containing X. */
1990 glyph = row->glyphs[*area];
1991 end = glyph + row->used[*area];
1992 x -= x0;
1993 while (glyph < end && x >= glyph->pixel_width)
1994 {
1995 x -= glyph->pixel_width;
1996 ++glyph;
1997 }
1998
1999 if (glyph == end)
2000 return NULL;
2001
2002 if (dx)
2003 {
2004 *dx = x;
2005 *dy = y - (row->y + row->ascent - glyph->ascent);
2006 }
2007
2008 *hpos = glyph - row->glyphs[*area];
2009 return glyph;
2010 }
2011
2012 /* Convert frame-relative x/y to coordinates relative to window W.
2013 Takes pseudo-windows into account. */
2014
2015 static void
2016 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2017 {
2018 if (w->pseudo_window_p)
2019 {
2020 /* A pseudo-window is always full-width, and starts at the
2021 left edge of the frame, plus a frame border. */
2022 struct frame *f = XFRAME (w->frame);
2023 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2024 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2025 }
2026 else
2027 {
2028 *x -= WINDOW_LEFT_EDGE_X (w);
2029 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2030 }
2031 }
2032
2033 #ifdef HAVE_WINDOW_SYSTEM
2034
2035 /* EXPORT:
2036 Return in RECTS[] at most N clipping rectangles for glyph string S.
2037 Return the number of stored rectangles. */
2038
2039 int
2040 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2041 {
2042 XRectangle r;
2043
2044 if (n <= 0)
2045 return 0;
2046
2047 if (s->row->full_width_p)
2048 {
2049 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2050 r.x = WINDOW_LEFT_EDGE_X (s->w);
2051 if (s->row->mode_line_p)
2052 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2053 else
2054 r.width = WINDOW_PIXEL_WIDTH (s->w);
2055
2056 /* Unless displaying a mode or menu bar line, which are always
2057 fully visible, clip to the visible part of the row. */
2058 if (s->w->pseudo_window_p)
2059 r.height = s->row->visible_height;
2060 else
2061 r.height = s->height;
2062 }
2063 else
2064 {
2065 /* This is a text line that may be partially visible. */
2066 r.x = window_box_left (s->w, s->area);
2067 r.width = window_box_width (s->w, s->area);
2068 r.height = s->row->visible_height;
2069 }
2070
2071 if (s->clip_head)
2072 if (r.x < s->clip_head->x)
2073 {
2074 if (r.width >= s->clip_head->x - r.x)
2075 r.width -= s->clip_head->x - r.x;
2076 else
2077 r.width = 0;
2078 r.x = s->clip_head->x;
2079 }
2080 if (s->clip_tail)
2081 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2082 {
2083 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2084 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2085 else
2086 r.width = 0;
2087 }
2088
2089 /* If S draws overlapping rows, it's sufficient to use the top and
2090 bottom of the window for clipping because this glyph string
2091 intentionally draws over other lines. */
2092 if (s->for_overlaps)
2093 {
2094 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2095 r.height = window_text_bottom_y (s->w) - r.y;
2096
2097 /* Alas, the above simple strategy does not work for the
2098 environments with anti-aliased text: if the same text is
2099 drawn onto the same place multiple times, it gets thicker.
2100 If the overlap we are processing is for the erased cursor, we
2101 take the intersection with the rectangle of the cursor. */
2102 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2103 {
2104 XRectangle rc, r_save = r;
2105
2106 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2107 rc.y = s->w->phys_cursor.y;
2108 rc.width = s->w->phys_cursor_width;
2109 rc.height = s->w->phys_cursor_height;
2110
2111 x_intersect_rectangles (&r_save, &rc, &r);
2112 }
2113 }
2114 else
2115 {
2116 /* Don't use S->y for clipping because it doesn't take partially
2117 visible lines into account. For example, it can be negative for
2118 partially visible lines at the top of a window. */
2119 if (!s->row->full_width_p
2120 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2121 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2122 else
2123 r.y = max (0, s->row->y);
2124 }
2125
2126 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2127
2128 /* If drawing the cursor, don't let glyph draw outside its
2129 advertised boundaries. Cleartype does this under some circumstances. */
2130 if (s->hl == DRAW_CURSOR)
2131 {
2132 struct glyph *glyph = s->first_glyph;
2133 int height, max_y;
2134
2135 if (s->x > r.x)
2136 {
2137 r.width -= s->x - r.x;
2138 r.x = s->x;
2139 }
2140 r.width = min (r.width, glyph->pixel_width);
2141
2142 /* If r.y is below window bottom, ensure that we still see a cursor. */
2143 height = min (glyph->ascent + glyph->descent,
2144 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2145 max_y = window_text_bottom_y (s->w) - height;
2146 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2147 if (s->ybase - glyph->ascent > max_y)
2148 {
2149 r.y = max_y;
2150 r.height = height;
2151 }
2152 else
2153 {
2154 /* Don't draw cursor glyph taller than our actual glyph. */
2155 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2156 if (height < r.height)
2157 {
2158 max_y = r.y + r.height;
2159 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2160 r.height = min (max_y - r.y, height);
2161 }
2162 }
2163 }
2164
2165 if (s->row->clip)
2166 {
2167 XRectangle r_save = r;
2168
2169 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2170 r.width = 0;
2171 }
2172
2173 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2174 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2175 {
2176 #ifdef CONVERT_FROM_XRECT
2177 CONVERT_FROM_XRECT (r, *rects);
2178 #else
2179 *rects = r;
2180 #endif
2181 return 1;
2182 }
2183 else
2184 {
2185 /* If we are processing overlapping and allowed to return
2186 multiple clipping rectangles, we exclude the row of the glyph
2187 string from the clipping rectangle. This is to avoid drawing
2188 the same text on the environment with anti-aliasing. */
2189 #ifdef CONVERT_FROM_XRECT
2190 XRectangle rs[2];
2191 #else
2192 XRectangle *rs = rects;
2193 #endif
2194 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2195
2196 if (s->for_overlaps & OVERLAPS_PRED)
2197 {
2198 rs[i] = r;
2199 if (r.y + r.height > row_y)
2200 {
2201 if (r.y < row_y)
2202 rs[i].height = row_y - r.y;
2203 else
2204 rs[i].height = 0;
2205 }
2206 i++;
2207 }
2208 if (s->for_overlaps & OVERLAPS_SUCC)
2209 {
2210 rs[i] = r;
2211 if (r.y < row_y + s->row->visible_height)
2212 {
2213 if (r.y + r.height > row_y + s->row->visible_height)
2214 {
2215 rs[i].y = row_y + s->row->visible_height;
2216 rs[i].height = r.y + r.height - rs[i].y;
2217 }
2218 else
2219 rs[i].height = 0;
2220 }
2221 i++;
2222 }
2223
2224 n = i;
2225 #ifdef CONVERT_FROM_XRECT
2226 for (i = 0; i < n; i++)
2227 CONVERT_FROM_XRECT (rs[i], rects[i]);
2228 #endif
2229 return n;
2230 }
2231 }
2232
2233 /* EXPORT:
2234 Return in *NR the clipping rectangle for glyph string S. */
2235
2236 void
2237 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2238 {
2239 get_glyph_string_clip_rects (s, nr, 1);
2240 }
2241
2242
2243 /* EXPORT:
2244 Return the position and height of the phys cursor in window W.
2245 Set w->phys_cursor_width to width of phys cursor.
2246 */
2247
2248 void
2249 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2250 struct glyph *glyph, int *xp, int *yp, int *heightp)
2251 {
2252 struct frame *f = XFRAME (WINDOW_FRAME (w));
2253 int x, y, wd, h, h0, y0;
2254
2255 /* Compute the width of the rectangle to draw. If on a stretch
2256 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2257 rectangle as wide as the glyph, but use a canonical character
2258 width instead. */
2259 wd = glyph->pixel_width - 1;
2260 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2261 wd++; /* Why? */
2262 #endif
2263
2264 x = w->phys_cursor.x;
2265 if (x < 0)
2266 {
2267 wd += x;
2268 x = 0;
2269 }
2270
2271 if (glyph->type == STRETCH_GLYPH
2272 && !x_stretch_cursor_p)
2273 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2274 w->phys_cursor_width = wd;
2275
2276 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2277
2278 /* If y is below window bottom, ensure that we still see a cursor. */
2279 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2280
2281 h = max (h0, glyph->ascent + glyph->descent);
2282 h0 = min (h0, glyph->ascent + glyph->descent);
2283
2284 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2285 if (y < y0)
2286 {
2287 h = max (h - (y0 - y) + 1, h0);
2288 y = y0 - 1;
2289 }
2290 else
2291 {
2292 y0 = window_text_bottom_y (w) - h0;
2293 if (y > y0)
2294 {
2295 h += y - y0;
2296 y = y0;
2297 }
2298 }
2299
2300 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2301 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2302 *heightp = h;
2303 }
2304
2305 /*
2306 * Remember which glyph the mouse is over.
2307 */
2308
2309 void
2310 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2311 {
2312 Lisp_Object window;
2313 struct window *w;
2314 struct glyph_row *r, *gr, *end_row;
2315 enum window_part part;
2316 enum glyph_row_area area;
2317 int x, y, width, height;
2318
2319 /* Try to determine frame pixel position and size of the glyph under
2320 frame pixel coordinates X/Y on frame F. */
2321
2322 if (window_resize_pixelwise)
2323 {
2324 width = height = 1;
2325 goto virtual_glyph;
2326 }
2327 else if (!f->glyphs_initialized_p
2328 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2329 NILP (window)))
2330 {
2331 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2332 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2333 goto virtual_glyph;
2334 }
2335
2336 w = XWINDOW (window);
2337 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2338 height = WINDOW_FRAME_LINE_HEIGHT (w);
2339
2340 x = window_relative_x_coord (w, part, gx);
2341 y = gy - WINDOW_TOP_EDGE_Y (w);
2342
2343 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2344 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2345
2346 if (w->pseudo_window_p)
2347 {
2348 area = TEXT_AREA;
2349 part = ON_MODE_LINE; /* Don't adjust margin. */
2350 goto text_glyph;
2351 }
2352
2353 switch (part)
2354 {
2355 case ON_LEFT_MARGIN:
2356 area = LEFT_MARGIN_AREA;
2357 goto text_glyph;
2358
2359 case ON_RIGHT_MARGIN:
2360 area = RIGHT_MARGIN_AREA;
2361 goto text_glyph;
2362
2363 case ON_HEADER_LINE:
2364 case ON_MODE_LINE:
2365 gr = (part == ON_HEADER_LINE
2366 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2367 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2368 gy = gr->y;
2369 area = TEXT_AREA;
2370 goto text_glyph_row_found;
2371
2372 case ON_TEXT:
2373 area = TEXT_AREA;
2374
2375 text_glyph:
2376 gr = 0; gy = 0;
2377 for (; r <= end_row && r->enabled_p; ++r)
2378 if (r->y + r->height > y)
2379 {
2380 gr = r; gy = r->y;
2381 break;
2382 }
2383
2384 text_glyph_row_found:
2385 if (gr && gy <= y)
2386 {
2387 struct glyph *g = gr->glyphs[area];
2388 struct glyph *end = g + gr->used[area];
2389
2390 height = gr->height;
2391 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2392 if (gx + g->pixel_width > x)
2393 break;
2394
2395 if (g < end)
2396 {
2397 if (g->type == IMAGE_GLYPH)
2398 {
2399 /* Don't remember when mouse is over image, as
2400 image may have hot-spots. */
2401 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2402 return;
2403 }
2404 width = g->pixel_width;
2405 }
2406 else
2407 {
2408 /* Use nominal char spacing at end of line. */
2409 x -= gx;
2410 gx += (x / width) * width;
2411 }
2412
2413 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2414 gx += window_box_left_offset (w, area);
2415 }
2416 else
2417 {
2418 /* Use nominal line height at end of window. */
2419 gx = (x / width) * width;
2420 y -= gy;
2421 gy += (y / height) * height;
2422 }
2423 break;
2424
2425 case ON_LEFT_FRINGE:
2426 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2427 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2428 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2429 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2430 goto row_glyph;
2431
2432 case ON_RIGHT_FRINGE:
2433 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2434 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2435 : window_box_right_offset (w, TEXT_AREA));
2436 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2437 goto row_glyph;
2438
2439 case ON_SCROLL_BAR:
2440 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2441 ? 0
2442 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2443 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2444 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2445 : 0)));
2446 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2447
2448 row_glyph:
2449 gr = 0, gy = 0;
2450 for (; r <= end_row && r->enabled_p; ++r)
2451 if (r->y + r->height > y)
2452 {
2453 gr = r; gy = r->y;
2454 break;
2455 }
2456
2457 if (gr && gy <= y)
2458 height = gr->height;
2459 else
2460 {
2461 /* Use nominal line height at end of window. */
2462 y -= gy;
2463 gy += (y / height) * height;
2464 }
2465 break;
2466
2467 default:
2468 ;
2469 virtual_glyph:
2470 /* If there is no glyph under the mouse, then we divide the screen
2471 into a grid of the smallest glyph in the frame, and use that
2472 as our "glyph". */
2473
2474 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2475 round down even for negative values. */
2476 if (gx < 0)
2477 gx -= width - 1;
2478 if (gy < 0)
2479 gy -= height - 1;
2480
2481 gx = (gx / width) * width;
2482 gy = (gy / height) * height;
2483
2484 goto store_rect;
2485 }
2486
2487 gx += WINDOW_LEFT_EDGE_X (w);
2488 gy += WINDOW_TOP_EDGE_Y (w);
2489
2490 store_rect:
2491 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2492
2493 /* Visible feedback for debugging. */
2494 #if 0
2495 #if HAVE_X_WINDOWS
2496 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2497 f->output_data.x->normal_gc,
2498 gx, gy, width, height);
2499 #endif
2500 #endif
2501 }
2502
2503
2504 #endif /* HAVE_WINDOW_SYSTEM */
2505
2506 static void
2507 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2508 {
2509 eassert (w);
2510 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2511 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2512 w->window_end_vpos
2513 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2514 }
2515
2516 /***********************************************************************
2517 Lisp form evaluation
2518 ***********************************************************************/
2519
2520 /* Error handler for safe_eval and safe_call. */
2521
2522 static Lisp_Object
2523 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2524 {
2525 add_to_log ("Error during redisplay: %S signaled %S",
2526 Flist (nargs, args), arg);
2527 return Qnil;
2528 }
2529
2530 /* Call function FUNC with the rest of NARGS - 1 arguments
2531 following. Return the result, or nil if something went
2532 wrong. Prevent redisplay during the evaluation. */
2533
2534 Lisp_Object
2535 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2536 {
2537 Lisp_Object val;
2538
2539 if (inhibit_eval_during_redisplay)
2540 val = Qnil;
2541 else
2542 {
2543 va_list ap;
2544 ptrdiff_t i;
2545 ptrdiff_t count = SPECPDL_INDEX ();
2546 struct gcpro gcpro1;
2547 Lisp_Object *args = alloca (nargs * word_size);
2548
2549 args[0] = func;
2550 va_start (ap, func);
2551 for (i = 1; i < nargs; i++)
2552 args[i] = va_arg (ap, Lisp_Object);
2553 va_end (ap);
2554
2555 GCPRO1 (args[0]);
2556 gcpro1.nvars = nargs;
2557 specbind (Qinhibit_redisplay, Qt);
2558 /* Use Qt to ensure debugger does not run,
2559 so there is no possibility of wanting to redisplay. */
2560 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2561 safe_eval_handler);
2562 UNGCPRO;
2563 val = unbind_to (count, val);
2564 }
2565
2566 return val;
2567 }
2568
2569
2570 /* Call function FN with one argument ARG.
2571 Return the result, or nil if something went wrong. */
2572
2573 Lisp_Object
2574 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2575 {
2576 return safe_call (2, fn, arg);
2577 }
2578
2579 static Lisp_Object Qeval;
2580
2581 Lisp_Object
2582 safe_eval (Lisp_Object sexpr)
2583 {
2584 return safe_call1 (Qeval, sexpr);
2585 }
2586
2587 /* Call function FN with two arguments ARG1 and ARG2.
2588 Return the result, or nil if something went wrong. */
2589
2590 Lisp_Object
2591 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2592 {
2593 return safe_call (3, fn, arg1, arg2);
2594 }
2595
2596
2597 \f
2598 /***********************************************************************
2599 Debugging
2600 ***********************************************************************/
2601
2602 #if 0
2603
2604 /* Define CHECK_IT to perform sanity checks on iterators.
2605 This is for debugging. It is too slow to do unconditionally. */
2606
2607 static void
2608 check_it (struct it *it)
2609 {
2610 if (it->method == GET_FROM_STRING)
2611 {
2612 eassert (STRINGP (it->string));
2613 eassert (IT_STRING_CHARPOS (*it) >= 0);
2614 }
2615 else
2616 {
2617 eassert (IT_STRING_CHARPOS (*it) < 0);
2618 if (it->method == GET_FROM_BUFFER)
2619 {
2620 /* Check that character and byte positions agree. */
2621 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2622 }
2623 }
2624
2625 if (it->dpvec)
2626 eassert (it->current.dpvec_index >= 0);
2627 else
2628 eassert (it->current.dpvec_index < 0);
2629 }
2630
2631 #define CHECK_IT(IT) check_it ((IT))
2632
2633 #else /* not 0 */
2634
2635 #define CHECK_IT(IT) (void) 0
2636
2637 #endif /* not 0 */
2638
2639
2640 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2641
2642 /* Check that the window end of window W is what we expect it
2643 to be---the last row in the current matrix displaying text. */
2644
2645 static void
2646 check_window_end (struct window *w)
2647 {
2648 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2649 {
2650 struct glyph_row *row;
2651 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2652 !row->enabled_p
2653 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2654 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2655 }
2656 }
2657
2658 #define CHECK_WINDOW_END(W) check_window_end ((W))
2659
2660 #else
2661
2662 #define CHECK_WINDOW_END(W) (void) 0
2663
2664 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2665
2666 /***********************************************************************
2667 Iterator initialization
2668 ***********************************************************************/
2669
2670 /* Initialize IT for displaying current_buffer in window W, starting
2671 at character position CHARPOS. CHARPOS < 0 means that no buffer
2672 position is specified which is useful when the iterator is assigned
2673 a position later. BYTEPOS is the byte position corresponding to
2674 CHARPOS.
2675
2676 If ROW is not null, calls to produce_glyphs with IT as parameter
2677 will produce glyphs in that row.
2678
2679 BASE_FACE_ID is the id of a base face to use. It must be one of
2680 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2681 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2682 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2683
2684 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2685 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2686 will be initialized to use the corresponding mode line glyph row of
2687 the desired matrix of W. */
2688
2689 void
2690 init_iterator (struct it *it, struct window *w,
2691 ptrdiff_t charpos, ptrdiff_t bytepos,
2692 struct glyph_row *row, enum face_id base_face_id)
2693 {
2694 enum face_id remapped_base_face_id = base_face_id;
2695
2696 /* Some precondition checks. */
2697 eassert (w != NULL && it != NULL);
2698 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2699 && charpos <= ZV));
2700
2701 /* If face attributes have been changed since the last redisplay,
2702 free realized faces now because they depend on face definitions
2703 that might have changed. Don't free faces while there might be
2704 desired matrices pending which reference these faces. */
2705 if (face_change_count && !inhibit_free_realized_faces)
2706 {
2707 face_change_count = 0;
2708 free_all_realized_faces (Qnil);
2709 }
2710
2711 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2712 if (! NILP (Vface_remapping_alist))
2713 remapped_base_face_id
2714 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2715
2716 /* Use one of the mode line rows of W's desired matrix if
2717 appropriate. */
2718 if (row == NULL)
2719 {
2720 if (base_face_id == MODE_LINE_FACE_ID
2721 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2722 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2723 else if (base_face_id == HEADER_LINE_FACE_ID)
2724 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2725 }
2726
2727 /* Clear IT. */
2728 memset (it, 0, sizeof *it);
2729 it->current.overlay_string_index = -1;
2730 it->current.dpvec_index = -1;
2731 it->base_face_id = remapped_base_face_id;
2732 it->string = Qnil;
2733 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2734 it->paragraph_embedding = L2R;
2735 it->bidi_it.string.lstring = Qnil;
2736 it->bidi_it.string.s = NULL;
2737 it->bidi_it.string.bufpos = 0;
2738 it->bidi_it.w = w;
2739
2740 /* The window in which we iterate over current_buffer: */
2741 XSETWINDOW (it->window, w);
2742 it->w = w;
2743 it->f = XFRAME (w->frame);
2744
2745 it->cmp_it.id = -1;
2746
2747 /* Extra space between lines (on window systems only). */
2748 if (base_face_id == DEFAULT_FACE_ID
2749 && FRAME_WINDOW_P (it->f))
2750 {
2751 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2752 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2753 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2754 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2755 * FRAME_LINE_HEIGHT (it->f));
2756 else if (it->f->extra_line_spacing > 0)
2757 it->extra_line_spacing = it->f->extra_line_spacing;
2758 it->max_extra_line_spacing = 0;
2759 }
2760
2761 /* If realized faces have been removed, e.g. because of face
2762 attribute changes of named faces, recompute them. When running
2763 in batch mode, the face cache of the initial frame is null. If
2764 we happen to get called, make a dummy face cache. */
2765 if (FRAME_FACE_CACHE (it->f) == NULL)
2766 init_frame_faces (it->f);
2767 if (FRAME_FACE_CACHE (it->f)->used == 0)
2768 recompute_basic_faces (it->f);
2769
2770 /* Current value of the `slice', `space-width', and 'height' properties. */
2771 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2772 it->space_width = Qnil;
2773 it->font_height = Qnil;
2774 it->override_ascent = -1;
2775
2776 /* Are control characters displayed as `^C'? */
2777 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2778
2779 /* -1 means everything between a CR and the following line end
2780 is invisible. >0 means lines indented more than this value are
2781 invisible. */
2782 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2783 ? (clip_to_bounds
2784 (-1, XINT (BVAR (current_buffer, selective_display)),
2785 PTRDIFF_MAX))
2786 : (!NILP (BVAR (current_buffer, selective_display))
2787 ? -1 : 0));
2788 it->selective_display_ellipsis_p
2789 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2790
2791 /* Display table to use. */
2792 it->dp = window_display_table (w);
2793
2794 /* Are multibyte characters enabled in current_buffer? */
2795 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2796
2797 /* Get the position at which the redisplay_end_trigger hook should
2798 be run, if it is to be run at all. */
2799 if (MARKERP (w->redisplay_end_trigger)
2800 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2801 it->redisplay_end_trigger_charpos
2802 = marker_position (w->redisplay_end_trigger);
2803 else if (INTEGERP (w->redisplay_end_trigger))
2804 it->redisplay_end_trigger_charpos =
2805 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2806
2807 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2808
2809 /* Are lines in the display truncated? */
2810 if (base_face_id != DEFAULT_FACE_ID
2811 || it->w->hscroll
2812 || (! WINDOW_FULL_WIDTH_P (it->w)
2813 && ((!NILP (Vtruncate_partial_width_windows)
2814 && !INTEGERP (Vtruncate_partial_width_windows))
2815 || (INTEGERP (Vtruncate_partial_width_windows)
2816 /* PXW: Shall we do something about this ? */
2817 && (WINDOW_TOTAL_COLS (it->w)
2818 < XINT (Vtruncate_partial_width_windows))))))
2819 it->line_wrap = TRUNCATE;
2820 else if (NILP (BVAR (current_buffer, truncate_lines)))
2821 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2822 ? WINDOW_WRAP : WORD_WRAP;
2823 else
2824 it->line_wrap = TRUNCATE;
2825
2826 /* Get dimensions of truncation and continuation glyphs. These are
2827 displayed as fringe bitmaps under X, but we need them for such
2828 frames when the fringes are turned off. But leave the dimensions
2829 zero for tooltip frames, as these glyphs look ugly there and also
2830 sabotage calculations of tooltip dimensions in x-show-tip. */
2831 #ifdef HAVE_WINDOW_SYSTEM
2832 if (!(FRAME_WINDOW_P (it->f)
2833 && FRAMEP (tip_frame)
2834 && it->f == XFRAME (tip_frame)))
2835 #endif
2836 {
2837 if (it->line_wrap == TRUNCATE)
2838 {
2839 /* We will need the truncation glyph. */
2840 eassert (it->glyph_row == NULL);
2841 produce_special_glyphs (it, IT_TRUNCATION);
2842 it->truncation_pixel_width = it->pixel_width;
2843 }
2844 else
2845 {
2846 /* We will need the continuation glyph. */
2847 eassert (it->glyph_row == NULL);
2848 produce_special_glyphs (it, IT_CONTINUATION);
2849 it->continuation_pixel_width = it->pixel_width;
2850 }
2851 }
2852
2853 /* Reset these values to zero because the produce_special_glyphs
2854 above has changed them. */
2855 it->pixel_width = it->ascent = it->descent = 0;
2856 it->phys_ascent = it->phys_descent = 0;
2857
2858 /* Set this after getting the dimensions of truncation and
2859 continuation glyphs, so that we don't produce glyphs when calling
2860 produce_special_glyphs, above. */
2861 it->glyph_row = row;
2862 it->area = TEXT_AREA;
2863
2864 /* Forget any previous info about this row being reversed. */
2865 if (it->glyph_row)
2866 it->glyph_row->reversed_p = 0;
2867
2868 /* Get the dimensions of the display area. The display area
2869 consists of the visible window area plus a horizontally scrolled
2870 part to the left of the window. All x-values are relative to the
2871 start of this total display area. */
2872 if (base_face_id != DEFAULT_FACE_ID)
2873 {
2874 /* Mode lines, menu bar in terminal frames. */
2875 it->first_visible_x = 0;
2876 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2877 }
2878 else
2879 {
2880 it->first_visible_x
2881 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2882 it->last_visible_x = (it->first_visible_x
2883 + window_box_width (w, TEXT_AREA));
2884
2885 /* If we truncate lines, leave room for the truncation glyph(s) at
2886 the right margin. Otherwise, leave room for the continuation
2887 glyph(s). Done only if the window has no fringes. Since we
2888 don't know at this point whether there will be any R2L lines in
2889 the window, we reserve space for truncation/continuation glyphs
2890 even if only one of the fringes is absent. */
2891 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2892 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2893 {
2894 if (it->line_wrap == TRUNCATE)
2895 it->last_visible_x -= it->truncation_pixel_width;
2896 else
2897 it->last_visible_x -= it->continuation_pixel_width;
2898 }
2899
2900 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2901 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2902 }
2903
2904 /* Leave room for a border glyph. */
2905 if (!FRAME_WINDOW_P (it->f)
2906 && !WINDOW_RIGHTMOST_P (it->w))
2907 it->last_visible_x -= 1;
2908
2909 it->last_visible_y = window_text_bottom_y (w);
2910
2911 /* For mode lines and alike, arrange for the first glyph having a
2912 left box line if the face specifies a box. */
2913 if (base_face_id != DEFAULT_FACE_ID)
2914 {
2915 struct face *face;
2916
2917 it->face_id = remapped_base_face_id;
2918
2919 /* If we have a boxed mode line, make the first character appear
2920 with a left box line. */
2921 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2922 if (face->box != FACE_NO_BOX)
2923 it->start_of_box_run_p = true;
2924 }
2925
2926 /* If a buffer position was specified, set the iterator there,
2927 getting overlays and face properties from that position. */
2928 if (charpos >= BUF_BEG (current_buffer))
2929 {
2930 it->end_charpos = ZV;
2931 eassert (charpos == BYTE_TO_CHAR (bytepos));
2932 IT_CHARPOS (*it) = charpos;
2933 IT_BYTEPOS (*it) = bytepos;
2934
2935 /* We will rely on `reseat' to set this up properly, via
2936 handle_face_prop. */
2937 it->face_id = it->base_face_id;
2938
2939 it->start = it->current;
2940 /* Do we need to reorder bidirectional text? Not if this is a
2941 unibyte buffer: by definition, none of the single-byte
2942 characters are strong R2L, so no reordering is needed. And
2943 bidi.c doesn't support unibyte buffers anyway. Also, don't
2944 reorder while we are loading loadup.el, since the tables of
2945 character properties needed for reordering are not yet
2946 available. */
2947 it->bidi_p =
2948 NILP (Vpurify_flag)
2949 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2950 && it->multibyte_p;
2951
2952 /* If we are to reorder bidirectional text, init the bidi
2953 iterator. */
2954 if (it->bidi_p)
2955 {
2956 /* Note the paragraph direction that this buffer wants to
2957 use. */
2958 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2959 Qleft_to_right))
2960 it->paragraph_embedding = L2R;
2961 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2962 Qright_to_left))
2963 it->paragraph_embedding = R2L;
2964 else
2965 it->paragraph_embedding = NEUTRAL_DIR;
2966 bidi_unshelve_cache (NULL, 0);
2967 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2968 &it->bidi_it);
2969 }
2970
2971 /* Compute faces etc. */
2972 reseat (it, it->current.pos, 1);
2973 }
2974
2975 CHECK_IT (it);
2976 }
2977
2978
2979 /* Initialize IT for the display of window W with window start POS. */
2980
2981 void
2982 start_display (struct it *it, struct window *w, struct text_pos pos)
2983 {
2984 struct glyph_row *row;
2985 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2986
2987 row = w->desired_matrix->rows + first_vpos;
2988 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2989 it->first_vpos = first_vpos;
2990
2991 /* Don't reseat to previous visible line start if current start
2992 position is in a string or image. */
2993 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2994 {
2995 int start_at_line_beg_p;
2996 int first_y = it->current_y;
2997
2998 /* If window start is not at a line start, skip forward to POS to
2999 get the correct continuation lines width. */
3000 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3001 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3002 if (!start_at_line_beg_p)
3003 {
3004 int new_x;
3005
3006 reseat_at_previous_visible_line_start (it);
3007 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3008
3009 new_x = it->current_x + it->pixel_width;
3010
3011 /* If lines are continued, this line may end in the middle
3012 of a multi-glyph character (e.g. a control character
3013 displayed as \003, or in the middle of an overlay
3014 string). In this case move_it_to above will not have
3015 taken us to the start of the continuation line but to the
3016 end of the continued line. */
3017 if (it->current_x > 0
3018 && it->line_wrap != TRUNCATE /* Lines are continued. */
3019 && (/* And glyph doesn't fit on the line. */
3020 new_x > it->last_visible_x
3021 /* Or it fits exactly and we're on a window
3022 system frame. */
3023 || (new_x == it->last_visible_x
3024 && FRAME_WINDOW_P (it->f)
3025 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3026 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3027 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3028 {
3029 if ((it->current.dpvec_index >= 0
3030 || it->current.overlay_string_index >= 0)
3031 /* If we are on a newline from a display vector or
3032 overlay string, then we are already at the end of
3033 a screen line; no need to go to the next line in
3034 that case, as this line is not really continued.
3035 (If we do go to the next line, C-e will not DTRT.) */
3036 && it->c != '\n')
3037 {
3038 set_iterator_to_next (it, 1);
3039 move_it_in_display_line_to (it, -1, -1, 0);
3040 }
3041
3042 it->continuation_lines_width += it->current_x;
3043 }
3044 /* If the character at POS is displayed via a display
3045 vector, move_it_to above stops at the final glyph of
3046 IT->dpvec. To make the caller redisplay that character
3047 again (a.k.a. start at POS), we need to reset the
3048 dpvec_index to the beginning of IT->dpvec. */
3049 else if (it->current.dpvec_index >= 0)
3050 it->current.dpvec_index = 0;
3051
3052 /* We're starting a new display line, not affected by the
3053 height of the continued line, so clear the appropriate
3054 fields in the iterator structure. */
3055 it->max_ascent = it->max_descent = 0;
3056 it->max_phys_ascent = it->max_phys_descent = 0;
3057
3058 it->current_y = first_y;
3059 it->vpos = 0;
3060 it->current_x = it->hpos = 0;
3061 }
3062 }
3063 }
3064
3065
3066 /* Return 1 if POS is a position in ellipses displayed for invisible
3067 text. W is the window we display, for text property lookup. */
3068
3069 static int
3070 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3071 {
3072 Lisp_Object prop, window;
3073 int ellipses_p = 0;
3074 ptrdiff_t charpos = CHARPOS (pos->pos);
3075
3076 /* If POS specifies a position in a display vector, this might
3077 be for an ellipsis displayed for invisible text. We won't
3078 get the iterator set up for delivering that ellipsis unless
3079 we make sure that it gets aware of the invisible text. */
3080 if (pos->dpvec_index >= 0
3081 && pos->overlay_string_index < 0
3082 && CHARPOS (pos->string_pos) < 0
3083 && charpos > BEGV
3084 && (XSETWINDOW (window, w),
3085 prop = Fget_char_property (make_number (charpos),
3086 Qinvisible, window),
3087 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3088 {
3089 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3090 window);
3091 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3092 }
3093
3094 return ellipses_p;
3095 }
3096
3097
3098 /* Initialize IT for stepping through current_buffer in window W,
3099 starting at position POS that includes overlay string and display
3100 vector/ control character translation position information. Value
3101 is zero if there are overlay strings with newlines at POS. */
3102
3103 static int
3104 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3105 {
3106 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3107 int i, overlay_strings_with_newlines = 0;
3108
3109 /* If POS specifies a position in a display vector, this might
3110 be for an ellipsis displayed for invisible text. We won't
3111 get the iterator set up for delivering that ellipsis unless
3112 we make sure that it gets aware of the invisible text. */
3113 if (in_ellipses_for_invisible_text_p (pos, w))
3114 {
3115 --charpos;
3116 bytepos = 0;
3117 }
3118
3119 /* Keep in mind: the call to reseat in init_iterator skips invisible
3120 text, so we might end up at a position different from POS. This
3121 is only a problem when POS is a row start after a newline and an
3122 overlay starts there with an after-string, and the overlay has an
3123 invisible property. Since we don't skip invisible text in
3124 display_line and elsewhere immediately after consuming the
3125 newline before the row start, such a POS will not be in a string,
3126 but the call to init_iterator below will move us to the
3127 after-string. */
3128 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3129
3130 /* This only scans the current chunk -- it should scan all chunks.
3131 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3132 to 16 in 22.1 to make this a lesser problem. */
3133 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3134 {
3135 const char *s = SSDATA (it->overlay_strings[i]);
3136 const char *e = s + SBYTES (it->overlay_strings[i]);
3137
3138 while (s < e && *s != '\n')
3139 ++s;
3140
3141 if (s < e)
3142 {
3143 overlay_strings_with_newlines = 1;
3144 break;
3145 }
3146 }
3147
3148 /* If position is within an overlay string, set up IT to the right
3149 overlay string. */
3150 if (pos->overlay_string_index >= 0)
3151 {
3152 int relative_index;
3153
3154 /* If the first overlay string happens to have a `display'
3155 property for an image, the iterator will be set up for that
3156 image, and we have to undo that setup first before we can
3157 correct the overlay string index. */
3158 if (it->method == GET_FROM_IMAGE)
3159 pop_it (it);
3160
3161 /* We already have the first chunk of overlay strings in
3162 IT->overlay_strings. Load more until the one for
3163 pos->overlay_string_index is in IT->overlay_strings. */
3164 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3165 {
3166 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3167 it->current.overlay_string_index = 0;
3168 while (n--)
3169 {
3170 load_overlay_strings (it, 0);
3171 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3172 }
3173 }
3174
3175 it->current.overlay_string_index = pos->overlay_string_index;
3176 relative_index = (it->current.overlay_string_index
3177 % OVERLAY_STRING_CHUNK_SIZE);
3178 it->string = it->overlay_strings[relative_index];
3179 eassert (STRINGP (it->string));
3180 it->current.string_pos = pos->string_pos;
3181 it->method = GET_FROM_STRING;
3182 it->end_charpos = SCHARS (it->string);
3183 /* Set up the bidi iterator for this overlay string. */
3184 if (it->bidi_p)
3185 {
3186 it->bidi_it.string.lstring = it->string;
3187 it->bidi_it.string.s = NULL;
3188 it->bidi_it.string.schars = SCHARS (it->string);
3189 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3190 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3191 it->bidi_it.string.unibyte = !it->multibyte_p;
3192 it->bidi_it.w = it->w;
3193 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3194 FRAME_WINDOW_P (it->f), &it->bidi_it);
3195
3196 /* Synchronize the state of the bidi iterator with
3197 pos->string_pos. For any string position other than
3198 zero, this will be done automagically when we resume
3199 iteration over the string and get_visually_first_element
3200 is called. But if string_pos is zero, and the string is
3201 to be reordered for display, we need to resync manually,
3202 since it could be that the iteration state recorded in
3203 pos ended at string_pos of 0 moving backwards in string. */
3204 if (CHARPOS (pos->string_pos) == 0)
3205 {
3206 get_visually_first_element (it);
3207 if (IT_STRING_CHARPOS (*it) != 0)
3208 do {
3209 /* Paranoia. */
3210 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3211 bidi_move_to_visually_next (&it->bidi_it);
3212 } while (it->bidi_it.charpos != 0);
3213 }
3214 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3215 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3216 }
3217 }
3218
3219 if (CHARPOS (pos->string_pos) >= 0)
3220 {
3221 /* Recorded position is not in an overlay string, but in another
3222 string. This can only be a string from a `display' property.
3223 IT should already be filled with that string. */
3224 it->current.string_pos = pos->string_pos;
3225 eassert (STRINGP (it->string));
3226 if (it->bidi_p)
3227 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3228 FRAME_WINDOW_P (it->f), &it->bidi_it);
3229 }
3230
3231 /* Restore position in display vector translations, control
3232 character translations or ellipses. */
3233 if (pos->dpvec_index >= 0)
3234 {
3235 if (it->dpvec == NULL)
3236 get_next_display_element (it);
3237 eassert (it->dpvec && it->current.dpvec_index == 0);
3238 it->current.dpvec_index = pos->dpvec_index;
3239 }
3240
3241 CHECK_IT (it);
3242 return !overlay_strings_with_newlines;
3243 }
3244
3245
3246 /* Initialize IT for stepping through current_buffer in window W
3247 starting at ROW->start. */
3248
3249 static void
3250 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3251 {
3252 init_from_display_pos (it, w, &row->start);
3253 it->start = row->start;
3254 it->continuation_lines_width = row->continuation_lines_width;
3255 CHECK_IT (it);
3256 }
3257
3258
3259 /* Initialize IT for stepping through current_buffer in window W
3260 starting in the line following ROW, i.e. starting at ROW->end.
3261 Value is zero if there are overlay strings with newlines at ROW's
3262 end position. */
3263
3264 static int
3265 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3266 {
3267 int success = 0;
3268
3269 if (init_from_display_pos (it, w, &row->end))
3270 {
3271 if (row->continued_p)
3272 it->continuation_lines_width
3273 = row->continuation_lines_width + row->pixel_width;
3274 CHECK_IT (it);
3275 success = 1;
3276 }
3277
3278 return success;
3279 }
3280
3281
3282
3283 \f
3284 /***********************************************************************
3285 Text properties
3286 ***********************************************************************/
3287
3288 /* Called when IT reaches IT->stop_charpos. Handle text property and
3289 overlay changes. Set IT->stop_charpos to the next position where
3290 to stop. */
3291
3292 static void
3293 handle_stop (struct it *it)
3294 {
3295 enum prop_handled handled;
3296 int handle_overlay_change_p;
3297 struct props *p;
3298
3299 it->dpvec = NULL;
3300 it->current.dpvec_index = -1;
3301 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3302 it->ignore_overlay_strings_at_pos_p = 0;
3303 it->ellipsis_p = 0;
3304
3305 /* Use face of preceding text for ellipsis (if invisible) */
3306 if (it->selective_display_ellipsis_p)
3307 it->saved_face_id = it->face_id;
3308
3309 do
3310 {
3311 handled = HANDLED_NORMALLY;
3312
3313 /* Call text property handlers. */
3314 for (p = it_props; p->handler; ++p)
3315 {
3316 handled = p->handler (it);
3317
3318 if (handled == HANDLED_RECOMPUTE_PROPS)
3319 break;
3320 else if (handled == HANDLED_RETURN)
3321 {
3322 /* We still want to show before and after strings from
3323 overlays even if the actual buffer text is replaced. */
3324 if (!handle_overlay_change_p
3325 || it->sp > 1
3326 /* Don't call get_overlay_strings_1 if we already
3327 have overlay strings loaded, because doing so
3328 will load them again and push the iterator state
3329 onto the stack one more time, which is not
3330 expected by the rest of the code that processes
3331 overlay strings. */
3332 || (it->current.overlay_string_index < 0
3333 ? !get_overlay_strings_1 (it, 0, 0)
3334 : 0))
3335 {
3336 if (it->ellipsis_p)
3337 setup_for_ellipsis (it, 0);
3338 /* When handling a display spec, we might load an
3339 empty string. In that case, discard it here. We
3340 used to discard it in handle_single_display_spec,
3341 but that causes get_overlay_strings_1, above, to
3342 ignore overlay strings that we must check. */
3343 if (STRINGP (it->string) && !SCHARS (it->string))
3344 pop_it (it);
3345 return;
3346 }
3347 else if (STRINGP (it->string) && !SCHARS (it->string))
3348 pop_it (it);
3349 else
3350 {
3351 it->ignore_overlay_strings_at_pos_p = true;
3352 it->string_from_display_prop_p = 0;
3353 it->from_disp_prop_p = 0;
3354 handle_overlay_change_p = 0;
3355 }
3356 handled = HANDLED_RECOMPUTE_PROPS;
3357 break;
3358 }
3359 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3360 handle_overlay_change_p = 0;
3361 }
3362
3363 if (handled != HANDLED_RECOMPUTE_PROPS)
3364 {
3365 /* Don't check for overlay strings below when set to deliver
3366 characters from a display vector. */
3367 if (it->method == GET_FROM_DISPLAY_VECTOR)
3368 handle_overlay_change_p = 0;
3369
3370 /* Handle overlay changes.
3371 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3372 if it finds overlays. */
3373 if (handle_overlay_change_p)
3374 handled = handle_overlay_change (it);
3375 }
3376
3377 if (it->ellipsis_p)
3378 {
3379 setup_for_ellipsis (it, 0);
3380 break;
3381 }
3382 }
3383 while (handled == HANDLED_RECOMPUTE_PROPS);
3384
3385 /* Determine where to stop next. */
3386 if (handled == HANDLED_NORMALLY)
3387 compute_stop_pos (it);
3388 }
3389
3390
3391 /* Compute IT->stop_charpos from text property and overlay change
3392 information for IT's current position. */
3393
3394 static void
3395 compute_stop_pos (struct it *it)
3396 {
3397 register INTERVAL iv, next_iv;
3398 Lisp_Object object, limit, position;
3399 ptrdiff_t charpos, bytepos;
3400
3401 if (STRINGP (it->string))
3402 {
3403 /* Strings are usually short, so don't limit the search for
3404 properties. */
3405 it->stop_charpos = it->end_charpos;
3406 object = it->string;
3407 limit = Qnil;
3408 charpos = IT_STRING_CHARPOS (*it);
3409 bytepos = IT_STRING_BYTEPOS (*it);
3410 }
3411 else
3412 {
3413 ptrdiff_t pos;
3414
3415 /* If end_charpos is out of range for some reason, such as a
3416 misbehaving display function, rationalize it (Bug#5984). */
3417 if (it->end_charpos > ZV)
3418 it->end_charpos = ZV;
3419 it->stop_charpos = it->end_charpos;
3420
3421 /* If next overlay change is in front of the current stop pos
3422 (which is IT->end_charpos), stop there. Note: value of
3423 next_overlay_change is point-max if no overlay change
3424 follows. */
3425 charpos = IT_CHARPOS (*it);
3426 bytepos = IT_BYTEPOS (*it);
3427 pos = next_overlay_change (charpos);
3428 if (pos < it->stop_charpos)
3429 it->stop_charpos = pos;
3430
3431 /* Set up variables for computing the stop position from text
3432 property changes. */
3433 XSETBUFFER (object, current_buffer);
3434 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3435 }
3436
3437 /* Get the interval containing IT's position. Value is a null
3438 interval if there isn't such an interval. */
3439 position = make_number (charpos);
3440 iv = validate_interval_range (object, &position, &position, 0);
3441 if (iv)
3442 {
3443 Lisp_Object values_here[LAST_PROP_IDX];
3444 struct props *p;
3445
3446 /* Get properties here. */
3447 for (p = it_props; p->handler; ++p)
3448 values_here[p->idx] = textget (iv->plist, *p->name);
3449
3450 /* Look for an interval following iv that has different
3451 properties. */
3452 for (next_iv = next_interval (iv);
3453 (next_iv
3454 && (NILP (limit)
3455 || XFASTINT (limit) > next_iv->position));
3456 next_iv = next_interval (next_iv))
3457 {
3458 for (p = it_props; p->handler; ++p)
3459 {
3460 Lisp_Object new_value;
3461
3462 new_value = textget (next_iv->plist, *p->name);
3463 if (!EQ (values_here[p->idx], new_value))
3464 break;
3465 }
3466
3467 if (p->handler)
3468 break;
3469 }
3470
3471 if (next_iv)
3472 {
3473 if (INTEGERP (limit)
3474 && next_iv->position >= XFASTINT (limit))
3475 /* No text property change up to limit. */
3476 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3477 else
3478 /* Text properties change in next_iv. */
3479 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3480 }
3481 }
3482
3483 if (it->cmp_it.id < 0)
3484 {
3485 ptrdiff_t stoppos = it->end_charpos;
3486
3487 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3488 stoppos = -1;
3489 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3490 stoppos, it->string);
3491 }
3492
3493 eassert (STRINGP (it->string)
3494 || (it->stop_charpos >= BEGV
3495 && it->stop_charpos >= IT_CHARPOS (*it)));
3496 }
3497
3498
3499 /* Return the position of the next overlay change after POS in
3500 current_buffer. Value is point-max if no overlay change
3501 follows. This is like `next-overlay-change' but doesn't use
3502 xmalloc. */
3503
3504 static ptrdiff_t
3505 next_overlay_change (ptrdiff_t pos)
3506 {
3507 ptrdiff_t i, noverlays;
3508 ptrdiff_t endpos;
3509 Lisp_Object *overlays;
3510
3511 /* Get all overlays at the given position. */
3512 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3513
3514 /* If any of these overlays ends before endpos,
3515 use its ending point instead. */
3516 for (i = 0; i < noverlays; ++i)
3517 {
3518 Lisp_Object oend;
3519 ptrdiff_t oendpos;
3520
3521 oend = OVERLAY_END (overlays[i]);
3522 oendpos = OVERLAY_POSITION (oend);
3523 endpos = min (endpos, oendpos);
3524 }
3525
3526 return endpos;
3527 }
3528
3529 /* How many characters forward to search for a display property or
3530 display string. Searching too far forward makes the bidi display
3531 sluggish, especially in small windows. */
3532 #define MAX_DISP_SCAN 250
3533
3534 /* Return the character position of a display string at or after
3535 position specified by POSITION. If no display string exists at or
3536 after POSITION, return ZV. A display string is either an overlay
3537 with `display' property whose value is a string, or a `display'
3538 text property whose value is a string. STRING is data about the
3539 string to iterate; if STRING->lstring is nil, we are iterating a
3540 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3541 on a GUI frame. DISP_PROP is set to zero if we searched
3542 MAX_DISP_SCAN characters forward without finding any display
3543 strings, non-zero otherwise. It is set to 2 if the display string
3544 uses any kind of `(space ...)' spec that will produce a stretch of
3545 white space in the text area. */
3546 ptrdiff_t
3547 compute_display_string_pos (struct text_pos *position,
3548 struct bidi_string_data *string,
3549 struct window *w,
3550 int frame_window_p, int *disp_prop)
3551 {
3552 /* OBJECT = nil means current buffer. */
3553 Lisp_Object object, object1;
3554 Lisp_Object pos, spec, limpos;
3555 int string_p = (string && (STRINGP (string->lstring) || string->s));
3556 ptrdiff_t eob = string_p ? string->schars : ZV;
3557 ptrdiff_t begb = string_p ? 0 : BEGV;
3558 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3559 ptrdiff_t lim =
3560 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3561 struct text_pos tpos;
3562 int rv = 0;
3563
3564 if (string && STRINGP (string->lstring))
3565 object1 = object = string->lstring;
3566 else if (w && !string_p)
3567 {
3568 XSETWINDOW (object, w);
3569 object1 = Qnil;
3570 }
3571 else
3572 object1 = object = Qnil;
3573
3574 *disp_prop = 1;
3575
3576 if (charpos >= eob
3577 /* We don't support display properties whose values are strings
3578 that have display string properties. */
3579 || string->from_disp_str
3580 /* C strings cannot have display properties. */
3581 || (string->s && !STRINGP (object)))
3582 {
3583 *disp_prop = 0;
3584 return eob;
3585 }
3586
3587 /* If the character at CHARPOS is where the display string begins,
3588 return CHARPOS. */
3589 pos = make_number (charpos);
3590 if (STRINGP (object))
3591 bufpos = string->bufpos;
3592 else
3593 bufpos = charpos;
3594 tpos = *position;
3595 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3596 && (charpos <= begb
3597 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3598 object),
3599 spec))
3600 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3601 frame_window_p)))
3602 {
3603 if (rv == 2)
3604 *disp_prop = 2;
3605 return charpos;
3606 }
3607
3608 /* Look forward for the first character with a `display' property
3609 that will replace the underlying text when displayed. */
3610 limpos = make_number (lim);
3611 do {
3612 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3613 CHARPOS (tpos) = XFASTINT (pos);
3614 if (CHARPOS (tpos) >= lim)
3615 {
3616 *disp_prop = 0;
3617 break;
3618 }
3619 if (STRINGP (object))
3620 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3621 else
3622 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3623 spec = Fget_char_property (pos, Qdisplay, object);
3624 if (!STRINGP (object))
3625 bufpos = CHARPOS (tpos);
3626 } while (NILP (spec)
3627 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3628 bufpos, frame_window_p)));
3629 if (rv == 2)
3630 *disp_prop = 2;
3631
3632 return CHARPOS (tpos);
3633 }
3634
3635 /* Return the character position of the end of the display string that
3636 started at CHARPOS. If there's no display string at CHARPOS,
3637 return -1. A display string is either an overlay with `display'
3638 property whose value is a string or a `display' text property whose
3639 value is a string. */
3640 ptrdiff_t
3641 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3642 {
3643 /* OBJECT = nil means current buffer. */
3644 Lisp_Object object =
3645 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3646 Lisp_Object pos = make_number (charpos);
3647 ptrdiff_t eob =
3648 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3649
3650 if (charpos >= eob || (string->s && !STRINGP (object)))
3651 return eob;
3652
3653 /* It could happen that the display property or overlay was removed
3654 since we found it in compute_display_string_pos above. One way
3655 this can happen is if JIT font-lock was called (through
3656 handle_fontified_prop), and jit-lock-functions remove text
3657 properties or overlays from the portion of buffer that includes
3658 CHARPOS. Muse mode is known to do that, for example. In this
3659 case, we return -1 to the caller, to signal that no display
3660 string is actually present at CHARPOS. See bidi_fetch_char for
3661 how this is handled.
3662
3663 An alternative would be to never look for display properties past
3664 it->stop_charpos. But neither compute_display_string_pos nor
3665 bidi_fetch_char that calls it know or care where the next
3666 stop_charpos is. */
3667 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3668 return -1;
3669
3670 /* Look forward for the first character where the `display' property
3671 changes. */
3672 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3673
3674 return XFASTINT (pos);
3675 }
3676
3677
3678 \f
3679 /***********************************************************************
3680 Fontification
3681 ***********************************************************************/
3682
3683 /* Handle changes in the `fontified' property of the current buffer by
3684 calling hook functions from Qfontification_functions to fontify
3685 regions of text. */
3686
3687 static enum prop_handled
3688 handle_fontified_prop (struct it *it)
3689 {
3690 Lisp_Object prop, pos;
3691 enum prop_handled handled = HANDLED_NORMALLY;
3692
3693 if (!NILP (Vmemory_full))
3694 return handled;
3695
3696 /* Get the value of the `fontified' property at IT's current buffer
3697 position. (The `fontified' property doesn't have a special
3698 meaning in strings.) If the value is nil, call functions from
3699 Qfontification_functions. */
3700 if (!STRINGP (it->string)
3701 && it->s == NULL
3702 && !NILP (Vfontification_functions)
3703 && !NILP (Vrun_hooks)
3704 && (pos = make_number (IT_CHARPOS (*it)),
3705 prop = Fget_char_property (pos, Qfontified, Qnil),
3706 /* Ignore the special cased nil value always present at EOB since
3707 no amount of fontifying will be able to change it. */
3708 NILP (prop) && IT_CHARPOS (*it) < Z))
3709 {
3710 ptrdiff_t count = SPECPDL_INDEX ();
3711 Lisp_Object val;
3712 struct buffer *obuf = current_buffer;
3713 ptrdiff_t begv = BEGV, zv = ZV;
3714 bool old_clip_changed = current_buffer->clip_changed;
3715
3716 val = Vfontification_functions;
3717 specbind (Qfontification_functions, Qnil);
3718
3719 eassert (it->end_charpos == ZV);
3720
3721 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3722 safe_call1 (val, pos);
3723 else
3724 {
3725 Lisp_Object fns, fn;
3726 struct gcpro gcpro1, gcpro2;
3727
3728 fns = Qnil;
3729 GCPRO2 (val, fns);
3730
3731 for (; CONSP (val); val = XCDR (val))
3732 {
3733 fn = XCAR (val);
3734
3735 if (EQ (fn, Qt))
3736 {
3737 /* A value of t indicates this hook has a local
3738 binding; it means to run the global binding too.
3739 In a global value, t should not occur. If it
3740 does, we must ignore it to avoid an endless
3741 loop. */
3742 for (fns = Fdefault_value (Qfontification_functions);
3743 CONSP (fns);
3744 fns = XCDR (fns))
3745 {
3746 fn = XCAR (fns);
3747 if (!EQ (fn, Qt))
3748 safe_call1 (fn, pos);
3749 }
3750 }
3751 else
3752 safe_call1 (fn, pos);
3753 }
3754
3755 UNGCPRO;
3756 }
3757
3758 unbind_to (count, Qnil);
3759
3760 /* Fontification functions routinely call `save-restriction'.
3761 Normally, this tags clip_changed, which can confuse redisplay
3762 (see discussion in Bug#6671). Since we don't perform any
3763 special handling of fontification changes in the case where
3764 `save-restriction' isn't called, there's no point doing so in
3765 this case either. So, if the buffer's restrictions are
3766 actually left unchanged, reset clip_changed. */
3767 if (obuf == current_buffer)
3768 {
3769 if (begv == BEGV && zv == ZV)
3770 current_buffer->clip_changed = old_clip_changed;
3771 }
3772 /* There isn't much we can reasonably do to protect against
3773 misbehaving fontification, but here's a fig leaf. */
3774 else if (BUFFER_LIVE_P (obuf))
3775 set_buffer_internal_1 (obuf);
3776
3777 /* The fontification code may have added/removed text.
3778 It could do even a lot worse, but let's at least protect against
3779 the most obvious case where only the text past `pos' gets changed',
3780 as is/was done in grep.el where some escapes sequences are turned
3781 into face properties (bug#7876). */
3782 it->end_charpos = ZV;
3783
3784 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3785 something. This avoids an endless loop if they failed to
3786 fontify the text for which reason ever. */
3787 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3788 handled = HANDLED_RECOMPUTE_PROPS;
3789 }
3790
3791 return handled;
3792 }
3793
3794
3795 \f
3796 /***********************************************************************
3797 Faces
3798 ***********************************************************************/
3799
3800 /* Set up iterator IT from face properties at its current position.
3801 Called from handle_stop. */
3802
3803 static enum prop_handled
3804 handle_face_prop (struct it *it)
3805 {
3806 int new_face_id;
3807 ptrdiff_t next_stop;
3808
3809 if (!STRINGP (it->string))
3810 {
3811 new_face_id
3812 = face_at_buffer_position (it->w,
3813 IT_CHARPOS (*it),
3814 &next_stop,
3815 (IT_CHARPOS (*it)
3816 + TEXT_PROP_DISTANCE_LIMIT),
3817 0, it->base_face_id);
3818
3819 /* Is this a start of a run of characters with box face?
3820 Caveat: this can be called for a freshly initialized
3821 iterator; face_id is -1 in this case. We know that the new
3822 face will not change until limit, i.e. if the new face has a
3823 box, all characters up to limit will have one. But, as
3824 usual, we don't know whether limit is really the end. */
3825 if (new_face_id != it->face_id)
3826 {
3827 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3828 /* If it->face_id is -1, old_face below will be NULL, see
3829 the definition of FACE_FROM_ID. This will happen if this
3830 is the initial call that gets the face. */
3831 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3832
3833 /* If the value of face_id of the iterator is -1, we have to
3834 look in front of IT's position and see whether there is a
3835 face there that's different from new_face_id. */
3836 if (!old_face && IT_CHARPOS (*it) > BEG)
3837 {
3838 int prev_face_id = face_before_it_pos (it);
3839
3840 old_face = FACE_FROM_ID (it->f, prev_face_id);
3841 }
3842
3843 /* If the new face has a box, but the old face does not,
3844 this is the start of a run of characters with box face,
3845 i.e. this character has a shadow on the left side. */
3846 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3847 && (old_face == NULL || !old_face->box));
3848 it->face_box_p = new_face->box != FACE_NO_BOX;
3849 }
3850 }
3851 else
3852 {
3853 int base_face_id;
3854 ptrdiff_t bufpos;
3855 int i;
3856 Lisp_Object from_overlay
3857 = (it->current.overlay_string_index >= 0
3858 ? it->string_overlays[it->current.overlay_string_index
3859 % OVERLAY_STRING_CHUNK_SIZE]
3860 : Qnil);
3861
3862 /* See if we got to this string directly or indirectly from
3863 an overlay property. That includes the before-string or
3864 after-string of an overlay, strings in display properties
3865 provided by an overlay, their text properties, etc.
3866
3867 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3868 if (! NILP (from_overlay))
3869 for (i = it->sp - 1; i >= 0; i--)
3870 {
3871 if (it->stack[i].current.overlay_string_index >= 0)
3872 from_overlay
3873 = it->string_overlays[it->stack[i].current.overlay_string_index
3874 % OVERLAY_STRING_CHUNK_SIZE];
3875 else if (! NILP (it->stack[i].from_overlay))
3876 from_overlay = it->stack[i].from_overlay;
3877
3878 if (!NILP (from_overlay))
3879 break;
3880 }
3881
3882 if (! NILP (from_overlay))
3883 {
3884 bufpos = IT_CHARPOS (*it);
3885 /* For a string from an overlay, the base face depends
3886 only on text properties and ignores overlays. */
3887 base_face_id
3888 = face_for_overlay_string (it->w,
3889 IT_CHARPOS (*it),
3890 &next_stop,
3891 (IT_CHARPOS (*it)
3892 + TEXT_PROP_DISTANCE_LIMIT),
3893 0,
3894 from_overlay);
3895 }
3896 else
3897 {
3898 bufpos = 0;
3899
3900 /* For strings from a `display' property, use the face at
3901 IT's current buffer position as the base face to merge
3902 with, so that overlay strings appear in the same face as
3903 surrounding text, unless they specify their own faces.
3904 For strings from wrap-prefix and line-prefix properties,
3905 use the default face, possibly remapped via
3906 Vface_remapping_alist. */
3907 base_face_id = it->string_from_prefix_prop_p
3908 ? (!NILP (Vface_remapping_alist)
3909 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3910 : DEFAULT_FACE_ID)
3911 : underlying_face_id (it);
3912 }
3913
3914 new_face_id = face_at_string_position (it->w,
3915 it->string,
3916 IT_STRING_CHARPOS (*it),
3917 bufpos,
3918 &next_stop,
3919 base_face_id, 0);
3920
3921 /* Is this a start of a run of characters with box? Caveat:
3922 this can be called for a freshly allocated iterator; face_id
3923 is -1 is this case. We know that the new face will not
3924 change until the next check pos, i.e. if the new face has a
3925 box, all characters up to that position will have a
3926 box. But, as usual, we don't know whether that position
3927 is really the end. */
3928 if (new_face_id != it->face_id)
3929 {
3930 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3931 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3932
3933 /* If new face has a box but old face hasn't, this is the
3934 start of a run of characters with box, i.e. it has a
3935 shadow on the left side. */
3936 it->start_of_box_run_p
3937 = new_face->box && (old_face == NULL || !old_face->box);
3938 it->face_box_p = new_face->box != FACE_NO_BOX;
3939 }
3940 }
3941
3942 it->face_id = new_face_id;
3943 return HANDLED_NORMALLY;
3944 }
3945
3946
3947 /* Return the ID of the face ``underlying'' IT's current position,
3948 which is in a string. If the iterator is associated with a
3949 buffer, return the face at IT's current buffer position.
3950 Otherwise, use the iterator's base_face_id. */
3951
3952 static int
3953 underlying_face_id (struct it *it)
3954 {
3955 int face_id = it->base_face_id, i;
3956
3957 eassert (STRINGP (it->string));
3958
3959 for (i = it->sp - 1; i >= 0; --i)
3960 if (NILP (it->stack[i].string))
3961 face_id = it->stack[i].face_id;
3962
3963 return face_id;
3964 }
3965
3966
3967 /* Compute the face one character before or after the current position
3968 of IT, in the visual order. BEFORE_P non-zero means get the face
3969 in front (to the left in L2R paragraphs, to the right in R2L
3970 paragraphs) of IT's screen position. Value is the ID of the face. */
3971
3972 static int
3973 face_before_or_after_it_pos (struct it *it, int before_p)
3974 {
3975 int face_id, limit;
3976 ptrdiff_t next_check_charpos;
3977 struct it it_copy;
3978 void *it_copy_data = NULL;
3979
3980 eassert (it->s == NULL);
3981
3982 if (STRINGP (it->string))
3983 {
3984 ptrdiff_t bufpos, charpos;
3985 int base_face_id;
3986
3987 /* No face change past the end of the string (for the case
3988 we are padding with spaces). No face change before the
3989 string start. */
3990 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3991 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3992 return it->face_id;
3993
3994 if (!it->bidi_p)
3995 {
3996 /* Set charpos to the position before or after IT's current
3997 position, in the logical order, which in the non-bidi
3998 case is the same as the visual order. */
3999 if (before_p)
4000 charpos = IT_STRING_CHARPOS (*it) - 1;
4001 else if (it->what == IT_COMPOSITION)
4002 /* For composition, we must check the character after the
4003 composition. */
4004 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4005 else
4006 charpos = IT_STRING_CHARPOS (*it) + 1;
4007 }
4008 else
4009 {
4010 if (before_p)
4011 {
4012 /* With bidi iteration, the character before the current
4013 in the visual order cannot be found by simple
4014 iteration, because "reverse" reordering is not
4015 supported. Instead, we need to use the move_it_*
4016 family of functions. */
4017 /* Ignore face changes before the first visible
4018 character on this display line. */
4019 if (it->current_x <= it->first_visible_x)
4020 return it->face_id;
4021 SAVE_IT (it_copy, *it, it_copy_data);
4022 /* Implementation note: Since move_it_in_display_line
4023 works in the iterator geometry, and thinks the first
4024 character is always the leftmost, even in R2L lines,
4025 we don't need to distinguish between the R2L and L2R
4026 cases here. */
4027 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4028 it_copy.current_x - 1, MOVE_TO_X);
4029 charpos = IT_STRING_CHARPOS (it_copy);
4030 RESTORE_IT (it, it, it_copy_data);
4031 }
4032 else
4033 {
4034 /* Set charpos to the string position of the character
4035 that comes after IT's current position in the visual
4036 order. */
4037 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4038
4039 it_copy = *it;
4040 while (n--)
4041 bidi_move_to_visually_next (&it_copy.bidi_it);
4042
4043 charpos = it_copy.bidi_it.charpos;
4044 }
4045 }
4046 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4047
4048 if (it->current.overlay_string_index >= 0)
4049 bufpos = IT_CHARPOS (*it);
4050 else
4051 bufpos = 0;
4052
4053 base_face_id = underlying_face_id (it);
4054
4055 /* Get the face for ASCII, or unibyte. */
4056 face_id = face_at_string_position (it->w,
4057 it->string,
4058 charpos,
4059 bufpos,
4060 &next_check_charpos,
4061 base_face_id, 0);
4062
4063 /* Correct the face for charsets different from ASCII. Do it
4064 for the multibyte case only. The face returned above is
4065 suitable for unibyte text if IT->string is unibyte. */
4066 if (STRING_MULTIBYTE (it->string))
4067 {
4068 struct text_pos pos1 = string_pos (charpos, it->string);
4069 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4070 int c, len;
4071 struct face *face = FACE_FROM_ID (it->f, face_id);
4072
4073 c = string_char_and_length (p, &len);
4074 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4075 }
4076 }
4077 else
4078 {
4079 struct text_pos pos;
4080
4081 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4082 || (IT_CHARPOS (*it) <= BEGV && before_p))
4083 return it->face_id;
4084
4085 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4086 pos = it->current.pos;
4087
4088 if (!it->bidi_p)
4089 {
4090 if (before_p)
4091 DEC_TEXT_POS (pos, it->multibyte_p);
4092 else
4093 {
4094 if (it->what == IT_COMPOSITION)
4095 {
4096 /* For composition, we must check the position after
4097 the composition. */
4098 pos.charpos += it->cmp_it.nchars;
4099 pos.bytepos += it->len;
4100 }
4101 else
4102 INC_TEXT_POS (pos, it->multibyte_p);
4103 }
4104 }
4105 else
4106 {
4107 if (before_p)
4108 {
4109 /* With bidi iteration, the character before the current
4110 in the visual order cannot be found by simple
4111 iteration, because "reverse" reordering is not
4112 supported. Instead, we need to use the move_it_*
4113 family of functions. */
4114 /* Ignore face changes before the first visible
4115 character on this display line. */
4116 if (it->current_x <= it->first_visible_x)
4117 return it->face_id;
4118 SAVE_IT (it_copy, *it, it_copy_data);
4119 /* Implementation note: Since move_it_in_display_line
4120 works in the iterator geometry, and thinks the first
4121 character is always the leftmost, even in R2L lines,
4122 we don't need to distinguish between the R2L and L2R
4123 cases here. */
4124 move_it_in_display_line (&it_copy, ZV,
4125 it_copy.current_x - 1, MOVE_TO_X);
4126 pos = it_copy.current.pos;
4127 RESTORE_IT (it, it, it_copy_data);
4128 }
4129 else
4130 {
4131 /* Set charpos to the buffer position of the character
4132 that comes after IT's current position in the visual
4133 order. */
4134 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4135
4136 it_copy = *it;
4137 while (n--)
4138 bidi_move_to_visually_next (&it_copy.bidi_it);
4139
4140 SET_TEXT_POS (pos,
4141 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4142 }
4143 }
4144 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4145
4146 /* Determine face for CHARSET_ASCII, or unibyte. */
4147 face_id = face_at_buffer_position (it->w,
4148 CHARPOS (pos),
4149 &next_check_charpos,
4150 limit, 0, -1);
4151
4152 /* Correct the face for charsets different from ASCII. Do it
4153 for the multibyte case only. The face returned above is
4154 suitable for unibyte text if current_buffer is unibyte. */
4155 if (it->multibyte_p)
4156 {
4157 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4158 struct face *face = FACE_FROM_ID (it->f, face_id);
4159 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4160 }
4161 }
4162
4163 return face_id;
4164 }
4165
4166
4167 \f
4168 /***********************************************************************
4169 Invisible text
4170 ***********************************************************************/
4171
4172 /* Set up iterator IT from invisible properties at its current
4173 position. Called from handle_stop. */
4174
4175 static enum prop_handled
4176 handle_invisible_prop (struct it *it)
4177 {
4178 enum prop_handled handled = HANDLED_NORMALLY;
4179 int invis_p;
4180 Lisp_Object prop;
4181
4182 if (STRINGP (it->string))
4183 {
4184 Lisp_Object end_charpos, limit, charpos;
4185
4186 /* Get the value of the invisible text property at the
4187 current position. Value will be nil if there is no such
4188 property. */
4189 charpos = make_number (IT_STRING_CHARPOS (*it));
4190 prop = Fget_text_property (charpos, Qinvisible, it->string);
4191 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4192
4193 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4194 {
4195 /* Record whether we have to display an ellipsis for the
4196 invisible text. */
4197 int display_ellipsis_p = (invis_p == 2);
4198 ptrdiff_t len, endpos;
4199
4200 handled = HANDLED_RECOMPUTE_PROPS;
4201
4202 /* Get the position at which the next visible text can be
4203 found in IT->string, if any. */
4204 endpos = len = SCHARS (it->string);
4205 XSETINT (limit, len);
4206 do
4207 {
4208 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4209 it->string, limit);
4210 if (INTEGERP (end_charpos))
4211 {
4212 endpos = XFASTINT (end_charpos);
4213 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4214 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4215 if (invis_p == 2)
4216 display_ellipsis_p = true;
4217 }
4218 }
4219 while (invis_p && endpos < len);
4220
4221 if (display_ellipsis_p)
4222 it->ellipsis_p = true;
4223
4224 if (endpos < len)
4225 {
4226 /* Text at END_CHARPOS is visible. Move IT there. */
4227 struct text_pos old;
4228 ptrdiff_t oldpos;
4229
4230 old = it->current.string_pos;
4231 oldpos = CHARPOS (old);
4232 if (it->bidi_p)
4233 {
4234 if (it->bidi_it.first_elt
4235 && it->bidi_it.charpos < SCHARS (it->string))
4236 bidi_paragraph_init (it->paragraph_embedding,
4237 &it->bidi_it, 1);
4238 /* Bidi-iterate out of the invisible text. */
4239 do
4240 {
4241 bidi_move_to_visually_next (&it->bidi_it);
4242 }
4243 while (oldpos <= it->bidi_it.charpos
4244 && it->bidi_it.charpos < endpos);
4245
4246 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4247 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4248 if (IT_CHARPOS (*it) >= endpos)
4249 it->prev_stop = endpos;
4250 }
4251 else
4252 {
4253 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4254 compute_string_pos (&it->current.string_pos, old, it->string);
4255 }
4256 }
4257 else
4258 {
4259 /* The rest of the string is invisible. If this is an
4260 overlay string, proceed with the next overlay string
4261 or whatever comes and return a character from there. */
4262 if (it->current.overlay_string_index >= 0
4263 && !display_ellipsis_p)
4264 {
4265 next_overlay_string (it);
4266 /* Don't check for overlay strings when we just
4267 finished processing them. */
4268 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4269 }
4270 else
4271 {
4272 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4273 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4274 }
4275 }
4276 }
4277 }
4278 else
4279 {
4280 ptrdiff_t newpos, next_stop, start_charpos, tem;
4281 Lisp_Object pos, overlay;
4282
4283 /* First of all, is there invisible text at this position? */
4284 tem = start_charpos = IT_CHARPOS (*it);
4285 pos = make_number (tem);
4286 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4287 &overlay);
4288 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4289
4290 /* If we are on invisible text, skip over it. */
4291 if (invis_p && start_charpos < it->end_charpos)
4292 {
4293 /* Record whether we have to display an ellipsis for the
4294 invisible text. */
4295 int display_ellipsis_p = invis_p == 2;
4296
4297 handled = HANDLED_RECOMPUTE_PROPS;
4298
4299 /* Loop skipping over invisible text. The loop is left at
4300 ZV or with IT on the first char being visible again. */
4301 do
4302 {
4303 /* Try to skip some invisible text. Return value is the
4304 position reached which can be equal to where we start
4305 if there is nothing invisible there. This skips both
4306 over invisible text properties and overlays with
4307 invisible property. */
4308 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4309
4310 /* If we skipped nothing at all we weren't at invisible
4311 text in the first place. If everything to the end of
4312 the buffer was skipped, end the loop. */
4313 if (newpos == tem || newpos >= ZV)
4314 invis_p = 0;
4315 else
4316 {
4317 /* We skipped some characters but not necessarily
4318 all there are. Check if we ended up on visible
4319 text. Fget_char_property returns the property of
4320 the char before the given position, i.e. if we
4321 get invis_p = 0, this means that the char at
4322 newpos is visible. */
4323 pos = make_number (newpos);
4324 prop = Fget_char_property (pos, Qinvisible, it->window);
4325 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4326 }
4327
4328 /* If we ended up on invisible text, proceed to
4329 skip starting with next_stop. */
4330 if (invis_p)
4331 tem = next_stop;
4332
4333 /* If there are adjacent invisible texts, don't lose the
4334 second one's ellipsis. */
4335 if (invis_p == 2)
4336 display_ellipsis_p = true;
4337 }
4338 while (invis_p);
4339
4340 /* The position newpos is now either ZV or on visible text. */
4341 if (it->bidi_p)
4342 {
4343 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4344 int on_newline
4345 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4346 int after_newline
4347 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4348
4349 /* If the invisible text ends on a newline or on a
4350 character after a newline, we can avoid the costly,
4351 character by character, bidi iteration to NEWPOS, and
4352 instead simply reseat the iterator there. That's
4353 because all bidi reordering information is tossed at
4354 the newline. This is a big win for modes that hide
4355 complete lines, like Outline, Org, etc. */
4356 if (on_newline || after_newline)
4357 {
4358 struct text_pos tpos;
4359 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4360
4361 SET_TEXT_POS (tpos, newpos, bpos);
4362 reseat_1 (it, tpos, 0);
4363 /* If we reseat on a newline/ZV, we need to prep the
4364 bidi iterator for advancing to the next character
4365 after the newline/EOB, keeping the current paragraph
4366 direction (so that PRODUCE_GLYPHS does TRT wrt
4367 prepending/appending glyphs to a glyph row). */
4368 if (on_newline)
4369 {
4370 it->bidi_it.first_elt = 0;
4371 it->bidi_it.paragraph_dir = pdir;
4372 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4373 it->bidi_it.nchars = 1;
4374 it->bidi_it.ch_len = 1;
4375 }
4376 }
4377 else /* Must use the slow method. */
4378 {
4379 /* With bidi iteration, the region of invisible text
4380 could start and/or end in the middle of a
4381 non-base embedding level. Therefore, we need to
4382 skip invisible text using the bidi iterator,
4383 starting at IT's current position, until we find
4384 ourselves outside of the invisible text.
4385 Skipping invisible text _after_ bidi iteration
4386 avoids affecting the visual order of the
4387 displayed text when invisible properties are
4388 added or removed. */
4389 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4390 {
4391 /* If we were `reseat'ed to a new paragraph,
4392 determine the paragraph base direction. We
4393 need to do it now because
4394 next_element_from_buffer may not have a
4395 chance to do it, if we are going to skip any
4396 text at the beginning, which resets the
4397 FIRST_ELT flag. */
4398 bidi_paragraph_init (it->paragraph_embedding,
4399 &it->bidi_it, 1);
4400 }
4401 do
4402 {
4403 bidi_move_to_visually_next (&it->bidi_it);
4404 }
4405 while (it->stop_charpos <= it->bidi_it.charpos
4406 && it->bidi_it.charpos < newpos);
4407 IT_CHARPOS (*it) = it->bidi_it.charpos;
4408 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4409 /* If we overstepped NEWPOS, record its position in
4410 the iterator, so that we skip invisible text if
4411 later the bidi iteration lands us in the
4412 invisible region again. */
4413 if (IT_CHARPOS (*it) >= newpos)
4414 it->prev_stop = newpos;
4415 }
4416 }
4417 else
4418 {
4419 IT_CHARPOS (*it) = newpos;
4420 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4421 }
4422
4423 /* If there are before-strings at the start of invisible
4424 text, and the text is invisible because of a text
4425 property, arrange to show before-strings because 20.x did
4426 it that way. (If the text is invisible because of an
4427 overlay property instead of a text property, this is
4428 already handled in the overlay code.) */
4429 if (NILP (overlay)
4430 && get_overlay_strings (it, it->stop_charpos))
4431 {
4432 handled = HANDLED_RECOMPUTE_PROPS;
4433 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4434 }
4435 else if (display_ellipsis_p)
4436 {
4437 /* Make sure that the glyphs of the ellipsis will get
4438 correct `charpos' values. If we would not update
4439 it->position here, the glyphs would belong to the
4440 last visible character _before_ the invisible
4441 text, which confuses `set_cursor_from_row'.
4442
4443 We use the last invisible position instead of the
4444 first because this way the cursor is always drawn on
4445 the first "." of the ellipsis, whenever PT is inside
4446 the invisible text. Otherwise the cursor would be
4447 placed _after_ the ellipsis when the point is after the
4448 first invisible character. */
4449 if (!STRINGP (it->object))
4450 {
4451 it->position.charpos = newpos - 1;
4452 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4453 }
4454 it->ellipsis_p = true;
4455 /* Let the ellipsis display before
4456 considering any properties of the following char.
4457 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4458 handled = HANDLED_RETURN;
4459 }
4460 }
4461 }
4462
4463 return handled;
4464 }
4465
4466
4467 /* Make iterator IT return `...' next.
4468 Replaces LEN characters from buffer. */
4469
4470 static void
4471 setup_for_ellipsis (struct it *it, int len)
4472 {
4473 /* Use the display table definition for `...'. Invalid glyphs
4474 will be handled by the method returning elements from dpvec. */
4475 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4476 {
4477 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4478 it->dpvec = v->contents;
4479 it->dpend = v->contents + v->header.size;
4480 }
4481 else
4482 {
4483 /* Default `...'. */
4484 it->dpvec = default_invis_vector;
4485 it->dpend = default_invis_vector + 3;
4486 }
4487
4488 it->dpvec_char_len = len;
4489 it->current.dpvec_index = 0;
4490 it->dpvec_face_id = -1;
4491
4492 /* Remember the current face id in case glyphs specify faces.
4493 IT's face is restored in set_iterator_to_next.
4494 saved_face_id was set to preceding char's face in handle_stop. */
4495 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4496 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4497
4498 it->method = GET_FROM_DISPLAY_VECTOR;
4499 it->ellipsis_p = true;
4500 }
4501
4502
4503 \f
4504 /***********************************************************************
4505 'display' property
4506 ***********************************************************************/
4507
4508 /* Set up iterator IT from `display' property at its current position.
4509 Called from handle_stop.
4510 We return HANDLED_RETURN if some part of the display property
4511 overrides the display of the buffer text itself.
4512 Otherwise we return HANDLED_NORMALLY. */
4513
4514 static enum prop_handled
4515 handle_display_prop (struct it *it)
4516 {
4517 Lisp_Object propval, object, overlay;
4518 struct text_pos *position;
4519 ptrdiff_t bufpos;
4520 /* Nonzero if some property replaces the display of the text itself. */
4521 int display_replaced_p = 0;
4522
4523 if (STRINGP (it->string))
4524 {
4525 object = it->string;
4526 position = &it->current.string_pos;
4527 bufpos = CHARPOS (it->current.pos);
4528 }
4529 else
4530 {
4531 XSETWINDOW (object, it->w);
4532 position = &it->current.pos;
4533 bufpos = CHARPOS (*position);
4534 }
4535
4536 /* Reset those iterator values set from display property values. */
4537 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4538 it->space_width = Qnil;
4539 it->font_height = Qnil;
4540 it->voffset = 0;
4541
4542 /* We don't support recursive `display' properties, i.e. string
4543 values that have a string `display' property, that have a string
4544 `display' property etc. */
4545 if (!it->string_from_display_prop_p)
4546 it->area = TEXT_AREA;
4547
4548 propval = get_char_property_and_overlay (make_number (position->charpos),
4549 Qdisplay, object, &overlay);
4550 if (NILP (propval))
4551 return HANDLED_NORMALLY;
4552 /* Now OVERLAY is the overlay that gave us this property, or nil
4553 if it was a text property. */
4554
4555 if (!STRINGP (it->string))
4556 object = it->w->contents;
4557
4558 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4559 position, bufpos,
4560 FRAME_WINDOW_P (it->f));
4561
4562 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4563 }
4564
4565 /* Subroutine of handle_display_prop. Returns non-zero if the display
4566 specification in SPEC is a replacing specification, i.e. it would
4567 replace the text covered by `display' property with something else,
4568 such as an image or a display string. If SPEC includes any kind or
4569 `(space ...) specification, the value is 2; this is used by
4570 compute_display_string_pos, which see.
4571
4572 See handle_single_display_spec for documentation of arguments.
4573 frame_window_p is non-zero if the window being redisplayed is on a
4574 GUI frame; this argument is used only if IT is NULL, see below.
4575
4576 IT can be NULL, if this is called by the bidi reordering code
4577 through compute_display_string_pos, which see. In that case, this
4578 function only examines SPEC, but does not otherwise "handle" it, in
4579 the sense that it doesn't set up members of IT from the display
4580 spec. */
4581 static int
4582 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4583 Lisp_Object overlay, struct text_pos *position,
4584 ptrdiff_t bufpos, int frame_window_p)
4585 {
4586 int replacing_p = 0;
4587 int rv;
4588
4589 if (CONSP (spec)
4590 /* Simple specifications. */
4591 && !EQ (XCAR (spec), Qimage)
4592 && !EQ (XCAR (spec), Qspace)
4593 && !EQ (XCAR (spec), Qwhen)
4594 && !EQ (XCAR (spec), Qslice)
4595 && !EQ (XCAR (spec), Qspace_width)
4596 && !EQ (XCAR (spec), Qheight)
4597 && !EQ (XCAR (spec), Qraise)
4598 /* Marginal area specifications. */
4599 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4600 && !EQ (XCAR (spec), Qleft_fringe)
4601 && !EQ (XCAR (spec), Qright_fringe)
4602 && !NILP (XCAR (spec)))
4603 {
4604 for (; CONSP (spec); spec = XCDR (spec))
4605 {
4606 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4607 overlay, position, bufpos,
4608 replacing_p, frame_window_p)))
4609 {
4610 replacing_p = rv;
4611 /* If some text in a string is replaced, `position' no
4612 longer points to the position of `object'. */
4613 if (!it || STRINGP (object))
4614 break;
4615 }
4616 }
4617 }
4618 else if (VECTORP (spec))
4619 {
4620 ptrdiff_t i;
4621 for (i = 0; i < ASIZE (spec); ++i)
4622 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4623 overlay, position, bufpos,
4624 replacing_p, frame_window_p)))
4625 {
4626 replacing_p = rv;
4627 /* If some text in a string is replaced, `position' no
4628 longer points to the position of `object'. */
4629 if (!it || STRINGP (object))
4630 break;
4631 }
4632 }
4633 else
4634 {
4635 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4636 position, bufpos, 0,
4637 frame_window_p)))
4638 replacing_p = rv;
4639 }
4640
4641 return replacing_p;
4642 }
4643
4644 /* Value is the position of the end of the `display' property starting
4645 at START_POS in OBJECT. */
4646
4647 static struct text_pos
4648 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4649 {
4650 Lisp_Object end;
4651 struct text_pos end_pos;
4652
4653 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4654 Qdisplay, object, Qnil);
4655 CHARPOS (end_pos) = XFASTINT (end);
4656 if (STRINGP (object))
4657 compute_string_pos (&end_pos, start_pos, it->string);
4658 else
4659 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4660
4661 return end_pos;
4662 }
4663
4664
4665 /* Set up IT from a single `display' property specification SPEC. OBJECT
4666 is the object in which the `display' property was found. *POSITION
4667 is the position in OBJECT at which the `display' property was found.
4668 BUFPOS is the buffer position of OBJECT (different from POSITION if
4669 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4670 previously saw a display specification which already replaced text
4671 display with something else, for example an image; we ignore such
4672 properties after the first one has been processed.
4673
4674 OVERLAY is the overlay this `display' property came from,
4675 or nil if it was a text property.
4676
4677 If SPEC is a `space' or `image' specification, and in some other
4678 cases too, set *POSITION to the position where the `display'
4679 property ends.
4680
4681 If IT is NULL, only examine the property specification in SPEC, but
4682 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4683 is intended to be displayed in a window on a GUI frame.
4684
4685 Value is non-zero if something was found which replaces the display
4686 of buffer or string text. */
4687
4688 static int
4689 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4690 Lisp_Object overlay, struct text_pos *position,
4691 ptrdiff_t bufpos, int display_replaced_p,
4692 int frame_window_p)
4693 {
4694 Lisp_Object form;
4695 Lisp_Object location, value;
4696 struct text_pos start_pos = *position;
4697 int valid_p;
4698
4699 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4700 If the result is non-nil, use VALUE instead of SPEC. */
4701 form = Qt;
4702 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4703 {
4704 spec = XCDR (spec);
4705 if (!CONSP (spec))
4706 return 0;
4707 form = XCAR (spec);
4708 spec = XCDR (spec);
4709 }
4710
4711 if (!NILP (form) && !EQ (form, Qt))
4712 {
4713 ptrdiff_t count = SPECPDL_INDEX ();
4714 struct gcpro gcpro1;
4715
4716 /* Bind `object' to the object having the `display' property, a
4717 buffer or string. Bind `position' to the position in the
4718 object where the property was found, and `buffer-position'
4719 to the current position in the buffer. */
4720
4721 if (NILP (object))
4722 XSETBUFFER (object, current_buffer);
4723 specbind (Qobject, object);
4724 specbind (Qposition, make_number (CHARPOS (*position)));
4725 specbind (Qbuffer_position, make_number (bufpos));
4726 GCPRO1 (form);
4727 form = safe_eval (form);
4728 UNGCPRO;
4729 unbind_to (count, Qnil);
4730 }
4731
4732 if (NILP (form))
4733 return 0;
4734
4735 /* Handle `(height HEIGHT)' specifications. */
4736 if (CONSP (spec)
4737 && EQ (XCAR (spec), Qheight)
4738 && CONSP (XCDR (spec)))
4739 {
4740 if (it)
4741 {
4742 if (!FRAME_WINDOW_P (it->f))
4743 return 0;
4744
4745 it->font_height = XCAR (XCDR (spec));
4746 if (!NILP (it->font_height))
4747 {
4748 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4749 int new_height = -1;
4750
4751 if (CONSP (it->font_height)
4752 && (EQ (XCAR (it->font_height), Qplus)
4753 || EQ (XCAR (it->font_height), Qminus))
4754 && CONSP (XCDR (it->font_height))
4755 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4756 {
4757 /* `(+ N)' or `(- N)' where N is an integer. */
4758 int steps = XINT (XCAR (XCDR (it->font_height)));
4759 if (EQ (XCAR (it->font_height), Qplus))
4760 steps = - steps;
4761 it->face_id = smaller_face (it->f, it->face_id, steps);
4762 }
4763 else if (FUNCTIONP (it->font_height))
4764 {
4765 /* Call function with current height as argument.
4766 Value is the new height. */
4767 Lisp_Object height;
4768 height = safe_call1 (it->font_height,
4769 face->lface[LFACE_HEIGHT_INDEX]);
4770 if (NUMBERP (height))
4771 new_height = XFLOATINT (height);
4772 }
4773 else if (NUMBERP (it->font_height))
4774 {
4775 /* Value is a multiple of the canonical char height. */
4776 struct face *f;
4777
4778 f = FACE_FROM_ID (it->f,
4779 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4780 new_height = (XFLOATINT (it->font_height)
4781 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4782 }
4783 else
4784 {
4785 /* Evaluate IT->font_height with `height' bound to the
4786 current specified height to get the new height. */
4787 ptrdiff_t count = SPECPDL_INDEX ();
4788
4789 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4790 value = safe_eval (it->font_height);
4791 unbind_to (count, Qnil);
4792
4793 if (NUMBERP (value))
4794 new_height = XFLOATINT (value);
4795 }
4796
4797 if (new_height > 0)
4798 it->face_id = face_with_height (it->f, it->face_id, new_height);
4799 }
4800 }
4801
4802 return 0;
4803 }
4804
4805 /* Handle `(space-width WIDTH)'. */
4806 if (CONSP (spec)
4807 && EQ (XCAR (spec), Qspace_width)
4808 && CONSP (XCDR (spec)))
4809 {
4810 if (it)
4811 {
4812 if (!FRAME_WINDOW_P (it->f))
4813 return 0;
4814
4815 value = XCAR (XCDR (spec));
4816 if (NUMBERP (value) && XFLOATINT (value) > 0)
4817 it->space_width = value;
4818 }
4819
4820 return 0;
4821 }
4822
4823 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4824 if (CONSP (spec)
4825 && EQ (XCAR (spec), Qslice))
4826 {
4827 Lisp_Object tem;
4828
4829 if (it)
4830 {
4831 if (!FRAME_WINDOW_P (it->f))
4832 return 0;
4833
4834 if (tem = XCDR (spec), CONSP (tem))
4835 {
4836 it->slice.x = XCAR (tem);
4837 if (tem = XCDR (tem), CONSP (tem))
4838 {
4839 it->slice.y = XCAR (tem);
4840 if (tem = XCDR (tem), CONSP (tem))
4841 {
4842 it->slice.width = XCAR (tem);
4843 if (tem = XCDR (tem), CONSP (tem))
4844 it->slice.height = XCAR (tem);
4845 }
4846 }
4847 }
4848 }
4849
4850 return 0;
4851 }
4852
4853 /* Handle `(raise FACTOR)'. */
4854 if (CONSP (spec)
4855 && EQ (XCAR (spec), Qraise)
4856 && CONSP (XCDR (spec)))
4857 {
4858 if (it)
4859 {
4860 if (!FRAME_WINDOW_P (it->f))
4861 return 0;
4862
4863 #ifdef HAVE_WINDOW_SYSTEM
4864 value = XCAR (XCDR (spec));
4865 if (NUMBERP (value))
4866 {
4867 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4868 it->voffset = - (XFLOATINT (value)
4869 * (FONT_HEIGHT (face->font)));
4870 }
4871 #endif /* HAVE_WINDOW_SYSTEM */
4872 }
4873
4874 return 0;
4875 }
4876
4877 /* Don't handle the other kinds of display specifications
4878 inside a string that we got from a `display' property. */
4879 if (it && it->string_from_display_prop_p)
4880 return 0;
4881
4882 /* Characters having this form of property are not displayed, so
4883 we have to find the end of the property. */
4884 if (it)
4885 {
4886 start_pos = *position;
4887 *position = display_prop_end (it, object, start_pos);
4888 }
4889 value = Qnil;
4890
4891 /* Stop the scan at that end position--we assume that all
4892 text properties change there. */
4893 if (it)
4894 it->stop_charpos = position->charpos;
4895
4896 /* Handle `(left-fringe BITMAP [FACE])'
4897 and `(right-fringe BITMAP [FACE])'. */
4898 if (CONSP (spec)
4899 && (EQ (XCAR (spec), Qleft_fringe)
4900 || EQ (XCAR (spec), Qright_fringe))
4901 && CONSP (XCDR (spec)))
4902 {
4903 int fringe_bitmap;
4904
4905 if (it)
4906 {
4907 if (!FRAME_WINDOW_P (it->f))
4908 /* If we return here, POSITION has been advanced
4909 across the text with this property. */
4910 {
4911 /* Synchronize the bidi iterator with POSITION. This is
4912 needed because we are not going to push the iterator
4913 on behalf of this display property, so there will be
4914 no pop_it call to do this synchronization for us. */
4915 if (it->bidi_p)
4916 {
4917 it->position = *position;
4918 iterate_out_of_display_property (it);
4919 *position = it->position;
4920 }
4921 return 1;
4922 }
4923 }
4924 else if (!frame_window_p)
4925 return 1;
4926
4927 #ifdef HAVE_WINDOW_SYSTEM
4928 value = XCAR (XCDR (spec));
4929 if (!SYMBOLP (value)
4930 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4931 /* If we return here, POSITION has been advanced
4932 across the text with this property. */
4933 {
4934 if (it && it->bidi_p)
4935 {
4936 it->position = *position;
4937 iterate_out_of_display_property (it);
4938 *position = it->position;
4939 }
4940 return 1;
4941 }
4942
4943 if (it)
4944 {
4945 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4946
4947 if (CONSP (XCDR (XCDR (spec))))
4948 {
4949 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4950 int face_id2 = lookup_derived_face (it->f, face_name,
4951 FRINGE_FACE_ID, 0);
4952 if (face_id2 >= 0)
4953 face_id = face_id2;
4954 }
4955
4956 /* Save current settings of IT so that we can restore them
4957 when we are finished with the glyph property value. */
4958 push_it (it, position);
4959
4960 it->area = TEXT_AREA;
4961 it->what = IT_IMAGE;
4962 it->image_id = -1; /* no image */
4963 it->position = start_pos;
4964 it->object = NILP (object) ? it->w->contents : object;
4965 it->method = GET_FROM_IMAGE;
4966 it->from_overlay = Qnil;
4967 it->face_id = face_id;
4968 it->from_disp_prop_p = true;
4969
4970 /* Say that we haven't consumed the characters with
4971 `display' property yet. The call to pop_it in
4972 set_iterator_to_next will clean this up. */
4973 *position = start_pos;
4974
4975 if (EQ (XCAR (spec), Qleft_fringe))
4976 {
4977 it->left_user_fringe_bitmap = fringe_bitmap;
4978 it->left_user_fringe_face_id = face_id;
4979 }
4980 else
4981 {
4982 it->right_user_fringe_bitmap = fringe_bitmap;
4983 it->right_user_fringe_face_id = face_id;
4984 }
4985 }
4986 #endif /* HAVE_WINDOW_SYSTEM */
4987 return 1;
4988 }
4989
4990 /* Prepare to handle `((margin left-margin) ...)',
4991 `((margin right-margin) ...)' and `((margin nil) ...)'
4992 prefixes for display specifications. */
4993 location = Qunbound;
4994 if (CONSP (spec) && CONSP (XCAR (spec)))
4995 {
4996 Lisp_Object tem;
4997
4998 value = XCDR (spec);
4999 if (CONSP (value))
5000 value = XCAR (value);
5001
5002 tem = XCAR (spec);
5003 if (EQ (XCAR (tem), Qmargin)
5004 && (tem = XCDR (tem),
5005 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5006 (NILP (tem)
5007 || EQ (tem, Qleft_margin)
5008 || EQ (tem, Qright_margin))))
5009 location = tem;
5010 }
5011
5012 if (EQ (location, Qunbound))
5013 {
5014 location = Qnil;
5015 value = spec;
5016 }
5017
5018 /* After this point, VALUE is the property after any
5019 margin prefix has been stripped. It must be a string,
5020 an image specification, or `(space ...)'.
5021
5022 LOCATION specifies where to display: `left-margin',
5023 `right-margin' or nil. */
5024
5025 valid_p = (STRINGP (value)
5026 #ifdef HAVE_WINDOW_SYSTEM
5027 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5028 && valid_image_p (value))
5029 #endif /* not HAVE_WINDOW_SYSTEM */
5030 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5031
5032 if (valid_p && !display_replaced_p)
5033 {
5034 int retval = 1;
5035
5036 if (!it)
5037 {
5038 /* Callers need to know whether the display spec is any kind
5039 of `(space ...)' spec that is about to affect text-area
5040 display. */
5041 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5042 retval = 2;
5043 return retval;
5044 }
5045
5046 /* Save current settings of IT so that we can restore them
5047 when we are finished with the glyph property value. */
5048 push_it (it, position);
5049 it->from_overlay = overlay;
5050 it->from_disp_prop_p = true;
5051
5052 if (NILP (location))
5053 it->area = TEXT_AREA;
5054 else if (EQ (location, Qleft_margin))
5055 it->area = LEFT_MARGIN_AREA;
5056 else
5057 it->area = RIGHT_MARGIN_AREA;
5058
5059 if (STRINGP (value))
5060 {
5061 it->string = value;
5062 it->multibyte_p = STRING_MULTIBYTE (it->string);
5063 it->current.overlay_string_index = -1;
5064 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5065 it->end_charpos = it->string_nchars = SCHARS (it->string);
5066 it->method = GET_FROM_STRING;
5067 it->stop_charpos = 0;
5068 it->prev_stop = 0;
5069 it->base_level_stop = 0;
5070 it->string_from_display_prop_p = true;
5071 /* Say that we haven't consumed the characters with
5072 `display' property yet. The call to pop_it in
5073 set_iterator_to_next will clean this up. */
5074 if (BUFFERP (object))
5075 *position = start_pos;
5076
5077 /* Force paragraph direction to be that of the parent
5078 object. If the parent object's paragraph direction is
5079 not yet determined, default to L2R. */
5080 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5081 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5082 else
5083 it->paragraph_embedding = L2R;
5084
5085 /* Set up the bidi iterator for this display string. */
5086 if (it->bidi_p)
5087 {
5088 it->bidi_it.string.lstring = it->string;
5089 it->bidi_it.string.s = NULL;
5090 it->bidi_it.string.schars = it->end_charpos;
5091 it->bidi_it.string.bufpos = bufpos;
5092 it->bidi_it.string.from_disp_str = 1;
5093 it->bidi_it.string.unibyte = !it->multibyte_p;
5094 it->bidi_it.w = it->w;
5095 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5096 }
5097 }
5098 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5099 {
5100 it->method = GET_FROM_STRETCH;
5101 it->object = value;
5102 *position = it->position = start_pos;
5103 retval = 1 + (it->area == TEXT_AREA);
5104 }
5105 #ifdef HAVE_WINDOW_SYSTEM
5106 else
5107 {
5108 it->what = IT_IMAGE;
5109 it->image_id = lookup_image (it->f, value);
5110 it->position = start_pos;
5111 it->object = NILP (object) ? it->w->contents : object;
5112 it->method = GET_FROM_IMAGE;
5113
5114 /* Say that we haven't consumed the characters with
5115 `display' property yet. The call to pop_it in
5116 set_iterator_to_next will clean this up. */
5117 *position = start_pos;
5118 }
5119 #endif /* HAVE_WINDOW_SYSTEM */
5120
5121 return retval;
5122 }
5123
5124 /* Invalid property or property not supported. Restore
5125 POSITION to what it was before. */
5126 *position = start_pos;
5127 return 0;
5128 }
5129
5130 /* Check if PROP is a display property value whose text should be
5131 treated as intangible. OVERLAY is the overlay from which PROP
5132 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5133 specify the buffer position covered by PROP. */
5134
5135 int
5136 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5137 ptrdiff_t charpos, ptrdiff_t bytepos)
5138 {
5139 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5140 struct text_pos position;
5141
5142 SET_TEXT_POS (position, charpos, bytepos);
5143 return handle_display_spec (NULL, prop, Qnil, overlay,
5144 &position, charpos, frame_window_p);
5145 }
5146
5147
5148 /* Return 1 if PROP is a display sub-property value containing STRING.
5149
5150 Implementation note: this and the following function are really
5151 special cases of handle_display_spec and
5152 handle_single_display_spec, and should ideally use the same code.
5153 Until they do, these two pairs must be consistent and must be
5154 modified in sync. */
5155
5156 static int
5157 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5158 {
5159 if (EQ (string, prop))
5160 return 1;
5161
5162 /* Skip over `when FORM'. */
5163 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5164 {
5165 prop = XCDR (prop);
5166 if (!CONSP (prop))
5167 return 0;
5168 /* Actually, the condition following `when' should be eval'ed,
5169 like handle_single_display_spec does, and we should return
5170 zero if it evaluates to nil. However, this function is
5171 called only when the buffer was already displayed and some
5172 glyph in the glyph matrix was found to come from a display
5173 string. Therefore, the condition was already evaluated, and
5174 the result was non-nil, otherwise the display string wouldn't
5175 have been displayed and we would have never been called for
5176 this property. Thus, we can skip the evaluation and assume
5177 its result is non-nil. */
5178 prop = XCDR (prop);
5179 }
5180
5181 if (CONSP (prop))
5182 /* Skip over `margin LOCATION'. */
5183 if (EQ (XCAR (prop), Qmargin))
5184 {
5185 prop = XCDR (prop);
5186 if (!CONSP (prop))
5187 return 0;
5188
5189 prop = XCDR (prop);
5190 if (!CONSP (prop))
5191 return 0;
5192 }
5193
5194 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5195 }
5196
5197
5198 /* Return 1 if STRING appears in the `display' property PROP. */
5199
5200 static int
5201 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5202 {
5203 if (CONSP (prop)
5204 && !EQ (XCAR (prop), Qwhen)
5205 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5206 {
5207 /* A list of sub-properties. */
5208 while (CONSP (prop))
5209 {
5210 if (single_display_spec_string_p (XCAR (prop), string))
5211 return 1;
5212 prop = XCDR (prop);
5213 }
5214 }
5215 else if (VECTORP (prop))
5216 {
5217 /* A vector of sub-properties. */
5218 ptrdiff_t i;
5219 for (i = 0; i < ASIZE (prop); ++i)
5220 if (single_display_spec_string_p (AREF (prop, i), string))
5221 return 1;
5222 }
5223 else
5224 return single_display_spec_string_p (prop, string);
5225
5226 return 0;
5227 }
5228
5229 /* Look for STRING in overlays and text properties in the current
5230 buffer, between character positions FROM and TO (excluding TO).
5231 BACK_P non-zero means look back (in this case, TO is supposed to be
5232 less than FROM).
5233 Value is the first character position where STRING was found, or
5234 zero if it wasn't found before hitting TO.
5235
5236 This function may only use code that doesn't eval because it is
5237 called asynchronously from note_mouse_highlight. */
5238
5239 static ptrdiff_t
5240 string_buffer_position_lim (Lisp_Object string,
5241 ptrdiff_t from, ptrdiff_t to, int back_p)
5242 {
5243 Lisp_Object limit, prop, pos;
5244 int found = 0;
5245
5246 pos = make_number (max (from, BEGV));
5247
5248 if (!back_p) /* looking forward */
5249 {
5250 limit = make_number (min (to, ZV));
5251 while (!found && !EQ (pos, limit))
5252 {
5253 prop = Fget_char_property (pos, Qdisplay, Qnil);
5254 if (!NILP (prop) && display_prop_string_p (prop, string))
5255 found = 1;
5256 else
5257 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5258 limit);
5259 }
5260 }
5261 else /* looking back */
5262 {
5263 limit = make_number (max (to, BEGV));
5264 while (!found && !EQ (pos, limit))
5265 {
5266 prop = Fget_char_property (pos, Qdisplay, Qnil);
5267 if (!NILP (prop) && display_prop_string_p (prop, string))
5268 found = 1;
5269 else
5270 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5271 limit);
5272 }
5273 }
5274
5275 return found ? XINT (pos) : 0;
5276 }
5277
5278 /* Determine which buffer position in current buffer STRING comes from.
5279 AROUND_CHARPOS is an approximate position where it could come from.
5280 Value is the buffer position or 0 if it couldn't be determined.
5281
5282 This function is necessary because we don't record buffer positions
5283 in glyphs generated from strings (to keep struct glyph small).
5284 This function may only use code that doesn't eval because it is
5285 called asynchronously from note_mouse_highlight. */
5286
5287 static ptrdiff_t
5288 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5289 {
5290 const int MAX_DISTANCE = 1000;
5291 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5292 around_charpos + MAX_DISTANCE,
5293 0);
5294
5295 if (!found)
5296 found = string_buffer_position_lim (string, around_charpos,
5297 around_charpos - MAX_DISTANCE, 1);
5298 return found;
5299 }
5300
5301
5302 \f
5303 /***********************************************************************
5304 `composition' property
5305 ***********************************************************************/
5306
5307 /* Set up iterator IT from `composition' property at its current
5308 position. Called from handle_stop. */
5309
5310 static enum prop_handled
5311 handle_composition_prop (struct it *it)
5312 {
5313 Lisp_Object prop, string;
5314 ptrdiff_t pos, pos_byte, start, end;
5315
5316 if (STRINGP (it->string))
5317 {
5318 unsigned char *s;
5319
5320 pos = IT_STRING_CHARPOS (*it);
5321 pos_byte = IT_STRING_BYTEPOS (*it);
5322 string = it->string;
5323 s = SDATA (string) + pos_byte;
5324 it->c = STRING_CHAR (s);
5325 }
5326 else
5327 {
5328 pos = IT_CHARPOS (*it);
5329 pos_byte = IT_BYTEPOS (*it);
5330 string = Qnil;
5331 it->c = FETCH_CHAR (pos_byte);
5332 }
5333
5334 /* If there's a valid composition and point is not inside of the
5335 composition (in the case that the composition is from the current
5336 buffer), draw a glyph composed from the composition components. */
5337 if (find_composition (pos, -1, &start, &end, &prop, string)
5338 && composition_valid_p (start, end, prop)
5339 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5340 {
5341 if (start < pos)
5342 /* As we can't handle this situation (perhaps font-lock added
5343 a new composition), we just return here hoping that next
5344 redisplay will detect this composition much earlier. */
5345 return HANDLED_NORMALLY;
5346 if (start != pos)
5347 {
5348 if (STRINGP (it->string))
5349 pos_byte = string_char_to_byte (it->string, start);
5350 else
5351 pos_byte = CHAR_TO_BYTE (start);
5352 }
5353 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5354 prop, string);
5355
5356 if (it->cmp_it.id >= 0)
5357 {
5358 it->cmp_it.ch = -1;
5359 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5360 it->cmp_it.nglyphs = -1;
5361 }
5362 }
5363
5364 return HANDLED_NORMALLY;
5365 }
5366
5367
5368 \f
5369 /***********************************************************************
5370 Overlay strings
5371 ***********************************************************************/
5372
5373 /* The following structure is used to record overlay strings for
5374 later sorting in load_overlay_strings. */
5375
5376 struct overlay_entry
5377 {
5378 Lisp_Object overlay;
5379 Lisp_Object string;
5380 EMACS_INT priority;
5381 int after_string_p;
5382 };
5383
5384
5385 /* Set up iterator IT from overlay strings at its current position.
5386 Called from handle_stop. */
5387
5388 static enum prop_handled
5389 handle_overlay_change (struct it *it)
5390 {
5391 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5392 return HANDLED_RECOMPUTE_PROPS;
5393 else
5394 return HANDLED_NORMALLY;
5395 }
5396
5397
5398 /* Set up the next overlay string for delivery by IT, if there is an
5399 overlay string to deliver. Called by set_iterator_to_next when the
5400 end of the current overlay string is reached. If there are more
5401 overlay strings to display, IT->string and
5402 IT->current.overlay_string_index are set appropriately here.
5403 Otherwise IT->string is set to nil. */
5404
5405 static void
5406 next_overlay_string (struct it *it)
5407 {
5408 ++it->current.overlay_string_index;
5409 if (it->current.overlay_string_index == it->n_overlay_strings)
5410 {
5411 /* No more overlay strings. Restore IT's settings to what
5412 they were before overlay strings were processed, and
5413 continue to deliver from current_buffer. */
5414
5415 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5416 pop_it (it);
5417 eassert (it->sp > 0
5418 || (NILP (it->string)
5419 && it->method == GET_FROM_BUFFER
5420 && it->stop_charpos >= BEGV
5421 && it->stop_charpos <= it->end_charpos));
5422 it->current.overlay_string_index = -1;
5423 it->n_overlay_strings = 0;
5424 it->overlay_strings_charpos = -1;
5425 /* If there's an empty display string on the stack, pop the
5426 stack, to resync the bidi iterator with IT's position. Such
5427 empty strings are pushed onto the stack in
5428 get_overlay_strings_1. */
5429 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5430 pop_it (it);
5431
5432 /* If we're at the end of the buffer, record that we have
5433 processed the overlay strings there already, so that
5434 next_element_from_buffer doesn't try it again. */
5435 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5436 it->overlay_strings_at_end_processed_p = true;
5437 }
5438 else
5439 {
5440 /* There are more overlay strings to process. If
5441 IT->current.overlay_string_index has advanced to a position
5442 where we must load IT->overlay_strings with more strings, do
5443 it. We must load at the IT->overlay_strings_charpos where
5444 IT->n_overlay_strings was originally computed; when invisible
5445 text is present, this might not be IT_CHARPOS (Bug#7016). */
5446 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5447
5448 if (it->current.overlay_string_index && i == 0)
5449 load_overlay_strings (it, it->overlay_strings_charpos);
5450
5451 /* Initialize IT to deliver display elements from the overlay
5452 string. */
5453 it->string = it->overlay_strings[i];
5454 it->multibyte_p = STRING_MULTIBYTE (it->string);
5455 SET_TEXT_POS (it->current.string_pos, 0, 0);
5456 it->method = GET_FROM_STRING;
5457 it->stop_charpos = 0;
5458 it->end_charpos = SCHARS (it->string);
5459 if (it->cmp_it.stop_pos >= 0)
5460 it->cmp_it.stop_pos = 0;
5461 it->prev_stop = 0;
5462 it->base_level_stop = 0;
5463
5464 /* Set up the bidi iterator for this overlay string. */
5465 if (it->bidi_p)
5466 {
5467 it->bidi_it.string.lstring = it->string;
5468 it->bidi_it.string.s = NULL;
5469 it->bidi_it.string.schars = SCHARS (it->string);
5470 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5471 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5472 it->bidi_it.string.unibyte = !it->multibyte_p;
5473 it->bidi_it.w = it->w;
5474 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5475 }
5476 }
5477
5478 CHECK_IT (it);
5479 }
5480
5481
5482 /* Compare two overlay_entry structures E1 and E2. Used as a
5483 comparison function for qsort in load_overlay_strings. Overlay
5484 strings for the same position are sorted so that
5485
5486 1. All after-strings come in front of before-strings, except
5487 when they come from the same overlay.
5488
5489 2. Within after-strings, strings are sorted so that overlay strings
5490 from overlays with higher priorities come first.
5491
5492 2. Within before-strings, strings are sorted so that overlay
5493 strings from overlays with higher priorities come last.
5494
5495 Value is analogous to strcmp. */
5496
5497
5498 static int
5499 compare_overlay_entries (const void *e1, const void *e2)
5500 {
5501 struct overlay_entry const *entry1 = e1;
5502 struct overlay_entry const *entry2 = e2;
5503 int result;
5504
5505 if (entry1->after_string_p != entry2->after_string_p)
5506 {
5507 /* Let after-strings appear in front of before-strings if
5508 they come from different overlays. */
5509 if (EQ (entry1->overlay, entry2->overlay))
5510 result = entry1->after_string_p ? 1 : -1;
5511 else
5512 result = entry1->after_string_p ? -1 : 1;
5513 }
5514 else if (entry1->priority != entry2->priority)
5515 {
5516 if (entry1->after_string_p)
5517 /* After-strings sorted in order of decreasing priority. */
5518 result = entry2->priority < entry1->priority ? -1 : 1;
5519 else
5520 /* Before-strings sorted in order of increasing priority. */
5521 result = entry1->priority < entry2->priority ? -1 : 1;
5522 }
5523 else
5524 result = 0;
5525
5526 return result;
5527 }
5528
5529
5530 /* Load the vector IT->overlay_strings with overlay strings from IT's
5531 current buffer position, or from CHARPOS if that is > 0. Set
5532 IT->n_overlays to the total number of overlay strings found.
5533
5534 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5535 a time. On entry into load_overlay_strings,
5536 IT->current.overlay_string_index gives the number of overlay
5537 strings that have already been loaded by previous calls to this
5538 function.
5539
5540 IT->add_overlay_start contains an additional overlay start
5541 position to consider for taking overlay strings from, if non-zero.
5542 This position comes into play when the overlay has an `invisible'
5543 property, and both before and after-strings. When we've skipped to
5544 the end of the overlay, because of its `invisible' property, we
5545 nevertheless want its before-string to appear.
5546 IT->add_overlay_start will contain the overlay start position
5547 in this case.
5548
5549 Overlay strings are sorted so that after-string strings come in
5550 front of before-string strings. Within before and after-strings,
5551 strings are sorted by overlay priority. See also function
5552 compare_overlay_entries. */
5553
5554 static void
5555 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5556 {
5557 Lisp_Object overlay, window, str, invisible;
5558 struct Lisp_Overlay *ov;
5559 ptrdiff_t start, end;
5560 ptrdiff_t size = 20;
5561 ptrdiff_t n = 0, i, j;
5562 int invis_p;
5563 struct overlay_entry *entries = alloca (size * sizeof *entries);
5564 USE_SAFE_ALLOCA;
5565
5566 if (charpos <= 0)
5567 charpos = IT_CHARPOS (*it);
5568
5569 /* Append the overlay string STRING of overlay OVERLAY to vector
5570 `entries' which has size `size' and currently contains `n'
5571 elements. AFTER_P non-zero means STRING is an after-string of
5572 OVERLAY. */
5573 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5574 do \
5575 { \
5576 Lisp_Object priority; \
5577 \
5578 if (n == size) \
5579 { \
5580 struct overlay_entry *old = entries; \
5581 SAFE_NALLOCA (entries, 2, size); \
5582 memcpy (entries, old, size * sizeof *entries); \
5583 size *= 2; \
5584 } \
5585 \
5586 entries[n].string = (STRING); \
5587 entries[n].overlay = (OVERLAY); \
5588 priority = Foverlay_get ((OVERLAY), Qpriority); \
5589 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5590 entries[n].after_string_p = (AFTER_P); \
5591 ++n; \
5592 } \
5593 while (0)
5594
5595 /* Process overlay before the overlay center. */
5596 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5597 {
5598 XSETMISC (overlay, ov);
5599 eassert (OVERLAYP (overlay));
5600 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5601 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5602
5603 if (end < charpos)
5604 break;
5605
5606 /* Skip this overlay if it doesn't start or end at IT's current
5607 position. */
5608 if (end != charpos && start != charpos)
5609 continue;
5610
5611 /* Skip this overlay if it doesn't apply to IT->w. */
5612 window = Foverlay_get (overlay, Qwindow);
5613 if (WINDOWP (window) && XWINDOW (window) != it->w)
5614 continue;
5615
5616 /* If the text ``under'' the overlay is invisible, both before-
5617 and after-strings from this overlay are visible; start and
5618 end position are indistinguishable. */
5619 invisible = Foverlay_get (overlay, Qinvisible);
5620 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5621
5622 /* If overlay has a non-empty before-string, record it. */
5623 if ((start == charpos || (end == charpos && invis_p))
5624 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5625 && SCHARS (str))
5626 RECORD_OVERLAY_STRING (overlay, str, 0);
5627
5628 /* If overlay has a non-empty after-string, record it. */
5629 if ((end == charpos || (start == charpos && invis_p))
5630 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5631 && SCHARS (str))
5632 RECORD_OVERLAY_STRING (overlay, str, 1);
5633 }
5634
5635 /* Process overlays after the overlay center. */
5636 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5637 {
5638 XSETMISC (overlay, ov);
5639 eassert (OVERLAYP (overlay));
5640 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5641 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5642
5643 if (start > charpos)
5644 break;
5645
5646 /* Skip this overlay if it doesn't start or end at IT's current
5647 position. */
5648 if (end != charpos && start != charpos)
5649 continue;
5650
5651 /* Skip this overlay if it doesn't apply to IT->w. */
5652 window = Foverlay_get (overlay, Qwindow);
5653 if (WINDOWP (window) && XWINDOW (window) != it->w)
5654 continue;
5655
5656 /* If the text ``under'' the overlay is invisible, it has a zero
5657 dimension, and both before- and after-strings apply. */
5658 invisible = Foverlay_get (overlay, Qinvisible);
5659 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5660
5661 /* If overlay has a non-empty before-string, record it. */
5662 if ((start == charpos || (end == charpos && invis_p))
5663 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5664 && SCHARS (str))
5665 RECORD_OVERLAY_STRING (overlay, str, 0);
5666
5667 /* If overlay has a non-empty after-string, record it. */
5668 if ((end == charpos || (start == charpos && invis_p))
5669 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5670 && SCHARS (str))
5671 RECORD_OVERLAY_STRING (overlay, str, 1);
5672 }
5673
5674 #undef RECORD_OVERLAY_STRING
5675
5676 /* Sort entries. */
5677 if (n > 1)
5678 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5679
5680 /* Record number of overlay strings, and where we computed it. */
5681 it->n_overlay_strings = n;
5682 it->overlay_strings_charpos = charpos;
5683
5684 /* IT->current.overlay_string_index is the number of overlay strings
5685 that have already been consumed by IT. Copy some of the
5686 remaining overlay strings to IT->overlay_strings. */
5687 i = 0;
5688 j = it->current.overlay_string_index;
5689 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5690 {
5691 it->overlay_strings[i] = entries[j].string;
5692 it->string_overlays[i++] = entries[j++].overlay;
5693 }
5694
5695 CHECK_IT (it);
5696 SAFE_FREE ();
5697 }
5698
5699
5700 /* Get the first chunk of overlay strings at IT's current buffer
5701 position, or at CHARPOS if that is > 0. Value is non-zero if at
5702 least one overlay string was found. */
5703
5704 static int
5705 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5706 {
5707 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5708 process. This fills IT->overlay_strings with strings, and sets
5709 IT->n_overlay_strings to the total number of strings to process.
5710 IT->pos.overlay_string_index has to be set temporarily to zero
5711 because load_overlay_strings needs this; it must be set to -1
5712 when no overlay strings are found because a zero value would
5713 indicate a position in the first overlay string. */
5714 it->current.overlay_string_index = 0;
5715 load_overlay_strings (it, charpos);
5716
5717 /* If we found overlay strings, set up IT to deliver display
5718 elements from the first one. Otherwise set up IT to deliver
5719 from current_buffer. */
5720 if (it->n_overlay_strings)
5721 {
5722 /* Make sure we know settings in current_buffer, so that we can
5723 restore meaningful values when we're done with the overlay
5724 strings. */
5725 if (compute_stop_p)
5726 compute_stop_pos (it);
5727 eassert (it->face_id >= 0);
5728
5729 /* Save IT's settings. They are restored after all overlay
5730 strings have been processed. */
5731 eassert (!compute_stop_p || it->sp == 0);
5732
5733 /* When called from handle_stop, there might be an empty display
5734 string loaded. In that case, don't bother saving it. But
5735 don't use this optimization with the bidi iterator, since we
5736 need the corresponding pop_it call to resync the bidi
5737 iterator's position with IT's position, after we are done
5738 with the overlay strings. (The corresponding call to pop_it
5739 in case of an empty display string is in
5740 next_overlay_string.) */
5741 if (!(!it->bidi_p
5742 && STRINGP (it->string) && !SCHARS (it->string)))
5743 push_it (it, NULL);
5744
5745 /* Set up IT to deliver display elements from the first overlay
5746 string. */
5747 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5748 it->string = it->overlay_strings[0];
5749 it->from_overlay = Qnil;
5750 it->stop_charpos = 0;
5751 eassert (STRINGP (it->string));
5752 it->end_charpos = SCHARS (it->string);
5753 it->prev_stop = 0;
5754 it->base_level_stop = 0;
5755 it->multibyte_p = STRING_MULTIBYTE (it->string);
5756 it->method = GET_FROM_STRING;
5757 it->from_disp_prop_p = 0;
5758
5759 /* Force paragraph direction to be that of the parent
5760 buffer. */
5761 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5762 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5763 else
5764 it->paragraph_embedding = L2R;
5765
5766 /* Set up the bidi iterator for this overlay string. */
5767 if (it->bidi_p)
5768 {
5769 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5770
5771 it->bidi_it.string.lstring = it->string;
5772 it->bidi_it.string.s = NULL;
5773 it->bidi_it.string.schars = SCHARS (it->string);
5774 it->bidi_it.string.bufpos = pos;
5775 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5776 it->bidi_it.string.unibyte = !it->multibyte_p;
5777 it->bidi_it.w = it->w;
5778 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5779 }
5780 return 1;
5781 }
5782
5783 it->current.overlay_string_index = -1;
5784 return 0;
5785 }
5786
5787 static int
5788 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5789 {
5790 it->string = Qnil;
5791 it->method = GET_FROM_BUFFER;
5792
5793 (void) get_overlay_strings_1 (it, charpos, 1);
5794
5795 CHECK_IT (it);
5796
5797 /* Value is non-zero if we found at least one overlay string. */
5798 return STRINGP (it->string);
5799 }
5800
5801
5802 \f
5803 /***********************************************************************
5804 Saving and restoring state
5805 ***********************************************************************/
5806
5807 /* Save current settings of IT on IT->stack. Called, for example,
5808 before setting up IT for an overlay string, to be able to restore
5809 IT's settings to what they were after the overlay string has been
5810 processed. If POSITION is non-NULL, it is the position to save on
5811 the stack instead of IT->position. */
5812
5813 static void
5814 push_it (struct it *it, struct text_pos *position)
5815 {
5816 struct iterator_stack_entry *p;
5817
5818 eassert (it->sp < IT_STACK_SIZE);
5819 p = it->stack + it->sp;
5820
5821 p->stop_charpos = it->stop_charpos;
5822 p->prev_stop = it->prev_stop;
5823 p->base_level_stop = it->base_level_stop;
5824 p->cmp_it = it->cmp_it;
5825 eassert (it->face_id >= 0);
5826 p->face_id = it->face_id;
5827 p->string = it->string;
5828 p->method = it->method;
5829 p->from_overlay = it->from_overlay;
5830 switch (p->method)
5831 {
5832 case GET_FROM_IMAGE:
5833 p->u.image.object = it->object;
5834 p->u.image.image_id = it->image_id;
5835 p->u.image.slice = it->slice;
5836 break;
5837 case GET_FROM_STRETCH:
5838 p->u.stretch.object = it->object;
5839 break;
5840 }
5841 p->position = position ? *position : it->position;
5842 p->current = it->current;
5843 p->end_charpos = it->end_charpos;
5844 p->string_nchars = it->string_nchars;
5845 p->area = it->area;
5846 p->multibyte_p = it->multibyte_p;
5847 p->avoid_cursor_p = it->avoid_cursor_p;
5848 p->space_width = it->space_width;
5849 p->font_height = it->font_height;
5850 p->voffset = it->voffset;
5851 p->string_from_display_prop_p = it->string_from_display_prop_p;
5852 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5853 p->display_ellipsis_p = 0;
5854 p->line_wrap = it->line_wrap;
5855 p->bidi_p = it->bidi_p;
5856 p->paragraph_embedding = it->paragraph_embedding;
5857 p->from_disp_prop_p = it->from_disp_prop_p;
5858 ++it->sp;
5859
5860 /* Save the state of the bidi iterator as well. */
5861 if (it->bidi_p)
5862 bidi_push_it (&it->bidi_it);
5863 }
5864
5865 static void
5866 iterate_out_of_display_property (struct it *it)
5867 {
5868 int buffer_p = !STRINGP (it->string);
5869 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5870 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5871
5872 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5873
5874 /* Maybe initialize paragraph direction. If we are at the beginning
5875 of a new paragraph, next_element_from_buffer may not have a
5876 chance to do that. */
5877 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5878 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5879 /* prev_stop can be zero, so check against BEGV as well. */
5880 while (it->bidi_it.charpos >= bob
5881 && it->prev_stop <= it->bidi_it.charpos
5882 && it->bidi_it.charpos < CHARPOS (it->position)
5883 && it->bidi_it.charpos < eob)
5884 bidi_move_to_visually_next (&it->bidi_it);
5885 /* Record the stop_pos we just crossed, for when we cross it
5886 back, maybe. */
5887 if (it->bidi_it.charpos > CHARPOS (it->position))
5888 it->prev_stop = CHARPOS (it->position);
5889 /* If we ended up not where pop_it put us, resync IT's
5890 positional members with the bidi iterator. */
5891 if (it->bidi_it.charpos != CHARPOS (it->position))
5892 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5893 if (buffer_p)
5894 it->current.pos = it->position;
5895 else
5896 it->current.string_pos = it->position;
5897 }
5898
5899 /* Restore IT's settings from IT->stack. Called, for example, when no
5900 more overlay strings must be processed, and we return to delivering
5901 display elements from a buffer, or when the end of a string from a
5902 `display' property is reached and we return to delivering display
5903 elements from an overlay string, or from a buffer. */
5904
5905 static void
5906 pop_it (struct it *it)
5907 {
5908 struct iterator_stack_entry *p;
5909 int from_display_prop = it->from_disp_prop_p;
5910
5911 eassert (it->sp > 0);
5912 --it->sp;
5913 p = it->stack + it->sp;
5914 it->stop_charpos = p->stop_charpos;
5915 it->prev_stop = p->prev_stop;
5916 it->base_level_stop = p->base_level_stop;
5917 it->cmp_it = p->cmp_it;
5918 it->face_id = p->face_id;
5919 it->current = p->current;
5920 it->position = p->position;
5921 it->string = p->string;
5922 it->from_overlay = p->from_overlay;
5923 if (NILP (it->string))
5924 SET_TEXT_POS (it->current.string_pos, -1, -1);
5925 it->method = p->method;
5926 switch (it->method)
5927 {
5928 case GET_FROM_IMAGE:
5929 it->image_id = p->u.image.image_id;
5930 it->object = p->u.image.object;
5931 it->slice = p->u.image.slice;
5932 break;
5933 case GET_FROM_STRETCH:
5934 it->object = p->u.stretch.object;
5935 break;
5936 case GET_FROM_BUFFER:
5937 it->object = it->w->contents;
5938 break;
5939 case GET_FROM_STRING:
5940 it->object = it->string;
5941 break;
5942 case GET_FROM_DISPLAY_VECTOR:
5943 if (it->s)
5944 it->method = GET_FROM_C_STRING;
5945 else if (STRINGP (it->string))
5946 it->method = GET_FROM_STRING;
5947 else
5948 {
5949 it->method = GET_FROM_BUFFER;
5950 it->object = it->w->contents;
5951 }
5952 }
5953 it->end_charpos = p->end_charpos;
5954 it->string_nchars = p->string_nchars;
5955 it->area = p->area;
5956 it->multibyte_p = p->multibyte_p;
5957 it->avoid_cursor_p = p->avoid_cursor_p;
5958 it->space_width = p->space_width;
5959 it->font_height = p->font_height;
5960 it->voffset = p->voffset;
5961 it->string_from_display_prop_p = p->string_from_display_prop_p;
5962 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5963 it->line_wrap = p->line_wrap;
5964 it->bidi_p = p->bidi_p;
5965 it->paragraph_embedding = p->paragraph_embedding;
5966 it->from_disp_prop_p = p->from_disp_prop_p;
5967 if (it->bidi_p)
5968 {
5969 bidi_pop_it (&it->bidi_it);
5970 /* Bidi-iterate until we get out of the portion of text, if any,
5971 covered by a `display' text property or by an overlay with
5972 `display' property. (We cannot just jump there, because the
5973 internal coherency of the bidi iterator state can not be
5974 preserved across such jumps.) We also must determine the
5975 paragraph base direction if the overlay we just processed is
5976 at the beginning of a new paragraph. */
5977 if (from_display_prop
5978 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5979 iterate_out_of_display_property (it);
5980
5981 eassert ((BUFFERP (it->object)
5982 && IT_CHARPOS (*it) == it->bidi_it.charpos
5983 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5984 || (STRINGP (it->object)
5985 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5986 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5987 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5988 }
5989 }
5990
5991
5992 \f
5993 /***********************************************************************
5994 Moving over lines
5995 ***********************************************************************/
5996
5997 /* Set IT's current position to the previous line start. */
5998
5999 static void
6000 back_to_previous_line_start (struct it *it)
6001 {
6002 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6003
6004 DEC_BOTH (cp, bp);
6005 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6006 }
6007
6008
6009 /* Move IT to the next line start.
6010
6011 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6012 we skipped over part of the text (as opposed to moving the iterator
6013 continuously over the text). Otherwise, don't change the value
6014 of *SKIPPED_P.
6015
6016 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6017 iterator on the newline, if it was found.
6018
6019 Newlines may come from buffer text, overlay strings, or strings
6020 displayed via the `display' property. That's the reason we can't
6021 simply use find_newline_no_quit.
6022
6023 Note that this function may not skip over invisible text that is so
6024 because of text properties and immediately follows a newline. If
6025 it would, function reseat_at_next_visible_line_start, when called
6026 from set_iterator_to_next, would effectively make invisible
6027 characters following a newline part of the wrong glyph row, which
6028 leads to wrong cursor motion. */
6029
6030 static int
6031 forward_to_next_line_start (struct it *it, int *skipped_p,
6032 struct bidi_it *bidi_it_prev)
6033 {
6034 ptrdiff_t old_selective;
6035 int newline_found_p, n;
6036 const int MAX_NEWLINE_DISTANCE = 500;
6037
6038 /* If already on a newline, just consume it to avoid unintended
6039 skipping over invisible text below. */
6040 if (it->what == IT_CHARACTER
6041 && it->c == '\n'
6042 && CHARPOS (it->position) == IT_CHARPOS (*it))
6043 {
6044 if (it->bidi_p && bidi_it_prev)
6045 *bidi_it_prev = it->bidi_it;
6046 set_iterator_to_next (it, 0);
6047 it->c = 0;
6048 return 1;
6049 }
6050
6051 /* Don't handle selective display in the following. It's (a)
6052 unnecessary because it's done by the caller, and (b) leads to an
6053 infinite recursion because next_element_from_ellipsis indirectly
6054 calls this function. */
6055 old_selective = it->selective;
6056 it->selective = 0;
6057
6058 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6059 from buffer text. */
6060 for (n = newline_found_p = 0;
6061 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6062 n += STRINGP (it->string) ? 0 : 1)
6063 {
6064 if (!get_next_display_element (it))
6065 return 0;
6066 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6067 if (newline_found_p && it->bidi_p && bidi_it_prev)
6068 *bidi_it_prev = it->bidi_it;
6069 set_iterator_to_next (it, 0);
6070 }
6071
6072 /* If we didn't find a newline near enough, see if we can use a
6073 short-cut. */
6074 if (!newline_found_p)
6075 {
6076 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6077 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6078 1, &bytepos);
6079 Lisp_Object pos;
6080
6081 eassert (!STRINGP (it->string));
6082
6083 /* If there isn't any `display' property in sight, and no
6084 overlays, we can just use the position of the newline in
6085 buffer text. */
6086 if (it->stop_charpos >= limit
6087 || ((pos = Fnext_single_property_change (make_number (start),
6088 Qdisplay, Qnil,
6089 make_number (limit)),
6090 NILP (pos))
6091 && next_overlay_change (start) == ZV))
6092 {
6093 if (!it->bidi_p)
6094 {
6095 IT_CHARPOS (*it) = limit;
6096 IT_BYTEPOS (*it) = bytepos;
6097 }
6098 else
6099 {
6100 struct bidi_it bprev;
6101
6102 /* Help bidi.c avoid expensive searches for display
6103 properties and overlays, by telling it that there are
6104 none up to `limit'. */
6105 if (it->bidi_it.disp_pos < limit)
6106 {
6107 it->bidi_it.disp_pos = limit;
6108 it->bidi_it.disp_prop = 0;
6109 }
6110 do {
6111 bprev = it->bidi_it;
6112 bidi_move_to_visually_next (&it->bidi_it);
6113 } while (it->bidi_it.charpos != limit);
6114 IT_CHARPOS (*it) = limit;
6115 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6116 if (bidi_it_prev)
6117 *bidi_it_prev = bprev;
6118 }
6119 *skipped_p = newline_found_p = true;
6120 }
6121 else
6122 {
6123 while (get_next_display_element (it)
6124 && !newline_found_p)
6125 {
6126 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6127 if (newline_found_p && it->bidi_p && bidi_it_prev)
6128 *bidi_it_prev = it->bidi_it;
6129 set_iterator_to_next (it, 0);
6130 }
6131 }
6132 }
6133
6134 it->selective = old_selective;
6135 return newline_found_p;
6136 }
6137
6138
6139 /* Set IT's current position to the previous visible line start. Skip
6140 invisible text that is so either due to text properties or due to
6141 selective display. Caution: this does not change IT->current_x and
6142 IT->hpos. */
6143
6144 static void
6145 back_to_previous_visible_line_start (struct it *it)
6146 {
6147 while (IT_CHARPOS (*it) > BEGV)
6148 {
6149 back_to_previous_line_start (it);
6150
6151 if (IT_CHARPOS (*it) <= BEGV)
6152 break;
6153
6154 /* If selective > 0, then lines indented more than its value are
6155 invisible. */
6156 if (it->selective > 0
6157 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6158 it->selective))
6159 continue;
6160
6161 /* Check the newline before point for invisibility. */
6162 {
6163 Lisp_Object prop;
6164 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6165 Qinvisible, it->window);
6166 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6167 continue;
6168 }
6169
6170 if (IT_CHARPOS (*it) <= BEGV)
6171 break;
6172
6173 {
6174 struct it it2;
6175 void *it2data = NULL;
6176 ptrdiff_t pos;
6177 ptrdiff_t beg, end;
6178 Lisp_Object val, overlay;
6179
6180 SAVE_IT (it2, *it, it2data);
6181
6182 /* If newline is part of a composition, continue from start of composition */
6183 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6184 && beg < IT_CHARPOS (*it))
6185 goto replaced;
6186
6187 /* If newline is replaced by a display property, find start of overlay
6188 or interval and continue search from that point. */
6189 pos = --IT_CHARPOS (it2);
6190 --IT_BYTEPOS (it2);
6191 it2.sp = 0;
6192 bidi_unshelve_cache (NULL, 0);
6193 it2.string_from_display_prop_p = 0;
6194 it2.from_disp_prop_p = 0;
6195 if (handle_display_prop (&it2) == HANDLED_RETURN
6196 && !NILP (val = get_char_property_and_overlay
6197 (make_number (pos), Qdisplay, Qnil, &overlay))
6198 && (OVERLAYP (overlay)
6199 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6200 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6201 {
6202 RESTORE_IT (it, it, it2data);
6203 goto replaced;
6204 }
6205
6206 /* Newline is not replaced by anything -- so we are done. */
6207 RESTORE_IT (it, it, it2data);
6208 break;
6209
6210 replaced:
6211 if (beg < BEGV)
6212 beg = BEGV;
6213 IT_CHARPOS (*it) = beg;
6214 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6215 }
6216 }
6217
6218 it->continuation_lines_width = 0;
6219
6220 eassert (IT_CHARPOS (*it) >= BEGV);
6221 eassert (IT_CHARPOS (*it) == BEGV
6222 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6223 CHECK_IT (it);
6224 }
6225
6226
6227 /* Reseat iterator IT at the previous visible line start. Skip
6228 invisible text that is so either due to text properties or due to
6229 selective display. At the end, update IT's overlay information,
6230 face information etc. */
6231
6232 void
6233 reseat_at_previous_visible_line_start (struct it *it)
6234 {
6235 back_to_previous_visible_line_start (it);
6236 reseat (it, it->current.pos, 1);
6237 CHECK_IT (it);
6238 }
6239
6240
6241 /* Reseat iterator IT on the next visible line start in the current
6242 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6243 preceding the line start. Skip over invisible text that is so
6244 because of selective display. Compute faces, overlays etc at the
6245 new position. Note that this function does not skip over text that
6246 is invisible because of text properties. */
6247
6248 static void
6249 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6250 {
6251 int newline_found_p, skipped_p = 0;
6252 struct bidi_it bidi_it_prev;
6253
6254 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6255
6256 /* Skip over lines that are invisible because they are indented
6257 more than the value of IT->selective. */
6258 if (it->selective > 0)
6259 while (IT_CHARPOS (*it) < ZV
6260 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6261 it->selective))
6262 {
6263 eassert (IT_BYTEPOS (*it) == BEGV
6264 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6265 newline_found_p =
6266 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6267 }
6268
6269 /* Position on the newline if that's what's requested. */
6270 if (on_newline_p && newline_found_p)
6271 {
6272 if (STRINGP (it->string))
6273 {
6274 if (IT_STRING_CHARPOS (*it) > 0)
6275 {
6276 if (!it->bidi_p)
6277 {
6278 --IT_STRING_CHARPOS (*it);
6279 --IT_STRING_BYTEPOS (*it);
6280 }
6281 else
6282 {
6283 /* We need to restore the bidi iterator to the state
6284 it had on the newline, and resync the IT's
6285 position with that. */
6286 it->bidi_it = bidi_it_prev;
6287 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6288 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6289 }
6290 }
6291 }
6292 else if (IT_CHARPOS (*it) > BEGV)
6293 {
6294 if (!it->bidi_p)
6295 {
6296 --IT_CHARPOS (*it);
6297 --IT_BYTEPOS (*it);
6298 }
6299 else
6300 {
6301 /* We need to restore the bidi iterator to the state it
6302 had on the newline and resync IT with that. */
6303 it->bidi_it = bidi_it_prev;
6304 IT_CHARPOS (*it) = it->bidi_it.charpos;
6305 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6306 }
6307 reseat (it, it->current.pos, 0);
6308 }
6309 }
6310 else if (skipped_p)
6311 reseat (it, it->current.pos, 0);
6312
6313 CHECK_IT (it);
6314 }
6315
6316
6317 \f
6318 /***********************************************************************
6319 Changing an iterator's position
6320 ***********************************************************************/
6321
6322 /* Change IT's current position to POS in current_buffer. If FORCE_P
6323 is non-zero, always check for text properties at the new position.
6324 Otherwise, text properties are only looked up if POS >=
6325 IT->check_charpos of a property. */
6326
6327 static void
6328 reseat (struct it *it, struct text_pos pos, int force_p)
6329 {
6330 ptrdiff_t original_pos = IT_CHARPOS (*it);
6331
6332 reseat_1 (it, pos, 0);
6333
6334 /* Determine where to check text properties. Avoid doing it
6335 where possible because text property lookup is very expensive. */
6336 if (force_p
6337 || CHARPOS (pos) > it->stop_charpos
6338 || CHARPOS (pos) < original_pos)
6339 {
6340 if (it->bidi_p)
6341 {
6342 /* For bidi iteration, we need to prime prev_stop and
6343 base_level_stop with our best estimations. */
6344 /* Implementation note: Of course, POS is not necessarily a
6345 stop position, so assigning prev_pos to it is a lie; we
6346 should have called compute_stop_backwards. However, if
6347 the current buffer does not include any R2L characters,
6348 that call would be a waste of cycles, because the
6349 iterator will never move back, and thus never cross this
6350 "fake" stop position. So we delay that backward search
6351 until the time we really need it, in next_element_from_buffer. */
6352 if (CHARPOS (pos) != it->prev_stop)
6353 it->prev_stop = CHARPOS (pos);
6354 if (CHARPOS (pos) < it->base_level_stop)
6355 it->base_level_stop = 0; /* meaning it's unknown */
6356 handle_stop (it);
6357 }
6358 else
6359 {
6360 handle_stop (it);
6361 it->prev_stop = it->base_level_stop = 0;
6362 }
6363
6364 }
6365
6366 CHECK_IT (it);
6367 }
6368
6369
6370 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6371 IT->stop_pos to POS, also. */
6372
6373 static void
6374 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6375 {
6376 /* Don't call this function when scanning a C string. */
6377 eassert (it->s == NULL);
6378
6379 /* POS must be a reasonable value. */
6380 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6381
6382 it->current.pos = it->position = pos;
6383 it->end_charpos = ZV;
6384 it->dpvec = NULL;
6385 it->current.dpvec_index = -1;
6386 it->current.overlay_string_index = -1;
6387 IT_STRING_CHARPOS (*it) = -1;
6388 IT_STRING_BYTEPOS (*it) = -1;
6389 it->string = Qnil;
6390 it->method = GET_FROM_BUFFER;
6391 it->object = it->w->contents;
6392 it->area = TEXT_AREA;
6393 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6394 it->sp = 0;
6395 it->string_from_display_prop_p = 0;
6396 it->string_from_prefix_prop_p = 0;
6397
6398 it->from_disp_prop_p = 0;
6399 it->face_before_selective_p = 0;
6400 if (it->bidi_p)
6401 {
6402 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6403 &it->bidi_it);
6404 bidi_unshelve_cache (NULL, 0);
6405 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6406 it->bidi_it.string.s = NULL;
6407 it->bidi_it.string.lstring = Qnil;
6408 it->bidi_it.string.bufpos = 0;
6409 it->bidi_it.string.unibyte = 0;
6410 it->bidi_it.w = it->w;
6411 }
6412
6413 if (set_stop_p)
6414 {
6415 it->stop_charpos = CHARPOS (pos);
6416 it->base_level_stop = CHARPOS (pos);
6417 }
6418 /* This make the information stored in it->cmp_it invalidate. */
6419 it->cmp_it.id = -1;
6420 }
6421
6422
6423 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6424 If S is non-null, it is a C string to iterate over. Otherwise,
6425 STRING gives a Lisp string to iterate over.
6426
6427 If PRECISION > 0, don't return more then PRECISION number of
6428 characters from the string.
6429
6430 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6431 characters have been returned. FIELD_WIDTH < 0 means an infinite
6432 field width.
6433
6434 MULTIBYTE = 0 means disable processing of multibyte characters,
6435 MULTIBYTE > 0 means enable it,
6436 MULTIBYTE < 0 means use IT->multibyte_p.
6437
6438 IT must be initialized via a prior call to init_iterator before
6439 calling this function. */
6440
6441 static void
6442 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6443 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6444 int multibyte)
6445 {
6446 /* No text property checks performed by default, but see below. */
6447 it->stop_charpos = -1;
6448
6449 /* Set iterator position and end position. */
6450 memset (&it->current, 0, sizeof it->current);
6451 it->current.overlay_string_index = -1;
6452 it->current.dpvec_index = -1;
6453 eassert (charpos >= 0);
6454
6455 /* If STRING is specified, use its multibyteness, otherwise use the
6456 setting of MULTIBYTE, if specified. */
6457 if (multibyte >= 0)
6458 it->multibyte_p = multibyte > 0;
6459
6460 /* Bidirectional reordering of strings is controlled by the default
6461 value of bidi-display-reordering. Don't try to reorder while
6462 loading loadup.el, as the necessary character property tables are
6463 not yet available. */
6464 it->bidi_p =
6465 NILP (Vpurify_flag)
6466 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6467
6468 if (s == NULL)
6469 {
6470 eassert (STRINGP (string));
6471 it->string = string;
6472 it->s = NULL;
6473 it->end_charpos = it->string_nchars = SCHARS (string);
6474 it->method = GET_FROM_STRING;
6475 it->current.string_pos = string_pos (charpos, string);
6476
6477 if (it->bidi_p)
6478 {
6479 it->bidi_it.string.lstring = string;
6480 it->bidi_it.string.s = NULL;
6481 it->bidi_it.string.schars = it->end_charpos;
6482 it->bidi_it.string.bufpos = 0;
6483 it->bidi_it.string.from_disp_str = 0;
6484 it->bidi_it.string.unibyte = !it->multibyte_p;
6485 it->bidi_it.w = it->w;
6486 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6487 FRAME_WINDOW_P (it->f), &it->bidi_it);
6488 }
6489 }
6490 else
6491 {
6492 it->s = (const unsigned char *) s;
6493 it->string = Qnil;
6494
6495 /* Note that we use IT->current.pos, not it->current.string_pos,
6496 for displaying C strings. */
6497 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6498 if (it->multibyte_p)
6499 {
6500 it->current.pos = c_string_pos (charpos, s, 1);
6501 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6502 }
6503 else
6504 {
6505 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6506 it->end_charpos = it->string_nchars = strlen (s);
6507 }
6508
6509 if (it->bidi_p)
6510 {
6511 it->bidi_it.string.lstring = Qnil;
6512 it->bidi_it.string.s = (const unsigned char *) s;
6513 it->bidi_it.string.schars = it->end_charpos;
6514 it->bidi_it.string.bufpos = 0;
6515 it->bidi_it.string.from_disp_str = 0;
6516 it->bidi_it.string.unibyte = !it->multibyte_p;
6517 it->bidi_it.w = it->w;
6518 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6519 &it->bidi_it);
6520 }
6521 it->method = GET_FROM_C_STRING;
6522 }
6523
6524 /* PRECISION > 0 means don't return more than PRECISION characters
6525 from the string. */
6526 if (precision > 0 && it->end_charpos - charpos > precision)
6527 {
6528 it->end_charpos = it->string_nchars = charpos + precision;
6529 if (it->bidi_p)
6530 it->bidi_it.string.schars = it->end_charpos;
6531 }
6532
6533 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6534 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6535 FIELD_WIDTH < 0 means infinite field width. This is useful for
6536 padding with `-' at the end of a mode line. */
6537 if (field_width < 0)
6538 field_width = INFINITY;
6539 /* Implementation note: We deliberately don't enlarge
6540 it->bidi_it.string.schars here to fit it->end_charpos, because
6541 the bidi iterator cannot produce characters out of thin air. */
6542 if (field_width > it->end_charpos - charpos)
6543 it->end_charpos = charpos + field_width;
6544
6545 /* Use the standard display table for displaying strings. */
6546 if (DISP_TABLE_P (Vstandard_display_table))
6547 it->dp = XCHAR_TABLE (Vstandard_display_table);
6548
6549 it->stop_charpos = charpos;
6550 it->prev_stop = charpos;
6551 it->base_level_stop = 0;
6552 if (it->bidi_p)
6553 {
6554 it->bidi_it.first_elt = 1;
6555 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6556 it->bidi_it.disp_pos = -1;
6557 }
6558 if (s == NULL && it->multibyte_p)
6559 {
6560 ptrdiff_t endpos = SCHARS (it->string);
6561 if (endpos > it->end_charpos)
6562 endpos = it->end_charpos;
6563 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6564 it->string);
6565 }
6566 CHECK_IT (it);
6567 }
6568
6569
6570 \f
6571 /***********************************************************************
6572 Iteration
6573 ***********************************************************************/
6574
6575 /* Map enum it_method value to corresponding next_element_from_* function. */
6576
6577 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6578 {
6579 next_element_from_buffer,
6580 next_element_from_display_vector,
6581 next_element_from_string,
6582 next_element_from_c_string,
6583 next_element_from_image,
6584 next_element_from_stretch
6585 };
6586
6587 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6588
6589
6590 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6591 (possibly with the following characters). */
6592
6593 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6594 ((IT)->cmp_it.id >= 0 \
6595 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6596 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6597 END_CHARPOS, (IT)->w, \
6598 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6599 (IT)->string)))
6600
6601
6602 /* Lookup the char-table Vglyphless_char_display for character C (-1
6603 if we want information for no-font case), and return the display
6604 method symbol. By side-effect, update it->what and
6605 it->glyphless_method. This function is called from
6606 get_next_display_element for each character element, and from
6607 x_produce_glyphs when no suitable font was found. */
6608
6609 Lisp_Object
6610 lookup_glyphless_char_display (int c, struct it *it)
6611 {
6612 Lisp_Object glyphless_method = Qnil;
6613
6614 if (CHAR_TABLE_P (Vglyphless_char_display)
6615 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6616 {
6617 if (c >= 0)
6618 {
6619 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6620 if (CONSP (glyphless_method))
6621 glyphless_method = FRAME_WINDOW_P (it->f)
6622 ? XCAR (glyphless_method)
6623 : XCDR (glyphless_method);
6624 }
6625 else
6626 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6627 }
6628
6629 retry:
6630 if (NILP (glyphless_method))
6631 {
6632 if (c >= 0)
6633 /* The default is to display the character by a proper font. */
6634 return Qnil;
6635 /* The default for the no-font case is to display an empty box. */
6636 glyphless_method = Qempty_box;
6637 }
6638 if (EQ (glyphless_method, Qzero_width))
6639 {
6640 if (c >= 0)
6641 return glyphless_method;
6642 /* This method can't be used for the no-font case. */
6643 glyphless_method = Qempty_box;
6644 }
6645 if (EQ (glyphless_method, Qthin_space))
6646 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6647 else if (EQ (glyphless_method, Qempty_box))
6648 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6649 else if (EQ (glyphless_method, Qhex_code))
6650 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6651 else if (STRINGP (glyphless_method))
6652 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6653 else
6654 {
6655 /* Invalid value. We use the default method. */
6656 glyphless_method = Qnil;
6657 goto retry;
6658 }
6659 it->what = IT_GLYPHLESS;
6660 return glyphless_method;
6661 }
6662
6663 /* Merge escape glyph face and cache the result. */
6664
6665 static struct frame *last_escape_glyph_frame = NULL;
6666 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6667 static int last_escape_glyph_merged_face_id = 0;
6668
6669 static int
6670 merge_escape_glyph_face (struct it *it)
6671 {
6672 int face_id;
6673
6674 if (it->f == last_escape_glyph_frame
6675 && it->face_id == last_escape_glyph_face_id)
6676 face_id = last_escape_glyph_merged_face_id;
6677 else
6678 {
6679 /* Merge the `escape-glyph' face into the current face. */
6680 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6681 last_escape_glyph_frame = it->f;
6682 last_escape_glyph_face_id = it->face_id;
6683 last_escape_glyph_merged_face_id = face_id;
6684 }
6685 return face_id;
6686 }
6687
6688 /* Likewise for glyphless glyph face. */
6689
6690 static struct frame *last_glyphless_glyph_frame = NULL;
6691 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6692 static int last_glyphless_glyph_merged_face_id = 0;
6693
6694 int
6695 merge_glyphless_glyph_face (struct it *it)
6696 {
6697 int face_id;
6698
6699 if (it->f == last_glyphless_glyph_frame
6700 && it->face_id == last_glyphless_glyph_face_id)
6701 face_id = last_glyphless_glyph_merged_face_id;
6702 else
6703 {
6704 /* Merge the `glyphless-char' face into the current face. */
6705 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6706 last_glyphless_glyph_frame = it->f;
6707 last_glyphless_glyph_face_id = it->face_id;
6708 last_glyphless_glyph_merged_face_id = face_id;
6709 }
6710 return face_id;
6711 }
6712
6713 /* Load IT's display element fields with information about the next
6714 display element from the current position of IT. Value is zero if
6715 end of buffer (or C string) is reached. */
6716
6717 static int
6718 get_next_display_element (struct it *it)
6719 {
6720 /* Non-zero means that we found a display element. Zero means that
6721 we hit the end of what we iterate over. Performance note: the
6722 function pointer `method' used here turns out to be faster than
6723 using a sequence of if-statements. */
6724 int success_p;
6725
6726 get_next:
6727 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6728
6729 if (it->what == IT_CHARACTER)
6730 {
6731 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6732 and only if (a) the resolved directionality of that character
6733 is R..." */
6734 /* FIXME: Do we need an exception for characters from display
6735 tables? */
6736 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6737 it->c = bidi_mirror_char (it->c);
6738 /* Map via display table or translate control characters.
6739 IT->c, IT->len etc. have been set to the next character by
6740 the function call above. If we have a display table, and it
6741 contains an entry for IT->c, translate it. Don't do this if
6742 IT->c itself comes from a display table, otherwise we could
6743 end up in an infinite recursion. (An alternative could be to
6744 count the recursion depth of this function and signal an
6745 error when a certain maximum depth is reached.) Is it worth
6746 it? */
6747 if (success_p && it->dpvec == NULL)
6748 {
6749 Lisp_Object dv;
6750 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6751 int nonascii_space_p = 0;
6752 int nonascii_hyphen_p = 0;
6753 int c = it->c; /* This is the character to display. */
6754
6755 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6756 {
6757 eassert (SINGLE_BYTE_CHAR_P (c));
6758 if (unibyte_display_via_language_environment)
6759 {
6760 c = DECODE_CHAR (unibyte, c);
6761 if (c < 0)
6762 c = BYTE8_TO_CHAR (it->c);
6763 }
6764 else
6765 c = BYTE8_TO_CHAR (it->c);
6766 }
6767
6768 if (it->dp
6769 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6770 VECTORP (dv)))
6771 {
6772 struct Lisp_Vector *v = XVECTOR (dv);
6773
6774 /* Return the first character from the display table
6775 entry, if not empty. If empty, don't display the
6776 current character. */
6777 if (v->header.size)
6778 {
6779 it->dpvec_char_len = it->len;
6780 it->dpvec = v->contents;
6781 it->dpend = v->contents + v->header.size;
6782 it->current.dpvec_index = 0;
6783 it->dpvec_face_id = -1;
6784 it->saved_face_id = it->face_id;
6785 it->method = GET_FROM_DISPLAY_VECTOR;
6786 it->ellipsis_p = 0;
6787 }
6788 else
6789 {
6790 set_iterator_to_next (it, 0);
6791 }
6792 goto get_next;
6793 }
6794
6795 if (! NILP (lookup_glyphless_char_display (c, it)))
6796 {
6797 if (it->what == IT_GLYPHLESS)
6798 goto done;
6799 /* Don't display this character. */
6800 set_iterator_to_next (it, 0);
6801 goto get_next;
6802 }
6803
6804 /* If `nobreak-char-display' is non-nil, we display
6805 non-ASCII spaces and hyphens specially. */
6806 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6807 {
6808 if (c == 0xA0)
6809 nonascii_space_p = true;
6810 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6811 nonascii_hyphen_p = true;
6812 }
6813
6814 /* Translate control characters into `\003' or `^C' form.
6815 Control characters coming from a display table entry are
6816 currently not translated because we use IT->dpvec to hold
6817 the translation. This could easily be changed but I
6818 don't believe that it is worth doing.
6819
6820 The characters handled by `nobreak-char-display' must be
6821 translated too.
6822
6823 Non-printable characters and raw-byte characters are also
6824 translated to octal form. */
6825 if (((c < ' ' || c == 127) /* ASCII control chars. */
6826 ? (it->area != TEXT_AREA
6827 /* In mode line, treat \n, \t like other crl chars. */
6828 || (c != '\t'
6829 && it->glyph_row
6830 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6831 || (c != '\n' && c != '\t'))
6832 : (nonascii_space_p
6833 || nonascii_hyphen_p
6834 || CHAR_BYTE8_P (c)
6835 || ! CHAR_PRINTABLE_P (c))))
6836 {
6837 /* C is a control character, non-ASCII space/hyphen,
6838 raw-byte, or a non-printable character which must be
6839 displayed either as '\003' or as `^C' where the '\\'
6840 and '^' can be defined in the display table. Fill
6841 IT->ctl_chars with glyphs for what we have to
6842 display. Then, set IT->dpvec to these glyphs. */
6843 Lisp_Object gc;
6844 int ctl_len;
6845 int face_id;
6846 int lface_id = 0;
6847 int escape_glyph;
6848
6849 /* Handle control characters with ^. */
6850
6851 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6852 {
6853 int g;
6854
6855 g = '^'; /* default glyph for Control */
6856 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6857 if (it->dp
6858 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6859 {
6860 g = GLYPH_CODE_CHAR (gc);
6861 lface_id = GLYPH_CODE_FACE (gc);
6862 }
6863
6864 face_id = (lface_id
6865 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6866 : merge_escape_glyph_face (it));
6867
6868 XSETINT (it->ctl_chars[0], g);
6869 XSETINT (it->ctl_chars[1], c ^ 0100);
6870 ctl_len = 2;
6871 goto display_control;
6872 }
6873
6874 /* Handle non-ascii space in the mode where it only gets
6875 highlighting. */
6876
6877 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6878 {
6879 /* Merge `nobreak-space' into the current face. */
6880 face_id = merge_faces (it->f, Qnobreak_space, 0,
6881 it->face_id);
6882 XSETINT (it->ctl_chars[0], ' ');
6883 ctl_len = 1;
6884 goto display_control;
6885 }
6886
6887 /* Handle sequences that start with the "escape glyph". */
6888
6889 /* the default escape glyph is \. */
6890 escape_glyph = '\\';
6891
6892 if (it->dp
6893 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6894 {
6895 escape_glyph = GLYPH_CODE_CHAR (gc);
6896 lface_id = GLYPH_CODE_FACE (gc);
6897 }
6898
6899 face_id = (lface_id
6900 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6901 : merge_escape_glyph_face (it));
6902
6903 /* Draw non-ASCII hyphen with just highlighting: */
6904
6905 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6906 {
6907 XSETINT (it->ctl_chars[0], '-');
6908 ctl_len = 1;
6909 goto display_control;
6910 }
6911
6912 /* Draw non-ASCII space/hyphen with escape glyph: */
6913
6914 if (nonascii_space_p || nonascii_hyphen_p)
6915 {
6916 XSETINT (it->ctl_chars[0], escape_glyph);
6917 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6918 ctl_len = 2;
6919 goto display_control;
6920 }
6921
6922 {
6923 char str[10];
6924 int len, i;
6925
6926 if (CHAR_BYTE8_P (c))
6927 /* Display \200 instead of \17777600. */
6928 c = CHAR_TO_BYTE8 (c);
6929 len = sprintf (str, "%03o", c);
6930
6931 XSETINT (it->ctl_chars[0], escape_glyph);
6932 for (i = 0; i < len; i++)
6933 XSETINT (it->ctl_chars[i + 1], str[i]);
6934 ctl_len = len + 1;
6935 }
6936
6937 display_control:
6938 /* Set up IT->dpvec and return first character from it. */
6939 it->dpvec_char_len = it->len;
6940 it->dpvec = it->ctl_chars;
6941 it->dpend = it->dpvec + ctl_len;
6942 it->current.dpvec_index = 0;
6943 it->dpvec_face_id = face_id;
6944 it->saved_face_id = it->face_id;
6945 it->method = GET_FROM_DISPLAY_VECTOR;
6946 it->ellipsis_p = 0;
6947 goto get_next;
6948 }
6949 it->char_to_display = c;
6950 }
6951 else if (success_p)
6952 {
6953 it->char_to_display = it->c;
6954 }
6955 }
6956
6957 #ifdef HAVE_WINDOW_SYSTEM
6958 /* Adjust face id for a multibyte character. There are no multibyte
6959 character in unibyte text. */
6960 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6961 && it->multibyte_p
6962 && success_p
6963 && FRAME_WINDOW_P (it->f))
6964 {
6965 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6966
6967 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6968 {
6969 /* Automatic composition with glyph-string. */
6970 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6971
6972 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6973 }
6974 else
6975 {
6976 ptrdiff_t pos = (it->s ? -1
6977 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6978 : IT_CHARPOS (*it));
6979 int c;
6980
6981 if (it->what == IT_CHARACTER)
6982 c = it->char_to_display;
6983 else
6984 {
6985 struct composition *cmp = composition_table[it->cmp_it.id];
6986 int i;
6987
6988 c = ' ';
6989 for (i = 0; i < cmp->glyph_len; i++)
6990 /* TAB in a composition means display glyphs with
6991 padding space on the left or right. */
6992 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6993 break;
6994 }
6995 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6996 }
6997 }
6998 #endif /* HAVE_WINDOW_SYSTEM */
6999
7000 done:
7001 /* Is this character the last one of a run of characters with
7002 box? If yes, set IT->end_of_box_run_p to 1. */
7003 if (it->face_box_p
7004 && it->s == NULL)
7005 {
7006 if (it->method == GET_FROM_STRING && it->sp)
7007 {
7008 int face_id = underlying_face_id (it);
7009 struct face *face = FACE_FROM_ID (it->f, face_id);
7010
7011 if (face)
7012 {
7013 if (face->box == FACE_NO_BOX)
7014 {
7015 /* If the box comes from face properties in a
7016 display string, check faces in that string. */
7017 int string_face_id = face_after_it_pos (it);
7018 it->end_of_box_run_p
7019 = (FACE_FROM_ID (it->f, string_face_id)->box
7020 == FACE_NO_BOX);
7021 }
7022 /* Otherwise, the box comes from the underlying face.
7023 If this is the last string character displayed, check
7024 the next buffer location. */
7025 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7026 && (it->current.overlay_string_index
7027 == it->n_overlay_strings - 1))
7028 {
7029 ptrdiff_t ignore;
7030 int next_face_id;
7031 struct text_pos pos = it->current.pos;
7032 INC_TEXT_POS (pos, it->multibyte_p);
7033
7034 next_face_id = face_at_buffer_position
7035 (it->w, CHARPOS (pos), &ignore,
7036 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7037 -1);
7038 it->end_of_box_run_p
7039 = (FACE_FROM_ID (it->f, next_face_id)->box
7040 == FACE_NO_BOX);
7041 }
7042 }
7043 }
7044 /* next_element_from_display_vector sets this flag according to
7045 faces of the display vector glyphs, see there. */
7046 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7047 {
7048 int face_id = face_after_it_pos (it);
7049 it->end_of_box_run_p
7050 = (face_id != it->face_id
7051 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7052 }
7053 }
7054 /* If we reached the end of the object we've been iterating (e.g., a
7055 display string or an overlay string), and there's something on
7056 IT->stack, proceed with what's on the stack. It doesn't make
7057 sense to return zero if there's unprocessed stuff on the stack,
7058 because otherwise that stuff will never be displayed. */
7059 if (!success_p && it->sp > 0)
7060 {
7061 set_iterator_to_next (it, 0);
7062 success_p = get_next_display_element (it);
7063 }
7064
7065 /* Value is 0 if end of buffer or string reached. */
7066 return success_p;
7067 }
7068
7069
7070 /* Move IT to the next display element.
7071
7072 RESEAT_P non-zero means if called on a newline in buffer text,
7073 skip to the next visible line start.
7074
7075 Functions get_next_display_element and set_iterator_to_next are
7076 separate because I find this arrangement easier to handle than a
7077 get_next_display_element function that also increments IT's
7078 position. The way it is we can first look at an iterator's current
7079 display element, decide whether it fits on a line, and if it does,
7080 increment the iterator position. The other way around we probably
7081 would either need a flag indicating whether the iterator has to be
7082 incremented the next time, or we would have to implement a
7083 decrement position function which would not be easy to write. */
7084
7085 void
7086 set_iterator_to_next (struct it *it, int reseat_p)
7087 {
7088 /* Reset flags indicating start and end of a sequence of characters
7089 with box. Reset them at the start of this function because
7090 moving the iterator to a new position might set them. */
7091 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7092
7093 switch (it->method)
7094 {
7095 case GET_FROM_BUFFER:
7096 /* The current display element of IT is a character from
7097 current_buffer. Advance in the buffer, and maybe skip over
7098 invisible lines that are so because of selective display. */
7099 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7100 reseat_at_next_visible_line_start (it, 0);
7101 else if (it->cmp_it.id >= 0)
7102 {
7103 /* We are currently getting glyphs from a composition. */
7104 int i;
7105
7106 if (! it->bidi_p)
7107 {
7108 IT_CHARPOS (*it) += it->cmp_it.nchars;
7109 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7110 if (it->cmp_it.to < it->cmp_it.nglyphs)
7111 {
7112 it->cmp_it.from = it->cmp_it.to;
7113 }
7114 else
7115 {
7116 it->cmp_it.id = -1;
7117 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7118 IT_BYTEPOS (*it),
7119 it->end_charpos, Qnil);
7120 }
7121 }
7122 else if (! it->cmp_it.reversed_p)
7123 {
7124 /* Composition created while scanning forward. */
7125 /* Update IT's char/byte positions to point to the first
7126 character of the next grapheme cluster, or to the
7127 character visually after the current composition. */
7128 for (i = 0; i < it->cmp_it.nchars; i++)
7129 bidi_move_to_visually_next (&it->bidi_it);
7130 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7131 IT_CHARPOS (*it) = it->bidi_it.charpos;
7132
7133 if (it->cmp_it.to < it->cmp_it.nglyphs)
7134 {
7135 /* Proceed to the next grapheme cluster. */
7136 it->cmp_it.from = it->cmp_it.to;
7137 }
7138 else
7139 {
7140 /* No more grapheme clusters in this composition.
7141 Find the next stop position. */
7142 ptrdiff_t stop = it->end_charpos;
7143 if (it->bidi_it.scan_dir < 0)
7144 /* Now we are scanning backward and don't know
7145 where to stop. */
7146 stop = -1;
7147 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7148 IT_BYTEPOS (*it), stop, Qnil);
7149 }
7150 }
7151 else
7152 {
7153 /* Composition created while scanning backward. */
7154 /* Update IT's char/byte positions to point to the last
7155 character of the previous grapheme cluster, or the
7156 character visually after the current composition. */
7157 for (i = 0; i < it->cmp_it.nchars; i++)
7158 bidi_move_to_visually_next (&it->bidi_it);
7159 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7160 IT_CHARPOS (*it) = it->bidi_it.charpos;
7161 if (it->cmp_it.from > 0)
7162 {
7163 /* Proceed to the previous grapheme cluster. */
7164 it->cmp_it.to = it->cmp_it.from;
7165 }
7166 else
7167 {
7168 /* No more grapheme clusters in this composition.
7169 Find the next stop position. */
7170 ptrdiff_t stop = it->end_charpos;
7171 if (it->bidi_it.scan_dir < 0)
7172 /* Now we are scanning backward and don't know
7173 where to stop. */
7174 stop = -1;
7175 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7176 IT_BYTEPOS (*it), stop, Qnil);
7177 }
7178 }
7179 }
7180 else
7181 {
7182 eassert (it->len != 0);
7183
7184 if (!it->bidi_p)
7185 {
7186 IT_BYTEPOS (*it) += it->len;
7187 IT_CHARPOS (*it) += 1;
7188 }
7189 else
7190 {
7191 int prev_scan_dir = it->bidi_it.scan_dir;
7192 /* If this is a new paragraph, determine its base
7193 direction (a.k.a. its base embedding level). */
7194 if (it->bidi_it.new_paragraph)
7195 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7196 bidi_move_to_visually_next (&it->bidi_it);
7197 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7198 IT_CHARPOS (*it) = it->bidi_it.charpos;
7199 if (prev_scan_dir != it->bidi_it.scan_dir)
7200 {
7201 /* As the scan direction was changed, we must
7202 re-compute the stop position for composition. */
7203 ptrdiff_t stop = it->end_charpos;
7204 if (it->bidi_it.scan_dir < 0)
7205 stop = -1;
7206 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7207 IT_BYTEPOS (*it), stop, Qnil);
7208 }
7209 }
7210 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7211 }
7212 break;
7213
7214 case GET_FROM_C_STRING:
7215 /* Current display element of IT is from a C string. */
7216 if (!it->bidi_p
7217 /* If the string position is beyond string's end, it means
7218 next_element_from_c_string is padding the string with
7219 blanks, in which case we bypass the bidi iterator,
7220 because it cannot deal with such virtual characters. */
7221 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7222 {
7223 IT_BYTEPOS (*it) += it->len;
7224 IT_CHARPOS (*it) += 1;
7225 }
7226 else
7227 {
7228 bidi_move_to_visually_next (&it->bidi_it);
7229 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7230 IT_CHARPOS (*it) = it->bidi_it.charpos;
7231 }
7232 break;
7233
7234 case GET_FROM_DISPLAY_VECTOR:
7235 /* Current display element of IT is from a display table entry.
7236 Advance in the display table definition. Reset it to null if
7237 end reached, and continue with characters from buffers/
7238 strings. */
7239 ++it->current.dpvec_index;
7240
7241 /* Restore face of the iterator to what they were before the
7242 display vector entry (these entries may contain faces). */
7243 it->face_id = it->saved_face_id;
7244
7245 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7246 {
7247 int recheck_faces = it->ellipsis_p;
7248
7249 if (it->s)
7250 it->method = GET_FROM_C_STRING;
7251 else if (STRINGP (it->string))
7252 it->method = GET_FROM_STRING;
7253 else
7254 {
7255 it->method = GET_FROM_BUFFER;
7256 it->object = it->w->contents;
7257 }
7258
7259 it->dpvec = NULL;
7260 it->current.dpvec_index = -1;
7261
7262 /* Skip over characters which were displayed via IT->dpvec. */
7263 if (it->dpvec_char_len < 0)
7264 reseat_at_next_visible_line_start (it, 1);
7265 else if (it->dpvec_char_len > 0)
7266 {
7267 if (it->method == GET_FROM_STRING
7268 && it->current.overlay_string_index >= 0
7269 && it->n_overlay_strings > 0)
7270 it->ignore_overlay_strings_at_pos_p = true;
7271 it->len = it->dpvec_char_len;
7272 set_iterator_to_next (it, reseat_p);
7273 }
7274
7275 /* Maybe recheck faces after display vector. */
7276 if (recheck_faces)
7277 it->stop_charpos = IT_CHARPOS (*it);
7278 }
7279 break;
7280
7281 case GET_FROM_STRING:
7282 /* Current display element is a character from a Lisp string. */
7283 eassert (it->s == NULL && STRINGP (it->string));
7284 /* Don't advance past string end. These conditions are true
7285 when set_iterator_to_next is called at the end of
7286 get_next_display_element, in which case the Lisp string is
7287 already exhausted, and all we want is pop the iterator
7288 stack. */
7289 if (it->current.overlay_string_index >= 0)
7290 {
7291 /* This is an overlay string, so there's no padding with
7292 spaces, and the number of characters in the string is
7293 where the string ends. */
7294 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7295 goto consider_string_end;
7296 }
7297 else
7298 {
7299 /* Not an overlay string. There could be padding, so test
7300 against it->end_charpos. */
7301 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7302 goto consider_string_end;
7303 }
7304 if (it->cmp_it.id >= 0)
7305 {
7306 int i;
7307
7308 if (! it->bidi_p)
7309 {
7310 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7311 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7312 if (it->cmp_it.to < it->cmp_it.nglyphs)
7313 it->cmp_it.from = it->cmp_it.to;
7314 else
7315 {
7316 it->cmp_it.id = -1;
7317 composition_compute_stop_pos (&it->cmp_it,
7318 IT_STRING_CHARPOS (*it),
7319 IT_STRING_BYTEPOS (*it),
7320 it->end_charpos, it->string);
7321 }
7322 }
7323 else if (! it->cmp_it.reversed_p)
7324 {
7325 for (i = 0; i < it->cmp_it.nchars; i++)
7326 bidi_move_to_visually_next (&it->bidi_it);
7327 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7328 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7329
7330 if (it->cmp_it.to < it->cmp_it.nglyphs)
7331 it->cmp_it.from = it->cmp_it.to;
7332 else
7333 {
7334 ptrdiff_t stop = it->end_charpos;
7335 if (it->bidi_it.scan_dir < 0)
7336 stop = -1;
7337 composition_compute_stop_pos (&it->cmp_it,
7338 IT_STRING_CHARPOS (*it),
7339 IT_STRING_BYTEPOS (*it), stop,
7340 it->string);
7341 }
7342 }
7343 else
7344 {
7345 for (i = 0; i < it->cmp_it.nchars; i++)
7346 bidi_move_to_visually_next (&it->bidi_it);
7347 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7348 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7349 if (it->cmp_it.from > 0)
7350 it->cmp_it.to = it->cmp_it.from;
7351 else
7352 {
7353 ptrdiff_t stop = it->end_charpos;
7354 if (it->bidi_it.scan_dir < 0)
7355 stop = -1;
7356 composition_compute_stop_pos (&it->cmp_it,
7357 IT_STRING_CHARPOS (*it),
7358 IT_STRING_BYTEPOS (*it), stop,
7359 it->string);
7360 }
7361 }
7362 }
7363 else
7364 {
7365 if (!it->bidi_p
7366 /* If the string position is beyond string's end, it
7367 means next_element_from_string is padding the string
7368 with blanks, in which case we bypass the bidi
7369 iterator, because it cannot deal with such virtual
7370 characters. */
7371 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7372 {
7373 IT_STRING_BYTEPOS (*it) += it->len;
7374 IT_STRING_CHARPOS (*it) += 1;
7375 }
7376 else
7377 {
7378 int prev_scan_dir = it->bidi_it.scan_dir;
7379
7380 bidi_move_to_visually_next (&it->bidi_it);
7381 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7382 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7383 if (prev_scan_dir != it->bidi_it.scan_dir)
7384 {
7385 ptrdiff_t stop = it->end_charpos;
7386
7387 if (it->bidi_it.scan_dir < 0)
7388 stop = -1;
7389 composition_compute_stop_pos (&it->cmp_it,
7390 IT_STRING_CHARPOS (*it),
7391 IT_STRING_BYTEPOS (*it), stop,
7392 it->string);
7393 }
7394 }
7395 }
7396
7397 consider_string_end:
7398
7399 if (it->current.overlay_string_index >= 0)
7400 {
7401 /* IT->string is an overlay string. Advance to the
7402 next, if there is one. */
7403 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7404 {
7405 it->ellipsis_p = 0;
7406 next_overlay_string (it);
7407 if (it->ellipsis_p)
7408 setup_for_ellipsis (it, 0);
7409 }
7410 }
7411 else
7412 {
7413 /* IT->string is not an overlay string. If we reached
7414 its end, and there is something on IT->stack, proceed
7415 with what is on the stack. This can be either another
7416 string, this time an overlay string, or a buffer. */
7417 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7418 && it->sp > 0)
7419 {
7420 pop_it (it);
7421 if (it->method == GET_FROM_STRING)
7422 goto consider_string_end;
7423 }
7424 }
7425 break;
7426
7427 case GET_FROM_IMAGE:
7428 case GET_FROM_STRETCH:
7429 /* The position etc with which we have to proceed are on
7430 the stack. The position may be at the end of a string,
7431 if the `display' property takes up the whole string. */
7432 eassert (it->sp > 0);
7433 pop_it (it);
7434 if (it->method == GET_FROM_STRING)
7435 goto consider_string_end;
7436 break;
7437
7438 default:
7439 /* There are no other methods defined, so this should be a bug. */
7440 emacs_abort ();
7441 }
7442
7443 eassert (it->method != GET_FROM_STRING
7444 || (STRINGP (it->string)
7445 && IT_STRING_CHARPOS (*it) >= 0));
7446 }
7447
7448 /* Load IT's display element fields with information about the next
7449 display element which comes from a display table entry or from the
7450 result of translating a control character to one of the forms `^C'
7451 or `\003'.
7452
7453 IT->dpvec holds the glyphs to return as characters.
7454 IT->saved_face_id holds the face id before the display vector--it
7455 is restored into IT->face_id in set_iterator_to_next. */
7456
7457 static int
7458 next_element_from_display_vector (struct it *it)
7459 {
7460 Lisp_Object gc;
7461 int prev_face_id = it->face_id;
7462 int next_face_id;
7463
7464 /* Precondition. */
7465 eassert (it->dpvec && it->current.dpvec_index >= 0);
7466
7467 it->face_id = it->saved_face_id;
7468
7469 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7470 That seemed totally bogus - so I changed it... */
7471 gc = it->dpvec[it->current.dpvec_index];
7472
7473 if (GLYPH_CODE_P (gc))
7474 {
7475 struct face *this_face, *prev_face, *next_face;
7476
7477 it->c = GLYPH_CODE_CHAR (gc);
7478 it->len = CHAR_BYTES (it->c);
7479
7480 /* The entry may contain a face id to use. Such a face id is
7481 the id of a Lisp face, not a realized face. A face id of
7482 zero means no face is specified. */
7483 if (it->dpvec_face_id >= 0)
7484 it->face_id = it->dpvec_face_id;
7485 else
7486 {
7487 int lface_id = GLYPH_CODE_FACE (gc);
7488 if (lface_id > 0)
7489 it->face_id = merge_faces (it->f, Qt, lface_id,
7490 it->saved_face_id);
7491 }
7492
7493 /* Glyphs in the display vector could have the box face, so we
7494 need to set the related flags in the iterator, as
7495 appropriate. */
7496 this_face = FACE_FROM_ID (it->f, it->face_id);
7497 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7498
7499 /* Is this character the first character of a box-face run? */
7500 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7501 && (!prev_face
7502 || prev_face->box == FACE_NO_BOX));
7503
7504 /* For the last character of the box-face run, we need to look
7505 either at the next glyph from the display vector, or at the
7506 face we saw before the display vector. */
7507 next_face_id = it->saved_face_id;
7508 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7509 {
7510 if (it->dpvec_face_id >= 0)
7511 next_face_id = it->dpvec_face_id;
7512 else
7513 {
7514 int lface_id =
7515 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7516
7517 if (lface_id > 0)
7518 next_face_id = merge_faces (it->f, Qt, lface_id,
7519 it->saved_face_id);
7520 }
7521 }
7522 next_face = FACE_FROM_ID (it->f, next_face_id);
7523 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7524 && (!next_face
7525 || next_face->box == FACE_NO_BOX));
7526 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7527 }
7528 else
7529 /* Display table entry is invalid. Return a space. */
7530 it->c = ' ', it->len = 1;
7531
7532 /* Don't change position and object of the iterator here. They are
7533 still the values of the character that had this display table
7534 entry or was translated, and that's what we want. */
7535 it->what = IT_CHARACTER;
7536 return 1;
7537 }
7538
7539 /* Get the first element of string/buffer in the visual order, after
7540 being reseated to a new position in a string or a buffer. */
7541 static void
7542 get_visually_first_element (struct it *it)
7543 {
7544 int string_p = STRINGP (it->string) || it->s;
7545 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7546 ptrdiff_t bob = (string_p ? 0 : BEGV);
7547
7548 if (STRINGP (it->string))
7549 {
7550 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7551 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7552 }
7553 else
7554 {
7555 it->bidi_it.charpos = IT_CHARPOS (*it);
7556 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7557 }
7558
7559 if (it->bidi_it.charpos == eob)
7560 {
7561 /* Nothing to do, but reset the FIRST_ELT flag, like
7562 bidi_paragraph_init does, because we are not going to
7563 call it. */
7564 it->bidi_it.first_elt = 0;
7565 }
7566 else if (it->bidi_it.charpos == bob
7567 || (!string_p
7568 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7569 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7570 {
7571 /* If we are at the beginning of a line/string, we can produce
7572 the next element right away. */
7573 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7574 bidi_move_to_visually_next (&it->bidi_it);
7575 }
7576 else
7577 {
7578 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7579
7580 /* We need to prime the bidi iterator starting at the line's or
7581 string's beginning, before we will be able to produce the
7582 next element. */
7583 if (string_p)
7584 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7585 else
7586 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7587 IT_BYTEPOS (*it), -1,
7588 &it->bidi_it.bytepos);
7589 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7590 do
7591 {
7592 /* Now return to buffer/string position where we were asked
7593 to get the next display element, and produce that. */
7594 bidi_move_to_visually_next (&it->bidi_it);
7595 }
7596 while (it->bidi_it.bytepos != orig_bytepos
7597 && it->bidi_it.charpos < eob);
7598 }
7599
7600 /* Adjust IT's position information to where we ended up. */
7601 if (STRINGP (it->string))
7602 {
7603 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7604 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7605 }
7606 else
7607 {
7608 IT_CHARPOS (*it) = it->bidi_it.charpos;
7609 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7610 }
7611
7612 if (STRINGP (it->string) || !it->s)
7613 {
7614 ptrdiff_t stop, charpos, bytepos;
7615
7616 if (STRINGP (it->string))
7617 {
7618 eassert (!it->s);
7619 stop = SCHARS (it->string);
7620 if (stop > it->end_charpos)
7621 stop = it->end_charpos;
7622 charpos = IT_STRING_CHARPOS (*it);
7623 bytepos = IT_STRING_BYTEPOS (*it);
7624 }
7625 else
7626 {
7627 stop = it->end_charpos;
7628 charpos = IT_CHARPOS (*it);
7629 bytepos = IT_BYTEPOS (*it);
7630 }
7631 if (it->bidi_it.scan_dir < 0)
7632 stop = -1;
7633 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7634 it->string);
7635 }
7636 }
7637
7638 /* Load IT with the next display element from Lisp string IT->string.
7639 IT->current.string_pos is the current position within the string.
7640 If IT->current.overlay_string_index >= 0, the Lisp string is an
7641 overlay string. */
7642
7643 static int
7644 next_element_from_string (struct it *it)
7645 {
7646 struct text_pos position;
7647
7648 eassert (STRINGP (it->string));
7649 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7650 eassert (IT_STRING_CHARPOS (*it) >= 0);
7651 position = it->current.string_pos;
7652
7653 /* With bidi reordering, the character to display might not be the
7654 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7655 that we were reseat()ed to a new string, whose paragraph
7656 direction is not known. */
7657 if (it->bidi_p && it->bidi_it.first_elt)
7658 {
7659 get_visually_first_element (it);
7660 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7661 }
7662
7663 /* Time to check for invisible text? */
7664 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7665 {
7666 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7667 {
7668 if (!(!it->bidi_p
7669 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7670 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7671 {
7672 /* With bidi non-linear iteration, we could find
7673 ourselves far beyond the last computed stop_charpos,
7674 with several other stop positions in between that we
7675 missed. Scan them all now, in buffer's logical
7676 order, until we find and handle the last stop_charpos
7677 that precedes our current position. */
7678 handle_stop_backwards (it, it->stop_charpos);
7679 return GET_NEXT_DISPLAY_ELEMENT (it);
7680 }
7681 else
7682 {
7683 if (it->bidi_p)
7684 {
7685 /* Take note of the stop position we just moved
7686 across, for when we will move back across it. */
7687 it->prev_stop = it->stop_charpos;
7688 /* If we are at base paragraph embedding level, take
7689 note of the last stop position seen at this
7690 level. */
7691 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7692 it->base_level_stop = it->stop_charpos;
7693 }
7694 handle_stop (it);
7695
7696 /* Since a handler may have changed IT->method, we must
7697 recurse here. */
7698 return GET_NEXT_DISPLAY_ELEMENT (it);
7699 }
7700 }
7701 else if (it->bidi_p
7702 /* If we are before prev_stop, we may have overstepped
7703 on our way backwards a stop_pos, and if so, we need
7704 to handle that stop_pos. */
7705 && IT_STRING_CHARPOS (*it) < it->prev_stop
7706 /* We can sometimes back up for reasons that have nothing
7707 to do with bidi reordering. E.g., compositions. The
7708 code below is only needed when we are above the base
7709 embedding level, so test for that explicitly. */
7710 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7711 {
7712 /* If we lost track of base_level_stop, we have no better
7713 place for handle_stop_backwards to start from than string
7714 beginning. This happens, e.g., when we were reseated to
7715 the previous screenful of text by vertical-motion. */
7716 if (it->base_level_stop <= 0
7717 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7718 it->base_level_stop = 0;
7719 handle_stop_backwards (it, it->base_level_stop);
7720 return GET_NEXT_DISPLAY_ELEMENT (it);
7721 }
7722 }
7723
7724 if (it->current.overlay_string_index >= 0)
7725 {
7726 /* Get the next character from an overlay string. In overlay
7727 strings, there is no field width or padding with spaces to
7728 do. */
7729 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7730 {
7731 it->what = IT_EOB;
7732 return 0;
7733 }
7734 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7735 IT_STRING_BYTEPOS (*it),
7736 it->bidi_it.scan_dir < 0
7737 ? -1
7738 : SCHARS (it->string))
7739 && next_element_from_composition (it))
7740 {
7741 return 1;
7742 }
7743 else if (STRING_MULTIBYTE (it->string))
7744 {
7745 const unsigned char *s = (SDATA (it->string)
7746 + IT_STRING_BYTEPOS (*it));
7747 it->c = string_char_and_length (s, &it->len);
7748 }
7749 else
7750 {
7751 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7752 it->len = 1;
7753 }
7754 }
7755 else
7756 {
7757 /* Get the next character from a Lisp string that is not an
7758 overlay string. Such strings come from the mode line, for
7759 example. We may have to pad with spaces, or truncate the
7760 string. See also next_element_from_c_string. */
7761 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7762 {
7763 it->what = IT_EOB;
7764 return 0;
7765 }
7766 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7767 {
7768 /* Pad with spaces. */
7769 it->c = ' ', it->len = 1;
7770 CHARPOS (position) = BYTEPOS (position) = -1;
7771 }
7772 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7773 IT_STRING_BYTEPOS (*it),
7774 it->bidi_it.scan_dir < 0
7775 ? -1
7776 : it->string_nchars)
7777 && next_element_from_composition (it))
7778 {
7779 return 1;
7780 }
7781 else if (STRING_MULTIBYTE (it->string))
7782 {
7783 const unsigned char *s = (SDATA (it->string)
7784 + IT_STRING_BYTEPOS (*it));
7785 it->c = string_char_and_length (s, &it->len);
7786 }
7787 else
7788 {
7789 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7790 it->len = 1;
7791 }
7792 }
7793
7794 /* Record what we have and where it came from. */
7795 it->what = IT_CHARACTER;
7796 it->object = it->string;
7797 it->position = position;
7798 return 1;
7799 }
7800
7801
7802 /* Load IT with next display element from C string IT->s.
7803 IT->string_nchars is the maximum number of characters to return
7804 from the string. IT->end_charpos may be greater than
7805 IT->string_nchars when this function is called, in which case we
7806 may have to return padding spaces. Value is zero if end of string
7807 reached, including padding spaces. */
7808
7809 static int
7810 next_element_from_c_string (struct it *it)
7811 {
7812 bool success_p = true;
7813
7814 eassert (it->s);
7815 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7816 it->what = IT_CHARACTER;
7817 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7818 it->object = Qnil;
7819
7820 /* With bidi reordering, the character to display might not be the
7821 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7822 we were reseated to a new string, whose paragraph direction is
7823 not known. */
7824 if (it->bidi_p && it->bidi_it.first_elt)
7825 get_visually_first_element (it);
7826
7827 /* IT's position can be greater than IT->string_nchars in case a
7828 field width or precision has been specified when the iterator was
7829 initialized. */
7830 if (IT_CHARPOS (*it) >= it->end_charpos)
7831 {
7832 /* End of the game. */
7833 it->what = IT_EOB;
7834 success_p = 0;
7835 }
7836 else if (IT_CHARPOS (*it) >= it->string_nchars)
7837 {
7838 /* Pad with spaces. */
7839 it->c = ' ', it->len = 1;
7840 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7841 }
7842 else if (it->multibyte_p)
7843 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7844 else
7845 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7846
7847 return success_p;
7848 }
7849
7850
7851 /* Set up IT to return characters from an ellipsis, if appropriate.
7852 The definition of the ellipsis glyphs may come from a display table
7853 entry. This function fills IT with the first glyph from the
7854 ellipsis if an ellipsis is to be displayed. */
7855
7856 static int
7857 next_element_from_ellipsis (struct it *it)
7858 {
7859 if (it->selective_display_ellipsis_p)
7860 setup_for_ellipsis (it, it->len);
7861 else
7862 {
7863 /* The face at the current position may be different from the
7864 face we find after the invisible text. Remember what it
7865 was in IT->saved_face_id, and signal that it's there by
7866 setting face_before_selective_p. */
7867 it->saved_face_id = it->face_id;
7868 it->method = GET_FROM_BUFFER;
7869 it->object = it->w->contents;
7870 reseat_at_next_visible_line_start (it, 1);
7871 it->face_before_selective_p = true;
7872 }
7873
7874 return GET_NEXT_DISPLAY_ELEMENT (it);
7875 }
7876
7877
7878 /* Deliver an image display element. The iterator IT is already
7879 filled with image information (done in handle_display_prop). Value
7880 is always 1. */
7881
7882
7883 static int
7884 next_element_from_image (struct it *it)
7885 {
7886 it->what = IT_IMAGE;
7887 it->ignore_overlay_strings_at_pos_p = 0;
7888 return 1;
7889 }
7890
7891
7892 /* Fill iterator IT with next display element from a stretch glyph
7893 property. IT->object is the value of the text property. Value is
7894 always 1. */
7895
7896 static int
7897 next_element_from_stretch (struct it *it)
7898 {
7899 it->what = IT_STRETCH;
7900 return 1;
7901 }
7902
7903 /* Scan backwards from IT's current position until we find a stop
7904 position, or until BEGV. This is called when we find ourself
7905 before both the last known prev_stop and base_level_stop while
7906 reordering bidirectional text. */
7907
7908 static void
7909 compute_stop_pos_backwards (struct it *it)
7910 {
7911 const int SCAN_BACK_LIMIT = 1000;
7912 struct text_pos pos;
7913 struct display_pos save_current = it->current;
7914 struct text_pos save_position = it->position;
7915 ptrdiff_t charpos = IT_CHARPOS (*it);
7916 ptrdiff_t where_we_are = charpos;
7917 ptrdiff_t save_stop_pos = it->stop_charpos;
7918 ptrdiff_t save_end_pos = it->end_charpos;
7919
7920 eassert (NILP (it->string) && !it->s);
7921 eassert (it->bidi_p);
7922 it->bidi_p = 0;
7923 do
7924 {
7925 it->end_charpos = min (charpos + 1, ZV);
7926 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7927 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7928 reseat_1 (it, pos, 0);
7929 compute_stop_pos (it);
7930 /* We must advance forward, right? */
7931 if (it->stop_charpos <= charpos)
7932 emacs_abort ();
7933 }
7934 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7935
7936 if (it->stop_charpos <= where_we_are)
7937 it->prev_stop = it->stop_charpos;
7938 else
7939 it->prev_stop = BEGV;
7940 it->bidi_p = true;
7941 it->current = save_current;
7942 it->position = save_position;
7943 it->stop_charpos = save_stop_pos;
7944 it->end_charpos = save_end_pos;
7945 }
7946
7947 /* Scan forward from CHARPOS in the current buffer/string, until we
7948 find a stop position > current IT's position. Then handle the stop
7949 position before that. This is called when we bump into a stop
7950 position while reordering bidirectional text. CHARPOS should be
7951 the last previously processed stop_pos (or BEGV/0, if none were
7952 processed yet) whose position is less that IT's current
7953 position. */
7954
7955 static void
7956 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7957 {
7958 int bufp = !STRINGP (it->string);
7959 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7960 struct display_pos save_current = it->current;
7961 struct text_pos save_position = it->position;
7962 struct text_pos pos1;
7963 ptrdiff_t next_stop;
7964
7965 /* Scan in strict logical order. */
7966 eassert (it->bidi_p);
7967 it->bidi_p = 0;
7968 do
7969 {
7970 it->prev_stop = charpos;
7971 if (bufp)
7972 {
7973 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7974 reseat_1 (it, pos1, 0);
7975 }
7976 else
7977 it->current.string_pos = string_pos (charpos, it->string);
7978 compute_stop_pos (it);
7979 /* We must advance forward, right? */
7980 if (it->stop_charpos <= it->prev_stop)
7981 emacs_abort ();
7982 charpos = it->stop_charpos;
7983 }
7984 while (charpos <= where_we_are);
7985
7986 it->bidi_p = true;
7987 it->current = save_current;
7988 it->position = save_position;
7989 next_stop = it->stop_charpos;
7990 it->stop_charpos = it->prev_stop;
7991 handle_stop (it);
7992 it->stop_charpos = next_stop;
7993 }
7994
7995 /* Load IT with the next display element from current_buffer. Value
7996 is zero if end of buffer reached. IT->stop_charpos is the next
7997 position at which to stop and check for text properties or buffer
7998 end. */
7999
8000 static int
8001 next_element_from_buffer (struct it *it)
8002 {
8003 bool success_p = true;
8004
8005 eassert (IT_CHARPOS (*it) >= BEGV);
8006 eassert (NILP (it->string) && !it->s);
8007 eassert (!it->bidi_p
8008 || (EQ (it->bidi_it.string.lstring, Qnil)
8009 && it->bidi_it.string.s == NULL));
8010
8011 /* With bidi reordering, the character to display might not be the
8012 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8013 we were reseat()ed to a new buffer position, which is potentially
8014 a different paragraph. */
8015 if (it->bidi_p && it->bidi_it.first_elt)
8016 {
8017 get_visually_first_element (it);
8018 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8019 }
8020
8021 if (IT_CHARPOS (*it) >= it->stop_charpos)
8022 {
8023 if (IT_CHARPOS (*it) >= it->end_charpos)
8024 {
8025 int overlay_strings_follow_p;
8026
8027 /* End of the game, except when overlay strings follow that
8028 haven't been returned yet. */
8029 if (it->overlay_strings_at_end_processed_p)
8030 overlay_strings_follow_p = 0;
8031 else
8032 {
8033 it->overlay_strings_at_end_processed_p = true;
8034 overlay_strings_follow_p = get_overlay_strings (it, 0);
8035 }
8036
8037 if (overlay_strings_follow_p)
8038 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8039 else
8040 {
8041 it->what = IT_EOB;
8042 it->position = it->current.pos;
8043 success_p = 0;
8044 }
8045 }
8046 else if (!(!it->bidi_p
8047 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8048 || IT_CHARPOS (*it) == it->stop_charpos))
8049 {
8050 /* With bidi non-linear iteration, we could find ourselves
8051 far beyond the last computed stop_charpos, with several
8052 other stop positions in between that we missed. Scan
8053 them all now, in buffer's logical order, until we find
8054 and handle the last stop_charpos that precedes our
8055 current position. */
8056 handle_stop_backwards (it, it->stop_charpos);
8057 return GET_NEXT_DISPLAY_ELEMENT (it);
8058 }
8059 else
8060 {
8061 if (it->bidi_p)
8062 {
8063 /* Take note of the stop position we just moved across,
8064 for when we will move back across it. */
8065 it->prev_stop = it->stop_charpos;
8066 /* If we are at base paragraph embedding level, take
8067 note of the last stop position seen at this
8068 level. */
8069 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8070 it->base_level_stop = it->stop_charpos;
8071 }
8072 handle_stop (it);
8073 return GET_NEXT_DISPLAY_ELEMENT (it);
8074 }
8075 }
8076 else if (it->bidi_p
8077 /* If we are before prev_stop, we may have overstepped on
8078 our way backwards a stop_pos, and if so, we need to
8079 handle that stop_pos. */
8080 && IT_CHARPOS (*it) < it->prev_stop
8081 /* We can sometimes back up for reasons that have nothing
8082 to do with bidi reordering. E.g., compositions. The
8083 code below is only needed when we are above the base
8084 embedding level, so test for that explicitly. */
8085 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8086 {
8087 if (it->base_level_stop <= 0
8088 || IT_CHARPOS (*it) < it->base_level_stop)
8089 {
8090 /* If we lost track of base_level_stop, we need to find
8091 prev_stop by looking backwards. This happens, e.g., when
8092 we were reseated to the previous screenful of text by
8093 vertical-motion. */
8094 it->base_level_stop = BEGV;
8095 compute_stop_pos_backwards (it);
8096 handle_stop_backwards (it, it->prev_stop);
8097 }
8098 else
8099 handle_stop_backwards (it, it->base_level_stop);
8100 return GET_NEXT_DISPLAY_ELEMENT (it);
8101 }
8102 else
8103 {
8104 /* No face changes, overlays etc. in sight, so just return a
8105 character from current_buffer. */
8106 unsigned char *p;
8107 ptrdiff_t stop;
8108
8109 /* Maybe run the redisplay end trigger hook. Performance note:
8110 This doesn't seem to cost measurable time. */
8111 if (it->redisplay_end_trigger_charpos
8112 && it->glyph_row
8113 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8114 run_redisplay_end_trigger_hook (it);
8115
8116 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8117 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8118 stop)
8119 && next_element_from_composition (it))
8120 {
8121 return 1;
8122 }
8123
8124 /* Get the next character, maybe multibyte. */
8125 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8126 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8127 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8128 else
8129 it->c = *p, it->len = 1;
8130
8131 /* Record what we have and where it came from. */
8132 it->what = IT_CHARACTER;
8133 it->object = it->w->contents;
8134 it->position = it->current.pos;
8135
8136 /* Normally we return the character found above, except when we
8137 really want to return an ellipsis for selective display. */
8138 if (it->selective)
8139 {
8140 if (it->c == '\n')
8141 {
8142 /* A value of selective > 0 means hide lines indented more
8143 than that number of columns. */
8144 if (it->selective > 0
8145 && IT_CHARPOS (*it) + 1 < ZV
8146 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8147 IT_BYTEPOS (*it) + 1,
8148 it->selective))
8149 {
8150 success_p = next_element_from_ellipsis (it);
8151 it->dpvec_char_len = -1;
8152 }
8153 }
8154 else if (it->c == '\r' && it->selective == -1)
8155 {
8156 /* A value of selective == -1 means that everything from the
8157 CR to the end of the line is invisible, with maybe an
8158 ellipsis displayed for it. */
8159 success_p = next_element_from_ellipsis (it);
8160 it->dpvec_char_len = -1;
8161 }
8162 }
8163 }
8164
8165 /* Value is zero if end of buffer reached. */
8166 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8167 return success_p;
8168 }
8169
8170
8171 /* Run the redisplay end trigger hook for IT. */
8172
8173 static void
8174 run_redisplay_end_trigger_hook (struct it *it)
8175 {
8176 Lisp_Object args[3];
8177
8178 /* IT->glyph_row should be non-null, i.e. we should be actually
8179 displaying something, or otherwise we should not run the hook. */
8180 eassert (it->glyph_row);
8181
8182 /* Set up hook arguments. */
8183 args[0] = Qredisplay_end_trigger_functions;
8184 args[1] = it->window;
8185 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8186 it->redisplay_end_trigger_charpos = 0;
8187
8188 /* Since we are *trying* to run these functions, don't try to run
8189 them again, even if they get an error. */
8190 wset_redisplay_end_trigger (it->w, Qnil);
8191 Frun_hook_with_args (3, args);
8192
8193 /* Notice if it changed the face of the character we are on. */
8194 handle_face_prop (it);
8195 }
8196
8197
8198 /* Deliver a composition display element. Unlike the other
8199 next_element_from_XXX, this function is not registered in the array
8200 get_next_element[]. It is called from next_element_from_buffer and
8201 next_element_from_string when necessary. */
8202
8203 static int
8204 next_element_from_composition (struct it *it)
8205 {
8206 it->what = IT_COMPOSITION;
8207 it->len = it->cmp_it.nbytes;
8208 if (STRINGP (it->string))
8209 {
8210 if (it->c < 0)
8211 {
8212 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8213 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8214 return 0;
8215 }
8216 it->position = it->current.string_pos;
8217 it->object = it->string;
8218 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8219 IT_STRING_BYTEPOS (*it), it->string);
8220 }
8221 else
8222 {
8223 if (it->c < 0)
8224 {
8225 IT_CHARPOS (*it) += it->cmp_it.nchars;
8226 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8227 if (it->bidi_p)
8228 {
8229 if (it->bidi_it.new_paragraph)
8230 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8231 /* Resync the bidi iterator with IT's new position.
8232 FIXME: this doesn't support bidirectional text. */
8233 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8234 bidi_move_to_visually_next (&it->bidi_it);
8235 }
8236 return 0;
8237 }
8238 it->position = it->current.pos;
8239 it->object = it->w->contents;
8240 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8241 IT_BYTEPOS (*it), Qnil);
8242 }
8243 return 1;
8244 }
8245
8246
8247 \f
8248 /***********************************************************************
8249 Moving an iterator without producing glyphs
8250 ***********************************************************************/
8251
8252 /* Check if iterator is at a position corresponding to a valid buffer
8253 position after some move_it_ call. */
8254
8255 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8256 ((it)->method == GET_FROM_STRING \
8257 ? IT_STRING_CHARPOS (*it) == 0 \
8258 : 1)
8259
8260
8261 /* Move iterator IT to a specified buffer or X position within one
8262 line on the display without producing glyphs.
8263
8264 OP should be a bit mask including some or all of these bits:
8265 MOVE_TO_X: Stop upon reaching x-position TO_X.
8266 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8267 Regardless of OP's value, stop upon reaching the end of the display line.
8268
8269 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8270 This means, in particular, that TO_X includes window's horizontal
8271 scroll amount.
8272
8273 The return value has several possible values that
8274 say what condition caused the scan to stop:
8275
8276 MOVE_POS_MATCH_OR_ZV
8277 - when TO_POS or ZV was reached.
8278
8279 MOVE_X_REACHED
8280 -when TO_X was reached before TO_POS or ZV were reached.
8281
8282 MOVE_LINE_CONTINUED
8283 - when we reached the end of the display area and the line must
8284 be continued.
8285
8286 MOVE_LINE_TRUNCATED
8287 - when we reached the end of the display area and the line is
8288 truncated.
8289
8290 MOVE_NEWLINE_OR_CR
8291 - when we stopped at a line end, i.e. a newline or a CR and selective
8292 display is on. */
8293
8294 static enum move_it_result
8295 move_it_in_display_line_to (struct it *it,
8296 ptrdiff_t to_charpos, int to_x,
8297 enum move_operation_enum op)
8298 {
8299 enum move_it_result result = MOVE_UNDEFINED;
8300 struct glyph_row *saved_glyph_row;
8301 struct it wrap_it, atpos_it, atx_it, ppos_it;
8302 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8303 void *ppos_data = NULL;
8304 int may_wrap = 0;
8305 enum it_method prev_method = it->method;
8306 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8307 int saw_smaller_pos = prev_pos < to_charpos;
8308
8309 /* Don't produce glyphs in produce_glyphs. */
8310 saved_glyph_row = it->glyph_row;
8311 it->glyph_row = NULL;
8312
8313 /* Use wrap_it to save a copy of IT wherever a word wrap could
8314 occur. Use atpos_it to save a copy of IT at the desired buffer
8315 position, if found, so that we can scan ahead and check if the
8316 word later overshoots the window edge. Use atx_it similarly, for
8317 pixel positions. */
8318 wrap_it.sp = -1;
8319 atpos_it.sp = -1;
8320 atx_it.sp = -1;
8321
8322 /* Use ppos_it under bidi reordering to save a copy of IT for the
8323 position > CHARPOS that is the closest to CHARPOS. We restore
8324 that position in IT when we have scanned the entire display line
8325 without finding a match for CHARPOS and all the character
8326 positions are greater than CHARPOS. */
8327 if (it->bidi_p)
8328 {
8329 SAVE_IT (ppos_it, *it, ppos_data);
8330 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8331 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8332 SAVE_IT (ppos_it, *it, ppos_data);
8333 }
8334
8335 #define BUFFER_POS_REACHED_P() \
8336 ((op & MOVE_TO_POS) != 0 \
8337 && BUFFERP (it->object) \
8338 && (IT_CHARPOS (*it) == to_charpos \
8339 || ((!it->bidi_p \
8340 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8341 && IT_CHARPOS (*it) > to_charpos) \
8342 || (it->what == IT_COMPOSITION \
8343 && ((IT_CHARPOS (*it) > to_charpos \
8344 && to_charpos >= it->cmp_it.charpos) \
8345 || (IT_CHARPOS (*it) < to_charpos \
8346 && to_charpos <= it->cmp_it.charpos)))) \
8347 && (it->method == GET_FROM_BUFFER \
8348 || (it->method == GET_FROM_DISPLAY_VECTOR \
8349 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8350
8351 /* If there's a line-/wrap-prefix, handle it. */
8352 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8353 && it->current_y < it->last_visible_y)
8354 handle_line_prefix (it);
8355
8356 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8357 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8358
8359 while (1)
8360 {
8361 int x, i, ascent = 0, descent = 0;
8362
8363 /* Utility macro to reset an iterator with x, ascent, and descent. */
8364 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8365 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8366 (IT)->max_descent = descent)
8367
8368 /* Stop if we move beyond TO_CHARPOS (after an image or a
8369 display string or stretch glyph). */
8370 if ((op & MOVE_TO_POS) != 0
8371 && BUFFERP (it->object)
8372 && it->method == GET_FROM_BUFFER
8373 && (((!it->bidi_p
8374 /* When the iterator is at base embedding level, we
8375 are guaranteed that characters are delivered for
8376 display in strictly increasing order of their
8377 buffer positions. */
8378 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8379 && IT_CHARPOS (*it) > to_charpos)
8380 || (it->bidi_p
8381 && (prev_method == GET_FROM_IMAGE
8382 || prev_method == GET_FROM_STRETCH
8383 || prev_method == GET_FROM_STRING)
8384 /* Passed TO_CHARPOS from left to right. */
8385 && ((prev_pos < to_charpos
8386 && IT_CHARPOS (*it) > to_charpos)
8387 /* Passed TO_CHARPOS from right to left. */
8388 || (prev_pos > to_charpos
8389 && IT_CHARPOS (*it) < to_charpos)))))
8390 {
8391 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8392 {
8393 result = MOVE_POS_MATCH_OR_ZV;
8394 break;
8395 }
8396 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8397 /* If wrap_it is valid, the current position might be in a
8398 word that is wrapped. So, save the iterator in
8399 atpos_it and continue to see if wrapping happens. */
8400 SAVE_IT (atpos_it, *it, atpos_data);
8401 }
8402
8403 /* Stop when ZV reached.
8404 We used to stop here when TO_CHARPOS reached as well, but that is
8405 too soon if this glyph does not fit on this line. So we handle it
8406 explicitly below. */
8407 if (!get_next_display_element (it))
8408 {
8409 result = MOVE_POS_MATCH_OR_ZV;
8410 break;
8411 }
8412
8413 if (it->line_wrap == TRUNCATE)
8414 {
8415 if (BUFFER_POS_REACHED_P ())
8416 {
8417 result = MOVE_POS_MATCH_OR_ZV;
8418 break;
8419 }
8420 }
8421 else
8422 {
8423 if (it->line_wrap == WORD_WRAP)
8424 {
8425 if (IT_DISPLAYING_WHITESPACE (it))
8426 may_wrap = 1;
8427 else if (may_wrap)
8428 {
8429 /* We have reached a glyph that follows one or more
8430 whitespace characters. If the position is
8431 already found, we are done. */
8432 if (atpos_it.sp >= 0)
8433 {
8434 RESTORE_IT (it, &atpos_it, atpos_data);
8435 result = MOVE_POS_MATCH_OR_ZV;
8436 goto done;
8437 }
8438 if (atx_it.sp >= 0)
8439 {
8440 RESTORE_IT (it, &atx_it, atx_data);
8441 result = MOVE_X_REACHED;
8442 goto done;
8443 }
8444 /* Otherwise, we can wrap here. */
8445 SAVE_IT (wrap_it, *it, wrap_data);
8446 may_wrap = 0;
8447 }
8448 }
8449 }
8450
8451 /* Remember the line height for the current line, in case
8452 the next element doesn't fit on the line. */
8453 ascent = it->max_ascent;
8454 descent = it->max_descent;
8455
8456 /* The call to produce_glyphs will get the metrics of the
8457 display element IT is loaded with. Record the x-position
8458 before this display element, in case it doesn't fit on the
8459 line. */
8460 x = it->current_x;
8461
8462 PRODUCE_GLYPHS (it);
8463
8464 if (it->area != TEXT_AREA)
8465 {
8466 prev_method = it->method;
8467 if (it->method == GET_FROM_BUFFER)
8468 prev_pos = IT_CHARPOS (*it);
8469 set_iterator_to_next (it, 1);
8470 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8471 SET_TEXT_POS (this_line_min_pos,
8472 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8473 if (it->bidi_p
8474 && (op & MOVE_TO_POS)
8475 && IT_CHARPOS (*it) > to_charpos
8476 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8477 SAVE_IT (ppos_it, *it, ppos_data);
8478 continue;
8479 }
8480
8481 /* The number of glyphs we get back in IT->nglyphs will normally
8482 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8483 character on a terminal frame, or (iii) a line end. For the
8484 second case, IT->nglyphs - 1 padding glyphs will be present.
8485 (On X frames, there is only one glyph produced for a
8486 composite character.)
8487
8488 The behavior implemented below means, for continuation lines,
8489 that as many spaces of a TAB as fit on the current line are
8490 displayed there. For terminal frames, as many glyphs of a
8491 multi-glyph character are displayed in the current line, too.
8492 This is what the old redisplay code did, and we keep it that
8493 way. Under X, the whole shape of a complex character must
8494 fit on the line or it will be completely displayed in the
8495 next line.
8496
8497 Note that both for tabs and padding glyphs, all glyphs have
8498 the same width. */
8499 if (it->nglyphs)
8500 {
8501 /* More than one glyph or glyph doesn't fit on line. All
8502 glyphs have the same width. */
8503 int single_glyph_width = it->pixel_width / it->nglyphs;
8504 int new_x;
8505 int x_before_this_char = x;
8506 int hpos_before_this_char = it->hpos;
8507
8508 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8509 {
8510 new_x = x + single_glyph_width;
8511
8512 /* We want to leave anything reaching TO_X to the caller. */
8513 if ((op & MOVE_TO_X) && new_x > to_x)
8514 {
8515 if (BUFFER_POS_REACHED_P ())
8516 {
8517 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8518 goto buffer_pos_reached;
8519 if (atpos_it.sp < 0)
8520 {
8521 SAVE_IT (atpos_it, *it, atpos_data);
8522 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8523 }
8524 }
8525 else
8526 {
8527 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8528 {
8529 it->current_x = x;
8530 result = MOVE_X_REACHED;
8531 break;
8532 }
8533 if (atx_it.sp < 0)
8534 {
8535 SAVE_IT (atx_it, *it, atx_data);
8536 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8537 }
8538 }
8539 }
8540
8541 if (/* Lines are continued. */
8542 it->line_wrap != TRUNCATE
8543 && (/* And glyph doesn't fit on the line. */
8544 new_x > it->last_visible_x
8545 /* Or it fits exactly and we're on a window
8546 system frame. */
8547 || (new_x == it->last_visible_x
8548 && FRAME_WINDOW_P (it->f)
8549 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8550 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8551 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8552 {
8553 if (/* IT->hpos == 0 means the very first glyph
8554 doesn't fit on the line, e.g. a wide image. */
8555 it->hpos == 0
8556 || (new_x == it->last_visible_x
8557 && FRAME_WINDOW_P (it->f)))
8558 {
8559 ++it->hpos;
8560 it->current_x = new_x;
8561
8562 /* The character's last glyph just barely fits
8563 in this row. */
8564 if (i == it->nglyphs - 1)
8565 {
8566 /* If this is the destination position,
8567 return a position *before* it in this row,
8568 now that we know it fits in this row. */
8569 if (BUFFER_POS_REACHED_P ())
8570 {
8571 if (it->line_wrap != WORD_WRAP
8572 || wrap_it.sp < 0)
8573 {
8574 it->hpos = hpos_before_this_char;
8575 it->current_x = x_before_this_char;
8576 result = MOVE_POS_MATCH_OR_ZV;
8577 break;
8578 }
8579 if (it->line_wrap == WORD_WRAP
8580 && atpos_it.sp < 0)
8581 {
8582 SAVE_IT (atpos_it, *it, atpos_data);
8583 atpos_it.current_x = x_before_this_char;
8584 atpos_it.hpos = hpos_before_this_char;
8585 }
8586 }
8587
8588 prev_method = it->method;
8589 if (it->method == GET_FROM_BUFFER)
8590 prev_pos = IT_CHARPOS (*it);
8591 set_iterator_to_next (it, 1);
8592 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8593 SET_TEXT_POS (this_line_min_pos,
8594 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8595 /* On graphical terminals, newlines may
8596 "overflow" into the fringe if
8597 overflow-newline-into-fringe is non-nil.
8598 On text terminals, and on graphical
8599 terminals with no right margin, newlines
8600 may overflow into the last glyph on the
8601 display line.*/
8602 if (!FRAME_WINDOW_P (it->f)
8603 || ((it->bidi_p
8604 && it->bidi_it.paragraph_dir == R2L)
8605 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8606 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8607 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8608 {
8609 if (!get_next_display_element (it))
8610 {
8611 result = MOVE_POS_MATCH_OR_ZV;
8612 break;
8613 }
8614 if (BUFFER_POS_REACHED_P ())
8615 {
8616 if (ITERATOR_AT_END_OF_LINE_P (it))
8617 result = MOVE_POS_MATCH_OR_ZV;
8618 else
8619 result = MOVE_LINE_CONTINUED;
8620 break;
8621 }
8622 if (ITERATOR_AT_END_OF_LINE_P (it)
8623 && (it->line_wrap != WORD_WRAP
8624 || wrap_it.sp < 0))
8625 {
8626 result = MOVE_NEWLINE_OR_CR;
8627 break;
8628 }
8629 }
8630 }
8631 }
8632 else
8633 IT_RESET_X_ASCENT_DESCENT (it);
8634
8635 if (wrap_it.sp >= 0)
8636 {
8637 RESTORE_IT (it, &wrap_it, wrap_data);
8638 atpos_it.sp = -1;
8639 atx_it.sp = -1;
8640 }
8641
8642 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8643 IT_CHARPOS (*it)));
8644 result = MOVE_LINE_CONTINUED;
8645 break;
8646 }
8647
8648 if (BUFFER_POS_REACHED_P ())
8649 {
8650 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8651 goto buffer_pos_reached;
8652 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8653 {
8654 SAVE_IT (atpos_it, *it, atpos_data);
8655 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8656 }
8657 }
8658
8659 if (new_x > it->first_visible_x)
8660 {
8661 /* Glyph is visible. Increment number of glyphs that
8662 would be displayed. */
8663 ++it->hpos;
8664 }
8665 }
8666
8667 if (result != MOVE_UNDEFINED)
8668 break;
8669 }
8670 else if (BUFFER_POS_REACHED_P ())
8671 {
8672 buffer_pos_reached:
8673 IT_RESET_X_ASCENT_DESCENT (it);
8674 result = MOVE_POS_MATCH_OR_ZV;
8675 break;
8676 }
8677 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8678 {
8679 /* Stop when TO_X specified and reached. This check is
8680 necessary here because of lines consisting of a line end,
8681 only. The line end will not produce any glyphs and we
8682 would never get MOVE_X_REACHED. */
8683 eassert (it->nglyphs == 0);
8684 result = MOVE_X_REACHED;
8685 break;
8686 }
8687
8688 /* Is this a line end? If yes, we're done. */
8689 if (ITERATOR_AT_END_OF_LINE_P (it))
8690 {
8691 /* If we are past TO_CHARPOS, but never saw any character
8692 positions smaller than TO_CHARPOS, return
8693 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8694 did. */
8695 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8696 {
8697 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8698 {
8699 if (IT_CHARPOS (ppos_it) < ZV)
8700 {
8701 RESTORE_IT (it, &ppos_it, ppos_data);
8702 result = MOVE_POS_MATCH_OR_ZV;
8703 }
8704 else
8705 goto buffer_pos_reached;
8706 }
8707 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8708 && IT_CHARPOS (*it) > to_charpos)
8709 goto buffer_pos_reached;
8710 else
8711 result = MOVE_NEWLINE_OR_CR;
8712 }
8713 else
8714 result = MOVE_NEWLINE_OR_CR;
8715 break;
8716 }
8717
8718 prev_method = it->method;
8719 if (it->method == GET_FROM_BUFFER)
8720 prev_pos = IT_CHARPOS (*it);
8721 /* The current display element has been consumed. Advance
8722 to the next. */
8723 set_iterator_to_next (it, 1);
8724 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8725 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8726 if (IT_CHARPOS (*it) < to_charpos)
8727 saw_smaller_pos = 1;
8728 if (it->bidi_p
8729 && (op & MOVE_TO_POS)
8730 && IT_CHARPOS (*it) >= to_charpos
8731 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8732 SAVE_IT (ppos_it, *it, ppos_data);
8733
8734 /* Stop if lines are truncated and IT's current x-position is
8735 past the right edge of the window now. */
8736 if (it->line_wrap == TRUNCATE
8737 && it->current_x >= it->last_visible_x)
8738 {
8739 if (!FRAME_WINDOW_P (it->f)
8740 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8741 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8742 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8743 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8744 {
8745 int at_eob_p = 0;
8746
8747 if ((at_eob_p = !get_next_display_element (it))
8748 || BUFFER_POS_REACHED_P ()
8749 /* If we are past TO_CHARPOS, but never saw any
8750 character positions smaller than TO_CHARPOS,
8751 return MOVE_POS_MATCH_OR_ZV, like the
8752 unidirectional display did. */
8753 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8754 && !saw_smaller_pos
8755 && IT_CHARPOS (*it) > to_charpos))
8756 {
8757 if (it->bidi_p
8758 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8759 RESTORE_IT (it, &ppos_it, ppos_data);
8760 result = MOVE_POS_MATCH_OR_ZV;
8761 break;
8762 }
8763 if (ITERATOR_AT_END_OF_LINE_P (it))
8764 {
8765 result = MOVE_NEWLINE_OR_CR;
8766 break;
8767 }
8768 }
8769 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8770 && !saw_smaller_pos
8771 && IT_CHARPOS (*it) > to_charpos)
8772 {
8773 if (IT_CHARPOS (ppos_it) < ZV)
8774 RESTORE_IT (it, &ppos_it, ppos_data);
8775 result = MOVE_POS_MATCH_OR_ZV;
8776 break;
8777 }
8778 result = MOVE_LINE_TRUNCATED;
8779 break;
8780 }
8781 #undef IT_RESET_X_ASCENT_DESCENT
8782 }
8783
8784 #undef BUFFER_POS_REACHED_P
8785
8786 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8787 restore the saved iterator. */
8788 if (atpos_it.sp >= 0)
8789 RESTORE_IT (it, &atpos_it, atpos_data);
8790 else if (atx_it.sp >= 0)
8791 RESTORE_IT (it, &atx_it, atx_data);
8792
8793 done:
8794
8795 if (atpos_data)
8796 bidi_unshelve_cache (atpos_data, 1);
8797 if (atx_data)
8798 bidi_unshelve_cache (atx_data, 1);
8799 if (wrap_data)
8800 bidi_unshelve_cache (wrap_data, 1);
8801 if (ppos_data)
8802 bidi_unshelve_cache (ppos_data, 1);
8803
8804 /* Restore the iterator settings altered at the beginning of this
8805 function. */
8806 it->glyph_row = saved_glyph_row;
8807 return result;
8808 }
8809
8810 /* For external use. */
8811 void
8812 move_it_in_display_line (struct it *it,
8813 ptrdiff_t to_charpos, int to_x,
8814 enum move_operation_enum op)
8815 {
8816 if (it->line_wrap == WORD_WRAP
8817 && (op & MOVE_TO_X))
8818 {
8819 struct it save_it;
8820 void *save_data = NULL;
8821 int skip;
8822
8823 SAVE_IT (save_it, *it, save_data);
8824 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8825 /* When word-wrap is on, TO_X may lie past the end
8826 of a wrapped line. Then it->current is the
8827 character on the next line, so backtrack to the
8828 space before the wrap point. */
8829 if (skip == MOVE_LINE_CONTINUED)
8830 {
8831 int prev_x = max (it->current_x - 1, 0);
8832 RESTORE_IT (it, &save_it, save_data);
8833 move_it_in_display_line_to
8834 (it, -1, prev_x, MOVE_TO_X);
8835 }
8836 else
8837 bidi_unshelve_cache (save_data, 1);
8838 }
8839 else
8840 move_it_in_display_line_to (it, to_charpos, to_x, op);
8841 }
8842
8843
8844 /* Move IT forward until it satisfies one or more of the criteria in
8845 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8846
8847 OP is a bit-mask that specifies where to stop, and in particular,
8848 which of those four position arguments makes a difference. See the
8849 description of enum move_operation_enum.
8850
8851 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8852 screen line, this function will set IT to the next position that is
8853 displayed to the right of TO_CHARPOS on the screen.
8854
8855 Return the maximum pixel length of any line scanned but never more
8856 than it.last_visible_x. */
8857
8858 int
8859 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8860 {
8861 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8862 int line_height, line_start_x = 0, reached = 0;
8863 int max_current_x = 0;
8864 void *backup_data = NULL;
8865
8866 for (;;)
8867 {
8868 if (op & MOVE_TO_VPOS)
8869 {
8870 /* If no TO_CHARPOS and no TO_X specified, stop at the
8871 start of the line TO_VPOS. */
8872 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8873 {
8874 if (it->vpos == to_vpos)
8875 {
8876 reached = 1;
8877 break;
8878 }
8879 else
8880 skip = move_it_in_display_line_to (it, -1, -1, 0);
8881 }
8882 else
8883 {
8884 /* TO_VPOS >= 0 means stop at TO_X in the line at
8885 TO_VPOS, or at TO_POS, whichever comes first. */
8886 if (it->vpos == to_vpos)
8887 {
8888 reached = 2;
8889 break;
8890 }
8891
8892 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8893
8894 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8895 {
8896 reached = 3;
8897 break;
8898 }
8899 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8900 {
8901 /* We have reached TO_X but not in the line we want. */
8902 skip = move_it_in_display_line_to (it, to_charpos,
8903 -1, MOVE_TO_POS);
8904 if (skip == MOVE_POS_MATCH_OR_ZV)
8905 {
8906 reached = 4;
8907 break;
8908 }
8909 }
8910 }
8911 }
8912 else if (op & MOVE_TO_Y)
8913 {
8914 struct it it_backup;
8915
8916 if (it->line_wrap == WORD_WRAP)
8917 SAVE_IT (it_backup, *it, backup_data);
8918
8919 /* TO_Y specified means stop at TO_X in the line containing
8920 TO_Y---or at TO_CHARPOS if this is reached first. The
8921 problem is that we can't really tell whether the line
8922 contains TO_Y before we have completely scanned it, and
8923 this may skip past TO_X. What we do is to first scan to
8924 TO_X.
8925
8926 If TO_X is not specified, use a TO_X of zero. The reason
8927 is to make the outcome of this function more predictable.
8928 If we didn't use TO_X == 0, we would stop at the end of
8929 the line which is probably not what a caller would expect
8930 to happen. */
8931 skip = move_it_in_display_line_to
8932 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8933 (MOVE_TO_X | (op & MOVE_TO_POS)));
8934
8935 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8936 if (skip == MOVE_POS_MATCH_OR_ZV)
8937 reached = 5;
8938 else if (skip == MOVE_X_REACHED)
8939 {
8940 /* If TO_X was reached, we want to know whether TO_Y is
8941 in the line. We know this is the case if the already
8942 scanned glyphs make the line tall enough. Otherwise,
8943 we must check by scanning the rest of the line. */
8944 line_height = it->max_ascent + it->max_descent;
8945 if (to_y >= it->current_y
8946 && to_y < it->current_y + line_height)
8947 {
8948 reached = 6;
8949 break;
8950 }
8951 SAVE_IT (it_backup, *it, backup_data);
8952 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8953 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8954 op & MOVE_TO_POS);
8955 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8956 line_height = it->max_ascent + it->max_descent;
8957 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8958
8959 if (to_y >= it->current_y
8960 && to_y < it->current_y + line_height)
8961 {
8962 /* If TO_Y is in this line and TO_X was reached
8963 above, we scanned too far. We have to restore
8964 IT's settings to the ones before skipping. But
8965 keep the more accurate values of max_ascent and
8966 max_descent we've found while skipping the rest
8967 of the line, for the sake of callers, such as
8968 pos_visible_p, that need to know the line
8969 height. */
8970 int max_ascent = it->max_ascent;
8971 int max_descent = it->max_descent;
8972
8973 RESTORE_IT (it, &it_backup, backup_data);
8974 it->max_ascent = max_ascent;
8975 it->max_descent = max_descent;
8976 reached = 6;
8977 }
8978 else
8979 {
8980 skip = skip2;
8981 if (skip == MOVE_POS_MATCH_OR_ZV)
8982 reached = 7;
8983 }
8984 }
8985 else
8986 {
8987 /* Check whether TO_Y is in this line. */
8988 line_height = it->max_ascent + it->max_descent;
8989 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8990
8991 if (to_y >= it->current_y
8992 && to_y < it->current_y + line_height)
8993 {
8994 if (to_y > it->current_y)
8995 max_current_x = max (it->current_x, max_current_x);
8996
8997 /* When word-wrap is on, TO_X may lie past the end
8998 of a wrapped line. Then it->current is the
8999 character on the next line, so backtrack to the
9000 space before the wrap point. */
9001 if (skip == MOVE_LINE_CONTINUED
9002 && it->line_wrap == WORD_WRAP)
9003 {
9004 int prev_x = max (it->current_x - 1, 0);
9005 RESTORE_IT (it, &it_backup, backup_data);
9006 skip = move_it_in_display_line_to
9007 (it, -1, prev_x, MOVE_TO_X);
9008 }
9009
9010 reached = 6;
9011 }
9012 }
9013
9014 if (reached)
9015 {
9016 max_current_x = max (it->current_x, max_current_x);
9017 break;
9018 }
9019 }
9020 else if (BUFFERP (it->object)
9021 && (it->method == GET_FROM_BUFFER
9022 || it->method == GET_FROM_STRETCH)
9023 && IT_CHARPOS (*it) >= to_charpos
9024 /* Under bidi iteration, a call to set_iterator_to_next
9025 can scan far beyond to_charpos if the initial
9026 portion of the next line needs to be reordered. In
9027 that case, give move_it_in_display_line_to another
9028 chance below. */
9029 && !(it->bidi_p
9030 && it->bidi_it.scan_dir == -1))
9031 skip = MOVE_POS_MATCH_OR_ZV;
9032 else
9033 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9034
9035 switch (skip)
9036 {
9037 case MOVE_POS_MATCH_OR_ZV:
9038 max_current_x = max (it->current_x, max_current_x);
9039 reached = 8;
9040 goto out;
9041
9042 case MOVE_NEWLINE_OR_CR:
9043 max_current_x = max (it->current_x, max_current_x);
9044 set_iterator_to_next (it, 1);
9045 it->continuation_lines_width = 0;
9046 break;
9047
9048 case MOVE_LINE_TRUNCATED:
9049 max_current_x = it->last_visible_x;
9050 it->continuation_lines_width = 0;
9051 reseat_at_next_visible_line_start (it, 0);
9052 if ((op & MOVE_TO_POS) != 0
9053 && IT_CHARPOS (*it) > to_charpos)
9054 {
9055 reached = 9;
9056 goto out;
9057 }
9058 break;
9059
9060 case MOVE_LINE_CONTINUED:
9061 max_current_x = it->last_visible_x;
9062 /* For continued lines ending in a tab, some of the glyphs
9063 associated with the tab are displayed on the current
9064 line. Since it->current_x does not include these glyphs,
9065 we use it->last_visible_x instead. */
9066 if (it->c == '\t')
9067 {
9068 it->continuation_lines_width += it->last_visible_x;
9069 /* When moving by vpos, ensure that the iterator really
9070 advances to the next line (bug#847, bug#969). Fixme:
9071 do we need to do this in other circumstances? */
9072 if (it->current_x != it->last_visible_x
9073 && (op & MOVE_TO_VPOS)
9074 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9075 {
9076 line_start_x = it->current_x + it->pixel_width
9077 - it->last_visible_x;
9078 set_iterator_to_next (it, 0);
9079 }
9080 }
9081 else
9082 it->continuation_lines_width += it->current_x;
9083 break;
9084
9085 default:
9086 emacs_abort ();
9087 }
9088
9089 /* Reset/increment for the next run. */
9090 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9091 it->current_x = line_start_x;
9092 line_start_x = 0;
9093 it->hpos = 0;
9094 it->current_y += it->max_ascent + it->max_descent;
9095 ++it->vpos;
9096 last_height = it->max_ascent + it->max_descent;
9097 last_max_ascent = it->max_ascent;
9098 it->max_ascent = it->max_descent = 0;
9099 }
9100
9101 out:
9102
9103 /* On text terminals, we may stop at the end of a line in the middle
9104 of a multi-character glyph. If the glyph itself is continued,
9105 i.e. it is actually displayed on the next line, don't treat this
9106 stopping point as valid; move to the next line instead (unless
9107 that brings us offscreen). */
9108 if (!FRAME_WINDOW_P (it->f)
9109 && op & MOVE_TO_POS
9110 && IT_CHARPOS (*it) == to_charpos
9111 && it->what == IT_CHARACTER
9112 && it->nglyphs > 1
9113 && it->line_wrap == WINDOW_WRAP
9114 && it->current_x == it->last_visible_x - 1
9115 && it->c != '\n'
9116 && it->c != '\t'
9117 && it->vpos < it->w->window_end_vpos)
9118 {
9119 it->continuation_lines_width += it->current_x;
9120 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9121 it->current_y += it->max_ascent + it->max_descent;
9122 ++it->vpos;
9123 last_height = it->max_ascent + it->max_descent;
9124 last_max_ascent = it->max_ascent;
9125 }
9126
9127 if (backup_data)
9128 bidi_unshelve_cache (backup_data, 1);
9129
9130 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9131
9132 return max_current_x;
9133 }
9134
9135
9136 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9137
9138 If DY > 0, move IT backward at least that many pixels. DY = 0
9139 means move IT backward to the preceding line start or BEGV. This
9140 function may move over more than DY pixels if IT->current_y - DY
9141 ends up in the middle of a line; in this case IT->current_y will be
9142 set to the top of the line moved to. */
9143
9144 void
9145 move_it_vertically_backward (struct it *it, int dy)
9146 {
9147 int nlines, h;
9148 struct it it2, it3;
9149 void *it2data = NULL, *it3data = NULL;
9150 ptrdiff_t start_pos;
9151 int nchars_per_row
9152 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9153 ptrdiff_t pos_limit;
9154
9155 move_further_back:
9156 eassert (dy >= 0);
9157
9158 start_pos = IT_CHARPOS (*it);
9159
9160 /* Estimate how many newlines we must move back. */
9161 nlines = max (1, dy / default_line_pixel_height (it->w));
9162 if (it->line_wrap == TRUNCATE)
9163 pos_limit = BEGV;
9164 else
9165 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9166
9167 /* Set the iterator's position that many lines back. But don't go
9168 back more than NLINES full screen lines -- this wins a day with
9169 buffers which have very long lines. */
9170 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9171 back_to_previous_visible_line_start (it);
9172
9173 /* Reseat the iterator here. When moving backward, we don't want
9174 reseat to skip forward over invisible text, set up the iterator
9175 to deliver from overlay strings at the new position etc. So,
9176 use reseat_1 here. */
9177 reseat_1 (it, it->current.pos, 1);
9178
9179 /* We are now surely at a line start. */
9180 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9181 reordering is in effect. */
9182 it->continuation_lines_width = 0;
9183
9184 /* Move forward and see what y-distance we moved. First move to the
9185 start of the next line so that we get its height. We need this
9186 height to be able to tell whether we reached the specified
9187 y-distance. */
9188 SAVE_IT (it2, *it, it2data);
9189 it2.max_ascent = it2.max_descent = 0;
9190 do
9191 {
9192 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9193 MOVE_TO_POS | MOVE_TO_VPOS);
9194 }
9195 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9196 /* If we are in a display string which starts at START_POS,
9197 and that display string includes a newline, and we are
9198 right after that newline (i.e. at the beginning of a
9199 display line), exit the loop, because otherwise we will
9200 infloop, since move_it_to will see that it is already at
9201 START_POS and will not move. */
9202 || (it2.method == GET_FROM_STRING
9203 && IT_CHARPOS (it2) == start_pos
9204 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9205 eassert (IT_CHARPOS (*it) >= BEGV);
9206 SAVE_IT (it3, it2, it3data);
9207
9208 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9209 eassert (IT_CHARPOS (*it) >= BEGV);
9210 /* H is the actual vertical distance from the position in *IT
9211 and the starting position. */
9212 h = it2.current_y - it->current_y;
9213 /* NLINES is the distance in number of lines. */
9214 nlines = it2.vpos - it->vpos;
9215
9216 /* Correct IT's y and vpos position
9217 so that they are relative to the starting point. */
9218 it->vpos -= nlines;
9219 it->current_y -= h;
9220
9221 if (dy == 0)
9222 {
9223 /* DY == 0 means move to the start of the screen line. The
9224 value of nlines is > 0 if continuation lines were involved,
9225 or if the original IT position was at start of a line. */
9226 RESTORE_IT (it, it, it2data);
9227 if (nlines > 0)
9228 move_it_by_lines (it, nlines);
9229 /* The above code moves us to some position NLINES down,
9230 usually to its first glyph (leftmost in an L2R line), but
9231 that's not necessarily the start of the line, under bidi
9232 reordering. We want to get to the character position
9233 that is immediately after the newline of the previous
9234 line. */
9235 if (it->bidi_p
9236 && !it->continuation_lines_width
9237 && !STRINGP (it->string)
9238 && IT_CHARPOS (*it) > BEGV
9239 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9240 {
9241 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9242
9243 DEC_BOTH (cp, bp);
9244 cp = find_newline_no_quit (cp, bp, -1, NULL);
9245 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9246 }
9247 bidi_unshelve_cache (it3data, 1);
9248 }
9249 else
9250 {
9251 /* The y-position we try to reach, relative to *IT.
9252 Note that H has been subtracted in front of the if-statement. */
9253 int target_y = it->current_y + h - dy;
9254 int y0 = it3.current_y;
9255 int y1;
9256 int line_height;
9257
9258 RESTORE_IT (&it3, &it3, it3data);
9259 y1 = line_bottom_y (&it3);
9260 line_height = y1 - y0;
9261 RESTORE_IT (it, it, it2data);
9262 /* If we did not reach target_y, try to move further backward if
9263 we can. If we moved too far backward, try to move forward. */
9264 if (target_y < it->current_y
9265 /* This is heuristic. In a window that's 3 lines high, with
9266 a line height of 13 pixels each, recentering with point
9267 on the bottom line will try to move -39/2 = 19 pixels
9268 backward. Try to avoid moving into the first line. */
9269 && (it->current_y - target_y
9270 > min (window_box_height (it->w), line_height * 2 / 3))
9271 && IT_CHARPOS (*it) > BEGV)
9272 {
9273 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9274 target_y - it->current_y));
9275 dy = it->current_y - target_y;
9276 goto move_further_back;
9277 }
9278 else if (target_y >= it->current_y + line_height
9279 && IT_CHARPOS (*it) < ZV)
9280 {
9281 /* Should move forward by at least one line, maybe more.
9282
9283 Note: Calling move_it_by_lines can be expensive on
9284 terminal frames, where compute_motion is used (via
9285 vmotion) to do the job, when there are very long lines
9286 and truncate-lines is nil. That's the reason for
9287 treating terminal frames specially here. */
9288
9289 if (!FRAME_WINDOW_P (it->f))
9290 move_it_vertically (it, target_y - (it->current_y + line_height));
9291 else
9292 {
9293 do
9294 {
9295 move_it_by_lines (it, 1);
9296 }
9297 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9298 }
9299 }
9300 }
9301 }
9302
9303
9304 /* Move IT by a specified amount of pixel lines DY. DY negative means
9305 move backwards. DY = 0 means move to start of screen line. At the
9306 end, IT will be on the start of a screen line. */
9307
9308 void
9309 move_it_vertically (struct it *it, int dy)
9310 {
9311 if (dy <= 0)
9312 move_it_vertically_backward (it, -dy);
9313 else
9314 {
9315 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9316 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9317 MOVE_TO_POS | MOVE_TO_Y);
9318 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9319
9320 /* If buffer ends in ZV without a newline, move to the start of
9321 the line to satisfy the post-condition. */
9322 if (IT_CHARPOS (*it) == ZV
9323 && ZV > BEGV
9324 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9325 move_it_by_lines (it, 0);
9326 }
9327 }
9328
9329
9330 /* Move iterator IT past the end of the text line it is in. */
9331
9332 void
9333 move_it_past_eol (struct it *it)
9334 {
9335 enum move_it_result rc;
9336
9337 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9338 if (rc == MOVE_NEWLINE_OR_CR)
9339 set_iterator_to_next (it, 0);
9340 }
9341
9342
9343 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9344 negative means move up. DVPOS == 0 means move to the start of the
9345 screen line.
9346
9347 Optimization idea: If we would know that IT->f doesn't use
9348 a face with proportional font, we could be faster for
9349 truncate-lines nil. */
9350
9351 void
9352 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9353 {
9354
9355 /* The commented-out optimization uses vmotion on terminals. This
9356 gives bad results, because elements like it->what, on which
9357 callers such as pos_visible_p rely, aren't updated. */
9358 /* struct position pos;
9359 if (!FRAME_WINDOW_P (it->f))
9360 {
9361 struct text_pos textpos;
9362
9363 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9364 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9365 reseat (it, textpos, 1);
9366 it->vpos += pos.vpos;
9367 it->current_y += pos.vpos;
9368 }
9369 else */
9370
9371 if (dvpos == 0)
9372 {
9373 /* DVPOS == 0 means move to the start of the screen line. */
9374 move_it_vertically_backward (it, 0);
9375 /* Let next call to line_bottom_y calculate real line height. */
9376 last_height = 0;
9377 }
9378 else if (dvpos > 0)
9379 {
9380 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9381 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9382 {
9383 /* Only move to the next buffer position if we ended up in a
9384 string from display property, not in an overlay string
9385 (before-string or after-string). That is because the
9386 latter don't conceal the underlying buffer position, so
9387 we can ask to move the iterator to the exact position we
9388 are interested in. Note that, even if we are already at
9389 IT_CHARPOS (*it), the call below is not a no-op, as it
9390 will detect that we are at the end of the string, pop the
9391 iterator, and compute it->current_x and it->hpos
9392 correctly. */
9393 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9394 -1, -1, -1, MOVE_TO_POS);
9395 }
9396 }
9397 else
9398 {
9399 struct it it2;
9400 void *it2data = NULL;
9401 ptrdiff_t start_charpos, i;
9402 int nchars_per_row
9403 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9404 ptrdiff_t pos_limit;
9405
9406 /* Start at the beginning of the screen line containing IT's
9407 position. This may actually move vertically backwards,
9408 in case of overlays, so adjust dvpos accordingly. */
9409 dvpos += it->vpos;
9410 move_it_vertically_backward (it, 0);
9411 dvpos -= it->vpos;
9412
9413 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9414 screen lines, and reseat the iterator there. */
9415 start_charpos = IT_CHARPOS (*it);
9416 if (it->line_wrap == TRUNCATE)
9417 pos_limit = BEGV;
9418 else
9419 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9420 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9421 back_to_previous_visible_line_start (it);
9422 reseat (it, it->current.pos, 1);
9423
9424 /* Move further back if we end up in a string or an image. */
9425 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9426 {
9427 /* First try to move to start of display line. */
9428 dvpos += it->vpos;
9429 move_it_vertically_backward (it, 0);
9430 dvpos -= it->vpos;
9431 if (IT_POS_VALID_AFTER_MOVE_P (it))
9432 break;
9433 /* If start of line is still in string or image,
9434 move further back. */
9435 back_to_previous_visible_line_start (it);
9436 reseat (it, it->current.pos, 1);
9437 dvpos--;
9438 }
9439
9440 it->current_x = it->hpos = 0;
9441
9442 /* Above call may have moved too far if continuation lines
9443 are involved. Scan forward and see if it did. */
9444 SAVE_IT (it2, *it, it2data);
9445 it2.vpos = it2.current_y = 0;
9446 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9447 it->vpos -= it2.vpos;
9448 it->current_y -= it2.current_y;
9449 it->current_x = it->hpos = 0;
9450
9451 /* If we moved too far back, move IT some lines forward. */
9452 if (it2.vpos > -dvpos)
9453 {
9454 int delta = it2.vpos + dvpos;
9455
9456 RESTORE_IT (&it2, &it2, it2data);
9457 SAVE_IT (it2, *it, it2data);
9458 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9459 /* Move back again if we got too far ahead. */
9460 if (IT_CHARPOS (*it) >= start_charpos)
9461 RESTORE_IT (it, &it2, it2data);
9462 else
9463 bidi_unshelve_cache (it2data, 1);
9464 }
9465 else
9466 RESTORE_IT (it, it, it2data);
9467 }
9468 }
9469
9470 /* Return 1 if IT points into the middle of a display vector. */
9471
9472 int
9473 in_display_vector_p (struct it *it)
9474 {
9475 return (it->method == GET_FROM_DISPLAY_VECTOR
9476 && it->current.dpvec_index > 0
9477 && it->dpvec + it->current.dpvec_index != it->dpend);
9478 }
9479
9480 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9481 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9482 WINDOW must be a live window and defaults to the selected one. The
9483 return value is a cons of the maximum pixel-width of any text line and
9484 the maximum pixel-height of all text lines.
9485
9486 The optional argument FROM, if non-nil, specifies the first text
9487 position and defaults to the minimum accessible position of the buffer.
9488 If FROM is t, use the minimum accessible position that is not a newline
9489 character. TO, if non-nil, specifies the last text position and
9490 defaults to the maximum accessible position of the buffer. If TO is t,
9491 use the maximum accessible position that is not a newline character.
9492
9493 The optional argument X_LIMIT, if non-nil, specifies the maximum text
9494 width that can be returned. X_LIMIT nil or omitted, means to use the
9495 pixel-width of WINDOW's body; use this if you do not intend to change
9496 the width of WINDOW. Use the maximum width WINDOW may assume if you
9497 intend to change WINDOW's width.
9498
9499 The optional argument Y_LIMIT, if non-nil, specifies the maximum text
9500 height that can be returned. Text lines whose y-coordinate is beyond
9501 Y_LIMIT are ignored. Since calculating the text height of a large
9502 buffer can take some time, it makes sense to specify this argument if
9503 the size of the buffer is unknown.
9504
9505 Optional argument MODE_AND_HEADER_LINE nil or omitted means do not
9506 include the height of the mode- or header-line of WINDOW in the return
9507 value. If it is either the symbol `mode-line' or `header-line', include
9508 only the height of that line, if present, in the return value. If t,
9509 include the height of any of these lines in the return value. */)
9510 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9511 Lisp_Object mode_and_header_line)
9512 {
9513 struct window *w = decode_live_window (window);
9514 Lisp_Object buf;
9515 struct buffer *b;
9516 struct it it;
9517 struct buffer *old_buffer = NULL;
9518 ptrdiff_t start, end, pos;
9519 struct text_pos startp;
9520 void *itdata = NULL;
9521 int c, max_y = -1, x = 0, y = 0;
9522
9523 buf = w->contents;
9524 CHECK_BUFFER (buf);
9525 b = XBUFFER (buf);
9526
9527 if (b != current_buffer)
9528 {
9529 old_buffer = current_buffer;
9530 set_buffer_internal (b);
9531 }
9532
9533 if (NILP (from))
9534 start = BEGV;
9535 else if (EQ (from, Qt))
9536 {
9537 start = pos = BEGV;
9538 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9539 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9540 start = pos;
9541 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9542 start = pos;
9543 }
9544 else
9545 {
9546 CHECK_NUMBER_COERCE_MARKER (from);
9547 start = min (max (XINT (from), BEGV), ZV);
9548 }
9549
9550 if (NILP (to))
9551 end = ZV;
9552 else if (EQ (to, Qt))
9553 {
9554 end = pos = ZV;
9555 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9556 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9557 end = pos;
9558 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9559 end = pos;
9560 }
9561 else
9562 {
9563 CHECK_NUMBER_COERCE_MARKER (to);
9564 end = max (start, min (XINT (to), ZV));
9565 }
9566
9567 if (!NILP (y_limit))
9568 {
9569 CHECK_NUMBER (y_limit);
9570 max_y = XINT (y_limit);
9571 }
9572
9573 itdata = bidi_shelve_cache ();
9574 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9575 start_display (&it, w, startp);
9576
9577 /** move_it_vertically_backward (&it, 0); **/
9578 if (NILP (x_limit))
9579 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9580 else
9581 {
9582 CHECK_NUMBER (x_limit);
9583 it.last_visible_x = XINT (x_limit);
9584 /* Actually, we never want move_it_to stop at to_x. But to make
9585 sure that move_it_in_display_line_to always moves far enough,
9586 we set it to INT_MAX and specify MOVE_TO_X. */
9587 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9588 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9589 }
9590
9591 if (start == end)
9592 y = it.current_y;
9593 else
9594 {
9595 /* Count last line. */
9596 last_height = 0;
9597 y = line_bottom_y (&it); /* - y; */
9598 }
9599
9600 if (!EQ (mode_and_header_line, Qheader_line)
9601 && !EQ (mode_and_header_line, Qt))
9602 /* Do not count the header-line which was counted automatically by
9603 start_display. */
9604 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9605
9606 if (EQ (mode_and_header_line, Qmode_line)
9607 || EQ (mode_and_header_line, Qt))
9608 /* Do count the mode-line which is not included automatically by
9609 start_display. */
9610 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9611
9612 bidi_unshelve_cache (itdata, 0);
9613
9614 if (old_buffer)
9615 set_buffer_internal (old_buffer);
9616
9617 return Fcons (make_number (x), make_number (y));
9618 }
9619 \f
9620 /***********************************************************************
9621 Messages
9622 ***********************************************************************/
9623
9624
9625 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9626 to *Messages*. */
9627
9628 void
9629 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9630 {
9631 Lisp_Object args[3];
9632 Lisp_Object msg, fmt;
9633 char *buffer;
9634 ptrdiff_t len;
9635 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9636 USE_SAFE_ALLOCA;
9637
9638 fmt = msg = Qnil;
9639 GCPRO4 (fmt, msg, arg1, arg2);
9640
9641 args[0] = fmt = build_string (format);
9642 args[1] = arg1;
9643 args[2] = arg2;
9644 msg = Fformat (3, args);
9645
9646 len = SBYTES (msg) + 1;
9647 buffer = SAFE_ALLOCA (len);
9648 memcpy (buffer, SDATA (msg), len);
9649
9650 message_dolog (buffer, len - 1, 1, 0);
9651 SAFE_FREE ();
9652
9653 UNGCPRO;
9654 }
9655
9656
9657 /* Output a newline in the *Messages* buffer if "needs" one. */
9658
9659 void
9660 message_log_maybe_newline (void)
9661 {
9662 if (message_log_need_newline)
9663 message_dolog ("", 0, 1, 0);
9664 }
9665
9666
9667 /* Add a string M of length NBYTES to the message log, optionally
9668 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9669 true, means interpret the contents of M as multibyte. This
9670 function calls low-level routines in order to bypass text property
9671 hooks, etc. which might not be safe to run.
9672
9673 This may GC (insert may run before/after change hooks),
9674 so the buffer M must NOT point to a Lisp string. */
9675
9676 void
9677 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9678 {
9679 const unsigned char *msg = (const unsigned char *) m;
9680
9681 if (!NILP (Vmemory_full))
9682 return;
9683
9684 if (!NILP (Vmessage_log_max))
9685 {
9686 struct buffer *oldbuf;
9687 Lisp_Object oldpoint, oldbegv, oldzv;
9688 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9689 ptrdiff_t point_at_end = 0;
9690 ptrdiff_t zv_at_end = 0;
9691 Lisp_Object old_deactivate_mark;
9692 struct gcpro gcpro1;
9693
9694 old_deactivate_mark = Vdeactivate_mark;
9695 oldbuf = current_buffer;
9696
9697 /* Ensure the Messages buffer exists, and switch to it.
9698 If we created it, set the major-mode. */
9699 {
9700 int newbuffer = 0;
9701 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9702
9703 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9704
9705 if (newbuffer
9706 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9707 call0 (intern ("messages-buffer-mode"));
9708 }
9709
9710 bset_undo_list (current_buffer, Qt);
9711 bset_cache_long_scans (current_buffer, Qnil);
9712
9713 oldpoint = message_dolog_marker1;
9714 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9715 oldbegv = message_dolog_marker2;
9716 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9717 oldzv = message_dolog_marker3;
9718 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9719 GCPRO1 (old_deactivate_mark);
9720
9721 if (PT == Z)
9722 point_at_end = 1;
9723 if (ZV == Z)
9724 zv_at_end = 1;
9725
9726 BEGV = BEG;
9727 BEGV_BYTE = BEG_BYTE;
9728 ZV = Z;
9729 ZV_BYTE = Z_BYTE;
9730 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9731
9732 /* Insert the string--maybe converting multibyte to single byte
9733 or vice versa, so that all the text fits the buffer. */
9734 if (multibyte
9735 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9736 {
9737 ptrdiff_t i;
9738 int c, char_bytes;
9739 char work[1];
9740
9741 /* Convert a multibyte string to single-byte
9742 for the *Message* buffer. */
9743 for (i = 0; i < nbytes; i += char_bytes)
9744 {
9745 c = string_char_and_length (msg + i, &char_bytes);
9746 work[0] = (ASCII_CHAR_P (c)
9747 ? c
9748 : multibyte_char_to_unibyte (c));
9749 insert_1_both (work, 1, 1, 1, 0, 0);
9750 }
9751 }
9752 else if (! multibyte
9753 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9754 {
9755 ptrdiff_t i;
9756 int c, char_bytes;
9757 unsigned char str[MAX_MULTIBYTE_LENGTH];
9758 /* Convert a single-byte string to multibyte
9759 for the *Message* buffer. */
9760 for (i = 0; i < nbytes; i++)
9761 {
9762 c = msg[i];
9763 MAKE_CHAR_MULTIBYTE (c);
9764 char_bytes = CHAR_STRING (c, str);
9765 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9766 }
9767 }
9768 else if (nbytes)
9769 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9770
9771 if (nlflag)
9772 {
9773 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9774 printmax_t dups;
9775
9776 insert_1_both ("\n", 1, 1, 1, 0, 0);
9777
9778 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9779 this_bol = PT;
9780 this_bol_byte = PT_BYTE;
9781
9782 /* See if this line duplicates the previous one.
9783 If so, combine duplicates. */
9784 if (this_bol > BEG)
9785 {
9786 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9787 prev_bol = PT;
9788 prev_bol_byte = PT_BYTE;
9789
9790 dups = message_log_check_duplicate (prev_bol_byte,
9791 this_bol_byte);
9792 if (dups)
9793 {
9794 del_range_both (prev_bol, prev_bol_byte,
9795 this_bol, this_bol_byte, 0);
9796 if (dups > 1)
9797 {
9798 char dupstr[sizeof " [ times]"
9799 + INT_STRLEN_BOUND (printmax_t)];
9800
9801 /* If you change this format, don't forget to also
9802 change message_log_check_duplicate. */
9803 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9804 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9805 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9806 }
9807 }
9808 }
9809
9810 /* If we have more than the desired maximum number of lines
9811 in the *Messages* buffer now, delete the oldest ones.
9812 This is safe because we don't have undo in this buffer. */
9813
9814 if (NATNUMP (Vmessage_log_max))
9815 {
9816 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9817 -XFASTINT (Vmessage_log_max) - 1, 0);
9818 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9819 }
9820 }
9821 BEGV = marker_position (oldbegv);
9822 BEGV_BYTE = marker_byte_position (oldbegv);
9823
9824 if (zv_at_end)
9825 {
9826 ZV = Z;
9827 ZV_BYTE = Z_BYTE;
9828 }
9829 else
9830 {
9831 ZV = marker_position (oldzv);
9832 ZV_BYTE = marker_byte_position (oldzv);
9833 }
9834
9835 if (point_at_end)
9836 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9837 else
9838 /* We can't do Fgoto_char (oldpoint) because it will run some
9839 Lisp code. */
9840 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9841 marker_byte_position (oldpoint));
9842
9843 UNGCPRO;
9844 unchain_marker (XMARKER (oldpoint));
9845 unchain_marker (XMARKER (oldbegv));
9846 unchain_marker (XMARKER (oldzv));
9847
9848 /* We called insert_1_both above with its 5th argument (PREPARE)
9849 zero, which prevents insert_1_both from calling
9850 prepare_to_modify_buffer, which in turns prevents us from
9851 incrementing windows_or_buffers_changed even if *Messages* is
9852 shown in some window. So we must manually set
9853 windows_or_buffers_changed here to make up for that. */
9854 windows_or_buffers_changed = old_windows_or_buffers_changed;
9855 bset_redisplay (current_buffer);
9856
9857 set_buffer_internal (oldbuf);
9858
9859 message_log_need_newline = !nlflag;
9860 Vdeactivate_mark = old_deactivate_mark;
9861 }
9862 }
9863
9864
9865 /* We are at the end of the buffer after just having inserted a newline.
9866 (Note: We depend on the fact we won't be crossing the gap.)
9867 Check to see if the most recent message looks a lot like the previous one.
9868 Return 0 if different, 1 if the new one should just replace it, or a
9869 value N > 1 if we should also append " [N times]". */
9870
9871 static intmax_t
9872 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9873 {
9874 ptrdiff_t i;
9875 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9876 int seen_dots = 0;
9877 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9878 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9879
9880 for (i = 0; i < len; i++)
9881 {
9882 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9883 seen_dots = 1;
9884 if (p1[i] != p2[i])
9885 return seen_dots;
9886 }
9887 p1 += len;
9888 if (*p1 == '\n')
9889 return 2;
9890 if (*p1++ == ' ' && *p1++ == '[')
9891 {
9892 char *pend;
9893 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9894 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9895 return n + 1;
9896 }
9897 return 0;
9898 }
9899 \f
9900
9901 /* Display an echo area message M with a specified length of NBYTES
9902 bytes. The string may include null characters. If M is not a
9903 string, clear out any existing message, and let the mini-buffer
9904 text show through.
9905
9906 This function cancels echoing. */
9907
9908 void
9909 message3 (Lisp_Object m)
9910 {
9911 struct gcpro gcpro1;
9912
9913 GCPRO1 (m);
9914 clear_message (true, true);
9915 cancel_echoing ();
9916
9917 /* First flush out any partial line written with print. */
9918 message_log_maybe_newline ();
9919 if (STRINGP (m))
9920 {
9921 ptrdiff_t nbytes = SBYTES (m);
9922 bool multibyte = STRING_MULTIBYTE (m);
9923 USE_SAFE_ALLOCA;
9924 char *buffer = SAFE_ALLOCA (nbytes);
9925 memcpy (buffer, SDATA (m), nbytes);
9926 message_dolog (buffer, nbytes, 1, multibyte);
9927 SAFE_FREE ();
9928 }
9929 message3_nolog (m);
9930
9931 UNGCPRO;
9932 }
9933
9934
9935 /* The non-logging version of message3.
9936 This does not cancel echoing, because it is used for echoing.
9937 Perhaps we need to make a separate function for echoing
9938 and make this cancel echoing. */
9939
9940 void
9941 message3_nolog (Lisp_Object m)
9942 {
9943 struct frame *sf = SELECTED_FRAME ();
9944
9945 if (FRAME_INITIAL_P (sf))
9946 {
9947 if (noninteractive_need_newline)
9948 putc ('\n', stderr);
9949 noninteractive_need_newline = 0;
9950 if (STRINGP (m))
9951 {
9952 Lisp_Object s = ENCODE_SYSTEM (m);
9953
9954 fwrite (SDATA (s), SBYTES (s), 1, stderr);
9955 }
9956 if (cursor_in_echo_area == 0)
9957 fprintf (stderr, "\n");
9958 fflush (stderr);
9959 }
9960 /* Error messages get reported properly by cmd_error, so this must be just an
9961 informative message; if the frame hasn't really been initialized yet, just
9962 toss it. */
9963 else if (INTERACTIVE && sf->glyphs_initialized_p)
9964 {
9965 /* Get the frame containing the mini-buffer
9966 that the selected frame is using. */
9967 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9968 Lisp_Object frame = XWINDOW (mini_window)->frame;
9969 struct frame *f = XFRAME (frame);
9970
9971 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9972 Fmake_frame_visible (frame);
9973
9974 if (STRINGP (m) && SCHARS (m) > 0)
9975 {
9976 set_message (m);
9977 if (minibuffer_auto_raise)
9978 Fraise_frame (frame);
9979 /* Assume we are not echoing.
9980 (If we are, echo_now will override this.) */
9981 echo_message_buffer = Qnil;
9982 }
9983 else
9984 clear_message (true, true);
9985
9986 do_pending_window_change (0);
9987 echo_area_display (1);
9988 do_pending_window_change (0);
9989 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9990 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9991 }
9992 }
9993
9994
9995 /* Display a null-terminated echo area message M. If M is 0, clear
9996 out any existing message, and let the mini-buffer text show through.
9997
9998 The buffer M must continue to exist until after the echo area gets
9999 cleared or some other message gets displayed there. Do not pass
10000 text that is stored in a Lisp string. Do not pass text in a buffer
10001 that was alloca'd. */
10002
10003 void
10004 message1 (const char *m)
10005 {
10006 message3 (m ? build_unibyte_string (m) : Qnil);
10007 }
10008
10009
10010 /* The non-logging counterpart of message1. */
10011
10012 void
10013 message1_nolog (const char *m)
10014 {
10015 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10016 }
10017
10018 /* Display a message M which contains a single %s
10019 which gets replaced with STRING. */
10020
10021 void
10022 message_with_string (const char *m, Lisp_Object string, int log)
10023 {
10024 CHECK_STRING (string);
10025
10026 if (noninteractive)
10027 {
10028 if (m)
10029 {
10030 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10031 String whose data pointer might be passed to us in M. So
10032 we use a local copy. */
10033 char *fmt = xstrdup (m);
10034
10035 if (noninteractive_need_newline)
10036 putc ('\n', stderr);
10037 noninteractive_need_newline = 0;
10038 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10039 if (!cursor_in_echo_area)
10040 fprintf (stderr, "\n");
10041 fflush (stderr);
10042 xfree (fmt);
10043 }
10044 }
10045 else if (INTERACTIVE)
10046 {
10047 /* The frame whose minibuffer we're going to display the message on.
10048 It may be larger than the selected frame, so we need
10049 to use its buffer, not the selected frame's buffer. */
10050 Lisp_Object mini_window;
10051 struct frame *f, *sf = SELECTED_FRAME ();
10052
10053 /* Get the frame containing the minibuffer
10054 that the selected frame is using. */
10055 mini_window = FRAME_MINIBUF_WINDOW (sf);
10056 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10057
10058 /* Error messages get reported properly by cmd_error, so this must be
10059 just an informative message; if the frame hasn't really been
10060 initialized yet, just toss it. */
10061 if (f->glyphs_initialized_p)
10062 {
10063 Lisp_Object args[2], msg;
10064 struct gcpro gcpro1, gcpro2;
10065
10066 args[0] = build_string (m);
10067 args[1] = msg = string;
10068 GCPRO2 (args[0], msg);
10069 gcpro1.nvars = 2;
10070
10071 msg = Fformat (2, args);
10072
10073 if (log)
10074 message3 (msg);
10075 else
10076 message3_nolog (msg);
10077
10078 UNGCPRO;
10079
10080 /* Print should start at the beginning of the message
10081 buffer next time. */
10082 message_buf_print = 0;
10083 }
10084 }
10085 }
10086
10087
10088 /* Dump an informative message to the minibuf. If M is 0, clear out
10089 any existing message, and let the mini-buffer text show through. */
10090
10091 static void
10092 vmessage (const char *m, va_list ap)
10093 {
10094 if (noninteractive)
10095 {
10096 if (m)
10097 {
10098 if (noninteractive_need_newline)
10099 putc ('\n', stderr);
10100 noninteractive_need_newline = 0;
10101 vfprintf (stderr, m, ap);
10102 if (cursor_in_echo_area == 0)
10103 fprintf (stderr, "\n");
10104 fflush (stderr);
10105 }
10106 }
10107 else if (INTERACTIVE)
10108 {
10109 /* The frame whose mini-buffer we're going to display the message
10110 on. It may be larger than the selected frame, so we need to
10111 use its buffer, not the selected frame's buffer. */
10112 Lisp_Object mini_window;
10113 struct frame *f, *sf = SELECTED_FRAME ();
10114
10115 /* Get the frame containing the mini-buffer
10116 that the selected frame is using. */
10117 mini_window = FRAME_MINIBUF_WINDOW (sf);
10118 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10119
10120 /* Error messages get reported properly by cmd_error, so this must be
10121 just an informative message; if the frame hasn't really been
10122 initialized yet, just toss it. */
10123 if (f->glyphs_initialized_p)
10124 {
10125 if (m)
10126 {
10127 ptrdiff_t len;
10128 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10129 char *message_buf = alloca (maxsize + 1);
10130
10131 len = doprnt (message_buf, maxsize, m, 0, ap);
10132
10133 message3 (make_string (message_buf, len));
10134 }
10135 else
10136 message1 (0);
10137
10138 /* Print should start at the beginning of the message
10139 buffer next time. */
10140 message_buf_print = 0;
10141 }
10142 }
10143 }
10144
10145 void
10146 message (const char *m, ...)
10147 {
10148 va_list ap;
10149 va_start (ap, m);
10150 vmessage (m, ap);
10151 va_end (ap);
10152 }
10153
10154
10155 #if 0
10156 /* The non-logging version of message. */
10157
10158 void
10159 message_nolog (const char *m, ...)
10160 {
10161 Lisp_Object old_log_max;
10162 va_list ap;
10163 va_start (ap, m);
10164 old_log_max = Vmessage_log_max;
10165 Vmessage_log_max = Qnil;
10166 vmessage (m, ap);
10167 Vmessage_log_max = old_log_max;
10168 va_end (ap);
10169 }
10170 #endif
10171
10172
10173 /* Display the current message in the current mini-buffer. This is
10174 only called from error handlers in process.c, and is not time
10175 critical. */
10176
10177 void
10178 update_echo_area (void)
10179 {
10180 if (!NILP (echo_area_buffer[0]))
10181 {
10182 Lisp_Object string;
10183 string = Fcurrent_message ();
10184 message3 (string);
10185 }
10186 }
10187
10188
10189 /* Make sure echo area buffers in `echo_buffers' are live.
10190 If they aren't, make new ones. */
10191
10192 static void
10193 ensure_echo_area_buffers (void)
10194 {
10195 int i;
10196
10197 for (i = 0; i < 2; ++i)
10198 if (!BUFFERP (echo_buffer[i])
10199 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10200 {
10201 char name[30];
10202 Lisp_Object old_buffer;
10203 int j;
10204
10205 old_buffer = echo_buffer[i];
10206 echo_buffer[i] = Fget_buffer_create
10207 (make_formatted_string (name, " *Echo Area %d*", i));
10208 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10209 /* to force word wrap in echo area -
10210 it was decided to postpone this*/
10211 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10212
10213 for (j = 0; j < 2; ++j)
10214 if (EQ (old_buffer, echo_area_buffer[j]))
10215 echo_area_buffer[j] = echo_buffer[i];
10216 }
10217 }
10218
10219
10220 /* Call FN with args A1..A2 with either the current or last displayed
10221 echo_area_buffer as current buffer.
10222
10223 WHICH zero means use the current message buffer
10224 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10225 from echo_buffer[] and clear it.
10226
10227 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10228 suitable buffer from echo_buffer[] and clear it.
10229
10230 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10231 that the current message becomes the last displayed one, make
10232 choose a suitable buffer for echo_area_buffer[0], and clear it.
10233
10234 Value is what FN returns. */
10235
10236 static int
10237 with_echo_area_buffer (struct window *w, int which,
10238 int (*fn) (ptrdiff_t, Lisp_Object),
10239 ptrdiff_t a1, Lisp_Object a2)
10240 {
10241 Lisp_Object buffer;
10242 int this_one, the_other, clear_buffer_p, rc;
10243 ptrdiff_t count = SPECPDL_INDEX ();
10244
10245 /* If buffers aren't live, make new ones. */
10246 ensure_echo_area_buffers ();
10247
10248 clear_buffer_p = 0;
10249
10250 if (which == 0)
10251 this_one = 0, the_other = 1;
10252 else if (which > 0)
10253 this_one = 1, the_other = 0;
10254 else
10255 {
10256 this_one = 0, the_other = 1;
10257 clear_buffer_p = true;
10258
10259 /* We need a fresh one in case the current echo buffer equals
10260 the one containing the last displayed echo area message. */
10261 if (!NILP (echo_area_buffer[this_one])
10262 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10263 echo_area_buffer[this_one] = Qnil;
10264 }
10265
10266 /* Choose a suitable buffer from echo_buffer[] is we don't
10267 have one. */
10268 if (NILP (echo_area_buffer[this_one]))
10269 {
10270 echo_area_buffer[this_one]
10271 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10272 ? echo_buffer[the_other]
10273 : echo_buffer[this_one]);
10274 clear_buffer_p = true;
10275 }
10276
10277 buffer = echo_area_buffer[this_one];
10278
10279 /* Don't get confused by reusing the buffer used for echoing
10280 for a different purpose. */
10281 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10282 cancel_echoing ();
10283
10284 record_unwind_protect (unwind_with_echo_area_buffer,
10285 with_echo_area_buffer_unwind_data (w));
10286
10287 /* Make the echo area buffer current. Note that for display
10288 purposes, it is not necessary that the displayed window's buffer
10289 == current_buffer, except for text property lookup. So, let's
10290 only set that buffer temporarily here without doing a full
10291 Fset_window_buffer. We must also change w->pointm, though,
10292 because otherwise an assertions in unshow_buffer fails, and Emacs
10293 aborts. */
10294 set_buffer_internal_1 (XBUFFER (buffer));
10295 if (w)
10296 {
10297 wset_buffer (w, buffer);
10298 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10299 }
10300
10301 bset_undo_list (current_buffer, Qt);
10302 bset_read_only (current_buffer, Qnil);
10303 specbind (Qinhibit_read_only, Qt);
10304 specbind (Qinhibit_modification_hooks, Qt);
10305
10306 if (clear_buffer_p && Z > BEG)
10307 del_range (BEG, Z);
10308
10309 eassert (BEGV >= BEG);
10310 eassert (ZV <= Z && ZV >= BEGV);
10311
10312 rc = fn (a1, a2);
10313
10314 eassert (BEGV >= BEG);
10315 eassert (ZV <= Z && ZV >= BEGV);
10316
10317 unbind_to (count, Qnil);
10318 return rc;
10319 }
10320
10321
10322 /* Save state that should be preserved around the call to the function
10323 FN called in with_echo_area_buffer. */
10324
10325 static Lisp_Object
10326 with_echo_area_buffer_unwind_data (struct window *w)
10327 {
10328 int i = 0;
10329 Lisp_Object vector, tmp;
10330
10331 /* Reduce consing by keeping one vector in
10332 Vwith_echo_area_save_vector. */
10333 vector = Vwith_echo_area_save_vector;
10334 Vwith_echo_area_save_vector = Qnil;
10335
10336 if (NILP (vector))
10337 vector = Fmake_vector (make_number (9), Qnil);
10338
10339 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10340 ASET (vector, i, Vdeactivate_mark); ++i;
10341 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10342
10343 if (w)
10344 {
10345 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10346 ASET (vector, i, w->contents); ++i;
10347 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10348 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10349 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10350 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10351 }
10352 else
10353 {
10354 int end = i + 6;
10355 for (; i < end; ++i)
10356 ASET (vector, i, Qnil);
10357 }
10358
10359 eassert (i == ASIZE (vector));
10360 return vector;
10361 }
10362
10363
10364 /* Restore global state from VECTOR which was created by
10365 with_echo_area_buffer_unwind_data. */
10366
10367 static void
10368 unwind_with_echo_area_buffer (Lisp_Object vector)
10369 {
10370 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10371 Vdeactivate_mark = AREF (vector, 1);
10372 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10373
10374 if (WINDOWP (AREF (vector, 3)))
10375 {
10376 struct window *w;
10377 Lisp_Object buffer;
10378
10379 w = XWINDOW (AREF (vector, 3));
10380 buffer = AREF (vector, 4);
10381
10382 wset_buffer (w, buffer);
10383 set_marker_both (w->pointm, buffer,
10384 XFASTINT (AREF (vector, 5)),
10385 XFASTINT (AREF (vector, 6)));
10386 set_marker_both (w->start, buffer,
10387 XFASTINT (AREF (vector, 7)),
10388 XFASTINT (AREF (vector, 8)));
10389 }
10390
10391 Vwith_echo_area_save_vector = vector;
10392 }
10393
10394
10395 /* Set up the echo area for use by print functions. MULTIBYTE_P
10396 non-zero means we will print multibyte. */
10397
10398 void
10399 setup_echo_area_for_printing (int multibyte_p)
10400 {
10401 /* If we can't find an echo area any more, exit. */
10402 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10403 Fkill_emacs (Qnil);
10404
10405 ensure_echo_area_buffers ();
10406
10407 if (!message_buf_print)
10408 {
10409 /* A message has been output since the last time we printed.
10410 Choose a fresh echo area buffer. */
10411 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10412 echo_area_buffer[0] = echo_buffer[1];
10413 else
10414 echo_area_buffer[0] = echo_buffer[0];
10415
10416 /* Switch to that buffer and clear it. */
10417 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10418 bset_truncate_lines (current_buffer, Qnil);
10419
10420 if (Z > BEG)
10421 {
10422 ptrdiff_t count = SPECPDL_INDEX ();
10423 specbind (Qinhibit_read_only, Qt);
10424 /* Note that undo recording is always disabled. */
10425 del_range (BEG, Z);
10426 unbind_to (count, Qnil);
10427 }
10428 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10429
10430 /* Set up the buffer for the multibyteness we need. */
10431 if (multibyte_p
10432 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10433 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10434
10435 /* Raise the frame containing the echo area. */
10436 if (minibuffer_auto_raise)
10437 {
10438 struct frame *sf = SELECTED_FRAME ();
10439 Lisp_Object mini_window;
10440 mini_window = FRAME_MINIBUF_WINDOW (sf);
10441 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10442 }
10443
10444 message_log_maybe_newline ();
10445 message_buf_print = 1;
10446 }
10447 else
10448 {
10449 if (NILP (echo_area_buffer[0]))
10450 {
10451 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10452 echo_area_buffer[0] = echo_buffer[1];
10453 else
10454 echo_area_buffer[0] = echo_buffer[0];
10455 }
10456
10457 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10458 {
10459 /* Someone switched buffers between print requests. */
10460 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10461 bset_truncate_lines (current_buffer, Qnil);
10462 }
10463 }
10464 }
10465
10466
10467 /* Display an echo area message in window W. Value is non-zero if W's
10468 height is changed. If display_last_displayed_message_p is
10469 non-zero, display the message that was last displayed, otherwise
10470 display the current message. */
10471
10472 static int
10473 display_echo_area (struct window *w)
10474 {
10475 int i, no_message_p, window_height_changed_p;
10476
10477 /* Temporarily disable garbage collections while displaying the echo
10478 area. This is done because a GC can print a message itself.
10479 That message would modify the echo area buffer's contents while a
10480 redisplay of the buffer is going on, and seriously confuse
10481 redisplay. */
10482 ptrdiff_t count = inhibit_garbage_collection ();
10483
10484 /* If there is no message, we must call display_echo_area_1
10485 nevertheless because it resizes the window. But we will have to
10486 reset the echo_area_buffer in question to nil at the end because
10487 with_echo_area_buffer will sets it to an empty buffer. */
10488 i = display_last_displayed_message_p ? 1 : 0;
10489 no_message_p = NILP (echo_area_buffer[i]);
10490
10491 window_height_changed_p
10492 = with_echo_area_buffer (w, display_last_displayed_message_p,
10493 display_echo_area_1,
10494 (intptr_t) w, Qnil);
10495
10496 if (no_message_p)
10497 echo_area_buffer[i] = Qnil;
10498
10499 unbind_to (count, Qnil);
10500 return window_height_changed_p;
10501 }
10502
10503
10504 /* Helper for display_echo_area. Display the current buffer which
10505 contains the current echo area message in window W, a mini-window,
10506 a pointer to which is passed in A1. A2..A4 are currently not used.
10507 Change the height of W so that all of the message is displayed.
10508 Value is non-zero if height of W was changed. */
10509
10510 static int
10511 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10512 {
10513 intptr_t i1 = a1;
10514 struct window *w = (struct window *) i1;
10515 Lisp_Object window;
10516 struct text_pos start;
10517 int window_height_changed_p = 0;
10518
10519 /* Do this before displaying, so that we have a large enough glyph
10520 matrix for the display. If we can't get enough space for the
10521 whole text, display the last N lines. That works by setting w->start. */
10522 window_height_changed_p = resize_mini_window (w, 0);
10523
10524 /* Use the starting position chosen by resize_mini_window. */
10525 SET_TEXT_POS_FROM_MARKER (start, w->start);
10526
10527 /* Display. */
10528 clear_glyph_matrix (w->desired_matrix);
10529 XSETWINDOW (window, w);
10530 try_window (window, start, 0);
10531
10532 return window_height_changed_p;
10533 }
10534
10535
10536 /* Resize the echo area window to exactly the size needed for the
10537 currently displayed message, if there is one. If a mini-buffer
10538 is active, don't shrink it. */
10539
10540 void
10541 resize_echo_area_exactly (void)
10542 {
10543 if (BUFFERP (echo_area_buffer[0])
10544 && WINDOWP (echo_area_window))
10545 {
10546 struct window *w = XWINDOW (echo_area_window);
10547 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10548 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10549 (intptr_t) w, resize_exactly);
10550 if (resized_p)
10551 {
10552 windows_or_buffers_changed = 42;
10553 update_mode_lines = 30;
10554 redisplay_internal ();
10555 }
10556 }
10557 }
10558
10559
10560 /* Callback function for with_echo_area_buffer, when used from
10561 resize_echo_area_exactly. A1 contains a pointer to the window to
10562 resize, EXACTLY non-nil means resize the mini-window exactly to the
10563 size of the text displayed. A3 and A4 are not used. Value is what
10564 resize_mini_window returns. */
10565
10566 static int
10567 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10568 {
10569 intptr_t i1 = a1;
10570 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10571 }
10572
10573
10574 /* Resize mini-window W to fit the size of its contents. EXACT_P
10575 means size the window exactly to the size needed. Otherwise, it's
10576 only enlarged until W's buffer is empty.
10577
10578 Set W->start to the right place to begin display. If the whole
10579 contents fit, start at the beginning. Otherwise, start so as
10580 to make the end of the contents appear. This is particularly
10581 important for y-or-n-p, but seems desirable generally.
10582
10583 Value is non-zero if the window height has been changed. */
10584
10585 int
10586 resize_mini_window (struct window *w, int exact_p)
10587 {
10588 struct frame *f = XFRAME (w->frame);
10589 int window_height_changed_p = 0;
10590
10591 eassert (MINI_WINDOW_P (w));
10592
10593 /* By default, start display at the beginning. */
10594 set_marker_both (w->start, w->contents,
10595 BUF_BEGV (XBUFFER (w->contents)),
10596 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10597
10598 /* Don't resize windows while redisplaying a window; it would
10599 confuse redisplay functions when the size of the window they are
10600 displaying changes from under them. Such a resizing can happen,
10601 for instance, when which-func prints a long message while
10602 we are running fontification-functions. We're running these
10603 functions with safe_call which binds inhibit-redisplay to t. */
10604 if (!NILP (Vinhibit_redisplay))
10605 return 0;
10606
10607 /* Nil means don't try to resize. */
10608 if (NILP (Vresize_mini_windows)
10609 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10610 return 0;
10611
10612 if (!FRAME_MINIBUF_ONLY_P (f))
10613 {
10614 struct it it;
10615 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10616 + WINDOW_PIXEL_HEIGHT (w));
10617 int unit = FRAME_LINE_HEIGHT (f);
10618 int height, max_height;
10619 struct text_pos start;
10620 struct buffer *old_current_buffer = NULL;
10621
10622 if (current_buffer != XBUFFER (w->contents))
10623 {
10624 old_current_buffer = current_buffer;
10625 set_buffer_internal (XBUFFER (w->contents));
10626 }
10627
10628 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10629
10630 /* Compute the max. number of lines specified by the user. */
10631 if (FLOATP (Vmax_mini_window_height))
10632 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10633 else if (INTEGERP (Vmax_mini_window_height))
10634 max_height = XINT (Vmax_mini_window_height) * unit;
10635 else
10636 max_height = total_height / 4;
10637
10638 /* Correct that max. height if it's bogus. */
10639 max_height = clip_to_bounds (unit, max_height, total_height);
10640
10641 /* Find out the height of the text in the window. */
10642 if (it.line_wrap == TRUNCATE)
10643 height = unit;
10644 else
10645 {
10646 last_height = 0;
10647 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10648 if (it.max_ascent == 0 && it.max_descent == 0)
10649 height = it.current_y + last_height;
10650 else
10651 height = it.current_y + it.max_ascent + it.max_descent;
10652 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10653 }
10654
10655 /* Compute a suitable window start. */
10656 if (height > max_height)
10657 {
10658 height = max_height;
10659 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10660 move_it_vertically_backward (&it, height);
10661 start = it.current.pos;
10662 }
10663 else
10664 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10665 SET_MARKER_FROM_TEXT_POS (w->start, start);
10666
10667 if (EQ (Vresize_mini_windows, Qgrow_only))
10668 {
10669 /* Let it grow only, until we display an empty message, in which
10670 case the window shrinks again. */
10671 if (height > WINDOW_PIXEL_HEIGHT (w))
10672 {
10673 int old_height = WINDOW_PIXEL_HEIGHT (w);
10674
10675 FRAME_WINDOWS_FROZEN (f) = 1;
10676 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10677 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10678 }
10679 else if (height < WINDOW_PIXEL_HEIGHT (w)
10680 && (exact_p || BEGV == ZV))
10681 {
10682 int old_height = WINDOW_PIXEL_HEIGHT (w);
10683
10684 FRAME_WINDOWS_FROZEN (f) = 0;
10685 shrink_mini_window (w, 1);
10686 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10687 }
10688 }
10689 else
10690 {
10691 /* Always resize to exact size needed. */
10692 if (height > WINDOW_PIXEL_HEIGHT (w))
10693 {
10694 int old_height = WINDOW_PIXEL_HEIGHT (w);
10695
10696 FRAME_WINDOWS_FROZEN (f) = 1;
10697 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10698 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10699 }
10700 else if (height < WINDOW_PIXEL_HEIGHT (w))
10701 {
10702 int old_height = WINDOW_PIXEL_HEIGHT (w);
10703
10704 FRAME_WINDOWS_FROZEN (f) = 0;
10705 shrink_mini_window (w, 1);
10706
10707 if (height)
10708 {
10709 FRAME_WINDOWS_FROZEN (f) = 1;
10710 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10711 }
10712
10713 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10714 }
10715 }
10716
10717 if (old_current_buffer)
10718 set_buffer_internal (old_current_buffer);
10719 }
10720
10721 return window_height_changed_p;
10722 }
10723
10724
10725 /* Value is the current message, a string, or nil if there is no
10726 current message. */
10727
10728 Lisp_Object
10729 current_message (void)
10730 {
10731 Lisp_Object msg;
10732
10733 if (!BUFFERP (echo_area_buffer[0]))
10734 msg = Qnil;
10735 else
10736 {
10737 with_echo_area_buffer (0, 0, current_message_1,
10738 (intptr_t) &msg, Qnil);
10739 if (NILP (msg))
10740 echo_area_buffer[0] = Qnil;
10741 }
10742
10743 return msg;
10744 }
10745
10746
10747 static int
10748 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10749 {
10750 intptr_t i1 = a1;
10751 Lisp_Object *msg = (Lisp_Object *) i1;
10752
10753 if (Z > BEG)
10754 *msg = make_buffer_string (BEG, Z, 1);
10755 else
10756 *msg = Qnil;
10757 return 0;
10758 }
10759
10760
10761 /* Push the current message on Vmessage_stack for later restoration
10762 by restore_message. Value is non-zero if the current message isn't
10763 empty. This is a relatively infrequent operation, so it's not
10764 worth optimizing. */
10765
10766 bool
10767 push_message (void)
10768 {
10769 Lisp_Object msg = current_message ();
10770 Vmessage_stack = Fcons (msg, Vmessage_stack);
10771 return STRINGP (msg);
10772 }
10773
10774
10775 /* Restore message display from the top of Vmessage_stack. */
10776
10777 void
10778 restore_message (void)
10779 {
10780 eassert (CONSP (Vmessage_stack));
10781 message3_nolog (XCAR (Vmessage_stack));
10782 }
10783
10784
10785 /* Handler for unwind-protect calling pop_message. */
10786
10787 void
10788 pop_message_unwind (void)
10789 {
10790 /* Pop the top-most entry off Vmessage_stack. */
10791 eassert (CONSP (Vmessage_stack));
10792 Vmessage_stack = XCDR (Vmessage_stack);
10793 }
10794
10795
10796 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10797 exits. If the stack is not empty, we have a missing pop_message
10798 somewhere. */
10799
10800 void
10801 check_message_stack (void)
10802 {
10803 if (!NILP (Vmessage_stack))
10804 emacs_abort ();
10805 }
10806
10807
10808 /* Truncate to NCHARS what will be displayed in the echo area the next
10809 time we display it---but don't redisplay it now. */
10810
10811 void
10812 truncate_echo_area (ptrdiff_t nchars)
10813 {
10814 if (nchars == 0)
10815 echo_area_buffer[0] = Qnil;
10816 else if (!noninteractive
10817 && INTERACTIVE
10818 && !NILP (echo_area_buffer[0]))
10819 {
10820 struct frame *sf = SELECTED_FRAME ();
10821 /* Error messages get reported properly by cmd_error, so this must be
10822 just an informative message; if the frame hasn't really been
10823 initialized yet, just toss it. */
10824 if (sf->glyphs_initialized_p)
10825 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10826 }
10827 }
10828
10829
10830 /* Helper function for truncate_echo_area. Truncate the current
10831 message to at most NCHARS characters. */
10832
10833 static int
10834 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10835 {
10836 if (BEG + nchars < Z)
10837 del_range (BEG + nchars, Z);
10838 if (Z == BEG)
10839 echo_area_buffer[0] = Qnil;
10840 return 0;
10841 }
10842
10843 /* Set the current message to STRING. */
10844
10845 static void
10846 set_message (Lisp_Object string)
10847 {
10848 eassert (STRINGP (string));
10849
10850 message_enable_multibyte = STRING_MULTIBYTE (string);
10851
10852 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10853 message_buf_print = 0;
10854 help_echo_showing_p = 0;
10855
10856 if (STRINGP (Vdebug_on_message)
10857 && STRINGP (string)
10858 && fast_string_match (Vdebug_on_message, string) >= 0)
10859 call_debugger (list2 (Qerror, string));
10860 }
10861
10862
10863 /* Helper function for set_message. First argument is ignored and second
10864 argument has the same meaning as for set_message.
10865 This function is called with the echo area buffer being current. */
10866
10867 static int
10868 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10869 {
10870 eassert (STRINGP (string));
10871
10872 /* Change multibyteness of the echo buffer appropriately. */
10873 if (message_enable_multibyte
10874 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10875 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10876
10877 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10878 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10879 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10880
10881 /* Insert new message at BEG. */
10882 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10883
10884 /* This function takes care of single/multibyte conversion.
10885 We just have to ensure that the echo area buffer has the right
10886 setting of enable_multibyte_characters. */
10887 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10888
10889 return 0;
10890 }
10891
10892
10893 /* Clear messages. CURRENT_P non-zero means clear the current
10894 message. LAST_DISPLAYED_P non-zero means clear the message
10895 last displayed. */
10896
10897 void
10898 clear_message (bool current_p, bool last_displayed_p)
10899 {
10900 if (current_p)
10901 {
10902 echo_area_buffer[0] = Qnil;
10903 message_cleared_p = true;
10904 }
10905
10906 if (last_displayed_p)
10907 echo_area_buffer[1] = Qnil;
10908
10909 message_buf_print = 0;
10910 }
10911
10912 /* Clear garbaged frames.
10913
10914 This function is used where the old redisplay called
10915 redraw_garbaged_frames which in turn called redraw_frame which in
10916 turn called clear_frame. The call to clear_frame was a source of
10917 flickering. I believe a clear_frame is not necessary. It should
10918 suffice in the new redisplay to invalidate all current matrices,
10919 and ensure a complete redisplay of all windows. */
10920
10921 static void
10922 clear_garbaged_frames (void)
10923 {
10924 if (frame_garbaged)
10925 {
10926 Lisp_Object tail, frame;
10927
10928 FOR_EACH_FRAME (tail, frame)
10929 {
10930 struct frame *f = XFRAME (frame);
10931
10932 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10933 {
10934 if (f->resized_p)
10935 redraw_frame (f);
10936 else
10937 clear_current_matrices (f);
10938 fset_redisplay (f);
10939 f->garbaged = false;
10940 f->resized_p = false;
10941 }
10942 }
10943
10944 frame_garbaged = false;
10945 }
10946 }
10947
10948
10949 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10950 is non-zero update selected_frame. Value is non-zero if the
10951 mini-windows height has been changed. */
10952
10953 static int
10954 echo_area_display (int update_frame_p)
10955 {
10956 Lisp_Object mini_window;
10957 struct window *w;
10958 struct frame *f;
10959 int window_height_changed_p = 0;
10960 struct frame *sf = SELECTED_FRAME ();
10961
10962 mini_window = FRAME_MINIBUF_WINDOW (sf);
10963 w = XWINDOW (mini_window);
10964 f = XFRAME (WINDOW_FRAME (w));
10965
10966 /* Don't display if frame is invisible or not yet initialized. */
10967 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10968 return 0;
10969
10970 #ifdef HAVE_WINDOW_SYSTEM
10971 /* When Emacs starts, selected_frame may be the initial terminal
10972 frame. If we let this through, a message would be displayed on
10973 the terminal. */
10974 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10975 return 0;
10976 #endif /* HAVE_WINDOW_SYSTEM */
10977
10978 /* Redraw garbaged frames. */
10979 clear_garbaged_frames ();
10980
10981 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10982 {
10983 echo_area_window = mini_window;
10984 window_height_changed_p = display_echo_area (w);
10985 w->must_be_updated_p = true;
10986
10987 /* Update the display, unless called from redisplay_internal.
10988 Also don't update the screen during redisplay itself. The
10989 update will happen at the end of redisplay, and an update
10990 here could cause confusion. */
10991 if (update_frame_p && !redisplaying_p)
10992 {
10993 int n = 0;
10994
10995 /* If the display update has been interrupted by pending
10996 input, update mode lines in the frame. Due to the
10997 pending input, it might have been that redisplay hasn't
10998 been called, so that mode lines above the echo area are
10999 garbaged. This looks odd, so we prevent it here. */
11000 if (!display_completed)
11001 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11002
11003 if (window_height_changed_p
11004 /* Don't do this if Emacs is shutting down. Redisplay
11005 needs to run hooks. */
11006 && !NILP (Vrun_hooks))
11007 {
11008 /* Must update other windows. Likewise as in other
11009 cases, don't let this update be interrupted by
11010 pending input. */
11011 ptrdiff_t count = SPECPDL_INDEX ();
11012 specbind (Qredisplay_dont_pause, Qt);
11013 windows_or_buffers_changed = 44;
11014 redisplay_internal ();
11015 unbind_to (count, Qnil);
11016 }
11017 else if (FRAME_WINDOW_P (f) && n == 0)
11018 {
11019 /* Window configuration is the same as before.
11020 Can do with a display update of the echo area,
11021 unless we displayed some mode lines. */
11022 update_single_window (w, 1);
11023 flush_frame (f);
11024 }
11025 else
11026 update_frame (f, 1, 1);
11027
11028 /* If cursor is in the echo area, make sure that the next
11029 redisplay displays the minibuffer, so that the cursor will
11030 be replaced with what the minibuffer wants. */
11031 if (cursor_in_echo_area)
11032 wset_redisplay (XWINDOW (mini_window));
11033 }
11034 }
11035 else if (!EQ (mini_window, selected_window))
11036 wset_redisplay (XWINDOW (mini_window));
11037
11038 /* Last displayed message is now the current message. */
11039 echo_area_buffer[1] = echo_area_buffer[0];
11040 /* Inform read_char that we're not echoing. */
11041 echo_message_buffer = Qnil;
11042
11043 /* Prevent redisplay optimization in redisplay_internal by resetting
11044 this_line_start_pos. This is done because the mini-buffer now
11045 displays the message instead of its buffer text. */
11046 if (EQ (mini_window, selected_window))
11047 CHARPOS (this_line_start_pos) = 0;
11048
11049 return window_height_changed_p;
11050 }
11051
11052 /* Nonzero if W's buffer was changed but not saved. */
11053
11054 static int
11055 window_buffer_changed (struct window *w)
11056 {
11057 struct buffer *b = XBUFFER (w->contents);
11058
11059 eassert (BUFFER_LIVE_P (b));
11060
11061 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11062 }
11063
11064 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11065
11066 static int
11067 mode_line_update_needed (struct window *w)
11068 {
11069 return (w->column_number_displayed != -1
11070 && !(PT == w->last_point && !window_outdated (w))
11071 && (w->column_number_displayed != current_column ()));
11072 }
11073
11074 /* Nonzero if window start of W is frozen and may not be changed during
11075 redisplay. */
11076
11077 static bool
11078 window_frozen_p (struct window *w)
11079 {
11080 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11081 {
11082 Lisp_Object window;
11083
11084 XSETWINDOW (window, w);
11085 if (MINI_WINDOW_P (w))
11086 return 0;
11087 else if (EQ (window, selected_window))
11088 return 0;
11089 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11090 && EQ (window, Vminibuf_scroll_window))
11091 /* This special window can't be frozen too. */
11092 return 0;
11093 else
11094 return 1;
11095 }
11096 return 0;
11097 }
11098
11099 /***********************************************************************
11100 Mode Lines and Frame Titles
11101 ***********************************************************************/
11102
11103 /* A buffer for constructing non-propertized mode-line strings and
11104 frame titles in it; allocated from the heap in init_xdisp and
11105 resized as needed in store_mode_line_noprop_char. */
11106
11107 static char *mode_line_noprop_buf;
11108
11109 /* The buffer's end, and a current output position in it. */
11110
11111 static char *mode_line_noprop_buf_end;
11112 static char *mode_line_noprop_ptr;
11113
11114 #define MODE_LINE_NOPROP_LEN(start) \
11115 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11116
11117 static enum {
11118 MODE_LINE_DISPLAY = 0,
11119 MODE_LINE_TITLE,
11120 MODE_LINE_NOPROP,
11121 MODE_LINE_STRING
11122 } mode_line_target;
11123
11124 /* Alist that caches the results of :propertize.
11125 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11126 static Lisp_Object mode_line_proptrans_alist;
11127
11128 /* List of strings making up the mode-line. */
11129 static Lisp_Object mode_line_string_list;
11130
11131 /* Base face property when building propertized mode line string. */
11132 static Lisp_Object mode_line_string_face;
11133 static Lisp_Object mode_line_string_face_prop;
11134
11135
11136 /* Unwind data for mode line strings */
11137
11138 static Lisp_Object Vmode_line_unwind_vector;
11139
11140 static Lisp_Object
11141 format_mode_line_unwind_data (struct frame *target_frame,
11142 struct buffer *obuf,
11143 Lisp_Object owin,
11144 int save_proptrans)
11145 {
11146 Lisp_Object vector, tmp;
11147
11148 /* Reduce consing by keeping one vector in
11149 Vwith_echo_area_save_vector. */
11150 vector = Vmode_line_unwind_vector;
11151 Vmode_line_unwind_vector = Qnil;
11152
11153 if (NILP (vector))
11154 vector = Fmake_vector (make_number (10), Qnil);
11155
11156 ASET (vector, 0, make_number (mode_line_target));
11157 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11158 ASET (vector, 2, mode_line_string_list);
11159 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11160 ASET (vector, 4, mode_line_string_face);
11161 ASET (vector, 5, mode_line_string_face_prop);
11162
11163 if (obuf)
11164 XSETBUFFER (tmp, obuf);
11165 else
11166 tmp = Qnil;
11167 ASET (vector, 6, tmp);
11168 ASET (vector, 7, owin);
11169 if (target_frame)
11170 {
11171 /* Similarly to `with-selected-window', if the operation selects
11172 a window on another frame, we must restore that frame's
11173 selected window, and (for a tty) the top-frame. */
11174 ASET (vector, 8, target_frame->selected_window);
11175 if (FRAME_TERMCAP_P (target_frame))
11176 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11177 }
11178
11179 return vector;
11180 }
11181
11182 static void
11183 unwind_format_mode_line (Lisp_Object vector)
11184 {
11185 Lisp_Object old_window = AREF (vector, 7);
11186 Lisp_Object target_frame_window = AREF (vector, 8);
11187 Lisp_Object old_top_frame = AREF (vector, 9);
11188
11189 mode_line_target = XINT (AREF (vector, 0));
11190 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11191 mode_line_string_list = AREF (vector, 2);
11192 if (! EQ (AREF (vector, 3), Qt))
11193 mode_line_proptrans_alist = AREF (vector, 3);
11194 mode_line_string_face = AREF (vector, 4);
11195 mode_line_string_face_prop = AREF (vector, 5);
11196
11197 /* Select window before buffer, since it may change the buffer. */
11198 if (!NILP (old_window))
11199 {
11200 /* If the operation that we are unwinding had selected a window
11201 on a different frame, reset its frame-selected-window. For a
11202 text terminal, reset its top-frame if necessary. */
11203 if (!NILP (target_frame_window))
11204 {
11205 Lisp_Object frame
11206 = WINDOW_FRAME (XWINDOW (target_frame_window));
11207
11208 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11209 Fselect_window (target_frame_window, Qt);
11210
11211 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11212 Fselect_frame (old_top_frame, Qt);
11213 }
11214
11215 Fselect_window (old_window, Qt);
11216 }
11217
11218 if (!NILP (AREF (vector, 6)))
11219 {
11220 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11221 ASET (vector, 6, Qnil);
11222 }
11223
11224 Vmode_line_unwind_vector = vector;
11225 }
11226
11227
11228 /* Store a single character C for the frame title in mode_line_noprop_buf.
11229 Re-allocate mode_line_noprop_buf if necessary. */
11230
11231 static void
11232 store_mode_line_noprop_char (char c)
11233 {
11234 /* If output position has reached the end of the allocated buffer,
11235 increase the buffer's size. */
11236 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11237 {
11238 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11239 ptrdiff_t size = len;
11240 mode_line_noprop_buf =
11241 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11242 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11243 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11244 }
11245
11246 *mode_line_noprop_ptr++ = c;
11247 }
11248
11249
11250 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11251 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11252 characters that yield more columns than PRECISION; PRECISION <= 0
11253 means copy the whole string. Pad with spaces until FIELD_WIDTH
11254 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11255 pad. Called from display_mode_element when it is used to build a
11256 frame title. */
11257
11258 static int
11259 store_mode_line_noprop (const char *string, int field_width, int precision)
11260 {
11261 const unsigned char *str = (const unsigned char *) string;
11262 int n = 0;
11263 ptrdiff_t dummy, nbytes;
11264
11265 /* Copy at most PRECISION chars from STR. */
11266 nbytes = strlen (string);
11267 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11268 while (nbytes--)
11269 store_mode_line_noprop_char (*str++);
11270
11271 /* Fill up with spaces until FIELD_WIDTH reached. */
11272 while (field_width > 0
11273 && n < field_width)
11274 {
11275 store_mode_line_noprop_char (' ');
11276 ++n;
11277 }
11278
11279 return n;
11280 }
11281
11282 /***********************************************************************
11283 Frame Titles
11284 ***********************************************************************/
11285
11286 #ifdef HAVE_WINDOW_SYSTEM
11287
11288 /* Set the title of FRAME, if it has changed. The title format is
11289 Vicon_title_format if FRAME is iconified, otherwise it is
11290 frame_title_format. */
11291
11292 static void
11293 x_consider_frame_title (Lisp_Object frame)
11294 {
11295 struct frame *f = XFRAME (frame);
11296
11297 if (FRAME_WINDOW_P (f)
11298 || FRAME_MINIBUF_ONLY_P (f)
11299 || f->explicit_name)
11300 {
11301 /* Do we have more than one visible frame on this X display? */
11302 Lisp_Object tail, other_frame, fmt;
11303 ptrdiff_t title_start;
11304 char *title;
11305 ptrdiff_t len;
11306 struct it it;
11307 ptrdiff_t count = SPECPDL_INDEX ();
11308
11309 FOR_EACH_FRAME (tail, other_frame)
11310 {
11311 struct frame *tf = XFRAME (other_frame);
11312
11313 if (tf != f
11314 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11315 && !FRAME_MINIBUF_ONLY_P (tf)
11316 && !EQ (other_frame, tip_frame)
11317 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11318 break;
11319 }
11320
11321 /* Set global variable indicating that multiple frames exist. */
11322 multiple_frames = CONSP (tail);
11323
11324 /* Switch to the buffer of selected window of the frame. Set up
11325 mode_line_target so that display_mode_element will output into
11326 mode_line_noprop_buf; then display the title. */
11327 record_unwind_protect (unwind_format_mode_line,
11328 format_mode_line_unwind_data
11329 (f, current_buffer, selected_window, 0));
11330
11331 Fselect_window (f->selected_window, Qt);
11332 set_buffer_internal_1
11333 (XBUFFER (XWINDOW (f->selected_window)->contents));
11334 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11335
11336 mode_line_target = MODE_LINE_TITLE;
11337 title_start = MODE_LINE_NOPROP_LEN (0);
11338 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11339 NULL, DEFAULT_FACE_ID);
11340 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11341 len = MODE_LINE_NOPROP_LEN (title_start);
11342 title = mode_line_noprop_buf + title_start;
11343 unbind_to (count, Qnil);
11344
11345 /* Set the title only if it's changed. This avoids consing in
11346 the common case where it hasn't. (If it turns out that we've
11347 already wasted too much time by walking through the list with
11348 display_mode_element, then we might need to optimize at a
11349 higher level than this.) */
11350 if (! STRINGP (f->name)
11351 || SBYTES (f->name) != len
11352 || memcmp (title, SDATA (f->name), len) != 0)
11353 x_implicitly_set_name (f, make_string (title, len), Qnil);
11354 }
11355 }
11356
11357 #endif /* not HAVE_WINDOW_SYSTEM */
11358
11359 \f
11360 /***********************************************************************
11361 Menu Bars
11362 ***********************************************************************/
11363
11364 /* Non-zero if we will not redisplay all visible windows. */
11365 #define REDISPLAY_SOME_P() \
11366 ((windows_or_buffers_changed == 0 \
11367 || windows_or_buffers_changed == REDISPLAY_SOME) \
11368 && (update_mode_lines == 0 \
11369 || update_mode_lines == REDISPLAY_SOME))
11370
11371 /* Prepare for redisplay by updating menu-bar item lists when
11372 appropriate. This can call eval. */
11373
11374 static void
11375 prepare_menu_bars (void)
11376 {
11377 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11378 bool some_windows = REDISPLAY_SOME_P ();
11379 struct gcpro gcpro1, gcpro2;
11380 Lisp_Object tooltip_frame;
11381
11382 #ifdef HAVE_WINDOW_SYSTEM
11383 tooltip_frame = tip_frame;
11384 #else
11385 tooltip_frame = Qnil;
11386 #endif
11387
11388 if (FUNCTIONP (Vpre_redisplay_function))
11389 {
11390 Lisp_Object windows = all_windows ? Qt : Qnil;
11391 if (all_windows && some_windows)
11392 {
11393 Lisp_Object ws = window_list ();
11394 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11395 {
11396 Lisp_Object this = XCAR (ws);
11397 struct window *w = XWINDOW (this);
11398 if (w->redisplay
11399 || XFRAME (w->frame)->redisplay
11400 || XBUFFER (w->contents)->text->redisplay)
11401 {
11402 windows = Fcons (this, windows);
11403 }
11404 }
11405 }
11406 safe_call1 (Vpre_redisplay_function, windows);
11407 }
11408
11409 /* Update all frame titles based on their buffer names, etc. We do
11410 this before the menu bars so that the buffer-menu will show the
11411 up-to-date frame titles. */
11412 #ifdef HAVE_WINDOW_SYSTEM
11413 if (all_windows)
11414 {
11415 Lisp_Object tail, frame;
11416
11417 FOR_EACH_FRAME (tail, frame)
11418 {
11419 struct frame *f = XFRAME (frame);
11420 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11421 if (some_windows
11422 && !f->redisplay
11423 && !w->redisplay
11424 && !XBUFFER (w->contents)->text->redisplay)
11425 continue;
11426
11427 if (!EQ (frame, tooltip_frame)
11428 && (FRAME_ICONIFIED_P (f)
11429 || FRAME_VISIBLE_P (f) == 1
11430 /* Exclude TTY frames that are obscured because they
11431 are not the top frame on their console. This is
11432 because x_consider_frame_title actually switches
11433 to the frame, which for TTY frames means it is
11434 marked as garbaged, and will be completely
11435 redrawn on the next redisplay cycle. This causes
11436 TTY frames to be completely redrawn, when there
11437 are more than one of them, even though nothing
11438 should be changed on display. */
11439 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11440 x_consider_frame_title (frame);
11441 }
11442 }
11443 #endif /* HAVE_WINDOW_SYSTEM */
11444
11445 /* Update the menu bar item lists, if appropriate. This has to be
11446 done before any actual redisplay or generation of display lines. */
11447
11448 if (all_windows)
11449 {
11450 Lisp_Object tail, frame;
11451 ptrdiff_t count = SPECPDL_INDEX ();
11452 /* 1 means that update_menu_bar has run its hooks
11453 so any further calls to update_menu_bar shouldn't do so again. */
11454 int menu_bar_hooks_run = 0;
11455
11456 record_unwind_save_match_data ();
11457
11458 FOR_EACH_FRAME (tail, frame)
11459 {
11460 struct frame *f = XFRAME (frame);
11461 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11462
11463 /* Ignore tooltip frame. */
11464 if (EQ (frame, tooltip_frame))
11465 continue;
11466
11467 if (some_windows
11468 && !f->redisplay
11469 && !w->redisplay
11470 && !XBUFFER (w->contents)->text->redisplay)
11471 continue;
11472
11473 /* If a window on this frame changed size, report that to
11474 the user and clear the size-change flag. */
11475 if (FRAME_WINDOW_SIZES_CHANGED (f))
11476 {
11477 Lisp_Object functions;
11478
11479 /* Clear flag first in case we get an error below. */
11480 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11481 functions = Vwindow_size_change_functions;
11482 GCPRO2 (tail, functions);
11483
11484 while (CONSP (functions))
11485 {
11486 if (!EQ (XCAR (functions), Qt))
11487 call1 (XCAR (functions), frame);
11488 functions = XCDR (functions);
11489 }
11490 UNGCPRO;
11491 }
11492
11493 GCPRO1 (tail);
11494 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11495 #ifdef HAVE_WINDOW_SYSTEM
11496 update_tool_bar (f, 0);
11497 #endif
11498 #ifdef HAVE_NS
11499 if (windows_or_buffers_changed
11500 && FRAME_NS_P (f))
11501 ns_set_doc_edited
11502 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11503 #endif
11504 UNGCPRO;
11505 }
11506
11507 unbind_to (count, Qnil);
11508 }
11509 else
11510 {
11511 struct frame *sf = SELECTED_FRAME ();
11512 update_menu_bar (sf, 1, 0);
11513 #ifdef HAVE_WINDOW_SYSTEM
11514 update_tool_bar (sf, 1);
11515 #endif
11516 }
11517 }
11518
11519
11520 /* Update the menu bar item list for frame F. This has to be done
11521 before we start to fill in any display lines, because it can call
11522 eval.
11523
11524 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11525
11526 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11527 already ran the menu bar hooks for this redisplay, so there
11528 is no need to run them again. The return value is the
11529 updated value of this flag, to pass to the next call. */
11530
11531 static int
11532 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11533 {
11534 Lisp_Object window;
11535 register struct window *w;
11536
11537 /* If called recursively during a menu update, do nothing. This can
11538 happen when, for instance, an activate-menubar-hook causes a
11539 redisplay. */
11540 if (inhibit_menubar_update)
11541 return hooks_run;
11542
11543 window = FRAME_SELECTED_WINDOW (f);
11544 w = XWINDOW (window);
11545
11546 if (FRAME_WINDOW_P (f)
11547 ?
11548 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11549 || defined (HAVE_NS) || defined (USE_GTK)
11550 FRAME_EXTERNAL_MENU_BAR (f)
11551 #else
11552 FRAME_MENU_BAR_LINES (f) > 0
11553 #endif
11554 : FRAME_MENU_BAR_LINES (f) > 0)
11555 {
11556 /* If the user has switched buffers or windows, we need to
11557 recompute to reflect the new bindings. But we'll
11558 recompute when update_mode_lines is set too; that means
11559 that people can use force-mode-line-update to request
11560 that the menu bar be recomputed. The adverse effect on
11561 the rest of the redisplay algorithm is about the same as
11562 windows_or_buffers_changed anyway. */
11563 if (windows_or_buffers_changed
11564 /* This used to test w->update_mode_line, but we believe
11565 there is no need to recompute the menu in that case. */
11566 || update_mode_lines
11567 || window_buffer_changed (w))
11568 {
11569 struct buffer *prev = current_buffer;
11570 ptrdiff_t count = SPECPDL_INDEX ();
11571
11572 specbind (Qinhibit_menubar_update, Qt);
11573
11574 set_buffer_internal_1 (XBUFFER (w->contents));
11575 if (save_match_data)
11576 record_unwind_save_match_data ();
11577 if (NILP (Voverriding_local_map_menu_flag))
11578 {
11579 specbind (Qoverriding_terminal_local_map, Qnil);
11580 specbind (Qoverriding_local_map, Qnil);
11581 }
11582
11583 if (!hooks_run)
11584 {
11585 /* Run the Lucid hook. */
11586 safe_run_hooks (Qactivate_menubar_hook);
11587
11588 /* If it has changed current-menubar from previous value,
11589 really recompute the menu-bar from the value. */
11590 if (! NILP (Vlucid_menu_bar_dirty_flag))
11591 call0 (Qrecompute_lucid_menubar);
11592
11593 safe_run_hooks (Qmenu_bar_update_hook);
11594
11595 hooks_run = 1;
11596 }
11597
11598 XSETFRAME (Vmenu_updating_frame, f);
11599 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11600
11601 /* Redisplay the menu bar in case we changed it. */
11602 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11603 || defined (HAVE_NS) || defined (USE_GTK)
11604 if (FRAME_WINDOW_P (f))
11605 {
11606 #if defined (HAVE_NS)
11607 /* All frames on Mac OS share the same menubar. So only
11608 the selected frame should be allowed to set it. */
11609 if (f == SELECTED_FRAME ())
11610 #endif
11611 set_frame_menubar (f, 0, 0);
11612 }
11613 else
11614 /* On a terminal screen, the menu bar is an ordinary screen
11615 line, and this makes it get updated. */
11616 w->update_mode_line = 1;
11617 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11618 /* In the non-toolkit version, the menu bar is an ordinary screen
11619 line, and this makes it get updated. */
11620 w->update_mode_line = 1;
11621 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11622
11623 unbind_to (count, Qnil);
11624 set_buffer_internal_1 (prev);
11625 }
11626 }
11627
11628 return hooks_run;
11629 }
11630
11631 /***********************************************************************
11632 Tool-bars
11633 ***********************************************************************/
11634
11635 #ifdef HAVE_WINDOW_SYSTEM
11636
11637 /* Tool-bar item index of the item on which a mouse button was pressed
11638 or -1. */
11639
11640 int last_tool_bar_item;
11641
11642 /* Select `frame' temporarily without running all the code in
11643 do_switch_frame.
11644 FIXME: Maybe do_switch_frame should be trimmed down similarly
11645 when `norecord' is set. */
11646 static void
11647 fast_set_selected_frame (Lisp_Object frame)
11648 {
11649 if (!EQ (selected_frame, frame))
11650 {
11651 selected_frame = frame;
11652 selected_window = XFRAME (frame)->selected_window;
11653 }
11654 }
11655
11656 /* Update the tool-bar item list for frame F. This has to be done
11657 before we start to fill in any display lines. Called from
11658 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11659 and restore it here. */
11660
11661 static void
11662 update_tool_bar (struct frame *f, int save_match_data)
11663 {
11664 #if defined (USE_GTK) || defined (HAVE_NS)
11665 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11666 #else
11667 int do_update = (WINDOWP (f->tool_bar_window)
11668 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11669 #endif
11670
11671 if (do_update)
11672 {
11673 Lisp_Object window;
11674 struct window *w;
11675
11676 window = FRAME_SELECTED_WINDOW (f);
11677 w = XWINDOW (window);
11678
11679 /* If the user has switched buffers or windows, we need to
11680 recompute to reflect the new bindings. But we'll
11681 recompute when update_mode_lines is set too; that means
11682 that people can use force-mode-line-update to request
11683 that the menu bar be recomputed. The adverse effect on
11684 the rest of the redisplay algorithm is about the same as
11685 windows_or_buffers_changed anyway. */
11686 if (windows_or_buffers_changed
11687 || w->update_mode_line
11688 || update_mode_lines
11689 || window_buffer_changed (w))
11690 {
11691 struct buffer *prev = current_buffer;
11692 ptrdiff_t count = SPECPDL_INDEX ();
11693 Lisp_Object frame, new_tool_bar;
11694 int new_n_tool_bar;
11695 struct gcpro gcpro1;
11696
11697 /* Set current_buffer to the buffer of the selected
11698 window of the frame, so that we get the right local
11699 keymaps. */
11700 set_buffer_internal_1 (XBUFFER (w->contents));
11701
11702 /* Save match data, if we must. */
11703 if (save_match_data)
11704 record_unwind_save_match_data ();
11705
11706 /* Make sure that we don't accidentally use bogus keymaps. */
11707 if (NILP (Voverriding_local_map_menu_flag))
11708 {
11709 specbind (Qoverriding_terminal_local_map, Qnil);
11710 specbind (Qoverriding_local_map, Qnil);
11711 }
11712
11713 GCPRO1 (new_tool_bar);
11714
11715 /* We must temporarily set the selected frame to this frame
11716 before calling tool_bar_items, because the calculation of
11717 the tool-bar keymap uses the selected frame (see
11718 `tool-bar-make-keymap' in tool-bar.el). */
11719 eassert (EQ (selected_window,
11720 /* Since we only explicitly preserve selected_frame,
11721 check that selected_window would be redundant. */
11722 XFRAME (selected_frame)->selected_window));
11723 record_unwind_protect (fast_set_selected_frame, selected_frame);
11724 XSETFRAME (frame, f);
11725 fast_set_selected_frame (frame);
11726
11727 /* Build desired tool-bar items from keymaps. */
11728 new_tool_bar
11729 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11730 &new_n_tool_bar);
11731
11732 /* Redisplay the tool-bar if we changed it. */
11733 if (new_n_tool_bar != f->n_tool_bar_items
11734 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11735 {
11736 /* Redisplay that happens asynchronously due to an expose event
11737 may access f->tool_bar_items. Make sure we update both
11738 variables within BLOCK_INPUT so no such event interrupts. */
11739 block_input ();
11740 fset_tool_bar_items (f, new_tool_bar);
11741 f->n_tool_bar_items = new_n_tool_bar;
11742 w->update_mode_line = 1;
11743 unblock_input ();
11744 }
11745
11746 UNGCPRO;
11747
11748 unbind_to (count, Qnil);
11749 set_buffer_internal_1 (prev);
11750 }
11751 }
11752 }
11753
11754 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11755
11756 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11757 F's desired tool-bar contents. F->tool_bar_items must have
11758 been set up previously by calling prepare_menu_bars. */
11759
11760 static void
11761 build_desired_tool_bar_string (struct frame *f)
11762 {
11763 int i, size, size_needed;
11764 struct gcpro gcpro1, gcpro2, gcpro3;
11765 Lisp_Object image, plist, props;
11766
11767 image = plist = props = Qnil;
11768 GCPRO3 (image, plist, props);
11769
11770 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11771 Otherwise, make a new string. */
11772
11773 /* The size of the string we might be able to reuse. */
11774 size = (STRINGP (f->desired_tool_bar_string)
11775 ? SCHARS (f->desired_tool_bar_string)
11776 : 0);
11777
11778 /* We need one space in the string for each image. */
11779 size_needed = f->n_tool_bar_items;
11780
11781 /* Reuse f->desired_tool_bar_string, if possible. */
11782 if (size < size_needed || NILP (f->desired_tool_bar_string))
11783 fset_desired_tool_bar_string
11784 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11785 else
11786 {
11787 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11788 Fremove_text_properties (make_number (0), make_number (size),
11789 props, f->desired_tool_bar_string);
11790 }
11791
11792 /* Put a `display' property on the string for the images to display,
11793 put a `menu_item' property on tool-bar items with a value that
11794 is the index of the item in F's tool-bar item vector. */
11795 for (i = 0; i < f->n_tool_bar_items; ++i)
11796 {
11797 #define PROP(IDX) \
11798 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11799
11800 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11801 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11802 int hmargin, vmargin, relief, idx, end;
11803
11804 /* If image is a vector, choose the image according to the
11805 button state. */
11806 image = PROP (TOOL_BAR_ITEM_IMAGES);
11807 if (VECTORP (image))
11808 {
11809 if (enabled_p)
11810 idx = (selected_p
11811 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11812 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11813 else
11814 idx = (selected_p
11815 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11816 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11817
11818 eassert (ASIZE (image) >= idx);
11819 image = AREF (image, idx);
11820 }
11821 else
11822 idx = -1;
11823
11824 /* Ignore invalid image specifications. */
11825 if (!valid_image_p (image))
11826 continue;
11827
11828 /* Display the tool-bar button pressed, or depressed. */
11829 plist = Fcopy_sequence (XCDR (image));
11830
11831 /* Compute margin and relief to draw. */
11832 relief = (tool_bar_button_relief >= 0
11833 ? tool_bar_button_relief
11834 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11835 hmargin = vmargin = relief;
11836
11837 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11838 INT_MAX - max (hmargin, vmargin)))
11839 {
11840 hmargin += XFASTINT (Vtool_bar_button_margin);
11841 vmargin += XFASTINT (Vtool_bar_button_margin);
11842 }
11843 else if (CONSP (Vtool_bar_button_margin))
11844 {
11845 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11846 INT_MAX - hmargin))
11847 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11848
11849 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11850 INT_MAX - vmargin))
11851 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11852 }
11853
11854 if (auto_raise_tool_bar_buttons_p)
11855 {
11856 /* Add a `:relief' property to the image spec if the item is
11857 selected. */
11858 if (selected_p)
11859 {
11860 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11861 hmargin -= relief;
11862 vmargin -= relief;
11863 }
11864 }
11865 else
11866 {
11867 /* If image is selected, display it pressed, i.e. with a
11868 negative relief. If it's not selected, display it with a
11869 raised relief. */
11870 plist = Fplist_put (plist, QCrelief,
11871 (selected_p
11872 ? make_number (-relief)
11873 : make_number (relief)));
11874 hmargin -= relief;
11875 vmargin -= relief;
11876 }
11877
11878 /* Put a margin around the image. */
11879 if (hmargin || vmargin)
11880 {
11881 if (hmargin == vmargin)
11882 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11883 else
11884 plist = Fplist_put (plist, QCmargin,
11885 Fcons (make_number (hmargin),
11886 make_number (vmargin)));
11887 }
11888
11889 /* If button is not enabled, and we don't have special images
11890 for the disabled state, make the image appear disabled by
11891 applying an appropriate algorithm to it. */
11892 if (!enabled_p && idx < 0)
11893 plist = Fplist_put (plist, QCconversion, Qdisabled);
11894
11895 /* Put a `display' text property on the string for the image to
11896 display. Put a `menu-item' property on the string that gives
11897 the start of this item's properties in the tool-bar items
11898 vector. */
11899 image = Fcons (Qimage, plist);
11900 props = list4 (Qdisplay, image,
11901 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11902
11903 /* Let the last image hide all remaining spaces in the tool bar
11904 string. The string can be longer than needed when we reuse a
11905 previous string. */
11906 if (i + 1 == f->n_tool_bar_items)
11907 end = SCHARS (f->desired_tool_bar_string);
11908 else
11909 end = i + 1;
11910 Fadd_text_properties (make_number (i), make_number (end),
11911 props, f->desired_tool_bar_string);
11912 #undef PROP
11913 }
11914
11915 UNGCPRO;
11916 }
11917
11918
11919 /* Display one line of the tool-bar of frame IT->f.
11920
11921 HEIGHT specifies the desired height of the tool-bar line.
11922 If the actual height of the glyph row is less than HEIGHT, the
11923 row's height is increased to HEIGHT, and the icons are centered
11924 vertically in the new height.
11925
11926 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11927 count a final empty row in case the tool-bar width exactly matches
11928 the window width.
11929 */
11930
11931 static void
11932 display_tool_bar_line (struct it *it, int height)
11933 {
11934 struct glyph_row *row = it->glyph_row;
11935 int max_x = it->last_visible_x;
11936 struct glyph *last;
11937
11938 prepare_desired_row (row);
11939 row->y = it->current_y;
11940
11941 /* Note that this isn't made use of if the face hasn't a box,
11942 so there's no need to check the face here. */
11943 it->start_of_box_run_p = 1;
11944
11945 while (it->current_x < max_x)
11946 {
11947 int x, n_glyphs_before, i, nglyphs;
11948 struct it it_before;
11949
11950 /* Get the next display element. */
11951 if (!get_next_display_element (it))
11952 {
11953 /* Don't count empty row if we are counting needed tool-bar lines. */
11954 if (height < 0 && !it->hpos)
11955 return;
11956 break;
11957 }
11958
11959 /* Produce glyphs. */
11960 n_glyphs_before = row->used[TEXT_AREA];
11961 it_before = *it;
11962
11963 PRODUCE_GLYPHS (it);
11964
11965 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11966 i = 0;
11967 x = it_before.current_x;
11968 while (i < nglyphs)
11969 {
11970 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11971
11972 if (x + glyph->pixel_width > max_x)
11973 {
11974 /* Glyph doesn't fit on line. Backtrack. */
11975 row->used[TEXT_AREA] = n_glyphs_before;
11976 *it = it_before;
11977 /* If this is the only glyph on this line, it will never fit on the
11978 tool-bar, so skip it. But ensure there is at least one glyph,
11979 so we don't accidentally disable the tool-bar. */
11980 if (n_glyphs_before == 0
11981 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11982 break;
11983 goto out;
11984 }
11985
11986 ++it->hpos;
11987 x += glyph->pixel_width;
11988 ++i;
11989 }
11990
11991 /* Stop at line end. */
11992 if (ITERATOR_AT_END_OF_LINE_P (it))
11993 break;
11994
11995 set_iterator_to_next (it, 1);
11996 }
11997
11998 out:;
11999
12000 row->displays_text_p = row->used[TEXT_AREA] != 0;
12001
12002 /* Use default face for the border below the tool bar.
12003
12004 FIXME: When auto-resize-tool-bars is grow-only, there is
12005 no additional border below the possibly empty tool-bar lines.
12006 So to make the extra empty lines look "normal", we have to
12007 use the tool-bar face for the border too. */
12008 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12009 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12010 it->face_id = DEFAULT_FACE_ID;
12011
12012 extend_face_to_end_of_line (it);
12013 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12014 last->right_box_line_p = 1;
12015 if (last == row->glyphs[TEXT_AREA])
12016 last->left_box_line_p = 1;
12017
12018 /* Make line the desired height and center it vertically. */
12019 if ((height -= it->max_ascent + it->max_descent) > 0)
12020 {
12021 /* Don't add more than one line height. */
12022 height %= FRAME_LINE_HEIGHT (it->f);
12023 it->max_ascent += height / 2;
12024 it->max_descent += (height + 1) / 2;
12025 }
12026
12027 compute_line_metrics (it);
12028
12029 /* If line is empty, make it occupy the rest of the tool-bar. */
12030 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12031 {
12032 row->height = row->phys_height = it->last_visible_y - row->y;
12033 row->visible_height = row->height;
12034 row->ascent = row->phys_ascent = 0;
12035 row->extra_line_spacing = 0;
12036 }
12037
12038 row->full_width_p = 1;
12039 row->continued_p = 0;
12040 row->truncated_on_left_p = 0;
12041 row->truncated_on_right_p = 0;
12042
12043 it->current_x = it->hpos = 0;
12044 it->current_y += row->height;
12045 ++it->vpos;
12046 ++it->glyph_row;
12047 }
12048
12049
12050 /* Max tool-bar height. Basically, this is what makes all other windows
12051 disappear when the frame gets too small. Rethink this! */
12052
12053 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12054 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12055
12056 /* Value is the number of screen lines needed to make all tool-bar
12057 items of frame F visible. The number of actual rows needed is
12058 returned in *N_ROWS if non-NULL. */
12059
12060 static int
12061 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12062 {
12063 struct window *w = XWINDOW (f->tool_bar_window);
12064 struct it it;
12065 /* tool_bar_height is called from redisplay_tool_bar after building
12066 the desired matrix, so use (unused) mode-line row as temporary row to
12067 avoid destroying the first tool-bar row. */
12068 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12069
12070 /* Initialize an iterator for iteration over
12071 F->desired_tool_bar_string in the tool-bar window of frame F. */
12072 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12073 it.first_visible_x = 0;
12074 /* PXW: Use FRAME_PIXEL_WIDTH (f) here ? */
12075 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12076 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12077 it.paragraph_embedding = L2R;
12078
12079 while (!ITERATOR_AT_END_P (&it))
12080 {
12081 clear_glyph_row (temp_row);
12082 it.glyph_row = temp_row;
12083 display_tool_bar_line (&it, -1);
12084 }
12085 clear_glyph_row (temp_row);
12086
12087 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12088 if (n_rows)
12089 *n_rows = it.vpos > 0 ? it.vpos : -1;
12090
12091 if (pixelwise)
12092 return it.current_y;
12093 else
12094 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12095 }
12096
12097 #endif /* !USE_GTK && !HAVE_NS */
12098
12099 #if defined USE_GTK || defined HAVE_NS
12100 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12101 #endif
12102
12103 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12104 0, 2, 0,
12105 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12106 If FRAME is nil or omitted, use the selected frame. Optional argument
12107 PIXELWISE non-nil means return the height of the tool bar inpixels. */)
12108 (Lisp_Object frame, Lisp_Object pixelwise)
12109 {
12110 int height = 0;
12111
12112 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12113 struct frame *f = decode_any_frame (frame);
12114
12115 if (WINDOWP (f->tool_bar_window)
12116 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12117 {
12118 update_tool_bar (f, 1);
12119 if (f->n_tool_bar_items)
12120 {
12121 build_desired_tool_bar_string (f);
12122 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12123 }
12124 }
12125 #endif
12126
12127 return make_number (height);
12128 }
12129
12130
12131 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12132 height should be changed. */
12133
12134 static int
12135 redisplay_tool_bar (struct frame *f)
12136 {
12137 #if defined (USE_GTK) || defined (HAVE_NS)
12138
12139 if (FRAME_EXTERNAL_TOOL_BAR (f))
12140 update_frame_tool_bar (f);
12141 return 0;
12142
12143 #else /* !USE_GTK && !HAVE_NS */
12144
12145 struct window *w;
12146 struct it it;
12147 struct glyph_row *row;
12148
12149 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12150 do anything. This means you must start with tool-bar-lines
12151 non-zero to get the auto-sizing effect. Or in other words, you
12152 can turn off tool-bars by specifying tool-bar-lines zero. */
12153 if (!WINDOWP (f->tool_bar_window)
12154 || (w = XWINDOW (f->tool_bar_window),
12155 WINDOW_PIXEL_HEIGHT (w) == 0))
12156 return 0;
12157
12158 /* Set up an iterator for the tool-bar window. */
12159 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12160 it.first_visible_x = 0;
12161 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12162 row = it.glyph_row;
12163
12164 /* Build a string that represents the contents of the tool-bar. */
12165 build_desired_tool_bar_string (f);
12166 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12167 /* FIXME: This should be controlled by a user option. But it
12168 doesn't make sense to have an R2L tool bar if the menu bar cannot
12169 be drawn also R2L, and making the menu bar R2L is tricky due
12170 toolkit-specific code that implements it. If an R2L tool bar is
12171 ever supported, display_tool_bar_line should also be augmented to
12172 call unproduce_glyphs like display_line and display_string
12173 do. */
12174 it.paragraph_embedding = L2R;
12175
12176 if (f->n_tool_bar_rows == 0)
12177 {
12178 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12179
12180 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12181 {
12182 Lisp_Object frame;
12183 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12184 / FRAME_LINE_HEIGHT (f));
12185
12186 XSETFRAME (frame, f);
12187 Fmodify_frame_parameters (frame,
12188 list1 (Fcons (Qtool_bar_lines,
12189 make_number (new_lines))));
12190 /* Always do that now. */
12191 clear_glyph_matrix (w->desired_matrix);
12192 f->fonts_changed = 1;
12193 return 1;
12194 }
12195 }
12196
12197 /* Display as many lines as needed to display all tool-bar items. */
12198
12199 if (f->n_tool_bar_rows > 0)
12200 {
12201 int border, rows, height, extra;
12202
12203 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12204 border = XINT (Vtool_bar_border);
12205 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12206 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12207 else if (EQ (Vtool_bar_border, Qborder_width))
12208 border = f->border_width;
12209 else
12210 border = 0;
12211 if (border < 0)
12212 border = 0;
12213
12214 rows = f->n_tool_bar_rows;
12215 height = max (1, (it.last_visible_y - border) / rows);
12216 extra = it.last_visible_y - border - height * rows;
12217
12218 while (it.current_y < it.last_visible_y)
12219 {
12220 int h = 0;
12221 if (extra > 0 && rows-- > 0)
12222 {
12223 h = (extra + rows - 1) / rows;
12224 extra -= h;
12225 }
12226 display_tool_bar_line (&it, height + h);
12227 }
12228 }
12229 else
12230 {
12231 while (it.current_y < it.last_visible_y)
12232 display_tool_bar_line (&it, 0);
12233 }
12234
12235 /* It doesn't make much sense to try scrolling in the tool-bar
12236 window, so don't do it. */
12237 w->desired_matrix->no_scrolling_p = 1;
12238 w->must_be_updated_p = 1;
12239
12240 if (!NILP (Vauto_resize_tool_bars))
12241 {
12242 /* Do we really allow the toolbar to occupy the whole frame? */
12243 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12244 int change_height_p = 0;
12245
12246 /* If we couldn't display everything, change the tool-bar's
12247 height if there is room for more. */
12248 if (IT_STRING_CHARPOS (it) < it.end_charpos
12249 && it.current_y < max_tool_bar_height)
12250 change_height_p = 1;
12251
12252 row = it.glyph_row - 1;
12253
12254 /* If there are blank lines at the end, except for a partially
12255 visible blank line at the end that is smaller than
12256 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12257 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12258 && row->height >= FRAME_LINE_HEIGHT (f))
12259 change_height_p = 1;
12260
12261 /* If row displays tool-bar items, but is partially visible,
12262 change the tool-bar's height. */
12263 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12264 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12265 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12266 change_height_p = 1;
12267
12268 /* Resize windows as needed by changing the `tool-bar-lines'
12269 frame parameter. */
12270 if (change_height_p)
12271 {
12272 Lisp_Object frame;
12273 int nrows;
12274 int new_height = tool_bar_height (f, &nrows, 1);
12275
12276 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12277 && !f->minimize_tool_bar_window_p)
12278 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12279 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12280 f->minimize_tool_bar_window_p = 0;
12281
12282 if (change_height_p)
12283 {
12284 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12285 / FRAME_LINE_HEIGHT (f));
12286
12287 XSETFRAME (frame, f);
12288 Fmodify_frame_parameters (frame,
12289 list1 (Fcons (Qtool_bar_lines,
12290 make_number (new_lines))));
12291 /* Always do that now. */
12292 clear_glyph_matrix (w->desired_matrix);
12293 f->n_tool_bar_rows = nrows;
12294 f->fonts_changed = 1;
12295 return 1;
12296 }
12297 }
12298 }
12299
12300 f->minimize_tool_bar_window_p = 0;
12301 return 0;
12302
12303 #endif /* USE_GTK || HAVE_NS */
12304 }
12305
12306 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12307
12308 /* Get information about the tool-bar item which is displayed in GLYPH
12309 on frame F. Return in *PROP_IDX the index where tool-bar item
12310 properties start in F->tool_bar_items. Value is zero if
12311 GLYPH doesn't display a tool-bar item. */
12312
12313 static int
12314 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12315 {
12316 Lisp_Object prop;
12317 int success_p;
12318 int charpos;
12319
12320 /* This function can be called asynchronously, which means we must
12321 exclude any possibility that Fget_text_property signals an
12322 error. */
12323 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12324 charpos = max (0, charpos);
12325
12326 /* Get the text property `menu-item' at pos. The value of that
12327 property is the start index of this item's properties in
12328 F->tool_bar_items. */
12329 prop = Fget_text_property (make_number (charpos),
12330 Qmenu_item, f->current_tool_bar_string);
12331 if (INTEGERP (prop))
12332 {
12333 *prop_idx = XINT (prop);
12334 success_p = 1;
12335 }
12336 else
12337 success_p = 0;
12338
12339 return success_p;
12340 }
12341
12342 \f
12343 /* Get information about the tool-bar item at position X/Y on frame F.
12344 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12345 the current matrix of the tool-bar window of F, or NULL if not
12346 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12347 item in F->tool_bar_items. Value is
12348
12349 -1 if X/Y is not on a tool-bar item
12350 0 if X/Y is on the same item that was highlighted before.
12351 1 otherwise. */
12352
12353 static int
12354 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12355 int *hpos, int *vpos, int *prop_idx)
12356 {
12357 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12358 struct window *w = XWINDOW (f->tool_bar_window);
12359 int area;
12360
12361 /* Find the glyph under X/Y. */
12362 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12363 if (*glyph == NULL)
12364 return -1;
12365
12366 /* Get the start of this tool-bar item's properties in
12367 f->tool_bar_items. */
12368 if (!tool_bar_item_info (f, *glyph, prop_idx))
12369 return -1;
12370
12371 /* Is mouse on the highlighted item? */
12372 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12373 && *vpos >= hlinfo->mouse_face_beg_row
12374 && *vpos <= hlinfo->mouse_face_end_row
12375 && (*vpos > hlinfo->mouse_face_beg_row
12376 || *hpos >= hlinfo->mouse_face_beg_col)
12377 && (*vpos < hlinfo->mouse_face_end_row
12378 || *hpos < hlinfo->mouse_face_end_col
12379 || hlinfo->mouse_face_past_end))
12380 return 0;
12381
12382 return 1;
12383 }
12384
12385
12386 /* EXPORT:
12387 Handle mouse button event on the tool-bar of frame F, at
12388 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12389 0 for button release. MODIFIERS is event modifiers for button
12390 release. */
12391
12392 void
12393 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12394 int modifiers)
12395 {
12396 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12397 struct window *w = XWINDOW (f->tool_bar_window);
12398 int hpos, vpos, prop_idx;
12399 struct glyph *glyph;
12400 Lisp_Object enabled_p;
12401 int ts;
12402
12403 /* If not on the highlighted tool-bar item, and mouse-highlight is
12404 non-nil, return. This is so we generate the tool-bar button
12405 click only when the mouse button is released on the same item as
12406 where it was pressed. However, when mouse-highlight is disabled,
12407 generate the click when the button is released regardless of the
12408 highlight, since tool-bar items are not highlighted in that
12409 case. */
12410 frame_to_window_pixel_xy (w, &x, &y);
12411 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12412 if (ts == -1
12413 || (ts != 0 && !NILP (Vmouse_highlight)))
12414 return;
12415
12416 /* When mouse-highlight is off, generate the click for the item
12417 where the button was pressed, disregarding where it was
12418 released. */
12419 if (NILP (Vmouse_highlight) && !down_p)
12420 prop_idx = last_tool_bar_item;
12421
12422 /* If item is disabled, do nothing. */
12423 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12424 if (NILP (enabled_p))
12425 return;
12426
12427 if (down_p)
12428 {
12429 /* Show item in pressed state. */
12430 if (!NILP (Vmouse_highlight))
12431 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12432 last_tool_bar_item = prop_idx;
12433 }
12434 else
12435 {
12436 Lisp_Object key, frame;
12437 struct input_event event;
12438 EVENT_INIT (event);
12439
12440 /* Show item in released state. */
12441 if (!NILP (Vmouse_highlight))
12442 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12443
12444 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12445
12446 XSETFRAME (frame, f);
12447 event.kind = TOOL_BAR_EVENT;
12448 event.frame_or_window = frame;
12449 event.arg = frame;
12450 kbd_buffer_store_event (&event);
12451
12452 event.kind = TOOL_BAR_EVENT;
12453 event.frame_or_window = frame;
12454 event.arg = key;
12455 event.modifiers = modifiers;
12456 kbd_buffer_store_event (&event);
12457 last_tool_bar_item = -1;
12458 }
12459 }
12460
12461
12462 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12463 tool-bar window-relative coordinates X/Y. Called from
12464 note_mouse_highlight. */
12465
12466 static void
12467 note_tool_bar_highlight (struct frame *f, int x, int y)
12468 {
12469 Lisp_Object window = f->tool_bar_window;
12470 struct window *w = XWINDOW (window);
12471 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12472 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12473 int hpos, vpos;
12474 struct glyph *glyph;
12475 struct glyph_row *row;
12476 int i;
12477 Lisp_Object enabled_p;
12478 int prop_idx;
12479 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12480 int mouse_down_p, rc;
12481
12482 /* Function note_mouse_highlight is called with negative X/Y
12483 values when mouse moves outside of the frame. */
12484 if (x <= 0 || y <= 0)
12485 {
12486 clear_mouse_face (hlinfo);
12487 return;
12488 }
12489
12490 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12491 if (rc < 0)
12492 {
12493 /* Not on tool-bar item. */
12494 clear_mouse_face (hlinfo);
12495 return;
12496 }
12497 else if (rc == 0)
12498 /* On same tool-bar item as before. */
12499 goto set_help_echo;
12500
12501 clear_mouse_face (hlinfo);
12502
12503 /* Mouse is down, but on different tool-bar item? */
12504 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12505 && f == dpyinfo->last_mouse_frame);
12506
12507 if (mouse_down_p
12508 && last_tool_bar_item != prop_idx)
12509 return;
12510
12511 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12512
12513 /* If tool-bar item is not enabled, don't highlight it. */
12514 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12515 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12516 {
12517 /* Compute the x-position of the glyph. In front and past the
12518 image is a space. We include this in the highlighted area. */
12519 row = MATRIX_ROW (w->current_matrix, vpos);
12520 for (i = x = 0; i < hpos; ++i)
12521 x += row->glyphs[TEXT_AREA][i].pixel_width;
12522
12523 /* Record this as the current active region. */
12524 hlinfo->mouse_face_beg_col = hpos;
12525 hlinfo->mouse_face_beg_row = vpos;
12526 hlinfo->mouse_face_beg_x = x;
12527 hlinfo->mouse_face_past_end = 0;
12528
12529 hlinfo->mouse_face_end_col = hpos + 1;
12530 hlinfo->mouse_face_end_row = vpos;
12531 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12532 hlinfo->mouse_face_window = window;
12533 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12534
12535 /* Display it as active. */
12536 show_mouse_face (hlinfo, draw);
12537 }
12538
12539 set_help_echo:
12540
12541 /* Set help_echo_string to a help string to display for this tool-bar item.
12542 XTread_socket does the rest. */
12543 help_echo_object = help_echo_window = Qnil;
12544 help_echo_pos = -1;
12545 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12546 if (NILP (help_echo_string))
12547 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12548 }
12549
12550 #endif /* !USE_GTK && !HAVE_NS */
12551
12552 #endif /* HAVE_WINDOW_SYSTEM */
12553
12554
12555 \f
12556 /************************************************************************
12557 Horizontal scrolling
12558 ************************************************************************/
12559
12560 static int hscroll_window_tree (Lisp_Object);
12561 static int hscroll_windows (Lisp_Object);
12562
12563 /* For all leaf windows in the window tree rooted at WINDOW, set their
12564 hscroll value so that PT is (i) visible in the window, and (ii) so
12565 that it is not within a certain margin at the window's left and
12566 right border. Value is non-zero if any window's hscroll has been
12567 changed. */
12568
12569 static int
12570 hscroll_window_tree (Lisp_Object window)
12571 {
12572 int hscrolled_p = 0;
12573 int hscroll_relative_p = FLOATP (Vhscroll_step);
12574 int hscroll_step_abs = 0;
12575 double hscroll_step_rel = 0;
12576
12577 if (hscroll_relative_p)
12578 {
12579 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12580 if (hscroll_step_rel < 0)
12581 {
12582 hscroll_relative_p = 0;
12583 hscroll_step_abs = 0;
12584 }
12585 }
12586 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12587 {
12588 hscroll_step_abs = XINT (Vhscroll_step);
12589 if (hscroll_step_abs < 0)
12590 hscroll_step_abs = 0;
12591 }
12592 else
12593 hscroll_step_abs = 0;
12594
12595 while (WINDOWP (window))
12596 {
12597 struct window *w = XWINDOW (window);
12598
12599 if (WINDOWP (w->contents))
12600 hscrolled_p |= hscroll_window_tree (w->contents);
12601 else if (w->cursor.vpos >= 0)
12602 {
12603 int h_margin;
12604 int text_area_width;
12605 struct glyph_row *current_cursor_row
12606 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12607 struct glyph_row *desired_cursor_row
12608 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12609 struct glyph_row *cursor_row
12610 = (desired_cursor_row->enabled_p
12611 ? desired_cursor_row
12612 : current_cursor_row);
12613 int row_r2l_p = cursor_row->reversed_p;
12614
12615 text_area_width = window_box_width (w, TEXT_AREA);
12616
12617 /* Scroll when cursor is inside this scroll margin. */
12618 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12619
12620 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12621 /* For left-to-right rows, hscroll when cursor is either
12622 (i) inside the right hscroll margin, or (ii) if it is
12623 inside the left margin and the window is already
12624 hscrolled. */
12625 && ((!row_r2l_p
12626 && ((w->hscroll
12627 && w->cursor.x <= h_margin)
12628 || (cursor_row->enabled_p
12629 && cursor_row->truncated_on_right_p
12630 && (w->cursor.x >= text_area_width - h_margin))))
12631 /* For right-to-left rows, the logic is similar,
12632 except that rules for scrolling to left and right
12633 are reversed. E.g., if cursor.x <= h_margin, we
12634 need to hscroll "to the right" unconditionally,
12635 and that will scroll the screen to the left so as
12636 to reveal the next portion of the row. */
12637 || (row_r2l_p
12638 && ((cursor_row->enabled_p
12639 /* FIXME: It is confusing to set the
12640 truncated_on_right_p flag when R2L rows
12641 are actually truncated on the left. */
12642 && cursor_row->truncated_on_right_p
12643 && w->cursor.x <= h_margin)
12644 || (w->hscroll
12645 && (w->cursor.x >= text_area_width - h_margin))))))
12646 {
12647 struct it it;
12648 ptrdiff_t hscroll;
12649 struct buffer *saved_current_buffer;
12650 ptrdiff_t pt;
12651 int wanted_x;
12652
12653 /* Find point in a display of infinite width. */
12654 saved_current_buffer = current_buffer;
12655 current_buffer = XBUFFER (w->contents);
12656
12657 if (w == XWINDOW (selected_window))
12658 pt = PT;
12659 else
12660 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12661
12662 /* Move iterator to pt starting at cursor_row->start in
12663 a line with infinite width. */
12664 init_to_row_start (&it, w, cursor_row);
12665 it.last_visible_x = INFINITY;
12666 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12667 current_buffer = saved_current_buffer;
12668
12669 /* Position cursor in window. */
12670 if (!hscroll_relative_p && hscroll_step_abs == 0)
12671 hscroll = max (0, (it.current_x
12672 - (ITERATOR_AT_END_OF_LINE_P (&it)
12673 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12674 : (text_area_width / 2))))
12675 / FRAME_COLUMN_WIDTH (it.f);
12676 else if ((!row_r2l_p
12677 && w->cursor.x >= text_area_width - h_margin)
12678 || (row_r2l_p && w->cursor.x <= h_margin))
12679 {
12680 if (hscroll_relative_p)
12681 wanted_x = text_area_width * (1 - hscroll_step_rel)
12682 - h_margin;
12683 else
12684 wanted_x = text_area_width
12685 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12686 - h_margin;
12687 hscroll
12688 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12689 }
12690 else
12691 {
12692 if (hscroll_relative_p)
12693 wanted_x = text_area_width * hscroll_step_rel
12694 + h_margin;
12695 else
12696 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12697 + h_margin;
12698 hscroll
12699 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12700 }
12701 hscroll = max (hscroll, w->min_hscroll);
12702
12703 /* Don't prevent redisplay optimizations if hscroll
12704 hasn't changed, as it will unnecessarily slow down
12705 redisplay. */
12706 if (w->hscroll != hscroll)
12707 {
12708 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12709 w->hscroll = hscroll;
12710 hscrolled_p = 1;
12711 }
12712 }
12713 }
12714
12715 window = w->next;
12716 }
12717
12718 /* Value is non-zero if hscroll of any leaf window has been changed. */
12719 return hscrolled_p;
12720 }
12721
12722
12723 /* Set hscroll so that cursor is visible and not inside horizontal
12724 scroll margins for all windows in the tree rooted at WINDOW. See
12725 also hscroll_window_tree above. Value is non-zero if any window's
12726 hscroll has been changed. If it has, desired matrices on the frame
12727 of WINDOW are cleared. */
12728
12729 static int
12730 hscroll_windows (Lisp_Object window)
12731 {
12732 int hscrolled_p = hscroll_window_tree (window);
12733 if (hscrolled_p)
12734 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12735 return hscrolled_p;
12736 }
12737
12738
12739 \f
12740 /************************************************************************
12741 Redisplay
12742 ************************************************************************/
12743
12744 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12745 to a non-zero value. This is sometimes handy to have in a debugger
12746 session. */
12747
12748 #ifdef GLYPH_DEBUG
12749
12750 /* First and last unchanged row for try_window_id. */
12751
12752 static int debug_first_unchanged_at_end_vpos;
12753 static int debug_last_unchanged_at_beg_vpos;
12754
12755 /* Delta vpos and y. */
12756
12757 static int debug_dvpos, debug_dy;
12758
12759 /* Delta in characters and bytes for try_window_id. */
12760
12761 static ptrdiff_t debug_delta, debug_delta_bytes;
12762
12763 /* Values of window_end_pos and window_end_vpos at the end of
12764 try_window_id. */
12765
12766 static ptrdiff_t debug_end_vpos;
12767
12768 /* Append a string to W->desired_matrix->method. FMT is a printf
12769 format string. If trace_redisplay_p is non-zero also printf the
12770 resulting string to stderr. */
12771
12772 static void debug_method_add (struct window *, char const *, ...)
12773 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12774
12775 static void
12776 debug_method_add (struct window *w, char const *fmt, ...)
12777 {
12778 void *ptr = w;
12779 char *method = w->desired_matrix->method;
12780 int len = strlen (method);
12781 int size = sizeof w->desired_matrix->method;
12782 int remaining = size - len - 1;
12783 va_list ap;
12784
12785 if (len && remaining)
12786 {
12787 method[len] = '|';
12788 --remaining, ++len;
12789 }
12790
12791 va_start (ap, fmt);
12792 vsnprintf (method + len, remaining + 1, fmt, ap);
12793 va_end (ap);
12794
12795 if (trace_redisplay_p)
12796 fprintf (stderr, "%p (%s): %s\n",
12797 ptr,
12798 ((BUFFERP (w->contents)
12799 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12800 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12801 : "no buffer"),
12802 method + len);
12803 }
12804
12805 #endif /* GLYPH_DEBUG */
12806
12807
12808 /* Value is non-zero if all changes in window W, which displays
12809 current_buffer, are in the text between START and END. START is a
12810 buffer position, END is given as a distance from Z. Used in
12811 redisplay_internal for display optimization. */
12812
12813 static int
12814 text_outside_line_unchanged_p (struct window *w,
12815 ptrdiff_t start, ptrdiff_t end)
12816 {
12817 int unchanged_p = 1;
12818
12819 /* If text or overlays have changed, see where. */
12820 if (window_outdated (w))
12821 {
12822 /* Gap in the line? */
12823 if (GPT < start || Z - GPT < end)
12824 unchanged_p = 0;
12825
12826 /* Changes start in front of the line, or end after it? */
12827 if (unchanged_p
12828 && (BEG_UNCHANGED < start - 1
12829 || END_UNCHANGED < end))
12830 unchanged_p = 0;
12831
12832 /* If selective display, can't optimize if changes start at the
12833 beginning of the line. */
12834 if (unchanged_p
12835 && INTEGERP (BVAR (current_buffer, selective_display))
12836 && XINT (BVAR (current_buffer, selective_display)) > 0
12837 && (BEG_UNCHANGED < start || GPT <= start))
12838 unchanged_p = 0;
12839
12840 /* If there are overlays at the start or end of the line, these
12841 may have overlay strings with newlines in them. A change at
12842 START, for instance, may actually concern the display of such
12843 overlay strings as well, and they are displayed on different
12844 lines. So, quickly rule out this case. (For the future, it
12845 might be desirable to implement something more telling than
12846 just BEG/END_UNCHANGED.) */
12847 if (unchanged_p)
12848 {
12849 if (BEG + BEG_UNCHANGED == start
12850 && overlay_touches_p (start))
12851 unchanged_p = 0;
12852 if (END_UNCHANGED == end
12853 && overlay_touches_p (Z - end))
12854 unchanged_p = 0;
12855 }
12856
12857 /* Under bidi reordering, adding or deleting a character in the
12858 beginning of a paragraph, before the first strong directional
12859 character, can change the base direction of the paragraph (unless
12860 the buffer specifies a fixed paragraph direction), which will
12861 require to redisplay the whole paragraph. It might be worthwhile
12862 to find the paragraph limits and widen the range of redisplayed
12863 lines to that, but for now just give up this optimization. */
12864 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12865 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12866 unchanged_p = 0;
12867 }
12868
12869 return unchanged_p;
12870 }
12871
12872
12873 /* Do a frame update, taking possible shortcuts into account. This is
12874 the main external entry point for redisplay.
12875
12876 If the last redisplay displayed an echo area message and that message
12877 is no longer requested, we clear the echo area or bring back the
12878 mini-buffer if that is in use. */
12879
12880 void
12881 redisplay (void)
12882 {
12883 redisplay_internal ();
12884 }
12885
12886
12887 static Lisp_Object
12888 overlay_arrow_string_or_property (Lisp_Object var)
12889 {
12890 Lisp_Object val;
12891
12892 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12893 return val;
12894
12895 return Voverlay_arrow_string;
12896 }
12897
12898 /* Return 1 if there are any overlay-arrows in current_buffer. */
12899 static int
12900 overlay_arrow_in_current_buffer_p (void)
12901 {
12902 Lisp_Object vlist;
12903
12904 for (vlist = Voverlay_arrow_variable_list;
12905 CONSP (vlist);
12906 vlist = XCDR (vlist))
12907 {
12908 Lisp_Object var = XCAR (vlist);
12909 Lisp_Object val;
12910
12911 if (!SYMBOLP (var))
12912 continue;
12913 val = find_symbol_value (var);
12914 if (MARKERP (val)
12915 && current_buffer == XMARKER (val)->buffer)
12916 return 1;
12917 }
12918 return 0;
12919 }
12920
12921
12922 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12923 has changed. */
12924
12925 static int
12926 overlay_arrows_changed_p (void)
12927 {
12928 Lisp_Object vlist;
12929
12930 for (vlist = Voverlay_arrow_variable_list;
12931 CONSP (vlist);
12932 vlist = XCDR (vlist))
12933 {
12934 Lisp_Object var = XCAR (vlist);
12935 Lisp_Object val, pstr;
12936
12937 if (!SYMBOLP (var))
12938 continue;
12939 val = find_symbol_value (var);
12940 if (!MARKERP (val))
12941 continue;
12942 if (! EQ (COERCE_MARKER (val),
12943 Fget (var, Qlast_arrow_position))
12944 || ! (pstr = overlay_arrow_string_or_property (var),
12945 EQ (pstr, Fget (var, Qlast_arrow_string))))
12946 return 1;
12947 }
12948 return 0;
12949 }
12950
12951 /* Mark overlay arrows to be updated on next redisplay. */
12952
12953 static void
12954 update_overlay_arrows (int up_to_date)
12955 {
12956 Lisp_Object vlist;
12957
12958 for (vlist = Voverlay_arrow_variable_list;
12959 CONSP (vlist);
12960 vlist = XCDR (vlist))
12961 {
12962 Lisp_Object var = XCAR (vlist);
12963
12964 if (!SYMBOLP (var))
12965 continue;
12966
12967 if (up_to_date > 0)
12968 {
12969 Lisp_Object val = find_symbol_value (var);
12970 Fput (var, Qlast_arrow_position,
12971 COERCE_MARKER (val));
12972 Fput (var, Qlast_arrow_string,
12973 overlay_arrow_string_or_property (var));
12974 }
12975 else if (up_to_date < 0
12976 || !NILP (Fget (var, Qlast_arrow_position)))
12977 {
12978 Fput (var, Qlast_arrow_position, Qt);
12979 Fput (var, Qlast_arrow_string, Qt);
12980 }
12981 }
12982 }
12983
12984
12985 /* Return overlay arrow string to display at row.
12986 Return integer (bitmap number) for arrow bitmap in left fringe.
12987 Return nil if no overlay arrow. */
12988
12989 static Lisp_Object
12990 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12991 {
12992 Lisp_Object vlist;
12993
12994 for (vlist = Voverlay_arrow_variable_list;
12995 CONSP (vlist);
12996 vlist = XCDR (vlist))
12997 {
12998 Lisp_Object var = XCAR (vlist);
12999 Lisp_Object val;
13000
13001 if (!SYMBOLP (var))
13002 continue;
13003
13004 val = find_symbol_value (var);
13005
13006 if (MARKERP (val)
13007 && current_buffer == XMARKER (val)->buffer
13008 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13009 {
13010 if (FRAME_WINDOW_P (it->f)
13011 /* FIXME: if ROW->reversed_p is set, this should test
13012 the right fringe, not the left one. */
13013 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13014 {
13015 #ifdef HAVE_WINDOW_SYSTEM
13016 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13017 {
13018 int fringe_bitmap;
13019 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13020 return make_number (fringe_bitmap);
13021 }
13022 #endif
13023 return make_number (-1); /* Use default arrow bitmap. */
13024 }
13025 return overlay_arrow_string_or_property (var);
13026 }
13027 }
13028
13029 return Qnil;
13030 }
13031
13032 /* Return 1 if point moved out of or into a composition. Otherwise
13033 return 0. PREV_BUF and PREV_PT are the last point buffer and
13034 position. BUF and PT are the current point buffer and position. */
13035
13036 static int
13037 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13038 struct buffer *buf, ptrdiff_t pt)
13039 {
13040 ptrdiff_t start, end;
13041 Lisp_Object prop;
13042 Lisp_Object buffer;
13043
13044 XSETBUFFER (buffer, buf);
13045 /* Check a composition at the last point if point moved within the
13046 same buffer. */
13047 if (prev_buf == buf)
13048 {
13049 if (prev_pt == pt)
13050 /* Point didn't move. */
13051 return 0;
13052
13053 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13054 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13055 && composition_valid_p (start, end, prop)
13056 && start < prev_pt && end > prev_pt)
13057 /* The last point was within the composition. Return 1 iff
13058 point moved out of the composition. */
13059 return (pt <= start || pt >= end);
13060 }
13061
13062 /* Check a composition at the current point. */
13063 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13064 && find_composition (pt, -1, &start, &end, &prop, buffer)
13065 && composition_valid_p (start, end, prop)
13066 && start < pt && end > pt);
13067 }
13068
13069 /* Reconsider the clip changes of buffer which is displayed in W. */
13070
13071 static void
13072 reconsider_clip_changes (struct window *w)
13073 {
13074 struct buffer *b = XBUFFER (w->contents);
13075
13076 if (b->clip_changed
13077 && w->window_end_valid
13078 && w->current_matrix->buffer == b
13079 && w->current_matrix->zv == BUF_ZV (b)
13080 && w->current_matrix->begv == BUF_BEGV (b))
13081 b->clip_changed = 0;
13082
13083 /* If display wasn't paused, and W is not a tool bar window, see if
13084 point has been moved into or out of a composition. In that case,
13085 we set b->clip_changed to 1 to force updating the screen. If
13086 b->clip_changed has already been set to 1, we can skip this
13087 check. */
13088 if (!b->clip_changed && w->window_end_valid)
13089 {
13090 ptrdiff_t pt = (w == XWINDOW (selected_window)
13091 ? PT : marker_position (w->pointm));
13092
13093 if ((w->current_matrix->buffer != b || pt != w->last_point)
13094 && check_point_in_composition (w->current_matrix->buffer,
13095 w->last_point, b, pt))
13096 b->clip_changed = 1;
13097 }
13098 }
13099
13100 static void
13101 propagate_buffer_redisplay (void)
13102 { /* Resetting b->text->redisplay is problematic!
13103 We can't just reset it in the case that some window that displays
13104 it has not been redisplayed; and such a window can stay
13105 unredisplayed for a long time if it's currently invisible.
13106 But we do want to reset it at the end of redisplay otherwise
13107 its displayed windows will keep being redisplayed over and over
13108 again.
13109 So we copy all b->text->redisplay flags up to their windows here,
13110 such that mark_window_display_accurate can safely reset
13111 b->text->redisplay. */
13112 Lisp_Object ws = window_list ();
13113 for (; CONSP (ws); ws = XCDR (ws))
13114 {
13115 struct window *thisw = XWINDOW (XCAR (ws));
13116 struct buffer *thisb = XBUFFER (thisw->contents);
13117 if (thisb->text->redisplay)
13118 thisw->redisplay = true;
13119 }
13120 }
13121
13122 #define STOP_POLLING \
13123 do { if (! polling_stopped_here) stop_polling (); \
13124 polling_stopped_here = 1; } while (0)
13125
13126 #define RESUME_POLLING \
13127 do { if (polling_stopped_here) start_polling (); \
13128 polling_stopped_here = 0; } while (0)
13129
13130
13131 /* Perhaps in the future avoid recentering windows if it
13132 is not necessary; currently that causes some problems. */
13133
13134 static void
13135 redisplay_internal (void)
13136 {
13137 struct window *w = XWINDOW (selected_window);
13138 struct window *sw;
13139 struct frame *fr;
13140 int pending;
13141 bool must_finish = 0, match_p;
13142 struct text_pos tlbufpos, tlendpos;
13143 int number_of_visible_frames;
13144 ptrdiff_t count;
13145 struct frame *sf;
13146 int polling_stopped_here = 0;
13147 Lisp_Object tail, frame;
13148
13149 /* True means redisplay has to consider all windows on all
13150 frames. False, only selected_window is considered. */
13151 bool consider_all_windows_p;
13152
13153 /* True means redisplay has to redisplay the miniwindow. */
13154 bool update_miniwindow_p = false;
13155
13156 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13157
13158 /* No redisplay if running in batch mode or frame is not yet fully
13159 initialized, or redisplay is explicitly turned off by setting
13160 Vinhibit_redisplay. */
13161 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13162 || !NILP (Vinhibit_redisplay))
13163 return;
13164
13165 /* Don't examine these until after testing Vinhibit_redisplay.
13166 When Emacs is shutting down, perhaps because its connection to
13167 X has dropped, we should not look at them at all. */
13168 fr = XFRAME (w->frame);
13169 sf = SELECTED_FRAME ();
13170
13171 if (!fr->glyphs_initialized_p)
13172 return;
13173
13174 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13175 if (popup_activated ())
13176 return;
13177 #endif
13178
13179 /* I don't think this happens but let's be paranoid. */
13180 if (redisplaying_p)
13181 return;
13182
13183 /* Record a function that clears redisplaying_p
13184 when we leave this function. */
13185 count = SPECPDL_INDEX ();
13186 record_unwind_protect_void (unwind_redisplay);
13187 redisplaying_p = 1;
13188 specbind (Qinhibit_free_realized_faces, Qnil);
13189
13190 /* Record this function, so it appears on the profiler's backtraces. */
13191 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13192
13193 FOR_EACH_FRAME (tail, frame)
13194 XFRAME (frame)->already_hscrolled_p = 0;
13195
13196 retry:
13197 /* Remember the currently selected window. */
13198 sw = w;
13199
13200 pending = 0;
13201 last_escape_glyph_frame = NULL;
13202 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13203 last_glyphless_glyph_frame = NULL;
13204 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13205
13206 /* If face_change_count is non-zero, init_iterator will free all
13207 realized faces, which includes the faces referenced from current
13208 matrices. So, we can't reuse current matrices in this case. */
13209 if (face_change_count)
13210 windows_or_buffers_changed = 47;
13211
13212 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13213 && FRAME_TTY (sf)->previous_frame != sf)
13214 {
13215 /* Since frames on a single ASCII terminal share the same
13216 display area, displaying a different frame means redisplay
13217 the whole thing. */
13218 SET_FRAME_GARBAGED (sf);
13219 #ifndef DOS_NT
13220 set_tty_color_mode (FRAME_TTY (sf), sf);
13221 #endif
13222 FRAME_TTY (sf)->previous_frame = sf;
13223 }
13224
13225 /* Set the visible flags for all frames. Do this before checking for
13226 resized or garbaged frames; they want to know if their frames are
13227 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13228 number_of_visible_frames = 0;
13229
13230 FOR_EACH_FRAME (tail, frame)
13231 {
13232 struct frame *f = XFRAME (frame);
13233
13234 if (FRAME_VISIBLE_P (f))
13235 {
13236 ++number_of_visible_frames;
13237 /* Adjust matrices for visible frames only. */
13238 if (f->fonts_changed)
13239 {
13240 adjust_frame_glyphs (f);
13241 f->fonts_changed = 0;
13242 }
13243 /* If cursor type has been changed on the frame
13244 other than selected, consider all frames. */
13245 if (f != sf && f->cursor_type_changed)
13246 update_mode_lines = 31;
13247 }
13248 clear_desired_matrices (f);
13249 }
13250
13251 /* Notice any pending interrupt request to change frame size. */
13252 do_pending_window_change (1);
13253
13254 /* do_pending_window_change could change the selected_window due to
13255 frame resizing which makes the selected window too small. */
13256 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13257 sw = w;
13258
13259 /* Clear frames marked as garbaged. */
13260 clear_garbaged_frames ();
13261
13262 /* Build menubar and tool-bar items. */
13263 if (NILP (Vmemory_full))
13264 prepare_menu_bars ();
13265
13266 reconsider_clip_changes (w);
13267
13268 /* In most cases selected window displays current buffer. */
13269 match_p = XBUFFER (w->contents) == current_buffer;
13270 if (match_p)
13271 {
13272 /* Detect case that we need to write or remove a star in the mode line. */
13273 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13274 w->update_mode_line = 1;
13275
13276 if (mode_line_update_needed (w))
13277 w->update_mode_line = 1;
13278 }
13279
13280 /* Normally the message* functions will have already displayed and
13281 updated the echo area, but the frame may have been trashed, or
13282 the update may have been preempted, so display the echo area
13283 again here. Checking message_cleared_p captures the case that
13284 the echo area should be cleared. */
13285 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13286 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13287 || (message_cleared_p
13288 && minibuf_level == 0
13289 /* If the mini-window is currently selected, this means the
13290 echo-area doesn't show through. */
13291 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13292 {
13293 int window_height_changed_p = echo_area_display (0);
13294
13295 if (message_cleared_p)
13296 update_miniwindow_p = true;
13297
13298 must_finish = 1;
13299
13300 /* If we don't display the current message, don't clear the
13301 message_cleared_p flag, because, if we did, we wouldn't clear
13302 the echo area in the next redisplay which doesn't preserve
13303 the echo area. */
13304 if (!display_last_displayed_message_p)
13305 message_cleared_p = 0;
13306
13307 if (window_height_changed_p)
13308 {
13309 windows_or_buffers_changed = 50;
13310
13311 /* If window configuration was changed, frames may have been
13312 marked garbaged. Clear them or we will experience
13313 surprises wrt scrolling. */
13314 clear_garbaged_frames ();
13315 }
13316 }
13317 else if (EQ (selected_window, minibuf_window)
13318 && (current_buffer->clip_changed || window_outdated (w))
13319 && resize_mini_window (w, 0))
13320 {
13321 /* Resized active mini-window to fit the size of what it is
13322 showing if its contents might have changed. */
13323 must_finish = 1;
13324
13325 /* If window configuration was changed, frames may have been
13326 marked garbaged. Clear them or we will experience
13327 surprises wrt scrolling. */
13328 clear_garbaged_frames ();
13329 }
13330
13331 if (windows_or_buffers_changed && !update_mode_lines)
13332 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13333 only the windows's contents needs to be refreshed, or whether the
13334 mode-lines also need a refresh. */
13335 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13336 ? REDISPLAY_SOME : 32);
13337
13338 /* If specs for an arrow have changed, do thorough redisplay
13339 to ensure we remove any arrow that should no longer exist. */
13340 if (overlay_arrows_changed_p ())
13341 /* Apparently, this is the only case where we update other windows,
13342 without updating other mode-lines. */
13343 windows_or_buffers_changed = 49;
13344
13345 consider_all_windows_p = (update_mode_lines
13346 || windows_or_buffers_changed);
13347
13348 #define AINC(a,i) \
13349 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13350 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13351
13352 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13353 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13354
13355 /* Optimize the case that only the line containing the cursor in the
13356 selected window has changed. Variables starting with this_ are
13357 set in display_line and record information about the line
13358 containing the cursor. */
13359 tlbufpos = this_line_start_pos;
13360 tlendpos = this_line_end_pos;
13361 if (!consider_all_windows_p
13362 && CHARPOS (tlbufpos) > 0
13363 && !w->update_mode_line
13364 && !current_buffer->clip_changed
13365 && !current_buffer->prevent_redisplay_optimizations_p
13366 && FRAME_VISIBLE_P (XFRAME (w->frame))
13367 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13368 && !XFRAME (w->frame)->cursor_type_changed
13369 /* Make sure recorded data applies to current buffer, etc. */
13370 && this_line_buffer == current_buffer
13371 && match_p
13372 && !w->force_start
13373 && !w->optional_new_start
13374 /* Point must be on the line that we have info recorded about. */
13375 && PT >= CHARPOS (tlbufpos)
13376 && PT <= Z - CHARPOS (tlendpos)
13377 /* All text outside that line, including its final newline,
13378 must be unchanged. */
13379 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13380 CHARPOS (tlendpos)))
13381 {
13382 if (CHARPOS (tlbufpos) > BEGV
13383 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13384 && (CHARPOS (tlbufpos) == ZV
13385 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13386 /* Former continuation line has disappeared by becoming empty. */
13387 goto cancel;
13388 else if (window_outdated (w) || MINI_WINDOW_P (w))
13389 {
13390 /* We have to handle the case of continuation around a
13391 wide-column character (see the comment in indent.c around
13392 line 1340).
13393
13394 For instance, in the following case:
13395
13396 -------- Insert --------
13397 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13398 J_I_ ==> J_I_ `^^' are cursors.
13399 ^^ ^^
13400 -------- --------
13401
13402 As we have to redraw the line above, we cannot use this
13403 optimization. */
13404
13405 struct it it;
13406 int line_height_before = this_line_pixel_height;
13407
13408 /* Note that start_display will handle the case that the
13409 line starting at tlbufpos is a continuation line. */
13410 start_display (&it, w, tlbufpos);
13411
13412 /* Implementation note: It this still necessary? */
13413 if (it.current_x != this_line_start_x)
13414 goto cancel;
13415
13416 TRACE ((stderr, "trying display optimization 1\n"));
13417 w->cursor.vpos = -1;
13418 overlay_arrow_seen = 0;
13419 it.vpos = this_line_vpos;
13420 it.current_y = this_line_y;
13421 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13422 display_line (&it);
13423
13424 /* If line contains point, is not continued,
13425 and ends at same distance from eob as before, we win. */
13426 if (w->cursor.vpos >= 0
13427 /* Line is not continued, otherwise this_line_start_pos
13428 would have been set to 0 in display_line. */
13429 && CHARPOS (this_line_start_pos)
13430 /* Line ends as before. */
13431 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13432 /* Line has same height as before. Otherwise other lines
13433 would have to be shifted up or down. */
13434 && this_line_pixel_height == line_height_before)
13435 {
13436 /* If this is not the window's last line, we must adjust
13437 the charstarts of the lines below. */
13438 if (it.current_y < it.last_visible_y)
13439 {
13440 struct glyph_row *row
13441 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13442 ptrdiff_t delta, delta_bytes;
13443
13444 /* We used to distinguish between two cases here,
13445 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13446 when the line ends in a newline or the end of the
13447 buffer's accessible portion. But both cases did
13448 the same, so they were collapsed. */
13449 delta = (Z
13450 - CHARPOS (tlendpos)
13451 - MATRIX_ROW_START_CHARPOS (row));
13452 delta_bytes = (Z_BYTE
13453 - BYTEPOS (tlendpos)
13454 - MATRIX_ROW_START_BYTEPOS (row));
13455
13456 increment_matrix_positions (w->current_matrix,
13457 this_line_vpos + 1,
13458 w->current_matrix->nrows,
13459 delta, delta_bytes);
13460 }
13461
13462 /* If this row displays text now but previously didn't,
13463 or vice versa, w->window_end_vpos may have to be
13464 adjusted. */
13465 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13466 {
13467 if (w->window_end_vpos < this_line_vpos)
13468 w->window_end_vpos = this_line_vpos;
13469 }
13470 else if (w->window_end_vpos == this_line_vpos
13471 && this_line_vpos > 0)
13472 w->window_end_vpos = this_line_vpos - 1;
13473 w->window_end_valid = 0;
13474
13475 /* Update hint: No need to try to scroll in update_window. */
13476 w->desired_matrix->no_scrolling_p = 1;
13477
13478 #ifdef GLYPH_DEBUG
13479 *w->desired_matrix->method = 0;
13480 debug_method_add (w, "optimization 1");
13481 #endif
13482 #ifdef HAVE_WINDOW_SYSTEM
13483 update_window_fringes (w, 0);
13484 #endif
13485 goto update;
13486 }
13487 else
13488 goto cancel;
13489 }
13490 else if (/* Cursor position hasn't changed. */
13491 PT == w->last_point
13492 /* Make sure the cursor was last displayed
13493 in this window. Otherwise we have to reposition it. */
13494
13495 /* PXW: Must be pixelized, probably. */
13496 && 0 <= w->cursor.vpos
13497 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13498 {
13499 if (!must_finish)
13500 {
13501 do_pending_window_change (1);
13502 /* If selected_window changed, redisplay again. */
13503 if (WINDOWP (selected_window)
13504 && (w = XWINDOW (selected_window)) != sw)
13505 goto retry;
13506
13507 /* We used to always goto end_of_redisplay here, but this
13508 isn't enough if we have a blinking cursor. */
13509 if (w->cursor_off_p == w->last_cursor_off_p)
13510 goto end_of_redisplay;
13511 }
13512 goto update;
13513 }
13514 /* If highlighting the region, or if the cursor is in the echo area,
13515 then we can't just move the cursor. */
13516 else if (NILP (Vshow_trailing_whitespace)
13517 && !cursor_in_echo_area)
13518 {
13519 struct it it;
13520 struct glyph_row *row;
13521
13522 /* Skip from tlbufpos to PT and see where it is. Note that
13523 PT may be in invisible text. If so, we will end at the
13524 next visible position. */
13525 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13526 NULL, DEFAULT_FACE_ID);
13527 it.current_x = this_line_start_x;
13528 it.current_y = this_line_y;
13529 it.vpos = this_line_vpos;
13530
13531 /* The call to move_it_to stops in front of PT, but
13532 moves over before-strings. */
13533 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13534
13535 if (it.vpos == this_line_vpos
13536 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13537 row->enabled_p))
13538 {
13539 eassert (this_line_vpos == it.vpos);
13540 eassert (this_line_y == it.current_y);
13541 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13542 #ifdef GLYPH_DEBUG
13543 *w->desired_matrix->method = 0;
13544 debug_method_add (w, "optimization 3");
13545 #endif
13546 goto update;
13547 }
13548 else
13549 goto cancel;
13550 }
13551
13552 cancel:
13553 /* Text changed drastically or point moved off of line. */
13554 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13555 }
13556
13557 CHARPOS (this_line_start_pos) = 0;
13558 ++clear_face_cache_count;
13559 #ifdef HAVE_WINDOW_SYSTEM
13560 ++clear_image_cache_count;
13561 #endif
13562
13563 /* Build desired matrices, and update the display. If
13564 consider_all_windows_p is non-zero, do it for all windows on all
13565 frames. Otherwise do it for selected_window, only. */
13566
13567 if (consider_all_windows_p)
13568 {
13569 FOR_EACH_FRAME (tail, frame)
13570 XFRAME (frame)->updated_p = 0;
13571
13572 propagate_buffer_redisplay ();
13573
13574 FOR_EACH_FRAME (tail, frame)
13575 {
13576 struct frame *f = XFRAME (frame);
13577
13578 /* We don't have to do anything for unselected terminal
13579 frames. */
13580 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13581 && !EQ (FRAME_TTY (f)->top_frame, frame))
13582 continue;
13583
13584 retry_frame:
13585
13586 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13587 {
13588 bool gcscrollbars
13589 /* Only GC scollbars when we redisplay the whole frame. */
13590 = f->redisplay || !REDISPLAY_SOME_P ();
13591 /* Mark all the scroll bars to be removed; we'll redeem
13592 the ones we want when we redisplay their windows. */
13593 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13594 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13595
13596 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13597 redisplay_windows (FRAME_ROOT_WINDOW (f));
13598 /* Remember that the invisible frames need to be redisplayed next
13599 time they're visible. */
13600 else if (!REDISPLAY_SOME_P ())
13601 f->redisplay = true;
13602
13603 /* The X error handler may have deleted that frame. */
13604 if (!FRAME_LIVE_P (f))
13605 continue;
13606
13607 /* Any scroll bars which redisplay_windows should have
13608 nuked should now go away. */
13609 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13610 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13611
13612 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13613 {
13614 /* If fonts changed on visible frame, display again. */
13615 if (f->fonts_changed)
13616 {
13617 adjust_frame_glyphs (f);
13618 f->fonts_changed = 0;
13619 goto retry_frame;
13620 }
13621
13622 /* See if we have to hscroll. */
13623 if (!f->already_hscrolled_p)
13624 {
13625 f->already_hscrolled_p = 1;
13626 if (hscroll_windows (f->root_window))
13627 goto retry_frame;
13628 }
13629
13630 /* Prevent various kinds of signals during display
13631 update. stdio is not robust about handling
13632 signals, which can cause an apparent I/O error. */
13633 if (interrupt_input)
13634 unrequest_sigio ();
13635 STOP_POLLING;
13636
13637 pending |= update_frame (f, 0, 0);
13638 f->cursor_type_changed = 0;
13639 f->updated_p = 1;
13640 }
13641 }
13642 }
13643
13644 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13645
13646 if (!pending)
13647 {
13648 /* Do the mark_window_display_accurate after all windows have
13649 been redisplayed because this call resets flags in buffers
13650 which are needed for proper redisplay. */
13651 FOR_EACH_FRAME (tail, frame)
13652 {
13653 struct frame *f = XFRAME (frame);
13654 if (f->updated_p)
13655 {
13656 f->redisplay = false;
13657 mark_window_display_accurate (f->root_window, 1);
13658 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13659 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13660 }
13661 }
13662 }
13663 }
13664 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13665 {
13666 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13667 struct frame *mini_frame;
13668
13669 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13670 /* Use list_of_error, not Qerror, so that
13671 we catch only errors and don't run the debugger. */
13672 internal_condition_case_1 (redisplay_window_1, selected_window,
13673 list_of_error,
13674 redisplay_window_error);
13675 if (update_miniwindow_p)
13676 internal_condition_case_1 (redisplay_window_1, mini_window,
13677 list_of_error,
13678 redisplay_window_error);
13679
13680 /* Compare desired and current matrices, perform output. */
13681
13682 update:
13683 /* If fonts changed, display again. */
13684 if (sf->fonts_changed)
13685 goto retry;
13686
13687 /* Prevent various kinds of signals during display update.
13688 stdio is not robust about handling signals,
13689 which can cause an apparent I/O error. */
13690 if (interrupt_input)
13691 unrequest_sigio ();
13692 STOP_POLLING;
13693
13694 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13695 {
13696 if (hscroll_windows (selected_window))
13697 goto retry;
13698
13699 XWINDOW (selected_window)->must_be_updated_p = true;
13700 pending = update_frame (sf, 0, 0);
13701 sf->cursor_type_changed = 0;
13702 }
13703
13704 /* We may have called echo_area_display at the top of this
13705 function. If the echo area is on another frame, that may
13706 have put text on a frame other than the selected one, so the
13707 above call to update_frame would not have caught it. Catch
13708 it here. */
13709 mini_window = FRAME_MINIBUF_WINDOW (sf);
13710 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13711
13712 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13713 {
13714 XWINDOW (mini_window)->must_be_updated_p = true;
13715 pending |= update_frame (mini_frame, 0, 0);
13716 mini_frame->cursor_type_changed = 0;
13717 if (!pending && hscroll_windows (mini_window))
13718 goto retry;
13719 }
13720 }
13721
13722 /* If display was paused because of pending input, make sure we do a
13723 thorough update the next time. */
13724 if (pending)
13725 {
13726 /* Prevent the optimization at the beginning of
13727 redisplay_internal that tries a single-line update of the
13728 line containing the cursor in the selected window. */
13729 CHARPOS (this_line_start_pos) = 0;
13730
13731 /* Let the overlay arrow be updated the next time. */
13732 update_overlay_arrows (0);
13733
13734 /* If we pause after scrolling, some rows in the current
13735 matrices of some windows are not valid. */
13736 if (!WINDOW_FULL_WIDTH_P (w)
13737 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13738 update_mode_lines = 36;
13739 }
13740 else
13741 {
13742 if (!consider_all_windows_p)
13743 {
13744 /* This has already been done above if
13745 consider_all_windows_p is set. */
13746 if (XBUFFER (w->contents)->text->redisplay
13747 && buffer_window_count (XBUFFER (w->contents)) > 1)
13748 /* This can happen if b->text->redisplay was set during
13749 jit-lock. */
13750 propagate_buffer_redisplay ();
13751 mark_window_display_accurate_1 (w, 1);
13752
13753 /* Say overlay arrows are up to date. */
13754 update_overlay_arrows (1);
13755
13756 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13757 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13758 }
13759
13760 update_mode_lines = 0;
13761 windows_or_buffers_changed = 0;
13762 }
13763
13764 /* Start SIGIO interrupts coming again. Having them off during the
13765 code above makes it less likely one will discard output, but not
13766 impossible, since there might be stuff in the system buffer here.
13767 But it is much hairier to try to do anything about that. */
13768 if (interrupt_input)
13769 request_sigio ();
13770 RESUME_POLLING;
13771
13772 /* If a frame has become visible which was not before, redisplay
13773 again, so that we display it. Expose events for such a frame
13774 (which it gets when becoming visible) don't call the parts of
13775 redisplay constructing glyphs, so simply exposing a frame won't
13776 display anything in this case. So, we have to display these
13777 frames here explicitly. */
13778 if (!pending)
13779 {
13780 int new_count = 0;
13781
13782 FOR_EACH_FRAME (tail, frame)
13783 {
13784 if (XFRAME (frame)->visible)
13785 new_count++;
13786 }
13787
13788 if (new_count != number_of_visible_frames)
13789 windows_or_buffers_changed = 52;
13790 }
13791
13792 /* Change frame size now if a change is pending. */
13793 do_pending_window_change (1);
13794
13795 /* If we just did a pending size change, or have additional
13796 visible frames, or selected_window changed, redisplay again. */
13797 if ((windows_or_buffers_changed && !pending)
13798 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13799 goto retry;
13800
13801 /* Clear the face and image caches.
13802
13803 We used to do this only if consider_all_windows_p. But the cache
13804 needs to be cleared if a timer creates images in the current
13805 buffer (e.g. the test case in Bug#6230). */
13806
13807 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13808 {
13809 clear_face_cache (0);
13810 clear_face_cache_count = 0;
13811 }
13812
13813 #ifdef HAVE_WINDOW_SYSTEM
13814 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13815 {
13816 clear_image_caches (Qnil);
13817 clear_image_cache_count = 0;
13818 }
13819 #endif /* HAVE_WINDOW_SYSTEM */
13820
13821 end_of_redisplay:
13822 if (interrupt_input && interrupts_deferred)
13823 request_sigio ();
13824
13825 unbind_to (count, Qnil);
13826 RESUME_POLLING;
13827 }
13828
13829
13830 /* Redisplay, but leave alone any recent echo area message unless
13831 another message has been requested in its place.
13832
13833 This is useful in situations where you need to redisplay but no
13834 user action has occurred, making it inappropriate for the message
13835 area to be cleared. See tracking_off and
13836 wait_reading_process_output for examples of these situations.
13837
13838 FROM_WHERE is an integer saying from where this function was
13839 called. This is useful for debugging. */
13840
13841 void
13842 redisplay_preserve_echo_area (int from_where)
13843 {
13844 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13845
13846 if (!NILP (echo_area_buffer[1]))
13847 {
13848 /* We have a previously displayed message, but no current
13849 message. Redisplay the previous message. */
13850 display_last_displayed_message_p = 1;
13851 redisplay_internal ();
13852 display_last_displayed_message_p = 0;
13853 }
13854 else
13855 redisplay_internal ();
13856
13857 flush_frame (SELECTED_FRAME ());
13858 }
13859
13860
13861 /* Function registered with record_unwind_protect in redisplay_internal. */
13862
13863 static void
13864 unwind_redisplay (void)
13865 {
13866 redisplaying_p = 0;
13867 }
13868
13869
13870 /* Mark the display of leaf window W as accurate or inaccurate.
13871 If ACCURATE_P is non-zero mark display of W as accurate. If
13872 ACCURATE_P is zero, arrange for W to be redisplayed the next
13873 time redisplay_internal is called. */
13874
13875 static void
13876 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13877 {
13878 struct buffer *b = XBUFFER (w->contents);
13879
13880 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13881 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13882 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13883
13884 if (accurate_p)
13885 {
13886 b->clip_changed = false;
13887 b->prevent_redisplay_optimizations_p = false;
13888 eassert (buffer_window_count (b) > 0);
13889 /* Resetting b->text->redisplay is problematic!
13890 In order to make it safer to do it here, redisplay_internal must
13891 have copied all b->text->redisplay to their respective windows. */
13892 b->text->redisplay = false;
13893
13894 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13895 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13896 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13897 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13898
13899 w->current_matrix->buffer = b;
13900 w->current_matrix->begv = BUF_BEGV (b);
13901 w->current_matrix->zv = BUF_ZV (b);
13902
13903 w->last_cursor_vpos = w->cursor.vpos;
13904 w->last_cursor_off_p = w->cursor_off_p;
13905
13906 if (w == XWINDOW (selected_window))
13907 w->last_point = BUF_PT (b);
13908 else
13909 w->last_point = marker_position (w->pointm);
13910
13911 w->window_end_valid = true;
13912 w->update_mode_line = false;
13913 }
13914
13915 w->redisplay = !accurate_p;
13916 }
13917
13918
13919 /* Mark the display of windows in the window tree rooted at WINDOW as
13920 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13921 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13922 be redisplayed the next time redisplay_internal is called. */
13923
13924 void
13925 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13926 {
13927 struct window *w;
13928
13929 for (; !NILP (window); window = w->next)
13930 {
13931 w = XWINDOW (window);
13932 if (WINDOWP (w->contents))
13933 mark_window_display_accurate (w->contents, accurate_p);
13934 else
13935 mark_window_display_accurate_1 (w, accurate_p);
13936 }
13937
13938 if (accurate_p)
13939 update_overlay_arrows (1);
13940 else
13941 /* Force a thorough redisplay the next time by setting
13942 last_arrow_position and last_arrow_string to t, which is
13943 unequal to any useful value of Voverlay_arrow_... */
13944 update_overlay_arrows (-1);
13945 }
13946
13947
13948 /* Return value in display table DP (Lisp_Char_Table *) for character
13949 C. Since a display table doesn't have any parent, we don't have to
13950 follow parent. Do not call this function directly but use the
13951 macro DISP_CHAR_VECTOR. */
13952
13953 Lisp_Object
13954 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13955 {
13956 Lisp_Object val;
13957
13958 if (ASCII_CHAR_P (c))
13959 {
13960 val = dp->ascii;
13961 if (SUB_CHAR_TABLE_P (val))
13962 val = XSUB_CHAR_TABLE (val)->contents[c];
13963 }
13964 else
13965 {
13966 Lisp_Object table;
13967
13968 XSETCHAR_TABLE (table, dp);
13969 val = char_table_ref (table, c);
13970 }
13971 if (NILP (val))
13972 val = dp->defalt;
13973 return val;
13974 }
13975
13976
13977 \f
13978 /***********************************************************************
13979 Window Redisplay
13980 ***********************************************************************/
13981
13982 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13983
13984 static void
13985 redisplay_windows (Lisp_Object window)
13986 {
13987 while (!NILP (window))
13988 {
13989 struct window *w = XWINDOW (window);
13990
13991 if (WINDOWP (w->contents))
13992 redisplay_windows (w->contents);
13993 else if (BUFFERP (w->contents))
13994 {
13995 displayed_buffer = XBUFFER (w->contents);
13996 /* Use list_of_error, not Qerror, so that
13997 we catch only errors and don't run the debugger. */
13998 internal_condition_case_1 (redisplay_window_0, window,
13999 list_of_error,
14000 redisplay_window_error);
14001 }
14002
14003 window = w->next;
14004 }
14005 }
14006
14007 static Lisp_Object
14008 redisplay_window_error (Lisp_Object ignore)
14009 {
14010 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14011 return Qnil;
14012 }
14013
14014 static Lisp_Object
14015 redisplay_window_0 (Lisp_Object window)
14016 {
14017 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14018 redisplay_window (window, false);
14019 return Qnil;
14020 }
14021
14022 static Lisp_Object
14023 redisplay_window_1 (Lisp_Object window)
14024 {
14025 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14026 redisplay_window (window, true);
14027 return Qnil;
14028 }
14029 \f
14030
14031 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14032 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14033 which positions recorded in ROW differ from current buffer
14034 positions.
14035
14036 Return 0 if cursor is not on this row, 1 otherwise. */
14037
14038 static int
14039 set_cursor_from_row (struct window *w, struct glyph_row *row,
14040 struct glyph_matrix *matrix,
14041 ptrdiff_t delta, ptrdiff_t delta_bytes,
14042 int dy, int dvpos)
14043 {
14044 struct glyph *glyph = row->glyphs[TEXT_AREA];
14045 struct glyph *end = glyph + row->used[TEXT_AREA];
14046 struct glyph *cursor = NULL;
14047 /* The last known character position in row. */
14048 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14049 int x = row->x;
14050 ptrdiff_t pt_old = PT - delta;
14051 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14052 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14053 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14054 /* A glyph beyond the edge of TEXT_AREA which we should never
14055 touch. */
14056 struct glyph *glyphs_end = end;
14057 /* Non-zero means we've found a match for cursor position, but that
14058 glyph has the avoid_cursor_p flag set. */
14059 int match_with_avoid_cursor = 0;
14060 /* Non-zero means we've seen at least one glyph that came from a
14061 display string. */
14062 int string_seen = 0;
14063 /* Largest and smallest buffer positions seen so far during scan of
14064 glyph row. */
14065 ptrdiff_t bpos_max = pos_before;
14066 ptrdiff_t bpos_min = pos_after;
14067 /* Last buffer position covered by an overlay string with an integer
14068 `cursor' property. */
14069 ptrdiff_t bpos_covered = 0;
14070 /* Non-zero means the display string on which to display the cursor
14071 comes from a text property, not from an overlay. */
14072 int string_from_text_prop = 0;
14073
14074 /* Don't even try doing anything if called for a mode-line or
14075 header-line row, since the rest of the code isn't prepared to
14076 deal with such calamities. */
14077 eassert (!row->mode_line_p);
14078 if (row->mode_line_p)
14079 return 0;
14080
14081 /* Skip over glyphs not having an object at the start and the end of
14082 the row. These are special glyphs like truncation marks on
14083 terminal frames. */
14084 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14085 {
14086 if (!row->reversed_p)
14087 {
14088 while (glyph < end
14089 && INTEGERP (glyph->object)
14090 && glyph->charpos < 0)
14091 {
14092 x += glyph->pixel_width;
14093 ++glyph;
14094 }
14095 while (end > glyph
14096 && INTEGERP ((end - 1)->object)
14097 /* CHARPOS is zero for blanks and stretch glyphs
14098 inserted by extend_face_to_end_of_line. */
14099 && (end - 1)->charpos <= 0)
14100 --end;
14101 glyph_before = glyph - 1;
14102 glyph_after = end;
14103 }
14104 else
14105 {
14106 struct glyph *g;
14107
14108 /* If the glyph row is reversed, we need to process it from back
14109 to front, so swap the edge pointers. */
14110 glyphs_end = end = glyph - 1;
14111 glyph += row->used[TEXT_AREA] - 1;
14112
14113 while (glyph > end + 1
14114 && INTEGERP (glyph->object)
14115 && glyph->charpos < 0)
14116 {
14117 --glyph;
14118 x -= glyph->pixel_width;
14119 }
14120 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14121 --glyph;
14122 /* By default, in reversed rows we put the cursor on the
14123 rightmost (first in the reading order) glyph. */
14124 for (g = end + 1; g < glyph; g++)
14125 x += g->pixel_width;
14126 while (end < glyph
14127 && INTEGERP ((end + 1)->object)
14128 && (end + 1)->charpos <= 0)
14129 ++end;
14130 glyph_before = glyph + 1;
14131 glyph_after = end;
14132 }
14133 }
14134 else if (row->reversed_p)
14135 {
14136 /* In R2L rows that don't display text, put the cursor on the
14137 rightmost glyph. Case in point: an empty last line that is
14138 part of an R2L paragraph. */
14139 cursor = end - 1;
14140 /* Avoid placing the cursor on the last glyph of the row, where
14141 on terminal frames we hold the vertical border between
14142 adjacent windows. */
14143 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14144 && !WINDOW_RIGHTMOST_P (w)
14145 && cursor == row->glyphs[LAST_AREA] - 1)
14146 cursor--;
14147 x = -1; /* will be computed below, at label compute_x */
14148 }
14149
14150 /* Step 1: Try to find the glyph whose character position
14151 corresponds to point. If that's not possible, find 2 glyphs
14152 whose character positions are the closest to point, one before
14153 point, the other after it. */
14154 if (!row->reversed_p)
14155 while (/* not marched to end of glyph row */
14156 glyph < end
14157 /* glyph was not inserted by redisplay for internal purposes */
14158 && !INTEGERP (glyph->object))
14159 {
14160 if (BUFFERP (glyph->object))
14161 {
14162 ptrdiff_t dpos = glyph->charpos - pt_old;
14163
14164 if (glyph->charpos > bpos_max)
14165 bpos_max = glyph->charpos;
14166 if (glyph->charpos < bpos_min)
14167 bpos_min = glyph->charpos;
14168 if (!glyph->avoid_cursor_p)
14169 {
14170 /* If we hit point, we've found the glyph on which to
14171 display the cursor. */
14172 if (dpos == 0)
14173 {
14174 match_with_avoid_cursor = 0;
14175 break;
14176 }
14177 /* See if we've found a better approximation to
14178 POS_BEFORE or to POS_AFTER. */
14179 if (0 > dpos && dpos > pos_before - pt_old)
14180 {
14181 pos_before = glyph->charpos;
14182 glyph_before = glyph;
14183 }
14184 else if (0 < dpos && dpos < pos_after - pt_old)
14185 {
14186 pos_after = glyph->charpos;
14187 glyph_after = glyph;
14188 }
14189 }
14190 else if (dpos == 0)
14191 match_with_avoid_cursor = 1;
14192 }
14193 else if (STRINGP (glyph->object))
14194 {
14195 Lisp_Object chprop;
14196 ptrdiff_t glyph_pos = glyph->charpos;
14197
14198 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14199 glyph->object);
14200 if (!NILP (chprop))
14201 {
14202 /* If the string came from a `display' text property,
14203 look up the buffer position of that property and
14204 use that position to update bpos_max, as if we
14205 actually saw such a position in one of the row's
14206 glyphs. This helps with supporting integer values
14207 of `cursor' property on the display string in
14208 situations where most or all of the row's buffer
14209 text is completely covered by display properties,
14210 so that no glyph with valid buffer positions is
14211 ever seen in the row. */
14212 ptrdiff_t prop_pos =
14213 string_buffer_position_lim (glyph->object, pos_before,
14214 pos_after, 0);
14215
14216 if (prop_pos >= pos_before)
14217 bpos_max = prop_pos - 1;
14218 }
14219 if (INTEGERP (chprop))
14220 {
14221 bpos_covered = bpos_max + XINT (chprop);
14222 /* If the `cursor' property covers buffer positions up
14223 to and including point, we should display cursor on
14224 this glyph. Note that, if a `cursor' property on one
14225 of the string's characters has an integer value, we
14226 will break out of the loop below _before_ we get to
14227 the position match above. IOW, integer values of
14228 the `cursor' property override the "exact match for
14229 point" strategy of positioning the cursor. */
14230 /* Implementation note: bpos_max == pt_old when, e.g.,
14231 we are in an empty line, where bpos_max is set to
14232 MATRIX_ROW_START_CHARPOS, see above. */
14233 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14234 {
14235 cursor = glyph;
14236 break;
14237 }
14238 }
14239
14240 string_seen = 1;
14241 }
14242 x += glyph->pixel_width;
14243 ++glyph;
14244 }
14245 else if (glyph > end) /* row is reversed */
14246 while (!INTEGERP (glyph->object))
14247 {
14248 if (BUFFERP (glyph->object))
14249 {
14250 ptrdiff_t dpos = glyph->charpos - pt_old;
14251
14252 if (glyph->charpos > bpos_max)
14253 bpos_max = glyph->charpos;
14254 if (glyph->charpos < bpos_min)
14255 bpos_min = glyph->charpos;
14256 if (!glyph->avoid_cursor_p)
14257 {
14258 if (dpos == 0)
14259 {
14260 match_with_avoid_cursor = 0;
14261 break;
14262 }
14263 if (0 > dpos && dpos > pos_before - pt_old)
14264 {
14265 pos_before = glyph->charpos;
14266 glyph_before = glyph;
14267 }
14268 else if (0 < dpos && dpos < pos_after - pt_old)
14269 {
14270 pos_after = glyph->charpos;
14271 glyph_after = glyph;
14272 }
14273 }
14274 else if (dpos == 0)
14275 match_with_avoid_cursor = 1;
14276 }
14277 else if (STRINGP (glyph->object))
14278 {
14279 Lisp_Object chprop;
14280 ptrdiff_t glyph_pos = glyph->charpos;
14281
14282 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14283 glyph->object);
14284 if (!NILP (chprop))
14285 {
14286 ptrdiff_t prop_pos =
14287 string_buffer_position_lim (glyph->object, pos_before,
14288 pos_after, 0);
14289
14290 if (prop_pos >= pos_before)
14291 bpos_max = prop_pos - 1;
14292 }
14293 if (INTEGERP (chprop))
14294 {
14295 bpos_covered = bpos_max + XINT (chprop);
14296 /* If the `cursor' property covers buffer positions up
14297 to and including point, we should display cursor on
14298 this glyph. */
14299 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14300 {
14301 cursor = glyph;
14302 break;
14303 }
14304 }
14305 string_seen = 1;
14306 }
14307 --glyph;
14308 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14309 {
14310 x--; /* can't use any pixel_width */
14311 break;
14312 }
14313 x -= glyph->pixel_width;
14314 }
14315
14316 /* Step 2: If we didn't find an exact match for point, we need to
14317 look for a proper place to put the cursor among glyphs between
14318 GLYPH_BEFORE and GLYPH_AFTER. */
14319 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14320 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14321 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14322 {
14323 /* An empty line has a single glyph whose OBJECT is zero and
14324 whose CHARPOS is the position of a newline on that line.
14325 Note that on a TTY, there are more glyphs after that, which
14326 were produced by extend_face_to_end_of_line, but their
14327 CHARPOS is zero or negative. */
14328 int empty_line_p =
14329 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14330 && INTEGERP (glyph->object) && glyph->charpos > 0
14331 /* On a TTY, continued and truncated rows also have a glyph at
14332 their end whose OBJECT is zero and whose CHARPOS is
14333 positive (the continuation and truncation glyphs), but such
14334 rows are obviously not "empty". */
14335 && !(row->continued_p || row->truncated_on_right_p);
14336
14337 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14338 {
14339 ptrdiff_t ellipsis_pos;
14340
14341 /* Scan back over the ellipsis glyphs. */
14342 if (!row->reversed_p)
14343 {
14344 ellipsis_pos = (glyph - 1)->charpos;
14345 while (glyph > row->glyphs[TEXT_AREA]
14346 && (glyph - 1)->charpos == ellipsis_pos)
14347 glyph--, x -= glyph->pixel_width;
14348 /* That loop always goes one position too far, including
14349 the glyph before the ellipsis. So scan forward over
14350 that one. */
14351 x += glyph->pixel_width;
14352 glyph++;
14353 }
14354 else /* row is reversed */
14355 {
14356 ellipsis_pos = (glyph + 1)->charpos;
14357 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14358 && (glyph + 1)->charpos == ellipsis_pos)
14359 glyph++, x += glyph->pixel_width;
14360 x -= glyph->pixel_width;
14361 glyph--;
14362 }
14363 }
14364 else if (match_with_avoid_cursor)
14365 {
14366 cursor = glyph_after;
14367 x = -1;
14368 }
14369 else if (string_seen)
14370 {
14371 int incr = row->reversed_p ? -1 : +1;
14372
14373 /* Need to find the glyph that came out of a string which is
14374 present at point. That glyph is somewhere between
14375 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14376 positioned between POS_BEFORE and POS_AFTER in the
14377 buffer. */
14378 struct glyph *start, *stop;
14379 ptrdiff_t pos = pos_before;
14380
14381 x = -1;
14382
14383 /* If the row ends in a newline from a display string,
14384 reordering could have moved the glyphs belonging to the
14385 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14386 in this case we extend the search to the last glyph in
14387 the row that was not inserted by redisplay. */
14388 if (row->ends_in_newline_from_string_p)
14389 {
14390 glyph_after = end;
14391 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14392 }
14393
14394 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14395 correspond to POS_BEFORE and POS_AFTER, respectively. We
14396 need START and STOP in the order that corresponds to the
14397 row's direction as given by its reversed_p flag. If the
14398 directionality of characters between POS_BEFORE and
14399 POS_AFTER is the opposite of the row's base direction,
14400 these characters will have been reordered for display,
14401 and we need to reverse START and STOP. */
14402 if (!row->reversed_p)
14403 {
14404 start = min (glyph_before, glyph_after);
14405 stop = max (glyph_before, glyph_after);
14406 }
14407 else
14408 {
14409 start = max (glyph_before, glyph_after);
14410 stop = min (glyph_before, glyph_after);
14411 }
14412 for (glyph = start + incr;
14413 row->reversed_p ? glyph > stop : glyph < stop; )
14414 {
14415
14416 /* Any glyphs that come from the buffer are here because
14417 of bidi reordering. Skip them, and only pay
14418 attention to glyphs that came from some string. */
14419 if (STRINGP (glyph->object))
14420 {
14421 Lisp_Object str;
14422 ptrdiff_t tem;
14423 /* If the display property covers the newline, we
14424 need to search for it one position farther. */
14425 ptrdiff_t lim = pos_after
14426 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14427
14428 string_from_text_prop = 0;
14429 str = glyph->object;
14430 tem = string_buffer_position_lim (str, pos, lim, 0);
14431 if (tem == 0 /* from overlay */
14432 || pos <= tem)
14433 {
14434 /* If the string from which this glyph came is
14435 found in the buffer at point, or at position
14436 that is closer to point than pos_after, then
14437 we've found the glyph we've been looking for.
14438 If it comes from an overlay (tem == 0), and
14439 it has the `cursor' property on one of its
14440 glyphs, record that glyph as a candidate for
14441 displaying the cursor. (As in the
14442 unidirectional version, we will display the
14443 cursor on the last candidate we find.) */
14444 if (tem == 0
14445 || tem == pt_old
14446 || (tem - pt_old > 0 && tem < pos_after))
14447 {
14448 /* The glyphs from this string could have
14449 been reordered. Find the one with the
14450 smallest string position. Or there could
14451 be a character in the string with the
14452 `cursor' property, which means display
14453 cursor on that character's glyph. */
14454 ptrdiff_t strpos = glyph->charpos;
14455
14456 if (tem)
14457 {
14458 cursor = glyph;
14459 string_from_text_prop = 1;
14460 }
14461 for ( ;
14462 (row->reversed_p ? glyph > stop : glyph < stop)
14463 && EQ (glyph->object, str);
14464 glyph += incr)
14465 {
14466 Lisp_Object cprop;
14467 ptrdiff_t gpos = glyph->charpos;
14468
14469 cprop = Fget_char_property (make_number (gpos),
14470 Qcursor,
14471 glyph->object);
14472 if (!NILP (cprop))
14473 {
14474 cursor = glyph;
14475 break;
14476 }
14477 if (tem && glyph->charpos < strpos)
14478 {
14479 strpos = glyph->charpos;
14480 cursor = glyph;
14481 }
14482 }
14483
14484 if (tem == pt_old
14485 || (tem - pt_old > 0 && tem < pos_after))
14486 goto compute_x;
14487 }
14488 if (tem)
14489 pos = tem + 1; /* don't find previous instances */
14490 }
14491 /* This string is not what we want; skip all of the
14492 glyphs that came from it. */
14493 while ((row->reversed_p ? glyph > stop : glyph < stop)
14494 && EQ (glyph->object, str))
14495 glyph += incr;
14496 }
14497 else
14498 glyph += incr;
14499 }
14500
14501 /* If we reached the end of the line, and END was from a string,
14502 the cursor is not on this line. */
14503 if (cursor == NULL
14504 && (row->reversed_p ? glyph <= end : glyph >= end)
14505 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14506 && STRINGP (end->object)
14507 && row->continued_p)
14508 return 0;
14509 }
14510 /* A truncated row may not include PT among its character positions.
14511 Setting the cursor inside the scroll margin will trigger
14512 recalculation of hscroll in hscroll_window_tree. But if a
14513 display string covers point, defer to the string-handling
14514 code below to figure this out. */
14515 else if (row->truncated_on_left_p && pt_old < bpos_min)
14516 {
14517 cursor = glyph_before;
14518 x = -1;
14519 }
14520 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14521 /* Zero-width characters produce no glyphs. */
14522 || (!empty_line_p
14523 && (row->reversed_p
14524 ? glyph_after > glyphs_end
14525 : glyph_after < glyphs_end)))
14526 {
14527 cursor = glyph_after;
14528 x = -1;
14529 }
14530 }
14531
14532 compute_x:
14533 if (cursor != NULL)
14534 glyph = cursor;
14535 else if (glyph == glyphs_end
14536 && pos_before == pos_after
14537 && STRINGP ((row->reversed_p
14538 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14539 : row->glyphs[TEXT_AREA])->object))
14540 {
14541 /* If all the glyphs of this row came from strings, put the
14542 cursor on the first glyph of the row. This avoids having the
14543 cursor outside of the text area in this very rare and hard
14544 use case. */
14545 glyph =
14546 row->reversed_p
14547 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14548 : row->glyphs[TEXT_AREA];
14549 }
14550 if (x < 0)
14551 {
14552 struct glyph *g;
14553
14554 /* Need to compute x that corresponds to GLYPH. */
14555 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14556 {
14557 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14558 emacs_abort ();
14559 x += g->pixel_width;
14560 }
14561 }
14562
14563 /* ROW could be part of a continued line, which, under bidi
14564 reordering, might have other rows whose start and end charpos
14565 occlude point. Only set w->cursor if we found a better
14566 approximation to the cursor position than we have from previously
14567 examined candidate rows belonging to the same continued line. */
14568 if (/* We already have a candidate row. */
14569 w->cursor.vpos >= 0
14570 /* That candidate is not the row we are processing. */
14571 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14572 /* Make sure cursor.vpos specifies a row whose start and end
14573 charpos occlude point, and it is valid candidate for being a
14574 cursor-row. This is because some callers of this function
14575 leave cursor.vpos at the row where the cursor was displayed
14576 during the last redisplay cycle. */
14577 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14578 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14579 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14580 {
14581 struct glyph *g1
14582 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14583
14584 /* Don't consider glyphs that are outside TEXT_AREA. */
14585 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14586 return 0;
14587 /* Keep the candidate whose buffer position is the closest to
14588 point or has the `cursor' property. */
14589 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14590 w->cursor.hpos >= 0
14591 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14592 && ((BUFFERP (g1->object)
14593 && (g1->charpos == pt_old /* An exact match always wins. */
14594 || (BUFFERP (glyph->object)
14595 && eabs (g1->charpos - pt_old)
14596 < eabs (glyph->charpos - pt_old))))
14597 /* Previous candidate is a glyph from a string that has
14598 a non-nil `cursor' property. */
14599 || (STRINGP (g1->object)
14600 && (!NILP (Fget_char_property (make_number (g1->charpos),
14601 Qcursor, g1->object))
14602 /* Previous candidate is from the same display
14603 string as this one, and the display string
14604 came from a text property. */
14605 || (EQ (g1->object, glyph->object)
14606 && string_from_text_prop)
14607 /* this candidate is from newline and its
14608 position is not an exact match */
14609 || (INTEGERP (glyph->object)
14610 && glyph->charpos != pt_old)))))
14611 return 0;
14612 /* If this candidate gives an exact match, use that. */
14613 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14614 /* If this candidate is a glyph created for the
14615 terminating newline of a line, and point is on that
14616 newline, it wins because it's an exact match. */
14617 || (!row->continued_p
14618 && INTEGERP (glyph->object)
14619 && glyph->charpos == 0
14620 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14621 /* Otherwise, keep the candidate that comes from a row
14622 spanning less buffer positions. This may win when one or
14623 both candidate positions are on glyphs that came from
14624 display strings, for which we cannot compare buffer
14625 positions. */
14626 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14627 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14628 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14629 return 0;
14630 }
14631 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14632 w->cursor.x = x;
14633 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14634 w->cursor.y = row->y + dy;
14635
14636 if (w == XWINDOW (selected_window))
14637 {
14638 if (!row->continued_p
14639 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14640 && row->x == 0)
14641 {
14642 this_line_buffer = XBUFFER (w->contents);
14643
14644 CHARPOS (this_line_start_pos)
14645 = MATRIX_ROW_START_CHARPOS (row) + delta;
14646 BYTEPOS (this_line_start_pos)
14647 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14648
14649 CHARPOS (this_line_end_pos)
14650 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14651 BYTEPOS (this_line_end_pos)
14652 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14653
14654 this_line_y = w->cursor.y;
14655 this_line_pixel_height = row->height;
14656 this_line_vpos = w->cursor.vpos;
14657 this_line_start_x = row->x;
14658 }
14659 else
14660 CHARPOS (this_line_start_pos) = 0;
14661 }
14662
14663 return 1;
14664 }
14665
14666
14667 /* Run window scroll functions, if any, for WINDOW with new window
14668 start STARTP. Sets the window start of WINDOW to that position.
14669
14670 We assume that the window's buffer is really current. */
14671
14672 static struct text_pos
14673 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14674 {
14675 struct window *w = XWINDOW (window);
14676 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14677
14678 eassert (current_buffer == XBUFFER (w->contents));
14679
14680 if (!NILP (Vwindow_scroll_functions))
14681 {
14682 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14683 make_number (CHARPOS (startp)));
14684 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14685 /* In case the hook functions switch buffers. */
14686 set_buffer_internal (XBUFFER (w->contents));
14687 }
14688
14689 return startp;
14690 }
14691
14692
14693 /* Make sure the line containing the cursor is fully visible.
14694 A value of 1 means there is nothing to be done.
14695 (Either the line is fully visible, or it cannot be made so,
14696 or we cannot tell.)
14697
14698 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14699 is higher than window.
14700
14701 A value of 0 means the caller should do scrolling
14702 as if point had gone off the screen. */
14703
14704 static int
14705 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14706 {
14707 struct glyph_matrix *matrix;
14708 struct glyph_row *row;
14709 int window_height;
14710
14711 if (!make_cursor_line_fully_visible_p)
14712 return 1;
14713
14714 /* It's not always possible to find the cursor, e.g, when a window
14715 is full of overlay strings. Don't do anything in that case. */
14716 if (w->cursor.vpos < 0)
14717 return 1;
14718
14719 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14720 row = MATRIX_ROW (matrix, w->cursor.vpos);
14721
14722 /* If the cursor row is not partially visible, there's nothing to do. */
14723 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14724 return 1;
14725
14726 /* If the row the cursor is in is taller than the window's height,
14727 it's not clear what to do, so do nothing. */
14728 window_height = window_box_height (w);
14729 if (row->height >= window_height)
14730 {
14731 if (!force_p || MINI_WINDOW_P (w)
14732 || w->vscroll || w->cursor.vpos == 0)
14733 return 1;
14734 }
14735 return 0;
14736 }
14737
14738
14739 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14740 non-zero means only WINDOW is redisplayed in redisplay_internal.
14741 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14742 in redisplay_window to bring a partially visible line into view in
14743 the case that only the cursor has moved.
14744
14745 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14746 last screen line's vertical height extends past the end of the screen.
14747
14748 Value is
14749
14750 1 if scrolling succeeded
14751
14752 0 if scrolling didn't find point.
14753
14754 -1 if new fonts have been loaded so that we must interrupt
14755 redisplay, adjust glyph matrices, and try again. */
14756
14757 enum
14758 {
14759 SCROLLING_SUCCESS,
14760 SCROLLING_FAILED,
14761 SCROLLING_NEED_LARGER_MATRICES
14762 };
14763
14764 /* If scroll-conservatively is more than this, never recenter.
14765
14766 If you change this, don't forget to update the doc string of
14767 `scroll-conservatively' and the Emacs manual. */
14768 #define SCROLL_LIMIT 100
14769
14770 static int
14771 try_scrolling (Lisp_Object window, int just_this_one_p,
14772 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14773 int temp_scroll_step, int last_line_misfit)
14774 {
14775 struct window *w = XWINDOW (window);
14776 struct frame *f = XFRAME (w->frame);
14777 struct text_pos pos, startp;
14778 struct it it;
14779 int this_scroll_margin, scroll_max, rc, height;
14780 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14781 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14782 Lisp_Object aggressive;
14783 /* We will never try scrolling more than this number of lines. */
14784 int scroll_limit = SCROLL_LIMIT;
14785 int frame_line_height = default_line_pixel_height (w);
14786 int window_total_lines
14787 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14788
14789 #ifdef GLYPH_DEBUG
14790 debug_method_add (w, "try_scrolling");
14791 #endif
14792
14793 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14794
14795 /* Compute scroll margin height in pixels. We scroll when point is
14796 within this distance from the top or bottom of the window. */
14797 if (scroll_margin > 0)
14798 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14799 * frame_line_height;
14800 else
14801 this_scroll_margin = 0;
14802
14803 /* Force arg_scroll_conservatively to have a reasonable value, to
14804 avoid scrolling too far away with slow move_it_* functions. Note
14805 that the user can supply scroll-conservatively equal to
14806 `most-positive-fixnum', which can be larger than INT_MAX. */
14807 if (arg_scroll_conservatively > scroll_limit)
14808 {
14809 arg_scroll_conservatively = scroll_limit + 1;
14810 scroll_max = scroll_limit * frame_line_height;
14811 }
14812 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14813 /* Compute how much we should try to scroll maximally to bring
14814 point into view. */
14815 scroll_max = (max (scroll_step,
14816 max (arg_scroll_conservatively, temp_scroll_step))
14817 * frame_line_height);
14818 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14819 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14820 /* We're trying to scroll because of aggressive scrolling but no
14821 scroll_step is set. Choose an arbitrary one. */
14822 scroll_max = 10 * frame_line_height;
14823 else
14824 scroll_max = 0;
14825
14826 too_near_end:
14827
14828 /* Decide whether to scroll down. */
14829 if (PT > CHARPOS (startp))
14830 {
14831 int scroll_margin_y;
14832
14833 /* Compute the pixel ypos of the scroll margin, then move IT to
14834 either that ypos or PT, whichever comes first. */
14835 start_display (&it, w, startp);
14836 scroll_margin_y = it.last_visible_y - this_scroll_margin
14837 - frame_line_height * extra_scroll_margin_lines;
14838 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14839 (MOVE_TO_POS | MOVE_TO_Y));
14840
14841 if (PT > CHARPOS (it.current.pos))
14842 {
14843 int y0 = line_bottom_y (&it);
14844 /* Compute how many pixels below window bottom to stop searching
14845 for PT. This avoids costly search for PT that is far away if
14846 the user limited scrolling by a small number of lines, but
14847 always finds PT if scroll_conservatively is set to a large
14848 number, such as most-positive-fixnum. */
14849 int slack = max (scroll_max, 10 * frame_line_height);
14850 int y_to_move = it.last_visible_y + slack;
14851
14852 /* Compute the distance from the scroll margin to PT or to
14853 the scroll limit, whichever comes first. This should
14854 include the height of the cursor line, to make that line
14855 fully visible. */
14856 move_it_to (&it, PT, -1, y_to_move,
14857 -1, MOVE_TO_POS | MOVE_TO_Y);
14858 dy = line_bottom_y (&it) - y0;
14859
14860 if (dy > scroll_max)
14861 return SCROLLING_FAILED;
14862
14863 if (dy > 0)
14864 scroll_down_p = 1;
14865 }
14866 }
14867
14868 if (scroll_down_p)
14869 {
14870 /* Point is in or below the bottom scroll margin, so move the
14871 window start down. If scrolling conservatively, move it just
14872 enough down to make point visible. If scroll_step is set,
14873 move it down by scroll_step. */
14874 if (arg_scroll_conservatively)
14875 amount_to_scroll
14876 = min (max (dy, frame_line_height),
14877 frame_line_height * arg_scroll_conservatively);
14878 else if (scroll_step || temp_scroll_step)
14879 amount_to_scroll = scroll_max;
14880 else
14881 {
14882 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14883 height = WINDOW_BOX_TEXT_HEIGHT (w);
14884 if (NUMBERP (aggressive))
14885 {
14886 double float_amount = XFLOATINT (aggressive) * height;
14887 int aggressive_scroll = float_amount;
14888 if (aggressive_scroll == 0 && float_amount > 0)
14889 aggressive_scroll = 1;
14890 /* Don't let point enter the scroll margin near top of
14891 the window. This could happen if the value of
14892 scroll_up_aggressively is too large and there are
14893 non-zero margins, because scroll_up_aggressively
14894 means put point that fraction of window height
14895 _from_the_bottom_margin_. */
14896 if (aggressive_scroll + 2*this_scroll_margin > height)
14897 aggressive_scroll = height - 2*this_scroll_margin;
14898 amount_to_scroll = dy + aggressive_scroll;
14899 }
14900 }
14901
14902 if (amount_to_scroll <= 0)
14903 return SCROLLING_FAILED;
14904
14905 start_display (&it, w, startp);
14906 if (arg_scroll_conservatively <= scroll_limit)
14907 move_it_vertically (&it, amount_to_scroll);
14908 else
14909 {
14910 /* Extra precision for users who set scroll-conservatively
14911 to a large number: make sure the amount we scroll
14912 the window start is never less than amount_to_scroll,
14913 which was computed as distance from window bottom to
14914 point. This matters when lines at window top and lines
14915 below window bottom have different height. */
14916 struct it it1;
14917 void *it1data = NULL;
14918 /* We use a temporary it1 because line_bottom_y can modify
14919 its argument, if it moves one line down; see there. */
14920 int start_y;
14921
14922 SAVE_IT (it1, it, it1data);
14923 start_y = line_bottom_y (&it1);
14924 do {
14925 RESTORE_IT (&it, &it, it1data);
14926 move_it_by_lines (&it, 1);
14927 SAVE_IT (it1, it, it1data);
14928 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14929 }
14930
14931 /* If STARTP is unchanged, move it down another screen line. */
14932 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14933 move_it_by_lines (&it, 1);
14934 startp = it.current.pos;
14935 }
14936 else
14937 {
14938 struct text_pos scroll_margin_pos = startp;
14939 int y_offset = 0;
14940
14941 /* See if point is inside the scroll margin at the top of the
14942 window. */
14943 if (this_scroll_margin)
14944 {
14945 int y_start;
14946
14947 start_display (&it, w, startp);
14948 y_start = it.current_y;
14949 move_it_vertically (&it, this_scroll_margin);
14950 scroll_margin_pos = it.current.pos;
14951 /* If we didn't move enough before hitting ZV, request
14952 additional amount of scroll, to move point out of the
14953 scroll margin. */
14954 if (IT_CHARPOS (it) == ZV
14955 && it.current_y - y_start < this_scroll_margin)
14956 y_offset = this_scroll_margin - (it.current_y - y_start);
14957 }
14958
14959 if (PT < CHARPOS (scroll_margin_pos))
14960 {
14961 /* Point is in the scroll margin at the top of the window or
14962 above what is displayed in the window. */
14963 int y0, y_to_move;
14964
14965 /* Compute the vertical distance from PT to the scroll
14966 margin position. Move as far as scroll_max allows, or
14967 one screenful, or 10 screen lines, whichever is largest.
14968 Give up if distance is greater than scroll_max or if we
14969 didn't reach the scroll margin position. */
14970 SET_TEXT_POS (pos, PT, PT_BYTE);
14971 start_display (&it, w, pos);
14972 y0 = it.current_y;
14973 y_to_move = max (it.last_visible_y,
14974 max (scroll_max, 10 * frame_line_height));
14975 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14976 y_to_move, -1,
14977 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14978 dy = it.current_y - y0;
14979 if (dy > scroll_max
14980 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14981 return SCROLLING_FAILED;
14982
14983 /* Additional scroll for when ZV was too close to point. */
14984 dy += y_offset;
14985
14986 /* Compute new window start. */
14987 start_display (&it, w, startp);
14988
14989 if (arg_scroll_conservatively)
14990 amount_to_scroll = max (dy, frame_line_height *
14991 max (scroll_step, temp_scroll_step));
14992 else if (scroll_step || temp_scroll_step)
14993 amount_to_scroll = scroll_max;
14994 else
14995 {
14996 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14997 height = WINDOW_BOX_TEXT_HEIGHT (w);
14998 if (NUMBERP (aggressive))
14999 {
15000 double float_amount = XFLOATINT (aggressive) * height;
15001 int aggressive_scroll = float_amount;
15002 if (aggressive_scroll == 0 && float_amount > 0)
15003 aggressive_scroll = 1;
15004 /* Don't let point enter the scroll margin near
15005 bottom of the window, if the value of
15006 scroll_down_aggressively happens to be too
15007 large. */
15008 if (aggressive_scroll + 2*this_scroll_margin > height)
15009 aggressive_scroll = height - 2*this_scroll_margin;
15010 amount_to_scroll = dy + aggressive_scroll;
15011 }
15012 }
15013
15014 if (amount_to_scroll <= 0)
15015 return SCROLLING_FAILED;
15016
15017 move_it_vertically_backward (&it, amount_to_scroll);
15018 startp = it.current.pos;
15019 }
15020 }
15021
15022 /* Run window scroll functions. */
15023 startp = run_window_scroll_functions (window, startp);
15024
15025 /* Display the window. Give up if new fonts are loaded, or if point
15026 doesn't appear. */
15027 if (!try_window (window, startp, 0))
15028 rc = SCROLLING_NEED_LARGER_MATRICES;
15029 else if (w->cursor.vpos < 0)
15030 {
15031 clear_glyph_matrix (w->desired_matrix);
15032 rc = SCROLLING_FAILED;
15033 }
15034 else
15035 {
15036 /* Maybe forget recorded base line for line number display. */
15037 if (!just_this_one_p
15038 || current_buffer->clip_changed
15039 || BEG_UNCHANGED < CHARPOS (startp))
15040 w->base_line_number = 0;
15041
15042 /* If cursor ends up on a partially visible line,
15043 treat that as being off the bottom of the screen. */
15044 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15045 /* It's possible that the cursor is on the first line of the
15046 buffer, which is partially obscured due to a vscroll
15047 (Bug#7537). In that case, avoid looping forever . */
15048 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15049 {
15050 clear_glyph_matrix (w->desired_matrix);
15051 ++extra_scroll_margin_lines;
15052 goto too_near_end;
15053 }
15054 rc = SCROLLING_SUCCESS;
15055 }
15056
15057 return rc;
15058 }
15059
15060
15061 /* Compute a suitable window start for window W if display of W starts
15062 on a continuation line. Value is non-zero if a new window start
15063 was computed.
15064
15065 The new window start will be computed, based on W's width, starting
15066 from the start of the continued line. It is the start of the
15067 screen line with the minimum distance from the old start W->start. */
15068
15069 static int
15070 compute_window_start_on_continuation_line (struct window *w)
15071 {
15072 struct text_pos pos, start_pos;
15073 int window_start_changed_p = 0;
15074
15075 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15076
15077 /* If window start is on a continuation line... Window start may be
15078 < BEGV in case there's invisible text at the start of the
15079 buffer (M-x rmail, for example). */
15080 if (CHARPOS (start_pos) > BEGV
15081 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15082 {
15083 struct it it;
15084 struct glyph_row *row;
15085
15086 /* Handle the case that the window start is out of range. */
15087 if (CHARPOS (start_pos) < BEGV)
15088 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15089 else if (CHARPOS (start_pos) > ZV)
15090 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15091
15092 /* Find the start of the continued line. This should be fast
15093 because find_newline is fast (newline cache). */
15094 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15095 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15096 row, DEFAULT_FACE_ID);
15097 reseat_at_previous_visible_line_start (&it);
15098
15099 /* If the line start is "too far" away from the window start,
15100 say it takes too much time to compute a new window start. */
15101 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15102 /* PXW: Do we need upper bounds here ? */
15103 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15104 {
15105 int min_distance, distance;
15106
15107 /* Move forward by display lines to find the new window
15108 start. If window width was enlarged, the new start can
15109 be expected to be > the old start. If window width was
15110 decreased, the new window start will be < the old start.
15111 So, we're looking for the display line start with the
15112 minimum distance from the old window start. */
15113 pos = it.current.pos;
15114 min_distance = INFINITY;
15115 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15116 distance < min_distance)
15117 {
15118 min_distance = distance;
15119 pos = it.current.pos;
15120 if (it.line_wrap == WORD_WRAP)
15121 {
15122 /* Under WORD_WRAP, move_it_by_lines is likely to
15123 overshoot and stop not at the first, but the
15124 second character from the left margin. So in
15125 that case, we need a more tight control on the X
15126 coordinate of the iterator than move_it_by_lines
15127 promises in its contract. The method is to first
15128 go to the last (rightmost) visible character of a
15129 line, then move to the leftmost character on the
15130 next line in a separate call. */
15131 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15132 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15133 move_it_to (&it, ZV, 0,
15134 it.current_y + it.max_ascent + it.max_descent, -1,
15135 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15136 }
15137 else
15138 move_it_by_lines (&it, 1);
15139 }
15140
15141 /* Set the window start there. */
15142 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15143 window_start_changed_p = 1;
15144 }
15145 }
15146
15147 return window_start_changed_p;
15148 }
15149
15150
15151 /* Try cursor movement in case text has not changed in window WINDOW,
15152 with window start STARTP. Value is
15153
15154 CURSOR_MOVEMENT_SUCCESS if successful
15155
15156 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15157
15158 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15159 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15160 we want to scroll as if scroll-step were set to 1. See the code.
15161
15162 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15163 which case we have to abort this redisplay, and adjust matrices
15164 first. */
15165
15166 enum
15167 {
15168 CURSOR_MOVEMENT_SUCCESS,
15169 CURSOR_MOVEMENT_CANNOT_BE_USED,
15170 CURSOR_MOVEMENT_MUST_SCROLL,
15171 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15172 };
15173
15174 static int
15175 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15176 {
15177 struct window *w = XWINDOW (window);
15178 struct frame *f = XFRAME (w->frame);
15179 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15180
15181 #ifdef GLYPH_DEBUG
15182 if (inhibit_try_cursor_movement)
15183 return rc;
15184 #endif
15185
15186 /* Previously, there was a check for Lisp integer in the
15187 if-statement below. Now, this field is converted to
15188 ptrdiff_t, thus zero means invalid position in a buffer. */
15189 eassert (w->last_point > 0);
15190 /* Likewise there was a check whether window_end_vpos is nil or larger
15191 than the window. Now window_end_vpos is int and so never nil, but
15192 let's leave eassert to check whether it fits in the window. */
15193 eassert (w->window_end_vpos < w->current_matrix->nrows);
15194
15195 /* Handle case where text has not changed, only point, and it has
15196 not moved off the frame. */
15197 if (/* Point may be in this window. */
15198 PT >= CHARPOS (startp)
15199 /* Selective display hasn't changed. */
15200 && !current_buffer->clip_changed
15201 /* Function force-mode-line-update is used to force a thorough
15202 redisplay. It sets either windows_or_buffers_changed or
15203 update_mode_lines. So don't take a shortcut here for these
15204 cases. */
15205 && !update_mode_lines
15206 && !windows_or_buffers_changed
15207 && !f->cursor_type_changed
15208 && NILP (Vshow_trailing_whitespace)
15209 /* This code is not used for mini-buffer for the sake of the case
15210 of redisplaying to replace an echo area message; since in
15211 that case the mini-buffer contents per se are usually
15212 unchanged. This code is of no real use in the mini-buffer
15213 since the handling of this_line_start_pos, etc., in redisplay
15214 handles the same cases. */
15215 && !EQ (window, minibuf_window)
15216 && (FRAME_WINDOW_P (f)
15217 || !overlay_arrow_in_current_buffer_p ()))
15218 {
15219 int this_scroll_margin, top_scroll_margin;
15220 struct glyph_row *row = NULL;
15221 int frame_line_height = default_line_pixel_height (w);
15222 int window_total_lines
15223 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15224
15225 #ifdef GLYPH_DEBUG
15226 debug_method_add (w, "cursor movement");
15227 #endif
15228
15229 /* Scroll if point within this distance from the top or bottom
15230 of the window. This is a pixel value. */
15231 if (scroll_margin > 0)
15232 {
15233 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15234 this_scroll_margin *= frame_line_height;
15235 }
15236 else
15237 this_scroll_margin = 0;
15238
15239 top_scroll_margin = this_scroll_margin;
15240 if (WINDOW_WANTS_HEADER_LINE_P (w))
15241 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15242
15243 /* Start with the row the cursor was displayed during the last
15244 not paused redisplay. Give up if that row is not valid. */
15245 if (w->last_cursor_vpos < 0
15246 || w->last_cursor_vpos >= w->current_matrix->nrows)
15247 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15248 else
15249 {
15250 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15251 if (row->mode_line_p)
15252 ++row;
15253 if (!row->enabled_p)
15254 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15255 }
15256
15257 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15258 {
15259 int scroll_p = 0, must_scroll = 0;
15260 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15261
15262 if (PT > w->last_point)
15263 {
15264 /* Point has moved forward. */
15265 while (MATRIX_ROW_END_CHARPOS (row) < PT
15266 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15267 {
15268 eassert (row->enabled_p);
15269 ++row;
15270 }
15271
15272 /* If the end position of a row equals the start
15273 position of the next row, and PT is at that position,
15274 we would rather display cursor in the next line. */
15275 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15276 && MATRIX_ROW_END_CHARPOS (row) == PT
15277 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15278 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15279 && !cursor_row_p (row))
15280 ++row;
15281
15282 /* If within the scroll margin, scroll. Note that
15283 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15284 the next line would be drawn, and that
15285 this_scroll_margin can be zero. */
15286 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15287 || PT > MATRIX_ROW_END_CHARPOS (row)
15288 /* Line is completely visible last line in window
15289 and PT is to be set in the next line. */
15290 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15291 && PT == MATRIX_ROW_END_CHARPOS (row)
15292 && !row->ends_at_zv_p
15293 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15294 scroll_p = 1;
15295 }
15296 else if (PT < w->last_point)
15297 {
15298 /* Cursor has to be moved backward. Note that PT >=
15299 CHARPOS (startp) because of the outer if-statement. */
15300 while (!row->mode_line_p
15301 && (MATRIX_ROW_START_CHARPOS (row) > PT
15302 || (MATRIX_ROW_START_CHARPOS (row) == PT
15303 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15304 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15305 row > w->current_matrix->rows
15306 && (row-1)->ends_in_newline_from_string_p))))
15307 && (row->y > top_scroll_margin
15308 || CHARPOS (startp) == BEGV))
15309 {
15310 eassert (row->enabled_p);
15311 --row;
15312 }
15313
15314 /* Consider the following case: Window starts at BEGV,
15315 there is invisible, intangible text at BEGV, so that
15316 display starts at some point START > BEGV. It can
15317 happen that we are called with PT somewhere between
15318 BEGV and START. Try to handle that case. */
15319 if (row < w->current_matrix->rows
15320 || row->mode_line_p)
15321 {
15322 row = w->current_matrix->rows;
15323 if (row->mode_line_p)
15324 ++row;
15325 }
15326
15327 /* Due to newlines in overlay strings, we may have to
15328 skip forward over overlay strings. */
15329 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15330 && MATRIX_ROW_END_CHARPOS (row) == PT
15331 && !cursor_row_p (row))
15332 ++row;
15333
15334 /* If within the scroll margin, scroll. */
15335 if (row->y < top_scroll_margin
15336 && CHARPOS (startp) != BEGV)
15337 scroll_p = 1;
15338 }
15339 else
15340 {
15341 /* Cursor did not move. So don't scroll even if cursor line
15342 is partially visible, as it was so before. */
15343 rc = CURSOR_MOVEMENT_SUCCESS;
15344 }
15345
15346 if (PT < MATRIX_ROW_START_CHARPOS (row)
15347 || PT > MATRIX_ROW_END_CHARPOS (row))
15348 {
15349 /* if PT is not in the glyph row, give up. */
15350 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15351 must_scroll = 1;
15352 }
15353 else if (rc != CURSOR_MOVEMENT_SUCCESS
15354 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15355 {
15356 struct glyph_row *row1;
15357
15358 /* If rows are bidi-reordered and point moved, back up
15359 until we find a row that does not belong to a
15360 continuation line. This is because we must consider
15361 all rows of a continued line as candidates for the
15362 new cursor positioning, since row start and end
15363 positions change non-linearly with vertical position
15364 in such rows. */
15365 /* FIXME: Revisit this when glyph ``spilling'' in
15366 continuation lines' rows is implemented for
15367 bidi-reordered rows. */
15368 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15369 MATRIX_ROW_CONTINUATION_LINE_P (row);
15370 --row)
15371 {
15372 /* If we hit the beginning of the displayed portion
15373 without finding the first row of a continued
15374 line, give up. */
15375 if (row <= row1)
15376 {
15377 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15378 break;
15379 }
15380 eassert (row->enabled_p);
15381 }
15382 }
15383 if (must_scroll)
15384 ;
15385 else if (rc != CURSOR_MOVEMENT_SUCCESS
15386 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15387 /* Make sure this isn't a header line by any chance, since
15388 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15389 && !row->mode_line_p
15390 && make_cursor_line_fully_visible_p)
15391 {
15392 if (PT == MATRIX_ROW_END_CHARPOS (row)
15393 && !row->ends_at_zv_p
15394 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15395 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15396 else if (row->height > window_box_height (w))
15397 {
15398 /* If we end up in a partially visible line, let's
15399 make it fully visible, except when it's taller
15400 than the window, in which case we can't do much
15401 about it. */
15402 *scroll_step = 1;
15403 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15404 }
15405 else
15406 {
15407 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15408 if (!cursor_row_fully_visible_p (w, 0, 1))
15409 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15410 else
15411 rc = CURSOR_MOVEMENT_SUCCESS;
15412 }
15413 }
15414 else if (scroll_p)
15415 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15416 else if (rc != CURSOR_MOVEMENT_SUCCESS
15417 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15418 {
15419 /* With bidi-reordered rows, there could be more than
15420 one candidate row whose start and end positions
15421 occlude point. We need to let set_cursor_from_row
15422 find the best candidate. */
15423 /* FIXME: Revisit this when glyph ``spilling'' in
15424 continuation lines' rows is implemented for
15425 bidi-reordered rows. */
15426 int rv = 0;
15427
15428 do
15429 {
15430 int at_zv_p = 0, exact_match_p = 0;
15431
15432 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15433 && PT <= MATRIX_ROW_END_CHARPOS (row)
15434 && cursor_row_p (row))
15435 rv |= set_cursor_from_row (w, row, w->current_matrix,
15436 0, 0, 0, 0);
15437 /* As soon as we've found the exact match for point,
15438 or the first suitable row whose ends_at_zv_p flag
15439 is set, we are done. */
15440 at_zv_p =
15441 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15442 if (rv && !at_zv_p
15443 && w->cursor.hpos >= 0
15444 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15445 w->cursor.vpos))
15446 {
15447 struct glyph_row *candidate =
15448 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15449 struct glyph *g =
15450 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15451 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15452
15453 exact_match_p =
15454 (BUFFERP (g->object) && g->charpos == PT)
15455 || (INTEGERP (g->object)
15456 && (g->charpos == PT
15457 || (g->charpos == 0 && endpos - 1 == PT)));
15458 }
15459 if (rv && (at_zv_p || exact_match_p))
15460 {
15461 rc = CURSOR_MOVEMENT_SUCCESS;
15462 break;
15463 }
15464 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15465 break;
15466 ++row;
15467 }
15468 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15469 || row->continued_p)
15470 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15471 || (MATRIX_ROW_START_CHARPOS (row) == PT
15472 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15473 /* If we didn't find any candidate rows, or exited the
15474 loop before all the candidates were examined, signal
15475 to the caller that this method failed. */
15476 if (rc != CURSOR_MOVEMENT_SUCCESS
15477 && !(rv
15478 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15479 && !row->continued_p))
15480 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15481 else if (rv)
15482 rc = CURSOR_MOVEMENT_SUCCESS;
15483 }
15484 else
15485 {
15486 do
15487 {
15488 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15489 {
15490 rc = CURSOR_MOVEMENT_SUCCESS;
15491 break;
15492 }
15493 ++row;
15494 }
15495 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15496 && MATRIX_ROW_START_CHARPOS (row) == PT
15497 && cursor_row_p (row));
15498 }
15499 }
15500 }
15501
15502 return rc;
15503 }
15504
15505 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15506 static
15507 #endif
15508 void
15509 set_vertical_scroll_bar (struct window *w)
15510 {
15511 ptrdiff_t start, end, whole;
15512
15513 /* Calculate the start and end positions for the current window.
15514 At some point, it would be nice to choose between scrollbars
15515 which reflect the whole buffer size, with special markers
15516 indicating narrowing, and scrollbars which reflect only the
15517 visible region.
15518
15519 Note that mini-buffers sometimes aren't displaying any text. */
15520 if (!MINI_WINDOW_P (w)
15521 || (w == XWINDOW (minibuf_window)
15522 && NILP (echo_area_buffer[0])))
15523 {
15524 struct buffer *buf = XBUFFER (w->contents);
15525 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15526 start = marker_position (w->start) - BUF_BEGV (buf);
15527 /* I don't think this is guaranteed to be right. For the
15528 moment, we'll pretend it is. */
15529 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15530
15531 if (end < start)
15532 end = start;
15533 if (whole < (end - start))
15534 whole = end - start;
15535 }
15536 else
15537 start = end = whole = 0;
15538
15539 /* Indicate what this scroll bar ought to be displaying now. */
15540 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15541 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15542 (w, end - start, whole, start);
15543 }
15544
15545
15546 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15547 selected_window is redisplayed.
15548
15549 We can return without actually redisplaying the window if fonts has been
15550 changed on window's frame. In that case, redisplay_internal will retry. */
15551
15552 static void
15553 redisplay_window (Lisp_Object window, bool just_this_one_p)
15554 {
15555 struct window *w = XWINDOW (window);
15556 struct frame *f = XFRAME (w->frame);
15557 struct buffer *buffer = XBUFFER (w->contents);
15558 struct buffer *old = current_buffer;
15559 struct text_pos lpoint, opoint, startp;
15560 int update_mode_line;
15561 int tem;
15562 struct it it;
15563 /* Record it now because it's overwritten. */
15564 bool current_matrix_up_to_date_p = false;
15565 bool used_current_matrix_p = false;
15566 /* This is less strict than current_matrix_up_to_date_p.
15567 It indicates that the buffer contents and narrowing are unchanged. */
15568 bool buffer_unchanged_p = false;
15569 int temp_scroll_step = 0;
15570 ptrdiff_t count = SPECPDL_INDEX ();
15571 int rc;
15572 int centering_position = -1;
15573 int last_line_misfit = 0;
15574 ptrdiff_t beg_unchanged, end_unchanged;
15575 int frame_line_height;
15576
15577 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15578 opoint = lpoint;
15579
15580 #ifdef GLYPH_DEBUG
15581 *w->desired_matrix->method = 0;
15582 #endif
15583
15584 if (!just_this_one_p
15585 && REDISPLAY_SOME_P ()
15586 && !w->redisplay
15587 && !f->redisplay
15588 && !buffer->text->redisplay)
15589 return;
15590
15591 /* Make sure that both W's markers are valid. */
15592 eassert (XMARKER (w->start)->buffer == buffer);
15593 eassert (XMARKER (w->pointm)->buffer == buffer);
15594
15595 restart:
15596 reconsider_clip_changes (w);
15597 frame_line_height = default_line_pixel_height (w);
15598
15599 /* Has the mode line to be updated? */
15600 update_mode_line = (w->update_mode_line
15601 || update_mode_lines
15602 || buffer->clip_changed
15603 || buffer->prevent_redisplay_optimizations_p);
15604
15605 if (!just_this_one_p)
15606 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15607 cleverly elsewhere. */
15608 w->must_be_updated_p = true;
15609
15610 if (MINI_WINDOW_P (w))
15611 {
15612 if (w == XWINDOW (echo_area_window)
15613 && !NILP (echo_area_buffer[0]))
15614 {
15615 if (update_mode_line)
15616 /* We may have to update a tty frame's menu bar or a
15617 tool-bar. Example `M-x C-h C-h C-g'. */
15618 goto finish_menu_bars;
15619 else
15620 /* We've already displayed the echo area glyphs in this window. */
15621 goto finish_scroll_bars;
15622 }
15623 else if ((w != XWINDOW (minibuf_window)
15624 || minibuf_level == 0)
15625 /* When buffer is nonempty, redisplay window normally. */
15626 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15627 /* Quail displays non-mini buffers in minibuffer window.
15628 In that case, redisplay the window normally. */
15629 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15630 {
15631 /* W is a mini-buffer window, but it's not active, so clear
15632 it. */
15633 int yb = window_text_bottom_y (w);
15634 struct glyph_row *row;
15635 int y;
15636
15637 for (y = 0, row = w->desired_matrix->rows;
15638 y < yb;
15639 y += row->height, ++row)
15640 blank_row (w, row, y);
15641 goto finish_scroll_bars;
15642 }
15643
15644 clear_glyph_matrix (w->desired_matrix);
15645 }
15646
15647 /* Otherwise set up data on this window; select its buffer and point
15648 value. */
15649 /* Really select the buffer, for the sake of buffer-local
15650 variables. */
15651 set_buffer_internal_1 (XBUFFER (w->contents));
15652
15653 current_matrix_up_to_date_p
15654 = (w->window_end_valid
15655 && !current_buffer->clip_changed
15656 && !current_buffer->prevent_redisplay_optimizations_p
15657 && !window_outdated (w));
15658
15659 /* Run the window-bottom-change-functions
15660 if it is possible that the text on the screen has changed
15661 (either due to modification of the text, or any other reason). */
15662 if (!current_matrix_up_to_date_p
15663 && !NILP (Vwindow_text_change_functions))
15664 {
15665 safe_run_hooks (Qwindow_text_change_functions);
15666 goto restart;
15667 }
15668
15669 beg_unchanged = BEG_UNCHANGED;
15670 end_unchanged = END_UNCHANGED;
15671
15672 SET_TEXT_POS (opoint, PT, PT_BYTE);
15673
15674 specbind (Qinhibit_point_motion_hooks, Qt);
15675
15676 buffer_unchanged_p
15677 = (w->window_end_valid
15678 && !current_buffer->clip_changed
15679 && !window_outdated (w));
15680
15681 /* When windows_or_buffers_changed is non-zero, we can't rely
15682 on the window end being valid, so set it to zero there. */
15683 if (windows_or_buffers_changed)
15684 {
15685 /* If window starts on a continuation line, maybe adjust the
15686 window start in case the window's width changed. */
15687 if (XMARKER (w->start)->buffer == current_buffer)
15688 compute_window_start_on_continuation_line (w);
15689
15690 w->window_end_valid = false;
15691 /* If so, we also can't rely on current matrix
15692 and should not fool try_cursor_movement below. */
15693 current_matrix_up_to_date_p = false;
15694 }
15695
15696 /* Some sanity checks. */
15697 CHECK_WINDOW_END (w);
15698 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15699 emacs_abort ();
15700 if (BYTEPOS (opoint) < CHARPOS (opoint))
15701 emacs_abort ();
15702
15703 if (mode_line_update_needed (w))
15704 update_mode_line = 1;
15705
15706 /* Point refers normally to the selected window. For any other
15707 window, set up appropriate value. */
15708 if (!EQ (window, selected_window))
15709 {
15710 ptrdiff_t new_pt = marker_position (w->pointm);
15711 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15712 if (new_pt < BEGV)
15713 {
15714 new_pt = BEGV;
15715 new_pt_byte = BEGV_BYTE;
15716 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15717 }
15718 else if (new_pt > (ZV - 1))
15719 {
15720 new_pt = ZV;
15721 new_pt_byte = ZV_BYTE;
15722 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15723 }
15724
15725 /* We don't use SET_PT so that the point-motion hooks don't run. */
15726 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15727 }
15728
15729 /* If any of the character widths specified in the display table
15730 have changed, invalidate the width run cache. It's true that
15731 this may be a bit late to catch such changes, but the rest of
15732 redisplay goes (non-fatally) haywire when the display table is
15733 changed, so why should we worry about doing any better? */
15734 if (current_buffer->width_run_cache)
15735 {
15736 struct Lisp_Char_Table *disptab = buffer_display_table ();
15737
15738 if (! disptab_matches_widthtab
15739 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15740 {
15741 invalidate_region_cache (current_buffer,
15742 current_buffer->width_run_cache,
15743 BEG, Z);
15744 recompute_width_table (current_buffer, disptab);
15745 }
15746 }
15747
15748 /* If window-start is screwed up, choose a new one. */
15749 if (XMARKER (w->start)->buffer != current_buffer)
15750 goto recenter;
15751
15752 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15753
15754 /* If someone specified a new starting point but did not insist,
15755 check whether it can be used. */
15756 if (w->optional_new_start
15757 && CHARPOS (startp) >= BEGV
15758 && CHARPOS (startp) <= ZV)
15759 {
15760 w->optional_new_start = 0;
15761 start_display (&it, w, startp);
15762 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15763 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15764 if (IT_CHARPOS (it) == PT)
15765 w->force_start = 1;
15766 /* IT may overshoot PT if text at PT is invisible. */
15767 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15768 w->force_start = 1;
15769 }
15770
15771 force_start:
15772
15773 /* Handle case where place to start displaying has been specified,
15774 unless the specified location is outside the accessible range. */
15775 if (w->force_start || window_frozen_p (w))
15776 {
15777 /* We set this later on if we have to adjust point. */
15778 int new_vpos = -1;
15779
15780 w->force_start = 0;
15781 w->vscroll = 0;
15782 w->window_end_valid = 0;
15783
15784 /* Forget any recorded base line for line number display. */
15785 if (!buffer_unchanged_p)
15786 w->base_line_number = 0;
15787
15788 /* Redisplay the mode line. Select the buffer properly for that.
15789 Also, run the hook window-scroll-functions
15790 because we have scrolled. */
15791 /* Note, we do this after clearing force_start because
15792 if there's an error, it is better to forget about force_start
15793 than to get into an infinite loop calling the hook functions
15794 and having them get more errors. */
15795 if (!update_mode_line
15796 || ! NILP (Vwindow_scroll_functions))
15797 {
15798 update_mode_line = 1;
15799 w->update_mode_line = 1;
15800 startp = run_window_scroll_functions (window, startp);
15801 }
15802
15803 if (CHARPOS (startp) < BEGV)
15804 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15805 else if (CHARPOS (startp) > ZV)
15806 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15807
15808 /* Redisplay, then check if cursor has been set during the
15809 redisplay. Give up if new fonts were loaded. */
15810 /* We used to issue a CHECK_MARGINS argument to try_window here,
15811 but this causes scrolling to fail when point begins inside
15812 the scroll margin (bug#148) -- cyd */
15813 if (!try_window (window, startp, 0))
15814 {
15815 w->force_start = 1;
15816 clear_glyph_matrix (w->desired_matrix);
15817 goto need_larger_matrices;
15818 }
15819
15820 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15821 {
15822 /* If point does not appear, try to move point so it does
15823 appear. The desired matrix has been built above, so we
15824 can use it here. */
15825 new_vpos = window_box_height (w) / 2;
15826 }
15827
15828 if (!cursor_row_fully_visible_p (w, 0, 0))
15829 {
15830 /* Point does appear, but on a line partly visible at end of window.
15831 Move it back to a fully-visible line. */
15832 new_vpos = window_box_height (w);
15833 }
15834 else if (w->cursor.vpos >= 0)
15835 {
15836 /* Some people insist on not letting point enter the scroll
15837 margin, even though this part handles windows that didn't
15838 scroll at all. */
15839 int window_total_lines
15840 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15841 int margin = min (scroll_margin, window_total_lines / 4);
15842 int pixel_margin = margin * frame_line_height;
15843 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15844
15845 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15846 below, which finds the row to move point to, advances by
15847 the Y coordinate of the _next_ row, see the definition of
15848 MATRIX_ROW_BOTTOM_Y. */
15849 if (w->cursor.vpos < margin + header_line)
15850 {
15851 w->cursor.vpos = -1;
15852 clear_glyph_matrix (w->desired_matrix);
15853 goto try_to_scroll;
15854 }
15855 else
15856 {
15857 int window_height = window_box_height (w);
15858
15859 if (header_line)
15860 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15861 if (w->cursor.y >= window_height - pixel_margin)
15862 {
15863 w->cursor.vpos = -1;
15864 clear_glyph_matrix (w->desired_matrix);
15865 goto try_to_scroll;
15866 }
15867 }
15868 }
15869
15870 /* If we need to move point for either of the above reasons,
15871 now actually do it. */
15872 if (new_vpos >= 0)
15873 {
15874 struct glyph_row *row;
15875
15876 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15877 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15878 ++row;
15879
15880 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15881 MATRIX_ROW_START_BYTEPOS (row));
15882
15883 if (w != XWINDOW (selected_window))
15884 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15885 else if (current_buffer == old)
15886 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15887
15888 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15889
15890 /* If we are highlighting the region, then we just changed
15891 the region, so redisplay to show it. */
15892 /* FIXME: We need to (re)run pre-redisplay-function! */
15893 /* if (markpos_of_region () >= 0)
15894 {
15895 clear_glyph_matrix (w->desired_matrix);
15896 if (!try_window (window, startp, 0))
15897 goto need_larger_matrices;
15898 }
15899 */
15900 }
15901
15902 #ifdef GLYPH_DEBUG
15903 debug_method_add (w, "forced window start");
15904 #endif
15905 goto done;
15906 }
15907
15908 /* Handle case where text has not changed, only point, and it has
15909 not moved off the frame, and we are not retrying after hscroll.
15910 (current_matrix_up_to_date_p is nonzero when retrying.) */
15911 if (current_matrix_up_to_date_p
15912 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15913 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15914 {
15915 switch (rc)
15916 {
15917 case CURSOR_MOVEMENT_SUCCESS:
15918 used_current_matrix_p = 1;
15919 goto done;
15920
15921 case CURSOR_MOVEMENT_MUST_SCROLL:
15922 goto try_to_scroll;
15923
15924 default:
15925 emacs_abort ();
15926 }
15927 }
15928 /* If current starting point was originally the beginning of a line
15929 but no longer is, find a new starting point. */
15930 else if (w->start_at_line_beg
15931 && !(CHARPOS (startp) <= BEGV
15932 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15933 {
15934 #ifdef GLYPH_DEBUG
15935 debug_method_add (w, "recenter 1");
15936 #endif
15937 goto recenter;
15938 }
15939
15940 /* Try scrolling with try_window_id. Value is > 0 if update has
15941 been done, it is -1 if we know that the same window start will
15942 not work. It is 0 if unsuccessful for some other reason. */
15943 else if ((tem = try_window_id (w)) != 0)
15944 {
15945 #ifdef GLYPH_DEBUG
15946 debug_method_add (w, "try_window_id %d", tem);
15947 #endif
15948
15949 if (f->fonts_changed)
15950 goto need_larger_matrices;
15951 if (tem > 0)
15952 goto done;
15953
15954 /* Otherwise try_window_id has returned -1 which means that we
15955 don't want the alternative below this comment to execute. */
15956 }
15957 else if (CHARPOS (startp) >= BEGV
15958 && CHARPOS (startp) <= ZV
15959 && PT >= CHARPOS (startp)
15960 && (CHARPOS (startp) < ZV
15961 /* Avoid starting at end of buffer. */
15962 || CHARPOS (startp) == BEGV
15963 || !window_outdated (w)))
15964 {
15965 int d1, d2, d3, d4, d5, d6;
15966
15967 /* If first window line is a continuation line, and window start
15968 is inside the modified region, but the first change is before
15969 current window start, we must select a new window start.
15970
15971 However, if this is the result of a down-mouse event (e.g. by
15972 extending the mouse-drag-overlay), we don't want to select a
15973 new window start, since that would change the position under
15974 the mouse, resulting in an unwanted mouse-movement rather
15975 than a simple mouse-click. */
15976 if (!w->start_at_line_beg
15977 && NILP (do_mouse_tracking)
15978 && CHARPOS (startp) > BEGV
15979 && CHARPOS (startp) > BEG + beg_unchanged
15980 && CHARPOS (startp) <= Z - end_unchanged
15981 /* Even if w->start_at_line_beg is nil, a new window may
15982 start at a line_beg, since that's how set_buffer_window
15983 sets it. So, we need to check the return value of
15984 compute_window_start_on_continuation_line. (See also
15985 bug#197). */
15986 && XMARKER (w->start)->buffer == current_buffer
15987 && compute_window_start_on_continuation_line (w)
15988 /* It doesn't make sense to force the window start like we
15989 do at label force_start if it is already known that point
15990 will not be visible in the resulting window, because
15991 doing so will move point from its correct position
15992 instead of scrolling the window to bring point into view.
15993 See bug#9324. */
15994 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15995 {
15996 w->force_start = 1;
15997 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15998 goto force_start;
15999 }
16000
16001 #ifdef GLYPH_DEBUG
16002 debug_method_add (w, "same window start");
16003 #endif
16004
16005 /* Try to redisplay starting at same place as before.
16006 If point has not moved off frame, accept the results. */
16007 if (!current_matrix_up_to_date_p
16008 /* Don't use try_window_reusing_current_matrix in this case
16009 because a window scroll function can have changed the
16010 buffer. */
16011 || !NILP (Vwindow_scroll_functions)
16012 || MINI_WINDOW_P (w)
16013 || !(used_current_matrix_p
16014 = try_window_reusing_current_matrix (w)))
16015 {
16016 IF_DEBUG (debug_method_add (w, "1"));
16017 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16018 /* -1 means we need to scroll.
16019 0 means we need new matrices, but fonts_changed
16020 is set in that case, so we will detect it below. */
16021 goto try_to_scroll;
16022 }
16023
16024 if (f->fonts_changed)
16025 goto need_larger_matrices;
16026
16027 if (w->cursor.vpos >= 0)
16028 {
16029 if (!just_this_one_p
16030 || current_buffer->clip_changed
16031 || BEG_UNCHANGED < CHARPOS (startp))
16032 /* Forget any recorded base line for line number display. */
16033 w->base_line_number = 0;
16034
16035 if (!cursor_row_fully_visible_p (w, 1, 0))
16036 {
16037 clear_glyph_matrix (w->desired_matrix);
16038 last_line_misfit = 1;
16039 }
16040 /* Drop through and scroll. */
16041 else
16042 goto done;
16043 }
16044 else
16045 clear_glyph_matrix (w->desired_matrix);
16046 }
16047
16048 try_to_scroll:
16049
16050 /* Redisplay the mode line. Select the buffer properly for that. */
16051 if (!update_mode_line)
16052 {
16053 update_mode_line = 1;
16054 w->update_mode_line = 1;
16055 }
16056
16057 /* Try to scroll by specified few lines. */
16058 if ((scroll_conservatively
16059 || emacs_scroll_step
16060 || temp_scroll_step
16061 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16062 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16063 && CHARPOS (startp) >= BEGV
16064 && CHARPOS (startp) <= ZV)
16065 {
16066 /* The function returns -1 if new fonts were loaded, 1 if
16067 successful, 0 if not successful. */
16068 int ss = try_scrolling (window, just_this_one_p,
16069 scroll_conservatively,
16070 emacs_scroll_step,
16071 temp_scroll_step, last_line_misfit);
16072 switch (ss)
16073 {
16074 case SCROLLING_SUCCESS:
16075 goto done;
16076
16077 case SCROLLING_NEED_LARGER_MATRICES:
16078 goto need_larger_matrices;
16079
16080 case SCROLLING_FAILED:
16081 break;
16082
16083 default:
16084 emacs_abort ();
16085 }
16086 }
16087
16088 /* Finally, just choose a place to start which positions point
16089 according to user preferences. */
16090
16091 recenter:
16092
16093 #ifdef GLYPH_DEBUG
16094 debug_method_add (w, "recenter");
16095 #endif
16096
16097 /* Forget any previously recorded base line for line number display. */
16098 if (!buffer_unchanged_p)
16099 w->base_line_number = 0;
16100
16101 /* Determine the window start relative to point. */
16102 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16103 it.current_y = it.last_visible_y;
16104 if (centering_position < 0)
16105 {
16106 int window_total_lines
16107 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16108 int margin =
16109 scroll_margin > 0
16110 ? min (scroll_margin, window_total_lines / 4)
16111 : 0;
16112 ptrdiff_t margin_pos = CHARPOS (startp);
16113 Lisp_Object aggressive;
16114 int scrolling_up;
16115
16116 /* If there is a scroll margin at the top of the window, find
16117 its character position. */
16118 if (margin
16119 /* Cannot call start_display if startp is not in the
16120 accessible region of the buffer. This can happen when we
16121 have just switched to a different buffer and/or changed
16122 its restriction. In that case, startp is initialized to
16123 the character position 1 (BEGV) because we did not yet
16124 have chance to display the buffer even once. */
16125 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16126 {
16127 struct it it1;
16128 void *it1data = NULL;
16129
16130 SAVE_IT (it1, it, it1data);
16131 start_display (&it1, w, startp);
16132 move_it_vertically (&it1, margin * frame_line_height);
16133 margin_pos = IT_CHARPOS (it1);
16134 RESTORE_IT (&it, &it, it1data);
16135 }
16136 scrolling_up = PT > margin_pos;
16137 aggressive =
16138 scrolling_up
16139 ? BVAR (current_buffer, scroll_up_aggressively)
16140 : BVAR (current_buffer, scroll_down_aggressively);
16141
16142 if (!MINI_WINDOW_P (w)
16143 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16144 {
16145 int pt_offset = 0;
16146
16147 /* Setting scroll-conservatively overrides
16148 scroll-*-aggressively. */
16149 if (!scroll_conservatively && NUMBERP (aggressive))
16150 {
16151 double float_amount = XFLOATINT (aggressive);
16152
16153 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16154 if (pt_offset == 0 && float_amount > 0)
16155 pt_offset = 1;
16156 if (pt_offset && margin > 0)
16157 margin -= 1;
16158 }
16159 /* Compute how much to move the window start backward from
16160 point so that point will be displayed where the user
16161 wants it. */
16162 if (scrolling_up)
16163 {
16164 centering_position = it.last_visible_y;
16165 if (pt_offset)
16166 centering_position -= pt_offset;
16167 centering_position -=
16168 frame_line_height * (1 + margin + (last_line_misfit != 0))
16169 + WINDOW_HEADER_LINE_HEIGHT (w);
16170 /* Don't let point enter the scroll margin near top of
16171 the window. */
16172 if (centering_position < margin * frame_line_height)
16173 centering_position = margin * frame_line_height;
16174 }
16175 else
16176 centering_position = margin * frame_line_height + pt_offset;
16177 }
16178 else
16179 /* Set the window start half the height of the window backward
16180 from point. */
16181 centering_position = window_box_height (w) / 2;
16182 }
16183 move_it_vertically_backward (&it, centering_position);
16184
16185 eassert (IT_CHARPOS (it) >= BEGV);
16186
16187 /* The function move_it_vertically_backward may move over more
16188 than the specified y-distance. If it->w is small, e.g. a
16189 mini-buffer window, we may end up in front of the window's
16190 display area. Start displaying at the start of the line
16191 containing PT in this case. */
16192 if (it.current_y <= 0)
16193 {
16194 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16195 move_it_vertically_backward (&it, 0);
16196 it.current_y = 0;
16197 }
16198
16199 it.current_x = it.hpos = 0;
16200
16201 /* Set the window start position here explicitly, to avoid an
16202 infinite loop in case the functions in window-scroll-functions
16203 get errors. */
16204 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16205
16206 /* Run scroll hooks. */
16207 startp = run_window_scroll_functions (window, it.current.pos);
16208
16209 /* Redisplay the window. */
16210 if (!current_matrix_up_to_date_p
16211 || windows_or_buffers_changed
16212 || f->cursor_type_changed
16213 /* Don't use try_window_reusing_current_matrix in this case
16214 because it can have changed the buffer. */
16215 || !NILP (Vwindow_scroll_functions)
16216 || !just_this_one_p
16217 || MINI_WINDOW_P (w)
16218 || !(used_current_matrix_p
16219 = try_window_reusing_current_matrix (w)))
16220 try_window (window, startp, 0);
16221
16222 /* If new fonts have been loaded (due to fontsets), give up. We
16223 have to start a new redisplay since we need to re-adjust glyph
16224 matrices. */
16225 if (f->fonts_changed)
16226 goto need_larger_matrices;
16227
16228 /* If cursor did not appear assume that the middle of the window is
16229 in the first line of the window. Do it again with the next line.
16230 (Imagine a window of height 100, displaying two lines of height
16231 60. Moving back 50 from it->last_visible_y will end in the first
16232 line.) */
16233 if (w->cursor.vpos < 0)
16234 {
16235 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16236 {
16237 clear_glyph_matrix (w->desired_matrix);
16238 move_it_by_lines (&it, 1);
16239 try_window (window, it.current.pos, 0);
16240 }
16241 else if (PT < IT_CHARPOS (it))
16242 {
16243 clear_glyph_matrix (w->desired_matrix);
16244 move_it_by_lines (&it, -1);
16245 try_window (window, it.current.pos, 0);
16246 }
16247 else
16248 {
16249 /* Not much we can do about it. */
16250 }
16251 }
16252
16253 /* Consider the following case: Window starts at BEGV, there is
16254 invisible, intangible text at BEGV, so that display starts at
16255 some point START > BEGV. It can happen that we are called with
16256 PT somewhere between BEGV and START. Try to handle that case. */
16257 if (w->cursor.vpos < 0)
16258 {
16259 struct glyph_row *row = w->current_matrix->rows;
16260 if (row->mode_line_p)
16261 ++row;
16262 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16263 }
16264
16265 if (!cursor_row_fully_visible_p (w, 0, 0))
16266 {
16267 /* If vscroll is enabled, disable it and try again. */
16268 if (w->vscroll)
16269 {
16270 w->vscroll = 0;
16271 clear_glyph_matrix (w->desired_matrix);
16272 goto recenter;
16273 }
16274
16275 /* Users who set scroll-conservatively to a large number want
16276 point just above/below the scroll margin. If we ended up
16277 with point's row partially visible, move the window start to
16278 make that row fully visible and out of the margin. */
16279 if (scroll_conservatively > SCROLL_LIMIT)
16280 {
16281 int window_total_lines
16282 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16283 int margin =
16284 scroll_margin > 0
16285 ? min (scroll_margin, window_total_lines / 4)
16286 : 0;
16287 int move_down = w->cursor.vpos >= window_total_lines / 2;
16288
16289 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16290 clear_glyph_matrix (w->desired_matrix);
16291 if (1 == try_window (window, it.current.pos,
16292 TRY_WINDOW_CHECK_MARGINS))
16293 goto done;
16294 }
16295
16296 /* If centering point failed to make the whole line visible,
16297 put point at the top instead. That has to make the whole line
16298 visible, if it can be done. */
16299 if (centering_position == 0)
16300 goto done;
16301
16302 clear_glyph_matrix (w->desired_matrix);
16303 centering_position = 0;
16304 goto recenter;
16305 }
16306
16307 done:
16308
16309 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16310 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16311 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16312
16313 /* Display the mode line, if we must. */
16314 if ((update_mode_line
16315 /* If window not full width, must redo its mode line
16316 if (a) the window to its side is being redone and
16317 (b) we do a frame-based redisplay. This is a consequence
16318 of how inverted lines are drawn in frame-based redisplay. */
16319 || (!just_this_one_p
16320 && !FRAME_WINDOW_P (f)
16321 && !WINDOW_FULL_WIDTH_P (w))
16322 /* Line number to display. */
16323 || w->base_line_pos > 0
16324 /* Column number is displayed and different from the one displayed. */
16325 || (w->column_number_displayed != -1
16326 && (w->column_number_displayed != current_column ())))
16327 /* This means that the window has a mode line. */
16328 && (WINDOW_WANTS_MODELINE_P (w)
16329 || WINDOW_WANTS_HEADER_LINE_P (w)))
16330 {
16331
16332 display_mode_lines (w);
16333
16334 /* If mode line height has changed, arrange for a thorough
16335 immediate redisplay using the correct mode line height. */
16336 if (WINDOW_WANTS_MODELINE_P (w)
16337 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16338 {
16339 f->fonts_changed = 1;
16340 w->mode_line_height = -1;
16341 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16342 = DESIRED_MODE_LINE_HEIGHT (w);
16343 }
16344
16345 /* If header line height has changed, arrange for a thorough
16346 immediate redisplay using the correct header line height. */
16347 if (WINDOW_WANTS_HEADER_LINE_P (w)
16348 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16349 {
16350 f->fonts_changed = 1;
16351 w->header_line_height = -1;
16352 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16353 = DESIRED_HEADER_LINE_HEIGHT (w);
16354 }
16355
16356 if (f->fonts_changed)
16357 goto need_larger_matrices;
16358 }
16359
16360 if (!line_number_displayed && w->base_line_pos != -1)
16361 {
16362 w->base_line_pos = 0;
16363 w->base_line_number = 0;
16364 }
16365
16366 finish_menu_bars:
16367
16368 /* When we reach a frame's selected window, redo the frame's menu bar. */
16369 if (update_mode_line
16370 && EQ (FRAME_SELECTED_WINDOW (f), window))
16371 {
16372 int redisplay_menu_p = 0;
16373
16374 if (FRAME_WINDOW_P (f))
16375 {
16376 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16377 || defined (HAVE_NS) || defined (USE_GTK)
16378 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16379 #else
16380 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16381 #endif
16382 }
16383 else
16384 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16385
16386 if (redisplay_menu_p)
16387 display_menu_bar (w);
16388
16389 #ifdef HAVE_WINDOW_SYSTEM
16390 if (FRAME_WINDOW_P (f))
16391 {
16392 #if defined (USE_GTK) || defined (HAVE_NS)
16393 if (FRAME_EXTERNAL_TOOL_BAR (f))
16394 redisplay_tool_bar (f);
16395 #else
16396 if (WINDOWP (f->tool_bar_window)
16397 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16398 || !NILP (Vauto_resize_tool_bars))
16399 && redisplay_tool_bar (f))
16400 ignore_mouse_drag_p = 1;
16401 #endif
16402 }
16403 #endif
16404 }
16405
16406 #ifdef HAVE_WINDOW_SYSTEM
16407 if (FRAME_WINDOW_P (f)
16408 && update_window_fringes (w, (just_this_one_p
16409 || (!used_current_matrix_p && !overlay_arrow_seen)
16410 || w->pseudo_window_p)))
16411 {
16412 update_begin (f);
16413 block_input ();
16414 if (draw_window_fringes (w, 1))
16415 {
16416 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16417 x_draw_right_divider (w);
16418 else
16419 x_draw_vertical_border (w);
16420 }
16421 unblock_input ();
16422 update_end (f);
16423 }
16424
16425 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16426 x_draw_bottom_divider (w);
16427 #endif /* HAVE_WINDOW_SYSTEM */
16428
16429 /* We go to this label, with fonts_changed set, if it is
16430 necessary to try again using larger glyph matrices.
16431 We have to redeem the scroll bar even in this case,
16432 because the loop in redisplay_internal expects that. */
16433 need_larger_matrices:
16434 ;
16435 finish_scroll_bars:
16436
16437 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16438 {
16439 /* Set the thumb's position and size. */
16440 set_vertical_scroll_bar (w);
16441
16442 /* Note that we actually used the scroll bar attached to this
16443 window, so it shouldn't be deleted at the end of redisplay. */
16444 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16445 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16446 }
16447
16448 /* Restore current_buffer and value of point in it. The window
16449 update may have changed the buffer, so first make sure `opoint'
16450 is still valid (Bug#6177). */
16451 if (CHARPOS (opoint) < BEGV)
16452 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16453 else if (CHARPOS (opoint) > ZV)
16454 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16455 else
16456 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16457
16458 set_buffer_internal_1 (old);
16459 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16460 shorter. This can be caused by log truncation in *Messages*. */
16461 if (CHARPOS (lpoint) <= ZV)
16462 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16463
16464 unbind_to (count, Qnil);
16465 }
16466
16467
16468 /* Build the complete desired matrix of WINDOW with a window start
16469 buffer position POS.
16470
16471 Value is 1 if successful. It is zero if fonts were loaded during
16472 redisplay which makes re-adjusting glyph matrices necessary, and -1
16473 if point would appear in the scroll margins.
16474 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16475 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16476 set in FLAGS.) */
16477
16478 int
16479 try_window (Lisp_Object window, struct text_pos pos, int flags)
16480 {
16481 struct window *w = XWINDOW (window);
16482 struct it it;
16483 struct glyph_row *last_text_row = NULL;
16484 struct frame *f = XFRAME (w->frame);
16485 int frame_line_height = default_line_pixel_height (w);
16486
16487 /* Make POS the new window start. */
16488 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16489
16490 /* Mark cursor position as unknown. No overlay arrow seen. */
16491 w->cursor.vpos = -1;
16492 overlay_arrow_seen = 0;
16493
16494 /* Initialize iterator and info to start at POS. */
16495 start_display (&it, w, pos);
16496
16497 /* Display all lines of W. */
16498 while (it.current_y < it.last_visible_y)
16499 {
16500 if (display_line (&it))
16501 last_text_row = it.glyph_row - 1;
16502 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16503 return 0;
16504 }
16505
16506 /* Don't let the cursor end in the scroll margins. */
16507 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16508 && !MINI_WINDOW_P (w))
16509 {
16510 int this_scroll_margin;
16511 int window_total_lines
16512 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16513
16514 if (scroll_margin > 0)
16515 {
16516 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16517 this_scroll_margin *= frame_line_height;
16518 }
16519 else
16520 this_scroll_margin = 0;
16521
16522 if ((w->cursor.y >= 0 /* not vscrolled */
16523 && w->cursor.y < this_scroll_margin
16524 && CHARPOS (pos) > BEGV
16525 && IT_CHARPOS (it) < ZV)
16526 /* rms: considering make_cursor_line_fully_visible_p here
16527 seems to give wrong results. We don't want to recenter
16528 when the last line is partly visible, we want to allow
16529 that case to be handled in the usual way. */
16530 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16531 {
16532 w->cursor.vpos = -1;
16533 clear_glyph_matrix (w->desired_matrix);
16534 return -1;
16535 }
16536 }
16537
16538 /* If bottom moved off end of frame, change mode line percentage. */
16539 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16540 w->update_mode_line = 1;
16541
16542 /* Set window_end_pos to the offset of the last character displayed
16543 on the window from the end of current_buffer. Set
16544 window_end_vpos to its row number. */
16545 if (last_text_row)
16546 {
16547 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16548 adjust_window_ends (w, last_text_row, 0);
16549 eassert
16550 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16551 w->window_end_vpos)));
16552 }
16553 else
16554 {
16555 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16556 w->window_end_pos = Z - ZV;
16557 w->window_end_vpos = 0;
16558 }
16559
16560 /* But that is not valid info until redisplay finishes. */
16561 w->window_end_valid = 0;
16562 return 1;
16563 }
16564
16565
16566 \f
16567 /************************************************************************
16568 Window redisplay reusing current matrix when buffer has not changed
16569 ************************************************************************/
16570
16571 /* Try redisplay of window W showing an unchanged buffer with a
16572 different window start than the last time it was displayed by
16573 reusing its current matrix. Value is non-zero if successful.
16574 W->start is the new window start. */
16575
16576 static int
16577 try_window_reusing_current_matrix (struct window *w)
16578 {
16579 struct frame *f = XFRAME (w->frame);
16580 struct glyph_row *bottom_row;
16581 struct it it;
16582 struct run run;
16583 struct text_pos start, new_start;
16584 int nrows_scrolled, i;
16585 struct glyph_row *last_text_row;
16586 struct glyph_row *last_reused_text_row;
16587 struct glyph_row *start_row;
16588 int start_vpos, min_y, max_y;
16589
16590 #ifdef GLYPH_DEBUG
16591 if (inhibit_try_window_reusing)
16592 return 0;
16593 #endif
16594
16595 if (/* This function doesn't handle terminal frames. */
16596 !FRAME_WINDOW_P (f)
16597 /* Don't try to reuse the display if windows have been split
16598 or such. */
16599 || windows_or_buffers_changed
16600 || f->cursor_type_changed)
16601 return 0;
16602
16603 /* Can't do this if showing trailing whitespace. */
16604 if (!NILP (Vshow_trailing_whitespace))
16605 return 0;
16606
16607 /* If top-line visibility has changed, give up. */
16608 if (WINDOW_WANTS_HEADER_LINE_P (w)
16609 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16610 return 0;
16611
16612 /* Give up if old or new display is scrolled vertically. We could
16613 make this function handle this, but right now it doesn't. */
16614 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16615 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16616 return 0;
16617
16618 /* The variable new_start now holds the new window start. The old
16619 start `start' can be determined from the current matrix. */
16620 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16621 start = start_row->minpos;
16622 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16623
16624 /* Clear the desired matrix for the display below. */
16625 clear_glyph_matrix (w->desired_matrix);
16626
16627 if (CHARPOS (new_start) <= CHARPOS (start))
16628 {
16629 /* Don't use this method if the display starts with an ellipsis
16630 displayed for invisible text. It's not easy to handle that case
16631 below, and it's certainly not worth the effort since this is
16632 not a frequent case. */
16633 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16634 return 0;
16635
16636 IF_DEBUG (debug_method_add (w, "twu1"));
16637
16638 /* Display up to a row that can be reused. The variable
16639 last_text_row is set to the last row displayed that displays
16640 text. Note that it.vpos == 0 if or if not there is a
16641 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16642 start_display (&it, w, new_start);
16643 w->cursor.vpos = -1;
16644 last_text_row = last_reused_text_row = NULL;
16645
16646 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16647 {
16648 /* If we have reached into the characters in the START row,
16649 that means the line boundaries have changed. So we
16650 can't start copying with the row START. Maybe it will
16651 work to start copying with the following row. */
16652 while (IT_CHARPOS (it) > CHARPOS (start))
16653 {
16654 /* Advance to the next row as the "start". */
16655 start_row++;
16656 start = start_row->minpos;
16657 /* If there are no more rows to try, or just one, give up. */
16658 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16659 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16660 || CHARPOS (start) == ZV)
16661 {
16662 clear_glyph_matrix (w->desired_matrix);
16663 return 0;
16664 }
16665
16666 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16667 }
16668 /* If we have reached alignment, we can copy the rest of the
16669 rows. */
16670 if (IT_CHARPOS (it) == CHARPOS (start)
16671 /* Don't accept "alignment" inside a display vector,
16672 since start_row could have started in the middle of
16673 that same display vector (thus their character
16674 positions match), and we have no way of telling if
16675 that is the case. */
16676 && it.current.dpvec_index < 0)
16677 break;
16678
16679 if (display_line (&it))
16680 last_text_row = it.glyph_row - 1;
16681
16682 }
16683
16684 /* A value of current_y < last_visible_y means that we stopped
16685 at the previous window start, which in turn means that we
16686 have at least one reusable row. */
16687 if (it.current_y < it.last_visible_y)
16688 {
16689 struct glyph_row *row;
16690
16691 /* IT.vpos always starts from 0; it counts text lines. */
16692 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16693
16694 /* Find PT if not already found in the lines displayed. */
16695 if (w->cursor.vpos < 0)
16696 {
16697 int dy = it.current_y - start_row->y;
16698
16699 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16700 row = row_containing_pos (w, PT, row, NULL, dy);
16701 if (row)
16702 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16703 dy, nrows_scrolled);
16704 else
16705 {
16706 clear_glyph_matrix (w->desired_matrix);
16707 return 0;
16708 }
16709 }
16710
16711 /* Scroll the display. Do it before the current matrix is
16712 changed. The problem here is that update has not yet
16713 run, i.e. part of the current matrix is not up to date.
16714 scroll_run_hook will clear the cursor, and use the
16715 current matrix to get the height of the row the cursor is
16716 in. */
16717 run.current_y = start_row->y;
16718 run.desired_y = it.current_y;
16719 run.height = it.last_visible_y - it.current_y;
16720
16721 if (run.height > 0 && run.current_y != run.desired_y)
16722 {
16723 update_begin (f);
16724 FRAME_RIF (f)->update_window_begin_hook (w);
16725 FRAME_RIF (f)->clear_window_mouse_face (w);
16726 FRAME_RIF (f)->scroll_run_hook (w, &run);
16727 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16728 update_end (f);
16729 }
16730
16731 /* Shift current matrix down by nrows_scrolled lines. */
16732 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16733 rotate_matrix (w->current_matrix,
16734 start_vpos,
16735 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16736 nrows_scrolled);
16737
16738 /* Disable lines that must be updated. */
16739 for (i = 0; i < nrows_scrolled; ++i)
16740 (start_row + i)->enabled_p = 0;
16741
16742 /* Re-compute Y positions. */
16743 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16744 max_y = it.last_visible_y;
16745 for (row = start_row + nrows_scrolled;
16746 row < bottom_row;
16747 ++row)
16748 {
16749 row->y = it.current_y;
16750 row->visible_height = row->height;
16751
16752 if (row->y < min_y)
16753 row->visible_height -= min_y - row->y;
16754 if (row->y + row->height > max_y)
16755 row->visible_height -= row->y + row->height - max_y;
16756 if (row->fringe_bitmap_periodic_p)
16757 row->redraw_fringe_bitmaps_p = 1;
16758
16759 it.current_y += row->height;
16760
16761 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16762 last_reused_text_row = row;
16763 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16764 break;
16765 }
16766
16767 /* Disable lines in the current matrix which are now
16768 below the window. */
16769 for (++row; row < bottom_row; ++row)
16770 row->enabled_p = row->mode_line_p = 0;
16771 }
16772
16773 /* Update window_end_pos etc.; last_reused_text_row is the last
16774 reused row from the current matrix containing text, if any.
16775 The value of last_text_row is the last displayed line
16776 containing text. */
16777 if (last_reused_text_row)
16778 adjust_window_ends (w, last_reused_text_row, 1);
16779 else if (last_text_row)
16780 adjust_window_ends (w, last_text_row, 0);
16781 else
16782 {
16783 /* This window must be completely empty. */
16784 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16785 w->window_end_pos = Z - ZV;
16786 w->window_end_vpos = 0;
16787 }
16788 w->window_end_valid = 0;
16789
16790 /* Update hint: don't try scrolling again in update_window. */
16791 w->desired_matrix->no_scrolling_p = 1;
16792
16793 #ifdef GLYPH_DEBUG
16794 debug_method_add (w, "try_window_reusing_current_matrix 1");
16795 #endif
16796 return 1;
16797 }
16798 else if (CHARPOS (new_start) > CHARPOS (start))
16799 {
16800 struct glyph_row *pt_row, *row;
16801 struct glyph_row *first_reusable_row;
16802 struct glyph_row *first_row_to_display;
16803 int dy;
16804 int yb = window_text_bottom_y (w);
16805
16806 /* Find the row starting at new_start, if there is one. Don't
16807 reuse a partially visible line at the end. */
16808 first_reusable_row = start_row;
16809 while (first_reusable_row->enabled_p
16810 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16811 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16812 < CHARPOS (new_start)))
16813 ++first_reusable_row;
16814
16815 /* Give up if there is no row to reuse. */
16816 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16817 || !first_reusable_row->enabled_p
16818 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16819 != CHARPOS (new_start)))
16820 return 0;
16821
16822 /* We can reuse fully visible rows beginning with
16823 first_reusable_row to the end of the window. Set
16824 first_row_to_display to the first row that cannot be reused.
16825 Set pt_row to the row containing point, if there is any. */
16826 pt_row = NULL;
16827 for (first_row_to_display = first_reusable_row;
16828 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16829 ++first_row_to_display)
16830 {
16831 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16832 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16833 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16834 && first_row_to_display->ends_at_zv_p
16835 && pt_row == NULL)))
16836 pt_row = first_row_to_display;
16837 }
16838
16839 /* Start displaying at the start of first_row_to_display. */
16840 eassert (first_row_to_display->y < yb);
16841 init_to_row_start (&it, w, first_row_to_display);
16842
16843 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16844 - start_vpos);
16845 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16846 - nrows_scrolled);
16847 it.current_y = (first_row_to_display->y - first_reusable_row->y
16848 + WINDOW_HEADER_LINE_HEIGHT (w));
16849
16850 /* Display lines beginning with first_row_to_display in the
16851 desired matrix. Set last_text_row to the last row displayed
16852 that displays text. */
16853 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16854 if (pt_row == NULL)
16855 w->cursor.vpos = -1;
16856 last_text_row = NULL;
16857 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16858 if (display_line (&it))
16859 last_text_row = it.glyph_row - 1;
16860
16861 /* If point is in a reused row, adjust y and vpos of the cursor
16862 position. */
16863 if (pt_row)
16864 {
16865 w->cursor.vpos -= nrows_scrolled;
16866 w->cursor.y -= first_reusable_row->y - start_row->y;
16867 }
16868
16869 /* Give up if point isn't in a row displayed or reused. (This
16870 also handles the case where w->cursor.vpos < nrows_scrolled
16871 after the calls to display_line, which can happen with scroll
16872 margins. See bug#1295.) */
16873 if (w->cursor.vpos < 0)
16874 {
16875 clear_glyph_matrix (w->desired_matrix);
16876 return 0;
16877 }
16878
16879 /* Scroll the display. */
16880 run.current_y = first_reusable_row->y;
16881 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16882 run.height = it.last_visible_y - run.current_y;
16883 dy = run.current_y - run.desired_y;
16884
16885 if (run.height)
16886 {
16887 update_begin (f);
16888 FRAME_RIF (f)->update_window_begin_hook (w);
16889 FRAME_RIF (f)->clear_window_mouse_face (w);
16890 FRAME_RIF (f)->scroll_run_hook (w, &run);
16891 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16892 update_end (f);
16893 }
16894
16895 /* Adjust Y positions of reused rows. */
16896 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16897 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16898 max_y = it.last_visible_y;
16899 for (row = first_reusable_row; row < first_row_to_display; ++row)
16900 {
16901 row->y -= dy;
16902 row->visible_height = row->height;
16903 if (row->y < min_y)
16904 row->visible_height -= min_y - row->y;
16905 if (row->y + row->height > max_y)
16906 row->visible_height -= row->y + row->height - max_y;
16907 if (row->fringe_bitmap_periodic_p)
16908 row->redraw_fringe_bitmaps_p = 1;
16909 }
16910
16911 /* Scroll the current matrix. */
16912 eassert (nrows_scrolled > 0);
16913 rotate_matrix (w->current_matrix,
16914 start_vpos,
16915 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16916 -nrows_scrolled);
16917
16918 /* Disable rows not reused. */
16919 for (row -= nrows_scrolled; row < bottom_row; ++row)
16920 row->enabled_p = 0;
16921
16922 /* Point may have moved to a different line, so we cannot assume that
16923 the previous cursor position is valid; locate the correct row. */
16924 if (pt_row)
16925 {
16926 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16927 row < bottom_row
16928 && PT >= MATRIX_ROW_END_CHARPOS (row)
16929 && !row->ends_at_zv_p;
16930 row++)
16931 {
16932 w->cursor.vpos++;
16933 w->cursor.y = row->y;
16934 }
16935 if (row < bottom_row)
16936 {
16937 /* Can't simply scan the row for point with
16938 bidi-reordered glyph rows. Let set_cursor_from_row
16939 figure out where to put the cursor, and if it fails,
16940 give up. */
16941 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16942 {
16943 if (!set_cursor_from_row (w, row, w->current_matrix,
16944 0, 0, 0, 0))
16945 {
16946 clear_glyph_matrix (w->desired_matrix);
16947 return 0;
16948 }
16949 }
16950 else
16951 {
16952 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16953 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16954
16955 for (; glyph < end
16956 && (!BUFFERP (glyph->object)
16957 || glyph->charpos < PT);
16958 glyph++)
16959 {
16960 w->cursor.hpos++;
16961 w->cursor.x += glyph->pixel_width;
16962 }
16963 }
16964 }
16965 }
16966
16967 /* Adjust window end. A null value of last_text_row means that
16968 the window end is in reused rows which in turn means that
16969 only its vpos can have changed. */
16970 if (last_text_row)
16971 adjust_window_ends (w, last_text_row, 0);
16972 else
16973 w->window_end_vpos -= nrows_scrolled;
16974
16975 w->window_end_valid = 0;
16976 w->desired_matrix->no_scrolling_p = 1;
16977
16978 #ifdef GLYPH_DEBUG
16979 debug_method_add (w, "try_window_reusing_current_matrix 2");
16980 #endif
16981 return 1;
16982 }
16983
16984 return 0;
16985 }
16986
16987
16988 \f
16989 /************************************************************************
16990 Window redisplay reusing current matrix when buffer has changed
16991 ************************************************************************/
16992
16993 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16994 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16995 ptrdiff_t *, ptrdiff_t *);
16996 static struct glyph_row *
16997 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16998 struct glyph_row *);
16999
17000
17001 /* Return the last row in MATRIX displaying text. If row START is
17002 non-null, start searching with that row. IT gives the dimensions
17003 of the display. Value is null if matrix is empty; otherwise it is
17004 a pointer to the row found. */
17005
17006 static struct glyph_row *
17007 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17008 struct glyph_row *start)
17009 {
17010 struct glyph_row *row, *row_found;
17011
17012 /* Set row_found to the last row in IT->w's current matrix
17013 displaying text. The loop looks funny but think of partially
17014 visible lines. */
17015 row_found = NULL;
17016 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17017 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17018 {
17019 eassert (row->enabled_p);
17020 row_found = row;
17021 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17022 break;
17023 ++row;
17024 }
17025
17026 return row_found;
17027 }
17028
17029
17030 /* Return the last row in the current matrix of W that is not affected
17031 by changes at the start of current_buffer that occurred since W's
17032 current matrix was built. Value is null if no such row exists.
17033
17034 BEG_UNCHANGED us the number of characters unchanged at the start of
17035 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17036 first changed character in current_buffer. Characters at positions <
17037 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17038 when the current matrix was built. */
17039
17040 static struct glyph_row *
17041 find_last_unchanged_at_beg_row (struct window *w)
17042 {
17043 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17044 struct glyph_row *row;
17045 struct glyph_row *row_found = NULL;
17046 int yb = window_text_bottom_y (w);
17047
17048 /* Find the last row displaying unchanged text. */
17049 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17050 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17051 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17052 ++row)
17053 {
17054 if (/* If row ends before first_changed_pos, it is unchanged,
17055 except in some case. */
17056 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17057 /* When row ends in ZV and we write at ZV it is not
17058 unchanged. */
17059 && !row->ends_at_zv_p
17060 /* When first_changed_pos is the end of a continued line,
17061 row is not unchanged because it may be no longer
17062 continued. */
17063 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17064 && (row->continued_p
17065 || row->exact_window_width_line_p))
17066 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17067 needs to be recomputed, so don't consider this row as
17068 unchanged. This happens when the last line was
17069 bidi-reordered and was killed immediately before this
17070 redisplay cycle. In that case, ROW->end stores the
17071 buffer position of the first visual-order character of
17072 the killed text, which is now beyond ZV. */
17073 && CHARPOS (row->end.pos) <= ZV)
17074 row_found = row;
17075
17076 /* Stop if last visible row. */
17077 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17078 break;
17079 }
17080
17081 return row_found;
17082 }
17083
17084
17085 /* Find the first glyph row in the current matrix of W that is not
17086 affected by changes at the end of current_buffer since the
17087 time W's current matrix was built.
17088
17089 Return in *DELTA the number of chars by which buffer positions in
17090 unchanged text at the end of current_buffer must be adjusted.
17091
17092 Return in *DELTA_BYTES the corresponding number of bytes.
17093
17094 Value is null if no such row exists, i.e. all rows are affected by
17095 changes. */
17096
17097 static struct glyph_row *
17098 find_first_unchanged_at_end_row (struct window *w,
17099 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17100 {
17101 struct glyph_row *row;
17102 struct glyph_row *row_found = NULL;
17103
17104 *delta = *delta_bytes = 0;
17105
17106 /* Display must not have been paused, otherwise the current matrix
17107 is not up to date. */
17108 eassert (w->window_end_valid);
17109
17110 /* A value of window_end_pos >= END_UNCHANGED means that the window
17111 end is in the range of changed text. If so, there is no
17112 unchanged row at the end of W's current matrix. */
17113 if (w->window_end_pos >= END_UNCHANGED)
17114 return NULL;
17115
17116 /* Set row to the last row in W's current matrix displaying text. */
17117 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17118
17119 /* If matrix is entirely empty, no unchanged row exists. */
17120 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17121 {
17122 /* The value of row is the last glyph row in the matrix having a
17123 meaningful buffer position in it. The end position of row
17124 corresponds to window_end_pos. This allows us to translate
17125 buffer positions in the current matrix to current buffer
17126 positions for characters not in changed text. */
17127 ptrdiff_t Z_old =
17128 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17129 ptrdiff_t Z_BYTE_old =
17130 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17131 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17132 struct glyph_row *first_text_row
17133 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17134
17135 *delta = Z - Z_old;
17136 *delta_bytes = Z_BYTE - Z_BYTE_old;
17137
17138 /* Set last_unchanged_pos to the buffer position of the last
17139 character in the buffer that has not been changed. Z is the
17140 index + 1 of the last character in current_buffer, i.e. by
17141 subtracting END_UNCHANGED we get the index of the last
17142 unchanged character, and we have to add BEG to get its buffer
17143 position. */
17144 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17145 last_unchanged_pos_old = last_unchanged_pos - *delta;
17146
17147 /* Search backward from ROW for a row displaying a line that
17148 starts at a minimum position >= last_unchanged_pos_old. */
17149 for (; row > first_text_row; --row)
17150 {
17151 /* This used to abort, but it can happen.
17152 It is ok to just stop the search instead here. KFS. */
17153 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17154 break;
17155
17156 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17157 row_found = row;
17158 }
17159 }
17160
17161 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17162
17163 return row_found;
17164 }
17165
17166
17167 /* Make sure that glyph rows in the current matrix of window W
17168 reference the same glyph memory as corresponding rows in the
17169 frame's frame matrix. This function is called after scrolling W's
17170 current matrix on a terminal frame in try_window_id and
17171 try_window_reusing_current_matrix. */
17172
17173 static void
17174 sync_frame_with_window_matrix_rows (struct window *w)
17175 {
17176 struct frame *f = XFRAME (w->frame);
17177 struct glyph_row *window_row, *window_row_end, *frame_row;
17178
17179 /* Preconditions: W must be a leaf window and full-width. Its frame
17180 must have a frame matrix. */
17181 eassert (BUFFERP (w->contents));
17182 eassert (WINDOW_FULL_WIDTH_P (w));
17183 eassert (!FRAME_WINDOW_P (f));
17184
17185 /* If W is a full-width window, glyph pointers in W's current matrix
17186 have, by definition, to be the same as glyph pointers in the
17187 corresponding frame matrix. Note that frame matrices have no
17188 marginal areas (see build_frame_matrix). */
17189 window_row = w->current_matrix->rows;
17190 window_row_end = window_row + w->current_matrix->nrows;
17191 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17192 while (window_row < window_row_end)
17193 {
17194 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17195 struct glyph *end = window_row->glyphs[LAST_AREA];
17196
17197 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17198 frame_row->glyphs[TEXT_AREA] = start;
17199 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17200 frame_row->glyphs[LAST_AREA] = end;
17201
17202 /* Disable frame rows whose corresponding window rows have
17203 been disabled in try_window_id. */
17204 if (!window_row->enabled_p)
17205 frame_row->enabled_p = 0;
17206
17207 ++window_row, ++frame_row;
17208 }
17209 }
17210
17211
17212 /* Find the glyph row in window W containing CHARPOS. Consider all
17213 rows between START and END (not inclusive). END null means search
17214 all rows to the end of the display area of W. Value is the row
17215 containing CHARPOS or null. */
17216
17217 struct glyph_row *
17218 row_containing_pos (struct window *w, ptrdiff_t charpos,
17219 struct glyph_row *start, struct glyph_row *end, int dy)
17220 {
17221 struct glyph_row *row = start;
17222 struct glyph_row *best_row = NULL;
17223 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17224 int last_y;
17225
17226 /* If we happen to start on a header-line, skip that. */
17227 if (row->mode_line_p)
17228 ++row;
17229
17230 if ((end && row >= end) || !row->enabled_p)
17231 return NULL;
17232
17233 last_y = window_text_bottom_y (w) - dy;
17234
17235 while (1)
17236 {
17237 /* Give up if we have gone too far. */
17238 if (end && row >= end)
17239 return NULL;
17240 /* This formerly returned if they were equal.
17241 I think that both quantities are of a "last plus one" type;
17242 if so, when they are equal, the row is within the screen. -- rms. */
17243 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17244 return NULL;
17245
17246 /* If it is in this row, return this row. */
17247 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17248 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17249 /* The end position of a row equals the start
17250 position of the next row. If CHARPOS is there, we
17251 would rather consider it displayed in the next
17252 line, except when this line ends in ZV. */
17253 && !row_for_charpos_p (row, charpos)))
17254 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17255 {
17256 struct glyph *g;
17257
17258 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17259 || (!best_row && !row->continued_p))
17260 return row;
17261 /* In bidi-reordered rows, there could be several rows whose
17262 edges surround CHARPOS, all of these rows belonging to
17263 the same continued line. We need to find the row which
17264 fits CHARPOS the best. */
17265 for (g = row->glyphs[TEXT_AREA];
17266 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17267 g++)
17268 {
17269 if (!STRINGP (g->object))
17270 {
17271 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17272 {
17273 mindif = eabs (g->charpos - charpos);
17274 best_row = row;
17275 /* Exact match always wins. */
17276 if (mindif == 0)
17277 return best_row;
17278 }
17279 }
17280 }
17281 }
17282 else if (best_row && !row->continued_p)
17283 return best_row;
17284 ++row;
17285 }
17286 }
17287
17288
17289 /* Try to redisplay window W by reusing its existing display. W's
17290 current matrix must be up to date when this function is called,
17291 i.e. window_end_valid must be nonzero.
17292
17293 Value is
17294
17295 1 if display has been updated
17296 0 if otherwise unsuccessful
17297 -1 if redisplay with same window start is known not to succeed
17298
17299 The following steps are performed:
17300
17301 1. Find the last row in the current matrix of W that is not
17302 affected by changes at the start of current_buffer. If no such row
17303 is found, give up.
17304
17305 2. Find the first row in W's current matrix that is not affected by
17306 changes at the end of current_buffer. Maybe there is no such row.
17307
17308 3. Display lines beginning with the row + 1 found in step 1 to the
17309 row found in step 2 or, if step 2 didn't find a row, to the end of
17310 the window.
17311
17312 4. If cursor is not known to appear on the window, give up.
17313
17314 5. If display stopped at the row found in step 2, scroll the
17315 display and current matrix as needed.
17316
17317 6. Maybe display some lines at the end of W, if we must. This can
17318 happen under various circumstances, like a partially visible line
17319 becoming fully visible, or because newly displayed lines are displayed
17320 in smaller font sizes.
17321
17322 7. Update W's window end information. */
17323
17324 static int
17325 try_window_id (struct window *w)
17326 {
17327 struct frame *f = XFRAME (w->frame);
17328 struct glyph_matrix *current_matrix = w->current_matrix;
17329 struct glyph_matrix *desired_matrix = w->desired_matrix;
17330 struct glyph_row *last_unchanged_at_beg_row;
17331 struct glyph_row *first_unchanged_at_end_row;
17332 struct glyph_row *row;
17333 struct glyph_row *bottom_row;
17334 int bottom_vpos;
17335 struct it it;
17336 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17337 int dvpos, dy;
17338 struct text_pos start_pos;
17339 struct run run;
17340 int first_unchanged_at_end_vpos = 0;
17341 struct glyph_row *last_text_row, *last_text_row_at_end;
17342 struct text_pos start;
17343 ptrdiff_t first_changed_charpos, last_changed_charpos;
17344
17345 #ifdef GLYPH_DEBUG
17346 if (inhibit_try_window_id)
17347 return 0;
17348 #endif
17349
17350 /* This is handy for debugging. */
17351 #if 0
17352 #define GIVE_UP(X) \
17353 do { \
17354 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17355 return 0; \
17356 } while (0)
17357 #else
17358 #define GIVE_UP(X) return 0
17359 #endif
17360
17361 SET_TEXT_POS_FROM_MARKER (start, w->start);
17362
17363 /* Don't use this for mini-windows because these can show
17364 messages and mini-buffers, and we don't handle that here. */
17365 if (MINI_WINDOW_P (w))
17366 GIVE_UP (1);
17367
17368 /* This flag is used to prevent redisplay optimizations. */
17369 if (windows_or_buffers_changed || f->cursor_type_changed)
17370 GIVE_UP (2);
17371
17372 /* Verify that narrowing has not changed.
17373 Also verify that we were not told to prevent redisplay optimizations.
17374 It would be nice to further
17375 reduce the number of cases where this prevents try_window_id. */
17376 if (current_buffer->clip_changed
17377 || current_buffer->prevent_redisplay_optimizations_p)
17378 GIVE_UP (3);
17379
17380 /* Window must either use window-based redisplay or be full width. */
17381 if (!FRAME_WINDOW_P (f)
17382 && (!FRAME_LINE_INS_DEL_OK (f)
17383 || !WINDOW_FULL_WIDTH_P (w)))
17384 GIVE_UP (4);
17385
17386 /* Give up if point is known NOT to appear in W. */
17387 if (PT < CHARPOS (start))
17388 GIVE_UP (5);
17389
17390 /* Another way to prevent redisplay optimizations. */
17391 if (w->last_modified == 0)
17392 GIVE_UP (6);
17393
17394 /* Verify that window is not hscrolled. */
17395 if (w->hscroll != 0)
17396 GIVE_UP (7);
17397
17398 /* Verify that display wasn't paused. */
17399 if (!w->window_end_valid)
17400 GIVE_UP (8);
17401
17402 /* Likewise if highlighting trailing whitespace. */
17403 if (!NILP (Vshow_trailing_whitespace))
17404 GIVE_UP (11);
17405
17406 /* Can't use this if overlay arrow position and/or string have
17407 changed. */
17408 if (overlay_arrows_changed_p ())
17409 GIVE_UP (12);
17410
17411 /* When word-wrap is on, adding a space to the first word of a
17412 wrapped line can change the wrap position, altering the line
17413 above it. It might be worthwhile to handle this more
17414 intelligently, but for now just redisplay from scratch. */
17415 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17416 GIVE_UP (21);
17417
17418 /* Under bidi reordering, adding or deleting a character in the
17419 beginning of a paragraph, before the first strong directional
17420 character, can change the base direction of the paragraph (unless
17421 the buffer specifies a fixed paragraph direction), which will
17422 require to redisplay the whole paragraph. It might be worthwhile
17423 to find the paragraph limits and widen the range of redisplayed
17424 lines to that, but for now just give up this optimization and
17425 redisplay from scratch. */
17426 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17427 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17428 GIVE_UP (22);
17429
17430 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17431 only if buffer has really changed. The reason is that the gap is
17432 initially at Z for freshly visited files. The code below would
17433 set end_unchanged to 0 in that case. */
17434 if (MODIFF > SAVE_MODIFF
17435 /* This seems to happen sometimes after saving a buffer. */
17436 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17437 {
17438 if (GPT - BEG < BEG_UNCHANGED)
17439 BEG_UNCHANGED = GPT - BEG;
17440 if (Z - GPT < END_UNCHANGED)
17441 END_UNCHANGED = Z - GPT;
17442 }
17443
17444 /* The position of the first and last character that has been changed. */
17445 first_changed_charpos = BEG + BEG_UNCHANGED;
17446 last_changed_charpos = Z - END_UNCHANGED;
17447
17448 /* If window starts after a line end, and the last change is in
17449 front of that newline, then changes don't affect the display.
17450 This case happens with stealth-fontification. Note that although
17451 the display is unchanged, glyph positions in the matrix have to
17452 be adjusted, of course. */
17453 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17454 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17455 && ((last_changed_charpos < CHARPOS (start)
17456 && CHARPOS (start) == BEGV)
17457 || (last_changed_charpos < CHARPOS (start) - 1
17458 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17459 {
17460 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17461 struct glyph_row *r0;
17462
17463 /* Compute how many chars/bytes have been added to or removed
17464 from the buffer. */
17465 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17466 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17467 Z_delta = Z - Z_old;
17468 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17469
17470 /* Give up if PT is not in the window. Note that it already has
17471 been checked at the start of try_window_id that PT is not in
17472 front of the window start. */
17473 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17474 GIVE_UP (13);
17475
17476 /* If window start is unchanged, we can reuse the whole matrix
17477 as is, after adjusting glyph positions. No need to compute
17478 the window end again, since its offset from Z hasn't changed. */
17479 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17480 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17481 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17482 /* PT must not be in a partially visible line. */
17483 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17484 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17485 {
17486 /* Adjust positions in the glyph matrix. */
17487 if (Z_delta || Z_delta_bytes)
17488 {
17489 struct glyph_row *r1
17490 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17491 increment_matrix_positions (w->current_matrix,
17492 MATRIX_ROW_VPOS (r0, current_matrix),
17493 MATRIX_ROW_VPOS (r1, current_matrix),
17494 Z_delta, Z_delta_bytes);
17495 }
17496
17497 /* Set the cursor. */
17498 row = row_containing_pos (w, PT, r0, NULL, 0);
17499 if (row)
17500 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17501 return 1;
17502 }
17503 }
17504
17505 /* Handle the case that changes are all below what is displayed in
17506 the window, and that PT is in the window. This shortcut cannot
17507 be taken if ZV is visible in the window, and text has been added
17508 there that is visible in the window. */
17509 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17510 /* ZV is not visible in the window, or there are no
17511 changes at ZV, actually. */
17512 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17513 || first_changed_charpos == last_changed_charpos))
17514 {
17515 struct glyph_row *r0;
17516
17517 /* Give up if PT is not in the window. Note that it already has
17518 been checked at the start of try_window_id that PT is not in
17519 front of the window start. */
17520 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17521 GIVE_UP (14);
17522
17523 /* If window start is unchanged, we can reuse the whole matrix
17524 as is, without changing glyph positions since no text has
17525 been added/removed in front of the window end. */
17526 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17527 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17528 /* PT must not be in a partially visible line. */
17529 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17530 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17531 {
17532 /* We have to compute the window end anew since text
17533 could have been added/removed after it. */
17534 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17535 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17536
17537 /* Set the cursor. */
17538 row = row_containing_pos (w, PT, r0, NULL, 0);
17539 if (row)
17540 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17541 return 2;
17542 }
17543 }
17544
17545 /* Give up if window start is in the changed area.
17546
17547 The condition used to read
17548
17549 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17550
17551 but why that was tested escapes me at the moment. */
17552 if (CHARPOS (start) >= first_changed_charpos
17553 && CHARPOS (start) <= last_changed_charpos)
17554 GIVE_UP (15);
17555
17556 /* Check that window start agrees with the start of the first glyph
17557 row in its current matrix. Check this after we know the window
17558 start is not in changed text, otherwise positions would not be
17559 comparable. */
17560 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17561 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17562 GIVE_UP (16);
17563
17564 /* Give up if the window ends in strings. Overlay strings
17565 at the end are difficult to handle, so don't try. */
17566 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17567 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17568 GIVE_UP (20);
17569
17570 /* Compute the position at which we have to start displaying new
17571 lines. Some of the lines at the top of the window might be
17572 reusable because they are not displaying changed text. Find the
17573 last row in W's current matrix not affected by changes at the
17574 start of current_buffer. Value is null if changes start in the
17575 first line of window. */
17576 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17577 if (last_unchanged_at_beg_row)
17578 {
17579 /* Avoid starting to display in the middle of a character, a TAB
17580 for instance. This is easier than to set up the iterator
17581 exactly, and it's not a frequent case, so the additional
17582 effort wouldn't really pay off. */
17583 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17584 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17585 && last_unchanged_at_beg_row > w->current_matrix->rows)
17586 --last_unchanged_at_beg_row;
17587
17588 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17589 GIVE_UP (17);
17590
17591 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17592 GIVE_UP (18);
17593 start_pos = it.current.pos;
17594
17595 /* Start displaying new lines in the desired matrix at the same
17596 vpos we would use in the current matrix, i.e. below
17597 last_unchanged_at_beg_row. */
17598 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17599 current_matrix);
17600 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17601 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17602
17603 eassert (it.hpos == 0 && it.current_x == 0);
17604 }
17605 else
17606 {
17607 /* There are no reusable lines at the start of the window.
17608 Start displaying in the first text line. */
17609 start_display (&it, w, start);
17610 it.vpos = it.first_vpos;
17611 start_pos = it.current.pos;
17612 }
17613
17614 /* Find the first row that is not affected by changes at the end of
17615 the buffer. Value will be null if there is no unchanged row, in
17616 which case we must redisplay to the end of the window. delta
17617 will be set to the value by which buffer positions beginning with
17618 first_unchanged_at_end_row have to be adjusted due to text
17619 changes. */
17620 first_unchanged_at_end_row
17621 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17622 IF_DEBUG (debug_delta = delta);
17623 IF_DEBUG (debug_delta_bytes = delta_bytes);
17624
17625 /* Set stop_pos to the buffer position up to which we will have to
17626 display new lines. If first_unchanged_at_end_row != NULL, this
17627 is the buffer position of the start of the line displayed in that
17628 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17629 that we don't stop at a buffer position. */
17630 stop_pos = 0;
17631 if (first_unchanged_at_end_row)
17632 {
17633 eassert (last_unchanged_at_beg_row == NULL
17634 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17635
17636 /* If this is a continuation line, move forward to the next one
17637 that isn't. Changes in lines above affect this line.
17638 Caution: this may move first_unchanged_at_end_row to a row
17639 not displaying text. */
17640 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17641 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17642 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17643 < it.last_visible_y))
17644 ++first_unchanged_at_end_row;
17645
17646 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17647 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17648 >= it.last_visible_y))
17649 first_unchanged_at_end_row = NULL;
17650 else
17651 {
17652 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17653 + delta);
17654 first_unchanged_at_end_vpos
17655 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17656 eassert (stop_pos >= Z - END_UNCHANGED);
17657 }
17658 }
17659 else if (last_unchanged_at_beg_row == NULL)
17660 GIVE_UP (19);
17661
17662
17663 #ifdef GLYPH_DEBUG
17664
17665 /* Either there is no unchanged row at the end, or the one we have
17666 now displays text. This is a necessary condition for the window
17667 end pos calculation at the end of this function. */
17668 eassert (first_unchanged_at_end_row == NULL
17669 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17670
17671 debug_last_unchanged_at_beg_vpos
17672 = (last_unchanged_at_beg_row
17673 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17674 : -1);
17675 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17676
17677 #endif /* GLYPH_DEBUG */
17678
17679
17680 /* Display new lines. Set last_text_row to the last new line
17681 displayed which has text on it, i.e. might end up as being the
17682 line where the window_end_vpos is. */
17683 w->cursor.vpos = -1;
17684 last_text_row = NULL;
17685 overlay_arrow_seen = 0;
17686 while (it.current_y < it.last_visible_y
17687 && !f->fonts_changed
17688 && (first_unchanged_at_end_row == NULL
17689 || IT_CHARPOS (it) < stop_pos))
17690 {
17691 if (display_line (&it))
17692 last_text_row = it.glyph_row - 1;
17693 }
17694
17695 if (f->fonts_changed)
17696 return -1;
17697
17698
17699 /* Compute differences in buffer positions, y-positions etc. for
17700 lines reused at the bottom of the window. Compute what we can
17701 scroll. */
17702 if (first_unchanged_at_end_row
17703 /* No lines reused because we displayed everything up to the
17704 bottom of the window. */
17705 && it.current_y < it.last_visible_y)
17706 {
17707 dvpos = (it.vpos
17708 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17709 current_matrix));
17710 dy = it.current_y - first_unchanged_at_end_row->y;
17711 run.current_y = first_unchanged_at_end_row->y;
17712 run.desired_y = run.current_y + dy;
17713 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17714 }
17715 else
17716 {
17717 delta = delta_bytes = dvpos = dy
17718 = run.current_y = run.desired_y = run.height = 0;
17719 first_unchanged_at_end_row = NULL;
17720 }
17721 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17722
17723
17724 /* Find the cursor if not already found. We have to decide whether
17725 PT will appear on this window (it sometimes doesn't, but this is
17726 not a very frequent case.) This decision has to be made before
17727 the current matrix is altered. A value of cursor.vpos < 0 means
17728 that PT is either in one of the lines beginning at
17729 first_unchanged_at_end_row or below the window. Don't care for
17730 lines that might be displayed later at the window end; as
17731 mentioned, this is not a frequent case. */
17732 if (w->cursor.vpos < 0)
17733 {
17734 /* Cursor in unchanged rows at the top? */
17735 if (PT < CHARPOS (start_pos)
17736 && last_unchanged_at_beg_row)
17737 {
17738 row = row_containing_pos (w, PT,
17739 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17740 last_unchanged_at_beg_row + 1, 0);
17741 if (row)
17742 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17743 }
17744
17745 /* Start from first_unchanged_at_end_row looking for PT. */
17746 else if (first_unchanged_at_end_row)
17747 {
17748 row = row_containing_pos (w, PT - delta,
17749 first_unchanged_at_end_row, NULL, 0);
17750 if (row)
17751 set_cursor_from_row (w, row, w->current_matrix, delta,
17752 delta_bytes, dy, dvpos);
17753 }
17754
17755 /* Give up if cursor was not found. */
17756 if (w->cursor.vpos < 0)
17757 {
17758 clear_glyph_matrix (w->desired_matrix);
17759 return -1;
17760 }
17761 }
17762
17763 /* Don't let the cursor end in the scroll margins. */
17764 {
17765 int this_scroll_margin, cursor_height;
17766 int frame_line_height = default_line_pixel_height (w);
17767 int window_total_lines
17768 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17769
17770 this_scroll_margin =
17771 max (0, min (scroll_margin, window_total_lines / 4));
17772 this_scroll_margin *= frame_line_height;
17773 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17774
17775 if ((w->cursor.y < this_scroll_margin
17776 && CHARPOS (start) > BEGV)
17777 /* Old redisplay didn't take scroll margin into account at the bottom,
17778 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17779 || (w->cursor.y + (make_cursor_line_fully_visible_p
17780 ? cursor_height + this_scroll_margin
17781 : 1)) > it.last_visible_y)
17782 {
17783 w->cursor.vpos = -1;
17784 clear_glyph_matrix (w->desired_matrix);
17785 return -1;
17786 }
17787 }
17788
17789 /* Scroll the display. Do it before changing the current matrix so
17790 that xterm.c doesn't get confused about where the cursor glyph is
17791 found. */
17792 if (dy && run.height)
17793 {
17794 update_begin (f);
17795
17796 if (FRAME_WINDOW_P (f))
17797 {
17798 FRAME_RIF (f)->update_window_begin_hook (w);
17799 FRAME_RIF (f)->clear_window_mouse_face (w);
17800 FRAME_RIF (f)->scroll_run_hook (w, &run);
17801 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17802 }
17803 else
17804 {
17805 /* Terminal frame. In this case, dvpos gives the number of
17806 lines to scroll by; dvpos < 0 means scroll up. */
17807 int from_vpos
17808 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17809 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17810 int end = (WINDOW_TOP_EDGE_LINE (w)
17811 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17812 + window_internal_height (w));
17813
17814 #if defined (HAVE_GPM) || defined (MSDOS)
17815 x_clear_window_mouse_face (w);
17816 #endif
17817 /* Perform the operation on the screen. */
17818 if (dvpos > 0)
17819 {
17820 /* Scroll last_unchanged_at_beg_row to the end of the
17821 window down dvpos lines. */
17822 set_terminal_window (f, end);
17823
17824 /* On dumb terminals delete dvpos lines at the end
17825 before inserting dvpos empty lines. */
17826 if (!FRAME_SCROLL_REGION_OK (f))
17827 ins_del_lines (f, end - dvpos, -dvpos);
17828
17829 /* Insert dvpos empty lines in front of
17830 last_unchanged_at_beg_row. */
17831 ins_del_lines (f, from, dvpos);
17832 }
17833 else if (dvpos < 0)
17834 {
17835 /* Scroll up last_unchanged_at_beg_vpos to the end of
17836 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17837 set_terminal_window (f, end);
17838
17839 /* Delete dvpos lines in front of
17840 last_unchanged_at_beg_vpos. ins_del_lines will set
17841 the cursor to the given vpos and emit |dvpos| delete
17842 line sequences. */
17843 ins_del_lines (f, from + dvpos, dvpos);
17844
17845 /* On a dumb terminal insert dvpos empty lines at the
17846 end. */
17847 if (!FRAME_SCROLL_REGION_OK (f))
17848 ins_del_lines (f, end + dvpos, -dvpos);
17849 }
17850
17851 set_terminal_window (f, 0);
17852 }
17853
17854 update_end (f);
17855 }
17856
17857 /* Shift reused rows of the current matrix to the right position.
17858 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17859 text. */
17860 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17861 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17862 if (dvpos < 0)
17863 {
17864 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17865 bottom_vpos, dvpos);
17866 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17867 bottom_vpos);
17868 }
17869 else if (dvpos > 0)
17870 {
17871 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17872 bottom_vpos, dvpos);
17873 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17874 first_unchanged_at_end_vpos + dvpos);
17875 }
17876
17877 /* For frame-based redisplay, make sure that current frame and window
17878 matrix are in sync with respect to glyph memory. */
17879 if (!FRAME_WINDOW_P (f))
17880 sync_frame_with_window_matrix_rows (w);
17881
17882 /* Adjust buffer positions in reused rows. */
17883 if (delta || delta_bytes)
17884 increment_matrix_positions (current_matrix,
17885 first_unchanged_at_end_vpos + dvpos,
17886 bottom_vpos, delta, delta_bytes);
17887
17888 /* Adjust Y positions. */
17889 if (dy)
17890 shift_glyph_matrix (w, current_matrix,
17891 first_unchanged_at_end_vpos + dvpos,
17892 bottom_vpos, dy);
17893
17894 if (first_unchanged_at_end_row)
17895 {
17896 first_unchanged_at_end_row += dvpos;
17897 if (first_unchanged_at_end_row->y >= it.last_visible_y
17898 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17899 first_unchanged_at_end_row = NULL;
17900 }
17901
17902 /* If scrolling up, there may be some lines to display at the end of
17903 the window. */
17904 last_text_row_at_end = NULL;
17905 if (dy < 0)
17906 {
17907 /* Scrolling up can leave for example a partially visible line
17908 at the end of the window to be redisplayed. */
17909 /* Set last_row to the glyph row in the current matrix where the
17910 window end line is found. It has been moved up or down in
17911 the matrix by dvpos. */
17912 int last_vpos = w->window_end_vpos + dvpos;
17913 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17914
17915 /* If last_row is the window end line, it should display text. */
17916 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17917
17918 /* If window end line was partially visible before, begin
17919 displaying at that line. Otherwise begin displaying with the
17920 line following it. */
17921 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17922 {
17923 init_to_row_start (&it, w, last_row);
17924 it.vpos = last_vpos;
17925 it.current_y = last_row->y;
17926 }
17927 else
17928 {
17929 init_to_row_end (&it, w, last_row);
17930 it.vpos = 1 + last_vpos;
17931 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17932 ++last_row;
17933 }
17934
17935 /* We may start in a continuation line. If so, we have to
17936 get the right continuation_lines_width and current_x. */
17937 it.continuation_lines_width = last_row->continuation_lines_width;
17938 it.hpos = it.current_x = 0;
17939
17940 /* Display the rest of the lines at the window end. */
17941 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17942 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17943 {
17944 /* Is it always sure that the display agrees with lines in
17945 the current matrix? I don't think so, so we mark rows
17946 displayed invalid in the current matrix by setting their
17947 enabled_p flag to zero. */
17948 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17949 if (display_line (&it))
17950 last_text_row_at_end = it.glyph_row - 1;
17951 }
17952 }
17953
17954 /* Update window_end_pos and window_end_vpos. */
17955 if (first_unchanged_at_end_row && !last_text_row_at_end)
17956 {
17957 /* Window end line if one of the preserved rows from the current
17958 matrix. Set row to the last row displaying text in current
17959 matrix starting at first_unchanged_at_end_row, after
17960 scrolling. */
17961 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17962 row = find_last_row_displaying_text (w->current_matrix, &it,
17963 first_unchanged_at_end_row);
17964 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17965 adjust_window_ends (w, row, 1);
17966 eassert (w->window_end_bytepos >= 0);
17967 IF_DEBUG (debug_method_add (w, "A"));
17968 }
17969 else if (last_text_row_at_end)
17970 {
17971 adjust_window_ends (w, last_text_row_at_end, 0);
17972 eassert (w->window_end_bytepos >= 0);
17973 IF_DEBUG (debug_method_add (w, "B"));
17974 }
17975 else if (last_text_row)
17976 {
17977 /* We have displayed either to the end of the window or at the
17978 end of the window, i.e. the last row with text is to be found
17979 in the desired matrix. */
17980 adjust_window_ends (w, last_text_row, 0);
17981 eassert (w->window_end_bytepos >= 0);
17982 }
17983 else if (first_unchanged_at_end_row == NULL
17984 && last_text_row == NULL
17985 && last_text_row_at_end == NULL)
17986 {
17987 /* Displayed to end of window, but no line containing text was
17988 displayed. Lines were deleted at the end of the window. */
17989 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17990 int vpos = w->window_end_vpos;
17991 struct glyph_row *current_row = current_matrix->rows + vpos;
17992 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17993
17994 for (row = NULL;
17995 row == NULL && vpos >= first_vpos;
17996 --vpos, --current_row, --desired_row)
17997 {
17998 if (desired_row->enabled_p)
17999 {
18000 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18001 row = desired_row;
18002 }
18003 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18004 row = current_row;
18005 }
18006
18007 eassert (row != NULL);
18008 w->window_end_vpos = vpos + 1;
18009 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18010 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18011 eassert (w->window_end_bytepos >= 0);
18012 IF_DEBUG (debug_method_add (w, "C"));
18013 }
18014 else
18015 emacs_abort ();
18016
18017 IF_DEBUG (debug_end_pos = w->window_end_pos;
18018 debug_end_vpos = w->window_end_vpos);
18019
18020 /* Record that display has not been completed. */
18021 w->window_end_valid = 0;
18022 w->desired_matrix->no_scrolling_p = 1;
18023 return 3;
18024
18025 #undef GIVE_UP
18026 }
18027
18028
18029 \f
18030 /***********************************************************************
18031 More debugging support
18032 ***********************************************************************/
18033
18034 #ifdef GLYPH_DEBUG
18035
18036 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18037 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18038 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18039
18040
18041 /* Dump the contents of glyph matrix MATRIX on stderr.
18042
18043 GLYPHS 0 means don't show glyph contents.
18044 GLYPHS 1 means show glyphs in short form
18045 GLYPHS > 1 means show glyphs in long form. */
18046
18047 void
18048 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18049 {
18050 int i;
18051 for (i = 0; i < matrix->nrows; ++i)
18052 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18053 }
18054
18055
18056 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18057 the glyph row and area where the glyph comes from. */
18058
18059 void
18060 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18061 {
18062 if (glyph->type == CHAR_GLYPH
18063 || glyph->type == GLYPHLESS_GLYPH)
18064 {
18065 fprintf (stderr,
18066 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18067 glyph - row->glyphs[TEXT_AREA],
18068 (glyph->type == CHAR_GLYPH
18069 ? 'C'
18070 : 'G'),
18071 glyph->charpos,
18072 (BUFFERP (glyph->object)
18073 ? 'B'
18074 : (STRINGP (glyph->object)
18075 ? 'S'
18076 : (INTEGERP (glyph->object)
18077 ? '0'
18078 : '-'))),
18079 glyph->pixel_width,
18080 glyph->u.ch,
18081 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18082 ? glyph->u.ch
18083 : '.'),
18084 glyph->face_id,
18085 glyph->left_box_line_p,
18086 glyph->right_box_line_p);
18087 }
18088 else if (glyph->type == STRETCH_GLYPH)
18089 {
18090 fprintf (stderr,
18091 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18092 glyph - row->glyphs[TEXT_AREA],
18093 'S',
18094 glyph->charpos,
18095 (BUFFERP (glyph->object)
18096 ? 'B'
18097 : (STRINGP (glyph->object)
18098 ? 'S'
18099 : (INTEGERP (glyph->object)
18100 ? '0'
18101 : '-'))),
18102 glyph->pixel_width,
18103 0,
18104 ' ',
18105 glyph->face_id,
18106 glyph->left_box_line_p,
18107 glyph->right_box_line_p);
18108 }
18109 else if (glyph->type == IMAGE_GLYPH)
18110 {
18111 fprintf (stderr,
18112 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18113 glyph - row->glyphs[TEXT_AREA],
18114 'I',
18115 glyph->charpos,
18116 (BUFFERP (glyph->object)
18117 ? 'B'
18118 : (STRINGP (glyph->object)
18119 ? 'S'
18120 : (INTEGERP (glyph->object)
18121 ? '0'
18122 : '-'))),
18123 glyph->pixel_width,
18124 glyph->u.img_id,
18125 '.',
18126 glyph->face_id,
18127 glyph->left_box_line_p,
18128 glyph->right_box_line_p);
18129 }
18130 else if (glyph->type == COMPOSITE_GLYPH)
18131 {
18132 fprintf (stderr,
18133 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18134 glyph - row->glyphs[TEXT_AREA],
18135 '+',
18136 glyph->charpos,
18137 (BUFFERP (glyph->object)
18138 ? 'B'
18139 : (STRINGP (glyph->object)
18140 ? 'S'
18141 : (INTEGERP (glyph->object)
18142 ? '0'
18143 : '-'))),
18144 glyph->pixel_width,
18145 glyph->u.cmp.id);
18146 if (glyph->u.cmp.automatic)
18147 fprintf (stderr,
18148 "[%d-%d]",
18149 glyph->slice.cmp.from, glyph->slice.cmp.to);
18150 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18151 glyph->face_id,
18152 glyph->left_box_line_p,
18153 glyph->right_box_line_p);
18154 }
18155 }
18156
18157
18158 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18159 GLYPHS 0 means don't show glyph contents.
18160 GLYPHS 1 means show glyphs in short form
18161 GLYPHS > 1 means show glyphs in long form. */
18162
18163 void
18164 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18165 {
18166 if (glyphs != 1)
18167 {
18168 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18169 fprintf (stderr, "==============================================================================\n");
18170
18171 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18172 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18173 vpos,
18174 MATRIX_ROW_START_CHARPOS (row),
18175 MATRIX_ROW_END_CHARPOS (row),
18176 row->used[TEXT_AREA],
18177 row->contains_overlapping_glyphs_p,
18178 row->enabled_p,
18179 row->truncated_on_left_p,
18180 row->truncated_on_right_p,
18181 row->continued_p,
18182 MATRIX_ROW_CONTINUATION_LINE_P (row),
18183 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18184 row->ends_at_zv_p,
18185 row->fill_line_p,
18186 row->ends_in_middle_of_char_p,
18187 row->starts_in_middle_of_char_p,
18188 row->mouse_face_p,
18189 row->x,
18190 row->y,
18191 row->pixel_width,
18192 row->height,
18193 row->visible_height,
18194 row->ascent,
18195 row->phys_ascent);
18196 /* The next 3 lines should align to "Start" in the header. */
18197 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18198 row->end.overlay_string_index,
18199 row->continuation_lines_width);
18200 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18201 CHARPOS (row->start.string_pos),
18202 CHARPOS (row->end.string_pos));
18203 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18204 row->end.dpvec_index);
18205 }
18206
18207 if (glyphs > 1)
18208 {
18209 int area;
18210
18211 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18212 {
18213 struct glyph *glyph = row->glyphs[area];
18214 struct glyph *glyph_end = glyph + row->used[area];
18215
18216 /* Glyph for a line end in text. */
18217 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18218 ++glyph_end;
18219
18220 if (glyph < glyph_end)
18221 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18222
18223 for (; glyph < glyph_end; ++glyph)
18224 dump_glyph (row, glyph, area);
18225 }
18226 }
18227 else if (glyphs == 1)
18228 {
18229 int area;
18230
18231 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18232 {
18233 char *s = alloca (row->used[area] + 4);
18234 int i;
18235
18236 for (i = 0; i < row->used[area]; ++i)
18237 {
18238 struct glyph *glyph = row->glyphs[area] + i;
18239 if (i == row->used[area] - 1
18240 && area == TEXT_AREA
18241 && INTEGERP (glyph->object)
18242 && glyph->type == CHAR_GLYPH
18243 && glyph->u.ch == ' ')
18244 {
18245 strcpy (&s[i], "[\\n]");
18246 i += 4;
18247 }
18248 else if (glyph->type == CHAR_GLYPH
18249 && glyph->u.ch < 0x80
18250 && glyph->u.ch >= ' ')
18251 s[i] = glyph->u.ch;
18252 else
18253 s[i] = '.';
18254 }
18255
18256 s[i] = '\0';
18257 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18258 }
18259 }
18260 }
18261
18262
18263 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18264 Sdump_glyph_matrix, 0, 1, "p",
18265 doc: /* Dump the current matrix of the selected window to stderr.
18266 Shows contents of glyph row structures. With non-nil
18267 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18268 glyphs in short form, otherwise show glyphs in long form. */)
18269 (Lisp_Object glyphs)
18270 {
18271 struct window *w = XWINDOW (selected_window);
18272 struct buffer *buffer = XBUFFER (w->contents);
18273
18274 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18275 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18276 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18277 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18278 fprintf (stderr, "=============================================\n");
18279 dump_glyph_matrix (w->current_matrix,
18280 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18281 return Qnil;
18282 }
18283
18284
18285 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18286 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18287 (void)
18288 {
18289 struct frame *f = XFRAME (selected_frame);
18290 dump_glyph_matrix (f->current_matrix, 1);
18291 return Qnil;
18292 }
18293
18294
18295 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18296 doc: /* Dump glyph row ROW to stderr.
18297 GLYPH 0 means don't dump glyphs.
18298 GLYPH 1 means dump glyphs in short form.
18299 GLYPH > 1 or omitted means dump glyphs in long form. */)
18300 (Lisp_Object row, Lisp_Object glyphs)
18301 {
18302 struct glyph_matrix *matrix;
18303 EMACS_INT vpos;
18304
18305 CHECK_NUMBER (row);
18306 matrix = XWINDOW (selected_window)->current_matrix;
18307 vpos = XINT (row);
18308 if (vpos >= 0 && vpos < matrix->nrows)
18309 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18310 vpos,
18311 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18312 return Qnil;
18313 }
18314
18315
18316 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18317 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18318 GLYPH 0 means don't dump glyphs.
18319 GLYPH 1 means dump glyphs in short form.
18320 GLYPH > 1 or omitted means dump glyphs in long form.
18321
18322 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18323 do nothing. */)
18324 (Lisp_Object row, Lisp_Object glyphs)
18325 {
18326 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18327 struct frame *sf = SELECTED_FRAME ();
18328 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18329 EMACS_INT vpos;
18330
18331 CHECK_NUMBER (row);
18332 vpos = XINT (row);
18333 if (vpos >= 0 && vpos < m->nrows)
18334 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18335 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18336 #endif
18337 return Qnil;
18338 }
18339
18340
18341 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18342 doc: /* Toggle tracing of redisplay.
18343 With ARG, turn tracing on if and only if ARG is positive. */)
18344 (Lisp_Object arg)
18345 {
18346 if (NILP (arg))
18347 trace_redisplay_p = !trace_redisplay_p;
18348 else
18349 {
18350 arg = Fprefix_numeric_value (arg);
18351 trace_redisplay_p = XINT (arg) > 0;
18352 }
18353
18354 return Qnil;
18355 }
18356
18357
18358 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18359 doc: /* Like `format', but print result to stderr.
18360 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18361 (ptrdiff_t nargs, Lisp_Object *args)
18362 {
18363 Lisp_Object s = Fformat (nargs, args);
18364 fprintf (stderr, "%s", SDATA (s));
18365 return Qnil;
18366 }
18367
18368 #endif /* GLYPH_DEBUG */
18369
18370
18371 \f
18372 /***********************************************************************
18373 Building Desired Matrix Rows
18374 ***********************************************************************/
18375
18376 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18377 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18378
18379 static struct glyph_row *
18380 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18381 {
18382 struct frame *f = XFRAME (WINDOW_FRAME (w));
18383 struct buffer *buffer = XBUFFER (w->contents);
18384 struct buffer *old = current_buffer;
18385 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18386 int arrow_len = SCHARS (overlay_arrow_string);
18387 const unsigned char *arrow_end = arrow_string + arrow_len;
18388 const unsigned char *p;
18389 struct it it;
18390 bool multibyte_p;
18391 int n_glyphs_before;
18392
18393 set_buffer_temp (buffer);
18394 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18395 it.glyph_row->used[TEXT_AREA] = 0;
18396 SET_TEXT_POS (it.position, 0, 0);
18397
18398 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18399 p = arrow_string;
18400 while (p < arrow_end)
18401 {
18402 Lisp_Object face, ilisp;
18403
18404 /* Get the next character. */
18405 if (multibyte_p)
18406 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18407 else
18408 {
18409 it.c = it.char_to_display = *p, it.len = 1;
18410 if (! ASCII_CHAR_P (it.c))
18411 it.char_to_display = BYTE8_TO_CHAR (it.c);
18412 }
18413 p += it.len;
18414
18415 /* Get its face. */
18416 ilisp = make_number (p - arrow_string);
18417 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18418 it.face_id = compute_char_face (f, it.char_to_display, face);
18419
18420 /* Compute its width, get its glyphs. */
18421 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18422 SET_TEXT_POS (it.position, -1, -1);
18423 PRODUCE_GLYPHS (&it);
18424
18425 /* If this character doesn't fit any more in the line, we have
18426 to remove some glyphs. */
18427 if (it.current_x > it.last_visible_x)
18428 {
18429 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18430 break;
18431 }
18432 }
18433
18434 set_buffer_temp (old);
18435 return it.glyph_row;
18436 }
18437
18438
18439 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18440 glyphs to insert is determined by produce_special_glyphs. */
18441
18442 static void
18443 insert_left_trunc_glyphs (struct it *it)
18444 {
18445 struct it truncate_it;
18446 struct glyph *from, *end, *to, *toend;
18447
18448 eassert (!FRAME_WINDOW_P (it->f)
18449 || (!it->glyph_row->reversed_p
18450 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18451 || (it->glyph_row->reversed_p
18452 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18453
18454 /* Get the truncation glyphs. */
18455 truncate_it = *it;
18456 truncate_it.current_x = 0;
18457 truncate_it.face_id = DEFAULT_FACE_ID;
18458 truncate_it.glyph_row = &scratch_glyph_row;
18459 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18460 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18461 truncate_it.object = make_number (0);
18462 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18463
18464 /* Overwrite glyphs from IT with truncation glyphs. */
18465 if (!it->glyph_row->reversed_p)
18466 {
18467 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18468
18469 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18470 end = from + tused;
18471 to = it->glyph_row->glyphs[TEXT_AREA];
18472 toend = to + it->glyph_row->used[TEXT_AREA];
18473 if (FRAME_WINDOW_P (it->f))
18474 {
18475 /* On GUI frames, when variable-size fonts are displayed,
18476 the truncation glyphs may need more pixels than the row's
18477 glyphs they overwrite. We overwrite more glyphs to free
18478 enough screen real estate, and enlarge the stretch glyph
18479 on the right (see display_line), if there is one, to
18480 preserve the screen position of the truncation glyphs on
18481 the right. */
18482 int w = 0;
18483 struct glyph *g = to;
18484 short used;
18485
18486 /* The first glyph could be partially visible, in which case
18487 it->glyph_row->x will be negative. But we want the left
18488 truncation glyphs to be aligned at the left margin of the
18489 window, so we override the x coordinate at which the row
18490 will begin. */
18491 it->glyph_row->x = 0;
18492 while (g < toend && w < it->truncation_pixel_width)
18493 {
18494 w += g->pixel_width;
18495 ++g;
18496 }
18497 if (g - to - tused > 0)
18498 {
18499 memmove (to + tused, g, (toend - g) * sizeof(*g));
18500 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18501 }
18502 used = it->glyph_row->used[TEXT_AREA];
18503 if (it->glyph_row->truncated_on_right_p
18504 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18505 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18506 == STRETCH_GLYPH)
18507 {
18508 int extra = w - it->truncation_pixel_width;
18509
18510 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18511 }
18512 }
18513
18514 while (from < end)
18515 *to++ = *from++;
18516
18517 /* There may be padding glyphs left over. Overwrite them too. */
18518 if (!FRAME_WINDOW_P (it->f))
18519 {
18520 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18521 {
18522 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18523 while (from < end)
18524 *to++ = *from++;
18525 }
18526 }
18527
18528 if (to > toend)
18529 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18530 }
18531 else
18532 {
18533 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18534
18535 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18536 that back to front. */
18537 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18538 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18539 toend = it->glyph_row->glyphs[TEXT_AREA];
18540 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18541 if (FRAME_WINDOW_P (it->f))
18542 {
18543 int w = 0;
18544 struct glyph *g = to;
18545
18546 while (g >= toend && w < it->truncation_pixel_width)
18547 {
18548 w += g->pixel_width;
18549 --g;
18550 }
18551 if (to - g - tused > 0)
18552 to = g + tused;
18553 if (it->glyph_row->truncated_on_right_p
18554 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18555 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18556 {
18557 int extra = w - it->truncation_pixel_width;
18558
18559 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18560 }
18561 }
18562
18563 while (from >= end && to >= toend)
18564 *to-- = *from--;
18565 if (!FRAME_WINDOW_P (it->f))
18566 {
18567 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18568 {
18569 from =
18570 truncate_it.glyph_row->glyphs[TEXT_AREA]
18571 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18572 while (from >= end && to >= toend)
18573 *to-- = *from--;
18574 }
18575 }
18576 if (from >= end)
18577 {
18578 /* Need to free some room before prepending additional
18579 glyphs. */
18580 int move_by = from - end + 1;
18581 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18582 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18583
18584 for ( ; g >= g0; g--)
18585 g[move_by] = *g;
18586 while (from >= end)
18587 *to-- = *from--;
18588 it->glyph_row->used[TEXT_AREA] += move_by;
18589 }
18590 }
18591 }
18592
18593 /* Compute the hash code for ROW. */
18594 unsigned
18595 row_hash (struct glyph_row *row)
18596 {
18597 int area, k;
18598 unsigned hashval = 0;
18599
18600 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18601 for (k = 0; k < row->used[area]; ++k)
18602 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18603 + row->glyphs[area][k].u.val
18604 + row->glyphs[area][k].face_id
18605 + row->glyphs[area][k].padding_p
18606 + (row->glyphs[area][k].type << 2));
18607
18608 return hashval;
18609 }
18610
18611 /* Compute the pixel height and width of IT->glyph_row.
18612
18613 Most of the time, ascent and height of a display line will be equal
18614 to the max_ascent and max_height values of the display iterator
18615 structure. This is not the case if
18616
18617 1. We hit ZV without displaying anything. In this case, max_ascent
18618 and max_height will be zero.
18619
18620 2. We have some glyphs that don't contribute to the line height.
18621 (The glyph row flag contributes_to_line_height_p is for future
18622 pixmap extensions).
18623
18624 The first case is easily covered by using default values because in
18625 these cases, the line height does not really matter, except that it
18626 must not be zero. */
18627
18628 static void
18629 compute_line_metrics (struct it *it)
18630 {
18631 struct glyph_row *row = it->glyph_row;
18632
18633 if (FRAME_WINDOW_P (it->f))
18634 {
18635 int i, min_y, max_y;
18636
18637 /* The line may consist of one space only, that was added to
18638 place the cursor on it. If so, the row's height hasn't been
18639 computed yet. */
18640 if (row->height == 0)
18641 {
18642 if (it->max_ascent + it->max_descent == 0)
18643 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18644 row->ascent = it->max_ascent;
18645 row->height = it->max_ascent + it->max_descent;
18646 row->phys_ascent = it->max_phys_ascent;
18647 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18648 row->extra_line_spacing = it->max_extra_line_spacing;
18649 }
18650
18651 /* Compute the width of this line. */
18652 row->pixel_width = row->x;
18653 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18654 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18655
18656 eassert (row->pixel_width >= 0);
18657 eassert (row->ascent >= 0 && row->height > 0);
18658
18659 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18660 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18661
18662 /* If first line's physical ascent is larger than its logical
18663 ascent, use the physical ascent, and make the row taller.
18664 This makes accented characters fully visible. */
18665 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18666 && row->phys_ascent > row->ascent)
18667 {
18668 row->height += row->phys_ascent - row->ascent;
18669 row->ascent = row->phys_ascent;
18670 }
18671
18672 /* Compute how much of the line is visible. */
18673 row->visible_height = row->height;
18674
18675 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18676 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18677
18678 if (row->y < min_y)
18679 row->visible_height -= min_y - row->y;
18680 if (row->y + row->height > max_y)
18681 row->visible_height -= row->y + row->height - max_y;
18682 }
18683 else
18684 {
18685 row->pixel_width = row->used[TEXT_AREA];
18686 if (row->continued_p)
18687 row->pixel_width -= it->continuation_pixel_width;
18688 else if (row->truncated_on_right_p)
18689 row->pixel_width -= it->truncation_pixel_width;
18690 row->ascent = row->phys_ascent = 0;
18691 row->height = row->phys_height = row->visible_height = 1;
18692 row->extra_line_spacing = 0;
18693 }
18694
18695 /* Compute a hash code for this row. */
18696 row->hash = row_hash (row);
18697
18698 it->max_ascent = it->max_descent = 0;
18699 it->max_phys_ascent = it->max_phys_descent = 0;
18700 }
18701
18702
18703 /* Append one space to the glyph row of iterator IT if doing a
18704 window-based redisplay. The space has the same face as
18705 IT->face_id. Value is non-zero if a space was added.
18706
18707 This function is called to make sure that there is always one glyph
18708 at the end of a glyph row that the cursor can be set on under
18709 window-systems. (If there weren't such a glyph we would not know
18710 how wide and tall a box cursor should be displayed).
18711
18712 At the same time this space let's a nicely handle clearing to the
18713 end of the line if the row ends in italic text. */
18714
18715 static int
18716 append_space_for_newline (struct it *it, int default_face_p)
18717 {
18718 if (FRAME_WINDOW_P (it->f))
18719 {
18720 int n = it->glyph_row->used[TEXT_AREA];
18721
18722 if (it->glyph_row->glyphs[TEXT_AREA] + n
18723 < it->glyph_row->glyphs[1 + TEXT_AREA])
18724 {
18725 /* Save some values that must not be changed.
18726 Must save IT->c and IT->len because otherwise
18727 ITERATOR_AT_END_P wouldn't work anymore after
18728 append_space_for_newline has been called. */
18729 enum display_element_type saved_what = it->what;
18730 int saved_c = it->c, saved_len = it->len;
18731 int saved_char_to_display = it->char_to_display;
18732 int saved_x = it->current_x;
18733 int saved_face_id = it->face_id;
18734 int saved_box_end = it->end_of_box_run_p;
18735 struct text_pos saved_pos;
18736 Lisp_Object saved_object;
18737 struct face *face;
18738
18739 saved_object = it->object;
18740 saved_pos = it->position;
18741
18742 it->what = IT_CHARACTER;
18743 memset (&it->position, 0, sizeof it->position);
18744 it->object = make_number (0);
18745 it->c = it->char_to_display = ' ';
18746 it->len = 1;
18747
18748 /* If the default face was remapped, be sure to use the
18749 remapped face for the appended newline. */
18750 if (default_face_p)
18751 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18752 else if (it->face_before_selective_p)
18753 it->face_id = it->saved_face_id;
18754 face = FACE_FROM_ID (it->f, it->face_id);
18755 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18756 /* In R2L rows, we will prepend a stretch glyph that will
18757 have the end_of_box_run_p flag set for it, so there's no
18758 need for the appended newline glyph to have that flag
18759 set. */
18760 if (it->glyph_row->reversed_p
18761 /* But if the appended newline glyph goes all the way to
18762 the end of the row, there will be no stretch glyph,
18763 so leave the box flag set. */
18764 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18765 it->end_of_box_run_p = 0;
18766
18767 PRODUCE_GLYPHS (it);
18768
18769 it->override_ascent = -1;
18770 it->constrain_row_ascent_descent_p = 0;
18771 it->current_x = saved_x;
18772 it->object = saved_object;
18773 it->position = saved_pos;
18774 it->what = saved_what;
18775 it->face_id = saved_face_id;
18776 it->len = saved_len;
18777 it->c = saved_c;
18778 it->char_to_display = saved_char_to_display;
18779 it->end_of_box_run_p = saved_box_end;
18780 return 1;
18781 }
18782 }
18783
18784 return 0;
18785 }
18786
18787
18788 /* Extend the face of the last glyph in the text area of IT->glyph_row
18789 to the end of the display line. Called from display_line. If the
18790 glyph row is empty, add a space glyph to it so that we know the
18791 face to draw. Set the glyph row flag fill_line_p. If the glyph
18792 row is R2L, prepend a stretch glyph to cover the empty space to the
18793 left of the leftmost glyph. */
18794
18795 static void
18796 extend_face_to_end_of_line (struct it *it)
18797 {
18798 struct face *face, *default_face;
18799 struct frame *f = it->f;
18800
18801 /* If line is already filled, do nothing. Non window-system frames
18802 get a grace of one more ``pixel'' because their characters are
18803 1-``pixel'' wide, so they hit the equality too early. This grace
18804 is needed only for R2L rows that are not continued, to produce
18805 one extra blank where we could display the cursor. */
18806 if (it->current_x >= it->last_visible_x
18807 + (!FRAME_WINDOW_P (f)
18808 && it->glyph_row->reversed_p
18809 && !it->glyph_row->continued_p))
18810 return;
18811
18812 /* The default face, possibly remapped. */
18813 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18814
18815 /* Face extension extends the background and box of IT->face_id
18816 to the end of the line. If the background equals the background
18817 of the frame, we don't have to do anything. */
18818 if (it->face_before_selective_p)
18819 face = FACE_FROM_ID (f, it->saved_face_id);
18820 else
18821 face = FACE_FROM_ID (f, it->face_id);
18822
18823 if (FRAME_WINDOW_P (f)
18824 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18825 && face->box == FACE_NO_BOX
18826 && face->background == FRAME_BACKGROUND_PIXEL (f)
18827 #ifdef HAVE_WINDOW_SYSTEM
18828 && !face->stipple
18829 #endif
18830 && !it->glyph_row->reversed_p)
18831 return;
18832
18833 /* Set the glyph row flag indicating that the face of the last glyph
18834 in the text area has to be drawn to the end of the text area. */
18835 it->glyph_row->fill_line_p = 1;
18836
18837 /* If current character of IT is not ASCII, make sure we have the
18838 ASCII face. This will be automatically undone the next time
18839 get_next_display_element returns a multibyte character. Note
18840 that the character will always be single byte in unibyte
18841 text. */
18842 if (!ASCII_CHAR_P (it->c))
18843 {
18844 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18845 }
18846
18847 if (FRAME_WINDOW_P (f))
18848 {
18849 /* If the row is empty, add a space with the current face of IT,
18850 so that we know which face to draw. */
18851 if (it->glyph_row->used[TEXT_AREA] == 0)
18852 {
18853 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18854 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18855 it->glyph_row->used[TEXT_AREA] = 1;
18856 }
18857 #ifdef HAVE_WINDOW_SYSTEM
18858 if (it->glyph_row->reversed_p)
18859 {
18860 /* Prepend a stretch glyph to the row, such that the
18861 rightmost glyph will be drawn flushed all the way to the
18862 right margin of the window. The stretch glyph that will
18863 occupy the empty space, if any, to the left of the
18864 glyphs. */
18865 struct font *font = face->font ? face->font : FRAME_FONT (f);
18866 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18867 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18868 struct glyph *g;
18869 int row_width, stretch_ascent, stretch_width;
18870 struct text_pos saved_pos;
18871 int saved_face_id, saved_avoid_cursor, saved_box_start;
18872
18873 for (row_width = 0, g = row_start; g < row_end; g++)
18874 row_width += g->pixel_width;
18875 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18876 if (stretch_width > 0)
18877 {
18878 stretch_ascent =
18879 (((it->ascent + it->descent)
18880 * FONT_BASE (font)) / FONT_HEIGHT (font));
18881 saved_pos = it->position;
18882 memset (&it->position, 0, sizeof it->position);
18883 saved_avoid_cursor = it->avoid_cursor_p;
18884 it->avoid_cursor_p = 1;
18885 saved_face_id = it->face_id;
18886 saved_box_start = it->start_of_box_run_p;
18887 /* The last row's stretch glyph should get the default
18888 face, to avoid painting the rest of the window with
18889 the region face, if the region ends at ZV. */
18890 if (it->glyph_row->ends_at_zv_p)
18891 it->face_id = default_face->id;
18892 else
18893 it->face_id = face->id;
18894 it->start_of_box_run_p = 0;
18895 append_stretch_glyph (it, make_number (0), stretch_width,
18896 it->ascent + it->descent, stretch_ascent);
18897 it->position = saved_pos;
18898 it->avoid_cursor_p = saved_avoid_cursor;
18899 it->face_id = saved_face_id;
18900 it->start_of_box_run_p = saved_box_start;
18901 }
18902 }
18903 #endif /* HAVE_WINDOW_SYSTEM */
18904 }
18905 else
18906 {
18907 /* Save some values that must not be changed. */
18908 int saved_x = it->current_x;
18909 struct text_pos saved_pos;
18910 Lisp_Object saved_object;
18911 enum display_element_type saved_what = it->what;
18912 int saved_face_id = it->face_id;
18913
18914 saved_object = it->object;
18915 saved_pos = it->position;
18916
18917 it->what = IT_CHARACTER;
18918 memset (&it->position, 0, sizeof it->position);
18919 it->object = make_number (0);
18920 it->c = it->char_to_display = ' ';
18921 it->len = 1;
18922 /* The last row's blank glyphs should get the default face, to
18923 avoid painting the rest of the window with the region face,
18924 if the region ends at ZV. */
18925 if (it->glyph_row->ends_at_zv_p)
18926 it->face_id = default_face->id;
18927 else
18928 it->face_id = face->id;
18929
18930 PRODUCE_GLYPHS (it);
18931
18932 while (it->current_x <= it->last_visible_x)
18933 PRODUCE_GLYPHS (it);
18934
18935 /* Don't count these blanks really. It would let us insert a left
18936 truncation glyph below and make us set the cursor on them, maybe. */
18937 it->current_x = saved_x;
18938 it->object = saved_object;
18939 it->position = saved_pos;
18940 it->what = saved_what;
18941 it->face_id = saved_face_id;
18942 }
18943 }
18944
18945
18946 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18947 trailing whitespace. */
18948
18949 static int
18950 trailing_whitespace_p (ptrdiff_t charpos)
18951 {
18952 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18953 int c = 0;
18954
18955 while (bytepos < ZV_BYTE
18956 && (c = FETCH_CHAR (bytepos),
18957 c == ' ' || c == '\t'))
18958 ++bytepos;
18959
18960 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18961 {
18962 if (bytepos != PT_BYTE)
18963 return 1;
18964 }
18965 return 0;
18966 }
18967
18968
18969 /* Highlight trailing whitespace, if any, in ROW. */
18970
18971 static void
18972 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18973 {
18974 int used = row->used[TEXT_AREA];
18975
18976 if (used)
18977 {
18978 struct glyph *start = row->glyphs[TEXT_AREA];
18979 struct glyph *glyph = start + used - 1;
18980
18981 if (row->reversed_p)
18982 {
18983 /* Right-to-left rows need to be processed in the opposite
18984 direction, so swap the edge pointers. */
18985 glyph = start;
18986 start = row->glyphs[TEXT_AREA] + used - 1;
18987 }
18988
18989 /* Skip over glyphs inserted to display the cursor at the
18990 end of a line, for extending the face of the last glyph
18991 to the end of the line on terminals, and for truncation
18992 and continuation glyphs. */
18993 if (!row->reversed_p)
18994 {
18995 while (glyph >= start
18996 && glyph->type == CHAR_GLYPH
18997 && INTEGERP (glyph->object))
18998 --glyph;
18999 }
19000 else
19001 {
19002 while (glyph <= start
19003 && glyph->type == CHAR_GLYPH
19004 && INTEGERP (glyph->object))
19005 ++glyph;
19006 }
19007
19008 /* If last glyph is a space or stretch, and it's trailing
19009 whitespace, set the face of all trailing whitespace glyphs in
19010 IT->glyph_row to `trailing-whitespace'. */
19011 if ((row->reversed_p ? glyph <= start : glyph >= start)
19012 && BUFFERP (glyph->object)
19013 && (glyph->type == STRETCH_GLYPH
19014 || (glyph->type == CHAR_GLYPH
19015 && glyph->u.ch == ' '))
19016 && trailing_whitespace_p (glyph->charpos))
19017 {
19018 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19019 if (face_id < 0)
19020 return;
19021
19022 if (!row->reversed_p)
19023 {
19024 while (glyph >= start
19025 && BUFFERP (glyph->object)
19026 && (glyph->type == STRETCH_GLYPH
19027 || (glyph->type == CHAR_GLYPH
19028 && glyph->u.ch == ' ')))
19029 (glyph--)->face_id = face_id;
19030 }
19031 else
19032 {
19033 while (glyph <= start
19034 && BUFFERP (glyph->object)
19035 && (glyph->type == STRETCH_GLYPH
19036 || (glyph->type == CHAR_GLYPH
19037 && glyph->u.ch == ' ')))
19038 (glyph++)->face_id = face_id;
19039 }
19040 }
19041 }
19042 }
19043
19044
19045 /* Value is non-zero if glyph row ROW should be
19046 considered to hold the buffer position CHARPOS. */
19047
19048 static int
19049 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19050 {
19051 int result = 1;
19052
19053 if (charpos == CHARPOS (row->end.pos)
19054 || charpos == MATRIX_ROW_END_CHARPOS (row))
19055 {
19056 /* Suppose the row ends on a string.
19057 Unless the row is continued, that means it ends on a newline
19058 in the string. If it's anything other than a display string
19059 (e.g., a before-string from an overlay), we don't want the
19060 cursor there. (This heuristic seems to give the optimal
19061 behavior for the various types of multi-line strings.)
19062 One exception: if the string has `cursor' property on one of
19063 its characters, we _do_ want the cursor there. */
19064 if (CHARPOS (row->end.string_pos) >= 0)
19065 {
19066 if (row->continued_p)
19067 result = 1;
19068 else
19069 {
19070 /* Check for `display' property. */
19071 struct glyph *beg = row->glyphs[TEXT_AREA];
19072 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19073 struct glyph *glyph;
19074
19075 result = 0;
19076 for (glyph = end; glyph >= beg; --glyph)
19077 if (STRINGP (glyph->object))
19078 {
19079 Lisp_Object prop
19080 = Fget_char_property (make_number (charpos),
19081 Qdisplay, Qnil);
19082 result =
19083 (!NILP (prop)
19084 && display_prop_string_p (prop, glyph->object));
19085 /* If there's a `cursor' property on one of the
19086 string's characters, this row is a cursor row,
19087 even though this is not a display string. */
19088 if (!result)
19089 {
19090 Lisp_Object s = glyph->object;
19091
19092 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19093 {
19094 ptrdiff_t gpos = glyph->charpos;
19095
19096 if (!NILP (Fget_char_property (make_number (gpos),
19097 Qcursor, s)))
19098 {
19099 result = 1;
19100 break;
19101 }
19102 }
19103 }
19104 break;
19105 }
19106 }
19107 }
19108 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19109 {
19110 /* If the row ends in middle of a real character,
19111 and the line is continued, we want the cursor here.
19112 That's because CHARPOS (ROW->end.pos) would equal
19113 PT if PT is before the character. */
19114 if (!row->ends_in_ellipsis_p)
19115 result = row->continued_p;
19116 else
19117 /* If the row ends in an ellipsis, then
19118 CHARPOS (ROW->end.pos) will equal point after the
19119 invisible text. We want that position to be displayed
19120 after the ellipsis. */
19121 result = 0;
19122 }
19123 /* If the row ends at ZV, display the cursor at the end of that
19124 row instead of at the start of the row below. */
19125 else if (row->ends_at_zv_p)
19126 result = 1;
19127 else
19128 result = 0;
19129 }
19130
19131 return result;
19132 }
19133
19134 /* Value is non-zero if glyph row ROW should be
19135 used to hold the cursor. */
19136
19137 static int
19138 cursor_row_p (struct glyph_row *row)
19139 {
19140 return row_for_charpos_p (row, PT);
19141 }
19142
19143 \f
19144
19145 /* Push the property PROP so that it will be rendered at the current
19146 position in IT. Return 1 if PROP was successfully pushed, 0
19147 otherwise. Called from handle_line_prefix to handle the
19148 `line-prefix' and `wrap-prefix' properties. */
19149
19150 static int
19151 push_prefix_prop (struct it *it, Lisp_Object prop)
19152 {
19153 struct text_pos pos =
19154 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19155
19156 eassert (it->method == GET_FROM_BUFFER
19157 || it->method == GET_FROM_DISPLAY_VECTOR
19158 || it->method == GET_FROM_STRING);
19159
19160 /* We need to save the current buffer/string position, so it will be
19161 restored by pop_it, because iterate_out_of_display_property
19162 depends on that being set correctly, but some situations leave
19163 it->position not yet set when this function is called. */
19164 push_it (it, &pos);
19165
19166 if (STRINGP (prop))
19167 {
19168 if (SCHARS (prop) == 0)
19169 {
19170 pop_it (it);
19171 return 0;
19172 }
19173
19174 it->string = prop;
19175 it->string_from_prefix_prop_p = 1;
19176 it->multibyte_p = STRING_MULTIBYTE (it->string);
19177 it->current.overlay_string_index = -1;
19178 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19179 it->end_charpos = it->string_nchars = SCHARS (it->string);
19180 it->method = GET_FROM_STRING;
19181 it->stop_charpos = 0;
19182 it->prev_stop = 0;
19183 it->base_level_stop = 0;
19184
19185 /* Force paragraph direction to be that of the parent
19186 buffer/string. */
19187 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19188 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19189 else
19190 it->paragraph_embedding = L2R;
19191
19192 /* Set up the bidi iterator for this display string. */
19193 if (it->bidi_p)
19194 {
19195 it->bidi_it.string.lstring = it->string;
19196 it->bidi_it.string.s = NULL;
19197 it->bidi_it.string.schars = it->end_charpos;
19198 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19199 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19200 it->bidi_it.string.unibyte = !it->multibyte_p;
19201 it->bidi_it.w = it->w;
19202 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19203 }
19204 }
19205 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19206 {
19207 it->method = GET_FROM_STRETCH;
19208 it->object = prop;
19209 }
19210 #ifdef HAVE_WINDOW_SYSTEM
19211 else if (IMAGEP (prop))
19212 {
19213 it->what = IT_IMAGE;
19214 it->image_id = lookup_image (it->f, prop);
19215 it->method = GET_FROM_IMAGE;
19216 }
19217 #endif /* HAVE_WINDOW_SYSTEM */
19218 else
19219 {
19220 pop_it (it); /* bogus display property, give up */
19221 return 0;
19222 }
19223
19224 return 1;
19225 }
19226
19227 /* Return the character-property PROP at the current position in IT. */
19228
19229 static Lisp_Object
19230 get_it_property (struct it *it, Lisp_Object prop)
19231 {
19232 Lisp_Object position, object = it->object;
19233
19234 if (STRINGP (object))
19235 position = make_number (IT_STRING_CHARPOS (*it));
19236 else if (BUFFERP (object))
19237 {
19238 position = make_number (IT_CHARPOS (*it));
19239 object = it->window;
19240 }
19241 else
19242 return Qnil;
19243
19244 return Fget_char_property (position, prop, object);
19245 }
19246
19247 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19248
19249 static void
19250 handle_line_prefix (struct it *it)
19251 {
19252 Lisp_Object prefix;
19253
19254 if (it->continuation_lines_width > 0)
19255 {
19256 prefix = get_it_property (it, Qwrap_prefix);
19257 if (NILP (prefix))
19258 prefix = Vwrap_prefix;
19259 }
19260 else
19261 {
19262 prefix = get_it_property (it, Qline_prefix);
19263 if (NILP (prefix))
19264 prefix = Vline_prefix;
19265 }
19266 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19267 {
19268 /* If the prefix is wider than the window, and we try to wrap
19269 it, it would acquire its own wrap prefix, and so on till the
19270 iterator stack overflows. So, don't wrap the prefix. */
19271 it->line_wrap = TRUNCATE;
19272 it->avoid_cursor_p = 1;
19273 }
19274 }
19275
19276 \f
19277
19278 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19279 only for R2L lines from display_line and display_string, when they
19280 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19281 the line/string needs to be continued on the next glyph row. */
19282 static void
19283 unproduce_glyphs (struct it *it, int n)
19284 {
19285 struct glyph *glyph, *end;
19286
19287 eassert (it->glyph_row);
19288 eassert (it->glyph_row->reversed_p);
19289 eassert (it->area == TEXT_AREA);
19290 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19291
19292 if (n > it->glyph_row->used[TEXT_AREA])
19293 n = it->glyph_row->used[TEXT_AREA];
19294 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19295 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19296 for ( ; glyph < end; glyph++)
19297 glyph[-n] = *glyph;
19298 }
19299
19300 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19301 and ROW->maxpos. */
19302 static void
19303 find_row_edges (struct it *it, struct glyph_row *row,
19304 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19305 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19306 {
19307 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19308 lines' rows is implemented for bidi-reordered rows. */
19309
19310 /* ROW->minpos is the value of min_pos, the minimal buffer position
19311 we have in ROW, or ROW->start.pos if that is smaller. */
19312 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19313 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19314 else
19315 /* We didn't find buffer positions smaller than ROW->start, or
19316 didn't find _any_ valid buffer positions in any of the glyphs,
19317 so we must trust the iterator's computed positions. */
19318 row->minpos = row->start.pos;
19319 if (max_pos <= 0)
19320 {
19321 max_pos = CHARPOS (it->current.pos);
19322 max_bpos = BYTEPOS (it->current.pos);
19323 }
19324
19325 /* Here are the various use-cases for ending the row, and the
19326 corresponding values for ROW->maxpos:
19327
19328 Line ends in a newline from buffer eol_pos + 1
19329 Line is continued from buffer max_pos + 1
19330 Line is truncated on right it->current.pos
19331 Line ends in a newline from string max_pos + 1(*)
19332 (*) + 1 only when line ends in a forward scan
19333 Line is continued from string max_pos
19334 Line is continued from display vector max_pos
19335 Line is entirely from a string min_pos == max_pos
19336 Line is entirely from a display vector min_pos == max_pos
19337 Line that ends at ZV ZV
19338
19339 If you discover other use-cases, please add them here as
19340 appropriate. */
19341 if (row->ends_at_zv_p)
19342 row->maxpos = it->current.pos;
19343 else if (row->used[TEXT_AREA])
19344 {
19345 int seen_this_string = 0;
19346 struct glyph_row *r1 = row - 1;
19347
19348 /* Did we see the same display string on the previous row? */
19349 if (STRINGP (it->object)
19350 /* this is not the first row */
19351 && row > it->w->desired_matrix->rows
19352 /* previous row is not the header line */
19353 && !r1->mode_line_p
19354 /* previous row also ends in a newline from a string */
19355 && r1->ends_in_newline_from_string_p)
19356 {
19357 struct glyph *start, *end;
19358
19359 /* Search for the last glyph of the previous row that came
19360 from buffer or string. Depending on whether the row is
19361 L2R or R2L, we need to process it front to back or the
19362 other way round. */
19363 if (!r1->reversed_p)
19364 {
19365 start = r1->glyphs[TEXT_AREA];
19366 end = start + r1->used[TEXT_AREA];
19367 /* Glyphs inserted by redisplay have an integer (zero)
19368 as their object. */
19369 while (end > start
19370 && INTEGERP ((end - 1)->object)
19371 && (end - 1)->charpos <= 0)
19372 --end;
19373 if (end > start)
19374 {
19375 if (EQ ((end - 1)->object, it->object))
19376 seen_this_string = 1;
19377 }
19378 else
19379 /* If all the glyphs of the previous row were inserted
19380 by redisplay, it means the previous row was
19381 produced from a single newline, which is only
19382 possible if that newline came from the same string
19383 as the one which produced this ROW. */
19384 seen_this_string = 1;
19385 }
19386 else
19387 {
19388 end = r1->glyphs[TEXT_AREA] - 1;
19389 start = end + r1->used[TEXT_AREA];
19390 while (end < start
19391 && INTEGERP ((end + 1)->object)
19392 && (end + 1)->charpos <= 0)
19393 ++end;
19394 if (end < start)
19395 {
19396 if (EQ ((end + 1)->object, it->object))
19397 seen_this_string = 1;
19398 }
19399 else
19400 seen_this_string = 1;
19401 }
19402 }
19403 /* Take note of each display string that covers a newline only
19404 once, the first time we see it. This is for when a display
19405 string includes more than one newline in it. */
19406 if (row->ends_in_newline_from_string_p && !seen_this_string)
19407 {
19408 /* If we were scanning the buffer forward when we displayed
19409 the string, we want to account for at least one buffer
19410 position that belongs to this row (position covered by
19411 the display string), so that cursor positioning will
19412 consider this row as a candidate when point is at the end
19413 of the visual line represented by this row. This is not
19414 required when scanning back, because max_pos will already
19415 have a much larger value. */
19416 if (CHARPOS (row->end.pos) > max_pos)
19417 INC_BOTH (max_pos, max_bpos);
19418 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19419 }
19420 else if (CHARPOS (it->eol_pos) > 0)
19421 SET_TEXT_POS (row->maxpos,
19422 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19423 else if (row->continued_p)
19424 {
19425 /* If max_pos is different from IT's current position, it
19426 means IT->method does not belong to the display element
19427 at max_pos. However, it also means that the display
19428 element at max_pos was displayed in its entirety on this
19429 line, which is equivalent to saying that the next line
19430 starts at the next buffer position. */
19431 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19432 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19433 else
19434 {
19435 INC_BOTH (max_pos, max_bpos);
19436 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19437 }
19438 }
19439 else if (row->truncated_on_right_p)
19440 /* display_line already called reseat_at_next_visible_line_start,
19441 which puts the iterator at the beginning of the next line, in
19442 the logical order. */
19443 row->maxpos = it->current.pos;
19444 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19445 /* A line that is entirely from a string/image/stretch... */
19446 row->maxpos = row->minpos;
19447 else
19448 emacs_abort ();
19449 }
19450 else
19451 row->maxpos = it->current.pos;
19452 }
19453
19454 /* Construct the glyph row IT->glyph_row in the desired matrix of
19455 IT->w from text at the current position of IT. See dispextern.h
19456 for an overview of struct it. Value is non-zero if
19457 IT->glyph_row displays text, as opposed to a line displaying ZV
19458 only. */
19459
19460 static int
19461 display_line (struct it *it)
19462 {
19463 struct glyph_row *row = it->glyph_row;
19464 Lisp_Object overlay_arrow_string;
19465 struct it wrap_it;
19466 void *wrap_data = NULL;
19467 int may_wrap = 0, wrap_x IF_LINT (= 0);
19468 int wrap_row_used = -1;
19469 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19470 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19471 int wrap_row_extra_line_spacing IF_LINT (= 0);
19472 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19473 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19474 int cvpos;
19475 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19476 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19477
19478 /* We always start displaying at hpos zero even if hscrolled. */
19479 eassert (it->hpos == 0 && it->current_x == 0);
19480
19481 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19482 >= it->w->desired_matrix->nrows)
19483 {
19484 it->w->nrows_scale_factor++;
19485 it->f->fonts_changed = 1;
19486 return 0;
19487 }
19488
19489 /* Clear the result glyph row and enable it. */
19490 prepare_desired_row (row);
19491
19492 row->y = it->current_y;
19493 row->start = it->start;
19494 row->continuation_lines_width = it->continuation_lines_width;
19495 row->displays_text_p = 1;
19496 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19497 it->starts_in_middle_of_char_p = 0;
19498
19499 /* Arrange the overlays nicely for our purposes. Usually, we call
19500 display_line on only one line at a time, in which case this
19501 can't really hurt too much, or we call it on lines which appear
19502 one after another in the buffer, in which case all calls to
19503 recenter_overlay_lists but the first will be pretty cheap. */
19504 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19505
19506 /* Move over display elements that are not visible because we are
19507 hscrolled. This may stop at an x-position < IT->first_visible_x
19508 if the first glyph is partially visible or if we hit a line end. */
19509 if (it->current_x < it->first_visible_x)
19510 {
19511 enum move_it_result move_result;
19512
19513 this_line_min_pos = row->start.pos;
19514 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19515 MOVE_TO_POS | MOVE_TO_X);
19516 /* If we are under a large hscroll, move_it_in_display_line_to
19517 could hit the end of the line without reaching
19518 it->first_visible_x. Pretend that we did reach it. This is
19519 especially important on a TTY, where we will call
19520 extend_face_to_end_of_line, which needs to know how many
19521 blank glyphs to produce. */
19522 if (it->current_x < it->first_visible_x
19523 && (move_result == MOVE_NEWLINE_OR_CR
19524 || move_result == MOVE_POS_MATCH_OR_ZV))
19525 it->current_x = it->first_visible_x;
19526
19527 /* Record the smallest positions seen while we moved over
19528 display elements that are not visible. This is needed by
19529 redisplay_internal for optimizing the case where the cursor
19530 stays inside the same line. The rest of this function only
19531 considers positions that are actually displayed, so
19532 RECORD_MAX_MIN_POS will not otherwise record positions that
19533 are hscrolled to the left of the left edge of the window. */
19534 min_pos = CHARPOS (this_line_min_pos);
19535 min_bpos = BYTEPOS (this_line_min_pos);
19536 }
19537 else
19538 {
19539 /* We only do this when not calling `move_it_in_display_line_to'
19540 above, because move_it_in_display_line_to calls
19541 handle_line_prefix itself. */
19542 handle_line_prefix (it);
19543 }
19544
19545 /* Get the initial row height. This is either the height of the
19546 text hscrolled, if there is any, or zero. */
19547 row->ascent = it->max_ascent;
19548 row->height = it->max_ascent + it->max_descent;
19549 row->phys_ascent = it->max_phys_ascent;
19550 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19551 row->extra_line_spacing = it->max_extra_line_spacing;
19552
19553 /* Utility macro to record max and min buffer positions seen until now. */
19554 #define RECORD_MAX_MIN_POS(IT) \
19555 do \
19556 { \
19557 int composition_p = !STRINGP ((IT)->string) \
19558 && ((IT)->what == IT_COMPOSITION); \
19559 ptrdiff_t current_pos = \
19560 composition_p ? (IT)->cmp_it.charpos \
19561 : IT_CHARPOS (*(IT)); \
19562 ptrdiff_t current_bpos = \
19563 composition_p ? CHAR_TO_BYTE (current_pos) \
19564 : IT_BYTEPOS (*(IT)); \
19565 if (current_pos < min_pos) \
19566 { \
19567 min_pos = current_pos; \
19568 min_bpos = current_bpos; \
19569 } \
19570 if (IT_CHARPOS (*it) > max_pos) \
19571 { \
19572 max_pos = IT_CHARPOS (*it); \
19573 max_bpos = IT_BYTEPOS (*it); \
19574 } \
19575 } \
19576 while (0)
19577
19578 /* Loop generating characters. The loop is left with IT on the next
19579 character to display. */
19580 while (1)
19581 {
19582 int n_glyphs_before, hpos_before, x_before;
19583 int x, nglyphs;
19584 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19585
19586 /* Retrieve the next thing to display. Value is zero if end of
19587 buffer reached. */
19588 if (!get_next_display_element (it))
19589 {
19590 /* Maybe add a space at the end of this line that is used to
19591 display the cursor there under X. Set the charpos of the
19592 first glyph of blank lines not corresponding to any text
19593 to -1. */
19594 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19595 row->exact_window_width_line_p = 1;
19596 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19597 || row->used[TEXT_AREA] == 0)
19598 {
19599 row->glyphs[TEXT_AREA]->charpos = -1;
19600 row->displays_text_p = 0;
19601
19602 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19603 && (!MINI_WINDOW_P (it->w)
19604 || (minibuf_level && EQ (it->window, minibuf_window))))
19605 row->indicate_empty_line_p = 1;
19606 }
19607
19608 it->continuation_lines_width = 0;
19609 row->ends_at_zv_p = 1;
19610 /* A row that displays right-to-left text must always have
19611 its last face extended all the way to the end of line,
19612 even if this row ends in ZV, because we still write to
19613 the screen left to right. We also need to extend the
19614 last face if the default face is remapped to some
19615 different face, otherwise the functions that clear
19616 portions of the screen will clear with the default face's
19617 background color. */
19618 if (row->reversed_p
19619 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19620 extend_face_to_end_of_line (it);
19621 break;
19622 }
19623
19624 /* Now, get the metrics of what we want to display. This also
19625 generates glyphs in `row' (which is IT->glyph_row). */
19626 n_glyphs_before = row->used[TEXT_AREA];
19627 x = it->current_x;
19628
19629 /* Remember the line height so far in case the next element doesn't
19630 fit on the line. */
19631 if (it->line_wrap != TRUNCATE)
19632 {
19633 ascent = it->max_ascent;
19634 descent = it->max_descent;
19635 phys_ascent = it->max_phys_ascent;
19636 phys_descent = it->max_phys_descent;
19637
19638 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19639 {
19640 if (IT_DISPLAYING_WHITESPACE (it))
19641 may_wrap = 1;
19642 else if (may_wrap)
19643 {
19644 SAVE_IT (wrap_it, *it, wrap_data);
19645 wrap_x = x;
19646 wrap_row_used = row->used[TEXT_AREA];
19647 wrap_row_ascent = row->ascent;
19648 wrap_row_height = row->height;
19649 wrap_row_phys_ascent = row->phys_ascent;
19650 wrap_row_phys_height = row->phys_height;
19651 wrap_row_extra_line_spacing = row->extra_line_spacing;
19652 wrap_row_min_pos = min_pos;
19653 wrap_row_min_bpos = min_bpos;
19654 wrap_row_max_pos = max_pos;
19655 wrap_row_max_bpos = max_bpos;
19656 may_wrap = 0;
19657 }
19658 }
19659 }
19660
19661 PRODUCE_GLYPHS (it);
19662
19663 /* If this display element was in marginal areas, continue with
19664 the next one. */
19665 if (it->area != TEXT_AREA)
19666 {
19667 row->ascent = max (row->ascent, it->max_ascent);
19668 row->height = max (row->height, it->max_ascent + it->max_descent);
19669 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19670 row->phys_height = max (row->phys_height,
19671 it->max_phys_ascent + it->max_phys_descent);
19672 row->extra_line_spacing = max (row->extra_line_spacing,
19673 it->max_extra_line_spacing);
19674 set_iterator_to_next (it, 1);
19675 continue;
19676 }
19677
19678 /* Does the display element fit on the line? If we truncate
19679 lines, we should draw past the right edge of the window. If
19680 we don't truncate, we want to stop so that we can display the
19681 continuation glyph before the right margin. If lines are
19682 continued, there are two possible strategies for characters
19683 resulting in more than 1 glyph (e.g. tabs): Display as many
19684 glyphs as possible in this line and leave the rest for the
19685 continuation line, or display the whole element in the next
19686 line. Original redisplay did the former, so we do it also. */
19687 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19688 hpos_before = it->hpos;
19689 x_before = x;
19690
19691 if (/* Not a newline. */
19692 nglyphs > 0
19693 /* Glyphs produced fit entirely in the line. */
19694 && it->current_x < it->last_visible_x)
19695 {
19696 it->hpos += nglyphs;
19697 row->ascent = max (row->ascent, it->max_ascent);
19698 row->height = max (row->height, it->max_ascent + it->max_descent);
19699 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19700 row->phys_height = max (row->phys_height,
19701 it->max_phys_ascent + it->max_phys_descent);
19702 row->extra_line_spacing = max (row->extra_line_spacing,
19703 it->max_extra_line_spacing);
19704 if (it->current_x - it->pixel_width < it->first_visible_x)
19705 row->x = x - it->first_visible_x;
19706 /* Record the maximum and minimum buffer positions seen so
19707 far in glyphs that will be displayed by this row. */
19708 if (it->bidi_p)
19709 RECORD_MAX_MIN_POS (it);
19710 }
19711 else
19712 {
19713 int i, new_x;
19714 struct glyph *glyph;
19715
19716 for (i = 0; i < nglyphs; ++i, x = new_x)
19717 {
19718 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19719 new_x = x + glyph->pixel_width;
19720
19721 if (/* Lines are continued. */
19722 it->line_wrap != TRUNCATE
19723 && (/* Glyph doesn't fit on the line. */
19724 new_x > it->last_visible_x
19725 /* Or it fits exactly on a window system frame. */
19726 || (new_x == it->last_visible_x
19727 && FRAME_WINDOW_P (it->f)
19728 && (row->reversed_p
19729 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19730 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19731 {
19732 /* End of a continued line. */
19733
19734 if (it->hpos == 0
19735 || (new_x == it->last_visible_x
19736 && FRAME_WINDOW_P (it->f)
19737 && (row->reversed_p
19738 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19739 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19740 {
19741 /* Current glyph is the only one on the line or
19742 fits exactly on the line. We must continue
19743 the line because we can't draw the cursor
19744 after the glyph. */
19745 row->continued_p = 1;
19746 it->current_x = new_x;
19747 it->continuation_lines_width += new_x;
19748 ++it->hpos;
19749 if (i == nglyphs - 1)
19750 {
19751 /* If line-wrap is on, check if a previous
19752 wrap point was found. */
19753 if (wrap_row_used > 0
19754 /* Even if there is a previous wrap
19755 point, continue the line here as
19756 usual, if (i) the previous character
19757 was a space or tab AND (ii) the
19758 current character is not. */
19759 && (!may_wrap
19760 || IT_DISPLAYING_WHITESPACE (it)))
19761 goto back_to_wrap;
19762
19763 /* Record the maximum and minimum buffer
19764 positions seen so far in glyphs that will be
19765 displayed by this row. */
19766 if (it->bidi_p)
19767 RECORD_MAX_MIN_POS (it);
19768 set_iterator_to_next (it, 1);
19769 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19770 {
19771 if (!get_next_display_element (it))
19772 {
19773 row->exact_window_width_line_p = 1;
19774 it->continuation_lines_width = 0;
19775 row->continued_p = 0;
19776 row->ends_at_zv_p = 1;
19777 }
19778 else if (ITERATOR_AT_END_OF_LINE_P (it))
19779 {
19780 row->continued_p = 0;
19781 row->exact_window_width_line_p = 1;
19782 }
19783 }
19784 }
19785 else if (it->bidi_p)
19786 RECORD_MAX_MIN_POS (it);
19787 }
19788 else if (CHAR_GLYPH_PADDING_P (*glyph)
19789 && !FRAME_WINDOW_P (it->f))
19790 {
19791 /* A padding glyph that doesn't fit on this line.
19792 This means the whole character doesn't fit
19793 on the line. */
19794 if (row->reversed_p)
19795 unproduce_glyphs (it, row->used[TEXT_AREA]
19796 - n_glyphs_before);
19797 row->used[TEXT_AREA] = n_glyphs_before;
19798
19799 /* Fill the rest of the row with continuation
19800 glyphs like in 20.x. */
19801 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19802 < row->glyphs[1 + TEXT_AREA])
19803 produce_special_glyphs (it, IT_CONTINUATION);
19804
19805 row->continued_p = 1;
19806 it->current_x = x_before;
19807 it->continuation_lines_width += x_before;
19808
19809 /* Restore the height to what it was before the
19810 element not fitting on the line. */
19811 it->max_ascent = ascent;
19812 it->max_descent = descent;
19813 it->max_phys_ascent = phys_ascent;
19814 it->max_phys_descent = phys_descent;
19815 }
19816 else if (wrap_row_used > 0)
19817 {
19818 back_to_wrap:
19819 if (row->reversed_p)
19820 unproduce_glyphs (it,
19821 row->used[TEXT_AREA] - wrap_row_used);
19822 RESTORE_IT (it, &wrap_it, wrap_data);
19823 it->continuation_lines_width += wrap_x;
19824 row->used[TEXT_AREA] = wrap_row_used;
19825 row->ascent = wrap_row_ascent;
19826 row->height = wrap_row_height;
19827 row->phys_ascent = wrap_row_phys_ascent;
19828 row->phys_height = wrap_row_phys_height;
19829 row->extra_line_spacing = wrap_row_extra_line_spacing;
19830 min_pos = wrap_row_min_pos;
19831 min_bpos = wrap_row_min_bpos;
19832 max_pos = wrap_row_max_pos;
19833 max_bpos = wrap_row_max_bpos;
19834 row->continued_p = 1;
19835 row->ends_at_zv_p = 0;
19836 row->exact_window_width_line_p = 0;
19837 it->continuation_lines_width += x;
19838
19839 /* Make sure that a non-default face is extended
19840 up to the right margin of the window. */
19841 extend_face_to_end_of_line (it);
19842 }
19843 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19844 {
19845 /* A TAB that extends past the right edge of the
19846 window. This produces a single glyph on
19847 window system frames. We leave the glyph in
19848 this row and let it fill the row, but don't
19849 consume the TAB. */
19850 if ((row->reversed_p
19851 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19852 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19853 produce_special_glyphs (it, IT_CONTINUATION);
19854 it->continuation_lines_width += it->last_visible_x;
19855 row->ends_in_middle_of_char_p = 1;
19856 row->continued_p = 1;
19857 glyph->pixel_width = it->last_visible_x - x;
19858 it->starts_in_middle_of_char_p = 1;
19859 }
19860 else
19861 {
19862 /* Something other than a TAB that draws past
19863 the right edge of the window. Restore
19864 positions to values before the element. */
19865 if (row->reversed_p)
19866 unproduce_glyphs (it, row->used[TEXT_AREA]
19867 - (n_glyphs_before + i));
19868 row->used[TEXT_AREA] = n_glyphs_before + i;
19869
19870 /* Display continuation glyphs. */
19871 it->current_x = x_before;
19872 it->continuation_lines_width += x;
19873 if (!FRAME_WINDOW_P (it->f)
19874 || (row->reversed_p
19875 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19876 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19877 produce_special_glyphs (it, IT_CONTINUATION);
19878 row->continued_p = 1;
19879
19880 extend_face_to_end_of_line (it);
19881
19882 if (nglyphs > 1 && i > 0)
19883 {
19884 row->ends_in_middle_of_char_p = 1;
19885 it->starts_in_middle_of_char_p = 1;
19886 }
19887
19888 /* Restore the height to what it was before the
19889 element not fitting on the line. */
19890 it->max_ascent = ascent;
19891 it->max_descent = descent;
19892 it->max_phys_ascent = phys_ascent;
19893 it->max_phys_descent = phys_descent;
19894 }
19895
19896 break;
19897 }
19898 else if (new_x > it->first_visible_x)
19899 {
19900 /* Increment number of glyphs actually displayed. */
19901 ++it->hpos;
19902
19903 /* Record the maximum and minimum buffer positions
19904 seen so far in glyphs that will be displayed by
19905 this row. */
19906 if (it->bidi_p)
19907 RECORD_MAX_MIN_POS (it);
19908
19909 if (x < it->first_visible_x)
19910 /* Glyph is partially visible, i.e. row starts at
19911 negative X position. */
19912 row->x = x - it->first_visible_x;
19913 }
19914 else
19915 {
19916 /* Glyph is completely off the left margin of the
19917 window. This should not happen because of the
19918 move_it_in_display_line at the start of this
19919 function, unless the text display area of the
19920 window is empty. */
19921 eassert (it->first_visible_x <= it->last_visible_x);
19922 }
19923 }
19924 /* Even if this display element produced no glyphs at all,
19925 we want to record its position. */
19926 if (it->bidi_p && nglyphs == 0)
19927 RECORD_MAX_MIN_POS (it);
19928
19929 row->ascent = max (row->ascent, it->max_ascent);
19930 row->height = max (row->height, it->max_ascent + it->max_descent);
19931 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19932 row->phys_height = max (row->phys_height,
19933 it->max_phys_ascent + it->max_phys_descent);
19934 row->extra_line_spacing = max (row->extra_line_spacing,
19935 it->max_extra_line_spacing);
19936
19937 /* End of this display line if row is continued. */
19938 if (row->continued_p || row->ends_at_zv_p)
19939 break;
19940 }
19941
19942 at_end_of_line:
19943 /* Is this a line end? If yes, we're also done, after making
19944 sure that a non-default face is extended up to the right
19945 margin of the window. */
19946 if (ITERATOR_AT_END_OF_LINE_P (it))
19947 {
19948 int used_before = row->used[TEXT_AREA];
19949
19950 row->ends_in_newline_from_string_p = STRINGP (it->object);
19951
19952 /* Add a space at the end of the line that is used to
19953 display the cursor there. */
19954 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19955 append_space_for_newline (it, 0);
19956
19957 /* Extend the face to the end of the line. */
19958 extend_face_to_end_of_line (it);
19959
19960 /* Make sure we have the position. */
19961 if (used_before == 0)
19962 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19963
19964 /* Record the position of the newline, for use in
19965 find_row_edges. */
19966 it->eol_pos = it->current.pos;
19967
19968 /* Consume the line end. This skips over invisible lines. */
19969 set_iterator_to_next (it, 1);
19970 it->continuation_lines_width = 0;
19971 break;
19972 }
19973
19974 /* Proceed with next display element. Note that this skips
19975 over lines invisible because of selective display. */
19976 set_iterator_to_next (it, 1);
19977
19978 /* If we truncate lines, we are done when the last displayed
19979 glyphs reach past the right margin of the window. */
19980 if (it->line_wrap == TRUNCATE
19981 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19982 ? (it->current_x >= it->last_visible_x)
19983 : (it->current_x > it->last_visible_x)))
19984 {
19985 /* Maybe add truncation glyphs. */
19986 if (!FRAME_WINDOW_P (it->f)
19987 || (row->reversed_p
19988 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19989 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19990 {
19991 int i, n;
19992
19993 if (!row->reversed_p)
19994 {
19995 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19996 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19997 break;
19998 }
19999 else
20000 {
20001 for (i = 0; i < row->used[TEXT_AREA]; i++)
20002 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20003 break;
20004 /* Remove any padding glyphs at the front of ROW, to
20005 make room for the truncation glyphs we will be
20006 adding below. The loop below always inserts at
20007 least one truncation glyph, so also remove the
20008 last glyph added to ROW. */
20009 unproduce_glyphs (it, i + 1);
20010 /* Adjust i for the loop below. */
20011 i = row->used[TEXT_AREA] - (i + 1);
20012 }
20013
20014 it->current_x = x_before;
20015 if (!FRAME_WINDOW_P (it->f))
20016 {
20017 for (n = row->used[TEXT_AREA]; i < n; ++i)
20018 {
20019 row->used[TEXT_AREA] = i;
20020 produce_special_glyphs (it, IT_TRUNCATION);
20021 }
20022 }
20023 else
20024 {
20025 row->used[TEXT_AREA] = i;
20026 produce_special_glyphs (it, IT_TRUNCATION);
20027 }
20028 }
20029 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20030 {
20031 /* Don't truncate if we can overflow newline into fringe. */
20032 if (!get_next_display_element (it))
20033 {
20034 it->continuation_lines_width = 0;
20035 row->ends_at_zv_p = 1;
20036 row->exact_window_width_line_p = 1;
20037 break;
20038 }
20039 if (ITERATOR_AT_END_OF_LINE_P (it))
20040 {
20041 row->exact_window_width_line_p = 1;
20042 goto at_end_of_line;
20043 }
20044 it->current_x = x_before;
20045 }
20046
20047 row->truncated_on_right_p = 1;
20048 it->continuation_lines_width = 0;
20049 reseat_at_next_visible_line_start (it, 0);
20050 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20051 it->hpos = hpos_before;
20052 break;
20053 }
20054 }
20055
20056 if (wrap_data)
20057 bidi_unshelve_cache (wrap_data, 1);
20058
20059 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20060 at the left window margin. */
20061 if (it->first_visible_x
20062 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20063 {
20064 if (!FRAME_WINDOW_P (it->f)
20065 || (row->reversed_p
20066 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20067 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20068 insert_left_trunc_glyphs (it);
20069 row->truncated_on_left_p = 1;
20070 }
20071
20072 /* Remember the position at which this line ends.
20073
20074 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20075 cannot be before the call to find_row_edges below, since that is
20076 where these positions are determined. */
20077 row->end = it->current;
20078 if (!it->bidi_p)
20079 {
20080 row->minpos = row->start.pos;
20081 row->maxpos = row->end.pos;
20082 }
20083 else
20084 {
20085 /* ROW->minpos and ROW->maxpos must be the smallest and
20086 `1 + the largest' buffer positions in ROW. But if ROW was
20087 bidi-reordered, these two positions can be anywhere in the
20088 row, so we must determine them now. */
20089 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20090 }
20091
20092 /* If the start of this line is the overlay arrow-position, then
20093 mark this glyph row as the one containing the overlay arrow.
20094 This is clearly a mess with variable size fonts. It would be
20095 better to let it be displayed like cursors under X. */
20096 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20097 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20098 !NILP (overlay_arrow_string)))
20099 {
20100 /* Overlay arrow in window redisplay is a fringe bitmap. */
20101 if (STRINGP (overlay_arrow_string))
20102 {
20103 struct glyph_row *arrow_row
20104 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20105 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20106 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20107 struct glyph *p = row->glyphs[TEXT_AREA];
20108 struct glyph *p2, *end;
20109
20110 /* Copy the arrow glyphs. */
20111 while (glyph < arrow_end)
20112 *p++ = *glyph++;
20113
20114 /* Throw away padding glyphs. */
20115 p2 = p;
20116 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20117 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20118 ++p2;
20119 if (p2 > p)
20120 {
20121 while (p2 < end)
20122 *p++ = *p2++;
20123 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20124 }
20125 }
20126 else
20127 {
20128 eassert (INTEGERP (overlay_arrow_string));
20129 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20130 }
20131 overlay_arrow_seen = 1;
20132 }
20133
20134 /* Highlight trailing whitespace. */
20135 if (!NILP (Vshow_trailing_whitespace))
20136 highlight_trailing_whitespace (it->f, it->glyph_row);
20137
20138 /* Compute pixel dimensions of this line. */
20139 compute_line_metrics (it);
20140
20141 /* Implementation note: No changes in the glyphs of ROW or in their
20142 faces can be done past this point, because compute_line_metrics
20143 computes ROW's hash value and stores it within the glyph_row
20144 structure. */
20145
20146 /* Record whether this row ends inside an ellipsis. */
20147 row->ends_in_ellipsis_p
20148 = (it->method == GET_FROM_DISPLAY_VECTOR
20149 && it->ellipsis_p);
20150
20151 /* Save fringe bitmaps in this row. */
20152 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20153 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20154 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20155 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20156
20157 it->left_user_fringe_bitmap = 0;
20158 it->left_user_fringe_face_id = 0;
20159 it->right_user_fringe_bitmap = 0;
20160 it->right_user_fringe_face_id = 0;
20161
20162 /* Maybe set the cursor. */
20163 cvpos = it->w->cursor.vpos;
20164 if ((cvpos < 0
20165 /* In bidi-reordered rows, keep checking for proper cursor
20166 position even if one has been found already, because buffer
20167 positions in such rows change non-linearly with ROW->VPOS,
20168 when a line is continued. One exception: when we are at ZV,
20169 display cursor on the first suitable glyph row, since all
20170 the empty rows after that also have their position set to ZV. */
20171 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20172 lines' rows is implemented for bidi-reordered rows. */
20173 || (it->bidi_p
20174 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20175 && PT >= MATRIX_ROW_START_CHARPOS (row)
20176 && PT <= MATRIX_ROW_END_CHARPOS (row)
20177 && cursor_row_p (row))
20178 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20179
20180 /* Prepare for the next line. This line starts horizontally at (X
20181 HPOS) = (0 0). Vertical positions are incremented. As a
20182 convenience for the caller, IT->glyph_row is set to the next
20183 row to be used. */
20184 it->current_x = it->hpos = 0;
20185 it->current_y += row->height;
20186 SET_TEXT_POS (it->eol_pos, 0, 0);
20187 ++it->vpos;
20188 ++it->glyph_row;
20189 /* The next row should by default use the same value of the
20190 reversed_p flag as this one. set_iterator_to_next decides when
20191 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20192 the flag accordingly. */
20193 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20194 it->glyph_row->reversed_p = row->reversed_p;
20195 it->start = row->end;
20196 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20197
20198 #undef RECORD_MAX_MIN_POS
20199 }
20200
20201 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20202 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20203 doc: /* Return paragraph direction at point in BUFFER.
20204 Value is either `left-to-right' or `right-to-left'.
20205 If BUFFER is omitted or nil, it defaults to the current buffer.
20206
20207 Paragraph direction determines how the text in the paragraph is displayed.
20208 In left-to-right paragraphs, text begins at the left margin of the window
20209 and the reading direction is generally left to right. In right-to-left
20210 paragraphs, text begins at the right margin and is read from right to left.
20211
20212 See also `bidi-paragraph-direction'. */)
20213 (Lisp_Object buffer)
20214 {
20215 struct buffer *buf = current_buffer;
20216 struct buffer *old = buf;
20217
20218 if (! NILP (buffer))
20219 {
20220 CHECK_BUFFER (buffer);
20221 buf = XBUFFER (buffer);
20222 }
20223
20224 if (NILP (BVAR (buf, bidi_display_reordering))
20225 || NILP (BVAR (buf, enable_multibyte_characters))
20226 /* When we are loading loadup.el, the character property tables
20227 needed for bidi iteration are not yet available. */
20228 || !NILP (Vpurify_flag))
20229 return Qleft_to_right;
20230 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20231 return BVAR (buf, bidi_paragraph_direction);
20232 else
20233 {
20234 /* Determine the direction from buffer text. We could try to
20235 use current_matrix if it is up to date, but this seems fast
20236 enough as it is. */
20237 struct bidi_it itb;
20238 ptrdiff_t pos = BUF_PT (buf);
20239 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20240 int c;
20241 void *itb_data = bidi_shelve_cache ();
20242
20243 set_buffer_temp (buf);
20244 /* bidi_paragraph_init finds the base direction of the paragraph
20245 by searching forward from paragraph start. We need the base
20246 direction of the current or _previous_ paragraph, so we need
20247 to make sure we are within that paragraph. To that end, find
20248 the previous non-empty line. */
20249 if (pos >= ZV && pos > BEGV)
20250 DEC_BOTH (pos, bytepos);
20251 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20252 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20253 {
20254 while ((c = FETCH_BYTE (bytepos)) == '\n'
20255 || c == ' ' || c == '\t' || c == '\f')
20256 {
20257 if (bytepos <= BEGV_BYTE)
20258 break;
20259 bytepos--;
20260 pos--;
20261 }
20262 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20263 bytepos--;
20264 }
20265 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20266 itb.paragraph_dir = NEUTRAL_DIR;
20267 itb.string.s = NULL;
20268 itb.string.lstring = Qnil;
20269 itb.string.bufpos = 0;
20270 itb.string.unibyte = 0;
20271 /* We have no window to use here for ignoring window-specific
20272 overlays. Using NULL for window pointer will cause
20273 compute_display_string_pos to use the current buffer. */
20274 itb.w = NULL;
20275 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20276 bidi_unshelve_cache (itb_data, 0);
20277 set_buffer_temp (old);
20278 switch (itb.paragraph_dir)
20279 {
20280 case L2R:
20281 return Qleft_to_right;
20282 break;
20283 case R2L:
20284 return Qright_to_left;
20285 break;
20286 default:
20287 emacs_abort ();
20288 }
20289 }
20290 }
20291
20292 DEFUN ("move-point-visually", Fmove_point_visually,
20293 Smove_point_visually, 1, 1, 0,
20294 doc: /* Move point in the visual order in the specified DIRECTION.
20295 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20296 left.
20297
20298 Value is the new character position of point. */)
20299 (Lisp_Object direction)
20300 {
20301 struct window *w = XWINDOW (selected_window);
20302 struct buffer *b = XBUFFER (w->contents);
20303 struct glyph_row *row;
20304 int dir;
20305 Lisp_Object paragraph_dir;
20306
20307 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20308 (!(ROW)->continued_p \
20309 && INTEGERP ((GLYPH)->object) \
20310 && (GLYPH)->type == CHAR_GLYPH \
20311 && (GLYPH)->u.ch == ' ' \
20312 && (GLYPH)->charpos >= 0 \
20313 && !(GLYPH)->avoid_cursor_p)
20314
20315 CHECK_NUMBER (direction);
20316 dir = XINT (direction);
20317 if (dir > 0)
20318 dir = 1;
20319 else
20320 dir = -1;
20321
20322 /* If current matrix is up-to-date, we can use the information
20323 recorded in the glyphs, at least as long as the goal is on the
20324 screen. */
20325 if (w->window_end_valid
20326 && !windows_or_buffers_changed
20327 && b
20328 && !b->clip_changed
20329 && !b->prevent_redisplay_optimizations_p
20330 && !window_outdated (w)
20331 && w->cursor.vpos >= 0
20332 && w->cursor.vpos < w->current_matrix->nrows
20333 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20334 {
20335 struct glyph *g = row->glyphs[TEXT_AREA];
20336 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20337 struct glyph *gpt = g + w->cursor.hpos;
20338
20339 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20340 {
20341 if (BUFFERP (g->object) && g->charpos != PT)
20342 {
20343 SET_PT (g->charpos);
20344 w->cursor.vpos = -1;
20345 return make_number (PT);
20346 }
20347 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20348 {
20349 ptrdiff_t new_pos;
20350
20351 if (BUFFERP (gpt->object))
20352 {
20353 new_pos = PT;
20354 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20355 new_pos += (row->reversed_p ? -dir : dir);
20356 else
20357 new_pos -= (row->reversed_p ? -dir : dir);;
20358 }
20359 else if (BUFFERP (g->object))
20360 new_pos = g->charpos;
20361 else
20362 break;
20363 SET_PT (new_pos);
20364 w->cursor.vpos = -1;
20365 return make_number (PT);
20366 }
20367 else if (ROW_GLYPH_NEWLINE_P (row, g))
20368 {
20369 /* Glyphs inserted at the end of a non-empty line for
20370 positioning the cursor have zero charpos, so we must
20371 deduce the value of point by other means. */
20372 if (g->charpos > 0)
20373 SET_PT (g->charpos);
20374 else if (row->ends_at_zv_p && PT != ZV)
20375 SET_PT (ZV);
20376 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20377 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20378 else
20379 break;
20380 w->cursor.vpos = -1;
20381 return make_number (PT);
20382 }
20383 }
20384 if (g == e || INTEGERP (g->object))
20385 {
20386 if (row->truncated_on_left_p || row->truncated_on_right_p)
20387 goto simulate_display;
20388 if (!row->reversed_p)
20389 row += dir;
20390 else
20391 row -= dir;
20392 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20393 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20394 goto simulate_display;
20395
20396 if (dir > 0)
20397 {
20398 if (row->reversed_p && !row->continued_p)
20399 {
20400 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20401 w->cursor.vpos = -1;
20402 return make_number (PT);
20403 }
20404 g = row->glyphs[TEXT_AREA];
20405 e = g + row->used[TEXT_AREA];
20406 for ( ; g < e; g++)
20407 {
20408 if (BUFFERP (g->object)
20409 /* Empty lines have only one glyph, which stands
20410 for the newline, and whose charpos is the
20411 buffer position of the newline. */
20412 || ROW_GLYPH_NEWLINE_P (row, g)
20413 /* When the buffer ends in a newline, the line at
20414 EOB also has one glyph, but its charpos is -1. */
20415 || (row->ends_at_zv_p
20416 && !row->reversed_p
20417 && INTEGERP (g->object)
20418 && g->type == CHAR_GLYPH
20419 && g->u.ch == ' '))
20420 {
20421 if (g->charpos > 0)
20422 SET_PT (g->charpos);
20423 else if (!row->reversed_p
20424 && row->ends_at_zv_p
20425 && PT != ZV)
20426 SET_PT (ZV);
20427 else
20428 continue;
20429 w->cursor.vpos = -1;
20430 return make_number (PT);
20431 }
20432 }
20433 }
20434 else
20435 {
20436 if (!row->reversed_p && !row->continued_p)
20437 {
20438 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20439 w->cursor.vpos = -1;
20440 return make_number (PT);
20441 }
20442 e = row->glyphs[TEXT_AREA];
20443 g = e + row->used[TEXT_AREA] - 1;
20444 for ( ; g >= e; g--)
20445 {
20446 if (BUFFERP (g->object)
20447 || (ROW_GLYPH_NEWLINE_P (row, g)
20448 && g->charpos > 0)
20449 /* Empty R2L lines on GUI frames have the buffer
20450 position of the newline stored in the stretch
20451 glyph. */
20452 || g->type == STRETCH_GLYPH
20453 || (row->ends_at_zv_p
20454 && row->reversed_p
20455 && INTEGERP (g->object)
20456 && g->type == CHAR_GLYPH
20457 && g->u.ch == ' '))
20458 {
20459 if (g->charpos > 0)
20460 SET_PT (g->charpos);
20461 else if (row->reversed_p
20462 && row->ends_at_zv_p
20463 && PT != ZV)
20464 SET_PT (ZV);
20465 else
20466 continue;
20467 w->cursor.vpos = -1;
20468 return make_number (PT);
20469 }
20470 }
20471 }
20472 }
20473 }
20474
20475 simulate_display:
20476
20477 /* If we wind up here, we failed to move by using the glyphs, so we
20478 need to simulate display instead. */
20479
20480 if (b)
20481 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20482 else
20483 paragraph_dir = Qleft_to_right;
20484 if (EQ (paragraph_dir, Qright_to_left))
20485 dir = -dir;
20486 if (PT <= BEGV && dir < 0)
20487 xsignal0 (Qbeginning_of_buffer);
20488 else if (PT >= ZV && dir > 0)
20489 xsignal0 (Qend_of_buffer);
20490 else
20491 {
20492 struct text_pos pt;
20493 struct it it;
20494 int pt_x, target_x, pixel_width, pt_vpos;
20495 bool at_eol_p;
20496 bool overshoot_expected = false;
20497 bool target_is_eol_p = false;
20498
20499 /* Setup the arena. */
20500 SET_TEXT_POS (pt, PT, PT_BYTE);
20501 start_display (&it, w, pt);
20502
20503 if (it.cmp_it.id < 0
20504 && it.method == GET_FROM_STRING
20505 && it.area == TEXT_AREA
20506 && it.string_from_display_prop_p
20507 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20508 overshoot_expected = true;
20509
20510 /* Find the X coordinate of point. We start from the beginning
20511 of this or previous line to make sure we are before point in
20512 the logical order (since the move_it_* functions can only
20513 move forward). */
20514 reseat_at_previous_visible_line_start (&it);
20515 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20516 if (IT_CHARPOS (it) != PT)
20517 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20518 -1, -1, -1, MOVE_TO_POS);
20519 pt_x = it.current_x;
20520 pt_vpos = it.vpos;
20521 if (dir > 0 || overshoot_expected)
20522 {
20523 struct glyph_row *row = it.glyph_row;
20524
20525 /* When point is at beginning of line, we don't have
20526 information about the glyph there loaded into struct
20527 it. Calling get_next_display_element fixes that. */
20528 if (pt_x == 0)
20529 get_next_display_element (&it);
20530 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20531 it.glyph_row = NULL;
20532 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20533 it.glyph_row = row;
20534 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20535 it, lest it will become out of sync with it's buffer
20536 position. */
20537 it.current_x = pt_x;
20538 }
20539 else
20540 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20541 pixel_width = it.pixel_width;
20542 if (overshoot_expected && at_eol_p)
20543 pixel_width = 0;
20544 else if (pixel_width <= 0)
20545 pixel_width = 1;
20546
20547 /* If there's a display string at point, we are actually at the
20548 glyph to the left of point, so we need to correct the X
20549 coordinate. */
20550 if (overshoot_expected)
20551 pt_x += pixel_width;
20552
20553 /* Compute target X coordinate, either to the left or to the
20554 right of point. On TTY frames, all characters have the same
20555 pixel width of 1, so we can use that. On GUI frames we don't
20556 have an easy way of getting at the pixel width of the
20557 character to the left of point, so we use a different method
20558 of getting to that place. */
20559 if (dir > 0)
20560 target_x = pt_x + pixel_width;
20561 else
20562 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20563
20564 /* Target X coordinate could be one line above or below the line
20565 of point, in which case we need to adjust the target X
20566 coordinate. Also, if moving to the left, we need to begin at
20567 the left edge of the point's screen line. */
20568 if (dir < 0)
20569 {
20570 if (pt_x > 0)
20571 {
20572 start_display (&it, w, pt);
20573 reseat_at_previous_visible_line_start (&it);
20574 it.current_x = it.current_y = it.hpos = 0;
20575 if (pt_vpos != 0)
20576 move_it_by_lines (&it, pt_vpos);
20577 }
20578 else
20579 {
20580 move_it_by_lines (&it, -1);
20581 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20582 target_is_eol_p = true;
20583 }
20584 }
20585 else
20586 {
20587 if (at_eol_p
20588 || (target_x >= it.last_visible_x
20589 && it.line_wrap != TRUNCATE))
20590 {
20591 if (pt_x > 0)
20592 move_it_by_lines (&it, 0);
20593 move_it_by_lines (&it, 1);
20594 target_x = 0;
20595 }
20596 }
20597
20598 /* Move to the target X coordinate. */
20599 #ifdef HAVE_WINDOW_SYSTEM
20600 /* On GUI frames, as we don't know the X coordinate of the
20601 character to the left of point, moving point to the left
20602 requires walking, one grapheme cluster at a time, until we
20603 find ourself at a place immediately to the left of the
20604 character at point. */
20605 if (FRAME_WINDOW_P (it.f) && dir < 0)
20606 {
20607 struct text_pos new_pos = it.current.pos;
20608 enum move_it_result rc = MOVE_X_REACHED;
20609
20610 while (it.current_x + it.pixel_width <= target_x
20611 && rc == MOVE_X_REACHED)
20612 {
20613 int new_x = it.current_x + it.pixel_width;
20614
20615 new_pos = it.current.pos;
20616 if (new_x == it.current_x)
20617 new_x++;
20618 rc = move_it_in_display_line_to (&it, ZV, new_x,
20619 MOVE_TO_POS | MOVE_TO_X);
20620 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20621 break;
20622 }
20623 /* If we ended up on a composed character inside
20624 bidi-reordered text (e.g., Hebrew text with diacritics),
20625 the iterator gives us the buffer position of the last (in
20626 logical order) character of the composed grapheme cluster,
20627 which is not what we want. So we cheat: we compute the
20628 character position of the character that follows (in the
20629 logical order) the one where the above loop stopped. That
20630 character will appear on display to the left of point. */
20631 if (it.bidi_p
20632 && it.bidi_it.scan_dir == -1
20633 && new_pos.charpos - IT_CHARPOS (it) > 1)
20634 {
20635 new_pos.charpos = IT_CHARPOS (it) + 1;
20636 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20637 }
20638 it.current.pos = new_pos;
20639 }
20640 else
20641 #endif
20642 if (it.current_x != target_x)
20643 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20644
20645 /* When lines are truncated, the above loop will stop at the
20646 window edge. But we want to get to the end of line, even if
20647 it is beyond the window edge; automatic hscroll will then
20648 scroll the window to show point as appropriate. */
20649 if (target_is_eol_p && it.line_wrap == TRUNCATE
20650 && get_next_display_element (&it))
20651 {
20652 struct text_pos new_pos = it.current.pos;
20653
20654 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20655 {
20656 set_iterator_to_next (&it, 0);
20657 if (it.method == GET_FROM_BUFFER)
20658 new_pos = it.current.pos;
20659 if (!get_next_display_element (&it))
20660 break;
20661 }
20662
20663 it.current.pos = new_pos;
20664 }
20665
20666 /* If we ended up in a display string that covers point, move to
20667 buffer position to the right in the visual order. */
20668 if (dir > 0)
20669 {
20670 while (IT_CHARPOS (it) == PT)
20671 {
20672 set_iterator_to_next (&it, 0);
20673 if (!get_next_display_element (&it))
20674 break;
20675 }
20676 }
20677
20678 /* Move point to that position. */
20679 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20680 }
20681
20682 return make_number (PT);
20683
20684 #undef ROW_GLYPH_NEWLINE_P
20685 }
20686
20687 \f
20688 /***********************************************************************
20689 Menu Bar
20690 ***********************************************************************/
20691
20692 /* Redisplay the menu bar in the frame for window W.
20693
20694 The menu bar of X frames that don't have X toolkit support is
20695 displayed in a special window W->frame->menu_bar_window.
20696
20697 The menu bar of terminal frames is treated specially as far as
20698 glyph matrices are concerned. Menu bar lines are not part of
20699 windows, so the update is done directly on the frame matrix rows
20700 for the menu bar. */
20701
20702 static void
20703 display_menu_bar (struct window *w)
20704 {
20705 struct frame *f = XFRAME (WINDOW_FRAME (w));
20706 struct it it;
20707 Lisp_Object items;
20708 int i;
20709
20710 /* Don't do all this for graphical frames. */
20711 #ifdef HAVE_NTGUI
20712 if (FRAME_W32_P (f))
20713 return;
20714 #endif
20715 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20716 if (FRAME_X_P (f))
20717 return;
20718 #endif
20719
20720 #ifdef HAVE_NS
20721 if (FRAME_NS_P (f))
20722 return;
20723 #endif /* HAVE_NS */
20724
20725 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20726 eassert (!FRAME_WINDOW_P (f));
20727 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20728 it.first_visible_x = 0;
20729 /* PXW: Use FRAME_PIXEL_WIDTH (f) here ? */
20730 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20731 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20732 if (FRAME_WINDOW_P (f))
20733 {
20734 /* Menu bar lines are displayed in the desired matrix of the
20735 dummy window menu_bar_window. */
20736 struct window *menu_w;
20737 menu_w = XWINDOW (f->menu_bar_window);
20738 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20739 MENU_FACE_ID);
20740 it.first_visible_x = 0;
20741 /* PXW: Use FRAME_PIXEL_WIDTH (f) here ? */
20742 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20743 }
20744 else
20745 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20746 {
20747 /* This is a TTY frame, i.e. character hpos/vpos are used as
20748 pixel x/y. */
20749 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20750 MENU_FACE_ID);
20751 it.first_visible_x = 0;
20752 it.last_visible_x = FRAME_COLS (f);
20753 }
20754
20755 /* FIXME: This should be controlled by a user option. See the
20756 comments in redisplay_tool_bar and display_mode_line about
20757 this. */
20758 it.paragraph_embedding = L2R;
20759
20760 /* Clear all rows of the menu bar. */
20761 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20762 {
20763 struct glyph_row *row = it.glyph_row + i;
20764 clear_glyph_row (row);
20765 row->enabled_p = 1;
20766 row->full_width_p = 1;
20767 }
20768
20769 /* Display all items of the menu bar. */
20770 items = FRAME_MENU_BAR_ITEMS (it.f);
20771 for (i = 0; i < ASIZE (items); i += 4)
20772 {
20773 Lisp_Object string;
20774
20775 /* Stop at nil string. */
20776 string = AREF (items, i + 1);
20777 if (NILP (string))
20778 break;
20779
20780 /* Remember where item was displayed. */
20781 ASET (items, i + 3, make_number (it.hpos));
20782
20783 /* Display the item, pad with one space. */
20784 if (it.current_x < it.last_visible_x)
20785 display_string (NULL, string, Qnil, 0, 0, &it,
20786 SCHARS (string) + 1, 0, 0, -1);
20787 }
20788
20789 /* Fill out the line with spaces. */
20790 if (it.current_x < it.last_visible_x)
20791 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20792
20793 /* Compute the total height of the lines. */
20794 compute_line_metrics (&it);
20795 }
20796
20797 /* Deep copy of a glyph row, including the glyphs. */
20798 static void
20799 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
20800 {
20801 struct glyph *pointers[1 + LAST_AREA];
20802 int to_used = to->used[TEXT_AREA];
20803
20804 /* Save glyph pointers of TO. */
20805 memcpy (pointers, to->glyphs, sizeof to->glyphs);
20806
20807 /* Do a structure assignment. */
20808 *to = *from;
20809
20810 /* Restore original glyph pointers of TO. */
20811 memcpy (to->glyphs, pointers, sizeof to->glyphs);
20812
20813 /* Copy the glyphs. */
20814 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
20815 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
20816
20817 /* If we filled only part of the TO row, fill the rest with
20818 space_glyph (which will display as empty space). */
20819 if (to_used > from->used[TEXT_AREA])
20820 fill_up_frame_row_with_spaces (to, to_used);
20821 }
20822
20823 /* Display one menu item on a TTY, by overwriting the glyphs in the
20824 frame F's desired glyph matrix with glyphs produced from the menu
20825 item text. Called from term.c to display TTY drop-down menus one
20826 item at a time.
20827
20828 ITEM_TEXT is the menu item text as a C string.
20829
20830 FACE_ID is the face ID to be used for this menu item. FACE_ID
20831 could specify one of 3 faces: a face for an enabled item, a face
20832 for a disabled item, or a face for a selected item.
20833
20834 X and Y are coordinates of the first glyph in the frame's desired
20835 matrix to be overwritten by the menu item. Since this is a TTY, Y
20836 is the zero-based number of the glyph row and X is the zero-based
20837 glyph number in the row, starting from left, where to start
20838 displaying the item.
20839
20840 SUBMENU non-zero means this menu item drops down a submenu, which
20841 should be indicated by displaying a proper visual cue after the
20842 item text. */
20843
20844 void
20845 display_tty_menu_item (const char *item_text, int width, int face_id,
20846 int x, int y, int submenu)
20847 {
20848 struct it it;
20849 struct frame *f = SELECTED_FRAME ();
20850 struct window *w = XWINDOW (f->selected_window);
20851 int saved_used, saved_truncated, saved_width, saved_reversed;
20852 struct glyph_row *row;
20853 size_t item_len = strlen (item_text);
20854
20855 eassert (FRAME_TERMCAP_P (f));
20856
20857 /* Don't write beyond the matrix's last row. This can happen for
20858 TTY screens that are not high enough to show the entire menu.
20859 (This is actually a bit of defensive programming, as
20860 tty_menu_display already limits the number of menu items to one
20861 less than the number of screen lines.) */
20862 if (y >= f->desired_matrix->nrows)
20863 return;
20864
20865 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
20866 it.first_visible_x = 0;
20867 it.last_visible_x = FRAME_COLS (f) - 1;
20868 row = it.glyph_row;
20869 /* Start with the row contents from the current matrix. */
20870 deep_copy_glyph_row (row, f->current_matrix->rows + y);
20871 saved_width = row->full_width_p;
20872 row->full_width_p = 1;
20873 saved_reversed = row->reversed_p;
20874 row->reversed_p = 0;
20875 row->enabled_p = 1;
20876
20877 /* Arrange for the menu item glyphs to start at (X,Y) and have the
20878 desired face. */
20879 eassert (x < f->desired_matrix->matrix_w);
20880 it.current_x = it.hpos = x;
20881 it.current_y = it.vpos = y;
20882 saved_used = row->used[TEXT_AREA];
20883 saved_truncated = row->truncated_on_right_p;
20884 row->used[TEXT_AREA] = x;
20885 it.face_id = face_id;
20886 it.line_wrap = TRUNCATE;
20887
20888 /* FIXME: This should be controlled by a user option. See the
20889 comments in redisplay_tool_bar and display_mode_line about this.
20890 Also, if paragraph_embedding could ever be R2L, changes will be
20891 needed to avoid shifting to the right the row characters in
20892 term.c:append_glyph. */
20893 it.paragraph_embedding = L2R;
20894
20895 /* Pad with a space on the left. */
20896 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
20897 width--;
20898 /* Display the menu item, pad with spaces to WIDTH. */
20899 if (submenu)
20900 {
20901 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20902 item_len, 0, FRAME_COLS (f) - 1, -1);
20903 width -= item_len;
20904 /* Indicate with " >" that there's a submenu. */
20905 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
20906 FRAME_COLS (f) - 1, -1);
20907 }
20908 else
20909 display_string (item_text, Qnil, Qnil, 0, 0, &it,
20910 width, 0, FRAME_COLS (f) - 1, -1);
20911
20912 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
20913 row->truncated_on_right_p = saved_truncated;
20914 row->hash = row_hash (row);
20915 row->full_width_p = saved_width;
20916 row->reversed_p = saved_reversed;
20917 }
20918 \f
20919 /***********************************************************************
20920 Mode Line
20921 ***********************************************************************/
20922
20923 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20924 FORCE is non-zero, redisplay mode lines unconditionally.
20925 Otherwise, redisplay only mode lines that are garbaged. Value is
20926 the number of windows whose mode lines were redisplayed. */
20927
20928 static int
20929 redisplay_mode_lines (Lisp_Object window, bool force)
20930 {
20931 int nwindows = 0;
20932
20933 while (!NILP (window))
20934 {
20935 struct window *w = XWINDOW (window);
20936
20937 if (WINDOWP (w->contents))
20938 nwindows += redisplay_mode_lines (w->contents, force);
20939 else if (force
20940 || FRAME_GARBAGED_P (XFRAME (w->frame))
20941 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20942 {
20943 struct text_pos lpoint;
20944 struct buffer *old = current_buffer;
20945
20946 /* Set the window's buffer for the mode line display. */
20947 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20948 set_buffer_internal_1 (XBUFFER (w->contents));
20949
20950 /* Point refers normally to the selected window. For any
20951 other window, set up appropriate value. */
20952 if (!EQ (window, selected_window))
20953 {
20954 struct text_pos pt;
20955
20956 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
20957 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20958 }
20959
20960 /* Display mode lines. */
20961 clear_glyph_matrix (w->desired_matrix);
20962 if (display_mode_lines (w))
20963 ++nwindows;
20964
20965 /* Restore old settings. */
20966 set_buffer_internal_1 (old);
20967 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20968 }
20969
20970 window = w->next;
20971 }
20972
20973 return nwindows;
20974 }
20975
20976
20977 /* Display the mode and/or header line of window W. Value is the
20978 sum number of mode lines and header lines displayed. */
20979
20980 static int
20981 display_mode_lines (struct window *w)
20982 {
20983 Lisp_Object old_selected_window = selected_window;
20984 Lisp_Object old_selected_frame = selected_frame;
20985 Lisp_Object new_frame = w->frame;
20986 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20987 int n = 0;
20988
20989 selected_frame = new_frame;
20990 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20991 or window's point, then we'd need select_window_1 here as well. */
20992 XSETWINDOW (selected_window, w);
20993 XFRAME (new_frame)->selected_window = selected_window;
20994
20995 /* These will be set while the mode line specs are processed. */
20996 line_number_displayed = 0;
20997 w->column_number_displayed = -1;
20998
20999 if (WINDOW_WANTS_MODELINE_P (w))
21000 {
21001 struct window *sel_w = XWINDOW (old_selected_window);
21002
21003 /* Select mode line face based on the real selected window. */
21004 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21005 BVAR (current_buffer, mode_line_format));
21006 ++n;
21007 }
21008
21009 if (WINDOW_WANTS_HEADER_LINE_P (w))
21010 {
21011 display_mode_line (w, HEADER_LINE_FACE_ID,
21012 BVAR (current_buffer, header_line_format));
21013 ++n;
21014 }
21015
21016 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21017 selected_frame = old_selected_frame;
21018 selected_window = old_selected_window;
21019 if (n > 0)
21020 w->must_be_updated_p = true;
21021 return n;
21022 }
21023
21024
21025 /* Display mode or header line of window W. FACE_ID specifies which
21026 line to display; it is either MODE_LINE_FACE_ID or
21027 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21028 display. Value is the pixel height of the mode/header line
21029 displayed. */
21030
21031 static int
21032 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21033 {
21034 struct it it;
21035 struct face *face;
21036 ptrdiff_t count = SPECPDL_INDEX ();
21037
21038 init_iterator (&it, w, -1, -1, NULL, face_id);
21039 /* Don't extend on a previously drawn mode-line.
21040 This may happen if called from pos_visible_p. */
21041 it.glyph_row->enabled_p = 0;
21042 prepare_desired_row (it.glyph_row);
21043
21044 it.glyph_row->mode_line_p = 1;
21045
21046 /* FIXME: This should be controlled by a user option. But
21047 supporting such an option is not trivial, since the mode line is
21048 made up of many separate strings. */
21049 it.paragraph_embedding = L2R;
21050
21051 record_unwind_protect (unwind_format_mode_line,
21052 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21053
21054 mode_line_target = MODE_LINE_DISPLAY;
21055
21056 /* Temporarily make frame's keyboard the current kboard so that
21057 kboard-local variables in the mode_line_format will get the right
21058 values. */
21059 push_kboard (FRAME_KBOARD (it.f));
21060 record_unwind_save_match_data ();
21061 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21062 pop_kboard ();
21063
21064 unbind_to (count, Qnil);
21065
21066 /* Fill up with spaces. */
21067 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21068
21069 compute_line_metrics (&it);
21070 it.glyph_row->full_width_p = 1;
21071 it.glyph_row->continued_p = 0;
21072 it.glyph_row->truncated_on_left_p = 0;
21073 it.glyph_row->truncated_on_right_p = 0;
21074
21075 /* Make a 3D mode-line have a shadow at its right end. */
21076 face = FACE_FROM_ID (it.f, face_id);
21077 extend_face_to_end_of_line (&it);
21078 if (face->box != FACE_NO_BOX)
21079 {
21080 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21081 + it.glyph_row->used[TEXT_AREA] - 1);
21082 last->right_box_line_p = 1;
21083 }
21084
21085 return it.glyph_row->height;
21086 }
21087
21088 /* Move element ELT in LIST to the front of LIST.
21089 Return the updated list. */
21090
21091 static Lisp_Object
21092 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21093 {
21094 register Lisp_Object tail, prev;
21095 register Lisp_Object tem;
21096
21097 tail = list;
21098 prev = Qnil;
21099 while (CONSP (tail))
21100 {
21101 tem = XCAR (tail);
21102
21103 if (EQ (elt, tem))
21104 {
21105 /* Splice out the link TAIL. */
21106 if (NILP (prev))
21107 list = XCDR (tail);
21108 else
21109 Fsetcdr (prev, XCDR (tail));
21110
21111 /* Now make it the first. */
21112 Fsetcdr (tail, list);
21113 return tail;
21114 }
21115 else
21116 prev = tail;
21117 tail = XCDR (tail);
21118 QUIT;
21119 }
21120
21121 /* Not found--return unchanged LIST. */
21122 return list;
21123 }
21124
21125 /* Contribute ELT to the mode line for window IT->w. How it
21126 translates into text depends on its data type.
21127
21128 IT describes the display environment in which we display, as usual.
21129
21130 DEPTH is the depth in recursion. It is used to prevent
21131 infinite recursion here.
21132
21133 FIELD_WIDTH is the number of characters the display of ELT should
21134 occupy in the mode line, and PRECISION is the maximum number of
21135 characters to display from ELT's representation. See
21136 display_string for details.
21137
21138 Returns the hpos of the end of the text generated by ELT.
21139
21140 PROPS is a property list to add to any string we encounter.
21141
21142 If RISKY is nonzero, remove (disregard) any properties in any string
21143 we encounter, and ignore :eval and :propertize.
21144
21145 The global variable `mode_line_target' determines whether the
21146 output is passed to `store_mode_line_noprop',
21147 `store_mode_line_string', or `display_string'. */
21148
21149 static int
21150 display_mode_element (struct it *it, int depth, int field_width, int precision,
21151 Lisp_Object elt, Lisp_Object props, int risky)
21152 {
21153 int n = 0, field, prec;
21154 int literal = 0;
21155
21156 tail_recurse:
21157 if (depth > 100)
21158 elt = build_string ("*too-deep*");
21159
21160 depth++;
21161
21162 switch (XTYPE (elt))
21163 {
21164 case Lisp_String:
21165 {
21166 /* A string: output it and check for %-constructs within it. */
21167 unsigned char c;
21168 ptrdiff_t offset = 0;
21169
21170 if (SCHARS (elt) > 0
21171 && (!NILP (props) || risky))
21172 {
21173 Lisp_Object oprops, aelt;
21174 oprops = Ftext_properties_at (make_number (0), elt);
21175
21176 /* If the starting string's properties are not what
21177 we want, translate the string. Also, if the string
21178 is risky, do that anyway. */
21179
21180 if (NILP (Fequal (props, oprops)) || risky)
21181 {
21182 /* If the starting string has properties,
21183 merge the specified ones onto the existing ones. */
21184 if (! NILP (oprops) && !risky)
21185 {
21186 Lisp_Object tem;
21187
21188 oprops = Fcopy_sequence (oprops);
21189 tem = props;
21190 while (CONSP (tem))
21191 {
21192 oprops = Fplist_put (oprops, XCAR (tem),
21193 XCAR (XCDR (tem)));
21194 tem = XCDR (XCDR (tem));
21195 }
21196 props = oprops;
21197 }
21198
21199 aelt = Fassoc (elt, mode_line_proptrans_alist);
21200 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21201 {
21202 /* AELT is what we want. Move it to the front
21203 without consing. */
21204 elt = XCAR (aelt);
21205 mode_line_proptrans_alist
21206 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21207 }
21208 else
21209 {
21210 Lisp_Object tem;
21211
21212 /* If AELT has the wrong props, it is useless.
21213 so get rid of it. */
21214 if (! NILP (aelt))
21215 mode_line_proptrans_alist
21216 = Fdelq (aelt, mode_line_proptrans_alist);
21217
21218 elt = Fcopy_sequence (elt);
21219 Fset_text_properties (make_number (0), Flength (elt),
21220 props, elt);
21221 /* Add this item to mode_line_proptrans_alist. */
21222 mode_line_proptrans_alist
21223 = Fcons (Fcons (elt, props),
21224 mode_line_proptrans_alist);
21225 /* Truncate mode_line_proptrans_alist
21226 to at most 50 elements. */
21227 tem = Fnthcdr (make_number (50),
21228 mode_line_proptrans_alist);
21229 if (! NILP (tem))
21230 XSETCDR (tem, Qnil);
21231 }
21232 }
21233 }
21234
21235 offset = 0;
21236
21237 if (literal)
21238 {
21239 prec = precision - n;
21240 switch (mode_line_target)
21241 {
21242 case MODE_LINE_NOPROP:
21243 case MODE_LINE_TITLE:
21244 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21245 break;
21246 case MODE_LINE_STRING:
21247 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21248 break;
21249 case MODE_LINE_DISPLAY:
21250 n += display_string (NULL, elt, Qnil, 0, 0, it,
21251 0, prec, 0, STRING_MULTIBYTE (elt));
21252 break;
21253 }
21254
21255 break;
21256 }
21257
21258 /* Handle the non-literal case. */
21259
21260 while ((precision <= 0 || n < precision)
21261 && SREF (elt, offset) != 0
21262 && (mode_line_target != MODE_LINE_DISPLAY
21263 || it->current_x < it->last_visible_x))
21264 {
21265 ptrdiff_t last_offset = offset;
21266
21267 /* Advance to end of string or next format specifier. */
21268 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21269 ;
21270
21271 if (offset - 1 != last_offset)
21272 {
21273 ptrdiff_t nchars, nbytes;
21274
21275 /* Output to end of string or up to '%'. Field width
21276 is length of string. Don't output more than
21277 PRECISION allows us. */
21278 offset--;
21279
21280 prec = c_string_width (SDATA (elt) + last_offset,
21281 offset - last_offset, precision - n,
21282 &nchars, &nbytes);
21283
21284 switch (mode_line_target)
21285 {
21286 case MODE_LINE_NOPROP:
21287 case MODE_LINE_TITLE:
21288 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21289 break;
21290 case MODE_LINE_STRING:
21291 {
21292 ptrdiff_t bytepos = last_offset;
21293 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21294 ptrdiff_t endpos = (precision <= 0
21295 ? string_byte_to_char (elt, offset)
21296 : charpos + nchars);
21297
21298 n += store_mode_line_string (NULL,
21299 Fsubstring (elt, make_number (charpos),
21300 make_number (endpos)),
21301 0, 0, 0, Qnil);
21302 }
21303 break;
21304 case MODE_LINE_DISPLAY:
21305 {
21306 ptrdiff_t bytepos = last_offset;
21307 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21308
21309 if (precision <= 0)
21310 nchars = string_byte_to_char (elt, offset) - charpos;
21311 n += display_string (NULL, elt, Qnil, 0, charpos,
21312 it, 0, nchars, 0,
21313 STRING_MULTIBYTE (elt));
21314 }
21315 break;
21316 }
21317 }
21318 else /* c == '%' */
21319 {
21320 ptrdiff_t percent_position = offset;
21321
21322 /* Get the specified minimum width. Zero means
21323 don't pad. */
21324 field = 0;
21325 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21326 field = field * 10 + c - '0';
21327
21328 /* Don't pad beyond the total padding allowed. */
21329 if (field_width - n > 0 && field > field_width - n)
21330 field = field_width - n;
21331
21332 /* Note that either PRECISION <= 0 or N < PRECISION. */
21333 prec = precision - n;
21334
21335 if (c == 'M')
21336 n += display_mode_element (it, depth, field, prec,
21337 Vglobal_mode_string, props,
21338 risky);
21339 else if (c != 0)
21340 {
21341 bool multibyte;
21342 ptrdiff_t bytepos, charpos;
21343 const char *spec;
21344 Lisp_Object string;
21345
21346 bytepos = percent_position;
21347 charpos = (STRING_MULTIBYTE (elt)
21348 ? string_byte_to_char (elt, bytepos)
21349 : bytepos);
21350 spec = decode_mode_spec (it->w, c, field, &string);
21351 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21352
21353 switch (mode_line_target)
21354 {
21355 case MODE_LINE_NOPROP:
21356 case MODE_LINE_TITLE:
21357 n += store_mode_line_noprop (spec, field, prec);
21358 break;
21359 case MODE_LINE_STRING:
21360 {
21361 Lisp_Object tem = build_string (spec);
21362 props = Ftext_properties_at (make_number (charpos), elt);
21363 /* Should only keep face property in props */
21364 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21365 }
21366 break;
21367 case MODE_LINE_DISPLAY:
21368 {
21369 int nglyphs_before, nwritten;
21370
21371 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21372 nwritten = display_string (spec, string, elt,
21373 charpos, 0, it,
21374 field, prec, 0,
21375 multibyte);
21376
21377 /* Assign to the glyphs written above the
21378 string where the `%x' came from, position
21379 of the `%'. */
21380 if (nwritten > 0)
21381 {
21382 struct glyph *glyph
21383 = (it->glyph_row->glyphs[TEXT_AREA]
21384 + nglyphs_before);
21385 int i;
21386
21387 for (i = 0; i < nwritten; ++i)
21388 {
21389 glyph[i].object = elt;
21390 glyph[i].charpos = charpos;
21391 }
21392
21393 n += nwritten;
21394 }
21395 }
21396 break;
21397 }
21398 }
21399 else /* c == 0 */
21400 break;
21401 }
21402 }
21403 }
21404 break;
21405
21406 case Lisp_Symbol:
21407 /* A symbol: process the value of the symbol recursively
21408 as if it appeared here directly. Avoid error if symbol void.
21409 Special case: if value of symbol is a string, output the string
21410 literally. */
21411 {
21412 register Lisp_Object tem;
21413
21414 /* If the variable is not marked as risky to set
21415 then its contents are risky to use. */
21416 if (NILP (Fget (elt, Qrisky_local_variable)))
21417 risky = 1;
21418
21419 tem = Fboundp (elt);
21420 if (!NILP (tem))
21421 {
21422 tem = Fsymbol_value (elt);
21423 /* If value is a string, output that string literally:
21424 don't check for % within it. */
21425 if (STRINGP (tem))
21426 literal = 1;
21427
21428 if (!EQ (tem, elt))
21429 {
21430 /* Give up right away for nil or t. */
21431 elt = tem;
21432 goto tail_recurse;
21433 }
21434 }
21435 }
21436 break;
21437
21438 case Lisp_Cons:
21439 {
21440 register Lisp_Object car, tem;
21441
21442 /* A cons cell: five distinct cases.
21443 If first element is :eval or :propertize, do something special.
21444 If first element is a string or a cons, process all the elements
21445 and effectively concatenate them.
21446 If first element is a negative number, truncate displaying cdr to
21447 at most that many characters. If positive, pad (with spaces)
21448 to at least that many characters.
21449 If first element is a symbol, process the cadr or caddr recursively
21450 according to whether the symbol's value is non-nil or nil. */
21451 car = XCAR (elt);
21452 if (EQ (car, QCeval))
21453 {
21454 /* An element of the form (:eval FORM) means evaluate FORM
21455 and use the result as mode line elements. */
21456
21457 if (risky)
21458 break;
21459
21460 if (CONSP (XCDR (elt)))
21461 {
21462 Lisp_Object spec;
21463 spec = safe_eval (XCAR (XCDR (elt)));
21464 n += display_mode_element (it, depth, field_width - n,
21465 precision - n, spec, props,
21466 risky);
21467 }
21468 }
21469 else if (EQ (car, QCpropertize))
21470 {
21471 /* An element of the form (:propertize ELT PROPS...)
21472 means display ELT but applying properties PROPS. */
21473
21474 if (risky)
21475 break;
21476
21477 if (CONSP (XCDR (elt)))
21478 n += display_mode_element (it, depth, field_width - n,
21479 precision - n, XCAR (XCDR (elt)),
21480 XCDR (XCDR (elt)), risky);
21481 }
21482 else if (SYMBOLP (car))
21483 {
21484 tem = Fboundp (car);
21485 elt = XCDR (elt);
21486 if (!CONSP (elt))
21487 goto invalid;
21488 /* elt is now the cdr, and we know it is a cons cell.
21489 Use its car if CAR has a non-nil value. */
21490 if (!NILP (tem))
21491 {
21492 tem = Fsymbol_value (car);
21493 if (!NILP (tem))
21494 {
21495 elt = XCAR (elt);
21496 goto tail_recurse;
21497 }
21498 }
21499 /* Symbol's value is nil (or symbol is unbound)
21500 Get the cddr of the original list
21501 and if possible find the caddr and use that. */
21502 elt = XCDR (elt);
21503 if (NILP (elt))
21504 break;
21505 else if (!CONSP (elt))
21506 goto invalid;
21507 elt = XCAR (elt);
21508 goto tail_recurse;
21509 }
21510 else if (INTEGERP (car))
21511 {
21512 register int lim = XINT (car);
21513 elt = XCDR (elt);
21514 if (lim < 0)
21515 {
21516 /* Negative int means reduce maximum width. */
21517 if (precision <= 0)
21518 precision = -lim;
21519 else
21520 precision = min (precision, -lim);
21521 }
21522 else if (lim > 0)
21523 {
21524 /* Padding specified. Don't let it be more than
21525 current maximum. */
21526 if (precision > 0)
21527 lim = min (precision, lim);
21528
21529 /* If that's more padding than already wanted, queue it.
21530 But don't reduce padding already specified even if
21531 that is beyond the current truncation point. */
21532 field_width = max (lim, field_width);
21533 }
21534 goto tail_recurse;
21535 }
21536 else if (STRINGP (car) || CONSP (car))
21537 {
21538 Lisp_Object halftail = elt;
21539 int len = 0;
21540
21541 while (CONSP (elt)
21542 && (precision <= 0 || n < precision))
21543 {
21544 n += display_mode_element (it, depth,
21545 /* Do padding only after the last
21546 element in the list. */
21547 (! CONSP (XCDR (elt))
21548 ? field_width - n
21549 : 0),
21550 precision - n, XCAR (elt),
21551 props, risky);
21552 elt = XCDR (elt);
21553 len++;
21554 if ((len & 1) == 0)
21555 halftail = XCDR (halftail);
21556 /* Check for cycle. */
21557 if (EQ (halftail, elt))
21558 break;
21559 }
21560 }
21561 }
21562 break;
21563
21564 default:
21565 invalid:
21566 elt = build_string ("*invalid*");
21567 goto tail_recurse;
21568 }
21569
21570 /* Pad to FIELD_WIDTH. */
21571 if (field_width > 0 && n < field_width)
21572 {
21573 switch (mode_line_target)
21574 {
21575 case MODE_LINE_NOPROP:
21576 case MODE_LINE_TITLE:
21577 n += store_mode_line_noprop ("", field_width - n, 0);
21578 break;
21579 case MODE_LINE_STRING:
21580 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21581 break;
21582 case MODE_LINE_DISPLAY:
21583 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21584 0, 0, 0);
21585 break;
21586 }
21587 }
21588
21589 return n;
21590 }
21591
21592 /* Store a mode-line string element in mode_line_string_list.
21593
21594 If STRING is non-null, display that C string. Otherwise, the Lisp
21595 string LISP_STRING is displayed.
21596
21597 FIELD_WIDTH is the minimum number of output glyphs to produce.
21598 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21599 with spaces. FIELD_WIDTH <= 0 means don't pad.
21600
21601 PRECISION is the maximum number of characters to output from
21602 STRING. PRECISION <= 0 means don't truncate the string.
21603
21604 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21605 properties to the string.
21606
21607 PROPS are the properties to add to the string.
21608 The mode_line_string_face face property is always added to the string.
21609 */
21610
21611 static int
21612 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21613 int field_width, int precision, Lisp_Object props)
21614 {
21615 ptrdiff_t len;
21616 int n = 0;
21617
21618 if (string != NULL)
21619 {
21620 len = strlen (string);
21621 if (precision > 0 && len > precision)
21622 len = precision;
21623 lisp_string = make_string (string, len);
21624 if (NILP (props))
21625 props = mode_line_string_face_prop;
21626 else if (!NILP (mode_line_string_face))
21627 {
21628 Lisp_Object face = Fplist_get (props, Qface);
21629 props = Fcopy_sequence (props);
21630 if (NILP (face))
21631 face = mode_line_string_face;
21632 else
21633 face = list2 (face, mode_line_string_face);
21634 props = Fplist_put (props, Qface, face);
21635 }
21636 Fadd_text_properties (make_number (0), make_number (len),
21637 props, lisp_string);
21638 }
21639 else
21640 {
21641 len = XFASTINT (Flength (lisp_string));
21642 if (precision > 0 && len > precision)
21643 {
21644 len = precision;
21645 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21646 precision = -1;
21647 }
21648 if (!NILP (mode_line_string_face))
21649 {
21650 Lisp_Object face;
21651 if (NILP (props))
21652 props = Ftext_properties_at (make_number (0), lisp_string);
21653 face = Fplist_get (props, Qface);
21654 if (NILP (face))
21655 face = mode_line_string_face;
21656 else
21657 face = list2 (face, mode_line_string_face);
21658 props = list2 (Qface, face);
21659 if (copy_string)
21660 lisp_string = Fcopy_sequence (lisp_string);
21661 }
21662 if (!NILP (props))
21663 Fadd_text_properties (make_number (0), make_number (len),
21664 props, lisp_string);
21665 }
21666
21667 if (len > 0)
21668 {
21669 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21670 n += len;
21671 }
21672
21673 if (field_width > len)
21674 {
21675 field_width -= len;
21676 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21677 if (!NILP (props))
21678 Fadd_text_properties (make_number (0), make_number (field_width),
21679 props, lisp_string);
21680 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21681 n += field_width;
21682 }
21683
21684 return n;
21685 }
21686
21687
21688 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21689 1, 4, 0,
21690 doc: /* Format a string out of a mode line format specification.
21691 First arg FORMAT specifies the mode line format (see `mode-line-format'
21692 for details) to use.
21693
21694 By default, the format is evaluated for the currently selected window.
21695
21696 Optional second arg FACE specifies the face property to put on all
21697 characters for which no face is specified. The value nil means the
21698 default face. The value t means whatever face the window's mode line
21699 currently uses (either `mode-line' or `mode-line-inactive',
21700 depending on whether the window is the selected window or not).
21701 An integer value means the value string has no text
21702 properties.
21703
21704 Optional third and fourth args WINDOW and BUFFER specify the window
21705 and buffer to use as the context for the formatting (defaults
21706 are the selected window and the WINDOW's buffer). */)
21707 (Lisp_Object format, Lisp_Object face,
21708 Lisp_Object window, Lisp_Object buffer)
21709 {
21710 struct it it;
21711 int len;
21712 struct window *w;
21713 struct buffer *old_buffer = NULL;
21714 int face_id;
21715 int no_props = INTEGERP (face);
21716 ptrdiff_t count = SPECPDL_INDEX ();
21717 Lisp_Object str;
21718 int string_start = 0;
21719
21720 w = decode_any_window (window);
21721 XSETWINDOW (window, w);
21722
21723 if (NILP (buffer))
21724 buffer = w->contents;
21725 CHECK_BUFFER (buffer);
21726
21727 /* Make formatting the modeline a non-op when noninteractive, otherwise
21728 there will be problems later caused by a partially initialized frame. */
21729 if (NILP (format) || noninteractive)
21730 return empty_unibyte_string;
21731
21732 if (no_props)
21733 face = Qnil;
21734
21735 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21736 : EQ (face, Qt) ? (EQ (window, selected_window)
21737 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21738 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21739 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21740 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21741 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21742 : DEFAULT_FACE_ID;
21743
21744 old_buffer = current_buffer;
21745
21746 /* Save things including mode_line_proptrans_alist,
21747 and set that to nil so that we don't alter the outer value. */
21748 record_unwind_protect (unwind_format_mode_line,
21749 format_mode_line_unwind_data
21750 (XFRAME (WINDOW_FRAME (w)),
21751 old_buffer, selected_window, 1));
21752 mode_line_proptrans_alist = Qnil;
21753
21754 Fselect_window (window, Qt);
21755 set_buffer_internal_1 (XBUFFER (buffer));
21756
21757 init_iterator (&it, w, -1, -1, NULL, face_id);
21758
21759 if (no_props)
21760 {
21761 mode_line_target = MODE_LINE_NOPROP;
21762 mode_line_string_face_prop = Qnil;
21763 mode_line_string_list = Qnil;
21764 string_start = MODE_LINE_NOPROP_LEN (0);
21765 }
21766 else
21767 {
21768 mode_line_target = MODE_LINE_STRING;
21769 mode_line_string_list = Qnil;
21770 mode_line_string_face = face;
21771 mode_line_string_face_prop
21772 = NILP (face) ? Qnil : list2 (Qface, face);
21773 }
21774
21775 push_kboard (FRAME_KBOARD (it.f));
21776 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21777 pop_kboard ();
21778
21779 if (no_props)
21780 {
21781 len = MODE_LINE_NOPROP_LEN (string_start);
21782 str = make_string (mode_line_noprop_buf + string_start, len);
21783 }
21784 else
21785 {
21786 mode_line_string_list = Fnreverse (mode_line_string_list);
21787 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21788 empty_unibyte_string);
21789 }
21790
21791 unbind_to (count, Qnil);
21792 return str;
21793 }
21794
21795 /* Write a null-terminated, right justified decimal representation of
21796 the positive integer D to BUF using a minimal field width WIDTH. */
21797
21798 static void
21799 pint2str (register char *buf, register int width, register ptrdiff_t d)
21800 {
21801 register char *p = buf;
21802
21803 if (d <= 0)
21804 *p++ = '0';
21805 else
21806 {
21807 while (d > 0)
21808 {
21809 *p++ = d % 10 + '0';
21810 d /= 10;
21811 }
21812 }
21813
21814 for (width -= (int) (p - buf); width > 0; --width)
21815 *p++ = ' ';
21816 *p-- = '\0';
21817 while (p > buf)
21818 {
21819 d = *buf;
21820 *buf++ = *p;
21821 *p-- = d;
21822 }
21823 }
21824
21825 /* Write a null-terminated, right justified decimal and "human
21826 readable" representation of the nonnegative integer D to BUF using
21827 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21828
21829 static const char power_letter[] =
21830 {
21831 0, /* no letter */
21832 'k', /* kilo */
21833 'M', /* mega */
21834 'G', /* giga */
21835 'T', /* tera */
21836 'P', /* peta */
21837 'E', /* exa */
21838 'Z', /* zetta */
21839 'Y' /* yotta */
21840 };
21841
21842 static void
21843 pint2hrstr (char *buf, int width, ptrdiff_t d)
21844 {
21845 /* We aim to represent the nonnegative integer D as
21846 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21847 ptrdiff_t quotient = d;
21848 int remainder = 0;
21849 /* -1 means: do not use TENTHS. */
21850 int tenths = -1;
21851 int exponent = 0;
21852
21853 /* Length of QUOTIENT.TENTHS as a string. */
21854 int length;
21855
21856 char * psuffix;
21857 char * p;
21858
21859 if (quotient >= 1000)
21860 {
21861 /* Scale to the appropriate EXPONENT. */
21862 do
21863 {
21864 remainder = quotient % 1000;
21865 quotient /= 1000;
21866 exponent++;
21867 }
21868 while (quotient >= 1000);
21869
21870 /* Round to nearest and decide whether to use TENTHS or not. */
21871 if (quotient <= 9)
21872 {
21873 tenths = remainder / 100;
21874 if (remainder % 100 >= 50)
21875 {
21876 if (tenths < 9)
21877 tenths++;
21878 else
21879 {
21880 quotient++;
21881 if (quotient == 10)
21882 tenths = -1;
21883 else
21884 tenths = 0;
21885 }
21886 }
21887 }
21888 else
21889 if (remainder >= 500)
21890 {
21891 if (quotient < 999)
21892 quotient++;
21893 else
21894 {
21895 quotient = 1;
21896 exponent++;
21897 tenths = 0;
21898 }
21899 }
21900 }
21901
21902 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21903 if (tenths == -1 && quotient <= 99)
21904 if (quotient <= 9)
21905 length = 1;
21906 else
21907 length = 2;
21908 else
21909 length = 3;
21910 p = psuffix = buf + max (width, length);
21911
21912 /* Print EXPONENT. */
21913 *psuffix++ = power_letter[exponent];
21914 *psuffix = '\0';
21915
21916 /* Print TENTHS. */
21917 if (tenths >= 0)
21918 {
21919 *--p = '0' + tenths;
21920 *--p = '.';
21921 }
21922
21923 /* Print QUOTIENT. */
21924 do
21925 {
21926 int digit = quotient % 10;
21927 *--p = '0' + digit;
21928 }
21929 while ((quotient /= 10) != 0);
21930
21931 /* Print leading spaces. */
21932 while (buf < p)
21933 *--p = ' ';
21934 }
21935
21936 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21937 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21938 type of CODING_SYSTEM. Return updated pointer into BUF. */
21939
21940 static unsigned char invalid_eol_type[] = "(*invalid*)";
21941
21942 static char *
21943 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21944 {
21945 Lisp_Object val;
21946 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21947 const unsigned char *eol_str;
21948 int eol_str_len;
21949 /* The EOL conversion we are using. */
21950 Lisp_Object eoltype;
21951
21952 val = CODING_SYSTEM_SPEC (coding_system);
21953 eoltype = Qnil;
21954
21955 if (!VECTORP (val)) /* Not yet decided. */
21956 {
21957 *buf++ = multibyte ? '-' : ' ';
21958 if (eol_flag)
21959 eoltype = eol_mnemonic_undecided;
21960 /* Don't mention EOL conversion if it isn't decided. */
21961 }
21962 else
21963 {
21964 Lisp_Object attrs;
21965 Lisp_Object eolvalue;
21966
21967 attrs = AREF (val, 0);
21968 eolvalue = AREF (val, 2);
21969
21970 *buf++ = multibyte
21971 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21972 : ' ';
21973
21974 if (eol_flag)
21975 {
21976 /* The EOL conversion that is normal on this system. */
21977
21978 if (NILP (eolvalue)) /* Not yet decided. */
21979 eoltype = eol_mnemonic_undecided;
21980 else if (VECTORP (eolvalue)) /* Not yet decided. */
21981 eoltype = eol_mnemonic_undecided;
21982 else /* eolvalue is Qunix, Qdos, or Qmac. */
21983 eoltype = (EQ (eolvalue, Qunix)
21984 ? eol_mnemonic_unix
21985 : (EQ (eolvalue, Qdos) == 1
21986 ? eol_mnemonic_dos : eol_mnemonic_mac));
21987 }
21988 }
21989
21990 if (eol_flag)
21991 {
21992 /* Mention the EOL conversion if it is not the usual one. */
21993 if (STRINGP (eoltype))
21994 {
21995 eol_str = SDATA (eoltype);
21996 eol_str_len = SBYTES (eoltype);
21997 }
21998 else if (CHARACTERP (eoltype))
21999 {
22000 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22001 int c = XFASTINT (eoltype);
22002 eol_str_len = CHAR_STRING (c, tmp);
22003 eol_str = tmp;
22004 }
22005 else
22006 {
22007 eol_str = invalid_eol_type;
22008 eol_str_len = sizeof (invalid_eol_type) - 1;
22009 }
22010 memcpy (buf, eol_str, eol_str_len);
22011 buf += eol_str_len;
22012 }
22013
22014 return buf;
22015 }
22016
22017 /* Return a string for the output of a mode line %-spec for window W,
22018 generated by character C. FIELD_WIDTH > 0 means pad the string
22019 returned with spaces to that value. Return a Lisp string in
22020 *STRING if the resulting string is taken from that Lisp string.
22021
22022 Note we operate on the current buffer for most purposes. */
22023
22024 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22025
22026 static const char *
22027 decode_mode_spec (struct window *w, register int c, int field_width,
22028 Lisp_Object *string)
22029 {
22030 Lisp_Object obj;
22031 struct frame *f = XFRAME (WINDOW_FRAME (w));
22032 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22033 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22034 produce strings from numerical values, so limit preposterously
22035 large values of FIELD_WIDTH to avoid overrunning the buffer's
22036 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22037 bytes plus the terminating null. */
22038 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22039 struct buffer *b = current_buffer;
22040
22041 obj = Qnil;
22042 *string = Qnil;
22043
22044 switch (c)
22045 {
22046 case '*':
22047 if (!NILP (BVAR (b, read_only)))
22048 return "%";
22049 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22050 return "*";
22051 return "-";
22052
22053 case '+':
22054 /* This differs from %* only for a modified read-only buffer. */
22055 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22056 return "*";
22057 if (!NILP (BVAR (b, read_only)))
22058 return "%";
22059 return "-";
22060
22061 case '&':
22062 /* This differs from %* in ignoring read-only-ness. */
22063 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22064 return "*";
22065 return "-";
22066
22067 case '%':
22068 return "%";
22069
22070 case '[':
22071 {
22072 int i;
22073 char *p;
22074
22075 if (command_loop_level > 5)
22076 return "[[[... ";
22077 p = decode_mode_spec_buf;
22078 for (i = 0; i < command_loop_level; i++)
22079 *p++ = '[';
22080 *p = 0;
22081 return decode_mode_spec_buf;
22082 }
22083
22084 case ']':
22085 {
22086 int i;
22087 char *p;
22088
22089 if (command_loop_level > 5)
22090 return " ...]]]";
22091 p = decode_mode_spec_buf;
22092 for (i = 0; i < command_loop_level; i++)
22093 *p++ = ']';
22094 *p = 0;
22095 return decode_mode_spec_buf;
22096 }
22097
22098 case '-':
22099 {
22100 register int i;
22101
22102 /* Let lots_of_dashes be a string of infinite length. */
22103 if (mode_line_target == MODE_LINE_NOPROP
22104 || mode_line_target == MODE_LINE_STRING)
22105 return "--";
22106 if (field_width <= 0
22107 || field_width > sizeof (lots_of_dashes))
22108 {
22109 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22110 decode_mode_spec_buf[i] = '-';
22111 decode_mode_spec_buf[i] = '\0';
22112 return decode_mode_spec_buf;
22113 }
22114 else
22115 return lots_of_dashes;
22116 }
22117
22118 case 'b':
22119 obj = BVAR (b, name);
22120 break;
22121
22122 case 'c':
22123 /* %c and %l are ignored in `frame-title-format'.
22124 (In redisplay_internal, the frame title is drawn _before_ the
22125 windows are updated, so the stuff which depends on actual
22126 window contents (such as %l) may fail to render properly, or
22127 even crash emacs.) */
22128 if (mode_line_target == MODE_LINE_TITLE)
22129 return "";
22130 else
22131 {
22132 ptrdiff_t col = current_column ();
22133 w->column_number_displayed = col;
22134 pint2str (decode_mode_spec_buf, width, col);
22135 return decode_mode_spec_buf;
22136 }
22137
22138 case 'e':
22139 #ifndef SYSTEM_MALLOC
22140 {
22141 if (NILP (Vmemory_full))
22142 return "";
22143 else
22144 return "!MEM FULL! ";
22145 }
22146 #else
22147 return "";
22148 #endif
22149
22150 case 'F':
22151 /* %F displays the frame name. */
22152 if (!NILP (f->title))
22153 return SSDATA (f->title);
22154 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22155 return SSDATA (f->name);
22156 return "Emacs";
22157
22158 case 'f':
22159 obj = BVAR (b, filename);
22160 break;
22161
22162 case 'i':
22163 {
22164 ptrdiff_t size = ZV - BEGV;
22165 pint2str (decode_mode_spec_buf, width, size);
22166 return decode_mode_spec_buf;
22167 }
22168
22169 case 'I':
22170 {
22171 ptrdiff_t size = ZV - BEGV;
22172 pint2hrstr (decode_mode_spec_buf, width, size);
22173 return decode_mode_spec_buf;
22174 }
22175
22176 case 'l':
22177 {
22178 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22179 ptrdiff_t topline, nlines, height;
22180 ptrdiff_t junk;
22181
22182 /* %c and %l are ignored in `frame-title-format'. */
22183 if (mode_line_target == MODE_LINE_TITLE)
22184 return "";
22185
22186 startpos = marker_position (w->start);
22187 startpos_byte = marker_byte_position (w->start);
22188 height = WINDOW_TOTAL_LINES (w);
22189
22190 /* If we decided that this buffer isn't suitable for line numbers,
22191 don't forget that too fast. */
22192 if (w->base_line_pos == -1)
22193 goto no_value;
22194
22195 /* If the buffer is very big, don't waste time. */
22196 if (INTEGERP (Vline_number_display_limit)
22197 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22198 {
22199 w->base_line_pos = 0;
22200 w->base_line_number = 0;
22201 goto no_value;
22202 }
22203
22204 if (w->base_line_number > 0
22205 && w->base_line_pos > 0
22206 && w->base_line_pos <= startpos)
22207 {
22208 line = w->base_line_number;
22209 linepos = w->base_line_pos;
22210 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22211 }
22212 else
22213 {
22214 line = 1;
22215 linepos = BUF_BEGV (b);
22216 linepos_byte = BUF_BEGV_BYTE (b);
22217 }
22218
22219 /* Count lines from base line to window start position. */
22220 nlines = display_count_lines (linepos_byte,
22221 startpos_byte,
22222 startpos, &junk);
22223
22224 topline = nlines + line;
22225
22226 /* Determine a new base line, if the old one is too close
22227 or too far away, or if we did not have one.
22228 "Too close" means it's plausible a scroll-down would
22229 go back past it. */
22230 if (startpos == BUF_BEGV (b))
22231 {
22232 w->base_line_number = topline;
22233 w->base_line_pos = BUF_BEGV (b);
22234 }
22235 else if (nlines < height + 25 || nlines > height * 3 + 50
22236 || linepos == BUF_BEGV (b))
22237 {
22238 ptrdiff_t limit = BUF_BEGV (b);
22239 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22240 ptrdiff_t position;
22241 ptrdiff_t distance =
22242 (height * 2 + 30) * line_number_display_limit_width;
22243
22244 if (startpos - distance > limit)
22245 {
22246 limit = startpos - distance;
22247 limit_byte = CHAR_TO_BYTE (limit);
22248 }
22249
22250 nlines = display_count_lines (startpos_byte,
22251 limit_byte,
22252 - (height * 2 + 30),
22253 &position);
22254 /* If we couldn't find the lines we wanted within
22255 line_number_display_limit_width chars per line,
22256 give up on line numbers for this window. */
22257 if (position == limit_byte && limit == startpos - distance)
22258 {
22259 w->base_line_pos = -1;
22260 w->base_line_number = 0;
22261 goto no_value;
22262 }
22263
22264 w->base_line_number = topline - nlines;
22265 w->base_line_pos = BYTE_TO_CHAR (position);
22266 }
22267
22268 /* Now count lines from the start pos to point. */
22269 nlines = display_count_lines (startpos_byte,
22270 PT_BYTE, PT, &junk);
22271
22272 /* Record that we did display the line number. */
22273 line_number_displayed = 1;
22274
22275 /* Make the string to show. */
22276 pint2str (decode_mode_spec_buf, width, topline + nlines);
22277 return decode_mode_spec_buf;
22278 no_value:
22279 {
22280 char* p = decode_mode_spec_buf;
22281 int pad = width - 2;
22282 while (pad-- > 0)
22283 *p++ = ' ';
22284 *p++ = '?';
22285 *p++ = '?';
22286 *p = '\0';
22287 return decode_mode_spec_buf;
22288 }
22289 }
22290 break;
22291
22292 case 'm':
22293 obj = BVAR (b, mode_name);
22294 break;
22295
22296 case 'n':
22297 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22298 return " Narrow";
22299 break;
22300
22301 case 'p':
22302 {
22303 ptrdiff_t pos = marker_position (w->start);
22304 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22305
22306 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22307 {
22308 if (pos <= BUF_BEGV (b))
22309 return "All";
22310 else
22311 return "Bottom";
22312 }
22313 else if (pos <= BUF_BEGV (b))
22314 return "Top";
22315 else
22316 {
22317 if (total > 1000000)
22318 /* Do it differently for a large value, to avoid overflow. */
22319 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22320 else
22321 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22322 /* We can't normally display a 3-digit number,
22323 so get us a 2-digit number that is close. */
22324 if (total == 100)
22325 total = 99;
22326 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22327 return decode_mode_spec_buf;
22328 }
22329 }
22330
22331 /* Display percentage of size above the bottom of the screen. */
22332 case 'P':
22333 {
22334 ptrdiff_t toppos = marker_position (w->start);
22335 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22336 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22337
22338 if (botpos >= BUF_ZV (b))
22339 {
22340 if (toppos <= BUF_BEGV (b))
22341 return "All";
22342 else
22343 return "Bottom";
22344 }
22345 else
22346 {
22347 if (total > 1000000)
22348 /* Do it differently for a large value, to avoid overflow. */
22349 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22350 else
22351 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22352 /* We can't normally display a 3-digit number,
22353 so get us a 2-digit number that is close. */
22354 if (total == 100)
22355 total = 99;
22356 if (toppos <= BUF_BEGV (b))
22357 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22358 else
22359 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22360 return decode_mode_spec_buf;
22361 }
22362 }
22363
22364 case 's':
22365 /* status of process */
22366 obj = Fget_buffer_process (Fcurrent_buffer ());
22367 if (NILP (obj))
22368 return "no process";
22369 #ifndef MSDOS
22370 obj = Fsymbol_name (Fprocess_status (obj));
22371 #endif
22372 break;
22373
22374 case '@':
22375 {
22376 ptrdiff_t count = inhibit_garbage_collection ();
22377 Lisp_Object val = call1 (intern ("file-remote-p"),
22378 BVAR (current_buffer, directory));
22379 unbind_to (count, Qnil);
22380
22381 if (NILP (val))
22382 return "-";
22383 else
22384 return "@";
22385 }
22386
22387 case 'z':
22388 /* coding-system (not including end-of-line format) */
22389 case 'Z':
22390 /* coding-system (including end-of-line type) */
22391 {
22392 int eol_flag = (c == 'Z');
22393 char *p = decode_mode_spec_buf;
22394
22395 if (! FRAME_WINDOW_P (f))
22396 {
22397 /* No need to mention EOL here--the terminal never needs
22398 to do EOL conversion. */
22399 p = decode_mode_spec_coding (CODING_ID_NAME
22400 (FRAME_KEYBOARD_CODING (f)->id),
22401 p, 0);
22402 p = decode_mode_spec_coding (CODING_ID_NAME
22403 (FRAME_TERMINAL_CODING (f)->id),
22404 p, 0);
22405 }
22406 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22407 p, eol_flag);
22408
22409 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22410 #ifdef subprocesses
22411 obj = Fget_buffer_process (Fcurrent_buffer ());
22412 if (PROCESSP (obj))
22413 {
22414 p = decode_mode_spec_coding
22415 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22416 p = decode_mode_spec_coding
22417 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22418 }
22419 #endif /* subprocesses */
22420 #endif /* 0 */
22421 *p = 0;
22422 return decode_mode_spec_buf;
22423 }
22424 }
22425
22426 if (STRINGP (obj))
22427 {
22428 *string = obj;
22429 return SSDATA (obj);
22430 }
22431 else
22432 return "";
22433 }
22434
22435
22436 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22437 means count lines back from START_BYTE. But don't go beyond
22438 LIMIT_BYTE. Return the number of lines thus found (always
22439 nonnegative).
22440
22441 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22442 either the position COUNT lines after/before START_BYTE, if we
22443 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22444 COUNT lines. */
22445
22446 static ptrdiff_t
22447 display_count_lines (ptrdiff_t start_byte,
22448 ptrdiff_t limit_byte, ptrdiff_t count,
22449 ptrdiff_t *byte_pos_ptr)
22450 {
22451 register unsigned char *cursor;
22452 unsigned char *base;
22453
22454 register ptrdiff_t ceiling;
22455 register unsigned char *ceiling_addr;
22456 ptrdiff_t orig_count = count;
22457
22458 /* If we are not in selective display mode,
22459 check only for newlines. */
22460 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22461 && !INTEGERP (BVAR (current_buffer, selective_display)));
22462
22463 if (count > 0)
22464 {
22465 while (start_byte < limit_byte)
22466 {
22467 ceiling = BUFFER_CEILING_OF (start_byte);
22468 ceiling = min (limit_byte - 1, ceiling);
22469 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22470 base = (cursor = BYTE_POS_ADDR (start_byte));
22471
22472 do
22473 {
22474 if (selective_display)
22475 {
22476 while (*cursor != '\n' && *cursor != 015
22477 && ++cursor != ceiling_addr)
22478 continue;
22479 if (cursor == ceiling_addr)
22480 break;
22481 }
22482 else
22483 {
22484 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22485 if (! cursor)
22486 break;
22487 }
22488
22489 cursor++;
22490
22491 if (--count == 0)
22492 {
22493 start_byte += cursor - base;
22494 *byte_pos_ptr = start_byte;
22495 return orig_count;
22496 }
22497 }
22498 while (cursor < ceiling_addr);
22499
22500 start_byte += ceiling_addr - base;
22501 }
22502 }
22503 else
22504 {
22505 while (start_byte > limit_byte)
22506 {
22507 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22508 ceiling = max (limit_byte, ceiling);
22509 ceiling_addr = BYTE_POS_ADDR (ceiling);
22510 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22511 while (1)
22512 {
22513 if (selective_display)
22514 {
22515 while (--cursor >= ceiling_addr
22516 && *cursor != '\n' && *cursor != 015)
22517 continue;
22518 if (cursor < ceiling_addr)
22519 break;
22520 }
22521 else
22522 {
22523 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22524 if (! cursor)
22525 break;
22526 }
22527
22528 if (++count == 0)
22529 {
22530 start_byte += cursor - base + 1;
22531 *byte_pos_ptr = start_byte;
22532 /* When scanning backwards, we should
22533 not count the newline posterior to which we stop. */
22534 return - orig_count - 1;
22535 }
22536 }
22537 start_byte += ceiling_addr - base;
22538 }
22539 }
22540
22541 *byte_pos_ptr = limit_byte;
22542
22543 if (count < 0)
22544 return - orig_count + count;
22545 return orig_count - count;
22546
22547 }
22548
22549
22550 \f
22551 /***********************************************************************
22552 Displaying strings
22553 ***********************************************************************/
22554
22555 /* Display a NUL-terminated string, starting with index START.
22556
22557 If STRING is non-null, display that C string. Otherwise, the Lisp
22558 string LISP_STRING is displayed. There's a case that STRING is
22559 non-null and LISP_STRING is not nil. It means STRING is a string
22560 data of LISP_STRING. In that case, we display LISP_STRING while
22561 ignoring its text properties.
22562
22563 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22564 FACE_STRING. Display STRING or LISP_STRING with the face at
22565 FACE_STRING_POS in FACE_STRING:
22566
22567 Display the string in the environment given by IT, but use the
22568 standard display table, temporarily.
22569
22570 FIELD_WIDTH is the minimum number of output glyphs to produce.
22571 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22572 with spaces. If STRING has more characters, more than FIELD_WIDTH
22573 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22574
22575 PRECISION is the maximum number of characters to output from
22576 STRING. PRECISION < 0 means don't truncate the string.
22577
22578 This is roughly equivalent to printf format specifiers:
22579
22580 FIELD_WIDTH PRECISION PRINTF
22581 ----------------------------------------
22582 -1 -1 %s
22583 -1 10 %.10s
22584 10 -1 %10s
22585 20 10 %20.10s
22586
22587 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22588 display them, and < 0 means obey the current buffer's value of
22589 enable_multibyte_characters.
22590
22591 Value is the number of columns displayed. */
22592
22593 static int
22594 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22595 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22596 int field_width, int precision, int max_x, int multibyte)
22597 {
22598 int hpos_at_start = it->hpos;
22599 int saved_face_id = it->face_id;
22600 struct glyph_row *row = it->glyph_row;
22601 ptrdiff_t it_charpos;
22602
22603 /* Initialize the iterator IT for iteration over STRING beginning
22604 with index START. */
22605 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22606 precision, field_width, multibyte);
22607 if (string && STRINGP (lisp_string))
22608 /* LISP_STRING is the one returned by decode_mode_spec. We should
22609 ignore its text properties. */
22610 it->stop_charpos = it->end_charpos;
22611
22612 /* If displaying STRING, set up the face of the iterator from
22613 FACE_STRING, if that's given. */
22614 if (STRINGP (face_string))
22615 {
22616 ptrdiff_t endptr;
22617 struct face *face;
22618
22619 it->face_id
22620 = face_at_string_position (it->w, face_string, face_string_pos,
22621 0, &endptr, it->base_face_id, 0);
22622 face = FACE_FROM_ID (it->f, it->face_id);
22623 it->face_box_p = face->box != FACE_NO_BOX;
22624 }
22625
22626 /* Set max_x to the maximum allowed X position. Don't let it go
22627 beyond the right edge of the window. */
22628 if (max_x <= 0)
22629 max_x = it->last_visible_x;
22630 else
22631 max_x = min (max_x, it->last_visible_x);
22632
22633 /* Skip over display elements that are not visible. because IT->w is
22634 hscrolled. */
22635 if (it->current_x < it->first_visible_x)
22636 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22637 MOVE_TO_POS | MOVE_TO_X);
22638
22639 row->ascent = it->max_ascent;
22640 row->height = it->max_ascent + it->max_descent;
22641 row->phys_ascent = it->max_phys_ascent;
22642 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22643 row->extra_line_spacing = it->max_extra_line_spacing;
22644
22645 if (STRINGP (it->string))
22646 it_charpos = IT_STRING_CHARPOS (*it);
22647 else
22648 it_charpos = IT_CHARPOS (*it);
22649
22650 /* This condition is for the case that we are called with current_x
22651 past last_visible_x. */
22652 while (it->current_x < max_x)
22653 {
22654 int x_before, x, n_glyphs_before, i, nglyphs;
22655
22656 /* Get the next display element. */
22657 if (!get_next_display_element (it))
22658 break;
22659
22660 /* Produce glyphs. */
22661 x_before = it->current_x;
22662 n_glyphs_before = row->used[TEXT_AREA];
22663 PRODUCE_GLYPHS (it);
22664
22665 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22666 i = 0;
22667 x = x_before;
22668 while (i < nglyphs)
22669 {
22670 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22671
22672 if (it->line_wrap != TRUNCATE
22673 && x + glyph->pixel_width > max_x)
22674 {
22675 /* End of continued line or max_x reached. */
22676 if (CHAR_GLYPH_PADDING_P (*glyph))
22677 {
22678 /* A wide character is unbreakable. */
22679 if (row->reversed_p)
22680 unproduce_glyphs (it, row->used[TEXT_AREA]
22681 - n_glyphs_before);
22682 row->used[TEXT_AREA] = n_glyphs_before;
22683 it->current_x = x_before;
22684 }
22685 else
22686 {
22687 if (row->reversed_p)
22688 unproduce_glyphs (it, row->used[TEXT_AREA]
22689 - (n_glyphs_before + i));
22690 row->used[TEXT_AREA] = n_glyphs_before + i;
22691 it->current_x = x;
22692 }
22693 break;
22694 }
22695 else if (x + glyph->pixel_width >= it->first_visible_x)
22696 {
22697 /* Glyph is at least partially visible. */
22698 ++it->hpos;
22699 if (x < it->first_visible_x)
22700 row->x = x - it->first_visible_x;
22701 }
22702 else
22703 {
22704 /* Glyph is off the left margin of the display area.
22705 Should not happen. */
22706 emacs_abort ();
22707 }
22708
22709 row->ascent = max (row->ascent, it->max_ascent);
22710 row->height = max (row->height, it->max_ascent + it->max_descent);
22711 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22712 row->phys_height = max (row->phys_height,
22713 it->max_phys_ascent + it->max_phys_descent);
22714 row->extra_line_spacing = max (row->extra_line_spacing,
22715 it->max_extra_line_spacing);
22716 x += glyph->pixel_width;
22717 ++i;
22718 }
22719
22720 /* Stop if max_x reached. */
22721 if (i < nglyphs)
22722 break;
22723
22724 /* Stop at line ends. */
22725 if (ITERATOR_AT_END_OF_LINE_P (it))
22726 {
22727 it->continuation_lines_width = 0;
22728 break;
22729 }
22730
22731 set_iterator_to_next (it, 1);
22732 if (STRINGP (it->string))
22733 it_charpos = IT_STRING_CHARPOS (*it);
22734 else
22735 it_charpos = IT_CHARPOS (*it);
22736
22737 /* Stop if truncating at the right edge. */
22738 if (it->line_wrap == TRUNCATE
22739 && it->current_x >= it->last_visible_x)
22740 {
22741 /* Add truncation mark, but don't do it if the line is
22742 truncated at a padding space. */
22743 if (it_charpos < it->string_nchars)
22744 {
22745 if (!FRAME_WINDOW_P (it->f))
22746 {
22747 int ii, n;
22748
22749 if (it->current_x > it->last_visible_x)
22750 {
22751 if (!row->reversed_p)
22752 {
22753 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22754 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22755 break;
22756 }
22757 else
22758 {
22759 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22760 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22761 break;
22762 unproduce_glyphs (it, ii + 1);
22763 ii = row->used[TEXT_AREA] - (ii + 1);
22764 }
22765 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22766 {
22767 row->used[TEXT_AREA] = ii;
22768 produce_special_glyphs (it, IT_TRUNCATION);
22769 }
22770 }
22771 produce_special_glyphs (it, IT_TRUNCATION);
22772 }
22773 row->truncated_on_right_p = 1;
22774 }
22775 break;
22776 }
22777 }
22778
22779 /* Maybe insert a truncation at the left. */
22780 if (it->first_visible_x
22781 && it_charpos > 0)
22782 {
22783 if (!FRAME_WINDOW_P (it->f)
22784 || (row->reversed_p
22785 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22786 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22787 insert_left_trunc_glyphs (it);
22788 row->truncated_on_left_p = 1;
22789 }
22790
22791 it->face_id = saved_face_id;
22792
22793 /* Value is number of columns displayed. */
22794 return it->hpos - hpos_at_start;
22795 }
22796
22797
22798 \f
22799 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22800 appears as an element of LIST or as the car of an element of LIST.
22801 If PROPVAL is a list, compare each element against LIST in that
22802 way, and return 1/2 if any element of PROPVAL is found in LIST.
22803 Otherwise return 0. This function cannot quit.
22804 The return value is 2 if the text is invisible but with an ellipsis
22805 and 1 if it's invisible and without an ellipsis. */
22806
22807 int
22808 invisible_p (register Lisp_Object propval, Lisp_Object list)
22809 {
22810 register Lisp_Object tail, proptail;
22811
22812 for (tail = list; CONSP (tail); tail = XCDR (tail))
22813 {
22814 register Lisp_Object tem;
22815 tem = XCAR (tail);
22816 if (EQ (propval, tem))
22817 return 1;
22818 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22819 return NILP (XCDR (tem)) ? 1 : 2;
22820 }
22821
22822 if (CONSP (propval))
22823 {
22824 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22825 {
22826 Lisp_Object propelt;
22827 propelt = XCAR (proptail);
22828 for (tail = list; CONSP (tail); tail = XCDR (tail))
22829 {
22830 register Lisp_Object tem;
22831 tem = XCAR (tail);
22832 if (EQ (propelt, tem))
22833 return 1;
22834 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22835 return NILP (XCDR (tem)) ? 1 : 2;
22836 }
22837 }
22838 }
22839
22840 return 0;
22841 }
22842
22843 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22844 doc: /* Non-nil if the property makes the text invisible.
22845 POS-OR-PROP can be a marker or number, in which case it is taken to be
22846 a position in the current buffer and the value of the `invisible' property
22847 is checked; or it can be some other value, which is then presumed to be the
22848 value of the `invisible' property of the text of interest.
22849 The non-nil value returned can be t for truly invisible text or something
22850 else if the text is replaced by an ellipsis. */)
22851 (Lisp_Object pos_or_prop)
22852 {
22853 Lisp_Object prop
22854 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22855 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22856 : pos_or_prop);
22857 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22858 return (invis == 0 ? Qnil
22859 : invis == 1 ? Qt
22860 : make_number (invis));
22861 }
22862
22863 /* Calculate a width or height in pixels from a specification using
22864 the following elements:
22865
22866 SPEC ::=
22867 NUM - a (fractional) multiple of the default font width/height
22868 (NUM) - specifies exactly NUM pixels
22869 UNIT - a fixed number of pixels, see below.
22870 ELEMENT - size of a display element in pixels, see below.
22871 (NUM . SPEC) - equals NUM * SPEC
22872 (+ SPEC SPEC ...) - add pixel values
22873 (- SPEC SPEC ...) - subtract pixel values
22874 (- SPEC) - negate pixel value
22875
22876 NUM ::=
22877 INT or FLOAT - a number constant
22878 SYMBOL - use symbol's (buffer local) variable binding.
22879
22880 UNIT ::=
22881 in - pixels per inch *)
22882 mm - pixels per 1/1000 meter *)
22883 cm - pixels per 1/100 meter *)
22884 width - width of current font in pixels.
22885 height - height of current font in pixels.
22886
22887 *) using the ratio(s) defined in display-pixels-per-inch.
22888
22889 ELEMENT ::=
22890
22891 left-fringe - left fringe width in pixels
22892 right-fringe - right fringe width in pixels
22893
22894 left-margin - left margin width in pixels
22895 right-margin - right margin width in pixels
22896
22897 scroll-bar - scroll-bar area width in pixels
22898
22899 Examples:
22900
22901 Pixels corresponding to 5 inches:
22902 (5 . in)
22903
22904 Total width of non-text areas on left side of window (if scroll-bar is on left):
22905 '(space :width (+ left-fringe left-margin scroll-bar))
22906
22907 Align to first text column (in header line):
22908 '(space :align-to 0)
22909
22910 Align to middle of text area minus half the width of variable `my-image'
22911 containing a loaded image:
22912 '(space :align-to (0.5 . (- text my-image)))
22913
22914 Width of left margin minus width of 1 character in the default font:
22915 '(space :width (- left-margin 1))
22916
22917 Width of left margin minus width of 2 characters in the current font:
22918 '(space :width (- left-margin (2 . width)))
22919
22920 Center 1 character over left-margin (in header line):
22921 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22922
22923 Different ways to express width of left fringe plus left margin minus one pixel:
22924 '(space :width (- (+ left-fringe left-margin) (1)))
22925 '(space :width (+ left-fringe left-margin (- (1))))
22926 '(space :width (+ left-fringe left-margin (-1)))
22927
22928 */
22929
22930 static int
22931 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22932 struct font *font, int width_p, int *align_to)
22933 {
22934 double pixels;
22935
22936 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22937 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22938
22939 if (NILP (prop))
22940 return OK_PIXELS (0);
22941
22942 eassert (FRAME_LIVE_P (it->f));
22943
22944 if (SYMBOLP (prop))
22945 {
22946 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22947 {
22948 char *unit = SSDATA (SYMBOL_NAME (prop));
22949
22950 if (unit[0] == 'i' && unit[1] == 'n')
22951 pixels = 1.0;
22952 else if (unit[0] == 'm' && unit[1] == 'm')
22953 pixels = 25.4;
22954 else if (unit[0] == 'c' && unit[1] == 'm')
22955 pixels = 2.54;
22956 else
22957 pixels = 0;
22958 if (pixels > 0)
22959 {
22960 double ppi = (width_p ? FRAME_RES_X (it->f)
22961 : FRAME_RES_Y (it->f));
22962
22963 if (ppi > 0)
22964 return OK_PIXELS (ppi / pixels);
22965 return 0;
22966 }
22967 }
22968
22969 #ifdef HAVE_WINDOW_SYSTEM
22970 if (EQ (prop, Qheight))
22971 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22972 if (EQ (prop, Qwidth))
22973 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22974 #else
22975 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22976 return OK_PIXELS (1);
22977 #endif
22978
22979 if (EQ (prop, Qtext))
22980 return OK_PIXELS (width_p
22981 ? window_box_width (it->w, TEXT_AREA)
22982 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22983
22984 if (align_to && *align_to < 0)
22985 {
22986 *res = 0;
22987 if (EQ (prop, Qleft))
22988 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22989 if (EQ (prop, Qright))
22990 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22991 if (EQ (prop, Qcenter))
22992 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22993 + window_box_width (it->w, TEXT_AREA) / 2);
22994 if (EQ (prop, Qleft_fringe))
22995 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22996 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22997 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22998 if (EQ (prop, Qright_fringe))
22999 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23000 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23001 : window_box_right_offset (it->w, TEXT_AREA));
23002 if (EQ (prop, Qleft_margin))
23003 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23004 if (EQ (prop, Qright_margin))
23005 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23006 if (EQ (prop, Qscroll_bar))
23007 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23008 ? 0
23009 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23010 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23011 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23012 : 0)));
23013 }
23014 else
23015 {
23016 if (EQ (prop, Qleft_fringe))
23017 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23018 if (EQ (prop, Qright_fringe))
23019 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23020 if (EQ (prop, Qleft_margin))
23021 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23022 if (EQ (prop, Qright_margin))
23023 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23024 if (EQ (prop, Qscroll_bar))
23025 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23026 }
23027
23028 prop = buffer_local_value_1 (prop, it->w->contents);
23029 if (EQ (prop, Qunbound))
23030 prop = Qnil;
23031 }
23032
23033 if (INTEGERP (prop) || FLOATP (prop))
23034 {
23035 int base_unit = (width_p
23036 ? FRAME_COLUMN_WIDTH (it->f)
23037 : FRAME_LINE_HEIGHT (it->f));
23038 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23039 }
23040
23041 if (CONSP (prop))
23042 {
23043 Lisp_Object car = XCAR (prop);
23044 Lisp_Object cdr = XCDR (prop);
23045
23046 if (SYMBOLP (car))
23047 {
23048 #ifdef HAVE_WINDOW_SYSTEM
23049 if (FRAME_WINDOW_P (it->f)
23050 && valid_image_p (prop))
23051 {
23052 ptrdiff_t id = lookup_image (it->f, prop);
23053 struct image *img = IMAGE_FROM_ID (it->f, id);
23054
23055 return OK_PIXELS (width_p ? img->width : img->height);
23056 }
23057 #endif
23058 if (EQ (car, Qplus) || EQ (car, Qminus))
23059 {
23060 int first = 1;
23061 double px;
23062
23063 pixels = 0;
23064 while (CONSP (cdr))
23065 {
23066 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23067 font, width_p, align_to))
23068 return 0;
23069 if (first)
23070 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23071 else
23072 pixels += px;
23073 cdr = XCDR (cdr);
23074 }
23075 if (EQ (car, Qminus))
23076 pixels = -pixels;
23077 return OK_PIXELS (pixels);
23078 }
23079
23080 car = buffer_local_value_1 (car, it->w->contents);
23081 if (EQ (car, Qunbound))
23082 car = Qnil;
23083 }
23084
23085 if (INTEGERP (car) || FLOATP (car))
23086 {
23087 double fact;
23088 pixels = XFLOATINT (car);
23089 if (NILP (cdr))
23090 return OK_PIXELS (pixels);
23091 if (calc_pixel_width_or_height (&fact, it, cdr,
23092 font, width_p, align_to))
23093 return OK_PIXELS (pixels * fact);
23094 return 0;
23095 }
23096
23097 return 0;
23098 }
23099
23100 return 0;
23101 }
23102
23103 \f
23104 /***********************************************************************
23105 Glyph Display
23106 ***********************************************************************/
23107
23108 #ifdef HAVE_WINDOW_SYSTEM
23109
23110 #ifdef GLYPH_DEBUG
23111
23112 void
23113 dump_glyph_string (struct glyph_string *s)
23114 {
23115 fprintf (stderr, "glyph string\n");
23116 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23117 s->x, s->y, s->width, s->height);
23118 fprintf (stderr, " ybase = %d\n", s->ybase);
23119 fprintf (stderr, " hl = %d\n", s->hl);
23120 fprintf (stderr, " left overhang = %d, right = %d\n",
23121 s->left_overhang, s->right_overhang);
23122 fprintf (stderr, " nchars = %d\n", s->nchars);
23123 fprintf (stderr, " extends to end of line = %d\n",
23124 s->extends_to_end_of_line_p);
23125 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23126 fprintf (stderr, " bg width = %d\n", s->background_width);
23127 }
23128
23129 #endif /* GLYPH_DEBUG */
23130
23131 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23132 of XChar2b structures for S; it can't be allocated in
23133 init_glyph_string because it must be allocated via `alloca'. W
23134 is the window on which S is drawn. ROW and AREA are the glyph row
23135 and area within the row from which S is constructed. START is the
23136 index of the first glyph structure covered by S. HL is a
23137 face-override for drawing S. */
23138
23139 #ifdef HAVE_NTGUI
23140 #define OPTIONAL_HDC(hdc) HDC hdc,
23141 #define DECLARE_HDC(hdc) HDC hdc;
23142 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23143 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23144 #endif
23145
23146 #ifndef OPTIONAL_HDC
23147 #define OPTIONAL_HDC(hdc)
23148 #define DECLARE_HDC(hdc)
23149 #define ALLOCATE_HDC(hdc, f)
23150 #define RELEASE_HDC(hdc, f)
23151 #endif
23152
23153 static void
23154 init_glyph_string (struct glyph_string *s,
23155 OPTIONAL_HDC (hdc)
23156 XChar2b *char2b, struct window *w, struct glyph_row *row,
23157 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23158 {
23159 memset (s, 0, sizeof *s);
23160 s->w = w;
23161 s->f = XFRAME (w->frame);
23162 #ifdef HAVE_NTGUI
23163 s->hdc = hdc;
23164 #endif
23165 s->display = FRAME_X_DISPLAY (s->f);
23166 s->window = FRAME_X_WINDOW (s->f);
23167 s->char2b = char2b;
23168 s->hl = hl;
23169 s->row = row;
23170 s->area = area;
23171 s->first_glyph = row->glyphs[area] + start;
23172 s->height = row->height;
23173 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23174 s->ybase = s->y + row->ascent;
23175 }
23176
23177
23178 /* Append the list of glyph strings with head H and tail T to the list
23179 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23180
23181 static void
23182 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23183 struct glyph_string *h, struct glyph_string *t)
23184 {
23185 if (h)
23186 {
23187 if (*head)
23188 (*tail)->next = h;
23189 else
23190 *head = h;
23191 h->prev = *tail;
23192 *tail = t;
23193 }
23194 }
23195
23196
23197 /* Prepend the list of glyph strings with head H and tail T to the
23198 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23199 result. */
23200
23201 static void
23202 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23203 struct glyph_string *h, struct glyph_string *t)
23204 {
23205 if (h)
23206 {
23207 if (*head)
23208 (*head)->prev = t;
23209 else
23210 *tail = t;
23211 t->next = *head;
23212 *head = h;
23213 }
23214 }
23215
23216
23217 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23218 Set *HEAD and *TAIL to the resulting list. */
23219
23220 static void
23221 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23222 struct glyph_string *s)
23223 {
23224 s->next = s->prev = NULL;
23225 append_glyph_string_lists (head, tail, s, s);
23226 }
23227
23228
23229 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23230 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23231 make sure that X resources for the face returned are allocated.
23232 Value is a pointer to a realized face that is ready for display if
23233 DISPLAY_P is non-zero. */
23234
23235 static struct face *
23236 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23237 XChar2b *char2b, int display_p)
23238 {
23239 struct face *face = FACE_FROM_ID (f, face_id);
23240 unsigned code = 0;
23241
23242 if (face->font)
23243 {
23244 code = face->font->driver->encode_char (face->font, c);
23245
23246 if (code == FONT_INVALID_CODE)
23247 code = 0;
23248 }
23249 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23250
23251 /* Make sure X resources of the face are allocated. */
23252 #ifdef HAVE_X_WINDOWS
23253 if (display_p)
23254 #endif
23255 {
23256 eassert (face != NULL);
23257 PREPARE_FACE_FOR_DISPLAY (f, face);
23258 }
23259
23260 return face;
23261 }
23262
23263
23264 /* Get face and two-byte form of character glyph GLYPH on frame F.
23265 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23266 a pointer to a realized face that is ready for display. */
23267
23268 static struct face *
23269 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23270 XChar2b *char2b, int *two_byte_p)
23271 {
23272 struct face *face;
23273 unsigned code = 0;
23274
23275 eassert (glyph->type == CHAR_GLYPH);
23276 face = FACE_FROM_ID (f, glyph->face_id);
23277
23278 /* Make sure X resources of the face are allocated. */
23279 eassert (face != NULL);
23280 PREPARE_FACE_FOR_DISPLAY (f, face);
23281
23282 if (two_byte_p)
23283 *two_byte_p = 0;
23284
23285 if (face->font)
23286 {
23287 if (CHAR_BYTE8_P (glyph->u.ch))
23288 code = CHAR_TO_BYTE8 (glyph->u.ch);
23289 else
23290 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23291
23292 if (code == FONT_INVALID_CODE)
23293 code = 0;
23294 }
23295
23296 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23297 return face;
23298 }
23299
23300
23301 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23302 Return 1 if FONT has a glyph for C, otherwise return 0. */
23303
23304 static int
23305 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23306 {
23307 unsigned code;
23308
23309 if (CHAR_BYTE8_P (c))
23310 code = CHAR_TO_BYTE8 (c);
23311 else
23312 code = font->driver->encode_char (font, c);
23313
23314 if (code == FONT_INVALID_CODE)
23315 return 0;
23316 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23317 return 1;
23318 }
23319
23320
23321 /* Fill glyph string S with composition components specified by S->cmp.
23322
23323 BASE_FACE is the base face of the composition.
23324 S->cmp_from is the index of the first component for S.
23325
23326 OVERLAPS non-zero means S should draw the foreground only, and use
23327 its physical height for clipping. See also draw_glyphs.
23328
23329 Value is the index of a component not in S. */
23330
23331 static int
23332 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23333 int overlaps)
23334 {
23335 int i;
23336 /* For all glyphs of this composition, starting at the offset
23337 S->cmp_from, until we reach the end of the definition or encounter a
23338 glyph that requires the different face, add it to S. */
23339 struct face *face;
23340
23341 eassert (s);
23342
23343 s->for_overlaps = overlaps;
23344 s->face = NULL;
23345 s->font = NULL;
23346 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23347 {
23348 int c = COMPOSITION_GLYPH (s->cmp, i);
23349
23350 /* TAB in a composition means display glyphs with padding space
23351 on the left or right. */
23352 if (c != '\t')
23353 {
23354 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23355 -1, Qnil);
23356
23357 face = get_char_face_and_encoding (s->f, c, face_id,
23358 s->char2b + i, 1);
23359 if (face)
23360 {
23361 if (! s->face)
23362 {
23363 s->face = face;
23364 s->font = s->face->font;
23365 }
23366 else if (s->face != face)
23367 break;
23368 }
23369 }
23370 ++s->nchars;
23371 }
23372 s->cmp_to = i;
23373
23374 if (s->face == NULL)
23375 {
23376 s->face = base_face->ascii_face;
23377 s->font = s->face->font;
23378 }
23379
23380 /* All glyph strings for the same composition has the same width,
23381 i.e. the width set for the first component of the composition. */
23382 s->width = s->first_glyph->pixel_width;
23383
23384 /* If the specified font could not be loaded, use the frame's
23385 default font, but record the fact that we couldn't load it in
23386 the glyph string so that we can draw rectangles for the
23387 characters of the glyph string. */
23388 if (s->font == NULL)
23389 {
23390 s->font_not_found_p = 1;
23391 s->font = FRAME_FONT (s->f);
23392 }
23393
23394 /* Adjust base line for subscript/superscript text. */
23395 s->ybase += s->first_glyph->voffset;
23396
23397 /* This glyph string must always be drawn with 16-bit functions. */
23398 s->two_byte_p = 1;
23399
23400 return s->cmp_to;
23401 }
23402
23403 static int
23404 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23405 int start, int end, int overlaps)
23406 {
23407 struct glyph *glyph, *last;
23408 Lisp_Object lgstring;
23409 int i;
23410
23411 s->for_overlaps = overlaps;
23412 glyph = s->row->glyphs[s->area] + start;
23413 last = s->row->glyphs[s->area] + end;
23414 s->cmp_id = glyph->u.cmp.id;
23415 s->cmp_from = glyph->slice.cmp.from;
23416 s->cmp_to = glyph->slice.cmp.to + 1;
23417 s->face = FACE_FROM_ID (s->f, face_id);
23418 lgstring = composition_gstring_from_id (s->cmp_id);
23419 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23420 glyph++;
23421 while (glyph < last
23422 && glyph->u.cmp.automatic
23423 && glyph->u.cmp.id == s->cmp_id
23424 && s->cmp_to == glyph->slice.cmp.from)
23425 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23426
23427 for (i = s->cmp_from; i < s->cmp_to; i++)
23428 {
23429 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23430 unsigned code = LGLYPH_CODE (lglyph);
23431
23432 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23433 }
23434 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23435 return glyph - s->row->glyphs[s->area];
23436 }
23437
23438
23439 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23440 See the comment of fill_glyph_string for arguments.
23441 Value is the index of the first glyph not in S. */
23442
23443
23444 static int
23445 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23446 int start, int end, int overlaps)
23447 {
23448 struct glyph *glyph, *last;
23449 int voffset;
23450
23451 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23452 s->for_overlaps = overlaps;
23453 glyph = s->row->glyphs[s->area] + start;
23454 last = s->row->glyphs[s->area] + end;
23455 voffset = glyph->voffset;
23456 s->face = FACE_FROM_ID (s->f, face_id);
23457 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23458 s->nchars = 1;
23459 s->width = glyph->pixel_width;
23460 glyph++;
23461 while (glyph < last
23462 && glyph->type == GLYPHLESS_GLYPH
23463 && glyph->voffset == voffset
23464 && glyph->face_id == face_id)
23465 {
23466 s->nchars++;
23467 s->width += glyph->pixel_width;
23468 glyph++;
23469 }
23470 s->ybase += voffset;
23471 return glyph - s->row->glyphs[s->area];
23472 }
23473
23474
23475 /* Fill glyph string S from a sequence of character glyphs.
23476
23477 FACE_ID is the face id of the string. START is the index of the
23478 first glyph to consider, END is the index of the last + 1.
23479 OVERLAPS non-zero means S should draw the foreground only, and use
23480 its physical height for clipping. See also draw_glyphs.
23481
23482 Value is the index of the first glyph not in S. */
23483
23484 static int
23485 fill_glyph_string (struct glyph_string *s, int face_id,
23486 int start, int end, int overlaps)
23487 {
23488 struct glyph *glyph, *last;
23489 int voffset;
23490 int glyph_not_available_p;
23491
23492 eassert (s->f == XFRAME (s->w->frame));
23493 eassert (s->nchars == 0);
23494 eassert (start >= 0 && end > start);
23495
23496 s->for_overlaps = overlaps;
23497 glyph = s->row->glyphs[s->area] + start;
23498 last = s->row->glyphs[s->area] + end;
23499 voffset = glyph->voffset;
23500 s->padding_p = glyph->padding_p;
23501 glyph_not_available_p = glyph->glyph_not_available_p;
23502
23503 while (glyph < last
23504 && glyph->type == CHAR_GLYPH
23505 && glyph->voffset == voffset
23506 /* Same face id implies same font, nowadays. */
23507 && glyph->face_id == face_id
23508 && glyph->glyph_not_available_p == glyph_not_available_p)
23509 {
23510 int two_byte_p;
23511
23512 s->face = get_glyph_face_and_encoding (s->f, glyph,
23513 s->char2b + s->nchars,
23514 &two_byte_p);
23515 s->two_byte_p = two_byte_p;
23516 ++s->nchars;
23517 eassert (s->nchars <= end - start);
23518 s->width += glyph->pixel_width;
23519 if (glyph++->padding_p != s->padding_p)
23520 break;
23521 }
23522
23523 s->font = s->face->font;
23524
23525 /* If the specified font could not be loaded, use the frame's font,
23526 but record the fact that we couldn't load it in
23527 S->font_not_found_p so that we can draw rectangles for the
23528 characters of the glyph string. */
23529 if (s->font == NULL || glyph_not_available_p)
23530 {
23531 s->font_not_found_p = 1;
23532 s->font = FRAME_FONT (s->f);
23533 }
23534
23535 /* Adjust base line for subscript/superscript text. */
23536 s->ybase += voffset;
23537
23538 eassert (s->face && s->face->gc);
23539 return glyph - s->row->glyphs[s->area];
23540 }
23541
23542
23543 /* Fill glyph string S from image glyph S->first_glyph. */
23544
23545 static void
23546 fill_image_glyph_string (struct glyph_string *s)
23547 {
23548 eassert (s->first_glyph->type == IMAGE_GLYPH);
23549 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23550 eassert (s->img);
23551 s->slice = s->first_glyph->slice.img;
23552 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23553 s->font = s->face->font;
23554 s->width = s->first_glyph->pixel_width;
23555
23556 /* Adjust base line for subscript/superscript text. */
23557 s->ybase += s->first_glyph->voffset;
23558 }
23559
23560
23561 /* Fill glyph string S from a sequence of stretch glyphs.
23562
23563 START is the index of the first glyph to consider,
23564 END is the index of the last + 1.
23565
23566 Value is the index of the first glyph not in S. */
23567
23568 static int
23569 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23570 {
23571 struct glyph *glyph, *last;
23572 int voffset, face_id;
23573
23574 eassert (s->first_glyph->type == STRETCH_GLYPH);
23575
23576 glyph = s->row->glyphs[s->area] + start;
23577 last = s->row->glyphs[s->area] + end;
23578 face_id = glyph->face_id;
23579 s->face = FACE_FROM_ID (s->f, face_id);
23580 s->font = s->face->font;
23581 s->width = glyph->pixel_width;
23582 s->nchars = 1;
23583 voffset = glyph->voffset;
23584
23585 for (++glyph;
23586 (glyph < last
23587 && glyph->type == STRETCH_GLYPH
23588 && glyph->voffset == voffset
23589 && glyph->face_id == face_id);
23590 ++glyph)
23591 s->width += glyph->pixel_width;
23592
23593 /* Adjust base line for subscript/superscript text. */
23594 s->ybase += voffset;
23595
23596 /* The case that face->gc == 0 is handled when drawing the glyph
23597 string by calling PREPARE_FACE_FOR_DISPLAY. */
23598 eassert (s->face);
23599 return glyph - s->row->glyphs[s->area];
23600 }
23601
23602 static struct font_metrics *
23603 get_per_char_metric (struct font *font, XChar2b *char2b)
23604 {
23605 static struct font_metrics metrics;
23606 unsigned code;
23607
23608 if (! font)
23609 return NULL;
23610 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23611 if (code == FONT_INVALID_CODE)
23612 return NULL;
23613 font->driver->text_extents (font, &code, 1, &metrics);
23614 return &metrics;
23615 }
23616
23617 /* EXPORT for RIF:
23618 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23619 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23620 assumed to be zero. */
23621
23622 void
23623 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23624 {
23625 *left = *right = 0;
23626
23627 if (glyph->type == CHAR_GLYPH)
23628 {
23629 struct face *face;
23630 XChar2b char2b;
23631 struct font_metrics *pcm;
23632
23633 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23634 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23635 {
23636 if (pcm->rbearing > pcm->width)
23637 *right = pcm->rbearing - pcm->width;
23638 if (pcm->lbearing < 0)
23639 *left = -pcm->lbearing;
23640 }
23641 }
23642 else if (glyph->type == COMPOSITE_GLYPH)
23643 {
23644 if (! glyph->u.cmp.automatic)
23645 {
23646 struct composition *cmp = composition_table[glyph->u.cmp.id];
23647
23648 if (cmp->rbearing > cmp->pixel_width)
23649 *right = cmp->rbearing - cmp->pixel_width;
23650 if (cmp->lbearing < 0)
23651 *left = - cmp->lbearing;
23652 }
23653 else
23654 {
23655 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23656 struct font_metrics metrics;
23657
23658 composition_gstring_width (gstring, glyph->slice.cmp.from,
23659 glyph->slice.cmp.to + 1, &metrics);
23660 if (metrics.rbearing > metrics.width)
23661 *right = metrics.rbearing - metrics.width;
23662 if (metrics.lbearing < 0)
23663 *left = - metrics.lbearing;
23664 }
23665 }
23666 }
23667
23668
23669 /* Return the index of the first glyph preceding glyph string S that
23670 is overwritten by S because of S's left overhang. Value is -1
23671 if no glyphs are overwritten. */
23672
23673 static int
23674 left_overwritten (struct glyph_string *s)
23675 {
23676 int k;
23677
23678 if (s->left_overhang)
23679 {
23680 int x = 0, i;
23681 struct glyph *glyphs = s->row->glyphs[s->area];
23682 int first = s->first_glyph - glyphs;
23683
23684 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23685 x -= glyphs[i].pixel_width;
23686
23687 k = i + 1;
23688 }
23689 else
23690 k = -1;
23691
23692 return k;
23693 }
23694
23695
23696 /* Return the index of the first glyph preceding glyph string S that
23697 is overwriting S because of its right overhang. Value is -1 if no
23698 glyph in front of S overwrites S. */
23699
23700 static int
23701 left_overwriting (struct glyph_string *s)
23702 {
23703 int i, k, x;
23704 struct glyph *glyphs = s->row->glyphs[s->area];
23705 int first = s->first_glyph - glyphs;
23706
23707 k = -1;
23708 x = 0;
23709 for (i = first - 1; i >= 0; --i)
23710 {
23711 int left, right;
23712 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23713 if (x + right > 0)
23714 k = i;
23715 x -= glyphs[i].pixel_width;
23716 }
23717
23718 return k;
23719 }
23720
23721
23722 /* Return the index of the last glyph following glyph string S that is
23723 overwritten by S because of S's right overhang. Value is -1 if
23724 no such glyph is found. */
23725
23726 static int
23727 right_overwritten (struct glyph_string *s)
23728 {
23729 int k = -1;
23730
23731 if (s->right_overhang)
23732 {
23733 int x = 0, i;
23734 struct glyph *glyphs = s->row->glyphs[s->area];
23735 int first = (s->first_glyph - glyphs
23736 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23737 int end = s->row->used[s->area];
23738
23739 for (i = first; i < end && s->right_overhang > x; ++i)
23740 x += glyphs[i].pixel_width;
23741
23742 k = i;
23743 }
23744
23745 return k;
23746 }
23747
23748
23749 /* Return the index of the last glyph following glyph string S that
23750 overwrites S because of its left overhang. Value is negative
23751 if no such glyph is found. */
23752
23753 static int
23754 right_overwriting (struct glyph_string *s)
23755 {
23756 int i, k, x;
23757 int end = s->row->used[s->area];
23758 struct glyph *glyphs = s->row->glyphs[s->area];
23759 int first = (s->first_glyph - glyphs
23760 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23761
23762 k = -1;
23763 x = 0;
23764 for (i = first; i < end; ++i)
23765 {
23766 int left, right;
23767 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23768 if (x - left < 0)
23769 k = i;
23770 x += glyphs[i].pixel_width;
23771 }
23772
23773 return k;
23774 }
23775
23776
23777 /* Set background width of glyph string S. START is the index of the
23778 first glyph following S. LAST_X is the right-most x-position + 1
23779 in the drawing area. */
23780
23781 static void
23782 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23783 {
23784 /* If the face of this glyph string has to be drawn to the end of
23785 the drawing area, set S->extends_to_end_of_line_p. */
23786
23787 if (start == s->row->used[s->area]
23788 && s->area == TEXT_AREA
23789 && ((s->row->fill_line_p
23790 && (s->hl == DRAW_NORMAL_TEXT
23791 || s->hl == DRAW_IMAGE_RAISED
23792 || s->hl == DRAW_IMAGE_SUNKEN))
23793 || s->hl == DRAW_MOUSE_FACE))
23794 s->extends_to_end_of_line_p = 1;
23795
23796 /* If S extends its face to the end of the line, set its
23797 background_width to the distance to the right edge of the drawing
23798 area. */
23799 if (s->extends_to_end_of_line_p)
23800 s->background_width = last_x - s->x + 1;
23801 else
23802 s->background_width = s->width;
23803 }
23804
23805
23806 /* Compute overhangs and x-positions for glyph string S and its
23807 predecessors, or successors. X is the starting x-position for S.
23808 BACKWARD_P non-zero means process predecessors. */
23809
23810 static void
23811 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23812 {
23813 if (backward_p)
23814 {
23815 while (s)
23816 {
23817 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23818 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23819 x -= s->width;
23820 s->x = x;
23821 s = s->prev;
23822 }
23823 }
23824 else
23825 {
23826 while (s)
23827 {
23828 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23829 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23830 s->x = x;
23831 x += s->width;
23832 s = s->next;
23833 }
23834 }
23835 }
23836
23837
23838
23839 /* The following macros are only called from draw_glyphs below.
23840 They reference the following parameters of that function directly:
23841 `w', `row', `area', and `overlap_p'
23842 as well as the following local variables:
23843 `s', `f', and `hdc' (in W32) */
23844
23845 #ifdef HAVE_NTGUI
23846 /* On W32, silently add local `hdc' variable to argument list of
23847 init_glyph_string. */
23848 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23849 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23850 #else
23851 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23852 init_glyph_string (s, char2b, w, row, area, start, hl)
23853 #endif
23854
23855 /* Add a glyph string for a stretch glyph to the list of strings
23856 between HEAD and TAIL. START is the index of the stretch glyph in
23857 row area AREA of glyph row ROW. END is the index of the last glyph
23858 in that glyph row area. X is the current output position assigned
23859 to the new glyph string constructed. HL overrides that face of the
23860 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23861 is the right-most x-position of the drawing area. */
23862
23863 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23864 and below -- keep them on one line. */
23865 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23866 do \
23867 { \
23868 s = alloca (sizeof *s); \
23869 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23870 START = fill_stretch_glyph_string (s, START, END); \
23871 append_glyph_string (&HEAD, &TAIL, s); \
23872 s->x = (X); \
23873 } \
23874 while (0)
23875
23876
23877 /* Add a glyph string for an image glyph to the list of strings
23878 between HEAD and TAIL. START is the index of the image glyph in
23879 row area AREA of glyph row ROW. END is the index of the last glyph
23880 in that glyph row area. X is the current output position assigned
23881 to the new glyph string constructed. HL overrides that face of the
23882 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23883 is the right-most x-position of the drawing area. */
23884
23885 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23886 do \
23887 { \
23888 s = alloca (sizeof *s); \
23889 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23890 fill_image_glyph_string (s); \
23891 append_glyph_string (&HEAD, &TAIL, s); \
23892 ++START; \
23893 s->x = (X); \
23894 } \
23895 while (0)
23896
23897
23898 /* Add a glyph string for a sequence of character glyphs to the list
23899 of strings between HEAD and TAIL. START is the index of the first
23900 glyph in row area AREA of glyph row ROW that is part of the new
23901 glyph string. END is the index of the last glyph in that glyph row
23902 area. X is the current output position assigned to the new glyph
23903 string constructed. HL overrides that face of the glyph; e.g. it
23904 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23905 right-most x-position of the drawing area. */
23906
23907 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23908 do \
23909 { \
23910 int face_id; \
23911 XChar2b *char2b; \
23912 \
23913 face_id = (row)->glyphs[area][START].face_id; \
23914 \
23915 s = alloca (sizeof *s); \
23916 char2b = alloca ((END - START) * sizeof *char2b); \
23917 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23918 append_glyph_string (&HEAD, &TAIL, s); \
23919 s->x = (X); \
23920 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23921 } \
23922 while (0)
23923
23924
23925 /* Add a glyph string for a composite sequence to the list of strings
23926 between HEAD and TAIL. START is the index of the first glyph in
23927 row area AREA of glyph row ROW that is part of the new glyph
23928 string. END is the index of the last glyph in that glyph row area.
23929 X is the current output position assigned to the new glyph string
23930 constructed. HL overrides that face of the glyph; e.g. it is
23931 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23932 x-position of the drawing area. */
23933
23934 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23935 do { \
23936 int face_id = (row)->glyphs[area][START].face_id; \
23937 struct face *base_face = FACE_FROM_ID (f, face_id); \
23938 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23939 struct composition *cmp = composition_table[cmp_id]; \
23940 XChar2b *char2b; \
23941 struct glyph_string *first_s = NULL; \
23942 int n; \
23943 \
23944 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23945 \
23946 /* Make glyph_strings for each glyph sequence that is drawable by \
23947 the same face, and append them to HEAD/TAIL. */ \
23948 for (n = 0; n < cmp->glyph_len;) \
23949 { \
23950 s = alloca (sizeof *s); \
23951 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23952 append_glyph_string (&(HEAD), &(TAIL), s); \
23953 s->cmp = cmp; \
23954 s->cmp_from = n; \
23955 s->x = (X); \
23956 if (n == 0) \
23957 first_s = s; \
23958 n = fill_composite_glyph_string (s, base_face, overlaps); \
23959 } \
23960 \
23961 ++START; \
23962 s = first_s; \
23963 } while (0)
23964
23965
23966 /* Add a glyph string for a glyph-string sequence to the list of strings
23967 between HEAD and TAIL. */
23968
23969 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23970 do { \
23971 int face_id; \
23972 XChar2b *char2b; \
23973 Lisp_Object gstring; \
23974 \
23975 face_id = (row)->glyphs[area][START].face_id; \
23976 gstring = (composition_gstring_from_id \
23977 ((row)->glyphs[area][START].u.cmp.id)); \
23978 s = alloca (sizeof *s); \
23979 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23980 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23981 append_glyph_string (&(HEAD), &(TAIL), s); \
23982 s->x = (X); \
23983 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23984 } while (0)
23985
23986
23987 /* Add a glyph string for a sequence of glyphless character's glyphs
23988 to the list of strings between HEAD and TAIL. The meanings of
23989 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23990
23991 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23992 do \
23993 { \
23994 int face_id; \
23995 \
23996 face_id = (row)->glyphs[area][START].face_id; \
23997 \
23998 s = alloca (sizeof *s); \
23999 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24000 append_glyph_string (&HEAD, &TAIL, s); \
24001 s->x = (X); \
24002 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24003 overlaps); \
24004 } \
24005 while (0)
24006
24007
24008 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24009 of AREA of glyph row ROW on window W between indices START and END.
24010 HL overrides the face for drawing glyph strings, e.g. it is
24011 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24012 x-positions of the drawing area.
24013
24014 This is an ugly monster macro construct because we must use alloca
24015 to allocate glyph strings (because draw_glyphs can be called
24016 asynchronously). */
24017
24018 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24019 do \
24020 { \
24021 HEAD = TAIL = NULL; \
24022 while (START < END) \
24023 { \
24024 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24025 switch (first_glyph->type) \
24026 { \
24027 case CHAR_GLYPH: \
24028 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24029 HL, X, LAST_X); \
24030 break; \
24031 \
24032 case COMPOSITE_GLYPH: \
24033 if (first_glyph->u.cmp.automatic) \
24034 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24035 HL, X, LAST_X); \
24036 else \
24037 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24038 HL, X, LAST_X); \
24039 break; \
24040 \
24041 case STRETCH_GLYPH: \
24042 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24043 HL, X, LAST_X); \
24044 break; \
24045 \
24046 case IMAGE_GLYPH: \
24047 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24048 HL, X, LAST_X); \
24049 break; \
24050 \
24051 case GLYPHLESS_GLYPH: \
24052 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24053 HL, X, LAST_X); \
24054 break; \
24055 \
24056 default: \
24057 emacs_abort (); \
24058 } \
24059 \
24060 if (s) \
24061 { \
24062 set_glyph_string_background_width (s, START, LAST_X); \
24063 (X) += s->width; \
24064 } \
24065 } \
24066 } while (0)
24067
24068
24069 /* Draw glyphs between START and END in AREA of ROW on window W,
24070 starting at x-position X. X is relative to AREA in W. HL is a
24071 face-override with the following meaning:
24072
24073 DRAW_NORMAL_TEXT draw normally
24074 DRAW_CURSOR draw in cursor face
24075 DRAW_MOUSE_FACE draw in mouse face.
24076 DRAW_INVERSE_VIDEO draw in mode line face
24077 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24078 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24079
24080 If OVERLAPS is non-zero, draw only the foreground of characters and
24081 clip to the physical height of ROW. Non-zero value also defines
24082 the overlapping part to be drawn:
24083
24084 OVERLAPS_PRED overlap with preceding rows
24085 OVERLAPS_SUCC overlap with succeeding rows
24086 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24087 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24088
24089 Value is the x-position reached, relative to AREA of W. */
24090
24091 static int
24092 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24093 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24094 enum draw_glyphs_face hl, int overlaps)
24095 {
24096 struct glyph_string *head, *tail;
24097 struct glyph_string *s;
24098 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24099 int i, j, x_reached, last_x, area_left = 0;
24100 struct frame *f = XFRAME (WINDOW_FRAME (w));
24101 DECLARE_HDC (hdc);
24102
24103 ALLOCATE_HDC (hdc, f);
24104
24105 /* Let's rather be paranoid than getting a SEGV. */
24106 end = min (end, row->used[area]);
24107 start = clip_to_bounds (0, start, end);
24108
24109 /* Translate X to frame coordinates. Set last_x to the right
24110 end of the drawing area. */
24111 if (row->full_width_p)
24112 {
24113 /* X is relative to the left edge of W, without scroll bars
24114 or fringes. */
24115 area_left = WINDOW_LEFT_EDGE_X (w);
24116 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24117 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24118 }
24119 else
24120 {
24121 area_left = window_box_left (w, area);
24122 last_x = area_left + window_box_width (w, area);
24123 }
24124 x += area_left;
24125
24126 /* Build a doubly-linked list of glyph_string structures between
24127 head and tail from what we have to draw. Note that the macro
24128 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24129 the reason we use a separate variable `i'. */
24130 i = start;
24131 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24132 if (tail)
24133 x_reached = tail->x + tail->background_width;
24134 else
24135 x_reached = x;
24136
24137 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24138 the row, redraw some glyphs in front or following the glyph
24139 strings built above. */
24140 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24141 {
24142 struct glyph_string *h, *t;
24143 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24144 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24145 int check_mouse_face = 0;
24146 int dummy_x = 0;
24147
24148 /* If mouse highlighting is on, we may need to draw adjacent
24149 glyphs using mouse-face highlighting. */
24150 if (area == TEXT_AREA && row->mouse_face_p
24151 && hlinfo->mouse_face_beg_row >= 0
24152 && hlinfo->mouse_face_end_row >= 0)
24153 {
24154 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24155
24156 if (row_vpos >= hlinfo->mouse_face_beg_row
24157 && row_vpos <= hlinfo->mouse_face_end_row)
24158 {
24159 check_mouse_face = 1;
24160 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24161 ? hlinfo->mouse_face_beg_col : 0;
24162 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24163 ? hlinfo->mouse_face_end_col
24164 : row->used[TEXT_AREA];
24165 }
24166 }
24167
24168 /* Compute overhangs for all glyph strings. */
24169 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24170 for (s = head; s; s = s->next)
24171 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24172
24173 /* Prepend glyph strings for glyphs in front of the first glyph
24174 string that are overwritten because of the first glyph
24175 string's left overhang. The background of all strings
24176 prepended must be drawn because the first glyph string
24177 draws over it. */
24178 i = left_overwritten (head);
24179 if (i >= 0)
24180 {
24181 enum draw_glyphs_face overlap_hl;
24182
24183 /* If this row contains mouse highlighting, attempt to draw
24184 the overlapped glyphs with the correct highlight. This
24185 code fails if the overlap encompasses more than one glyph
24186 and mouse-highlight spans only some of these glyphs.
24187 However, making it work perfectly involves a lot more
24188 code, and I don't know if the pathological case occurs in
24189 practice, so we'll stick to this for now. --- cyd */
24190 if (check_mouse_face
24191 && mouse_beg_col < start && mouse_end_col > i)
24192 overlap_hl = DRAW_MOUSE_FACE;
24193 else
24194 overlap_hl = DRAW_NORMAL_TEXT;
24195
24196 j = i;
24197 BUILD_GLYPH_STRINGS (j, start, h, t,
24198 overlap_hl, dummy_x, last_x);
24199 start = i;
24200 compute_overhangs_and_x (t, head->x, 1);
24201 prepend_glyph_string_lists (&head, &tail, h, t);
24202 clip_head = head;
24203 }
24204
24205 /* Prepend glyph strings for glyphs in front of the first glyph
24206 string that overwrite that glyph string because of their
24207 right overhang. For these strings, only the foreground must
24208 be drawn, because it draws over the glyph string at `head'.
24209 The background must not be drawn because this would overwrite
24210 right overhangs of preceding glyphs for which no glyph
24211 strings exist. */
24212 i = left_overwriting (head);
24213 if (i >= 0)
24214 {
24215 enum draw_glyphs_face overlap_hl;
24216
24217 if (check_mouse_face
24218 && mouse_beg_col < start && mouse_end_col > i)
24219 overlap_hl = DRAW_MOUSE_FACE;
24220 else
24221 overlap_hl = DRAW_NORMAL_TEXT;
24222
24223 clip_head = head;
24224 BUILD_GLYPH_STRINGS (i, start, h, t,
24225 overlap_hl, dummy_x, last_x);
24226 for (s = h; s; s = s->next)
24227 s->background_filled_p = 1;
24228 compute_overhangs_and_x (t, head->x, 1);
24229 prepend_glyph_string_lists (&head, &tail, h, t);
24230 }
24231
24232 /* Append glyphs strings for glyphs following the last glyph
24233 string tail that are overwritten by tail. The background of
24234 these strings has to be drawn because tail's foreground draws
24235 over it. */
24236 i = right_overwritten (tail);
24237 if (i >= 0)
24238 {
24239 enum draw_glyphs_face overlap_hl;
24240
24241 if (check_mouse_face
24242 && mouse_beg_col < i && mouse_end_col > end)
24243 overlap_hl = DRAW_MOUSE_FACE;
24244 else
24245 overlap_hl = DRAW_NORMAL_TEXT;
24246
24247 BUILD_GLYPH_STRINGS (end, i, h, t,
24248 overlap_hl, x, last_x);
24249 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24250 we don't have `end = i;' here. */
24251 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24252 append_glyph_string_lists (&head, &tail, h, t);
24253 clip_tail = tail;
24254 }
24255
24256 /* Append glyph strings for glyphs following the last glyph
24257 string tail that overwrite tail. The foreground of such
24258 glyphs has to be drawn because it writes into the background
24259 of tail. The background must not be drawn because it could
24260 paint over the foreground of following glyphs. */
24261 i = right_overwriting (tail);
24262 if (i >= 0)
24263 {
24264 enum draw_glyphs_face overlap_hl;
24265 if (check_mouse_face
24266 && mouse_beg_col < i && mouse_end_col > end)
24267 overlap_hl = DRAW_MOUSE_FACE;
24268 else
24269 overlap_hl = DRAW_NORMAL_TEXT;
24270
24271 clip_tail = tail;
24272 i++; /* We must include the Ith glyph. */
24273 BUILD_GLYPH_STRINGS (end, i, h, t,
24274 overlap_hl, x, last_x);
24275 for (s = h; s; s = s->next)
24276 s->background_filled_p = 1;
24277 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24278 append_glyph_string_lists (&head, &tail, h, t);
24279 }
24280 if (clip_head || clip_tail)
24281 for (s = head; s; s = s->next)
24282 {
24283 s->clip_head = clip_head;
24284 s->clip_tail = clip_tail;
24285 }
24286 }
24287
24288 /* Draw all strings. */
24289 for (s = head; s; s = s->next)
24290 FRAME_RIF (f)->draw_glyph_string (s);
24291
24292 #ifndef HAVE_NS
24293 /* When focus a sole frame and move horizontally, this sets on_p to 0
24294 causing a failure to erase prev cursor position. */
24295 if (area == TEXT_AREA
24296 && !row->full_width_p
24297 /* When drawing overlapping rows, only the glyph strings'
24298 foreground is drawn, which doesn't erase a cursor
24299 completely. */
24300 && !overlaps)
24301 {
24302 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24303 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24304 : (tail ? tail->x + tail->background_width : x));
24305 x0 -= area_left;
24306 x1 -= area_left;
24307
24308 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24309 row->y, MATRIX_ROW_BOTTOM_Y (row));
24310 }
24311 #endif
24312
24313 /* Value is the x-position up to which drawn, relative to AREA of W.
24314 This doesn't include parts drawn because of overhangs. */
24315 if (row->full_width_p)
24316 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24317 else
24318 x_reached -= area_left;
24319
24320 RELEASE_HDC (hdc, f);
24321
24322 return x_reached;
24323 }
24324
24325 /* Expand row matrix if too narrow. Don't expand if area
24326 is not present. */
24327
24328 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24329 { \
24330 if (!it->f->fonts_changed \
24331 && (it->glyph_row->glyphs[area] \
24332 < it->glyph_row->glyphs[area + 1])) \
24333 { \
24334 it->w->ncols_scale_factor++; \
24335 it->f->fonts_changed = 1; \
24336 } \
24337 }
24338
24339 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24340 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24341
24342 static void
24343 append_glyph (struct it *it)
24344 {
24345 struct glyph *glyph;
24346 enum glyph_row_area area = it->area;
24347
24348 eassert (it->glyph_row);
24349 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24350
24351 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24352 if (glyph < it->glyph_row->glyphs[area + 1])
24353 {
24354 /* If the glyph row is reversed, we need to prepend the glyph
24355 rather than append it. */
24356 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24357 {
24358 struct glyph *g;
24359
24360 /* Make room for the additional glyph. */
24361 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24362 g[1] = *g;
24363 glyph = it->glyph_row->glyphs[area];
24364 }
24365 glyph->charpos = CHARPOS (it->position);
24366 glyph->object = it->object;
24367 if (it->pixel_width > 0)
24368 {
24369 glyph->pixel_width = it->pixel_width;
24370 glyph->padding_p = 0;
24371 }
24372 else
24373 {
24374 /* Assure at least 1-pixel width. Otherwise, cursor can't
24375 be displayed correctly. */
24376 glyph->pixel_width = 1;
24377 glyph->padding_p = 1;
24378 }
24379 glyph->ascent = it->ascent;
24380 glyph->descent = it->descent;
24381 glyph->voffset = it->voffset;
24382 glyph->type = CHAR_GLYPH;
24383 glyph->avoid_cursor_p = it->avoid_cursor_p;
24384 glyph->multibyte_p = it->multibyte_p;
24385 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24386 {
24387 /* In R2L rows, the left and the right box edges need to be
24388 drawn in reverse direction. */
24389 glyph->right_box_line_p = it->start_of_box_run_p;
24390 glyph->left_box_line_p = it->end_of_box_run_p;
24391 }
24392 else
24393 {
24394 glyph->left_box_line_p = it->start_of_box_run_p;
24395 glyph->right_box_line_p = it->end_of_box_run_p;
24396 }
24397 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24398 || it->phys_descent > it->descent);
24399 glyph->glyph_not_available_p = it->glyph_not_available_p;
24400 glyph->face_id = it->face_id;
24401 glyph->u.ch = it->char_to_display;
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 /* Store one glyph for the composition IT->cmp_it.id in
24423 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24424 non-null. */
24425
24426 static void
24427 append_composite_glyph (struct it *it)
24428 {
24429 struct glyph *glyph;
24430 enum glyph_row_area area = it->area;
24431
24432 eassert (it->glyph_row);
24433
24434 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24435 if (glyph < it->glyph_row->glyphs[area + 1])
24436 {
24437 /* If the glyph row is reversed, we need to prepend the glyph
24438 rather than append it. */
24439 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24440 {
24441 struct glyph *g;
24442
24443 /* Make room for the new glyph. */
24444 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24445 g[1] = *g;
24446 glyph = it->glyph_row->glyphs[it->area];
24447 }
24448 glyph->charpos = it->cmp_it.charpos;
24449 glyph->object = it->object;
24450 glyph->pixel_width = it->pixel_width;
24451 glyph->ascent = it->ascent;
24452 glyph->descent = it->descent;
24453 glyph->voffset = it->voffset;
24454 glyph->type = COMPOSITE_GLYPH;
24455 if (it->cmp_it.ch < 0)
24456 {
24457 glyph->u.cmp.automatic = 0;
24458 glyph->u.cmp.id = it->cmp_it.id;
24459 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24460 }
24461 else
24462 {
24463 glyph->u.cmp.automatic = 1;
24464 glyph->u.cmp.id = it->cmp_it.id;
24465 glyph->slice.cmp.from = it->cmp_it.from;
24466 glyph->slice.cmp.to = it->cmp_it.to - 1;
24467 }
24468 glyph->avoid_cursor_p = it->avoid_cursor_p;
24469 glyph->multibyte_p = it->multibyte_p;
24470 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24471 {
24472 /* In R2L rows, the left and the right box edges need to be
24473 drawn in reverse direction. */
24474 glyph->right_box_line_p = it->start_of_box_run_p;
24475 glyph->left_box_line_p = it->end_of_box_run_p;
24476 }
24477 else
24478 {
24479 glyph->left_box_line_p = it->start_of_box_run_p;
24480 glyph->right_box_line_p = it->end_of_box_run_p;
24481 }
24482 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24483 || it->phys_descent > it->descent);
24484 glyph->padding_p = 0;
24485 glyph->glyph_not_available_p = 0;
24486 glyph->face_id = it->face_id;
24487 glyph->font_type = FONT_TYPE_UNKNOWN;
24488 if (it->bidi_p)
24489 {
24490 glyph->resolved_level = it->bidi_it.resolved_level;
24491 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24492 emacs_abort ();
24493 glyph->bidi_type = it->bidi_it.type;
24494 }
24495 ++it->glyph_row->used[area];
24496 }
24497 else
24498 IT_EXPAND_MATRIX_WIDTH (it, area);
24499 }
24500
24501
24502 /* Change IT->ascent and IT->height according to the setting of
24503 IT->voffset. */
24504
24505 static void
24506 take_vertical_position_into_account (struct it *it)
24507 {
24508 if (it->voffset)
24509 {
24510 if (it->voffset < 0)
24511 /* Increase the ascent so that we can display the text higher
24512 in the line. */
24513 it->ascent -= it->voffset;
24514 else
24515 /* Increase the descent so that we can display the text lower
24516 in the line. */
24517 it->descent += it->voffset;
24518 }
24519 }
24520
24521
24522 /* Produce glyphs/get display metrics for the image IT is loaded with.
24523 See the description of struct display_iterator in dispextern.h for
24524 an overview of struct display_iterator. */
24525
24526 static void
24527 produce_image_glyph (struct it *it)
24528 {
24529 struct image *img;
24530 struct face *face;
24531 int glyph_ascent, crop;
24532 struct glyph_slice slice;
24533
24534 eassert (it->what == IT_IMAGE);
24535
24536 face = FACE_FROM_ID (it->f, it->face_id);
24537 eassert (face);
24538 /* Make sure X resources of the face is loaded. */
24539 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24540
24541 if (it->image_id < 0)
24542 {
24543 /* Fringe bitmap. */
24544 it->ascent = it->phys_ascent = 0;
24545 it->descent = it->phys_descent = 0;
24546 it->pixel_width = 0;
24547 it->nglyphs = 0;
24548 return;
24549 }
24550
24551 img = IMAGE_FROM_ID (it->f, it->image_id);
24552 eassert (img);
24553 /* Make sure X resources of the image is loaded. */
24554 prepare_image_for_display (it->f, img);
24555
24556 slice.x = slice.y = 0;
24557 slice.width = img->width;
24558 slice.height = img->height;
24559
24560 if (INTEGERP (it->slice.x))
24561 slice.x = XINT (it->slice.x);
24562 else if (FLOATP (it->slice.x))
24563 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24564
24565 if (INTEGERP (it->slice.y))
24566 slice.y = XINT (it->slice.y);
24567 else if (FLOATP (it->slice.y))
24568 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24569
24570 if (INTEGERP (it->slice.width))
24571 slice.width = XINT (it->slice.width);
24572 else if (FLOATP (it->slice.width))
24573 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24574
24575 if (INTEGERP (it->slice.height))
24576 slice.height = XINT (it->slice.height);
24577 else if (FLOATP (it->slice.height))
24578 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24579
24580 if (slice.x >= img->width)
24581 slice.x = img->width;
24582 if (slice.y >= img->height)
24583 slice.y = img->height;
24584 if (slice.x + slice.width >= img->width)
24585 slice.width = img->width - slice.x;
24586 if (slice.y + slice.height > img->height)
24587 slice.height = img->height - slice.y;
24588
24589 if (slice.width == 0 || slice.height == 0)
24590 return;
24591
24592 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24593
24594 it->descent = slice.height - glyph_ascent;
24595 if (slice.y == 0)
24596 it->descent += img->vmargin;
24597 if (slice.y + slice.height == img->height)
24598 it->descent += img->vmargin;
24599 it->phys_descent = it->descent;
24600
24601 it->pixel_width = slice.width;
24602 if (slice.x == 0)
24603 it->pixel_width += img->hmargin;
24604 if (slice.x + slice.width == img->width)
24605 it->pixel_width += img->hmargin;
24606
24607 /* It's quite possible for images to have an ascent greater than
24608 their height, so don't get confused in that case. */
24609 if (it->descent < 0)
24610 it->descent = 0;
24611
24612 it->nglyphs = 1;
24613
24614 if (face->box != FACE_NO_BOX)
24615 {
24616 if (face->box_line_width > 0)
24617 {
24618 if (slice.y == 0)
24619 it->ascent += face->box_line_width;
24620 if (slice.y + slice.height == img->height)
24621 it->descent += face->box_line_width;
24622 }
24623
24624 if (it->start_of_box_run_p && slice.x == 0)
24625 it->pixel_width += eabs (face->box_line_width);
24626 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24627 it->pixel_width += eabs (face->box_line_width);
24628 }
24629
24630 take_vertical_position_into_account (it);
24631
24632 /* Automatically crop wide image glyphs at right edge so we can
24633 draw the cursor on same display row. */
24634 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24635 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24636 {
24637 it->pixel_width -= crop;
24638 slice.width -= crop;
24639 }
24640
24641 if (it->glyph_row)
24642 {
24643 struct glyph *glyph;
24644 enum glyph_row_area area = it->area;
24645
24646 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24647 if (glyph < it->glyph_row->glyphs[area + 1])
24648 {
24649 glyph->charpos = CHARPOS (it->position);
24650 glyph->object = it->object;
24651 glyph->pixel_width = it->pixel_width;
24652 glyph->ascent = glyph_ascent;
24653 glyph->descent = it->descent;
24654 glyph->voffset = it->voffset;
24655 glyph->type = IMAGE_GLYPH;
24656 glyph->avoid_cursor_p = it->avoid_cursor_p;
24657 glyph->multibyte_p = it->multibyte_p;
24658 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24659 {
24660 /* In R2L rows, the left and the right box edges need to be
24661 drawn in reverse direction. */
24662 glyph->right_box_line_p = it->start_of_box_run_p;
24663 glyph->left_box_line_p = it->end_of_box_run_p;
24664 }
24665 else
24666 {
24667 glyph->left_box_line_p = it->start_of_box_run_p;
24668 glyph->right_box_line_p = it->end_of_box_run_p;
24669 }
24670 glyph->overlaps_vertically_p = 0;
24671 glyph->padding_p = 0;
24672 glyph->glyph_not_available_p = 0;
24673 glyph->face_id = it->face_id;
24674 glyph->u.img_id = img->id;
24675 glyph->slice.img = slice;
24676 glyph->font_type = FONT_TYPE_UNKNOWN;
24677 if (it->bidi_p)
24678 {
24679 glyph->resolved_level = it->bidi_it.resolved_level;
24680 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24681 emacs_abort ();
24682 glyph->bidi_type = it->bidi_it.type;
24683 }
24684 ++it->glyph_row->used[area];
24685 }
24686 else
24687 IT_EXPAND_MATRIX_WIDTH (it, area);
24688 }
24689 }
24690
24691
24692 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24693 of the glyph, WIDTH and HEIGHT are the width and height of the
24694 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24695
24696 static void
24697 append_stretch_glyph (struct it *it, Lisp_Object object,
24698 int width, int height, int ascent)
24699 {
24700 struct glyph *glyph;
24701 enum glyph_row_area area = it->area;
24702
24703 eassert (ascent >= 0 && ascent <= height);
24704
24705 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24706 if (glyph < it->glyph_row->glyphs[area + 1])
24707 {
24708 /* If the glyph row is reversed, we need to prepend the glyph
24709 rather than append it. */
24710 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24711 {
24712 struct glyph *g;
24713
24714 /* Make room for the additional glyph. */
24715 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24716 g[1] = *g;
24717 glyph = it->glyph_row->glyphs[area];
24718 }
24719 glyph->charpos = CHARPOS (it->position);
24720 glyph->object = object;
24721 glyph->pixel_width = width;
24722 glyph->ascent = ascent;
24723 glyph->descent = height - ascent;
24724 glyph->voffset = it->voffset;
24725 glyph->type = STRETCH_GLYPH;
24726 glyph->avoid_cursor_p = it->avoid_cursor_p;
24727 glyph->multibyte_p = it->multibyte_p;
24728 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24729 {
24730 /* In R2L rows, the left and the right box edges need to be
24731 drawn in reverse direction. */
24732 glyph->right_box_line_p = it->start_of_box_run_p;
24733 glyph->left_box_line_p = it->end_of_box_run_p;
24734 }
24735 else
24736 {
24737 glyph->left_box_line_p = it->start_of_box_run_p;
24738 glyph->right_box_line_p = it->end_of_box_run_p;
24739 }
24740 glyph->overlaps_vertically_p = 0;
24741 glyph->padding_p = 0;
24742 glyph->glyph_not_available_p = 0;
24743 glyph->face_id = it->face_id;
24744 glyph->u.stretch.ascent = ascent;
24745 glyph->u.stretch.height = height;
24746 glyph->slice.img = null_glyph_slice;
24747 glyph->font_type = FONT_TYPE_UNKNOWN;
24748 if (it->bidi_p)
24749 {
24750 glyph->resolved_level = it->bidi_it.resolved_level;
24751 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24752 emacs_abort ();
24753 glyph->bidi_type = it->bidi_it.type;
24754 }
24755 else
24756 {
24757 glyph->resolved_level = 0;
24758 glyph->bidi_type = UNKNOWN_BT;
24759 }
24760 ++it->glyph_row->used[area];
24761 }
24762 else
24763 IT_EXPAND_MATRIX_WIDTH (it, area);
24764 }
24765
24766 #endif /* HAVE_WINDOW_SYSTEM */
24767
24768 /* Produce a stretch glyph for iterator IT. IT->object is the value
24769 of the glyph property displayed. The value must be a list
24770 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24771 being recognized:
24772
24773 1. `:width WIDTH' specifies that the space should be WIDTH *
24774 canonical char width wide. WIDTH may be an integer or floating
24775 point number.
24776
24777 2. `:relative-width FACTOR' specifies that the width of the stretch
24778 should be computed from the width of the first character having the
24779 `glyph' property, and should be FACTOR times that width.
24780
24781 3. `:align-to HPOS' specifies that the space should be wide enough
24782 to reach HPOS, a value in canonical character units.
24783
24784 Exactly one of the above pairs must be present.
24785
24786 4. `:height HEIGHT' specifies that the height of the stretch produced
24787 should be HEIGHT, measured in canonical character units.
24788
24789 5. `:relative-height FACTOR' specifies that the height of the
24790 stretch should be FACTOR times the height of the characters having
24791 the glyph property.
24792
24793 Either none or exactly one of 4 or 5 must be present.
24794
24795 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24796 of the stretch should be used for the ascent of the stretch.
24797 ASCENT must be in the range 0 <= ASCENT <= 100. */
24798
24799 void
24800 produce_stretch_glyph (struct it *it)
24801 {
24802 /* (space :width WIDTH :height HEIGHT ...) */
24803 Lisp_Object prop, plist;
24804 int width = 0, height = 0, align_to = -1;
24805 int zero_width_ok_p = 0;
24806 double tem;
24807 struct font *font = NULL;
24808
24809 #ifdef HAVE_WINDOW_SYSTEM
24810 int ascent = 0;
24811 int zero_height_ok_p = 0;
24812
24813 if (FRAME_WINDOW_P (it->f))
24814 {
24815 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24816 font = face->font ? face->font : FRAME_FONT (it->f);
24817 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24818 }
24819 #endif
24820
24821 /* List should start with `space'. */
24822 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24823 plist = XCDR (it->object);
24824
24825 /* Compute the width of the stretch. */
24826 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24827 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24828 {
24829 /* Absolute width `:width WIDTH' specified and valid. */
24830 zero_width_ok_p = 1;
24831 width = (int)tem;
24832 }
24833 #ifdef HAVE_WINDOW_SYSTEM
24834 else if (FRAME_WINDOW_P (it->f)
24835 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24836 {
24837 /* Relative width `:relative-width FACTOR' specified and valid.
24838 Compute the width of the characters having the `glyph'
24839 property. */
24840 struct it it2;
24841 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24842
24843 it2 = *it;
24844 if (it->multibyte_p)
24845 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24846 else
24847 {
24848 it2.c = it2.char_to_display = *p, it2.len = 1;
24849 if (! ASCII_CHAR_P (it2.c))
24850 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24851 }
24852
24853 it2.glyph_row = NULL;
24854 it2.what = IT_CHARACTER;
24855 x_produce_glyphs (&it2);
24856 width = NUMVAL (prop) * it2.pixel_width;
24857 }
24858 #endif /* HAVE_WINDOW_SYSTEM */
24859 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24860 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24861 {
24862 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24863 align_to = (align_to < 0
24864 ? 0
24865 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24866 else if (align_to < 0)
24867 align_to = window_box_left_offset (it->w, TEXT_AREA);
24868 width = max (0, (int)tem + align_to - it->current_x);
24869 zero_width_ok_p = 1;
24870 }
24871 else
24872 /* Nothing specified -> width defaults to canonical char width. */
24873 width = FRAME_COLUMN_WIDTH (it->f);
24874
24875 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24876 width = 1;
24877
24878 #ifdef HAVE_WINDOW_SYSTEM
24879 /* Compute height. */
24880 if (FRAME_WINDOW_P (it->f))
24881 {
24882 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24883 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24884 {
24885 height = (int)tem;
24886 zero_height_ok_p = 1;
24887 }
24888 else if (prop = Fplist_get (plist, QCrelative_height),
24889 NUMVAL (prop) > 0)
24890 height = FONT_HEIGHT (font) * NUMVAL (prop);
24891 else
24892 height = FONT_HEIGHT (font);
24893
24894 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24895 height = 1;
24896
24897 /* Compute percentage of height used for ascent. If
24898 `:ascent ASCENT' is present and valid, use that. Otherwise,
24899 derive the ascent from the font in use. */
24900 if (prop = Fplist_get (plist, QCascent),
24901 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24902 ascent = height * NUMVAL (prop) / 100.0;
24903 else if (!NILP (prop)
24904 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24905 ascent = min (max (0, (int)tem), height);
24906 else
24907 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24908 }
24909 else
24910 #endif /* HAVE_WINDOW_SYSTEM */
24911 height = 1;
24912
24913 if (width > 0 && it->line_wrap != TRUNCATE
24914 && it->current_x + width > it->last_visible_x)
24915 {
24916 width = it->last_visible_x - it->current_x;
24917 #ifdef HAVE_WINDOW_SYSTEM
24918 /* Subtract one more pixel from the stretch width, but only on
24919 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24920 width -= FRAME_WINDOW_P (it->f);
24921 #endif
24922 }
24923
24924 if (width > 0 && height > 0 && it->glyph_row)
24925 {
24926 Lisp_Object o_object = it->object;
24927 Lisp_Object object = it->stack[it->sp - 1].string;
24928 int n = width;
24929
24930 if (!STRINGP (object))
24931 object = it->w->contents;
24932 #ifdef HAVE_WINDOW_SYSTEM
24933 if (FRAME_WINDOW_P (it->f))
24934 append_stretch_glyph (it, object, width, height, ascent);
24935 else
24936 #endif
24937 {
24938 it->object = object;
24939 it->char_to_display = ' ';
24940 it->pixel_width = it->len = 1;
24941 while (n--)
24942 tty_append_glyph (it);
24943 it->object = o_object;
24944 }
24945 }
24946
24947 it->pixel_width = width;
24948 #ifdef HAVE_WINDOW_SYSTEM
24949 if (FRAME_WINDOW_P (it->f))
24950 {
24951 it->ascent = it->phys_ascent = ascent;
24952 it->descent = it->phys_descent = height - it->ascent;
24953 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24954 take_vertical_position_into_account (it);
24955 }
24956 else
24957 #endif
24958 it->nglyphs = width;
24959 }
24960
24961 /* Get information about special display element WHAT in an
24962 environment described by IT. WHAT is one of IT_TRUNCATION or
24963 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24964 non-null glyph_row member. This function ensures that fields like
24965 face_id, c, len of IT are left untouched. */
24966
24967 static void
24968 produce_special_glyphs (struct it *it, enum display_element_type what)
24969 {
24970 struct it temp_it;
24971 Lisp_Object gc;
24972 GLYPH glyph;
24973
24974 temp_it = *it;
24975 temp_it.object = make_number (0);
24976 memset (&temp_it.current, 0, sizeof temp_it.current);
24977
24978 if (what == IT_CONTINUATION)
24979 {
24980 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24981 if (it->bidi_it.paragraph_dir == R2L)
24982 SET_GLYPH_FROM_CHAR (glyph, '/');
24983 else
24984 SET_GLYPH_FROM_CHAR (glyph, '\\');
24985 if (it->dp
24986 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24987 {
24988 /* FIXME: Should we mirror GC for R2L lines? */
24989 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24990 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24991 }
24992 }
24993 else if (what == IT_TRUNCATION)
24994 {
24995 /* Truncation glyph. */
24996 SET_GLYPH_FROM_CHAR (glyph, '$');
24997 if (it->dp
24998 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24999 {
25000 /* FIXME: Should we mirror GC for R2L lines? */
25001 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25002 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25003 }
25004 }
25005 else
25006 emacs_abort ();
25007
25008 #ifdef HAVE_WINDOW_SYSTEM
25009 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25010 is turned off, we precede the truncation/continuation glyphs by a
25011 stretch glyph whose width is computed such that these special
25012 glyphs are aligned at the window margin, even when very different
25013 fonts are used in different glyph rows. */
25014 if (FRAME_WINDOW_P (temp_it.f)
25015 /* init_iterator calls this with it->glyph_row == NULL, and it
25016 wants only the pixel width of the truncation/continuation
25017 glyphs. */
25018 && temp_it.glyph_row
25019 /* insert_left_trunc_glyphs calls us at the beginning of the
25020 row, and it has its own calculation of the stretch glyph
25021 width. */
25022 && temp_it.glyph_row->used[TEXT_AREA] > 0
25023 && (temp_it.glyph_row->reversed_p
25024 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25025 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25026 {
25027 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25028
25029 if (stretch_width > 0)
25030 {
25031 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25032 struct font *font =
25033 face->font ? face->font : FRAME_FONT (temp_it.f);
25034 int stretch_ascent =
25035 (((temp_it.ascent + temp_it.descent)
25036 * FONT_BASE (font)) / FONT_HEIGHT (font));
25037
25038 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25039 temp_it.ascent + temp_it.descent,
25040 stretch_ascent);
25041 }
25042 }
25043 #endif
25044
25045 temp_it.dp = NULL;
25046 temp_it.what = IT_CHARACTER;
25047 temp_it.len = 1;
25048 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25049 temp_it.face_id = GLYPH_FACE (glyph);
25050 temp_it.len = CHAR_BYTES (temp_it.c);
25051
25052 PRODUCE_GLYPHS (&temp_it);
25053 it->pixel_width = temp_it.pixel_width;
25054 it->nglyphs = temp_it.pixel_width;
25055 }
25056
25057 #ifdef HAVE_WINDOW_SYSTEM
25058
25059 /* Calculate line-height and line-spacing properties.
25060 An integer value specifies explicit pixel value.
25061 A float value specifies relative value to current face height.
25062 A cons (float . face-name) specifies relative value to
25063 height of specified face font.
25064
25065 Returns height in pixels, or nil. */
25066
25067
25068 static Lisp_Object
25069 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25070 int boff, int override)
25071 {
25072 Lisp_Object face_name = Qnil;
25073 int ascent, descent, height;
25074
25075 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25076 return val;
25077
25078 if (CONSP (val))
25079 {
25080 face_name = XCAR (val);
25081 val = XCDR (val);
25082 if (!NUMBERP (val))
25083 val = make_number (1);
25084 if (NILP (face_name))
25085 {
25086 height = it->ascent + it->descent;
25087 goto scale;
25088 }
25089 }
25090
25091 if (NILP (face_name))
25092 {
25093 font = FRAME_FONT (it->f);
25094 boff = FRAME_BASELINE_OFFSET (it->f);
25095 }
25096 else if (EQ (face_name, Qt))
25097 {
25098 override = 0;
25099 }
25100 else
25101 {
25102 int face_id;
25103 struct face *face;
25104
25105 face_id = lookup_named_face (it->f, face_name, 0);
25106 if (face_id < 0)
25107 return make_number (-1);
25108
25109 face = FACE_FROM_ID (it->f, face_id);
25110 font = face->font;
25111 if (font == NULL)
25112 return make_number (-1);
25113 boff = font->baseline_offset;
25114 if (font->vertical_centering)
25115 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25116 }
25117
25118 ascent = FONT_BASE (font) + boff;
25119 descent = FONT_DESCENT (font) - boff;
25120
25121 if (override)
25122 {
25123 it->override_ascent = ascent;
25124 it->override_descent = descent;
25125 it->override_boff = boff;
25126 }
25127
25128 height = ascent + descent;
25129
25130 scale:
25131 if (FLOATP (val))
25132 height = (int)(XFLOAT_DATA (val) * height);
25133 else if (INTEGERP (val))
25134 height *= XINT (val);
25135
25136 return make_number (height);
25137 }
25138
25139
25140 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25141 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25142 and only if this is for a character for which no font was found.
25143
25144 If the display method (it->glyphless_method) is
25145 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25146 length of the acronym or the hexadecimal string, UPPER_XOFF and
25147 UPPER_YOFF are pixel offsets for the upper part of the string,
25148 LOWER_XOFF and LOWER_YOFF are for the lower part.
25149
25150 For the other display methods, LEN through LOWER_YOFF are zero. */
25151
25152 static void
25153 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25154 short upper_xoff, short upper_yoff,
25155 short lower_xoff, short lower_yoff)
25156 {
25157 struct glyph *glyph;
25158 enum glyph_row_area area = it->area;
25159
25160 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25161 if (glyph < it->glyph_row->glyphs[area + 1])
25162 {
25163 /* If the glyph row is reversed, we need to prepend the glyph
25164 rather than append it. */
25165 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25166 {
25167 struct glyph *g;
25168
25169 /* Make room for the additional glyph. */
25170 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25171 g[1] = *g;
25172 glyph = it->glyph_row->glyphs[area];
25173 }
25174 glyph->charpos = CHARPOS (it->position);
25175 glyph->object = it->object;
25176 glyph->pixel_width = it->pixel_width;
25177 glyph->ascent = it->ascent;
25178 glyph->descent = it->descent;
25179 glyph->voffset = it->voffset;
25180 glyph->type = GLYPHLESS_GLYPH;
25181 glyph->u.glyphless.method = it->glyphless_method;
25182 glyph->u.glyphless.for_no_font = for_no_font;
25183 glyph->u.glyphless.len = len;
25184 glyph->u.glyphless.ch = it->c;
25185 glyph->slice.glyphless.upper_xoff = upper_xoff;
25186 glyph->slice.glyphless.upper_yoff = upper_yoff;
25187 glyph->slice.glyphless.lower_xoff = lower_xoff;
25188 glyph->slice.glyphless.lower_yoff = lower_yoff;
25189 glyph->avoid_cursor_p = it->avoid_cursor_p;
25190 glyph->multibyte_p = it->multibyte_p;
25191 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25192 {
25193 /* In R2L rows, the left and the right box edges need to be
25194 drawn in reverse direction. */
25195 glyph->right_box_line_p = it->start_of_box_run_p;
25196 glyph->left_box_line_p = it->end_of_box_run_p;
25197 }
25198 else
25199 {
25200 glyph->left_box_line_p = it->start_of_box_run_p;
25201 glyph->right_box_line_p = it->end_of_box_run_p;
25202 }
25203 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25204 || it->phys_descent > it->descent);
25205 glyph->padding_p = 0;
25206 glyph->glyph_not_available_p = 0;
25207 glyph->face_id = face_id;
25208 glyph->font_type = FONT_TYPE_UNKNOWN;
25209 if (it->bidi_p)
25210 {
25211 glyph->resolved_level = it->bidi_it.resolved_level;
25212 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25213 emacs_abort ();
25214 glyph->bidi_type = it->bidi_it.type;
25215 }
25216 ++it->glyph_row->used[area];
25217 }
25218 else
25219 IT_EXPAND_MATRIX_WIDTH (it, area);
25220 }
25221
25222
25223 /* Produce a glyph for a glyphless character for iterator IT.
25224 IT->glyphless_method specifies which method to use for displaying
25225 the character. See the description of enum
25226 glyphless_display_method in dispextern.h for the detail.
25227
25228 FOR_NO_FONT is nonzero if and only if this is for a character for
25229 which no font was found. ACRONYM, if non-nil, is an acronym string
25230 for the character. */
25231
25232 static void
25233 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25234 {
25235 int face_id;
25236 struct face *face;
25237 struct font *font;
25238 int base_width, base_height, width, height;
25239 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25240 int len;
25241
25242 /* Get the metrics of the base font. We always refer to the current
25243 ASCII face. */
25244 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25245 font = face->font ? face->font : FRAME_FONT (it->f);
25246 it->ascent = FONT_BASE (font) + font->baseline_offset;
25247 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25248 base_height = it->ascent + it->descent;
25249 base_width = font->average_width;
25250
25251 face_id = merge_glyphless_glyph_face (it);
25252
25253 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25254 {
25255 it->pixel_width = THIN_SPACE_WIDTH;
25256 len = 0;
25257 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25258 }
25259 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25260 {
25261 width = CHAR_WIDTH (it->c);
25262 if (width == 0)
25263 width = 1;
25264 else if (width > 4)
25265 width = 4;
25266 it->pixel_width = base_width * width;
25267 len = 0;
25268 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25269 }
25270 else
25271 {
25272 char buf[7];
25273 const char *str;
25274 unsigned int code[6];
25275 int upper_len;
25276 int ascent, descent;
25277 struct font_metrics metrics_upper, metrics_lower;
25278
25279 face = FACE_FROM_ID (it->f, face_id);
25280 font = face->font ? face->font : FRAME_FONT (it->f);
25281 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25282
25283 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25284 {
25285 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25286 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25287 if (CONSP (acronym))
25288 acronym = XCAR (acronym);
25289 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25290 }
25291 else
25292 {
25293 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25294 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25295 str = buf;
25296 }
25297 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25298 code[len] = font->driver->encode_char (font, str[len]);
25299 upper_len = (len + 1) / 2;
25300 font->driver->text_extents (font, code, upper_len,
25301 &metrics_upper);
25302 font->driver->text_extents (font, code + upper_len, len - upper_len,
25303 &metrics_lower);
25304
25305
25306
25307 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25308 width = max (metrics_upper.width, metrics_lower.width) + 4;
25309 upper_xoff = upper_yoff = 2; /* the typical case */
25310 if (base_width >= width)
25311 {
25312 /* Align the upper to the left, the lower to the right. */
25313 it->pixel_width = base_width;
25314 lower_xoff = base_width - 2 - metrics_lower.width;
25315 }
25316 else
25317 {
25318 /* Center the shorter one. */
25319 it->pixel_width = width;
25320 if (metrics_upper.width >= metrics_lower.width)
25321 lower_xoff = (width - metrics_lower.width) / 2;
25322 else
25323 {
25324 /* FIXME: This code doesn't look right. It formerly was
25325 missing the "lower_xoff = 0;", which couldn't have
25326 been right since it left lower_xoff uninitialized. */
25327 lower_xoff = 0;
25328 upper_xoff = (width - metrics_upper.width) / 2;
25329 }
25330 }
25331
25332 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25333 top, bottom, and between upper and lower strings. */
25334 height = (metrics_upper.ascent + metrics_upper.descent
25335 + metrics_lower.ascent + metrics_lower.descent) + 5;
25336 /* Center vertically.
25337 H:base_height, D:base_descent
25338 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25339
25340 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25341 descent = D - H/2 + h/2;
25342 lower_yoff = descent - 2 - ld;
25343 upper_yoff = lower_yoff - la - 1 - ud; */
25344 ascent = - (it->descent - (base_height + height + 1) / 2);
25345 descent = it->descent - (base_height - height) / 2;
25346 lower_yoff = descent - 2 - metrics_lower.descent;
25347 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25348 - metrics_upper.descent);
25349 /* Don't make the height shorter than the base height. */
25350 if (height > base_height)
25351 {
25352 it->ascent = ascent;
25353 it->descent = descent;
25354 }
25355 }
25356
25357 it->phys_ascent = it->ascent;
25358 it->phys_descent = it->descent;
25359 if (it->glyph_row)
25360 append_glyphless_glyph (it, face_id, for_no_font, len,
25361 upper_xoff, upper_yoff,
25362 lower_xoff, lower_yoff);
25363 it->nglyphs = 1;
25364 take_vertical_position_into_account (it);
25365 }
25366
25367
25368 /* RIF:
25369 Produce glyphs/get display metrics for the display element IT is
25370 loaded with. See the description of struct it in dispextern.h
25371 for an overview of struct it. */
25372
25373 void
25374 x_produce_glyphs (struct it *it)
25375 {
25376 int extra_line_spacing = it->extra_line_spacing;
25377
25378 it->glyph_not_available_p = 0;
25379
25380 if (it->what == IT_CHARACTER)
25381 {
25382 XChar2b char2b;
25383 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25384 struct font *font = face->font;
25385 struct font_metrics *pcm = NULL;
25386 int boff; /* Baseline offset. */
25387
25388 if (font == NULL)
25389 {
25390 /* When no suitable font is found, display this character by
25391 the method specified in the first extra slot of
25392 Vglyphless_char_display. */
25393 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25394
25395 eassert (it->what == IT_GLYPHLESS);
25396 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25397 goto done;
25398 }
25399
25400 boff = font->baseline_offset;
25401 if (font->vertical_centering)
25402 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25403
25404 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25405 {
25406 int stretched_p;
25407
25408 it->nglyphs = 1;
25409
25410 if (it->override_ascent >= 0)
25411 {
25412 it->ascent = it->override_ascent;
25413 it->descent = it->override_descent;
25414 boff = it->override_boff;
25415 }
25416 else
25417 {
25418 it->ascent = FONT_BASE (font) + boff;
25419 it->descent = FONT_DESCENT (font) - boff;
25420 }
25421
25422 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25423 {
25424 pcm = get_per_char_metric (font, &char2b);
25425 if (pcm->width == 0
25426 && pcm->rbearing == 0 && pcm->lbearing == 0)
25427 pcm = NULL;
25428 }
25429
25430 if (pcm)
25431 {
25432 it->phys_ascent = pcm->ascent + boff;
25433 it->phys_descent = pcm->descent - boff;
25434 it->pixel_width = pcm->width;
25435 }
25436 else
25437 {
25438 it->glyph_not_available_p = 1;
25439 it->phys_ascent = it->ascent;
25440 it->phys_descent = it->descent;
25441 it->pixel_width = font->space_width;
25442 }
25443
25444 if (it->constrain_row_ascent_descent_p)
25445 {
25446 if (it->descent > it->max_descent)
25447 {
25448 it->ascent += it->descent - it->max_descent;
25449 it->descent = it->max_descent;
25450 }
25451 if (it->ascent > it->max_ascent)
25452 {
25453 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25454 it->ascent = it->max_ascent;
25455 }
25456 it->phys_ascent = min (it->phys_ascent, it->ascent);
25457 it->phys_descent = min (it->phys_descent, it->descent);
25458 extra_line_spacing = 0;
25459 }
25460
25461 /* If this is a space inside a region of text with
25462 `space-width' property, change its width. */
25463 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25464 if (stretched_p)
25465 it->pixel_width *= XFLOATINT (it->space_width);
25466
25467 /* If face has a box, add the box thickness to the character
25468 height. If character has a box line to the left and/or
25469 right, add the box line width to the character's width. */
25470 if (face->box != FACE_NO_BOX)
25471 {
25472 int thick = face->box_line_width;
25473
25474 if (thick > 0)
25475 {
25476 it->ascent += thick;
25477 it->descent += thick;
25478 }
25479 else
25480 thick = -thick;
25481
25482 if (it->start_of_box_run_p)
25483 it->pixel_width += thick;
25484 if (it->end_of_box_run_p)
25485 it->pixel_width += thick;
25486 }
25487
25488 /* If face has an overline, add the height of the overline
25489 (1 pixel) and a 1 pixel margin to the character height. */
25490 if (face->overline_p)
25491 it->ascent += overline_margin;
25492
25493 if (it->constrain_row_ascent_descent_p)
25494 {
25495 if (it->ascent > it->max_ascent)
25496 it->ascent = it->max_ascent;
25497 if (it->descent > it->max_descent)
25498 it->descent = it->max_descent;
25499 }
25500
25501 take_vertical_position_into_account (it);
25502
25503 /* If we have to actually produce glyphs, do it. */
25504 if (it->glyph_row)
25505 {
25506 if (stretched_p)
25507 {
25508 /* Translate a space with a `space-width' property
25509 into a stretch glyph. */
25510 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25511 / FONT_HEIGHT (font));
25512 append_stretch_glyph (it, it->object, it->pixel_width,
25513 it->ascent + it->descent, ascent);
25514 }
25515 else
25516 append_glyph (it);
25517
25518 /* If characters with lbearing or rbearing are displayed
25519 in this line, record that fact in a flag of the
25520 glyph row. This is used to optimize X output code. */
25521 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25522 it->glyph_row->contains_overlapping_glyphs_p = 1;
25523 }
25524 if (! stretched_p && it->pixel_width == 0)
25525 /* We assure that all visible glyphs have at least 1-pixel
25526 width. */
25527 it->pixel_width = 1;
25528 }
25529 else if (it->char_to_display == '\n')
25530 {
25531 /* A newline has no width, but we need the height of the
25532 line. But if previous part of the line sets a height,
25533 don't increase that height. */
25534
25535 Lisp_Object height;
25536 Lisp_Object total_height = Qnil;
25537
25538 it->override_ascent = -1;
25539 it->pixel_width = 0;
25540 it->nglyphs = 0;
25541
25542 height = get_it_property (it, Qline_height);
25543 /* Split (line-height total-height) list. */
25544 if (CONSP (height)
25545 && CONSP (XCDR (height))
25546 && NILP (XCDR (XCDR (height))))
25547 {
25548 total_height = XCAR (XCDR (height));
25549 height = XCAR (height);
25550 }
25551 height = calc_line_height_property (it, height, font, boff, 1);
25552
25553 if (it->override_ascent >= 0)
25554 {
25555 it->ascent = it->override_ascent;
25556 it->descent = it->override_descent;
25557 boff = it->override_boff;
25558 }
25559 else
25560 {
25561 it->ascent = FONT_BASE (font) + boff;
25562 it->descent = FONT_DESCENT (font) - boff;
25563 }
25564
25565 if (EQ (height, Qt))
25566 {
25567 if (it->descent > it->max_descent)
25568 {
25569 it->ascent += it->descent - it->max_descent;
25570 it->descent = it->max_descent;
25571 }
25572 if (it->ascent > it->max_ascent)
25573 {
25574 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25575 it->ascent = it->max_ascent;
25576 }
25577 it->phys_ascent = min (it->phys_ascent, it->ascent);
25578 it->phys_descent = min (it->phys_descent, it->descent);
25579 it->constrain_row_ascent_descent_p = 1;
25580 extra_line_spacing = 0;
25581 }
25582 else
25583 {
25584 Lisp_Object spacing;
25585
25586 it->phys_ascent = it->ascent;
25587 it->phys_descent = it->descent;
25588
25589 if ((it->max_ascent > 0 || it->max_descent > 0)
25590 && face->box != FACE_NO_BOX
25591 && face->box_line_width > 0)
25592 {
25593 it->ascent += face->box_line_width;
25594 it->descent += face->box_line_width;
25595 }
25596 if (!NILP (height)
25597 && XINT (height) > it->ascent + it->descent)
25598 it->ascent = XINT (height) - it->descent;
25599
25600 if (!NILP (total_height))
25601 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25602 else
25603 {
25604 spacing = get_it_property (it, Qline_spacing);
25605 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25606 }
25607 if (INTEGERP (spacing))
25608 {
25609 extra_line_spacing = XINT (spacing);
25610 if (!NILP (total_height))
25611 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25612 }
25613 }
25614 }
25615 else /* i.e. (it->char_to_display == '\t') */
25616 {
25617 if (font->space_width > 0)
25618 {
25619 int tab_width = it->tab_width * font->space_width;
25620 int x = it->current_x + it->continuation_lines_width;
25621 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25622
25623 /* If the distance from the current position to the next tab
25624 stop is less than a space character width, use the
25625 tab stop after that. */
25626 if (next_tab_x - x < font->space_width)
25627 next_tab_x += tab_width;
25628
25629 it->pixel_width = next_tab_x - x;
25630 it->nglyphs = 1;
25631 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25632 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25633
25634 if (it->glyph_row)
25635 {
25636 append_stretch_glyph (it, it->object, it->pixel_width,
25637 it->ascent + it->descent, it->ascent);
25638 }
25639 }
25640 else
25641 {
25642 it->pixel_width = 0;
25643 it->nglyphs = 1;
25644 }
25645 }
25646 }
25647 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25648 {
25649 /* A static composition.
25650
25651 Note: A composition is represented as one glyph in the
25652 glyph matrix. There are no padding glyphs.
25653
25654 Important note: pixel_width, ascent, and descent are the
25655 values of what is drawn by draw_glyphs (i.e. the values of
25656 the overall glyphs composed). */
25657 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25658 int boff; /* baseline offset */
25659 struct composition *cmp = composition_table[it->cmp_it.id];
25660 int glyph_len = cmp->glyph_len;
25661 struct font *font = face->font;
25662
25663 it->nglyphs = 1;
25664
25665 /* If we have not yet calculated pixel size data of glyphs of
25666 the composition for the current face font, calculate them
25667 now. Theoretically, we have to check all fonts for the
25668 glyphs, but that requires much time and memory space. So,
25669 here we check only the font of the first glyph. This may
25670 lead to incorrect display, but it's very rare, and C-l
25671 (recenter-top-bottom) can correct the display anyway. */
25672 if (! cmp->font || cmp->font != font)
25673 {
25674 /* Ascent and descent of the font of the first character
25675 of this composition (adjusted by baseline offset).
25676 Ascent and descent of overall glyphs should not be less
25677 than these, respectively. */
25678 int font_ascent, font_descent, font_height;
25679 /* Bounding box of the overall glyphs. */
25680 int leftmost, rightmost, lowest, highest;
25681 int lbearing, rbearing;
25682 int i, width, ascent, descent;
25683 int left_padded = 0, right_padded = 0;
25684 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25685 XChar2b char2b;
25686 struct font_metrics *pcm;
25687 int font_not_found_p;
25688 ptrdiff_t pos;
25689
25690 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25691 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25692 break;
25693 if (glyph_len < cmp->glyph_len)
25694 right_padded = 1;
25695 for (i = 0; i < glyph_len; i++)
25696 {
25697 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25698 break;
25699 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25700 }
25701 if (i > 0)
25702 left_padded = 1;
25703
25704 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25705 : IT_CHARPOS (*it));
25706 /* If no suitable font is found, use the default font. */
25707 font_not_found_p = font == NULL;
25708 if (font_not_found_p)
25709 {
25710 face = face->ascii_face;
25711 font = face->font;
25712 }
25713 boff = font->baseline_offset;
25714 if (font->vertical_centering)
25715 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25716 font_ascent = FONT_BASE (font) + boff;
25717 font_descent = FONT_DESCENT (font) - boff;
25718 font_height = FONT_HEIGHT (font);
25719
25720 cmp->font = font;
25721
25722 pcm = NULL;
25723 if (! font_not_found_p)
25724 {
25725 get_char_face_and_encoding (it->f, c, it->face_id,
25726 &char2b, 0);
25727 pcm = get_per_char_metric (font, &char2b);
25728 }
25729
25730 /* Initialize the bounding box. */
25731 if (pcm)
25732 {
25733 width = cmp->glyph_len > 0 ? pcm->width : 0;
25734 ascent = pcm->ascent;
25735 descent = pcm->descent;
25736 lbearing = pcm->lbearing;
25737 rbearing = pcm->rbearing;
25738 }
25739 else
25740 {
25741 width = cmp->glyph_len > 0 ? font->space_width : 0;
25742 ascent = FONT_BASE (font);
25743 descent = FONT_DESCENT (font);
25744 lbearing = 0;
25745 rbearing = width;
25746 }
25747
25748 rightmost = width;
25749 leftmost = 0;
25750 lowest = - descent + boff;
25751 highest = ascent + boff;
25752
25753 if (! font_not_found_p
25754 && font->default_ascent
25755 && CHAR_TABLE_P (Vuse_default_ascent)
25756 && !NILP (Faref (Vuse_default_ascent,
25757 make_number (it->char_to_display))))
25758 highest = font->default_ascent + boff;
25759
25760 /* Draw the first glyph at the normal position. It may be
25761 shifted to right later if some other glyphs are drawn
25762 at the left. */
25763 cmp->offsets[i * 2] = 0;
25764 cmp->offsets[i * 2 + 1] = boff;
25765 cmp->lbearing = lbearing;
25766 cmp->rbearing = rbearing;
25767
25768 /* Set cmp->offsets for the remaining glyphs. */
25769 for (i++; i < glyph_len; i++)
25770 {
25771 int left, right, btm, top;
25772 int ch = COMPOSITION_GLYPH (cmp, i);
25773 int face_id;
25774 struct face *this_face;
25775
25776 if (ch == '\t')
25777 ch = ' ';
25778 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25779 this_face = FACE_FROM_ID (it->f, face_id);
25780 font = this_face->font;
25781
25782 if (font == NULL)
25783 pcm = NULL;
25784 else
25785 {
25786 get_char_face_and_encoding (it->f, ch, face_id,
25787 &char2b, 0);
25788 pcm = get_per_char_metric (font, &char2b);
25789 }
25790 if (! pcm)
25791 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25792 else
25793 {
25794 width = pcm->width;
25795 ascent = pcm->ascent;
25796 descent = pcm->descent;
25797 lbearing = pcm->lbearing;
25798 rbearing = pcm->rbearing;
25799 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25800 {
25801 /* Relative composition with or without
25802 alternate chars. */
25803 left = (leftmost + rightmost - width) / 2;
25804 btm = - descent + boff;
25805 if (font->relative_compose
25806 && (! CHAR_TABLE_P (Vignore_relative_composition)
25807 || NILP (Faref (Vignore_relative_composition,
25808 make_number (ch)))))
25809 {
25810
25811 if (- descent >= font->relative_compose)
25812 /* One extra pixel between two glyphs. */
25813 btm = highest + 1;
25814 else if (ascent <= 0)
25815 /* One extra pixel between two glyphs. */
25816 btm = lowest - 1 - ascent - descent;
25817 }
25818 }
25819 else
25820 {
25821 /* A composition rule is specified by an integer
25822 value that encodes global and new reference
25823 points (GREF and NREF). GREF and NREF are
25824 specified by numbers as below:
25825
25826 0---1---2 -- ascent
25827 | |
25828 | |
25829 | |
25830 9--10--11 -- center
25831 | |
25832 ---3---4---5--- baseline
25833 | |
25834 6---7---8 -- descent
25835 */
25836 int rule = COMPOSITION_RULE (cmp, i);
25837 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25838
25839 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25840 grefx = gref % 3, nrefx = nref % 3;
25841 grefy = gref / 3, nrefy = nref / 3;
25842 if (xoff)
25843 xoff = font_height * (xoff - 128) / 256;
25844 if (yoff)
25845 yoff = font_height * (yoff - 128) / 256;
25846
25847 left = (leftmost
25848 + grefx * (rightmost - leftmost) / 2
25849 - nrefx * width / 2
25850 + xoff);
25851
25852 btm = ((grefy == 0 ? highest
25853 : grefy == 1 ? 0
25854 : grefy == 2 ? lowest
25855 : (highest + lowest) / 2)
25856 - (nrefy == 0 ? ascent + descent
25857 : nrefy == 1 ? descent - boff
25858 : nrefy == 2 ? 0
25859 : (ascent + descent) / 2)
25860 + yoff);
25861 }
25862
25863 cmp->offsets[i * 2] = left;
25864 cmp->offsets[i * 2 + 1] = btm + descent;
25865
25866 /* Update the bounding box of the overall glyphs. */
25867 if (width > 0)
25868 {
25869 right = left + width;
25870 if (left < leftmost)
25871 leftmost = left;
25872 if (right > rightmost)
25873 rightmost = right;
25874 }
25875 top = btm + descent + ascent;
25876 if (top > highest)
25877 highest = top;
25878 if (btm < lowest)
25879 lowest = btm;
25880
25881 if (cmp->lbearing > left + lbearing)
25882 cmp->lbearing = left + lbearing;
25883 if (cmp->rbearing < left + rbearing)
25884 cmp->rbearing = left + rbearing;
25885 }
25886 }
25887
25888 /* If there are glyphs whose x-offsets are negative,
25889 shift all glyphs to the right and make all x-offsets
25890 non-negative. */
25891 if (leftmost < 0)
25892 {
25893 for (i = 0; i < cmp->glyph_len; i++)
25894 cmp->offsets[i * 2] -= leftmost;
25895 rightmost -= leftmost;
25896 cmp->lbearing -= leftmost;
25897 cmp->rbearing -= leftmost;
25898 }
25899
25900 if (left_padded && cmp->lbearing < 0)
25901 {
25902 for (i = 0; i < cmp->glyph_len; i++)
25903 cmp->offsets[i * 2] -= cmp->lbearing;
25904 rightmost -= cmp->lbearing;
25905 cmp->rbearing -= cmp->lbearing;
25906 cmp->lbearing = 0;
25907 }
25908 if (right_padded && rightmost < cmp->rbearing)
25909 {
25910 rightmost = cmp->rbearing;
25911 }
25912
25913 cmp->pixel_width = rightmost;
25914 cmp->ascent = highest;
25915 cmp->descent = - lowest;
25916 if (cmp->ascent < font_ascent)
25917 cmp->ascent = font_ascent;
25918 if (cmp->descent < font_descent)
25919 cmp->descent = font_descent;
25920 }
25921
25922 if (it->glyph_row
25923 && (cmp->lbearing < 0
25924 || cmp->rbearing > cmp->pixel_width))
25925 it->glyph_row->contains_overlapping_glyphs_p = 1;
25926
25927 it->pixel_width = cmp->pixel_width;
25928 it->ascent = it->phys_ascent = cmp->ascent;
25929 it->descent = it->phys_descent = cmp->descent;
25930 if (face->box != FACE_NO_BOX)
25931 {
25932 int thick = face->box_line_width;
25933
25934 if (thick > 0)
25935 {
25936 it->ascent += thick;
25937 it->descent += thick;
25938 }
25939 else
25940 thick = - thick;
25941
25942 if (it->start_of_box_run_p)
25943 it->pixel_width += thick;
25944 if (it->end_of_box_run_p)
25945 it->pixel_width += thick;
25946 }
25947
25948 /* If face has an overline, add the height of the overline
25949 (1 pixel) and a 1 pixel margin to the character height. */
25950 if (face->overline_p)
25951 it->ascent += overline_margin;
25952
25953 take_vertical_position_into_account (it);
25954 if (it->ascent < 0)
25955 it->ascent = 0;
25956 if (it->descent < 0)
25957 it->descent = 0;
25958
25959 if (it->glyph_row && cmp->glyph_len > 0)
25960 append_composite_glyph (it);
25961 }
25962 else if (it->what == IT_COMPOSITION)
25963 {
25964 /* A dynamic (automatic) composition. */
25965 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25966 Lisp_Object gstring;
25967 struct font_metrics metrics;
25968
25969 it->nglyphs = 1;
25970
25971 gstring = composition_gstring_from_id (it->cmp_it.id);
25972 it->pixel_width
25973 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25974 &metrics);
25975 if (it->glyph_row
25976 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25977 it->glyph_row->contains_overlapping_glyphs_p = 1;
25978 it->ascent = it->phys_ascent = metrics.ascent;
25979 it->descent = it->phys_descent = metrics.descent;
25980 if (face->box != FACE_NO_BOX)
25981 {
25982 int thick = face->box_line_width;
25983
25984 if (thick > 0)
25985 {
25986 it->ascent += thick;
25987 it->descent += thick;
25988 }
25989 else
25990 thick = - thick;
25991
25992 if (it->start_of_box_run_p)
25993 it->pixel_width += thick;
25994 if (it->end_of_box_run_p)
25995 it->pixel_width += thick;
25996 }
25997 /* If face has an overline, add the height of the overline
25998 (1 pixel) and a 1 pixel margin to the character height. */
25999 if (face->overline_p)
26000 it->ascent += overline_margin;
26001 take_vertical_position_into_account (it);
26002 if (it->ascent < 0)
26003 it->ascent = 0;
26004 if (it->descent < 0)
26005 it->descent = 0;
26006
26007 if (it->glyph_row)
26008 append_composite_glyph (it);
26009 }
26010 else if (it->what == IT_GLYPHLESS)
26011 produce_glyphless_glyph (it, 0, Qnil);
26012 else if (it->what == IT_IMAGE)
26013 produce_image_glyph (it);
26014 else if (it->what == IT_STRETCH)
26015 produce_stretch_glyph (it);
26016
26017 done:
26018 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26019 because this isn't true for images with `:ascent 100'. */
26020 eassert (it->ascent >= 0 && it->descent >= 0);
26021 if (it->area == TEXT_AREA)
26022 it->current_x += it->pixel_width;
26023
26024 if (extra_line_spacing > 0)
26025 {
26026 it->descent += extra_line_spacing;
26027 if (extra_line_spacing > it->max_extra_line_spacing)
26028 it->max_extra_line_spacing = extra_line_spacing;
26029 }
26030
26031 it->max_ascent = max (it->max_ascent, it->ascent);
26032 it->max_descent = max (it->max_descent, it->descent);
26033 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26034 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26035 }
26036
26037 /* EXPORT for RIF:
26038 Output LEN glyphs starting at START at the nominal cursor position.
26039 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26040 being updated, and UPDATED_AREA is the area of that row being updated. */
26041
26042 void
26043 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26044 struct glyph *start, enum glyph_row_area updated_area, int len)
26045 {
26046 int x, hpos, chpos = w->phys_cursor.hpos;
26047
26048 eassert (updated_row);
26049 /* When the window is hscrolled, cursor hpos can legitimately be out
26050 of bounds, but we draw the cursor at the corresponding window
26051 margin in that case. */
26052 if (!updated_row->reversed_p && chpos < 0)
26053 chpos = 0;
26054 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26055 chpos = updated_row->used[TEXT_AREA] - 1;
26056
26057 block_input ();
26058
26059 /* Write glyphs. */
26060
26061 hpos = start - updated_row->glyphs[updated_area];
26062 x = draw_glyphs (w, w->output_cursor.x,
26063 updated_row, updated_area,
26064 hpos, hpos + len,
26065 DRAW_NORMAL_TEXT, 0);
26066
26067 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26068 if (updated_area == TEXT_AREA
26069 && w->phys_cursor_on_p
26070 && w->phys_cursor.vpos == w->output_cursor.vpos
26071 && chpos >= hpos
26072 && chpos < hpos + len)
26073 w->phys_cursor_on_p = 0;
26074
26075 unblock_input ();
26076
26077 /* Advance the output cursor. */
26078 w->output_cursor.hpos += len;
26079 w->output_cursor.x = x;
26080 }
26081
26082
26083 /* EXPORT for RIF:
26084 Insert LEN glyphs from START at the nominal cursor position. */
26085
26086 void
26087 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26088 struct glyph *start, enum glyph_row_area updated_area, int len)
26089 {
26090 struct frame *f;
26091 int line_height, shift_by_width, shifted_region_width;
26092 struct glyph_row *row;
26093 struct glyph *glyph;
26094 int frame_x, frame_y;
26095 ptrdiff_t hpos;
26096
26097 eassert (updated_row);
26098 block_input ();
26099 f = XFRAME (WINDOW_FRAME (w));
26100
26101 /* Get the height of the line we are in. */
26102 row = updated_row;
26103 line_height = row->height;
26104
26105 /* Get the width of the glyphs to insert. */
26106 shift_by_width = 0;
26107 for (glyph = start; glyph < start + len; ++glyph)
26108 shift_by_width += glyph->pixel_width;
26109
26110 /* Get the width of the region to shift right. */
26111 shifted_region_width = (window_box_width (w, updated_area)
26112 - w->output_cursor.x
26113 - shift_by_width);
26114
26115 /* Shift right. */
26116 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26117 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26118
26119 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26120 line_height, shift_by_width);
26121
26122 /* Write the glyphs. */
26123 hpos = start - row->glyphs[updated_area];
26124 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26125 hpos, hpos + len,
26126 DRAW_NORMAL_TEXT, 0);
26127
26128 /* Advance the output cursor. */
26129 w->output_cursor.hpos += len;
26130 w->output_cursor.x += shift_by_width;
26131 unblock_input ();
26132 }
26133
26134
26135 /* EXPORT for RIF:
26136 Erase the current text line from the nominal cursor position
26137 (inclusive) to pixel column TO_X (exclusive). The idea is that
26138 everything from TO_X onward is already erased.
26139
26140 TO_X is a pixel position relative to UPDATED_AREA of currently
26141 updated window W. TO_X == -1 means clear to the end of this area. */
26142
26143 void
26144 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26145 enum glyph_row_area updated_area, int to_x)
26146 {
26147 struct frame *f;
26148 int max_x, min_y, max_y;
26149 int from_x, from_y, to_y;
26150
26151 eassert (updated_row);
26152 f = XFRAME (w->frame);
26153
26154 if (updated_row->full_width_p)
26155 max_x = (WINDOW_PIXEL_WIDTH (w)
26156 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26157 else
26158 max_x = window_box_width (w, updated_area);
26159 max_y = window_text_bottom_y (w);
26160
26161 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26162 of window. For TO_X > 0, truncate to end of drawing area. */
26163 if (to_x == 0)
26164 return;
26165 else if (to_x < 0)
26166 to_x = max_x;
26167 else
26168 to_x = min (to_x, max_x);
26169
26170 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26171
26172 /* Notice if the cursor will be cleared by this operation. */
26173 if (!updated_row->full_width_p)
26174 notice_overwritten_cursor (w, updated_area,
26175 w->output_cursor.x, -1,
26176 updated_row->y,
26177 MATRIX_ROW_BOTTOM_Y (updated_row));
26178
26179 from_x = w->output_cursor.x;
26180
26181 /* Translate to frame coordinates. */
26182 if (updated_row->full_width_p)
26183 {
26184 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26185 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26186 }
26187 else
26188 {
26189 int area_left = window_box_left (w, updated_area);
26190 from_x += area_left;
26191 to_x += area_left;
26192 }
26193
26194 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26195 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26196 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26197
26198 /* Prevent inadvertently clearing to end of the X window. */
26199 if (to_x > from_x && to_y > from_y)
26200 {
26201 block_input ();
26202 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26203 to_x - from_x, to_y - from_y);
26204 unblock_input ();
26205 }
26206 }
26207
26208 #endif /* HAVE_WINDOW_SYSTEM */
26209
26210
26211 \f
26212 /***********************************************************************
26213 Cursor types
26214 ***********************************************************************/
26215
26216 /* Value is the internal representation of the specified cursor type
26217 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26218 of the bar cursor. */
26219
26220 static enum text_cursor_kinds
26221 get_specified_cursor_type (Lisp_Object arg, int *width)
26222 {
26223 enum text_cursor_kinds type;
26224
26225 if (NILP (arg))
26226 return NO_CURSOR;
26227
26228 if (EQ (arg, Qbox))
26229 return FILLED_BOX_CURSOR;
26230
26231 if (EQ (arg, Qhollow))
26232 return HOLLOW_BOX_CURSOR;
26233
26234 if (EQ (arg, Qbar))
26235 {
26236 *width = 2;
26237 return BAR_CURSOR;
26238 }
26239
26240 if (CONSP (arg)
26241 && EQ (XCAR (arg), Qbar)
26242 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26243 {
26244 *width = XINT (XCDR (arg));
26245 return BAR_CURSOR;
26246 }
26247
26248 if (EQ (arg, Qhbar))
26249 {
26250 *width = 2;
26251 return HBAR_CURSOR;
26252 }
26253
26254 if (CONSP (arg)
26255 && EQ (XCAR (arg), Qhbar)
26256 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26257 {
26258 *width = XINT (XCDR (arg));
26259 return HBAR_CURSOR;
26260 }
26261
26262 /* Treat anything unknown as "hollow box cursor".
26263 It was bad to signal an error; people have trouble fixing
26264 .Xdefaults with Emacs, when it has something bad in it. */
26265 type = HOLLOW_BOX_CURSOR;
26266
26267 return type;
26268 }
26269
26270 /* Set the default cursor types for specified frame. */
26271 void
26272 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26273 {
26274 int width = 1;
26275 Lisp_Object tem;
26276
26277 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26278 FRAME_CURSOR_WIDTH (f) = width;
26279
26280 /* By default, set up the blink-off state depending on the on-state. */
26281
26282 tem = Fassoc (arg, Vblink_cursor_alist);
26283 if (!NILP (tem))
26284 {
26285 FRAME_BLINK_OFF_CURSOR (f)
26286 = get_specified_cursor_type (XCDR (tem), &width);
26287 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26288 }
26289 else
26290 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26291
26292 /* Make sure the cursor gets redrawn. */
26293 f->cursor_type_changed = 1;
26294 }
26295
26296
26297 #ifdef HAVE_WINDOW_SYSTEM
26298
26299 /* Return the cursor we want to be displayed in window W. Return
26300 width of bar/hbar cursor through WIDTH arg. Return with
26301 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26302 (i.e. if the `system caret' should track this cursor).
26303
26304 In a mini-buffer window, we want the cursor only to appear if we
26305 are reading input from this window. For the selected window, we
26306 want the cursor type given by the frame parameter or buffer local
26307 setting of cursor-type. If explicitly marked off, draw no cursor.
26308 In all other cases, we want a hollow box cursor. */
26309
26310 static enum text_cursor_kinds
26311 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26312 int *active_cursor)
26313 {
26314 struct frame *f = XFRAME (w->frame);
26315 struct buffer *b = XBUFFER (w->contents);
26316 int cursor_type = DEFAULT_CURSOR;
26317 Lisp_Object alt_cursor;
26318 int non_selected = 0;
26319
26320 *active_cursor = 1;
26321
26322 /* Echo area */
26323 if (cursor_in_echo_area
26324 && FRAME_HAS_MINIBUF_P (f)
26325 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26326 {
26327 if (w == XWINDOW (echo_area_window))
26328 {
26329 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26330 {
26331 *width = FRAME_CURSOR_WIDTH (f);
26332 return FRAME_DESIRED_CURSOR (f);
26333 }
26334 else
26335 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26336 }
26337
26338 *active_cursor = 0;
26339 non_selected = 1;
26340 }
26341
26342 /* Detect a nonselected window or nonselected frame. */
26343 else if (w != XWINDOW (f->selected_window)
26344 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26345 {
26346 *active_cursor = 0;
26347
26348 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26349 return NO_CURSOR;
26350
26351 non_selected = 1;
26352 }
26353
26354 /* Never display a cursor in a window in which cursor-type is nil. */
26355 if (NILP (BVAR (b, cursor_type)))
26356 return NO_CURSOR;
26357
26358 /* Get the normal cursor type for this window. */
26359 if (EQ (BVAR (b, cursor_type), Qt))
26360 {
26361 cursor_type = FRAME_DESIRED_CURSOR (f);
26362 *width = FRAME_CURSOR_WIDTH (f);
26363 }
26364 else
26365 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26366
26367 /* Use cursor-in-non-selected-windows instead
26368 for non-selected window or frame. */
26369 if (non_selected)
26370 {
26371 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26372 if (!EQ (Qt, alt_cursor))
26373 return get_specified_cursor_type (alt_cursor, width);
26374 /* t means modify the normal cursor type. */
26375 if (cursor_type == FILLED_BOX_CURSOR)
26376 cursor_type = HOLLOW_BOX_CURSOR;
26377 else if (cursor_type == BAR_CURSOR && *width > 1)
26378 --*width;
26379 return cursor_type;
26380 }
26381
26382 /* Use normal cursor if not blinked off. */
26383 if (!w->cursor_off_p)
26384 {
26385 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26386 {
26387 if (cursor_type == FILLED_BOX_CURSOR)
26388 {
26389 /* Using a block cursor on large images can be very annoying.
26390 So use a hollow cursor for "large" images.
26391 If image is not transparent (no mask), also use hollow cursor. */
26392 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26393 if (img != NULL && IMAGEP (img->spec))
26394 {
26395 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26396 where N = size of default frame font size.
26397 This should cover most of the "tiny" icons people may use. */
26398 if (!img->mask
26399 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26400 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26401 cursor_type = HOLLOW_BOX_CURSOR;
26402 }
26403 }
26404 else if (cursor_type != NO_CURSOR)
26405 {
26406 /* Display current only supports BOX and HOLLOW cursors for images.
26407 So for now, unconditionally use a HOLLOW cursor when cursor is
26408 not a solid box cursor. */
26409 cursor_type = HOLLOW_BOX_CURSOR;
26410 }
26411 }
26412 return cursor_type;
26413 }
26414
26415 /* Cursor is blinked off, so determine how to "toggle" it. */
26416
26417 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26418 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26419 return get_specified_cursor_type (XCDR (alt_cursor), width);
26420
26421 /* Then see if frame has specified a specific blink off cursor type. */
26422 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26423 {
26424 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26425 return FRAME_BLINK_OFF_CURSOR (f);
26426 }
26427
26428 #if 0
26429 /* Some people liked having a permanently visible blinking cursor,
26430 while others had very strong opinions against it. So it was
26431 decided to remove it. KFS 2003-09-03 */
26432
26433 /* Finally perform built-in cursor blinking:
26434 filled box <-> hollow box
26435 wide [h]bar <-> narrow [h]bar
26436 narrow [h]bar <-> no cursor
26437 other type <-> no cursor */
26438
26439 if (cursor_type == FILLED_BOX_CURSOR)
26440 return HOLLOW_BOX_CURSOR;
26441
26442 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26443 {
26444 *width = 1;
26445 return cursor_type;
26446 }
26447 #endif
26448
26449 return NO_CURSOR;
26450 }
26451
26452
26453 /* Notice when the text cursor of window W has been completely
26454 overwritten by a drawing operation that outputs glyphs in AREA
26455 starting at X0 and ending at X1 in the line starting at Y0 and
26456 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26457 the rest of the line after X0 has been written. Y coordinates
26458 are window-relative. */
26459
26460 static void
26461 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26462 int x0, int x1, int y0, int y1)
26463 {
26464 int cx0, cx1, cy0, cy1;
26465 struct glyph_row *row;
26466
26467 if (!w->phys_cursor_on_p)
26468 return;
26469 if (area != TEXT_AREA)
26470 return;
26471
26472 if (w->phys_cursor.vpos < 0
26473 || w->phys_cursor.vpos >= w->current_matrix->nrows
26474 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26475 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26476 return;
26477
26478 if (row->cursor_in_fringe_p)
26479 {
26480 row->cursor_in_fringe_p = 0;
26481 draw_fringe_bitmap (w, row, row->reversed_p);
26482 w->phys_cursor_on_p = 0;
26483 return;
26484 }
26485
26486 cx0 = w->phys_cursor.x;
26487 cx1 = cx0 + w->phys_cursor_width;
26488 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26489 return;
26490
26491 /* The cursor image will be completely removed from the
26492 screen if the output area intersects the cursor area in
26493 y-direction. When we draw in [y0 y1[, and some part of
26494 the cursor is at y < y0, that part must have been drawn
26495 before. When scrolling, the cursor is erased before
26496 actually scrolling, so we don't come here. When not
26497 scrolling, the rows above the old cursor row must have
26498 changed, and in this case these rows must have written
26499 over the cursor image.
26500
26501 Likewise if part of the cursor is below y1, with the
26502 exception of the cursor being in the first blank row at
26503 the buffer and window end because update_text_area
26504 doesn't draw that row. (Except when it does, but
26505 that's handled in update_text_area.) */
26506
26507 cy0 = w->phys_cursor.y;
26508 cy1 = cy0 + w->phys_cursor_height;
26509 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26510 return;
26511
26512 w->phys_cursor_on_p = 0;
26513 }
26514
26515 #endif /* HAVE_WINDOW_SYSTEM */
26516
26517 \f
26518 /************************************************************************
26519 Mouse Face
26520 ************************************************************************/
26521
26522 #ifdef HAVE_WINDOW_SYSTEM
26523
26524 /* EXPORT for RIF:
26525 Fix the display of area AREA of overlapping row ROW in window W
26526 with respect to the overlapping part OVERLAPS. */
26527
26528 void
26529 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26530 enum glyph_row_area area, int overlaps)
26531 {
26532 int i, x;
26533
26534 block_input ();
26535
26536 x = 0;
26537 for (i = 0; i < row->used[area];)
26538 {
26539 if (row->glyphs[area][i].overlaps_vertically_p)
26540 {
26541 int start = i, start_x = x;
26542
26543 do
26544 {
26545 x += row->glyphs[area][i].pixel_width;
26546 ++i;
26547 }
26548 while (i < row->used[area]
26549 && row->glyphs[area][i].overlaps_vertically_p);
26550
26551 draw_glyphs (w, start_x, row, area,
26552 start, i,
26553 DRAW_NORMAL_TEXT, overlaps);
26554 }
26555 else
26556 {
26557 x += row->glyphs[area][i].pixel_width;
26558 ++i;
26559 }
26560 }
26561
26562 unblock_input ();
26563 }
26564
26565
26566 /* EXPORT:
26567 Draw the cursor glyph of window W in glyph row ROW. See the
26568 comment of draw_glyphs for the meaning of HL. */
26569
26570 void
26571 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26572 enum draw_glyphs_face hl)
26573 {
26574 /* If cursor hpos is out of bounds, don't draw garbage. This can
26575 happen in mini-buffer windows when switching between echo area
26576 glyphs and mini-buffer. */
26577 if ((row->reversed_p
26578 ? (w->phys_cursor.hpos >= 0)
26579 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26580 {
26581 int on_p = w->phys_cursor_on_p;
26582 int x1;
26583 int hpos = w->phys_cursor.hpos;
26584
26585 /* When the window is hscrolled, cursor hpos can legitimately be
26586 out of bounds, but we draw the cursor at the corresponding
26587 window margin in that case. */
26588 if (!row->reversed_p && hpos < 0)
26589 hpos = 0;
26590 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26591 hpos = row->used[TEXT_AREA] - 1;
26592
26593 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26594 hl, 0);
26595 w->phys_cursor_on_p = on_p;
26596
26597 if (hl == DRAW_CURSOR)
26598 w->phys_cursor_width = x1 - w->phys_cursor.x;
26599 /* When we erase the cursor, and ROW is overlapped by other
26600 rows, make sure that these overlapping parts of other rows
26601 are redrawn. */
26602 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26603 {
26604 w->phys_cursor_width = x1 - w->phys_cursor.x;
26605
26606 if (row > w->current_matrix->rows
26607 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26608 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26609 OVERLAPS_ERASED_CURSOR);
26610
26611 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26612 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26613 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26614 OVERLAPS_ERASED_CURSOR);
26615 }
26616 }
26617 }
26618
26619
26620 /* Erase the image of a cursor of window W from the screen. */
26621
26622 #ifndef HAVE_NTGUI
26623 static
26624 #endif
26625 void
26626 erase_phys_cursor (struct window *w)
26627 {
26628 struct frame *f = XFRAME (w->frame);
26629 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26630 int hpos = w->phys_cursor.hpos;
26631 int vpos = w->phys_cursor.vpos;
26632 int mouse_face_here_p = 0;
26633 struct glyph_matrix *active_glyphs = w->current_matrix;
26634 struct glyph_row *cursor_row;
26635 struct glyph *cursor_glyph;
26636 enum draw_glyphs_face hl;
26637
26638 /* No cursor displayed or row invalidated => nothing to do on the
26639 screen. */
26640 if (w->phys_cursor_type == NO_CURSOR)
26641 goto mark_cursor_off;
26642
26643 /* VPOS >= active_glyphs->nrows means that window has been resized.
26644 Don't bother to erase the cursor. */
26645 if (vpos >= active_glyphs->nrows)
26646 goto mark_cursor_off;
26647
26648 /* If row containing cursor is marked invalid, there is nothing we
26649 can do. */
26650 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26651 if (!cursor_row->enabled_p)
26652 goto mark_cursor_off;
26653
26654 /* If line spacing is > 0, old cursor may only be partially visible in
26655 window after split-window. So adjust visible height. */
26656 cursor_row->visible_height = min (cursor_row->visible_height,
26657 window_text_bottom_y (w) - cursor_row->y);
26658
26659 /* If row is completely invisible, don't attempt to delete a cursor which
26660 isn't there. This can happen if cursor is at top of a window, and
26661 we switch to a buffer with a header line in that window. */
26662 if (cursor_row->visible_height <= 0)
26663 goto mark_cursor_off;
26664
26665 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26666 if (cursor_row->cursor_in_fringe_p)
26667 {
26668 cursor_row->cursor_in_fringe_p = 0;
26669 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26670 goto mark_cursor_off;
26671 }
26672
26673 /* This can happen when the new row is shorter than the old one.
26674 In this case, either draw_glyphs or clear_end_of_line
26675 should have cleared the cursor. Note that we wouldn't be
26676 able to erase the cursor in this case because we don't have a
26677 cursor glyph at hand. */
26678 if ((cursor_row->reversed_p
26679 ? (w->phys_cursor.hpos < 0)
26680 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26681 goto mark_cursor_off;
26682
26683 /* When the window is hscrolled, cursor hpos can legitimately be out
26684 of bounds, but we draw the cursor at the corresponding window
26685 margin in that case. */
26686 if (!cursor_row->reversed_p && hpos < 0)
26687 hpos = 0;
26688 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26689 hpos = cursor_row->used[TEXT_AREA] - 1;
26690
26691 /* If the cursor is in the mouse face area, redisplay that when
26692 we clear the cursor. */
26693 if (! NILP (hlinfo->mouse_face_window)
26694 && coords_in_mouse_face_p (w, hpos, vpos)
26695 /* Don't redraw the cursor's spot in mouse face if it is at the
26696 end of a line (on a newline). The cursor appears there, but
26697 mouse highlighting does not. */
26698 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26699 mouse_face_here_p = 1;
26700
26701 /* Maybe clear the display under the cursor. */
26702 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26703 {
26704 int x, y, left_x;
26705 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26706 int width;
26707
26708 cursor_glyph = get_phys_cursor_glyph (w);
26709 if (cursor_glyph == NULL)
26710 goto mark_cursor_off;
26711
26712 width = cursor_glyph->pixel_width;
26713 left_x = window_box_left_offset (w, TEXT_AREA);
26714 x = w->phys_cursor.x;
26715 if (x < left_x)
26716 width -= left_x - x;
26717 width = min (width, window_box_width (w, TEXT_AREA) - x);
26718 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26719 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26720
26721 if (width > 0)
26722 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26723 }
26724
26725 /* Erase the cursor by redrawing the character underneath it. */
26726 if (mouse_face_here_p)
26727 hl = DRAW_MOUSE_FACE;
26728 else
26729 hl = DRAW_NORMAL_TEXT;
26730 draw_phys_cursor_glyph (w, cursor_row, hl);
26731
26732 mark_cursor_off:
26733 w->phys_cursor_on_p = 0;
26734 w->phys_cursor_type = NO_CURSOR;
26735 }
26736
26737
26738 /* EXPORT:
26739 Display or clear cursor of window W. If ON is zero, clear the
26740 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26741 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26742
26743 void
26744 display_and_set_cursor (struct window *w, bool on,
26745 int hpos, int vpos, int x, int y)
26746 {
26747 struct frame *f = XFRAME (w->frame);
26748 int new_cursor_type;
26749 int new_cursor_width;
26750 int active_cursor;
26751 struct glyph_row *glyph_row;
26752 struct glyph *glyph;
26753
26754 /* This is pointless on invisible frames, and dangerous on garbaged
26755 windows and frames; in the latter case, the frame or window may
26756 be in the midst of changing its size, and x and y may be off the
26757 window. */
26758 if (! FRAME_VISIBLE_P (f)
26759 || FRAME_GARBAGED_P (f)
26760 || vpos >= w->current_matrix->nrows
26761 || hpos >= w->current_matrix->matrix_w)
26762 return;
26763
26764 /* If cursor is off and we want it off, return quickly. */
26765 if (!on && !w->phys_cursor_on_p)
26766 return;
26767
26768 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26769 /* If cursor row is not enabled, we don't really know where to
26770 display the cursor. */
26771 if (!glyph_row->enabled_p)
26772 {
26773 w->phys_cursor_on_p = 0;
26774 return;
26775 }
26776
26777 glyph = NULL;
26778 if (!glyph_row->exact_window_width_line_p
26779 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26780 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26781
26782 eassert (input_blocked_p ());
26783
26784 /* Set new_cursor_type to the cursor we want to be displayed. */
26785 new_cursor_type = get_window_cursor_type (w, glyph,
26786 &new_cursor_width, &active_cursor);
26787
26788 /* If cursor is currently being shown and we don't want it to be or
26789 it is in the wrong place, or the cursor type is not what we want,
26790 erase it. */
26791 if (w->phys_cursor_on_p
26792 && (!on
26793 || w->phys_cursor.x != x
26794 || w->phys_cursor.y != y
26795 || new_cursor_type != w->phys_cursor_type
26796 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26797 && new_cursor_width != w->phys_cursor_width)))
26798 erase_phys_cursor (w);
26799
26800 /* Don't check phys_cursor_on_p here because that flag is only set
26801 to zero in some cases where we know that the cursor has been
26802 completely erased, to avoid the extra work of erasing the cursor
26803 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26804 still not be visible, or it has only been partly erased. */
26805 if (on)
26806 {
26807 w->phys_cursor_ascent = glyph_row->ascent;
26808 w->phys_cursor_height = glyph_row->height;
26809
26810 /* Set phys_cursor_.* before x_draw_.* is called because some
26811 of them may need the information. */
26812 w->phys_cursor.x = x;
26813 w->phys_cursor.y = glyph_row->y;
26814 w->phys_cursor.hpos = hpos;
26815 w->phys_cursor.vpos = vpos;
26816 }
26817
26818 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26819 new_cursor_type, new_cursor_width,
26820 on, active_cursor);
26821 }
26822
26823
26824 /* Switch the display of W's cursor on or off, according to the value
26825 of ON. */
26826
26827 static void
26828 update_window_cursor (struct window *w, bool on)
26829 {
26830 /* Don't update cursor in windows whose frame is in the process
26831 of being deleted. */
26832 if (w->current_matrix)
26833 {
26834 int hpos = w->phys_cursor.hpos;
26835 int vpos = w->phys_cursor.vpos;
26836 struct glyph_row *row;
26837
26838 if (vpos >= w->current_matrix->nrows
26839 || hpos >= w->current_matrix->matrix_w)
26840 return;
26841
26842 row = MATRIX_ROW (w->current_matrix, vpos);
26843
26844 /* When the window is hscrolled, cursor hpos can legitimately be
26845 out of bounds, but we draw the cursor at the corresponding
26846 window margin in that case. */
26847 if (!row->reversed_p && hpos < 0)
26848 hpos = 0;
26849 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26850 hpos = row->used[TEXT_AREA] - 1;
26851
26852 block_input ();
26853 display_and_set_cursor (w, on, hpos, vpos,
26854 w->phys_cursor.x, w->phys_cursor.y);
26855 unblock_input ();
26856 }
26857 }
26858
26859
26860 /* Call update_window_cursor with parameter ON_P on all leaf windows
26861 in the window tree rooted at W. */
26862
26863 static void
26864 update_cursor_in_window_tree (struct window *w, bool on_p)
26865 {
26866 while (w)
26867 {
26868 if (WINDOWP (w->contents))
26869 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26870 else
26871 update_window_cursor (w, on_p);
26872
26873 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26874 }
26875 }
26876
26877
26878 /* EXPORT:
26879 Display the cursor on window W, or clear it, according to ON_P.
26880 Don't change the cursor's position. */
26881
26882 void
26883 x_update_cursor (struct frame *f, bool on_p)
26884 {
26885 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26886 }
26887
26888
26889 /* EXPORT:
26890 Clear the cursor of window W to background color, and mark the
26891 cursor as not shown. This is used when the text where the cursor
26892 is about to be rewritten. */
26893
26894 void
26895 x_clear_cursor (struct window *w)
26896 {
26897 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26898 update_window_cursor (w, 0);
26899 }
26900
26901 #endif /* HAVE_WINDOW_SYSTEM */
26902
26903 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26904 and MSDOS. */
26905 static void
26906 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26907 int start_hpos, int end_hpos,
26908 enum draw_glyphs_face draw)
26909 {
26910 #ifdef HAVE_WINDOW_SYSTEM
26911 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26912 {
26913 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26914 return;
26915 }
26916 #endif
26917 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26918 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26919 #endif
26920 }
26921
26922 /* Display the active region described by mouse_face_* according to DRAW. */
26923
26924 static void
26925 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26926 {
26927 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26928 struct frame *f = XFRAME (WINDOW_FRAME (w));
26929
26930 if (/* If window is in the process of being destroyed, don't bother
26931 to do anything. */
26932 w->current_matrix != NULL
26933 /* Don't update mouse highlight if hidden */
26934 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26935 /* Recognize when we are called to operate on rows that don't exist
26936 anymore. This can happen when a window is split. */
26937 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26938 {
26939 int phys_cursor_on_p = w->phys_cursor_on_p;
26940 struct glyph_row *row, *first, *last;
26941
26942 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26943 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26944
26945 for (row = first; row <= last && row->enabled_p; ++row)
26946 {
26947 int start_hpos, end_hpos, start_x;
26948
26949 /* For all but the first row, the highlight starts at column 0. */
26950 if (row == first)
26951 {
26952 /* R2L rows have BEG and END in reversed order, but the
26953 screen drawing geometry is always left to right. So
26954 we need to mirror the beginning and end of the
26955 highlighted area in R2L rows. */
26956 if (!row->reversed_p)
26957 {
26958 start_hpos = hlinfo->mouse_face_beg_col;
26959 start_x = hlinfo->mouse_face_beg_x;
26960 }
26961 else if (row == last)
26962 {
26963 start_hpos = hlinfo->mouse_face_end_col;
26964 start_x = hlinfo->mouse_face_end_x;
26965 }
26966 else
26967 {
26968 start_hpos = 0;
26969 start_x = 0;
26970 }
26971 }
26972 else if (row->reversed_p && row == last)
26973 {
26974 start_hpos = hlinfo->mouse_face_end_col;
26975 start_x = hlinfo->mouse_face_end_x;
26976 }
26977 else
26978 {
26979 start_hpos = 0;
26980 start_x = 0;
26981 }
26982
26983 if (row == last)
26984 {
26985 if (!row->reversed_p)
26986 end_hpos = hlinfo->mouse_face_end_col;
26987 else if (row == first)
26988 end_hpos = hlinfo->mouse_face_beg_col;
26989 else
26990 {
26991 end_hpos = row->used[TEXT_AREA];
26992 if (draw == DRAW_NORMAL_TEXT)
26993 row->fill_line_p = 1; /* Clear to end of line */
26994 }
26995 }
26996 else if (row->reversed_p && row == first)
26997 end_hpos = hlinfo->mouse_face_beg_col;
26998 else
26999 {
27000 end_hpos = row->used[TEXT_AREA];
27001 if (draw == DRAW_NORMAL_TEXT)
27002 row->fill_line_p = 1; /* Clear to end of line */
27003 }
27004
27005 if (end_hpos > start_hpos)
27006 {
27007 draw_row_with_mouse_face (w, start_x, row,
27008 start_hpos, end_hpos, draw);
27009
27010 row->mouse_face_p
27011 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27012 }
27013 }
27014
27015 #ifdef HAVE_WINDOW_SYSTEM
27016 /* When we've written over the cursor, arrange for it to
27017 be displayed again. */
27018 if (FRAME_WINDOW_P (f)
27019 && phys_cursor_on_p && !w->phys_cursor_on_p)
27020 {
27021 int hpos = w->phys_cursor.hpos;
27022
27023 /* When the window is hscrolled, cursor hpos can legitimately be
27024 out of bounds, but we draw the cursor at the corresponding
27025 window margin in that case. */
27026 if (!row->reversed_p && hpos < 0)
27027 hpos = 0;
27028 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27029 hpos = row->used[TEXT_AREA] - 1;
27030
27031 block_input ();
27032 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27033 w->phys_cursor.x, w->phys_cursor.y);
27034 unblock_input ();
27035 }
27036 #endif /* HAVE_WINDOW_SYSTEM */
27037 }
27038
27039 #ifdef HAVE_WINDOW_SYSTEM
27040 /* Change the mouse cursor. */
27041 if (FRAME_WINDOW_P (f))
27042 {
27043 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27044 if (draw == DRAW_NORMAL_TEXT
27045 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27046 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27047 else
27048 #endif
27049 if (draw == DRAW_MOUSE_FACE)
27050 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27051 else
27052 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27053 }
27054 #endif /* HAVE_WINDOW_SYSTEM */
27055 }
27056
27057 /* EXPORT:
27058 Clear out the mouse-highlighted active region.
27059 Redraw it un-highlighted first. Value is non-zero if mouse
27060 face was actually drawn unhighlighted. */
27061
27062 int
27063 clear_mouse_face (Mouse_HLInfo *hlinfo)
27064 {
27065 int cleared = 0;
27066
27067 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27068 {
27069 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27070 cleared = 1;
27071 }
27072
27073 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27074 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27075 hlinfo->mouse_face_window = Qnil;
27076 hlinfo->mouse_face_overlay = Qnil;
27077 return cleared;
27078 }
27079
27080 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
27081 within the mouse face on that window. */
27082 static int
27083 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27084 {
27085 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27086
27087 /* Quickly resolve the easy cases. */
27088 if (!(WINDOWP (hlinfo->mouse_face_window)
27089 && XWINDOW (hlinfo->mouse_face_window) == w))
27090 return 0;
27091 if (vpos < hlinfo->mouse_face_beg_row
27092 || vpos > hlinfo->mouse_face_end_row)
27093 return 0;
27094 if (vpos > hlinfo->mouse_face_beg_row
27095 && vpos < hlinfo->mouse_face_end_row)
27096 return 1;
27097
27098 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27099 {
27100 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27101 {
27102 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27103 return 1;
27104 }
27105 else if ((vpos == hlinfo->mouse_face_beg_row
27106 && hpos >= hlinfo->mouse_face_beg_col)
27107 || (vpos == hlinfo->mouse_face_end_row
27108 && hpos < hlinfo->mouse_face_end_col))
27109 return 1;
27110 }
27111 else
27112 {
27113 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27114 {
27115 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27116 return 1;
27117 }
27118 else if ((vpos == hlinfo->mouse_face_beg_row
27119 && hpos <= hlinfo->mouse_face_beg_col)
27120 || (vpos == hlinfo->mouse_face_end_row
27121 && hpos > hlinfo->mouse_face_end_col))
27122 return 1;
27123 }
27124 return 0;
27125 }
27126
27127
27128 /* EXPORT:
27129 Non-zero if physical cursor of window W is within mouse face. */
27130
27131 int
27132 cursor_in_mouse_face_p (struct window *w)
27133 {
27134 int hpos = w->phys_cursor.hpos;
27135 int vpos = w->phys_cursor.vpos;
27136 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27137
27138 /* When the window is hscrolled, cursor hpos can legitimately be out
27139 of bounds, but we draw the cursor at the corresponding window
27140 margin in that case. */
27141 if (!row->reversed_p && hpos < 0)
27142 hpos = 0;
27143 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27144 hpos = row->used[TEXT_AREA] - 1;
27145
27146 return coords_in_mouse_face_p (w, hpos, vpos);
27147 }
27148
27149
27150 \f
27151 /* Find the glyph rows START_ROW and END_ROW of window W that display
27152 characters between buffer positions START_CHARPOS and END_CHARPOS
27153 (excluding END_CHARPOS). DISP_STRING is a display string that
27154 covers these buffer positions. This is similar to
27155 row_containing_pos, but is more accurate when bidi reordering makes
27156 buffer positions change non-linearly with glyph rows. */
27157 static void
27158 rows_from_pos_range (struct window *w,
27159 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27160 Lisp_Object disp_string,
27161 struct glyph_row **start, struct glyph_row **end)
27162 {
27163 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27164 int last_y = window_text_bottom_y (w);
27165 struct glyph_row *row;
27166
27167 *start = NULL;
27168 *end = NULL;
27169
27170 while (!first->enabled_p
27171 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27172 first++;
27173
27174 /* Find the START row. */
27175 for (row = first;
27176 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27177 row++)
27178 {
27179 /* A row can potentially be the START row if the range of the
27180 characters it displays intersects the range
27181 [START_CHARPOS..END_CHARPOS). */
27182 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27183 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27184 /* See the commentary in row_containing_pos, for the
27185 explanation of the complicated way to check whether
27186 some position is beyond the end of the characters
27187 displayed by a row. */
27188 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27189 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27190 && !row->ends_at_zv_p
27191 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27192 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27193 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27194 && !row->ends_at_zv_p
27195 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27196 {
27197 /* Found a candidate row. Now make sure at least one of the
27198 glyphs it displays has a charpos from the range
27199 [START_CHARPOS..END_CHARPOS).
27200
27201 This is not obvious because bidi reordering could make
27202 buffer positions of a row be 1,2,3,102,101,100, and if we
27203 want to highlight characters in [50..60), we don't want
27204 this row, even though [50..60) does intersect [1..103),
27205 the range of character positions given by the row's start
27206 and end positions. */
27207 struct glyph *g = row->glyphs[TEXT_AREA];
27208 struct glyph *e = g + row->used[TEXT_AREA];
27209
27210 while (g < e)
27211 {
27212 if (((BUFFERP (g->object) || INTEGERP (g->object))
27213 && start_charpos <= g->charpos && g->charpos < end_charpos)
27214 /* A glyph that comes from DISP_STRING is by
27215 definition to be highlighted. */
27216 || EQ (g->object, disp_string))
27217 *start = row;
27218 g++;
27219 }
27220 if (*start)
27221 break;
27222 }
27223 }
27224
27225 /* Find the END row. */
27226 if (!*start
27227 /* If the last row is partially visible, start looking for END
27228 from that row, instead of starting from FIRST. */
27229 && !(row->enabled_p
27230 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27231 row = first;
27232 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27233 {
27234 struct glyph_row *next = row + 1;
27235 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27236
27237 if (!next->enabled_p
27238 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27239 /* The first row >= START whose range of displayed characters
27240 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27241 is the row END + 1. */
27242 || (start_charpos < next_start
27243 && end_charpos < next_start)
27244 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27245 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27246 && !next->ends_at_zv_p
27247 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27248 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27249 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27250 && !next->ends_at_zv_p
27251 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27252 {
27253 *end = row;
27254 break;
27255 }
27256 else
27257 {
27258 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27259 but none of the characters it displays are in the range, it is
27260 also END + 1. */
27261 struct glyph *g = next->glyphs[TEXT_AREA];
27262 struct glyph *s = g;
27263 struct glyph *e = g + next->used[TEXT_AREA];
27264
27265 while (g < e)
27266 {
27267 if (((BUFFERP (g->object) || INTEGERP (g->object))
27268 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27269 /* If the buffer position of the first glyph in
27270 the row is equal to END_CHARPOS, it means
27271 the last character to be highlighted is the
27272 newline of ROW, and we must consider NEXT as
27273 END, not END+1. */
27274 || (((!next->reversed_p && g == s)
27275 || (next->reversed_p && g == e - 1))
27276 && (g->charpos == end_charpos
27277 /* Special case for when NEXT is an
27278 empty line at ZV. */
27279 || (g->charpos == -1
27280 && !row->ends_at_zv_p
27281 && next_start == end_charpos)))))
27282 /* A glyph that comes from DISP_STRING is by
27283 definition to be highlighted. */
27284 || EQ (g->object, disp_string))
27285 break;
27286 g++;
27287 }
27288 if (g == e)
27289 {
27290 *end = row;
27291 break;
27292 }
27293 /* The first row that ends at ZV must be the last to be
27294 highlighted. */
27295 else if (next->ends_at_zv_p)
27296 {
27297 *end = next;
27298 break;
27299 }
27300 }
27301 }
27302 }
27303
27304 /* This function sets the mouse_face_* elements of HLINFO, assuming
27305 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27306 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27307 for the overlay or run of text properties specifying the mouse
27308 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27309 before-string and after-string that must also be highlighted.
27310 DISP_STRING, if non-nil, is a display string that may cover some
27311 or all of the highlighted text. */
27312
27313 static void
27314 mouse_face_from_buffer_pos (Lisp_Object window,
27315 Mouse_HLInfo *hlinfo,
27316 ptrdiff_t mouse_charpos,
27317 ptrdiff_t start_charpos,
27318 ptrdiff_t end_charpos,
27319 Lisp_Object before_string,
27320 Lisp_Object after_string,
27321 Lisp_Object disp_string)
27322 {
27323 struct window *w = XWINDOW (window);
27324 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27325 struct glyph_row *r1, *r2;
27326 struct glyph *glyph, *end;
27327 ptrdiff_t ignore, pos;
27328 int x;
27329
27330 eassert (NILP (disp_string) || STRINGP (disp_string));
27331 eassert (NILP (before_string) || STRINGP (before_string));
27332 eassert (NILP (after_string) || STRINGP (after_string));
27333
27334 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27335 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27336 if (r1 == NULL)
27337 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27338 /* If the before-string or display-string contains newlines,
27339 rows_from_pos_range skips to its last row. Move back. */
27340 if (!NILP (before_string) || !NILP (disp_string))
27341 {
27342 struct glyph_row *prev;
27343 while ((prev = r1 - 1, prev >= first)
27344 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27345 && prev->used[TEXT_AREA] > 0)
27346 {
27347 struct glyph *beg = prev->glyphs[TEXT_AREA];
27348 glyph = beg + prev->used[TEXT_AREA];
27349 while (--glyph >= beg && INTEGERP (glyph->object));
27350 if (glyph < beg
27351 || !(EQ (glyph->object, before_string)
27352 || EQ (glyph->object, disp_string)))
27353 break;
27354 r1 = prev;
27355 }
27356 }
27357 if (r2 == NULL)
27358 {
27359 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27360 hlinfo->mouse_face_past_end = 1;
27361 }
27362 else if (!NILP (after_string))
27363 {
27364 /* If the after-string has newlines, advance to its last row. */
27365 struct glyph_row *next;
27366 struct glyph_row *last
27367 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27368
27369 for (next = r2 + 1;
27370 next <= last
27371 && next->used[TEXT_AREA] > 0
27372 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27373 ++next)
27374 r2 = next;
27375 }
27376 /* The rest of the display engine assumes that mouse_face_beg_row is
27377 either above mouse_face_end_row or identical to it. But with
27378 bidi-reordered continued lines, the row for START_CHARPOS could
27379 be below the row for END_CHARPOS. If so, swap the rows and store
27380 them in correct order. */
27381 if (r1->y > r2->y)
27382 {
27383 struct glyph_row *tem = r2;
27384
27385 r2 = r1;
27386 r1 = tem;
27387 }
27388
27389 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27390 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27391
27392 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27393 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27394 could be anywhere in the row and in any order. The strategy
27395 below is to find the leftmost and the rightmost glyph that
27396 belongs to either of these 3 strings, or whose position is
27397 between START_CHARPOS and END_CHARPOS, and highlight all the
27398 glyphs between those two. This may cover more than just the text
27399 between START_CHARPOS and END_CHARPOS if the range of characters
27400 strides the bidi level boundary, e.g. if the beginning is in R2L
27401 text while the end is in L2R text or vice versa. */
27402 if (!r1->reversed_p)
27403 {
27404 /* This row is in a left to right paragraph. Scan it left to
27405 right. */
27406 glyph = r1->glyphs[TEXT_AREA];
27407 end = glyph + r1->used[TEXT_AREA];
27408 x = r1->x;
27409
27410 /* Skip truncation glyphs at the start of the glyph row. */
27411 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27412 for (; glyph < end
27413 && INTEGERP (glyph->object)
27414 && glyph->charpos < 0;
27415 ++glyph)
27416 x += glyph->pixel_width;
27417
27418 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27419 or DISP_STRING, and the first glyph from buffer whose
27420 position is between START_CHARPOS and END_CHARPOS. */
27421 for (; glyph < end
27422 && !INTEGERP (glyph->object)
27423 && !EQ (glyph->object, disp_string)
27424 && !(BUFFERP (glyph->object)
27425 && (glyph->charpos >= start_charpos
27426 && glyph->charpos < end_charpos));
27427 ++glyph)
27428 {
27429 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27430 are present at buffer positions between START_CHARPOS and
27431 END_CHARPOS, or if they come from an overlay. */
27432 if (EQ (glyph->object, before_string))
27433 {
27434 pos = string_buffer_position (before_string,
27435 start_charpos);
27436 /* If pos == 0, it means before_string came from an
27437 overlay, not from a buffer position. */
27438 if (!pos || (pos >= start_charpos && pos < end_charpos))
27439 break;
27440 }
27441 else if (EQ (glyph->object, after_string))
27442 {
27443 pos = string_buffer_position (after_string, end_charpos);
27444 if (!pos || (pos >= start_charpos && pos < end_charpos))
27445 break;
27446 }
27447 x += glyph->pixel_width;
27448 }
27449 hlinfo->mouse_face_beg_x = x;
27450 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27451 }
27452 else
27453 {
27454 /* This row is in a right to left paragraph. Scan it right to
27455 left. */
27456 struct glyph *g;
27457
27458 end = r1->glyphs[TEXT_AREA] - 1;
27459 glyph = end + r1->used[TEXT_AREA];
27460
27461 /* Skip truncation glyphs at the start of the glyph row. */
27462 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27463 for (; glyph > end
27464 && INTEGERP (glyph->object)
27465 && glyph->charpos < 0;
27466 --glyph)
27467 ;
27468
27469 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27470 or DISP_STRING, and the first glyph from buffer whose
27471 position is between START_CHARPOS and END_CHARPOS. */
27472 for (; glyph > end
27473 && !INTEGERP (glyph->object)
27474 && !EQ (glyph->object, disp_string)
27475 && !(BUFFERP (glyph->object)
27476 && (glyph->charpos >= start_charpos
27477 && glyph->charpos < end_charpos));
27478 --glyph)
27479 {
27480 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27481 are present at buffer positions between START_CHARPOS and
27482 END_CHARPOS, or if they come from an overlay. */
27483 if (EQ (glyph->object, before_string))
27484 {
27485 pos = string_buffer_position (before_string, start_charpos);
27486 /* If pos == 0, it means before_string came from an
27487 overlay, not from a buffer position. */
27488 if (!pos || (pos >= start_charpos && pos < end_charpos))
27489 break;
27490 }
27491 else if (EQ (glyph->object, after_string))
27492 {
27493 pos = string_buffer_position (after_string, end_charpos);
27494 if (!pos || (pos >= start_charpos && pos < end_charpos))
27495 break;
27496 }
27497 }
27498
27499 glyph++; /* first glyph to the right of the highlighted area */
27500 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27501 x += g->pixel_width;
27502 hlinfo->mouse_face_beg_x = x;
27503 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27504 }
27505
27506 /* If the highlight ends in a different row, compute GLYPH and END
27507 for the end row. Otherwise, reuse the values computed above for
27508 the row where the highlight begins. */
27509 if (r2 != r1)
27510 {
27511 if (!r2->reversed_p)
27512 {
27513 glyph = r2->glyphs[TEXT_AREA];
27514 end = glyph + r2->used[TEXT_AREA];
27515 x = r2->x;
27516 }
27517 else
27518 {
27519 end = r2->glyphs[TEXT_AREA] - 1;
27520 glyph = end + r2->used[TEXT_AREA];
27521 }
27522 }
27523
27524 if (!r2->reversed_p)
27525 {
27526 /* Skip truncation and continuation glyphs near the end of the
27527 row, and also blanks and stretch glyphs inserted by
27528 extend_face_to_end_of_line. */
27529 while (end > glyph
27530 && INTEGERP ((end - 1)->object))
27531 --end;
27532 /* Scan the rest of the glyph row from the end, looking for the
27533 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27534 DISP_STRING, or whose position is between START_CHARPOS
27535 and END_CHARPOS */
27536 for (--end;
27537 end > glyph
27538 && !INTEGERP (end->object)
27539 && !EQ (end->object, disp_string)
27540 && !(BUFFERP (end->object)
27541 && (end->charpos >= start_charpos
27542 && end->charpos < end_charpos));
27543 --end)
27544 {
27545 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27546 are present at buffer positions between START_CHARPOS and
27547 END_CHARPOS, or if they come from an overlay. */
27548 if (EQ (end->object, before_string))
27549 {
27550 pos = string_buffer_position (before_string, start_charpos);
27551 if (!pos || (pos >= start_charpos && pos < end_charpos))
27552 break;
27553 }
27554 else if (EQ (end->object, after_string))
27555 {
27556 pos = string_buffer_position (after_string, end_charpos);
27557 if (!pos || (pos >= start_charpos && pos < end_charpos))
27558 break;
27559 }
27560 }
27561 /* Find the X coordinate of the last glyph to be highlighted. */
27562 for (; glyph <= end; ++glyph)
27563 x += glyph->pixel_width;
27564
27565 hlinfo->mouse_face_end_x = x;
27566 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27567 }
27568 else
27569 {
27570 /* Skip truncation and continuation glyphs near the end of the
27571 row, and also blanks and stretch glyphs inserted by
27572 extend_face_to_end_of_line. */
27573 x = r2->x;
27574 end++;
27575 while (end < glyph
27576 && INTEGERP (end->object))
27577 {
27578 x += end->pixel_width;
27579 ++end;
27580 }
27581 /* Scan the rest of the glyph row from the end, looking for the
27582 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27583 DISP_STRING, or whose position is between START_CHARPOS
27584 and END_CHARPOS */
27585 for ( ;
27586 end < glyph
27587 && !INTEGERP (end->object)
27588 && !EQ (end->object, disp_string)
27589 && !(BUFFERP (end->object)
27590 && (end->charpos >= start_charpos
27591 && end->charpos < end_charpos));
27592 ++end)
27593 {
27594 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27595 are present at buffer positions between START_CHARPOS and
27596 END_CHARPOS, or if they come from an overlay. */
27597 if (EQ (end->object, before_string))
27598 {
27599 pos = string_buffer_position (before_string, start_charpos);
27600 if (!pos || (pos >= start_charpos && pos < end_charpos))
27601 break;
27602 }
27603 else if (EQ (end->object, after_string))
27604 {
27605 pos = string_buffer_position (after_string, end_charpos);
27606 if (!pos || (pos >= start_charpos && pos < end_charpos))
27607 break;
27608 }
27609 x += end->pixel_width;
27610 }
27611 /* If we exited the above loop because we arrived at the last
27612 glyph of the row, and its buffer position is still not in
27613 range, it means the last character in range is the preceding
27614 newline. Bump the end column and x values to get past the
27615 last glyph. */
27616 if (end == glyph
27617 && BUFFERP (end->object)
27618 && (end->charpos < start_charpos
27619 || end->charpos >= end_charpos))
27620 {
27621 x += end->pixel_width;
27622 ++end;
27623 }
27624 hlinfo->mouse_face_end_x = x;
27625 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27626 }
27627
27628 hlinfo->mouse_face_window = window;
27629 hlinfo->mouse_face_face_id
27630 = face_at_buffer_position (w, mouse_charpos, &ignore,
27631 mouse_charpos + 1,
27632 !hlinfo->mouse_face_hidden, -1);
27633 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27634 }
27635
27636 /* The following function is not used anymore (replaced with
27637 mouse_face_from_string_pos), but I leave it here for the time
27638 being, in case someone would. */
27639
27640 #if 0 /* not used */
27641
27642 /* Find the position of the glyph for position POS in OBJECT in
27643 window W's current matrix, and return in *X, *Y the pixel
27644 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27645
27646 RIGHT_P non-zero means return the position of the right edge of the
27647 glyph, RIGHT_P zero means return the left edge position.
27648
27649 If no glyph for POS exists in the matrix, return the position of
27650 the glyph with the next smaller position that is in the matrix, if
27651 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27652 exists in the matrix, return the position of the glyph with the
27653 next larger position in OBJECT.
27654
27655 Value is non-zero if a glyph was found. */
27656
27657 static int
27658 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27659 int *hpos, int *vpos, int *x, int *y, int right_p)
27660 {
27661 int yb = window_text_bottom_y (w);
27662 struct glyph_row *r;
27663 struct glyph *best_glyph = NULL;
27664 struct glyph_row *best_row = NULL;
27665 int best_x = 0;
27666
27667 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27668 r->enabled_p && r->y < yb;
27669 ++r)
27670 {
27671 struct glyph *g = r->glyphs[TEXT_AREA];
27672 struct glyph *e = g + r->used[TEXT_AREA];
27673 int gx;
27674
27675 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27676 if (EQ (g->object, object))
27677 {
27678 if (g->charpos == pos)
27679 {
27680 best_glyph = g;
27681 best_x = gx;
27682 best_row = r;
27683 goto found;
27684 }
27685 else if (best_glyph == NULL
27686 || ((eabs (g->charpos - pos)
27687 < eabs (best_glyph->charpos - pos))
27688 && (right_p
27689 ? g->charpos < pos
27690 : g->charpos > pos)))
27691 {
27692 best_glyph = g;
27693 best_x = gx;
27694 best_row = r;
27695 }
27696 }
27697 }
27698
27699 found:
27700
27701 if (best_glyph)
27702 {
27703 *x = best_x;
27704 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27705
27706 if (right_p)
27707 {
27708 *x += best_glyph->pixel_width;
27709 ++*hpos;
27710 }
27711
27712 *y = best_row->y;
27713 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27714 }
27715
27716 return best_glyph != NULL;
27717 }
27718 #endif /* not used */
27719
27720 /* Find the positions of the first and the last glyphs in window W's
27721 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
27722 (assumed to be a string), and return in HLINFO's mouse_face_*
27723 members the pixel and column/row coordinates of those glyphs. */
27724
27725 static void
27726 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27727 Lisp_Object object,
27728 ptrdiff_t startpos, ptrdiff_t endpos)
27729 {
27730 int yb = window_text_bottom_y (w);
27731 struct glyph_row *r;
27732 struct glyph *g, *e;
27733 int gx;
27734 int found = 0;
27735
27736 /* Find the glyph row with at least one position in the range
27737 [STARTPOS..ENDPOS), and the first glyph in that row whose
27738 position belongs to that range. */
27739 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27740 r->enabled_p && r->y < yb;
27741 ++r)
27742 {
27743 if (!r->reversed_p)
27744 {
27745 g = r->glyphs[TEXT_AREA];
27746 e = g + r->used[TEXT_AREA];
27747 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27748 if (EQ (g->object, object)
27749 && startpos <= g->charpos && g->charpos < endpos)
27750 {
27751 hlinfo->mouse_face_beg_row
27752 = MATRIX_ROW_VPOS (r, w->current_matrix);
27753 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27754 hlinfo->mouse_face_beg_x = gx;
27755 found = 1;
27756 break;
27757 }
27758 }
27759 else
27760 {
27761 struct glyph *g1;
27762
27763 e = r->glyphs[TEXT_AREA];
27764 g = e + r->used[TEXT_AREA];
27765 for ( ; g > e; --g)
27766 if (EQ ((g-1)->object, object)
27767 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
27768 {
27769 hlinfo->mouse_face_beg_row
27770 = MATRIX_ROW_VPOS (r, w->current_matrix);
27771 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27772 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27773 gx += g1->pixel_width;
27774 hlinfo->mouse_face_beg_x = gx;
27775 found = 1;
27776 break;
27777 }
27778 }
27779 if (found)
27780 break;
27781 }
27782
27783 if (!found)
27784 return;
27785
27786 /* Starting with the next row, look for the first row which does NOT
27787 include any glyphs whose positions are in the range. */
27788 for (++r; r->enabled_p && r->y < yb; ++r)
27789 {
27790 g = r->glyphs[TEXT_AREA];
27791 e = g + r->used[TEXT_AREA];
27792 found = 0;
27793 for ( ; g < e; ++g)
27794 if (EQ (g->object, object)
27795 && startpos <= g->charpos && g->charpos < endpos)
27796 {
27797 found = 1;
27798 break;
27799 }
27800 if (!found)
27801 break;
27802 }
27803
27804 /* The highlighted region ends on the previous row. */
27805 r--;
27806
27807 /* Set the end row. */
27808 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27809
27810 /* Compute and set the end column and the end column's horizontal
27811 pixel coordinate. */
27812 if (!r->reversed_p)
27813 {
27814 g = r->glyphs[TEXT_AREA];
27815 e = g + r->used[TEXT_AREA];
27816 for ( ; e > g; --e)
27817 if (EQ ((e-1)->object, object)
27818 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
27819 break;
27820 hlinfo->mouse_face_end_col = e - g;
27821
27822 for (gx = r->x; g < e; ++g)
27823 gx += g->pixel_width;
27824 hlinfo->mouse_face_end_x = gx;
27825 }
27826 else
27827 {
27828 e = r->glyphs[TEXT_AREA];
27829 g = e + r->used[TEXT_AREA];
27830 for (gx = r->x ; e < g; ++e)
27831 {
27832 if (EQ (e->object, object)
27833 && startpos <= e->charpos && e->charpos < endpos)
27834 break;
27835 gx += e->pixel_width;
27836 }
27837 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27838 hlinfo->mouse_face_end_x = gx;
27839 }
27840 }
27841
27842 #ifdef HAVE_WINDOW_SYSTEM
27843
27844 /* See if position X, Y is within a hot-spot of an image. */
27845
27846 static int
27847 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27848 {
27849 if (!CONSP (hot_spot))
27850 return 0;
27851
27852 if (EQ (XCAR (hot_spot), Qrect))
27853 {
27854 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27855 Lisp_Object rect = XCDR (hot_spot);
27856 Lisp_Object tem;
27857 if (!CONSP (rect))
27858 return 0;
27859 if (!CONSP (XCAR (rect)))
27860 return 0;
27861 if (!CONSP (XCDR (rect)))
27862 return 0;
27863 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27864 return 0;
27865 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27866 return 0;
27867 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27868 return 0;
27869 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27870 return 0;
27871 return 1;
27872 }
27873 else if (EQ (XCAR (hot_spot), Qcircle))
27874 {
27875 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27876 Lisp_Object circ = XCDR (hot_spot);
27877 Lisp_Object lr, lx0, ly0;
27878 if (CONSP (circ)
27879 && CONSP (XCAR (circ))
27880 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27881 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27882 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27883 {
27884 double r = XFLOATINT (lr);
27885 double dx = XINT (lx0) - x;
27886 double dy = XINT (ly0) - y;
27887 return (dx * dx + dy * dy <= r * r);
27888 }
27889 }
27890 else if (EQ (XCAR (hot_spot), Qpoly))
27891 {
27892 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27893 if (VECTORP (XCDR (hot_spot)))
27894 {
27895 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27896 Lisp_Object *poly = v->contents;
27897 ptrdiff_t n = v->header.size;
27898 ptrdiff_t i;
27899 int inside = 0;
27900 Lisp_Object lx, ly;
27901 int x0, y0;
27902
27903 /* Need an even number of coordinates, and at least 3 edges. */
27904 if (n < 6 || n & 1)
27905 return 0;
27906
27907 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27908 If count is odd, we are inside polygon. Pixels on edges
27909 may or may not be included depending on actual geometry of the
27910 polygon. */
27911 if ((lx = poly[n-2], !INTEGERP (lx))
27912 || (ly = poly[n-1], !INTEGERP (lx)))
27913 return 0;
27914 x0 = XINT (lx), y0 = XINT (ly);
27915 for (i = 0; i < n; i += 2)
27916 {
27917 int x1 = x0, y1 = y0;
27918 if ((lx = poly[i], !INTEGERP (lx))
27919 || (ly = poly[i+1], !INTEGERP (ly)))
27920 return 0;
27921 x0 = XINT (lx), y0 = XINT (ly);
27922
27923 /* Does this segment cross the X line? */
27924 if (x0 >= x)
27925 {
27926 if (x1 >= x)
27927 continue;
27928 }
27929 else if (x1 < x)
27930 continue;
27931 if (y > y0 && y > y1)
27932 continue;
27933 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27934 inside = !inside;
27935 }
27936 return inside;
27937 }
27938 }
27939 return 0;
27940 }
27941
27942 Lisp_Object
27943 find_hot_spot (Lisp_Object map, int x, int y)
27944 {
27945 while (CONSP (map))
27946 {
27947 if (CONSP (XCAR (map))
27948 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27949 return XCAR (map);
27950 map = XCDR (map);
27951 }
27952
27953 return Qnil;
27954 }
27955
27956 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27957 3, 3, 0,
27958 doc: /* Lookup in image map MAP coordinates X and Y.
27959 An image map is an alist where each element has the format (AREA ID PLIST).
27960 An AREA is specified as either a rectangle, a circle, or a polygon:
27961 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27962 pixel coordinates of the upper left and bottom right corners.
27963 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27964 and the radius of the circle; r may be a float or integer.
27965 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27966 vector describes one corner in the polygon.
27967 Returns the alist element for the first matching AREA in MAP. */)
27968 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27969 {
27970 if (NILP (map))
27971 return Qnil;
27972
27973 CHECK_NUMBER (x);
27974 CHECK_NUMBER (y);
27975
27976 return find_hot_spot (map,
27977 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27978 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27979 }
27980
27981
27982 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27983 static void
27984 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27985 {
27986 /* Do not change cursor shape while dragging mouse. */
27987 if (!NILP (do_mouse_tracking))
27988 return;
27989
27990 if (!NILP (pointer))
27991 {
27992 if (EQ (pointer, Qarrow))
27993 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27994 else if (EQ (pointer, Qhand))
27995 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27996 else if (EQ (pointer, Qtext))
27997 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27998 else if (EQ (pointer, intern ("hdrag")))
27999 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28000 else if (EQ (pointer, intern ("nhdrag")))
28001 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28002 #ifdef HAVE_X_WINDOWS
28003 else if (EQ (pointer, intern ("vdrag")))
28004 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28005 #endif
28006 else if (EQ (pointer, intern ("hourglass")))
28007 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28008 else if (EQ (pointer, Qmodeline))
28009 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28010 else
28011 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28012 }
28013
28014 if (cursor != No_Cursor)
28015 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28016 }
28017
28018 #endif /* HAVE_WINDOW_SYSTEM */
28019
28020 /* Take proper action when mouse has moved to the mode or header line
28021 or marginal area AREA of window W, x-position X and y-position Y.
28022 X is relative to the start of the text display area of W, so the
28023 width of bitmap areas and scroll bars must be subtracted to get a
28024 position relative to the start of the mode line. */
28025
28026 static void
28027 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28028 enum window_part area)
28029 {
28030 struct window *w = XWINDOW (window);
28031 struct frame *f = XFRAME (w->frame);
28032 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28033 #ifdef HAVE_WINDOW_SYSTEM
28034 Display_Info *dpyinfo;
28035 #endif
28036 Cursor cursor = No_Cursor;
28037 Lisp_Object pointer = Qnil;
28038 int dx, dy, width, height;
28039 ptrdiff_t charpos;
28040 Lisp_Object string, object = Qnil;
28041 Lisp_Object pos IF_LINT (= Qnil), help;
28042
28043 Lisp_Object mouse_face;
28044 int original_x_pixel = x;
28045 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28046 struct glyph_row *row IF_LINT (= 0);
28047
28048 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28049 {
28050 int x0;
28051 struct glyph *end;
28052
28053 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28054 returns them in row/column units! */
28055 string = mode_line_string (w, area, &x, &y, &charpos,
28056 &object, &dx, &dy, &width, &height);
28057
28058 row = (area == ON_MODE_LINE
28059 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28060 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28061
28062 /* Find the glyph under the mouse pointer. */
28063 if (row->mode_line_p && row->enabled_p)
28064 {
28065 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28066 end = glyph + row->used[TEXT_AREA];
28067
28068 for (x0 = original_x_pixel;
28069 glyph < end && x0 >= glyph->pixel_width;
28070 ++glyph)
28071 x0 -= glyph->pixel_width;
28072
28073 if (glyph >= end)
28074 glyph = NULL;
28075 }
28076 }
28077 else
28078 {
28079 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28080 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28081 returns them in row/column units! */
28082 string = marginal_area_string (w, area, &x, &y, &charpos,
28083 &object, &dx, &dy, &width, &height);
28084 }
28085
28086 help = Qnil;
28087
28088 #ifdef HAVE_WINDOW_SYSTEM
28089 if (IMAGEP (object))
28090 {
28091 Lisp_Object image_map, hotspot;
28092 if ((image_map = Fplist_get (XCDR (object), QCmap),
28093 !NILP (image_map))
28094 && (hotspot = find_hot_spot (image_map, dx, dy),
28095 CONSP (hotspot))
28096 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28097 {
28098 Lisp_Object plist;
28099
28100 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28101 If so, we could look for mouse-enter, mouse-leave
28102 properties in PLIST (and do something...). */
28103 hotspot = XCDR (hotspot);
28104 if (CONSP (hotspot)
28105 && (plist = XCAR (hotspot), CONSP (plist)))
28106 {
28107 pointer = Fplist_get (plist, Qpointer);
28108 if (NILP (pointer))
28109 pointer = Qhand;
28110 help = Fplist_get (plist, Qhelp_echo);
28111 if (!NILP (help))
28112 {
28113 help_echo_string = help;
28114 XSETWINDOW (help_echo_window, w);
28115 help_echo_object = w->contents;
28116 help_echo_pos = charpos;
28117 }
28118 }
28119 }
28120 if (NILP (pointer))
28121 pointer = Fplist_get (XCDR (object), QCpointer);
28122 }
28123 #endif /* HAVE_WINDOW_SYSTEM */
28124
28125 if (STRINGP (string))
28126 pos = make_number (charpos);
28127
28128 /* Set the help text and mouse pointer. If the mouse is on a part
28129 of the mode line without any text (e.g. past the right edge of
28130 the mode line text), use the default help text and pointer. */
28131 if (STRINGP (string) || area == ON_MODE_LINE)
28132 {
28133 /* Arrange to display the help by setting the global variables
28134 help_echo_string, help_echo_object, and help_echo_pos. */
28135 if (NILP (help))
28136 {
28137 if (STRINGP (string))
28138 help = Fget_text_property (pos, Qhelp_echo, string);
28139
28140 if (!NILP (help))
28141 {
28142 help_echo_string = help;
28143 XSETWINDOW (help_echo_window, w);
28144 help_echo_object = string;
28145 help_echo_pos = charpos;
28146 }
28147 else if (area == ON_MODE_LINE)
28148 {
28149 Lisp_Object default_help
28150 = buffer_local_value_1 (Qmode_line_default_help_echo,
28151 w->contents);
28152
28153 if (STRINGP (default_help))
28154 {
28155 help_echo_string = default_help;
28156 XSETWINDOW (help_echo_window, w);
28157 help_echo_object = Qnil;
28158 help_echo_pos = -1;
28159 }
28160 }
28161 }
28162
28163 #ifdef HAVE_WINDOW_SYSTEM
28164 /* Change the mouse pointer according to what is under it. */
28165 if (FRAME_WINDOW_P (f))
28166 {
28167 dpyinfo = FRAME_DISPLAY_INFO (f);
28168 if (STRINGP (string))
28169 {
28170 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28171
28172 if (NILP (pointer))
28173 pointer = Fget_text_property (pos, Qpointer, string);
28174
28175 /* Change the mouse pointer according to what is under X/Y. */
28176 if (NILP (pointer)
28177 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28178 {
28179 Lisp_Object map;
28180 map = Fget_text_property (pos, Qlocal_map, string);
28181 if (!KEYMAPP (map))
28182 map = Fget_text_property (pos, Qkeymap, string);
28183 if (!KEYMAPP (map))
28184 cursor = dpyinfo->vertical_scroll_bar_cursor;
28185 }
28186 }
28187 else
28188 /* Default mode-line pointer. */
28189 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28190 }
28191 #endif
28192 }
28193
28194 /* Change the mouse face according to what is under X/Y. */
28195 if (STRINGP (string))
28196 {
28197 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28198 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28199 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28200 && glyph)
28201 {
28202 Lisp_Object b, e;
28203
28204 struct glyph * tmp_glyph;
28205
28206 int gpos;
28207 int gseq_length;
28208 int total_pixel_width;
28209 ptrdiff_t begpos, endpos, ignore;
28210
28211 int vpos, hpos;
28212
28213 b = Fprevious_single_property_change (make_number (charpos + 1),
28214 Qmouse_face, string, Qnil);
28215 if (NILP (b))
28216 begpos = 0;
28217 else
28218 begpos = XINT (b);
28219
28220 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28221 if (NILP (e))
28222 endpos = SCHARS (string);
28223 else
28224 endpos = XINT (e);
28225
28226 /* Calculate the glyph position GPOS of GLYPH in the
28227 displayed string, relative to the beginning of the
28228 highlighted part of the string.
28229
28230 Note: GPOS is different from CHARPOS. CHARPOS is the
28231 position of GLYPH in the internal string object. A mode
28232 line string format has structures which are converted to
28233 a flattened string by the Emacs Lisp interpreter. The
28234 internal string is an element of those structures. The
28235 displayed string is the flattened string. */
28236 tmp_glyph = row_start_glyph;
28237 while (tmp_glyph < glyph
28238 && (!(EQ (tmp_glyph->object, glyph->object)
28239 && begpos <= tmp_glyph->charpos
28240 && tmp_glyph->charpos < endpos)))
28241 tmp_glyph++;
28242 gpos = glyph - tmp_glyph;
28243
28244 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28245 the highlighted part of the displayed string to which
28246 GLYPH belongs. Note: GSEQ_LENGTH is different from
28247 SCHARS (STRING), because the latter returns the length of
28248 the internal string. */
28249 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28250 tmp_glyph > glyph
28251 && (!(EQ (tmp_glyph->object, glyph->object)
28252 && begpos <= tmp_glyph->charpos
28253 && tmp_glyph->charpos < endpos));
28254 tmp_glyph--)
28255 ;
28256 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28257
28258 /* Calculate the total pixel width of all the glyphs between
28259 the beginning of the highlighted area and GLYPH. */
28260 total_pixel_width = 0;
28261 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28262 total_pixel_width += tmp_glyph->pixel_width;
28263
28264 /* Pre calculation of re-rendering position. Note: X is in
28265 column units here, after the call to mode_line_string or
28266 marginal_area_string. */
28267 hpos = x - gpos;
28268 vpos = (area == ON_MODE_LINE
28269 ? (w->current_matrix)->nrows - 1
28270 : 0);
28271
28272 /* If GLYPH's position is included in the region that is
28273 already drawn in mouse face, we have nothing to do. */
28274 if ( EQ (window, hlinfo->mouse_face_window)
28275 && (!row->reversed_p
28276 ? (hlinfo->mouse_face_beg_col <= hpos
28277 && hpos < hlinfo->mouse_face_end_col)
28278 /* In R2L rows we swap BEG and END, see below. */
28279 : (hlinfo->mouse_face_end_col <= hpos
28280 && hpos < hlinfo->mouse_face_beg_col))
28281 && hlinfo->mouse_face_beg_row == vpos )
28282 return;
28283
28284 if (clear_mouse_face (hlinfo))
28285 cursor = No_Cursor;
28286
28287 if (!row->reversed_p)
28288 {
28289 hlinfo->mouse_face_beg_col = hpos;
28290 hlinfo->mouse_face_beg_x = original_x_pixel
28291 - (total_pixel_width + dx);
28292 hlinfo->mouse_face_end_col = hpos + gseq_length;
28293 hlinfo->mouse_face_end_x = 0;
28294 }
28295 else
28296 {
28297 /* In R2L rows, show_mouse_face expects BEG and END
28298 coordinates to be swapped. */
28299 hlinfo->mouse_face_end_col = hpos;
28300 hlinfo->mouse_face_end_x = original_x_pixel
28301 - (total_pixel_width + dx);
28302 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28303 hlinfo->mouse_face_beg_x = 0;
28304 }
28305
28306 hlinfo->mouse_face_beg_row = vpos;
28307 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28308 hlinfo->mouse_face_past_end = 0;
28309 hlinfo->mouse_face_window = window;
28310
28311 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28312 charpos,
28313 0, &ignore,
28314 glyph->face_id,
28315 1);
28316 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28317
28318 if (NILP (pointer))
28319 pointer = Qhand;
28320 }
28321 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28322 clear_mouse_face (hlinfo);
28323 }
28324 #ifdef HAVE_WINDOW_SYSTEM
28325 if (FRAME_WINDOW_P (f))
28326 define_frame_cursor1 (f, cursor, pointer);
28327 #endif
28328 }
28329
28330
28331 /* EXPORT:
28332 Take proper action when the mouse has moved to position X, Y on
28333 frame F with regards to highlighting portions of display that have
28334 mouse-face properties. Also de-highlight portions of display where
28335 the mouse was before, set the mouse pointer shape as appropriate
28336 for the mouse coordinates, and activate help echo (tooltips).
28337 X and Y can be negative or out of range. */
28338
28339 void
28340 note_mouse_highlight (struct frame *f, int x, int y)
28341 {
28342 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28343 enum window_part part = ON_NOTHING;
28344 Lisp_Object window;
28345 struct window *w;
28346 Cursor cursor = No_Cursor;
28347 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28348 struct buffer *b;
28349
28350 /* When a menu is active, don't highlight because this looks odd. */
28351 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28352 if (popup_activated ())
28353 return;
28354 #endif
28355
28356 if (!f->glyphs_initialized_p
28357 || f->pointer_invisible)
28358 return;
28359
28360 hlinfo->mouse_face_mouse_x = x;
28361 hlinfo->mouse_face_mouse_y = y;
28362 hlinfo->mouse_face_mouse_frame = f;
28363
28364 if (hlinfo->mouse_face_defer)
28365 return;
28366
28367 /* Which window is that in? */
28368 window = window_from_coordinates (f, x, y, &part, 1);
28369
28370 /* If displaying active text in another window, clear that. */
28371 if (! EQ (window, hlinfo->mouse_face_window)
28372 /* Also clear if we move out of text area in same window. */
28373 || (!NILP (hlinfo->mouse_face_window)
28374 && !NILP (window)
28375 && part != ON_TEXT
28376 && part != ON_MODE_LINE
28377 && part != ON_HEADER_LINE))
28378 clear_mouse_face (hlinfo);
28379
28380 /* Not on a window -> return. */
28381 if (!WINDOWP (window))
28382 return;
28383
28384 /* Reset help_echo_string. It will get recomputed below. */
28385 help_echo_string = Qnil;
28386
28387 /* Convert to window-relative pixel coordinates. */
28388 w = XWINDOW (window);
28389 frame_to_window_pixel_xy (w, &x, &y);
28390
28391 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28392 /* Handle tool-bar window differently since it doesn't display a
28393 buffer. */
28394 if (EQ (window, f->tool_bar_window))
28395 {
28396 note_tool_bar_highlight (f, x, y);
28397 return;
28398 }
28399 #endif
28400
28401 /* Mouse is on the mode, header line or margin? */
28402 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28403 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28404 {
28405 note_mode_line_or_margin_highlight (window, x, y, part);
28406 return;
28407 }
28408
28409 #ifdef HAVE_WINDOW_SYSTEM
28410 if (part == ON_VERTICAL_BORDER)
28411 {
28412 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28413 help_echo_string = build_string ("drag-mouse-1: resize");
28414 }
28415 else if (part == ON_RIGHT_DIVIDER)
28416 {
28417 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28418 help_echo_string = build_string ("drag-mouse-1: resize");
28419 }
28420 else if (part == ON_BOTTOM_DIVIDER)
28421 {
28422 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28423 help_echo_string = build_string ("drag-mouse-1: resize");
28424 }
28425 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28426 || part == ON_SCROLL_BAR)
28427 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28428 else
28429 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28430 #endif
28431
28432 /* Are we in a window whose display is up to date?
28433 And verify the buffer's text has not changed. */
28434 b = XBUFFER (w->contents);
28435 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28436 {
28437 int hpos, vpos, dx, dy, area = LAST_AREA;
28438 ptrdiff_t pos;
28439 struct glyph *glyph;
28440 Lisp_Object object;
28441 Lisp_Object mouse_face = Qnil, position;
28442 Lisp_Object *overlay_vec = NULL;
28443 ptrdiff_t i, noverlays;
28444 struct buffer *obuf;
28445 ptrdiff_t obegv, ozv;
28446 int same_region;
28447
28448 /* Find the glyph under X/Y. */
28449 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28450
28451 #ifdef HAVE_WINDOW_SYSTEM
28452 /* Look for :pointer property on image. */
28453 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28454 {
28455 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28456 if (img != NULL && IMAGEP (img->spec))
28457 {
28458 Lisp_Object image_map, hotspot;
28459 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28460 !NILP (image_map))
28461 && (hotspot = find_hot_spot (image_map,
28462 glyph->slice.img.x + dx,
28463 glyph->slice.img.y + dy),
28464 CONSP (hotspot))
28465 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28466 {
28467 Lisp_Object plist;
28468
28469 /* Could check XCAR (hotspot) to see if we enter/leave
28470 this hot-spot.
28471 If so, we could look for mouse-enter, mouse-leave
28472 properties in PLIST (and do something...). */
28473 hotspot = XCDR (hotspot);
28474 if (CONSP (hotspot)
28475 && (plist = XCAR (hotspot), CONSP (plist)))
28476 {
28477 pointer = Fplist_get (plist, Qpointer);
28478 if (NILP (pointer))
28479 pointer = Qhand;
28480 help_echo_string = Fplist_get (plist, Qhelp_echo);
28481 if (!NILP (help_echo_string))
28482 {
28483 help_echo_window = window;
28484 help_echo_object = glyph->object;
28485 help_echo_pos = glyph->charpos;
28486 }
28487 }
28488 }
28489 if (NILP (pointer))
28490 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28491 }
28492 }
28493 #endif /* HAVE_WINDOW_SYSTEM */
28494
28495 /* Clear mouse face if X/Y not over text. */
28496 if (glyph == NULL
28497 || area != TEXT_AREA
28498 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28499 /* Glyph's OBJECT is an integer for glyphs inserted by the
28500 display engine for its internal purposes, like truncation
28501 and continuation glyphs and blanks beyond the end of
28502 line's text on text terminals. If we are over such a
28503 glyph, we are not over any text. */
28504 || INTEGERP (glyph->object)
28505 /* R2L rows have a stretch glyph at their front, which
28506 stands for no text, whereas L2R rows have no glyphs at
28507 all beyond the end of text. Treat such stretch glyphs
28508 like we do with NULL glyphs in L2R rows. */
28509 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28510 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28511 && glyph->type == STRETCH_GLYPH
28512 && glyph->avoid_cursor_p))
28513 {
28514 if (clear_mouse_face (hlinfo))
28515 cursor = No_Cursor;
28516 #ifdef HAVE_WINDOW_SYSTEM
28517 if (FRAME_WINDOW_P (f) && NILP (pointer))
28518 {
28519 if (area != TEXT_AREA)
28520 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28521 else
28522 pointer = Vvoid_text_area_pointer;
28523 }
28524 #endif
28525 goto set_cursor;
28526 }
28527
28528 pos = glyph->charpos;
28529 object = glyph->object;
28530 if (!STRINGP (object) && !BUFFERP (object))
28531 goto set_cursor;
28532
28533 /* If we get an out-of-range value, return now; avoid an error. */
28534 if (BUFFERP (object) && pos > BUF_Z (b))
28535 goto set_cursor;
28536
28537 /* Make the window's buffer temporarily current for
28538 overlays_at and compute_char_face. */
28539 obuf = current_buffer;
28540 current_buffer = b;
28541 obegv = BEGV;
28542 ozv = ZV;
28543 BEGV = BEG;
28544 ZV = Z;
28545
28546 /* Is this char mouse-active or does it have help-echo? */
28547 position = make_number (pos);
28548
28549 if (BUFFERP (object))
28550 {
28551 /* Put all the overlays we want in a vector in overlay_vec. */
28552 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28553 /* Sort overlays into increasing priority order. */
28554 noverlays = sort_overlays (overlay_vec, noverlays, w);
28555 }
28556 else
28557 noverlays = 0;
28558
28559 if (NILP (Vmouse_highlight))
28560 {
28561 clear_mouse_face (hlinfo);
28562 goto check_help_echo;
28563 }
28564
28565 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28566
28567 if (same_region)
28568 cursor = No_Cursor;
28569
28570 /* Check mouse-face highlighting. */
28571 if (! same_region
28572 /* If there exists an overlay with mouse-face overlapping
28573 the one we are currently highlighting, we have to
28574 check if we enter the overlapping overlay, and then
28575 highlight only that. */
28576 || (OVERLAYP (hlinfo->mouse_face_overlay)
28577 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28578 {
28579 /* Find the highest priority overlay with a mouse-face. */
28580 Lisp_Object overlay = Qnil;
28581 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28582 {
28583 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28584 if (!NILP (mouse_face))
28585 overlay = overlay_vec[i];
28586 }
28587
28588 /* If we're highlighting the same overlay as before, there's
28589 no need to do that again. */
28590 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28591 goto check_help_echo;
28592 hlinfo->mouse_face_overlay = overlay;
28593
28594 /* Clear the display of the old active region, if any. */
28595 if (clear_mouse_face (hlinfo))
28596 cursor = No_Cursor;
28597
28598 /* If no overlay applies, get a text property. */
28599 if (NILP (overlay))
28600 mouse_face = Fget_text_property (position, Qmouse_face, object);
28601
28602 /* Next, compute the bounds of the mouse highlighting and
28603 display it. */
28604 if (!NILP (mouse_face) && STRINGP (object))
28605 {
28606 /* The mouse-highlighting comes from a display string
28607 with a mouse-face. */
28608 Lisp_Object s, e;
28609 ptrdiff_t ignore;
28610
28611 s = Fprevious_single_property_change
28612 (make_number (pos + 1), Qmouse_face, object, Qnil);
28613 e = Fnext_single_property_change
28614 (position, Qmouse_face, object, Qnil);
28615 if (NILP (s))
28616 s = make_number (0);
28617 if (NILP (e))
28618 e = make_number (SCHARS (object));
28619 mouse_face_from_string_pos (w, hlinfo, object,
28620 XINT (s), XINT (e));
28621 hlinfo->mouse_face_past_end = 0;
28622 hlinfo->mouse_face_window = window;
28623 hlinfo->mouse_face_face_id
28624 = face_at_string_position (w, object, pos, 0, &ignore,
28625 glyph->face_id, 1);
28626 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28627 cursor = No_Cursor;
28628 }
28629 else
28630 {
28631 /* The mouse-highlighting, if any, comes from an overlay
28632 or text property in the buffer. */
28633 Lisp_Object buffer IF_LINT (= Qnil);
28634 Lisp_Object disp_string IF_LINT (= Qnil);
28635
28636 if (STRINGP (object))
28637 {
28638 /* If we are on a display string with no mouse-face,
28639 check if the text under it has one. */
28640 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28641 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28642 pos = string_buffer_position (object, start);
28643 if (pos > 0)
28644 {
28645 mouse_face = get_char_property_and_overlay
28646 (make_number (pos), Qmouse_face, w->contents, &overlay);
28647 buffer = w->contents;
28648 disp_string = object;
28649 }
28650 }
28651 else
28652 {
28653 buffer = object;
28654 disp_string = Qnil;
28655 }
28656
28657 if (!NILP (mouse_face))
28658 {
28659 Lisp_Object before, after;
28660 Lisp_Object before_string, after_string;
28661 /* To correctly find the limits of mouse highlight
28662 in a bidi-reordered buffer, we must not use the
28663 optimization of limiting the search in
28664 previous-single-property-change and
28665 next-single-property-change, because
28666 rows_from_pos_range needs the real start and end
28667 positions to DTRT in this case. That's because
28668 the first row visible in a window does not
28669 necessarily display the character whose position
28670 is the smallest. */
28671 Lisp_Object lim1
28672 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28673 ? Fmarker_position (w->start)
28674 : Qnil;
28675 Lisp_Object lim2
28676 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28677 ? make_number (BUF_Z (XBUFFER (buffer))
28678 - w->window_end_pos)
28679 : Qnil;
28680
28681 if (NILP (overlay))
28682 {
28683 /* Handle the text property case. */
28684 before = Fprevious_single_property_change
28685 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28686 after = Fnext_single_property_change
28687 (make_number (pos), Qmouse_face, buffer, lim2);
28688 before_string = after_string = Qnil;
28689 }
28690 else
28691 {
28692 /* Handle the overlay case. */
28693 before = Foverlay_start (overlay);
28694 after = Foverlay_end (overlay);
28695 before_string = Foverlay_get (overlay, Qbefore_string);
28696 after_string = Foverlay_get (overlay, Qafter_string);
28697
28698 if (!STRINGP (before_string)) before_string = Qnil;
28699 if (!STRINGP (after_string)) after_string = Qnil;
28700 }
28701
28702 mouse_face_from_buffer_pos (window, hlinfo, pos,
28703 NILP (before)
28704 ? 1
28705 : XFASTINT (before),
28706 NILP (after)
28707 ? BUF_Z (XBUFFER (buffer))
28708 : XFASTINT (after),
28709 before_string, after_string,
28710 disp_string);
28711 cursor = No_Cursor;
28712 }
28713 }
28714 }
28715
28716 check_help_echo:
28717
28718 /* Look for a `help-echo' property. */
28719 if (NILP (help_echo_string)) {
28720 Lisp_Object help, overlay;
28721
28722 /* Check overlays first. */
28723 help = overlay = Qnil;
28724 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28725 {
28726 overlay = overlay_vec[i];
28727 help = Foverlay_get (overlay, Qhelp_echo);
28728 }
28729
28730 if (!NILP (help))
28731 {
28732 help_echo_string = help;
28733 help_echo_window = window;
28734 help_echo_object = overlay;
28735 help_echo_pos = pos;
28736 }
28737 else
28738 {
28739 Lisp_Object obj = glyph->object;
28740 ptrdiff_t charpos = glyph->charpos;
28741
28742 /* Try text properties. */
28743 if (STRINGP (obj)
28744 && charpos >= 0
28745 && charpos < SCHARS (obj))
28746 {
28747 help = Fget_text_property (make_number (charpos),
28748 Qhelp_echo, obj);
28749 if (NILP (help))
28750 {
28751 /* If the string itself doesn't specify a help-echo,
28752 see if the buffer text ``under'' it does. */
28753 struct glyph_row *r
28754 = MATRIX_ROW (w->current_matrix, vpos);
28755 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28756 ptrdiff_t p = string_buffer_position (obj, start);
28757 if (p > 0)
28758 {
28759 help = Fget_char_property (make_number (p),
28760 Qhelp_echo, w->contents);
28761 if (!NILP (help))
28762 {
28763 charpos = p;
28764 obj = w->contents;
28765 }
28766 }
28767 }
28768 }
28769 else if (BUFFERP (obj)
28770 && charpos >= BEGV
28771 && charpos < ZV)
28772 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28773 obj);
28774
28775 if (!NILP (help))
28776 {
28777 help_echo_string = help;
28778 help_echo_window = window;
28779 help_echo_object = obj;
28780 help_echo_pos = charpos;
28781 }
28782 }
28783 }
28784
28785 #ifdef HAVE_WINDOW_SYSTEM
28786 /* Look for a `pointer' property. */
28787 if (FRAME_WINDOW_P (f) && NILP (pointer))
28788 {
28789 /* Check overlays first. */
28790 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28791 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28792
28793 if (NILP (pointer))
28794 {
28795 Lisp_Object obj = glyph->object;
28796 ptrdiff_t charpos = glyph->charpos;
28797
28798 /* Try text properties. */
28799 if (STRINGP (obj)
28800 && charpos >= 0
28801 && charpos < SCHARS (obj))
28802 {
28803 pointer = Fget_text_property (make_number (charpos),
28804 Qpointer, obj);
28805 if (NILP (pointer))
28806 {
28807 /* If the string itself doesn't specify a pointer,
28808 see if the buffer text ``under'' it does. */
28809 struct glyph_row *r
28810 = MATRIX_ROW (w->current_matrix, vpos);
28811 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28812 ptrdiff_t p = string_buffer_position (obj, start);
28813 if (p > 0)
28814 pointer = Fget_char_property (make_number (p),
28815 Qpointer, w->contents);
28816 }
28817 }
28818 else if (BUFFERP (obj)
28819 && charpos >= BEGV
28820 && charpos < ZV)
28821 pointer = Fget_text_property (make_number (charpos),
28822 Qpointer, obj);
28823 }
28824 }
28825 #endif /* HAVE_WINDOW_SYSTEM */
28826
28827 BEGV = obegv;
28828 ZV = ozv;
28829 current_buffer = obuf;
28830 }
28831
28832 set_cursor:
28833
28834 #ifdef HAVE_WINDOW_SYSTEM
28835 if (FRAME_WINDOW_P (f))
28836 define_frame_cursor1 (f, cursor, pointer);
28837 #else
28838 /* This is here to prevent a compiler error, about "label at end of
28839 compound statement". */
28840 return;
28841 #endif
28842 }
28843
28844
28845 /* EXPORT for RIF:
28846 Clear any mouse-face on window W. This function is part of the
28847 redisplay interface, and is called from try_window_id and similar
28848 functions to ensure the mouse-highlight is off. */
28849
28850 void
28851 x_clear_window_mouse_face (struct window *w)
28852 {
28853 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28854 Lisp_Object window;
28855
28856 block_input ();
28857 XSETWINDOW (window, w);
28858 if (EQ (window, hlinfo->mouse_face_window))
28859 clear_mouse_face (hlinfo);
28860 unblock_input ();
28861 }
28862
28863
28864 /* EXPORT:
28865 Just discard the mouse face information for frame F, if any.
28866 This is used when the size of F is changed. */
28867
28868 void
28869 cancel_mouse_face (struct frame *f)
28870 {
28871 Lisp_Object window;
28872 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28873
28874 window = hlinfo->mouse_face_window;
28875 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28876 reset_mouse_highlight (hlinfo);
28877 }
28878
28879
28880 \f
28881 /***********************************************************************
28882 Exposure Events
28883 ***********************************************************************/
28884
28885 #ifdef HAVE_WINDOW_SYSTEM
28886
28887 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28888 which intersects rectangle R. R is in window-relative coordinates. */
28889
28890 static void
28891 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28892 enum glyph_row_area area)
28893 {
28894 struct glyph *first = row->glyphs[area];
28895 struct glyph *end = row->glyphs[area] + row->used[area];
28896 struct glyph *last;
28897 int first_x, start_x, x;
28898
28899 if (area == TEXT_AREA && row->fill_line_p)
28900 /* If row extends face to end of line write the whole line. */
28901 draw_glyphs (w, 0, row, area,
28902 0, row->used[area],
28903 DRAW_NORMAL_TEXT, 0);
28904 else
28905 {
28906 /* Set START_X to the window-relative start position for drawing glyphs of
28907 AREA. The first glyph of the text area can be partially visible.
28908 The first glyphs of other areas cannot. */
28909 start_x = window_box_left_offset (w, area);
28910 x = start_x;
28911 if (area == TEXT_AREA)
28912 x += row->x;
28913
28914 /* Find the first glyph that must be redrawn. */
28915 while (first < end
28916 && x + first->pixel_width < r->x)
28917 {
28918 x += first->pixel_width;
28919 ++first;
28920 }
28921
28922 /* Find the last one. */
28923 last = first;
28924 first_x = x;
28925 while (last < end
28926 && x < r->x + r->width)
28927 {
28928 x += last->pixel_width;
28929 ++last;
28930 }
28931
28932 /* Repaint. */
28933 if (last > first)
28934 draw_glyphs (w, first_x - start_x, row, area,
28935 first - row->glyphs[area], last - row->glyphs[area],
28936 DRAW_NORMAL_TEXT, 0);
28937 }
28938 }
28939
28940
28941 /* Redraw the parts of the glyph row ROW on window W intersecting
28942 rectangle R. R is in window-relative coordinates. Value is
28943 non-zero if mouse-face was overwritten. */
28944
28945 static int
28946 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28947 {
28948 eassert (row->enabled_p);
28949
28950 if (row->mode_line_p || w->pseudo_window_p)
28951 draw_glyphs (w, 0, row, TEXT_AREA,
28952 0, row->used[TEXT_AREA],
28953 DRAW_NORMAL_TEXT, 0);
28954 else
28955 {
28956 if (row->used[LEFT_MARGIN_AREA])
28957 expose_area (w, row, r, LEFT_MARGIN_AREA);
28958 if (row->used[TEXT_AREA])
28959 expose_area (w, row, r, TEXT_AREA);
28960 if (row->used[RIGHT_MARGIN_AREA])
28961 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28962 draw_row_fringe_bitmaps (w, row);
28963 }
28964
28965 return row->mouse_face_p;
28966 }
28967
28968
28969 /* Redraw those parts of glyphs rows during expose event handling that
28970 overlap other rows. Redrawing of an exposed line writes over parts
28971 of lines overlapping that exposed line; this function fixes that.
28972
28973 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28974 row in W's current matrix that is exposed and overlaps other rows.
28975 LAST_OVERLAPPING_ROW is the last such row. */
28976
28977 static void
28978 expose_overlaps (struct window *w,
28979 struct glyph_row *first_overlapping_row,
28980 struct glyph_row *last_overlapping_row,
28981 XRectangle *r)
28982 {
28983 struct glyph_row *row;
28984
28985 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28986 if (row->overlapping_p)
28987 {
28988 eassert (row->enabled_p && !row->mode_line_p);
28989
28990 row->clip = r;
28991 if (row->used[LEFT_MARGIN_AREA])
28992 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28993
28994 if (row->used[TEXT_AREA])
28995 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28996
28997 if (row->used[RIGHT_MARGIN_AREA])
28998 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28999 row->clip = NULL;
29000 }
29001 }
29002
29003
29004 /* Return non-zero if W's cursor intersects rectangle R. */
29005
29006 static int
29007 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29008 {
29009 XRectangle cr, result;
29010 struct glyph *cursor_glyph;
29011 struct glyph_row *row;
29012
29013 if (w->phys_cursor.vpos >= 0
29014 && w->phys_cursor.vpos < w->current_matrix->nrows
29015 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29016 row->enabled_p)
29017 && row->cursor_in_fringe_p)
29018 {
29019 /* Cursor is in the fringe. */
29020 cr.x = window_box_right_offset (w,
29021 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29022 ? RIGHT_MARGIN_AREA
29023 : TEXT_AREA));
29024 cr.y = row->y;
29025 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29026 cr.height = row->height;
29027 return x_intersect_rectangles (&cr, r, &result);
29028 }
29029
29030 cursor_glyph = get_phys_cursor_glyph (w);
29031 if (cursor_glyph)
29032 {
29033 /* r is relative to W's box, but w->phys_cursor.x is relative
29034 to left edge of W's TEXT area. Adjust it. */
29035 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29036 cr.y = w->phys_cursor.y;
29037 cr.width = cursor_glyph->pixel_width;
29038 cr.height = w->phys_cursor_height;
29039 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29040 I assume the effect is the same -- and this is portable. */
29041 return x_intersect_rectangles (&cr, r, &result);
29042 }
29043 /* If we don't understand the format, pretend we're not in the hot-spot. */
29044 return 0;
29045 }
29046
29047
29048 /* EXPORT:
29049 Draw a vertical window border to the right of window W if W doesn't
29050 have vertical scroll bars. */
29051
29052 void
29053 x_draw_vertical_border (struct window *w)
29054 {
29055 struct frame *f = XFRAME (WINDOW_FRAME (w));
29056
29057 /* We could do better, if we knew what type of scroll-bar the adjacent
29058 windows (on either side) have... But we don't :-(
29059 However, I think this works ok. ++KFS 2003-04-25 */
29060
29061 /* Redraw borders between horizontally adjacent windows. Don't
29062 do it for frames with vertical scroll bars because either the
29063 right scroll bar of a window, or the left scroll bar of its
29064 neighbor will suffice as a border. */
29065 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29066 return;
29067
29068 /* Note: It is necessary to redraw both the left and the right
29069 borders, for when only this single window W is being
29070 redisplayed. */
29071 if (!WINDOW_RIGHTMOST_P (w)
29072 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29073 {
29074 int x0, x1, y0, y1;
29075
29076 window_box_edges (w, &x0, &y0, &x1, &y1);
29077 y1 -= 1;
29078
29079 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29080 x1 -= 1;
29081
29082 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29083 }
29084
29085 if (!WINDOW_LEFTMOST_P (w)
29086 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29087 {
29088 int x0, x1, y0, y1;
29089
29090 window_box_edges (w, &x0, &y0, &x1, &y1);
29091 y1 -= 1;
29092
29093 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29094 x0 -= 1;
29095
29096 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29097 }
29098 }
29099
29100
29101 /* Draw window dividers for window W. */
29102
29103 void
29104 x_draw_right_divider (struct window *w)
29105 {
29106 struct frame *f = WINDOW_XFRAME (w);
29107
29108 if (w->mini || w->pseudo_window_p)
29109 return;
29110 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29111 {
29112 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29113 int x1 = WINDOW_RIGHT_EDGE_X (w);
29114 int y0 = WINDOW_TOP_EDGE_Y (w);
29115 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29116
29117 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29118 }
29119 }
29120
29121 void
29122 x_draw_bottom_divider (struct window *w)
29123 {
29124 struct frame *f = XFRAME (WINDOW_FRAME (w));
29125
29126 if (w->mini || w->pseudo_window_p)
29127 return;
29128 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29129 {
29130 int x0 = WINDOW_LEFT_EDGE_X (w);
29131 int x1 = WINDOW_RIGHT_EDGE_X (w);
29132 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29133 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29134
29135 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29136 }
29137 }
29138
29139 /* Redraw the part of window W intersection rectangle FR. Pixel
29140 coordinates in FR are frame-relative. Call this function with
29141 input blocked. Value is non-zero if the exposure overwrites
29142 mouse-face. */
29143
29144 static int
29145 expose_window (struct window *w, XRectangle *fr)
29146 {
29147 struct frame *f = XFRAME (w->frame);
29148 XRectangle wr, r;
29149 int mouse_face_overwritten_p = 0;
29150
29151 /* If window is not yet fully initialized, do nothing. This can
29152 happen when toolkit scroll bars are used and a window is split.
29153 Reconfiguring the scroll bar will generate an expose for a newly
29154 created window. */
29155 if (w->current_matrix == NULL)
29156 return 0;
29157
29158 /* When we're currently updating the window, display and current
29159 matrix usually don't agree. Arrange for a thorough display
29160 later. */
29161 if (w->must_be_updated_p)
29162 {
29163 SET_FRAME_GARBAGED (f);
29164 return 0;
29165 }
29166
29167 /* Frame-relative pixel rectangle of W. */
29168 wr.x = WINDOW_LEFT_EDGE_X (w);
29169 wr.y = WINDOW_TOP_EDGE_Y (w);
29170 wr.width = WINDOW_PIXEL_WIDTH (w);
29171 wr.height = WINDOW_PIXEL_HEIGHT (w);
29172
29173 if (x_intersect_rectangles (fr, &wr, &r))
29174 {
29175 int yb = window_text_bottom_y (w);
29176 struct glyph_row *row;
29177 int cursor_cleared_p, phys_cursor_on_p;
29178 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29179
29180 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29181 r.x, r.y, r.width, r.height));
29182
29183 /* Convert to window coordinates. */
29184 r.x -= WINDOW_LEFT_EDGE_X (w);
29185 r.y -= WINDOW_TOP_EDGE_Y (w);
29186
29187 /* Turn off the cursor. */
29188 if (!w->pseudo_window_p
29189 && phys_cursor_in_rect_p (w, &r))
29190 {
29191 x_clear_cursor (w);
29192 cursor_cleared_p = 1;
29193 }
29194 else
29195 cursor_cleared_p = 0;
29196
29197 /* If the row containing the cursor extends face to end of line,
29198 then expose_area might overwrite the cursor outside the
29199 rectangle and thus notice_overwritten_cursor might clear
29200 w->phys_cursor_on_p. We remember the original value and
29201 check later if it is changed. */
29202 phys_cursor_on_p = w->phys_cursor_on_p;
29203
29204 /* Update lines intersecting rectangle R. */
29205 first_overlapping_row = last_overlapping_row = NULL;
29206 for (row = w->current_matrix->rows;
29207 row->enabled_p;
29208 ++row)
29209 {
29210 int y0 = row->y;
29211 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29212
29213 if ((y0 >= r.y && y0 < r.y + r.height)
29214 || (y1 > r.y && y1 < r.y + r.height)
29215 || (r.y >= y0 && r.y < y1)
29216 || (r.y + r.height > y0 && r.y + r.height < y1))
29217 {
29218 /* A header line may be overlapping, but there is no need
29219 to fix overlapping areas for them. KFS 2005-02-12 */
29220 if (row->overlapping_p && !row->mode_line_p)
29221 {
29222 if (first_overlapping_row == NULL)
29223 first_overlapping_row = row;
29224 last_overlapping_row = row;
29225 }
29226
29227 row->clip = fr;
29228 if (expose_line (w, row, &r))
29229 mouse_face_overwritten_p = 1;
29230 row->clip = NULL;
29231 }
29232 else if (row->overlapping_p)
29233 {
29234 /* We must redraw a row overlapping the exposed area. */
29235 if (y0 < r.y
29236 ? y0 + row->phys_height > r.y
29237 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29238 {
29239 if (first_overlapping_row == NULL)
29240 first_overlapping_row = row;
29241 last_overlapping_row = row;
29242 }
29243 }
29244
29245 if (y1 >= yb)
29246 break;
29247 }
29248
29249 /* Display the mode line if there is one. */
29250 if (WINDOW_WANTS_MODELINE_P (w)
29251 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29252 row->enabled_p)
29253 && row->y < r.y + r.height)
29254 {
29255 if (expose_line (w, row, &r))
29256 mouse_face_overwritten_p = 1;
29257 }
29258
29259 if (!w->pseudo_window_p)
29260 {
29261 /* Fix the display of overlapping rows. */
29262 if (first_overlapping_row)
29263 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29264 fr);
29265
29266 /* Draw border between windows. */
29267 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29268 x_draw_right_divider (w);
29269 else
29270 x_draw_vertical_border (w);
29271
29272 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29273 x_draw_bottom_divider (w);
29274
29275 /* Turn the cursor on again. */
29276 if (cursor_cleared_p
29277 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29278 update_window_cursor (w, 1);
29279 }
29280 }
29281
29282 return mouse_face_overwritten_p;
29283 }
29284
29285
29286
29287 /* Redraw (parts) of all windows in the window tree rooted at W that
29288 intersect R. R contains frame pixel coordinates. Value is
29289 non-zero if the exposure overwrites mouse-face. */
29290
29291 static int
29292 expose_window_tree (struct window *w, XRectangle *r)
29293 {
29294 struct frame *f = XFRAME (w->frame);
29295 int mouse_face_overwritten_p = 0;
29296
29297 while (w && !FRAME_GARBAGED_P (f))
29298 {
29299 if (WINDOWP (w->contents))
29300 mouse_face_overwritten_p
29301 |= expose_window_tree (XWINDOW (w->contents), r);
29302 else
29303 mouse_face_overwritten_p |= expose_window (w, r);
29304
29305 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29306 }
29307
29308 return mouse_face_overwritten_p;
29309 }
29310
29311
29312 /* EXPORT:
29313 Redisplay an exposed area of frame F. X and Y are the upper-left
29314 corner of the exposed rectangle. W and H are width and height of
29315 the exposed area. All are pixel values. W or H zero means redraw
29316 the entire frame. */
29317
29318 void
29319 expose_frame (struct frame *f, int x, int y, int w, int h)
29320 {
29321 XRectangle r;
29322 int mouse_face_overwritten_p = 0;
29323
29324 TRACE ((stderr, "expose_frame "));
29325
29326 /* No need to redraw if frame will be redrawn soon. */
29327 if (FRAME_GARBAGED_P (f))
29328 {
29329 TRACE ((stderr, " garbaged\n"));
29330 return;
29331 }
29332
29333 /* If basic faces haven't been realized yet, there is no point in
29334 trying to redraw anything. This can happen when we get an expose
29335 event while Emacs is starting, e.g. by moving another window. */
29336 if (FRAME_FACE_CACHE (f) == NULL
29337 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29338 {
29339 TRACE ((stderr, " no faces\n"));
29340 return;
29341 }
29342
29343 if (w == 0 || h == 0)
29344 {
29345 r.x = r.y = 0;
29346 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29347 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29348 }
29349 else
29350 {
29351 r.x = x;
29352 r.y = y;
29353 r.width = w;
29354 r.height = h;
29355 }
29356
29357 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29358 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29359
29360 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29361 if (WINDOWP (f->tool_bar_window))
29362 mouse_face_overwritten_p
29363 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29364 #endif
29365
29366 #ifdef HAVE_X_WINDOWS
29367 #ifndef MSDOS
29368 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29369 if (WINDOWP (f->menu_bar_window))
29370 mouse_face_overwritten_p
29371 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29372 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29373 #endif
29374 #endif
29375
29376 /* Some window managers support a focus-follows-mouse style with
29377 delayed raising of frames. Imagine a partially obscured frame,
29378 and moving the mouse into partially obscured mouse-face on that
29379 frame. The visible part of the mouse-face will be highlighted,
29380 then the WM raises the obscured frame. With at least one WM, KDE
29381 2.1, Emacs is not getting any event for the raising of the frame
29382 (even tried with SubstructureRedirectMask), only Expose events.
29383 These expose events will draw text normally, i.e. not
29384 highlighted. Which means we must redo the highlight here.
29385 Subsume it under ``we love X''. --gerd 2001-08-15 */
29386 /* Included in Windows version because Windows most likely does not
29387 do the right thing if any third party tool offers
29388 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29389 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29390 {
29391 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29392 if (f == hlinfo->mouse_face_mouse_frame)
29393 {
29394 int mouse_x = hlinfo->mouse_face_mouse_x;
29395 int mouse_y = hlinfo->mouse_face_mouse_y;
29396 clear_mouse_face (hlinfo);
29397 note_mouse_highlight (f, mouse_x, mouse_y);
29398 }
29399 }
29400 }
29401
29402
29403 /* EXPORT:
29404 Determine the intersection of two rectangles R1 and R2. Return
29405 the intersection in *RESULT. Value is non-zero if RESULT is not
29406 empty. */
29407
29408 int
29409 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29410 {
29411 XRectangle *left, *right;
29412 XRectangle *upper, *lower;
29413 int intersection_p = 0;
29414
29415 /* Rearrange so that R1 is the left-most rectangle. */
29416 if (r1->x < r2->x)
29417 left = r1, right = r2;
29418 else
29419 left = r2, right = r1;
29420
29421 /* X0 of the intersection is right.x0, if this is inside R1,
29422 otherwise there is no intersection. */
29423 if (right->x <= left->x + left->width)
29424 {
29425 result->x = right->x;
29426
29427 /* The right end of the intersection is the minimum of
29428 the right ends of left and right. */
29429 result->width = (min (left->x + left->width, right->x + right->width)
29430 - result->x);
29431
29432 /* Same game for Y. */
29433 if (r1->y < r2->y)
29434 upper = r1, lower = r2;
29435 else
29436 upper = r2, lower = r1;
29437
29438 /* The upper end of the intersection is lower.y0, if this is inside
29439 of upper. Otherwise, there is no intersection. */
29440 if (lower->y <= upper->y + upper->height)
29441 {
29442 result->y = lower->y;
29443
29444 /* The lower end of the intersection is the minimum of the lower
29445 ends of upper and lower. */
29446 result->height = (min (lower->y + lower->height,
29447 upper->y + upper->height)
29448 - result->y);
29449 intersection_p = 1;
29450 }
29451 }
29452
29453 return intersection_p;
29454 }
29455
29456 #endif /* HAVE_WINDOW_SYSTEM */
29457
29458 \f
29459 /***********************************************************************
29460 Initialization
29461 ***********************************************************************/
29462
29463 void
29464 syms_of_xdisp (void)
29465 {
29466 Vwith_echo_area_save_vector = Qnil;
29467 staticpro (&Vwith_echo_area_save_vector);
29468
29469 Vmessage_stack = Qnil;
29470 staticpro (&Vmessage_stack);
29471
29472 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29473 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29474
29475 message_dolog_marker1 = Fmake_marker ();
29476 staticpro (&message_dolog_marker1);
29477 message_dolog_marker2 = Fmake_marker ();
29478 staticpro (&message_dolog_marker2);
29479 message_dolog_marker3 = Fmake_marker ();
29480 staticpro (&message_dolog_marker3);
29481
29482 #ifdef GLYPH_DEBUG
29483 defsubr (&Sdump_frame_glyph_matrix);
29484 defsubr (&Sdump_glyph_matrix);
29485 defsubr (&Sdump_glyph_row);
29486 defsubr (&Sdump_tool_bar_row);
29487 defsubr (&Strace_redisplay);
29488 defsubr (&Strace_to_stderr);
29489 #endif
29490 #ifdef HAVE_WINDOW_SYSTEM
29491 defsubr (&Stool_bar_height);
29492 defsubr (&Slookup_image_map);
29493 #endif
29494 defsubr (&Sline_pixel_height);
29495 defsubr (&Sformat_mode_line);
29496 defsubr (&Sinvisible_p);
29497 defsubr (&Scurrent_bidi_paragraph_direction);
29498 defsubr (&Swindow_text_pixel_size);
29499 defsubr (&Smove_point_visually);
29500
29501 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29502 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29503 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29504 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29505 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29506 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29507 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29508 DEFSYM (Qeval, "eval");
29509 DEFSYM (QCdata, ":data");
29510 DEFSYM (Qdisplay, "display");
29511 DEFSYM (Qspace_width, "space-width");
29512 DEFSYM (Qraise, "raise");
29513 DEFSYM (Qslice, "slice");
29514 DEFSYM (Qspace, "space");
29515 DEFSYM (Qmargin, "margin");
29516 DEFSYM (Qpointer, "pointer");
29517 DEFSYM (Qleft_margin, "left-margin");
29518 DEFSYM (Qright_margin, "right-margin");
29519 DEFSYM (Qcenter, "center");
29520 DEFSYM (Qline_height, "line-height");
29521 DEFSYM (QCalign_to, ":align-to");
29522 DEFSYM (QCrelative_width, ":relative-width");
29523 DEFSYM (QCrelative_height, ":relative-height");
29524 DEFSYM (QCeval, ":eval");
29525 DEFSYM (QCpropertize, ":propertize");
29526 DEFSYM (QCfile, ":file");
29527 DEFSYM (Qfontified, "fontified");
29528 DEFSYM (Qfontification_functions, "fontification-functions");
29529 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29530 DEFSYM (Qescape_glyph, "escape-glyph");
29531 DEFSYM (Qnobreak_space, "nobreak-space");
29532 DEFSYM (Qimage, "image");
29533 DEFSYM (Qtext, "text");
29534 DEFSYM (Qboth, "both");
29535 DEFSYM (Qboth_horiz, "both-horiz");
29536 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29537 DEFSYM (QCmap, ":map");
29538 DEFSYM (QCpointer, ":pointer");
29539 DEFSYM (Qrect, "rect");
29540 DEFSYM (Qcircle, "circle");
29541 DEFSYM (Qpoly, "poly");
29542 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29543 DEFSYM (Qgrow_only, "grow-only");
29544 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29545 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29546 DEFSYM (Qposition, "position");
29547 DEFSYM (Qbuffer_position, "buffer-position");
29548 DEFSYM (Qobject, "object");
29549 DEFSYM (Qbar, "bar");
29550 DEFSYM (Qhbar, "hbar");
29551 DEFSYM (Qbox, "box");
29552 DEFSYM (Qhollow, "hollow");
29553 DEFSYM (Qhand, "hand");
29554 DEFSYM (Qarrow, "arrow");
29555 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29556
29557 list_of_error = list1 (list2 (intern_c_string ("error"),
29558 intern_c_string ("void-variable")));
29559 staticpro (&list_of_error);
29560
29561 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29562 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29563 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29564 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29565
29566 echo_buffer[0] = echo_buffer[1] = Qnil;
29567 staticpro (&echo_buffer[0]);
29568 staticpro (&echo_buffer[1]);
29569
29570 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29571 staticpro (&echo_area_buffer[0]);
29572 staticpro (&echo_area_buffer[1]);
29573
29574 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29575 staticpro (&Vmessages_buffer_name);
29576
29577 mode_line_proptrans_alist = Qnil;
29578 staticpro (&mode_line_proptrans_alist);
29579 mode_line_string_list = Qnil;
29580 staticpro (&mode_line_string_list);
29581 mode_line_string_face = Qnil;
29582 staticpro (&mode_line_string_face);
29583 mode_line_string_face_prop = Qnil;
29584 staticpro (&mode_line_string_face_prop);
29585 Vmode_line_unwind_vector = Qnil;
29586 staticpro (&Vmode_line_unwind_vector);
29587
29588 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29589
29590 help_echo_string = Qnil;
29591 staticpro (&help_echo_string);
29592 help_echo_object = Qnil;
29593 staticpro (&help_echo_object);
29594 help_echo_window = Qnil;
29595 staticpro (&help_echo_window);
29596 previous_help_echo_string = Qnil;
29597 staticpro (&previous_help_echo_string);
29598 help_echo_pos = -1;
29599
29600 DEFSYM (Qright_to_left, "right-to-left");
29601 DEFSYM (Qleft_to_right, "left-to-right");
29602
29603 #ifdef HAVE_WINDOW_SYSTEM
29604 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29605 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29606 For example, if a block cursor is over a tab, it will be drawn as
29607 wide as that tab on the display. */);
29608 x_stretch_cursor_p = 0;
29609 #endif
29610
29611 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29612 doc: /* Non-nil means highlight trailing whitespace.
29613 The face used for trailing whitespace is `trailing-whitespace'. */);
29614 Vshow_trailing_whitespace = Qnil;
29615
29616 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29617 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29618 If the value is t, Emacs highlights non-ASCII chars which have the
29619 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29620 or `escape-glyph' face respectively.
29621
29622 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29623 U+2011 (non-breaking hyphen) are affected.
29624
29625 Any other non-nil value means to display these characters as a escape
29626 glyph followed by an ordinary space or hyphen.
29627
29628 A value of nil means no special handling of these characters. */);
29629 Vnobreak_char_display = Qt;
29630
29631 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29632 doc: /* The pointer shape to show in void text areas.
29633 A value of nil means to show the text pointer. Other options are `arrow',
29634 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29635 Vvoid_text_area_pointer = Qarrow;
29636
29637 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29638 doc: /* Non-nil means don't actually do any redisplay.
29639 This is used for internal purposes. */);
29640 Vinhibit_redisplay = Qnil;
29641
29642 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29643 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29644 Vglobal_mode_string = Qnil;
29645
29646 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29647 doc: /* Marker for where to display an arrow on top of the buffer text.
29648 This must be the beginning of a line in order to work.
29649 See also `overlay-arrow-string'. */);
29650 Voverlay_arrow_position = Qnil;
29651
29652 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29653 doc: /* String to display as an arrow in non-window frames.
29654 See also `overlay-arrow-position'. */);
29655 Voverlay_arrow_string = build_pure_c_string ("=>");
29656
29657 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29658 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29659 The symbols on this list are examined during redisplay to determine
29660 where to display overlay arrows. */);
29661 Voverlay_arrow_variable_list
29662 = list1 (intern_c_string ("overlay-arrow-position"));
29663
29664 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29665 doc: /* The number of lines to try scrolling a window by when point moves out.
29666 If that fails to bring point back on frame, point is centered instead.
29667 If this is zero, point is always centered after it moves off frame.
29668 If you want scrolling to always be a line at a time, you should set
29669 `scroll-conservatively' to a large value rather than set this to 1. */);
29670
29671 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29672 doc: /* Scroll up to this many lines, to bring point back on screen.
29673 If point moves off-screen, redisplay will scroll by up to
29674 `scroll-conservatively' lines in order to bring point just barely
29675 onto the screen again. If that cannot be done, then redisplay
29676 recenters point as usual.
29677
29678 If the value is greater than 100, redisplay will never recenter point,
29679 but will always scroll just enough text to bring point into view, even
29680 if you move far away.
29681
29682 A value of zero means always recenter point if it moves off screen. */);
29683 scroll_conservatively = 0;
29684
29685 DEFVAR_INT ("scroll-margin", scroll_margin,
29686 doc: /* Number of lines of margin at the top and bottom of a window.
29687 Recenter the window whenever point gets within this many lines
29688 of the top or bottom of the window. */);
29689 scroll_margin = 0;
29690
29691 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29692 doc: /* Pixels per inch value for non-window system displays.
29693 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29694 Vdisplay_pixels_per_inch = make_float (72.0);
29695
29696 #ifdef GLYPH_DEBUG
29697 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29698 #endif
29699
29700 DEFVAR_LISP ("truncate-partial-width-windows",
29701 Vtruncate_partial_width_windows,
29702 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29703 For an integer value, truncate lines in each window narrower than the
29704 full frame width, provided the window width is less than that integer;
29705 otherwise, respect the value of `truncate-lines'.
29706
29707 For any other non-nil value, truncate lines in all windows that do
29708 not span the full frame width.
29709
29710 A value of nil means to respect the value of `truncate-lines'.
29711
29712 If `word-wrap' is enabled, you might want to reduce this. */);
29713 Vtruncate_partial_width_windows = make_number (50);
29714
29715 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29716 doc: /* Maximum buffer size for which line number should be displayed.
29717 If the buffer is bigger than this, the line number does not appear
29718 in the mode line. A value of nil means no limit. */);
29719 Vline_number_display_limit = Qnil;
29720
29721 DEFVAR_INT ("line-number-display-limit-width",
29722 line_number_display_limit_width,
29723 doc: /* Maximum line width (in characters) for line number display.
29724 If the average length of the lines near point is bigger than this, then the
29725 line number may be omitted from the mode line. */);
29726 line_number_display_limit_width = 200;
29727
29728 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29729 doc: /* Non-nil means highlight region even in nonselected windows. */);
29730 highlight_nonselected_windows = 0;
29731
29732 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29733 doc: /* Non-nil if more than one frame is visible on this display.
29734 Minibuffer-only frames don't count, but iconified frames do.
29735 This variable is not guaranteed to be accurate except while processing
29736 `frame-title-format' and `icon-title-format'. */);
29737
29738 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29739 doc: /* Template for displaying the title bar of visible frames.
29740 \(Assuming the window manager supports this feature.)
29741
29742 This variable has the same structure as `mode-line-format', except that
29743 the %c and %l constructs are ignored. It is used only on frames for
29744 which no explicit name has been set \(see `modify-frame-parameters'). */);
29745
29746 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29747 doc: /* Template for displaying the title bar of an iconified frame.
29748 \(Assuming the window manager supports this feature.)
29749 This variable has the same structure as `mode-line-format' (which see),
29750 and is used only on frames for which no explicit name has been set
29751 \(see `modify-frame-parameters'). */);
29752 Vicon_title_format
29753 = Vframe_title_format
29754 = listn (CONSTYPE_PURE, 3,
29755 intern_c_string ("multiple-frames"),
29756 build_pure_c_string ("%b"),
29757 listn (CONSTYPE_PURE, 4,
29758 empty_unibyte_string,
29759 intern_c_string ("invocation-name"),
29760 build_pure_c_string ("@"),
29761 intern_c_string ("system-name")));
29762
29763 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29764 doc: /* Maximum number of lines to keep in the message log buffer.
29765 If nil, disable message logging. If t, log messages but don't truncate
29766 the buffer when it becomes large. */);
29767 Vmessage_log_max = make_number (1000);
29768
29769 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29770 doc: /* Functions called before redisplay, if window sizes have changed.
29771 The value should be a list of functions that take one argument.
29772 Just before redisplay, for each frame, if any of its windows have changed
29773 size since the last redisplay, or have been split or deleted,
29774 all the functions in the list are called, with the frame as argument. */);
29775 Vwindow_size_change_functions = Qnil;
29776
29777 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29778 doc: /* List of functions to call before redisplaying a window with scrolling.
29779 Each function is called with two arguments, the window and its new
29780 display-start position. Note that these functions are also called by
29781 `set-window-buffer'. Also note that the value of `window-end' is not
29782 valid when these functions are called.
29783
29784 Warning: Do not use this feature to alter the way the window
29785 is scrolled. It is not designed for that, and such use probably won't
29786 work. */);
29787 Vwindow_scroll_functions = Qnil;
29788
29789 DEFVAR_LISP ("window-text-change-functions",
29790 Vwindow_text_change_functions,
29791 doc: /* Functions to call in redisplay when text in the window might change. */);
29792 Vwindow_text_change_functions = Qnil;
29793
29794 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29795 doc: /* Functions called when redisplay of a window reaches the end trigger.
29796 Each function is called with two arguments, the window and the end trigger value.
29797 See `set-window-redisplay-end-trigger'. */);
29798 Vredisplay_end_trigger_functions = Qnil;
29799
29800 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29801 doc: /* Non-nil means autoselect window with mouse pointer.
29802 If nil, do not autoselect windows.
29803 A positive number means delay autoselection by that many seconds: a
29804 window is autoselected only after the mouse has remained in that
29805 window for the duration of the delay.
29806 A negative number has a similar effect, but causes windows to be
29807 autoselected only after the mouse has stopped moving. \(Because of
29808 the way Emacs compares mouse events, you will occasionally wait twice
29809 that time before the window gets selected.\)
29810 Any other value means to autoselect window instantaneously when the
29811 mouse pointer enters it.
29812
29813 Autoselection selects the minibuffer only if it is active, and never
29814 unselects the minibuffer if it is active.
29815
29816 When customizing this variable make sure that the actual value of
29817 `focus-follows-mouse' matches the behavior of your window manager. */);
29818 Vmouse_autoselect_window = Qnil;
29819
29820 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29821 doc: /* Non-nil means automatically resize tool-bars.
29822 This dynamically changes the tool-bar's height to the minimum height
29823 that is needed to make all tool-bar items visible.
29824 If value is `grow-only', the tool-bar's height is only increased
29825 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29826 Vauto_resize_tool_bars = Qt;
29827
29828 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29829 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29830 auto_raise_tool_bar_buttons_p = 1;
29831
29832 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29833 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29834 make_cursor_line_fully_visible_p = 1;
29835
29836 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29837 doc: /* Border below tool-bar in pixels.
29838 If an integer, use it as the height of the border.
29839 If it is one of `internal-border-width' or `border-width', use the
29840 value of the corresponding frame parameter.
29841 Otherwise, no border is added below the tool-bar. */);
29842 Vtool_bar_border = Qinternal_border_width;
29843
29844 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29845 doc: /* Margin around tool-bar buttons in pixels.
29846 If an integer, use that for both horizontal and vertical margins.
29847 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29848 HORZ specifying the horizontal margin, and VERT specifying the
29849 vertical margin. */);
29850 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29851
29852 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29853 doc: /* Relief thickness of tool-bar buttons. */);
29854 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29855
29856 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29857 doc: /* Tool bar style to use.
29858 It can be one of
29859 image - show images only
29860 text - show text only
29861 both - show both, text below image
29862 both-horiz - show text to the right of the image
29863 text-image-horiz - show text to the left of the image
29864 any other - use system default or image if no system default.
29865
29866 This variable only affects the GTK+ toolkit version of Emacs. */);
29867 Vtool_bar_style = Qnil;
29868
29869 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29870 doc: /* Maximum number of characters a label can have to be shown.
29871 The tool bar style must also show labels for this to have any effect, see
29872 `tool-bar-style'. */);
29873 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29874
29875 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29876 doc: /* List of functions to call to fontify regions of text.
29877 Each function is called with one argument POS. Functions must
29878 fontify a region starting at POS in the current buffer, and give
29879 fontified regions the property `fontified'. */);
29880 Vfontification_functions = Qnil;
29881 Fmake_variable_buffer_local (Qfontification_functions);
29882
29883 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29884 unibyte_display_via_language_environment,
29885 doc: /* Non-nil means display unibyte text according to language environment.
29886 Specifically, this means that raw bytes in the range 160-255 decimal
29887 are displayed by converting them to the equivalent multibyte characters
29888 according to the current language environment. As a result, they are
29889 displayed according to the current fontset.
29890
29891 Note that this variable affects only how these bytes are displayed,
29892 but does not change the fact they are interpreted as raw bytes. */);
29893 unibyte_display_via_language_environment = 0;
29894
29895 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29896 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29897 If a float, it specifies a fraction of the mini-window frame's height.
29898 If an integer, it specifies a number of lines. */);
29899 Vmax_mini_window_height = make_float (0.25);
29900
29901 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29902 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29903 A value of nil means don't automatically resize mini-windows.
29904 A value of t means resize them to fit the text displayed in them.
29905 A value of `grow-only', the default, means let mini-windows grow only;
29906 they return to their normal size when the minibuffer is closed, or the
29907 echo area becomes empty. */);
29908 Vresize_mini_windows = Qgrow_only;
29909
29910 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29911 doc: /* Alist specifying how to blink the cursor off.
29912 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29913 `cursor-type' frame-parameter or variable equals ON-STATE,
29914 comparing using `equal', Emacs uses OFF-STATE to specify
29915 how to blink it off. ON-STATE and OFF-STATE are values for
29916 the `cursor-type' frame parameter.
29917
29918 If a frame's ON-STATE has no entry in this list,
29919 the frame's other specifications determine how to blink the cursor off. */);
29920 Vblink_cursor_alist = Qnil;
29921
29922 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29923 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29924 If non-nil, windows are automatically scrolled horizontally to make
29925 point visible. */);
29926 automatic_hscrolling_p = 1;
29927 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29928
29929 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29930 doc: /* How many columns away from the window edge point is allowed to get
29931 before automatic hscrolling will horizontally scroll the window. */);
29932 hscroll_margin = 5;
29933
29934 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29935 doc: /* How many columns to scroll the window when point gets too close to the edge.
29936 When point is less than `hscroll-margin' columns from the window
29937 edge, automatic hscrolling will scroll the window by the amount of columns
29938 determined by this variable. If its value is a positive integer, scroll that
29939 many columns. If it's a positive floating-point number, it specifies the
29940 fraction of the window's width to scroll. If it's nil or zero, point will be
29941 centered horizontally after the scroll. Any other value, including negative
29942 numbers, are treated as if the value were zero.
29943
29944 Automatic hscrolling always moves point outside the scroll margin, so if
29945 point was more than scroll step columns inside the margin, the window will
29946 scroll more than the value given by the scroll step.
29947
29948 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29949 and `scroll-right' overrides this variable's effect. */);
29950 Vhscroll_step = make_number (0);
29951
29952 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29953 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29954 Bind this around calls to `message' to let it take effect. */);
29955 message_truncate_lines = 0;
29956
29957 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29958 doc: /* Normal hook run to update the menu bar definitions.
29959 Redisplay runs this hook before it redisplays the menu bar.
29960 This is used to update submenus such as Buffers,
29961 whose contents depend on various data. */);
29962 Vmenu_bar_update_hook = Qnil;
29963
29964 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29965 doc: /* Frame for which we are updating a menu.
29966 The enable predicate for a menu binding should check this variable. */);
29967 Vmenu_updating_frame = Qnil;
29968
29969 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29970 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29971 inhibit_menubar_update = 0;
29972
29973 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29974 doc: /* Prefix prepended to all continuation lines at display time.
29975 The value may be a string, an image, or a stretch-glyph; it is
29976 interpreted in the same way as the value of a `display' text property.
29977
29978 This variable is overridden by any `wrap-prefix' text or overlay
29979 property.
29980
29981 To add a prefix to non-continuation lines, use `line-prefix'. */);
29982 Vwrap_prefix = Qnil;
29983 DEFSYM (Qwrap_prefix, "wrap-prefix");
29984 Fmake_variable_buffer_local (Qwrap_prefix);
29985
29986 DEFVAR_LISP ("line-prefix", Vline_prefix,
29987 doc: /* Prefix prepended to all non-continuation lines at display time.
29988 The value may be a string, an image, or a stretch-glyph; it is
29989 interpreted in the same way as the value of a `display' text property.
29990
29991 This variable is overridden by any `line-prefix' text or overlay
29992 property.
29993
29994 To add a prefix to continuation lines, use `wrap-prefix'. */);
29995 Vline_prefix = Qnil;
29996 DEFSYM (Qline_prefix, "line-prefix");
29997 Fmake_variable_buffer_local (Qline_prefix);
29998
29999 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30000 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30001 inhibit_eval_during_redisplay = 0;
30002
30003 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30004 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30005 inhibit_free_realized_faces = 0;
30006
30007 #ifdef GLYPH_DEBUG
30008 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30009 doc: /* Inhibit try_window_id display optimization. */);
30010 inhibit_try_window_id = 0;
30011
30012 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30013 doc: /* Inhibit try_window_reusing display optimization. */);
30014 inhibit_try_window_reusing = 0;
30015
30016 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30017 doc: /* Inhibit try_cursor_movement display optimization. */);
30018 inhibit_try_cursor_movement = 0;
30019 #endif /* GLYPH_DEBUG */
30020
30021 DEFVAR_INT ("overline-margin", overline_margin,
30022 doc: /* Space between overline and text, in pixels.
30023 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30024 margin to the character height. */);
30025 overline_margin = 2;
30026
30027 DEFVAR_INT ("underline-minimum-offset",
30028 underline_minimum_offset,
30029 doc: /* Minimum distance between baseline and underline.
30030 This can improve legibility of underlined text at small font sizes,
30031 particularly when using variable `x-use-underline-position-properties'
30032 with fonts that specify an UNDERLINE_POSITION relatively close to the
30033 baseline. The default value is 1. */);
30034 underline_minimum_offset = 1;
30035
30036 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30037 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30038 This feature only works when on a window system that can change
30039 cursor shapes. */);
30040 display_hourglass_p = 1;
30041
30042 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30043 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30044 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30045
30046 #ifdef HAVE_WINDOW_SYSTEM
30047 hourglass_atimer = NULL;
30048 hourglass_shown_p = 0;
30049 #endif /* HAVE_WINDOW_SYSTEM */
30050
30051 DEFSYM (Qglyphless_char, "glyphless-char");
30052 DEFSYM (Qhex_code, "hex-code");
30053 DEFSYM (Qempty_box, "empty-box");
30054 DEFSYM (Qthin_space, "thin-space");
30055 DEFSYM (Qzero_width, "zero-width");
30056
30057 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30058 doc: /* Function run just before redisplay.
30059 It is called with one argument, which is the set of windows that are to
30060 be redisplayed. This set can be nil (meaning, only the selected window),
30061 or t (meaning all windows). */);
30062 Vpre_redisplay_function = intern ("ignore");
30063
30064 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30065 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30066
30067 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30068 doc: /* Char-table defining glyphless characters.
30069 Each element, if non-nil, should be one of the following:
30070 an ASCII acronym string: display this string in a box
30071 `hex-code': display the hexadecimal code of a character in a box
30072 `empty-box': display as an empty box
30073 `thin-space': display as 1-pixel width space
30074 `zero-width': don't display
30075 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30076 display method for graphical terminals and text terminals respectively.
30077 GRAPHICAL and TEXT should each have one of the values listed above.
30078
30079 The char-table has one extra slot to control the display of a character for
30080 which no font is found. This slot only takes effect on graphical terminals.
30081 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30082 `thin-space'. The default is `empty-box'.
30083
30084 If a character has a non-nil entry in an active display table, the
30085 display table takes effect; in this case, Emacs does not consult
30086 `glyphless-char-display' at all. */);
30087 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30088 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30089 Qempty_box);
30090
30091 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30092 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30093 Vdebug_on_message = Qnil;
30094
30095 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30096 doc: /* */);
30097 Vredisplay__all_windows_cause
30098 = Fmake_vector (make_number (100), make_number (0));
30099
30100 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30101 doc: /* */);
30102 Vredisplay__mode_lines_cause
30103 = Fmake_vector (make_number (100), make_number (0));
30104 }
30105
30106
30107 /* Initialize this module when Emacs starts. */
30108
30109 void
30110 init_xdisp (void)
30111 {
30112 CHARPOS (this_line_start_pos) = 0;
30113
30114 if (!noninteractive)
30115 {
30116 struct window *m = XWINDOW (minibuf_window);
30117 Lisp_Object frame = m->frame;
30118 struct frame *f = XFRAME (frame);
30119 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30120 struct window *r = XWINDOW (root);
30121 int i;
30122
30123 echo_area_window = minibuf_window;
30124
30125 r->top_line = FRAME_TOP_MARGIN (f);
30126 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30127 r->total_cols = FRAME_COLS (f);
30128 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30129 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30130 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30131
30132 m->top_line = FRAME_LINES (f) - 1;
30133 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30134 m->total_cols = FRAME_COLS (f);
30135 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30136 m->total_lines = 1;
30137 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30138
30139 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30140 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30141 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30142
30143 /* The default ellipsis glyphs `...'. */
30144 for (i = 0; i < 3; ++i)
30145 default_invis_vector[i] = make_number ('.');
30146 }
30147
30148 {
30149 /* Allocate the buffer for frame titles.
30150 Also used for `format-mode-line'. */
30151 int size = 100;
30152 mode_line_noprop_buf = xmalloc (size);
30153 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30154 mode_line_noprop_ptr = mode_line_noprop_buf;
30155 mode_line_target = MODE_LINE_DISPLAY;
30156 }
30157
30158 help_echo_showing_p = 0;
30159 }
30160
30161 #ifdef HAVE_WINDOW_SYSTEM
30162
30163 /* Platform-independent portion of hourglass implementation. */
30164
30165 /* Cancel a currently active hourglass timer, and start a new one. */
30166 void
30167 start_hourglass (void)
30168 {
30169 struct timespec delay;
30170
30171 cancel_hourglass ();
30172
30173 if (INTEGERP (Vhourglass_delay)
30174 && XINT (Vhourglass_delay) > 0)
30175 delay = make_timespec (min (XINT (Vhourglass_delay),
30176 TYPE_MAXIMUM (time_t)),
30177 0);
30178 else if (FLOATP (Vhourglass_delay)
30179 && XFLOAT_DATA (Vhourglass_delay) > 0)
30180 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30181 else
30182 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30183
30184 #ifdef HAVE_NTGUI
30185 {
30186 extern void w32_note_current_window (void);
30187 w32_note_current_window ();
30188 }
30189 #endif /* HAVE_NTGUI */
30190
30191 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30192 show_hourglass, NULL);
30193 }
30194
30195
30196 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30197 shown. */
30198 void
30199 cancel_hourglass (void)
30200 {
30201 if (hourglass_atimer)
30202 {
30203 cancel_atimer (hourglass_atimer);
30204 hourglass_atimer = NULL;
30205 }
30206
30207 if (hourglass_shown_p)
30208 hide_hourglass ();
30209 }
30210
30211 #endif /* HAVE_WINDOW_SYSTEM */