merge trunk
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 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 "keyboard.h"
279 #include "frame.h"
280 #include "window.h"
281 #include "termchar.h"
282 #include "dispextern.h"
283 #include "character.h"
284 #include "buffer.h"
285 #include "charset.h"
286 #include "indent.h"
287 #include "commands.h"
288 #include "keymap.h"
289 #include "macros.h"
290 #include "disptab.h"
291 #include "termhooks.h"
292 #include "termopts.h"
293 #include "intervals.h"
294 #include "coding.h"
295 #include "process.h"
296 #include "region-cache.h"
297 #include "font.h"
298 #include "fontset.h"
299 #include "blockinput.h"
300
301 #ifdef HAVE_X_WINDOWS
302 #include "xterm.h"
303 #endif
304 #ifdef WINDOWSNT
305 #include "w32term.h"
306 #endif
307 #ifdef HAVE_NS
308 #include "nsterm.h"
309 #endif
310 #ifdef USE_GTK
311 #include "gtkutil.h"
312 #endif
313
314 #include "font.h"
315
316 #ifndef FRAME_X_OUTPUT
317 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
318 #endif
319
320 #define INFINITY 10000000
321
322 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
323 Lisp_Object Qwindow_scroll_functions;
324 static Lisp_Object Qwindow_text_change_functions;
325 static Lisp_Object Qredisplay_end_trigger_functions;
326 Lisp_Object Qinhibit_point_motion_hooks;
327 static Lisp_Object QCeval, QCpropertize;
328 Lisp_Object QCfile, QCdata;
329 static Lisp_Object Qfontified;
330 static Lisp_Object Qgrow_only;
331 static Lisp_Object Qinhibit_eval_during_redisplay;
332 static Lisp_Object Qbuffer_position, Qposition, Qobject;
333 static Lisp_Object Qright_to_left, Qleft_to_right;
334
335 /* Cursor shapes */
336 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
337
338 /* Pointer shapes */
339 static Lisp_Object Qarrow, Qhand;
340 Lisp_Object Qtext;
341
342 /* Holds the list (error). */
343 static Lisp_Object list_of_error;
344
345 static Lisp_Object Qfontification_functions;
346
347 static Lisp_Object Qwrap_prefix;
348 static Lisp_Object Qline_prefix;
349
350 /* Non-nil means don't actually do any redisplay. */
351
352 Lisp_Object Qinhibit_redisplay;
353
354 /* Names of text properties relevant for redisplay. */
355
356 Lisp_Object Qdisplay;
357
358 Lisp_Object Qspace, QCalign_to;
359 static Lisp_Object QCrelative_width, QCrelative_height;
360 Lisp_Object Qleft_margin, Qright_margin;
361 static Lisp_Object Qspace_width, Qraise;
362 static Lisp_Object Qslice;
363 Lisp_Object Qcenter;
364 static Lisp_Object Qmargin, Qpointer;
365 static Lisp_Object Qline_height;
366
367 /* These setters are used only in this file, so they can be private. */
368 static inline void
369 wset_base_line_number (struct window *w, Lisp_Object val)
370 {
371 w->base_line_number = val;
372 }
373 static inline void
374 wset_base_line_pos (struct window *w, Lisp_Object val)
375 {
376 w->base_line_pos = val;
377 }
378 static inline void
379 wset_column_number_displayed (struct window *w, Lisp_Object val)
380 {
381 w->column_number_displayed = val;
382 }
383 static inline void
384 wset_region_showing (struct window *w, Lisp_Object val)
385 {
386 w->region_showing = val;
387 }
388
389 #ifdef HAVE_WINDOW_SYSTEM
390
391 /* Test if overflow newline into fringe. Called with iterator IT
392 at or past right window margin, and with IT->current_x set. */
393
394 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
395 (!NILP (Voverflow_newline_into_fringe) \
396 && FRAME_WINDOW_P ((IT)->f) \
397 && ((IT)->bidi_it.paragraph_dir == R2L \
398 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
399 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
400 && (IT)->current_x == (IT)->last_visible_x \
401 && (IT)->line_wrap != WORD_WRAP)
402
403 #else /* !HAVE_WINDOW_SYSTEM */
404 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
405 #endif /* HAVE_WINDOW_SYSTEM */
406
407 /* Test if the display element loaded in IT, or the underlying buffer
408 or string character, is a space or a TAB character. This is used
409 to determine where word wrapping can occur. */
410
411 #define IT_DISPLAYING_WHITESPACE(it) \
412 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
413 || ((STRINGP (it->string) \
414 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
415 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
416 || (it->s \
417 && (it->s[IT_BYTEPOS (*it)] == ' ' \
418 || it->s[IT_BYTEPOS (*it)] == '\t')) \
419 || (IT_BYTEPOS (*it) < ZV_BYTE \
420 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
421 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
422
423 /* Name of the face used to highlight trailing whitespace. */
424
425 static Lisp_Object Qtrailing_whitespace;
426
427 /* Name and number of the face used to highlight escape glyphs. */
428
429 static Lisp_Object Qescape_glyph;
430
431 /* Name and number of the face used to highlight non-breaking spaces. */
432
433 static Lisp_Object Qnobreak_space;
434
435 /* The symbol `image' which is the car of the lists used to represent
436 images in Lisp. Also a tool bar style. */
437
438 Lisp_Object Qimage;
439
440 /* The image map types. */
441 Lisp_Object QCmap;
442 static Lisp_Object QCpointer;
443 static Lisp_Object Qrect, Qcircle, Qpoly;
444
445 /* Tool bar styles */
446 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
447
448 /* Non-zero means print newline to stdout before next mini-buffer
449 message. */
450
451 int noninteractive_need_newline;
452
453 /* Non-zero means print newline to message log before next message. */
454
455 static int message_log_need_newline;
456
457 /* Three markers that message_dolog uses.
458 It could allocate them itself, but that causes trouble
459 in handling memory-full errors. */
460 static Lisp_Object message_dolog_marker1;
461 static Lisp_Object message_dolog_marker2;
462 static Lisp_Object message_dolog_marker3;
463 \f
464 /* The buffer position of the first character appearing entirely or
465 partially on the line of the selected window which contains the
466 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
467 redisplay optimization in redisplay_internal. */
468
469 static struct text_pos this_line_start_pos;
470
471 /* Number of characters past the end of the line above, including the
472 terminating newline. */
473
474 static struct text_pos this_line_end_pos;
475
476 /* The vertical positions and the height of this line. */
477
478 static int this_line_vpos;
479 static int this_line_y;
480 static int this_line_pixel_height;
481
482 /* X position at which this display line starts. Usually zero;
483 negative if first character is partially visible. */
484
485 static int this_line_start_x;
486
487 /* The smallest character position seen by move_it_* functions as they
488 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
489 hscrolled lines, see display_line. */
490
491 static struct text_pos this_line_min_pos;
492
493 /* Buffer that this_line_.* variables are referring to. */
494
495 static struct buffer *this_line_buffer;
496
497
498 /* Values of those variables at last redisplay are stored as
499 properties on `overlay-arrow-position' symbol. However, if
500 Voverlay_arrow_position is a marker, last-arrow-position is its
501 numerical position. */
502
503 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
504
505 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
506 properties on a symbol in overlay-arrow-variable-list. */
507
508 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
509
510 Lisp_Object Qmenu_bar_update_hook;
511
512 /* Nonzero if an overlay arrow has been displayed in this window. */
513
514 static int overlay_arrow_seen;
515
516 /* Number of windows showing the buffer of the selected window (or
517 another buffer with the same base buffer). keyboard.c refers to
518 this. */
519
520 int buffer_shared;
521
522 /* Vector containing glyphs for an ellipsis `...'. */
523
524 static Lisp_Object default_invis_vector[3];
525
526 /* This is the window where the echo area message was displayed. It
527 is always a mini-buffer window, but it may not be the same window
528 currently active as a mini-buffer. */
529
530 Lisp_Object echo_area_window;
531
532 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
533 pushes the current message and the value of
534 message_enable_multibyte on the stack, the function restore_message
535 pops the stack and displays MESSAGE again. */
536
537 static Lisp_Object Vmessage_stack;
538
539 /* Nonzero means multibyte characters were enabled when the echo area
540 message was specified. */
541
542 static int message_enable_multibyte;
543
544 /* Nonzero if we should redraw the mode lines on the next redisplay. */
545
546 int update_mode_lines;
547
548 /* Nonzero if window sizes or contents have changed since last
549 redisplay that finished. */
550
551 int windows_or_buffers_changed;
552
553 /* Nonzero means a frame's cursor type has been changed. */
554
555 int cursor_type_changed;
556
557 /* Nonzero after display_mode_line if %l was used and it displayed a
558 line number. */
559
560 static int line_number_displayed;
561
562 /* The name of the *Messages* buffer, a string. */
563
564 static Lisp_Object Vmessages_buffer_name;
565
566 /* Current, index 0, and last displayed echo area message. Either
567 buffers from echo_buffers, or nil to indicate no message. */
568
569 Lisp_Object echo_area_buffer[2];
570
571 /* The buffers referenced from echo_area_buffer. */
572
573 static Lisp_Object echo_buffer[2];
574
575 /* A vector saved used in with_area_buffer to reduce consing. */
576
577 static Lisp_Object Vwith_echo_area_save_vector;
578
579 /* Non-zero means display_echo_area should display the last echo area
580 message again. Set by redisplay_preserve_echo_area. */
581
582 static int display_last_displayed_message_p;
583
584 /* Nonzero if echo area is being used by print; zero if being used by
585 message. */
586
587 static int message_buf_print;
588
589 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
590
591 static Lisp_Object Qinhibit_menubar_update;
592 static Lisp_Object Qmessage_truncate_lines;
593
594 /* Set to 1 in clear_message to make redisplay_internal aware
595 of an emptied echo area. */
596
597 static int message_cleared_p;
598
599 /* A scratch glyph row with contents used for generating truncation
600 glyphs. Also used in direct_output_for_insert. */
601
602 #define MAX_SCRATCH_GLYPHS 100
603 static struct glyph_row scratch_glyph_row;
604 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
605
606 /* Ascent and height of the last line processed by move_it_to. */
607
608 static int last_max_ascent, last_height;
609
610 /* Non-zero if there's a help-echo in the echo area. */
611
612 int help_echo_showing_p;
613
614 /* If >= 0, computed, exact values of mode-line and header-line height
615 to use in the macros CURRENT_MODE_LINE_HEIGHT and
616 CURRENT_HEADER_LINE_HEIGHT. */
617
618 int current_mode_line_height, current_header_line_height;
619
620 /* The maximum distance to look ahead for text properties. Values
621 that are too small let us call compute_char_face and similar
622 functions too often which is expensive. Values that are too large
623 let us call compute_char_face and alike too often because we
624 might not be interested in text properties that far away. */
625
626 #define TEXT_PROP_DISTANCE_LIMIT 100
627
628 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
629 iterator state and later restore it. This is needed because the
630 bidi iterator on bidi.c keeps a stacked cache of its states, which
631 is really a singleton. When we use scratch iterator objects to
632 move around the buffer, we can cause the bidi cache to be pushed or
633 popped, and therefore we need to restore the cache state when we
634 return to the original iterator. */
635 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
636 do { \
637 if (CACHE) \
638 bidi_unshelve_cache (CACHE, 1); \
639 ITCOPY = ITORIG; \
640 CACHE = bidi_shelve_cache (); \
641 } while (0)
642
643 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
644 do { \
645 if (pITORIG != pITCOPY) \
646 *(pITORIG) = *(pITCOPY); \
647 bidi_unshelve_cache (CACHE, 0); \
648 CACHE = NULL; \
649 } while (0)
650
651 #ifdef GLYPH_DEBUG
652
653 /* Non-zero means print traces of redisplay if compiled with
654 GLYPH_DEBUG defined. */
655
656 int trace_redisplay_p;
657
658 #endif /* GLYPH_DEBUG */
659
660 #ifdef DEBUG_TRACE_MOVE
661 /* Non-zero means trace with TRACE_MOVE to stderr. */
662 int trace_move;
663
664 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
665 #else
666 #define TRACE_MOVE(x) (void) 0
667 #endif
668
669 static Lisp_Object Qauto_hscroll_mode;
670
671 /* Buffer being redisplayed -- for redisplay_window_error. */
672
673 static struct buffer *displayed_buffer;
674
675 /* Value returned from text property handlers (see below). */
676
677 enum prop_handled
678 {
679 HANDLED_NORMALLY,
680 HANDLED_RECOMPUTE_PROPS,
681 HANDLED_OVERLAY_STRING_CONSUMED,
682 HANDLED_RETURN
683 };
684
685 /* A description of text properties that redisplay is interested
686 in. */
687
688 struct props
689 {
690 /* The name of the property. */
691 Lisp_Object *name;
692
693 /* A unique index for the property. */
694 enum prop_idx idx;
695
696 /* A handler function called to set up iterator IT from the property
697 at IT's current position. Value is used to steer handle_stop. */
698 enum prop_handled (*handler) (struct it *it);
699 };
700
701 static enum prop_handled handle_face_prop (struct it *);
702 static enum prop_handled handle_invisible_prop (struct it *);
703 static enum prop_handled handle_display_prop (struct it *);
704 static enum prop_handled handle_composition_prop (struct it *);
705 static enum prop_handled handle_overlay_change (struct it *);
706 static enum prop_handled handle_fontified_prop (struct it *);
707
708 /* Properties handled by iterators. */
709
710 static struct props it_props[] =
711 {
712 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
713 /* Handle `face' before `display' because some sub-properties of
714 `display' need to know the face. */
715 {&Qface, FACE_PROP_IDX, handle_face_prop},
716 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
717 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
718 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
719 {NULL, 0, NULL}
720 };
721
722 /* Value is the position described by X. If X is a marker, value is
723 the marker_position of X. Otherwise, value is X. */
724
725 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
726
727 /* Enumeration returned by some move_it_.* functions internally. */
728
729 enum move_it_result
730 {
731 /* Not used. Undefined value. */
732 MOVE_UNDEFINED,
733
734 /* Move ended at the requested buffer position or ZV. */
735 MOVE_POS_MATCH_OR_ZV,
736
737 /* Move ended at the requested X pixel position. */
738 MOVE_X_REACHED,
739
740 /* Move within a line ended at the end of a line that must be
741 continued. */
742 MOVE_LINE_CONTINUED,
743
744 /* Move within a line ended at the end of a line that would
745 be displayed truncated. */
746 MOVE_LINE_TRUNCATED,
747
748 /* Move within a line ended at a line end. */
749 MOVE_NEWLINE_OR_CR
750 };
751
752 /* This counter is used to clear the face cache every once in a while
753 in redisplay_internal. It is incremented for each redisplay.
754 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
755 cleared. */
756
757 #define CLEAR_FACE_CACHE_COUNT 500
758 static int clear_face_cache_count;
759
760 /* Similarly for the image cache. */
761
762 #ifdef HAVE_WINDOW_SYSTEM
763 #define CLEAR_IMAGE_CACHE_COUNT 101
764 static int clear_image_cache_count;
765
766 /* Null glyph slice */
767 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
768 #endif
769
770 /* True while redisplay_internal is in progress. */
771
772 bool redisplaying_p;
773
774 static Lisp_Object Qinhibit_free_realized_faces;
775 static Lisp_Object Qmode_line_default_help_echo;
776
777 /* If a string, XTread_socket generates an event to display that string.
778 (The display is done in read_char.) */
779
780 Lisp_Object help_echo_string;
781 Lisp_Object help_echo_window;
782 Lisp_Object help_echo_object;
783 ptrdiff_t help_echo_pos;
784
785 /* Temporary variable for XTread_socket. */
786
787 Lisp_Object previous_help_echo_string;
788
789 /* Platform-independent portion of hourglass implementation. */
790
791 /* Non-zero means an hourglass cursor is currently shown. */
792 int hourglass_shown_p;
793
794 /* If non-null, an asynchronous timer that, when it expires, displays
795 an hourglass cursor on all frames. */
796 struct atimer *hourglass_atimer;
797
798 /* Name of the face used to display glyphless characters. */
799 Lisp_Object Qglyphless_char;
800
801 /* Symbol for the purpose of Vglyphless_char_display. */
802 static Lisp_Object Qglyphless_char_display;
803
804 /* Method symbols for Vglyphless_char_display. */
805 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
806
807 /* Default pixel width of `thin-space' display method. */
808 #define THIN_SPACE_WIDTH 1
809
810 /* Default number of seconds to wait before displaying an hourglass
811 cursor. */
812 #define DEFAULT_HOURGLASS_DELAY 1
813
814 \f
815 /* Function prototypes. */
816
817 static void setup_for_ellipsis (struct it *, int);
818 static void set_iterator_to_next (struct it *, int);
819 static void mark_window_display_accurate_1 (struct window *, int);
820 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
821 static int display_prop_string_p (Lisp_Object, Lisp_Object);
822 static int cursor_row_p (struct glyph_row *);
823 static int redisplay_mode_lines (Lisp_Object, int);
824 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
825
826 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
827
828 static void handle_line_prefix (struct it *);
829
830 static void pint2str (char *, int, ptrdiff_t);
831 static void pint2hrstr (char *, int, ptrdiff_t);
832 static struct text_pos run_window_scroll_functions (Lisp_Object,
833 struct text_pos);
834 static void reconsider_clip_changes (struct window *, struct buffer *);
835 static int text_outside_line_unchanged_p (struct window *,
836 ptrdiff_t, ptrdiff_t);
837 static void store_mode_line_noprop_char (char);
838 static int store_mode_line_noprop (const char *, int, int);
839 static void handle_stop (struct it *);
840 static void handle_stop_backwards (struct it *, ptrdiff_t);
841 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
842 static void ensure_echo_area_buffers (void);
843 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
844 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
845 static int with_echo_area_buffer (struct window *, int,
846 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
847 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
848 static void clear_garbaged_frames (void);
849 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
850 static void pop_message (void);
851 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
852 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
853 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
854 static int display_echo_area (struct window *);
855 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
856 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
857 static Lisp_Object unwind_redisplay (Lisp_Object);
858 static int string_char_and_length (const unsigned char *, int *);
859 static struct text_pos display_prop_end (struct it *, Lisp_Object,
860 struct text_pos);
861 static int compute_window_start_on_continuation_line (struct window *);
862 static void insert_left_trunc_glyphs (struct it *);
863 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
864 Lisp_Object);
865 static void extend_face_to_end_of_line (struct it *);
866 static int append_space_for_newline (struct it *, int);
867 static int cursor_row_fully_visible_p (struct window *, int, int);
868 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
869 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
870 static int trailing_whitespace_p (ptrdiff_t);
871 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
872 static void push_it (struct it *, struct text_pos *);
873 static void iterate_out_of_display_property (struct it *);
874 static void pop_it (struct it *);
875 static void sync_frame_with_window_matrix_rows (struct window *);
876 static void select_frame_for_redisplay (Lisp_Object);
877 static void redisplay_internal (void);
878 static int echo_area_display (int);
879 static void redisplay_windows (Lisp_Object);
880 static void redisplay_window (Lisp_Object, int);
881 static Lisp_Object redisplay_window_error (Lisp_Object);
882 static Lisp_Object redisplay_window_0 (Lisp_Object);
883 static Lisp_Object redisplay_window_1 (Lisp_Object);
884 static int set_cursor_from_row (struct window *, struct glyph_row *,
885 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
886 int, int);
887 static int update_menu_bar (struct frame *, int, int);
888 static int try_window_reusing_current_matrix (struct window *);
889 static int try_window_id (struct window *);
890 static int display_line (struct it *);
891 static int display_mode_lines (struct window *);
892 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
893 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
894 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
895 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
896 static void display_menu_bar (struct window *);
897 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
898 ptrdiff_t *);
899 static int display_string (const char *, Lisp_Object, Lisp_Object,
900 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
901 static void compute_line_metrics (struct it *);
902 static void run_redisplay_end_trigger_hook (struct it *);
903 static int get_overlay_strings (struct it *, ptrdiff_t);
904 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
905 static void next_overlay_string (struct it *);
906 static void reseat (struct it *, struct text_pos, int);
907 static void reseat_1 (struct it *, struct text_pos, int);
908 static void back_to_previous_visible_line_start (struct it *);
909 void reseat_at_previous_visible_line_start (struct it *);
910 static void reseat_at_next_visible_line_start (struct it *, int);
911 static int next_element_from_ellipsis (struct it *);
912 static int next_element_from_display_vector (struct it *);
913 static int next_element_from_string (struct it *);
914 static int next_element_from_c_string (struct it *);
915 static int next_element_from_buffer (struct it *);
916 static int next_element_from_composition (struct it *);
917 static int next_element_from_image (struct it *);
918 static int next_element_from_stretch (struct it *);
919 static void load_overlay_strings (struct it *, ptrdiff_t);
920 static int init_from_display_pos (struct it *, struct window *,
921 struct display_pos *);
922 static void reseat_to_string (struct it *, const char *,
923 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
924 static int get_next_display_element (struct it *);
925 static enum move_it_result
926 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
927 enum move_operation_enum);
928 void move_it_vertically_backward (struct it *, int);
929 static void init_to_row_start (struct it *, struct window *,
930 struct glyph_row *);
931 static int init_to_row_end (struct it *, struct window *,
932 struct glyph_row *);
933 static void back_to_previous_line_start (struct it *);
934 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
935 static struct text_pos string_pos_nchars_ahead (struct text_pos,
936 Lisp_Object, ptrdiff_t);
937 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
938 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
939 static ptrdiff_t number_of_chars (const char *, int);
940 static void compute_stop_pos (struct it *);
941 static void compute_string_pos (struct text_pos *, struct text_pos,
942 Lisp_Object);
943 static int face_before_or_after_it_pos (struct it *, int);
944 static ptrdiff_t next_overlay_change (ptrdiff_t);
945 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
946 Lisp_Object, struct text_pos *, ptrdiff_t, int);
947 static int handle_single_display_spec (struct it *, Lisp_Object,
948 Lisp_Object, Lisp_Object,
949 struct text_pos *, ptrdiff_t, int, int);
950 static int underlying_face_id (struct it *);
951 static int in_ellipses_for_invisible_text_p (struct display_pos *,
952 struct window *);
953
954 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
955 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
956
957 #ifdef HAVE_WINDOW_SYSTEM
958
959 static void x_consider_frame_title (Lisp_Object);
960 static int tool_bar_lines_needed (struct frame *, int *);
961 static void update_tool_bar (struct frame *, int);
962 static void build_desired_tool_bar_string (struct frame *f);
963 static int redisplay_tool_bar (struct frame *);
964 static void display_tool_bar_line (struct it *, int);
965 static void notice_overwritten_cursor (struct window *,
966 enum glyph_row_area,
967 int, int, int, int);
968 static void append_stretch_glyph (struct it *, Lisp_Object,
969 int, int, int);
970
971
972 #endif /* HAVE_WINDOW_SYSTEM */
973
974 static void produce_special_glyphs (struct it *, enum display_element_type);
975 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
976 static int coords_in_mouse_face_p (struct window *, int, int);
977
978
979 \f
980 /***********************************************************************
981 Window display dimensions
982 ***********************************************************************/
983
984 /* Return the bottom boundary y-position for text lines in window W.
985 This is the first y position at which a line cannot start.
986 It is relative to the top of the window.
987
988 This is the height of W minus the height of a mode line, if any. */
989
990 int
991 window_text_bottom_y (struct window *w)
992 {
993 int height = WINDOW_TOTAL_HEIGHT (w);
994
995 if (WINDOW_WANTS_MODELINE_P (w))
996 height -= CURRENT_MODE_LINE_HEIGHT (w);
997 return height;
998 }
999
1000 /* Return the pixel width of display area AREA of window W. AREA < 0
1001 means return the total width of W, not including fringes to
1002 the left and right of the window. */
1003
1004 int
1005 window_box_width (struct window *w, int area)
1006 {
1007 int cols = XFASTINT (w->total_cols);
1008 int pixels = 0;
1009
1010 if (!w->pseudo_window_p)
1011 {
1012 cols -= WINDOW_SCROLL_BAR_COLS (w);
1013
1014 if (area == TEXT_AREA)
1015 {
1016 if (INTEGERP (w->left_margin_cols))
1017 cols -= XFASTINT (w->left_margin_cols);
1018 if (INTEGERP (w->right_margin_cols))
1019 cols -= XFASTINT (w->right_margin_cols);
1020 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1021 }
1022 else if (area == LEFT_MARGIN_AREA)
1023 {
1024 cols = (INTEGERP (w->left_margin_cols)
1025 ? XFASTINT (w->left_margin_cols) : 0);
1026 pixels = 0;
1027 }
1028 else if (area == RIGHT_MARGIN_AREA)
1029 {
1030 cols = (INTEGERP (w->right_margin_cols)
1031 ? XFASTINT (w->right_margin_cols) : 0);
1032 pixels = 0;
1033 }
1034 }
1035
1036 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1037 }
1038
1039
1040 /* Return the pixel height of the display area of window W, not
1041 including mode lines of W, if any. */
1042
1043 int
1044 window_box_height (struct window *w)
1045 {
1046 struct frame *f = XFRAME (w->frame);
1047 int height = WINDOW_TOTAL_HEIGHT (w);
1048
1049 eassert (height >= 0);
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. AREA < 0 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, int 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. AREA < 0 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, int 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. AREA < 0 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, int 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. AREA < 0 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, int 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. AREA < 0 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, int area, int *box_x, int *box_y,
1167 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
1184 /* Get the bounding box of the display area AREA of window W, without
1185 mode lines. AREA < 0 means the whole window, not including the
1186 left and right fringe 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 inline void
1193 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1194 int *bottom_right_x, int *bottom_right_y)
1195 {
1196 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1197 bottom_right_y);
1198 *bottom_right_x += *top_left_x;
1199 *bottom_right_y += *top_left_y;
1200 }
1201
1202
1203 \f
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 /* Subroutine of pos_visible_p below. Extracts a display string, if
1247 any, from the display spec given as its argument. */
1248 static Lisp_Object
1249 string_from_display_spec (Lisp_Object spec)
1250 {
1251 if (CONSP (spec))
1252 {
1253 while (CONSP (spec))
1254 {
1255 if (STRINGP (XCAR (spec)))
1256 return XCAR (spec);
1257 spec = XCDR (spec);
1258 }
1259 }
1260 else if (VECTORP (spec))
1261 {
1262 ptrdiff_t i;
1263
1264 for (i = 0; i < ASIZE (spec); i++)
1265 {
1266 if (STRINGP (AREF (spec, i)))
1267 return AREF (spec, i);
1268 }
1269 return Qnil;
1270 }
1271
1272 return spec;
1273 }
1274
1275
1276 /* Limit insanely large values of W->hscroll on frame F to the largest
1277 value that will still prevent first_visible_x and last_visible_x of
1278 'struct it' from overflowing an int. */
1279 static inline int
1280 window_hscroll_limited (struct window *w, struct frame *f)
1281 {
1282 ptrdiff_t window_hscroll = w->hscroll;
1283 int window_text_width = window_box_width (w, TEXT_AREA);
1284 int colwidth = FRAME_COLUMN_WIDTH (f);
1285
1286 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1287 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1288
1289 return window_hscroll;
1290 }
1291
1292 /* Return 1 if position CHARPOS is visible in window W.
1293 CHARPOS < 0 means return info about WINDOW_END position.
1294 If visible, set *X and *Y to pixel coordinates of top left corner.
1295 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1296 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1297
1298 int
1299 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1300 int *rtop, int *rbot, int *rowh, int *vpos)
1301 {
1302 struct it it;
1303 void *itdata = bidi_shelve_cache ();
1304 struct text_pos top;
1305 int visible_p = 0;
1306 struct buffer *old_buffer = NULL;
1307
1308 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1309 return visible_p;
1310
1311 if (XBUFFER (w->buffer) != current_buffer)
1312 {
1313 old_buffer = current_buffer;
1314 set_buffer_internal_1 (XBUFFER (w->buffer));
1315 }
1316
1317 SET_TEXT_POS_FROM_MARKER (top, w->start);
1318 /* Scrolling a minibuffer window via scroll bar when the echo area
1319 shows long text sometimes resets the minibuffer contents behind
1320 our backs. */
1321 if (CHARPOS (top) > ZV)
1322 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1323
1324 /* Compute exact mode line heights. */
1325 if (WINDOW_WANTS_MODELINE_P (w))
1326 current_mode_line_height
1327 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1328 BVAR (current_buffer, mode_line_format));
1329
1330 if (WINDOW_WANTS_HEADER_LINE_P (w))
1331 current_header_line_height
1332 = display_mode_line (w, HEADER_LINE_FACE_ID,
1333 BVAR (current_buffer, header_line_format));
1334
1335 start_display (&it, w, top);
1336 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1337 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1338
1339 if (charpos >= 0
1340 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1341 && IT_CHARPOS (it) >= charpos)
1342 /* When scanning backwards under bidi iteration, move_it_to
1343 stops at or _before_ CHARPOS, because it stops at or to
1344 the _right_ of the character at CHARPOS. */
1345 || (it.bidi_p && it.bidi_it.scan_dir == -1
1346 && IT_CHARPOS (it) <= charpos)))
1347 {
1348 /* We have reached CHARPOS, or passed it. How the call to
1349 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1350 or covered by a display property, move_it_to stops at the end
1351 of the invisible text, to the right of CHARPOS. (ii) If
1352 CHARPOS is in a display vector, move_it_to stops on its last
1353 glyph. */
1354 int top_x = it.current_x;
1355 int top_y = it.current_y;
1356 /* Calling line_bottom_y may change it.method, it.position, etc. */
1357 enum it_method it_method = it.method;
1358 int bottom_y = (last_height = 0, line_bottom_y (&it));
1359 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1360
1361 if (top_y < window_top_y)
1362 visible_p = bottom_y > window_top_y;
1363 else if (top_y < it.last_visible_y)
1364 visible_p = 1;
1365 if (bottom_y >= it.last_visible_y
1366 && it.bidi_p && it.bidi_it.scan_dir == -1
1367 && IT_CHARPOS (it) < charpos)
1368 {
1369 /* When the last line of the window is scanned backwards
1370 under bidi iteration, we could be duped into thinking
1371 that we have passed CHARPOS, when in fact move_it_to
1372 simply stopped short of CHARPOS because it reached
1373 last_visible_y. To see if that's what happened, we call
1374 move_it_to again with a slightly larger vertical limit,
1375 and see if it actually moved vertically; if it did, we
1376 didn't really reach CHARPOS, which is beyond window end. */
1377 struct it save_it = it;
1378 /* Why 10? because we don't know how many canonical lines
1379 will the height of the next line(s) be. So we guess. */
1380 int ten_more_lines =
1381 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1382
1383 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1384 MOVE_TO_POS | MOVE_TO_Y);
1385 if (it.current_y > top_y)
1386 visible_p = 0;
1387
1388 it = save_it;
1389 }
1390 if (visible_p)
1391 {
1392 if (it_method == GET_FROM_DISPLAY_VECTOR)
1393 {
1394 /* We stopped on the last glyph of a display vector.
1395 Try and recompute. Hack alert! */
1396 if (charpos < 2 || top.charpos >= charpos)
1397 top_x = it.glyph_row->x;
1398 else
1399 {
1400 struct it it2;
1401 start_display (&it2, w, top);
1402 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1403 get_next_display_element (&it2);
1404 PRODUCE_GLYPHS (&it2);
1405 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1406 || it2.current_x > it2.last_visible_x)
1407 top_x = it.glyph_row->x;
1408 else
1409 {
1410 top_x = it2.current_x;
1411 top_y = it2.current_y;
1412 }
1413 }
1414 }
1415 else if (IT_CHARPOS (it) != charpos)
1416 {
1417 Lisp_Object cpos = make_number (charpos);
1418 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1419 Lisp_Object string = string_from_display_spec (spec);
1420 int newline_in_string = 0;
1421
1422 if (STRINGP (string))
1423 {
1424 const char *s = SSDATA (string);
1425 const char *e = s + SBYTES (string);
1426 while (s < e)
1427 {
1428 if (*s++ == '\n')
1429 {
1430 newline_in_string = 1;
1431 break;
1432 }
1433 }
1434 }
1435 /* The tricky code below is needed because there's a
1436 discrepancy between move_it_to and how we set cursor
1437 when the display line ends in a newline from a
1438 display string. move_it_to will stop _after_ such
1439 display strings, whereas set_cursor_from_row
1440 conspires with cursor_row_p to place the cursor on
1441 the first glyph produced from the display string. */
1442
1443 /* We have overshoot PT because it is covered by a
1444 display property whose value is a string. If the
1445 string includes embedded newlines, we are also in the
1446 wrong display line. Backtrack to the correct line,
1447 where the display string begins. */
1448 if (newline_in_string)
1449 {
1450 Lisp_Object startpos, endpos;
1451 EMACS_INT start, end;
1452 struct it it3;
1453 int it3_moved;
1454
1455 /* Find the first and the last buffer positions
1456 covered by the display string. */
1457 endpos =
1458 Fnext_single_char_property_change (cpos, Qdisplay,
1459 Qnil, Qnil);
1460 startpos =
1461 Fprevious_single_char_property_change (endpos, Qdisplay,
1462 Qnil, Qnil);
1463 start = XFASTINT (startpos);
1464 end = XFASTINT (endpos);
1465 /* Move to the last buffer position before the
1466 display property. */
1467 start_display (&it3, w, top);
1468 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1469 /* Move forward one more line if the position before
1470 the display string is a newline or if it is the
1471 rightmost character on a line that is
1472 continued or word-wrapped. */
1473 if (it3.method == GET_FROM_BUFFER
1474 && it3.c == '\n')
1475 move_it_by_lines (&it3, 1);
1476 else if (move_it_in_display_line_to (&it3, -1,
1477 it3.current_x
1478 + it3.pixel_width,
1479 MOVE_TO_X)
1480 == MOVE_LINE_CONTINUED)
1481 {
1482 move_it_by_lines (&it3, 1);
1483 /* When we are under word-wrap, the #$@%!
1484 move_it_by_lines moves 2 lines, so we need to
1485 fix that up. */
1486 if (it3.line_wrap == WORD_WRAP)
1487 move_it_by_lines (&it3, -1);
1488 }
1489
1490 /* Record the vertical coordinate of the display
1491 line where we wound up. */
1492 top_y = it3.current_y;
1493 if (it3.bidi_p)
1494 {
1495 /* When characters are reordered for display,
1496 the character displayed to the left of the
1497 display string could be _after_ the display
1498 property in the logical order. Use the
1499 smallest vertical position of these two. */
1500 start_display (&it3, w, top);
1501 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1502 if (it3.current_y < top_y)
1503 top_y = it3.current_y;
1504 }
1505 /* Move from the top of the window to the beginning
1506 of the display line where the display string
1507 begins. */
1508 start_display (&it3, w, top);
1509 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1510 /* If it3_moved stays zero after the 'while' loop
1511 below, that means we already were at a newline
1512 before the loop (e.g., the display string begins
1513 with a newline), so we don't need to (and cannot)
1514 inspect the glyphs of it3.glyph_row, because
1515 PRODUCE_GLYPHS will not produce anything for a
1516 newline, and thus it3.glyph_row stays at its
1517 stale content it got at top of the window. */
1518 it3_moved = 0;
1519 /* Finally, advance the iterator until we hit the
1520 first display element whose character position is
1521 CHARPOS, or until the first newline from the
1522 display string, which signals the end of the
1523 display line. */
1524 while (get_next_display_element (&it3))
1525 {
1526 PRODUCE_GLYPHS (&it3);
1527 if (IT_CHARPOS (it3) == charpos
1528 || ITERATOR_AT_END_OF_LINE_P (&it3))
1529 break;
1530 it3_moved = 1;
1531 set_iterator_to_next (&it3, 0);
1532 }
1533 top_x = it3.current_x - it3.pixel_width;
1534 /* Normally, we would exit the above loop because we
1535 found the display element whose character
1536 position is CHARPOS. For the contingency that we
1537 didn't, and stopped at the first newline from the
1538 display string, move back over the glyphs
1539 produced from the string, until we find the
1540 rightmost glyph not from the string. */
1541 if (it3_moved
1542 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1543 {
1544 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1545 + it3.glyph_row->used[TEXT_AREA];
1546
1547 while (EQ ((g - 1)->object, string))
1548 {
1549 --g;
1550 top_x -= g->pixel_width;
1551 }
1552 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1553 + it3.glyph_row->used[TEXT_AREA]);
1554 }
1555 }
1556 }
1557
1558 *x = top_x;
1559 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1560 *rtop = max (0, window_top_y - top_y);
1561 *rbot = max (0, bottom_y - it.last_visible_y);
1562 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1563 - max (top_y, window_top_y)));
1564 *vpos = it.vpos;
1565 }
1566 }
1567 else
1568 {
1569 /* We were asked to provide info about WINDOW_END. */
1570 struct it it2;
1571 void *it2data = NULL;
1572
1573 SAVE_IT (it2, it, it2data);
1574 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1575 move_it_by_lines (&it, 1);
1576 if (charpos < IT_CHARPOS (it)
1577 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1578 {
1579 visible_p = 1;
1580 RESTORE_IT (&it2, &it2, it2data);
1581 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1582 *x = it2.current_x;
1583 *y = it2.current_y + it2.max_ascent - it2.ascent;
1584 *rtop = max (0, -it2.current_y);
1585 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1586 - it.last_visible_y));
1587 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1588 it.last_visible_y)
1589 - max (it2.current_y,
1590 WINDOW_HEADER_LINE_HEIGHT (w))));
1591 *vpos = it2.vpos;
1592 }
1593 else
1594 bidi_unshelve_cache (it2data, 1);
1595 }
1596 bidi_unshelve_cache (itdata, 0);
1597
1598 if (old_buffer)
1599 set_buffer_internal_1 (old_buffer);
1600
1601 current_header_line_height = current_mode_line_height = -1;
1602
1603 if (visible_p && w->hscroll > 0)
1604 *x -=
1605 window_hscroll_limited (w, WINDOW_XFRAME (w))
1606 * WINDOW_FRAME_COLUMN_WIDTH (w);
1607
1608 #if 0
1609 /* Debugging code. */
1610 if (visible_p)
1611 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1612 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1613 else
1614 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1615 #endif
1616
1617 return visible_p;
1618 }
1619
1620
1621 /* Return the next character from STR. Return in *LEN the length of
1622 the character. This is like STRING_CHAR_AND_LENGTH but never
1623 returns an invalid character. If we find one, we return a `?', but
1624 with the length of the invalid character. */
1625
1626 static inline int
1627 string_char_and_length (const unsigned char *str, int *len)
1628 {
1629 int c;
1630
1631 c = STRING_CHAR_AND_LENGTH (str, *len);
1632 if (!CHAR_VALID_P (c))
1633 /* We may not change the length here because other places in Emacs
1634 don't use this function, i.e. they silently accept invalid
1635 characters. */
1636 c = '?';
1637
1638 return c;
1639 }
1640
1641
1642
1643 /* Given a position POS containing a valid character and byte position
1644 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1645
1646 static struct text_pos
1647 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1648 {
1649 eassert (STRINGP (string) && nchars >= 0);
1650
1651 if (STRING_MULTIBYTE (string))
1652 {
1653 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1654 int len;
1655
1656 while (nchars--)
1657 {
1658 string_char_and_length (p, &len);
1659 p += len;
1660 CHARPOS (pos) += 1;
1661 BYTEPOS (pos) += len;
1662 }
1663 }
1664 else
1665 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1666
1667 return pos;
1668 }
1669
1670
1671 /* Value is the text position, i.e. character and byte position,
1672 for character position CHARPOS in STRING. */
1673
1674 static inline struct text_pos
1675 string_pos (ptrdiff_t charpos, Lisp_Object string)
1676 {
1677 struct text_pos pos;
1678 eassert (STRINGP (string));
1679 eassert (charpos >= 0);
1680 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1681 return pos;
1682 }
1683
1684
1685 /* Value is a text position, i.e. character and byte position, for
1686 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1687 means recognize multibyte characters. */
1688
1689 static struct text_pos
1690 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1691 {
1692 struct text_pos pos;
1693
1694 eassert (s != NULL);
1695 eassert (charpos >= 0);
1696
1697 if (multibyte_p)
1698 {
1699 int len;
1700
1701 SET_TEXT_POS (pos, 0, 0);
1702 while (charpos--)
1703 {
1704 string_char_and_length ((const unsigned char *) s, &len);
1705 s += len;
1706 CHARPOS (pos) += 1;
1707 BYTEPOS (pos) += len;
1708 }
1709 }
1710 else
1711 SET_TEXT_POS (pos, charpos, charpos);
1712
1713 return pos;
1714 }
1715
1716
1717 /* Value is the number of characters in C string S. MULTIBYTE_P
1718 non-zero means recognize multibyte characters. */
1719
1720 static ptrdiff_t
1721 number_of_chars (const char *s, int multibyte_p)
1722 {
1723 ptrdiff_t nchars;
1724
1725 if (multibyte_p)
1726 {
1727 ptrdiff_t rest = strlen (s);
1728 int len;
1729 const unsigned char *p = (const unsigned char *) s;
1730
1731 for (nchars = 0; rest > 0; ++nchars)
1732 {
1733 string_char_and_length (p, &len);
1734 rest -= len, p += len;
1735 }
1736 }
1737 else
1738 nchars = strlen (s);
1739
1740 return nchars;
1741 }
1742
1743
1744 /* Compute byte position NEWPOS->bytepos corresponding to
1745 NEWPOS->charpos. POS is a known position in string STRING.
1746 NEWPOS->charpos must be >= POS.charpos. */
1747
1748 static void
1749 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1750 {
1751 eassert (STRINGP (string));
1752 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1753
1754 if (STRING_MULTIBYTE (string))
1755 *newpos = string_pos_nchars_ahead (pos, string,
1756 CHARPOS (*newpos) - CHARPOS (pos));
1757 else
1758 BYTEPOS (*newpos) = CHARPOS (*newpos);
1759 }
1760
1761 /* EXPORT:
1762 Return an estimation of the pixel height of mode or header lines on
1763 frame F. FACE_ID specifies what line's height to estimate. */
1764
1765 int
1766 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1767 {
1768 #ifdef HAVE_WINDOW_SYSTEM
1769 if (FRAME_WINDOW_P (f))
1770 {
1771 int height = FONT_HEIGHT (FRAME_FONT (f));
1772
1773 /* This function is called so early when Emacs starts that the face
1774 cache and mode line face are not yet initialized. */
1775 if (FRAME_FACE_CACHE (f))
1776 {
1777 struct face *face = FACE_FROM_ID (f, face_id);
1778 if (face)
1779 {
1780 if (face->font)
1781 height = FONT_HEIGHT (face->font);
1782 if (face->box_line_width > 0)
1783 height += 2 * face->box_line_width;
1784 }
1785 }
1786
1787 return height;
1788 }
1789 #endif
1790
1791 return 1;
1792 }
1793
1794 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1795 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1796 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1797 not force the value into range. */
1798
1799 void
1800 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1801 int *x, int *y, NativeRectangle *bounds, int noclip)
1802 {
1803
1804 #ifdef HAVE_WINDOW_SYSTEM
1805 if (FRAME_WINDOW_P (f))
1806 {
1807 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1808 even for negative values. */
1809 if (pix_x < 0)
1810 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1811 if (pix_y < 0)
1812 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1813
1814 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1815 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1816
1817 if (bounds)
1818 STORE_NATIVE_RECT (*bounds,
1819 FRAME_COL_TO_PIXEL_X (f, pix_x),
1820 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1821 FRAME_COLUMN_WIDTH (f) - 1,
1822 FRAME_LINE_HEIGHT (f) - 1);
1823
1824 if (!noclip)
1825 {
1826 if (pix_x < 0)
1827 pix_x = 0;
1828 else if (pix_x > FRAME_TOTAL_COLS (f))
1829 pix_x = FRAME_TOTAL_COLS (f);
1830
1831 if (pix_y < 0)
1832 pix_y = 0;
1833 else if (pix_y > FRAME_LINES (f))
1834 pix_y = FRAME_LINES (f);
1835 }
1836 }
1837 #endif
1838
1839 *x = pix_x;
1840 *y = pix_y;
1841 }
1842
1843
1844 /* Find the glyph under window-relative coordinates X/Y in window W.
1845 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1846 strings. Return in *HPOS and *VPOS the row and column number of
1847 the glyph found. Return in *AREA the glyph area containing X.
1848 Value is a pointer to the glyph found or null if X/Y is not on
1849 text, or we can't tell because W's current matrix is not up to
1850 date. */
1851
1852 static
1853 struct glyph *
1854 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1855 int *dx, int *dy, int *area)
1856 {
1857 struct glyph *glyph, *end;
1858 struct glyph_row *row = NULL;
1859 int x0, i;
1860
1861 /* Find row containing Y. Give up if some row is not enabled. */
1862 for (i = 0; i < w->current_matrix->nrows; ++i)
1863 {
1864 row = MATRIX_ROW (w->current_matrix, i);
1865 if (!row->enabled_p)
1866 return NULL;
1867 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1868 break;
1869 }
1870
1871 *vpos = i;
1872 *hpos = 0;
1873
1874 /* Give up if Y is not in the window. */
1875 if (i == w->current_matrix->nrows)
1876 return NULL;
1877
1878 /* Get the glyph area containing X. */
1879 if (w->pseudo_window_p)
1880 {
1881 *area = TEXT_AREA;
1882 x0 = 0;
1883 }
1884 else
1885 {
1886 if (x < window_box_left_offset (w, TEXT_AREA))
1887 {
1888 *area = LEFT_MARGIN_AREA;
1889 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1890 }
1891 else if (x < window_box_right_offset (w, TEXT_AREA))
1892 {
1893 *area = TEXT_AREA;
1894 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1895 }
1896 else
1897 {
1898 *area = RIGHT_MARGIN_AREA;
1899 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1900 }
1901 }
1902
1903 /* Find glyph containing X. */
1904 glyph = row->glyphs[*area];
1905 end = glyph + row->used[*area];
1906 x -= x0;
1907 while (glyph < end && x >= glyph->pixel_width)
1908 {
1909 x -= glyph->pixel_width;
1910 ++glyph;
1911 }
1912
1913 if (glyph == end)
1914 return NULL;
1915
1916 if (dx)
1917 {
1918 *dx = x;
1919 *dy = y - (row->y + row->ascent - glyph->ascent);
1920 }
1921
1922 *hpos = glyph - row->glyphs[*area];
1923 return glyph;
1924 }
1925
1926 /* Convert frame-relative x/y to coordinates relative to window W.
1927 Takes pseudo-windows into account. */
1928
1929 static void
1930 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1931 {
1932 if (w->pseudo_window_p)
1933 {
1934 /* A pseudo-window is always full-width, and starts at the
1935 left edge of the frame, plus a frame border. */
1936 struct frame *f = XFRAME (w->frame);
1937 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1938 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1939 }
1940 else
1941 {
1942 *x -= WINDOW_LEFT_EDGE_X (w);
1943 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1944 }
1945 }
1946
1947 #ifdef HAVE_WINDOW_SYSTEM
1948
1949 /* EXPORT:
1950 Return in RECTS[] at most N clipping rectangles for glyph string S.
1951 Return the number of stored rectangles. */
1952
1953 int
1954 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1955 {
1956 XRectangle r;
1957
1958 if (n <= 0)
1959 return 0;
1960
1961 if (s->row->full_width_p)
1962 {
1963 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1964 r.x = WINDOW_LEFT_EDGE_X (s->w);
1965 r.width = WINDOW_TOTAL_WIDTH (s->w);
1966
1967 /* Unless displaying a mode or menu bar line, which are always
1968 fully visible, clip to the visible part of the row. */
1969 if (s->w->pseudo_window_p)
1970 r.height = s->row->visible_height;
1971 else
1972 r.height = s->height;
1973 }
1974 else
1975 {
1976 /* This is a text line that may be partially visible. */
1977 r.x = window_box_left (s->w, s->area);
1978 r.width = window_box_width (s->w, s->area);
1979 r.height = s->row->visible_height;
1980 }
1981
1982 if (s->clip_head)
1983 if (r.x < s->clip_head->x)
1984 {
1985 if (r.width >= s->clip_head->x - r.x)
1986 r.width -= s->clip_head->x - r.x;
1987 else
1988 r.width = 0;
1989 r.x = s->clip_head->x;
1990 }
1991 if (s->clip_tail)
1992 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1993 {
1994 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1995 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1996 else
1997 r.width = 0;
1998 }
1999
2000 /* If S draws overlapping rows, it's sufficient to use the top and
2001 bottom of the window for clipping because this glyph string
2002 intentionally draws over other lines. */
2003 if (s->for_overlaps)
2004 {
2005 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2006 r.height = window_text_bottom_y (s->w) - r.y;
2007
2008 /* Alas, the above simple strategy does not work for the
2009 environments with anti-aliased text: if the same text is
2010 drawn onto the same place multiple times, it gets thicker.
2011 If the overlap we are processing is for the erased cursor, we
2012 take the intersection with the rectangle of the cursor. */
2013 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2014 {
2015 XRectangle rc, r_save = r;
2016
2017 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2018 rc.y = s->w->phys_cursor.y;
2019 rc.width = s->w->phys_cursor_width;
2020 rc.height = s->w->phys_cursor_height;
2021
2022 x_intersect_rectangles (&r_save, &rc, &r);
2023 }
2024 }
2025 else
2026 {
2027 /* Don't use S->y for clipping because it doesn't take partially
2028 visible lines into account. For example, it can be negative for
2029 partially visible lines at the top of a window. */
2030 if (!s->row->full_width_p
2031 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2032 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2033 else
2034 r.y = max (0, s->row->y);
2035 }
2036
2037 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2038
2039 /* If drawing the cursor, don't let glyph draw outside its
2040 advertised boundaries. Cleartype does this under some circumstances. */
2041 if (s->hl == DRAW_CURSOR)
2042 {
2043 struct glyph *glyph = s->first_glyph;
2044 int height, max_y;
2045
2046 if (s->x > r.x)
2047 {
2048 r.width -= s->x - r.x;
2049 r.x = s->x;
2050 }
2051 r.width = min (r.width, glyph->pixel_width);
2052
2053 /* If r.y is below window bottom, ensure that we still see a cursor. */
2054 height = min (glyph->ascent + glyph->descent,
2055 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2056 max_y = window_text_bottom_y (s->w) - height;
2057 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2058 if (s->ybase - glyph->ascent > max_y)
2059 {
2060 r.y = max_y;
2061 r.height = height;
2062 }
2063 else
2064 {
2065 /* Don't draw cursor glyph taller than our actual glyph. */
2066 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2067 if (height < r.height)
2068 {
2069 max_y = r.y + r.height;
2070 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2071 r.height = min (max_y - r.y, height);
2072 }
2073 }
2074 }
2075
2076 if (s->row->clip)
2077 {
2078 XRectangle r_save = r;
2079
2080 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2081 r.width = 0;
2082 }
2083
2084 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2085 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2086 {
2087 #ifdef CONVERT_FROM_XRECT
2088 CONVERT_FROM_XRECT (r, *rects);
2089 #else
2090 *rects = r;
2091 #endif
2092 return 1;
2093 }
2094 else
2095 {
2096 /* If we are processing overlapping and allowed to return
2097 multiple clipping rectangles, we exclude the row of the glyph
2098 string from the clipping rectangle. This is to avoid drawing
2099 the same text on the environment with anti-aliasing. */
2100 #ifdef CONVERT_FROM_XRECT
2101 XRectangle rs[2];
2102 #else
2103 XRectangle *rs = rects;
2104 #endif
2105 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2106
2107 if (s->for_overlaps & OVERLAPS_PRED)
2108 {
2109 rs[i] = r;
2110 if (r.y + r.height > row_y)
2111 {
2112 if (r.y < row_y)
2113 rs[i].height = row_y - r.y;
2114 else
2115 rs[i].height = 0;
2116 }
2117 i++;
2118 }
2119 if (s->for_overlaps & OVERLAPS_SUCC)
2120 {
2121 rs[i] = r;
2122 if (r.y < row_y + s->row->visible_height)
2123 {
2124 if (r.y + r.height > row_y + s->row->visible_height)
2125 {
2126 rs[i].y = row_y + s->row->visible_height;
2127 rs[i].height = r.y + r.height - rs[i].y;
2128 }
2129 else
2130 rs[i].height = 0;
2131 }
2132 i++;
2133 }
2134
2135 n = i;
2136 #ifdef CONVERT_FROM_XRECT
2137 for (i = 0; i < n; i++)
2138 CONVERT_FROM_XRECT (rs[i], rects[i]);
2139 #endif
2140 return n;
2141 }
2142 }
2143
2144 /* EXPORT:
2145 Return in *NR the clipping rectangle for glyph string S. */
2146
2147 void
2148 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2149 {
2150 get_glyph_string_clip_rects (s, nr, 1);
2151 }
2152
2153
2154 /* EXPORT:
2155 Return the position and height of the phys cursor in window W.
2156 Set w->phys_cursor_width to width of phys cursor.
2157 */
2158
2159 void
2160 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2161 struct glyph *glyph, int *xp, int *yp, int *heightp)
2162 {
2163 struct frame *f = XFRAME (WINDOW_FRAME (w));
2164 int x, y, wd, h, h0, y0;
2165
2166 /* Compute the width of the rectangle to draw. If on a stretch
2167 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2168 rectangle as wide as the glyph, but use a canonical character
2169 width instead. */
2170 wd = glyph->pixel_width - 1;
2171 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2172 wd++; /* Why? */
2173 #endif
2174
2175 x = w->phys_cursor.x;
2176 if (x < 0)
2177 {
2178 wd += x;
2179 x = 0;
2180 }
2181
2182 if (glyph->type == STRETCH_GLYPH
2183 && !x_stretch_cursor_p)
2184 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2185 w->phys_cursor_width = wd;
2186
2187 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2188
2189 /* If y is below window bottom, ensure that we still see a cursor. */
2190 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2191
2192 h = max (h0, glyph->ascent + glyph->descent);
2193 h0 = min (h0, glyph->ascent + glyph->descent);
2194
2195 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2196 if (y < y0)
2197 {
2198 h = max (h - (y0 - y) + 1, h0);
2199 y = y0 - 1;
2200 }
2201 else
2202 {
2203 y0 = window_text_bottom_y (w) - h0;
2204 if (y > y0)
2205 {
2206 h += y - y0;
2207 y = y0;
2208 }
2209 }
2210
2211 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2212 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2213 *heightp = h;
2214 }
2215
2216 /*
2217 * Remember which glyph the mouse is over.
2218 */
2219
2220 void
2221 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2222 {
2223 Lisp_Object window;
2224 struct window *w;
2225 struct glyph_row *r, *gr, *end_row;
2226 enum window_part part;
2227 enum glyph_row_area area;
2228 int x, y, width, height;
2229
2230 /* Try to determine frame pixel position and size of the glyph under
2231 frame pixel coordinates X/Y on frame F. */
2232
2233 if (!f->glyphs_initialized_p
2234 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2235 NILP (window)))
2236 {
2237 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2238 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2239 goto virtual_glyph;
2240 }
2241
2242 w = XWINDOW (window);
2243 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2244 height = WINDOW_FRAME_LINE_HEIGHT (w);
2245
2246 x = window_relative_x_coord (w, part, gx);
2247 y = gy - WINDOW_TOP_EDGE_Y (w);
2248
2249 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2250 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2251
2252 if (w->pseudo_window_p)
2253 {
2254 area = TEXT_AREA;
2255 part = ON_MODE_LINE; /* Don't adjust margin. */
2256 goto text_glyph;
2257 }
2258
2259 switch (part)
2260 {
2261 case ON_LEFT_MARGIN:
2262 area = LEFT_MARGIN_AREA;
2263 goto text_glyph;
2264
2265 case ON_RIGHT_MARGIN:
2266 area = RIGHT_MARGIN_AREA;
2267 goto text_glyph;
2268
2269 case ON_HEADER_LINE:
2270 case ON_MODE_LINE:
2271 gr = (part == ON_HEADER_LINE
2272 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2273 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2274 gy = gr->y;
2275 area = TEXT_AREA;
2276 goto text_glyph_row_found;
2277
2278 case ON_TEXT:
2279 area = TEXT_AREA;
2280
2281 text_glyph:
2282 gr = 0; gy = 0;
2283 for (; r <= end_row && r->enabled_p; ++r)
2284 if (r->y + r->height > y)
2285 {
2286 gr = r; gy = r->y;
2287 break;
2288 }
2289
2290 text_glyph_row_found:
2291 if (gr && gy <= y)
2292 {
2293 struct glyph *g = gr->glyphs[area];
2294 struct glyph *end = g + gr->used[area];
2295
2296 height = gr->height;
2297 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2298 if (gx + g->pixel_width > x)
2299 break;
2300
2301 if (g < end)
2302 {
2303 if (g->type == IMAGE_GLYPH)
2304 {
2305 /* Don't remember when mouse is over image, as
2306 image may have hot-spots. */
2307 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2308 return;
2309 }
2310 width = g->pixel_width;
2311 }
2312 else
2313 {
2314 /* Use nominal char spacing at end of line. */
2315 x -= gx;
2316 gx += (x / width) * width;
2317 }
2318
2319 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2320 gx += window_box_left_offset (w, area);
2321 }
2322 else
2323 {
2324 /* Use nominal line height at end of window. */
2325 gx = (x / width) * width;
2326 y -= gy;
2327 gy += (y / height) * height;
2328 }
2329 break;
2330
2331 case ON_LEFT_FRINGE:
2332 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2333 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2334 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2335 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2336 goto row_glyph;
2337
2338 case ON_RIGHT_FRINGE:
2339 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2340 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2341 : window_box_right_offset (w, TEXT_AREA));
2342 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2343 goto row_glyph;
2344
2345 case ON_SCROLL_BAR:
2346 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2347 ? 0
2348 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2349 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2350 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2351 : 0)));
2352 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2353
2354 row_glyph:
2355 gr = 0, gy = 0;
2356 for (; r <= end_row && r->enabled_p; ++r)
2357 if (r->y + r->height > y)
2358 {
2359 gr = r; gy = r->y;
2360 break;
2361 }
2362
2363 if (gr && gy <= y)
2364 height = gr->height;
2365 else
2366 {
2367 /* Use nominal line height at end of window. */
2368 y -= gy;
2369 gy += (y / height) * height;
2370 }
2371 break;
2372
2373 default:
2374 ;
2375 virtual_glyph:
2376 /* If there is no glyph under the mouse, then we divide the screen
2377 into a grid of the smallest glyph in the frame, and use that
2378 as our "glyph". */
2379
2380 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2381 round down even for negative values. */
2382 if (gx < 0)
2383 gx -= width - 1;
2384 if (gy < 0)
2385 gy -= height - 1;
2386
2387 gx = (gx / width) * width;
2388 gy = (gy / height) * height;
2389
2390 goto store_rect;
2391 }
2392
2393 gx += WINDOW_LEFT_EDGE_X (w);
2394 gy += WINDOW_TOP_EDGE_Y (w);
2395
2396 store_rect:
2397 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2398
2399 /* Visible feedback for debugging. */
2400 #if 0
2401 #if HAVE_X_WINDOWS
2402 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2403 f->output_data.x->normal_gc,
2404 gx, gy, width, height);
2405 #endif
2406 #endif
2407 }
2408
2409
2410 #endif /* HAVE_WINDOW_SYSTEM */
2411
2412 \f
2413 /***********************************************************************
2414 Lisp form evaluation
2415 ***********************************************************************/
2416
2417 /* Error handler for safe_eval and safe_call. */
2418
2419 static Lisp_Object
2420 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2421 {
2422 add_to_log ("Error during redisplay: %S signaled %S",
2423 Flist (nargs, args), arg);
2424 return Qnil;
2425 }
2426
2427 /* Call function FUNC with the rest of NARGS - 1 arguments
2428 following. Return the result, or nil if something went
2429 wrong. Prevent redisplay during the evaluation. */
2430
2431 Lisp_Object
2432 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2433 {
2434 Lisp_Object val;
2435
2436 if (inhibit_eval_during_redisplay)
2437 val = Qnil;
2438 else
2439 {
2440 va_list ap;
2441 ptrdiff_t i;
2442 ptrdiff_t count = SPECPDL_INDEX ();
2443 struct gcpro gcpro1;
2444 Lisp_Object *args = alloca (nargs * word_size);
2445
2446 args[0] = func;
2447 va_start (ap, func);
2448 for (i = 1; i < nargs; i++)
2449 args[i] = va_arg (ap, Lisp_Object);
2450 va_end (ap);
2451
2452 GCPRO1 (args[0]);
2453 gcpro1.nvars = nargs;
2454 specbind (Qinhibit_redisplay, Qt);
2455 /* Use Qt to ensure debugger does not run,
2456 so there is no possibility of wanting to redisplay. */
2457 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2458 safe_eval_handler);
2459 UNGCPRO;
2460 val = unbind_to (count, val);
2461 }
2462
2463 return val;
2464 }
2465
2466
2467 /* Call function FN with one argument ARG.
2468 Return the result, or nil if something went wrong. */
2469
2470 Lisp_Object
2471 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2472 {
2473 return safe_call (2, fn, arg);
2474 }
2475
2476 static Lisp_Object Qeval;
2477
2478 Lisp_Object
2479 safe_eval (Lisp_Object sexpr)
2480 {
2481 return safe_call1 (Qeval, sexpr);
2482 }
2483
2484 /* Call function FN with two arguments ARG1 and ARG2.
2485 Return the result, or nil if something went wrong. */
2486
2487 Lisp_Object
2488 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2489 {
2490 return safe_call (3, fn, arg1, arg2);
2491 }
2492
2493
2494 \f
2495 /***********************************************************************
2496 Debugging
2497 ***********************************************************************/
2498
2499 #if 0
2500
2501 /* Define CHECK_IT to perform sanity checks on iterators.
2502 This is for debugging. It is too slow to do unconditionally. */
2503
2504 static void
2505 check_it (struct it *it)
2506 {
2507 if (it->method == GET_FROM_STRING)
2508 {
2509 eassert (STRINGP (it->string));
2510 eassert (IT_STRING_CHARPOS (*it) >= 0);
2511 }
2512 else
2513 {
2514 eassert (IT_STRING_CHARPOS (*it) < 0);
2515 if (it->method == GET_FROM_BUFFER)
2516 {
2517 /* Check that character and byte positions agree. */
2518 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2519 }
2520 }
2521
2522 if (it->dpvec)
2523 eassert (it->current.dpvec_index >= 0);
2524 else
2525 eassert (it->current.dpvec_index < 0);
2526 }
2527
2528 #define CHECK_IT(IT) check_it ((IT))
2529
2530 #else /* not 0 */
2531
2532 #define CHECK_IT(IT) (void) 0
2533
2534 #endif /* not 0 */
2535
2536
2537 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2538
2539 /* Check that the window end of window W is what we expect it
2540 to be---the last row in the current matrix displaying text. */
2541
2542 static void
2543 check_window_end (struct window *w)
2544 {
2545 if (!MINI_WINDOW_P (w)
2546 && !NILP (w->window_end_valid))
2547 {
2548 struct glyph_row *row;
2549 eassert ((row = MATRIX_ROW (w->current_matrix,
2550 XFASTINT (w->window_end_vpos)),
2551 !row->enabled_p
2552 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2553 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2554 }
2555 }
2556
2557 #define CHECK_WINDOW_END(W) check_window_end ((W))
2558
2559 #else
2560
2561 #define CHECK_WINDOW_END(W) (void) 0
2562
2563 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2564
2565
2566 \f
2567 /***********************************************************************
2568 Iterator initialization
2569 ***********************************************************************/
2570
2571 /* Initialize IT for displaying current_buffer in window W, starting
2572 at character position CHARPOS. CHARPOS < 0 means that no buffer
2573 position is specified which is useful when the iterator is assigned
2574 a position later. BYTEPOS is the byte position corresponding to
2575 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2576
2577 If ROW is not null, calls to produce_glyphs with IT as parameter
2578 will produce glyphs in that row.
2579
2580 BASE_FACE_ID is the id of a base face to use. It must be one of
2581 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2582 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2583 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2584
2585 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2586 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2587 will be initialized to use the corresponding mode line glyph row of
2588 the desired matrix of W. */
2589
2590 void
2591 init_iterator (struct it *it, struct window *w,
2592 ptrdiff_t charpos, ptrdiff_t bytepos,
2593 struct glyph_row *row, enum face_id base_face_id)
2594 {
2595 int highlight_region_p;
2596 enum face_id remapped_base_face_id = base_face_id;
2597
2598 /* Some precondition checks. */
2599 eassert (w != NULL && it != NULL);
2600 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2601 && charpos <= ZV));
2602
2603 /* If face attributes have been changed since the last redisplay,
2604 free realized faces now because they depend on face definitions
2605 that might have changed. Don't free faces while there might be
2606 desired matrices pending which reference these faces. */
2607 if (face_change_count && !inhibit_free_realized_faces)
2608 {
2609 face_change_count = 0;
2610 free_all_realized_faces (Qnil);
2611 }
2612
2613 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2614 if (! NILP (Vface_remapping_alist))
2615 remapped_base_face_id
2616 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2617
2618 /* Use one of the mode line rows of W's desired matrix if
2619 appropriate. */
2620 if (row == NULL)
2621 {
2622 if (base_face_id == MODE_LINE_FACE_ID
2623 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2624 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2625 else if (base_face_id == HEADER_LINE_FACE_ID)
2626 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2627 }
2628
2629 /* Clear IT. */
2630 memset (it, 0, sizeof *it);
2631 it->current.overlay_string_index = -1;
2632 it->current.dpvec_index = -1;
2633 it->base_face_id = remapped_base_face_id;
2634 it->string = Qnil;
2635 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2636 it->paragraph_embedding = L2R;
2637 it->bidi_it.string.lstring = Qnil;
2638 it->bidi_it.string.s = NULL;
2639 it->bidi_it.string.bufpos = 0;
2640
2641 /* The window in which we iterate over current_buffer: */
2642 XSETWINDOW (it->window, w);
2643 it->w = w;
2644 it->f = XFRAME (w->frame);
2645
2646 it->cmp_it.id = -1;
2647
2648 /* Extra space between lines (on window systems only). */
2649 if (base_face_id == DEFAULT_FACE_ID
2650 && FRAME_WINDOW_P (it->f))
2651 {
2652 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2653 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2654 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2655 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2656 * FRAME_LINE_HEIGHT (it->f));
2657 else if (it->f->extra_line_spacing > 0)
2658 it->extra_line_spacing = it->f->extra_line_spacing;
2659 it->max_extra_line_spacing = 0;
2660 }
2661
2662 /* If realized faces have been removed, e.g. because of face
2663 attribute changes of named faces, recompute them. When running
2664 in batch mode, the face cache of the initial frame is null. If
2665 we happen to get called, make a dummy face cache. */
2666 if (FRAME_FACE_CACHE (it->f) == NULL)
2667 init_frame_faces (it->f);
2668 if (FRAME_FACE_CACHE (it->f)->used == 0)
2669 recompute_basic_faces (it->f);
2670
2671 /* Current value of the `slice', `space-width', and 'height' properties. */
2672 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2673 it->space_width = Qnil;
2674 it->font_height = Qnil;
2675 it->override_ascent = -1;
2676
2677 /* Are control characters displayed as `^C'? */
2678 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2679
2680 /* -1 means everything between a CR and the following line end
2681 is invisible. >0 means lines indented more than this value are
2682 invisible. */
2683 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2684 ? (clip_to_bounds
2685 (-1, XINT (BVAR (current_buffer, selective_display)),
2686 PTRDIFF_MAX))
2687 : (!NILP (BVAR (current_buffer, selective_display))
2688 ? -1 : 0));
2689 it->selective_display_ellipsis_p
2690 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2691
2692 /* Display table to use. */
2693 it->dp = window_display_table (w);
2694
2695 /* Are multibyte characters enabled in current_buffer? */
2696 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2697
2698 /* Non-zero if we should highlight the region. */
2699 highlight_region_p
2700 = (!NILP (Vtransient_mark_mode)
2701 && !NILP (BVAR (current_buffer, mark_active))
2702 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2703
2704 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2705 start and end of a visible region in window IT->w. Set both to
2706 -1 to indicate no region. */
2707 if (highlight_region_p
2708 /* Maybe highlight only in selected window. */
2709 && (/* Either show region everywhere. */
2710 highlight_nonselected_windows
2711 /* Or show region in the selected window. */
2712 || w == XWINDOW (selected_window)
2713 /* Or show the region if we are in the mini-buffer and W is
2714 the window the mini-buffer refers to. */
2715 || (MINI_WINDOW_P (XWINDOW (selected_window))
2716 && WINDOWP (minibuf_selected_window)
2717 && w == XWINDOW (minibuf_selected_window))))
2718 {
2719 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2720 it->region_beg_charpos = min (PT, markpos);
2721 it->region_end_charpos = max (PT, markpos);
2722 }
2723 else
2724 it->region_beg_charpos = it->region_end_charpos = -1;
2725
2726 /* Get the position at which the redisplay_end_trigger hook should
2727 be run, if it is to be run at all. */
2728 if (MARKERP (w->redisplay_end_trigger)
2729 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2730 it->redisplay_end_trigger_charpos
2731 = marker_position (w->redisplay_end_trigger);
2732 else if (INTEGERP (w->redisplay_end_trigger))
2733 it->redisplay_end_trigger_charpos =
2734 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2735
2736 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2737
2738 /* Are lines in the display truncated? */
2739 if (base_face_id != DEFAULT_FACE_ID
2740 || it->w->hscroll
2741 || (! WINDOW_FULL_WIDTH_P (it->w)
2742 && ((!NILP (Vtruncate_partial_width_windows)
2743 && !INTEGERP (Vtruncate_partial_width_windows))
2744 || (INTEGERP (Vtruncate_partial_width_windows)
2745 && (WINDOW_TOTAL_COLS (it->w)
2746 < XINT (Vtruncate_partial_width_windows))))))
2747 it->line_wrap = TRUNCATE;
2748 else if (NILP (BVAR (current_buffer, truncate_lines)))
2749 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2750 ? WINDOW_WRAP : WORD_WRAP;
2751 else
2752 it->line_wrap = TRUNCATE;
2753
2754 /* Get dimensions of truncation and continuation glyphs. These are
2755 displayed as fringe bitmaps under X, but we need them for such
2756 frames when the fringes are turned off. But leave the dimensions
2757 zero for tooltip frames, as these glyphs look ugly there and also
2758 sabotage calculations of tooltip dimensions in x-show-tip. */
2759 #ifdef HAVE_WINDOW_SYSTEM
2760 if (!(FRAME_WINDOW_P (it->f)
2761 && FRAMEP (tip_frame)
2762 && it->f == XFRAME (tip_frame)))
2763 #endif
2764 {
2765 if (it->line_wrap == TRUNCATE)
2766 {
2767 /* We will need the truncation glyph. */
2768 eassert (it->glyph_row == NULL);
2769 produce_special_glyphs (it, IT_TRUNCATION);
2770 it->truncation_pixel_width = it->pixel_width;
2771 }
2772 else
2773 {
2774 /* We will need the continuation glyph. */
2775 eassert (it->glyph_row == NULL);
2776 produce_special_glyphs (it, IT_CONTINUATION);
2777 it->continuation_pixel_width = it->pixel_width;
2778 }
2779 }
2780
2781 /* Reset these values to zero because the produce_special_glyphs
2782 above has changed them. */
2783 it->pixel_width = it->ascent = it->descent = 0;
2784 it->phys_ascent = it->phys_descent = 0;
2785
2786 /* Set this after getting the dimensions of truncation and
2787 continuation glyphs, so that we don't produce glyphs when calling
2788 produce_special_glyphs, above. */
2789 it->glyph_row = row;
2790 it->area = TEXT_AREA;
2791
2792 /* Forget any previous info about this row being reversed. */
2793 if (it->glyph_row)
2794 it->glyph_row->reversed_p = 0;
2795
2796 /* Get the dimensions of the display area. The display area
2797 consists of the visible window area plus a horizontally scrolled
2798 part to the left of the window. All x-values are relative to the
2799 start of this total display area. */
2800 if (base_face_id != DEFAULT_FACE_ID)
2801 {
2802 /* Mode lines, menu bar in terminal frames. */
2803 it->first_visible_x = 0;
2804 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2805 }
2806 else
2807 {
2808 it->first_visible_x =
2809 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2810 it->last_visible_x = (it->first_visible_x
2811 + window_box_width (w, TEXT_AREA));
2812
2813 /* If we truncate lines, leave room for the truncation glyph(s) at
2814 the right margin. Otherwise, leave room for the continuation
2815 glyph(s). Done only if the window has no fringes. Since we
2816 don't know at this point whether there will be any R2L lines in
2817 the window, we reserve space for truncation/continuation glyphs
2818 even if only one of the fringes is absent. */
2819 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2820 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2821 {
2822 if (it->line_wrap == TRUNCATE)
2823 it->last_visible_x -= it->truncation_pixel_width;
2824 else
2825 it->last_visible_x -= it->continuation_pixel_width;
2826 }
2827
2828 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2829 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2830 }
2831
2832 /* Leave room for a border glyph. */
2833 if (!FRAME_WINDOW_P (it->f)
2834 && !WINDOW_RIGHTMOST_P (it->w))
2835 it->last_visible_x -= 1;
2836
2837 it->last_visible_y = window_text_bottom_y (w);
2838
2839 /* For mode lines and alike, arrange for the first glyph having a
2840 left box line if the face specifies a box. */
2841 if (base_face_id != DEFAULT_FACE_ID)
2842 {
2843 struct face *face;
2844
2845 it->face_id = remapped_base_face_id;
2846
2847 /* If we have a boxed mode line, make the first character appear
2848 with a left box line. */
2849 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2850 if (face->box != FACE_NO_BOX)
2851 it->start_of_box_run_p = 1;
2852 }
2853
2854 /* If a buffer position was specified, set the iterator there,
2855 getting overlays and face properties from that position. */
2856 if (charpos >= BUF_BEG (current_buffer))
2857 {
2858 it->end_charpos = ZV;
2859 IT_CHARPOS (*it) = charpos;
2860
2861 /* We will rely on `reseat' to set this up properly, via
2862 handle_face_prop. */
2863 it->face_id = it->base_face_id;
2864
2865 /* Compute byte position if not specified. */
2866 if (bytepos < charpos)
2867 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2868 else
2869 IT_BYTEPOS (*it) = bytepos;
2870
2871 it->start = it->current;
2872 /* Do we need to reorder bidirectional text? Not if this is a
2873 unibyte buffer: by definition, none of the single-byte
2874 characters are strong R2L, so no reordering is needed. And
2875 bidi.c doesn't support unibyte buffers anyway. Also, don't
2876 reorder while we are loading loadup.el, since the tables of
2877 character properties needed for reordering are not yet
2878 available. */
2879 it->bidi_p =
2880 NILP (Vpurify_flag)
2881 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2882 && it->multibyte_p;
2883
2884 /* If we are to reorder bidirectional text, init the bidi
2885 iterator. */
2886 if (it->bidi_p)
2887 {
2888 /* Note the paragraph direction that this buffer wants to
2889 use. */
2890 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2891 Qleft_to_right))
2892 it->paragraph_embedding = L2R;
2893 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2894 Qright_to_left))
2895 it->paragraph_embedding = R2L;
2896 else
2897 it->paragraph_embedding = NEUTRAL_DIR;
2898 bidi_unshelve_cache (NULL, 0);
2899 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2900 &it->bidi_it);
2901 }
2902
2903 /* Compute faces etc. */
2904 reseat (it, it->current.pos, 1);
2905 }
2906
2907 CHECK_IT (it);
2908 }
2909
2910
2911 /* Initialize IT for the display of window W with window start POS. */
2912
2913 void
2914 start_display (struct it *it, struct window *w, struct text_pos pos)
2915 {
2916 struct glyph_row *row;
2917 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2918
2919 row = w->desired_matrix->rows + first_vpos;
2920 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2921 it->first_vpos = first_vpos;
2922
2923 /* Don't reseat to previous visible line start if current start
2924 position is in a string or image. */
2925 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2926 {
2927 int start_at_line_beg_p;
2928 int first_y = it->current_y;
2929
2930 /* If window start is not at a line start, skip forward to POS to
2931 get the correct continuation lines width. */
2932 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2933 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2934 if (!start_at_line_beg_p)
2935 {
2936 int new_x;
2937
2938 reseat_at_previous_visible_line_start (it);
2939 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2940
2941 new_x = it->current_x + it->pixel_width;
2942
2943 /* If lines are continued, this line may end in the middle
2944 of a multi-glyph character (e.g. a control character
2945 displayed as \003, or in the middle of an overlay
2946 string). In this case move_it_to above will not have
2947 taken us to the start of the continuation line but to the
2948 end of the continued line. */
2949 if (it->current_x > 0
2950 && it->line_wrap != TRUNCATE /* Lines are continued. */
2951 && (/* And glyph doesn't fit on the line. */
2952 new_x > it->last_visible_x
2953 /* Or it fits exactly and we're on a window
2954 system frame. */
2955 || (new_x == it->last_visible_x
2956 && FRAME_WINDOW_P (it->f)
2957 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2958 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2959 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2960 {
2961 if ((it->current.dpvec_index >= 0
2962 || it->current.overlay_string_index >= 0)
2963 /* If we are on a newline from a display vector or
2964 overlay string, then we are already at the end of
2965 a screen line; no need to go to the next line in
2966 that case, as this line is not really continued.
2967 (If we do go to the next line, C-e will not DTRT.) */
2968 && it->c != '\n')
2969 {
2970 set_iterator_to_next (it, 1);
2971 move_it_in_display_line_to (it, -1, -1, 0);
2972 }
2973
2974 it->continuation_lines_width += it->current_x;
2975 }
2976 /* If the character at POS is displayed via a display
2977 vector, move_it_to above stops at the final glyph of
2978 IT->dpvec. To make the caller redisplay that character
2979 again (a.k.a. start at POS), we need to reset the
2980 dpvec_index to the beginning of IT->dpvec. */
2981 else if (it->current.dpvec_index >= 0)
2982 it->current.dpvec_index = 0;
2983
2984 /* We're starting a new display line, not affected by the
2985 height of the continued line, so clear the appropriate
2986 fields in the iterator structure. */
2987 it->max_ascent = it->max_descent = 0;
2988 it->max_phys_ascent = it->max_phys_descent = 0;
2989
2990 it->current_y = first_y;
2991 it->vpos = 0;
2992 it->current_x = it->hpos = 0;
2993 }
2994 }
2995 }
2996
2997
2998 /* Return 1 if POS is a position in ellipses displayed for invisible
2999 text. W is the window we display, for text property lookup. */
3000
3001 static int
3002 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3003 {
3004 Lisp_Object prop, window;
3005 int ellipses_p = 0;
3006 ptrdiff_t charpos = CHARPOS (pos->pos);
3007
3008 /* If POS specifies a position in a display vector, this might
3009 be for an ellipsis displayed for invisible text. We won't
3010 get the iterator set up for delivering that ellipsis unless
3011 we make sure that it gets aware of the invisible text. */
3012 if (pos->dpvec_index >= 0
3013 && pos->overlay_string_index < 0
3014 && CHARPOS (pos->string_pos) < 0
3015 && charpos > BEGV
3016 && (XSETWINDOW (window, w),
3017 prop = Fget_char_property (make_number (charpos),
3018 Qinvisible, window),
3019 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3020 {
3021 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3022 window);
3023 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3024 }
3025
3026 return ellipses_p;
3027 }
3028
3029
3030 /* Initialize IT for stepping through current_buffer in window W,
3031 starting at position POS that includes overlay string and display
3032 vector/ control character translation position information. Value
3033 is zero if there are overlay strings with newlines at POS. */
3034
3035 static int
3036 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3037 {
3038 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3039 int i, overlay_strings_with_newlines = 0;
3040
3041 /* If POS specifies a position in a display vector, this might
3042 be for an ellipsis displayed for invisible text. We won't
3043 get the iterator set up for delivering that ellipsis unless
3044 we make sure that it gets aware of the invisible text. */
3045 if (in_ellipses_for_invisible_text_p (pos, w))
3046 {
3047 --charpos;
3048 bytepos = 0;
3049 }
3050
3051 /* Keep in mind: the call to reseat in init_iterator skips invisible
3052 text, so we might end up at a position different from POS. This
3053 is only a problem when POS is a row start after a newline and an
3054 overlay starts there with an after-string, and the overlay has an
3055 invisible property. Since we don't skip invisible text in
3056 display_line and elsewhere immediately after consuming the
3057 newline before the row start, such a POS will not be in a string,
3058 but the call to init_iterator below will move us to the
3059 after-string. */
3060 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3061
3062 /* This only scans the current chunk -- it should scan all chunks.
3063 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3064 to 16 in 22.1 to make this a lesser problem. */
3065 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3066 {
3067 const char *s = SSDATA (it->overlay_strings[i]);
3068 const char *e = s + SBYTES (it->overlay_strings[i]);
3069
3070 while (s < e && *s != '\n')
3071 ++s;
3072
3073 if (s < e)
3074 {
3075 overlay_strings_with_newlines = 1;
3076 break;
3077 }
3078 }
3079
3080 /* If position is within an overlay string, set up IT to the right
3081 overlay string. */
3082 if (pos->overlay_string_index >= 0)
3083 {
3084 int relative_index;
3085
3086 /* If the first overlay string happens to have a `display'
3087 property for an image, the iterator will be set up for that
3088 image, and we have to undo that setup first before we can
3089 correct the overlay string index. */
3090 if (it->method == GET_FROM_IMAGE)
3091 pop_it (it);
3092
3093 /* We already have the first chunk of overlay strings in
3094 IT->overlay_strings. Load more until the one for
3095 pos->overlay_string_index is in IT->overlay_strings. */
3096 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3097 {
3098 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3099 it->current.overlay_string_index = 0;
3100 while (n--)
3101 {
3102 load_overlay_strings (it, 0);
3103 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3104 }
3105 }
3106
3107 it->current.overlay_string_index = pos->overlay_string_index;
3108 relative_index = (it->current.overlay_string_index
3109 % OVERLAY_STRING_CHUNK_SIZE);
3110 it->string = it->overlay_strings[relative_index];
3111 eassert (STRINGP (it->string));
3112 it->current.string_pos = pos->string_pos;
3113 it->method = GET_FROM_STRING;
3114 }
3115
3116 if (CHARPOS (pos->string_pos) >= 0)
3117 {
3118 /* Recorded position is not in an overlay string, but in another
3119 string. This can only be a string from a `display' property.
3120 IT should already be filled with that string. */
3121 it->current.string_pos = pos->string_pos;
3122 eassert (STRINGP (it->string));
3123 }
3124
3125 /* Restore position in display vector translations, control
3126 character translations or ellipses. */
3127 if (pos->dpvec_index >= 0)
3128 {
3129 if (it->dpvec == NULL)
3130 get_next_display_element (it);
3131 eassert (it->dpvec && it->current.dpvec_index == 0);
3132 it->current.dpvec_index = pos->dpvec_index;
3133 }
3134
3135 CHECK_IT (it);
3136 return !overlay_strings_with_newlines;
3137 }
3138
3139
3140 /* Initialize IT for stepping through current_buffer in window W
3141 starting at ROW->start. */
3142
3143 static void
3144 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3145 {
3146 init_from_display_pos (it, w, &row->start);
3147 it->start = row->start;
3148 it->continuation_lines_width = row->continuation_lines_width;
3149 CHECK_IT (it);
3150 }
3151
3152
3153 /* Initialize IT for stepping through current_buffer in window W
3154 starting in the line following ROW, i.e. starting at ROW->end.
3155 Value is zero if there are overlay strings with newlines at ROW's
3156 end position. */
3157
3158 static int
3159 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3160 {
3161 int success = 0;
3162
3163 if (init_from_display_pos (it, w, &row->end))
3164 {
3165 if (row->continued_p)
3166 it->continuation_lines_width
3167 = row->continuation_lines_width + row->pixel_width;
3168 CHECK_IT (it);
3169 success = 1;
3170 }
3171
3172 return success;
3173 }
3174
3175
3176
3177 \f
3178 /***********************************************************************
3179 Text properties
3180 ***********************************************************************/
3181
3182 /* Called when IT reaches IT->stop_charpos. Handle text property and
3183 overlay changes. Set IT->stop_charpos to the next position where
3184 to stop. */
3185
3186 static void
3187 handle_stop (struct it *it)
3188 {
3189 enum prop_handled handled;
3190 int handle_overlay_change_p;
3191 struct props *p;
3192
3193 it->dpvec = NULL;
3194 it->current.dpvec_index = -1;
3195 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3196 it->ignore_overlay_strings_at_pos_p = 0;
3197 it->ellipsis_p = 0;
3198
3199 /* Use face of preceding text for ellipsis (if invisible) */
3200 if (it->selective_display_ellipsis_p)
3201 it->saved_face_id = it->face_id;
3202
3203 do
3204 {
3205 handled = HANDLED_NORMALLY;
3206
3207 /* Call text property handlers. */
3208 for (p = it_props; p->handler; ++p)
3209 {
3210 handled = p->handler (it);
3211
3212 if (handled == HANDLED_RECOMPUTE_PROPS)
3213 break;
3214 else if (handled == HANDLED_RETURN)
3215 {
3216 /* We still want to show before and after strings from
3217 overlays even if the actual buffer text is replaced. */
3218 if (!handle_overlay_change_p
3219 || it->sp > 1
3220 /* Don't call get_overlay_strings_1 if we already
3221 have overlay strings loaded, because doing so
3222 will load them again and push the iterator state
3223 onto the stack one more time, which is not
3224 expected by the rest of the code that processes
3225 overlay strings. */
3226 || (it->current.overlay_string_index < 0
3227 ? !get_overlay_strings_1 (it, 0, 0)
3228 : 0))
3229 {
3230 if (it->ellipsis_p)
3231 setup_for_ellipsis (it, 0);
3232 /* When handling a display spec, we might load an
3233 empty string. In that case, discard it here. We
3234 used to discard it in handle_single_display_spec,
3235 but that causes get_overlay_strings_1, above, to
3236 ignore overlay strings that we must check. */
3237 if (STRINGP (it->string) && !SCHARS (it->string))
3238 pop_it (it);
3239 return;
3240 }
3241 else if (STRINGP (it->string) && !SCHARS (it->string))
3242 pop_it (it);
3243 else
3244 {
3245 it->ignore_overlay_strings_at_pos_p = 1;
3246 it->string_from_display_prop_p = 0;
3247 it->from_disp_prop_p = 0;
3248 handle_overlay_change_p = 0;
3249 }
3250 handled = HANDLED_RECOMPUTE_PROPS;
3251 break;
3252 }
3253 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3254 handle_overlay_change_p = 0;
3255 }
3256
3257 if (handled != HANDLED_RECOMPUTE_PROPS)
3258 {
3259 /* Don't check for overlay strings below when set to deliver
3260 characters from a display vector. */
3261 if (it->method == GET_FROM_DISPLAY_VECTOR)
3262 handle_overlay_change_p = 0;
3263
3264 /* Handle overlay changes.
3265 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3266 if it finds overlays. */
3267 if (handle_overlay_change_p)
3268 handled = handle_overlay_change (it);
3269 }
3270
3271 if (it->ellipsis_p)
3272 {
3273 setup_for_ellipsis (it, 0);
3274 break;
3275 }
3276 }
3277 while (handled == HANDLED_RECOMPUTE_PROPS);
3278
3279 /* Determine where to stop next. */
3280 if (handled == HANDLED_NORMALLY)
3281 compute_stop_pos (it);
3282 }
3283
3284
3285 /* Compute IT->stop_charpos from text property and overlay change
3286 information for IT's current position. */
3287
3288 static void
3289 compute_stop_pos (struct it *it)
3290 {
3291 register INTERVAL iv, next_iv;
3292 Lisp_Object object, limit, position;
3293 ptrdiff_t charpos, bytepos;
3294
3295 if (STRINGP (it->string))
3296 {
3297 /* Strings are usually short, so don't limit the search for
3298 properties. */
3299 it->stop_charpos = it->end_charpos;
3300 object = it->string;
3301 limit = Qnil;
3302 charpos = IT_STRING_CHARPOS (*it);
3303 bytepos = IT_STRING_BYTEPOS (*it);
3304 }
3305 else
3306 {
3307 ptrdiff_t pos;
3308
3309 /* If end_charpos is out of range for some reason, such as a
3310 misbehaving display function, rationalize it (Bug#5984). */
3311 if (it->end_charpos > ZV)
3312 it->end_charpos = ZV;
3313 it->stop_charpos = it->end_charpos;
3314
3315 /* If next overlay change is in front of the current stop pos
3316 (which is IT->end_charpos), stop there. Note: value of
3317 next_overlay_change is point-max if no overlay change
3318 follows. */
3319 charpos = IT_CHARPOS (*it);
3320 bytepos = IT_BYTEPOS (*it);
3321 pos = next_overlay_change (charpos);
3322 if (pos < it->stop_charpos)
3323 it->stop_charpos = pos;
3324
3325 /* If showing the region, we have to stop at the region
3326 start or end because the face might change there. */
3327 if (it->region_beg_charpos > 0)
3328 {
3329 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3330 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3331 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3332 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3333 }
3334
3335 /* Set up variables for computing the stop position from text
3336 property changes. */
3337 XSETBUFFER (object, current_buffer);
3338 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3339 }
3340
3341 /* Get the interval containing IT's position. Value is a null
3342 interval if there isn't such an interval. */
3343 position = make_number (charpos);
3344 iv = validate_interval_range (object, &position, &position, 0);
3345 if (iv)
3346 {
3347 Lisp_Object values_here[LAST_PROP_IDX];
3348 struct props *p;
3349
3350 /* Get properties here. */
3351 for (p = it_props; p->handler; ++p)
3352 values_here[p->idx] = textget (iv->plist, *p->name);
3353
3354 /* Look for an interval following iv that has different
3355 properties. */
3356 for (next_iv = next_interval (iv);
3357 (next_iv
3358 && (NILP (limit)
3359 || XFASTINT (limit) > next_iv->position));
3360 next_iv = next_interval (next_iv))
3361 {
3362 for (p = it_props; p->handler; ++p)
3363 {
3364 Lisp_Object new_value;
3365
3366 new_value = textget (next_iv->plist, *p->name);
3367 if (!EQ (values_here[p->idx], new_value))
3368 break;
3369 }
3370
3371 if (p->handler)
3372 break;
3373 }
3374
3375 if (next_iv)
3376 {
3377 if (INTEGERP (limit)
3378 && next_iv->position >= XFASTINT (limit))
3379 /* No text property change up to limit. */
3380 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3381 else
3382 /* Text properties change in next_iv. */
3383 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3384 }
3385 }
3386
3387 if (it->cmp_it.id < 0)
3388 {
3389 ptrdiff_t stoppos = it->end_charpos;
3390
3391 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3392 stoppos = -1;
3393 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3394 stoppos, it->string);
3395 }
3396
3397 eassert (STRINGP (it->string)
3398 || (it->stop_charpos >= BEGV
3399 && it->stop_charpos >= IT_CHARPOS (*it)));
3400 }
3401
3402
3403 /* Return the position of the next overlay change after POS in
3404 current_buffer. Value is point-max if no overlay change
3405 follows. This is like `next-overlay-change' but doesn't use
3406 xmalloc. */
3407
3408 static ptrdiff_t
3409 next_overlay_change (ptrdiff_t pos)
3410 {
3411 ptrdiff_t i, noverlays;
3412 ptrdiff_t endpos;
3413 Lisp_Object *overlays;
3414
3415 /* Get all overlays at the given position. */
3416 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3417
3418 /* If any of these overlays ends before endpos,
3419 use its ending point instead. */
3420 for (i = 0; i < noverlays; ++i)
3421 {
3422 Lisp_Object oend;
3423 ptrdiff_t oendpos;
3424
3425 oend = OVERLAY_END (overlays[i]);
3426 oendpos = OVERLAY_POSITION (oend);
3427 endpos = min (endpos, oendpos);
3428 }
3429
3430 return endpos;
3431 }
3432
3433 /* How many characters forward to search for a display property or
3434 display string. Searching too far forward makes the bidi display
3435 sluggish, especially in small windows. */
3436 #define MAX_DISP_SCAN 250
3437
3438 /* Return the character position of a display string at or after
3439 position specified by POSITION. If no display string exists at or
3440 after POSITION, return ZV. A display string is either an overlay
3441 with `display' property whose value is a string, or a `display'
3442 text property whose value is a string. STRING is data about the
3443 string to iterate; if STRING->lstring is nil, we are iterating a
3444 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3445 on a GUI frame. DISP_PROP is set to zero if we searched
3446 MAX_DISP_SCAN characters forward without finding any display
3447 strings, non-zero otherwise. It is set to 2 if the display string
3448 uses any kind of `(space ...)' spec that will produce a stretch of
3449 white space in the text area. */
3450 ptrdiff_t
3451 compute_display_string_pos (struct text_pos *position,
3452 struct bidi_string_data *string,
3453 int frame_window_p, int *disp_prop)
3454 {
3455 /* OBJECT = nil means current buffer. */
3456 Lisp_Object object =
3457 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3458 Lisp_Object pos, spec, limpos;
3459 int string_p = (string && (STRINGP (string->lstring) || string->s));
3460 ptrdiff_t eob = string_p ? string->schars : ZV;
3461 ptrdiff_t begb = string_p ? 0 : BEGV;
3462 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3463 ptrdiff_t lim =
3464 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3465 struct text_pos tpos;
3466 int rv = 0;
3467
3468 *disp_prop = 1;
3469
3470 if (charpos >= eob
3471 /* We don't support display properties whose values are strings
3472 that have display string properties. */
3473 || string->from_disp_str
3474 /* C strings cannot have display properties. */
3475 || (string->s && !STRINGP (object)))
3476 {
3477 *disp_prop = 0;
3478 return eob;
3479 }
3480
3481 /* If the character at CHARPOS is where the display string begins,
3482 return CHARPOS. */
3483 pos = make_number (charpos);
3484 if (STRINGP (object))
3485 bufpos = string->bufpos;
3486 else
3487 bufpos = charpos;
3488 tpos = *position;
3489 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3490 && (charpos <= begb
3491 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3492 object),
3493 spec))
3494 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3495 frame_window_p)))
3496 {
3497 if (rv == 2)
3498 *disp_prop = 2;
3499 return charpos;
3500 }
3501
3502 /* Look forward for the first character with a `display' property
3503 that will replace the underlying text when displayed. */
3504 limpos = make_number (lim);
3505 do {
3506 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3507 CHARPOS (tpos) = XFASTINT (pos);
3508 if (CHARPOS (tpos) >= lim)
3509 {
3510 *disp_prop = 0;
3511 break;
3512 }
3513 if (STRINGP (object))
3514 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3515 else
3516 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3517 spec = Fget_char_property (pos, Qdisplay, object);
3518 if (!STRINGP (object))
3519 bufpos = CHARPOS (tpos);
3520 } while (NILP (spec)
3521 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3522 bufpos, frame_window_p)));
3523 if (rv == 2)
3524 *disp_prop = 2;
3525
3526 return CHARPOS (tpos);
3527 }
3528
3529 /* Return the character position of the end of the display string that
3530 started at CHARPOS. If there's no display string at CHARPOS,
3531 return -1. A display string is either an overlay with `display'
3532 property whose value is a string or a `display' text property whose
3533 value is a string. */
3534 ptrdiff_t
3535 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3536 {
3537 /* OBJECT = nil means current buffer. */
3538 Lisp_Object object =
3539 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3540 Lisp_Object pos = make_number (charpos);
3541 ptrdiff_t eob =
3542 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3543
3544 if (charpos >= eob || (string->s && !STRINGP (object)))
3545 return eob;
3546
3547 /* It could happen that the display property or overlay was removed
3548 since we found it in compute_display_string_pos above. One way
3549 this can happen is if JIT font-lock was called (through
3550 handle_fontified_prop), and jit-lock-functions remove text
3551 properties or overlays from the portion of buffer that includes
3552 CHARPOS. Muse mode is known to do that, for example. In this
3553 case, we return -1 to the caller, to signal that no display
3554 string is actually present at CHARPOS. See bidi_fetch_char for
3555 how this is handled.
3556
3557 An alternative would be to never look for display properties past
3558 it->stop_charpos. But neither compute_display_string_pos nor
3559 bidi_fetch_char that calls it know or care where the next
3560 stop_charpos is. */
3561 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3562 return -1;
3563
3564 /* Look forward for the first character where the `display' property
3565 changes. */
3566 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3567
3568 return XFASTINT (pos);
3569 }
3570
3571
3572 \f
3573 /***********************************************************************
3574 Fontification
3575 ***********************************************************************/
3576
3577 /* Handle changes in the `fontified' property of the current buffer by
3578 calling hook functions from Qfontification_functions to fontify
3579 regions of text. */
3580
3581 static enum prop_handled
3582 handle_fontified_prop (struct it *it)
3583 {
3584 Lisp_Object prop, pos;
3585 enum prop_handled handled = HANDLED_NORMALLY;
3586
3587 if (!NILP (Vmemory_full))
3588 return handled;
3589
3590 /* Get the value of the `fontified' property at IT's current buffer
3591 position. (The `fontified' property doesn't have a special
3592 meaning in strings.) If the value is nil, call functions from
3593 Qfontification_functions. */
3594 if (!STRINGP (it->string)
3595 && it->s == NULL
3596 && !NILP (Vfontification_functions)
3597 && !NILP (Vrun_hooks)
3598 && (pos = make_number (IT_CHARPOS (*it)),
3599 prop = Fget_char_property (pos, Qfontified, Qnil),
3600 /* Ignore the special cased nil value always present at EOB since
3601 no amount of fontifying will be able to change it. */
3602 NILP (prop) && IT_CHARPOS (*it) < Z))
3603 {
3604 ptrdiff_t count = SPECPDL_INDEX ();
3605 Lisp_Object val;
3606 struct buffer *obuf = current_buffer;
3607 int begv = BEGV, zv = ZV;
3608 int old_clip_changed = current_buffer->clip_changed;
3609
3610 val = Vfontification_functions;
3611 specbind (Qfontification_functions, Qnil);
3612
3613 eassert (it->end_charpos == ZV);
3614
3615 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3616 safe_call1 (val, pos);
3617 else
3618 {
3619 Lisp_Object fns, fn;
3620 struct gcpro gcpro1, gcpro2;
3621
3622 fns = Qnil;
3623 GCPRO2 (val, fns);
3624
3625 for (; CONSP (val); val = XCDR (val))
3626 {
3627 fn = XCAR (val);
3628
3629 if (EQ (fn, Qt))
3630 {
3631 /* A value of t indicates this hook has a local
3632 binding; it means to run the global binding too.
3633 In a global value, t should not occur. If it
3634 does, we must ignore it to avoid an endless
3635 loop. */
3636 for (fns = Fdefault_value (Qfontification_functions);
3637 CONSP (fns);
3638 fns = XCDR (fns))
3639 {
3640 fn = XCAR (fns);
3641 if (!EQ (fn, Qt))
3642 safe_call1 (fn, pos);
3643 }
3644 }
3645 else
3646 safe_call1 (fn, pos);
3647 }
3648
3649 UNGCPRO;
3650 }
3651
3652 unbind_to (count, Qnil);
3653
3654 /* Fontification functions routinely call `save-restriction'.
3655 Normally, this tags clip_changed, which can confuse redisplay
3656 (see discussion in Bug#6671). Since we don't perform any
3657 special handling of fontification changes in the case where
3658 `save-restriction' isn't called, there's no point doing so in
3659 this case either. So, if the buffer's restrictions are
3660 actually left unchanged, reset clip_changed. */
3661 if (obuf == current_buffer)
3662 {
3663 if (begv == BEGV && zv == ZV)
3664 current_buffer->clip_changed = old_clip_changed;
3665 }
3666 /* There isn't much we can reasonably do to protect against
3667 misbehaving fontification, but here's a fig leaf. */
3668 else if (BUFFER_LIVE_P (obuf))
3669 set_buffer_internal_1 (obuf);
3670
3671 /* The fontification code may have added/removed text.
3672 It could do even a lot worse, but let's at least protect against
3673 the most obvious case where only the text past `pos' gets changed',
3674 as is/was done in grep.el where some escapes sequences are turned
3675 into face properties (bug#7876). */
3676 it->end_charpos = ZV;
3677
3678 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3679 something. This avoids an endless loop if they failed to
3680 fontify the text for which reason ever. */
3681 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3682 handled = HANDLED_RECOMPUTE_PROPS;
3683 }
3684
3685 return handled;
3686 }
3687
3688
3689 \f
3690 /***********************************************************************
3691 Faces
3692 ***********************************************************************/
3693
3694 /* Set up iterator IT from face properties at its current position.
3695 Called from handle_stop. */
3696
3697 static enum prop_handled
3698 handle_face_prop (struct it *it)
3699 {
3700 int new_face_id;
3701 ptrdiff_t next_stop;
3702
3703 if (!STRINGP (it->string))
3704 {
3705 new_face_id
3706 = face_at_buffer_position (it->w,
3707 IT_CHARPOS (*it),
3708 it->region_beg_charpos,
3709 it->region_end_charpos,
3710 &next_stop,
3711 (IT_CHARPOS (*it)
3712 + TEXT_PROP_DISTANCE_LIMIT),
3713 0, it->base_face_id);
3714
3715 /* Is this a start of a run of characters with box face?
3716 Caveat: this can be called for a freshly initialized
3717 iterator; face_id is -1 in this case. We know that the new
3718 face will not change until limit, i.e. if the new face has a
3719 box, all characters up to limit will have one. But, as
3720 usual, we don't know whether limit is really the end. */
3721 if (new_face_id != it->face_id)
3722 {
3723 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3724
3725 /* If new face has a box but old face has not, this is
3726 the start of a run of characters with box, i.e. it has
3727 a shadow on the left side. The value of face_id of the
3728 iterator will be -1 if this is the initial call that gets
3729 the face. In this case, we have to look in front of IT's
3730 position and see whether there is a face != new_face_id. */
3731 it->start_of_box_run_p
3732 = (new_face->box != FACE_NO_BOX
3733 && (it->face_id >= 0
3734 || IT_CHARPOS (*it) == BEG
3735 || new_face_id != face_before_it_pos (it)));
3736 it->face_box_p = new_face->box != FACE_NO_BOX;
3737 }
3738 }
3739 else
3740 {
3741 int base_face_id;
3742 ptrdiff_t bufpos;
3743 int i;
3744 Lisp_Object from_overlay
3745 = (it->current.overlay_string_index >= 0
3746 ? it->string_overlays[it->current.overlay_string_index
3747 % OVERLAY_STRING_CHUNK_SIZE]
3748 : Qnil);
3749
3750 /* See if we got to this string directly or indirectly from
3751 an overlay property. That includes the before-string or
3752 after-string of an overlay, strings in display properties
3753 provided by an overlay, their text properties, etc.
3754
3755 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3756 if (! NILP (from_overlay))
3757 for (i = it->sp - 1; i >= 0; i--)
3758 {
3759 if (it->stack[i].current.overlay_string_index >= 0)
3760 from_overlay
3761 = it->string_overlays[it->stack[i].current.overlay_string_index
3762 % OVERLAY_STRING_CHUNK_SIZE];
3763 else if (! NILP (it->stack[i].from_overlay))
3764 from_overlay = it->stack[i].from_overlay;
3765
3766 if (!NILP (from_overlay))
3767 break;
3768 }
3769
3770 if (! NILP (from_overlay))
3771 {
3772 bufpos = IT_CHARPOS (*it);
3773 /* For a string from an overlay, the base face depends
3774 only on text properties and ignores overlays. */
3775 base_face_id
3776 = face_for_overlay_string (it->w,
3777 IT_CHARPOS (*it),
3778 it->region_beg_charpos,
3779 it->region_end_charpos,
3780 &next_stop,
3781 (IT_CHARPOS (*it)
3782 + TEXT_PROP_DISTANCE_LIMIT),
3783 0,
3784 from_overlay);
3785 }
3786 else
3787 {
3788 bufpos = 0;
3789
3790 /* For strings from a `display' property, use the face at
3791 IT's current buffer position as the base face to merge
3792 with, so that overlay strings appear in the same face as
3793 surrounding text, unless they specify their own
3794 faces. */
3795 base_face_id = it->string_from_prefix_prop_p
3796 ? DEFAULT_FACE_ID
3797 : underlying_face_id (it);
3798 }
3799
3800 new_face_id = face_at_string_position (it->w,
3801 it->string,
3802 IT_STRING_CHARPOS (*it),
3803 bufpos,
3804 it->region_beg_charpos,
3805 it->region_end_charpos,
3806 &next_stop,
3807 base_face_id, 0);
3808
3809 /* Is this a start of a run of characters with box? Caveat:
3810 this can be called for a freshly allocated iterator; face_id
3811 is -1 is this case. We know that the new face will not
3812 change until the next check pos, i.e. if the new face has a
3813 box, all characters up to that position will have a
3814 box. But, as usual, we don't know whether that position
3815 is really the end. */
3816 if (new_face_id != it->face_id)
3817 {
3818 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3819 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3820
3821 /* If new face has a box but old face hasn't, this is the
3822 start of a run of characters with box, i.e. it has a
3823 shadow on the left side. */
3824 it->start_of_box_run_p
3825 = new_face->box && (old_face == NULL || !old_face->box);
3826 it->face_box_p = new_face->box != FACE_NO_BOX;
3827 }
3828 }
3829
3830 it->face_id = new_face_id;
3831 return HANDLED_NORMALLY;
3832 }
3833
3834
3835 /* Return the ID of the face ``underlying'' IT's current position,
3836 which is in a string. If the iterator is associated with a
3837 buffer, return the face at IT's current buffer position.
3838 Otherwise, use the iterator's base_face_id. */
3839
3840 static int
3841 underlying_face_id (struct it *it)
3842 {
3843 int face_id = it->base_face_id, i;
3844
3845 eassert (STRINGP (it->string));
3846
3847 for (i = it->sp - 1; i >= 0; --i)
3848 if (NILP (it->stack[i].string))
3849 face_id = it->stack[i].face_id;
3850
3851 return face_id;
3852 }
3853
3854
3855 /* Compute the face one character before or after the current position
3856 of IT, in the visual order. BEFORE_P non-zero means get the face
3857 in front (to the left in L2R paragraphs, to the right in R2L
3858 paragraphs) of IT's screen position. Value is the ID of the face. */
3859
3860 static int
3861 face_before_or_after_it_pos (struct it *it, int before_p)
3862 {
3863 int face_id, limit;
3864 ptrdiff_t next_check_charpos;
3865 struct it it_copy;
3866 void *it_copy_data = NULL;
3867
3868 eassert (it->s == NULL);
3869
3870 if (STRINGP (it->string))
3871 {
3872 ptrdiff_t bufpos, charpos;
3873 int base_face_id;
3874
3875 /* No face change past the end of the string (for the case
3876 we are padding with spaces). No face change before the
3877 string start. */
3878 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3879 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3880 return it->face_id;
3881
3882 if (!it->bidi_p)
3883 {
3884 /* Set charpos to the position before or after IT's current
3885 position, in the logical order, which in the non-bidi
3886 case is the same as the visual order. */
3887 if (before_p)
3888 charpos = IT_STRING_CHARPOS (*it) - 1;
3889 else if (it->what == IT_COMPOSITION)
3890 /* For composition, we must check the character after the
3891 composition. */
3892 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3893 else
3894 charpos = IT_STRING_CHARPOS (*it) + 1;
3895 }
3896 else
3897 {
3898 if (before_p)
3899 {
3900 /* With bidi iteration, the character before the current
3901 in the visual order cannot be found by simple
3902 iteration, because "reverse" reordering is not
3903 supported. Instead, we need to use the move_it_*
3904 family of functions. */
3905 /* Ignore face changes before the first visible
3906 character on this display line. */
3907 if (it->current_x <= it->first_visible_x)
3908 return it->face_id;
3909 SAVE_IT (it_copy, *it, it_copy_data);
3910 /* Implementation note: Since move_it_in_display_line
3911 works in the iterator geometry, and thinks the first
3912 character is always the leftmost, even in R2L lines,
3913 we don't need to distinguish between the R2L and L2R
3914 cases here. */
3915 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3916 it_copy.current_x - 1, MOVE_TO_X);
3917 charpos = IT_STRING_CHARPOS (it_copy);
3918 RESTORE_IT (it, it, it_copy_data);
3919 }
3920 else
3921 {
3922 /* Set charpos to the string position of the character
3923 that comes after IT's current position in the visual
3924 order. */
3925 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3926
3927 it_copy = *it;
3928 while (n--)
3929 bidi_move_to_visually_next (&it_copy.bidi_it);
3930
3931 charpos = it_copy.bidi_it.charpos;
3932 }
3933 }
3934 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3935
3936 if (it->current.overlay_string_index >= 0)
3937 bufpos = IT_CHARPOS (*it);
3938 else
3939 bufpos = 0;
3940
3941 base_face_id = underlying_face_id (it);
3942
3943 /* Get the face for ASCII, or unibyte. */
3944 face_id = face_at_string_position (it->w,
3945 it->string,
3946 charpos,
3947 bufpos,
3948 it->region_beg_charpos,
3949 it->region_end_charpos,
3950 &next_check_charpos,
3951 base_face_id, 0);
3952
3953 /* Correct the face for charsets different from ASCII. Do it
3954 for the multibyte case only. The face returned above is
3955 suitable for unibyte text if IT->string is unibyte. */
3956 if (STRING_MULTIBYTE (it->string))
3957 {
3958 struct text_pos pos1 = string_pos (charpos, it->string);
3959 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3960 int c, len;
3961 struct face *face = FACE_FROM_ID (it->f, face_id);
3962
3963 c = string_char_and_length (p, &len);
3964 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3965 }
3966 }
3967 else
3968 {
3969 struct text_pos pos;
3970
3971 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3972 || (IT_CHARPOS (*it) <= BEGV && before_p))
3973 return it->face_id;
3974
3975 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3976 pos = it->current.pos;
3977
3978 if (!it->bidi_p)
3979 {
3980 if (before_p)
3981 DEC_TEXT_POS (pos, it->multibyte_p);
3982 else
3983 {
3984 if (it->what == IT_COMPOSITION)
3985 {
3986 /* For composition, we must check the position after
3987 the composition. */
3988 pos.charpos += it->cmp_it.nchars;
3989 pos.bytepos += it->len;
3990 }
3991 else
3992 INC_TEXT_POS (pos, it->multibyte_p);
3993 }
3994 }
3995 else
3996 {
3997 if (before_p)
3998 {
3999 /* With bidi iteration, the character before the current
4000 in the visual order cannot be found by simple
4001 iteration, because "reverse" reordering is not
4002 supported. Instead, we need to use the move_it_*
4003 family of functions. */
4004 /* Ignore face changes before the first visible
4005 character on this display line. */
4006 if (it->current_x <= it->first_visible_x)
4007 return it->face_id;
4008 SAVE_IT (it_copy, *it, it_copy_data);
4009 /* Implementation note: Since move_it_in_display_line
4010 works in the iterator geometry, and thinks the first
4011 character is always the leftmost, even in R2L lines,
4012 we don't need to distinguish between the R2L and L2R
4013 cases here. */
4014 move_it_in_display_line (&it_copy, ZV,
4015 it_copy.current_x - 1, MOVE_TO_X);
4016 pos = it_copy.current.pos;
4017 RESTORE_IT (it, it, it_copy_data);
4018 }
4019 else
4020 {
4021 /* Set charpos to the buffer position of the character
4022 that comes after IT's current position in the visual
4023 order. */
4024 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4025
4026 it_copy = *it;
4027 while (n--)
4028 bidi_move_to_visually_next (&it_copy.bidi_it);
4029
4030 SET_TEXT_POS (pos,
4031 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4032 }
4033 }
4034 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4035
4036 /* Determine face for CHARSET_ASCII, or unibyte. */
4037 face_id = face_at_buffer_position (it->w,
4038 CHARPOS (pos),
4039 it->region_beg_charpos,
4040 it->region_end_charpos,
4041 &next_check_charpos,
4042 limit, 0, -1);
4043
4044 /* Correct the face for charsets different from ASCII. Do it
4045 for the multibyte case only. The face returned above is
4046 suitable for unibyte text if current_buffer is unibyte. */
4047 if (it->multibyte_p)
4048 {
4049 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4050 struct face *face = FACE_FROM_ID (it->f, face_id);
4051 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4052 }
4053 }
4054
4055 return face_id;
4056 }
4057
4058
4059 \f
4060 /***********************************************************************
4061 Invisible text
4062 ***********************************************************************/
4063
4064 /* Set up iterator IT from invisible properties at its current
4065 position. Called from handle_stop. */
4066
4067 static enum prop_handled
4068 handle_invisible_prop (struct it *it)
4069 {
4070 enum prop_handled handled = HANDLED_NORMALLY;
4071 int invis_p;
4072 Lisp_Object prop;
4073
4074 if (STRINGP (it->string))
4075 {
4076 Lisp_Object end_charpos, limit, charpos;
4077
4078 /* Get the value of the invisible text property at the
4079 current position. Value will be nil if there is no such
4080 property. */
4081 charpos = make_number (IT_STRING_CHARPOS (*it));
4082 prop = Fget_text_property (charpos, Qinvisible, it->string);
4083 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4084
4085 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4086 {
4087 /* Record whether we have to display an ellipsis for the
4088 invisible text. */
4089 int display_ellipsis_p = (invis_p == 2);
4090 ptrdiff_t len, endpos;
4091
4092 handled = HANDLED_RECOMPUTE_PROPS;
4093
4094 /* Get the position at which the next visible text can be
4095 found in IT->string, if any. */
4096 endpos = len = SCHARS (it->string);
4097 XSETINT (limit, len);
4098 do
4099 {
4100 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4101 it->string, limit);
4102 if (INTEGERP (end_charpos))
4103 {
4104 endpos = XFASTINT (end_charpos);
4105 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4106 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4107 if (invis_p == 2)
4108 display_ellipsis_p = 1;
4109 }
4110 }
4111 while (invis_p && endpos < len);
4112
4113 if (display_ellipsis_p)
4114 it->ellipsis_p = 1;
4115
4116 if (endpos < len)
4117 {
4118 /* Text at END_CHARPOS is visible. Move IT there. */
4119 struct text_pos old;
4120 ptrdiff_t oldpos;
4121
4122 old = it->current.string_pos;
4123 oldpos = CHARPOS (old);
4124 if (it->bidi_p)
4125 {
4126 if (it->bidi_it.first_elt
4127 && it->bidi_it.charpos < SCHARS (it->string))
4128 bidi_paragraph_init (it->paragraph_embedding,
4129 &it->bidi_it, 1);
4130 /* Bidi-iterate out of the invisible text. */
4131 do
4132 {
4133 bidi_move_to_visually_next (&it->bidi_it);
4134 }
4135 while (oldpos <= it->bidi_it.charpos
4136 && it->bidi_it.charpos < endpos);
4137
4138 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4139 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4140 if (IT_CHARPOS (*it) >= endpos)
4141 it->prev_stop = endpos;
4142 }
4143 else
4144 {
4145 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4146 compute_string_pos (&it->current.string_pos, old, it->string);
4147 }
4148 }
4149 else
4150 {
4151 /* The rest of the string is invisible. If this is an
4152 overlay string, proceed with the next overlay string
4153 or whatever comes and return a character from there. */
4154 if (it->current.overlay_string_index >= 0
4155 && !display_ellipsis_p)
4156 {
4157 next_overlay_string (it);
4158 /* Don't check for overlay strings when we just
4159 finished processing them. */
4160 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4161 }
4162 else
4163 {
4164 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4165 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4166 }
4167 }
4168 }
4169 }
4170 else
4171 {
4172 ptrdiff_t newpos, next_stop, start_charpos, tem;
4173 Lisp_Object pos, overlay;
4174
4175 /* First of all, is there invisible text at this position? */
4176 tem = start_charpos = IT_CHARPOS (*it);
4177 pos = make_number (tem);
4178 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4179 &overlay);
4180 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4181
4182 /* If we are on invisible text, skip over it. */
4183 if (invis_p && start_charpos < it->end_charpos)
4184 {
4185 /* Record whether we have to display an ellipsis for the
4186 invisible text. */
4187 int display_ellipsis_p = invis_p == 2;
4188
4189 handled = HANDLED_RECOMPUTE_PROPS;
4190
4191 /* Loop skipping over invisible text. The loop is left at
4192 ZV or with IT on the first char being visible again. */
4193 do
4194 {
4195 /* Try to skip some invisible text. Return value is the
4196 position reached which can be equal to where we start
4197 if there is nothing invisible there. This skips both
4198 over invisible text properties and overlays with
4199 invisible property. */
4200 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4201
4202 /* If we skipped nothing at all we weren't at invisible
4203 text in the first place. If everything to the end of
4204 the buffer was skipped, end the loop. */
4205 if (newpos == tem || newpos >= ZV)
4206 invis_p = 0;
4207 else
4208 {
4209 /* We skipped some characters but not necessarily
4210 all there are. Check if we ended up on visible
4211 text. Fget_char_property returns the property of
4212 the char before the given position, i.e. if we
4213 get invis_p = 0, this means that the char at
4214 newpos is visible. */
4215 pos = make_number (newpos);
4216 prop = Fget_char_property (pos, Qinvisible, it->window);
4217 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4218 }
4219
4220 /* If we ended up on invisible text, proceed to
4221 skip starting with next_stop. */
4222 if (invis_p)
4223 tem = next_stop;
4224
4225 /* If there are adjacent invisible texts, don't lose the
4226 second one's ellipsis. */
4227 if (invis_p == 2)
4228 display_ellipsis_p = 1;
4229 }
4230 while (invis_p);
4231
4232 /* The position newpos is now either ZV or on visible text. */
4233 if (it->bidi_p)
4234 {
4235 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4236 int on_newline =
4237 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4238 int after_newline =
4239 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4240
4241 /* If the invisible text ends on a newline or on a
4242 character after a newline, we can avoid the costly,
4243 character by character, bidi iteration to NEWPOS, and
4244 instead simply reseat the iterator there. That's
4245 because all bidi reordering information is tossed at
4246 the newline. This is a big win for modes that hide
4247 complete lines, like Outline, Org, etc. */
4248 if (on_newline || after_newline)
4249 {
4250 struct text_pos tpos;
4251 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4252
4253 SET_TEXT_POS (tpos, newpos, bpos);
4254 reseat_1 (it, tpos, 0);
4255 /* If we reseat on a newline/ZV, we need to prep the
4256 bidi iterator for advancing to the next character
4257 after the newline/EOB, keeping the current paragraph
4258 direction (so that PRODUCE_GLYPHS does TRT wrt
4259 prepending/appending glyphs to a glyph row). */
4260 if (on_newline)
4261 {
4262 it->bidi_it.first_elt = 0;
4263 it->bidi_it.paragraph_dir = pdir;
4264 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4265 it->bidi_it.nchars = 1;
4266 it->bidi_it.ch_len = 1;
4267 }
4268 }
4269 else /* Must use the slow method. */
4270 {
4271 /* With bidi iteration, the region of invisible text
4272 could start and/or end in the middle of a
4273 non-base embedding level. Therefore, we need to
4274 skip invisible text using the bidi iterator,
4275 starting at IT's current position, until we find
4276 ourselves outside of the invisible text.
4277 Skipping invisible text _after_ bidi iteration
4278 avoids affecting the visual order of the
4279 displayed text when invisible properties are
4280 added or removed. */
4281 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4282 {
4283 /* If we were `reseat'ed to a new paragraph,
4284 determine the paragraph base direction. We
4285 need to do it now because
4286 next_element_from_buffer may not have a
4287 chance to do it, if we are going to skip any
4288 text at the beginning, which resets the
4289 FIRST_ELT flag. */
4290 bidi_paragraph_init (it->paragraph_embedding,
4291 &it->bidi_it, 1);
4292 }
4293 do
4294 {
4295 bidi_move_to_visually_next (&it->bidi_it);
4296 }
4297 while (it->stop_charpos <= it->bidi_it.charpos
4298 && it->bidi_it.charpos < newpos);
4299 IT_CHARPOS (*it) = it->bidi_it.charpos;
4300 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4301 /* If we overstepped NEWPOS, record its position in
4302 the iterator, so that we skip invisible text if
4303 later the bidi iteration lands us in the
4304 invisible region again. */
4305 if (IT_CHARPOS (*it) >= newpos)
4306 it->prev_stop = newpos;
4307 }
4308 }
4309 else
4310 {
4311 IT_CHARPOS (*it) = newpos;
4312 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4313 }
4314
4315 /* If there are before-strings at the start of invisible
4316 text, and the text is invisible because of a text
4317 property, arrange to show before-strings because 20.x did
4318 it that way. (If the text is invisible because of an
4319 overlay property instead of a text property, this is
4320 already handled in the overlay code.) */
4321 if (NILP (overlay)
4322 && get_overlay_strings (it, it->stop_charpos))
4323 {
4324 handled = HANDLED_RECOMPUTE_PROPS;
4325 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4326 }
4327 else if (display_ellipsis_p)
4328 {
4329 /* Make sure that the glyphs of the ellipsis will get
4330 correct `charpos' values. If we would not update
4331 it->position here, the glyphs would belong to the
4332 last visible character _before_ the invisible
4333 text, which confuses `set_cursor_from_row'.
4334
4335 We use the last invisible position instead of the
4336 first because this way the cursor is always drawn on
4337 the first "." of the ellipsis, whenever PT is inside
4338 the invisible text. Otherwise the cursor would be
4339 placed _after_ the ellipsis when the point is after the
4340 first invisible character. */
4341 if (!STRINGP (it->object))
4342 {
4343 it->position.charpos = newpos - 1;
4344 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4345 }
4346 it->ellipsis_p = 1;
4347 /* Let the ellipsis display before
4348 considering any properties of the following char.
4349 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4350 handled = HANDLED_RETURN;
4351 }
4352 }
4353 }
4354
4355 return handled;
4356 }
4357
4358
4359 /* Make iterator IT return `...' next.
4360 Replaces LEN characters from buffer. */
4361
4362 static void
4363 setup_for_ellipsis (struct it *it, int len)
4364 {
4365 /* Use the display table definition for `...'. Invalid glyphs
4366 will be handled by the method returning elements from dpvec. */
4367 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4368 {
4369 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4370 it->dpvec = v->contents;
4371 it->dpend = v->contents + v->header.size;
4372 }
4373 else
4374 {
4375 /* Default `...'. */
4376 it->dpvec = default_invis_vector;
4377 it->dpend = default_invis_vector + 3;
4378 }
4379
4380 it->dpvec_char_len = len;
4381 it->current.dpvec_index = 0;
4382 it->dpvec_face_id = -1;
4383
4384 /* Remember the current face id in case glyphs specify faces.
4385 IT's face is restored in set_iterator_to_next.
4386 saved_face_id was set to preceding char's face in handle_stop. */
4387 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4388 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4389
4390 it->method = GET_FROM_DISPLAY_VECTOR;
4391 it->ellipsis_p = 1;
4392 }
4393
4394
4395 \f
4396 /***********************************************************************
4397 'display' property
4398 ***********************************************************************/
4399
4400 /* Set up iterator IT from `display' property at its current position.
4401 Called from handle_stop.
4402 We return HANDLED_RETURN if some part of the display property
4403 overrides the display of the buffer text itself.
4404 Otherwise we return HANDLED_NORMALLY. */
4405
4406 static enum prop_handled
4407 handle_display_prop (struct it *it)
4408 {
4409 Lisp_Object propval, object, overlay;
4410 struct text_pos *position;
4411 ptrdiff_t bufpos;
4412 /* Nonzero if some property replaces the display of the text itself. */
4413 int display_replaced_p = 0;
4414
4415 if (STRINGP (it->string))
4416 {
4417 object = it->string;
4418 position = &it->current.string_pos;
4419 bufpos = CHARPOS (it->current.pos);
4420 }
4421 else
4422 {
4423 XSETWINDOW (object, it->w);
4424 position = &it->current.pos;
4425 bufpos = CHARPOS (*position);
4426 }
4427
4428 /* Reset those iterator values set from display property values. */
4429 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4430 it->space_width = Qnil;
4431 it->font_height = Qnil;
4432 it->voffset = 0;
4433
4434 /* We don't support recursive `display' properties, i.e. string
4435 values that have a string `display' property, that have a string
4436 `display' property etc. */
4437 if (!it->string_from_display_prop_p)
4438 it->area = TEXT_AREA;
4439
4440 propval = get_char_property_and_overlay (make_number (position->charpos),
4441 Qdisplay, object, &overlay);
4442 if (NILP (propval))
4443 return HANDLED_NORMALLY;
4444 /* Now OVERLAY is the overlay that gave us this property, or nil
4445 if it was a text property. */
4446
4447 if (!STRINGP (it->string))
4448 object = it->w->buffer;
4449
4450 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4451 position, bufpos,
4452 FRAME_WINDOW_P (it->f));
4453
4454 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4455 }
4456
4457 /* Subroutine of handle_display_prop. Returns non-zero if the display
4458 specification in SPEC is a replacing specification, i.e. it would
4459 replace the text covered by `display' property with something else,
4460 such as an image or a display string. If SPEC includes any kind or
4461 `(space ...) specification, the value is 2; this is used by
4462 compute_display_string_pos, which see.
4463
4464 See handle_single_display_spec for documentation of arguments.
4465 frame_window_p is non-zero if the window being redisplayed is on a
4466 GUI frame; this argument is used only if IT is NULL, see below.
4467
4468 IT can be NULL, if this is called by the bidi reordering code
4469 through compute_display_string_pos, which see. In that case, this
4470 function only examines SPEC, but does not otherwise "handle" it, in
4471 the sense that it doesn't set up members of IT from the display
4472 spec. */
4473 static int
4474 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4475 Lisp_Object overlay, struct text_pos *position,
4476 ptrdiff_t bufpos, int frame_window_p)
4477 {
4478 int replacing_p = 0;
4479 int rv;
4480
4481 if (CONSP (spec)
4482 /* Simple specifications. */
4483 && !EQ (XCAR (spec), Qimage)
4484 && !EQ (XCAR (spec), Qspace)
4485 && !EQ (XCAR (spec), Qwhen)
4486 && !EQ (XCAR (spec), Qslice)
4487 && !EQ (XCAR (spec), Qspace_width)
4488 && !EQ (XCAR (spec), Qheight)
4489 && !EQ (XCAR (spec), Qraise)
4490 /* Marginal area specifications. */
4491 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4492 && !EQ (XCAR (spec), Qleft_fringe)
4493 && !EQ (XCAR (spec), Qright_fringe)
4494 && !NILP (XCAR (spec)))
4495 {
4496 for (; CONSP (spec); spec = XCDR (spec))
4497 {
4498 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4499 overlay, position, bufpos,
4500 replacing_p, frame_window_p)))
4501 {
4502 replacing_p = rv;
4503 /* If some text in a string is replaced, `position' no
4504 longer points to the position of `object'. */
4505 if (!it || STRINGP (object))
4506 break;
4507 }
4508 }
4509 }
4510 else if (VECTORP (spec))
4511 {
4512 ptrdiff_t i;
4513 for (i = 0; i < ASIZE (spec); ++i)
4514 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4515 overlay, position, bufpos,
4516 replacing_p, frame_window_p)))
4517 {
4518 replacing_p = rv;
4519 /* If some text in a string is replaced, `position' no
4520 longer points to the position of `object'. */
4521 if (!it || STRINGP (object))
4522 break;
4523 }
4524 }
4525 else
4526 {
4527 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4528 position, bufpos, 0,
4529 frame_window_p)))
4530 replacing_p = rv;
4531 }
4532
4533 return replacing_p;
4534 }
4535
4536 /* Value is the position of the end of the `display' property starting
4537 at START_POS in OBJECT. */
4538
4539 static struct text_pos
4540 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4541 {
4542 Lisp_Object end;
4543 struct text_pos end_pos;
4544
4545 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4546 Qdisplay, object, Qnil);
4547 CHARPOS (end_pos) = XFASTINT (end);
4548 if (STRINGP (object))
4549 compute_string_pos (&end_pos, start_pos, it->string);
4550 else
4551 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4552
4553 return end_pos;
4554 }
4555
4556
4557 /* Set up IT from a single `display' property specification SPEC. OBJECT
4558 is the object in which the `display' property was found. *POSITION
4559 is the position in OBJECT at which the `display' property was found.
4560 BUFPOS is the buffer position of OBJECT (different from POSITION if
4561 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4562 previously saw a display specification which already replaced text
4563 display with something else, for example an image; we ignore such
4564 properties after the first one has been processed.
4565
4566 OVERLAY is the overlay this `display' property came from,
4567 or nil if it was a text property.
4568
4569 If SPEC is a `space' or `image' specification, and in some other
4570 cases too, set *POSITION to the position where the `display'
4571 property ends.
4572
4573 If IT is NULL, only examine the property specification in SPEC, but
4574 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4575 is intended to be displayed in a window on a GUI frame.
4576
4577 Value is non-zero if something was found which replaces the display
4578 of buffer or string text. */
4579
4580 static int
4581 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4582 Lisp_Object overlay, struct text_pos *position,
4583 ptrdiff_t bufpos, int display_replaced_p,
4584 int frame_window_p)
4585 {
4586 Lisp_Object form;
4587 Lisp_Object location, value;
4588 struct text_pos start_pos = *position;
4589 int valid_p;
4590
4591 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4592 If the result is non-nil, use VALUE instead of SPEC. */
4593 form = Qt;
4594 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4595 {
4596 spec = XCDR (spec);
4597 if (!CONSP (spec))
4598 return 0;
4599 form = XCAR (spec);
4600 spec = XCDR (spec);
4601 }
4602
4603 if (!NILP (form) && !EQ (form, Qt))
4604 {
4605 ptrdiff_t count = SPECPDL_INDEX ();
4606 struct gcpro gcpro1;
4607
4608 /* Bind `object' to the object having the `display' property, a
4609 buffer or string. Bind `position' to the position in the
4610 object where the property was found, and `buffer-position'
4611 to the current position in the buffer. */
4612
4613 if (NILP (object))
4614 XSETBUFFER (object, current_buffer);
4615 specbind (Qobject, object);
4616 specbind (Qposition, make_number (CHARPOS (*position)));
4617 specbind (Qbuffer_position, make_number (bufpos));
4618 GCPRO1 (form);
4619 form = safe_eval (form);
4620 UNGCPRO;
4621 unbind_to (count, Qnil);
4622 }
4623
4624 if (NILP (form))
4625 return 0;
4626
4627 /* Handle `(height HEIGHT)' specifications. */
4628 if (CONSP (spec)
4629 && EQ (XCAR (spec), Qheight)
4630 && CONSP (XCDR (spec)))
4631 {
4632 if (it)
4633 {
4634 if (!FRAME_WINDOW_P (it->f))
4635 return 0;
4636
4637 it->font_height = XCAR (XCDR (spec));
4638 if (!NILP (it->font_height))
4639 {
4640 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4641 int new_height = -1;
4642
4643 if (CONSP (it->font_height)
4644 && (EQ (XCAR (it->font_height), Qplus)
4645 || EQ (XCAR (it->font_height), Qminus))
4646 && CONSP (XCDR (it->font_height))
4647 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4648 {
4649 /* `(+ N)' or `(- N)' where N is an integer. */
4650 int steps = XINT (XCAR (XCDR (it->font_height)));
4651 if (EQ (XCAR (it->font_height), Qplus))
4652 steps = - steps;
4653 it->face_id = smaller_face (it->f, it->face_id, steps);
4654 }
4655 else if (FUNCTIONP (it->font_height))
4656 {
4657 /* Call function with current height as argument.
4658 Value is the new height. */
4659 Lisp_Object height;
4660 height = safe_call1 (it->font_height,
4661 face->lface[LFACE_HEIGHT_INDEX]);
4662 if (NUMBERP (height))
4663 new_height = XFLOATINT (height);
4664 }
4665 else if (NUMBERP (it->font_height))
4666 {
4667 /* Value is a multiple of the canonical char height. */
4668 struct face *f;
4669
4670 f = FACE_FROM_ID (it->f,
4671 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4672 new_height = (XFLOATINT (it->font_height)
4673 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4674 }
4675 else
4676 {
4677 /* Evaluate IT->font_height with `height' bound to the
4678 current specified height to get the new height. */
4679 ptrdiff_t count = SPECPDL_INDEX ();
4680
4681 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4682 value = safe_eval (it->font_height);
4683 unbind_to (count, Qnil);
4684
4685 if (NUMBERP (value))
4686 new_height = XFLOATINT (value);
4687 }
4688
4689 if (new_height > 0)
4690 it->face_id = face_with_height (it->f, it->face_id, new_height);
4691 }
4692 }
4693
4694 return 0;
4695 }
4696
4697 /* Handle `(space-width WIDTH)'. */
4698 if (CONSP (spec)
4699 && EQ (XCAR (spec), Qspace_width)
4700 && CONSP (XCDR (spec)))
4701 {
4702 if (it)
4703 {
4704 if (!FRAME_WINDOW_P (it->f))
4705 return 0;
4706
4707 value = XCAR (XCDR (spec));
4708 if (NUMBERP (value) && XFLOATINT (value) > 0)
4709 it->space_width = value;
4710 }
4711
4712 return 0;
4713 }
4714
4715 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4716 if (CONSP (spec)
4717 && EQ (XCAR (spec), Qslice))
4718 {
4719 Lisp_Object tem;
4720
4721 if (it)
4722 {
4723 if (!FRAME_WINDOW_P (it->f))
4724 return 0;
4725
4726 if (tem = XCDR (spec), CONSP (tem))
4727 {
4728 it->slice.x = XCAR (tem);
4729 if (tem = XCDR (tem), CONSP (tem))
4730 {
4731 it->slice.y = XCAR (tem);
4732 if (tem = XCDR (tem), CONSP (tem))
4733 {
4734 it->slice.width = XCAR (tem);
4735 if (tem = XCDR (tem), CONSP (tem))
4736 it->slice.height = XCAR (tem);
4737 }
4738 }
4739 }
4740 }
4741
4742 return 0;
4743 }
4744
4745 /* Handle `(raise FACTOR)'. */
4746 if (CONSP (spec)
4747 && EQ (XCAR (spec), Qraise)
4748 && CONSP (XCDR (spec)))
4749 {
4750 if (it)
4751 {
4752 if (!FRAME_WINDOW_P (it->f))
4753 return 0;
4754
4755 #ifdef HAVE_WINDOW_SYSTEM
4756 value = XCAR (XCDR (spec));
4757 if (NUMBERP (value))
4758 {
4759 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4760 it->voffset = - (XFLOATINT (value)
4761 * (FONT_HEIGHT (face->font)));
4762 }
4763 #endif /* HAVE_WINDOW_SYSTEM */
4764 }
4765
4766 return 0;
4767 }
4768
4769 /* Don't handle the other kinds of display specifications
4770 inside a string that we got from a `display' property. */
4771 if (it && it->string_from_display_prop_p)
4772 return 0;
4773
4774 /* Characters having this form of property are not displayed, so
4775 we have to find the end of the property. */
4776 if (it)
4777 {
4778 start_pos = *position;
4779 *position = display_prop_end (it, object, start_pos);
4780 }
4781 value = Qnil;
4782
4783 /* Stop the scan at that end position--we assume that all
4784 text properties change there. */
4785 if (it)
4786 it->stop_charpos = position->charpos;
4787
4788 /* Handle `(left-fringe BITMAP [FACE])'
4789 and `(right-fringe BITMAP [FACE])'. */
4790 if (CONSP (spec)
4791 && (EQ (XCAR (spec), Qleft_fringe)
4792 || EQ (XCAR (spec), Qright_fringe))
4793 && CONSP (XCDR (spec)))
4794 {
4795 int fringe_bitmap;
4796
4797 if (it)
4798 {
4799 if (!FRAME_WINDOW_P (it->f))
4800 /* If we return here, POSITION has been advanced
4801 across the text with this property. */
4802 {
4803 /* Synchronize the bidi iterator with POSITION. This is
4804 needed because we are not going to push the iterator
4805 on behalf of this display property, so there will be
4806 no pop_it call to do this synchronization for us. */
4807 if (it->bidi_p)
4808 {
4809 it->position = *position;
4810 iterate_out_of_display_property (it);
4811 *position = it->position;
4812 }
4813 return 1;
4814 }
4815 }
4816 else if (!frame_window_p)
4817 return 1;
4818
4819 #ifdef HAVE_WINDOW_SYSTEM
4820 value = XCAR (XCDR (spec));
4821 if (!SYMBOLP (value)
4822 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4823 /* If we return here, POSITION has been advanced
4824 across the text with this property. */
4825 {
4826 if (it && it->bidi_p)
4827 {
4828 it->position = *position;
4829 iterate_out_of_display_property (it);
4830 *position = it->position;
4831 }
4832 return 1;
4833 }
4834
4835 if (it)
4836 {
4837 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4838
4839 if (CONSP (XCDR (XCDR (spec))))
4840 {
4841 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4842 int face_id2 = lookup_derived_face (it->f, face_name,
4843 FRINGE_FACE_ID, 0);
4844 if (face_id2 >= 0)
4845 face_id = face_id2;
4846 }
4847
4848 /* Save current settings of IT so that we can restore them
4849 when we are finished with the glyph property value. */
4850 push_it (it, position);
4851
4852 it->area = TEXT_AREA;
4853 it->what = IT_IMAGE;
4854 it->image_id = -1; /* no image */
4855 it->position = start_pos;
4856 it->object = NILP (object) ? it->w->buffer : object;
4857 it->method = GET_FROM_IMAGE;
4858 it->from_overlay = Qnil;
4859 it->face_id = face_id;
4860 it->from_disp_prop_p = 1;
4861
4862 /* Say that we haven't consumed the characters with
4863 `display' property yet. The call to pop_it in
4864 set_iterator_to_next will clean this up. */
4865 *position = start_pos;
4866
4867 if (EQ (XCAR (spec), Qleft_fringe))
4868 {
4869 it->left_user_fringe_bitmap = fringe_bitmap;
4870 it->left_user_fringe_face_id = face_id;
4871 }
4872 else
4873 {
4874 it->right_user_fringe_bitmap = fringe_bitmap;
4875 it->right_user_fringe_face_id = face_id;
4876 }
4877 }
4878 #endif /* HAVE_WINDOW_SYSTEM */
4879 return 1;
4880 }
4881
4882 /* Prepare to handle `((margin left-margin) ...)',
4883 `((margin right-margin) ...)' and `((margin nil) ...)'
4884 prefixes for display specifications. */
4885 location = Qunbound;
4886 if (CONSP (spec) && CONSP (XCAR (spec)))
4887 {
4888 Lisp_Object tem;
4889
4890 value = XCDR (spec);
4891 if (CONSP (value))
4892 value = XCAR (value);
4893
4894 tem = XCAR (spec);
4895 if (EQ (XCAR (tem), Qmargin)
4896 && (tem = XCDR (tem),
4897 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4898 (NILP (tem)
4899 || EQ (tem, Qleft_margin)
4900 || EQ (tem, Qright_margin))))
4901 location = tem;
4902 }
4903
4904 if (EQ (location, Qunbound))
4905 {
4906 location = Qnil;
4907 value = spec;
4908 }
4909
4910 /* After this point, VALUE is the property after any
4911 margin prefix has been stripped. It must be a string,
4912 an image specification, or `(space ...)'.
4913
4914 LOCATION specifies where to display: `left-margin',
4915 `right-margin' or nil. */
4916
4917 valid_p = (STRINGP (value)
4918 #ifdef HAVE_WINDOW_SYSTEM
4919 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4920 && valid_image_p (value))
4921 #endif /* not HAVE_WINDOW_SYSTEM */
4922 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4923
4924 if (valid_p && !display_replaced_p)
4925 {
4926 int retval = 1;
4927
4928 if (!it)
4929 {
4930 /* Callers need to know whether the display spec is any kind
4931 of `(space ...)' spec that is about to affect text-area
4932 display. */
4933 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4934 retval = 2;
4935 return retval;
4936 }
4937
4938 /* Save current settings of IT so that we can restore them
4939 when we are finished with the glyph property value. */
4940 push_it (it, position);
4941 it->from_overlay = overlay;
4942 it->from_disp_prop_p = 1;
4943
4944 if (NILP (location))
4945 it->area = TEXT_AREA;
4946 else if (EQ (location, Qleft_margin))
4947 it->area = LEFT_MARGIN_AREA;
4948 else
4949 it->area = RIGHT_MARGIN_AREA;
4950
4951 if (STRINGP (value))
4952 {
4953 it->string = value;
4954 it->multibyte_p = STRING_MULTIBYTE (it->string);
4955 it->current.overlay_string_index = -1;
4956 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4957 it->end_charpos = it->string_nchars = SCHARS (it->string);
4958 it->method = GET_FROM_STRING;
4959 it->stop_charpos = 0;
4960 it->prev_stop = 0;
4961 it->base_level_stop = 0;
4962 it->string_from_display_prop_p = 1;
4963 /* Say that we haven't consumed the characters with
4964 `display' property yet. The call to pop_it in
4965 set_iterator_to_next will clean this up. */
4966 if (BUFFERP (object))
4967 *position = start_pos;
4968
4969 /* Force paragraph direction to be that of the parent
4970 object. If the parent object's paragraph direction is
4971 not yet determined, default to L2R. */
4972 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4973 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4974 else
4975 it->paragraph_embedding = L2R;
4976
4977 /* Set up the bidi iterator for this display string. */
4978 if (it->bidi_p)
4979 {
4980 it->bidi_it.string.lstring = it->string;
4981 it->bidi_it.string.s = NULL;
4982 it->bidi_it.string.schars = it->end_charpos;
4983 it->bidi_it.string.bufpos = bufpos;
4984 it->bidi_it.string.from_disp_str = 1;
4985 it->bidi_it.string.unibyte = !it->multibyte_p;
4986 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4987 }
4988 }
4989 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4990 {
4991 it->method = GET_FROM_STRETCH;
4992 it->object = value;
4993 *position = it->position = start_pos;
4994 retval = 1 + (it->area == TEXT_AREA);
4995 }
4996 #ifdef HAVE_WINDOW_SYSTEM
4997 else
4998 {
4999 it->what = IT_IMAGE;
5000 it->image_id = lookup_image (it->f, value);
5001 it->position = start_pos;
5002 it->object = NILP (object) ? it->w->buffer : object;
5003 it->method = GET_FROM_IMAGE;
5004
5005 /* Say that we haven't consumed the characters with
5006 `display' property yet. The call to pop_it in
5007 set_iterator_to_next will clean this up. */
5008 *position = start_pos;
5009 }
5010 #endif /* HAVE_WINDOW_SYSTEM */
5011
5012 return retval;
5013 }
5014
5015 /* Invalid property or property not supported. Restore
5016 POSITION to what it was before. */
5017 *position = start_pos;
5018 return 0;
5019 }
5020
5021 /* Check if PROP is a display property value whose text should be
5022 treated as intangible. OVERLAY is the overlay from which PROP
5023 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5024 specify the buffer position covered by PROP. */
5025
5026 int
5027 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5028 ptrdiff_t charpos, ptrdiff_t bytepos)
5029 {
5030 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5031 struct text_pos position;
5032
5033 SET_TEXT_POS (position, charpos, bytepos);
5034 return handle_display_spec (NULL, prop, Qnil, overlay,
5035 &position, charpos, frame_window_p);
5036 }
5037
5038
5039 /* Return 1 if PROP is a display sub-property value containing STRING.
5040
5041 Implementation note: this and the following function are really
5042 special cases of handle_display_spec and
5043 handle_single_display_spec, and should ideally use the same code.
5044 Until they do, these two pairs must be consistent and must be
5045 modified in sync. */
5046
5047 static int
5048 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5049 {
5050 if (EQ (string, prop))
5051 return 1;
5052
5053 /* Skip over `when FORM'. */
5054 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5055 {
5056 prop = XCDR (prop);
5057 if (!CONSP (prop))
5058 return 0;
5059 /* Actually, the condition following `when' should be eval'ed,
5060 like handle_single_display_spec does, and we should return
5061 zero if it evaluates to nil. However, this function is
5062 called only when the buffer was already displayed and some
5063 glyph in the glyph matrix was found to come from a display
5064 string. Therefore, the condition was already evaluated, and
5065 the result was non-nil, otherwise the display string wouldn't
5066 have been displayed and we would have never been called for
5067 this property. Thus, we can skip the evaluation and assume
5068 its result is non-nil. */
5069 prop = XCDR (prop);
5070 }
5071
5072 if (CONSP (prop))
5073 /* Skip over `margin LOCATION'. */
5074 if (EQ (XCAR (prop), Qmargin))
5075 {
5076 prop = XCDR (prop);
5077 if (!CONSP (prop))
5078 return 0;
5079
5080 prop = XCDR (prop);
5081 if (!CONSP (prop))
5082 return 0;
5083 }
5084
5085 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5086 }
5087
5088
5089 /* Return 1 if STRING appears in the `display' property PROP. */
5090
5091 static int
5092 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5093 {
5094 if (CONSP (prop)
5095 && !EQ (XCAR (prop), Qwhen)
5096 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5097 {
5098 /* A list of sub-properties. */
5099 while (CONSP (prop))
5100 {
5101 if (single_display_spec_string_p (XCAR (prop), string))
5102 return 1;
5103 prop = XCDR (prop);
5104 }
5105 }
5106 else if (VECTORP (prop))
5107 {
5108 /* A vector of sub-properties. */
5109 ptrdiff_t i;
5110 for (i = 0; i < ASIZE (prop); ++i)
5111 if (single_display_spec_string_p (AREF (prop, i), string))
5112 return 1;
5113 }
5114 else
5115 return single_display_spec_string_p (prop, string);
5116
5117 return 0;
5118 }
5119
5120 /* Look for STRING in overlays and text properties in the current
5121 buffer, between character positions FROM and TO (excluding TO).
5122 BACK_P non-zero means look back (in this case, TO is supposed to be
5123 less than FROM).
5124 Value is the first character position where STRING was found, or
5125 zero if it wasn't found before hitting TO.
5126
5127 This function may only use code that doesn't eval because it is
5128 called asynchronously from note_mouse_highlight. */
5129
5130 static ptrdiff_t
5131 string_buffer_position_lim (Lisp_Object string,
5132 ptrdiff_t from, ptrdiff_t to, int back_p)
5133 {
5134 Lisp_Object limit, prop, pos;
5135 int found = 0;
5136
5137 pos = make_number (max (from, BEGV));
5138
5139 if (!back_p) /* looking forward */
5140 {
5141 limit = make_number (min (to, ZV));
5142 while (!found && !EQ (pos, limit))
5143 {
5144 prop = Fget_char_property (pos, Qdisplay, Qnil);
5145 if (!NILP (prop) && display_prop_string_p (prop, string))
5146 found = 1;
5147 else
5148 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5149 limit);
5150 }
5151 }
5152 else /* looking back */
5153 {
5154 limit = make_number (max (to, BEGV));
5155 while (!found && !EQ (pos, limit))
5156 {
5157 prop = Fget_char_property (pos, Qdisplay, Qnil);
5158 if (!NILP (prop) && display_prop_string_p (prop, string))
5159 found = 1;
5160 else
5161 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5162 limit);
5163 }
5164 }
5165
5166 return found ? XINT (pos) : 0;
5167 }
5168
5169 /* Determine which buffer position in current buffer STRING comes from.
5170 AROUND_CHARPOS is an approximate position where it could come from.
5171 Value is the buffer position or 0 if it couldn't be determined.
5172
5173 This function is necessary because we don't record buffer positions
5174 in glyphs generated from strings (to keep struct glyph small).
5175 This function may only use code that doesn't eval because it is
5176 called asynchronously from note_mouse_highlight. */
5177
5178 static ptrdiff_t
5179 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5180 {
5181 const int MAX_DISTANCE = 1000;
5182 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5183 around_charpos + MAX_DISTANCE,
5184 0);
5185
5186 if (!found)
5187 found = string_buffer_position_lim (string, around_charpos,
5188 around_charpos - MAX_DISTANCE, 1);
5189 return found;
5190 }
5191
5192
5193 \f
5194 /***********************************************************************
5195 `composition' property
5196 ***********************************************************************/
5197
5198 /* Set up iterator IT from `composition' property at its current
5199 position. Called from handle_stop. */
5200
5201 static enum prop_handled
5202 handle_composition_prop (struct it *it)
5203 {
5204 Lisp_Object prop, string;
5205 ptrdiff_t pos, pos_byte, start, end;
5206
5207 if (STRINGP (it->string))
5208 {
5209 unsigned char *s;
5210
5211 pos = IT_STRING_CHARPOS (*it);
5212 pos_byte = IT_STRING_BYTEPOS (*it);
5213 string = it->string;
5214 s = SDATA (string) + pos_byte;
5215 it->c = STRING_CHAR (s);
5216 }
5217 else
5218 {
5219 pos = IT_CHARPOS (*it);
5220 pos_byte = IT_BYTEPOS (*it);
5221 string = Qnil;
5222 it->c = FETCH_CHAR (pos_byte);
5223 }
5224
5225 /* If there's a valid composition and point is not inside of the
5226 composition (in the case that the composition is from the current
5227 buffer), draw a glyph composed from the composition components. */
5228 if (find_composition (pos, -1, &start, &end, &prop, string)
5229 && COMPOSITION_VALID_P (start, end, prop)
5230 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5231 {
5232 if (start < pos)
5233 /* As we can't handle this situation (perhaps font-lock added
5234 a new composition), we just return here hoping that next
5235 redisplay will detect this composition much earlier. */
5236 return HANDLED_NORMALLY;
5237 if (start != pos)
5238 {
5239 if (STRINGP (it->string))
5240 pos_byte = string_char_to_byte (it->string, start);
5241 else
5242 pos_byte = CHAR_TO_BYTE (start);
5243 }
5244 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5245 prop, string);
5246
5247 if (it->cmp_it.id >= 0)
5248 {
5249 it->cmp_it.ch = -1;
5250 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5251 it->cmp_it.nglyphs = -1;
5252 }
5253 }
5254
5255 return HANDLED_NORMALLY;
5256 }
5257
5258
5259 \f
5260 /***********************************************************************
5261 Overlay strings
5262 ***********************************************************************/
5263
5264 /* The following structure is used to record overlay strings for
5265 later sorting in load_overlay_strings. */
5266
5267 struct overlay_entry
5268 {
5269 Lisp_Object overlay;
5270 Lisp_Object string;
5271 EMACS_INT priority;
5272 int after_string_p;
5273 };
5274
5275
5276 /* Set up iterator IT from overlay strings at its current position.
5277 Called from handle_stop. */
5278
5279 static enum prop_handled
5280 handle_overlay_change (struct it *it)
5281 {
5282 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5283 return HANDLED_RECOMPUTE_PROPS;
5284 else
5285 return HANDLED_NORMALLY;
5286 }
5287
5288
5289 /* Set up the next overlay string for delivery by IT, if there is an
5290 overlay string to deliver. Called by set_iterator_to_next when the
5291 end of the current overlay string is reached. If there are more
5292 overlay strings to display, IT->string and
5293 IT->current.overlay_string_index are set appropriately here.
5294 Otherwise IT->string is set to nil. */
5295
5296 static void
5297 next_overlay_string (struct it *it)
5298 {
5299 ++it->current.overlay_string_index;
5300 if (it->current.overlay_string_index == it->n_overlay_strings)
5301 {
5302 /* No more overlay strings. Restore IT's settings to what
5303 they were before overlay strings were processed, and
5304 continue to deliver from current_buffer. */
5305
5306 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5307 pop_it (it);
5308 eassert (it->sp > 0
5309 || (NILP (it->string)
5310 && it->method == GET_FROM_BUFFER
5311 && it->stop_charpos >= BEGV
5312 && it->stop_charpos <= it->end_charpos));
5313 it->current.overlay_string_index = -1;
5314 it->n_overlay_strings = 0;
5315 it->overlay_strings_charpos = -1;
5316 /* If there's an empty display string on the stack, pop the
5317 stack, to resync the bidi iterator with IT's position. Such
5318 empty strings are pushed onto the stack in
5319 get_overlay_strings_1. */
5320 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5321 pop_it (it);
5322
5323 /* If we're at the end of the buffer, record that we have
5324 processed the overlay strings there already, so that
5325 next_element_from_buffer doesn't try it again. */
5326 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5327 it->overlay_strings_at_end_processed_p = 1;
5328 }
5329 else
5330 {
5331 /* There are more overlay strings to process. If
5332 IT->current.overlay_string_index has advanced to a position
5333 where we must load IT->overlay_strings with more strings, do
5334 it. We must load at the IT->overlay_strings_charpos where
5335 IT->n_overlay_strings was originally computed; when invisible
5336 text is present, this might not be IT_CHARPOS (Bug#7016). */
5337 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5338
5339 if (it->current.overlay_string_index && i == 0)
5340 load_overlay_strings (it, it->overlay_strings_charpos);
5341
5342 /* Initialize IT to deliver display elements from the overlay
5343 string. */
5344 it->string = it->overlay_strings[i];
5345 it->multibyte_p = STRING_MULTIBYTE (it->string);
5346 SET_TEXT_POS (it->current.string_pos, 0, 0);
5347 it->method = GET_FROM_STRING;
5348 it->stop_charpos = 0;
5349 if (it->cmp_it.stop_pos >= 0)
5350 it->cmp_it.stop_pos = 0;
5351 it->prev_stop = 0;
5352 it->base_level_stop = 0;
5353
5354 /* Set up the bidi iterator for this overlay string. */
5355 if (it->bidi_p)
5356 {
5357 it->bidi_it.string.lstring = it->string;
5358 it->bidi_it.string.s = NULL;
5359 it->bidi_it.string.schars = SCHARS (it->string);
5360 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5361 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5362 it->bidi_it.string.unibyte = !it->multibyte_p;
5363 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5364 }
5365 }
5366
5367 CHECK_IT (it);
5368 }
5369
5370
5371 /* Compare two overlay_entry structures E1 and E2. Used as a
5372 comparison function for qsort in load_overlay_strings. Overlay
5373 strings for the same position are sorted so that
5374
5375 1. All after-strings come in front of before-strings, except
5376 when they come from the same overlay.
5377
5378 2. Within after-strings, strings are sorted so that overlay strings
5379 from overlays with higher priorities come first.
5380
5381 2. Within before-strings, strings are sorted so that overlay
5382 strings from overlays with higher priorities come last.
5383
5384 Value is analogous to strcmp. */
5385
5386
5387 static int
5388 compare_overlay_entries (const void *e1, const void *e2)
5389 {
5390 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5391 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5392 int result;
5393
5394 if (entry1->after_string_p != entry2->after_string_p)
5395 {
5396 /* Let after-strings appear in front of before-strings if
5397 they come from different overlays. */
5398 if (EQ (entry1->overlay, entry2->overlay))
5399 result = entry1->after_string_p ? 1 : -1;
5400 else
5401 result = entry1->after_string_p ? -1 : 1;
5402 }
5403 else if (entry1->priority != entry2->priority)
5404 {
5405 if (entry1->after_string_p)
5406 /* After-strings sorted in order of decreasing priority. */
5407 result = entry2->priority < entry1->priority ? -1 : 1;
5408 else
5409 /* Before-strings sorted in order of increasing priority. */
5410 result = entry1->priority < entry2->priority ? -1 : 1;
5411 }
5412 else
5413 result = 0;
5414
5415 return result;
5416 }
5417
5418
5419 /* Load the vector IT->overlay_strings with overlay strings from IT's
5420 current buffer position, or from CHARPOS if that is > 0. Set
5421 IT->n_overlays to the total number of overlay strings found.
5422
5423 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5424 a time. On entry into load_overlay_strings,
5425 IT->current.overlay_string_index gives the number of overlay
5426 strings that have already been loaded by previous calls to this
5427 function.
5428
5429 IT->add_overlay_start contains an additional overlay start
5430 position to consider for taking overlay strings from, if non-zero.
5431 This position comes into play when the overlay has an `invisible'
5432 property, and both before and after-strings. When we've skipped to
5433 the end of the overlay, because of its `invisible' property, we
5434 nevertheless want its before-string to appear.
5435 IT->add_overlay_start will contain the overlay start position
5436 in this case.
5437
5438 Overlay strings are sorted so that after-string strings come in
5439 front of before-string strings. Within before and after-strings,
5440 strings are sorted by overlay priority. See also function
5441 compare_overlay_entries. */
5442
5443 static void
5444 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5445 {
5446 Lisp_Object overlay, window, str, invisible;
5447 struct Lisp_Overlay *ov;
5448 ptrdiff_t start, end;
5449 ptrdiff_t size = 20;
5450 ptrdiff_t n = 0, i, j;
5451 int invis_p;
5452 struct overlay_entry *entries = alloca (size * sizeof *entries);
5453 USE_SAFE_ALLOCA;
5454
5455 if (charpos <= 0)
5456 charpos = IT_CHARPOS (*it);
5457
5458 /* Append the overlay string STRING of overlay OVERLAY to vector
5459 `entries' which has size `size' and currently contains `n'
5460 elements. AFTER_P non-zero means STRING is an after-string of
5461 OVERLAY. */
5462 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5463 do \
5464 { \
5465 Lisp_Object priority; \
5466 \
5467 if (n == size) \
5468 { \
5469 struct overlay_entry *old = entries; \
5470 SAFE_NALLOCA (entries, 2, size); \
5471 memcpy (entries, old, size * sizeof *entries); \
5472 size *= 2; \
5473 } \
5474 \
5475 entries[n].string = (STRING); \
5476 entries[n].overlay = (OVERLAY); \
5477 priority = Foverlay_get ((OVERLAY), Qpriority); \
5478 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5479 entries[n].after_string_p = (AFTER_P); \
5480 ++n; \
5481 } \
5482 while (0)
5483
5484 /* Process overlay before the overlay center. */
5485 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5486 {
5487 XSETMISC (overlay, ov);
5488 eassert (OVERLAYP (overlay));
5489 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5490 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5491
5492 if (end < charpos)
5493 break;
5494
5495 /* Skip this overlay if it doesn't start or end at IT's current
5496 position. */
5497 if (end != charpos && start != charpos)
5498 continue;
5499
5500 /* Skip this overlay if it doesn't apply to IT->w. */
5501 window = Foverlay_get (overlay, Qwindow);
5502 if (WINDOWP (window) && XWINDOW (window) != it->w)
5503 continue;
5504
5505 /* If the text ``under'' the overlay is invisible, both before-
5506 and after-strings from this overlay are visible; start and
5507 end position are indistinguishable. */
5508 invisible = Foverlay_get (overlay, Qinvisible);
5509 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5510
5511 /* If overlay has a non-empty before-string, record it. */
5512 if ((start == charpos || (end == charpos && invis_p))
5513 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5514 && SCHARS (str))
5515 RECORD_OVERLAY_STRING (overlay, str, 0);
5516
5517 /* If overlay has a non-empty after-string, record it. */
5518 if ((end == charpos || (start == charpos && invis_p))
5519 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5520 && SCHARS (str))
5521 RECORD_OVERLAY_STRING (overlay, str, 1);
5522 }
5523
5524 /* Process overlays after the overlay center. */
5525 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5526 {
5527 XSETMISC (overlay, ov);
5528 eassert (OVERLAYP (overlay));
5529 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5530 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5531
5532 if (start > charpos)
5533 break;
5534
5535 /* Skip this overlay if it doesn't start or end at IT's current
5536 position. */
5537 if (end != charpos && start != charpos)
5538 continue;
5539
5540 /* Skip this overlay if it doesn't apply to IT->w. */
5541 window = Foverlay_get (overlay, Qwindow);
5542 if (WINDOWP (window) && XWINDOW (window) != it->w)
5543 continue;
5544
5545 /* If the text ``under'' the overlay is invisible, it has a zero
5546 dimension, and both before- and after-strings apply. */
5547 invisible = Foverlay_get (overlay, Qinvisible);
5548 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5549
5550 /* If overlay has a non-empty before-string, record it. */
5551 if ((start == charpos || (end == charpos && invis_p))
5552 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5553 && SCHARS (str))
5554 RECORD_OVERLAY_STRING (overlay, str, 0);
5555
5556 /* If overlay has a non-empty after-string, record it. */
5557 if ((end == charpos || (start == charpos && invis_p))
5558 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5559 && SCHARS (str))
5560 RECORD_OVERLAY_STRING (overlay, str, 1);
5561 }
5562
5563 #undef RECORD_OVERLAY_STRING
5564
5565 /* Sort entries. */
5566 if (n > 1)
5567 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5568
5569 /* Record number of overlay strings, and where we computed it. */
5570 it->n_overlay_strings = n;
5571 it->overlay_strings_charpos = charpos;
5572
5573 /* IT->current.overlay_string_index is the number of overlay strings
5574 that have already been consumed by IT. Copy some of the
5575 remaining overlay strings to IT->overlay_strings. */
5576 i = 0;
5577 j = it->current.overlay_string_index;
5578 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5579 {
5580 it->overlay_strings[i] = entries[j].string;
5581 it->string_overlays[i++] = entries[j++].overlay;
5582 }
5583
5584 CHECK_IT (it);
5585 SAFE_FREE ();
5586 }
5587
5588
5589 /* Get the first chunk of overlay strings at IT's current buffer
5590 position, or at CHARPOS if that is > 0. Value is non-zero if at
5591 least one overlay string was found. */
5592
5593 static int
5594 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5595 {
5596 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5597 process. This fills IT->overlay_strings with strings, and sets
5598 IT->n_overlay_strings to the total number of strings to process.
5599 IT->pos.overlay_string_index has to be set temporarily to zero
5600 because load_overlay_strings needs this; it must be set to -1
5601 when no overlay strings are found because a zero value would
5602 indicate a position in the first overlay string. */
5603 it->current.overlay_string_index = 0;
5604 load_overlay_strings (it, charpos);
5605
5606 /* If we found overlay strings, set up IT to deliver display
5607 elements from the first one. Otherwise set up IT to deliver
5608 from current_buffer. */
5609 if (it->n_overlay_strings)
5610 {
5611 /* Make sure we know settings in current_buffer, so that we can
5612 restore meaningful values when we're done with the overlay
5613 strings. */
5614 if (compute_stop_p)
5615 compute_stop_pos (it);
5616 eassert (it->face_id >= 0);
5617
5618 /* Save IT's settings. They are restored after all overlay
5619 strings have been processed. */
5620 eassert (!compute_stop_p || it->sp == 0);
5621
5622 /* When called from handle_stop, there might be an empty display
5623 string loaded. In that case, don't bother saving it. But
5624 don't use this optimization with the bidi iterator, since we
5625 need the corresponding pop_it call to resync the bidi
5626 iterator's position with IT's position, after we are done
5627 with the overlay strings. (The corresponding call to pop_it
5628 in case of an empty display string is in
5629 next_overlay_string.) */
5630 if (!(!it->bidi_p
5631 && STRINGP (it->string) && !SCHARS (it->string)))
5632 push_it (it, NULL);
5633
5634 /* Set up IT to deliver display elements from the first overlay
5635 string. */
5636 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5637 it->string = it->overlay_strings[0];
5638 it->from_overlay = Qnil;
5639 it->stop_charpos = 0;
5640 eassert (STRINGP (it->string));
5641 it->end_charpos = SCHARS (it->string);
5642 it->prev_stop = 0;
5643 it->base_level_stop = 0;
5644 it->multibyte_p = STRING_MULTIBYTE (it->string);
5645 it->method = GET_FROM_STRING;
5646 it->from_disp_prop_p = 0;
5647
5648 /* Force paragraph direction to be that of the parent
5649 buffer. */
5650 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5651 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5652 else
5653 it->paragraph_embedding = L2R;
5654
5655 /* Set up the bidi iterator for this overlay string. */
5656 if (it->bidi_p)
5657 {
5658 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5659
5660 it->bidi_it.string.lstring = it->string;
5661 it->bidi_it.string.s = NULL;
5662 it->bidi_it.string.schars = SCHARS (it->string);
5663 it->bidi_it.string.bufpos = pos;
5664 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5665 it->bidi_it.string.unibyte = !it->multibyte_p;
5666 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5667 }
5668 return 1;
5669 }
5670
5671 it->current.overlay_string_index = -1;
5672 return 0;
5673 }
5674
5675 static int
5676 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5677 {
5678 it->string = Qnil;
5679 it->method = GET_FROM_BUFFER;
5680
5681 (void) get_overlay_strings_1 (it, charpos, 1);
5682
5683 CHECK_IT (it);
5684
5685 /* Value is non-zero if we found at least one overlay string. */
5686 return STRINGP (it->string);
5687 }
5688
5689
5690 \f
5691 /***********************************************************************
5692 Saving and restoring state
5693 ***********************************************************************/
5694
5695 /* Save current settings of IT on IT->stack. Called, for example,
5696 before setting up IT for an overlay string, to be able to restore
5697 IT's settings to what they were after the overlay string has been
5698 processed. If POSITION is non-NULL, it is the position to save on
5699 the stack instead of IT->position. */
5700
5701 static void
5702 push_it (struct it *it, struct text_pos *position)
5703 {
5704 struct iterator_stack_entry *p;
5705
5706 eassert (it->sp < IT_STACK_SIZE);
5707 p = it->stack + it->sp;
5708
5709 p->stop_charpos = it->stop_charpos;
5710 p->prev_stop = it->prev_stop;
5711 p->base_level_stop = it->base_level_stop;
5712 p->cmp_it = it->cmp_it;
5713 eassert (it->face_id >= 0);
5714 p->face_id = it->face_id;
5715 p->string = it->string;
5716 p->method = it->method;
5717 p->from_overlay = it->from_overlay;
5718 switch (p->method)
5719 {
5720 case GET_FROM_IMAGE:
5721 p->u.image.object = it->object;
5722 p->u.image.image_id = it->image_id;
5723 p->u.image.slice = it->slice;
5724 break;
5725 case GET_FROM_STRETCH:
5726 p->u.stretch.object = it->object;
5727 break;
5728 }
5729 p->position = position ? *position : it->position;
5730 p->current = it->current;
5731 p->end_charpos = it->end_charpos;
5732 p->string_nchars = it->string_nchars;
5733 p->area = it->area;
5734 p->multibyte_p = it->multibyte_p;
5735 p->avoid_cursor_p = it->avoid_cursor_p;
5736 p->space_width = it->space_width;
5737 p->font_height = it->font_height;
5738 p->voffset = it->voffset;
5739 p->string_from_display_prop_p = it->string_from_display_prop_p;
5740 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5741 p->display_ellipsis_p = 0;
5742 p->line_wrap = it->line_wrap;
5743 p->bidi_p = it->bidi_p;
5744 p->paragraph_embedding = it->paragraph_embedding;
5745 p->from_disp_prop_p = it->from_disp_prop_p;
5746 ++it->sp;
5747
5748 /* Save the state of the bidi iterator as well. */
5749 if (it->bidi_p)
5750 bidi_push_it (&it->bidi_it);
5751 }
5752
5753 static void
5754 iterate_out_of_display_property (struct it *it)
5755 {
5756 int buffer_p = !STRINGP (it->string);
5757 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5758 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5759
5760 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5761
5762 /* Maybe initialize paragraph direction. If we are at the beginning
5763 of a new paragraph, next_element_from_buffer may not have a
5764 chance to do that. */
5765 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5766 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5767 /* prev_stop can be zero, so check against BEGV as well. */
5768 while (it->bidi_it.charpos >= bob
5769 && it->prev_stop <= it->bidi_it.charpos
5770 && it->bidi_it.charpos < CHARPOS (it->position)
5771 && it->bidi_it.charpos < eob)
5772 bidi_move_to_visually_next (&it->bidi_it);
5773 /* Record the stop_pos we just crossed, for when we cross it
5774 back, maybe. */
5775 if (it->bidi_it.charpos > CHARPOS (it->position))
5776 it->prev_stop = CHARPOS (it->position);
5777 /* If we ended up not where pop_it put us, resync IT's
5778 positional members with the bidi iterator. */
5779 if (it->bidi_it.charpos != CHARPOS (it->position))
5780 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5781 if (buffer_p)
5782 it->current.pos = it->position;
5783 else
5784 it->current.string_pos = it->position;
5785 }
5786
5787 /* Restore IT's settings from IT->stack. Called, for example, when no
5788 more overlay strings must be processed, and we return to delivering
5789 display elements from a buffer, or when the end of a string from a
5790 `display' property is reached and we return to delivering display
5791 elements from an overlay string, or from a buffer. */
5792
5793 static void
5794 pop_it (struct it *it)
5795 {
5796 struct iterator_stack_entry *p;
5797 int from_display_prop = it->from_disp_prop_p;
5798
5799 eassert (it->sp > 0);
5800 --it->sp;
5801 p = it->stack + it->sp;
5802 it->stop_charpos = p->stop_charpos;
5803 it->prev_stop = p->prev_stop;
5804 it->base_level_stop = p->base_level_stop;
5805 it->cmp_it = p->cmp_it;
5806 it->face_id = p->face_id;
5807 it->current = p->current;
5808 it->position = p->position;
5809 it->string = p->string;
5810 it->from_overlay = p->from_overlay;
5811 if (NILP (it->string))
5812 SET_TEXT_POS (it->current.string_pos, -1, -1);
5813 it->method = p->method;
5814 switch (it->method)
5815 {
5816 case GET_FROM_IMAGE:
5817 it->image_id = p->u.image.image_id;
5818 it->object = p->u.image.object;
5819 it->slice = p->u.image.slice;
5820 break;
5821 case GET_FROM_STRETCH:
5822 it->object = p->u.stretch.object;
5823 break;
5824 case GET_FROM_BUFFER:
5825 it->object = it->w->buffer;
5826 break;
5827 case GET_FROM_STRING:
5828 it->object = it->string;
5829 break;
5830 case GET_FROM_DISPLAY_VECTOR:
5831 if (it->s)
5832 it->method = GET_FROM_C_STRING;
5833 else if (STRINGP (it->string))
5834 it->method = GET_FROM_STRING;
5835 else
5836 {
5837 it->method = GET_FROM_BUFFER;
5838 it->object = it->w->buffer;
5839 }
5840 }
5841 it->end_charpos = p->end_charpos;
5842 it->string_nchars = p->string_nchars;
5843 it->area = p->area;
5844 it->multibyte_p = p->multibyte_p;
5845 it->avoid_cursor_p = p->avoid_cursor_p;
5846 it->space_width = p->space_width;
5847 it->font_height = p->font_height;
5848 it->voffset = p->voffset;
5849 it->string_from_display_prop_p = p->string_from_display_prop_p;
5850 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5851 it->line_wrap = p->line_wrap;
5852 it->bidi_p = p->bidi_p;
5853 it->paragraph_embedding = p->paragraph_embedding;
5854 it->from_disp_prop_p = p->from_disp_prop_p;
5855 if (it->bidi_p)
5856 {
5857 bidi_pop_it (&it->bidi_it);
5858 /* Bidi-iterate until we get out of the portion of text, if any,
5859 covered by a `display' text property or by an overlay with
5860 `display' property. (We cannot just jump there, because the
5861 internal coherency of the bidi iterator state can not be
5862 preserved across such jumps.) We also must determine the
5863 paragraph base direction if the overlay we just processed is
5864 at the beginning of a new paragraph. */
5865 if (from_display_prop
5866 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5867 iterate_out_of_display_property (it);
5868
5869 eassert ((BUFFERP (it->object)
5870 && IT_CHARPOS (*it) == it->bidi_it.charpos
5871 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5872 || (STRINGP (it->object)
5873 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5874 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5875 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5876 }
5877 }
5878
5879
5880 \f
5881 /***********************************************************************
5882 Moving over lines
5883 ***********************************************************************/
5884
5885 /* Set IT's current position to the previous line start. */
5886
5887 static void
5888 back_to_previous_line_start (struct it *it)
5889 {
5890 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5891 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5892 }
5893
5894
5895 /* Move IT to the next line start.
5896
5897 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5898 we skipped over part of the text (as opposed to moving the iterator
5899 continuously over the text). Otherwise, don't change the value
5900 of *SKIPPED_P.
5901
5902 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5903 iterator on the newline, if it was found.
5904
5905 Newlines may come from buffer text, overlay strings, or strings
5906 displayed via the `display' property. That's the reason we can't
5907 simply use find_next_newline_no_quit.
5908
5909 Note that this function may not skip over invisible text that is so
5910 because of text properties and immediately follows a newline. If
5911 it would, function reseat_at_next_visible_line_start, when called
5912 from set_iterator_to_next, would effectively make invisible
5913 characters following a newline part of the wrong glyph row, which
5914 leads to wrong cursor motion. */
5915
5916 static int
5917 forward_to_next_line_start (struct it *it, int *skipped_p,
5918 struct bidi_it *bidi_it_prev)
5919 {
5920 ptrdiff_t old_selective;
5921 int newline_found_p, n;
5922 const int MAX_NEWLINE_DISTANCE = 500;
5923
5924 /* If already on a newline, just consume it to avoid unintended
5925 skipping over invisible text below. */
5926 if (it->what == IT_CHARACTER
5927 && it->c == '\n'
5928 && CHARPOS (it->position) == IT_CHARPOS (*it))
5929 {
5930 if (it->bidi_p && bidi_it_prev)
5931 *bidi_it_prev = it->bidi_it;
5932 set_iterator_to_next (it, 0);
5933 it->c = 0;
5934 return 1;
5935 }
5936
5937 /* Don't handle selective display in the following. It's (a)
5938 unnecessary because it's done by the caller, and (b) leads to an
5939 infinite recursion because next_element_from_ellipsis indirectly
5940 calls this function. */
5941 old_selective = it->selective;
5942 it->selective = 0;
5943
5944 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5945 from buffer text. */
5946 for (n = newline_found_p = 0;
5947 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5948 n += STRINGP (it->string) ? 0 : 1)
5949 {
5950 if (!get_next_display_element (it))
5951 return 0;
5952 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5953 if (newline_found_p && it->bidi_p && bidi_it_prev)
5954 *bidi_it_prev = it->bidi_it;
5955 set_iterator_to_next (it, 0);
5956 }
5957
5958 /* If we didn't find a newline near enough, see if we can use a
5959 short-cut. */
5960 if (!newline_found_p)
5961 {
5962 ptrdiff_t start = IT_CHARPOS (*it);
5963 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5964 Lisp_Object pos;
5965
5966 eassert (!STRINGP (it->string));
5967
5968 /* If there isn't any `display' property in sight, and no
5969 overlays, we can just use the position of the newline in
5970 buffer text. */
5971 if (it->stop_charpos >= limit
5972 || ((pos = Fnext_single_property_change (make_number (start),
5973 Qdisplay, Qnil,
5974 make_number (limit)),
5975 NILP (pos))
5976 && next_overlay_change (start) == ZV))
5977 {
5978 if (!it->bidi_p)
5979 {
5980 IT_CHARPOS (*it) = limit;
5981 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5982 }
5983 else
5984 {
5985 struct bidi_it bprev;
5986
5987 /* Help bidi.c avoid expensive searches for display
5988 properties and overlays, by telling it that there are
5989 none up to `limit'. */
5990 if (it->bidi_it.disp_pos < limit)
5991 {
5992 it->bidi_it.disp_pos = limit;
5993 it->bidi_it.disp_prop = 0;
5994 }
5995 do {
5996 bprev = it->bidi_it;
5997 bidi_move_to_visually_next (&it->bidi_it);
5998 } while (it->bidi_it.charpos != limit);
5999 IT_CHARPOS (*it) = limit;
6000 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6001 if (bidi_it_prev)
6002 *bidi_it_prev = bprev;
6003 }
6004 *skipped_p = newline_found_p = 1;
6005 }
6006 else
6007 {
6008 while (get_next_display_element (it)
6009 && !newline_found_p)
6010 {
6011 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6012 if (newline_found_p && it->bidi_p && bidi_it_prev)
6013 *bidi_it_prev = it->bidi_it;
6014 set_iterator_to_next (it, 0);
6015 }
6016 }
6017 }
6018
6019 it->selective = old_selective;
6020 return newline_found_p;
6021 }
6022
6023
6024 /* Set IT's current position to the previous visible line start. Skip
6025 invisible text that is so either due to text properties or due to
6026 selective display. Caution: this does not change IT->current_x and
6027 IT->hpos. */
6028
6029 static void
6030 back_to_previous_visible_line_start (struct it *it)
6031 {
6032 while (IT_CHARPOS (*it) > BEGV)
6033 {
6034 back_to_previous_line_start (it);
6035
6036 if (IT_CHARPOS (*it) <= BEGV)
6037 break;
6038
6039 /* If selective > 0, then lines indented more than its value are
6040 invisible. */
6041 if (it->selective > 0
6042 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6043 it->selective))
6044 continue;
6045
6046 /* Check the newline before point for invisibility. */
6047 {
6048 Lisp_Object prop;
6049 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6050 Qinvisible, it->window);
6051 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6052 continue;
6053 }
6054
6055 if (IT_CHARPOS (*it) <= BEGV)
6056 break;
6057
6058 {
6059 struct it it2;
6060 void *it2data = NULL;
6061 ptrdiff_t pos;
6062 ptrdiff_t beg, end;
6063 Lisp_Object val, overlay;
6064
6065 SAVE_IT (it2, *it, it2data);
6066
6067 /* If newline is part of a composition, continue from start of composition */
6068 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6069 && beg < IT_CHARPOS (*it))
6070 goto replaced;
6071
6072 /* If newline is replaced by a display property, find start of overlay
6073 or interval and continue search from that point. */
6074 pos = --IT_CHARPOS (it2);
6075 --IT_BYTEPOS (it2);
6076 it2.sp = 0;
6077 bidi_unshelve_cache (NULL, 0);
6078 it2.string_from_display_prop_p = 0;
6079 it2.from_disp_prop_p = 0;
6080 if (handle_display_prop (&it2) == HANDLED_RETURN
6081 && !NILP (val = get_char_property_and_overlay
6082 (make_number (pos), Qdisplay, Qnil, &overlay))
6083 && (OVERLAYP (overlay)
6084 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6085 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6086 {
6087 RESTORE_IT (it, it, it2data);
6088 goto replaced;
6089 }
6090
6091 /* Newline is not replaced by anything -- so we are done. */
6092 RESTORE_IT (it, it, it2data);
6093 break;
6094
6095 replaced:
6096 if (beg < BEGV)
6097 beg = BEGV;
6098 IT_CHARPOS (*it) = beg;
6099 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6100 }
6101 }
6102
6103 it->continuation_lines_width = 0;
6104
6105 eassert (IT_CHARPOS (*it) >= BEGV);
6106 eassert (IT_CHARPOS (*it) == BEGV
6107 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6108 CHECK_IT (it);
6109 }
6110
6111
6112 /* Reseat iterator IT at the previous visible line start. Skip
6113 invisible text that is so either due to text properties or due to
6114 selective display. At the end, update IT's overlay information,
6115 face information etc. */
6116
6117 void
6118 reseat_at_previous_visible_line_start (struct it *it)
6119 {
6120 back_to_previous_visible_line_start (it);
6121 reseat (it, it->current.pos, 1);
6122 CHECK_IT (it);
6123 }
6124
6125
6126 /* Reseat iterator IT on the next visible line start in the current
6127 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6128 preceding the line start. Skip over invisible text that is so
6129 because of selective display. Compute faces, overlays etc at the
6130 new position. Note that this function does not skip over text that
6131 is invisible because of text properties. */
6132
6133 static void
6134 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6135 {
6136 int newline_found_p, skipped_p = 0;
6137 struct bidi_it bidi_it_prev;
6138
6139 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6140
6141 /* Skip over lines that are invisible because they are indented
6142 more than the value of IT->selective. */
6143 if (it->selective > 0)
6144 while (IT_CHARPOS (*it) < ZV
6145 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6146 it->selective))
6147 {
6148 eassert (IT_BYTEPOS (*it) == BEGV
6149 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6150 newline_found_p =
6151 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6152 }
6153
6154 /* Position on the newline if that's what's requested. */
6155 if (on_newline_p && newline_found_p)
6156 {
6157 if (STRINGP (it->string))
6158 {
6159 if (IT_STRING_CHARPOS (*it) > 0)
6160 {
6161 if (!it->bidi_p)
6162 {
6163 --IT_STRING_CHARPOS (*it);
6164 --IT_STRING_BYTEPOS (*it);
6165 }
6166 else
6167 {
6168 /* We need to restore the bidi iterator to the state
6169 it had on the newline, and resync the IT's
6170 position with that. */
6171 it->bidi_it = bidi_it_prev;
6172 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6173 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6174 }
6175 }
6176 }
6177 else if (IT_CHARPOS (*it) > BEGV)
6178 {
6179 if (!it->bidi_p)
6180 {
6181 --IT_CHARPOS (*it);
6182 --IT_BYTEPOS (*it);
6183 }
6184 else
6185 {
6186 /* We need to restore the bidi iterator to the state it
6187 had on the newline and resync IT with that. */
6188 it->bidi_it = bidi_it_prev;
6189 IT_CHARPOS (*it) = it->bidi_it.charpos;
6190 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6191 }
6192 reseat (it, it->current.pos, 0);
6193 }
6194 }
6195 else if (skipped_p)
6196 reseat (it, it->current.pos, 0);
6197
6198 CHECK_IT (it);
6199 }
6200
6201
6202 \f
6203 /***********************************************************************
6204 Changing an iterator's position
6205 ***********************************************************************/
6206
6207 /* Change IT's current position to POS in current_buffer. If FORCE_P
6208 is non-zero, always check for text properties at the new position.
6209 Otherwise, text properties are only looked up if POS >=
6210 IT->check_charpos of a property. */
6211
6212 static void
6213 reseat (struct it *it, struct text_pos pos, int force_p)
6214 {
6215 ptrdiff_t original_pos = IT_CHARPOS (*it);
6216
6217 reseat_1 (it, pos, 0);
6218
6219 /* Determine where to check text properties. Avoid doing it
6220 where possible because text property lookup is very expensive. */
6221 if (force_p
6222 || CHARPOS (pos) > it->stop_charpos
6223 || CHARPOS (pos) < original_pos)
6224 {
6225 if (it->bidi_p)
6226 {
6227 /* For bidi iteration, we need to prime prev_stop and
6228 base_level_stop with our best estimations. */
6229 /* Implementation note: Of course, POS is not necessarily a
6230 stop position, so assigning prev_pos to it is a lie; we
6231 should have called compute_stop_backwards. However, if
6232 the current buffer does not include any R2L characters,
6233 that call would be a waste of cycles, because the
6234 iterator will never move back, and thus never cross this
6235 "fake" stop position. So we delay that backward search
6236 until the time we really need it, in next_element_from_buffer. */
6237 if (CHARPOS (pos) != it->prev_stop)
6238 it->prev_stop = CHARPOS (pos);
6239 if (CHARPOS (pos) < it->base_level_stop)
6240 it->base_level_stop = 0; /* meaning it's unknown */
6241 handle_stop (it);
6242 }
6243 else
6244 {
6245 handle_stop (it);
6246 it->prev_stop = it->base_level_stop = 0;
6247 }
6248
6249 }
6250
6251 CHECK_IT (it);
6252 }
6253
6254
6255 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6256 IT->stop_pos to POS, also. */
6257
6258 static void
6259 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6260 {
6261 /* Don't call this function when scanning a C string. */
6262 eassert (it->s == NULL);
6263
6264 /* POS must be a reasonable value. */
6265 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6266
6267 it->current.pos = it->position = pos;
6268 it->end_charpos = ZV;
6269 it->dpvec = NULL;
6270 it->current.dpvec_index = -1;
6271 it->current.overlay_string_index = -1;
6272 IT_STRING_CHARPOS (*it) = -1;
6273 IT_STRING_BYTEPOS (*it) = -1;
6274 it->string = Qnil;
6275 it->method = GET_FROM_BUFFER;
6276 it->object = it->w->buffer;
6277 it->area = TEXT_AREA;
6278 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6279 it->sp = 0;
6280 it->string_from_display_prop_p = 0;
6281 it->string_from_prefix_prop_p = 0;
6282
6283 it->from_disp_prop_p = 0;
6284 it->face_before_selective_p = 0;
6285 if (it->bidi_p)
6286 {
6287 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6288 &it->bidi_it);
6289 bidi_unshelve_cache (NULL, 0);
6290 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6291 it->bidi_it.string.s = NULL;
6292 it->bidi_it.string.lstring = Qnil;
6293 it->bidi_it.string.bufpos = 0;
6294 it->bidi_it.string.unibyte = 0;
6295 }
6296
6297 if (set_stop_p)
6298 {
6299 it->stop_charpos = CHARPOS (pos);
6300 it->base_level_stop = CHARPOS (pos);
6301 }
6302 }
6303
6304
6305 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6306 If S is non-null, it is a C string to iterate over. Otherwise,
6307 STRING gives a Lisp string to iterate over.
6308
6309 If PRECISION > 0, don't return more then PRECISION number of
6310 characters from the string.
6311
6312 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6313 characters have been returned. FIELD_WIDTH < 0 means an infinite
6314 field width.
6315
6316 MULTIBYTE = 0 means disable processing of multibyte characters,
6317 MULTIBYTE > 0 means enable it,
6318 MULTIBYTE < 0 means use IT->multibyte_p.
6319
6320 IT must be initialized via a prior call to init_iterator before
6321 calling this function. */
6322
6323 static void
6324 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6325 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6326 int multibyte)
6327 {
6328 /* No region in strings. */
6329 it->region_beg_charpos = it->region_end_charpos = -1;
6330
6331 /* No text property checks performed by default, but see below. */
6332 it->stop_charpos = -1;
6333
6334 /* Set iterator position and end position. */
6335 memset (&it->current, 0, sizeof it->current);
6336 it->current.overlay_string_index = -1;
6337 it->current.dpvec_index = -1;
6338 eassert (charpos >= 0);
6339
6340 /* If STRING is specified, use its multibyteness, otherwise use the
6341 setting of MULTIBYTE, if specified. */
6342 if (multibyte >= 0)
6343 it->multibyte_p = multibyte > 0;
6344
6345 /* Bidirectional reordering of strings is controlled by the default
6346 value of bidi-display-reordering. Don't try to reorder while
6347 loading loadup.el, as the necessary character property tables are
6348 not yet available. */
6349 it->bidi_p =
6350 NILP (Vpurify_flag)
6351 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6352
6353 if (s == NULL)
6354 {
6355 eassert (STRINGP (string));
6356 it->string = string;
6357 it->s = NULL;
6358 it->end_charpos = it->string_nchars = SCHARS (string);
6359 it->method = GET_FROM_STRING;
6360 it->current.string_pos = string_pos (charpos, string);
6361
6362 if (it->bidi_p)
6363 {
6364 it->bidi_it.string.lstring = string;
6365 it->bidi_it.string.s = NULL;
6366 it->bidi_it.string.schars = it->end_charpos;
6367 it->bidi_it.string.bufpos = 0;
6368 it->bidi_it.string.from_disp_str = 0;
6369 it->bidi_it.string.unibyte = !it->multibyte_p;
6370 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6371 FRAME_WINDOW_P (it->f), &it->bidi_it);
6372 }
6373 }
6374 else
6375 {
6376 it->s = (const unsigned char *) s;
6377 it->string = Qnil;
6378
6379 /* Note that we use IT->current.pos, not it->current.string_pos,
6380 for displaying C strings. */
6381 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6382 if (it->multibyte_p)
6383 {
6384 it->current.pos = c_string_pos (charpos, s, 1);
6385 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6386 }
6387 else
6388 {
6389 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6390 it->end_charpos = it->string_nchars = strlen (s);
6391 }
6392
6393 if (it->bidi_p)
6394 {
6395 it->bidi_it.string.lstring = Qnil;
6396 it->bidi_it.string.s = (const unsigned char *) s;
6397 it->bidi_it.string.schars = it->end_charpos;
6398 it->bidi_it.string.bufpos = 0;
6399 it->bidi_it.string.from_disp_str = 0;
6400 it->bidi_it.string.unibyte = !it->multibyte_p;
6401 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6402 &it->bidi_it);
6403 }
6404 it->method = GET_FROM_C_STRING;
6405 }
6406
6407 /* PRECISION > 0 means don't return more than PRECISION characters
6408 from the string. */
6409 if (precision > 0 && it->end_charpos - charpos > precision)
6410 {
6411 it->end_charpos = it->string_nchars = charpos + precision;
6412 if (it->bidi_p)
6413 it->bidi_it.string.schars = it->end_charpos;
6414 }
6415
6416 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6417 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6418 FIELD_WIDTH < 0 means infinite field width. This is useful for
6419 padding with `-' at the end of a mode line. */
6420 if (field_width < 0)
6421 field_width = INFINITY;
6422 /* Implementation note: We deliberately don't enlarge
6423 it->bidi_it.string.schars here to fit it->end_charpos, because
6424 the bidi iterator cannot produce characters out of thin air. */
6425 if (field_width > it->end_charpos - charpos)
6426 it->end_charpos = charpos + field_width;
6427
6428 /* Use the standard display table for displaying strings. */
6429 if (DISP_TABLE_P (Vstandard_display_table))
6430 it->dp = XCHAR_TABLE (Vstandard_display_table);
6431
6432 it->stop_charpos = charpos;
6433 it->prev_stop = charpos;
6434 it->base_level_stop = 0;
6435 if (it->bidi_p)
6436 {
6437 it->bidi_it.first_elt = 1;
6438 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6439 it->bidi_it.disp_pos = -1;
6440 }
6441 if (s == NULL && it->multibyte_p)
6442 {
6443 ptrdiff_t endpos = SCHARS (it->string);
6444 if (endpos > it->end_charpos)
6445 endpos = it->end_charpos;
6446 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6447 it->string);
6448 }
6449 CHECK_IT (it);
6450 }
6451
6452
6453 \f
6454 /***********************************************************************
6455 Iteration
6456 ***********************************************************************/
6457
6458 /* Map enum it_method value to corresponding next_element_from_* function. */
6459
6460 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6461 {
6462 next_element_from_buffer,
6463 next_element_from_display_vector,
6464 next_element_from_string,
6465 next_element_from_c_string,
6466 next_element_from_image,
6467 next_element_from_stretch
6468 };
6469
6470 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6471
6472
6473 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6474 (possibly with the following characters). */
6475
6476 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6477 ((IT)->cmp_it.id >= 0 \
6478 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6479 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6480 END_CHARPOS, (IT)->w, \
6481 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6482 (IT)->string)))
6483
6484
6485 /* Lookup the char-table Vglyphless_char_display for character C (-1
6486 if we want information for no-font case), and return the display
6487 method symbol. By side-effect, update it->what and
6488 it->glyphless_method. This function is called from
6489 get_next_display_element for each character element, and from
6490 x_produce_glyphs when no suitable font was found. */
6491
6492 Lisp_Object
6493 lookup_glyphless_char_display (int c, struct it *it)
6494 {
6495 Lisp_Object glyphless_method = Qnil;
6496
6497 if (CHAR_TABLE_P (Vglyphless_char_display)
6498 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6499 {
6500 if (c >= 0)
6501 {
6502 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6503 if (CONSP (glyphless_method))
6504 glyphless_method = FRAME_WINDOW_P (it->f)
6505 ? XCAR (glyphless_method)
6506 : XCDR (glyphless_method);
6507 }
6508 else
6509 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6510 }
6511
6512 retry:
6513 if (NILP (glyphless_method))
6514 {
6515 if (c >= 0)
6516 /* The default is to display the character by a proper font. */
6517 return Qnil;
6518 /* The default for the no-font case is to display an empty box. */
6519 glyphless_method = Qempty_box;
6520 }
6521 if (EQ (glyphless_method, Qzero_width))
6522 {
6523 if (c >= 0)
6524 return glyphless_method;
6525 /* This method can't be used for the no-font case. */
6526 glyphless_method = Qempty_box;
6527 }
6528 if (EQ (glyphless_method, Qthin_space))
6529 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6530 else if (EQ (glyphless_method, Qempty_box))
6531 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6532 else if (EQ (glyphless_method, Qhex_code))
6533 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6534 else if (STRINGP (glyphless_method))
6535 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6536 else
6537 {
6538 /* Invalid value. We use the default method. */
6539 glyphless_method = Qnil;
6540 goto retry;
6541 }
6542 it->what = IT_GLYPHLESS;
6543 return glyphless_method;
6544 }
6545
6546 /* Load IT's display element fields with information about the next
6547 display element from the current position of IT. Value is zero if
6548 end of buffer (or C string) is reached. */
6549
6550 static struct frame *last_escape_glyph_frame = NULL;
6551 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6552 static int last_escape_glyph_merged_face_id = 0;
6553
6554 struct frame *last_glyphless_glyph_frame = NULL;
6555 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6556 int last_glyphless_glyph_merged_face_id = 0;
6557
6558 static int
6559 get_next_display_element (struct it *it)
6560 {
6561 /* Non-zero means that we found a display element. Zero means that
6562 we hit the end of what we iterate over. Performance note: the
6563 function pointer `method' used here turns out to be faster than
6564 using a sequence of if-statements. */
6565 int success_p;
6566
6567 get_next:
6568 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6569
6570 if (it->what == IT_CHARACTER)
6571 {
6572 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6573 and only if (a) the resolved directionality of that character
6574 is R..." */
6575 /* FIXME: Do we need an exception for characters from display
6576 tables? */
6577 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6578 it->c = bidi_mirror_char (it->c);
6579 /* Map via display table or translate control characters.
6580 IT->c, IT->len etc. have been set to the next character by
6581 the function call above. If we have a display table, and it
6582 contains an entry for IT->c, translate it. Don't do this if
6583 IT->c itself comes from a display table, otherwise we could
6584 end up in an infinite recursion. (An alternative could be to
6585 count the recursion depth of this function and signal an
6586 error when a certain maximum depth is reached.) Is it worth
6587 it? */
6588 if (success_p && it->dpvec == NULL)
6589 {
6590 Lisp_Object dv;
6591 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6592 int nonascii_space_p = 0;
6593 int nonascii_hyphen_p = 0;
6594 int c = it->c; /* This is the character to display. */
6595
6596 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6597 {
6598 eassert (SINGLE_BYTE_CHAR_P (c));
6599 if (unibyte_display_via_language_environment)
6600 {
6601 c = DECODE_CHAR (unibyte, c);
6602 if (c < 0)
6603 c = BYTE8_TO_CHAR (it->c);
6604 }
6605 else
6606 c = BYTE8_TO_CHAR (it->c);
6607 }
6608
6609 if (it->dp
6610 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6611 VECTORP (dv)))
6612 {
6613 struct Lisp_Vector *v = XVECTOR (dv);
6614
6615 /* Return the first character from the display table
6616 entry, if not empty. If empty, don't display the
6617 current character. */
6618 if (v->header.size)
6619 {
6620 it->dpvec_char_len = it->len;
6621 it->dpvec = v->contents;
6622 it->dpend = v->contents + v->header.size;
6623 it->current.dpvec_index = 0;
6624 it->dpvec_face_id = -1;
6625 it->saved_face_id = it->face_id;
6626 it->method = GET_FROM_DISPLAY_VECTOR;
6627 it->ellipsis_p = 0;
6628 }
6629 else
6630 {
6631 set_iterator_to_next (it, 0);
6632 }
6633 goto get_next;
6634 }
6635
6636 if (! NILP (lookup_glyphless_char_display (c, it)))
6637 {
6638 if (it->what == IT_GLYPHLESS)
6639 goto done;
6640 /* Don't display this character. */
6641 set_iterator_to_next (it, 0);
6642 goto get_next;
6643 }
6644
6645 /* If `nobreak-char-display' is non-nil, we display
6646 non-ASCII spaces and hyphens specially. */
6647 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6648 {
6649 if (c == 0xA0)
6650 nonascii_space_p = 1;
6651 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6652 nonascii_hyphen_p = 1;
6653 }
6654
6655 /* Translate control characters into `\003' or `^C' form.
6656 Control characters coming from a display table entry are
6657 currently not translated because we use IT->dpvec to hold
6658 the translation. This could easily be changed but I
6659 don't believe that it is worth doing.
6660
6661 The characters handled by `nobreak-char-display' must be
6662 translated too.
6663
6664 Non-printable characters and raw-byte characters are also
6665 translated to octal form. */
6666 if (((c < ' ' || c == 127) /* ASCII control chars */
6667 ? (it->area != TEXT_AREA
6668 /* In mode line, treat \n, \t like other crl chars. */
6669 || (c != '\t'
6670 && it->glyph_row
6671 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6672 || (c != '\n' && c != '\t'))
6673 : (nonascii_space_p
6674 || nonascii_hyphen_p
6675 || CHAR_BYTE8_P (c)
6676 || ! CHAR_PRINTABLE_P (c))))
6677 {
6678 /* C is a control character, non-ASCII space/hyphen,
6679 raw-byte, or a non-printable character which must be
6680 displayed either as '\003' or as `^C' where the '\\'
6681 and '^' can be defined in the display table. Fill
6682 IT->ctl_chars with glyphs for what we have to
6683 display. Then, set IT->dpvec to these glyphs. */
6684 Lisp_Object gc;
6685 int ctl_len;
6686 int face_id;
6687 int lface_id = 0;
6688 int escape_glyph;
6689
6690 /* Handle control characters with ^. */
6691
6692 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6693 {
6694 int g;
6695
6696 g = '^'; /* default glyph for Control */
6697 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6698 if (it->dp
6699 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6700 {
6701 g = GLYPH_CODE_CHAR (gc);
6702 lface_id = GLYPH_CODE_FACE (gc);
6703 }
6704 if (lface_id)
6705 {
6706 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6707 }
6708 else if (it->f == last_escape_glyph_frame
6709 && it->face_id == last_escape_glyph_face_id)
6710 {
6711 face_id = last_escape_glyph_merged_face_id;
6712 }
6713 else
6714 {
6715 /* Merge the escape-glyph face into the current face. */
6716 face_id = merge_faces (it->f, Qescape_glyph, 0,
6717 it->face_id);
6718 last_escape_glyph_frame = it->f;
6719 last_escape_glyph_face_id = it->face_id;
6720 last_escape_glyph_merged_face_id = face_id;
6721 }
6722
6723 XSETINT (it->ctl_chars[0], g);
6724 XSETINT (it->ctl_chars[1], c ^ 0100);
6725 ctl_len = 2;
6726 goto display_control;
6727 }
6728
6729 /* Handle non-ascii space in the mode where it only gets
6730 highlighting. */
6731
6732 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6733 {
6734 /* Merge `nobreak-space' into the current face. */
6735 face_id = merge_faces (it->f, Qnobreak_space, 0,
6736 it->face_id);
6737 XSETINT (it->ctl_chars[0], ' ');
6738 ctl_len = 1;
6739 goto display_control;
6740 }
6741
6742 /* Handle sequences that start with the "escape glyph". */
6743
6744 /* the default escape glyph is \. */
6745 escape_glyph = '\\';
6746
6747 if (it->dp
6748 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6749 {
6750 escape_glyph = GLYPH_CODE_CHAR (gc);
6751 lface_id = GLYPH_CODE_FACE (gc);
6752 }
6753 if (lface_id)
6754 {
6755 /* The display table specified a face.
6756 Merge it into face_id and also into escape_glyph. */
6757 face_id = merge_faces (it->f, Qt, lface_id,
6758 it->face_id);
6759 }
6760 else if (it->f == last_escape_glyph_frame
6761 && it->face_id == last_escape_glyph_face_id)
6762 {
6763 face_id = last_escape_glyph_merged_face_id;
6764 }
6765 else
6766 {
6767 /* Merge the escape-glyph face into the current face. */
6768 face_id = merge_faces (it->f, Qescape_glyph, 0,
6769 it->face_id);
6770 last_escape_glyph_frame = it->f;
6771 last_escape_glyph_face_id = it->face_id;
6772 last_escape_glyph_merged_face_id = face_id;
6773 }
6774
6775 /* Draw non-ASCII hyphen with just highlighting: */
6776
6777 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6778 {
6779 XSETINT (it->ctl_chars[0], '-');
6780 ctl_len = 1;
6781 goto display_control;
6782 }
6783
6784 /* Draw non-ASCII space/hyphen with escape glyph: */
6785
6786 if (nonascii_space_p || nonascii_hyphen_p)
6787 {
6788 XSETINT (it->ctl_chars[0], escape_glyph);
6789 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6790 ctl_len = 2;
6791 goto display_control;
6792 }
6793
6794 {
6795 char str[10];
6796 int len, i;
6797
6798 if (CHAR_BYTE8_P (c))
6799 /* Display \200 instead of \17777600. */
6800 c = CHAR_TO_BYTE8 (c);
6801 len = sprintf (str, "%03o", c);
6802
6803 XSETINT (it->ctl_chars[0], escape_glyph);
6804 for (i = 0; i < len; i++)
6805 XSETINT (it->ctl_chars[i + 1], str[i]);
6806 ctl_len = len + 1;
6807 }
6808
6809 display_control:
6810 /* Set up IT->dpvec and return first character from it. */
6811 it->dpvec_char_len = it->len;
6812 it->dpvec = it->ctl_chars;
6813 it->dpend = it->dpvec + ctl_len;
6814 it->current.dpvec_index = 0;
6815 it->dpvec_face_id = face_id;
6816 it->saved_face_id = it->face_id;
6817 it->method = GET_FROM_DISPLAY_VECTOR;
6818 it->ellipsis_p = 0;
6819 goto get_next;
6820 }
6821 it->char_to_display = c;
6822 }
6823 else if (success_p)
6824 {
6825 it->char_to_display = it->c;
6826 }
6827 }
6828
6829 /* Adjust face id for a multibyte character. There are no multibyte
6830 character in unibyte text. */
6831 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6832 && it->multibyte_p
6833 && success_p
6834 && FRAME_WINDOW_P (it->f))
6835 {
6836 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6837
6838 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6839 {
6840 /* Automatic composition with glyph-string. */
6841 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6842
6843 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6844 }
6845 else
6846 {
6847 ptrdiff_t pos = (it->s ? -1
6848 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6849 : IT_CHARPOS (*it));
6850 int c;
6851
6852 if (it->what == IT_CHARACTER)
6853 c = it->char_to_display;
6854 else
6855 {
6856 struct composition *cmp = composition_table[it->cmp_it.id];
6857 int i;
6858
6859 c = ' ';
6860 for (i = 0; i < cmp->glyph_len; i++)
6861 /* TAB in a composition means display glyphs with
6862 padding space on the left or right. */
6863 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6864 break;
6865 }
6866 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6867 }
6868 }
6869
6870 done:
6871 /* Is this character the last one of a run of characters with
6872 box? If yes, set IT->end_of_box_run_p to 1. */
6873 if (it->face_box_p
6874 && it->s == NULL)
6875 {
6876 if (it->method == GET_FROM_STRING && it->sp)
6877 {
6878 int face_id = underlying_face_id (it);
6879 struct face *face = FACE_FROM_ID (it->f, face_id);
6880
6881 if (face)
6882 {
6883 if (face->box == FACE_NO_BOX)
6884 {
6885 /* If the box comes from face properties in a
6886 display string, check faces in that string. */
6887 int string_face_id = face_after_it_pos (it);
6888 it->end_of_box_run_p
6889 = (FACE_FROM_ID (it->f, string_face_id)->box
6890 == FACE_NO_BOX);
6891 }
6892 /* Otherwise, the box comes from the underlying face.
6893 If this is the last string character displayed, check
6894 the next buffer location. */
6895 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6896 && (it->current.overlay_string_index
6897 == it->n_overlay_strings - 1))
6898 {
6899 ptrdiff_t ignore;
6900 int next_face_id;
6901 struct text_pos pos = it->current.pos;
6902 INC_TEXT_POS (pos, it->multibyte_p);
6903
6904 next_face_id = face_at_buffer_position
6905 (it->w, CHARPOS (pos), it->region_beg_charpos,
6906 it->region_end_charpos, &ignore,
6907 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6908 -1);
6909 it->end_of_box_run_p
6910 = (FACE_FROM_ID (it->f, next_face_id)->box
6911 == FACE_NO_BOX);
6912 }
6913 }
6914 }
6915 else
6916 {
6917 int face_id = face_after_it_pos (it);
6918 it->end_of_box_run_p
6919 = (face_id != it->face_id
6920 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6921 }
6922 }
6923 /* If we reached the end of the object we've been iterating (e.g., a
6924 display string or an overlay string), and there's something on
6925 IT->stack, proceed with what's on the stack. It doesn't make
6926 sense to return zero if there's unprocessed stuff on the stack,
6927 because otherwise that stuff will never be displayed. */
6928 if (!success_p && it->sp > 0)
6929 {
6930 set_iterator_to_next (it, 0);
6931 success_p = get_next_display_element (it);
6932 }
6933
6934 /* Value is 0 if end of buffer or string reached. */
6935 return success_p;
6936 }
6937
6938
6939 /* Move IT to the next display element.
6940
6941 RESEAT_P non-zero means if called on a newline in buffer text,
6942 skip to the next visible line start.
6943
6944 Functions get_next_display_element and set_iterator_to_next are
6945 separate because I find this arrangement easier to handle than a
6946 get_next_display_element function that also increments IT's
6947 position. The way it is we can first look at an iterator's current
6948 display element, decide whether it fits on a line, and if it does,
6949 increment the iterator position. The other way around we probably
6950 would either need a flag indicating whether the iterator has to be
6951 incremented the next time, or we would have to implement a
6952 decrement position function which would not be easy to write. */
6953
6954 void
6955 set_iterator_to_next (struct it *it, int reseat_p)
6956 {
6957 /* Reset flags indicating start and end of a sequence of characters
6958 with box. Reset them at the start of this function because
6959 moving the iterator to a new position might set them. */
6960 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6961
6962 switch (it->method)
6963 {
6964 case GET_FROM_BUFFER:
6965 /* The current display element of IT is a character from
6966 current_buffer. Advance in the buffer, and maybe skip over
6967 invisible lines that are so because of selective display. */
6968 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6969 reseat_at_next_visible_line_start (it, 0);
6970 else if (it->cmp_it.id >= 0)
6971 {
6972 /* We are currently getting glyphs from a composition. */
6973 int i;
6974
6975 if (! it->bidi_p)
6976 {
6977 IT_CHARPOS (*it) += it->cmp_it.nchars;
6978 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6979 if (it->cmp_it.to < it->cmp_it.nglyphs)
6980 {
6981 it->cmp_it.from = it->cmp_it.to;
6982 }
6983 else
6984 {
6985 it->cmp_it.id = -1;
6986 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6987 IT_BYTEPOS (*it),
6988 it->end_charpos, Qnil);
6989 }
6990 }
6991 else if (! it->cmp_it.reversed_p)
6992 {
6993 /* Composition created while scanning forward. */
6994 /* Update IT's char/byte positions to point to the first
6995 character of the next grapheme cluster, or to the
6996 character visually after the current composition. */
6997 for (i = 0; i < it->cmp_it.nchars; i++)
6998 bidi_move_to_visually_next (&it->bidi_it);
6999 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7000 IT_CHARPOS (*it) = it->bidi_it.charpos;
7001
7002 if (it->cmp_it.to < it->cmp_it.nglyphs)
7003 {
7004 /* Proceed to the next grapheme cluster. */
7005 it->cmp_it.from = it->cmp_it.to;
7006 }
7007 else
7008 {
7009 /* No more grapheme clusters in this composition.
7010 Find the next stop position. */
7011 ptrdiff_t stop = it->end_charpos;
7012 if (it->bidi_it.scan_dir < 0)
7013 /* Now we are scanning backward and don't know
7014 where to stop. */
7015 stop = -1;
7016 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7017 IT_BYTEPOS (*it), stop, Qnil);
7018 }
7019 }
7020 else
7021 {
7022 /* Composition created while scanning backward. */
7023 /* Update IT's char/byte positions to point to the last
7024 character of the previous grapheme cluster, or the
7025 character visually after the current composition. */
7026 for (i = 0; i < it->cmp_it.nchars; i++)
7027 bidi_move_to_visually_next (&it->bidi_it);
7028 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7029 IT_CHARPOS (*it) = it->bidi_it.charpos;
7030 if (it->cmp_it.from > 0)
7031 {
7032 /* Proceed to the previous grapheme cluster. */
7033 it->cmp_it.to = it->cmp_it.from;
7034 }
7035 else
7036 {
7037 /* No more grapheme clusters in this composition.
7038 Find the next stop position. */
7039 ptrdiff_t stop = it->end_charpos;
7040 if (it->bidi_it.scan_dir < 0)
7041 /* Now we are scanning backward and don't know
7042 where to stop. */
7043 stop = -1;
7044 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7045 IT_BYTEPOS (*it), stop, Qnil);
7046 }
7047 }
7048 }
7049 else
7050 {
7051 eassert (it->len != 0);
7052
7053 if (!it->bidi_p)
7054 {
7055 IT_BYTEPOS (*it) += it->len;
7056 IT_CHARPOS (*it) += 1;
7057 }
7058 else
7059 {
7060 int prev_scan_dir = it->bidi_it.scan_dir;
7061 /* If this is a new paragraph, determine its base
7062 direction (a.k.a. its base embedding level). */
7063 if (it->bidi_it.new_paragraph)
7064 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7065 bidi_move_to_visually_next (&it->bidi_it);
7066 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7067 IT_CHARPOS (*it) = it->bidi_it.charpos;
7068 if (prev_scan_dir != it->bidi_it.scan_dir)
7069 {
7070 /* As the scan direction was changed, we must
7071 re-compute the stop position for composition. */
7072 ptrdiff_t stop = it->end_charpos;
7073 if (it->bidi_it.scan_dir < 0)
7074 stop = -1;
7075 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7076 IT_BYTEPOS (*it), stop, Qnil);
7077 }
7078 }
7079 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7080 }
7081 break;
7082
7083 case GET_FROM_C_STRING:
7084 /* Current display element of IT is from a C string. */
7085 if (!it->bidi_p
7086 /* If the string position is beyond string's end, it means
7087 next_element_from_c_string is padding the string with
7088 blanks, in which case we bypass the bidi iterator,
7089 because it cannot deal with such virtual characters. */
7090 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7091 {
7092 IT_BYTEPOS (*it) += it->len;
7093 IT_CHARPOS (*it) += 1;
7094 }
7095 else
7096 {
7097 bidi_move_to_visually_next (&it->bidi_it);
7098 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7099 IT_CHARPOS (*it) = it->bidi_it.charpos;
7100 }
7101 break;
7102
7103 case GET_FROM_DISPLAY_VECTOR:
7104 /* Current display element of IT is from a display table entry.
7105 Advance in the display table definition. Reset it to null if
7106 end reached, and continue with characters from buffers/
7107 strings. */
7108 ++it->current.dpvec_index;
7109
7110 /* Restore face of the iterator to what they were before the
7111 display vector entry (these entries may contain faces). */
7112 it->face_id = it->saved_face_id;
7113
7114 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7115 {
7116 int recheck_faces = it->ellipsis_p;
7117
7118 if (it->s)
7119 it->method = GET_FROM_C_STRING;
7120 else if (STRINGP (it->string))
7121 it->method = GET_FROM_STRING;
7122 else
7123 {
7124 it->method = GET_FROM_BUFFER;
7125 it->object = it->w->buffer;
7126 }
7127
7128 it->dpvec = NULL;
7129 it->current.dpvec_index = -1;
7130
7131 /* Skip over characters which were displayed via IT->dpvec. */
7132 if (it->dpvec_char_len < 0)
7133 reseat_at_next_visible_line_start (it, 1);
7134 else if (it->dpvec_char_len > 0)
7135 {
7136 if (it->method == GET_FROM_STRING
7137 && it->n_overlay_strings > 0)
7138 it->ignore_overlay_strings_at_pos_p = 1;
7139 it->len = it->dpvec_char_len;
7140 set_iterator_to_next (it, reseat_p);
7141 }
7142
7143 /* Maybe recheck faces after display vector */
7144 if (recheck_faces)
7145 it->stop_charpos = IT_CHARPOS (*it);
7146 }
7147 break;
7148
7149 case GET_FROM_STRING:
7150 /* Current display element is a character from a Lisp string. */
7151 eassert (it->s == NULL && STRINGP (it->string));
7152 /* Don't advance past string end. These conditions are true
7153 when set_iterator_to_next is called at the end of
7154 get_next_display_element, in which case the Lisp string is
7155 already exhausted, and all we want is pop the iterator
7156 stack. */
7157 if (it->current.overlay_string_index >= 0)
7158 {
7159 /* This is an overlay string, so there's no padding with
7160 spaces, and the number of characters in the string is
7161 where the string ends. */
7162 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7163 goto consider_string_end;
7164 }
7165 else
7166 {
7167 /* Not an overlay string. There could be padding, so test
7168 against it->end_charpos . */
7169 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7170 goto consider_string_end;
7171 }
7172 if (it->cmp_it.id >= 0)
7173 {
7174 int i;
7175
7176 if (! it->bidi_p)
7177 {
7178 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7179 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7180 if (it->cmp_it.to < it->cmp_it.nglyphs)
7181 it->cmp_it.from = it->cmp_it.to;
7182 else
7183 {
7184 it->cmp_it.id = -1;
7185 composition_compute_stop_pos (&it->cmp_it,
7186 IT_STRING_CHARPOS (*it),
7187 IT_STRING_BYTEPOS (*it),
7188 it->end_charpos, it->string);
7189 }
7190 }
7191 else if (! it->cmp_it.reversed_p)
7192 {
7193 for (i = 0; i < it->cmp_it.nchars; i++)
7194 bidi_move_to_visually_next (&it->bidi_it);
7195 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7196 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7197
7198 if (it->cmp_it.to < it->cmp_it.nglyphs)
7199 it->cmp_it.from = it->cmp_it.to;
7200 else
7201 {
7202 ptrdiff_t stop = it->end_charpos;
7203 if (it->bidi_it.scan_dir < 0)
7204 stop = -1;
7205 composition_compute_stop_pos (&it->cmp_it,
7206 IT_STRING_CHARPOS (*it),
7207 IT_STRING_BYTEPOS (*it), stop,
7208 it->string);
7209 }
7210 }
7211 else
7212 {
7213 for (i = 0; i < it->cmp_it.nchars; i++)
7214 bidi_move_to_visually_next (&it->bidi_it);
7215 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7216 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7217 if (it->cmp_it.from > 0)
7218 it->cmp_it.to = it->cmp_it.from;
7219 else
7220 {
7221 ptrdiff_t stop = it->end_charpos;
7222 if (it->bidi_it.scan_dir < 0)
7223 stop = -1;
7224 composition_compute_stop_pos (&it->cmp_it,
7225 IT_STRING_CHARPOS (*it),
7226 IT_STRING_BYTEPOS (*it), stop,
7227 it->string);
7228 }
7229 }
7230 }
7231 else
7232 {
7233 if (!it->bidi_p
7234 /* If the string position is beyond string's end, it
7235 means next_element_from_string is padding the string
7236 with blanks, in which case we bypass the bidi
7237 iterator, because it cannot deal with such virtual
7238 characters. */
7239 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7240 {
7241 IT_STRING_BYTEPOS (*it) += it->len;
7242 IT_STRING_CHARPOS (*it) += 1;
7243 }
7244 else
7245 {
7246 int prev_scan_dir = it->bidi_it.scan_dir;
7247
7248 bidi_move_to_visually_next (&it->bidi_it);
7249 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7250 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7251 if (prev_scan_dir != it->bidi_it.scan_dir)
7252 {
7253 ptrdiff_t stop = it->end_charpos;
7254
7255 if (it->bidi_it.scan_dir < 0)
7256 stop = -1;
7257 composition_compute_stop_pos (&it->cmp_it,
7258 IT_STRING_CHARPOS (*it),
7259 IT_STRING_BYTEPOS (*it), stop,
7260 it->string);
7261 }
7262 }
7263 }
7264
7265 consider_string_end:
7266
7267 if (it->current.overlay_string_index >= 0)
7268 {
7269 /* IT->string is an overlay string. Advance to the
7270 next, if there is one. */
7271 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7272 {
7273 it->ellipsis_p = 0;
7274 next_overlay_string (it);
7275 if (it->ellipsis_p)
7276 setup_for_ellipsis (it, 0);
7277 }
7278 }
7279 else
7280 {
7281 /* IT->string is not an overlay string. If we reached
7282 its end, and there is something on IT->stack, proceed
7283 with what is on the stack. This can be either another
7284 string, this time an overlay string, or a buffer. */
7285 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7286 && it->sp > 0)
7287 {
7288 pop_it (it);
7289 if (it->method == GET_FROM_STRING)
7290 goto consider_string_end;
7291 }
7292 }
7293 break;
7294
7295 case GET_FROM_IMAGE:
7296 case GET_FROM_STRETCH:
7297 /* The position etc with which we have to proceed are on
7298 the stack. The position may be at the end of a string,
7299 if the `display' property takes up the whole string. */
7300 eassert (it->sp > 0);
7301 pop_it (it);
7302 if (it->method == GET_FROM_STRING)
7303 goto consider_string_end;
7304 break;
7305
7306 default:
7307 /* There are no other methods defined, so this should be a bug. */
7308 emacs_abort ();
7309 }
7310
7311 eassert (it->method != GET_FROM_STRING
7312 || (STRINGP (it->string)
7313 && IT_STRING_CHARPOS (*it) >= 0));
7314 }
7315
7316 /* Load IT's display element fields with information about the next
7317 display element which comes from a display table entry or from the
7318 result of translating a control character to one of the forms `^C'
7319 or `\003'.
7320
7321 IT->dpvec holds the glyphs to return as characters.
7322 IT->saved_face_id holds the face id before the display vector--it
7323 is restored into IT->face_id in set_iterator_to_next. */
7324
7325 static int
7326 next_element_from_display_vector (struct it *it)
7327 {
7328 Lisp_Object gc;
7329
7330 /* Precondition. */
7331 eassert (it->dpvec && it->current.dpvec_index >= 0);
7332
7333 it->face_id = it->saved_face_id;
7334
7335 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7336 That seemed totally bogus - so I changed it... */
7337 gc = it->dpvec[it->current.dpvec_index];
7338
7339 if (GLYPH_CODE_P (gc))
7340 {
7341 it->c = GLYPH_CODE_CHAR (gc);
7342 it->len = CHAR_BYTES (it->c);
7343
7344 /* The entry may contain a face id to use. Such a face id is
7345 the id of a Lisp face, not a realized face. A face id of
7346 zero means no face is specified. */
7347 if (it->dpvec_face_id >= 0)
7348 it->face_id = it->dpvec_face_id;
7349 else
7350 {
7351 int lface_id = GLYPH_CODE_FACE (gc);
7352 if (lface_id > 0)
7353 it->face_id = merge_faces (it->f, Qt, lface_id,
7354 it->saved_face_id);
7355 }
7356 }
7357 else
7358 /* Display table entry is invalid. Return a space. */
7359 it->c = ' ', it->len = 1;
7360
7361 /* Don't change position and object of the iterator here. They are
7362 still the values of the character that had this display table
7363 entry or was translated, and that's what we want. */
7364 it->what = IT_CHARACTER;
7365 return 1;
7366 }
7367
7368 /* Get the first element of string/buffer in the visual order, after
7369 being reseated to a new position in a string or a buffer. */
7370 static void
7371 get_visually_first_element (struct it *it)
7372 {
7373 int string_p = STRINGP (it->string) || it->s;
7374 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7375 ptrdiff_t bob = (string_p ? 0 : BEGV);
7376
7377 if (STRINGP (it->string))
7378 {
7379 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7380 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7381 }
7382 else
7383 {
7384 it->bidi_it.charpos = IT_CHARPOS (*it);
7385 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7386 }
7387
7388 if (it->bidi_it.charpos == eob)
7389 {
7390 /* Nothing to do, but reset the FIRST_ELT flag, like
7391 bidi_paragraph_init does, because we are not going to
7392 call it. */
7393 it->bidi_it.first_elt = 0;
7394 }
7395 else if (it->bidi_it.charpos == bob
7396 || (!string_p
7397 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7398 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7399 {
7400 /* If we are at the beginning of a line/string, we can produce
7401 the next element right away. */
7402 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7403 bidi_move_to_visually_next (&it->bidi_it);
7404 }
7405 else
7406 {
7407 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7408
7409 /* We need to prime the bidi iterator starting at the line's or
7410 string's beginning, before we will be able to produce the
7411 next element. */
7412 if (string_p)
7413 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7414 else
7415 {
7416 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7417 -1);
7418 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7419 }
7420 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7421 do
7422 {
7423 /* Now return to buffer/string position where we were asked
7424 to get the next display element, and produce that. */
7425 bidi_move_to_visually_next (&it->bidi_it);
7426 }
7427 while (it->bidi_it.bytepos != orig_bytepos
7428 && it->bidi_it.charpos < eob);
7429 }
7430
7431 /* Adjust IT's position information to where we ended up. */
7432 if (STRINGP (it->string))
7433 {
7434 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7435 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7436 }
7437 else
7438 {
7439 IT_CHARPOS (*it) = it->bidi_it.charpos;
7440 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7441 }
7442
7443 if (STRINGP (it->string) || !it->s)
7444 {
7445 ptrdiff_t stop, charpos, bytepos;
7446
7447 if (STRINGP (it->string))
7448 {
7449 eassert (!it->s);
7450 stop = SCHARS (it->string);
7451 if (stop > it->end_charpos)
7452 stop = it->end_charpos;
7453 charpos = IT_STRING_CHARPOS (*it);
7454 bytepos = IT_STRING_BYTEPOS (*it);
7455 }
7456 else
7457 {
7458 stop = it->end_charpos;
7459 charpos = IT_CHARPOS (*it);
7460 bytepos = IT_BYTEPOS (*it);
7461 }
7462 if (it->bidi_it.scan_dir < 0)
7463 stop = -1;
7464 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7465 it->string);
7466 }
7467 }
7468
7469 /* Load IT with the next display element from Lisp string IT->string.
7470 IT->current.string_pos is the current position within the string.
7471 If IT->current.overlay_string_index >= 0, the Lisp string is an
7472 overlay string. */
7473
7474 static int
7475 next_element_from_string (struct it *it)
7476 {
7477 struct text_pos position;
7478
7479 eassert (STRINGP (it->string));
7480 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7481 eassert (IT_STRING_CHARPOS (*it) >= 0);
7482 position = it->current.string_pos;
7483
7484 /* With bidi reordering, the character to display might not be the
7485 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7486 that we were reseat()ed to a new string, whose paragraph
7487 direction is not known. */
7488 if (it->bidi_p && it->bidi_it.first_elt)
7489 {
7490 get_visually_first_element (it);
7491 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7492 }
7493
7494 /* Time to check for invisible text? */
7495 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7496 {
7497 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7498 {
7499 if (!(!it->bidi_p
7500 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7501 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7502 {
7503 /* With bidi non-linear iteration, we could find
7504 ourselves far beyond the last computed stop_charpos,
7505 with several other stop positions in between that we
7506 missed. Scan them all now, in buffer's logical
7507 order, until we find and handle the last stop_charpos
7508 that precedes our current position. */
7509 handle_stop_backwards (it, it->stop_charpos);
7510 return GET_NEXT_DISPLAY_ELEMENT (it);
7511 }
7512 else
7513 {
7514 if (it->bidi_p)
7515 {
7516 /* Take note of the stop position we just moved
7517 across, for when we will move back across it. */
7518 it->prev_stop = it->stop_charpos;
7519 /* If we are at base paragraph embedding level, take
7520 note of the last stop position seen at this
7521 level. */
7522 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7523 it->base_level_stop = it->stop_charpos;
7524 }
7525 handle_stop (it);
7526
7527 /* Since a handler may have changed IT->method, we must
7528 recurse here. */
7529 return GET_NEXT_DISPLAY_ELEMENT (it);
7530 }
7531 }
7532 else if (it->bidi_p
7533 /* If we are before prev_stop, we may have overstepped
7534 on our way backwards a stop_pos, and if so, we need
7535 to handle that stop_pos. */
7536 && IT_STRING_CHARPOS (*it) < it->prev_stop
7537 /* We can sometimes back up for reasons that have nothing
7538 to do with bidi reordering. E.g., compositions. The
7539 code below is only needed when we are above the base
7540 embedding level, so test for that explicitly. */
7541 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7542 {
7543 /* If we lost track of base_level_stop, we have no better
7544 place for handle_stop_backwards to start from than string
7545 beginning. This happens, e.g., when we were reseated to
7546 the previous screenful of text by vertical-motion. */
7547 if (it->base_level_stop <= 0
7548 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7549 it->base_level_stop = 0;
7550 handle_stop_backwards (it, it->base_level_stop);
7551 return GET_NEXT_DISPLAY_ELEMENT (it);
7552 }
7553 }
7554
7555 if (it->current.overlay_string_index >= 0)
7556 {
7557 /* Get the next character from an overlay string. In overlay
7558 strings, there is no field width or padding with spaces to
7559 do. */
7560 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7561 {
7562 it->what = IT_EOB;
7563 return 0;
7564 }
7565 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7566 IT_STRING_BYTEPOS (*it),
7567 it->bidi_it.scan_dir < 0
7568 ? -1
7569 : SCHARS (it->string))
7570 && next_element_from_composition (it))
7571 {
7572 return 1;
7573 }
7574 else if (STRING_MULTIBYTE (it->string))
7575 {
7576 const unsigned char *s = (SDATA (it->string)
7577 + IT_STRING_BYTEPOS (*it));
7578 it->c = string_char_and_length (s, &it->len);
7579 }
7580 else
7581 {
7582 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7583 it->len = 1;
7584 }
7585 }
7586 else
7587 {
7588 /* Get the next character from a Lisp string that is not an
7589 overlay string. Such strings come from the mode line, for
7590 example. We may have to pad with spaces, or truncate the
7591 string. See also next_element_from_c_string. */
7592 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7593 {
7594 it->what = IT_EOB;
7595 return 0;
7596 }
7597 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7598 {
7599 /* Pad with spaces. */
7600 it->c = ' ', it->len = 1;
7601 CHARPOS (position) = BYTEPOS (position) = -1;
7602 }
7603 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7604 IT_STRING_BYTEPOS (*it),
7605 it->bidi_it.scan_dir < 0
7606 ? -1
7607 : it->string_nchars)
7608 && next_element_from_composition (it))
7609 {
7610 return 1;
7611 }
7612 else if (STRING_MULTIBYTE (it->string))
7613 {
7614 const unsigned char *s = (SDATA (it->string)
7615 + IT_STRING_BYTEPOS (*it));
7616 it->c = string_char_and_length (s, &it->len);
7617 }
7618 else
7619 {
7620 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7621 it->len = 1;
7622 }
7623 }
7624
7625 /* Record what we have and where it came from. */
7626 it->what = IT_CHARACTER;
7627 it->object = it->string;
7628 it->position = position;
7629 return 1;
7630 }
7631
7632
7633 /* Load IT with next display element from C string IT->s.
7634 IT->string_nchars is the maximum number of characters to return
7635 from the string. IT->end_charpos may be greater than
7636 IT->string_nchars when this function is called, in which case we
7637 may have to return padding spaces. Value is zero if end of string
7638 reached, including padding spaces. */
7639
7640 static int
7641 next_element_from_c_string (struct it *it)
7642 {
7643 int success_p = 1;
7644
7645 eassert (it->s);
7646 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7647 it->what = IT_CHARACTER;
7648 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7649 it->object = Qnil;
7650
7651 /* With bidi reordering, the character to display might not be the
7652 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7653 we were reseated to a new string, whose paragraph direction is
7654 not known. */
7655 if (it->bidi_p && it->bidi_it.first_elt)
7656 get_visually_first_element (it);
7657
7658 /* IT's position can be greater than IT->string_nchars in case a
7659 field width or precision has been specified when the iterator was
7660 initialized. */
7661 if (IT_CHARPOS (*it) >= it->end_charpos)
7662 {
7663 /* End of the game. */
7664 it->what = IT_EOB;
7665 success_p = 0;
7666 }
7667 else if (IT_CHARPOS (*it) >= it->string_nchars)
7668 {
7669 /* Pad with spaces. */
7670 it->c = ' ', it->len = 1;
7671 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7672 }
7673 else if (it->multibyte_p)
7674 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7675 else
7676 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7677
7678 return success_p;
7679 }
7680
7681
7682 /* Set up IT to return characters from an ellipsis, if appropriate.
7683 The definition of the ellipsis glyphs may come from a display table
7684 entry. This function fills IT with the first glyph from the
7685 ellipsis if an ellipsis is to be displayed. */
7686
7687 static int
7688 next_element_from_ellipsis (struct it *it)
7689 {
7690 if (it->selective_display_ellipsis_p)
7691 setup_for_ellipsis (it, it->len);
7692 else
7693 {
7694 /* The face at the current position may be different from the
7695 face we find after the invisible text. Remember what it
7696 was in IT->saved_face_id, and signal that it's there by
7697 setting face_before_selective_p. */
7698 it->saved_face_id = it->face_id;
7699 it->method = GET_FROM_BUFFER;
7700 it->object = it->w->buffer;
7701 reseat_at_next_visible_line_start (it, 1);
7702 it->face_before_selective_p = 1;
7703 }
7704
7705 return GET_NEXT_DISPLAY_ELEMENT (it);
7706 }
7707
7708
7709 /* Deliver an image display element. The iterator IT is already
7710 filled with image information (done in handle_display_prop). Value
7711 is always 1. */
7712
7713
7714 static int
7715 next_element_from_image (struct it *it)
7716 {
7717 it->what = IT_IMAGE;
7718 it->ignore_overlay_strings_at_pos_p = 0;
7719 return 1;
7720 }
7721
7722
7723 /* Fill iterator IT with next display element from a stretch glyph
7724 property. IT->object is the value of the text property. Value is
7725 always 1. */
7726
7727 static int
7728 next_element_from_stretch (struct it *it)
7729 {
7730 it->what = IT_STRETCH;
7731 return 1;
7732 }
7733
7734 /* Scan backwards from IT's current position until we find a stop
7735 position, or until BEGV. This is called when we find ourself
7736 before both the last known prev_stop and base_level_stop while
7737 reordering bidirectional text. */
7738
7739 static void
7740 compute_stop_pos_backwards (struct it *it)
7741 {
7742 const int SCAN_BACK_LIMIT = 1000;
7743 struct text_pos pos;
7744 struct display_pos save_current = it->current;
7745 struct text_pos save_position = it->position;
7746 ptrdiff_t charpos = IT_CHARPOS (*it);
7747 ptrdiff_t where_we_are = charpos;
7748 ptrdiff_t save_stop_pos = it->stop_charpos;
7749 ptrdiff_t save_end_pos = it->end_charpos;
7750
7751 eassert (NILP (it->string) && !it->s);
7752 eassert (it->bidi_p);
7753 it->bidi_p = 0;
7754 do
7755 {
7756 it->end_charpos = min (charpos + 1, ZV);
7757 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7758 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7759 reseat_1 (it, pos, 0);
7760 compute_stop_pos (it);
7761 /* We must advance forward, right? */
7762 if (it->stop_charpos <= charpos)
7763 emacs_abort ();
7764 }
7765 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7766
7767 if (it->stop_charpos <= where_we_are)
7768 it->prev_stop = it->stop_charpos;
7769 else
7770 it->prev_stop = BEGV;
7771 it->bidi_p = 1;
7772 it->current = save_current;
7773 it->position = save_position;
7774 it->stop_charpos = save_stop_pos;
7775 it->end_charpos = save_end_pos;
7776 }
7777
7778 /* Scan forward from CHARPOS in the current buffer/string, until we
7779 find a stop position > current IT's position. Then handle the stop
7780 position before that. This is called when we bump into a stop
7781 position while reordering bidirectional text. CHARPOS should be
7782 the last previously processed stop_pos (or BEGV/0, if none were
7783 processed yet) whose position is less that IT's current
7784 position. */
7785
7786 static void
7787 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7788 {
7789 int bufp = !STRINGP (it->string);
7790 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7791 struct display_pos save_current = it->current;
7792 struct text_pos save_position = it->position;
7793 struct text_pos pos1;
7794 ptrdiff_t next_stop;
7795
7796 /* Scan in strict logical order. */
7797 eassert (it->bidi_p);
7798 it->bidi_p = 0;
7799 do
7800 {
7801 it->prev_stop = charpos;
7802 if (bufp)
7803 {
7804 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7805 reseat_1 (it, pos1, 0);
7806 }
7807 else
7808 it->current.string_pos = string_pos (charpos, it->string);
7809 compute_stop_pos (it);
7810 /* We must advance forward, right? */
7811 if (it->stop_charpos <= it->prev_stop)
7812 emacs_abort ();
7813 charpos = it->stop_charpos;
7814 }
7815 while (charpos <= where_we_are);
7816
7817 it->bidi_p = 1;
7818 it->current = save_current;
7819 it->position = save_position;
7820 next_stop = it->stop_charpos;
7821 it->stop_charpos = it->prev_stop;
7822 handle_stop (it);
7823 it->stop_charpos = next_stop;
7824 }
7825
7826 /* Load IT with the next display element from current_buffer. Value
7827 is zero if end of buffer reached. IT->stop_charpos is the next
7828 position at which to stop and check for text properties or buffer
7829 end. */
7830
7831 static int
7832 next_element_from_buffer (struct it *it)
7833 {
7834 int success_p = 1;
7835
7836 eassert (IT_CHARPOS (*it) >= BEGV);
7837 eassert (NILP (it->string) && !it->s);
7838 eassert (!it->bidi_p
7839 || (EQ (it->bidi_it.string.lstring, Qnil)
7840 && it->bidi_it.string.s == NULL));
7841
7842 /* With bidi reordering, the character to display might not be the
7843 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7844 we were reseat()ed to a new buffer position, which is potentially
7845 a different paragraph. */
7846 if (it->bidi_p && it->bidi_it.first_elt)
7847 {
7848 get_visually_first_element (it);
7849 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7850 }
7851
7852 if (IT_CHARPOS (*it) >= it->stop_charpos)
7853 {
7854 if (IT_CHARPOS (*it) >= it->end_charpos)
7855 {
7856 int overlay_strings_follow_p;
7857
7858 /* End of the game, except when overlay strings follow that
7859 haven't been returned yet. */
7860 if (it->overlay_strings_at_end_processed_p)
7861 overlay_strings_follow_p = 0;
7862 else
7863 {
7864 it->overlay_strings_at_end_processed_p = 1;
7865 overlay_strings_follow_p = get_overlay_strings (it, 0);
7866 }
7867
7868 if (overlay_strings_follow_p)
7869 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7870 else
7871 {
7872 it->what = IT_EOB;
7873 it->position = it->current.pos;
7874 success_p = 0;
7875 }
7876 }
7877 else if (!(!it->bidi_p
7878 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7879 || IT_CHARPOS (*it) == it->stop_charpos))
7880 {
7881 /* With bidi non-linear iteration, we could find ourselves
7882 far beyond the last computed stop_charpos, with several
7883 other stop positions in between that we missed. Scan
7884 them all now, in buffer's logical order, until we find
7885 and handle the last stop_charpos that precedes our
7886 current position. */
7887 handle_stop_backwards (it, it->stop_charpos);
7888 return GET_NEXT_DISPLAY_ELEMENT (it);
7889 }
7890 else
7891 {
7892 if (it->bidi_p)
7893 {
7894 /* Take note of the stop position we just moved across,
7895 for when we will move back across it. */
7896 it->prev_stop = it->stop_charpos;
7897 /* If we are at base paragraph embedding level, take
7898 note of the last stop position seen at this
7899 level. */
7900 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7901 it->base_level_stop = it->stop_charpos;
7902 }
7903 handle_stop (it);
7904 return GET_NEXT_DISPLAY_ELEMENT (it);
7905 }
7906 }
7907 else if (it->bidi_p
7908 /* If we are before prev_stop, we may have overstepped on
7909 our way backwards a stop_pos, and if so, we need to
7910 handle that stop_pos. */
7911 && IT_CHARPOS (*it) < it->prev_stop
7912 /* We can sometimes back up for reasons that have nothing
7913 to do with bidi reordering. E.g., compositions. The
7914 code below is only needed when we are above the base
7915 embedding level, so test for that explicitly. */
7916 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7917 {
7918 if (it->base_level_stop <= 0
7919 || IT_CHARPOS (*it) < it->base_level_stop)
7920 {
7921 /* If we lost track of base_level_stop, we need to find
7922 prev_stop by looking backwards. This happens, e.g., when
7923 we were reseated to the previous screenful of text by
7924 vertical-motion. */
7925 it->base_level_stop = BEGV;
7926 compute_stop_pos_backwards (it);
7927 handle_stop_backwards (it, it->prev_stop);
7928 }
7929 else
7930 handle_stop_backwards (it, it->base_level_stop);
7931 return GET_NEXT_DISPLAY_ELEMENT (it);
7932 }
7933 else
7934 {
7935 /* No face changes, overlays etc. in sight, so just return a
7936 character from current_buffer. */
7937 unsigned char *p;
7938 ptrdiff_t stop;
7939
7940 /* Maybe run the redisplay end trigger hook. Performance note:
7941 This doesn't seem to cost measurable time. */
7942 if (it->redisplay_end_trigger_charpos
7943 && it->glyph_row
7944 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7945 run_redisplay_end_trigger_hook (it);
7946
7947 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7948 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7949 stop)
7950 && next_element_from_composition (it))
7951 {
7952 return 1;
7953 }
7954
7955 /* Get the next character, maybe multibyte. */
7956 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7957 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7958 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7959 else
7960 it->c = *p, it->len = 1;
7961
7962 /* Record what we have and where it came from. */
7963 it->what = IT_CHARACTER;
7964 it->object = it->w->buffer;
7965 it->position = it->current.pos;
7966
7967 /* Normally we return the character found above, except when we
7968 really want to return an ellipsis for selective display. */
7969 if (it->selective)
7970 {
7971 if (it->c == '\n')
7972 {
7973 /* A value of selective > 0 means hide lines indented more
7974 than that number of columns. */
7975 if (it->selective > 0
7976 && IT_CHARPOS (*it) + 1 < ZV
7977 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7978 IT_BYTEPOS (*it) + 1,
7979 it->selective))
7980 {
7981 success_p = next_element_from_ellipsis (it);
7982 it->dpvec_char_len = -1;
7983 }
7984 }
7985 else if (it->c == '\r' && it->selective == -1)
7986 {
7987 /* A value of selective == -1 means that everything from the
7988 CR to the end of the line is invisible, with maybe an
7989 ellipsis displayed for it. */
7990 success_p = next_element_from_ellipsis (it);
7991 it->dpvec_char_len = -1;
7992 }
7993 }
7994 }
7995
7996 /* Value is zero if end of buffer reached. */
7997 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7998 return success_p;
7999 }
8000
8001
8002 /* Run the redisplay end trigger hook for IT. */
8003
8004 static void
8005 run_redisplay_end_trigger_hook (struct it *it)
8006 {
8007 Lisp_Object args[3];
8008
8009 /* IT->glyph_row should be non-null, i.e. we should be actually
8010 displaying something, or otherwise we should not run the hook. */
8011 eassert (it->glyph_row);
8012
8013 /* Set up hook arguments. */
8014 args[0] = Qredisplay_end_trigger_functions;
8015 args[1] = it->window;
8016 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8017 it->redisplay_end_trigger_charpos = 0;
8018
8019 /* Since we are *trying* to run these functions, don't try to run
8020 them again, even if they get an error. */
8021 wset_redisplay_end_trigger (it->w, Qnil);
8022 Frun_hook_with_args (3, args);
8023
8024 /* Notice if it changed the face of the character we are on. */
8025 handle_face_prop (it);
8026 }
8027
8028
8029 /* Deliver a composition display element. Unlike the other
8030 next_element_from_XXX, this function is not registered in the array
8031 get_next_element[]. It is called from next_element_from_buffer and
8032 next_element_from_string when necessary. */
8033
8034 static int
8035 next_element_from_composition (struct it *it)
8036 {
8037 it->what = IT_COMPOSITION;
8038 it->len = it->cmp_it.nbytes;
8039 if (STRINGP (it->string))
8040 {
8041 if (it->c < 0)
8042 {
8043 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8044 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8045 return 0;
8046 }
8047 it->position = it->current.string_pos;
8048 it->object = it->string;
8049 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8050 IT_STRING_BYTEPOS (*it), it->string);
8051 }
8052 else
8053 {
8054 if (it->c < 0)
8055 {
8056 IT_CHARPOS (*it) += it->cmp_it.nchars;
8057 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8058 if (it->bidi_p)
8059 {
8060 if (it->bidi_it.new_paragraph)
8061 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8062 /* Resync the bidi iterator with IT's new position.
8063 FIXME: this doesn't support bidirectional text. */
8064 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8065 bidi_move_to_visually_next (&it->bidi_it);
8066 }
8067 return 0;
8068 }
8069 it->position = it->current.pos;
8070 it->object = it->w->buffer;
8071 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8072 IT_BYTEPOS (*it), Qnil);
8073 }
8074 return 1;
8075 }
8076
8077
8078 \f
8079 /***********************************************************************
8080 Moving an iterator without producing glyphs
8081 ***********************************************************************/
8082
8083 /* Check if iterator is at a position corresponding to a valid buffer
8084 position after some move_it_ call. */
8085
8086 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8087 ((it)->method == GET_FROM_STRING \
8088 ? IT_STRING_CHARPOS (*it) == 0 \
8089 : 1)
8090
8091
8092 /* Move iterator IT to a specified buffer or X position within one
8093 line on the display without producing glyphs.
8094
8095 OP should be a bit mask including some or all of these bits:
8096 MOVE_TO_X: Stop upon reaching x-position TO_X.
8097 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8098 Regardless of OP's value, stop upon reaching the end of the display line.
8099
8100 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8101 This means, in particular, that TO_X includes window's horizontal
8102 scroll amount.
8103
8104 The return value has several possible values that
8105 say what condition caused the scan to stop:
8106
8107 MOVE_POS_MATCH_OR_ZV
8108 - when TO_POS or ZV was reached.
8109
8110 MOVE_X_REACHED
8111 -when TO_X was reached before TO_POS or ZV were reached.
8112
8113 MOVE_LINE_CONTINUED
8114 - when we reached the end of the display area and the line must
8115 be continued.
8116
8117 MOVE_LINE_TRUNCATED
8118 - when we reached the end of the display area and the line is
8119 truncated.
8120
8121 MOVE_NEWLINE_OR_CR
8122 - when we stopped at a line end, i.e. a newline or a CR and selective
8123 display is on. */
8124
8125 static enum move_it_result
8126 move_it_in_display_line_to (struct it *it,
8127 ptrdiff_t to_charpos, int to_x,
8128 enum move_operation_enum op)
8129 {
8130 enum move_it_result result = MOVE_UNDEFINED;
8131 struct glyph_row *saved_glyph_row;
8132 struct it wrap_it, atpos_it, atx_it, ppos_it;
8133 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8134 void *ppos_data = NULL;
8135 int may_wrap = 0;
8136 enum it_method prev_method = it->method;
8137 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8138 int saw_smaller_pos = prev_pos < to_charpos;
8139
8140 /* Don't produce glyphs in produce_glyphs. */
8141 saved_glyph_row = it->glyph_row;
8142 it->glyph_row = NULL;
8143
8144 /* Use wrap_it to save a copy of IT wherever a word wrap could
8145 occur. Use atpos_it to save a copy of IT at the desired buffer
8146 position, if found, so that we can scan ahead and check if the
8147 word later overshoots the window edge. Use atx_it similarly, for
8148 pixel positions. */
8149 wrap_it.sp = -1;
8150 atpos_it.sp = -1;
8151 atx_it.sp = -1;
8152
8153 /* Use ppos_it under bidi reordering to save a copy of IT for the
8154 position > CHARPOS that is the closest to CHARPOS. We restore
8155 that position in IT when we have scanned the entire display line
8156 without finding a match for CHARPOS and all the character
8157 positions are greater than CHARPOS. */
8158 if (it->bidi_p)
8159 {
8160 SAVE_IT (ppos_it, *it, ppos_data);
8161 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8162 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8163 SAVE_IT (ppos_it, *it, ppos_data);
8164 }
8165
8166 #define BUFFER_POS_REACHED_P() \
8167 ((op & MOVE_TO_POS) != 0 \
8168 && BUFFERP (it->object) \
8169 && (IT_CHARPOS (*it) == to_charpos \
8170 || ((!it->bidi_p \
8171 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8172 && IT_CHARPOS (*it) > to_charpos) \
8173 || (it->what == IT_COMPOSITION \
8174 && ((IT_CHARPOS (*it) > to_charpos \
8175 && to_charpos >= it->cmp_it.charpos) \
8176 || (IT_CHARPOS (*it) < to_charpos \
8177 && to_charpos <= it->cmp_it.charpos)))) \
8178 && (it->method == GET_FROM_BUFFER \
8179 || (it->method == GET_FROM_DISPLAY_VECTOR \
8180 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8181
8182 /* If there's a line-/wrap-prefix, handle it. */
8183 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8184 && it->current_y < it->last_visible_y)
8185 handle_line_prefix (it);
8186
8187 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8188 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8189
8190 while (1)
8191 {
8192 int x, i, ascent = 0, descent = 0;
8193
8194 /* Utility macro to reset an iterator with x, ascent, and descent. */
8195 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8196 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8197 (IT)->max_descent = descent)
8198
8199 /* Stop if we move beyond TO_CHARPOS (after an image or a
8200 display string or stretch glyph). */
8201 if ((op & MOVE_TO_POS) != 0
8202 && BUFFERP (it->object)
8203 && it->method == GET_FROM_BUFFER
8204 && (((!it->bidi_p
8205 /* When the iterator is at base embedding level, we
8206 are guaranteed that characters are delivered for
8207 display in strictly increasing order of their
8208 buffer positions. */
8209 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8210 && IT_CHARPOS (*it) > to_charpos)
8211 || (it->bidi_p
8212 && (prev_method == GET_FROM_IMAGE
8213 || prev_method == GET_FROM_STRETCH
8214 || prev_method == GET_FROM_STRING)
8215 /* Passed TO_CHARPOS from left to right. */
8216 && ((prev_pos < to_charpos
8217 && IT_CHARPOS (*it) > to_charpos)
8218 /* Passed TO_CHARPOS from right to left. */
8219 || (prev_pos > to_charpos
8220 && IT_CHARPOS (*it) < to_charpos)))))
8221 {
8222 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8223 {
8224 result = MOVE_POS_MATCH_OR_ZV;
8225 break;
8226 }
8227 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8228 /* If wrap_it is valid, the current position might be in a
8229 word that is wrapped. So, save the iterator in
8230 atpos_it and continue to see if wrapping happens. */
8231 SAVE_IT (atpos_it, *it, atpos_data);
8232 }
8233
8234 /* Stop when ZV reached.
8235 We used to stop here when TO_CHARPOS reached as well, but that is
8236 too soon if this glyph does not fit on this line. So we handle it
8237 explicitly below. */
8238 if (!get_next_display_element (it))
8239 {
8240 result = MOVE_POS_MATCH_OR_ZV;
8241 break;
8242 }
8243
8244 if (it->line_wrap == TRUNCATE)
8245 {
8246 if (BUFFER_POS_REACHED_P ())
8247 {
8248 result = MOVE_POS_MATCH_OR_ZV;
8249 break;
8250 }
8251 }
8252 else
8253 {
8254 if (it->line_wrap == WORD_WRAP)
8255 {
8256 if (IT_DISPLAYING_WHITESPACE (it))
8257 may_wrap = 1;
8258 else if (may_wrap)
8259 {
8260 /* We have reached a glyph that follows one or more
8261 whitespace characters. If the position is
8262 already found, we are done. */
8263 if (atpos_it.sp >= 0)
8264 {
8265 RESTORE_IT (it, &atpos_it, atpos_data);
8266 result = MOVE_POS_MATCH_OR_ZV;
8267 goto done;
8268 }
8269 if (atx_it.sp >= 0)
8270 {
8271 RESTORE_IT (it, &atx_it, atx_data);
8272 result = MOVE_X_REACHED;
8273 goto done;
8274 }
8275 /* Otherwise, we can wrap here. */
8276 SAVE_IT (wrap_it, *it, wrap_data);
8277 may_wrap = 0;
8278 }
8279 }
8280 }
8281
8282 /* Remember the line height for the current line, in case
8283 the next element doesn't fit on the line. */
8284 ascent = it->max_ascent;
8285 descent = it->max_descent;
8286
8287 /* The call to produce_glyphs will get the metrics of the
8288 display element IT is loaded with. Record the x-position
8289 before this display element, in case it doesn't fit on the
8290 line. */
8291 x = it->current_x;
8292
8293 PRODUCE_GLYPHS (it);
8294
8295 if (it->area != TEXT_AREA)
8296 {
8297 prev_method = it->method;
8298 if (it->method == GET_FROM_BUFFER)
8299 prev_pos = IT_CHARPOS (*it);
8300 set_iterator_to_next (it, 1);
8301 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8302 SET_TEXT_POS (this_line_min_pos,
8303 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8304 if (it->bidi_p
8305 && (op & MOVE_TO_POS)
8306 && IT_CHARPOS (*it) > to_charpos
8307 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8308 SAVE_IT (ppos_it, *it, ppos_data);
8309 continue;
8310 }
8311
8312 /* The number of glyphs we get back in IT->nglyphs will normally
8313 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8314 character on a terminal frame, or (iii) a line end. For the
8315 second case, IT->nglyphs - 1 padding glyphs will be present.
8316 (On X frames, there is only one glyph produced for a
8317 composite character.)
8318
8319 The behavior implemented below means, for continuation lines,
8320 that as many spaces of a TAB as fit on the current line are
8321 displayed there. For terminal frames, as many glyphs of a
8322 multi-glyph character are displayed in the current line, too.
8323 This is what the old redisplay code did, and we keep it that
8324 way. Under X, the whole shape of a complex character must
8325 fit on the line or it will be completely displayed in the
8326 next line.
8327
8328 Note that both for tabs and padding glyphs, all glyphs have
8329 the same width. */
8330 if (it->nglyphs)
8331 {
8332 /* More than one glyph or glyph doesn't fit on line. All
8333 glyphs have the same width. */
8334 int single_glyph_width = it->pixel_width / it->nglyphs;
8335 int new_x;
8336 int x_before_this_char = x;
8337 int hpos_before_this_char = it->hpos;
8338
8339 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8340 {
8341 new_x = x + single_glyph_width;
8342
8343 /* We want to leave anything reaching TO_X to the caller. */
8344 if ((op & MOVE_TO_X) && new_x > to_x)
8345 {
8346 if (BUFFER_POS_REACHED_P ())
8347 {
8348 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8349 goto buffer_pos_reached;
8350 if (atpos_it.sp < 0)
8351 {
8352 SAVE_IT (atpos_it, *it, atpos_data);
8353 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8354 }
8355 }
8356 else
8357 {
8358 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8359 {
8360 it->current_x = x;
8361 result = MOVE_X_REACHED;
8362 break;
8363 }
8364 if (atx_it.sp < 0)
8365 {
8366 SAVE_IT (atx_it, *it, atx_data);
8367 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8368 }
8369 }
8370 }
8371
8372 if (/* Lines are continued. */
8373 it->line_wrap != TRUNCATE
8374 && (/* And glyph doesn't fit on the line. */
8375 new_x > it->last_visible_x
8376 /* Or it fits exactly and we're on a window
8377 system frame. */
8378 || (new_x == it->last_visible_x
8379 && FRAME_WINDOW_P (it->f)
8380 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8381 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8382 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8383 {
8384 if (/* IT->hpos == 0 means the very first glyph
8385 doesn't fit on the line, e.g. a wide image. */
8386 it->hpos == 0
8387 || (new_x == it->last_visible_x
8388 && FRAME_WINDOW_P (it->f)))
8389 {
8390 ++it->hpos;
8391 it->current_x = new_x;
8392
8393 /* The character's last glyph just barely fits
8394 in this row. */
8395 if (i == it->nglyphs - 1)
8396 {
8397 /* If this is the destination position,
8398 return a position *before* it in this row,
8399 now that we know it fits in this row. */
8400 if (BUFFER_POS_REACHED_P ())
8401 {
8402 if (it->line_wrap != WORD_WRAP
8403 || wrap_it.sp < 0)
8404 {
8405 it->hpos = hpos_before_this_char;
8406 it->current_x = x_before_this_char;
8407 result = MOVE_POS_MATCH_OR_ZV;
8408 break;
8409 }
8410 if (it->line_wrap == WORD_WRAP
8411 && atpos_it.sp < 0)
8412 {
8413 SAVE_IT (atpos_it, *it, atpos_data);
8414 atpos_it.current_x = x_before_this_char;
8415 atpos_it.hpos = hpos_before_this_char;
8416 }
8417 }
8418
8419 prev_method = it->method;
8420 if (it->method == GET_FROM_BUFFER)
8421 prev_pos = IT_CHARPOS (*it);
8422 set_iterator_to_next (it, 1);
8423 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8424 SET_TEXT_POS (this_line_min_pos,
8425 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8426 /* On graphical terminals, newlines may
8427 "overflow" into the fringe if
8428 overflow-newline-into-fringe is non-nil.
8429 On text terminals, and on graphical
8430 terminals with no right margin, newlines
8431 may overflow into the last glyph on the
8432 display line.*/
8433 if (!FRAME_WINDOW_P (it->f)
8434 || ((it->bidi_p
8435 && it->bidi_it.paragraph_dir == R2L)
8436 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8437 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8438 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8439 {
8440 if (!get_next_display_element (it))
8441 {
8442 result = MOVE_POS_MATCH_OR_ZV;
8443 break;
8444 }
8445 if (BUFFER_POS_REACHED_P ())
8446 {
8447 if (ITERATOR_AT_END_OF_LINE_P (it))
8448 result = MOVE_POS_MATCH_OR_ZV;
8449 else
8450 result = MOVE_LINE_CONTINUED;
8451 break;
8452 }
8453 if (ITERATOR_AT_END_OF_LINE_P (it))
8454 {
8455 result = MOVE_NEWLINE_OR_CR;
8456 break;
8457 }
8458 }
8459 }
8460 }
8461 else
8462 IT_RESET_X_ASCENT_DESCENT (it);
8463
8464 if (wrap_it.sp >= 0)
8465 {
8466 RESTORE_IT (it, &wrap_it, wrap_data);
8467 atpos_it.sp = -1;
8468 atx_it.sp = -1;
8469 }
8470
8471 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8472 IT_CHARPOS (*it)));
8473 result = MOVE_LINE_CONTINUED;
8474 break;
8475 }
8476
8477 if (BUFFER_POS_REACHED_P ())
8478 {
8479 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8480 goto buffer_pos_reached;
8481 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8482 {
8483 SAVE_IT (atpos_it, *it, atpos_data);
8484 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8485 }
8486 }
8487
8488 if (new_x > it->first_visible_x)
8489 {
8490 /* Glyph is visible. Increment number of glyphs that
8491 would be displayed. */
8492 ++it->hpos;
8493 }
8494 }
8495
8496 if (result != MOVE_UNDEFINED)
8497 break;
8498 }
8499 else if (BUFFER_POS_REACHED_P ())
8500 {
8501 buffer_pos_reached:
8502 IT_RESET_X_ASCENT_DESCENT (it);
8503 result = MOVE_POS_MATCH_OR_ZV;
8504 break;
8505 }
8506 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8507 {
8508 /* Stop when TO_X specified and reached. This check is
8509 necessary here because of lines consisting of a line end,
8510 only. The line end will not produce any glyphs and we
8511 would never get MOVE_X_REACHED. */
8512 eassert (it->nglyphs == 0);
8513 result = MOVE_X_REACHED;
8514 break;
8515 }
8516
8517 /* Is this a line end? If yes, we're done. */
8518 if (ITERATOR_AT_END_OF_LINE_P (it))
8519 {
8520 /* If we are past TO_CHARPOS, but never saw any character
8521 positions smaller than TO_CHARPOS, return
8522 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8523 did. */
8524 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8525 {
8526 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8527 {
8528 if (IT_CHARPOS (ppos_it) < ZV)
8529 {
8530 RESTORE_IT (it, &ppos_it, ppos_data);
8531 result = MOVE_POS_MATCH_OR_ZV;
8532 }
8533 else
8534 goto buffer_pos_reached;
8535 }
8536 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8537 && IT_CHARPOS (*it) > to_charpos)
8538 goto buffer_pos_reached;
8539 else
8540 result = MOVE_NEWLINE_OR_CR;
8541 }
8542 else
8543 result = MOVE_NEWLINE_OR_CR;
8544 break;
8545 }
8546
8547 prev_method = it->method;
8548 if (it->method == GET_FROM_BUFFER)
8549 prev_pos = IT_CHARPOS (*it);
8550 /* The current display element has been consumed. Advance
8551 to the next. */
8552 set_iterator_to_next (it, 1);
8553 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8554 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8555 if (IT_CHARPOS (*it) < to_charpos)
8556 saw_smaller_pos = 1;
8557 if (it->bidi_p
8558 && (op & MOVE_TO_POS)
8559 && IT_CHARPOS (*it) >= to_charpos
8560 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8561 SAVE_IT (ppos_it, *it, ppos_data);
8562
8563 /* Stop if lines are truncated and IT's current x-position is
8564 past the right edge of the window now. */
8565 if (it->line_wrap == TRUNCATE
8566 && it->current_x >= it->last_visible_x)
8567 {
8568 if (!FRAME_WINDOW_P (it->f)
8569 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8570 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8571 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8572 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8573 {
8574 int at_eob_p = 0;
8575
8576 if ((at_eob_p = !get_next_display_element (it))
8577 || BUFFER_POS_REACHED_P ()
8578 /* If we are past TO_CHARPOS, but never saw any
8579 character positions smaller than TO_CHARPOS,
8580 return MOVE_POS_MATCH_OR_ZV, like the
8581 unidirectional display did. */
8582 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8583 && !saw_smaller_pos
8584 && IT_CHARPOS (*it) > to_charpos))
8585 {
8586 if (it->bidi_p
8587 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8588 RESTORE_IT (it, &ppos_it, ppos_data);
8589 result = MOVE_POS_MATCH_OR_ZV;
8590 break;
8591 }
8592 if (ITERATOR_AT_END_OF_LINE_P (it))
8593 {
8594 result = MOVE_NEWLINE_OR_CR;
8595 break;
8596 }
8597 }
8598 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8599 && !saw_smaller_pos
8600 && IT_CHARPOS (*it) > to_charpos)
8601 {
8602 if (IT_CHARPOS (ppos_it) < ZV)
8603 RESTORE_IT (it, &ppos_it, ppos_data);
8604 result = MOVE_POS_MATCH_OR_ZV;
8605 break;
8606 }
8607 result = MOVE_LINE_TRUNCATED;
8608 break;
8609 }
8610 #undef IT_RESET_X_ASCENT_DESCENT
8611 }
8612
8613 #undef BUFFER_POS_REACHED_P
8614
8615 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8616 restore the saved iterator. */
8617 if (atpos_it.sp >= 0)
8618 RESTORE_IT (it, &atpos_it, atpos_data);
8619 else if (atx_it.sp >= 0)
8620 RESTORE_IT (it, &atx_it, atx_data);
8621
8622 done:
8623
8624 if (atpos_data)
8625 bidi_unshelve_cache (atpos_data, 1);
8626 if (atx_data)
8627 bidi_unshelve_cache (atx_data, 1);
8628 if (wrap_data)
8629 bidi_unshelve_cache (wrap_data, 1);
8630 if (ppos_data)
8631 bidi_unshelve_cache (ppos_data, 1);
8632
8633 /* Restore the iterator settings altered at the beginning of this
8634 function. */
8635 it->glyph_row = saved_glyph_row;
8636 return result;
8637 }
8638
8639 /* For external use. */
8640 void
8641 move_it_in_display_line (struct it *it,
8642 ptrdiff_t to_charpos, int to_x,
8643 enum move_operation_enum op)
8644 {
8645 if (it->line_wrap == WORD_WRAP
8646 && (op & MOVE_TO_X))
8647 {
8648 struct it save_it;
8649 void *save_data = NULL;
8650 int skip;
8651
8652 SAVE_IT (save_it, *it, save_data);
8653 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8654 /* When word-wrap is on, TO_X may lie past the end
8655 of a wrapped line. Then it->current is the
8656 character on the next line, so backtrack to the
8657 space before the wrap point. */
8658 if (skip == MOVE_LINE_CONTINUED)
8659 {
8660 int prev_x = max (it->current_x - 1, 0);
8661 RESTORE_IT (it, &save_it, save_data);
8662 move_it_in_display_line_to
8663 (it, -1, prev_x, MOVE_TO_X);
8664 }
8665 else
8666 bidi_unshelve_cache (save_data, 1);
8667 }
8668 else
8669 move_it_in_display_line_to (it, to_charpos, to_x, op);
8670 }
8671
8672
8673 /* Move IT forward until it satisfies one or more of the criteria in
8674 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8675
8676 OP is a bit-mask that specifies where to stop, and in particular,
8677 which of those four position arguments makes a difference. See the
8678 description of enum move_operation_enum.
8679
8680 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8681 screen line, this function will set IT to the next position that is
8682 displayed to the right of TO_CHARPOS on the screen. */
8683
8684 void
8685 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8686 {
8687 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8688 int line_height, line_start_x = 0, reached = 0;
8689 void *backup_data = NULL;
8690
8691 for (;;)
8692 {
8693 if (op & MOVE_TO_VPOS)
8694 {
8695 /* If no TO_CHARPOS and no TO_X specified, stop at the
8696 start of the line TO_VPOS. */
8697 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8698 {
8699 if (it->vpos == to_vpos)
8700 {
8701 reached = 1;
8702 break;
8703 }
8704 else
8705 skip = move_it_in_display_line_to (it, -1, -1, 0);
8706 }
8707 else
8708 {
8709 /* TO_VPOS >= 0 means stop at TO_X in the line at
8710 TO_VPOS, or at TO_POS, whichever comes first. */
8711 if (it->vpos == to_vpos)
8712 {
8713 reached = 2;
8714 break;
8715 }
8716
8717 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8718
8719 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8720 {
8721 reached = 3;
8722 break;
8723 }
8724 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8725 {
8726 /* We have reached TO_X but not in the line we want. */
8727 skip = move_it_in_display_line_to (it, to_charpos,
8728 -1, MOVE_TO_POS);
8729 if (skip == MOVE_POS_MATCH_OR_ZV)
8730 {
8731 reached = 4;
8732 break;
8733 }
8734 }
8735 }
8736 }
8737 else if (op & MOVE_TO_Y)
8738 {
8739 struct it it_backup;
8740
8741 if (it->line_wrap == WORD_WRAP)
8742 SAVE_IT (it_backup, *it, backup_data);
8743
8744 /* TO_Y specified means stop at TO_X in the line containing
8745 TO_Y---or at TO_CHARPOS if this is reached first. The
8746 problem is that we can't really tell whether the line
8747 contains TO_Y before we have completely scanned it, and
8748 this may skip past TO_X. What we do is to first scan to
8749 TO_X.
8750
8751 If TO_X is not specified, use a TO_X of zero. The reason
8752 is to make the outcome of this function more predictable.
8753 If we didn't use TO_X == 0, we would stop at the end of
8754 the line which is probably not what a caller would expect
8755 to happen. */
8756 skip = move_it_in_display_line_to
8757 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8758 (MOVE_TO_X | (op & MOVE_TO_POS)));
8759
8760 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8761 if (skip == MOVE_POS_MATCH_OR_ZV)
8762 reached = 5;
8763 else if (skip == MOVE_X_REACHED)
8764 {
8765 /* If TO_X was reached, we want to know whether TO_Y is
8766 in the line. We know this is the case if the already
8767 scanned glyphs make the line tall enough. Otherwise,
8768 we must check by scanning the rest of the line. */
8769 line_height = it->max_ascent + it->max_descent;
8770 if (to_y >= it->current_y
8771 && to_y < it->current_y + line_height)
8772 {
8773 reached = 6;
8774 break;
8775 }
8776 SAVE_IT (it_backup, *it, backup_data);
8777 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8778 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8779 op & MOVE_TO_POS);
8780 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8781 line_height = it->max_ascent + it->max_descent;
8782 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8783
8784 if (to_y >= it->current_y
8785 && to_y < it->current_y + line_height)
8786 {
8787 /* If TO_Y is in this line and TO_X was reached
8788 above, we scanned too far. We have to restore
8789 IT's settings to the ones before skipping. But
8790 keep the more accurate values of max_ascent and
8791 max_descent we've found while skipping the rest
8792 of the line, for the sake of callers, such as
8793 pos_visible_p, that need to know the line
8794 height. */
8795 int max_ascent = it->max_ascent;
8796 int max_descent = it->max_descent;
8797
8798 RESTORE_IT (it, &it_backup, backup_data);
8799 it->max_ascent = max_ascent;
8800 it->max_descent = max_descent;
8801 reached = 6;
8802 }
8803 else
8804 {
8805 skip = skip2;
8806 if (skip == MOVE_POS_MATCH_OR_ZV)
8807 reached = 7;
8808 }
8809 }
8810 else
8811 {
8812 /* Check whether TO_Y is in this line. */
8813 line_height = it->max_ascent + it->max_descent;
8814 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8815
8816 if (to_y >= it->current_y
8817 && to_y < it->current_y + line_height)
8818 {
8819 /* When word-wrap is on, TO_X may lie past the end
8820 of a wrapped line. Then it->current is the
8821 character on the next line, so backtrack to the
8822 space before the wrap point. */
8823 if (skip == MOVE_LINE_CONTINUED
8824 && it->line_wrap == WORD_WRAP)
8825 {
8826 int prev_x = max (it->current_x - 1, 0);
8827 RESTORE_IT (it, &it_backup, backup_data);
8828 skip = move_it_in_display_line_to
8829 (it, -1, prev_x, MOVE_TO_X);
8830 }
8831 reached = 6;
8832 }
8833 }
8834
8835 if (reached)
8836 break;
8837 }
8838 else if (BUFFERP (it->object)
8839 && (it->method == GET_FROM_BUFFER
8840 || it->method == GET_FROM_STRETCH)
8841 && IT_CHARPOS (*it) >= to_charpos
8842 /* Under bidi iteration, a call to set_iterator_to_next
8843 can scan far beyond to_charpos if the initial
8844 portion of the next line needs to be reordered. In
8845 that case, give move_it_in_display_line_to another
8846 chance below. */
8847 && !(it->bidi_p
8848 && it->bidi_it.scan_dir == -1))
8849 skip = MOVE_POS_MATCH_OR_ZV;
8850 else
8851 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8852
8853 switch (skip)
8854 {
8855 case MOVE_POS_MATCH_OR_ZV:
8856 reached = 8;
8857 goto out;
8858
8859 case MOVE_NEWLINE_OR_CR:
8860 set_iterator_to_next (it, 1);
8861 it->continuation_lines_width = 0;
8862 break;
8863
8864 case MOVE_LINE_TRUNCATED:
8865 it->continuation_lines_width = 0;
8866 reseat_at_next_visible_line_start (it, 0);
8867 if ((op & MOVE_TO_POS) != 0
8868 && IT_CHARPOS (*it) > to_charpos)
8869 {
8870 reached = 9;
8871 goto out;
8872 }
8873 break;
8874
8875 case MOVE_LINE_CONTINUED:
8876 /* For continued lines ending in a tab, some of the glyphs
8877 associated with the tab are displayed on the current
8878 line. Since it->current_x does not include these glyphs,
8879 we use it->last_visible_x instead. */
8880 if (it->c == '\t')
8881 {
8882 it->continuation_lines_width += it->last_visible_x;
8883 /* When moving by vpos, ensure that the iterator really
8884 advances to the next line (bug#847, bug#969). Fixme:
8885 do we need to do this in other circumstances? */
8886 if (it->current_x != it->last_visible_x
8887 && (op & MOVE_TO_VPOS)
8888 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8889 {
8890 line_start_x = it->current_x + it->pixel_width
8891 - it->last_visible_x;
8892 set_iterator_to_next (it, 0);
8893 }
8894 }
8895 else
8896 it->continuation_lines_width += it->current_x;
8897 break;
8898
8899 default:
8900 emacs_abort ();
8901 }
8902
8903 /* Reset/increment for the next run. */
8904 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8905 it->current_x = line_start_x;
8906 line_start_x = 0;
8907 it->hpos = 0;
8908 it->current_y += it->max_ascent + it->max_descent;
8909 ++it->vpos;
8910 last_height = it->max_ascent + it->max_descent;
8911 last_max_ascent = it->max_ascent;
8912 it->max_ascent = it->max_descent = 0;
8913 }
8914
8915 out:
8916
8917 /* On text terminals, we may stop at the end of a line in the middle
8918 of a multi-character glyph. If the glyph itself is continued,
8919 i.e. it is actually displayed on the next line, don't treat this
8920 stopping point as valid; move to the next line instead (unless
8921 that brings us offscreen). */
8922 if (!FRAME_WINDOW_P (it->f)
8923 && op & MOVE_TO_POS
8924 && IT_CHARPOS (*it) == to_charpos
8925 && it->what == IT_CHARACTER
8926 && it->nglyphs > 1
8927 && it->line_wrap == WINDOW_WRAP
8928 && it->current_x == it->last_visible_x - 1
8929 && it->c != '\n'
8930 && it->c != '\t'
8931 && it->vpos < XFASTINT (it->w->window_end_vpos))
8932 {
8933 it->continuation_lines_width += it->current_x;
8934 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8935 it->current_y += it->max_ascent + it->max_descent;
8936 ++it->vpos;
8937 last_height = it->max_ascent + it->max_descent;
8938 last_max_ascent = it->max_ascent;
8939 }
8940
8941 if (backup_data)
8942 bidi_unshelve_cache (backup_data, 1);
8943
8944 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8945 }
8946
8947
8948 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8949
8950 If DY > 0, move IT backward at least that many pixels. DY = 0
8951 means move IT backward to the preceding line start or BEGV. This
8952 function may move over more than DY pixels if IT->current_y - DY
8953 ends up in the middle of a line; in this case IT->current_y will be
8954 set to the top of the line moved to. */
8955
8956 void
8957 move_it_vertically_backward (struct it *it, int dy)
8958 {
8959 int nlines, h;
8960 struct it it2, it3;
8961 void *it2data = NULL, *it3data = NULL;
8962 ptrdiff_t start_pos;
8963
8964 move_further_back:
8965 eassert (dy >= 0);
8966
8967 start_pos = IT_CHARPOS (*it);
8968
8969 /* Estimate how many newlines we must move back. */
8970 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8971
8972 /* Set the iterator's position that many lines back. */
8973 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8974 back_to_previous_visible_line_start (it);
8975
8976 /* Reseat the iterator here. When moving backward, we don't want
8977 reseat to skip forward over invisible text, set up the iterator
8978 to deliver from overlay strings at the new position etc. So,
8979 use reseat_1 here. */
8980 reseat_1 (it, it->current.pos, 1);
8981
8982 /* We are now surely at a line start. */
8983 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8984 reordering is in effect. */
8985 it->continuation_lines_width = 0;
8986
8987 /* Move forward and see what y-distance we moved. First move to the
8988 start of the next line so that we get its height. We need this
8989 height to be able to tell whether we reached the specified
8990 y-distance. */
8991 SAVE_IT (it2, *it, it2data);
8992 it2.max_ascent = it2.max_descent = 0;
8993 do
8994 {
8995 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8996 MOVE_TO_POS | MOVE_TO_VPOS);
8997 }
8998 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8999 /* If we are in a display string which starts at START_POS,
9000 and that display string includes a newline, and we are
9001 right after that newline (i.e. at the beginning of a
9002 display line), exit the loop, because otherwise we will
9003 infloop, since move_it_to will see that it is already at
9004 START_POS and will not move. */
9005 || (it2.method == GET_FROM_STRING
9006 && IT_CHARPOS (it2) == start_pos
9007 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9008 eassert (IT_CHARPOS (*it) >= BEGV);
9009 SAVE_IT (it3, it2, it3data);
9010
9011 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9012 eassert (IT_CHARPOS (*it) >= BEGV);
9013 /* H is the actual vertical distance from the position in *IT
9014 and the starting position. */
9015 h = it2.current_y - it->current_y;
9016 /* NLINES is the distance in number of lines. */
9017 nlines = it2.vpos - it->vpos;
9018
9019 /* Correct IT's y and vpos position
9020 so that they are relative to the starting point. */
9021 it->vpos -= nlines;
9022 it->current_y -= h;
9023
9024 if (dy == 0)
9025 {
9026 /* DY == 0 means move to the start of the screen line. The
9027 value of nlines is > 0 if continuation lines were involved,
9028 or if the original IT position was at start of a line. */
9029 RESTORE_IT (it, it, it2data);
9030 if (nlines > 0)
9031 move_it_by_lines (it, nlines);
9032 /* The above code moves us to some position NLINES down,
9033 usually to its first glyph (leftmost in an L2R line), but
9034 that's not necessarily the start of the line, under bidi
9035 reordering. We want to get to the character position
9036 that is immediately after the newline of the previous
9037 line. */
9038 if (it->bidi_p
9039 && !it->continuation_lines_width
9040 && !STRINGP (it->string)
9041 && IT_CHARPOS (*it) > BEGV
9042 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9043 {
9044 ptrdiff_t nl_pos =
9045 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9046
9047 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9048 }
9049 bidi_unshelve_cache (it3data, 1);
9050 }
9051 else
9052 {
9053 /* The y-position we try to reach, relative to *IT.
9054 Note that H has been subtracted in front of the if-statement. */
9055 int target_y = it->current_y + h - dy;
9056 int y0 = it3.current_y;
9057 int y1;
9058 int line_height;
9059
9060 RESTORE_IT (&it3, &it3, it3data);
9061 y1 = line_bottom_y (&it3);
9062 line_height = y1 - y0;
9063 RESTORE_IT (it, it, it2data);
9064 /* If we did not reach target_y, try to move further backward if
9065 we can. If we moved too far backward, try to move forward. */
9066 if (target_y < it->current_y
9067 /* This is heuristic. In a window that's 3 lines high, with
9068 a line height of 13 pixels each, recentering with point
9069 on the bottom line will try to move -39/2 = 19 pixels
9070 backward. Try to avoid moving into the first line. */
9071 && (it->current_y - target_y
9072 > min (window_box_height (it->w), line_height * 2 / 3))
9073 && IT_CHARPOS (*it) > BEGV)
9074 {
9075 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9076 target_y - it->current_y));
9077 dy = it->current_y - target_y;
9078 goto move_further_back;
9079 }
9080 else if (target_y >= it->current_y + line_height
9081 && IT_CHARPOS (*it) < ZV)
9082 {
9083 /* Should move forward by at least one line, maybe more.
9084
9085 Note: Calling move_it_by_lines can be expensive on
9086 terminal frames, where compute_motion is used (via
9087 vmotion) to do the job, when there are very long lines
9088 and truncate-lines is nil. That's the reason for
9089 treating terminal frames specially here. */
9090
9091 if (!FRAME_WINDOW_P (it->f))
9092 move_it_vertically (it, target_y - (it->current_y + line_height));
9093 else
9094 {
9095 do
9096 {
9097 move_it_by_lines (it, 1);
9098 }
9099 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9100 }
9101 }
9102 }
9103 }
9104
9105
9106 /* Move IT by a specified amount of pixel lines DY. DY negative means
9107 move backwards. DY = 0 means move to start of screen line. At the
9108 end, IT will be on the start of a screen line. */
9109
9110 void
9111 move_it_vertically (struct it *it, int dy)
9112 {
9113 if (dy <= 0)
9114 move_it_vertically_backward (it, -dy);
9115 else
9116 {
9117 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9118 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9119 MOVE_TO_POS | MOVE_TO_Y);
9120 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9121
9122 /* If buffer ends in ZV without a newline, move to the start of
9123 the line to satisfy the post-condition. */
9124 if (IT_CHARPOS (*it) == ZV
9125 && ZV > BEGV
9126 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9127 move_it_by_lines (it, 0);
9128 }
9129 }
9130
9131
9132 /* Move iterator IT past the end of the text line it is in. */
9133
9134 void
9135 move_it_past_eol (struct it *it)
9136 {
9137 enum move_it_result rc;
9138
9139 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9140 if (rc == MOVE_NEWLINE_OR_CR)
9141 set_iterator_to_next (it, 0);
9142 }
9143
9144
9145 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9146 negative means move up. DVPOS == 0 means move to the start of the
9147 screen line.
9148
9149 Optimization idea: If we would know that IT->f doesn't use
9150 a face with proportional font, we could be faster for
9151 truncate-lines nil. */
9152
9153 void
9154 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9155 {
9156
9157 /* The commented-out optimization uses vmotion on terminals. This
9158 gives bad results, because elements like it->what, on which
9159 callers such as pos_visible_p rely, aren't updated. */
9160 /* struct position pos;
9161 if (!FRAME_WINDOW_P (it->f))
9162 {
9163 struct text_pos textpos;
9164
9165 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9166 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9167 reseat (it, textpos, 1);
9168 it->vpos += pos.vpos;
9169 it->current_y += pos.vpos;
9170 }
9171 else */
9172
9173 if (dvpos == 0)
9174 {
9175 /* DVPOS == 0 means move to the start of the screen line. */
9176 move_it_vertically_backward (it, 0);
9177 /* Let next call to line_bottom_y calculate real line height */
9178 last_height = 0;
9179 }
9180 else if (dvpos > 0)
9181 {
9182 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9183 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9184 {
9185 /* Only move to the next buffer position if we ended up in a
9186 string from display property, not in an overlay string
9187 (before-string or after-string). That is because the
9188 latter don't conceal the underlying buffer position, so
9189 we can ask to move the iterator to the exact position we
9190 are interested in. Note that, even if we are already at
9191 IT_CHARPOS (*it), the call below is not a no-op, as it
9192 will detect that we are at the end of the string, pop the
9193 iterator, and compute it->current_x and it->hpos
9194 correctly. */
9195 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9196 -1, -1, -1, MOVE_TO_POS);
9197 }
9198 }
9199 else
9200 {
9201 struct it it2;
9202 void *it2data = NULL;
9203 ptrdiff_t start_charpos, i;
9204
9205 /* Start at the beginning of the screen line containing IT's
9206 position. This may actually move vertically backwards,
9207 in case of overlays, so adjust dvpos accordingly. */
9208 dvpos += it->vpos;
9209 move_it_vertically_backward (it, 0);
9210 dvpos -= it->vpos;
9211
9212 /* Go back -DVPOS visible lines and reseat the iterator there. */
9213 start_charpos = IT_CHARPOS (*it);
9214 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9215 back_to_previous_visible_line_start (it);
9216 reseat (it, it->current.pos, 1);
9217
9218 /* Move further back if we end up in a string or an image. */
9219 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9220 {
9221 /* First try to move to start of display line. */
9222 dvpos += it->vpos;
9223 move_it_vertically_backward (it, 0);
9224 dvpos -= it->vpos;
9225 if (IT_POS_VALID_AFTER_MOVE_P (it))
9226 break;
9227 /* If start of line is still in string or image,
9228 move further back. */
9229 back_to_previous_visible_line_start (it);
9230 reseat (it, it->current.pos, 1);
9231 dvpos--;
9232 }
9233
9234 it->current_x = it->hpos = 0;
9235
9236 /* Above call may have moved too far if continuation lines
9237 are involved. Scan forward and see if it did. */
9238 SAVE_IT (it2, *it, it2data);
9239 it2.vpos = it2.current_y = 0;
9240 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9241 it->vpos -= it2.vpos;
9242 it->current_y -= it2.current_y;
9243 it->current_x = it->hpos = 0;
9244
9245 /* If we moved too far back, move IT some lines forward. */
9246 if (it2.vpos > -dvpos)
9247 {
9248 int delta = it2.vpos + dvpos;
9249
9250 RESTORE_IT (&it2, &it2, it2data);
9251 SAVE_IT (it2, *it, it2data);
9252 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9253 /* Move back again if we got too far ahead. */
9254 if (IT_CHARPOS (*it) >= start_charpos)
9255 RESTORE_IT (it, &it2, it2data);
9256 else
9257 bidi_unshelve_cache (it2data, 1);
9258 }
9259 else
9260 RESTORE_IT (it, it, it2data);
9261 }
9262 }
9263
9264 /* Return 1 if IT points into the middle of a display vector. */
9265
9266 int
9267 in_display_vector_p (struct it *it)
9268 {
9269 return (it->method == GET_FROM_DISPLAY_VECTOR
9270 && it->current.dpvec_index > 0
9271 && it->dpvec + it->current.dpvec_index != it->dpend);
9272 }
9273
9274 \f
9275 /***********************************************************************
9276 Messages
9277 ***********************************************************************/
9278
9279
9280 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9281 to *Messages*. */
9282
9283 void
9284 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9285 {
9286 Lisp_Object args[3];
9287 Lisp_Object msg, fmt;
9288 char *buffer;
9289 ptrdiff_t len;
9290 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9291 USE_SAFE_ALLOCA;
9292
9293 fmt = msg = Qnil;
9294 GCPRO4 (fmt, msg, arg1, arg2);
9295
9296 args[0] = fmt = build_string (format);
9297 args[1] = arg1;
9298 args[2] = arg2;
9299 msg = Fformat (3, args);
9300
9301 len = SBYTES (msg) + 1;
9302 buffer = SAFE_ALLOCA (len);
9303 memcpy (buffer, SDATA (msg), len);
9304
9305 message_dolog (buffer, len - 1, 1, 0);
9306 SAFE_FREE ();
9307
9308 UNGCPRO;
9309 }
9310
9311
9312 /* Output a newline in the *Messages* buffer if "needs" one. */
9313
9314 void
9315 message_log_maybe_newline (void)
9316 {
9317 if (message_log_need_newline)
9318 message_dolog ("", 0, 1, 0);
9319 }
9320
9321
9322 /* Add a string M of length NBYTES to the message log, optionally
9323 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9324 nonzero, means interpret the contents of M as multibyte. This
9325 function calls low-level routines in order to bypass text property
9326 hooks, etc. which might not be safe to run.
9327
9328 This may GC (insert may run before/after change hooks),
9329 so the buffer M must NOT point to a Lisp string. */
9330
9331 void
9332 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9333 {
9334 const unsigned char *msg = (const unsigned char *) m;
9335
9336 if (!NILP (Vmemory_full))
9337 return;
9338
9339 if (!NILP (Vmessage_log_max))
9340 {
9341 struct buffer *oldbuf;
9342 Lisp_Object oldpoint, oldbegv, oldzv;
9343 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9344 ptrdiff_t point_at_end = 0;
9345 ptrdiff_t zv_at_end = 0;
9346 Lisp_Object old_deactivate_mark, tem;
9347 struct gcpro gcpro1;
9348
9349 old_deactivate_mark = Vdeactivate_mark;
9350 oldbuf = current_buffer;
9351 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9352 bset_undo_list (current_buffer, Qt);
9353
9354 oldpoint = message_dolog_marker1;
9355 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9356 oldbegv = message_dolog_marker2;
9357 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9358 oldzv = message_dolog_marker3;
9359 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9360 GCPRO1 (old_deactivate_mark);
9361
9362 if (PT == Z)
9363 point_at_end = 1;
9364 if (ZV == Z)
9365 zv_at_end = 1;
9366
9367 BEGV = BEG;
9368 BEGV_BYTE = BEG_BYTE;
9369 ZV = Z;
9370 ZV_BYTE = Z_BYTE;
9371 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9372
9373 /* Insert the string--maybe converting multibyte to single byte
9374 or vice versa, so that all the text fits the buffer. */
9375 if (multibyte
9376 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9377 {
9378 ptrdiff_t i;
9379 int c, char_bytes;
9380 char work[1];
9381
9382 /* Convert a multibyte string to single-byte
9383 for the *Message* buffer. */
9384 for (i = 0; i < nbytes; i += char_bytes)
9385 {
9386 c = string_char_and_length (msg + i, &char_bytes);
9387 work[0] = (ASCII_CHAR_P (c)
9388 ? c
9389 : multibyte_char_to_unibyte (c));
9390 insert_1_both (work, 1, 1, 1, 0, 0);
9391 }
9392 }
9393 else if (! multibyte
9394 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9395 {
9396 ptrdiff_t i;
9397 int c, char_bytes;
9398 unsigned char str[MAX_MULTIBYTE_LENGTH];
9399 /* Convert a single-byte string to multibyte
9400 for the *Message* buffer. */
9401 for (i = 0; i < nbytes; i++)
9402 {
9403 c = msg[i];
9404 MAKE_CHAR_MULTIBYTE (c);
9405 char_bytes = CHAR_STRING (c, str);
9406 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9407 }
9408 }
9409 else if (nbytes)
9410 insert_1 (m, nbytes, 1, 0, 0);
9411
9412 if (nlflag)
9413 {
9414 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9415 printmax_t dups;
9416 insert_1 ("\n", 1, 1, 0, 0);
9417
9418 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9419 this_bol = PT;
9420 this_bol_byte = PT_BYTE;
9421
9422 /* See if this line duplicates the previous one.
9423 If so, combine duplicates. */
9424 if (this_bol > BEG)
9425 {
9426 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9427 prev_bol = PT;
9428 prev_bol_byte = PT_BYTE;
9429
9430 dups = message_log_check_duplicate (prev_bol_byte,
9431 this_bol_byte);
9432 if (dups)
9433 {
9434 del_range_both (prev_bol, prev_bol_byte,
9435 this_bol, this_bol_byte, 0);
9436 if (dups > 1)
9437 {
9438 char dupstr[sizeof " [ times]"
9439 + INT_STRLEN_BOUND (printmax_t)];
9440
9441 /* If you change this format, don't forget to also
9442 change message_log_check_duplicate. */
9443 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9444 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9445 insert_1 (dupstr, duplen, 1, 0, 1);
9446 }
9447 }
9448 }
9449
9450 /* If we have more than the desired maximum number of lines
9451 in the *Messages* buffer now, delete the oldest ones.
9452 This is safe because we don't have undo in this buffer. */
9453
9454 if (NATNUMP (Vmessage_log_max))
9455 {
9456 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9457 -XFASTINT (Vmessage_log_max) - 1, 0);
9458 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9459 }
9460 }
9461 BEGV = XMARKER (oldbegv)->charpos;
9462 BEGV_BYTE = marker_byte_position (oldbegv);
9463
9464 if (zv_at_end)
9465 {
9466 ZV = Z;
9467 ZV_BYTE = Z_BYTE;
9468 }
9469 else
9470 {
9471 ZV = XMARKER (oldzv)->charpos;
9472 ZV_BYTE = marker_byte_position (oldzv);
9473 }
9474
9475 if (point_at_end)
9476 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9477 else
9478 /* We can't do Fgoto_char (oldpoint) because it will run some
9479 Lisp code. */
9480 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9481 XMARKER (oldpoint)->bytepos);
9482
9483 UNGCPRO;
9484 unchain_marker (XMARKER (oldpoint));
9485 unchain_marker (XMARKER (oldbegv));
9486 unchain_marker (XMARKER (oldzv));
9487
9488 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9489 set_buffer_internal (oldbuf);
9490 if (NILP (tem))
9491 windows_or_buffers_changed = old_windows_or_buffers_changed;
9492 message_log_need_newline = !nlflag;
9493 Vdeactivate_mark = old_deactivate_mark;
9494 }
9495 }
9496
9497
9498 /* We are at the end of the buffer after just having inserted a newline.
9499 (Note: We depend on the fact we won't be crossing the gap.)
9500 Check to see if the most recent message looks a lot like the previous one.
9501 Return 0 if different, 1 if the new one should just replace it, or a
9502 value N > 1 if we should also append " [N times]". */
9503
9504 static intmax_t
9505 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9506 {
9507 ptrdiff_t i;
9508 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9509 int seen_dots = 0;
9510 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9511 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9512
9513 for (i = 0; i < len; i++)
9514 {
9515 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9516 seen_dots = 1;
9517 if (p1[i] != p2[i])
9518 return seen_dots;
9519 }
9520 p1 += len;
9521 if (*p1 == '\n')
9522 return 2;
9523 if (*p1++ == ' ' && *p1++ == '[')
9524 {
9525 char *pend;
9526 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9527 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9528 return n+1;
9529 }
9530 return 0;
9531 }
9532 \f
9533
9534 /* Display an echo area message M with a specified length of NBYTES
9535 bytes. The string may include null characters. If M is 0, clear
9536 out any existing message, and let the mini-buffer text show
9537 through.
9538
9539 This may GC, so the buffer M must NOT point to a Lisp string. */
9540
9541 void
9542 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9543 {
9544 /* First flush out any partial line written with print. */
9545 message_log_maybe_newline ();
9546 if (m)
9547 message_dolog (m, nbytes, 1, multibyte);
9548 message2_nolog (m, nbytes, multibyte);
9549 }
9550
9551
9552 /* The non-logging counterpart of message2. */
9553
9554 void
9555 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9556 {
9557 struct frame *sf = SELECTED_FRAME ();
9558 message_enable_multibyte = multibyte;
9559
9560 if (FRAME_INITIAL_P (sf))
9561 {
9562 if (noninteractive_need_newline)
9563 putc ('\n', stderr);
9564 noninteractive_need_newline = 0;
9565 if (m)
9566 fwrite (m, nbytes, 1, stderr);
9567 if (cursor_in_echo_area == 0)
9568 fprintf (stderr, "\n");
9569 fflush (stderr);
9570 }
9571 /* A null message buffer means that the frame hasn't really been
9572 initialized yet. Error messages get reported properly by
9573 cmd_error, so this must be just an informative message; toss it. */
9574 else if (INTERACTIVE
9575 && sf->glyphs_initialized_p
9576 && FRAME_MESSAGE_BUF (sf))
9577 {
9578 Lisp_Object mini_window;
9579 struct frame *f;
9580
9581 /* Get the frame containing the mini-buffer
9582 that the selected frame is using. */
9583 mini_window = FRAME_MINIBUF_WINDOW (sf);
9584 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9585
9586 FRAME_SAMPLE_VISIBILITY (f);
9587 if (FRAME_VISIBLE_P (sf)
9588 && ! FRAME_VISIBLE_P (f))
9589 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9590
9591 if (m)
9592 {
9593 set_message (m, Qnil, nbytes, multibyte);
9594 if (minibuffer_auto_raise)
9595 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9596 }
9597 else
9598 clear_message (1, 1);
9599
9600 do_pending_window_change (0);
9601 echo_area_display (1);
9602 do_pending_window_change (0);
9603 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9604 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9605 }
9606 }
9607
9608
9609 /* Display an echo area message M with a specified length of NBYTES
9610 bytes. The string may include null characters. If M is not a
9611 string, clear out any existing message, and let the mini-buffer
9612 text show through.
9613
9614 This function cancels echoing. */
9615
9616 void
9617 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9618 {
9619 struct gcpro gcpro1;
9620
9621 GCPRO1 (m);
9622 clear_message (1,1);
9623 cancel_echoing ();
9624
9625 /* First flush out any partial line written with print. */
9626 message_log_maybe_newline ();
9627 if (STRINGP (m))
9628 {
9629 USE_SAFE_ALLOCA;
9630 char *buffer = SAFE_ALLOCA (nbytes);
9631 memcpy (buffer, SDATA (m), nbytes);
9632 message_dolog (buffer, nbytes, 1, multibyte);
9633 SAFE_FREE ();
9634 }
9635 message3_nolog (m, nbytes, multibyte);
9636
9637 UNGCPRO;
9638 }
9639
9640
9641 /* The non-logging version of message3.
9642 This does not cancel echoing, because it is used for echoing.
9643 Perhaps we need to make a separate function for echoing
9644 and make this cancel echoing. */
9645
9646 void
9647 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9648 {
9649 struct frame *sf = SELECTED_FRAME ();
9650 message_enable_multibyte = multibyte;
9651
9652 if (FRAME_INITIAL_P (sf))
9653 {
9654 if (noninteractive_need_newline)
9655 putc ('\n', stderr);
9656 noninteractive_need_newline = 0;
9657 if (STRINGP (m))
9658 fwrite (SDATA (m), nbytes, 1, stderr);
9659 if (cursor_in_echo_area == 0)
9660 fprintf (stderr, "\n");
9661 fflush (stderr);
9662 }
9663 /* A null message buffer means that the frame hasn't really been
9664 initialized yet. Error messages get reported properly by
9665 cmd_error, so this must be just an informative message; toss it. */
9666 else if (INTERACTIVE
9667 && sf->glyphs_initialized_p
9668 && FRAME_MESSAGE_BUF (sf))
9669 {
9670 Lisp_Object mini_window;
9671 Lisp_Object frame;
9672 struct frame *f;
9673
9674 /* Get the frame containing the mini-buffer
9675 that the selected frame is using. */
9676 mini_window = FRAME_MINIBUF_WINDOW (sf);
9677 frame = XWINDOW (mini_window)->frame;
9678 f = XFRAME (frame);
9679
9680 FRAME_SAMPLE_VISIBILITY (f);
9681 if (FRAME_VISIBLE_P (sf)
9682 && !FRAME_VISIBLE_P (f))
9683 Fmake_frame_visible (frame);
9684
9685 if (STRINGP (m) && SCHARS (m) > 0)
9686 {
9687 set_message (NULL, m, nbytes, multibyte);
9688 if (minibuffer_auto_raise)
9689 Fraise_frame (frame);
9690 /* Assume we are not echoing.
9691 (If we are, echo_now will override this.) */
9692 echo_message_buffer = Qnil;
9693 }
9694 else
9695 clear_message (1, 1);
9696
9697 do_pending_window_change (0);
9698 echo_area_display (1);
9699 do_pending_window_change (0);
9700 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9701 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9702 }
9703 }
9704
9705
9706 /* Display a null-terminated echo area message M. If M is 0, clear
9707 out any existing message, and let the mini-buffer text show through.
9708
9709 The buffer M must continue to exist until after the echo area gets
9710 cleared or some other message gets displayed there. Do not pass
9711 text that is stored in a Lisp string. Do not pass text in a buffer
9712 that was alloca'd. */
9713
9714 void
9715 message1 (const char *m)
9716 {
9717 message2 (m, (m ? strlen (m) : 0), 0);
9718 }
9719
9720
9721 /* The non-logging counterpart of message1. */
9722
9723 void
9724 message1_nolog (const char *m)
9725 {
9726 message2_nolog (m, (m ? strlen (m) : 0), 0);
9727 }
9728
9729 /* Display a message M which contains a single %s
9730 which gets replaced with STRING. */
9731
9732 void
9733 message_with_string (const char *m, Lisp_Object string, int log)
9734 {
9735 CHECK_STRING (string);
9736
9737 if (noninteractive)
9738 {
9739 if (m)
9740 {
9741 if (noninteractive_need_newline)
9742 putc ('\n', stderr);
9743 noninteractive_need_newline = 0;
9744 fprintf (stderr, m, SDATA (string));
9745 if (!cursor_in_echo_area)
9746 fprintf (stderr, "\n");
9747 fflush (stderr);
9748 }
9749 }
9750 else if (INTERACTIVE)
9751 {
9752 /* The frame whose minibuffer we're going to display the message on.
9753 It may be larger than the selected frame, so we need
9754 to use its buffer, not the selected frame's buffer. */
9755 Lisp_Object mini_window;
9756 struct frame *f, *sf = SELECTED_FRAME ();
9757
9758 /* Get the frame containing the minibuffer
9759 that the selected frame is using. */
9760 mini_window = FRAME_MINIBUF_WINDOW (sf);
9761 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9762
9763 /* A null message buffer means that the frame hasn't really been
9764 initialized yet. Error messages get reported properly by
9765 cmd_error, so this must be just an informative message; toss it. */
9766 if (FRAME_MESSAGE_BUF (f))
9767 {
9768 Lisp_Object args[2], msg;
9769 struct gcpro gcpro1, gcpro2;
9770
9771 args[0] = build_string (m);
9772 args[1] = msg = string;
9773 GCPRO2 (args[0], msg);
9774 gcpro1.nvars = 2;
9775
9776 msg = Fformat (2, args);
9777
9778 if (log)
9779 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9780 else
9781 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9782
9783 UNGCPRO;
9784
9785 /* Print should start at the beginning of the message
9786 buffer next time. */
9787 message_buf_print = 0;
9788 }
9789 }
9790 }
9791
9792
9793 /* Dump an informative message to the minibuf. If M is 0, clear out
9794 any existing message, and let the mini-buffer text show through. */
9795
9796 static void
9797 vmessage (const char *m, va_list ap)
9798 {
9799 if (noninteractive)
9800 {
9801 if (m)
9802 {
9803 if (noninteractive_need_newline)
9804 putc ('\n', stderr);
9805 noninteractive_need_newline = 0;
9806 vfprintf (stderr, m, ap);
9807 if (cursor_in_echo_area == 0)
9808 fprintf (stderr, "\n");
9809 fflush (stderr);
9810 }
9811 }
9812 else if (INTERACTIVE)
9813 {
9814 /* The frame whose mini-buffer we're going to display the message
9815 on. It may be larger than the selected frame, so we need to
9816 use its buffer, not the selected frame's buffer. */
9817 Lisp_Object mini_window;
9818 struct frame *f, *sf = SELECTED_FRAME ();
9819
9820 /* Get the frame containing the mini-buffer
9821 that the selected frame is using. */
9822 mini_window = FRAME_MINIBUF_WINDOW (sf);
9823 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9824
9825 /* A null message buffer means that the frame hasn't really been
9826 initialized yet. Error messages get reported properly by
9827 cmd_error, so this must be just an informative message; toss
9828 it. */
9829 if (FRAME_MESSAGE_BUF (f))
9830 {
9831 if (m)
9832 {
9833 ptrdiff_t len;
9834
9835 len = doprnt (FRAME_MESSAGE_BUF (f),
9836 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9837
9838 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9839 }
9840 else
9841 message1 (0);
9842
9843 /* Print should start at the beginning of the message
9844 buffer next time. */
9845 message_buf_print = 0;
9846 }
9847 }
9848 }
9849
9850 void
9851 message (const char *m, ...)
9852 {
9853 va_list ap;
9854 va_start (ap, m);
9855 vmessage (m, ap);
9856 va_end (ap);
9857 }
9858
9859
9860 #if 0
9861 /* The non-logging version of message. */
9862
9863 void
9864 message_nolog (const char *m, ...)
9865 {
9866 Lisp_Object old_log_max;
9867 va_list ap;
9868 va_start (ap, m);
9869 old_log_max = Vmessage_log_max;
9870 Vmessage_log_max = Qnil;
9871 vmessage (m, ap);
9872 Vmessage_log_max = old_log_max;
9873 va_end (ap);
9874 }
9875 #endif
9876
9877
9878 /* Display the current message in the current mini-buffer. This is
9879 only called from error handlers in process.c, and is not time
9880 critical. */
9881
9882 void
9883 update_echo_area (void)
9884 {
9885 if (!NILP (echo_area_buffer[0]))
9886 {
9887 Lisp_Object string;
9888 string = Fcurrent_message ();
9889 message3 (string, SBYTES (string),
9890 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9891 }
9892 }
9893
9894
9895 /* Make sure echo area buffers in `echo_buffers' are live.
9896 If they aren't, make new ones. */
9897
9898 static void
9899 ensure_echo_area_buffers (void)
9900 {
9901 int i;
9902
9903 for (i = 0; i < 2; ++i)
9904 if (!BUFFERP (echo_buffer[i])
9905 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9906 {
9907 char name[30];
9908 Lisp_Object old_buffer;
9909 int j;
9910
9911 old_buffer = echo_buffer[i];
9912 echo_buffer[i] = Fget_buffer_create
9913 (make_formatted_string (name, " *Echo Area %d*", i));
9914 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9915 /* to force word wrap in echo area -
9916 it was decided to postpone this*/
9917 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9918
9919 for (j = 0; j < 2; ++j)
9920 if (EQ (old_buffer, echo_area_buffer[j]))
9921 echo_area_buffer[j] = echo_buffer[i];
9922 }
9923 }
9924
9925
9926 /* Call FN with args A1..A4 with either the current or last displayed
9927 echo_area_buffer as current buffer.
9928
9929 WHICH zero means use the current message buffer
9930 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9931 from echo_buffer[] and clear it.
9932
9933 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9934 suitable buffer from echo_buffer[] and clear it.
9935
9936 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9937 that the current message becomes the last displayed one, make
9938 choose a suitable buffer for echo_area_buffer[0], and clear it.
9939
9940 Value is what FN returns. */
9941
9942 static int
9943 with_echo_area_buffer (struct window *w, int which,
9944 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9945 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9946 {
9947 Lisp_Object buffer;
9948 int this_one, the_other, clear_buffer_p, rc;
9949 ptrdiff_t count = SPECPDL_INDEX ();
9950
9951 /* If buffers aren't live, make new ones. */
9952 ensure_echo_area_buffers ();
9953
9954 clear_buffer_p = 0;
9955
9956 if (which == 0)
9957 this_one = 0, the_other = 1;
9958 else if (which > 0)
9959 this_one = 1, the_other = 0;
9960 else
9961 {
9962 this_one = 0, the_other = 1;
9963 clear_buffer_p = 1;
9964
9965 /* We need a fresh one in case the current echo buffer equals
9966 the one containing the last displayed echo area message. */
9967 if (!NILP (echo_area_buffer[this_one])
9968 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9969 echo_area_buffer[this_one] = Qnil;
9970 }
9971
9972 /* Choose a suitable buffer from echo_buffer[] is we don't
9973 have one. */
9974 if (NILP (echo_area_buffer[this_one]))
9975 {
9976 echo_area_buffer[this_one]
9977 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9978 ? echo_buffer[the_other]
9979 : echo_buffer[this_one]);
9980 clear_buffer_p = 1;
9981 }
9982
9983 buffer = echo_area_buffer[this_one];
9984
9985 /* Don't get confused by reusing the buffer used for echoing
9986 for a different purpose. */
9987 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9988 cancel_echoing ();
9989
9990 record_unwind_protect (unwind_with_echo_area_buffer,
9991 with_echo_area_buffer_unwind_data (w));
9992
9993 /* Make the echo area buffer current. Note that for display
9994 purposes, it is not necessary that the displayed window's buffer
9995 == current_buffer, except for text property lookup. So, let's
9996 only set that buffer temporarily here without doing a full
9997 Fset_window_buffer. We must also change w->pointm, though,
9998 because otherwise an assertions in unshow_buffer fails, and Emacs
9999 aborts. */
10000 set_buffer_internal_1 (XBUFFER (buffer));
10001 if (w)
10002 {
10003 wset_buffer (w, buffer);
10004 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10005 }
10006
10007 bset_undo_list (current_buffer, Qt);
10008 bset_read_only (current_buffer, Qnil);
10009 specbind (Qinhibit_read_only, Qt);
10010 specbind (Qinhibit_modification_hooks, Qt);
10011
10012 if (clear_buffer_p && Z > BEG)
10013 del_range (BEG, Z);
10014
10015 eassert (BEGV >= BEG);
10016 eassert (ZV <= Z && ZV >= BEGV);
10017
10018 rc = fn (a1, a2, a3, a4);
10019
10020 eassert (BEGV >= BEG);
10021 eassert (ZV <= Z && ZV >= BEGV);
10022
10023 unbind_to (count, Qnil);
10024 return rc;
10025 }
10026
10027
10028 /* Save state that should be preserved around the call to the function
10029 FN called in with_echo_area_buffer. */
10030
10031 static Lisp_Object
10032 with_echo_area_buffer_unwind_data (struct window *w)
10033 {
10034 int i = 0;
10035 Lisp_Object vector, tmp;
10036
10037 /* Reduce consing by keeping one vector in
10038 Vwith_echo_area_save_vector. */
10039 vector = Vwith_echo_area_save_vector;
10040 Vwith_echo_area_save_vector = Qnil;
10041
10042 if (NILP (vector))
10043 vector = Fmake_vector (make_number (7), Qnil);
10044
10045 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10046 ASET (vector, i, Vdeactivate_mark); ++i;
10047 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10048
10049 if (w)
10050 {
10051 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10052 ASET (vector, i, w->buffer); ++i;
10053 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
10054 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
10055 }
10056 else
10057 {
10058 int end = i + 4;
10059 for (; i < end; ++i)
10060 ASET (vector, i, Qnil);
10061 }
10062
10063 eassert (i == ASIZE (vector));
10064 return vector;
10065 }
10066
10067
10068 /* Restore global state from VECTOR which was created by
10069 with_echo_area_buffer_unwind_data. */
10070
10071 static Lisp_Object
10072 unwind_with_echo_area_buffer (Lisp_Object vector)
10073 {
10074 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10075 Vdeactivate_mark = AREF (vector, 1);
10076 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10077
10078 if (WINDOWP (AREF (vector, 3)))
10079 {
10080 struct window *w;
10081 Lisp_Object buffer, charpos, bytepos;
10082
10083 w = XWINDOW (AREF (vector, 3));
10084 buffer = AREF (vector, 4);
10085 charpos = AREF (vector, 5);
10086 bytepos = AREF (vector, 6);
10087
10088 wset_buffer (w, buffer);
10089 set_marker_both (w->pointm, buffer,
10090 XFASTINT (charpos), XFASTINT (bytepos));
10091 }
10092
10093 Vwith_echo_area_save_vector = vector;
10094 return Qnil;
10095 }
10096
10097
10098 /* Set up the echo area for use by print functions. MULTIBYTE_P
10099 non-zero means we will print multibyte. */
10100
10101 void
10102 setup_echo_area_for_printing (int multibyte_p)
10103 {
10104 /* If we can't find an echo area any more, exit. */
10105 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10106 Fkill_emacs (Qnil);
10107
10108 ensure_echo_area_buffers ();
10109
10110 if (!message_buf_print)
10111 {
10112 /* A message has been output since the last time we printed.
10113 Choose a fresh echo area buffer. */
10114 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10115 echo_area_buffer[0] = echo_buffer[1];
10116 else
10117 echo_area_buffer[0] = echo_buffer[0];
10118
10119 /* Switch to that buffer and clear it. */
10120 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10121 bset_truncate_lines (current_buffer, Qnil);
10122
10123 if (Z > BEG)
10124 {
10125 ptrdiff_t count = SPECPDL_INDEX ();
10126 specbind (Qinhibit_read_only, Qt);
10127 /* Note that undo recording is always disabled. */
10128 del_range (BEG, Z);
10129 unbind_to (count, Qnil);
10130 }
10131 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10132
10133 /* Set up the buffer for the multibyteness we need. */
10134 if (multibyte_p
10135 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10136 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10137
10138 /* Raise the frame containing the echo area. */
10139 if (minibuffer_auto_raise)
10140 {
10141 struct frame *sf = SELECTED_FRAME ();
10142 Lisp_Object mini_window;
10143 mini_window = FRAME_MINIBUF_WINDOW (sf);
10144 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10145 }
10146
10147 message_log_maybe_newline ();
10148 message_buf_print = 1;
10149 }
10150 else
10151 {
10152 if (NILP (echo_area_buffer[0]))
10153 {
10154 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10155 echo_area_buffer[0] = echo_buffer[1];
10156 else
10157 echo_area_buffer[0] = echo_buffer[0];
10158 }
10159
10160 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10161 {
10162 /* Someone switched buffers between print requests. */
10163 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10164 bset_truncate_lines (current_buffer, Qnil);
10165 }
10166 }
10167 }
10168
10169
10170 /* Display an echo area message in window W. Value is non-zero if W's
10171 height is changed. If display_last_displayed_message_p is
10172 non-zero, display the message that was last displayed, otherwise
10173 display the current message. */
10174
10175 static int
10176 display_echo_area (struct window *w)
10177 {
10178 int i, no_message_p, window_height_changed_p;
10179
10180 /* Temporarily disable garbage collections while displaying the echo
10181 area. This is done because a GC can print a message itself.
10182 That message would modify the echo area buffer's contents while a
10183 redisplay of the buffer is going on, and seriously confuse
10184 redisplay. */
10185 ptrdiff_t count = inhibit_garbage_collection ();
10186
10187 /* If there is no message, we must call display_echo_area_1
10188 nevertheless because it resizes the window. But we will have to
10189 reset the echo_area_buffer in question to nil at the end because
10190 with_echo_area_buffer will sets it to an empty buffer. */
10191 i = display_last_displayed_message_p ? 1 : 0;
10192 no_message_p = NILP (echo_area_buffer[i]);
10193
10194 window_height_changed_p
10195 = with_echo_area_buffer (w, display_last_displayed_message_p,
10196 display_echo_area_1,
10197 (intptr_t) w, Qnil, 0, 0);
10198
10199 if (no_message_p)
10200 echo_area_buffer[i] = Qnil;
10201
10202 unbind_to (count, Qnil);
10203 return window_height_changed_p;
10204 }
10205
10206
10207 /* Helper for display_echo_area. Display the current buffer which
10208 contains the current echo area message in window W, a mini-window,
10209 a pointer to which is passed in A1. A2..A4 are currently not used.
10210 Change the height of W so that all of the message is displayed.
10211 Value is non-zero if height of W was changed. */
10212
10213 static int
10214 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10215 {
10216 intptr_t i1 = a1;
10217 struct window *w = (struct window *) i1;
10218 Lisp_Object window;
10219 struct text_pos start;
10220 int window_height_changed_p = 0;
10221
10222 /* Do this before displaying, so that we have a large enough glyph
10223 matrix for the display. If we can't get enough space for the
10224 whole text, display the last N lines. That works by setting w->start. */
10225 window_height_changed_p = resize_mini_window (w, 0);
10226
10227 /* Use the starting position chosen by resize_mini_window. */
10228 SET_TEXT_POS_FROM_MARKER (start, w->start);
10229
10230 /* Display. */
10231 clear_glyph_matrix (w->desired_matrix);
10232 XSETWINDOW (window, w);
10233 try_window (window, start, 0);
10234
10235 return window_height_changed_p;
10236 }
10237
10238
10239 /* Resize the echo area window to exactly the size needed for the
10240 currently displayed message, if there is one. If a mini-buffer
10241 is active, don't shrink it. */
10242
10243 void
10244 resize_echo_area_exactly (void)
10245 {
10246 if (BUFFERP (echo_area_buffer[0])
10247 && WINDOWP (echo_area_window))
10248 {
10249 struct window *w = XWINDOW (echo_area_window);
10250 int resized_p;
10251 Lisp_Object resize_exactly;
10252
10253 if (minibuf_level == 0)
10254 resize_exactly = Qt;
10255 else
10256 resize_exactly = Qnil;
10257
10258 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10259 (intptr_t) w, resize_exactly,
10260 0, 0);
10261 if (resized_p)
10262 {
10263 ++windows_or_buffers_changed;
10264 ++update_mode_lines;
10265 redisplay_internal ();
10266 }
10267 }
10268 }
10269
10270
10271 /* Callback function for with_echo_area_buffer, when used from
10272 resize_echo_area_exactly. A1 contains a pointer to the window to
10273 resize, EXACTLY non-nil means resize the mini-window exactly to the
10274 size of the text displayed. A3 and A4 are not used. Value is what
10275 resize_mini_window returns. */
10276
10277 static int
10278 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10279 {
10280 intptr_t i1 = a1;
10281 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10282 }
10283
10284
10285 /* Resize mini-window W to fit the size of its contents. EXACT_P
10286 means size the window exactly to the size needed. Otherwise, it's
10287 only enlarged until W's buffer is empty.
10288
10289 Set W->start to the right place to begin display. If the whole
10290 contents fit, start at the beginning. Otherwise, start so as
10291 to make the end of the contents appear. This is particularly
10292 important for y-or-n-p, but seems desirable generally.
10293
10294 Value is non-zero if the window height has been changed. */
10295
10296 int
10297 resize_mini_window (struct window *w, int exact_p)
10298 {
10299 struct frame *f = XFRAME (w->frame);
10300 int window_height_changed_p = 0;
10301
10302 eassert (MINI_WINDOW_P (w));
10303
10304 /* By default, start display at the beginning. */
10305 set_marker_both (w->start, w->buffer,
10306 BUF_BEGV (XBUFFER (w->buffer)),
10307 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10308
10309 /* Don't resize windows while redisplaying a window; it would
10310 confuse redisplay functions when the size of the window they are
10311 displaying changes from under them. Such a resizing can happen,
10312 for instance, when which-func prints a long message while
10313 we are running fontification-functions. We're running these
10314 functions with safe_call which binds inhibit-redisplay to t. */
10315 if (!NILP (Vinhibit_redisplay))
10316 return 0;
10317
10318 /* Nil means don't try to resize. */
10319 if (NILP (Vresize_mini_windows)
10320 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10321 return 0;
10322
10323 if (!FRAME_MINIBUF_ONLY_P (f))
10324 {
10325 struct it it;
10326 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10327 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10328 int height;
10329 EMACS_INT max_height;
10330 int unit = FRAME_LINE_HEIGHT (f);
10331 struct text_pos start;
10332 struct buffer *old_current_buffer = NULL;
10333
10334 if (current_buffer != XBUFFER (w->buffer))
10335 {
10336 old_current_buffer = current_buffer;
10337 set_buffer_internal (XBUFFER (w->buffer));
10338 }
10339
10340 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10341
10342 /* Compute the max. number of lines specified by the user. */
10343 if (FLOATP (Vmax_mini_window_height))
10344 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10345 else if (INTEGERP (Vmax_mini_window_height))
10346 max_height = XINT (Vmax_mini_window_height);
10347 else
10348 max_height = total_height / 4;
10349
10350 /* Correct that max. height if it's bogus. */
10351 max_height = max (1, max_height);
10352 max_height = min (total_height, max_height);
10353
10354 /* Find out the height of the text in the window. */
10355 if (it.line_wrap == TRUNCATE)
10356 height = 1;
10357 else
10358 {
10359 last_height = 0;
10360 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10361 if (it.max_ascent == 0 && it.max_descent == 0)
10362 height = it.current_y + last_height;
10363 else
10364 height = it.current_y + it.max_ascent + it.max_descent;
10365 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10366 height = (height + unit - 1) / unit;
10367 }
10368
10369 /* Compute a suitable window start. */
10370 if (height > max_height)
10371 {
10372 height = max_height;
10373 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10374 move_it_vertically_backward (&it, (height - 1) * unit);
10375 start = it.current.pos;
10376 }
10377 else
10378 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10379 SET_MARKER_FROM_TEXT_POS (w->start, start);
10380
10381 if (EQ (Vresize_mini_windows, Qgrow_only))
10382 {
10383 /* Let it grow only, until we display an empty message, in which
10384 case the window shrinks again. */
10385 if (height > WINDOW_TOTAL_LINES (w))
10386 {
10387 int old_height = WINDOW_TOTAL_LINES (w);
10388 freeze_window_starts (f, 1);
10389 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10390 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10391 }
10392 else if (height < WINDOW_TOTAL_LINES (w)
10393 && (exact_p || BEGV == ZV))
10394 {
10395 int old_height = WINDOW_TOTAL_LINES (w);
10396 freeze_window_starts (f, 0);
10397 shrink_mini_window (w);
10398 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10399 }
10400 }
10401 else
10402 {
10403 /* Always resize to exact size needed. */
10404 if (height > WINDOW_TOTAL_LINES (w))
10405 {
10406 int old_height = WINDOW_TOTAL_LINES (w);
10407 freeze_window_starts (f, 1);
10408 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10409 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10410 }
10411 else if (height < WINDOW_TOTAL_LINES (w))
10412 {
10413 int old_height = WINDOW_TOTAL_LINES (w);
10414 freeze_window_starts (f, 0);
10415 shrink_mini_window (w);
10416
10417 if (height)
10418 {
10419 freeze_window_starts (f, 1);
10420 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10421 }
10422
10423 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10424 }
10425 }
10426
10427 if (old_current_buffer)
10428 set_buffer_internal (old_current_buffer);
10429 }
10430
10431 return window_height_changed_p;
10432 }
10433
10434
10435 /* Value is the current message, a string, or nil if there is no
10436 current message. */
10437
10438 Lisp_Object
10439 current_message (void)
10440 {
10441 Lisp_Object msg;
10442
10443 if (!BUFFERP (echo_area_buffer[0]))
10444 msg = Qnil;
10445 else
10446 {
10447 with_echo_area_buffer (0, 0, current_message_1,
10448 (intptr_t) &msg, Qnil, 0, 0);
10449 if (NILP (msg))
10450 echo_area_buffer[0] = Qnil;
10451 }
10452
10453 return msg;
10454 }
10455
10456
10457 static int
10458 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10459 {
10460 intptr_t i1 = a1;
10461 Lisp_Object *msg = (Lisp_Object *) i1;
10462
10463 if (Z > BEG)
10464 *msg = make_buffer_string (BEG, Z, 1);
10465 else
10466 *msg = Qnil;
10467 return 0;
10468 }
10469
10470
10471 /* Push the current message on Vmessage_stack for later restoration
10472 by restore_message. Value is non-zero if the current message isn't
10473 empty. This is a relatively infrequent operation, so it's not
10474 worth optimizing. */
10475
10476 bool
10477 push_message (void)
10478 {
10479 Lisp_Object msg = current_message ();
10480 Vmessage_stack = Fcons (msg, Vmessage_stack);
10481 return STRINGP (msg);
10482 }
10483
10484
10485 /* Restore message display from the top of Vmessage_stack. */
10486
10487 void
10488 restore_message (void)
10489 {
10490 Lisp_Object msg;
10491
10492 eassert (CONSP (Vmessage_stack));
10493 msg = XCAR (Vmessage_stack);
10494 if (STRINGP (msg))
10495 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10496 else
10497 message3_nolog (msg, 0, 0);
10498 }
10499
10500
10501 /* Handler for record_unwind_protect calling pop_message. */
10502
10503 Lisp_Object
10504 pop_message_unwind (Lisp_Object dummy)
10505 {
10506 pop_message ();
10507 return Qnil;
10508 }
10509
10510 /* Pop the top-most entry off Vmessage_stack. */
10511
10512 static void
10513 pop_message (void)
10514 {
10515 eassert (CONSP (Vmessage_stack));
10516 Vmessage_stack = XCDR (Vmessage_stack);
10517 }
10518
10519
10520 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10521 exits. If the stack is not empty, we have a missing pop_message
10522 somewhere. */
10523
10524 void
10525 check_message_stack (void)
10526 {
10527 if (!NILP (Vmessage_stack))
10528 emacs_abort ();
10529 }
10530
10531
10532 /* Truncate to NCHARS what will be displayed in the echo area the next
10533 time we display it---but don't redisplay it now. */
10534
10535 void
10536 truncate_echo_area (ptrdiff_t nchars)
10537 {
10538 if (nchars == 0)
10539 echo_area_buffer[0] = Qnil;
10540 /* A null message buffer means that the frame hasn't really been
10541 initialized yet. Error messages get reported properly by
10542 cmd_error, so this must be just an informative message; toss it. */
10543 else if (!noninteractive
10544 && INTERACTIVE
10545 && !NILP (echo_area_buffer[0]))
10546 {
10547 struct frame *sf = SELECTED_FRAME ();
10548 if (FRAME_MESSAGE_BUF (sf))
10549 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10550 }
10551 }
10552
10553
10554 /* Helper function for truncate_echo_area. Truncate the current
10555 message to at most NCHARS characters. */
10556
10557 static int
10558 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10559 {
10560 if (BEG + nchars < Z)
10561 del_range (BEG + nchars, Z);
10562 if (Z == BEG)
10563 echo_area_buffer[0] = Qnil;
10564 return 0;
10565 }
10566
10567 /* Set the current message to a substring of S or STRING.
10568
10569 If STRING is a Lisp string, set the message to the first NBYTES
10570 bytes from STRING. NBYTES zero means use the whole string. If
10571 STRING is multibyte, the message will be displayed multibyte.
10572
10573 If S is not null, set the message to the first LEN bytes of S. LEN
10574 zero means use the whole string. MULTIBYTE_P non-zero means S is
10575 multibyte. Display the message multibyte in that case.
10576
10577 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10578 to t before calling set_message_1 (which calls insert).
10579 */
10580
10581 static void
10582 set_message (const char *s, Lisp_Object string,
10583 ptrdiff_t nbytes, int multibyte_p)
10584 {
10585 message_enable_multibyte
10586 = ((s && multibyte_p)
10587 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10588
10589 with_echo_area_buffer (0, -1, set_message_1,
10590 (intptr_t) s, string, nbytes, multibyte_p);
10591 message_buf_print = 0;
10592 help_echo_showing_p = 0;
10593
10594 if (STRINGP (Vdebug_on_message)
10595 && fast_string_match (Vdebug_on_message, string) >= 0)
10596 call_debugger (list2 (Qerror, string));
10597 }
10598
10599
10600 /* Helper function for set_message. Arguments have the same meaning
10601 as there, with A1 corresponding to S and A2 corresponding to STRING
10602 This function is called with the echo area buffer being
10603 current. */
10604
10605 static int
10606 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10607 {
10608 intptr_t i1 = a1;
10609 const char *s = (const char *) i1;
10610 const unsigned char *msg = (const unsigned char *) s;
10611 Lisp_Object string = a2;
10612
10613 /* Change multibyteness of the echo buffer appropriately. */
10614 if (message_enable_multibyte
10615 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10616 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10617
10618 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10619 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10620 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10621
10622 /* Insert new message at BEG. */
10623 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10624
10625 if (STRINGP (string))
10626 {
10627 ptrdiff_t nchars;
10628
10629 if (nbytes == 0)
10630 nbytes = SBYTES (string);
10631 nchars = string_byte_to_char (string, nbytes);
10632
10633 /* This function takes care of single/multibyte conversion. We
10634 just have to ensure that the echo area buffer has the right
10635 setting of enable_multibyte_characters. */
10636 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10637 }
10638 else if (s)
10639 {
10640 if (nbytes == 0)
10641 nbytes = strlen (s);
10642
10643 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10644 {
10645 /* Convert from multi-byte to single-byte. */
10646 ptrdiff_t i;
10647 int c, n;
10648 char work[1];
10649
10650 /* Convert a multibyte string to single-byte. */
10651 for (i = 0; i < nbytes; i += n)
10652 {
10653 c = string_char_and_length (msg + i, &n);
10654 work[0] = (ASCII_CHAR_P (c)
10655 ? c
10656 : multibyte_char_to_unibyte (c));
10657 insert_1_both (work, 1, 1, 1, 0, 0);
10658 }
10659 }
10660 else if (!multibyte_p
10661 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10662 {
10663 /* Convert from single-byte to multi-byte. */
10664 ptrdiff_t i;
10665 int c, n;
10666 unsigned char str[MAX_MULTIBYTE_LENGTH];
10667
10668 /* Convert a single-byte string to multibyte. */
10669 for (i = 0; i < nbytes; i++)
10670 {
10671 c = msg[i];
10672 MAKE_CHAR_MULTIBYTE (c);
10673 n = CHAR_STRING (c, str);
10674 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10675 }
10676 }
10677 else
10678 insert_1 (s, nbytes, 1, 0, 0);
10679 }
10680
10681 return 0;
10682 }
10683
10684
10685 /* Clear messages. CURRENT_P non-zero means clear the current
10686 message. LAST_DISPLAYED_P non-zero means clear the message
10687 last displayed. */
10688
10689 void
10690 clear_message (int current_p, int last_displayed_p)
10691 {
10692 if (current_p)
10693 {
10694 echo_area_buffer[0] = Qnil;
10695 message_cleared_p = 1;
10696 }
10697
10698 if (last_displayed_p)
10699 echo_area_buffer[1] = Qnil;
10700
10701 message_buf_print = 0;
10702 }
10703
10704 /* Clear garbaged frames.
10705
10706 This function is used where the old redisplay called
10707 redraw_garbaged_frames which in turn called redraw_frame which in
10708 turn called clear_frame. The call to clear_frame was a source of
10709 flickering. I believe a clear_frame is not necessary. It should
10710 suffice in the new redisplay to invalidate all current matrices,
10711 and ensure a complete redisplay of all windows. */
10712
10713 static void
10714 clear_garbaged_frames (void)
10715 {
10716 if (frame_garbaged)
10717 {
10718 Lisp_Object tail, frame;
10719 int changed_count = 0;
10720
10721 FOR_EACH_FRAME (tail, frame)
10722 {
10723 struct frame *f = XFRAME (frame);
10724
10725 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10726 {
10727 if (f->resized_p)
10728 {
10729 Fredraw_frame (frame);
10730 f->force_flush_display_p = 1;
10731 }
10732 clear_current_matrices (f);
10733 changed_count++;
10734 f->garbaged = 0;
10735 f->resized_p = 0;
10736 }
10737 }
10738
10739 frame_garbaged = 0;
10740 if (changed_count)
10741 ++windows_or_buffers_changed;
10742 }
10743 }
10744
10745
10746 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10747 is non-zero update selected_frame. Value is non-zero if the
10748 mini-windows height has been changed. */
10749
10750 static int
10751 echo_area_display (int update_frame_p)
10752 {
10753 Lisp_Object mini_window;
10754 struct window *w;
10755 struct frame *f;
10756 int window_height_changed_p = 0;
10757 struct frame *sf = SELECTED_FRAME ();
10758
10759 mini_window = FRAME_MINIBUF_WINDOW (sf);
10760 w = XWINDOW (mini_window);
10761 f = XFRAME (WINDOW_FRAME (w));
10762
10763 /* Don't display if frame is invisible or not yet initialized. */
10764 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10765 return 0;
10766
10767 #ifdef HAVE_WINDOW_SYSTEM
10768 /* When Emacs starts, selected_frame may be the initial terminal
10769 frame. If we let this through, a message would be displayed on
10770 the terminal. */
10771 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10772 return 0;
10773 #endif /* HAVE_WINDOW_SYSTEM */
10774
10775 /* Redraw garbaged frames. */
10776 if (frame_garbaged)
10777 clear_garbaged_frames ();
10778
10779 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10780 {
10781 echo_area_window = mini_window;
10782 window_height_changed_p = display_echo_area (w);
10783 w->must_be_updated_p = 1;
10784
10785 /* Update the display, unless called from redisplay_internal.
10786 Also don't update the screen during redisplay itself. The
10787 update will happen at the end of redisplay, and an update
10788 here could cause confusion. */
10789 if (update_frame_p && !redisplaying_p)
10790 {
10791 int n = 0;
10792
10793 /* If the display update has been interrupted by pending
10794 input, update mode lines in the frame. Due to the
10795 pending input, it might have been that redisplay hasn't
10796 been called, so that mode lines above the echo area are
10797 garbaged. This looks odd, so we prevent it here. */
10798 if (!display_completed)
10799 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10800
10801 if (window_height_changed_p
10802 /* Don't do this if Emacs is shutting down. Redisplay
10803 needs to run hooks. */
10804 && !NILP (Vrun_hooks))
10805 {
10806 /* Must update other windows. Likewise as in other
10807 cases, don't let this update be interrupted by
10808 pending input. */
10809 ptrdiff_t count = SPECPDL_INDEX ();
10810 specbind (Qredisplay_dont_pause, Qt);
10811 windows_or_buffers_changed = 1;
10812 redisplay_internal ();
10813 unbind_to (count, Qnil);
10814 }
10815 else if (FRAME_WINDOW_P (f) && n == 0)
10816 {
10817 /* Window configuration is the same as before.
10818 Can do with a display update of the echo area,
10819 unless we displayed some mode lines. */
10820 update_single_window (w, 1);
10821 FRAME_RIF (f)->flush_display (f);
10822 }
10823 else
10824 update_frame (f, 1, 1);
10825
10826 /* If cursor is in the echo area, make sure that the next
10827 redisplay displays the minibuffer, so that the cursor will
10828 be replaced with what the minibuffer wants. */
10829 if (cursor_in_echo_area)
10830 ++windows_or_buffers_changed;
10831 }
10832 }
10833 else if (!EQ (mini_window, selected_window))
10834 windows_or_buffers_changed++;
10835
10836 /* Last displayed message is now the current message. */
10837 echo_area_buffer[1] = echo_area_buffer[0];
10838 /* Inform read_char that we're not echoing. */
10839 echo_message_buffer = Qnil;
10840
10841 /* Prevent redisplay optimization in redisplay_internal by resetting
10842 this_line_start_pos. This is done because the mini-buffer now
10843 displays the message instead of its buffer text. */
10844 if (EQ (mini_window, selected_window))
10845 CHARPOS (this_line_start_pos) = 0;
10846
10847 return window_height_changed_p;
10848 }
10849
10850
10851 \f
10852 /***********************************************************************
10853 Mode Lines and Frame Titles
10854 ***********************************************************************/
10855
10856 /* A buffer for constructing non-propertized mode-line strings and
10857 frame titles in it; allocated from the heap in init_xdisp and
10858 resized as needed in store_mode_line_noprop_char. */
10859
10860 static char *mode_line_noprop_buf;
10861
10862 /* The buffer's end, and a current output position in it. */
10863
10864 static char *mode_line_noprop_buf_end;
10865 static char *mode_line_noprop_ptr;
10866
10867 #define MODE_LINE_NOPROP_LEN(start) \
10868 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10869
10870 static enum {
10871 MODE_LINE_DISPLAY = 0,
10872 MODE_LINE_TITLE,
10873 MODE_LINE_NOPROP,
10874 MODE_LINE_STRING
10875 } mode_line_target;
10876
10877 /* Alist that caches the results of :propertize.
10878 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10879 static Lisp_Object mode_line_proptrans_alist;
10880
10881 /* List of strings making up the mode-line. */
10882 static Lisp_Object mode_line_string_list;
10883
10884 /* Base face property when building propertized mode line string. */
10885 static Lisp_Object mode_line_string_face;
10886 static Lisp_Object mode_line_string_face_prop;
10887
10888
10889 /* Unwind data for mode line strings */
10890
10891 static Lisp_Object Vmode_line_unwind_vector;
10892
10893 static Lisp_Object
10894 format_mode_line_unwind_data (struct frame *target_frame,
10895 struct buffer *obuf,
10896 Lisp_Object owin,
10897 int save_proptrans)
10898 {
10899 Lisp_Object vector, tmp;
10900
10901 /* Reduce consing by keeping one vector in
10902 Vwith_echo_area_save_vector. */
10903 vector = Vmode_line_unwind_vector;
10904 Vmode_line_unwind_vector = Qnil;
10905
10906 if (NILP (vector))
10907 vector = Fmake_vector (make_number (10), Qnil);
10908
10909 ASET (vector, 0, make_number (mode_line_target));
10910 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10911 ASET (vector, 2, mode_line_string_list);
10912 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10913 ASET (vector, 4, mode_line_string_face);
10914 ASET (vector, 5, mode_line_string_face_prop);
10915
10916 if (obuf)
10917 XSETBUFFER (tmp, obuf);
10918 else
10919 tmp = Qnil;
10920 ASET (vector, 6, tmp);
10921 ASET (vector, 7, owin);
10922 if (target_frame)
10923 {
10924 /* Similarly to `with-selected-window', if the operation selects
10925 a window on another frame, we must restore that frame's
10926 selected window, and (for a tty) the top-frame. */
10927 ASET (vector, 8, target_frame->selected_window);
10928 if (FRAME_TERMCAP_P (target_frame))
10929 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10930 }
10931
10932 return vector;
10933 }
10934
10935 static Lisp_Object
10936 unwind_format_mode_line (Lisp_Object vector)
10937 {
10938 Lisp_Object old_window = AREF (vector, 7);
10939 Lisp_Object target_frame_window = AREF (vector, 8);
10940 Lisp_Object old_top_frame = AREF (vector, 9);
10941
10942 mode_line_target = XINT (AREF (vector, 0));
10943 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10944 mode_line_string_list = AREF (vector, 2);
10945 if (! EQ (AREF (vector, 3), Qt))
10946 mode_line_proptrans_alist = AREF (vector, 3);
10947 mode_line_string_face = AREF (vector, 4);
10948 mode_line_string_face_prop = AREF (vector, 5);
10949
10950 /* Select window before buffer, since it may change the buffer. */
10951 if (!NILP (old_window))
10952 {
10953 /* If the operation that we are unwinding had selected a window
10954 on a different frame, reset its frame-selected-window. For a
10955 text terminal, reset its top-frame if necessary. */
10956 if (!NILP (target_frame_window))
10957 {
10958 Lisp_Object frame
10959 = WINDOW_FRAME (XWINDOW (target_frame_window));
10960
10961 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10962 Fselect_window (target_frame_window, Qt);
10963
10964 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10965 Fselect_frame (old_top_frame, Qt);
10966 }
10967
10968 Fselect_window (old_window, Qt);
10969 }
10970
10971 if (!NILP (AREF (vector, 6)))
10972 {
10973 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10974 ASET (vector, 6, Qnil);
10975 }
10976
10977 Vmode_line_unwind_vector = vector;
10978 return Qnil;
10979 }
10980
10981
10982 /* Store a single character C for the frame title in mode_line_noprop_buf.
10983 Re-allocate mode_line_noprop_buf if necessary. */
10984
10985 static void
10986 store_mode_line_noprop_char (char c)
10987 {
10988 /* If output position has reached the end of the allocated buffer,
10989 increase the buffer's size. */
10990 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10991 {
10992 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10993 ptrdiff_t size = len;
10994 mode_line_noprop_buf =
10995 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10996 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10997 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10998 }
10999
11000 *mode_line_noprop_ptr++ = c;
11001 }
11002
11003
11004 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11005 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11006 characters that yield more columns than PRECISION; PRECISION <= 0
11007 means copy the whole string. Pad with spaces until FIELD_WIDTH
11008 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11009 pad. Called from display_mode_element when it is used to build a
11010 frame title. */
11011
11012 static int
11013 store_mode_line_noprop (const char *string, int field_width, int precision)
11014 {
11015 const unsigned char *str = (const unsigned char *) string;
11016 int n = 0;
11017 ptrdiff_t dummy, nbytes;
11018
11019 /* Copy at most PRECISION chars from STR. */
11020 nbytes = strlen (string);
11021 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11022 while (nbytes--)
11023 store_mode_line_noprop_char (*str++);
11024
11025 /* Fill up with spaces until FIELD_WIDTH reached. */
11026 while (field_width > 0
11027 && n < field_width)
11028 {
11029 store_mode_line_noprop_char (' ');
11030 ++n;
11031 }
11032
11033 return n;
11034 }
11035
11036 /***********************************************************************
11037 Frame Titles
11038 ***********************************************************************/
11039
11040 #ifdef HAVE_WINDOW_SYSTEM
11041
11042 /* Set the title of FRAME, if it has changed. The title format is
11043 Vicon_title_format if FRAME is iconified, otherwise it is
11044 frame_title_format. */
11045
11046 static void
11047 x_consider_frame_title (Lisp_Object frame)
11048 {
11049 struct frame *f = XFRAME (frame);
11050
11051 if (FRAME_WINDOW_P (f)
11052 || FRAME_MINIBUF_ONLY_P (f)
11053 || f->explicit_name)
11054 {
11055 /* Do we have more than one visible frame on this X display? */
11056 Lisp_Object tail;
11057 Lisp_Object fmt;
11058 ptrdiff_t title_start;
11059 char *title;
11060 ptrdiff_t len;
11061 struct it it;
11062 ptrdiff_t count = SPECPDL_INDEX ();
11063
11064 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
11065 {
11066 Lisp_Object other_frame = XCAR (tail);
11067 struct frame *tf = XFRAME (other_frame);
11068
11069 if (tf != f
11070 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11071 && !FRAME_MINIBUF_ONLY_P (tf)
11072 && !EQ (other_frame, tip_frame)
11073 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11074 break;
11075 }
11076
11077 /* Set global variable indicating that multiple frames exist. */
11078 multiple_frames = CONSP (tail);
11079
11080 /* Switch to the buffer of selected window of the frame. Set up
11081 mode_line_target so that display_mode_element will output into
11082 mode_line_noprop_buf; then display the title. */
11083 record_unwind_protect (unwind_format_mode_line,
11084 format_mode_line_unwind_data
11085 (f, current_buffer, selected_window, 0));
11086
11087 Fselect_window (f->selected_window, Qt);
11088 set_buffer_internal_1
11089 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11090 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11091
11092 mode_line_target = MODE_LINE_TITLE;
11093 title_start = MODE_LINE_NOPROP_LEN (0);
11094 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11095 NULL, DEFAULT_FACE_ID);
11096 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11097 len = MODE_LINE_NOPROP_LEN (title_start);
11098 title = mode_line_noprop_buf + title_start;
11099 unbind_to (count, Qnil);
11100
11101 /* Set the title only if it's changed. This avoids consing in
11102 the common case where it hasn't. (If it turns out that we've
11103 already wasted too much time by walking through the list with
11104 display_mode_element, then we might need to optimize at a
11105 higher level than this.) */
11106 if (! STRINGP (f->name)
11107 || SBYTES (f->name) != len
11108 || memcmp (title, SDATA (f->name), len) != 0)
11109 x_implicitly_set_name (f, make_string (title, len), Qnil);
11110 }
11111 }
11112
11113 #endif /* not HAVE_WINDOW_SYSTEM */
11114
11115 \f
11116 /***********************************************************************
11117 Menu Bars
11118 ***********************************************************************/
11119
11120
11121 /* Prepare for redisplay by updating menu-bar item lists when
11122 appropriate. This can call eval. */
11123
11124 void
11125 prepare_menu_bars (void)
11126 {
11127 int all_windows;
11128 struct gcpro gcpro1, gcpro2;
11129 struct frame *f;
11130 Lisp_Object tooltip_frame;
11131
11132 #ifdef HAVE_WINDOW_SYSTEM
11133 tooltip_frame = tip_frame;
11134 #else
11135 tooltip_frame = Qnil;
11136 #endif
11137
11138 /* Update all frame titles based on their buffer names, etc. We do
11139 this before the menu bars so that the buffer-menu will show the
11140 up-to-date frame titles. */
11141 #ifdef HAVE_WINDOW_SYSTEM
11142 if (windows_or_buffers_changed || update_mode_lines)
11143 {
11144 Lisp_Object tail, frame;
11145
11146 FOR_EACH_FRAME (tail, frame)
11147 {
11148 f = XFRAME (frame);
11149 if (!EQ (frame, tooltip_frame)
11150 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11151 x_consider_frame_title (frame);
11152 }
11153 }
11154 #endif /* HAVE_WINDOW_SYSTEM */
11155
11156 /* Update the menu bar item lists, if appropriate. This has to be
11157 done before any actual redisplay or generation of display lines. */
11158 all_windows = (update_mode_lines
11159 || buffer_shared > 1
11160 || windows_or_buffers_changed);
11161 if (all_windows)
11162 {
11163 Lisp_Object tail, frame;
11164 ptrdiff_t count = SPECPDL_INDEX ();
11165 /* 1 means that update_menu_bar has run its hooks
11166 so any further calls to update_menu_bar shouldn't do so again. */
11167 int menu_bar_hooks_run = 0;
11168
11169 record_unwind_save_match_data ();
11170
11171 FOR_EACH_FRAME (tail, frame)
11172 {
11173 f = XFRAME (frame);
11174
11175 /* Ignore tooltip frame. */
11176 if (EQ (frame, tooltip_frame))
11177 continue;
11178
11179 /* If a window on this frame changed size, report that to
11180 the user and clear the size-change flag. */
11181 if (FRAME_WINDOW_SIZES_CHANGED (f))
11182 {
11183 Lisp_Object functions;
11184
11185 /* Clear flag first in case we get an error below. */
11186 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11187 functions = Vwindow_size_change_functions;
11188 GCPRO2 (tail, functions);
11189
11190 while (CONSP (functions))
11191 {
11192 if (!EQ (XCAR (functions), Qt))
11193 call1 (XCAR (functions), frame);
11194 functions = XCDR (functions);
11195 }
11196 UNGCPRO;
11197 }
11198
11199 GCPRO1 (tail);
11200 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11201 #ifdef HAVE_WINDOW_SYSTEM
11202 update_tool_bar (f, 0);
11203 #endif
11204 #ifdef HAVE_NS
11205 if (windows_or_buffers_changed
11206 && FRAME_NS_P (f))
11207 ns_set_doc_edited
11208 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->buffer));
11209 #endif
11210 UNGCPRO;
11211 }
11212
11213 unbind_to (count, Qnil);
11214 }
11215 else
11216 {
11217 struct frame *sf = SELECTED_FRAME ();
11218 update_menu_bar (sf, 1, 0);
11219 #ifdef HAVE_WINDOW_SYSTEM
11220 update_tool_bar (sf, 1);
11221 #endif
11222 }
11223 }
11224
11225
11226 /* Update the menu bar item list for frame F. This has to be done
11227 before we start to fill in any display lines, because it can call
11228 eval.
11229
11230 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11231
11232 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11233 already ran the menu bar hooks for this redisplay, so there
11234 is no need to run them again. The return value is the
11235 updated value of this flag, to pass to the next call. */
11236
11237 static int
11238 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11239 {
11240 Lisp_Object window;
11241 register struct window *w;
11242
11243 /* If called recursively during a menu update, do nothing. This can
11244 happen when, for instance, an activate-menubar-hook causes a
11245 redisplay. */
11246 if (inhibit_menubar_update)
11247 return hooks_run;
11248
11249 window = FRAME_SELECTED_WINDOW (f);
11250 w = XWINDOW (window);
11251
11252 if (FRAME_WINDOW_P (f)
11253 ?
11254 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11255 || defined (HAVE_NS) || defined (USE_GTK)
11256 FRAME_EXTERNAL_MENU_BAR (f)
11257 #else
11258 FRAME_MENU_BAR_LINES (f) > 0
11259 #endif
11260 : FRAME_MENU_BAR_LINES (f) > 0)
11261 {
11262 /* If the user has switched buffers or windows, we need to
11263 recompute to reflect the new bindings. But we'll
11264 recompute when update_mode_lines is set too; that means
11265 that people can use force-mode-line-update to request
11266 that the menu bar be recomputed. The adverse effect on
11267 the rest of the redisplay algorithm is about the same as
11268 windows_or_buffers_changed anyway. */
11269 if (windows_or_buffers_changed
11270 /* This used to test w->update_mode_line, but we believe
11271 there is no need to recompute the menu in that case. */
11272 || update_mode_lines
11273 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11274 < BUF_MODIFF (XBUFFER (w->buffer)))
11275 != w->last_had_star)
11276 || ((!NILP (Vtransient_mark_mode)
11277 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11278 != !NILP (w->region_showing)))
11279 {
11280 struct buffer *prev = current_buffer;
11281 ptrdiff_t count = SPECPDL_INDEX ();
11282
11283 specbind (Qinhibit_menubar_update, Qt);
11284
11285 set_buffer_internal_1 (XBUFFER (w->buffer));
11286 if (save_match_data)
11287 record_unwind_save_match_data ();
11288 if (NILP (Voverriding_local_map_menu_flag))
11289 {
11290 specbind (Qoverriding_terminal_local_map, Qnil);
11291 specbind (Qoverriding_local_map, Qnil);
11292 }
11293
11294 if (!hooks_run)
11295 {
11296 /* Run the Lucid hook. */
11297 safe_run_hooks (Qactivate_menubar_hook);
11298
11299 /* If it has changed current-menubar from previous value,
11300 really recompute the menu-bar from the value. */
11301 if (! NILP (Vlucid_menu_bar_dirty_flag))
11302 call0 (Qrecompute_lucid_menubar);
11303
11304 safe_run_hooks (Qmenu_bar_update_hook);
11305
11306 hooks_run = 1;
11307 }
11308
11309 XSETFRAME (Vmenu_updating_frame, f);
11310 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11311
11312 /* Redisplay the menu bar in case we changed it. */
11313 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11314 || defined (HAVE_NS) || defined (USE_GTK)
11315 if (FRAME_WINDOW_P (f))
11316 {
11317 #if defined (HAVE_NS)
11318 /* All frames on Mac OS share the same menubar. So only
11319 the selected frame should be allowed to set it. */
11320 if (f == SELECTED_FRAME ())
11321 #endif
11322 set_frame_menubar (f, 0, 0);
11323 }
11324 else
11325 /* On a terminal screen, the menu bar is an ordinary screen
11326 line, and this makes it get updated. */
11327 w->update_mode_line = 1;
11328 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11329 /* In the non-toolkit version, the menu bar is an ordinary screen
11330 line, and this makes it get updated. */
11331 w->update_mode_line = 1;
11332 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11333
11334 unbind_to (count, Qnil);
11335 set_buffer_internal_1 (prev);
11336 }
11337 }
11338
11339 return hooks_run;
11340 }
11341
11342
11343 \f
11344 /***********************************************************************
11345 Output Cursor
11346 ***********************************************************************/
11347
11348 #ifdef HAVE_WINDOW_SYSTEM
11349
11350 /* EXPORT:
11351 Nominal cursor position -- where to draw output.
11352 HPOS and VPOS are window relative glyph matrix coordinates.
11353 X and Y are window relative pixel coordinates. */
11354
11355 struct cursor_pos output_cursor;
11356
11357
11358 /* EXPORT:
11359 Set the global variable output_cursor to CURSOR. All cursor
11360 positions are relative to updated_window. */
11361
11362 void
11363 set_output_cursor (struct cursor_pos *cursor)
11364 {
11365 output_cursor.hpos = cursor->hpos;
11366 output_cursor.vpos = cursor->vpos;
11367 output_cursor.x = cursor->x;
11368 output_cursor.y = cursor->y;
11369 }
11370
11371
11372 /* EXPORT for RIF:
11373 Set a nominal cursor position.
11374
11375 HPOS and VPOS are column/row positions in a window glyph matrix. X
11376 and Y are window text area relative pixel positions.
11377
11378 If this is done during an update, updated_window will contain the
11379 window that is being updated and the position is the future output
11380 cursor position for that window. If updated_window is null, use
11381 selected_window and display the cursor at the given position. */
11382
11383 void
11384 x_cursor_to (int vpos, int hpos, int y, int x)
11385 {
11386 struct window *w;
11387
11388 /* If updated_window is not set, work on selected_window. */
11389 if (updated_window)
11390 w = updated_window;
11391 else
11392 w = XWINDOW (selected_window);
11393
11394 /* Set the output cursor. */
11395 output_cursor.hpos = hpos;
11396 output_cursor.vpos = vpos;
11397 output_cursor.x = x;
11398 output_cursor.y = y;
11399
11400 /* If not called as part of an update, really display the cursor.
11401 This will also set the cursor position of W. */
11402 if (updated_window == NULL)
11403 {
11404 BLOCK_INPUT;
11405 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11406 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11407 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11408 UNBLOCK_INPUT;
11409 }
11410 }
11411
11412 #endif /* HAVE_WINDOW_SYSTEM */
11413
11414 \f
11415 /***********************************************************************
11416 Tool-bars
11417 ***********************************************************************/
11418
11419 #ifdef HAVE_WINDOW_SYSTEM
11420
11421 /* Where the mouse was last time we reported a mouse event. */
11422
11423 FRAME_PTR last_mouse_frame;
11424
11425 /* Tool-bar item index of the item on which a mouse button was pressed
11426 or -1. */
11427
11428 int last_tool_bar_item;
11429
11430
11431 static Lisp_Object
11432 update_tool_bar_unwind (Lisp_Object frame)
11433 {
11434 selected_frame = frame;
11435 return Qnil;
11436 }
11437
11438 /* Update the tool-bar item list for frame F. This has to be done
11439 before we start to fill in any display lines. Called from
11440 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11441 and restore it here. */
11442
11443 static void
11444 update_tool_bar (struct frame *f, int save_match_data)
11445 {
11446 #if defined (USE_GTK) || defined (HAVE_NS)
11447 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11448 #else
11449 int do_update = WINDOWP (f->tool_bar_window)
11450 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11451 #endif
11452
11453 if (do_update)
11454 {
11455 Lisp_Object window;
11456 struct window *w;
11457
11458 window = FRAME_SELECTED_WINDOW (f);
11459 w = XWINDOW (window);
11460
11461 /* If the user has switched buffers or windows, we need to
11462 recompute to reflect the new bindings. But we'll
11463 recompute when update_mode_lines is set too; that means
11464 that people can use force-mode-line-update to request
11465 that the menu bar be recomputed. The adverse effect on
11466 the rest of the redisplay algorithm is about the same as
11467 windows_or_buffers_changed anyway. */
11468 if (windows_or_buffers_changed
11469 || w->update_mode_line
11470 || update_mode_lines
11471 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11472 < BUF_MODIFF (XBUFFER (w->buffer)))
11473 != w->last_had_star)
11474 || ((!NILP (Vtransient_mark_mode)
11475 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11476 != !NILP (w->region_showing)))
11477 {
11478 struct buffer *prev = current_buffer;
11479 ptrdiff_t count = SPECPDL_INDEX ();
11480 Lisp_Object frame, new_tool_bar;
11481 int new_n_tool_bar;
11482 struct gcpro gcpro1;
11483
11484 /* Set current_buffer to the buffer of the selected
11485 window of the frame, so that we get the right local
11486 keymaps. */
11487 set_buffer_internal_1 (XBUFFER (w->buffer));
11488
11489 /* Save match data, if we must. */
11490 if (save_match_data)
11491 record_unwind_save_match_data ();
11492
11493 /* Make sure that we don't accidentally use bogus keymaps. */
11494 if (NILP (Voverriding_local_map_menu_flag))
11495 {
11496 specbind (Qoverriding_terminal_local_map, Qnil);
11497 specbind (Qoverriding_local_map, Qnil);
11498 }
11499
11500 GCPRO1 (new_tool_bar);
11501
11502 /* We must temporarily set the selected frame to this frame
11503 before calling tool_bar_items, because the calculation of
11504 the tool-bar keymap uses the selected frame (see
11505 `tool-bar-make-keymap' in tool-bar.el). */
11506 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11507 XSETFRAME (frame, f);
11508 selected_frame = frame;
11509
11510 /* Build desired tool-bar items from keymaps. */
11511 new_tool_bar
11512 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11513 &new_n_tool_bar);
11514
11515 /* Redisplay the tool-bar if we changed it. */
11516 if (new_n_tool_bar != f->n_tool_bar_items
11517 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11518 {
11519 /* Redisplay that happens asynchronously due to an expose event
11520 may access f->tool_bar_items. Make sure we update both
11521 variables within BLOCK_INPUT so no such event interrupts. */
11522 BLOCK_INPUT;
11523 fset_tool_bar_items (f, new_tool_bar);
11524 f->n_tool_bar_items = new_n_tool_bar;
11525 w->update_mode_line = 1;
11526 UNBLOCK_INPUT;
11527 }
11528
11529 UNGCPRO;
11530
11531 unbind_to (count, Qnil);
11532 set_buffer_internal_1 (prev);
11533 }
11534 }
11535 }
11536
11537
11538 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11539 F's desired tool-bar contents. F->tool_bar_items must have
11540 been set up previously by calling prepare_menu_bars. */
11541
11542 static void
11543 build_desired_tool_bar_string (struct frame *f)
11544 {
11545 int i, size, size_needed;
11546 struct gcpro gcpro1, gcpro2, gcpro3;
11547 Lisp_Object image, plist, props;
11548
11549 image = plist = props = Qnil;
11550 GCPRO3 (image, plist, props);
11551
11552 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11553 Otherwise, make a new string. */
11554
11555 /* The size of the string we might be able to reuse. */
11556 size = (STRINGP (f->desired_tool_bar_string)
11557 ? SCHARS (f->desired_tool_bar_string)
11558 : 0);
11559
11560 /* We need one space in the string for each image. */
11561 size_needed = f->n_tool_bar_items;
11562
11563 /* Reuse f->desired_tool_bar_string, if possible. */
11564 if (size < size_needed || NILP (f->desired_tool_bar_string))
11565 fset_desired_tool_bar_string
11566 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11567 else
11568 {
11569 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11570 Fremove_text_properties (make_number (0), make_number (size),
11571 props, f->desired_tool_bar_string);
11572 }
11573
11574 /* Put a `display' property on the string for the images to display,
11575 put a `menu_item' property on tool-bar items with a value that
11576 is the index of the item in F's tool-bar item vector. */
11577 for (i = 0; i < f->n_tool_bar_items; ++i)
11578 {
11579 #define PROP(IDX) \
11580 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11581
11582 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11583 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11584 int hmargin, vmargin, relief, idx, end;
11585
11586 /* If image is a vector, choose the image according to the
11587 button state. */
11588 image = PROP (TOOL_BAR_ITEM_IMAGES);
11589 if (VECTORP (image))
11590 {
11591 if (enabled_p)
11592 idx = (selected_p
11593 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11594 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11595 else
11596 idx = (selected_p
11597 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11598 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11599
11600 eassert (ASIZE (image) >= idx);
11601 image = AREF (image, idx);
11602 }
11603 else
11604 idx = -1;
11605
11606 /* Ignore invalid image specifications. */
11607 if (!valid_image_p (image))
11608 continue;
11609
11610 /* Display the tool-bar button pressed, or depressed. */
11611 plist = Fcopy_sequence (XCDR (image));
11612
11613 /* Compute margin and relief to draw. */
11614 relief = (tool_bar_button_relief >= 0
11615 ? tool_bar_button_relief
11616 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11617 hmargin = vmargin = relief;
11618
11619 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11620 INT_MAX - max (hmargin, vmargin)))
11621 {
11622 hmargin += XFASTINT (Vtool_bar_button_margin);
11623 vmargin += XFASTINT (Vtool_bar_button_margin);
11624 }
11625 else if (CONSP (Vtool_bar_button_margin))
11626 {
11627 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11628 INT_MAX - hmargin))
11629 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11630
11631 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11632 INT_MAX - vmargin))
11633 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11634 }
11635
11636 if (auto_raise_tool_bar_buttons_p)
11637 {
11638 /* Add a `:relief' property to the image spec if the item is
11639 selected. */
11640 if (selected_p)
11641 {
11642 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11643 hmargin -= relief;
11644 vmargin -= relief;
11645 }
11646 }
11647 else
11648 {
11649 /* If image is selected, display it pressed, i.e. with a
11650 negative relief. If it's not selected, display it with a
11651 raised relief. */
11652 plist = Fplist_put (plist, QCrelief,
11653 (selected_p
11654 ? make_number (-relief)
11655 : make_number (relief)));
11656 hmargin -= relief;
11657 vmargin -= relief;
11658 }
11659
11660 /* Put a margin around the image. */
11661 if (hmargin || vmargin)
11662 {
11663 if (hmargin == vmargin)
11664 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11665 else
11666 plist = Fplist_put (plist, QCmargin,
11667 Fcons (make_number (hmargin),
11668 make_number (vmargin)));
11669 }
11670
11671 /* If button is not enabled, and we don't have special images
11672 for the disabled state, make the image appear disabled by
11673 applying an appropriate algorithm to it. */
11674 if (!enabled_p && idx < 0)
11675 plist = Fplist_put (plist, QCconversion, Qdisabled);
11676
11677 /* Put a `display' text property on the string for the image to
11678 display. Put a `menu-item' property on the string that gives
11679 the start of this item's properties in the tool-bar items
11680 vector. */
11681 image = Fcons (Qimage, plist);
11682 props = list4 (Qdisplay, image,
11683 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11684
11685 /* Let the last image hide all remaining spaces in the tool bar
11686 string. The string can be longer than needed when we reuse a
11687 previous string. */
11688 if (i + 1 == f->n_tool_bar_items)
11689 end = SCHARS (f->desired_tool_bar_string);
11690 else
11691 end = i + 1;
11692 Fadd_text_properties (make_number (i), make_number (end),
11693 props, f->desired_tool_bar_string);
11694 #undef PROP
11695 }
11696
11697 UNGCPRO;
11698 }
11699
11700
11701 /* Display one line of the tool-bar of frame IT->f.
11702
11703 HEIGHT specifies the desired height of the tool-bar line.
11704 If the actual height of the glyph row is less than HEIGHT, the
11705 row's height is increased to HEIGHT, and the icons are centered
11706 vertically in the new height.
11707
11708 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11709 count a final empty row in case the tool-bar width exactly matches
11710 the window width.
11711 */
11712
11713 static void
11714 display_tool_bar_line (struct it *it, int height)
11715 {
11716 struct glyph_row *row = it->glyph_row;
11717 int max_x = it->last_visible_x;
11718 struct glyph *last;
11719
11720 prepare_desired_row (row);
11721 row->y = it->current_y;
11722
11723 /* Note that this isn't made use of if the face hasn't a box,
11724 so there's no need to check the face here. */
11725 it->start_of_box_run_p = 1;
11726
11727 while (it->current_x < max_x)
11728 {
11729 int x, n_glyphs_before, i, nglyphs;
11730 struct it it_before;
11731
11732 /* Get the next display element. */
11733 if (!get_next_display_element (it))
11734 {
11735 /* Don't count empty row if we are counting needed tool-bar lines. */
11736 if (height < 0 && !it->hpos)
11737 return;
11738 break;
11739 }
11740
11741 /* Produce glyphs. */
11742 n_glyphs_before = row->used[TEXT_AREA];
11743 it_before = *it;
11744
11745 PRODUCE_GLYPHS (it);
11746
11747 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11748 i = 0;
11749 x = it_before.current_x;
11750 while (i < nglyphs)
11751 {
11752 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11753
11754 if (x + glyph->pixel_width > max_x)
11755 {
11756 /* Glyph doesn't fit on line. Backtrack. */
11757 row->used[TEXT_AREA] = n_glyphs_before;
11758 *it = it_before;
11759 /* If this is the only glyph on this line, it will never fit on the
11760 tool-bar, so skip it. But ensure there is at least one glyph,
11761 so we don't accidentally disable the tool-bar. */
11762 if (n_glyphs_before == 0
11763 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11764 break;
11765 goto out;
11766 }
11767
11768 ++it->hpos;
11769 x += glyph->pixel_width;
11770 ++i;
11771 }
11772
11773 /* Stop at line end. */
11774 if (ITERATOR_AT_END_OF_LINE_P (it))
11775 break;
11776
11777 set_iterator_to_next (it, 1);
11778 }
11779
11780 out:;
11781
11782 row->displays_text_p = row->used[TEXT_AREA] != 0;
11783
11784 /* Use default face for the border below the tool bar.
11785
11786 FIXME: When auto-resize-tool-bars is grow-only, there is
11787 no additional border below the possibly empty tool-bar lines.
11788 So to make the extra empty lines look "normal", we have to
11789 use the tool-bar face for the border too. */
11790 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11791 it->face_id = DEFAULT_FACE_ID;
11792
11793 extend_face_to_end_of_line (it);
11794 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11795 last->right_box_line_p = 1;
11796 if (last == row->glyphs[TEXT_AREA])
11797 last->left_box_line_p = 1;
11798
11799 /* Make line the desired height and center it vertically. */
11800 if ((height -= it->max_ascent + it->max_descent) > 0)
11801 {
11802 /* Don't add more than one line height. */
11803 height %= FRAME_LINE_HEIGHT (it->f);
11804 it->max_ascent += height / 2;
11805 it->max_descent += (height + 1) / 2;
11806 }
11807
11808 compute_line_metrics (it);
11809
11810 /* If line is empty, make it occupy the rest of the tool-bar. */
11811 if (!row->displays_text_p)
11812 {
11813 row->height = row->phys_height = it->last_visible_y - row->y;
11814 row->visible_height = row->height;
11815 row->ascent = row->phys_ascent = 0;
11816 row->extra_line_spacing = 0;
11817 }
11818
11819 row->full_width_p = 1;
11820 row->continued_p = 0;
11821 row->truncated_on_left_p = 0;
11822 row->truncated_on_right_p = 0;
11823
11824 it->current_x = it->hpos = 0;
11825 it->current_y += row->height;
11826 ++it->vpos;
11827 ++it->glyph_row;
11828 }
11829
11830
11831 /* Max tool-bar height. */
11832
11833 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11834 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11835
11836 /* Value is the number of screen lines needed to make all tool-bar
11837 items of frame F visible. The number of actual rows needed is
11838 returned in *N_ROWS if non-NULL. */
11839
11840 static int
11841 tool_bar_lines_needed (struct frame *f, int *n_rows)
11842 {
11843 struct window *w = XWINDOW (f->tool_bar_window);
11844 struct it it;
11845 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11846 the desired matrix, so use (unused) mode-line row as temporary row to
11847 avoid destroying the first tool-bar row. */
11848 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11849
11850 /* Initialize an iterator for iteration over
11851 F->desired_tool_bar_string in the tool-bar window of frame F. */
11852 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11853 it.first_visible_x = 0;
11854 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11855 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11856 it.paragraph_embedding = L2R;
11857
11858 while (!ITERATOR_AT_END_P (&it))
11859 {
11860 clear_glyph_row (temp_row);
11861 it.glyph_row = temp_row;
11862 display_tool_bar_line (&it, -1);
11863 }
11864 clear_glyph_row (temp_row);
11865
11866 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11867 if (n_rows)
11868 *n_rows = it.vpos > 0 ? it.vpos : -1;
11869
11870 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11871 }
11872
11873
11874 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11875 0, 1, 0,
11876 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11877 (Lisp_Object frame)
11878 {
11879 struct frame *f;
11880 struct window *w;
11881 int nlines = 0;
11882
11883 if (NILP (frame))
11884 frame = selected_frame;
11885 else
11886 CHECK_FRAME (frame);
11887 f = XFRAME (frame);
11888
11889 if (WINDOWP (f->tool_bar_window)
11890 && (w = XWINDOW (f->tool_bar_window),
11891 WINDOW_TOTAL_LINES (w) > 0))
11892 {
11893 update_tool_bar (f, 1);
11894 if (f->n_tool_bar_items)
11895 {
11896 build_desired_tool_bar_string (f);
11897 nlines = tool_bar_lines_needed (f, NULL);
11898 }
11899 }
11900
11901 return make_number (nlines);
11902 }
11903
11904
11905 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11906 height should be changed. */
11907
11908 static int
11909 redisplay_tool_bar (struct frame *f)
11910 {
11911 struct window *w;
11912 struct it it;
11913 struct glyph_row *row;
11914
11915 #if defined (USE_GTK) || defined (HAVE_NS)
11916 if (FRAME_EXTERNAL_TOOL_BAR (f))
11917 update_frame_tool_bar (f);
11918 return 0;
11919 #endif
11920
11921 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11922 do anything. This means you must start with tool-bar-lines
11923 non-zero to get the auto-sizing effect. Or in other words, you
11924 can turn off tool-bars by specifying tool-bar-lines zero. */
11925 if (!WINDOWP (f->tool_bar_window)
11926 || (w = XWINDOW (f->tool_bar_window),
11927 WINDOW_TOTAL_LINES (w) == 0))
11928 return 0;
11929
11930 /* Set up an iterator for the tool-bar window. */
11931 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11932 it.first_visible_x = 0;
11933 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11934 row = it.glyph_row;
11935
11936 /* Build a string that represents the contents of the tool-bar. */
11937 build_desired_tool_bar_string (f);
11938 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11939 /* FIXME: This should be controlled by a user option. But it
11940 doesn't make sense to have an R2L tool bar if the menu bar cannot
11941 be drawn also R2L, and making the menu bar R2L is tricky due
11942 toolkit-specific code that implements it. If an R2L tool bar is
11943 ever supported, display_tool_bar_line should also be augmented to
11944 call unproduce_glyphs like display_line and display_string
11945 do. */
11946 it.paragraph_embedding = L2R;
11947
11948 if (f->n_tool_bar_rows == 0)
11949 {
11950 int nlines;
11951
11952 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11953 nlines != WINDOW_TOTAL_LINES (w)))
11954 {
11955 Lisp_Object frame;
11956 int old_height = WINDOW_TOTAL_LINES (w);
11957
11958 XSETFRAME (frame, f);
11959 Fmodify_frame_parameters (frame,
11960 Fcons (Fcons (Qtool_bar_lines,
11961 make_number (nlines)),
11962 Qnil));
11963 if (WINDOW_TOTAL_LINES (w) != old_height)
11964 {
11965 clear_glyph_matrix (w->desired_matrix);
11966 fonts_changed_p = 1;
11967 return 1;
11968 }
11969 }
11970 }
11971
11972 /* Display as many lines as needed to display all tool-bar items. */
11973
11974 if (f->n_tool_bar_rows > 0)
11975 {
11976 int border, rows, height, extra;
11977
11978 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11979 border = XINT (Vtool_bar_border);
11980 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11981 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11982 else if (EQ (Vtool_bar_border, Qborder_width))
11983 border = f->border_width;
11984 else
11985 border = 0;
11986 if (border < 0)
11987 border = 0;
11988
11989 rows = f->n_tool_bar_rows;
11990 height = max (1, (it.last_visible_y - border) / rows);
11991 extra = it.last_visible_y - border - height * rows;
11992
11993 while (it.current_y < it.last_visible_y)
11994 {
11995 int h = 0;
11996 if (extra > 0 && rows-- > 0)
11997 {
11998 h = (extra + rows - 1) / rows;
11999 extra -= h;
12000 }
12001 display_tool_bar_line (&it, height + h);
12002 }
12003 }
12004 else
12005 {
12006 while (it.current_y < it.last_visible_y)
12007 display_tool_bar_line (&it, 0);
12008 }
12009
12010 /* It doesn't make much sense to try scrolling in the tool-bar
12011 window, so don't do it. */
12012 w->desired_matrix->no_scrolling_p = 1;
12013 w->must_be_updated_p = 1;
12014
12015 if (!NILP (Vauto_resize_tool_bars))
12016 {
12017 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12018 int change_height_p = 0;
12019
12020 /* If we couldn't display everything, change the tool-bar's
12021 height if there is room for more. */
12022 if (IT_STRING_CHARPOS (it) < it.end_charpos
12023 && it.current_y < max_tool_bar_height)
12024 change_height_p = 1;
12025
12026 row = it.glyph_row - 1;
12027
12028 /* If there are blank lines at the end, except for a partially
12029 visible blank line at the end that is smaller than
12030 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12031 if (!row->displays_text_p
12032 && row->height >= FRAME_LINE_HEIGHT (f))
12033 change_height_p = 1;
12034
12035 /* If row displays tool-bar items, but is partially visible,
12036 change the tool-bar's height. */
12037 if (row->displays_text_p
12038 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12039 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12040 change_height_p = 1;
12041
12042 /* Resize windows as needed by changing the `tool-bar-lines'
12043 frame parameter. */
12044 if (change_height_p)
12045 {
12046 Lisp_Object frame;
12047 int old_height = WINDOW_TOTAL_LINES (w);
12048 int nrows;
12049 int nlines = tool_bar_lines_needed (f, &nrows);
12050
12051 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12052 && !f->minimize_tool_bar_window_p)
12053 ? (nlines > old_height)
12054 : (nlines != old_height));
12055 f->minimize_tool_bar_window_p = 0;
12056
12057 if (change_height_p)
12058 {
12059 XSETFRAME (frame, f);
12060 Fmodify_frame_parameters (frame,
12061 Fcons (Fcons (Qtool_bar_lines,
12062 make_number (nlines)),
12063 Qnil));
12064 if (WINDOW_TOTAL_LINES (w) != old_height)
12065 {
12066 clear_glyph_matrix (w->desired_matrix);
12067 f->n_tool_bar_rows = nrows;
12068 fonts_changed_p = 1;
12069 return 1;
12070 }
12071 }
12072 }
12073 }
12074
12075 f->minimize_tool_bar_window_p = 0;
12076 return 0;
12077 }
12078
12079
12080 /* Get information about the tool-bar item which is displayed in GLYPH
12081 on frame F. Return in *PROP_IDX the index where tool-bar item
12082 properties start in F->tool_bar_items. Value is zero if
12083 GLYPH doesn't display a tool-bar item. */
12084
12085 static int
12086 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12087 {
12088 Lisp_Object prop;
12089 int success_p;
12090 int charpos;
12091
12092 /* This function can be called asynchronously, which means we must
12093 exclude any possibility that Fget_text_property signals an
12094 error. */
12095 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12096 charpos = max (0, charpos);
12097
12098 /* Get the text property `menu-item' at pos. The value of that
12099 property is the start index of this item's properties in
12100 F->tool_bar_items. */
12101 prop = Fget_text_property (make_number (charpos),
12102 Qmenu_item, f->current_tool_bar_string);
12103 if (INTEGERP (prop))
12104 {
12105 *prop_idx = XINT (prop);
12106 success_p = 1;
12107 }
12108 else
12109 success_p = 0;
12110
12111 return success_p;
12112 }
12113
12114 \f
12115 /* Get information about the tool-bar item at position X/Y on frame F.
12116 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12117 the current matrix of the tool-bar window of F, or NULL if not
12118 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12119 item in F->tool_bar_items. Value is
12120
12121 -1 if X/Y is not on a tool-bar item
12122 0 if X/Y is on the same item that was highlighted before.
12123 1 otherwise. */
12124
12125 static int
12126 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12127 int *hpos, int *vpos, int *prop_idx)
12128 {
12129 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12130 struct window *w = XWINDOW (f->tool_bar_window);
12131 int area;
12132
12133 /* Find the glyph under X/Y. */
12134 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12135 if (*glyph == NULL)
12136 return -1;
12137
12138 /* Get the start of this tool-bar item's properties in
12139 f->tool_bar_items. */
12140 if (!tool_bar_item_info (f, *glyph, prop_idx))
12141 return -1;
12142
12143 /* Is mouse on the highlighted item? */
12144 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12145 && *vpos >= hlinfo->mouse_face_beg_row
12146 && *vpos <= hlinfo->mouse_face_end_row
12147 && (*vpos > hlinfo->mouse_face_beg_row
12148 || *hpos >= hlinfo->mouse_face_beg_col)
12149 && (*vpos < hlinfo->mouse_face_end_row
12150 || *hpos < hlinfo->mouse_face_end_col
12151 || hlinfo->mouse_face_past_end))
12152 return 0;
12153
12154 return 1;
12155 }
12156
12157
12158 /* EXPORT:
12159 Handle mouse button event on the tool-bar of frame F, at
12160 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12161 0 for button release. MODIFIERS is event modifiers for button
12162 release. */
12163
12164 void
12165 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12166 int modifiers)
12167 {
12168 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12169 struct window *w = XWINDOW (f->tool_bar_window);
12170 int hpos, vpos, prop_idx;
12171 struct glyph *glyph;
12172 Lisp_Object enabled_p;
12173
12174 /* If not on the highlighted tool-bar item, return. */
12175 frame_to_window_pixel_xy (w, &x, &y);
12176 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12177 return;
12178
12179 /* If item is disabled, do nothing. */
12180 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12181 if (NILP (enabled_p))
12182 return;
12183
12184 if (down_p)
12185 {
12186 /* Show item in pressed state. */
12187 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12188 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12189 last_tool_bar_item = prop_idx;
12190 }
12191 else
12192 {
12193 Lisp_Object key, frame;
12194 struct input_event event;
12195 EVENT_INIT (event);
12196
12197 /* Show item in released state. */
12198 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12199 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12200
12201 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12202
12203 XSETFRAME (frame, f);
12204 event.kind = TOOL_BAR_EVENT;
12205 event.frame_or_window = frame;
12206 event.arg = frame;
12207 kbd_buffer_store_event (&event);
12208
12209 event.kind = TOOL_BAR_EVENT;
12210 event.frame_or_window = frame;
12211 event.arg = key;
12212 event.modifiers = modifiers;
12213 kbd_buffer_store_event (&event);
12214 last_tool_bar_item = -1;
12215 }
12216 }
12217
12218
12219 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12220 tool-bar window-relative coordinates X/Y. Called from
12221 note_mouse_highlight. */
12222
12223 static void
12224 note_tool_bar_highlight (struct frame *f, int x, int y)
12225 {
12226 Lisp_Object window = f->tool_bar_window;
12227 struct window *w = XWINDOW (window);
12228 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12229 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12230 int hpos, vpos;
12231 struct glyph *glyph;
12232 struct glyph_row *row;
12233 int i;
12234 Lisp_Object enabled_p;
12235 int prop_idx;
12236 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12237 int mouse_down_p, rc;
12238
12239 /* Function note_mouse_highlight is called with negative X/Y
12240 values when mouse moves outside of the frame. */
12241 if (x <= 0 || y <= 0)
12242 {
12243 clear_mouse_face (hlinfo);
12244 return;
12245 }
12246
12247 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12248 if (rc < 0)
12249 {
12250 /* Not on tool-bar item. */
12251 clear_mouse_face (hlinfo);
12252 return;
12253 }
12254 else if (rc == 0)
12255 /* On same tool-bar item as before. */
12256 goto set_help_echo;
12257
12258 clear_mouse_face (hlinfo);
12259
12260 /* Mouse is down, but on different tool-bar item? */
12261 mouse_down_p = (dpyinfo->grabbed
12262 && f == last_mouse_frame
12263 && FRAME_LIVE_P (f));
12264 if (mouse_down_p
12265 && last_tool_bar_item != prop_idx)
12266 return;
12267
12268 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12269 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12270
12271 /* If tool-bar item is not enabled, don't highlight it. */
12272 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12273 if (!NILP (enabled_p))
12274 {
12275 /* Compute the x-position of the glyph. In front and past the
12276 image is a space. We include this in the highlighted area. */
12277 row = MATRIX_ROW (w->current_matrix, vpos);
12278 for (i = x = 0; i < hpos; ++i)
12279 x += row->glyphs[TEXT_AREA][i].pixel_width;
12280
12281 /* Record this as the current active region. */
12282 hlinfo->mouse_face_beg_col = hpos;
12283 hlinfo->mouse_face_beg_row = vpos;
12284 hlinfo->mouse_face_beg_x = x;
12285 hlinfo->mouse_face_beg_y = row->y;
12286 hlinfo->mouse_face_past_end = 0;
12287
12288 hlinfo->mouse_face_end_col = hpos + 1;
12289 hlinfo->mouse_face_end_row = vpos;
12290 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12291 hlinfo->mouse_face_end_y = row->y;
12292 hlinfo->mouse_face_window = window;
12293 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12294
12295 /* Display it as active. */
12296 show_mouse_face (hlinfo, draw);
12297 hlinfo->mouse_face_image_state = draw;
12298 }
12299
12300 set_help_echo:
12301
12302 /* Set help_echo_string to a help string to display for this tool-bar item.
12303 XTread_socket does the rest. */
12304 help_echo_object = help_echo_window = Qnil;
12305 help_echo_pos = -1;
12306 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12307 if (NILP (help_echo_string))
12308 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12309 }
12310
12311 #endif /* HAVE_WINDOW_SYSTEM */
12312
12313
12314 \f
12315 /************************************************************************
12316 Horizontal scrolling
12317 ************************************************************************/
12318
12319 static int hscroll_window_tree (Lisp_Object);
12320 static int hscroll_windows (Lisp_Object);
12321
12322 /* For all leaf windows in the window tree rooted at WINDOW, set their
12323 hscroll value so that PT is (i) visible in the window, and (ii) so
12324 that it is not within a certain margin at the window's left and
12325 right border. Value is non-zero if any window's hscroll has been
12326 changed. */
12327
12328 static int
12329 hscroll_window_tree (Lisp_Object window)
12330 {
12331 int hscrolled_p = 0;
12332 int hscroll_relative_p = FLOATP (Vhscroll_step);
12333 int hscroll_step_abs = 0;
12334 double hscroll_step_rel = 0;
12335
12336 if (hscroll_relative_p)
12337 {
12338 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12339 if (hscroll_step_rel < 0)
12340 {
12341 hscroll_relative_p = 0;
12342 hscroll_step_abs = 0;
12343 }
12344 }
12345 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12346 {
12347 hscroll_step_abs = XINT (Vhscroll_step);
12348 if (hscroll_step_abs < 0)
12349 hscroll_step_abs = 0;
12350 }
12351 else
12352 hscroll_step_abs = 0;
12353
12354 while (WINDOWP (window))
12355 {
12356 struct window *w = XWINDOW (window);
12357
12358 if (WINDOWP (w->hchild))
12359 hscrolled_p |= hscroll_window_tree (w->hchild);
12360 else if (WINDOWP (w->vchild))
12361 hscrolled_p |= hscroll_window_tree (w->vchild);
12362 else if (w->cursor.vpos >= 0)
12363 {
12364 int h_margin;
12365 int text_area_width;
12366 struct glyph_row *current_cursor_row
12367 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12368 struct glyph_row *desired_cursor_row
12369 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12370 struct glyph_row *cursor_row
12371 = (desired_cursor_row->enabled_p
12372 ? desired_cursor_row
12373 : current_cursor_row);
12374 int row_r2l_p = cursor_row->reversed_p;
12375
12376 text_area_width = window_box_width (w, TEXT_AREA);
12377
12378 /* Scroll when cursor is inside this scroll margin. */
12379 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12380
12381 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12382 /* For left-to-right rows, hscroll when cursor is either
12383 (i) inside the right hscroll margin, or (ii) if it is
12384 inside the left margin and the window is already
12385 hscrolled. */
12386 && ((!row_r2l_p
12387 && ((w->hscroll
12388 && w->cursor.x <= h_margin)
12389 || (cursor_row->enabled_p
12390 && cursor_row->truncated_on_right_p
12391 && (w->cursor.x >= text_area_width - h_margin))))
12392 /* For right-to-left rows, the logic is similar,
12393 except that rules for scrolling to left and right
12394 are reversed. E.g., if cursor.x <= h_margin, we
12395 need to hscroll "to the right" unconditionally,
12396 and that will scroll the screen to the left so as
12397 to reveal the next portion of the row. */
12398 || (row_r2l_p
12399 && ((cursor_row->enabled_p
12400 /* FIXME: It is confusing to set the
12401 truncated_on_right_p flag when R2L rows
12402 are actually truncated on the left. */
12403 && cursor_row->truncated_on_right_p
12404 && w->cursor.x <= h_margin)
12405 || (w->hscroll
12406 && (w->cursor.x >= text_area_width - h_margin))))))
12407 {
12408 struct it it;
12409 ptrdiff_t hscroll;
12410 struct buffer *saved_current_buffer;
12411 ptrdiff_t pt;
12412 int wanted_x;
12413
12414 /* Find point in a display of infinite width. */
12415 saved_current_buffer = current_buffer;
12416 current_buffer = XBUFFER (w->buffer);
12417
12418 if (w == XWINDOW (selected_window))
12419 pt = PT;
12420 else
12421 {
12422 pt = marker_position (w->pointm);
12423 pt = max (BEGV, pt);
12424 pt = min (ZV, pt);
12425 }
12426
12427 /* Move iterator to pt starting at cursor_row->start in
12428 a line with infinite width. */
12429 init_to_row_start (&it, w, cursor_row);
12430 it.last_visible_x = INFINITY;
12431 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12432 current_buffer = saved_current_buffer;
12433
12434 /* Position cursor in window. */
12435 if (!hscroll_relative_p && hscroll_step_abs == 0)
12436 hscroll = max (0, (it.current_x
12437 - (ITERATOR_AT_END_OF_LINE_P (&it)
12438 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12439 : (text_area_width / 2))))
12440 / FRAME_COLUMN_WIDTH (it.f);
12441 else if ((!row_r2l_p
12442 && w->cursor.x >= text_area_width - h_margin)
12443 || (row_r2l_p && w->cursor.x <= h_margin))
12444 {
12445 if (hscroll_relative_p)
12446 wanted_x = text_area_width * (1 - hscroll_step_rel)
12447 - h_margin;
12448 else
12449 wanted_x = text_area_width
12450 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12451 - h_margin;
12452 hscroll
12453 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12454 }
12455 else
12456 {
12457 if (hscroll_relative_p)
12458 wanted_x = text_area_width * hscroll_step_rel
12459 + h_margin;
12460 else
12461 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12462 + h_margin;
12463 hscroll
12464 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12465 }
12466 hscroll = max (hscroll, w->min_hscroll);
12467
12468 /* Don't prevent redisplay optimizations if hscroll
12469 hasn't changed, as it will unnecessarily slow down
12470 redisplay. */
12471 if (w->hscroll != hscroll)
12472 {
12473 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12474 w->hscroll = hscroll;
12475 hscrolled_p = 1;
12476 }
12477 }
12478 }
12479
12480 window = w->next;
12481 }
12482
12483 /* Value is non-zero if hscroll of any leaf window has been changed. */
12484 return hscrolled_p;
12485 }
12486
12487
12488 /* Set hscroll so that cursor is visible and not inside horizontal
12489 scroll margins for all windows in the tree rooted at WINDOW. See
12490 also hscroll_window_tree above. Value is non-zero if any window's
12491 hscroll has been changed. If it has, desired matrices on the frame
12492 of WINDOW are cleared. */
12493
12494 static int
12495 hscroll_windows (Lisp_Object window)
12496 {
12497 int hscrolled_p = hscroll_window_tree (window);
12498 if (hscrolled_p)
12499 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12500 return hscrolled_p;
12501 }
12502
12503
12504 \f
12505 /************************************************************************
12506 Redisplay
12507 ************************************************************************/
12508
12509 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12510 to a non-zero value. This is sometimes handy to have in a debugger
12511 session. */
12512
12513 #ifdef GLYPH_DEBUG
12514
12515 /* First and last unchanged row for try_window_id. */
12516
12517 static int debug_first_unchanged_at_end_vpos;
12518 static int debug_last_unchanged_at_beg_vpos;
12519
12520 /* Delta vpos and y. */
12521
12522 static int debug_dvpos, debug_dy;
12523
12524 /* Delta in characters and bytes for try_window_id. */
12525
12526 static ptrdiff_t debug_delta, debug_delta_bytes;
12527
12528 /* Values of window_end_pos and window_end_vpos at the end of
12529 try_window_id. */
12530
12531 static ptrdiff_t debug_end_vpos;
12532
12533 /* Append a string to W->desired_matrix->method. FMT is a printf
12534 format string. If trace_redisplay_p is non-zero also printf the
12535 resulting string to stderr. */
12536
12537 static void debug_method_add (struct window *, char const *, ...)
12538 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12539
12540 static void
12541 debug_method_add (struct window *w, char const *fmt, ...)
12542 {
12543 char *method = w->desired_matrix->method;
12544 int len = strlen (method);
12545 int size = sizeof w->desired_matrix->method;
12546 int remaining = size - len - 1;
12547 va_list ap;
12548
12549 if (len && remaining)
12550 {
12551 method[len] = '|';
12552 --remaining, ++len;
12553 }
12554
12555 va_start (ap, fmt);
12556 vsnprintf (method + len, remaining + 1, fmt, ap);
12557 va_end (ap);
12558
12559 if (trace_redisplay_p)
12560 fprintf (stderr, "%p (%s): %s\n",
12561 w,
12562 ((BUFFERP (w->buffer)
12563 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12564 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12565 : "no buffer"),
12566 method + len);
12567 }
12568
12569 #endif /* GLYPH_DEBUG */
12570
12571
12572 /* Value is non-zero if all changes in window W, which displays
12573 current_buffer, are in the text between START and END. START is a
12574 buffer position, END is given as a distance from Z. Used in
12575 redisplay_internal for display optimization. */
12576
12577 static inline int
12578 text_outside_line_unchanged_p (struct window *w,
12579 ptrdiff_t start, ptrdiff_t end)
12580 {
12581 int unchanged_p = 1;
12582
12583 /* If text or overlays have changed, see where. */
12584 if (w->last_modified < MODIFF
12585 || w->last_overlay_modified < OVERLAY_MODIFF)
12586 {
12587 /* Gap in the line? */
12588 if (GPT < start || Z - GPT < end)
12589 unchanged_p = 0;
12590
12591 /* Changes start in front of the line, or end after it? */
12592 if (unchanged_p
12593 && (BEG_UNCHANGED < start - 1
12594 || END_UNCHANGED < end))
12595 unchanged_p = 0;
12596
12597 /* If selective display, can't optimize if changes start at the
12598 beginning of the line. */
12599 if (unchanged_p
12600 && INTEGERP (BVAR (current_buffer, selective_display))
12601 && XINT (BVAR (current_buffer, selective_display)) > 0
12602 && (BEG_UNCHANGED < start || GPT <= start))
12603 unchanged_p = 0;
12604
12605 /* If there are overlays at the start or end of the line, these
12606 may have overlay strings with newlines in them. A change at
12607 START, for instance, may actually concern the display of such
12608 overlay strings as well, and they are displayed on different
12609 lines. So, quickly rule out this case. (For the future, it
12610 might be desirable to implement something more telling than
12611 just BEG/END_UNCHANGED.) */
12612 if (unchanged_p)
12613 {
12614 if (BEG + BEG_UNCHANGED == start
12615 && overlay_touches_p (start))
12616 unchanged_p = 0;
12617 if (END_UNCHANGED == end
12618 && overlay_touches_p (Z - end))
12619 unchanged_p = 0;
12620 }
12621
12622 /* Under bidi reordering, adding or deleting a character in the
12623 beginning of a paragraph, before the first strong directional
12624 character, can change the base direction of the paragraph (unless
12625 the buffer specifies a fixed paragraph direction), which will
12626 require to redisplay the whole paragraph. It might be worthwhile
12627 to find the paragraph limits and widen the range of redisplayed
12628 lines to that, but for now just give up this optimization. */
12629 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12630 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12631 unchanged_p = 0;
12632 }
12633
12634 return unchanged_p;
12635 }
12636
12637
12638 /* Do a frame update, taking possible shortcuts into account. This is
12639 the main external entry point for redisplay.
12640
12641 If the last redisplay displayed an echo area message and that message
12642 is no longer requested, we clear the echo area or bring back the
12643 mini-buffer if that is in use. */
12644
12645 void
12646 redisplay (void)
12647 {
12648 redisplay_internal ();
12649 }
12650
12651
12652 static Lisp_Object
12653 overlay_arrow_string_or_property (Lisp_Object var)
12654 {
12655 Lisp_Object val;
12656
12657 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12658 return val;
12659
12660 return Voverlay_arrow_string;
12661 }
12662
12663 /* Return 1 if there are any overlay-arrows in current_buffer. */
12664 static int
12665 overlay_arrow_in_current_buffer_p (void)
12666 {
12667 Lisp_Object vlist;
12668
12669 for (vlist = Voverlay_arrow_variable_list;
12670 CONSP (vlist);
12671 vlist = XCDR (vlist))
12672 {
12673 Lisp_Object var = XCAR (vlist);
12674 Lisp_Object val;
12675
12676 if (!SYMBOLP (var))
12677 continue;
12678 val = find_symbol_value (var);
12679 if (MARKERP (val)
12680 && current_buffer == XMARKER (val)->buffer)
12681 return 1;
12682 }
12683 return 0;
12684 }
12685
12686
12687 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12688 has changed. */
12689
12690 static int
12691 overlay_arrows_changed_p (void)
12692 {
12693 Lisp_Object vlist;
12694
12695 for (vlist = Voverlay_arrow_variable_list;
12696 CONSP (vlist);
12697 vlist = XCDR (vlist))
12698 {
12699 Lisp_Object var = XCAR (vlist);
12700 Lisp_Object val, pstr;
12701
12702 if (!SYMBOLP (var))
12703 continue;
12704 val = find_symbol_value (var);
12705 if (!MARKERP (val))
12706 continue;
12707 if (! EQ (COERCE_MARKER (val),
12708 Fget (var, Qlast_arrow_position))
12709 || ! (pstr = overlay_arrow_string_or_property (var),
12710 EQ (pstr, Fget (var, Qlast_arrow_string))))
12711 return 1;
12712 }
12713 return 0;
12714 }
12715
12716 /* Mark overlay arrows to be updated on next redisplay. */
12717
12718 static void
12719 update_overlay_arrows (int up_to_date)
12720 {
12721 Lisp_Object vlist;
12722
12723 for (vlist = Voverlay_arrow_variable_list;
12724 CONSP (vlist);
12725 vlist = XCDR (vlist))
12726 {
12727 Lisp_Object var = XCAR (vlist);
12728
12729 if (!SYMBOLP (var))
12730 continue;
12731
12732 if (up_to_date > 0)
12733 {
12734 Lisp_Object val = find_symbol_value (var);
12735 Fput (var, Qlast_arrow_position,
12736 COERCE_MARKER (val));
12737 Fput (var, Qlast_arrow_string,
12738 overlay_arrow_string_or_property (var));
12739 }
12740 else if (up_to_date < 0
12741 || !NILP (Fget (var, Qlast_arrow_position)))
12742 {
12743 Fput (var, Qlast_arrow_position, Qt);
12744 Fput (var, Qlast_arrow_string, Qt);
12745 }
12746 }
12747 }
12748
12749
12750 /* Return overlay arrow string to display at row.
12751 Return integer (bitmap number) for arrow bitmap in left fringe.
12752 Return nil if no overlay arrow. */
12753
12754 static Lisp_Object
12755 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12756 {
12757 Lisp_Object vlist;
12758
12759 for (vlist = Voverlay_arrow_variable_list;
12760 CONSP (vlist);
12761 vlist = XCDR (vlist))
12762 {
12763 Lisp_Object var = XCAR (vlist);
12764 Lisp_Object val;
12765
12766 if (!SYMBOLP (var))
12767 continue;
12768
12769 val = find_symbol_value (var);
12770
12771 if (MARKERP (val)
12772 && current_buffer == XMARKER (val)->buffer
12773 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12774 {
12775 if (FRAME_WINDOW_P (it->f)
12776 /* FIXME: if ROW->reversed_p is set, this should test
12777 the right fringe, not the left one. */
12778 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12779 {
12780 #ifdef HAVE_WINDOW_SYSTEM
12781 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12782 {
12783 int fringe_bitmap;
12784 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12785 return make_number (fringe_bitmap);
12786 }
12787 #endif
12788 return make_number (-1); /* Use default arrow bitmap. */
12789 }
12790 return overlay_arrow_string_or_property (var);
12791 }
12792 }
12793
12794 return Qnil;
12795 }
12796
12797 /* Return 1 if point moved out of or into a composition. Otherwise
12798 return 0. PREV_BUF and PREV_PT are the last point buffer and
12799 position. BUF and PT are the current point buffer and position. */
12800
12801 static int
12802 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12803 struct buffer *buf, ptrdiff_t pt)
12804 {
12805 ptrdiff_t start, end;
12806 Lisp_Object prop;
12807 Lisp_Object buffer;
12808
12809 XSETBUFFER (buffer, buf);
12810 /* Check a composition at the last point if point moved within the
12811 same buffer. */
12812 if (prev_buf == buf)
12813 {
12814 if (prev_pt == pt)
12815 /* Point didn't move. */
12816 return 0;
12817
12818 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12819 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12820 && COMPOSITION_VALID_P (start, end, prop)
12821 && start < prev_pt && end > prev_pt)
12822 /* The last point was within the composition. Return 1 iff
12823 point moved out of the composition. */
12824 return (pt <= start || pt >= end);
12825 }
12826
12827 /* Check a composition at the current point. */
12828 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12829 && find_composition (pt, -1, &start, &end, &prop, buffer)
12830 && COMPOSITION_VALID_P (start, end, prop)
12831 && start < pt && end > pt);
12832 }
12833
12834
12835 /* Reconsider the setting of B->clip_changed which is displayed
12836 in window W. */
12837
12838 static inline void
12839 reconsider_clip_changes (struct window *w, struct buffer *b)
12840 {
12841 if (b->clip_changed
12842 && !NILP (w->window_end_valid)
12843 && w->current_matrix->buffer == b
12844 && w->current_matrix->zv == BUF_ZV (b)
12845 && w->current_matrix->begv == BUF_BEGV (b))
12846 b->clip_changed = 0;
12847
12848 /* If display wasn't paused, and W is not a tool bar window, see if
12849 point has been moved into or out of a composition. In that case,
12850 we set b->clip_changed to 1 to force updating the screen. If
12851 b->clip_changed has already been set to 1, we can skip this
12852 check. */
12853 if (!b->clip_changed
12854 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12855 {
12856 ptrdiff_t pt;
12857
12858 if (w == XWINDOW (selected_window))
12859 pt = PT;
12860 else
12861 pt = marker_position (w->pointm);
12862
12863 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12864 || pt != w->last_point)
12865 && check_point_in_composition (w->current_matrix->buffer,
12866 w->last_point,
12867 XBUFFER (w->buffer), pt))
12868 b->clip_changed = 1;
12869 }
12870 }
12871 \f
12872
12873 /* Select FRAME to forward the values of frame-local variables into C
12874 variables so that the redisplay routines can access those values
12875 directly. */
12876
12877 static void
12878 select_frame_for_redisplay (Lisp_Object frame)
12879 {
12880 Lisp_Object tail, tem;
12881 Lisp_Object old = selected_frame;
12882 struct Lisp_Symbol *sym;
12883
12884 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12885
12886 selected_frame = frame;
12887
12888 do {
12889 for (tail = XFRAME (frame)->param_alist;
12890 CONSP (tail); tail = XCDR (tail))
12891 if (CONSP (XCAR (tail))
12892 && (tem = XCAR (XCAR (tail)),
12893 SYMBOLP (tem))
12894 && (sym = indirect_variable (XSYMBOL (tem)),
12895 sym->redirect == SYMBOL_LOCALIZED)
12896 && sym->val.blv->frame_local)
12897 /* Use find_symbol_value rather than Fsymbol_value
12898 to avoid an error if it is void. */
12899 find_symbol_value (tem);
12900 } while (!EQ (frame, old) && (frame = old, 1));
12901 }
12902
12903
12904 #define STOP_POLLING \
12905 do { if (! polling_stopped_here) stop_polling (); \
12906 polling_stopped_here = 1; } while (0)
12907
12908 #define RESUME_POLLING \
12909 do { if (polling_stopped_here) start_polling (); \
12910 polling_stopped_here = 0; } while (0)
12911
12912
12913 /* Perhaps in the future avoid recentering windows if it
12914 is not necessary; currently that causes some problems. */
12915
12916 static void
12917 redisplay_internal (void)
12918 {
12919 struct window *w = XWINDOW (selected_window);
12920 struct window *sw;
12921 struct frame *fr;
12922 int pending;
12923 int must_finish = 0;
12924 struct text_pos tlbufpos, tlendpos;
12925 int number_of_visible_frames;
12926 ptrdiff_t count, count1;
12927 struct frame *sf;
12928 int polling_stopped_here = 0;
12929 Lisp_Object old_frame = selected_frame;
12930
12931 /* Non-zero means redisplay has to consider all windows on all
12932 frames. Zero means, only selected_window is considered. */
12933 int consider_all_windows_p;
12934
12935 /* Non-zero means redisplay has to redisplay the miniwindow */
12936 int update_miniwindow_p = 0;
12937
12938 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12939
12940 /* No redisplay if running in batch mode or frame is not yet fully
12941 initialized, or redisplay is explicitly turned off by setting
12942 Vinhibit_redisplay. */
12943 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12944 || !NILP (Vinhibit_redisplay))
12945 return;
12946
12947 /* Don't examine these until after testing Vinhibit_redisplay.
12948 When Emacs is shutting down, perhaps because its connection to
12949 X has dropped, we should not look at them at all. */
12950 fr = XFRAME (w->frame);
12951 sf = SELECTED_FRAME ();
12952
12953 if (!fr->glyphs_initialized_p)
12954 return;
12955
12956 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12957 if (popup_activated ())
12958 return;
12959 #endif
12960
12961 /* I don't think this happens but let's be paranoid. */
12962 if (redisplaying_p)
12963 return;
12964
12965 /* Record a function that clears redisplaying_p
12966 when we leave this function. */
12967 count = SPECPDL_INDEX ();
12968 record_unwind_protect (unwind_redisplay, selected_frame);
12969 redisplaying_p = 1;
12970 specbind (Qinhibit_free_realized_faces, Qnil);
12971
12972 {
12973 Lisp_Object tail, frame;
12974
12975 FOR_EACH_FRAME (tail, frame)
12976 {
12977 struct frame *f = XFRAME (frame);
12978 f->already_hscrolled_p = 0;
12979 }
12980 }
12981
12982 retry:
12983 /* Remember the currently selected window. */
12984 sw = w;
12985
12986 if (!EQ (old_frame, selected_frame)
12987 && FRAME_LIVE_P (XFRAME (old_frame)))
12988 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12989 selected_frame and selected_window to be temporarily out-of-sync so
12990 when we come back here via `goto retry', we need to resync because we
12991 may need to run Elisp code (via prepare_menu_bars). */
12992 select_frame_for_redisplay (old_frame);
12993
12994 pending = 0;
12995 reconsider_clip_changes (w, current_buffer);
12996 last_escape_glyph_frame = NULL;
12997 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12998 last_glyphless_glyph_frame = NULL;
12999 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13000
13001 /* If new fonts have been loaded that make a glyph matrix adjustment
13002 necessary, do it. */
13003 if (fonts_changed_p)
13004 {
13005 adjust_glyphs (NULL);
13006 ++windows_or_buffers_changed;
13007 fonts_changed_p = 0;
13008 }
13009
13010 /* If face_change_count is non-zero, init_iterator will free all
13011 realized faces, which includes the faces referenced from current
13012 matrices. So, we can't reuse current matrices in this case. */
13013 if (face_change_count)
13014 ++windows_or_buffers_changed;
13015
13016 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13017 && FRAME_TTY (sf)->previous_frame != sf)
13018 {
13019 /* Since frames on a single ASCII terminal share the same
13020 display area, displaying a different frame means redisplay
13021 the whole thing. */
13022 windows_or_buffers_changed++;
13023 SET_FRAME_GARBAGED (sf);
13024 #ifndef DOS_NT
13025 set_tty_color_mode (FRAME_TTY (sf), sf);
13026 #endif
13027 FRAME_TTY (sf)->previous_frame = sf;
13028 }
13029
13030 /* Set the visible flags for all frames. Do this before checking
13031 for resized or garbaged frames; they want to know if their frames
13032 are visible. See the comment in frame.h for
13033 FRAME_SAMPLE_VISIBILITY. */
13034 {
13035 Lisp_Object tail, frame;
13036
13037 number_of_visible_frames = 0;
13038
13039 FOR_EACH_FRAME (tail, frame)
13040 {
13041 struct frame *f = XFRAME (frame);
13042
13043 FRAME_SAMPLE_VISIBILITY (f);
13044 if (FRAME_VISIBLE_P (f))
13045 ++number_of_visible_frames;
13046 clear_desired_matrices (f);
13047 }
13048 }
13049
13050 /* Notice any pending interrupt request to change frame size. */
13051 do_pending_window_change (1);
13052
13053 /* do_pending_window_change could change the selected_window due to
13054 frame resizing which makes the selected window too small. */
13055 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13056 {
13057 sw = w;
13058 reconsider_clip_changes (w, current_buffer);
13059 }
13060
13061 /* Clear frames marked as garbaged. */
13062 if (frame_garbaged)
13063 clear_garbaged_frames ();
13064
13065 /* Build menubar and tool-bar items. */
13066 if (NILP (Vmemory_full))
13067 prepare_menu_bars ();
13068
13069 if (windows_or_buffers_changed)
13070 update_mode_lines++;
13071
13072 /* Detect case that we need to write or remove a star in the mode line. */
13073 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13074 {
13075 w->update_mode_line = 1;
13076 if (buffer_shared > 1)
13077 update_mode_lines++;
13078 }
13079
13080 /* Avoid invocation of point motion hooks by `current_column' below. */
13081 count1 = SPECPDL_INDEX ();
13082 specbind (Qinhibit_point_motion_hooks, Qt);
13083
13084 /* If %c is in the mode line, update it if needed. */
13085 if (!NILP (w->column_number_displayed)
13086 /* This alternative quickly identifies a common case
13087 where no change is needed. */
13088 && !(PT == w->last_point
13089 && w->last_modified >= MODIFF
13090 && w->last_overlay_modified >= OVERLAY_MODIFF)
13091 && (XFASTINT (w->column_number_displayed) != current_column ()))
13092 w->update_mode_line = 1;
13093
13094 unbind_to (count1, Qnil);
13095
13096 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13097
13098 /* The variable buffer_shared is set in redisplay_window and
13099 indicates that we redisplay a buffer in different windows. See
13100 there. */
13101 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13102 || cursor_type_changed);
13103
13104 /* If specs for an arrow have changed, do thorough redisplay
13105 to ensure we remove any arrow that should no longer exist. */
13106 if (overlay_arrows_changed_p ())
13107 consider_all_windows_p = windows_or_buffers_changed = 1;
13108
13109 /* Normally the message* functions will have already displayed and
13110 updated the echo area, but the frame may have been trashed, or
13111 the update may have been preempted, so display the echo area
13112 again here. Checking message_cleared_p captures the case that
13113 the echo area should be cleared. */
13114 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13115 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13116 || (message_cleared_p
13117 && minibuf_level == 0
13118 /* If the mini-window is currently selected, this means the
13119 echo-area doesn't show through. */
13120 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13121 {
13122 int window_height_changed_p = echo_area_display (0);
13123
13124 if (message_cleared_p)
13125 update_miniwindow_p = 1;
13126
13127 must_finish = 1;
13128
13129 /* If we don't display the current message, don't clear the
13130 message_cleared_p flag, because, if we did, we wouldn't clear
13131 the echo area in the next redisplay which doesn't preserve
13132 the echo area. */
13133 if (!display_last_displayed_message_p)
13134 message_cleared_p = 0;
13135
13136 if (fonts_changed_p)
13137 goto retry;
13138 else if (window_height_changed_p)
13139 {
13140 consider_all_windows_p = 1;
13141 ++update_mode_lines;
13142 ++windows_or_buffers_changed;
13143
13144 /* If window configuration was changed, frames may have been
13145 marked garbaged. Clear them or we will experience
13146 surprises wrt scrolling. */
13147 if (frame_garbaged)
13148 clear_garbaged_frames ();
13149 }
13150 }
13151 else if (EQ (selected_window, minibuf_window)
13152 && (current_buffer->clip_changed
13153 || w->last_modified < MODIFF
13154 || w->last_overlay_modified < OVERLAY_MODIFF)
13155 && resize_mini_window (w, 0))
13156 {
13157 /* Resized active mini-window to fit the size of what it is
13158 showing if its contents might have changed. */
13159 must_finish = 1;
13160 /* FIXME: this causes all frames to be updated, which seems unnecessary
13161 since only the current frame needs to be considered. This function needs
13162 to be rewritten with two variables, consider_all_windows and
13163 consider_all_frames. */
13164 consider_all_windows_p = 1;
13165 ++windows_or_buffers_changed;
13166 ++update_mode_lines;
13167
13168 /* If window configuration was changed, frames may have been
13169 marked garbaged. Clear them or we will experience
13170 surprises wrt scrolling. */
13171 if (frame_garbaged)
13172 clear_garbaged_frames ();
13173 }
13174
13175
13176 /* If showing the region, and mark has changed, we must redisplay
13177 the whole window. The assignment to this_line_start_pos prevents
13178 the optimization directly below this if-statement. */
13179 if (((!NILP (Vtransient_mark_mode)
13180 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13181 != !NILP (w->region_showing))
13182 || (!NILP (w->region_showing)
13183 && !EQ (w->region_showing,
13184 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13185 CHARPOS (this_line_start_pos) = 0;
13186
13187 /* Optimize the case that only the line containing the cursor in the
13188 selected window has changed. Variables starting with this_ are
13189 set in display_line and record information about the line
13190 containing the cursor. */
13191 tlbufpos = this_line_start_pos;
13192 tlendpos = this_line_end_pos;
13193 if (!consider_all_windows_p
13194 && CHARPOS (tlbufpos) > 0
13195 && !w->update_mode_line
13196 && !current_buffer->clip_changed
13197 && !current_buffer->prevent_redisplay_optimizations_p
13198 && FRAME_VISIBLE_P (XFRAME (w->frame))
13199 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13200 /* Make sure recorded data applies to current buffer, etc. */
13201 && this_line_buffer == current_buffer
13202 && current_buffer == XBUFFER (w->buffer)
13203 && !w->force_start
13204 && !w->optional_new_start
13205 /* Point must be on the line that we have info recorded about. */
13206 && PT >= CHARPOS (tlbufpos)
13207 && PT <= Z - CHARPOS (tlendpos)
13208 /* All text outside that line, including its final newline,
13209 must be unchanged. */
13210 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13211 CHARPOS (tlendpos)))
13212 {
13213 if (CHARPOS (tlbufpos) > BEGV
13214 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13215 && (CHARPOS (tlbufpos) == ZV
13216 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13217 /* Former continuation line has disappeared by becoming empty. */
13218 goto cancel;
13219 else if (w->last_modified < MODIFF
13220 || w->last_overlay_modified < OVERLAY_MODIFF
13221 || MINI_WINDOW_P (w))
13222 {
13223 /* We have to handle the case of continuation around a
13224 wide-column character (see the comment in indent.c around
13225 line 1340).
13226
13227 For instance, in the following case:
13228
13229 -------- Insert --------
13230 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13231 J_I_ ==> J_I_ `^^' are cursors.
13232 ^^ ^^
13233 -------- --------
13234
13235 As we have to redraw the line above, we cannot use this
13236 optimization. */
13237
13238 struct it it;
13239 int line_height_before = this_line_pixel_height;
13240
13241 /* Note that start_display will handle the case that the
13242 line starting at tlbufpos is a continuation line. */
13243 start_display (&it, w, tlbufpos);
13244
13245 /* Implementation note: It this still necessary? */
13246 if (it.current_x != this_line_start_x)
13247 goto cancel;
13248
13249 TRACE ((stderr, "trying display optimization 1\n"));
13250 w->cursor.vpos = -1;
13251 overlay_arrow_seen = 0;
13252 it.vpos = this_line_vpos;
13253 it.current_y = this_line_y;
13254 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13255 display_line (&it);
13256
13257 /* If line contains point, is not continued,
13258 and ends at same distance from eob as before, we win. */
13259 if (w->cursor.vpos >= 0
13260 /* Line is not continued, otherwise this_line_start_pos
13261 would have been set to 0 in display_line. */
13262 && CHARPOS (this_line_start_pos)
13263 /* Line ends as before. */
13264 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13265 /* Line has same height as before. Otherwise other lines
13266 would have to be shifted up or down. */
13267 && this_line_pixel_height == line_height_before)
13268 {
13269 /* If this is not the window's last line, we must adjust
13270 the charstarts of the lines below. */
13271 if (it.current_y < it.last_visible_y)
13272 {
13273 struct glyph_row *row
13274 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13275 ptrdiff_t delta, delta_bytes;
13276
13277 /* We used to distinguish between two cases here,
13278 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13279 when the line ends in a newline or the end of the
13280 buffer's accessible portion. But both cases did
13281 the same, so they were collapsed. */
13282 delta = (Z
13283 - CHARPOS (tlendpos)
13284 - MATRIX_ROW_START_CHARPOS (row));
13285 delta_bytes = (Z_BYTE
13286 - BYTEPOS (tlendpos)
13287 - MATRIX_ROW_START_BYTEPOS (row));
13288
13289 increment_matrix_positions (w->current_matrix,
13290 this_line_vpos + 1,
13291 w->current_matrix->nrows,
13292 delta, delta_bytes);
13293 }
13294
13295 /* If this row displays text now but previously didn't,
13296 or vice versa, w->window_end_vpos may have to be
13297 adjusted. */
13298 if ((it.glyph_row - 1)->displays_text_p)
13299 {
13300 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13301 wset_window_end_vpos (w, make_number (this_line_vpos));
13302 }
13303 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13304 && this_line_vpos > 0)
13305 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13306 wset_window_end_valid (w, Qnil);
13307
13308 /* Update hint: No need to try to scroll in update_window. */
13309 w->desired_matrix->no_scrolling_p = 1;
13310
13311 #ifdef GLYPH_DEBUG
13312 *w->desired_matrix->method = 0;
13313 debug_method_add (w, "optimization 1");
13314 #endif
13315 #ifdef HAVE_WINDOW_SYSTEM
13316 update_window_fringes (w, 0);
13317 #endif
13318 goto update;
13319 }
13320 else
13321 goto cancel;
13322 }
13323 else if (/* Cursor position hasn't changed. */
13324 PT == w->last_point
13325 /* Make sure the cursor was last displayed
13326 in this window. Otherwise we have to reposition it. */
13327 && 0 <= w->cursor.vpos
13328 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13329 {
13330 if (!must_finish)
13331 {
13332 do_pending_window_change (1);
13333 /* If selected_window changed, redisplay again. */
13334 if (WINDOWP (selected_window)
13335 && (w = XWINDOW (selected_window)) != sw)
13336 goto retry;
13337
13338 /* We used to always goto end_of_redisplay here, but this
13339 isn't enough if we have a blinking cursor. */
13340 if (w->cursor_off_p == w->last_cursor_off_p)
13341 goto end_of_redisplay;
13342 }
13343 goto update;
13344 }
13345 /* If highlighting the region, or if the cursor is in the echo area,
13346 then we can't just move the cursor. */
13347 else if (! (!NILP (Vtransient_mark_mode)
13348 && !NILP (BVAR (current_buffer, mark_active)))
13349 && (EQ (selected_window,
13350 BVAR (current_buffer, last_selected_window))
13351 || highlight_nonselected_windows)
13352 && NILP (w->region_showing)
13353 && NILP (Vshow_trailing_whitespace)
13354 && !cursor_in_echo_area)
13355 {
13356 struct it it;
13357 struct glyph_row *row;
13358
13359 /* Skip from tlbufpos to PT and see where it is. Note that
13360 PT may be in invisible text. If so, we will end at the
13361 next visible position. */
13362 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13363 NULL, DEFAULT_FACE_ID);
13364 it.current_x = this_line_start_x;
13365 it.current_y = this_line_y;
13366 it.vpos = this_line_vpos;
13367
13368 /* The call to move_it_to stops in front of PT, but
13369 moves over before-strings. */
13370 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13371
13372 if (it.vpos == this_line_vpos
13373 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13374 row->enabled_p))
13375 {
13376 eassert (this_line_vpos == it.vpos);
13377 eassert (this_line_y == it.current_y);
13378 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13379 #ifdef GLYPH_DEBUG
13380 *w->desired_matrix->method = 0;
13381 debug_method_add (w, "optimization 3");
13382 #endif
13383 goto update;
13384 }
13385 else
13386 goto cancel;
13387 }
13388
13389 cancel:
13390 /* Text changed drastically or point moved off of line. */
13391 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13392 }
13393
13394 CHARPOS (this_line_start_pos) = 0;
13395 consider_all_windows_p |= buffer_shared > 1;
13396 ++clear_face_cache_count;
13397 #ifdef HAVE_WINDOW_SYSTEM
13398 ++clear_image_cache_count;
13399 #endif
13400
13401 /* Build desired matrices, and update the display. If
13402 consider_all_windows_p is non-zero, do it for all windows on all
13403 frames. Otherwise do it for selected_window, only. */
13404
13405 if (consider_all_windows_p)
13406 {
13407 Lisp_Object tail, frame;
13408
13409 FOR_EACH_FRAME (tail, frame)
13410 XFRAME (frame)->updated_p = 0;
13411
13412 /* Recompute # windows showing selected buffer. This will be
13413 incremented each time such a window is displayed. */
13414 buffer_shared = 0;
13415
13416 FOR_EACH_FRAME (tail, frame)
13417 {
13418 struct frame *f = XFRAME (frame);
13419
13420 /* We don't have to do anything for unselected terminal
13421 frames. */
13422 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13423 && !EQ (FRAME_TTY (f)->top_frame, frame))
13424 continue;
13425
13426 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13427 {
13428 if (! EQ (frame, selected_frame))
13429 /* Select the frame, for the sake of frame-local
13430 variables. */
13431 select_frame_for_redisplay (frame);
13432
13433 /* Mark all the scroll bars to be removed; we'll redeem
13434 the ones we want when we redisplay their windows. */
13435 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13436 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13437
13438 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13439 redisplay_windows (FRAME_ROOT_WINDOW (f));
13440
13441 /* The X error handler may have deleted that frame. */
13442 if (!FRAME_LIVE_P (f))
13443 continue;
13444
13445 /* Any scroll bars which redisplay_windows should have
13446 nuked should now go away. */
13447 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13448 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13449
13450 /* If fonts changed, display again. */
13451 /* ??? rms: I suspect it is a mistake to jump all the way
13452 back to retry here. It should just retry this frame. */
13453 if (fonts_changed_p)
13454 goto retry;
13455
13456 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13457 {
13458 /* See if we have to hscroll. */
13459 if (!f->already_hscrolled_p)
13460 {
13461 f->already_hscrolled_p = 1;
13462 if (hscroll_windows (f->root_window))
13463 goto retry;
13464 }
13465
13466 /* Prevent various kinds of signals during display
13467 update. stdio is not robust about handling
13468 signals, which can cause an apparent I/O
13469 error. */
13470 if (interrupt_input)
13471 unrequest_sigio ();
13472 STOP_POLLING;
13473
13474 /* Update the display. */
13475 set_window_update_flags (XWINDOW (f->root_window), 1);
13476 pending |= update_frame (f, 0, 0);
13477 f->updated_p = 1;
13478 }
13479 }
13480 }
13481
13482 if (!EQ (old_frame, selected_frame)
13483 && FRAME_LIVE_P (XFRAME (old_frame)))
13484 /* We played a bit fast-and-loose above and allowed selected_frame
13485 and selected_window to be temporarily out-of-sync but let's make
13486 sure this stays contained. */
13487 select_frame_for_redisplay (old_frame);
13488 eassert (EQ (XFRAME (selected_frame)->selected_window,
13489 selected_window));
13490
13491 if (!pending)
13492 {
13493 /* Do the mark_window_display_accurate after all windows have
13494 been redisplayed because this call resets flags in buffers
13495 which are needed for proper redisplay. */
13496 FOR_EACH_FRAME (tail, frame)
13497 {
13498 struct frame *f = XFRAME (frame);
13499 if (f->updated_p)
13500 {
13501 mark_window_display_accurate (f->root_window, 1);
13502 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13503 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13504 }
13505 }
13506 }
13507 }
13508 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13509 {
13510 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13511 struct frame *mini_frame;
13512
13513 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13514 /* Use list_of_error, not Qerror, so that
13515 we catch only errors and don't run the debugger. */
13516 internal_condition_case_1 (redisplay_window_1, selected_window,
13517 list_of_error,
13518 redisplay_window_error);
13519 if (update_miniwindow_p)
13520 internal_condition_case_1 (redisplay_window_1, mini_window,
13521 list_of_error,
13522 redisplay_window_error);
13523
13524 /* Compare desired and current matrices, perform output. */
13525
13526 update:
13527 /* If fonts changed, display again. */
13528 if (fonts_changed_p)
13529 goto retry;
13530
13531 /* Prevent various kinds of signals during display update.
13532 stdio is not robust about handling signals,
13533 which can cause an apparent I/O error. */
13534 if (interrupt_input)
13535 unrequest_sigio ();
13536 STOP_POLLING;
13537
13538 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13539 {
13540 if (hscroll_windows (selected_window))
13541 goto retry;
13542
13543 XWINDOW (selected_window)->must_be_updated_p = 1;
13544 pending = update_frame (sf, 0, 0);
13545 }
13546
13547 /* We may have called echo_area_display at the top of this
13548 function. If the echo area is on another frame, that may
13549 have put text on a frame other than the selected one, so the
13550 above call to update_frame would not have caught it. Catch
13551 it here. */
13552 mini_window = FRAME_MINIBUF_WINDOW (sf);
13553 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13554
13555 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13556 {
13557 XWINDOW (mini_window)->must_be_updated_p = 1;
13558 pending |= update_frame (mini_frame, 0, 0);
13559 if (!pending && hscroll_windows (mini_window))
13560 goto retry;
13561 }
13562 }
13563
13564 /* If display was paused because of pending input, make sure we do a
13565 thorough update the next time. */
13566 if (pending)
13567 {
13568 /* Prevent the optimization at the beginning of
13569 redisplay_internal that tries a single-line update of the
13570 line containing the cursor in the selected window. */
13571 CHARPOS (this_line_start_pos) = 0;
13572
13573 /* Let the overlay arrow be updated the next time. */
13574 update_overlay_arrows (0);
13575
13576 /* If we pause after scrolling, some rows in the current
13577 matrices of some windows are not valid. */
13578 if (!WINDOW_FULL_WIDTH_P (w)
13579 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13580 update_mode_lines = 1;
13581 }
13582 else
13583 {
13584 if (!consider_all_windows_p)
13585 {
13586 /* This has already been done above if
13587 consider_all_windows_p is set. */
13588 mark_window_display_accurate_1 (w, 1);
13589
13590 /* Say overlay arrows are up to date. */
13591 update_overlay_arrows (1);
13592
13593 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13594 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13595 }
13596
13597 update_mode_lines = 0;
13598 windows_or_buffers_changed = 0;
13599 cursor_type_changed = 0;
13600 }
13601
13602 /* Start SIGIO interrupts coming again. Having them off during the
13603 code above makes it less likely one will discard output, but not
13604 impossible, since there might be stuff in the system buffer here.
13605 But it is much hairier to try to do anything about that. */
13606 if (interrupt_input)
13607 request_sigio ();
13608 RESUME_POLLING;
13609
13610 /* If a frame has become visible which was not before, redisplay
13611 again, so that we display it. Expose events for such a frame
13612 (which it gets when becoming visible) don't call the parts of
13613 redisplay constructing glyphs, so simply exposing a frame won't
13614 display anything in this case. So, we have to display these
13615 frames here explicitly. */
13616 if (!pending)
13617 {
13618 Lisp_Object tail, frame;
13619 int new_count = 0;
13620
13621 FOR_EACH_FRAME (tail, frame)
13622 {
13623 int this_is_visible = 0;
13624
13625 if (XFRAME (frame)->visible)
13626 this_is_visible = 1;
13627 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13628 if (XFRAME (frame)->visible)
13629 this_is_visible = 1;
13630
13631 if (this_is_visible)
13632 new_count++;
13633 }
13634
13635 if (new_count != number_of_visible_frames)
13636 windows_or_buffers_changed++;
13637 }
13638
13639 /* Change frame size now if a change is pending. */
13640 do_pending_window_change (1);
13641
13642 /* If we just did a pending size change, or have additional
13643 visible frames, or selected_window changed, redisplay again. */
13644 if ((windows_or_buffers_changed && !pending)
13645 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13646 goto retry;
13647
13648 /* Clear the face and image caches.
13649
13650 We used to do this only if consider_all_windows_p. But the cache
13651 needs to be cleared if a timer creates images in the current
13652 buffer (e.g. the test case in Bug#6230). */
13653
13654 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13655 {
13656 clear_face_cache (0);
13657 clear_face_cache_count = 0;
13658 }
13659
13660 #ifdef HAVE_WINDOW_SYSTEM
13661 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13662 {
13663 clear_image_caches (Qnil);
13664 clear_image_cache_count = 0;
13665 }
13666 #endif /* HAVE_WINDOW_SYSTEM */
13667
13668 end_of_redisplay:
13669 unbind_to (count, Qnil);
13670 RESUME_POLLING;
13671 }
13672
13673
13674 /* Redisplay, but leave alone any recent echo area message unless
13675 another message has been requested in its place.
13676
13677 This is useful in situations where you need to redisplay but no
13678 user action has occurred, making it inappropriate for the message
13679 area to be cleared. See tracking_off and
13680 wait_reading_process_output for examples of these situations.
13681
13682 FROM_WHERE is an integer saying from where this function was
13683 called. This is useful for debugging. */
13684
13685 void
13686 redisplay_preserve_echo_area (int from_where)
13687 {
13688 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13689
13690 if (!NILP (echo_area_buffer[1]))
13691 {
13692 /* We have a previously displayed message, but no current
13693 message. Redisplay the previous message. */
13694 display_last_displayed_message_p = 1;
13695 redisplay_internal ();
13696 display_last_displayed_message_p = 0;
13697 }
13698 else
13699 redisplay_internal ();
13700
13701 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13702 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13703 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13704 }
13705
13706
13707 /* Function registered with record_unwind_protect in redisplay_internal.
13708 Clear redisplaying_p. Also, select the previously
13709 selected frame, unless it has been deleted (by an X connection
13710 failure during redisplay, for example). */
13711
13712 static Lisp_Object
13713 unwind_redisplay (Lisp_Object old_frame)
13714 {
13715 redisplaying_p = 0;
13716 if (! EQ (old_frame, selected_frame)
13717 && FRAME_LIVE_P (XFRAME (old_frame)))
13718 select_frame_for_redisplay (old_frame);
13719 return Qnil;
13720 }
13721
13722
13723 /* Mark the display of window W as accurate or inaccurate. If
13724 ACCURATE_P is non-zero mark display of W as accurate. If
13725 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13726 redisplay_internal is called. */
13727
13728 static void
13729 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13730 {
13731 if (BUFFERP (w->buffer))
13732 {
13733 struct buffer *b = XBUFFER (w->buffer);
13734
13735 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13736 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13737 w->last_had_star
13738 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13739
13740 if (accurate_p)
13741 {
13742 b->clip_changed = 0;
13743 b->prevent_redisplay_optimizations_p = 0;
13744
13745 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13746 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13747 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13748 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13749
13750 w->current_matrix->buffer = b;
13751 w->current_matrix->begv = BUF_BEGV (b);
13752 w->current_matrix->zv = BUF_ZV (b);
13753
13754 w->last_cursor = w->cursor;
13755 w->last_cursor_off_p = w->cursor_off_p;
13756
13757 if (w == XWINDOW (selected_window))
13758 w->last_point = BUF_PT (b);
13759 else
13760 w->last_point = XMARKER (w->pointm)->charpos;
13761 }
13762 }
13763
13764 if (accurate_p)
13765 {
13766 wset_window_end_valid (w, w->buffer);
13767 w->update_mode_line = 0;
13768 }
13769 }
13770
13771
13772 /* Mark the display of windows in the window tree rooted at WINDOW as
13773 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13774 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13775 be redisplayed the next time redisplay_internal is called. */
13776
13777 void
13778 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13779 {
13780 struct window *w;
13781
13782 for (; !NILP (window); window = w->next)
13783 {
13784 w = XWINDOW (window);
13785 mark_window_display_accurate_1 (w, accurate_p);
13786
13787 if (!NILP (w->vchild))
13788 mark_window_display_accurate (w->vchild, accurate_p);
13789 if (!NILP (w->hchild))
13790 mark_window_display_accurate (w->hchild, accurate_p);
13791 }
13792
13793 if (accurate_p)
13794 {
13795 update_overlay_arrows (1);
13796 }
13797 else
13798 {
13799 /* Force a thorough redisplay the next time by setting
13800 last_arrow_position and last_arrow_string to t, which is
13801 unequal to any useful value of Voverlay_arrow_... */
13802 update_overlay_arrows (-1);
13803 }
13804 }
13805
13806
13807 /* Return value in display table DP (Lisp_Char_Table *) for character
13808 C. Since a display table doesn't have any parent, we don't have to
13809 follow parent. Do not call this function directly but use the
13810 macro DISP_CHAR_VECTOR. */
13811
13812 Lisp_Object
13813 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13814 {
13815 Lisp_Object val;
13816
13817 if (ASCII_CHAR_P (c))
13818 {
13819 val = dp->ascii;
13820 if (SUB_CHAR_TABLE_P (val))
13821 val = XSUB_CHAR_TABLE (val)->contents[c];
13822 }
13823 else
13824 {
13825 Lisp_Object table;
13826
13827 XSETCHAR_TABLE (table, dp);
13828 val = char_table_ref (table, c);
13829 }
13830 if (NILP (val))
13831 val = dp->defalt;
13832 return val;
13833 }
13834
13835
13836 \f
13837 /***********************************************************************
13838 Window Redisplay
13839 ***********************************************************************/
13840
13841 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13842
13843 static void
13844 redisplay_windows (Lisp_Object window)
13845 {
13846 while (!NILP (window))
13847 {
13848 struct window *w = XWINDOW (window);
13849
13850 if (!NILP (w->hchild))
13851 redisplay_windows (w->hchild);
13852 else if (!NILP (w->vchild))
13853 redisplay_windows (w->vchild);
13854 else if (!NILP (w->buffer))
13855 {
13856 displayed_buffer = XBUFFER (w->buffer);
13857 /* Use list_of_error, not Qerror, so that
13858 we catch only errors and don't run the debugger. */
13859 internal_condition_case_1 (redisplay_window_0, window,
13860 list_of_error,
13861 redisplay_window_error);
13862 }
13863
13864 window = w->next;
13865 }
13866 }
13867
13868 static Lisp_Object
13869 redisplay_window_error (Lisp_Object ignore)
13870 {
13871 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13872 return Qnil;
13873 }
13874
13875 static Lisp_Object
13876 redisplay_window_0 (Lisp_Object window)
13877 {
13878 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13879 redisplay_window (window, 0);
13880 return Qnil;
13881 }
13882
13883 static Lisp_Object
13884 redisplay_window_1 (Lisp_Object window)
13885 {
13886 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13887 redisplay_window (window, 1);
13888 return Qnil;
13889 }
13890 \f
13891
13892 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13893 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13894 which positions recorded in ROW differ from current buffer
13895 positions.
13896
13897 Return 0 if cursor is not on this row, 1 otherwise. */
13898
13899 static int
13900 set_cursor_from_row (struct window *w, struct glyph_row *row,
13901 struct glyph_matrix *matrix,
13902 ptrdiff_t delta, ptrdiff_t delta_bytes,
13903 int dy, int dvpos)
13904 {
13905 struct glyph *glyph = row->glyphs[TEXT_AREA];
13906 struct glyph *end = glyph + row->used[TEXT_AREA];
13907 struct glyph *cursor = NULL;
13908 /* The last known character position in row. */
13909 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13910 int x = row->x;
13911 ptrdiff_t pt_old = PT - delta;
13912 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13913 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13914 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13915 /* A glyph beyond the edge of TEXT_AREA which we should never
13916 touch. */
13917 struct glyph *glyphs_end = end;
13918 /* Non-zero means we've found a match for cursor position, but that
13919 glyph has the avoid_cursor_p flag set. */
13920 int match_with_avoid_cursor = 0;
13921 /* Non-zero means we've seen at least one glyph that came from a
13922 display string. */
13923 int string_seen = 0;
13924 /* Largest and smallest buffer positions seen so far during scan of
13925 glyph row. */
13926 ptrdiff_t bpos_max = pos_before;
13927 ptrdiff_t bpos_min = pos_after;
13928 /* Last buffer position covered by an overlay string with an integer
13929 `cursor' property. */
13930 ptrdiff_t bpos_covered = 0;
13931 /* Non-zero means the display string on which to display the cursor
13932 comes from a text property, not from an overlay. */
13933 int string_from_text_prop = 0;
13934
13935 /* Don't even try doing anything if called for a mode-line or
13936 header-line row, since the rest of the code isn't prepared to
13937 deal with such calamities. */
13938 eassert (!row->mode_line_p);
13939 if (row->mode_line_p)
13940 return 0;
13941
13942 /* Skip over glyphs not having an object at the start and the end of
13943 the row. These are special glyphs like truncation marks on
13944 terminal frames. */
13945 if (row->displays_text_p)
13946 {
13947 if (!row->reversed_p)
13948 {
13949 while (glyph < end
13950 && INTEGERP (glyph->object)
13951 && glyph->charpos < 0)
13952 {
13953 x += glyph->pixel_width;
13954 ++glyph;
13955 }
13956 while (end > glyph
13957 && INTEGERP ((end - 1)->object)
13958 /* CHARPOS is zero for blanks and stretch glyphs
13959 inserted by extend_face_to_end_of_line. */
13960 && (end - 1)->charpos <= 0)
13961 --end;
13962 glyph_before = glyph - 1;
13963 glyph_after = end;
13964 }
13965 else
13966 {
13967 struct glyph *g;
13968
13969 /* If the glyph row is reversed, we need to process it from back
13970 to front, so swap the edge pointers. */
13971 glyphs_end = end = glyph - 1;
13972 glyph += row->used[TEXT_AREA] - 1;
13973
13974 while (glyph > end + 1
13975 && INTEGERP (glyph->object)
13976 && glyph->charpos < 0)
13977 {
13978 --glyph;
13979 x -= glyph->pixel_width;
13980 }
13981 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13982 --glyph;
13983 /* By default, in reversed rows we put the cursor on the
13984 rightmost (first in the reading order) glyph. */
13985 for (g = end + 1; g < glyph; g++)
13986 x += g->pixel_width;
13987 while (end < glyph
13988 && INTEGERP ((end + 1)->object)
13989 && (end + 1)->charpos <= 0)
13990 ++end;
13991 glyph_before = glyph + 1;
13992 glyph_after = end;
13993 }
13994 }
13995 else if (row->reversed_p)
13996 {
13997 /* In R2L rows that don't display text, put the cursor on the
13998 rightmost glyph. Case in point: an empty last line that is
13999 part of an R2L paragraph. */
14000 cursor = end - 1;
14001 /* Avoid placing the cursor on the last glyph of the row, where
14002 on terminal frames we hold the vertical border between
14003 adjacent windows. */
14004 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14005 && !WINDOW_RIGHTMOST_P (w)
14006 && cursor == row->glyphs[LAST_AREA] - 1)
14007 cursor--;
14008 x = -1; /* will be computed below, at label compute_x */
14009 }
14010
14011 /* Step 1: Try to find the glyph whose character position
14012 corresponds to point. If that's not possible, find 2 glyphs
14013 whose character positions are the closest to point, one before
14014 point, the other after it. */
14015 if (!row->reversed_p)
14016 while (/* not marched to end of glyph row */
14017 glyph < end
14018 /* glyph was not inserted by redisplay for internal purposes */
14019 && !INTEGERP (glyph->object))
14020 {
14021 if (BUFFERP (glyph->object))
14022 {
14023 ptrdiff_t dpos = glyph->charpos - pt_old;
14024
14025 if (glyph->charpos > bpos_max)
14026 bpos_max = glyph->charpos;
14027 if (glyph->charpos < bpos_min)
14028 bpos_min = glyph->charpos;
14029 if (!glyph->avoid_cursor_p)
14030 {
14031 /* If we hit point, we've found the glyph on which to
14032 display the cursor. */
14033 if (dpos == 0)
14034 {
14035 match_with_avoid_cursor = 0;
14036 break;
14037 }
14038 /* See if we've found a better approximation to
14039 POS_BEFORE or to POS_AFTER. */
14040 if (0 > dpos && dpos > pos_before - pt_old)
14041 {
14042 pos_before = glyph->charpos;
14043 glyph_before = glyph;
14044 }
14045 else if (0 < dpos && dpos < pos_after - pt_old)
14046 {
14047 pos_after = glyph->charpos;
14048 glyph_after = glyph;
14049 }
14050 }
14051 else if (dpos == 0)
14052 match_with_avoid_cursor = 1;
14053 }
14054 else if (STRINGP (glyph->object))
14055 {
14056 Lisp_Object chprop;
14057 ptrdiff_t glyph_pos = glyph->charpos;
14058
14059 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14060 glyph->object);
14061 if (!NILP (chprop))
14062 {
14063 /* If the string came from a `display' text property,
14064 look up the buffer position of that property and
14065 use that position to update bpos_max, as if we
14066 actually saw such a position in one of the row's
14067 glyphs. This helps with supporting integer values
14068 of `cursor' property on the display string in
14069 situations where most or all of the row's buffer
14070 text is completely covered by display properties,
14071 so that no glyph with valid buffer positions is
14072 ever seen in the row. */
14073 ptrdiff_t prop_pos =
14074 string_buffer_position_lim (glyph->object, pos_before,
14075 pos_after, 0);
14076
14077 if (prop_pos >= pos_before)
14078 bpos_max = prop_pos - 1;
14079 }
14080 if (INTEGERP (chprop))
14081 {
14082 bpos_covered = bpos_max + XINT (chprop);
14083 /* If the `cursor' property covers buffer positions up
14084 to and including point, we should display cursor on
14085 this glyph. Note that, if a `cursor' property on one
14086 of the string's characters has an integer value, we
14087 will break out of the loop below _before_ we get to
14088 the position match above. IOW, integer values of
14089 the `cursor' property override the "exact match for
14090 point" strategy of positioning the cursor. */
14091 /* Implementation note: bpos_max == pt_old when, e.g.,
14092 we are in an empty line, where bpos_max is set to
14093 MATRIX_ROW_START_CHARPOS, see above. */
14094 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14095 {
14096 cursor = glyph;
14097 break;
14098 }
14099 }
14100
14101 string_seen = 1;
14102 }
14103 x += glyph->pixel_width;
14104 ++glyph;
14105 }
14106 else if (glyph > end) /* row is reversed */
14107 while (!INTEGERP (glyph->object))
14108 {
14109 if (BUFFERP (glyph->object))
14110 {
14111 ptrdiff_t dpos = glyph->charpos - pt_old;
14112
14113 if (glyph->charpos > bpos_max)
14114 bpos_max = glyph->charpos;
14115 if (glyph->charpos < bpos_min)
14116 bpos_min = glyph->charpos;
14117 if (!glyph->avoid_cursor_p)
14118 {
14119 if (dpos == 0)
14120 {
14121 match_with_avoid_cursor = 0;
14122 break;
14123 }
14124 if (0 > dpos && dpos > pos_before - pt_old)
14125 {
14126 pos_before = glyph->charpos;
14127 glyph_before = glyph;
14128 }
14129 else if (0 < dpos && dpos < pos_after - pt_old)
14130 {
14131 pos_after = glyph->charpos;
14132 glyph_after = glyph;
14133 }
14134 }
14135 else if (dpos == 0)
14136 match_with_avoid_cursor = 1;
14137 }
14138 else if (STRINGP (glyph->object))
14139 {
14140 Lisp_Object chprop;
14141 ptrdiff_t glyph_pos = glyph->charpos;
14142
14143 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14144 glyph->object);
14145 if (!NILP (chprop))
14146 {
14147 ptrdiff_t prop_pos =
14148 string_buffer_position_lim (glyph->object, pos_before,
14149 pos_after, 0);
14150
14151 if (prop_pos >= pos_before)
14152 bpos_max = prop_pos - 1;
14153 }
14154 if (INTEGERP (chprop))
14155 {
14156 bpos_covered = bpos_max + XINT (chprop);
14157 /* If the `cursor' property covers buffer positions up
14158 to and including point, we should display cursor on
14159 this glyph. */
14160 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14161 {
14162 cursor = glyph;
14163 break;
14164 }
14165 }
14166 string_seen = 1;
14167 }
14168 --glyph;
14169 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14170 {
14171 x--; /* can't use any pixel_width */
14172 break;
14173 }
14174 x -= glyph->pixel_width;
14175 }
14176
14177 /* Step 2: If we didn't find an exact match for point, we need to
14178 look for a proper place to put the cursor among glyphs between
14179 GLYPH_BEFORE and GLYPH_AFTER. */
14180 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14181 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14182 && bpos_covered < pt_old)
14183 {
14184 /* An empty line has a single glyph whose OBJECT is zero and
14185 whose CHARPOS is the position of a newline on that line.
14186 Note that on a TTY, there are more glyphs after that, which
14187 were produced by extend_face_to_end_of_line, but their
14188 CHARPOS is zero or negative. */
14189 int empty_line_p =
14190 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14191 && INTEGERP (glyph->object) && glyph->charpos > 0;
14192
14193 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14194 {
14195 ptrdiff_t ellipsis_pos;
14196
14197 /* Scan back over the ellipsis glyphs. */
14198 if (!row->reversed_p)
14199 {
14200 ellipsis_pos = (glyph - 1)->charpos;
14201 while (glyph > row->glyphs[TEXT_AREA]
14202 && (glyph - 1)->charpos == ellipsis_pos)
14203 glyph--, x -= glyph->pixel_width;
14204 /* That loop always goes one position too far, including
14205 the glyph before the ellipsis. So scan forward over
14206 that one. */
14207 x += glyph->pixel_width;
14208 glyph++;
14209 }
14210 else /* row is reversed */
14211 {
14212 ellipsis_pos = (glyph + 1)->charpos;
14213 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14214 && (glyph + 1)->charpos == ellipsis_pos)
14215 glyph++, x += glyph->pixel_width;
14216 x -= glyph->pixel_width;
14217 glyph--;
14218 }
14219 }
14220 else if (match_with_avoid_cursor)
14221 {
14222 cursor = glyph_after;
14223 x = -1;
14224 }
14225 else if (string_seen)
14226 {
14227 int incr = row->reversed_p ? -1 : +1;
14228
14229 /* Need to find the glyph that came out of a string which is
14230 present at point. That glyph is somewhere between
14231 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14232 positioned between POS_BEFORE and POS_AFTER in the
14233 buffer. */
14234 struct glyph *start, *stop;
14235 ptrdiff_t pos = pos_before;
14236
14237 x = -1;
14238
14239 /* If the row ends in a newline from a display string,
14240 reordering could have moved the glyphs belonging to the
14241 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14242 in this case we extend the search to the last glyph in
14243 the row that was not inserted by redisplay. */
14244 if (row->ends_in_newline_from_string_p)
14245 {
14246 glyph_after = end;
14247 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14248 }
14249
14250 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14251 correspond to POS_BEFORE and POS_AFTER, respectively. We
14252 need START and STOP in the order that corresponds to the
14253 row's direction as given by its reversed_p flag. If the
14254 directionality of characters between POS_BEFORE and
14255 POS_AFTER is the opposite of the row's base direction,
14256 these characters will have been reordered for display,
14257 and we need to reverse START and STOP. */
14258 if (!row->reversed_p)
14259 {
14260 start = min (glyph_before, glyph_after);
14261 stop = max (glyph_before, glyph_after);
14262 }
14263 else
14264 {
14265 start = max (glyph_before, glyph_after);
14266 stop = min (glyph_before, glyph_after);
14267 }
14268 for (glyph = start + incr;
14269 row->reversed_p ? glyph > stop : glyph < stop; )
14270 {
14271
14272 /* Any glyphs that come from the buffer are here because
14273 of bidi reordering. Skip them, and only pay
14274 attention to glyphs that came from some string. */
14275 if (STRINGP (glyph->object))
14276 {
14277 Lisp_Object str;
14278 ptrdiff_t tem;
14279 /* If the display property covers the newline, we
14280 need to search for it one position farther. */
14281 ptrdiff_t lim = pos_after
14282 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14283
14284 string_from_text_prop = 0;
14285 str = glyph->object;
14286 tem = string_buffer_position_lim (str, pos, lim, 0);
14287 if (tem == 0 /* from overlay */
14288 || pos <= tem)
14289 {
14290 /* If the string from which this glyph came is
14291 found in the buffer at point, or at position
14292 that is closer to point than pos_after, then
14293 we've found the glyph we've been looking for.
14294 If it comes from an overlay (tem == 0), and
14295 it has the `cursor' property on one of its
14296 glyphs, record that glyph as a candidate for
14297 displaying the cursor. (As in the
14298 unidirectional version, we will display the
14299 cursor on the last candidate we find.) */
14300 if (tem == 0
14301 || tem == pt_old
14302 || (tem - pt_old > 0 && tem < pos_after))
14303 {
14304 /* The glyphs from this string could have
14305 been reordered. Find the one with the
14306 smallest string position. Or there could
14307 be a character in the string with the
14308 `cursor' property, which means display
14309 cursor on that character's glyph. */
14310 ptrdiff_t strpos = glyph->charpos;
14311
14312 if (tem)
14313 {
14314 cursor = glyph;
14315 string_from_text_prop = 1;
14316 }
14317 for ( ;
14318 (row->reversed_p ? glyph > stop : glyph < stop)
14319 && EQ (glyph->object, str);
14320 glyph += incr)
14321 {
14322 Lisp_Object cprop;
14323 ptrdiff_t gpos = glyph->charpos;
14324
14325 cprop = Fget_char_property (make_number (gpos),
14326 Qcursor,
14327 glyph->object);
14328 if (!NILP (cprop))
14329 {
14330 cursor = glyph;
14331 break;
14332 }
14333 if (tem && glyph->charpos < strpos)
14334 {
14335 strpos = glyph->charpos;
14336 cursor = glyph;
14337 }
14338 }
14339
14340 if (tem == pt_old
14341 || (tem - pt_old > 0 && tem < pos_after))
14342 goto compute_x;
14343 }
14344 if (tem)
14345 pos = tem + 1; /* don't find previous instances */
14346 }
14347 /* This string is not what we want; skip all of the
14348 glyphs that came from it. */
14349 while ((row->reversed_p ? glyph > stop : glyph < stop)
14350 && EQ (glyph->object, str))
14351 glyph += incr;
14352 }
14353 else
14354 glyph += incr;
14355 }
14356
14357 /* If we reached the end of the line, and END was from a string,
14358 the cursor is not on this line. */
14359 if (cursor == NULL
14360 && (row->reversed_p ? glyph <= end : glyph >= end)
14361 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14362 && STRINGP (end->object)
14363 && row->continued_p)
14364 return 0;
14365 }
14366 /* A truncated row may not include PT among its character positions.
14367 Setting the cursor inside the scroll margin will trigger
14368 recalculation of hscroll in hscroll_window_tree. But if a
14369 display string covers point, defer to the string-handling
14370 code below to figure this out. */
14371 else if (row->truncated_on_left_p && pt_old < bpos_min)
14372 {
14373 cursor = glyph_before;
14374 x = -1;
14375 }
14376 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14377 /* Zero-width characters produce no glyphs. */
14378 || (!empty_line_p
14379 && (row->reversed_p
14380 ? glyph_after > glyphs_end
14381 : glyph_after < glyphs_end)))
14382 {
14383 cursor = glyph_after;
14384 x = -1;
14385 }
14386 }
14387
14388 compute_x:
14389 if (cursor != NULL)
14390 glyph = cursor;
14391 else if (glyph == glyphs_end
14392 && pos_before == pos_after
14393 && STRINGP ((row->reversed_p
14394 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14395 : row->glyphs[TEXT_AREA])->object))
14396 {
14397 /* If all the glyphs of this row came from strings, put the
14398 cursor on the first glyph of the row. This avoids having the
14399 cursor outside of the text area in this very rare and hard
14400 use case. */
14401 glyph =
14402 row->reversed_p
14403 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14404 : row->glyphs[TEXT_AREA];
14405 }
14406 if (x < 0)
14407 {
14408 struct glyph *g;
14409
14410 /* Need to compute x that corresponds to GLYPH. */
14411 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14412 {
14413 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14414 emacs_abort ();
14415 x += g->pixel_width;
14416 }
14417 }
14418
14419 /* ROW could be part of a continued line, which, under bidi
14420 reordering, might have other rows whose start and end charpos
14421 occlude point. Only set w->cursor if we found a better
14422 approximation to the cursor position than we have from previously
14423 examined candidate rows belonging to the same continued line. */
14424 if (/* we already have a candidate row */
14425 w->cursor.vpos >= 0
14426 /* that candidate is not the row we are processing */
14427 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14428 /* Make sure cursor.vpos specifies a row whose start and end
14429 charpos occlude point, and it is valid candidate for being a
14430 cursor-row. This is because some callers of this function
14431 leave cursor.vpos at the row where the cursor was displayed
14432 during the last redisplay cycle. */
14433 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14434 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14435 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14436 {
14437 struct glyph *g1 =
14438 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14439
14440 /* Don't consider glyphs that are outside TEXT_AREA. */
14441 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14442 return 0;
14443 /* Keep the candidate whose buffer position is the closest to
14444 point or has the `cursor' property. */
14445 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14446 w->cursor.hpos >= 0
14447 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14448 && ((BUFFERP (g1->object)
14449 && (g1->charpos == pt_old /* an exact match always wins */
14450 || (BUFFERP (glyph->object)
14451 && eabs (g1->charpos - pt_old)
14452 < eabs (glyph->charpos - pt_old))))
14453 /* previous candidate is a glyph from a string that has
14454 a non-nil `cursor' property */
14455 || (STRINGP (g1->object)
14456 && (!NILP (Fget_char_property (make_number (g1->charpos),
14457 Qcursor, g1->object))
14458 /* previous candidate is from the same display
14459 string as this one, and the display string
14460 came from a text property */
14461 || (EQ (g1->object, glyph->object)
14462 && string_from_text_prop)
14463 /* this candidate is from newline and its
14464 position is not an exact match */
14465 || (INTEGERP (glyph->object)
14466 && glyph->charpos != pt_old)))))
14467 return 0;
14468 /* If this candidate gives an exact match, use that. */
14469 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14470 /* If this candidate is a glyph created for the
14471 terminating newline of a line, and point is on that
14472 newline, it wins because it's an exact match. */
14473 || (!row->continued_p
14474 && INTEGERP (glyph->object)
14475 && glyph->charpos == 0
14476 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14477 /* Otherwise, keep the candidate that comes from a row
14478 spanning less buffer positions. This may win when one or
14479 both candidate positions are on glyphs that came from
14480 display strings, for which we cannot compare buffer
14481 positions. */
14482 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14483 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14484 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14485 return 0;
14486 }
14487 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14488 w->cursor.x = x;
14489 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14490 w->cursor.y = row->y + dy;
14491
14492 if (w == XWINDOW (selected_window))
14493 {
14494 if (!row->continued_p
14495 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14496 && row->x == 0)
14497 {
14498 this_line_buffer = XBUFFER (w->buffer);
14499
14500 CHARPOS (this_line_start_pos)
14501 = MATRIX_ROW_START_CHARPOS (row) + delta;
14502 BYTEPOS (this_line_start_pos)
14503 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14504
14505 CHARPOS (this_line_end_pos)
14506 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14507 BYTEPOS (this_line_end_pos)
14508 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14509
14510 this_line_y = w->cursor.y;
14511 this_line_pixel_height = row->height;
14512 this_line_vpos = w->cursor.vpos;
14513 this_line_start_x = row->x;
14514 }
14515 else
14516 CHARPOS (this_line_start_pos) = 0;
14517 }
14518
14519 return 1;
14520 }
14521
14522
14523 /* Run window scroll functions, if any, for WINDOW with new window
14524 start STARTP. Sets the window start of WINDOW to that position.
14525
14526 We assume that the window's buffer is really current. */
14527
14528 static inline struct text_pos
14529 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14530 {
14531 struct window *w = XWINDOW (window);
14532 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14533
14534 if (current_buffer != XBUFFER (w->buffer))
14535 emacs_abort ();
14536
14537 if (!NILP (Vwindow_scroll_functions))
14538 {
14539 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14540 make_number (CHARPOS (startp)));
14541 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14542 /* In case the hook functions switch buffers. */
14543 set_buffer_internal (XBUFFER (w->buffer));
14544 }
14545
14546 return startp;
14547 }
14548
14549
14550 /* Make sure the line containing the cursor is fully visible.
14551 A value of 1 means there is nothing to be done.
14552 (Either the line is fully visible, or it cannot be made so,
14553 or we cannot tell.)
14554
14555 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14556 is higher than window.
14557
14558 A value of 0 means the caller should do scrolling
14559 as if point had gone off the screen. */
14560
14561 static int
14562 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14563 {
14564 struct glyph_matrix *matrix;
14565 struct glyph_row *row;
14566 int window_height;
14567
14568 if (!make_cursor_line_fully_visible_p)
14569 return 1;
14570
14571 /* It's not always possible to find the cursor, e.g, when a window
14572 is full of overlay strings. Don't do anything in that case. */
14573 if (w->cursor.vpos < 0)
14574 return 1;
14575
14576 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14577 row = MATRIX_ROW (matrix, w->cursor.vpos);
14578
14579 /* If the cursor row is not partially visible, there's nothing to do. */
14580 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14581 return 1;
14582
14583 /* If the row the cursor is in is taller than the window's height,
14584 it's not clear what to do, so do nothing. */
14585 window_height = window_box_height (w);
14586 if (row->height >= window_height)
14587 {
14588 if (!force_p || MINI_WINDOW_P (w)
14589 || w->vscroll || w->cursor.vpos == 0)
14590 return 1;
14591 }
14592 return 0;
14593 }
14594
14595
14596 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14597 non-zero means only WINDOW is redisplayed in redisplay_internal.
14598 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14599 in redisplay_window to bring a partially visible line into view in
14600 the case that only the cursor has moved.
14601
14602 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14603 last screen line's vertical height extends past the end of the screen.
14604
14605 Value is
14606
14607 1 if scrolling succeeded
14608
14609 0 if scrolling didn't find point.
14610
14611 -1 if new fonts have been loaded so that we must interrupt
14612 redisplay, adjust glyph matrices, and try again. */
14613
14614 enum
14615 {
14616 SCROLLING_SUCCESS,
14617 SCROLLING_FAILED,
14618 SCROLLING_NEED_LARGER_MATRICES
14619 };
14620
14621 /* If scroll-conservatively is more than this, never recenter.
14622
14623 If you change this, don't forget to update the doc string of
14624 `scroll-conservatively' and the Emacs manual. */
14625 #define SCROLL_LIMIT 100
14626
14627 static int
14628 try_scrolling (Lisp_Object window, int just_this_one_p,
14629 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14630 int temp_scroll_step, int last_line_misfit)
14631 {
14632 struct window *w = XWINDOW (window);
14633 struct frame *f = XFRAME (w->frame);
14634 struct text_pos pos, startp;
14635 struct it it;
14636 int this_scroll_margin, scroll_max, rc, height;
14637 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14638 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14639 Lisp_Object aggressive;
14640 /* We will never try scrolling more than this number of lines. */
14641 int scroll_limit = SCROLL_LIMIT;
14642
14643 #ifdef GLYPH_DEBUG
14644 debug_method_add (w, "try_scrolling");
14645 #endif
14646
14647 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14648
14649 /* Compute scroll margin height in pixels. We scroll when point is
14650 within this distance from the top or bottom of the window. */
14651 if (scroll_margin > 0)
14652 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14653 * FRAME_LINE_HEIGHT (f);
14654 else
14655 this_scroll_margin = 0;
14656
14657 /* Force arg_scroll_conservatively to have a reasonable value, to
14658 avoid scrolling too far away with slow move_it_* functions. Note
14659 that the user can supply scroll-conservatively equal to
14660 `most-positive-fixnum', which can be larger than INT_MAX. */
14661 if (arg_scroll_conservatively > scroll_limit)
14662 {
14663 arg_scroll_conservatively = scroll_limit + 1;
14664 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14665 }
14666 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14667 /* Compute how much we should try to scroll maximally to bring
14668 point into view. */
14669 scroll_max = (max (scroll_step,
14670 max (arg_scroll_conservatively, temp_scroll_step))
14671 * FRAME_LINE_HEIGHT (f));
14672 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14673 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14674 /* We're trying to scroll because of aggressive scrolling but no
14675 scroll_step is set. Choose an arbitrary one. */
14676 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14677 else
14678 scroll_max = 0;
14679
14680 too_near_end:
14681
14682 /* Decide whether to scroll down. */
14683 if (PT > CHARPOS (startp))
14684 {
14685 int scroll_margin_y;
14686
14687 /* Compute the pixel ypos of the scroll margin, then move IT to
14688 either that ypos or PT, whichever comes first. */
14689 start_display (&it, w, startp);
14690 scroll_margin_y = it.last_visible_y - this_scroll_margin
14691 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14692 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14693 (MOVE_TO_POS | MOVE_TO_Y));
14694
14695 if (PT > CHARPOS (it.current.pos))
14696 {
14697 int y0 = line_bottom_y (&it);
14698 /* Compute how many pixels below window bottom to stop searching
14699 for PT. This avoids costly search for PT that is far away if
14700 the user limited scrolling by a small number of lines, but
14701 always finds PT if scroll_conservatively is set to a large
14702 number, such as most-positive-fixnum. */
14703 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14704 int y_to_move = it.last_visible_y + slack;
14705
14706 /* Compute the distance from the scroll margin to PT or to
14707 the scroll limit, whichever comes first. This should
14708 include the height of the cursor line, to make that line
14709 fully visible. */
14710 move_it_to (&it, PT, -1, y_to_move,
14711 -1, MOVE_TO_POS | MOVE_TO_Y);
14712 dy = line_bottom_y (&it) - y0;
14713
14714 if (dy > scroll_max)
14715 return SCROLLING_FAILED;
14716
14717 if (dy > 0)
14718 scroll_down_p = 1;
14719 }
14720 }
14721
14722 if (scroll_down_p)
14723 {
14724 /* Point is in or below the bottom scroll margin, so move the
14725 window start down. If scrolling conservatively, move it just
14726 enough down to make point visible. If scroll_step is set,
14727 move it down by scroll_step. */
14728 if (arg_scroll_conservatively)
14729 amount_to_scroll
14730 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14731 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14732 else if (scroll_step || temp_scroll_step)
14733 amount_to_scroll = scroll_max;
14734 else
14735 {
14736 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14737 height = WINDOW_BOX_TEXT_HEIGHT (w);
14738 if (NUMBERP (aggressive))
14739 {
14740 double float_amount = XFLOATINT (aggressive) * height;
14741 amount_to_scroll = float_amount;
14742 if (amount_to_scroll == 0 && float_amount > 0)
14743 amount_to_scroll = 1;
14744 /* Don't let point enter the scroll margin near top of
14745 the window. */
14746 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14747 amount_to_scroll = height - 2*this_scroll_margin + dy;
14748 }
14749 }
14750
14751 if (amount_to_scroll <= 0)
14752 return SCROLLING_FAILED;
14753
14754 start_display (&it, w, startp);
14755 if (arg_scroll_conservatively <= scroll_limit)
14756 move_it_vertically (&it, amount_to_scroll);
14757 else
14758 {
14759 /* Extra precision for users who set scroll-conservatively
14760 to a large number: make sure the amount we scroll
14761 the window start is never less than amount_to_scroll,
14762 which was computed as distance from window bottom to
14763 point. This matters when lines at window top and lines
14764 below window bottom have different height. */
14765 struct it it1;
14766 void *it1data = NULL;
14767 /* We use a temporary it1 because line_bottom_y can modify
14768 its argument, if it moves one line down; see there. */
14769 int start_y;
14770
14771 SAVE_IT (it1, it, it1data);
14772 start_y = line_bottom_y (&it1);
14773 do {
14774 RESTORE_IT (&it, &it, it1data);
14775 move_it_by_lines (&it, 1);
14776 SAVE_IT (it1, it, it1data);
14777 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14778 }
14779
14780 /* If STARTP is unchanged, move it down another screen line. */
14781 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14782 move_it_by_lines (&it, 1);
14783 startp = it.current.pos;
14784 }
14785 else
14786 {
14787 struct text_pos scroll_margin_pos = startp;
14788
14789 /* See if point is inside the scroll margin at the top of the
14790 window. */
14791 if (this_scroll_margin)
14792 {
14793 start_display (&it, w, startp);
14794 move_it_vertically (&it, this_scroll_margin);
14795 scroll_margin_pos = it.current.pos;
14796 }
14797
14798 if (PT < CHARPOS (scroll_margin_pos))
14799 {
14800 /* Point is in the scroll margin at the top of the window or
14801 above what is displayed in the window. */
14802 int y0, y_to_move;
14803
14804 /* Compute the vertical distance from PT to the scroll
14805 margin position. Move as far as scroll_max allows, or
14806 one screenful, or 10 screen lines, whichever is largest.
14807 Give up if distance is greater than scroll_max. */
14808 SET_TEXT_POS (pos, PT, PT_BYTE);
14809 start_display (&it, w, pos);
14810 y0 = it.current_y;
14811 y_to_move = max (it.last_visible_y,
14812 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14813 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14814 y_to_move, -1,
14815 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14816 dy = it.current_y - y0;
14817 if (dy > scroll_max)
14818 return SCROLLING_FAILED;
14819
14820 /* Compute new window start. */
14821 start_display (&it, w, startp);
14822
14823 if (arg_scroll_conservatively)
14824 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14825 max (scroll_step, temp_scroll_step));
14826 else if (scroll_step || temp_scroll_step)
14827 amount_to_scroll = scroll_max;
14828 else
14829 {
14830 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14831 height = WINDOW_BOX_TEXT_HEIGHT (w);
14832 if (NUMBERP (aggressive))
14833 {
14834 double float_amount = XFLOATINT (aggressive) * height;
14835 amount_to_scroll = float_amount;
14836 if (amount_to_scroll == 0 && float_amount > 0)
14837 amount_to_scroll = 1;
14838 amount_to_scroll -=
14839 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14840 /* Don't let point enter the scroll margin near
14841 bottom of the window. */
14842 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14843 amount_to_scroll = height - 2*this_scroll_margin + dy;
14844 }
14845 }
14846
14847 if (amount_to_scroll <= 0)
14848 return SCROLLING_FAILED;
14849
14850 move_it_vertically_backward (&it, amount_to_scroll);
14851 startp = it.current.pos;
14852 }
14853 }
14854
14855 /* Run window scroll functions. */
14856 startp = run_window_scroll_functions (window, startp);
14857
14858 /* Display the window. Give up if new fonts are loaded, or if point
14859 doesn't appear. */
14860 if (!try_window (window, startp, 0))
14861 rc = SCROLLING_NEED_LARGER_MATRICES;
14862 else if (w->cursor.vpos < 0)
14863 {
14864 clear_glyph_matrix (w->desired_matrix);
14865 rc = SCROLLING_FAILED;
14866 }
14867 else
14868 {
14869 /* Maybe forget recorded base line for line number display. */
14870 if (!just_this_one_p
14871 || current_buffer->clip_changed
14872 || BEG_UNCHANGED < CHARPOS (startp))
14873 wset_base_line_number (w, Qnil);
14874
14875 /* If cursor ends up on a partially visible line,
14876 treat that as being off the bottom of the screen. */
14877 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14878 /* It's possible that the cursor is on the first line of the
14879 buffer, which is partially obscured due to a vscroll
14880 (Bug#7537). In that case, avoid looping forever . */
14881 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14882 {
14883 clear_glyph_matrix (w->desired_matrix);
14884 ++extra_scroll_margin_lines;
14885 goto too_near_end;
14886 }
14887 rc = SCROLLING_SUCCESS;
14888 }
14889
14890 return rc;
14891 }
14892
14893
14894 /* Compute a suitable window start for window W if display of W starts
14895 on a continuation line. Value is non-zero if a new window start
14896 was computed.
14897
14898 The new window start will be computed, based on W's width, starting
14899 from the start of the continued line. It is the start of the
14900 screen line with the minimum distance from the old start W->start. */
14901
14902 static int
14903 compute_window_start_on_continuation_line (struct window *w)
14904 {
14905 struct text_pos pos, start_pos;
14906 int window_start_changed_p = 0;
14907
14908 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14909
14910 /* If window start is on a continuation line... Window start may be
14911 < BEGV in case there's invisible text at the start of the
14912 buffer (M-x rmail, for example). */
14913 if (CHARPOS (start_pos) > BEGV
14914 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14915 {
14916 struct it it;
14917 struct glyph_row *row;
14918
14919 /* Handle the case that the window start is out of range. */
14920 if (CHARPOS (start_pos) < BEGV)
14921 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14922 else if (CHARPOS (start_pos) > ZV)
14923 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14924
14925 /* Find the start of the continued line. This should be fast
14926 because scan_buffer is fast (newline cache). */
14927 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14928 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14929 row, DEFAULT_FACE_ID);
14930 reseat_at_previous_visible_line_start (&it);
14931
14932 /* If the line start is "too far" away from the window start,
14933 say it takes too much time to compute a new window start. */
14934 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14935 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14936 {
14937 int min_distance, distance;
14938
14939 /* Move forward by display lines to find the new window
14940 start. If window width was enlarged, the new start can
14941 be expected to be > the old start. If window width was
14942 decreased, the new window start will be < the old start.
14943 So, we're looking for the display line start with the
14944 minimum distance from the old window start. */
14945 pos = it.current.pos;
14946 min_distance = INFINITY;
14947 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14948 distance < min_distance)
14949 {
14950 min_distance = distance;
14951 pos = it.current.pos;
14952 move_it_by_lines (&it, 1);
14953 }
14954
14955 /* Set the window start there. */
14956 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14957 window_start_changed_p = 1;
14958 }
14959 }
14960
14961 return window_start_changed_p;
14962 }
14963
14964
14965 /* Try cursor movement in case text has not changed in window WINDOW,
14966 with window start STARTP. Value is
14967
14968 CURSOR_MOVEMENT_SUCCESS if successful
14969
14970 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14971
14972 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14973 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14974 we want to scroll as if scroll-step were set to 1. See the code.
14975
14976 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14977 which case we have to abort this redisplay, and adjust matrices
14978 first. */
14979
14980 enum
14981 {
14982 CURSOR_MOVEMENT_SUCCESS,
14983 CURSOR_MOVEMENT_CANNOT_BE_USED,
14984 CURSOR_MOVEMENT_MUST_SCROLL,
14985 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14986 };
14987
14988 static int
14989 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14990 {
14991 struct window *w = XWINDOW (window);
14992 struct frame *f = XFRAME (w->frame);
14993 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14994
14995 #ifdef GLYPH_DEBUG
14996 if (inhibit_try_cursor_movement)
14997 return rc;
14998 #endif
14999
15000 /* Previously, there was a check for Lisp integer in the
15001 if-statement below. Now, this field is converted to
15002 ptrdiff_t, thus zero means invalid position in a buffer. */
15003 eassert (w->last_point > 0);
15004
15005 /* Handle case where text has not changed, only point, and it has
15006 not moved off the frame. */
15007 if (/* Point may be in this window. */
15008 PT >= CHARPOS (startp)
15009 /* Selective display hasn't changed. */
15010 && !current_buffer->clip_changed
15011 /* Function force-mode-line-update is used to force a thorough
15012 redisplay. It sets either windows_or_buffers_changed or
15013 update_mode_lines. So don't take a shortcut here for these
15014 cases. */
15015 && !update_mode_lines
15016 && !windows_or_buffers_changed
15017 && !cursor_type_changed
15018 /* Can't use this case if highlighting a region. When a
15019 region exists, cursor movement has to do more than just
15020 set the cursor. */
15021 && !(!NILP (Vtransient_mark_mode)
15022 && !NILP (BVAR (current_buffer, mark_active)))
15023 && NILP (w->region_showing)
15024 && NILP (Vshow_trailing_whitespace)
15025 /* This code is not used for mini-buffer for the sake of the case
15026 of redisplaying to replace an echo area message; since in
15027 that case the mini-buffer contents per se are usually
15028 unchanged. This code is of no real use in the mini-buffer
15029 since the handling of this_line_start_pos, etc., in redisplay
15030 handles the same cases. */
15031 && !EQ (window, minibuf_window)
15032 /* When splitting windows or for new windows, it happens that
15033 redisplay is called with a nil window_end_vpos or one being
15034 larger than the window. This should really be fixed in
15035 window.c. I don't have this on my list, now, so we do
15036 approximately the same as the old redisplay code. --gerd. */
15037 && INTEGERP (w->window_end_vpos)
15038 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
15039 && (FRAME_WINDOW_P (f)
15040 || !overlay_arrow_in_current_buffer_p ()))
15041 {
15042 int this_scroll_margin, top_scroll_margin;
15043 struct glyph_row *row = NULL;
15044
15045 #ifdef GLYPH_DEBUG
15046 debug_method_add (w, "cursor movement");
15047 #endif
15048
15049 /* Scroll if point within this distance from the top or bottom
15050 of the window. This is a pixel value. */
15051 if (scroll_margin > 0)
15052 {
15053 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15054 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15055 }
15056 else
15057 this_scroll_margin = 0;
15058
15059 top_scroll_margin = this_scroll_margin;
15060 if (WINDOW_WANTS_HEADER_LINE_P (w))
15061 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15062
15063 /* Start with the row the cursor was displayed during the last
15064 not paused redisplay. Give up if that row is not valid. */
15065 if (w->last_cursor.vpos < 0
15066 || w->last_cursor.vpos >= w->current_matrix->nrows)
15067 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15068 else
15069 {
15070 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15071 if (row->mode_line_p)
15072 ++row;
15073 if (!row->enabled_p)
15074 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15075 }
15076
15077 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15078 {
15079 int scroll_p = 0, must_scroll = 0;
15080 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15081
15082 if (PT > w->last_point)
15083 {
15084 /* Point has moved forward. */
15085 while (MATRIX_ROW_END_CHARPOS (row) < PT
15086 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15087 {
15088 eassert (row->enabled_p);
15089 ++row;
15090 }
15091
15092 /* If the end position of a row equals the start
15093 position of the next row, and PT is at that position,
15094 we would rather display cursor in the next line. */
15095 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15096 && MATRIX_ROW_END_CHARPOS (row) == PT
15097 && row < w->current_matrix->rows
15098 + w->current_matrix->nrows - 1
15099 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15100 && !cursor_row_p (row))
15101 ++row;
15102
15103 /* If within the scroll margin, scroll. Note that
15104 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15105 the next line would be drawn, and that
15106 this_scroll_margin can be zero. */
15107 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15108 || PT > MATRIX_ROW_END_CHARPOS (row)
15109 /* Line is completely visible last line in window
15110 and PT is to be set in the next line. */
15111 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15112 && PT == MATRIX_ROW_END_CHARPOS (row)
15113 && !row->ends_at_zv_p
15114 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15115 scroll_p = 1;
15116 }
15117 else if (PT < w->last_point)
15118 {
15119 /* Cursor has to be moved backward. Note that PT >=
15120 CHARPOS (startp) because of the outer if-statement. */
15121 while (!row->mode_line_p
15122 && (MATRIX_ROW_START_CHARPOS (row) > PT
15123 || (MATRIX_ROW_START_CHARPOS (row) == PT
15124 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15125 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15126 row > w->current_matrix->rows
15127 && (row-1)->ends_in_newline_from_string_p))))
15128 && (row->y > top_scroll_margin
15129 || CHARPOS (startp) == BEGV))
15130 {
15131 eassert (row->enabled_p);
15132 --row;
15133 }
15134
15135 /* Consider the following case: Window starts at BEGV,
15136 there is invisible, intangible text at BEGV, so that
15137 display starts at some point START > BEGV. It can
15138 happen that we are called with PT somewhere between
15139 BEGV and START. Try to handle that case. */
15140 if (row < w->current_matrix->rows
15141 || row->mode_line_p)
15142 {
15143 row = w->current_matrix->rows;
15144 if (row->mode_line_p)
15145 ++row;
15146 }
15147
15148 /* Due to newlines in overlay strings, we may have to
15149 skip forward over overlay strings. */
15150 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15151 && MATRIX_ROW_END_CHARPOS (row) == PT
15152 && !cursor_row_p (row))
15153 ++row;
15154
15155 /* If within the scroll margin, scroll. */
15156 if (row->y < top_scroll_margin
15157 && CHARPOS (startp) != BEGV)
15158 scroll_p = 1;
15159 }
15160 else
15161 {
15162 /* Cursor did not move. So don't scroll even if cursor line
15163 is partially visible, as it was so before. */
15164 rc = CURSOR_MOVEMENT_SUCCESS;
15165 }
15166
15167 if (PT < MATRIX_ROW_START_CHARPOS (row)
15168 || PT > MATRIX_ROW_END_CHARPOS (row))
15169 {
15170 /* if PT is not in the glyph row, give up. */
15171 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15172 must_scroll = 1;
15173 }
15174 else if (rc != CURSOR_MOVEMENT_SUCCESS
15175 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15176 {
15177 struct glyph_row *row1;
15178
15179 /* If rows are bidi-reordered and point moved, back up
15180 until we find a row that does not belong to a
15181 continuation line. This is because we must consider
15182 all rows of a continued line as candidates for the
15183 new cursor positioning, since row start and end
15184 positions change non-linearly with vertical position
15185 in such rows. */
15186 /* FIXME: Revisit this when glyph ``spilling'' in
15187 continuation lines' rows is implemented for
15188 bidi-reordered rows. */
15189 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15190 MATRIX_ROW_CONTINUATION_LINE_P (row);
15191 --row)
15192 {
15193 /* If we hit the beginning of the displayed portion
15194 without finding the first row of a continued
15195 line, give up. */
15196 if (row <= row1)
15197 {
15198 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15199 break;
15200 }
15201 eassert (row->enabled_p);
15202 }
15203 }
15204 if (must_scroll)
15205 ;
15206 else if (rc != CURSOR_MOVEMENT_SUCCESS
15207 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15208 /* Make sure this isn't a header line by any chance, since
15209 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15210 && !row->mode_line_p
15211 && make_cursor_line_fully_visible_p)
15212 {
15213 if (PT == MATRIX_ROW_END_CHARPOS (row)
15214 && !row->ends_at_zv_p
15215 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15216 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15217 else if (row->height > window_box_height (w))
15218 {
15219 /* If we end up in a partially visible line, let's
15220 make it fully visible, except when it's taller
15221 than the window, in which case we can't do much
15222 about it. */
15223 *scroll_step = 1;
15224 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15225 }
15226 else
15227 {
15228 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15229 if (!cursor_row_fully_visible_p (w, 0, 1))
15230 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15231 else
15232 rc = CURSOR_MOVEMENT_SUCCESS;
15233 }
15234 }
15235 else if (scroll_p)
15236 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15237 else if (rc != CURSOR_MOVEMENT_SUCCESS
15238 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15239 {
15240 /* With bidi-reordered rows, there could be more than
15241 one candidate row whose start and end positions
15242 occlude point. We need to let set_cursor_from_row
15243 find the best candidate. */
15244 /* FIXME: Revisit this when glyph ``spilling'' in
15245 continuation lines' rows is implemented for
15246 bidi-reordered rows. */
15247 int rv = 0;
15248
15249 do
15250 {
15251 int at_zv_p = 0, exact_match_p = 0;
15252
15253 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15254 && PT <= MATRIX_ROW_END_CHARPOS (row)
15255 && cursor_row_p (row))
15256 rv |= set_cursor_from_row (w, row, w->current_matrix,
15257 0, 0, 0, 0);
15258 /* As soon as we've found the exact match for point,
15259 or the first suitable row whose ends_at_zv_p flag
15260 is set, we are done. */
15261 at_zv_p =
15262 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15263 if (rv && !at_zv_p
15264 && w->cursor.hpos >= 0
15265 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15266 w->cursor.vpos))
15267 {
15268 struct glyph_row *candidate =
15269 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15270 struct glyph *g =
15271 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15272 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15273
15274 exact_match_p =
15275 (BUFFERP (g->object) && g->charpos == PT)
15276 || (INTEGERP (g->object)
15277 && (g->charpos == PT
15278 || (g->charpos == 0 && endpos - 1 == PT)));
15279 }
15280 if (rv && (at_zv_p || exact_match_p))
15281 {
15282 rc = CURSOR_MOVEMENT_SUCCESS;
15283 break;
15284 }
15285 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15286 break;
15287 ++row;
15288 }
15289 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15290 || row->continued_p)
15291 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15292 || (MATRIX_ROW_START_CHARPOS (row) == PT
15293 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15294 /* If we didn't find any candidate rows, or exited the
15295 loop before all the candidates were examined, signal
15296 to the caller that this method failed. */
15297 if (rc != CURSOR_MOVEMENT_SUCCESS
15298 && !(rv
15299 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15300 && !row->continued_p))
15301 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15302 else if (rv)
15303 rc = CURSOR_MOVEMENT_SUCCESS;
15304 }
15305 else
15306 {
15307 do
15308 {
15309 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15310 {
15311 rc = CURSOR_MOVEMENT_SUCCESS;
15312 break;
15313 }
15314 ++row;
15315 }
15316 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15317 && MATRIX_ROW_START_CHARPOS (row) == PT
15318 && cursor_row_p (row));
15319 }
15320 }
15321 }
15322
15323 return rc;
15324 }
15325
15326 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15327 static
15328 #endif
15329 void
15330 set_vertical_scroll_bar (struct window *w)
15331 {
15332 ptrdiff_t start, end, whole;
15333
15334 /* Calculate the start and end positions for the current window.
15335 At some point, it would be nice to choose between scrollbars
15336 which reflect the whole buffer size, with special markers
15337 indicating narrowing, and scrollbars which reflect only the
15338 visible region.
15339
15340 Note that mini-buffers sometimes aren't displaying any text. */
15341 if (!MINI_WINDOW_P (w)
15342 || (w == XWINDOW (minibuf_window)
15343 && NILP (echo_area_buffer[0])))
15344 {
15345 struct buffer *buf = XBUFFER (w->buffer);
15346 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15347 start = marker_position (w->start) - BUF_BEGV (buf);
15348 /* I don't think this is guaranteed to be right. For the
15349 moment, we'll pretend it is. */
15350 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15351
15352 if (end < start)
15353 end = start;
15354 if (whole < (end - start))
15355 whole = end - start;
15356 }
15357 else
15358 start = end = whole = 0;
15359
15360 /* Indicate what this scroll bar ought to be displaying now. */
15361 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15362 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15363 (w, end - start, whole, start);
15364 }
15365
15366
15367 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15368 selected_window is redisplayed.
15369
15370 We can return without actually redisplaying the window if
15371 fonts_changed_p. In that case, redisplay_internal will
15372 retry. */
15373
15374 static void
15375 redisplay_window (Lisp_Object window, int just_this_one_p)
15376 {
15377 struct window *w = XWINDOW (window);
15378 struct frame *f = XFRAME (w->frame);
15379 struct buffer *buffer = XBUFFER (w->buffer);
15380 struct buffer *old = current_buffer;
15381 struct text_pos lpoint, opoint, startp;
15382 int update_mode_line;
15383 int tem;
15384 struct it it;
15385 /* Record it now because it's overwritten. */
15386 int current_matrix_up_to_date_p = 0;
15387 int used_current_matrix_p = 0;
15388 /* This is less strict than current_matrix_up_to_date_p.
15389 It indicates that the buffer contents and narrowing are unchanged. */
15390 int buffer_unchanged_p = 0;
15391 int temp_scroll_step = 0;
15392 ptrdiff_t count = SPECPDL_INDEX ();
15393 int rc;
15394 int centering_position = -1;
15395 int last_line_misfit = 0;
15396 ptrdiff_t beg_unchanged, end_unchanged;
15397
15398 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15399 opoint = lpoint;
15400
15401 /* W must be a leaf window here. */
15402 eassert (!NILP (w->buffer));
15403 #ifdef GLYPH_DEBUG
15404 *w->desired_matrix->method = 0;
15405 #endif
15406
15407 restart:
15408 reconsider_clip_changes (w, buffer);
15409
15410 /* Has the mode line to be updated? */
15411 update_mode_line = (w->update_mode_line
15412 || update_mode_lines
15413 || buffer->clip_changed
15414 || buffer->prevent_redisplay_optimizations_p);
15415
15416 if (MINI_WINDOW_P (w))
15417 {
15418 if (w == XWINDOW (echo_area_window)
15419 && !NILP (echo_area_buffer[0]))
15420 {
15421 if (update_mode_line)
15422 /* We may have to update a tty frame's menu bar or a
15423 tool-bar. Example `M-x C-h C-h C-g'. */
15424 goto finish_menu_bars;
15425 else
15426 /* We've already displayed the echo area glyphs in this window. */
15427 goto finish_scroll_bars;
15428 }
15429 else if ((w != XWINDOW (minibuf_window)
15430 || minibuf_level == 0)
15431 /* When buffer is nonempty, redisplay window normally. */
15432 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15433 /* Quail displays non-mini buffers in minibuffer window.
15434 In that case, redisplay the window normally. */
15435 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15436 {
15437 /* W is a mini-buffer window, but it's not active, so clear
15438 it. */
15439 int yb = window_text_bottom_y (w);
15440 struct glyph_row *row;
15441 int y;
15442
15443 for (y = 0, row = w->desired_matrix->rows;
15444 y < yb;
15445 y += row->height, ++row)
15446 blank_row (w, row, y);
15447 goto finish_scroll_bars;
15448 }
15449
15450 clear_glyph_matrix (w->desired_matrix);
15451 }
15452
15453 /* Otherwise set up data on this window; select its buffer and point
15454 value. */
15455 /* Really select the buffer, for the sake of buffer-local
15456 variables. */
15457 set_buffer_internal_1 (XBUFFER (w->buffer));
15458
15459 current_matrix_up_to_date_p
15460 = (!NILP (w->window_end_valid)
15461 && !current_buffer->clip_changed
15462 && !current_buffer->prevent_redisplay_optimizations_p
15463 && w->last_modified >= MODIFF
15464 && w->last_overlay_modified >= OVERLAY_MODIFF);
15465
15466 /* Run the window-bottom-change-functions
15467 if it is possible that the text on the screen has changed
15468 (either due to modification of the text, or any other reason). */
15469 if (!current_matrix_up_to_date_p
15470 && !NILP (Vwindow_text_change_functions))
15471 {
15472 safe_run_hooks (Qwindow_text_change_functions);
15473 goto restart;
15474 }
15475
15476 beg_unchanged = BEG_UNCHANGED;
15477 end_unchanged = END_UNCHANGED;
15478
15479 SET_TEXT_POS (opoint, PT, PT_BYTE);
15480
15481 specbind (Qinhibit_point_motion_hooks, Qt);
15482
15483 buffer_unchanged_p
15484 = (!NILP (w->window_end_valid)
15485 && !current_buffer->clip_changed
15486 && w->last_modified >= MODIFF
15487 && w->last_overlay_modified >= OVERLAY_MODIFF);
15488
15489 /* When windows_or_buffers_changed is non-zero, we can't rely on
15490 the window end being valid, so set it to nil there. */
15491 if (windows_or_buffers_changed)
15492 {
15493 /* If window starts on a continuation line, maybe adjust the
15494 window start in case the window's width changed. */
15495 if (XMARKER (w->start)->buffer == current_buffer)
15496 compute_window_start_on_continuation_line (w);
15497
15498 wset_window_end_valid (w, Qnil);
15499 }
15500
15501 /* Some sanity checks. */
15502 CHECK_WINDOW_END (w);
15503 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15504 emacs_abort ();
15505 if (BYTEPOS (opoint) < CHARPOS (opoint))
15506 emacs_abort ();
15507
15508 /* If %c is in mode line, update it if needed. */
15509 if (!NILP (w->column_number_displayed)
15510 /* This alternative quickly identifies a common case
15511 where no change is needed. */
15512 && !(PT == w->last_point
15513 && w->last_modified >= MODIFF
15514 && w->last_overlay_modified >= OVERLAY_MODIFF)
15515 && (XFASTINT (w->column_number_displayed) != current_column ()))
15516 update_mode_line = 1;
15517
15518 /* Count number of windows showing the selected buffer. An indirect
15519 buffer counts as its base buffer. */
15520 if (!just_this_one_p)
15521 {
15522 struct buffer *current_base, *window_base;
15523 current_base = current_buffer;
15524 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15525 if (current_base->base_buffer)
15526 current_base = current_base->base_buffer;
15527 if (window_base->base_buffer)
15528 window_base = window_base->base_buffer;
15529 if (current_base == window_base)
15530 buffer_shared++;
15531 }
15532
15533 /* Point refers normally to the selected window. For any other
15534 window, set up appropriate value. */
15535 if (!EQ (window, selected_window))
15536 {
15537 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15538 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15539 if (new_pt < BEGV)
15540 {
15541 new_pt = BEGV;
15542 new_pt_byte = BEGV_BYTE;
15543 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15544 }
15545 else if (new_pt > (ZV - 1))
15546 {
15547 new_pt = ZV;
15548 new_pt_byte = ZV_BYTE;
15549 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15550 }
15551
15552 /* We don't use SET_PT so that the point-motion hooks don't run. */
15553 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15554 }
15555
15556 /* If any of the character widths specified in the display table
15557 have changed, invalidate the width run cache. It's true that
15558 this may be a bit late to catch such changes, but the rest of
15559 redisplay goes (non-fatally) haywire when the display table is
15560 changed, so why should we worry about doing any better? */
15561 if (current_buffer->width_run_cache)
15562 {
15563 struct Lisp_Char_Table *disptab = buffer_display_table ();
15564
15565 if (! disptab_matches_widthtab
15566 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15567 {
15568 invalidate_region_cache (current_buffer,
15569 current_buffer->width_run_cache,
15570 BEG, Z);
15571 recompute_width_table (current_buffer, disptab);
15572 }
15573 }
15574
15575 /* If window-start is screwed up, choose a new one. */
15576 if (XMARKER (w->start)->buffer != current_buffer)
15577 goto recenter;
15578
15579 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15580
15581 /* If someone specified a new starting point but did not insist,
15582 check whether it can be used. */
15583 if (w->optional_new_start
15584 && CHARPOS (startp) >= BEGV
15585 && CHARPOS (startp) <= ZV)
15586 {
15587 w->optional_new_start = 0;
15588 start_display (&it, w, startp);
15589 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15590 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15591 if (IT_CHARPOS (it) == PT)
15592 w->force_start = 1;
15593 /* IT may overshoot PT if text at PT is invisible. */
15594 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15595 w->force_start = 1;
15596 }
15597
15598 force_start:
15599
15600 /* Handle case where place to start displaying has been specified,
15601 unless the specified location is outside the accessible range. */
15602 if (w->force_start || w->frozen_window_start_p)
15603 {
15604 /* We set this later on if we have to adjust point. */
15605 int new_vpos = -1;
15606
15607 w->force_start = 0;
15608 w->vscroll = 0;
15609 wset_window_end_valid (w, Qnil);
15610
15611 /* Forget any recorded base line for line number display. */
15612 if (!buffer_unchanged_p)
15613 wset_base_line_number (w, Qnil);
15614
15615 /* Redisplay the mode line. Select the buffer properly for that.
15616 Also, run the hook window-scroll-functions
15617 because we have scrolled. */
15618 /* Note, we do this after clearing force_start because
15619 if there's an error, it is better to forget about force_start
15620 than to get into an infinite loop calling the hook functions
15621 and having them get more errors. */
15622 if (!update_mode_line
15623 || ! NILP (Vwindow_scroll_functions))
15624 {
15625 update_mode_line = 1;
15626 w->update_mode_line = 1;
15627 startp = run_window_scroll_functions (window, startp);
15628 }
15629
15630 w->last_modified = 0;
15631 w->last_overlay_modified = 0;
15632 if (CHARPOS (startp) < BEGV)
15633 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15634 else if (CHARPOS (startp) > ZV)
15635 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15636
15637 /* Redisplay, then check if cursor has been set during the
15638 redisplay. Give up if new fonts were loaded. */
15639 /* We used to issue a CHECK_MARGINS argument to try_window here,
15640 but this causes scrolling to fail when point begins inside
15641 the scroll margin (bug#148) -- cyd */
15642 if (!try_window (window, startp, 0))
15643 {
15644 w->force_start = 1;
15645 clear_glyph_matrix (w->desired_matrix);
15646 goto need_larger_matrices;
15647 }
15648
15649 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15650 {
15651 /* If point does not appear, try to move point so it does
15652 appear. The desired matrix has been built above, so we
15653 can use it here. */
15654 new_vpos = window_box_height (w) / 2;
15655 }
15656
15657 if (!cursor_row_fully_visible_p (w, 0, 0))
15658 {
15659 /* Point does appear, but on a line partly visible at end of window.
15660 Move it back to a fully-visible line. */
15661 new_vpos = window_box_height (w);
15662 }
15663
15664 /* If we need to move point for either of the above reasons,
15665 now actually do it. */
15666 if (new_vpos >= 0)
15667 {
15668 struct glyph_row *row;
15669
15670 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15671 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15672 ++row;
15673
15674 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15675 MATRIX_ROW_START_BYTEPOS (row));
15676
15677 if (w != XWINDOW (selected_window))
15678 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15679 else if (current_buffer == old)
15680 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15681
15682 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15683
15684 /* If we are highlighting the region, then we just changed
15685 the region, so redisplay to show it. */
15686 if (!NILP (Vtransient_mark_mode)
15687 && !NILP (BVAR (current_buffer, mark_active)))
15688 {
15689 clear_glyph_matrix (w->desired_matrix);
15690 if (!try_window (window, startp, 0))
15691 goto need_larger_matrices;
15692 }
15693 }
15694
15695 #ifdef GLYPH_DEBUG
15696 debug_method_add (w, "forced window start");
15697 #endif
15698 goto done;
15699 }
15700
15701 /* Handle case where text has not changed, only point, and it has
15702 not moved off the frame, and we are not retrying after hscroll.
15703 (current_matrix_up_to_date_p is nonzero when retrying.) */
15704 if (current_matrix_up_to_date_p
15705 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15706 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15707 {
15708 switch (rc)
15709 {
15710 case CURSOR_MOVEMENT_SUCCESS:
15711 used_current_matrix_p = 1;
15712 goto done;
15713
15714 case CURSOR_MOVEMENT_MUST_SCROLL:
15715 goto try_to_scroll;
15716
15717 default:
15718 emacs_abort ();
15719 }
15720 }
15721 /* If current starting point was originally the beginning of a line
15722 but no longer is, find a new starting point. */
15723 else if (w->start_at_line_beg
15724 && !(CHARPOS (startp) <= BEGV
15725 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15726 {
15727 #ifdef GLYPH_DEBUG
15728 debug_method_add (w, "recenter 1");
15729 #endif
15730 goto recenter;
15731 }
15732
15733 /* Try scrolling with try_window_id. Value is > 0 if update has
15734 been done, it is -1 if we know that the same window start will
15735 not work. It is 0 if unsuccessful for some other reason. */
15736 else if ((tem = try_window_id (w)) != 0)
15737 {
15738 #ifdef GLYPH_DEBUG
15739 debug_method_add (w, "try_window_id %d", tem);
15740 #endif
15741
15742 if (fonts_changed_p)
15743 goto need_larger_matrices;
15744 if (tem > 0)
15745 goto done;
15746
15747 /* Otherwise try_window_id has returned -1 which means that we
15748 don't want the alternative below this comment to execute. */
15749 }
15750 else if (CHARPOS (startp) >= BEGV
15751 && CHARPOS (startp) <= ZV
15752 && PT >= CHARPOS (startp)
15753 && (CHARPOS (startp) < ZV
15754 /* Avoid starting at end of buffer. */
15755 || CHARPOS (startp) == BEGV
15756 || (w->last_modified >= MODIFF
15757 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15758 {
15759 int d1, d2, d3, d4, d5, d6;
15760
15761 /* If first window line is a continuation line, and window start
15762 is inside the modified region, but the first change is before
15763 current window start, we must select a new window start.
15764
15765 However, if this is the result of a down-mouse event (e.g. by
15766 extending the mouse-drag-overlay), we don't want to select a
15767 new window start, since that would change the position under
15768 the mouse, resulting in an unwanted mouse-movement rather
15769 than a simple mouse-click. */
15770 if (!w->start_at_line_beg
15771 && NILP (do_mouse_tracking)
15772 && CHARPOS (startp) > BEGV
15773 && CHARPOS (startp) > BEG + beg_unchanged
15774 && CHARPOS (startp) <= Z - end_unchanged
15775 /* Even if w->start_at_line_beg is nil, a new window may
15776 start at a line_beg, since that's how set_buffer_window
15777 sets it. So, we need to check the return value of
15778 compute_window_start_on_continuation_line. (See also
15779 bug#197). */
15780 && XMARKER (w->start)->buffer == current_buffer
15781 && compute_window_start_on_continuation_line (w)
15782 /* It doesn't make sense to force the window start like we
15783 do at label force_start if it is already known that point
15784 will not be visible in the resulting window, because
15785 doing so will move point from its correct position
15786 instead of scrolling the window to bring point into view.
15787 See bug#9324. */
15788 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15789 {
15790 w->force_start = 1;
15791 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15792 goto force_start;
15793 }
15794
15795 #ifdef GLYPH_DEBUG
15796 debug_method_add (w, "same window start");
15797 #endif
15798
15799 /* Try to redisplay starting at same place as before.
15800 If point has not moved off frame, accept the results. */
15801 if (!current_matrix_up_to_date_p
15802 /* Don't use try_window_reusing_current_matrix in this case
15803 because a window scroll function can have changed the
15804 buffer. */
15805 || !NILP (Vwindow_scroll_functions)
15806 || MINI_WINDOW_P (w)
15807 || !(used_current_matrix_p
15808 = try_window_reusing_current_matrix (w)))
15809 {
15810 IF_DEBUG (debug_method_add (w, "1"));
15811 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15812 /* -1 means we need to scroll.
15813 0 means we need new matrices, but fonts_changed_p
15814 is set in that case, so we will detect it below. */
15815 goto try_to_scroll;
15816 }
15817
15818 if (fonts_changed_p)
15819 goto need_larger_matrices;
15820
15821 if (w->cursor.vpos >= 0)
15822 {
15823 if (!just_this_one_p
15824 || current_buffer->clip_changed
15825 || BEG_UNCHANGED < CHARPOS (startp))
15826 /* Forget any recorded base line for line number display. */
15827 wset_base_line_number (w, Qnil);
15828
15829 if (!cursor_row_fully_visible_p (w, 1, 0))
15830 {
15831 clear_glyph_matrix (w->desired_matrix);
15832 last_line_misfit = 1;
15833 }
15834 /* Drop through and scroll. */
15835 else
15836 goto done;
15837 }
15838 else
15839 clear_glyph_matrix (w->desired_matrix);
15840 }
15841
15842 try_to_scroll:
15843
15844 w->last_modified = 0;
15845 w->last_overlay_modified = 0;
15846
15847 /* Redisplay the mode line. Select the buffer properly for that. */
15848 if (!update_mode_line)
15849 {
15850 update_mode_line = 1;
15851 w->update_mode_line = 1;
15852 }
15853
15854 /* Try to scroll by specified few lines. */
15855 if ((scroll_conservatively
15856 || emacs_scroll_step
15857 || temp_scroll_step
15858 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15859 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15860 && CHARPOS (startp) >= BEGV
15861 && CHARPOS (startp) <= ZV)
15862 {
15863 /* The function returns -1 if new fonts were loaded, 1 if
15864 successful, 0 if not successful. */
15865 int ss = try_scrolling (window, just_this_one_p,
15866 scroll_conservatively,
15867 emacs_scroll_step,
15868 temp_scroll_step, last_line_misfit);
15869 switch (ss)
15870 {
15871 case SCROLLING_SUCCESS:
15872 goto done;
15873
15874 case SCROLLING_NEED_LARGER_MATRICES:
15875 goto need_larger_matrices;
15876
15877 case SCROLLING_FAILED:
15878 break;
15879
15880 default:
15881 emacs_abort ();
15882 }
15883 }
15884
15885 /* Finally, just choose a place to start which positions point
15886 according to user preferences. */
15887
15888 recenter:
15889
15890 #ifdef GLYPH_DEBUG
15891 debug_method_add (w, "recenter");
15892 #endif
15893
15894 /* w->vscroll = 0; */
15895
15896 /* Forget any previously recorded base line for line number display. */
15897 if (!buffer_unchanged_p)
15898 wset_base_line_number (w, Qnil);
15899
15900 /* Determine the window start relative to point. */
15901 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15902 it.current_y = it.last_visible_y;
15903 if (centering_position < 0)
15904 {
15905 int margin =
15906 scroll_margin > 0
15907 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15908 : 0;
15909 ptrdiff_t margin_pos = CHARPOS (startp);
15910 Lisp_Object aggressive;
15911 int scrolling_up;
15912
15913 /* If there is a scroll margin at the top of the window, find
15914 its character position. */
15915 if (margin
15916 /* Cannot call start_display if startp is not in the
15917 accessible region of the buffer. This can happen when we
15918 have just switched to a different buffer and/or changed
15919 its restriction. In that case, startp is initialized to
15920 the character position 1 (BEGV) because we did not yet
15921 have chance to display the buffer even once. */
15922 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15923 {
15924 struct it it1;
15925 void *it1data = NULL;
15926
15927 SAVE_IT (it1, it, it1data);
15928 start_display (&it1, w, startp);
15929 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15930 margin_pos = IT_CHARPOS (it1);
15931 RESTORE_IT (&it, &it, it1data);
15932 }
15933 scrolling_up = PT > margin_pos;
15934 aggressive =
15935 scrolling_up
15936 ? BVAR (current_buffer, scroll_up_aggressively)
15937 : BVAR (current_buffer, scroll_down_aggressively);
15938
15939 if (!MINI_WINDOW_P (w)
15940 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15941 {
15942 int pt_offset = 0;
15943
15944 /* Setting scroll-conservatively overrides
15945 scroll-*-aggressively. */
15946 if (!scroll_conservatively && NUMBERP (aggressive))
15947 {
15948 double float_amount = XFLOATINT (aggressive);
15949
15950 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15951 if (pt_offset == 0 && float_amount > 0)
15952 pt_offset = 1;
15953 if (pt_offset && margin > 0)
15954 margin -= 1;
15955 }
15956 /* Compute how much to move the window start backward from
15957 point so that point will be displayed where the user
15958 wants it. */
15959 if (scrolling_up)
15960 {
15961 centering_position = it.last_visible_y;
15962 if (pt_offset)
15963 centering_position -= pt_offset;
15964 centering_position -=
15965 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15966 + WINDOW_HEADER_LINE_HEIGHT (w);
15967 /* Don't let point enter the scroll margin near top of
15968 the window. */
15969 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15970 centering_position = margin * FRAME_LINE_HEIGHT (f);
15971 }
15972 else
15973 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15974 }
15975 else
15976 /* Set the window start half the height of the window backward
15977 from point. */
15978 centering_position = window_box_height (w) / 2;
15979 }
15980 move_it_vertically_backward (&it, centering_position);
15981
15982 eassert (IT_CHARPOS (it) >= BEGV);
15983
15984 /* The function move_it_vertically_backward may move over more
15985 than the specified y-distance. If it->w is small, e.g. a
15986 mini-buffer window, we may end up in front of the window's
15987 display area. Start displaying at the start of the line
15988 containing PT in this case. */
15989 if (it.current_y <= 0)
15990 {
15991 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15992 move_it_vertically_backward (&it, 0);
15993 it.current_y = 0;
15994 }
15995
15996 it.current_x = it.hpos = 0;
15997
15998 /* Set the window start position here explicitly, to avoid an
15999 infinite loop in case the functions in window-scroll-functions
16000 get errors. */
16001 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16002
16003 /* Run scroll hooks. */
16004 startp = run_window_scroll_functions (window, it.current.pos);
16005
16006 /* Redisplay the window. */
16007 if (!current_matrix_up_to_date_p
16008 || windows_or_buffers_changed
16009 || cursor_type_changed
16010 /* Don't use try_window_reusing_current_matrix in this case
16011 because it can have changed the buffer. */
16012 || !NILP (Vwindow_scroll_functions)
16013 || !just_this_one_p
16014 || MINI_WINDOW_P (w)
16015 || !(used_current_matrix_p
16016 = try_window_reusing_current_matrix (w)))
16017 try_window (window, startp, 0);
16018
16019 /* If new fonts have been loaded (due to fontsets), give up. We
16020 have to start a new redisplay since we need to re-adjust glyph
16021 matrices. */
16022 if (fonts_changed_p)
16023 goto need_larger_matrices;
16024
16025 /* If cursor did not appear assume that the middle of the window is
16026 in the first line of the window. Do it again with the next line.
16027 (Imagine a window of height 100, displaying two lines of height
16028 60. Moving back 50 from it->last_visible_y will end in the first
16029 line.) */
16030 if (w->cursor.vpos < 0)
16031 {
16032 if (!NILP (w->window_end_valid)
16033 && PT >= Z - XFASTINT (w->window_end_pos))
16034 {
16035 clear_glyph_matrix (w->desired_matrix);
16036 move_it_by_lines (&it, 1);
16037 try_window (window, it.current.pos, 0);
16038 }
16039 else if (PT < IT_CHARPOS (it))
16040 {
16041 clear_glyph_matrix (w->desired_matrix);
16042 move_it_by_lines (&it, -1);
16043 try_window (window, it.current.pos, 0);
16044 }
16045 else
16046 {
16047 /* Not much we can do about it. */
16048 }
16049 }
16050
16051 /* Consider the following case: Window starts at BEGV, there is
16052 invisible, intangible text at BEGV, so that display starts at
16053 some point START > BEGV. It can happen that we are called with
16054 PT somewhere between BEGV and START. Try to handle that case. */
16055 if (w->cursor.vpos < 0)
16056 {
16057 struct glyph_row *row = w->current_matrix->rows;
16058 if (row->mode_line_p)
16059 ++row;
16060 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16061 }
16062
16063 if (!cursor_row_fully_visible_p (w, 0, 0))
16064 {
16065 /* If vscroll is enabled, disable it and try again. */
16066 if (w->vscroll)
16067 {
16068 w->vscroll = 0;
16069 clear_glyph_matrix (w->desired_matrix);
16070 goto recenter;
16071 }
16072
16073 /* Users who set scroll-conservatively to a large number want
16074 point just above/below the scroll margin. If we ended up
16075 with point's row partially visible, move the window start to
16076 make that row fully visible and out of the margin. */
16077 if (scroll_conservatively > SCROLL_LIMIT)
16078 {
16079 int margin =
16080 scroll_margin > 0
16081 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16082 : 0;
16083 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16084
16085 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16086 clear_glyph_matrix (w->desired_matrix);
16087 if (1 == try_window (window, it.current.pos,
16088 TRY_WINDOW_CHECK_MARGINS))
16089 goto done;
16090 }
16091
16092 /* If centering point failed to make the whole line visible,
16093 put point at the top instead. That has to make the whole line
16094 visible, if it can be done. */
16095 if (centering_position == 0)
16096 goto done;
16097
16098 clear_glyph_matrix (w->desired_matrix);
16099 centering_position = 0;
16100 goto recenter;
16101 }
16102
16103 done:
16104
16105 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16106 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16107 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16108
16109 /* Display the mode line, if we must. */
16110 if ((update_mode_line
16111 /* If window not full width, must redo its mode line
16112 if (a) the window to its side is being redone and
16113 (b) we do a frame-based redisplay. This is a consequence
16114 of how inverted lines are drawn in frame-based redisplay. */
16115 || (!just_this_one_p
16116 && !FRAME_WINDOW_P (f)
16117 && !WINDOW_FULL_WIDTH_P (w))
16118 /* Line number to display. */
16119 || INTEGERP (w->base_line_pos)
16120 /* Column number is displayed and different from the one displayed. */
16121 || (!NILP (w->column_number_displayed)
16122 && (XFASTINT (w->column_number_displayed) != current_column ())))
16123 /* This means that the window has a mode line. */
16124 && (WINDOW_WANTS_MODELINE_P (w)
16125 || WINDOW_WANTS_HEADER_LINE_P (w)))
16126 {
16127 display_mode_lines (w);
16128
16129 /* If mode line height has changed, arrange for a thorough
16130 immediate redisplay using the correct mode line height. */
16131 if (WINDOW_WANTS_MODELINE_P (w)
16132 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16133 {
16134 fonts_changed_p = 1;
16135 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16136 = DESIRED_MODE_LINE_HEIGHT (w);
16137 }
16138
16139 /* If header line height has changed, arrange for a thorough
16140 immediate redisplay using the correct header line height. */
16141 if (WINDOW_WANTS_HEADER_LINE_P (w)
16142 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16143 {
16144 fonts_changed_p = 1;
16145 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16146 = DESIRED_HEADER_LINE_HEIGHT (w);
16147 }
16148
16149 if (fonts_changed_p)
16150 goto need_larger_matrices;
16151 }
16152
16153 if (!line_number_displayed
16154 && !BUFFERP (w->base_line_pos))
16155 {
16156 wset_base_line_pos (w, Qnil);
16157 wset_base_line_number (w, Qnil);
16158 }
16159
16160 finish_menu_bars:
16161
16162 /* When we reach a frame's selected window, redo the frame's menu bar. */
16163 if (update_mode_line
16164 && EQ (FRAME_SELECTED_WINDOW (f), window))
16165 {
16166 int redisplay_menu_p = 0;
16167
16168 if (FRAME_WINDOW_P (f))
16169 {
16170 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16171 || defined (HAVE_NS) || defined (USE_GTK)
16172 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16173 #else
16174 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16175 #endif
16176 }
16177 else
16178 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16179
16180 if (redisplay_menu_p)
16181 display_menu_bar (w);
16182
16183 #ifdef HAVE_WINDOW_SYSTEM
16184 if (FRAME_WINDOW_P (f))
16185 {
16186 #if defined (USE_GTK) || defined (HAVE_NS)
16187 if (FRAME_EXTERNAL_TOOL_BAR (f))
16188 redisplay_tool_bar (f);
16189 #else
16190 if (WINDOWP (f->tool_bar_window)
16191 && (FRAME_TOOL_BAR_LINES (f) > 0
16192 || !NILP (Vauto_resize_tool_bars))
16193 && redisplay_tool_bar (f))
16194 ignore_mouse_drag_p = 1;
16195 #endif
16196 }
16197 #endif
16198 }
16199
16200 #ifdef HAVE_WINDOW_SYSTEM
16201 if (FRAME_WINDOW_P (f)
16202 && update_window_fringes (w, (just_this_one_p
16203 || (!used_current_matrix_p && !overlay_arrow_seen)
16204 || w->pseudo_window_p)))
16205 {
16206 update_begin (f);
16207 BLOCK_INPUT;
16208 if (draw_window_fringes (w, 1))
16209 x_draw_vertical_border (w);
16210 UNBLOCK_INPUT;
16211 update_end (f);
16212 }
16213 #endif /* HAVE_WINDOW_SYSTEM */
16214
16215 /* We go to this label, with fonts_changed_p set,
16216 if it is necessary to try again using larger glyph matrices.
16217 We have to redeem the scroll bar even in this case,
16218 because the loop in redisplay_internal expects that. */
16219 need_larger_matrices:
16220 ;
16221 finish_scroll_bars:
16222
16223 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16224 {
16225 /* Set the thumb's position and size. */
16226 set_vertical_scroll_bar (w);
16227
16228 /* Note that we actually used the scroll bar attached to this
16229 window, so it shouldn't be deleted at the end of redisplay. */
16230 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16231 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16232 }
16233
16234 /* Restore current_buffer and value of point in it. The window
16235 update may have changed the buffer, so first make sure `opoint'
16236 is still valid (Bug#6177). */
16237 if (CHARPOS (opoint) < BEGV)
16238 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16239 else if (CHARPOS (opoint) > ZV)
16240 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16241 else
16242 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16243
16244 set_buffer_internal_1 (old);
16245 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16246 shorter. This can be caused by log truncation in *Messages*. */
16247 if (CHARPOS (lpoint) <= ZV)
16248 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16249
16250 unbind_to (count, Qnil);
16251 }
16252
16253
16254 /* Build the complete desired matrix of WINDOW with a window start
16255 buffer position POS.
16256
16257 Value is 1 if successful. It is zero if fonts were loaded during
16258 redisplay which makes re-adjusting glyph matrices necessary, and -1
16259 if point would appear in the scroll margins.
16260 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16261 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16262 set in FLAGS.) */
16263
16264 int
16265 try_window (Lisp_Object window, struct text_pos pos, int flags)
16266 {
16267 struct window *w = XWINDOW (window);
16268 struct it it;
16269 struct glyph_row *last_text_row = NULL;
16270 struct frame *f = XFRAME (w->frame);
16271
16272 /* Make POS the new window start. */
16273 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16274
16275 /* Mark cursor position as unknown. No overlay arrow seen. */
16276 w->cursor.vpos = -1;
16277 overlay_arrow_seen = 0;
16278
16279 /* Initialize iterator and info to start at POS. */
16280 start_display (&it, w, pos);
16281
16282 /* Display all lines of W. */
16283 while (it.current_y < it.last_visible_y)
16284 {
16285 if (display_line (&it))
16286 last_text_row = it.glyph_row - 1;
16287 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16288 return 0;
16289 }
16290
16291 /* Don't let the cursor end in the scroll margins. */
16292 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16293 && !MINI_WINDOW_P (w))
16294 {
16295 int this_scroll_margin;
16296
16297 if (scroll_margin > 0)
16298 {
16299 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16300 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16301 }
16302 else
16303 this_scroll_margin = 0;
16304
16305 if ((w->cursor.y >= 0 /* not vscrolled */
16306 && w->cursor.y < this_scroll_margin
16307 && CHARPOS (pos) > BEGV
16308 && IT_CHARPOS (it) < ZV)
16309 /* rms: considering make_cursor_line_fully_visible_p here
16310 seems to give wrong results. We don't want to recenter
16311 when the last line is partly visible, we want to allow
16312 that case to be handled in the usual way. */
16313 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16314 {
16315 w->cursor.vpos = -1;
16316 clear_glyph_matrix (w->desired_matrix);
16317 return -1;
16318 }
16319 }
16320
16321 /* If bottom moved off end of frame, change mode line percentage. */
16322 if (XFASTINT (w->window_end_pos) <= 0
16323 && Z != IT_CHARPOS (it))
16324 w->update_mode_line = 1;
16325
16326 /* Set window_end_pos to the offset of the last character displayed
16327 on the window from the end of current_buffer. Set
16328 window_end_vpos to its row number. */
16329 if (last_text_row)
16330 {
16331 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16332 w->window_end_bytepos
16333 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16334 wset_window_end_pos
16335 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16336 wset_window_end_vpos
16337 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16338 eassert
16339 (MATRIX_ROW (w->desired_matrix,
16340 XFASTINT (w->window_end_vpos))->displays_text_p);
16341 }
16342 else
16343 {
16344 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16345 wset_window_end_pos (w, make_number (Z - ZV));
16346 wset_window_end_vpos (w, make_number (0));
16347 }
16348
16349 /* But that is not valid info until redisplay finishes. */
16350 wset_window_end_valid (w, Qnil);
16351 return 1;
16352 }
16353
16354
16355 \f
16356 /************************************************************************
16357 Window redisplay reusing current matrix when buffer has not changed
16358 ************************************************************************/
16359
16360 /* Try redisplay of window W showing an unchanged buffer with a
16361 different window start than the last time it was displayed by
16362 reusing its current matrix. Value is non-zero if successful.
16363 W->start is the new window start. */
16364
16365 static int
16366 try_window_reusing_current_matrix (struct window *w)
16367 {
16368 struct frame *f = XFRAME (w->frame);
16369 struct glyph_row *bottom_row;
16370 struct it it;
16371 struct run run;
16372 struct text_pos start, new_start;
16373 int nrows_scrolled, i;
16374 struct glyph_row *last_text_row;
16375 struct glyph_row *last_reused_text_row;
16376 struct glyph_row *start_row;
16377 int start_vpos, min_y, max_y;
16378
16379 #ifdef GLYPH_DEBUG
16380 if (inhibit_try_window_reusing)
16381 return 0;
16382 #endif
16383
16384 if (/* This function doesn't handle terminal frames. */
16385 !FRAME_WINDOW_P (f)
16386 /* Don't try to reuse the display if windows have been split
16387 or such. */
16388 || windows_or_buffers_changed
16389 || cursor_type_changed)
16390 return 0;
16391
16392 /* Can't do this if region may have changed. */
16393 if ((!NILP (Vtransient_mark_mode)
16394 && !NILP (BVAR (current_buffer, mark_active)))
16395 || !NILP (w->region_showing)
16396 || !NILP (Vshow_trailing_whitespace))
16397 return 0;
16398
16399 /* If top-line visibility has changed, give up. */
16400 if (WINDOW_WANTS_HEADER_LINE_P (w)
16401 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16402 return 0;
16403
16404 /* Give up if old or new display is scrolled vertically. We could
16405 make this function handle this, but right now it doesn't. */
16406 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16407 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16408 return 0;
16409
16410 /* The variable new_start now holds the new window start. The old
16411 start `start' can be determined from the current matrix. */
16412 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16413 start = start_row->minpos;
16414 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16415
16416 /* Clear the desired matrix for the display below. */
16417 clear_glyph_matrix (w->desired_matrix);
16418
16419 if (CHARPOS (new_start) <= CHARPOS (start))
16420 {
16421 /* Don't use this method if the display starts with an ellipsis
16422 displayed for invisible text. It's not easy to handle that case
16423 below, and it's certainly not worth the effort since this is
16424 not a frequent case. */
16425 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16426 return 0;
16427
16428 IF_DEBUG (debug_method_add (w, "twu1"));
16429
16430 /* Display up to a row that can be reused. The variable
16431 last_text_row is set to the last row displayed that displays
16432 text. Note that it.vpos == 0 if or if not there is a
16433 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16434 start_display (&it, w, new_start);
16435 w->cursor.vpos = -1;
16436 last_text_row = last_reused_text_row = NULL;
16437
16438 while (it.current_y < it.last_visible_y
16439 && !fonts_changed_p)
16440 {
16441 /* If we have reached into the characters in the START row,
16442 that means the line boundaries have changed. So we
16443 can't start copying with the row START. Maybe it will
16444 work to start copying with the following row. */
16445 while (IT_CHARPOS (it) > CHARPOS (start))
16446 {
16447 /* Advance to the next row as the "start". */
16448 start_row++;
16449 start = start_row->minpos;
16450 /* If there are no more rows to try, or just one, give up. */
16451 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16452 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16453 || CHARPOS (start) == ZV)
16454 {
16455 clear_glyph_matrix (w->desired_matrix);
16456 return 0;
16457 }
16458
16459 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16460 }
16461 /* If we have reached alignment, we can copy the rest of the
16462 rows. */
16463 if (IT_CHARPOS (it) == CHARPOS (start)
16464 /* Don't accept "alignment" inside a display vector,
16465 since start_row could have started in the middle of
16466 that same display vector (thus their character
16467 positions match), and we have no way of telling if
16468 that is the case. */
16469 && it.current.dpvec_index < 0)
16470 break;
16471
16472 if (display_line (&it))
16473 last_text_row = it.glyph_row - 1;
16474
16475 }
16476
16477 /* A value of current_y < last_visible_y means that we stopped
16478 at the previous window start, which in turn means that we
16479 have at least one reusable row. */
16480 if (it.current_y < it.last_visible_y)
16481 {
16482 struct glyph_row *row;
16483
16484 /* IT.vpos always starts from 0; it counts text lines. */
16485 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16486
16487 /* Find PT if not already found in the lines displayed. */
16488 if (w->cursor.vpos < 0)
16489 {
16490 int dy = it.current_y - start_row->y;
16491
16492 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16493 row = row_containing_pos (w, PT, row, NULL, dy);
16494 if (row)
16495 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16496 dy, nrows_scrolled);
16497 else
16498 {
16499 clear_glyph_matrix (w->desired_matrix);
16500 return 0;
16501 }
16502 }
16503
16504 /* Scroll the display. Do it before the current matrix is
16505 changed. The problem here is that update has not yet
16506 run, i.e. part of the current matrix is not up to date.
16507 scroll_run_hook will clear the cursor, and use the
16508 current matrix to get the height of the row the cursor is
16509 in. */
16510 run.current_y = start_row->y;
16511 run.desired_y = it.current_y;
16512 run.height = it.last_visible_y - it.current_y;
16513
16514 if (run.height > 0 && run.current_y != run.desired_y)
16515 {
16516 update_begin (f);
16517 FRAME_RIF (f)->update_window_begin_hook (w);
16518 FRAME_RIF (f)->clear_window_mouse_face (w);
16519 FRAME_RIF (f)->scroll_run_hook (w, &run);
16520 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16521 update_end (f);
16522 }
16523
16524 /* Shift current matrix down by nrows_scrolled lines. */
16525 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16526 rotate_matrix (w->current_matrix,
16527 start_vpos,
16528 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16529 nrows_scrolled);
16530
16531 /* Disable lines that must be updated. */
16532 for (i = 0; i < nrows_scrolled; ++i)
16533 (start_row + i)->enabled_p = 0;
16534
16535 /* Re-compute Y positions. */
16536 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16537 max_y = it.last_visible_y;
16538 for (row = start_row + nrows_scrolled;
16539 row < bottom_row;
16540 ++row)
16541 {
16542 row->y = it.current_y;
16543 row->visible_height = row->height;
16544
16545 if (row->y < min_y)
16546 row->visible_height -= min_y - row->y;
16547 if (row->y + row->height > max_y)
16548 row->visible_height -= row->y + row->height - max_y;
16549 if (row->fringe_bitmap_periodic_p)
16550 row->redraw_fringe_bitmaps_p = 1;
16551
16552 it.current_y += row->height;
16553
16554 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16555 last_reused_text_row = row;
16556 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16557 break;
16558 }
16559
16560 /* Disable lines in the current matrix which are now
16561 below the window. */
16562 for (++row; row < bottom_row; ++row)
16563 row->enabled_p = row->mode_line_p = 0;
16564 }
16565
16566 /* Update window_end_pos etc.; last_reused_text_row is the last
16567 reused row from the current matrix containing text, if any.
16568 The value of last_text_row is the last displayed line
16569 containing text. */
16570 if (last_reused_text_row)
16571 {
16572 w->window_end_bytepos
16573 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16574 wset_window_end_pos
16575 (w, make_number (Z
16576 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16577 wset_window_end_vpos
16578 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16579 w->current_matrix)));
16580 }
16581 else if (last_text_row)
16582 {
16583 w->window_end_bytepos
16584 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16585 wset_window_end_pos
16586 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16587 wset_window_end_vpos
16588 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16589 w->desired_matrix)));
16590 }
16591 else
16592 {
16593 /* This window must be completely empty. */
16594 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16595 wset_window_end_pos (w, make_number (Z - ZV));
16596 wset_window_end_vpos (w, make_number (0));
16597 }
16598 wset_window_end_valid (w, Qnil);
16599
16600 /* Update hint: don't try scrolling again in update_window. */
16601 w->desired_matrix->no_scrolling_p = 1;
16602
16603 #ifdef GLYPH_DEBUG
16604 debug_method_add (w, "try_window_reusing_current_matrix 1");
16605 #endif
16606 return 1;
16607 }
16608 else if (CHARPOS (new_start) > CHARPOS (start))
16609 {
16610 struct glyph_row *pt_row, *row;
16611 struct glyph_row *first_reusable_row;
16612 struct glyph_row *first_row_to_display;
16613 int dy;
16614 int yb = window_text_bottom_y (w);
16615
16616 /* Find the row starting at new_start, if there is one. Don't
16617 reuse a partially visible line at the end. */
16618 first_reusable_row = start_row;
16619 while (first_reusable_row->enabled_p
16620 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16621 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16622 < CHARPOS (new_start)))
16623 ++first_reusable_row;
16624
16625 /* Give up if there is no row to reuse. */
16626 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16627 || !first_reusable_row->enabled_p
16628 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16629 != CHARPOS (new_start)))
16630 return 0;
16631
16632 /* We can reuse fully visible rows beginning with
16633 first_reusable_row to the end of the window. Set
16634 first_row_to_display to the first row that cannot be reused.
16635 Set pt_row to the row containing point, if there is any. */
16636 pt_row = NULL;
16637 for (first_row_to_display = first_reusable_row;
16638 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16639 ++first_row_to_display)
16640 {
16641 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16642 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16643 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16644 && first_row_to_display->ends_at_zv_p
16645 && pt_row == NULL)))
16646 pt_row = first_row_to_display;
16647 }
16648
16649 /* Start displaying at the start of first_row_to_display. */
16650 eassert (first_row_to_display->y < yb);
16651 init_to_row_start (&it, w, first_row_to_display);
16652
16653 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16654 - start_vpos);
16655 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16656 - nrows_scrolled);
16657 it.current_y = (first_row_to_display->y - first_reusable_row->y
16658 + WINDOW_HEADER_LINE_HEIGHT (w));
16659
16660 /* Display lines beginning with first_row_to_display in the
16661 desired matrix. Set last_text_row to the last row displayed
16662 that displays text. */
16663 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16664 if (pt_row == NULL)
16665 w->cursor.vpos = -1;
16666 last_text_row = NULL;
16667 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16668 if (display_line (&it))
16669 last_text_row = it.glyph_row - 1;
16670
16671 /* If point is in a reused row, adjust y and vpos of the cursor
16672 position. */
16673 if (pt_row)
16674 {
16675 w->cursor.vpos -= nrows_scrolled;
16676 w->cursor.y -= first_reusable_row->y - start_row->y;
16677 }
16678
16679 /* Give up if point isn't in a row displayed or reused. (This
16680 also handles the case where w->cursor.vpos < nrows_scrolled
16681 after the calls to display_line, which can happen with scroll
16682 margins. See bug#1295.) */
16683 if (w->cursor.vpos < 0)
16684 {
16685 clear_glyph_matrix (w->desired_matrix);
16686 return 0;
16687 }
16688
16689 /* Scroll the display. */
16690 run.current_y = first_reusable_row->y;
16691 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16692 run.height = it.last_visible_y - run.current_y;
16693 dy = run.current_y - run.desired_y;
16694
16695 if (run.height)
16696 {
16697 update_begin (f);
16698 FRAME_RIF (f)->update_window_begin_hook (w);
16699 FRAME_RIF (f)->clear_window_mouse_face (w);
16700 FRAME_RIF (f)->scroll_run_hook (w, &run);
16701 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16702 update_end (f);
16703 }
16704
16705 /* Adjust Y positions of reused rows. */
16706 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16707 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16708 max_y = it.last_visible_y;
16709 for (row = first_reusable_row; row < first_row_to_display; ++row)
16710 {
16711 row->y -= dy;
16712 row->visible_height = row->height;
16713 if (row->y < min_y)
16714 row->visible_height -= min_y - row->y;
16715 if (row->y + row->height > max_y)
16716 row->visible_height -= row->y + row->height - max_y;
16717 if (row->fringe_bitmap_periodic_p)
16718 row->redraw_fringe_bitmaps_p = 1;
16719 }
16720
16721 /* Scroll the current matrix. */
16722 eassert (nrows_scrolled > 0);
16723 rotate_matrix (w->current_matrix,
16724 start_vpos,
16725 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16726 -nrows_scrolled);
16727
16728 /* Disable rows not reused. */
16729 for (row -= nrows_scrolled; row < bottom_row; ++row)
16730 row->enabled_p = 0;
16731
16732 /* Point may have moved to a different line, so we cannot assume that
16733 the previous cursor position is valid; locate the correct row. */
16734 if (pt_row)
16735 {
16736 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16737 row < bottom_row
16738 && PT >= MATRIX_ROW_END_CHARPOS (row)
16739 && !row->ends_at_zv_p;
16740 row++)
16741 {
16742 w->cursor.vpos++;
16743 w->cursor.y = row->y;
16744 }
16745 if (row < bottom_row)
16746 {
16747 /* Can't simply scan the row for point with
16748 bidi-reordered glyph rows. Let set_cursor_from_row
16749 figure out where to put the cursor, and if it fails,
16750 give up. */
16751 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16752 {
16753 if (!set_cursor_from_row (w, row, w->current_matrix,
16754 0, 0, 0, 0))
16755 {
16756 clear_glyph_matrix (w->desired_matrix);
16757 return 0;
16758 }
16759 }
16760 else
16761 {
16762 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16763 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16764
16765 for (; glyph < end
16766 && (!BUFFERP (glyph->object)
16767 || glyph->charpos < PT);
16768 glyph++)
16769 {
16770 w->cursor.hpos++;
16771 w->cursor.x += glyph->pixel_width;
16772 }
16773 }
16774 }
16775 }
16776
16777 /* Adjust window end. A null value of last_text_row means that
16778 the window end is in reused rows which in turn means that
16779 only its vpos can have changed. */
16780 if (last_text_row)
16781 {
16782 w->window_end_bytepos
16783 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16784 wset_window_end_pos
16785 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16786 wset_window_end_vpos
16787 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16788 w->desired_matrix)));
16789 }
16790 else
16791 {
16792 wset_window_end_vpos
16793 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16794 }
16795
16796 wset_window_end_valid (w, Qnil);
16797 w->desired_matrix->no_scrolling_p = 1;
16798
16799 #ifdef GLYPH_DEBUG
16800 debug_method_add (w, "try_window_reusing_current_matrix 2");
16801 #endif
16802 return 1;
16803 }
16804
16805 return 0;
16806 }
16807
16808
16809 \f
16810 /************************************************************************
16811 Window redisplay reusing current matrix when buffer has changed
16812 ************************************************************************/
16813
16814 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16815 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16816 ptrdiff_t *, ptrdiff_t *);
16817 static struct glyph_row *
16818 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16819 struct glyph_row *);
16820
16821
16822 /* Return the last row in MATRIX displaying text. If row START is
16823 non-null, start searching with that row. IT gives the dimensions
16824 of the display. Value is null if matrix is empty; otherwise it is
16825 a pointer to the row found. */
16826
16827 static struct glyph_row *
16828 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16829 struct glyph_row *start)
16830 {
16831 struct glyph_row *row, *row_found;
16832
16833 /* Set row_found to the last row in IT->w's current matrix
16834 displaying text. The loop looks funny but think of partially
16835 visible lines. */
16836 row_found = NULL;
16837 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16838 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16839 {
16840 eassert (row->enabled_p);
16841 row_found = row;
16842 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16843 break;
16844 ++row;
16845 }
16846
16847 return row_found;
16848 }
16849
16850
16851 /* Return the last row in the current matrix of W that is not affected
16852 by changes at the start of current_buffer that occurred since W's
16853 current matrix was built. Value is null if no such row exists.
16854
16855 BEG_UNCHANGED us the number of characters unchanged at the start of
16856 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16857 first changed character in current_buffer. Characters at positions <
16858 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16859 when the current matrix was built. */
16860
16861 static struct glyph_row *
16862 find_last_unchanged_at_beg_row (struct window *w)
16863 {
16864 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16865 struct glyph_row *row;
16866 struct glyph_row *row_found = NULL;
16867 int yb = window_text_bottom_y (w);
16868
16869 /* Find the last row displaying unchanged text. */
16870 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16871 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16872 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16873 ++row)
16874 {
16875 if (/* If row ends before first_changed_pos, it is unchanged,
16876 except in some case. */
16877 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16878 /* When row ends in ZV and we write at ZV it is not
16879 unchanged. */
16880 && !row->ends_at_zv_p
16881 /* When first_changed_pos is the end of a continued line,
16882 row is not unchanged because it may be no longer
16883 continued. */
16884 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16885 && (row->continued_p
16886 || row->exact_window_width_line_p))
16887 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16888 needs to be recomputed, so don't consider this row as
16889 unchanged. This happens when the last line was
16890 bidi-reordered and was killed immediately before this
16891 redisplay cycle. In that case, ROW->end stores the
16892 buffer position of the first visual-order character of
16893 the killed text, which is now beyond ZV. */
16894 && CHARPOS (row->end.pos) <= ZV)
16895 row_found = row;
16896
16897 /* Stop if last visible row. */
16898 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16899 break;
16900 }
16901
16902 return row_found;
16903 }
16904
16905
16906 /* Find the first glyph row in the current matrix of W that is not
16907 affected by changes at the end of current_buffer since the
16908 time W's current matrix was built.
16909
16910 Return in *DELTA the number of chars by which buffer positions in
16911 unchanged text at the end of current_buffer must be adjusted.
16912
16913 Return in *DELTA_BYTES the corresponding number of bytes.
16914
16915 Value is null if no such row exists, i.e. all rows are affected by
16916 changes. */
16917
16918 static struct glyph_row *
16919 find_first_unchanged_at_end_row (struct window *w,
16920 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16921 {
16922 struct glyph_row *row;
16923 struct glyph_row *row_found = NULL;
16924
16925 *delta = *delta_bytes = 0;
16926
16927 /* Display must not have been paused, otherwise the current matrix
16928 is not up to date. */
16929 eassert (!NILP (w->window_end_valid));
16930
16931 /* A value of window_end_pos >= END_UNCHANGED means that the window
16932 end is in the range of changed text. If so, there is no
16933 unchanged row at the end of W's current matrix. */
16934 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16935 return NULL;
16936
16937 /* Set row to the last row in W's current matrix displaying text. */
16938 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16939
16940 /* If matrix is entirely empty, no unchanged row exists. */
16941 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16942 {
16943 /* The value of row is the last glyph row in the matrix having a
16944 meaningful buffer position in it. The end position of row
16945 corresponds to window_end_pos. This allows us to translate
16946 buffer positions in the current matrix to current buffer
16947 positions for characters not in changed text. */
16948 ptrdiff_t Z_old =
16949 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16950 ptrdiff_t Z_BYTE_old =
16951 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16952 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16953 struct glyph_row *first_text_row
16954 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16955
16956 *delta = Z - Z_old;
16957 *delta_bytes = Z_BYTE - Z_BYTE_old;
16958
16959 /* Set last_unchanged_pos to the buffer position of the last
16960 character in the buffer that has not been changed. Z is the
16961 index + 1 of the last character in current_buffer, i.e. by
16962 subtracting END_UNCHANGED we get the index of the last
16963 unchanged character, and we have to add BEG to get its buffer
16964 position. */
16965 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16966 last_unchanged_pos_old = last_unchanged_pos - *delta;
16967
16968 /* Search backward from ROW for a row displaying a line that
16969 starts at a minimum position >= last_unchanged_pos_old. */
16970 for (; row > first_text_row; --row)
16971 {
16972 /* This used to abort, but it can happen.
16973 It is ok to just stop the search instead here. KFS. */
16974 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16975 break;
16976
16977 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16978 row_found = row;
16979 }
16980 }
16981
16982 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16983
16984 return row_found;
16985 }
16986
16987
16988 /* Make sure that glyph rows in the current matrix of window W
16989 reference the same glyph memory as corresponding rows in the
16990 frame's frame matrix. This function is called after scrolling W's
16991 current matrix on a terminal frame in try_window_id and
16992 try_window_reusing_current_matrix. */
16993
16994 static void
16995 sync_frame_with_window_matrix_rows (struct window *w)
16996 {
16997 struct frame *f = XFRAME (w->frame);
16998 struct glyph_row *window_row, *window_row_end, *frame_row;
16999
17000 /* Preconditions: W must be a leaf window and full-width. Its frame
17001 must have a frame matrix. */
17002 eassert (NILP (w->hchild) && NILP (w->vchild));
17003 eassert (WINDOW_FULL_WIDTH_P (w));
17004 eassert (!FRAME_WINDOW_P (f));
17005
17006 /* If W is a full-width window, glyph pointers in W's current matrix
17007 have, by definition, to be the same as glyph pointers in the
17008 corresponding frame matrix. Note that frame matrices have no
17009 marginal areas (see build_frame_matrix). */
17010 window_row = w->current_matrix->rows;
17011 window_row_end = window_row + w->current_matrix->nrows;
17012 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17013 while (window_row < window_row_end)
17014 {
17015 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17016 struct glyph *end = window_row->glyphs[LAST_AREA];
17017
17018 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17019 frame_row->glyphs[TEXT_AREA] = start;
17020 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17021 frame_row->glyphs[LAST_AREA] = end;
17022
17023 /* Disable frame rows whose corresponding window rows have
17024 been disabled in try_window_id. */
17025 if (!window_row->enabled_p)
17026 frame_row->enabled_p = 0;
17027
17028 ++window_row, ++frame_row;
17029 }
17030 }
17031
17032
17033 /* Find the glyph row in window W containing CHARPOS. Consider all
17034 rows between START and END (not inclusive). END null means search
17035 all rows to the end of the display area of W. Value is the row
17036 containing CHARPOS or null. */
17037
17038 struct glyph_row *
17039 row_containing_pos (struct window *w, ptrdiff_t charpos,
17040 struct glyph_row *start, struct glyph_row *end, int dy)
17041 {
17042 struct glyph_row *row = start;
17043 struct glyph_row *best_row = NULL;
17044 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
17045 int last_y;
17046
17047 /* If we happen to start on a header-line, skip that. */
17048 if (row->mode_line_p)
17049 ++row;
17050
17051 if ((end && row >= end) || !row->enabled_p)
17052 return NULL;
17053
17054 last_y = window_text_bottom_y (w) - dy;
17055
17056 while (1)
17057 {
17058 /* Give up if we have gone too far. */
17059 if (end && row >= end)
17060 return NULL;
17061 /* This formerly returned if they were equal.
17062 I think that both quantities are of a "last plus one" type;
17063 if so, when they are equal, the row is within the screen. -- rms. */
17064 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17065 return NULL;
17066
17067 /* If it is in this row, return this row. */
17068 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17069 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17070 /* The end position of a row equals the start
17071 position of the next row. If CHARPOS is there, we
17072 would rather display it in the next line, except
17073 when this line ends in ZV. */
17074 && !row->ends_at_zv_p
17075 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17076 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17077 {
17078 struct glyph *g;
17079
17080 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17081 || (!best_row && !row->continued_p))
17082 return row;
17083 /* In bidi-reordered rows, there could be several rows
17084 occluding point, all of them belonging to the same
17085 continued line. We need to find the row which fits
17086 CHARPOS the best. */
17087 for (g = row->glyphs[TEXT_AREA];
17088 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17089 g++)
17090 {
17091 if (!STRINGP (g->object))
17092 {
17093 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17094 {
17095 mindif = eabs (g->charpos - charpos);
17096 best_row = row;
17097 /* Exact match always wins. */
17098 if (mindif == 0)
17099 return best_row;
17100 }
17101 }
17102 }
17103 }
17104 else if (best_row && !row->continued_p)
17105 return best_row;
17106 ++row;
17107 }
17108 }
17109
17110
17111 /* Try to redisplay window W by reusing its existing display. W's
17112 current matrix must be up to date when this function is called,
17113 i.e. window_end_valid must not be nil.
17114
17115 Value is
17116
17117 1 if display has been updated
17118 0 if otherwise unsuccessful
17119 -1 if redisplay with same window start is known not to succeed
17120
17121 The following steps are performed:
17122
17123 1. Find the last row in the current matrix of W that is not
17124 affected by changes at the start of current_buffer. If no such row
17125 is found, give up.
17126
17127 2. Find the first row in W's current matrix that is not affected by
17128 changes at the end of current_buffer. Maybe there is no such row.
17129
17130 3. Display lines beginning with the row + 1 found in step 1 to the
17131 row found in step 2 or, if step 2 didn't find a row, to the end of
17132 the window.
17133
17134 4. If cursor is not known to appear on the window, give up.
17135
17136 5. If display stopped at the row found in step 2, scroll the
17137 display and current matrix as needed.
17138
17139 6. Maybe display some lines at the end of W, if we must. This can
17140 happen under various circumstances, like a partially visible line
17141 becoming fully visible, or because newly displayed lines are displayed
17142 in smaller font sizes.
17143
17144 7. Update W's window end information. */
17145
17146 static int
17147 try_window_id (struct window *w)
17148 {
17149 struct frame *f = XFRAME (w->frame);
17150 struct glyph_matrix *current_matrix = w->current_matrix;
17151 struct glyph_matrix *desired_matrix = w->desired_matrix;
17152 struct glyph_row *last_unchanged_at_beg_row;
17153 struct glyph_row *first_unchanged_at_end_row;
17154 struct glyph_row *row;
17155 struct glyph_row *bottom_row;
17156 int bottom_vpos;
17157 struct it it;
17158 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17159 int dvpos, dy;
17160 struct text_pos start_pos;
17161 struct run run;
17162 int first_unchanged_at_end_vpos = 0;
17163 struct glyph_row *last_text_row, *last_text_row_at_end;
17164 struct text_pos start;
17165 ptrdiff_t first_changed_charpos, last_changed_charpos;
17166
17167 #ifdef GLYPH_DEBUG
17168 if (inhibit_try_window_id)
17169 return 0;
17170 #endif
17171
17172 /* This is handy for debugging. */
17173 #if 0
17174 #define GIVE_UP(X) \
17175 do { \
17176 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17177 return 0; \
17178 } while (0)
17179 #else
17180 #define GIVE_UP(X) return 0
17181 #endif
17182
17183 SET_TEXT_POS_FROM_MARKER (start, w->start);
17184
17185 /* Don't use this for mini-windows because these can show
17186 messages and mini-buffers, and we don't handle that here. */
17187 if (MINI_WINDOW_P (w))
17188 GIVE_UP (1);
17189
17190 /* This flag is used to prevent redisplay optimizations. */
17191 if (windows_or_buffers_changed || cursor_type_changed)
17192 GIVE_UP (2);
17193
17194 /* Verify that narrowing has not changed.
17195 Also verify that we were not told to prevent redisplay optimizations.
17196 It would be nice to further
17197 reduce the number of cases where this prevents try_window_id. */
17198 if (current_buffer->clip_changed
17199 || current_buffer->prevent_redisplay_optimizations_p)
17200 GIVE_UP (3);
17201
17202 /* Window must either use window-based redisplay or be full width. */
17203 if (!FRAME_WINDOW_P (f)
17204 && (!FRAME_LINE_INS_DEL_OK (f)
17205 || !WINDOW_FULL_WIDTH_P (w)))
17206 GIVE_UP (4);
17207
17208 /* Give up if point is known NOT to appear in W. */
17209 if (PT < CHARPOS (start))
17210 GIVE_UP (5);
17211
17212 /* Another way to prevent redisplay optimizations. */
17213 if (w->last_modified == 0)
17214 GIVE_UP (6);
17215
17216 /* Verify that window is not hscrolled. */
17217 if (w->hscroll != 0)
17218 GIVE_UP (7);
17219
17220 /* Verify that display wasn't paused. */
17221 if (NILP (w->window_end_valid))
17222 GIVE_UP (8);
17223
17224 /* Can't use this if highlighting a region because a cursor movement
17225 will do more than just set the cursor. */
17226 if (!NILP (Vtransient_mark_mode)
17227 && !NILP (BVAR (current_buffer, mark_active)))
17228 GIVE_UP (9);
17229
17230 /* Likewise if highlighting trailing whitespace. */
17231 if (!NILP (Vshow_trailing_whitespace))
17232 GIVE_UP (11);
17233
17234 /* Likewise if showing a region. */
17235 if (!NILP (w->region_showing))
17236 GIVE_UP (10);
17237
17238 /* Can't use this if overlay arrow position and/or string have
17239 changed. */
17240 if (overlay_arrows_changed_p ())
17241 GIVE_UP (12);
17242
17243 /* When word-wrap is on, adding a space to the first word of a
17244 wrapped line can change the wrap position, altering the line
17245 above it. It might be worthwhile to handle this more
17246 intelligently, but for now just redisplay from scratch. */
17247 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17248 GIVE_UP (21);
17249
17250 /* Under bidi reordering, adding or deleting a character in the
17251 beginning of a paragraph, before the first strong directional
17252 character, can change the base direction of the paragraph (unless
17253 the buffer specifies a fixed paragraph direction), which will
17254 require to redisplay the whole paragraph. It might be worthwhile
17255 to find the paragraph limits and widen the range of redisplayed
17256 lines to that, but for now just give up this optimization and
17257 redisplay from scratch. */
17258 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17259 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17260 GIVE_UP (22);
17261
17262 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17263 only if buffer has really changed. The reason is that the gap is
17264 initially at Z for freshly visited files. The code below would
17265 set end_unchanged to 0 in that case. */
17266 if (MODIFF > SAVE_MODIFF
17267 /* This seems to happen sometimes after saving a buffer. */
17268 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17269 {
17270 if (GPT - BEG < BEG_UNCHANGED)
17271 BEG_UNCHANGED = GPT - BEG;
17272 if (Z - GPT < END_UNCHANGED)
17273 END_UNCHANGED = Z - GPT;
17274 }
17275
17276 /* The position of the first and last character that has been changed. */
17277 first_changed_charpos = BEG + BEG_UNCHANGED;
17278 last_changed_charpos = Z - END_UNCHANGED;
17279
17280 /* If window starts after a line end, and the last change is in
17281 front of that newline, then changes don't affect the display.
17282 This case happens with stealth-fontification. Note that although
17283 the display is unchanged, glyph positions in the matrix have to
17284 be adjusted, of course. */
17285 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17286 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17287 && ((last_changed_charpos < CHARPOS (start)
17288 && CHARPOS (start) == BEGV)
17289 || (last_changed_charpos < CHARPOS (start) - 1
17290 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17291 {
17292 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17293 struct glyph_row *r0;
17294
17295 /* Compute how many chars/bytes have been added to or removed
17296 from the buffer. */
17297 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17298 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17299 Z_delta = Z - Z_old;
17300 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17301
17302 /* Give up if PT is not in the window. Note that it already has
17303 been checked at the start of try_window_id that PT is not in
17304 front of the window start. */
17305 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17306 GIVE_UP (13);
17307
17308 /* If window start is unchanged, we can reuse the whole matrix
17309 as is, after adjusting glyph positions. No need to compute
17310 the window end again, since its offset from Z hasn't changed. */
17311 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17312 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17313 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17314 /* PT must not be in a partially visible line. */
17315 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17316 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17317 {
17318 /* Adjust positions in the glyph matrix. */
17319 if (Z_delta || Z_delta_bytes)
17320 {
17321 struct glyph_row *r1
17322 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17323 increment_matrix_positions (w->current_matrix,
17324 MATRIX_ROW_VPOS (r0, current_matrix),
17325 MATRIX_ROW_VPOS (r1, current_matrix),
17326 Z_delta, Z_delta_bytes);
17327 }
17328
17329 /* Set the cursor. */
17330 row = row_containing_pos (w, PT, r0, NULL, 0);
17331 if (row)
17332 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17333 else
17334 emacs_abort ();
17335 return 1;
17336 }
17337 }
17338
17339 /* Handle the case that changes are all below what is displayed in
17340 the window, and that PT is in the window. This shortcut cannot
17341 be taken if ZV is visible in the window, and text has been added
17342 there that is visible in the window. */
17343 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17344 /* ZV is not visible in the window, or there are no
17345 changes at ZV, actually. */
17346 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17347 || first_changed_charpos == last_changed_charpos))
17348 {
17349 struct glyph_row *r0;
17350
17351 /* Give up if PT is not in the window. Note that it already has
17352 been checked at the start of try_window_id that PT is not in
17353 front of the window start. */
17354 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17355 GIVE_UP (14);
17356
17357 /* If window start is unchanged, we can reuse the whole matrix
17358 as is, without changing glyph positions since no text has
17359 been added/removed in front of the window end. */
17360 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17361 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17362 /* PT must not be in a partially visible line. */
17363 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17364 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17365 {
17366 /* We have to compute the window end anew since text
17367 could have been added/removed after it. */
17368 wset_window_end_pos
17369 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17370 w->window_end_bytepos
17371 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17372
17373 /* Set the cursor. */
17374 row = row_containing_pos (w, PT, r0, NULL, 0);
17375 if (row)
17376 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17377 else
17378 emacs_abort ();
17379 return 2;
17380 }
17381 }
17382
17383 /* Give up if window start is in the changed area.
17384
17385 The condition used to read
17386
17387 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17388
17389 but why that was tested escapes me at the moment. */
17390 if (CHARPOS (start) >= first_changed_charpos
17391 && CHARPOS (start) <= last_changed_charpos)
17392 GIVE_UP (15);
17393
17394 /* Check that window start agrees with the start of the first glyph
17395 row in its current matrix. Check this after we know the window
17396 start is not in changed text, otherwise positions would not be
17397 comparable. */
17398 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17399 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17400 GIVE_UP (16);
17401
17402 /* Give up if the window ends in strings. Overlay strings
17403 at the end are difficult to handle, so don't try. */
17404 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17405 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17406 GIVE_UP (20);
17407
17408 /* Compute the position at which we have to start displaying new
17409 lines. Some of the lines at the top of the window might be
17410 reusable because they are not displaying changed text. Find the
17411 last row in W's current matrix not affected by changes at the
17412 start of current_buffer. Value is null if changes start in the
17413 first line of window. */
17414 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17415 if (last_unchanged_at_beg_row)
17416 {
17417 /* Avoid starting to display in the middle of a character, a TAB
17418 for instance. This is easier than to set up the iterator
17419 exactly, and it's not a frequent case, so the additional
17420 effort wouldn't really pay off. */
17421 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17422 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17423 && last_unchanged_at_beg_row > w->current_matrix->rows)
17424 --last_unchanged_at_beg_row;
17425
17426 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17427 GIVE_UP (17);
17428
17429 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17430 GIVE_UP (18);
17431 start_pos = it.current.pos;
17432
17433 /* Start displaying new lines in the desired matrix at the same
17434 vpos we would use in the current matrix, i.e. below
17435 last_unchanged_at_beg_row. */
17436 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17437 current_matrix);
17438 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17439 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17440
17441 eassert (it.hpos == 0 && it.current_x == 0);
17442 }
17443 else
17444 {
17445 /* There are no reusable lines at the start of the window.
17446 Start displaying in the first text line. */
17447 start_display (&it, w, start);
17448 it.vpos = it.first_vpos;
17449 start_pos = it.current.pos;
17450 }
17451
17452 /* Find the first row that is not affected by changes at the end of
17453 the buffer. Value will be null if there is no unchanged row, in
17454 which case we must redisplay to the end of the window. delta
17455 will be set to the value by which buffer positions beginning with
17456 first_unchanged_at_end_row have to be adjusted due to text
17457 changes. */
17458 first_unchanged_at_end_row
17459 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17460 IF_DEBUG (debug_delta = delta);
17461 IF_DEBUG (debug_delta_bytes = delta_bytes);
17462
17463 /* Set stop_pos to the buffer position up to which we will have to
17464 display new lines. If first_unchanged_at_end_row != NULL, this
17465 is the buffer position of the start of the line displayed in that
17466 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17467 that we don't stop at a buffer position. */
17468 stop_pos = 0;
17469 if (first_unchanged_at_end_row)
17470 {
17471 eassert (last_unchanged_at_beg_row == NULL
17472 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17473
17474 /* If this is a continuation line, move forward to the next one
17475 that isn't. Changes in lines above affect this line.
17476 Caution: this may move first_unchanged_at_end_row to a row
17477 not displaying text. */
17478 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17479 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17480 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17481 < it.last_visible_y))
17482 ++first_unchanged_at_end_row;
17483
17484 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17485 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17486 >= it.last_visible_y))
17487 first_unchanged_at_end_row = NULL;
17488 else
17489 {
17490 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17491 + delta);
17492 first_unchanged_at_end_vpos
17493 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17494 eassert (stop_pos >= Z - END_UNCHANGED);
17495 }
17496 }
17497 else if (last_unchanged_at_beg_row == NULL)
17498 GIVE_UP (19);
17499
17500
17501 #ifdef GLYPH_DEBUG
17502
17503 /* Either there is no unchanged row at the end, or the one we have
17504 now displays text. This is a necessary condition for the window
17505 end pos calculation at the end of this function. */
17506 eassert (first_unchanged_at_end_row == NULL
17507 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17508
17509 debug_last_unchanged_at_beg_vpos
17510 = (last_unchanged_at_beg_row
17511 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17512 : -1);
17513 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17514
17515 #endif /* GLYPH_DEBUG */
17516
17517
17518 /* Display new lines. Set last_text_row to the last new line
17519 displayed which has text on it, i.e. might end up as being the
17520 line where the window_end_vpos is. */
17521 w->cursor.vpos = -1;
17522 last_text_row = NULL;
17523 overlay_arrow_seen = 0;
17524 while (it.current_y < it.last_visible_y
17525 && !fonts_changed_p
17526 && (first_unchanged_at_end_row == NULL
17527 || IT_CHARPOS (it) < stop_pos))
17528 {
17529 if (display_line (&it))
17530 last_text_row = it.glyph_row - 1;
17531 }
17532
17533 if (fonts_changed_p)
17534 return -1;
17535
17536
17537 /* Compute differences in buffer positions, y-positions etc. for
17538 lines reused at the bottom of the window. Compute what we can
17539 scroll. */
17540 if (first_unchanged_at_end_row
17541 /* No lines reused because we displayed everything up to the
17542 bottom of the window. */
17543 && it.current_y < it.last_visible_y)
17544 {
17545 dvpos = (it.vpos
17546 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17547 current_matrix));
17548 dy = it.current_y - first_unchanged_at_end_row->y;
17549 run.current_y = first_unchanged_at_end_row->y;
17550 run.desired_y = run.current_y + dy;
17551 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17552 }
17553 else
17554 {
17555 delta = delta_bytes = dvpos = dy
17556 = run.current_y = run.desired_y = run.height = 0;
17557 first_unchanged_at_end_row = NULL;
17558 }
17559 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17560
17561
17562 /* Find the cursor if not already found. We have to decide whether
17563 PT will appear on this window (it sometimes doesn't, but this is
17564 not a very frequent case.) This decision has to be made before
17565 the current matrix is altered. A value of cursor.vpos < 0 means
17566 that PT is either in one of the lines beginning at
17567 first_unchanged_at_end_row or below the window. Don't care for
17568 lines that might be displayed later at the window end; as
17569 mentioned, this is not a frequent case. */
17570 if (w->cursor.vpos < 0)
17571 {
17572 /* Cursor in unchanged rows at the top? */
17573 if (PT < CHARPOS (start_pos)
17574 && last_unchanged_at_beg_row)
17575 {
17576 row = row_containing_pos (w, PT,
17577 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17578 last_unchanged_at_beg_row + 1, 0);
17579 if (row)
17580 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17581 }
17582
17583 /* Start from first_unchanged_at_end_row looking for PT. */
17584 else if (first_unchanged_at_end_row)
17585 {
17586 row = row_containing_pos (w, PT - delta,
17587 first_unchanged_at_end_row, NULL, 0);
17588 if (row)
17589 set_cursor_from_row (w, row, w->current_matrix, delta,
17590 delta_bytes, dy, dvpos);
17591 }
17592
17593 /* Give up if cursor was not found. */
17594 if (w->cursor.vpos < 0)
17595 {
17596 clear_glyph_matrix (w->desired_matrix);
17597 return -1;
17598 }
17599 }
17600
17601 /* Don't let the cursor end in the scroll margins. */
17602 {
17603 int this_scroll_margin, cursor_height;
17604
17605 this_scroll_margin =
17606 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17607 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17608 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17609
17610 if ((w->cursor.y < this_scroll_margin
17611 && CHARPOS (start) > BEGV)
17612 /* Old redisplay didn't take scroll margin into account at the bottom,
17613 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17614 || (w->cursor.y + (make_cursor_line_fully_visible_p
17615 ? cursor_height + this_scroll_margin
17616 : 1)) > it.last_visible_y)
17617 {
17618 w->cursor.vpos = -1;
17619 clear_glyph_matrix (w->desired_matrix);
17620 return -1;
17621 }
17622 }
17623
17624 /* Scroll the display. Do it before changing the current matrix so
17625 that xterm.c doesn't get confused about where the cursor glyph is
17626 found. */
17627 if (dy && run.height)
17628 {
17629 update_begin (f);
17630
17631 if (FRAME_WINDOW_P (f))
17632 {
17633 FRAME_RIF (f)->update_window_begin_hook (w);
17634 FRAME_RIF (f)->clear_window_mouse_face (w);
17635 FRAME_RIF (f)->scroll_run_hook (w, &run);
17636 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17637 }
17638 else
17639 {
17640 /* Terminal frame. In this case, dvpos gives the number of
17641 lines to scroll by; dvpos < 0 means scroll up. */
17642 int from_vpos
17643 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17644 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17645 int end = (WINDOW_TOP_EDGE_LINE (w)
17646 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17647 + window_internal_height (w));
17648
17649 #if defined (HAVE_GPM) || defined (MSDOS)
17650 x_clear_window_mouse_face (w);
17651 #endif
17652 /* Perform the operation on the screen. */
17653 if (dvpos > 0)
17654 {
17655 /* Scroll last_unchanged_at_beg_row to the end of the
17656 window down dvpos lines. */
17657 set_terminal_window (f, end);
17658
17659 /* On dumb terminals delete dvpos lines at the end
17660 before inserting dvpos empty lines. */
17661 if (!FRAME_SCROLL_REGION_OK (f))
17662 ins_del_lines (f, end - dvpos, -dvpos);
17663
17664 /* Insert dvpos empty lines in front of
17665 last_unchanged_at_beg_row. */
17666 ins_del_lines (f, from, dvpos);
17667 }
17668 else if (dvpos < 0)
17669 {
17670 /* Scroll up last_unchanged_at_beg_vpos to the end of
17671 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17672 set_terminal_window (f, end);
17673
17674 /* Delete dvpos lines in front of
17675 last_unchanged_at_beg_vpos. ins_del_lines will set
17676 the cursor to the given vpos and emit |dvpos| delete
17677 line sequences. */
17678 ins_del_lines (f, from + dvpos, dvpos);
17679
17680 /* On a dumb terminal insert dvpos empty lines at the
17681 end. */
17682 if (!FRAME_SCROLL_REGION_OK (f))
17683 ins_del_lines (f, end + dvpos, -dvpos);
17684 }
17685
17686 set_terminal_window (f, 0);
17687 }
17688
17689 update_end (f);
17690 }
17691
17692 /* Shift reused rows of the current matrix to the right position.
17693 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17694 text. */
17695 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17696 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17697 if (dvpos < 0)
17698 {
17699 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17700 bottom_vpos, dvpos);
17701 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17702 bottom_vpos);
17703 }
17704 else if (dvpos > 0)
17705 {
17706 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17707 bottom_vpos, dvpos);
17708 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17709 first_unchanged_at_end_vpos + dvpos);
17710 }
17711
17712 /* For frame-based redisplay, make sure that current frame and window
17713 matrix are in sync with respect to glyph memory. */
17714 if (!FRAME_WINDOW_P (f))
17715 sync_frame_with_window_matrix_rows (w);
17716
17717 /* Adjust buffer positions in reused rows. */
17718 if (delta || delta_bytes)
17719 increment_matrix_positions (current_matrix,
17720 first_unchanged_at_end_vpos + dvpos,
17721 bottom_vpos, delta, delta_bytes);
17722
17723 /* Adjust Y positions. */
17724 if (dy)
17725 shift_glyph_matrix (w, current_matrix,
17726 first_unchanged_at_end_vpos + dvpos,
17727 bottom_vpos, dy);
17728
17729 if (first_unchanged_at_end_row)
17730 {
17731 first_unchanged_at_end_row += dvpos;
17732 if (first_unchanged_at_end_row->y >= it.last_visible_y
17733 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17734 first_unchanged_at_end_row = NULL;
17735 }
17736
17737 /* If scrolling up, there may be some lines to display at the end of
17738 the window. */
17739 last_text_row_at_end = NULL;
17740 if (dy < 0)
17741 {
17742 /* Scrolling up can leave for example a partially visible line
17743 at the end of the window to be redisplayed. */
17744 /* Set last_row to the glyph row in the current matrix where the
17745 window end line is found. It has been moved up or down in
17746 the matrix by dvpos. */
17747 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17748 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17749
17750 /* If last_row is the window end line, it should display text. */
17751 eassert (last_row->displays_text_p);
17752
17753 /* If window end line was partially visible before, begin
17754 displaying at that line. Otherwise begin displaying with the
17755 line following it. */
17756 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17757 {
17758 init_to_row_start (&it, w, last_row);
17759 it.vpos = last_vpos;
17760 it.current_y = last_row->y;
17761 }
17762 else
17763 {
17764 init_to_row_end (&it, w, last_row);
17765 it.vpos = 1 + last_vpos;
17766 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17767 ++last_row;
17768 }
17769
17770 /* We may start in a continuation line. If so, we have to
17771 get the right continuation_lines_width and current_x. */
17772 it.continuation_lines_width = last_row->continuation_lines_width;
17773 it.hpos = it.current_x = 0;
17774
17775 /* Display the rest of the lines at the window end. */
17776 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17777 while (it.current_y < it.last_visible_y
17778 && !fonts_changed_p)
17779 {
17780 /* Is it always sure that the display agrees with lines in
17781 the current matrix? I don't think so, so we mark rows
17782 displayed invalid in the current matrix by setting their
17783 enabled_p flag to zero. */
17784 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17785 if (display_line (&it))
17786 last_text_row_at_end = it.glyph_row - 1;
17787 }
17788 }
17789
17790 /* Update window_end_pos and window_end_vpos. */
17791 if (first_unchanged_at_end_row
17792 && !last_text_row_at_end)
17793 {
17794 /* Window end line if one of the preserved rows from the current
17795 matrix. Set row to the last row displaying text in current
17796 matrix starting at first_unchanged_at_end_row, after
17797 scrolling. */
17798 eassert (first_unchanged_at_end_row->displays_text_p);
17799 row = find_last_row_displaying_text (w->current_matrix, &it,
17800 first_unchanged_at_end_row);
17801 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17802
17803 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17804 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17805 wset_window_end_vpos
17806 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17807 eassert (w->window_end_bytepos >= 0);
17808 IF_DEBUG (debug_method_add (w, "A"));
17809 }
17810 else if (last_text_row_at_end)
17811 {
17812 wset_window_end_pos
17813 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17814 w->window_end_bytepos
17815 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17816 wset_window_end_vpos
17817 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17818 desired_matrix)));
17819 eassert (w->window_end_bytepos >= 0);
17820 IF_DEBUG (debug_method_add (w, "B"));
17821 }
17822 else if (last_text_row)
17823 {
17824 /* We have displayed either to the end of the window or at the
17825 end of the window, i.e. the last row with text is to be found
17826 in the desired matrix. */
17827 wset_window_end_pos
17828 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17829 w->window_end_bytepos
17830 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17831 wset_window_end_vpos
17832 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17833 eassert (w->window_end_bytepos >= 0);
17834 }
17835 else if (first_unchanged_at_end_row == NULL
17836 && last_text_row == NULL
17837 && last_text_row_at_end == NULL)
17838 {
17839 /* Displayed to end of window, but no line containing text was
17840 displayed. Lines were deleted at the end of the window. */
17841 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17842 int vpos = XFASTINT (w->window_end_vpos);
17843 struct glyph_row *current_row = current_matrix->rows + vpos;
17844 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17845
17846 for (row = NULL;
17847 row == NULL && vpos >= first_vpos;
17848 --vpos, --current_row, --desired_row)
17849 {
17850 if (desired_row->enabled_p)
17851 {
17852 if (desired_row->displays_text_p)
17853 row = desired_row;
17854 }
17855 else if (current_row->displays_text_p)
17856 row = current_row;
17857 }
17858
17859 eassert (row != NULL);
17860 wset_window_end_vpos (w, make_number (vpos + 1));
17861 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17862 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17863 eassert (w->window_end_bytepos >= 0);
17864 IF_DEBUG (debug_method_add (w, "C"));
17865 }
17866 else
17867 emacs_abort ();
17868
17869 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17870 debug_end_vpos = XFASTINT (w->window_end_vpos));
17871
17872 /* Record that display has not been completed. */
17873 wset_window_end_valid (w, Qnil);
17874 w->desired_matrix->no_scrolling_p = 1;
17875 return 3;
17876
17877 #undef GIVE_UP
17878 }
17879
17880
17881 \f
17882 /***********************************************************************
17883 More debugging support
17884 ***********************************************************************/
17885
17886 #ifdef GLYPH_DEBUG
17887
17888 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17889 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17890 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17891
17892
17893 /* Dump the contents of glyph matrix MATRIX on stderr.
17894
17895 GLYPHS 0 means don't show glyph contents.
17896 GLYPHS 1 means show glyphs in short form
17897 GLYPHS > 1 means show glyphs in long form. */
17898
17899 void
17900 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17901 {
17902 int i;
17903 for (i = 0; i < matrix->nrows; ++i)
17904 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17905 }
17906
17907
17908 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17909 the glyph row and area where the glyph comes from. */
17910
17911 void
17912 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17913 {
17914 if (glyph->type == CHAR_GLYPH)
17915 {
17916 fprintf (stderr,
17917 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17918 glyph - row->glyphs[TEXT_AREA],
17919 'C',
17920 glyph->charpos,
17921 (BUFFERP (glyph->object)
17922 ? 'B'
17923 : (STRINGP (glyph->object)
17924 ? 'S'
17925 : '-')),
17926 glyph->pixel_width,
17927 glyph->u.ch,
17928 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17929 ? glyph->u.ch
17930 : '.'),
17931 glyph->face_id,
17932 glyph->left_box_line_p,
17933 glyph->right_box_line_p);
17934 }
17935 else if (glyph->type == STRETCH_GLYPH)
17936 {
17937 fprintf (stderr,
17938 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17939 glyph - row->glyphs[TEXT_AREA],
17940 'S',
17941 glyph->charpos,
17942 (BUFFERP (glyph->object)
17943 ? 'B'
17944 : (STRINGP (glyph->object)
17945 ? 'S'
17946 : '-')),
17947 glyph->pixel_width,
17948 0,
17949 '.',
17950 glyph->face_id,
17951 glyph->left_box_line_p,
17952 glyph->right_box_line_p);
17953 }
17954 else if (glyph->type == IMAGE_GLYPH)
17955 {
17956 fprintf (stderr,
17957 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17958 glyph - row->glyphs[TEXT_AREA],
17959 'I',
17960 glyph->charpos,
17961 (BUFFERP (glyph->object)
17962 ? 'B'
17963 : (STRINGP (glyph->object)
17964 ? 'S'
17965 : '-')),
17966 glyph->pixel_width,
17967 glyph->u.img_id,
17968 '.',
17969 glyph->face_id,
17970 glyph->left_box_line_p,
17971 glyph->right_box_line_p);
17972 }
17973 else if (glyph->type == COMPOSITE_GLYPH)
17974 {
17975 fprintf (stderr,
17976 " %5td %4c %6"pI"d %c %3d 0x%05x",
17977 glyph - row->glyphs[TEXT_AREA],
17978 '+',
17979 glyph->charpos,
17980 (BUFFERP (glyph->object)
17981 ? 'B'
17982 : (STRINGP (glyph->object)
17983 ? 'S'
17984 : '-')),
17985 glyph->pixel_width,
17986 glyph->u.cmp.id);
17987 if (glyph->u.cmp.automatic)
17988 fprintf (stderr,
17989 "[%d-%d]",
17990 glyph->slice.cmp.from, glyph->slice.cmp.to);
17991 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17992 glyph->face_id,
17993 glyph->left_box_line_p,
17994 glyph->right_box_line_p);
17995 }
17996 }
17997
17998
17999 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18000 GLYPHS 0 means don't show glyph contents.
18001 GLYPHS 1 means show glyphs in short form
18002 GLYPHS > 1 means show glyphs in long form. */
18003
18004 void
18005 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18006 {
18007 if (glyphs != 1)
18008 {
18009 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18010 fprintf (stderr, "======================================================================\n");
18011
18012 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18013 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18014 vpos,
18015 MATRIX_ROW_START_CHARPOS (row),
18016 MATRIX_ROW_END_CHARPOS (row),
18017 row->used[TEXT_AREA],
18018 row->contains_overlapping_glyphs_p,
18019 row->enabled_p,
18020 row->truncated_on_left_p,
18021 row->truncated_on_right_p,
18022 row->continued_p,
18023 MATRIX_ROW_CONTINUATION_LINE_P (row),
18024 row->displays_text_p,
18025 row->ends_at_zv_p,
18026 row->fill_line_p,
18027 row->ends_in_middle_of_char_p,
18028 row->starts_in_middle_of_char_p,
18029 row->mouse_face_p,
18030 row->x,
18031 row->y,
18032 row->pixel_width,
18033 row->height,
18034 row->visible_height,
18035 row->ascent,
18036 row->phys_ascent);
18037 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
18038 row->end.overlay_string_index,
18039 row->continuation_lines_width);
18040 fprintf (stderr, "%9"pI"d %5"pI"d\n",
18041 CHARPOS (row->start.string_pos),
18042 CHARPOS (row->end.string_pos));
18043 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
18044 row->end.dpvec_index);
18045 }
18046
18047 if (glyphs > 1)
18048 {
18049 int area;
18050
18051 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18052 {
18053 struct glyph *glyph = row->glyphs[area];
18054 struct glyph *glyph_end = glyph + row->used[area];
18055
18056 /* Glyph for a line end in text. */
18057 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18058 ++glyph_end;
18059
18060 if (glyph < glyph_end)
18061 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18062
18063 for (; glyph < glyph_end; ++glyph)
18064 dump_glyph (row, glyph, area);
18065 }
18066 }
18067 else if (glyphs == 1)
18068 {
18069 int area;
18070
18071 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18072 {
18073 char *s = alloca (row->used[area] + 1);
18074 int i;
18075
18076 for (i = 0; i < row->used[area]; ++i)
18077 {
18078 struct glyph *glyph = row->glyphs[area] + i;
18079 if (glyph->type == CHAR_GLYPH
18080 && glyph->u.ch < 0x80
18081 && glyph->u.ch >= ' ')
18082 s[i] = glyph->u.ch;
18083 else
18084 s[i] = '.';
18085 }
18086
18087 s[i] = '\0';
18088 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18089 }
18090 }
18091 }
18092
18093
18094 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18095 Sdump_glyph_matrix, 0, 1, "p",
18096 doc: /* Dump the current matrix of the selected window to stderr.
18097 Shows contents of glyph row structures. With non-nil
18098 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18099 glyphs in short form, otherwise show glyphs in long form. */)
18100 (Lisp_Object glyphs)
18101 {
18102 struct window *w = XWINDOW (selected_window);
18103 struct buffer *buffer = XBUFFER (w->buffer);
18104
18105 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18106 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18107 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18108 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18109 fprintf (stderr, "=============================================\n");
18110 dump_glyph_matrix (w->current_matrix,
18111 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18112 return Qnil;
18113 }
18114
18115
18116 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18117 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18118 (void)
18119 {
18120 struct frame *f = XFRAME (selected_frame);
18121 dump_glyph_matrix (f->current_matrix, 1);
18122 return Qnil;
18123 }
18124
18125
18126 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18127 doc: /* Dump glyph row ROW to stderr.
18128 GLYPH 0 means don't dump glyphs.
18129 GLYPH 1 means dump glyphs in short form.
18130 GLYPH > 1 or omitted means dump glyphs in long form. */)
18131 (Lisp_Object row, Lisp_Object glyphs)
18132 {
18133 struct glyph_matrix *matrix;
18134 EMACS_INT vpos;
18135
18136 CHECK_NUMBER (row);
18137 matrix = XWINDOW (selected_window)->current_matrix;
18138 vpos = XINT (row);
18139 if (vpos >= 0 && vpos < matrix->nrows)
18140 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18141 vpos,
18142 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18143 return Qnil;
18144 }
18145
18146
18147 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18148 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18149 GLYPH 0 means don't dump glyphs.
18150 GLYPH 1 means dump glyphs in short form.
18151 GLYPH > 1 or omitted means dump glyphs in long form. */)
18152 (Lisp_Object row, Lisp_Object glyphs)
18153 {
18154 struct frame *sf = SELECTED_FRAME ();
18155 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18156 EMACS_INT vpos;
18157
18158 CHECK_NUMBER (row);
18159 vpos = XINT (row);
18160 if (vpos >= 0 && vpos < m->nrows)
18161 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18162 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18163 return Qnil;
18164 }
18165
18166
18167 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18168 doc: /* Toggle tracing of redisplay.
18169 With ARG, turn tracing on if and only if ARG is positive. */)
18170 (Lisp_Object arg)
18171 {
18172 if (NILP (arg))
18173 trace_redisplay_p = !trace_redisplay_p;
18174 else
18175 {
18176 arg = Fprefix_numeric_value (arg);
18177 trace_redisplay_p = XINT (arg) > 0;
18178 }
18179
18180 return Qnil;
18181 }
18182
18183
18184 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18185 doc: /* Like `format', but print result to stderr.
18186 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18187 (ptrdiff_t nargs, Lisp_Object *args)
18188 {
18189 Lisp_Object s = Fformat (nargs, args);
18190 fprintf (stderr, "%s", SDATA (s));
18191 return Qnil;
18192 }
18193
18194 #endif /* GLYPH_DEBUG */
18195
18196
18197 \f
18198 /***********************************************************************
18199 Building Desired Matrix Rows
18200 ***********************************************************************/
18201
18202 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18203 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18204
18205 static struct glyph_row *
18206 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18207 {
18208 struct frame *f = XFRAME (WINDOW_FRAME (w));
18209 struct buffer *buffer = XBUFFER (w->buffer);
18210 struct buffer *old = current_buffer;
18211 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18212 int arrow_len = SCHARS (overlay_arrow_string);
18213 const unsigned char *arrow_end = arrow_string + arrow_len;
18214 const unsigned char *p;
18215 struct it it;
18216 int multibyte_p;
18217 int n_glyphs_before;
18218
18219 set_buffer_temp (buffer);
18220 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18221 it.glyph_row->used[TEXT_AREA] = 0;
18222 SET_TEXT_POS (it.position, 0, 0);
18223
18224 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18225 p = arrow_string;
18226 while (p < arrow_end)
18227 {
18228 Lisp_Object face, ilisp;
18229
18230 /* Get the next character. */
18231 if (multibyte_p)
18232 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18233 else
18234 {
18235 it.c = it.char_to_display = *p, it.len = 1;
18236 if (! ASCII_CHAR_P (it.c))
18237 it.char_to_display = BYTE8_TO_CHAR (it.c);
18238 }
18239 p += it.len;
18240
18241 /* Get its face. */
18242 ilisp = make_number (p - arrow_string);
18243 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18244 it.face_id = compute_char_face (f, it.char_to_display, face);
18245
18246 /* Compute its width, get its glyphs. */
18247 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18248 SET_TEXT_POS (it.position, -1, -1);
18249 PRODUCE_GLYPHS (&it);
18250
18251 /* If this character doesn't fit any more in the line, we have
18252 to remove some glyphs. */
18253 if (it.current_x > it.last_visible_x)
18254 {
18255 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18256 break;
18257 }
18258 }
18259
18260 set_buffer_temp (old);
18261 return it.glyph_row;
18262 }
18263
18264
18265 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18266 glyphs to insert is determined by produce_special_glyphs. */
18267
18268 static void
18269 insert_left_trunc_glyphs (struct it *it)
18270 {
18271 struct it truncate_it;
18272 struct glyph *from, *end, *to, *toend;
18273
18274 eassert (!FRAME_WINDOW_P (it->f)
18275 || (!it->glyph_row->reversed_p
18276 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18277 || (it->glyph_row->reversed_p
18278 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18279
18280 /* Get the truncation glyphs. */
18281 truncate_it = *it;
18282 truncate_it.current_x = 0;
18283 truncate_it.face_id = DEFAULT_FACE_ID;
18284 truncate_it.glyph_row = &scratch_glyph_row;
18285 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18286 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18287 truncate_it.object = make_number (0);
18288 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18289
18290 /* Overwrite glyphs from IT with truncation glyphs. */
18291 if (!it->glyph_row->reversed_p)
18292 {
18293 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18294
18295 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18296 end = from + tused;
18297 to = it->glyph_row->glyphs[TEXT_AREA];
18298 toend = to + it->glyph_row->used[TEXT_AREA];
18299 if (FRAME_WINDOW_P (it->f))
18300 {
18301 /* On GUI frames, when variable-size fonts are displayed,
18302 the truncation glyphs may need more pixels than the row's
18303 glyphs they overwrite. We overwrite more glyphs to free
18304 enough screen real estate, and enlarge the stretch glyph
18305 on the right (see display_line), if there is one, to
18306 preserve the screen position of the truncation glyphs on
18307 the right. */
18308 int w = 0;
18309 struct glyph *g = to;
18310 short used;
18311
18312 /* The first glyph could be partially visible, in which case
18313 it->glyph_row->x will be negative. But we want the left
18314 truncation glyphs to be aligned at the left margin of the
18315 window, so we override the x coordinate at which the row
18316 will begin. */
18317 it->glyph_row->x = 0;
18318 while (g < toend && w < it->truncation_pixel_width)
18319 {
18320 w += g->pixel_width;
18321 ++g;
18322 }
18323 if (g - to - tused > 0)
18324 {
18325 memmove (to + tused, g, (toend - g) * sizeof(*g));
18326 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18327 }
18328 used = it->glyph_row->used[TEXT_AREA];
18329 if (it->glyph_row->truncated_on_right_p
18330 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18331 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18332 == STRETCH_GLYPH)
18333 {
18334 int extra = w - it->truncation_pixel_width;
18335
18336 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18337 }
18338 }
18339
18340 while (from < end)
18341 *to++ = *from++;
18342
18343 /* There may be padding glyphs left over. Overwrite them too. */
18344 if (!FRAME_WINDOW_P (it->f))
18345 {
18346 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18347 {
18348 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18349 while (from < end)
18350 *to++ = *from++;
18351 }
18352 }
18353
18354 if (to > toend)
18355 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18356 }
18357 else
18358 {
18359 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18360
18361 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18362 that back to front. */
18363 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18364 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18365 toend = it->glyph_row->glyphs[TEXT_AREA];
18366 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18367 if (FRAME_WINDOW_P (it->f))
18368 {
18369 int w = 0;
18370 struct glyph *g = to;
18371
18372 while (g >= toend && w < it->truncation_pixel_width)
18373 {
18374 w += g->pixel_width;
18375 --g;
18376 }
18377 if (to - g - tused > 0)
18378 to = g + tused;
18379 if (it->glyph_row->truncated_on_right_p
18380 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18381 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18382 {
18383 int extra = w - it->truncation_pixel_width;
18384
18385 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18386 }
18387 }
18388
18389 while (from >= end && to >= toend)
18390 *to-- = *from--;
18391 if (!FRAME_WINDOW_P (it->f))
18392 {
18393 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18394 {
18395 from =
18396 truncate_it.glyph_row->glyphs[TEXT_AREA]
18397 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18398 while (from >= end && to >= toend)
18399 *to-- = *from--;
18400 }
18401 }
18402 if (from >= end)
18403 {
18404 /* Need to free some room before prepending additional
18405 glyphs. */
18406 int move_by = from - end + 1;
18407 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18408 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18409
18410 for ( ; g >= g0; g--)
18411 g[move_by] = *g;
18412 while (from >= end)
18413 *to-- = *from--;
18414 it->glyph_row->used[TEXT_AREA] += move_by;
18415 }
18416 }
18417 }
18418
18419 /* Compute the hash code for ROW. */
18420 unsigned
18421 row_hash (struct glyph_row *row)
18422 {
18423 int area, k;
18424 unsigned hashval = 0;
18425
18426 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18427 for (k = 0; k < row->used[area]; ++k)
18428 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18429 + row->glyphs[area][k].u.val
18430 + row->glyphs[area][k].face_id
18431 + row->glyphs[area][k].padding_p
18432 + (row->glyphs[area][k].type << 2));
18433
18434 return hashval;
18435 }
18436
18437 /* Compute the pixel height and width of IT->glyph_row.
18438
18439 Most of the time, ascent and height of a display line will be equal
18440 to the max_ascent and max_height values of the display iterator
18441 structure. This is not the case if
18442
18443 1. We hit ZV without displaying anything. In this case, max_ascent
18444 and max_height will be zero.
18445
18446 2. We have some glyphs that don't contribute to the line height.
18447 (The glyph row flag contributes_to_line_height_p is for future
18448 pixmap extensions).
18449
18450 The first case is easily covered by using default values because in
18451 these cases, the line height does not really matter, except that it
18452 must not be zero. */
18453
18454 static void
18455 compute_line_metrics (struct it *it)
18456 {
18457 struct glyph_row *row = it->glyph_row;
18458
18459 if (FRAME_WINDOW_P (it->f))
18460 {
18461 int i, min_y, max_y;
18462
18463 /* The line may consist of one space only, that was added to
18464 place the cursor on it. If so, the row's height hasn't been
18465 computed yet. */
18466 if (row->height == 0)
18467 {
18468 if (it->max_ascent + it->max_descent == 0)
18469 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18470 row->ascent = it->max_ascent;
18471 row->height = it->max_ascent + it->max_descent;
18472 row->phys_ascent = it->max_phys_ascent;
18473 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18474 row->extra_line_spacing = it->max_extra_line_spacing;
18475 }
18476
18477 /* Compute the width of this line. */
18478 row->pixel_width = row->x;
18479 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18480 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18481
18482 eassert (row->pixel_width >= 0);
18483 eassert (row->ascent >= 0 && row->height > 0);
18484
18485 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18486 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18487
18488 /* If first line's physical ascent is larger than its logical
18489 ascent, use the physical ascent, and make the row taller.
18490 This makes accented characters fully visible. */
18491 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18492 && row->phys_ascent > row->ascent)
18493 {
18494 row->height += row->phys_ascent - row->ascent;
18495 row->ascent = row->phys_ascent;
18496 }
18497
18498 /* Compute how much of the line is visible. */
18499 row->visible_height = row->height;
18500
18501 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18502 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18503
18504 if (row->y < min_y)
18505 row->visible_height -= min_y - row->y;
18506 if (row->y + row->height > max_y)
18507 row->visible_height -= row->y + row->height - max_y;
18508 }
18509 else
18510 {
18511 row->pixel_width = row->used[TEXT_AREA];
18512 if (row->continued_p)
18513 row->pixel_width -= it->continuation_pixel_width;
18514 else if (row->truncated_on_right_p)
18515 row->pixel_width -= it->truncation_pixel_width;
18516 row->ascent = row->phys_ascent = 0;
18517 row->height = row->phys_height = row->visible_height = 1;
18518 row->extra_line_spacing = 0;
18519 }
18520
18521 /* Compute a hash code for this row. */
18522 row->hash = row_hash (row);
18523
18524 it->max_ascent = it->max_descent = 0;
18525 it->max_phys_ascent = it->max_phys_descent = 0;
18526 }
18527
18528
18529 /* Append one space to the glyph row of iterator IT if doing a
18530 window-based redisplay. The space has the same face as
18531 IT->face_id. Value is non-zero if a space was added.
18532
18533 This function is called to make sure that there is always one glyph
18534 at the end of a glyph row that the cursor can be set on under
18535 window-systems. (If there weren't such a glyph we would not know
18536 how wide and tall a box cursor should be displayed).
18537
18538 At the same time this space let's a nicely handle clearing to the
18539 end of the line if the row ends in italic text. */
18540
18541 static int
18542 append_space_for_newline (struct it *it, int default_face_p)
18543 {
18544 if (FRAME_WINDOW_P (it->f))
18545 {
18546 int n = it->glyph_row->used[TEXT_AREA];
18547
18548 if (it->glyph_row->glyphs[TEXT_AREA] + n
18549 < it->glyph_row->glyphs[1 + TEXT_AREA])
18550 {
18551 /* Save some values that must not be changed.
18552 Must save IT->c and IT->len because otherwise
18553 ITERATOR_AT_END_P wouldn't work anymore after
18554 append_space_for_newline has been called. */
18555 enum display_element_type saved_what = it->what;
18556 int saved_c = it->c, saved_len = it->len;
18557 int saved_char_to_display = it->char_to_display;
18558 int saved_x = it->current_x;
18559 int saved_face_id = it->face_id;
18560 struct text_pos saved_pos;
18561 Lisp_Object saved_object;
18562 struct face *face;
18563
18564 saved_object = it->object;
18565 saved_pos = it->position;
18566
18567 it->what = IT_CHARACTER;
18568 memset (&it->position, 0, sizeof it->position);
18569 it->object = make_number (0);
18570 it->c = it->char_to_display = ' ';
18571 it->len = 1;
18572
18573 /* If the default face was remapped, be sure to use the
18574 remapped face for the appended newline. */
18575 if (default_face_p)
18576 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18577 else if (it->face_before_selective_p)
18578 it->face_id = it->saved_face_id;
18579 face = FACE_FROM_ID (it->f, it->face_id);
18580 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18581
18582 PRODUCE_GLYPHS (it);
18583
18584 it->override_ascent = -1;
18585 it->constrain_row_ascent_descent_p = 0;
18586 it->current_x = saved_x;
18587 it->object = saved_object;
18588 it->position = saved_pos;
18589 it->what = saved_what;
18590 it->face_id = saved_face_id;
18591 it->len = saved_len;
18592 it->c = saved_c;
18593 it->char_to_display = saved_char_to_display;
18594 return 1;
18595 }
18596 }
18597
18598 return 0;
18599 }
18600
18601
18602 /* Extend the face of the last glyph in the text area of IT->glyph_row
18603 to the end of the display line. Called from display_line. If the
18604 glyph row is empty, add a space glyph to it so that we know the
18605 face to draw. Set the glyph row flag fill_line_p. If the glyph
18606 row is R2L, prepend a stretch glyph to cover the empty space to the
18607 left of the leftmost glyph. */
18608
18609 static void
18610 extend_face_to_end_of_line (struct it *it)
18611 {
18612 struct face *face, *default_face;
18613 struct frame *f = it->f;
18614
18615 /* If line is already filled, do nothing. Non window-system frames
18616 get a grace of one more ``pixel'' because their characters are
18617 1-``pixel'' wide, so they hit the equality too early. This grace
18618 is needed only for R2L rows that are not continued, to produce
18619 one extra blank where we could display the cursor. */
18620 if (it->current_x >= it->last_visible_x
18621 + (!FRAME_WINDOW_P (f)
18622 && it->glyph_row->reversed_p
18623 && !it->glyph_row->continued_p))
18624 return;
18625
18626 /* The default face, possibly remapped. */
18627 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18628
18629 /* Face extension extends the background and box of IT->face_id
18630 to the end of the line. If the background equals the background
18631 of the frame, we don't have to do anything. */
18632 if (it->face_before_selective_p)
18633 face = FACE_FROM_ID (f, it->saved_face_id);
18634 else
18635 face = FACE_FROM_ID (f, it->face_id);
18636
18637 if (FRAME_WINDOW_P (f)
18638 && it->glyph_row->displays_text_p
18639 && face->box == FACE_NO_BOX
18640 && face->background == FRAME_BACKGROUND_PIXEL (f)
18641 && !face->stipple
18642 && !it->glyph_row->reversed_p)
18643 return;
18644
18645 /* Set the glyph row flag indicating that the face of the last glyph
18646 in the text area has to be drawn to the end of the text area. */
18647 it->glyph_row->fill_line_p = 1;
18648
18649 /* If current character of IT is not ASCII, make sure we have the
18650 ASCII face. This will be automatically undone the next time
18651 get_next_display_element returns a multibyte character. Note
18652 that the character will always be single byte in unibyte
18653 text. */
18654 if (!ASCII_CHAR_P (it->c))
18655 {
18656 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18657 }
18658
18659 if (FRAME_WINDOW_P (f))
18660 {
18661 /* If the row is empty, add a space with the current face of IT,
18662 so that we know which face to draw. */
18663 if (it->glyph_row->used[TEXT_AREA] == 0)
18664 {
18665 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18666 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18667 it->glyph_row->used[TEXT_AREA] = 1;
18668 }
18669 #ifdef HAVE_WINDOW_SYSTEM
18670 if (it->glyph_row->reversed_p)
18671 {
18672 /* Prepend a stretch glyph to the row, such that the
18673 rightmost glyph will be drawn flushed all the way to the
18674 right margin of the window. The stretch glyph that will
18675 occupy the empty space, if any, to the left of the
18676 glyphs. */
18677 struct font *font = face->font ? face->font : FRAME_FONT (f);
18678 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18679 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18680 struct glyph *g;
18681 int row_width, stretch_ascent, stretch_width;
18682 struct text_pos saved_pos;
18683 int saved_face_id, saved_avoid_cursor;
18684
18685 for (row_width = 0, g = row_start; g < row_end; g++)
18686 row_width += g->pixel_width;
18687 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18688 if (stretch_width > 0)
18689 {
18690 stretch_ascent =
18691 (((it->ascent + it->descent)
18692 * FONT_BASE (font)) / FONT_HEIGHT (font));
18693 saved_pos = it->position;
18694 memset (&it->position, 0, sizeof it->position);
18695 saved_avoid_cursor = it->avoid_cursor_p;
18696 it->avoid_cursor_p = 1;
18697 saved_face_id = it->face_id;
18698 /* The last row's stretch glyph should get the default
18699 face, to avoid painting the rest of the window with
18700 the region face, if the region ends at ZV. */
18701 if (it->glyph_row->ends_at_zv_p)
18702 it->face_id = default_face->id;
18703 else
18704 it->face_id = face->id;
18705 append_stretch_glyph (it, make_number (0), stretch_width,
18706 it->ascent + it->descent, stretch_ascent);
18707 it->position = saved_pos;
18708 it->avoid_cursor_p = saved_avoid_cursor;
18709 it->face_id = saved_face_id;
18710 }
18711 }
18712 #endif /* HAVE_WINDOW_SYSTEM */
18713 }
18714 else
18715 {
18716 /* Save some values that must not be changed. */
18717 int saved_x = it->current_x;
18718 struct text_pos saved_pos;
18719 Lisp_Object saved_object;
18720 enum display_element_type saved_what = it->what;
18721 int saved_face_id = it->face_id;
18722
18723 saved_object = it->object;
18724 saved_pos = it->position;
18725
18726 it->what = IT_CHARACTER;
18727 memset (&it->position, 0, sizeof it->position);
18728 it->object = make_number (0);
18729 it->c = it->char_to_display = ' ';
18730 it->len = 1;
18731 /* The last row's blank glyphs should get the default face, to
18732 avoid painting the rest of the window with the region face,
18733 if the region ends at ZV. */
18734 if (it->glyph_row->ends_at_zv_p)
18735 it->face_id = default_face->id;
18736 else
18737 it->face_id = face->id;
18738
18739 PRODUCE_GLYPHS (it);
18740
18741 while (it->current_x <= it->last_visible_x)
18742 PRODUCE_GLYPHS (it);
18743
18744 /* Don't count these blanks really. It would let us insert a left
18745 truncation glyph below and make us set the cursor on them, maybe. */
18746 it->current_x = saved_x;
18747 it->object = saved_object;
18748 it->position = saved_pos;
18749 it->what = saved_what;
18750 it->face_id = saved_face_id;
18751 }
18752 }
18753
18754
18755 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18756 trailing whitespace. */
18757
18758 static int
18759 trailing_whitespace_p (ptrdiff_t charpos)
18760 {
18761 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18762 int c = 0;
18763
18764 while (bytepos < ZV_BYTE
18765 && (c = FETCH_CHAR (bytepos),
18766 c == ' ' || c == '\t'))
18767 ++bytepos;
18768
18769 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18770 {
18771 if (bytepos != PT_BYTE)
18772 return 1;
18773 }
18774 return 0;
18775 }
18776
18777
18778 /* Highlight trailing whitespace, if any, in ROW. */
18779
18780 static void
18781 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18782 {
18783 int used = row->used[TEXT_AREA];
18784
18785 if (used)
18786 {
18787 struct glyph *start = row->glyphs[TEXT_AREA];
18788 struct glyph *glyph = start + used - 1;
18789
18790 if (row->reversed_p)
18791 {
18792 /* Right-to-left rows need to be processed in the opposite
18793 direction, so swap the edge pointers. */
18794 glyph = start;
18795 start = row->glyphs[TEXT_AREA] + used - 1;
18796 }
18797
18798 /* Skip over glyphs inserted to display the cursor at the
18799 end of a line, for extending the face of the last glyph
18800 to the end of the line on terminals, and for truncation
18801 and continuation glyphs. */
18802 if (!row->reversed_p)
18803 {
18804 while (glyph >= start
18805 && glyph->type == CHAR_GLYPH
18806 && INTEGERP (glyph->object))
18807 --glyph;
18808 }
18809 else
18810 {
18811 while (glyph <= start
18812 && glyph->type == CHAR_GLYPH
18813 && INTEGERP (glyph->object))
18814 ++glyph;
18815 }
18816
18817 /* If last glyph is a space or stretch, and it's trailing
18818 whitespace, set the face of all trailing whitespace glyphs in
18819 IT->glyph_row to `trailing-whitespace'. */
18820 if ((row->reversed_p ? glyph <= start : glyph >= start)
18821 && BUFFERP (glyph->object)
18822 && (glyph->type == STRETCH_GLYPH
18823 || (glyph->type == CHAR_GLYPH
18824 && glyph->u.ch == ' '))
18825 && trailing_whitespace_p (glyph->charpos))
18826 {
18827 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18828 if (face_id < 0)
18829 return;
18830
18831 if (!row->reversed_p)
18832 {
18833 while (glyph >= start
18834 && BUFFERP (glyph->object)
18835 && (glyph->type == STRETCH_GLYPH
18836 || (glyph->type == CHAR_GLYPH
18837 && glyph->u.ch == ' ')))
18838 (glyph--)->face_id = face_id;
18839 }
18840 else
18841 {
18842 while (glyph <= start
18843 && BUFFERP (glyph->object)
18844 && (glyph->type == STRETCH_GLYPH
18845 || (glyph->type == CHAR_GLYPH
18846 && glyph->u.ch == ' ')))
18847 (glyph++)->face_id = face_id;
18848 }
18849 }
18850 }
18851 }
18852
18853
18854 /* Value is non-zero if glyph row ROW should be
18855 used to hold the cursor. */
18856
18857 static int
18858 cursor_row_p (struct glyph_row *row)
18859 {
18860 int result = 1;
18861
18862 if (PT == CHARPOS (row->end.pos)
18863 || PT == MATRIX_ROW_END_CHARPOS (row))
18864 {
18865 /* Suppose the row ends on a string.
18866 Unless the row is continued, that means it ends on a newline
18867 in the string. If it's anything other than a display string
18868 (e.g., a before-string from an overlay), we don't want the
18869 cursor there. (This heuristic seems to give the optimal
18870 behavior for the various types of multi-line strings.)
18871 One exception: if the string has `cursor' property on one of
18872 its characters, we _do_ want the cursor there. */
18873 if (CHARPOS (row->end.string_pos) >= 0)
18874 {
18875 if (row->continued_p)
18876 result = 1;
18877 else
18878 {
18879 /* Check for `display' property. */
18880 struct glyph *beg = row->glyphs[TEXT_AREA];
18881 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18882 struct glyph *glyph;
18883
18884 result = 0;
18885 for (glyph = end; glyph >= beg; --glyph)
18886 if (STRINGP (glyph->object))
18887 {
18888 Lisp_Object prop
18889 = Fget_char_property (make_number (PT),
18890 Qdisplay, Qnil);
18891 result =
18892 (!NILP (prop)
18893 && display_prop_string_p (prop, glyph->object));
18894 /* If there's a `cursor' property on one of the
18895 string's characters, this row is a cursor row,
18896 even though this is not a display string. */
18897 if (!result)
18898 {
18899 Lisp_Object s = glyph->object;
18900
18901 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18902 {
18903 ptrdiff_t gpos = glyph->charpos;
18904
18905 if (!NILP (Fget_char_property (make_number (gpos),
18906 Qcursor, s)))
18907 {
18908 result = 1;
18909 break;
18910 }
18911 }
18912 }
18913 break;
18914 }
18915 }
18916 }
18917 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18918 {
18919 /* If the row ends in middle of a real character,
18920 and the line is continued, we want the cursor here.
18921 That's because CHARPOS (ROW->end.pos) would equal
18922 PT if PT is before the character. */
18923 if (!row->ends_in_ellipsis_p)
18924 result = row->continued_p;
18925 else
18926 /* If the row ends in an ellipsis, then
18927 CHARPOS (ROW->end.pos) will equal point after the
18928 invisible text. We want that position to be displayed
18929 after the ellipsis. */
18930 result = 0;
18931 }
18932 /* If the row ends at ZV, display the cursor at the end of that
18933 row instead of at the start of the row below. */
18934 else if (row->ends_at_zv_p)
18935 result = 1;
18936 else
18937 result = 0;
18938 }
18939
18940 return result;
18941 }
18942
18943 \f
18944
18945 /* Push the property PROP so that it will be rendered at the current
18946 position in IT. Return 1 if PROP was successfully pushed, 0
18947 otherwise. Called from handle_line_prefix to handle the
18948 `line-prefix' and `wrap-prefix' properties. */
18949
18950 static int
18951 push_prefix_prop (struct it *it, Lisp_Object prop)
18952 {
18953 struct text_pos pos =
18954 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18955
18956 eassert (it->method == GET_FROM_BUFFER
18957 || it->method == GET_FROM_DISPLAY_VECTOR
18958 || it->method == GET_FROM_STRING);
18959
18960 /* We need to save the current buffer/string position, so it will be
18961 restored by pop_it, because iterate_out_of_display_property
18962 depends on that being set correctly, but some situations leave
18963 it->position not yet set when this function is called. */
18964 push_it (it, &pos);
18965
18966 if (STRINGP (prop))
18967 {
18968 if (SCHARS (prop) == 0)
18969 {
18970 pop_it (it);
18971 return 0;
18972 }
18973
18974 it->string = prop;
18975 it->string_from_prefix_prop_p = 1;
18976 it->multibyte_p = STRING_MULTIBYTE (it->string);
18977 it->current.overlay_string_index = -1;
18978 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18979 it->end_charpos = it->string_nchars = SCHARS (it->string);
18980 it->method = GET_FROM_STRING;
18981 it->stop_charpos = 0;
18982 it->prev_stop = 0;
18983 it->base_level_stop = 0;
18984
18985 /* Force paragraph direction to be that of the parent
18986 buffer/string. */
18987 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18988 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18989 else
18990 it->paragraph_embedding = L2R;
18991
18992 /* Set up the bidi iterator for this display string. */
18993 if (it->bidi_p)
18994 {
18995 it->bidi_it.string.lstring = it->string;
18996 it->bidi_it.string.s = NULL;
18997 it->bidi_it.string.schars = it->end_charpos;
18998 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18999 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19000 it->bidi_it.string.unibyte = !it->multibyte_p;
19001 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19002 }
19003 }
19004 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19005 {
19006 it->method = GET_FROM_STRETCH;
19007 it->object = prop;
19008 }
19009 #ifdef HAVE_WINDOW_SYSTEM
19010 else if (IMAGEP (prop))
19011 {
19012 it->what = IT_IMAGE;
19013 it->image_id = lookup_image (it->f, prop);
19014 it->method = GET_FROM_IMAGE;
19015 }
19016 #endif /* HAVE_WINDOW_SYSTEM */
19017 else
19018 {
19019 pop_it (it); /* bogus display property, give up */
19020 return 0;
19021 }
19022
19023 return 1;
19024 }
19025
19026 /* Return the character-property PROP at the current position in IT. */
19027
19028 static Lisp_Object
19029 get_it_property (struct it *it, Lisp_Object prop)
19030 {
19031 Lisp_Object position;
19032
19033 if (STRINGP (it->object))
19034 position = make_number (IT_STRING_CHARPOS (*it));
19035 else if (BUFFERP (it->object))
19036 position = make_number (IT_CHARPOS (*it));
19037 else
19038 return Qnil;
19039
19040 return Fget_char_property (position, prop, it->object);
19041 }
19042
19043 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19044
19045 static void
19046 handle_line_prefix (struct it *it)
19047 {
19048 Lisp_Object prefix;
19049
19050 if (it->continuation_lines_width > 0)
19051 {
19052 prefix = get_it_property (it, Qwrap_prefix);
19053 if (NILP (prefix))
19054 prefix = Vwrap_prefix;
19055 }
19056 else
19057 {
19058 prefix = get_it_property (it, Qline_prefix);
19059 if (NILP (prefix))
19060 prefix = Vline_prefix;
19061 }
19062 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19063 {
19064 /* If the prefix is wider than the window, and we try to wrap
19065 it, it would acquire its own wrap prefix, and so on till the
19066 iterator stack overflows. So, don't wrap the prefix. */
19067 it->line_wrap = TRUNCATE;
19068 it->avoid_cursor_p = 1;
19069 }
19070 }
19071
19072 \f
19073
19074 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19075 only for R2L lines from display_line and display_string, when they
19076 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19077 the line/string needs to be continued on the next glyph row. */
19078 static void
19079 unproduce_glyphs (struct it *it, int n)
19080 {
19081 struct glyph *glyph, *end;
19082
19083 eassert (it->glyph_row);
19084 eassert (it->glyph_row->reversed_p);
19085 eassert (it->area == TEXT_AREA);
19086 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19087
19088 if (n > it->glyph_row->used[TEXT_AREA])
19089 n = it->glyph_row->used[TEXT_AREA];
19090 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19091 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19092 for ( ; glyph < end; glyph++)
19093 glyph[-n] = *glyph;
19094 }
19095
19096 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19097 and ROW->maxpos. */
19098 static void
19099 find_row_edges (struct it *it, struct glyph_row *row,
19100 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19101 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19102 {
19103 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19104 lines' rows is implemented for bidi-reordered rows. */
19105
19106 /* ROW->minpos is the value of min_pos, the minimal buffer position
19107 we have in ROW, or ROW->start.pos if that is smaller. */
19108 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19109 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19110 else
19111 /* We didn't find buffer positions smaller than ROW->start, or
19112 didn't find _any_ valid buffer positions in any of the glyphs,
19113 so we must trust the iterator's computed positions. */
19114 row->minpos = row->start.pos;
19115 if (max_pos <= 0)
19116 {
19117 max_pos = CHARPOS (it->current.pos);
19118 max_bpos = BYTEPOS (it->current.pos);
19119 }
19120
19121 /* Here are the various use-cases for ending the row, and the
19122 corresponding values for ROW->maxpos:
19123
19124 Line ends in a newline from buffer eol_pos + 1
19125 Line is continued from buffer max_pos + 1
19126 Line is truncated on right it->current.pos
19127 Line ends in a newline from string max_pos + 1(*)
19128 (*) + 1 only when line ends in a forward scan
19129 Line is continued from string max_pos
19130 Line is continued from display vector max_pos
19131 Line is entirely from a string min_pos == max_pos
19132 Line is entirely from a display vector min_pos == max_pos
19133 Line that ends at ZV ZV
19134
19135 If you discover other use-cases, please add them here as
19136 appropriate. */
19137 if (row->ends_at_zv_p)
19138 row->maxpos = it->current.pos;
19139 else if (row->used[TEXT_AREA])
19140 {
19141 int seen_this_string = 0;
19142 struct glyph_row *r1 = row - 1;
19143
19144 /* Did we see the same display string on the previous row? */
19145 if (STRINGP (it->object)
19146 /* this is not the first row */
19147 && row > it->w->desired_matrix->rows
19148 /* previous row is not the header line */
19149 && !r1->mode_line_p
19150 /* previous row also ends in a newline from a string */
19151 && r1->ends_in_newline_from_string_p)
19152 {
19153 struct glyph *start, *end;
19154
19155 /* Search for the last glyph of the previous row that came
19156 from buffer or string. Depending on whether the row is
19157 L2R or R2L, we need to process it front to back or the
19158 other way round. */
19159 if (!r1->reversed_p)
19160 {
19161 start = r1->glyphs[TEXT_AREA];
19162 end = start + r1->used[TEXT_AREA];
19163 /* Glyphs inserted by redisplay have an integer (zero)
19164 as their object. */
19165 while (end > start
19166 && INTEGERP ((end - 1)->object)
19167 && (end - 1)->charpos <= 0)
19168 --end;
19169 if (end > start)
19170 {
19171 if (EQ ((end - 1)->object, it->object))
19172 seen_this_string = 1;
19173 }
19174 else
19175 /* If all the glyphs of the previous row were inserted
19176 by redisplay, it means the previous row was
19177 produced from a single newline, which is only
19178 possible if that newline came from the same string
19179 as the one which produced this ROW. */
19180 seen_this_string = 1;
19181 }
19182 else
19183 {
19184 end = r1->glyphs[TEXT_AREA] - 1;
19185 start = end + r1->used[TEXT_AREA];
19186 while (end < start
19187 && INTEGERP ((end + 1)->object)
19188 && (end + 1)->charpos <= 0)
19189 ++end;
19190 if (end < start)
19191 {
19192 if (EQ ((end + 1)->object, it->object))
19193 seen_this_string = 1;
19194 }
19195 else
19196 seen_this_string = 1;
19197 }
19198 }
19199 /* Take note of each display string that covers a newline only
19200 once, the first time we see it. This is for when a display
19201 string includes more than one newline in it. */
19202 if (row->ends_in_newline_from_string_p && !seen_this_string)
19203 {
19204 /* If we were scanning the buffer forward when we displayed
19205 the string, we want to account for at least one buffer
19206 position that belongs to this row (position covered by
19207 the display string), so that cursor positioning will
19208 consider this row as a candidate when point is at the end
19209 of the visual line represented by this row. This is not
19210 required when scanning back, because max_pos will already
19211 have a much larger value. */
19212 if (CHARPOS (row->end.pos) > max_pos)
19213 INC_BOTH (max_pos, max_bpos);
19214 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19215 }
19216 else if (CHARPOS (it->eol_pos) > 0)
19217 SET_TEXT_POS (row->maxpos,
19218 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19219 else if (row->continued_p)
19220 {
19221 /* If max_pos is different from IT's current position, it
19222 means IT->method does not belong to the display element
19223 at max_pos. However, it also means that the display
19224 element at max_pos was displayed in its entirety on this
19225 line, which is equivalent to saying that the next line
19226 starts at the next buffer position. */
19227 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19228 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19229 else
19230 {
19231 INC_BOTH (max_pos, max_bpos);
19232 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19233 }
19234 }
19235 else if (row->truncated_on_right_p)
19236 /* display_line already called reseat_at_next_visible_line_start,
19237 which puts the iterator at the beginning of the next line, in
19238 the logical order. */
19239 row->maxpos = it->current.pos;
19240 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19241 /* A line that is entirely from a string/image/stretch... */
19242 row->maxpos = row->minpos;
19243 else
19244 emacs_abort ();
19245 }
19246 else
19247 row->maxpos = it->current.pos;
19248 }
19249
19250 /* Construct the glyph row IT->glyph_row in the desired matrix of
19251 IT->w from text at the current position of IT. See dispextern.h
19252 for an overview of struct it. Value is non-zero if
19253 IT->glyph_row displays text, as opposed to a line displaying ZV
19254 only. */
19255
19256 static int
19257 display_line (struct it *it)
19258 {
19259 struct glyph_row *row = it->glyph_row;
19260 Lisp_Object overlay_arrow_string;
19261 struct it wrap_it;
19262 void *wrap_data = NULL;
19263 int may_wrap = 0, wrap_x IF_LINT (= 0);
19264 int wrap_row_used = -1;
19265 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19266 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19267 int wrap_row_extra_line_spacing IF_LINT (= 0);
19268 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19269 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19270 int cvpos;
19271 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19272 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19273
19274 /* We always start displaying at hpos zero even if hscrolled. */
19275 eassert (it->hpos == 0 && it->current_x == 0);
19276
19277 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19278 >= it->w->desired_matrix->nrows)
19279 {
19280 it->w->nrows_scale_factor++;
19281 fonts_changed_p = 1;
19282 return 0;
19283 }
19284
19285 /* Is IT->w showing the region? */
19286 wset_region_showing (it->w, it->region_beg_charpos > 0 ? Qt : Qnil);
19287
19288 /* Clear the result glyph row and enable it. */
19289 prepare_desired_row (row);
19290
19291 row->y = it->current_y;
19292 row->start = it->start;
19293 row->continuation_lines_width = it->continuation_lines_width;
19294 row->displays_text_p = 1;
19295 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19296 it->starts_in_middle_of_char_p = 0;
19297
19298 /* Arrange the overlays nicely for our purposes. Usually, we call
19299 display_line on only one line at a time, in which case this
19300 can't really hurt too much, or we call it on lines which appear
19301 one after another in the buffer, in which case all calls to
19302 recenter_overlay_lists but the first will be pretty cheap. */
19303 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19304
19305 /* Move over display elements that are not visible because we are
19306 hscrolled. This may stop at an x-position < IT->first_visible_x
19307 if the first glyph is partially visible or if we hit a line end. */
19308 if (it->current_x < it->first_visible_x)
19309 {
19310 enum move_it_result move_result;
19311
19312 this_line_min_pos = row->start.pos;
19313 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19314 MOVE_TO_POS | MOVE_TO_X);
19315 /* If we are under a large hscroll, move_it_in_display_line_to
19316 could hit the end of the line without reaching
19317 it->first_visible_x. Pretend that we did reach it. This is
19318 especially important on a TTY, where we will call
19319 extend_face_to_end_of_line, which needs to know how many
19320 blank glyphs to produce. */
19321 if (it->current_x < it->first_visible_x
19322 && (move_result == MOVE_NEWLINE_OR_CR
19323 || move_result == MOVE_POS_MATCH_OR_ZV))
19324 it->current_x = it->first_visible_x;
19325
19326 /* Record the smallest positions seen while we moved over
19327 display elements that are not visible. This is needed by
19328 redisplay_internal for optimizing the case where the cursor
19329 stays inside the same line. The rest of this function only
19330 considers positions that are actually displayed, so
19331 RECORD_MAX_MIN_POS will not otherwise record positions that
19332 are hscrolled to the left of the left edge of the window. */
19333 min_pos = CHARPOS (this_line_min_pos);
19334 min_bpos = BYTEPOS (this_line_min_pos);
19335 }
19336 else
19337 {
19338 /* We only do this when not calling `move_it_in_display_line_to'
19339 above, because move_it_in_display_line_to calls
19340 handle_line_prefix itself. */
19341 handle_line_prefix (it);
19342 }
19343
19344 /* Get the initial row height. This is either the height of the
19345 text hscrolled, if there is any, or zero. */
19346 row->ascent = it->max_ascent;
19347 row->height = it->max_ascent + it->max_descent;
19348 row->phys_ascent = it->max_phys_ascent;
19349 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19350 row->extra_line_spacing = it->max_extra_line_spacing;
19351
19352 /* Utility macro to record max and min buffer positions seen until now. */
19353 #define RECORD_MAX_MIN_POS(IT) \
19354 do \
19355 { \
19356 int composition_p = !STRINGP ((IT)->string) \
19357 && ((IT)->what == IT_COMPOSITION); \
19358 ptrdiff_t current_pos = \
19359 composition_p ? (IT)->cmp_it.charpos \
19360 : IT_CHARPOS (*(IT)); \
19361 ptrdiff_t current_bpos = \
19362 composition_p ? CHAR_TO_BYTE (current_pos) \
19363 : IT_BYTEPOS (*(IT)); \
19364 if (current_pos < min_pos) \
19365 { \
19366 min_pos = current_pos; \
19367 min_bpos = current_bpos; \
19368 } \
19369 if (IT_CHARPOS (*it) > max_pos) \
19370 { \
19371 max_pos = IT_CHARPOS (*it); \
19372 max_bpos = IT_BYTEPOS (*it); \
19373 } \
19374 } \
19375 while (0)
19376
19377 /* Loop generating characters. The loop is left with IT on the next
19378 character to display. */
19379 while (1)
19380 {
19381 int n_glyphs_before, hpos_before, x_before;
19382 int x, nglyphs;
19383 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19384
19385 /* Retrieve the next thing to display. Value is zero if end of
19386 buffer reached. */
19387 if (!get_next_display_element (it))
19388 {
19389 /* Maybe add a space at the end of this line that is used to
19390 display the cursor there under X. Set the charpos of the
19391 first glyph of blank lines not corresponding to any text
19392 to -1. */
19393 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19394 row->exact_window_width_line_p = 1;
19395 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19396 || row->used[TEXT_AREA] == 0)
19397 {
19398 row->glyphs[TEXT_AREA]->charpos = -1;
19399 row->displays_text_p = 0;
19400
19401 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19402 && (!MINI_WINDOW_P (it->w)
19403 || (minibuf_level && EQ (it->window, minibuf_window))))
19404 row->indicate_empty_line_p = 1;
19405 }
19406
19407 it->continuation_lines_width = 0;
19408 row->ends_at_zv_p = 1;
19409 /* A row that displays right-to-left text must always have
19410 its last face extended all the way to the end of line,
19411 even if this row ends in ZV, because we still write to
19412 the screen left to right. We also need to extend the
19413 last face if the default face is remapped to some
19414 different face, otherwise the functions that clear
19415 portions of the screen will clear with the default face's
19416 background color. */
19417 if (row->reversed_p
19418 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19419 extend_face_to_end_of_line (it);
19420 break;
19421 }
19422
19423 /* Now, get the metrics of what we want to display. This also
19424 generates glyphs in `row' (which is IT->glyph_row). */
19425 n_glyphs_before = row->used[TEXT_AREA];
19426 x = it->current_x;
19427
19428 /* Remember the line height so far in case the next element doesn't
19429 fit on the line. */
19430 if (it->line_wrap != TRUNCATE)
19431 {
19432 ascent = it->max_ascent;
19433 descent = it->max_descent;
19434 phys_ascent = it->max_phys_ascent;
19435 phys_descent = it->max_phys_descent;
19436
19437 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19438 {
19439 if (IT_DISPLAYING_WHITESPACE (it))
19440 may_wrap = 1;
19441 else if (may_wrap)
19442 {
19443 SAVE_IT (wrap_it, *it, wrap_data);
19444 wrap_x = x;
19445 wrap_row_used = row->used[TEXT_AREA];
19446 wrap_row_ascent = row->ascent;
19447 wrap_row_height = row->height;
19448 wrap_row_phys_ascent = row->phys_ascent;
19449 wrap_row_phys_height = row->phys_height;
19450 wrap_row_extra_line_spacing = row->extra_line_spacing;
19451 wrap_row_min_pos = min_pos;
19452 wrap_row_min_bpos = min_bpos;
19453 wrap_row_max_pos = max_pos;
19454 wrap_row_max_bpos = max_bpos;
19455 may_wrap = 0;
19456 }
19457 }
19458 }
19459
19460 PRODUCE_GLYPHS (it);
19461
19462 /* If this display element was in marginal areas, continue with
19463 the next one. */
19464 if (it->area != TEXT_AREA)
19465 {
19466 row->ascent = max (row->ascent, it->max_ascent);
19467 row->height = max (row->height, it->max_ascent + it->max_descent);
19468 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19469 row->phys_height = max (row->phys_height,
19470 it->max_phys_ascent + it->max_phys_descent);
19471 row->extra_line_spacing = max (row->extra_line_spacing,
19472 it->max_extra_line_spacing);
19473 set_iterator_to_next (it, 1);
19474 continue;
19475 }
19476
19477 /* Does the display element fit on the line? If we truncate
19478 lines, we should draw past the right edge of the window. If
19479 we don't truncate, we want to stop so that we can display the
19480 continuation glyph before the right margin. If lines are
19481 continued, there are two possible strategies for characters
19482 resulting in more than 1 glyph (e.g. tabs): Display as many
19483 glyphs as possible in this line and leave the rest for the
19484 continuation line, or display the whole element in the next
19485 line. Original redisplay did the former, so we do it also. */
19486 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19487 hpos_before = it->hpos;
19488 x_before = x;
19489
19490 if (/* Not a newline. */
19491 nglyphs > 0
19492 /* Glyphs produced fit entirely in the line. */
19493 && it->current_x < it->last_visible_x)
19494 {
19495 it->hpos += nglyphs;
19496 row->ascent = max (row->ascent, it->max_ascent);
19497 row->height = max (row->height, it->max_ascent + it->max_descent);
19498 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19499 row->phys_height = max (row->phys_height,
19500 it->max_phys_ascent + it->max_phys_descent);
19501 row->extra_line_spacing = max (row->extra_line_spacing,
19502 it->max_extra_line_spacing);
19503 if (it->current_x - it->pixel_width < it->first_visible_x)
19504 row->x = x - it->first_visible_x;
19505 /* Record the maximum and minimum buffer positions seen so
19506 far in glyphs that will be displayed by this row. */
19507 if (it->bidi_p)
19508 RECORD_MAX_MIN_POS (it);
19509 }
19510 else
19511 {
19512 int i, new_x;
19513 struct glyph *glyph;
19514
19515 for (i = 0; i < nglyphs; ++i, x = new_x)
19516 {
19517 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19518 new_x = x + glyph->pixel_width;
19519
19520 if (/* Lines are continued. */
19521 it->line_wrap != TRUNCATE
19522 && (/* Glyph doesn't fit on the line. */
19523 new_x > it->last_visible_x
19524 /* Or it fits exactly on a window system frame. */
19525 || (new_x == it->last_visible_x
19526 && FRAME_WINDOW_P (it->f)
19527 && (row->reversed_p
19528 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19529 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19530 {
19531 /* End of a continued line. */
19532
19533 if (it->hpos == 0
19534 || (new_x == it->last_visible_x
19535 && FRAME_WINDOW_P (it->f)
19536 && (row->reversed_p
19537 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19538 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19539 {
19540 /* Current glyph is the only one on the line or
19541 fits exactly on the line. We must continue
19542 the line because we can't draw the cursor
19543 after the glyph. */
19544 row->continued_p = 1;
19545 it->current_x = new_x;
19546 it->continuation_lines_width += new_x;
19547 ++it->hpos;
19548 if (i == nglyphs - 1)
19549 {
19550 /* If line-wrap is on, check if a previous
19551 wrap point was found. */
19552 if (wrap_row_used > 0
19553 /* Even if there is a previous wrap
19554 point, continue the line here as
19555 usual, if (i) the previous character
19556 was a space or tab AND (ii) the
19557 current character is not. */
19558 && (!may_wrap
19559 || IT_DISPLAYING_WHITESPACE (it)))
19560 goto back_to_wrap;
19561
19562 /* Record the maximum and minimum buffer
19563 positions seen so far in glyphs that will be
19564 displayed by this row. */
19565 if (it->bidi_p)
19566 RECORD_MAX_MIN_POS (it);
19567 set_iterator_to_next (it, 1);
19568 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19569 {
19570 if (!get_next_display_element (it))
19571 {
19572 row->exact_window_width_line_p = 1;
19573 it->continuation_lines_width = 0;
19574 row->continued_p = 0;
19575 row->ends_at_zv_p = 1;
19576 }
19577 else if (ITERATOR_AT_END_OF_LINE_P (it))
19578 {
19579 row->continued_p = 0;
19580 row->exact_window_width_line_p = 1;
19581 }
19582 }
19583 }
19584 else if (it->bidi_p)
19585 RECORD_MAX_MIN_POS (it);
19586 }
19587 else if (CHAR_GLYPH_PADDING_P (*glyph)
19588 && !FRAME_WINDOW_P (it->f))
19589 {
19590 /* A padding glyph that doesn't fit on this line.
19591 This means the whole character doesn't fit
19592 on the line. */
19593 if (row->reversed_p)
19594 unproduce_glyphs (it, row->used[TEXT_AREA]
19595 - n_glyphs_before);
19596 row->used[TEXT_AREA] = n_glyphs_before;
19597
19598 /* Fill the rest of the row with continuation
19599 glyphs like in 20.x. */
19600 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19601 < row->glyphs[1 + TEXT_AREA])
19602 produce_special_glyphs (it, IT_CONTINUATION);
19603
19604 row->continued_p = 1;
19605 it->current_x = x_before;
19606 it->continuation_lines_width += x_before;
19607
19608 /* Restore the height to what it was before the
19609 element not fitting on the line. */
19610 it->max_ascent = ascent;
19611 it->max_descent = descent;
19612 it->max_phys_ascent = phys_ascent;
19613 it->max_phys_descent = phys_descent;
19614 }
19615 else if (wrap_row_used > 0)
19616 {
19617 back_to_wrap:
19618 if (row->reversed_p)
19619 unproduce_glyphs (it,
19620 row->used[TEXT_AREA] - wrap_row_used);
19621 RESTORE_IT (it, &wrap_it, wrap_data);
19622 it->continuation_lines_width += wrap_x;
19623 row->used[TEXT_AREA] = wrap_row_used;
19624 row->ascent = wrap_row_ascent;
19625 row->height = wrap_row_height;
19626 row->phys_ascent = wrap_row_phys_ascent;
19627 row->phys_height = wrap_row_phys_height;
19628 row->extra_line_spacing = wrap_row_extra_line_spacing;
19629 min_pos = wrap_row_min_pos;
19630 min_bpos = wrap_row_min_bpos;
19631 max_pos = wrap_row_max_pos;
19632 max_bpos = wrap_row_max_bpos;
19633 row->continued_p = 1;
19634 row->ends_at_zv_p = 0;
19635 row->exact_window_width_line_p = 0;
19636 it->continuation_lines_width += x;
19637
19638 /* Make sure that a non-default face is extended
19639 up to the right margin of the window. */
19640 extend_face_to_end_of_line (it);
19641 }
19642 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19643 {
19644 /* A TAB that extends past the right edge of the
19645 window. This produces a single glyph on
19646 window system frames. We leave the glyph in
19647 this row and let it fill the row, but don't
19648 consume the TAB. */
19649 if ((row->reversed_p
19650 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19651 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19652 produce_special_glyphs (it, IT_CONTINUATION);
19653 it->continuation_lines_width += it->last_visible_x;
19654 row->ends_in_middle_of_char_p = 1;
19655 row->continued_p = 1;
19656 glyph->pixel_width = it->last_visible_x - x;
19657 it->starts_in_middle_of_char_p = 1;
19658 }
19659 else
19660 {
19661 /* Something other than a TAB that draws past
19662 the right edge of the window. Restore
19663 positions to values before the element. */
19664 if (row->reversed_p)
19665 unproduce_glyphs (it, row->used[TEXT_AREA]
19666 - (n_glyphs_before + i));
19667 row->used[TEXT_AREA] = n_glyphs_before + i;
19668
19669 /* Display continuation glyphs. */
19670 it->current_x = x_before;
19671 it->continuation_lines_width += x;
19672 if (!FRAME_WINDOW_P (it->f)
19673 || (row->reversed_p
19674 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19675 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19676 produce_special_glyphs (it, IT_CONTINUATION);
19677 row->continued_p = 1;
19678
19679 extend_face_to_end_of_line (it);
19680
19681 if (nglyphs > 1 && i > 0)
19682 {
19683 row->ends_in_middle_of_char_p = 1;
19684 it->starts_in_middle_of_char_p = 1;
19685 }
19686
19687 /* Restore the height to what it was before the
19688 element not fitting on the line. */
19689 it->max_ascent = ascent;
19690 it->max_descent = descent;
19691 it->max_phys_ascent = phys_ascent;
19692 it->max_phys_descent = phys_descent;
19693 }
19694
19695 break;
19696 }
19697 else if (new_x > it->first_visible_x)
19698 {
19699 /* Increment number of glyphs actually displayed. */
19700 ++it->hpos;
19701
19702 /* Record the maximum and minimum buffer positions
19703 seen so far in glyphs that will be displayed by
19704 this row. */
19705 if (it->bidi_p)
19706 RECORD_MAX_MIN_POS (it);
19707
19708 if (x < it->first_visible_x)
19709 /* Glyph is partially visible, i.e. row starts at
19710 negative X position. */
19711 row->x = x - it->first_visible_x;
19712 }
19713 else
19714 {
19715 /* Glyph is completely off the left margin of the
19716 window. This should not happen because of the
19717 move_it_in_display_line at the start of this
19718 function, unless the text display area of the
19719 window is empty. */
19720 eassert (it->first_visible_x <= it->last_visible_x);
19721 }
19722 }
19723 /* Even if this display element produced no glyphs at all,
19724 we want to record its position. */
19725 if (it->bidi_p && nglyphs == 0)
19726 RECORD_MAX_MIN_POS (it);
19727
19728 row->ascent = max (row->ascent, it->max_ascent);
19729 row->height = max (row->height, it->max_ascent + it->max_descent);
19730 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19731 row->phys_height = max (row->phys_height,
19732 it->max_phys_ascent + it->max_phys_descent);
19733 row->extra_line_spacing = max (row->extra_line_spacing,
19734 it->max_extra_line_spacing);
19735
19736 /* End of this display line if row is continued. */
19737 if (row->continued_p || row->ends_at_zv_p)
19738 break;
19739 }
19740
19741 at_end_of_line:
19742 /* Is this a line end? If yes, we're also done, after making
19743 sure that a non-default face is extended up to the right
19744 margin of the window. */
19745 if (ITERATOR_AT_END_OF_LINE_P (it))
19746 {
19747 int used_before = row->used[TEXT_AREA];
19748
19749 row->ends_in_newline_from_string_p = STRINGP (it->object);
19750
19751 /* Add a space at the end of the line that is used to
19752 display the cursor there. */
19753 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19754 append_space_for_newline (it, 0);
19755
19756 /* Extend the face to the end of the line. */
19757 extend_face_to_end_of_line (it);
19758
19759 /* Make sure we have the position. */
19760 if (used_before == 0)
19761 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19762
19763 /* Record the position of the newline, for use in
19764 find_row_edges. */
19765 it->eol_pos = it->current.pos;
19766
19767 /* Consume the line end. This skips over invisible lines. */
19768 set_iterator_to_next (it, 1);
19769 it->continuation_lines_width = 0;
19770 break;
19771 }
19772
19773 /* Proceed with next display element. Note that this skips
19774 over lines invisible because of selective display. */
19775 set_iterator_to_next (it, 1);
19776
19777 /* If we truncate lines, we are done when the last displayed
19778 glyphs reach past the right margin of the window. */
19779 if (it->line_wrap == TRUNCATE
19780 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19781 ? (it->current_x >= it->last_visible_x)
19782 : (it->current_x > it->last_visible_x)))
19783 {
19784 /* Maybe add truncation glyphs. */
19785 if (!FRAME_WINDOW_P (it->f)
19786 || (row->reversed_p
19787 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19788 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19789 {
19790 int i, n;
19791
19792 if (!row->reversed_p)
19793 {
19794 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19795 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19796 break;
19797 }
19798 else
19799 {
19800 for (i = 0; i < row->used[TEXT_AREA]; i++)
19801 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19802 break;
19803 /* Remove any padding glyphs at the front of ROW, to
19804 make room for the truncation glyphs we will be
19805 adding below. The loop below always inserts at
19806 least one truncation glyph, so also remove the
19807 last glyph added to ROW. */
19808 unproduce_glyphs (it, i + 1);
19809 /* Adjust i for the loop below. */
19810 i = row->used[TEXT_AREA] - (i + 1);
19811 }
19812
19813 it->current_x = x_before;
19814 if (!FRAME_WINDOW_P (it->f))
19815 {
19816 for (n = row->used[TEXT_AREA]; i < n; ++i)
19817 {
19818 row->used[TEXT_AREA] = i;
19819 produce_special_glyphs (it, IT_TRUNCATION);
19820 }
19821 }
19822 else
19823 {
19824 row->used[TEXT_AREA] = i;
19825 produce_special_glyphs (it, IT_TRUNCATION);
19826 }
19827 }
19828 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19829 {
19830 /* Don't truncate if we can overflow newline into fringe. */
19831 if (!get_next_display_element (it))
19832 {
19833 it->continuation_lines_width = 0;
19834 row->ends_at_zv_p = 1;
19835 row->exact_window_width_line_p = 1;
19836 break;
19837 }
19838 if (ITERATOR_AT_END_OF_LINE_P (it))
19839 {
19840 row->exact_window_width_line_p = 1;
19841 goto at_end_of_line;
19842 }
19843 it->current_x = x_before;
19844 }
19845
19846 row->truncated_on_right_p = 1;
19847 it->continuation_lines_width = 0;
19848 reseat_at_next_visible_line_start (it, 0);
19849 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19850 it->hpos = hpos_before;
19851 break;
19852 }
19853 }
19854
19855 if (wrap_data)
19856 bidi_unshelve_cache (wrap_data, 1);
19857
19858 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19859 at the left window margin. */
19860 if (it->first_visible_x
19861 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19862 {
19863 if (!FRAME_WINDOW_P (it->f)
19864 || (row->reversed_p
19865 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19866 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19867 insert_left_trunc_glyphs (it);
19868 row->truncated_on_left_p = 1;
19869 }
19870
19871 /* Remember the position at which this line ends.
19872
19873 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19874 cannot be before the call to find_row_edges below, since that is
19875 where these positions are determined. */
19876 row->end = it->current;
19877 if (!it->bidi_p)
19878 {
19879 row->minpos = row->start.pos;
19880 row->maxpos = row->end.pos;
19881 }
19882 else
19883 {
19884 /* ROW->minpos and ROW->maxpos must be the smallest and
19885 `1 + the largest' buffer positions in ROW. But if ROW was
19886 bidi-reordered, these two positions can be anywhere in the
19887 row, so we must determine them now. */
19888 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19889 }
19890
19891 /* If the start of this line is the overlay arrow-position, then
19892 mark this glyph row as the one containing the overlay arrow.
19893 This is clearly a mess with variable size fonts. It would be
19894 better to let it be displayed like cursors under X. */
19895 if ((row->displays_text_p || !overlay_arrow_seen)
19896 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19897 !NILP (overlay_arrow_string)))
19898 {
19899 /* Overlay arrow in window redisplay is a fringe bitmap. */
19900 if (STRINGP (overlay_arrow_string))
19901 {
19902 struct glyph_row *arrow_row
19903 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19904 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19905 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19906 struct glyph *p = row->glyphs[TEXT_AREA];
19907 struct glyph *p2, *end;
19908
19909 /* Copy the arrow glyphs. */
19910 while (glyph < arrow_end)
19911 *p++ = *glyph++;
19912
19913 /* Throw away padding glyphs. */
19914 p2 = p;
19915 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19916 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19917 ++p2;
19918 if (p2 > p)
19919 {
19920 while (p2 < end)
19921 *p++ = *p2++;
19922 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19923 }
19924 }
19925 else
19926 {
19927 eassert (INTEGERP (overlay_arrow_string));
19928 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19929 }
19930 overlay_arrow_seen = 1;
19931 }
19932
19933 /* Highlight trailing whitespace. */
19934 if (!NILP (Vshow_trailing_whitespace))
19935 highlight_trailing_whitespace (it->f, it->glyph_row);
19936
19937 /* Compute pixel dimensions of this line. */
19938 compute_line_metrics (it);
19939
19940 /* Implementation note: No changes in the glyphs of ROW or in their
19941 faces can be done past this point, because compute_line_metrics
19942 computes ROW's hash value and stores it within the glyph_row
19943 structure. */
19944
19945 /* Record whether this row ends inside an ellipsis. */
19946 row->ends_in_ellipsis_p
19947 = (it->method == GET_FROM_DISPLAY_VECTOR
19948 && it->ellipsis_p);
19949
19950 /* Save fringe bitmaps in this row. */
19951 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19952 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19953 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19954 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19955
19956 it->left_user_fringe_bitmap = 0;
19957 it->left_user_fringe_face_id = 0;
19958 it->right_user_fringe_bitmap = 0;
19959 it->right_user_fringe_face_id = 0;
19960
19961 /* Maybe set the cursor. */
19962 cvpos = it->w->cursor.vpos;
19963 if ((cvpos < 0
19964 /* In bidi-reordered rows, keep checking for proper cursor
19965 position even if one has been found already, because buffer
19966 positions in such rows change non-linearly with ROW->VPOS,
19967 when a line is continued. One exception: when we are at ZV,
19968 display cursor on the first suitable glyph row, since all
19969 the empty rows after that also have their position set to ZV. */
19970 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19971 lines' rows is implemented for bidi-reordered rows. */
19972 || (it->bidi_p
19973 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19974 && PT >= MATRIX_ROW_START_CHARPOS (row)
19975 && PT <= MATRIX_ROW_END_CHARPOS (row)
19976 && cursor_row_p (row))
19977 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19978
19979 /* Prepare for the next line. This line starts horizontally at (X
19980 HPOS) = (0 0). Vertical positions are incremented. As a
19981 convenience for the caller, IT->glyph_row is set to the next
19982 row to be used. */
19983 it->current_x = it->hpos = 0;
19984 it->current_y += row->height;
19985 SET_TEXT_POS (it->eol_pos, 0, 0);
19986 ++it->vpos;
19987 ++it->glyph_row;
19988 /* The next row should by default use the same value of the
19989 reversed_p flag as this one. set_iterator_to_next decides when
19990 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19991 the flag accordingly. */
19992 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19993 it->glyph_row->reversed_p = row->reversed_p;
19994 it->start = row->end;
19995 return row->displays_text_p;
19996
19997 #undef RECORD_MAX_MIN_POS
19998 }
19999
20000 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20001 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20002 doc: /* Return paragraph direction at point in BUFFER.
20003 Value is either `left-to-right' or `right-to-left'.
20004 If BUFFER is omitted or nil, it defaults to the current buffer.
20005
20006 Paragraph direction determines how the text in the paragraph is displayed.
20007 In left-to-right paragraphs, text begins at the left margin of the window
20008 and the reading direction is generally left to right. In right-to-left
20009 paragraphs, text begins at the right margin and is read from right to left.
20010
20011 See also `bidi-paragraph-direction'. */)
20012 (Lisp_Object buffer)
20013 {
20014 struct buffer *buf = current_buffer;
20015 struct buffer *old = buf;
20016
20017 if (! NILP (buffer))
20018 {
20019 CHECK_BUFFER (buffer);
20020 buf = XBUFFER (buffer);
20021 }
20022
20023 if (NILP (BVAR (buf, bidi_display_reordering))
20024 || NILP (BVAR (buf, enable_multibyte_characters))
20025 /* When we are loading loadup.el, the character property tables
20026 needed for bidi iteration are not yet available. */
20027 || !NILP (Vpurify_flag))
20028 return Qleft_to_right;
20029 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20030 return BVAR (buf, bidi_paragraph_direction);
20031 else
20032 {
20033 /* Determine the direction from buffer text. We could try to
20034 use current_matrix if it is up to date, but this seems fast
20035 enough as it is. */
20036 struct bidi_it itb;
20037 ptrdiff_t pos = BUF_PT (buf);
20038 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20039 int c;
20040 void *itb_data = bidi_shelve_cache ();
20041
20042 set_buffer_temp (buf);
20043 /* bidi_paragraph_init finds the base direction of the paragraph
20044 by searching forward from paragraph start. We need the base
20045 direction of the current or _previous_ paragraph, so we need
20046 to make sure we are within that paragraph. To that end, find
20047 the previous non-empty line. */
20048 if (pos >= ZV && pos > BEGV)
20049 {
20050 pos--;
20051 bytepos = CHAR_TO_BYTE (pos);
20052 }
20053 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20054 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20055 {
20056 while ((c = FETCH_BYTE (bytepos)) == '\n'
20057 || c == ' ' || c == '\t' || c == '\f')
20058 {
20059 if (bytepos <= BEGV_BYTE)
20060 break;
20061 bytepos--;
20062 pos--;
20063 }
20064 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20065 bytepos--;
20066 }
20067 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20068 itb.paragraph_dir = NEUTRAL_DIR;
20069 itb.string.s = NULL;
20070 itb.string.lstring = Qnil;
20071 itb.string.bufpos = 0;
20072 itb.string.unibyte = 0;
20073 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20074 bidi_unshelve_cache (itb_data, 0);
20075 set_buffer_temp (old);
20076 switch (itb.paragraph_dir)
20077 {
20078 case L2R:
20079 return Qleft_to_right;
20080 break;
20081 case R2L:
20082 return Qright_to_left;
20083 break;
20084 default:
20085 emacs_abort ();
20086 }
20087 }
20088 }
20089
20090
20091 \f
20092 /***********************************************************************
20093 Menu Bar
20094 ***********************************************************************/
20095
20096 /* Redisplay the menu bar in the frame for window W.
20097
20098 The menu bar of X frames that don't have X toolkit support is
20099 displayed in a special window W->frame->menu_bar_window.
20100
20101 The menu bar of terminal frames is treated specially as far as
20102 glyph matrices are concerned. Menu bar lines are not part of
20103 windows, so the update is done directly on the frame matrix rows
20104 for the menu bar. */
20105
20106 static void
20107 display_menu_bar (struct window *w)
20108 {
20109 struct frame *f = XFRAME (WINDOW_FRAME (w));
20110 struct it it;
20111 Lisp_Object items;
20112 int i;
20113
20114 /* Don't do all this for graphical frames. */
20115 #ifdef HAVE_NTGUI
20116 if (FRAME_W32_P (f))
20117 return;
20118 #endif
20119 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20120 if (FRAME_X_P (f))
20121 return;
20122 #endif
20123
20124 #ifdef HAVE_NS
20125 if (FRAME_NS_P (f))
20126 return;
20127 #endif /* HAVE_NS */
20128
20129 #ifdef USE_X_TOOLKIT
20130 eassert (!FRAME_WINDOW_P (f));
20131 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20132 it.first_visible_x = 0;
20133 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20134 #else /* not USE_X_TOOLKIT */
20135 if (FRAME_WINDOW_P (f))
20136 {
20137 /* Menu bar lines are displayed in the desired matrix of the
20138 dummy window menu_bar_window. */
20139 struct window *menu_w;
20140 eassert (WINDOWP (f->menu_bar_window));
20141 menu_w = XWINDOW (f->menu_bar_window);
20142 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20143 MENU_FACE_ID);
20144 it.first_visible_x = 0;
20145 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20146 }
20147 else
20148 {
20149 /* This is a TTY frame, i.e. character hpos/vpos are used as
20150 pixel x/y. */
20151 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20152 MENU_FACE_ID);
20153 it.first_visible_x = 0;
20154 it.last_visible_x = FRAME_COLS (f);
20155 }
20156 #endif /* not USE_X_TOOLKIT */
20157
20158 /* FIXME: This should be controlled by a user option. See the
20159 comments in redisplay_tool_bar and display_mode_line about
20160 this. */
20161 it.paragraph_embedding = L2R;
20162
20163 if (! mode_line_inverse_video)
20164 /* Force the menu-bar to be displayed in the default face. */
20165 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20166
20167 /* Clear all rows of the menu bar. */
20168 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20169 {
20170 struct glyph_row *row = it.glyph_row + i;
20171 clear_glyph_row (row);
20172 row->enabled_p = 1;
20173 row->full_width_p = 1;
20174 }
20175
20176 /* Display all items of the menu bar. */
20177 items = FRAME_MENU_BAR_ITEMS (it.f);
20178 for (i = 0; i < ASIZE (items); i += 4)
20179 {
20180 Lisp_Object string;
20181
20182 /* Stop at nil string. */
20183 string = AREF (items, i + 1);
20184 if (NILP (string))
20185 break;
20186
20187 /* Remember where item was displayed. */
20188 ASET (items, i + 3, make_number (it.hpos));
20189
20190 /* Display the item, pad with one space. */
20191 if (it.current_x < it.last_visible_x)
20192 display_string (NULL, string, Qnil, 0, 0, &it,
20193 SCHARS (string) + 1, 0, 0, -1);
20194 }
20195
20196 /* Fill out the line with spaces. */
20197 if (it.current_x < it.last_visible_x)
20198 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20199
20200 /* Compute the total height of the lines. */
20201 compute_line_metrics (&it);
20202 }
20203
20204
20205 \f
20206 /***********************************************************************
20207 Mode Line
20208 ***********************************************************************/
20209
20210 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20211 FORCE is non-zero, redisplay mode lines unconditionally.
20212 Otherwise, redisplay only mode lines that are garbaged. Value is
20213 the number of windows whose mode lines were redisplayed. */
20214
20215 static int
20216 redisplay_mode_lines (Lisp_Object window, int force)
20217 {
20218 int nwindows = 0;
20219
20220 while (!NILP (window))
20221 {
20222 struct window *w = XWINDOW (window);
20223
20224 if (WINDOWP (w->hchild))
20225 nwindows += redisplay_mode_lines (w->hchild, force);
20226 else if (WINDOWP (w->vchild))
20227 nwindows += redisplay_mode_lines (w->vchild, force);
20228 else if (force
20229 || FRAME_GARBAGED_P (XFRAME (w->frame))
20230 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20231 {
20232 struct text_pos lpoint;
20233 struct buffer *old = current_buffer;
20234
20235 /* Set the window's buffer for the mode line display. */
20236 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20237 set_buffer_internal_1 (XBUFFER (w->buffer));
20238
20239 /* Point refers normally to the selected window. For any
20240 other window, set up appropriate value. */
20241 if (!EQ (window, selected_window))
20242 {
20243 struct text_pos pt;
20244
20245 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20246 if (CHARPOS (pt) < BEGV)
20247 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20248 else if (CHARPOS (pt) > (ZV - 1))
20249 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20250 else
20251 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20252 }
20253
20254 /* Display mode lines. */
20255 clear_glyph_matrix (w->desired_matrix);
20256 if (display_mode_lines (w))
20257 {
20258 ++nwindows;
20259 w->must_be_updated_p = 1;
20260 }
20261
20262 /* Restore old settings. */
20263 set_buffer_internal_1 (old);
20264 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20265 }
20266
20267 window = w->next;
20268 }
20269
20270 return nwindows;
20271 }
20272
20273
20274 /* Display the mode and/or header line of window W. Value is the
20275 sum number of mode lines and header lines displayed. */
20276
20277 static int
20278 display_mode_lines (struct window *w)
20279 {
20280 Lisp_Object old_selected_window, old_selected_frame;
20281 int n = 0;
20282
20283 old_selected_frame = selected_frame;
20284 selected_frame = w->frame;
20285 old_selected_window = selected_window;
20286 XSETWINDOW (selected_window, w);
20287
20288 /* These will be set while the mode line specs are processed. */
20289 line_number_displayed = 0;
20290 wset_column_number_displayed (w, Qnil);
20291
20292 if (WINDOW_WANTS_MODELINE_P (w))
20293 {
20294 struct window *sel_w = XWINDOW (old_selected_window);
20295
20296 /* Select mode line face based on the real selected window. */
20297 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20298 BVAR (current_buffer, mode_line_format));
20299 ++n;
20300 }
20301
20302 if (WINDOW_WANTS_HEADER_LINE_P (w))
20303 {
20304 display_mode_line (w, HEADER_LINE_FACE_ID,
20305 BVAR (current_buffer, header_line_format));
20306 ++n;
20307 }
20308
20309 selected_frame = old_selected_frame;
20310 selected_window = old_selected_window;
20311 return n;
20312 }
20313
20314
20315 /* Display mode or header line of window W. FACE_ID specifies which
20316 line to display; it is either MODE_LINE_FACE_ID or
20317 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20318 display. Value is the pixel height of the mode/header line
20319 displayed. */
20320
20321 static int
20322 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20323 {
20324 struct it it;
20325 struct face *face;
20326 ptrdiff_t count = SPECPDL_INDEX ();
20327
20328 init_iterator (&it, w, -1, -1, NULL, face_id);
20329 /* Don't extend on a previously drawn mode-line.
20330 This may happen if called from pos_visible_p. */
20331 it.glyph_row->enabled_p = 0;
20332 prepare_desired_row (it.glyph_row);
20333
20334 it.glyph_row->mode_line_p = 1;
20335
20336 if (! mode_line_inverse_video)
20337 /* Force the mode-line to be displayed in the default face. */
20338 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20339
20340 /* FIXME: This should be controlled by a user option. But
20341 supporting such an option is not trivial, since the mode line is
20342 made up of many separate strings. */
20343 it.paragraph_embedding = L2R;
20344
20345 record_unwind_protect (unwind_format_mode_line,
20346 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20347
20348 mode_line_target = MODE_LINE_DISPLAY;
20349
20350 /* Temporarily make frame's keyboard the current kboard so that
20351 kboard-local variables in the mode_line_format will get the right
20352 values. */
20353 push_kboard (FRAME_KBOARD (it.f));
20354 record_unwind_save_match_data ();
20355 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20356 pop_kboard ();
20357
20358 unbind_to (count, Qnil);
20359
20360 /* Fill up with spaces. */
20361 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20362
20363 compute_line_metrics (&it);
20364 it.glyph_row->full_width_p = 1;
20365 it.glyph_row->continued_p = 0;
20366 it.glyph_row->truncated_on_left_p = 0;
20367 it.glyph_row->truncated_on_right_p = 0;
20368
20369 /* Make a 3D mode-line have a shadow at its right end. */
20370 face = FACE_FROM_ID (it.f, face_id);
20371 extend_face_to_end_of_line (&it);
20372 if (face->box != FACE_NO_BOX)
20373 {
20374 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20375 + it.glyph_row->used[TEXT_AREA] - 1);
20376 last->right_box_line_p = 1;
20377 }
20378
20379 return it.glyph_row->height;
20380 }
20381
20382 /* Move element ELT in LIST to the front of LIST.
20383 Return the updated list. */
20384
20385 static Lisp_Object
20386 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20387 {
20388 register Lisp_Object tail, prev;
20389 register Lisp_Object tem;
20390
20391 tail = list;
20392 prev = Qnil;
20393 while (CONSP (tail))
20394 {
20395 tem = XCAR (tail);
20396
20397 if (EQ (elt, tem))
20398 {
20399 /* Splice out the link TAIL. */
20400 if (NILP (prev))
20401 list = XCDR (tail);
20402 else
20403 Fsetcdr (prev, XCDR (tail));
20404
20405 /* Now make it the first. */
20406 Fsetcdr (tail, list);
20407 return tail;
20408 }
20409 else
20410 prev = tail;
20411 tail = XCDR (tail);
20412 QUIT;
20413 }
20414
20415 /* Not found--return unchanged LIST. */
20416 return list;
20417 }
20418
20419 /* Contribute ELT to the mode line for window IT->w. How it
20420 translates into text depends on its data type.
20421
20422 IT describes the display environment in which we display, as usual.
20423
20424 DEPTH is the depth in recursion. It is used to prevent
20425 infinite recursion here.
20426
20427 FIELD_WIDTH is the number of characters the display of ELT should
20428 occupy in the mode line, and PRECISION is the maximum number of
20429 characters to display from ELT's representation. See
20430 display_string for details.
20431
20432 Returns the hpos of the end of the text generated by ELT.
20433
20434 PROPS is a property list to add to any string we encounter.
20435
20436 If RISKY is nonzero, remove (disregard) any properties in any string
20437 we encounter, and ignore :eval and :propertize.
20438
20439 The global variable `mode_line_target' determines whether the
20440 output is passed to `store_mode_line_noprop',
20441 `store_mode_line_string', or `display_string'. */
20442
20443 static int
20444 display_mode_element (struct it *it, int depth, int field_width, int precision,
20445 Lisp_Object elt, Lisp_Object props, int risky)
20446 {
20447 int n = 0, field, prec;
20448 int literal = 0;
20449
20450 tail_recurse:
20451 if (depth > 100)
20452 elt = build_string ("*too-deep*");
20453
20454 depth++;
20455
20456 switch (XTYPE (elt))
20457 {
20458 case Lisp_String:
20459 {
20460 /* A string: output it and check for %-constructs within it. */
20461 unsigned char c;
20462 ptrdiff_t offset = 0;
20463
20464 if (SCHARS (elt) > 0
20465 && (!NILP (props) || risky))
20466 {
20467 Lisp_Object oprops, aelt;
20468 oprops = Ftext_properties_at (make_number (0), elt);
20469
20470 /* If the starting string's properties are not what
20471 we want, translate the string. Also, if the string
20472 is risky, do that anyway. */
20473
20474 if (NILP (Fequal (props, oprops)) || risky)
20475 {
20476 /* If the starting string has properties,
20477 merge the specified ones onto the existing ones. */
20478 if (! NILP (oprops) && !risky)
20479 {
20480 Lisp_Object tem;
20481
20482 oprops = Fcopy_sequence (oprops);
20483 tem = props;
20484 while (CONSP (tem))
20485 {
20486 oprops = Fplist_put (oprops, XCAR (tem),
20487 XCAR (XCDR (tem)));
20488 tem = XCDR (XCDR (tem));
20489 }
20490 props = oprops;
20491 }
20492
20493 aelt = Fassoc (elt, mode_line_proptrans_alist);
20494 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20495 {
20496 /* AELT is what we want. Move it to the front
20497 without consing. */
20498 elt = XCAR (aelt);
20499 mode_line_proptrans_alist
20500 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20501 }
20502 else
20503 {
20504 Lisp_Object tem;
20505
20506 /* If AELT has the wrong props, it is useless.
20507 so get rid of it. */
20508 if (! NILP (aelt))
20509 mode_line_proptrans_alist
20510 = Fdelq (aelt, mode_line_proptrans_alist);
20511
20512 elt = Fcopy_sequence (elt);
20513 Fset_text_properties (make_number (0), Flength (elt),
20514 props, elt);
20515 /* Add this item to mode_line_proptrans_alist. */
20516 mode_line_proptrans_alist
20517 = Fcons (Fcons (elt, props),
20518 mode_line_proptrans_alist);
20519 /* Truncate mode_line_proptrans_alist
20520 to at most 50 elements. */
20521 tem = Fnthcdr (make_number (50),
20522 mode_line_proptrans_alist);
20523 if (! NILP (tem))
20524 XSETCDR (tem, Qnil);
20525 }
20526 }
20527 }
20528
20529 offset = 0;
20530
20531 if (literal)
20532 {
20533 prec = precision - n;
20534 switch (mode_line_target)
20535 {
20536 case MODE_LINE_NOPROP:
20537 case MODE_LINE_TITLE:
20538 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20539 break;
20540 case MODE_LINE_STRING:
20541 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20542 break;
20543 case MODE_LINE_DISPLAY:
20544 n += display_string (NULL, elt, Qnil, 0, 0, it,
20545 0, prec, 0, STRING_MULTIBYTE (elt));
20546 break;
20547 }
20548
20549 break;
20550 }
20551
20552 /* Handle the non-literal case. */
20553
20554 while ((precision <= 0 || n < precision)
20555 && SREF (elt, offset) != 0
20556 && (mode_line_target != MODE_LINE_DISPLAY
20557 || it->current_x < it->last_visible_x))
20558 {
20559 ptrdiff_t last_offset = offset;
20560
20561 /* Advance to end of string or next format specifier. */
20562 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20563 ;
20564
20565 if (offset - 1 != last_offset)
20566 {
20567 ptrdiff_t nchars, nbytes;
20568
20569 /* Output to end of string or up to '%'. Field width
20570 is length of string. Don't output more than
20571 PRECISION allows us. */
20572 offset--;
20573
20574 prec = c_string_width (SDATA (elt) + last_offset,
20575 offset - last_offset, precision - n,
20576 &nchars, &nbytes);
20577
20578 switch (mode_line_target)
20579 {
20580 case MODE_LINE_NOPROP:
20581 case MODE_LINE_TITLE:
20582 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20583 break;
20584 case MODE_LINE_STRING:
20585 {
20586 ptrdiff_t bytepos = last_offset;
20587 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20588 ptrdiff_t endpos = (precision <= 0
20589 ? string_byte_to_char (elt, offset)
20590 : charpos + nchars);
20591
20592 n += store_mode_line_string (NULL,
20593 Fsubstring (elt, make_number (charpos),
20594 make_number (endpos)),
20595 0, 0, 0, Qnil);
20596 }
20597 break;
20598 case MODE_LINE_DISPLAY:
20599 {
20600 ptrdiff_t bytepos = last_offset;
20601 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20602
20603 if (precision <= 0)
20604 nchars = string_byte_to_char (elt, offset) - charpos;
20605 n += display_string (NULL, elt, Qnil, 0, charpos,
20606 it, 0, nchars, 0,
20607 STRING_MULTIBYTE (elt));
20608 }
20609 break;
20610 }
20611 }
20612 else /* c == '%' */
20613 {
20614 ptrdiff_t percent_position = offset;
20615
20616 /* Get the specified minimum width. Zero means
20617 don't pad. */
20618 field = 0;
20619 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20620 field = field * 10 + c - '0';
20621
20622 /* Don't pad beyond the total padding allowed. */
20623 if (field_width - n > 0 && field > field_width - n)
20624 field = field_width - n;
20625
20626 /* Note that either PRECISION <= 0 or N < PRECISION. */
20627 prec = precision - n;
20628
20629 if (c == 'M')
20630 n += display_mode_element (it, depth, field, prec,
20631 Vglobal_mode_string, props,
20632 risky);
20633 else if (c != 0)
20634 {
20635 int multibyte;
20636 ptrdiff_t bytepos, charpos;
20637 const char *spec;
20638 Lisp_Object string;
20639
20640 bytepos = percent_position;
20641 charpos = (STRING_MULTIBYTE (elt)
20642 ? string_byte_to_char (elt, bytepos)
20643 : bytepos);
20644 spec = decode_mode_spec (it->w, c, field, &string);
20645 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20646
20647 switch (mode_line_target)
20648 {
20649 case MODE_LINE_NOPROP:
20650 case MODE_LINE_TITLE:
20651 n += store_mode_line_noprop (spec, field, prec);
20652 break;
20653 case MODE_LINE_STRING:
20654 {
20655 Lisp_Object tem = build_string (spec);
20656 props = Ftext_properties_at (make_number (charpos), elt);
20657 /* Should only keep face property in props */
20658 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20659 }
20660 break;
20661 case MODE_LINE_DISPLAY:
20662 {
20663 int nglyphs_before, nwritten;
20664
20665 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20666 nwritten = display_string (spec, string, elt,
20667 charpos, 0, it,
20668 field, prec, 0,
20669 multibyte);
20670
20671 /* Assign to the glyphs written above the
20672 string where the `%x' came from, position
20673 of the `%'. */
20674 if (nwritten > 0)
20675 {
20676 struct glyph *glyph
20677 = (it->glyph_row->glyphs[TEXT_AREA]
20678 + nglyphs_before);
20679 int i;
20680
20681 for (i = 0; i < nwritten; ++i)
20682 {
20683 glyph[i].object = elt;
20684 glyph[i].charpos = charpos;
20685 }
20686
20687 n += nwritten;
20688 }
20689 }
20690 break;
20691 }
20692 }
20693 else /* c == 0 */
20694 break;
20695 }
20696 }
20697 }
20698 break;
20699
20700 case Lisp_Symbol:
20701 /* A symbol: process the value of the symbol recursively
20702 as if it appeared here directly. Avoid error if symbol void.
20703 Special case: if value of symbol is a string, output the string
20704 literally. */
20705 {
20706 register Lisp_Object tem;
20707
20708 /* If the variable is not marked as risky to set
20709 then its contents are risky to use. */
20710 if (NILP (Fget (elt, Qrisky_local_variable)))
20711 risky = 1;
20712
20713 tem = Fboundp (elt);
20714 if (!NILP (tem))
20715 {
20716 tem = Fsymbol_value (elt);
20717 /* If value is a string, output that string literally:
20718 don't check for % within it. */
20719 if (STRINGP (tem))
20720 literal = 1;
20721
20722 if (!EQ (tem, elt))
20723 {
20724 /* Give up right away for nil or t. */
20725 elt = tem;
20726 goto tail_recurse;
20727 }
20728 }
20729 }
20730 break;
20731
20732 case Lisp_Cons:
20733 {
20734 register Lisp_Object car, tem;
20735
20736 /* A cons cell: five distinct cases.
20737 If first element is :eval or :propertize, do something special.
20738 If first element is a string or a cons, process all the elements
20739 and effectively concatenate them.
20740 If first element is a negative number, truncate displaying cdr to
20741 at most that many characters. If positive, pad (with spaces)
20742 to at least that many characters.
20743 If first element is a symbol, process the cadr or caddr recursively
20744 according to whether the symbol's value is non-nil or nil. */
20745 car = XCAR (elt);
20746 if (EQ (car, QCeval))
20747 {
20748 /* An element of the form (:eval FORM) means evaluate FORM
20749 and use the result as mode line elements. */
20750
20751 if (risky)
20752 break;
20753
20754 if (CONSP (XCDR (elt)))
20755 {
20756 Lisp_Object spec;
20757 spec = safe_eval (XCAR (XCDR (elt)));
20758 n += display_mode_element (it, depth, field_width - n,
20759 precision - n, spec, props,
20760 risky);
20761 }
20762 }
20763 else if (EQ (car, QCpropertize))
20764 {
20765 /* An element of the form (:propertize ELT PROPS...)
20766 means display ELT but applying properties PROPS. */
20767
20768 if (risky)
20769 break;
20770
20771 if (CONSP (XCDR (elt)))
20772 n += display_mode_element (it, depth, field_width - n,
20773 precision - n, XCAR (XCDR (elt)),
20774 XCDR (XCDR (elt)), risky);
20775 }
20776 else if (SYMBOLP (car))
20777 {
20778 tem = Fboundp (car);
20779 elt = XCDR (elt);
20780 if (!CONSP (elt))
20781 goto invalid;
20782 /* elt is now the cdr, and we know it is a cons cell.
20783 Use its car if CAR has a non-nil value. */
20784 if (!NILP (tem))
20785 {
20786 tem = Fsymbol_value (car);
20787 if (!NILP (tem))
20788 {
20789 elt = XCAR (elt);
20790 goto tail_recurse;
20791 }
20792 }
20793 /* Symbol's value is nil (or symbol is unbound)
20794 Get the cddr of the original list
20795 and if possible find the caddr and use that. */
20796 elt = XCDR (elt);
20797 if (NILP (elt))
20798 break;
20799 else if (!CONSP (elt))
20800 goto invalid;
20801 elt = XCAR (elt);
20802 goto tail_recurse;
20803 }
20804 else if (INTEGERP (car))
20805 {
20806 register int lim = XINT (car);
20807 elt = XCDR (elt);
20808 if (lim < 0)
20809 {
20810 /* Negative int means reduce maximum width. */
20811 if (precision <= 0)
20812 precision = -lim;
20813 else
20814 precision = min (precision, -lim);
20815 }
20816 else if (lim > 0)
20817 {
20818 /* Padding specified. Don't let it be more than
20819 current maximum. */
20820 if (precision > 0)
20821 lim = min (precision, lim);
20822
20823 /* If that's more padding than already wanted, queue it.
20824 But don't reduce padding already specified even if
20825 that is beyond the current truncation point. */
20826 field_width = max (lim, field_width);
20827 }
20828 goto tail_recurse;
20829 }
20830 else if (STRINGP (car) || CONSP (car))
20831 {
20832 Lisp_Object halftail = elt;
20833 int len = 0;
20834
20835 while (CONSP (elt)
20836 && (precision <= 0 || n < precision))
20837 {
20838 n += display_mode_element (it, depth,
20839 /* Do padding only after the last
20840 element in the list. */
20841 (! CONSP (XCDR (elt))
20842 ? field_width - n
20843 : 0),
20844 precision - n, XCAR (elt),
20845 props, risky);
20846 elt = XCDR (elt);
20847 len++;
20848 if ((len & 1) == 0)
20849 halftail = XCDR (halftail);
20850 /* Check for cycle. */
20851 if (EQ (halftail, elt))
20852 break;
20853 }
20854 }
20855 }
20856 break;
20857
20858 default:
20859 invalid:
20860 elt = build_string ("*invalid*");
20861 goto tail_recurse;
20862 }
20863
20864 /* Pad to FIELD_WIDTH. */
20865 if (field_width > 0 && n < field_width)
20866 {
20867 switch (mode_line_target)
20868 {
20869 case MODE_LINE_NOPROP:
20870 case MODE_LINE_TITLE:
20871 n += store_mode_line_noprop ("", field_width - n, 0);
20872 break;
20873 case MODE_LINE_STRING:
20874 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20875 break;
20876 case MODE_LINE_DISPLAY:
20877 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20878 0, 0, 0);
20879 break;
20880 }
20881 }
20882
20883 return n;
20884 }
20885
20886 /* Store a mode-line string element in mode_line_string_list.
20887
20888 If STRING is non-null, display that C string. Otherwise, the Lisp
20889 string LISP_STRING is displayed.
20890
20891 FIELD_WIDTH is the minimum number of output glyphs to produce.
20892 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20893 with spaces. FIELD_WIDTH <= 0 means don't pad.
20894
20895 PRECISION is the maximum number of characters to output from
20896 STRING. PRECISION <= 0 means don't truncate the string.
20897
20898 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20899 properties to the string.
20900
20901 PROPS are the properties to add to the string.
20902 The mode_line_string_face face property is always added to the string.
20903 */
20904
20905 static int
20906 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20907 int field_width, int precision, Lisp_Object props)
20908 {
20909 ptrdiff_t len;
20910 int n = 0;
20911
20912 if (string != NULL)
20913 {
20914 len = strlen (string);
20915 if (precision > 0 && len > precision)
20916 len = precision;
20917 lisp_string = make_string (string, len);
20918 if (NILP (props))
20919 props = mode_line_string_face_prop;
20920 else if (!NILP (mode_line_string_face))
20921 {
20922 Lisp_Object face = Fplist_get (props, Qface);
20923 props = Fcopy_sequence (props);
20924 if (NILP (face))
20925 face = mode_line_string_face;
20926 else
20927 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20928 props = Fplist_put (props, Qface, face);
20929 }
20930 Fadd_text_properties (make_number (0), make_number (len),
20931 props, lisp_string);
20932 }
20933 else
20934 {
20935 len = XFASTINT (Flength (lisp_string));
20936 if (precision > 0 && len > precision)
20937 {
20938 len = precision;
20939 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20940 precision = -1;
20941 }
20942 if (!NILP (mode_line_string_face))
20943 {
20944 Lisp_Object face;
20945 if (NILP (props))
20946 props = Ftext_properties_at (make_number (0), lisp_string);
20947 face = Fplist_get (props, Qface);
20948 if (NILP (face))
20949 face = mode_line_string_face;
20950 else
20951 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20952 props = Fcons (Qface, Fcons (face, Qnil));
20953 if (copy_string)
20954 lisp_string = Fcopy_sequence (lisp_string);
20955 }
20956 if (!NILP (props))
20957 Fadd_text_properties (make_number (0), make_number (len),
20958 props, lisp_string);
20959 }
20960
20961 if (len > 0)
20962 {
20963 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20964 n += len;
20965 }
20966
20967 if (field_width > len)
20968 {
20969 field_width -= len;
20970 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20971 if (!NILP (props))
20972 Fadd_text_properties (make_number (0), make_number (field_width),
20973 props, lisp_string);
20974 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20975 n += field_width;
20976 }
20977
20978 return n;
20979 }
20980
20981
20982 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20983 1, 4, 0,
20984 doc: /* Format a string out of a mode line format specification.
20985 First arg FORMAT specifies the mode line format (see `mode-line-format'
20986 for details) to use.
20987
20988 By default, the format is evaluated for the currently selected window.
20989
20990 Optional second arg FACE specifies the face property to put on all
20991 characters for which no face is specified. The value nil means the
20992 default face. The value t means whatever face the window's mode line
20993 currently uses (either `mode-line' or `mode-line-inactive',
20994 depending on whether the window is the selected window or not).
20995 An integer value means the value string has no text
20996 properties.
20997
20998 Optional third and fourth args WINDOW and BUFFER specify the window
20999 and buffer to use as the context for the formatting (defaults
21000 are the selected window and the WINDOW's buffer). */)
21001 (Lisp_Object format, Lisp_Object face,
21002 Lisp_Object window, Lisp_Object buffer)
21003 {
21004 struct it it;
21005 int len;
21006 struct window *w;
21007 struct buffer *old_buffer = NULL;
21008 int face_id;
21009 int no_props = INTEGERP (face);
21010 ptrdiff_t count = SPECPDL_INDEX ();
21011 Lisp_Object str;
21012 int string_start = 0;
21013
21014 if (NILP (window))
21015 window = selected_window;
21016 CHECK_WINDOW (window);
21017 w = XWINDOW (window);
21018
21019 if (NILP (buffer))
21020 buffer = w->buffer;
21021 CHECK_BUFFER (buffer);
21022
21023 /* Make formatting the modeline a non-op when noninteractive, otherwise
21024 there will be problems later caused by a partially initialized frame. */
21025 if (NILP (format) || noninteractive)
21026 return empty_unibyte_string;
21027
21028 if (no_props)
21029 face = Qnil;
21030
21031 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21032 : EQ (face, Qt) ? (EQ (window, selected_window)
21033 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21034 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21035 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21036 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21037 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21038 : DEFAULT_FACE_ID;
21039
21040 old_buffer = current_buffer;
21041
21042 /* Save things including mode_line_proptrans_alist,
21043 and set that to nil so that we don't alter the outer value. */
21044 record_unwind_protect (unwind_format_mode_line,
21045 format_mode_line_unwind_data
21046 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
21047 old_buffer, selected_window, 1));
21048 mode_line_proptrans_alist = Qnil;
21049
21050 Fselect_window (window, Qt);
21051 set_buffer_internal_1 (XBUFFER (buffer));
21052
21053 init_iterator (&it, w, -1, -1, NULL, face_id);
21054
21055 if (no_props)
21056 {
21057 mode_line_target = MODE_LINE_NOPROP;
21058 mode_line_string_face_prop = Qnil;
21059 mode_line_string_list = Qnil;
21060 string_start = MODE_LINE_NOPROP_LEN (0);
21061 }
21062 else
21063 {
21064 mode_line_target = MODE_LINE_STRING;
21065 mode_line_string_list = Qnil;
21066 mode_line_string_face = face;
21067 mode_line_string_face_prop
21068 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21069 }
21070
21071 push_kboard (FRAME_KBOARD (it.f));
21072 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21073 pop_kboard ();
21074
21075 if (no_props)
21076 {
21077 len = MODE_LINE_NOPROP_LEN (string_start);
21078 str = make_string (mode_line_noprop_buf + string_start, len);
21079 }
21080 else
21081 {
21082 mode_line_string_list = Fnreverse (mode_line_string_list);
21083 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21084 empty_unibyte_string);
21085 }
21086
21087 unbind_to (count, Qnil);
21088 return str;
21089 }
21090
21091 /* Write a null-terminated, right justified decimal representation of
21092 the positive integer D to BUF using a minimal field width WIDTH. */
21093
21094 static void
21095 pint2str (register char *buf, register int width, register ptrdiff_t d)
21096 {
21097 register char *p = buf;
21098
21099 if (d <= 0)
21100 *p++ = '0';
21101 else
21102 {
21103 while (d > 0)
21104 {
21105 *p++ = d % 10 + '0';
21106 d /= 10;
21107 }
21108 }
21109
21110 for (width -= (int) (p - buf); width > 0; --width)
21111 *p++ = ' ';
21112 *p-- = '\0';
21113 while (p > buf)
21114 {
21115 d = *buf;
21116 *buf++ = *p;
21117 *p-- = d;
21118 }
21119 }
21120
21121 /* Write a null-terminated, right justified decimal and "human
21122 readable" representation of the nonnegative integer D to BUF using
21123 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21124
21125 static const char power_letter[] =
21126 {
21127 0, /* no letter */
21128 'k', /* kilo */
21129 'M', /* mega */
21130 'G', /* giga */
21131 'T', /* tera */
21132 'P', /* peta */
21133 'E', /* exa */
21134 'Z', /* zetta */
21135 'Y' /* yotta */
21136 };
21137
21138 static void
21139 pint2hrstr (char *buf, int width, ptrdiff_t d)
21140 {
21141 /* We aim to represent the nonnegative integer D as
21142 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21143 ptrdiff_t quotient = d;
21144 int remainder = 0;
21145 /* -1 means: do not use TENTHS. */
21146 int tenths = -1;
21147 int exponent = 0;
21148
21149 /* Length of QUOTIENT.TENTHS as a string. */
21150 int length;
21151
21152 char * psuffix;
21153 char * p;
21154
21155 if (1000 <= quotient)
21156 {
21157 /* Scale to the appropriate EXPONENT. */
21158 do
21159 {
21160 remainder = quotient % 1000;
21161 quotient /= 1000;
21162 exponent++;
21163 }
21164 while (1000 <= quotient);
21165
21166 /* Round to nearest and decide whether to use TENTHS or not. */
21167 if (quotient <= 9)
21168 {
21169 tenths = remainder / 100;
21170 if (50 <= remainder % 100)
21171 {
21172 if (tenths < 9)
21173 tenths++;
21174 else
21175 {
21176 quotient++;
21177 if (quotient == 10)
21178 tenths = -1;
21179 else
21180 tenths = 0;
21181 }
21182 }
21183 }
21184 else
21185 if (500 <= remainder)
21186 {
21187 if (quotient < 999)
21188 quotient++;
21189 else
21190 {
21191 quotient = 1;
21192 exponent++;
21193 tenths = 0;
21194 }
21195 }
21196 }
21197
21198 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21199 if (tenths == -1 && quotient <= 99)
21200 if (quotient <= 9)
21201 length = 1;
21202 else
21203 length = 2;
21204 else
21205 length = 3;
21206 p = psuffix = buf + max (width, length);
21207
21208 /* Print EXPONENT. */
21209 *psuffix++ = power_letter[exponent];
21210 *psuffix = '\0';
21211
21212 /* Print TENTHS. */
21213 if (tenths >= 0)
21214 {
21215 *--p = '0' + tenths;
21216 *--p = '.';
21217 }
21218
21219 /* Print QUOTIENT. */
21220 do
21221 {
21222 int digit = quotient % 10;
21223 *--p = '0' + digit;
21224 }
21225 while ((quotient /= 10) != 0);
21226
21227 /* Print leading spaces. */
21228 while (buf < p)
21229 *--p = ' ';
21230 }
21231
21232 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21233 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21234 type of CODING_SYSTEM. Return updated pointer into BUF. */
21235
21236 static unsigned char invalid_eol_type[] = "(*invalid*)";
21237
21238 static char *
21239 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21240 {
21241 Lisp_Object val;
21242 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21243 const unsigned char *eol_str;
21244 int eol_str_len;
21245 /* The EOL conversion we are using. */
21246 Lisp_Object eoltype;
21247
21248 val = CODING_SYSTEM_SPEC (coding_system);
21249 eoltype = Qnil;
21250
21251 if (!VECTORP (val)) /* Not yet decided. */
21252 {
21253 *buf++ = multibyte ? '-' : ' ';
21254 if (eol_flag)
21255 eoltype = eol_mnemonic_undecided;
21256 /* Don't mention EOL conversion if it isn't decided. */
21257 }
21258 else
21259 {
21260 Lisp_Object attrs;
21261 Lisp_Object eolvalue;
21262
21263 attrs = AREF (val, 0);
21264 eolvalue = AREF (val, 2);
21265
21266 *buf++ = multibyte
21267 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21268 : ' ';
21269
21270 if (eol_flag)
21271 {
21272 /* The EOL conversion that is normal on this system. */
21273
21274 if (NILP (eolvalue)) /* Not yet decided. */
21275 eoltype = eol_mnemonic_undecided;
21276 else if (VECTORP (eolvalue)) /* Not yet decided. */
21277 eoltype = eol_mnemonic_undecided;
21278 else /* eolvalue is Qunix, Qdos, or Qmac. */
21279 eoltype = (EQ (eolvalue, Qunix)
21280 ? eol_mnemonic_unix
21281 : (EQ (eolvalue, Qdos) == 1
21282 ? eol_mnemonic_dos : eol_mnemonic_mac));
21283 }
21284 }
21285
21286 if (eol_flag)
21287 {
21288 /* Mention the EOL conversion if it is not the usual one. */
21289 if (STRINGP (eoltype))
21290 {
21291 eol_str = SDATA (eoltype);
21292 eol_str_len = SBYTES (eoltype);
21293 }
21294 else if (CHARACTERP (eoltype))
21295 {
21296 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21297 int c = XFASTINT (eoltype);
21298 eol_str_len = CHAR_STRING (c, tmp);
21299 eol_str = tmp;
21300 }
21301 else
21302 {
21303 eol_str = invalid_eol_type;
21304 eol_str_len = sizeof (invalid_eol_type) - 1;
21305 }
21306 memcpy (buf, eol_str, eol_str_len);
21307 buf += eol_str_len;
21308 }
21309
21310 return buf;
21311 }
21312
21313 /* Return a string for the output of a mode line %-spec for window W,
21314 generated by character C. FIELD_WIDTH > 0 means pad the string
21315 returned with spaces to that value. Return a Lisp string in
21316 *STRING if the resulting string is taken from that Lisp string.
21317
21318 Note we operate on the current buffer for most purposes,
21319 the exception being w->base_line_pos. */
21320
21321 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21322
21323 static const char *
21324 decode_mode_spec (struct window *w, register int c, int field_width,
21325 Lisp_Object *string)
21326 {
21327 Lisp_Object obj;
21328 struct frame *f = XFRAME (WINDOW_FRAME (w));
21329 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21330 struct buffer *b = current_buffer;
21331
21332 obj = Qnil;
21333 *string = Qnil;
21334
21335 switch (c)
21336 {
21337 case '*':
21338 if (!NILP (BVAR (b, read_only)))
21339 return "%";
21340 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21341 return "*";
21342 return "-";
21343
21344 case '+':
21345 /* This differs from %* only for a modified read-only buffer. */
21346 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21347 return "*";
21348 if (!NILP (BVAR (b, read_only)))
21349 return "%";
21350 return "-";
21351
21352 case '&':
21353 /* This differs from %* in ignoring read-only-ness. */
21354 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21355 return "*";
21356 return "-";
21357
21358 case '%':
21359 return "%";
21360
21361 case '[':
21362 {
21363 int i;
21364 char *p;
21365
21366 if (command_loop_level > 5)
21367 return "[[[... ";
21368 p = decode_mode_spec_buf;
21369 for (i = 0; i < command_loop_level; i++)
21370 *p++ = '[';
21371 *p = 0;
21372 return decode_mode_spec_buf;
21373 }
21374
21375 case ']':
21376 {
21377 int i;
21378 char *p;
21379
21380 if (command_loop_level > 5)
21381 return " ...]]]";
21382 p = decode_mode_spec_buf;
21383 for (i = 0; i < command_loop_level; i++)
21384 *p++ = ']';
21385 *p = 0;
21386 return decode_mode_spec_buf;
21387 }
21388
21389 case '-':
21390 {
21391 register int i;
21392
21393 /* Let lots_of_dashes be a string of infinite length. */
21394 if (mode_line_target == MODE_LINE_NOPROP ||
21395 mode_line_target == MODE_LINE_STRING)
21396 return "--";
21397 if (field_width <= 0
21398 || field_width > sizeof (lots_of_dashes))
21399 {
21400 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21401 decode_mode_spec_buf[i] = '-';
21402 decode_mode_spec_buf[i] = '\0';
21403 return decode_mode_spec_buf;
21404 }
21405 else
21406 return lots_of_dashes;
21407 }
21408
21409 case 'b':
21410 obj = BVAR (b, name);
21411 break;
21412
21413 case 'c':
21414 /* %c and %l are ignored in `frame-title-format'.
21415 (In redisplay_internal, the frame title is drawn _before_ the
21416 windows are updated, so the stuff which depends on actual
21417 window contents (such as %l) may fail to render properly, or
21418 even crash emacs.) */
21419 if (mode_line_target == MODE_LINE_TITLE)
21420 return "";
21421 else
21422 {
21423 ptrdiff_t col = current_column ();
21424 wset_column_number_displayed (w, make_number (col));
21425 pint2str (decode_mode_spec_buf, field_width, col);
21426 return decode_mode_spec_buf;
21427 }
21428
21429 case 'e':
21430 #ifndef SYSTEM_MALLOC
21431 {
21432 if (NILP (Vmemory_full))
21433 return "";
21434 else
21435 return "!MEM FULL! ";
21436 }
21437 #else
21438 return "";
21439 #endif
21440
21441 case 'F':
21442 /* %F displays the frame name. */
21443 if (!NILP (f->title))
21444 return SSDATA (f->title);
21445 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21446 return SSDATA (f->name);
21447 return "Emacs";
21448
21449 case 'f':
21450 obj = BVAR (b, filename);
21451 break;
21452
21453 case 'i':
21454 {
21455 ptrdiff_t size = ZV - BEGV;
21456 pint2str (decode_mode_spec_buf, field_width, size);
21457 return decode_mode_spec_buf;
21458 }
21459
21460 case 'I':
21461 {
21462 ptrdiff_t size = ZV - BEGV;
21463 pint2hrstr (decode_mode_spec_buf, field_width, size);
21464 return decode_mode_spec_buf;
21465 }
21466
21467 case 'l':
21468 {
21469 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21470 ptrdiff_t topline, nlines, height;
21471 ptrdiff_t junk;
21472
21473 /* %c and %l are ignored in `frame-title-format'. */
21474 if (mode_line_target == MODE_LINE_TITLE)
21475 return "";
21476
21477 startpos = XMARKER (w->start)->charpos;
21478 startpos_byte = marker_byte_position (w->start);
21479 height = WINDOW_TOTAL_LINES (w);
21480
21481 /* If we decided that this buffer isn't suitable for line numbers,
21482 don't forget that too fast. */
21483 if (EQ (w->base_line_pos, w->buffer))
21484 goto no_value;
21485 /* But do forget it, if the window shows a different buffer now. */
21486 else if (BUFFERP (w->base_line_pos))
21487 wset_base_line_pos (w, Qnil);
21488
21489 /* If the buffer is very big, don't waste time. */
21490 if (INTEGERP (Vline_number_display_limit)
21491 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21492 {
21493 wset_base_line_pos (w, Qnil);
21494 wset_base_line_number (w, Qnil);
21495 goto no_value;
21496 }
21497
21498 if (INTEGERP (w->base_line_number)
21499 && INTEGERP (w->base_line_pos)
21500 && XFASTINT (w->base_line_pos) <= startpos)
21501 {
21502 line = XFASTINT (w->base_line_number);
21503 linepos = XFASTINT (w->base_line_pos);
21504 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21505 }
21506 else
21507 {
21508 line = 1;
21509 linepos = BUF_BEGV (b);
21510 linepos_byte = BUF_BEGV_BYTE (b);
21511 }
21512
21513 /* Count lines from base line to window start position. */
21514 nlines = display_count_lines (linepos_byte,
21515 startpos_byte,
21516 startpos, &junk);
21517
21518 topline = nlines + line;
21519
21520 /* Determine a new base line, if the old one is too close
21521 or too far away, or if we did not have one.
21522 "Too close" means it's plausible a scroll-down would
21523 go back past it. */
21524 if (startpos == BUF_BEGV (b))
21525 {
21526 wset_base_line_number (w, make_number (topline));
21527 wset_base_line_pos (w, make_number (BUF_BEGV (b)));
21528 }
21529 else if (nlines < height + 25 || nlines > height * 3 + 50
21530 || linepos == BUF_BEGV (b))
21531 {
21532 ptrdiff_t limit = BUF_BEGV (b);
21533 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21534 ptrdiff_t position;
21535 ptrdiff_t distance =
21536 (height * 2 + 30) * line_number_display_limit_width;
21537
21538 if (startpos - distance > limit)
21539 {
21540 limit = startpos - distance;
21541 limit_byte = CHAR_TO_BYTE (limit);
21542 }
21543
21544 nlines = display_count_lines (startpos_byte,
21545 limit_byte,
21546 - (height * 2 + 30),
21547 &position);
21548 /* If we couldn't find the lines we wanted within
21549 line_number_display_limit_width chars per line,
21550 give up on line numbers for this window. */
21551 if (position == limit_byte && limit == startpos - distance)
21552 {
21553 wset_base_line_pos (w, w->buffer);
21554 wset_base_line_number (w, Qnil);
21555 goto no_value;
21556 }
21557
21558 wset_base_line_number (w, make_number (topline - nlines));
21559 wset_base_line_pos (w, make_number (BYTE_TO_CHAR (position)));
21560 }
21561
21562 /* Now count lines from the start pos to point. */
21563 nlines = display_count_lines (startpos_byte,
21564 PT_BYTE, PT, &junk);
21565
21566 /* Record that we did display the line number. */
21567 line_number_displayed = 1;
21568
21569 /* Make the string to show. */
21570 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21571 return decode_mode_spec_buf;
21572 no_value:
21573 {
21574 char* p = decode_mode_spec_buf;
21575 int pad = field_width - 2;
21576 while (pad-- > 0)
21577 *p++ = ' ';
21578 *p++ = '?';
21579 *p++ = '?';
21580 *p = '\0';
21581 return decode_mode_spec_buf;
21582 }
21583 }
21584 break;
21585
21586 case 'm':
21587 obj = BVAR (b, mode_name);
21588 break;
21589
21590 case 'n':
21591 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21592 return " Narrow";
21593 break;
21594
21595 case 'p':
21596 {
21597 ptrdiff_t pos = marker_position (w->start);
21598 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21599
21600 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21601 {
21602 if (pos <= BUF_BEGV (b))
21603 return "All";
21604 else
21605 return "Bottom";
21606 }
21607 else if (pos <= BUF_BEGV (b))
21608 return "Top";
21609 else
21610 {
21611 if (total > 1000000)
21612 /* Do it differently for a large value, to avoid overflow. */
21613 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21614 else
21615 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21616 /* We can't normally display a 3-digit number,
21617 so get us a 2-digit number that is close. */
21618 if (total == 100)
21619 total = 99;
21620 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21621 return decode_mode_spec_buf;
21622 }
21623 }
21624
21625 /* Display percentage of size above the bottom of the screen. */
21626 case 'P':
21627 {
21628 ptrdiff_t toppos = marker_position (w->start);
21629 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21630 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21631
21632 if (botpos >= BUF_ZV (b))
21633 {
21634 if (toppos <= BUF_BEGV (b))
21635 return "All";
21636 else
21637 return "Bottom";
21638 }
21639 else
21640 {
21641 if (total > 1000000)
21642 /* Do it differently for a large value, to avoid overflow. */
21643 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21644 else
21645 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21646 /* We can't normally display a 3-digit number,
21647 so get us a 2-digit number that is close. */
21648 if (total == 100)
21649 total = 99;
21650 if (toppos <= BUF_BEGV (b))
21651 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21652 else
21653 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21654 return decode_mode_spec_buf;
21655 }
21656 }
21657
21658 case 's':
21659 /* status of process */
21660 obj = Fget_buffer_process (Fcurrent_buffer ());
21661 if (NILP (obj))
21662 return "no process";
21663 #ifndef MSDOS
21664 obj = Fsymbol_name (Fprocess_status (obj));
21665 #endif
21666 break;
21667
21668 case '@':
21669 {
21670 ptrdiff_t count = inhibit_garbage_collection ();
21671 Lisp_Object val = call1 (intern ("file-remote-p"),
21672 BVAR (current_buffer, directory));
21673 unbind_to (count, Qnil);
21674
21675 if (NILP (val))
21676 return "-";
21677 else
21678 return "@";
21679 }
21680
21681 case 't': /* indicate TEXT or BINARY */
21682 return "T";
21683
21684 case 'z':
21685 /* coding-system (not including end-of-line format) */
21686 case 'Z':
21687 /* coding-system (including end-of-line type) */
21688 {
21689 int eol_flag = (c == 'Z');
21690 char *p = decode_mode_spec_buf;
21691
21692 if (! FRAME_WINDOW_P (f))
21693 {
21694 /* No need to mention EOL here--the terminal never needs
21695 to do EOL conversion. */
21696 p = decode_mode_spec_coding (CODING_ID_NAME
21697 (FRAME_KEYBOARD_CODING (f)->id),
21698 p, 0);
21699 p = decode_mode_spec_coding (CODING_ID_NAME
21700 (FRAME_TERMINAL_CODING (f)->id),
21701 p, 0);
21702 }
21703 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21704 p, eol_flag);
21705
21706 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21707 #ifdef subprocesses
21708 obj = Fget_buffer_process (Fcurrent_buffer ());
21709 if (PROCESSP (obj))
21710 {
21711 p = decode_mode_spec_coding
21712 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21713 p = decode_mode_spec_coding
21714 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21715 }
21716 #endif /* subprocesses */
21717 #endif /* 0 */
21718 *p = 0;
21719 return decode_mode_spec_buf;
21720 }
21721 }
21722
21723 if (STRINGP (obj))
21724 {
21725 *string = obj;
21726 return SSDATA (obj);
21727 }
21728 else
21729 return "";
21730 }
21731
21732
21733 /* Count up to COUNT lines starting from START_BYTE.
21734 But don't go beyond LIMIT_BYTE.
21735 Return the number of lines thus found (always nonnegative).
21736
21737 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21738
21739 static ptrdiff_t
21740 display_count_lines (ptrdiff_t start_byte,
21741 ptrdiff_t limit_byte, ptrdiff_t count,
21742 ptrdiff_t *byte_pos_ptr)
21743 {
21744 register unsigned char *cursor;
21745 unsigned char *base;
21746
21747 register ptrdiff_t ceiling;
21748 register unsigned char *ceiling_addr;
21749 ptrdiff_t orig_count = count;
21750
21751 /* If we are not in selective display mode,
21752 check only for newlines. */
21753 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21754 && !INTEGERP (BVAR (current_buffer, selective_display)));
21755
21756 if (count > 0)
21757 {
21758 while (start_byte < limit_byte)
21759 {
21760 ceiling = BUFFER_CEILING_OF (start_byte);
21761 ceiling = min (limit_byte - 1, ceiling);
21762 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21763 base = (cursor = BYTE_POS_ADDR (start_byte));
21764 while (1)
21765 {
21766 if (selective_display)
21767 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21768 ;
21769 else
21770 while (*cursor != '\n' && ++cursor != ceiling_addr)
21771 ;
21772
21773 if (cursor != ceiling_addr)
21774 {
21775 if (--count == 0)
21776 {
21777 start_byte += cursor - base + 1;
21778 *byte_pos_ptr = start_byte;
21779 return orig_count;
21780 }
21781 else
21782 if (++cursor == ceiling_addr)
21783 break;
21784 }
21785 else
21786 break;
21787 }
21788 start_byte += cursor - base;
21789 }
21790 }
21791 else
21792 {
21793 while (start_byte > limit_byte)
21794 {
21795 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21796 ceiling = max (limit_byte, ceiling);
21797 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21798 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21799 while (1)
21800 {
21801 if (selective_display)
21802 while (--cursor != ceiling_addr
21803 && *cursor != '\n' && *cursor != 015)
21804 ;
21805 else
21806 while (--cursor != ceiling_addr && *cursor != '\n')
21807 ;
21808
21809 if (cursor != ceiling_addr)
21810 {
21811 if (++count == 0)
21812 {
21813 start_byte += cursor - base + 1;
21814 *byte_pos_ptr = start_byte;
21815 /* When scanning backwards, we should
21816 not count the newline posterior to which we stop. */
21817 return - orig_count - 1;
21818 }
21819 }
21820 else
21821 break;
21822 }
21823 /* Here we add 1 to compensate for the last decrement
21824 of CURSOR, which took it past the valid range. */
21825 start_byte += cursor - base + 1;
21826 }
21827 }
21828
21829 *byte_pos_ptr = limit_byte;
21830
21831 if (count < 0)
21832 return - orig_count + count;
21833 return orig_count - count;
21834
21835 }
21836
21837
21838 \f
21839 /***********************************************************************
21840 Displaying strings
21841 ***********************************************************************/
21842
21843 /* Display a NUL-terminated string, starting with index START.
21844
21845 If STRING is non-null, display that C string. Otherwise, the Lisp
21846 string LISP_STRING is displayed. There's a case that STRING is
21847 non-null and LISP_STRING is not nil. It means STRING is a string
21848 data of LISP_STRING. In that case, we display LISP_STRING while
21849 ignoring its text properties.
21850
21851 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21852 FACE_STRING. Display STRING or LISP_STRING with the face at
21853 FACE_STRING_POS in FACE_STRING:
21854
21855 Display the string in the environment given by IT, but use the
21856 standard display table, temporarily.
21857
21858 FIELD_WIDTH is the minimum number of output glyphs to produce.
21859 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21860 with spaces. If STRING has more characters, more than FIELD_WIDTH
21861 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21862
21863 PRECISION is the maximum number of characters to output from
21864 STRING. PRECISION < 0 means don't truncate the string.
21865
21866 This is roughly equivalent to printf format specifiers:
21867
21868 FIELD_WIDTH PRECISION PRINTF
21869 ----------------------------------------
21870 -1 -1 %s
21871 -1 10 %.10s
21872 10 -1 %10s
21873 20 10 %20.10s
21874
21875 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21876 display them, and < 0 means obey the current buffer's value of
21877 enable_multibyte_characters.
21878
21879 Value is the number of columns displayed. */
21880
21881 static int
21882 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21883 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21884 int field_width, int precision, int max_x, int multibyte)
21885 {
21886 int hpos_at_start = it->hpos;
21887 int saved_face_id = it->face_id;
21888 struct glyph_row *row = it->glyph_row;
21889 ptrdiff_t it_charpos;
21890
21891 /* Initialize the iterator IT for iteration over STRING beginning
21892 with index START. */
21893 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21894 precision, field_width, multibyte);
21895 if (string && STRINGP (lisp_string))
21896 /* LISP_STRING is the one returned by decode_mode_spec. We should
21897 ignore its text properties. */
21898 it->stop_charpos = it->end_charpos;
21899
21900 /* If displaying STRING, set up the face of the iterator from
21901 FACE_STRING, if that's given. */
21902 if (STRINGP (face_string))
21903 {
21904 ptrdiff_t endptr;
21905 struct face *face;
21906
21907 it->face_id
21908 = face_at_string_position (it->w, face_string, face_string_pos,
21909 0, it->region_beg_charpos,
21910 it->region_end_charpos,
21911 &endptr, it->base_face_id, 0);
21912 face = FACE_FROM_ID (it->f, it->face_id);
21913 it->face_box_p = face->box != FACE_NO_BOX;
21914 }
21915
21916 /* Set max_x to the maximum allowed X position. Don't let it go
21917 beyond the right edge of the window. */
21918 if (max_x <= 0)
21919 max_x = it->last_visible_x;
21920 else
21921 max_x = min (max_x, it->last_visible_x);
21922
21923 /* Skip over display elements that are not visible. because IT->w is
21924 hscrolled. */
21925 if (it->current_x < it->first_visible_x)
21926 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21927 MOVE_TO_POS | MOVE_TO_X);
21928
21929 row->ascent = it->max_ascent;
21930 row->height = it->max_ascent + it->max_descent;
21931 row->phys_ascent = it->max_phys_ascent;
21932 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21933 row->extra_line_spacing = it->max_extra_line_spacing;
21934
21935 if (STRINGP (it->string))
21936 it_charpos = IT_STRING_CHARPOS (*it);
21937 else
21938 it_charpos = IT_CHARPOS (*it);
21939
21940 /* This condition is for the case that we are called with current_x
21941 past last_visible_x. */
21942 while (it->current_x < max_x)
21943 {
21944 int x_before, x, n_glyphs_before, i, nglyphs;
21945
21946 /* Get the next display element. */
21947 if (!get_next_display_element (it))
21948 break;
21949
21950 /* Produce glyphs. */
21951 x_before = it->current_x;
21952 n_glyphs_before = row->used[TEXT_AREA];
21953 PRODUCE_GLYPHS (it);
21954
21955 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21956 i = 0;
21957 x = x_before;
21958 while (i < nglyphs)
21959 {
21960 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21961
21962 if (it->line_wrap != TRUNCATE
21963 && x + glyph->pixel_width > max_x)
21964 {
21965 /* End of continued line or max_x reached. */
21966 if (CHAR_GLYPH_PADDING_P (*glyph))
21967 {
21968 /* A wide character is unbreakable. */
21969 if (row->reversed_p)
21970 unproduce_glyphs (it, row->used[TEXT_AREA]
21971 - n_glyphs_before);
21972 row->used[TEXT_AREA] = n_glyphs_before;
21973 it->current_x = x_before;
21974 }
21975 else
21976 {
21977 if (row->reversed_p)
21978 unproduce_glyphs (it, row->used[TEXT_AREA]
21979 - (n_glyphs_before + i));
21980 row->used[TEXT_AREA] = n_glyphs_before + i;
21981 it->current_x = x;
21982 }
21983 break;
21984 }
21985 else if (x + glyph->pixel_width >= it->first_visible_x)
21986 {
21987 /* Glyph is at least partially visible. */
21988 ++it->hpos;
21989 if (x < it->first_visible_x)
21990 row->x = x - it->first_visible_x;
21991 }
21992 else
21993 {
21994 /* Glyph is off the left margin of the display area.
21995 Should not happen. */
21996 emacs_abort ();
21997 }
21998
21999 row->ascent = max (row->ascent, it->max_ascent);
22000 row->height = max (row->height, it->max_ascent + it->max_descent);
22001 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22002 row->phys_height = max (row->phys_height,
22003 it->max_phys_ascent + it->max_phys_descent);
22004 row->extra_line_spacing = max (row->extra_line_spacing,
22005 it->max_extra_line_spacing);
22006 x += glyph->pixel_width;
22007 ++i;
22008 }
22009
22010 /* Stop if max_x reached. */
22011 if (i < nglyphs)
22012 break;
22013
22014 /* Stop at line ends. */
22015 if (ITERATOR_AT_END_OF_LINE_P (it))
22016 {
22017 it->continuation_lines_width = 0;
22018 break;
22019 }
22020
22021 set_iterator_to_next (it, 1);
22022 if (STRINGP (it->string))
22023 it_charpos = IT_STRING_CHARPOS (*it);
22024 else
22025 it_charpos = IT_CHARPOS (*it);
22026
22027 /* Stop if truncating at the right edge. */
22028 if (it->line_wrap == TRUNCATE
22029 && it->current_x >= it->last_visible_x)
22030 {
22031 /* Add truncation mark, but don't do it if the line is
22032 truncated at a padding space. */
22033 if (it_charpos < it->string_nchars)
22034 {
22035 if (!FRAME_WINDOW_P (it->f))
22036 {
22037 int ii, n;
22038
22039 if (it->current_x > it->last_visible_x)
22040 {
22041 if (!row->reversed_p)
22042 {
22043 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22044 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22045 break;
22046 }
22047 else
22048 {
22049 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22050 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22051 break;
22052 unproduce_glyphs (it, ii + 1);
22053 ii = row->used[TEXT_AREA] - (ii + 1);
22054 }
22055 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22056 {
22057 row->used[TEXT_AREA] = ii;
22058 produce_special_glyphs (it, IT_TRUNCATION);
22059 }
22060 }
22061 produce_special_glyphs (it, IT_TRUNCATION);
22062 }
22063 row->truncated_on_right_p = 1;
22064 }
22065 break;
22066 }
22067 }
22068
22069 /* Maybe insert a truncation at the left. */
22070 if (it->first_visible_x
22071 && it_charpos > 0)
22072 {
22073 if (!FRAME_WINDOW_P (it->f)
22074 || (row->reversed_p
22075 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22076 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22077 insert_left_trunc_glyphs (it);
22078 row->truncated_on_left_p = 1;
22079 }
22080
22081 it->face_id = saved_face_id;
22082
22083 /* Value is number of columns displayed. */
22084 return it->hpos - hpos_at_start;
22085 }
22086
22087
22088 \f
22089 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22090 appears as an element of LIST or as the car of an element of LIST.
22091 If PROPVAL is a list, compare each element against LIST in that
22092 way, and return 1/2 if any element of PROPVAL is found in LIST.
22093 Otherwise return 0. This function cannot quit.
22094 The return value is 2 if the text is invisible but with an ellipsis
22095 and 1 if it's invisible and without an ellipsis. */
22096
22097 int
22098 invisible_p (register Lisp_Object propval, Lisp_Object list)
22099 {
22100 register Lisp_Object tail, proptail;
22101
22102 for (tail = list; CONSP (tail); tail = XCDR (tail))
22103 {
22104 register Lisp_Object tem;
22105 tem = XCAR (tail);
22106 if (EQ (propval, tem))
22107 return 1;
22108 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22109 return NILP (XCDR (tem)) ? 1 : 2;
22110 }
22111
22112 if (CONSP (propval))
22113 {
22114 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22115 {
22116 Lisp_Object propelt;
22117 propelt = XCAR (proptail);
22118 for (tail = list; CONSP (tail); tail = XCDR (tail))
22119 {
22120 register Lisp_Object tem;
22121 tem = XCAR (tail);
22122 if (EQ (propelt, tem))
22123 return 1;
22124 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22125 return NILP (XCDR (tem)) ? 1 : 2;
22126 }
22127 }
22128 }
22129
22130 return 0;
22131 }
22132
22133 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22134 doc: /* Non-nil if the property makes the text invisible.
22135 POS-OR-PROP can be a marker or number, in which case it is taken to be
22136 a position in the current buffer and the value of the `invisible' property
22137 is checked; or it can be some other value, which is then presumed to be the
22138 value of the `invisible' property of the text of interest.
22139 The non-nil value returned can be t for truly invisible text or something
22140 else if the text is replaced by an ellipsis. */)
22141 (Lisp_Object pos_or_prop)
22142 {
22143 Lisp_Object prop
22144 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22145 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22146 : pos_or_prop);
22147 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22148 return (invis == 0 ? Qnil
22149 : invis == 1 ? Qt
22150 : make_number (invis));
22151 }
22152
22153 /* Calculate a width or height in pixels from a specification using
22154 the following elements:
22155
22156 SPEC ::=
22157 NUM - a (fractional) multiple of the default font width/height
22158 (NUM) - specifies exactly NUM pixels
22159 UNIT - a fixed number of pixels, see below.
22160 ELEMENT - size of a display element in pixels, see below.
22161 (NUM . SPEC) - equals NUM * SPEC
22162 (+ SPEC SPEC ...) - add pixel values
22163 (- SPEC SPEC ...) - subtract pixel values
22164 (- SPEC) - negate pixel value
22165
22166 NUM ::=
22167 INT or FLOAT - a number constant
22168 SYMBOL - use symbol's (buffer local) variable binding.
22169
22170 UNIT ::=
22171 in - pixels per inch *)
22172 mm - pixels per 1/1000 meter *)
22173 cm - pixels per 1/100 meter *)
22174 width - width of current font in pixels.
22175 height - height of current font in pixels.
22176
22177 *) using the ratio(s) defined in display-pixels-per-inch.
22178
22179 ELEMENT ::=
22180
22181 left-fringe - left fringe width in pixels
22182 right-fringe - right fringe width in pixels
22183
22184 left-margin - left margin width in pixels
22185 right-margin - right margin width in pixels
22186
22187 scroll-bar - scroll-bar area width in pixels
22188
22189 Examples:
22190
22191 Pixels corresponding to 5 inches:
22192 (5 . in)
22193
22194 Total width of non-text areas on left side of window (if scroll-bar is on left):
22195 '(space :width (+ left-fringe left-margin scroll-bar))
22196
22197 Align to first text column (in header line):
22198 '(space :align-to 0)
22199
22200 Align to middle of text area minus half the width of variable `my-image'
22201 containing a loaded image:
22202 '(space :align-to (0.5 . (- text my-image)))
22203
22204 Width of left margin minus width of 1 character in the default font:
22205 '(space :width (- left-margin 1))
22206
22207 Width of left margin minus width of 2 characters in the current font:
22208 '(space :width (- left-margin (2 . width)))
22209
22210 Center 1 character over left-margin (in header line):
22211 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22212
22213 Different ways to express width of left fringe plus left margin minus one pixel:
22214 '(space :width (- (+ left-fringe left-margin) (1)))
22215 '(space :width (+ left-fringe left-margin (- (1))))
22216 '(space :width (+ left-fringe left-margin (-1)))
22217
22218 */
22219
22220 #define NUMVAL(X) \
22221 ((INTEGERP (X) || FLOATP (X)) \
22222 ? XFLOATINT (X) \
22223 : - 1)
22224
22225 static int
22226 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22227 struct font *font, int width_p, int *align_to)
22228 {
22229 double pixels;
22230
22231 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22232 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22233
22234 if (NILP (prop))
22235 return OK_PIXELS (0);
22236
22237 eassert (FRAME_LIVE_P (it->f));
22238
22239 if (SYMBOLP (prop))
22240 {
22241 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22242 {
22243 char *unit = SSDATA (SYMBOL_NAME (prop));
22244
22245 if (unit[0] == 'i' && unit[1] == 'n')
22246 pixels = 1.0;
22247 else if (unit[0] == 'm' && unit[1] == 'm')
22248 pixels = 25.4;
22249 else if (unit[0] == 'c' && unit[1] == 'm')
22250 pixels = 2.54;
22251 else
22252 pixels = 0;
22253 if (pixels > 0)
22254 {
22255 double ppi;
22256 #ifdef HAVE_WINDOW_SYSTEM
22257 if (FRAME_WINDOW_P (it->f)
22258 && (ppi = (width_p
22259 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22260 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22261 ppi > 0))
22262 return OK_PIXELS (ppi / pixels);
22263 #endif
22264
22265 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22266 || (CONSP (Vdisplay_pixels_per_inch)
22267 && (ppi = (width_p
22268 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22269 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22270 ppi > 0)))
22271 return OK_PIXELS (ppi / pixels);
22272
22273 return 0;
22274 }
22275 }
22276
22277 #ifdef HAVE_WINDOW_SYSTEM
22278 if (EQ (prop, Qheight))
22279 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22280 if (EQ (prop, Qwidth))
22281 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22282 #else
22283 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22284 return OK_PIXELS (1);
22285 #endif
22286
22287 if (EQ (prop, Qtext))
22288 return OK_PIXELS (width_p
22289 ? window_box_width (it->w, TEXT_AREA)
22290 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22291
22292 if (align_to && *align_to < 0)
22293 {
22294 *res = 0;
22295 if (EQ (prop, Qleft))
22296 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22297 if (EQ (prop, Qright))
22298 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22299 if (EQ (prop, Qcenter))
22300 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22301 + window_box_width (it->w, TEXT_AREA) / 2);
22302 if (EQ (prop, Qleft_fringe))
22303 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22304 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22305 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22306 if (EQ (prop, Qright_fringe))
22307 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22308 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22309 : window_box_right_offset (it->w, TEXT_AREA));
22310 if (EQ (prop, Qleft_margin))
22311 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22312 if (EQ (prop, Qright_margin))
22313 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22314 if (EQ (prop, Qscroll_bar))
22315 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22316 ? 0
22317 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22318 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22319 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22320 : 0)));
22321 }
22322 else
22323 {
22324 if (EQ (prop, Qleft_fringe))
22325 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22326 if (EQ (prop, Qright_fringe))
22327 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22328 if (EQ (prop, Qleft_margin))
22329 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22330 if (EQ (prop, Qright_margin))
22331 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22332 if (EQ (prop, Qscroll_bar))
22333 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22334 }
22335
22336 prop = buffer_local_value_1 (prop, it->w->buffer);
22337 if (EQ (prop, Qunbound))
22338 prop = Qnil;
22339 }
22340
22341 if (INTEGERP (prop) || FLOATP (prop))
22342 {
22343 int base_unit = (width_p
22344 ? FRAME_COLUMN_WIDTH (it->f)
22345 : FRAME_LINE_HEIGHT (it->f));
22346 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22347 }
22348
22349 if (CONSP (prop))
22350 {
22351 Lisp_Object car = XCAR (prop);
22352 Lisp_Object cdr = XCDR (prop);
22353
22354 if (SYMBOLP (car))
22355 {
22356 #ifdef HAVE_WINDOW_SYSTEM
22357 if (FRAME_WINDOW_P (it->f)
22358 && valid_image_p (prop))
22359 {
22360 ptrdiff_t id = lookup_image (it->f, prop);
22361 struct image *img = IMAGE_FROM_ID (it->f, id);
22362
22363 return OK_PIXELS (width_p ? img->width : img->height);
22364 }
22365 #endif
22366 if (EQ (car, Qplus) || EQ (car, Qminus))
22367 {
22368 int first = 1;
22369 double px;
22370
22371 pixels = 0;
22372 while (CONSP (cdr))
22373 {
22374 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22375 font, width_p, align_to))
22376 return 0;
22377 if (first)
22378 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22379 else
22380 pixels += px;
22381 cdr = XCDR (cdr);
22382 }
22383 if (EQ (car, Qminus))
22384 pixels = -pixels;
22385 return OK_PIXELS (pixels);
22386 }
22387
22388 car = buffer_local_value_1 (car, it->w->buffer);
22389 if (EQ (car, Qunbound))
22390 car = Qnil;
22391 }
22392
22393 if (INTEGERP (car) || FLOATP (car))
22394 {
22395 double fact;
22396 pixels = XFLOATINT (car);
22397 if (NILP (cdr))
22398 return OK_PIXELS (pixels);
22399 if (calc_pixel_width_or_height (&fact, it, cdr,
22400 font, width_p, align_to))
22401 return OK_PIXELS (pixels * fact);
22402 return 0;
22403 }
22404
22405 return 0;
22406 }
22407
22408 return 0;
22409 }
22410
22411 \f
22412 /***********************************************************************
22413 Glyph Display
22414 ***********************************************************************/
22415
22416 #ifdef HAVE_WINDOW_SYSTEM
22417
22418 #ifdef GLYPH_DEBUG
22419
22420 void
22421 dump_glyph_string (struct glyph_string *s)
22422 {
22423 fprintf (stderr, "glyph string\n");
22424 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22425 s->x, s->y, s->width, s->height);
22426 fprintf (stderr, " ybase = %d\n", s->ybase);
22427 fprintf (stderr, " hl = %d\n", s->hl);
22428 fprintf (stderr, " left overhang = %d, right = %d\n",
22429 s->left_overhang, s->right_overhang);
22430 fprintf (stderr, " nchars = %d\n", s->nchars);
22431 fprintf (stderr, " extends to end of line = %d\n",
22432 s->extends_to_end_of_line_p);
22433 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22434 fprintf (stderr, " bg width = %d\n", s->background_width);
22435 }
22436
22437 #endif /* GLYPH_DEBUG */
22438
22439 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22440 of XChar2b structures for S; it can't be allocated in
22441 init_glyph_string because it must be allocated via `alloca'. W
22442 is the window on which S is drawn. ROW and AREA are the glyph row
22443 and area within the row from which S is constructed. START is the
22444 index of the first glyph structure covered by S. HL is a
22445 face-override for drawing S. */
22446
22447 #ifdef HAVE_NTGUI
22448 #define OPTIONAL_HDC(hdc) HDC hdc,
22449 #define DECLARE_HDC(hdc) HDC hdc;
22450 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22451 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22452 #endif
22453
22454 #ifndef OPTIONAL_HDC
22455 #define OPTIONAL_HDC(hdc)
22456 #define DECLARE_HDC(hdc)
22457 #define ALLOCATE_HDC(hdc, f)
22458 #define RELEASE_HDC(hdc, f)
22459 #endif
22460
22461 static void
22462 init_glyph_string (struct glyph_string *s,
22463 OPTIONAL_HDC (hdc)
22464 XChar2b *char2b, struct window *w, struct glyph_row *row,
22465 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22466 {
22467 memset (s, 0, sizeof *s);
22468 s->w = w;
22469 s->f = XFRAME (w->frame);
22470 #ifdef HAVE_NTGUI
22471 s->hdc = hdc;
22472 #endif
22473 s->display = FRAME_X_DISPLAY (s->f);
22474 s->window = FRAME_X_WINDOW (s->f);
22475 s->char2b = char2b;
22476 s->hl = hl;
22477 s->row = row;
22478 s->area = area;
22479 s->first_glyph = row->glyphs[area] + start;
22480 s->height = row->height;
22481 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22482 s->ybase = s->y + row->ascent;
22483 }
22484
22485
22486 /* Append the list of glyph strings with head H and tail T to the list
22487 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22488
22489 static inline void
22490 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22491 struct glyph_string *h, struct glyph_string *t)
22492 {
22493 if (h)
22494 {
22495 if (*head)
22496 (*tail)->next = h;
22497 else
22498 *head = h;
22499 h->prev = *tail;
22500 *tail = t;
22501 }
22502 }
22503
22504
22505 /* Prepend the list of glyph strings with head H and tail T to the
22506 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22507 result. */
22508
22509 static inline void
22510 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22511 struct glyph_string *h, struct glyph_string *t)
22512 {
22513 if (h)
22514 {
22515 if (*head)
22516 (*head)->prev = t;
22517 else
22518 *tail = t;
22519 t->next = *head;
22520 *head = h;
22521 }
22522 }
22523
22524
22525 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22526 Set *HEAD and *TAIL to the resulting list. */
22527
22528 static inline void
22529 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22530 struct glyph_string *s)
22531 {
22532 s->next = s->prev = NULL;
22533 append_glyph_string_lists (head, tail, s, s);
22534 }
22535
22536
22537 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22538 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22539 make sure that X resources for the face returned are allocated.
22540 Value is a pointer to a realized face that is ready for display if
22541 DISPLAY_P is non-zero. */
22542
22543 static inline struct face *
22544 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22545 XChar2b *char2b, int display_p)
22546 {
22547 struct face *face = FACE_FROM_ID (f, face_id);
22548
22549 if (face->font)
22550 {
22551 unsigned code = face->font->driver->encode_char (face->font, c);
22552
22553 if (code != FONT_INVALID_CODE)
22554 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22555 else
22556 STORE_XCHAR2B (char2b, 0, 0);
22557 }
22558
22559 /* Make sure X resources of the face are allocated. */
22560 #ifdef HAVE_X_WINDOWS
22561 if (display_p)
22562 #endif
22563 {
22564 eassert (face != NULL);
22565 PREPARE_FACE_FOR_DISPLAY (f, face);
22566 }
22567
22568 return face;
22569 }
22570
22571
22572 /* Get face and two-byte form of character glyph GLYPH on frame F.
22573 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22574 a pointer to a realized face that is ready for display. */
22575
22576 static inline struct face *
22577 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22578 XChar2b *char2b, int *two_byte_p)
22579 {
22580 struct face *face;
22581
22582 eassert (glyph->type == CHAR_GLYPH);
22583 face = FACE_FROM_ID (f, glyph->face_id);
22584
22585 if (two_byte_p)
22586 *two_byte_p = 0;
22587
22588 if (face->font)
22589 {
22590 unsigned code;
22591
22592 if (CHAR_BYTE8_P (glyph->u.ch))
22593 code = CHAR_TO_BYTE8 (glyph->u.ch);
22594 else
22595 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22596
22597 if (code != FONT_INVALID_CODE)
22598 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22599 else
22600 STORE_XCHAR2B (char2b, 0, 0);
22601 }
22602
22603 /* Make sure X resources of the face are allocated. */
22604 eassert (face != NULL);
22605 PREPARE_FACE_FOR_DISPLAY (f, face);
22606 return face;
22607 }
22608
22609
22610 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22611 Return 1 if FONT has a glyph for C, otherwise return 0. */
22612
22613 static inline int
22614 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22615 {
22616 unsigned code;
22617
22618 if (CHAR_BYTE8_P (c))
22619 code = CHAR_TO_BYTE8 (c);
22620 else
22621 code = font->driver->encode_char (font, c);
22622
22623 if (code == FONT_INVALID_CODE)
22624 return 0;
22625 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22626 return 1;
22627 }
22628
22629
22630 /* Fill glyph string S with composition components specified by S->cmp.
22631
22632 BASE_FACE is the base face of the composition.
22633 S->cmp_from is the index of the first component for S.
22634
22635 OVERLAPS non-zero means S should draw the foreground only, and use
22636 its physical height for clipping. See also draw_glyphs.
22637
22638 Value is the index of a component not in S. */
22639
22640 static int
22641 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22642 int overlaps)
22643 {
22644 int i;
22645 /* For all glyphs of this composition, starting at the offset
22646 S->cmp_from, until we reach the end of the definition or encounter a
22647 glyph that requires the different face, add it to S. */
22648 struct face *face;
22649
22650 eassert (s);
22651
22652 s->for_overlaps = overlaps;
22653 s->face = NULL;
22654 s->font = NULL;
22655 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22656 {
22657 int c = COMPOSITION_GLYPH (s->cmp, i);
22658
22659 /* TAB in a composition means display glyphs with padding space
22660 on the left or right. */
22661 if (c != '\t')
22662 {
22663 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22664 -1, Qnil);
22665
22666 face = get_char_face_and_encoding (s->f, c, face_id,
22667 s->char2b + i, 1);
22668 if (face)
22669 {
22670 if (! s->face)
22671 {
22672 s->face = face;
22673 s->font = s->face->font;
22674 }
22675 else if (s->face != face)
22676 break;
22677 }
22678 }
22679 ++s->nchars;
22680 }
22681 s->cmp_to = i;
22682
22683 if (s->face == NULL)
22684 {
22685 s->face = base_face->ascii_face;
22686 s->font = s->face->font;
22687 }
22688
22689 /* All glyph strings for the same composition has the same width,
22690 i.e. the width set for the first component of the composition. */
22691 s->width = s->first_glyph->pixel_width;
22692
22693 /* If the specified font could not be loaded, use the frame's
22694 default font, but record the fact that we couldn't load it in
22695 the glyph string so that we can draw rectangles for the
22696 characters of the glyph string. */
22697 if (s->font == NULL)
22698 {
22699 s->font_not_found_p = 1;
22700 s->font = FRAME_FONT (s->f);
22701 }
22702
22703 /* Adjust base line for subscript/superscript text. */
22704 s->ybase += s->first_glyph->voffset;
22705
22706 /* This glyph string must always be drawn with 16-bit functions. */
22707 s->two_byte_p = 1;
22708
22709 return s->cmp_to;
22710 }
22711
22712 static int
22713 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22714 int start, int end, int overlaps)
22715 {
22716 struct glyph *glyph, *last;
22717 Lisp_Object lgstring;
22718 int i;
22719
22720 s->for_overlaps = overlaps;
22721 glyph = s->row->glyphs[s->area] + start;
22722 last = s->row->glyphs[s->area] + end;
22723 s->cmp_id = glyph->u.cmp.id;
22724 s->cmp_from = glyph->slice.cmp.from;
22725 s->cmp_to = glyph->slice.cmp.to + 1;
22726 s->face = FACE_FROM_ID (s->f, face_id);
22727 lgstring = composition_gstring_from_id (s->cmp_id);
22728 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22729 glyph++;
22730 while (glyph < last
22731 && glyph->u.cmp.automatic
22732 && glyph->u.cmp.id == s->cmp_id
22733 && s->cmp_to == glyph->slice.cmp.from)
22734 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22735
22736 for (i = s->cmp_from; i < s->cmp_to; i++)
22737 {
22738 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22739 unsigned code = LGLYPH_CODE (lglyph);
22740
22741 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22742 }
22743 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22744 return glyph - s->row->glyphs[s->area];
22745 }
22746
22747
22748 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22749 See the comment of fill_glyph_string for arguments.
22750 Value is the index of the first glyph not in S. */
22751
22752
22753 static int
22754 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22755 int start, int end, int overlaps)
22756 {
22757 struct glyph *glyph, *last;
22758 int voffset;
22759
22760 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22761 s->for_overlaps = overlaps;
22762 glyph = s->row->glyphs[s->area] + start;
22763 last = s->row->glyphs[s->area] + end;
22764 voffset = glyph->voffset;
22765 s->face = FACE_FROM_ID (s->f, face_id);
22766 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22767 s->nchars = 1;
22768 s->width = glyph->pixel_width;
22769 glyph++;
22770 while (glyph < last
22771 && glyph->type == GLYPHLESS_GLYPH
22772 && glyph->voffset == voffset
22773 && glyph->face_id == face_id)
22774 {
22775 s->nchars++;
22776 s->width += glyph->pixel_width;
22777 glyph++;
22778 }
22779 s->ybase += voffset;
22780 return glyph - s->row->glyphs[s->area];
22781 }
22782
22783
22784 /* Fill glyph string S from a sequence of character glyphs.
22785
22786 FACE_ID is the face id of the string. START is the index of the
22787 first glyph to consider, END is the index of the last + 1.
22788 OVERLAPS non-zero means S should draw the foreground only, and use
22789 its physical height for clipping. See also draw_glyphs.
22790
22791 Value is the index of the first glyph not in S. */
22792
22793 static int
22794 fill_glyph_string (struct glyph_string *s, int face_id,
22795 int start, int end, int overlaps)
22796 {
22797 struct glyph *glyph, *last;
22798 int voffset;
22799 int glyph_not_available_p;
22800
22801 eassert (s->f == XFRAME (s->w->frame));
22802 eassert (s->nchars == 0);
22803 eassert (start >= 0 && end > start);
22804
22805 s->for_overlaps = overlaps;
22806 glyph = s->row->glyphs[s->area] + start;
22807 last = s->row->glyphs[s->area] + end;
22808 voffset = glyph->voffset;
22809 s->padding_p = glyph->padding_p;
22810 glyph_not_available_p = glyph->glyph_not_available_p;
22811
22812 while (glyph < last
22813 && glyph->type == CHAR_GLYPH
22814 && glyph->voffset == voffset
22815 /* Same face id implies same font, nowadays. */
22816 && glyph->face_id == face_id
22817 && glyph->glyph_not_available_p == glyph_not_available_p)
22818 {
22819 int two_byte_p;
22820
22821 s->face = get_glyph_face_and_encoding (s->f, glyph,
22822 s->char2b + s->nchars,
22823 &two_byte_p);
22824 s->two_byte_p = two_byte_p;
22825 ++s->nchars;
22826 eassert (s->nchars <= end - start);
22827 s->width += glyph->pixel_width;
22828 if (glyph++->padding_p != s->padding_p)
22829 break;
22830 }
22831
22832 s->font = s->face->font;
22833
22834 /* If the specified font could not be loaded, use the frame's font,
22835 but record the fact that we couldn't load it in
22836 S->font_not_found_p so that we can draw rectangles for the
22837 characters of the glyph string. */
22838 if (s->font == NULL || glyph_not_available_p)
22839 {
22840 s->font_not_found_p = 1;
22841 s->font = FRAME_FONT (s->f);
22842 }
22843
22844 /* Adjust base line for subscript/superscript text. */
22845 s->ybase += voffset;
22846
22847 eassert (s->face && s->face->gc);
22848 return glyph - s->row->glyphs[s->area];
22849 }
22850
22851
22852 /* Fill glyph string S from image glyph S->first_glyph. */
22853
22854 static void
22855 fill_image_glyph_string (struct glyph_string *s)
22856 {
22857 eassert (s->first_glyph->type == IMAGE_GLYPH);
22858 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22859 eassert (s->img);
22860 s->slice = s->first_glyph->slice.img;
22861 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22862 s->font = s->face->font;
22863 s->width = s->first_glyph->pixel_width;
22864
22865 /* Adjust base line for subscript/superscript text. */
22866 s->ybase += s->first_glyph->voffset;
22867 }
22868
22869
22870 /* Fill glyph string S from a sequence of stretch glyphs.
22871
22872 START is the index of the first glyph to consider,
22873 END is the index of the last + 1.
22874
22875 Value is the index of the first glyph not in S. */
22876
22877 static int
22878 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22879 {
22880 struct glyph *glyph, *last;
22881 int voffset, face_id;
22882
22883 eassert (s->first_glyph->type == STRETCH_GLYPH);
22884
22885 glyph = s->row->glyphs[s->area] + start;
22886 last = s->row->glyphs[s->area] + end;
22887 face_id = glyph->face_id;
22888 s->face = FACE_FROM_ID (s->f, face_id);
22889 s->font = s->face->font;
22890 s->width = glyph->pixel_width;
22891 s->nchars = 1;
22892 voffset = glyph->voffset;
22893
22894 for (++glyph;
22895 (glyph < last
22896 && glyph->type == STRETCH_GLYPH
22897 && glyph->voffset == voffset
22898 && glyph->face_id == face_id);
22899 ++glyph)
22900 s->width += glyph->pixel_width;
22901
22902 /* Adjust base line for subscript/superscript text. */
22903 s->ybase += voffset;
22904
22905 /* The case that face->gc == 0 is handled when drawing the glyph
22906 string by calling PREPARE_FACE_FOR_DISPLAY. */
22907 eassert (s->face);
22908 return glyph - s->row->glyphs[s->area];
22909 }
22910
22911 static struct font_metrics *
22912 get_per_char_metric (struct font *font, XChar2b *char2b)
22913 {
22914 static struct font_metrics metrics;
22915 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22916
22917 if (! font || code == FONT_INVALID_CODE)
22918 return NULL;
22919 font->driver->text_extents (font, &code, 1, &metrics);
22920 return &metrics;
22921 }
22922
22923 /* EXPORT for RIF:
22924 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22925 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22926 assumed to be zero. */
22927
22928 void
22929 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22930 {
22931 *left = *right = 0;
22932
22933 if (glyph->type == CHAR_GLYPH)
22934 {
22935 struct face *face;
22936 XChar2b char2b;
22937 struct font_metrics *pcm;
22938
22939 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22940 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22941 {
22942 if (pcm->rbearing > pcm->width)
22943 *right = pcm->rbearing - pcm->width;
22944 if (pcm->lbearing < 0)
22945 *left = -pcm->lbearing;
22946 }
22947 }
22948 else if (glyph->type == COMPOSITE_GLYPH)
22949 {
22950 if (! glyph->u.cmp.automatic)
22951 {
22952 struct composition *cmp = composition_table[glyph->u.cmp.id];
22953
22954 if (cmp->rbearing > cmp->pixel_width)
22955 *right = cmp->rbearing - cmp->pixel_width;
22956 if (cmp->lbearing < 0)
22957 *left = - cmp->lbearing;
22958 }
22959 else
22960 {
22961 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22962 struct font_metrics metrics;
22963
22964 composition_gstring_width (gstring, glyph->slice.cmp.from,
22965 glyph->slice.cmp.to + 1, &metrics);
22966 if (metrics.rbearing > metrics.width)
22967 *right = metrics.rbearing - metrics.width;
22968 if (metrics.lbearing < 0)
22969 *left = - metrics.lbearing;
22970 }
22971 }
22972 }
22973
22974
22975 /* Return the index of the first glyph preceding glyph string S that
22976 is overwritten by S because of S's left overhang. Value is -1
22977 if no glyphs are overwritten. */
22978
22979 static int
22980 left_overwritten (struct glyph_string *s)
22981 {
22982 int k;
22983
22984 if (s->left_overhang)
22985 {
22986 int x = 0, i;
22987 struct glyph *glyphs = s->row->glyphs[s->area];
22988 int first = s->first_glyph - glyphs;
22989
22990 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22991 x -= glyphs[i].pixel_width;
22992
22993 k = i + 1;
22994 }
22995 else
22996 k = -1;
22997
22998 return k;
22999 }
23000
23001
23002 /* Return the index of the first glyph preceding glyph string S that
23003 is overwriting S because of its right overhang. Value is -1 if no
23004 glyph in front of S overwrites S. */
23005
23006 static int
23007 left_overwriting (struct glyph_string *s)
23008 {
23009 int i, k, x;
23010 struct glyph *glyphs = s->row->glyphs[s->area];
23011 int first = s->first_glyph - glyphs;
23012
23013 k = -1;
23014 x = 0;
23015 for (i = first - 1; i >= 0; --i)
23016 {
23017 int left, right;
23018 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23019 if (x + right > 0)
23020 k = i;
23021 x -= glyphs[i].pixel_width;
23022 }
23023
23024 return k;
23025 }
23026
23027
23028 /* Return the index of the last glyph following glyph string S that is
23029 overwritten by S because of S's right overhang. Value is -1 if
23030 no such glyph is found. */
23031
23032 static int
23033 right_overwritten (struct glyph_string *s)
23034 {
23035 int k = -1;
23036
23037 if (s->right_overhang)
23038 {
23039 int x = 0, i;
23040 struct glyph *glyphs = s->row->glyphs[s->area];
23041 int first = (s->first_glyph - glyphs
23042 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23043 int end = s->row->used[s->area];
23044
23045 for (i = first; i < end && s->right_overhang > x; ++i)
23046 x += glyphs[i].pixel_width;
23047
23048 k = i;
23049 }
23050
23051 return k;
23052 }
23053
23054
23055 /* Return the index of the last glyph following glyph string S that
23056 overwrites S because of its left overhang. Value is negative
23057 if no such glyph is found. */
23058
23059 static int
23060 right_overwriting (struct glyph_string *s)
23061 {
23062 int i, k, x;
23063 int end = s->row->used[s->area];
23064 struct glyph *glyphs = s->row->glyphs[s->area];
23065 int first = (s->first_glyph - glyphs
23066 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23067
23068 k = -1;
23069 x = 0;
23070 for (i = first; i < end; ++i)
23071 {
23072 int left, right;
23073 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23074 if (x - left < 0)
23075 k = i;
23076 x += glyphs[i].pixel_width;
23077 }
23078
23079 return k;
23080 }
23081
23082
23083 /* Set background width of glyph string S. START is the index of the
23084 first glyph following S. LAST_X is the right-most x-position + 1
23085 in the drawing area. */
23086
23087 static inline void
23088 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23089 {
23090 /* If the face of this glyph string has to be drawn to the end of
23091 the drawing area, set S->extends_to_end_of_line_p. */
23092
23093 if (start == s->row->used[s->area]
23094 && s->area == TEXT_AREA
23095 && ((s->row->fill_line_p
23096 && (s->hl == DRAW_NORMAL_TEXT
23097 || s->hl == DRAW_IMAGE_RAISED
23098 || s->hl == DRAW_IMAGE_SUNKEN))
23099 || s->hl == DRAW_MOUSE_FACE))
23100 s->extends_to_end_of_line_p = 1;
23101
23102 /* If S extends its face to the end of the line, set its
23103 background_width to the distance to the right edge of the drawing
23104 area. */
23105 if (s->extends_to_end_of_line_p)
23106 s->background_width = last_x - s->x + 1;
23107 else
23108 s->background_width = s->width;
23109 }
23110
23111
23112 /* Compute overhangs and x-positions for glyph string S and its
23113 predecessors, or successors. X is the starting x-position for S.
23114 BACKWARD_P non-zero means process predecessors. */
23115
23116 static void
23117 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23118 {
23119 if (backward_p)
23120 {
23121 while (s)
23122 {
23123 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23124 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23125 x -= s->width;
23126 s->x = x;
23127 s = s->prev;
23128 }
23129 }
23130 else
23131 {
23132 while (s)
23133 {
23134 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23135 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23136 s->x = x;
23137 x += s->width;
23138 s = s->next;
23139 }
23140 }
23141 }
23142
23143
23144
23145 /* The following macros are only called from draw_glyphs below.
23146 They reference the following parameters of that function directly:
23147 `w', `row', `area', and `overlap_p'
23148 as well as the following local variables:
23149 `s', `f', and `hdc' (in W32) */
23150
23151 #ifdef HAVE_NTGUI
23152 /* On W32, silently add local `hdc' variable to argument list of
23153 init_glyph_string. */
23154 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23155 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23156 #else
23157 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23158 init_glyph_string (s, char2b, w, row, area, start, hl)
23159 #endif
23160
23161 /* Add a glyph string for a stretch glyph to the list of strings
23162 between HEAD and TAIL. START is the index of the stretch glyph in
23163 row area AREA of glyph row ROW. END is the index of the last glyph
23164 in that glyph row area. X is the current output position assigned
23165 to the new glyph string constructed. HL overrides that face of the
23166 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23167 is the right-most x-position of the drawing area. */
23168
23169 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23170 and below -- keep them on one line. */
23171 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23172 do \
23173 { \
23174 s = alloca (sizeof *s); \
23175 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23176 START = fill_stretch_glyph_string (s, START, END); \
23177 append_glyph_string (&HEAD, &TAIL, s); \
23178 s->x = (X); \
23179 } \
23180 while (0)
23181
23182
23183 /* Add a glyph string for an image glyph to the list of strings
23184 between HEAD and TAIL. START is the index of the image glyph in
23185 row area AREA of glyph row ROW. END is the index of the last glyph
23186 in that glyph row area. X is the current output position assigned
23187 to the new glyph string constructed. HL overrides that face of the
23188 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23189 is the right-most x-position of the drawing area. */
23190
23191 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23192 do \
23193 { \
23194 s = alloca (sizeof *s); \
23195 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23196 fill_image_glyph_string (s); \
23197 append_glyph_string (&HEAD, &TAIL, s); \
23198 ++START; \
23199 s->x = (X); \
23200 } \
23201 while (0)
23202
23203
23204 /* Add a glyph string for a sequence of character glyphs to the list
23205 of strings between HEAD and TAIL. START is the index of the first
23206 glyph in row area AREA of glyph row ROW that is part of the new
23207 glyph string. END is the index of the last glyph in that glyph row
23208 area. X is the current output position assigned to the new glyph
23209 string constructed. HL overrides that face of the glyph; e.g. it
23210 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23211 right-most x-position of the drawing area. */
23212
23213 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23214 do \
23215 { \
23216 int face_id; \
23217 XChar2b *char2b; \
23218 \
23219 face_id = (row)->glyphs[area][START].face_id; \
23220 \
23221 s = alloca (sizeof *s); \
23222 char2b = alloca ((END - START) * sizeof *char2b); \
23223 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23224 append_glyph_string (&HEAD, &TAIL, s); \
23225 s->x = (X); \
23226 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23227 } \
23228 while (0)
23229
23230
23231 /* Add a glyph string for a composite sequence to the list of strings
23232 between HEAD and TAIL. START is the index of the first glyph in
23233 row area AREA of glyph row ROW that is part of the new glyph
23234 string. END is the index of the last glyph in that glyph row area.
23235 X is the current output position assigned to the new glyph string
23236 constructed. HL overrides that face of the glyph; e.g. it is
23237 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23238 x-position of the drawing area. */
23239
23240 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23241 do { \
23242 int face_id = (row)->glyphs[area][START].face_id; \
23243 struct face *base_face = FACE_FROM_ID (f, face_id); \
23244 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23245 struct composition *cmp = composition_table[cmp_id]; \
23246 XChar2b *char2b; \
23247 struct glyph_string *first_s = NULL; \
23248 int n; \
23249 \
23250 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23251 \
23252 /* Make glyph_strings for each glyph sequence that is drawable by \
23253 the same face, and append them to HEAD/TAIL. */ \
23254 for (n = 0; n < cmp->glyph_len;) \
23255 { \
23256 s = alloca (sizeof *s); \
23257 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23258 append_glyph_string (&(HEAD), &(TAIL), s); \
23259 s->cmp = cmp; \
23260 s->cmp_from = n; \
23261 s->x = (X); \
23262 if (n == 0) \
23263 first_s = s; \
23264 n = fill_composite_glyph_string (s, base_face, overlaps); \
23265 } \
23266 \
23267 ++START; \
23268 s = first_s; \
23269 } while (0)
23270
23271
23272 /* Add a glyph string for a glyph-string sequence to the list of strings
23273 between HEAD and TAIL. */
23274
23275 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23276 do { \
23277 int face_id; \
23278 XChar2b *char2b; \
23279 Lisp_Object gstring; \
23280 \
23281 face_id = (row)->glyphs[area][START].face_id; \
23282 gstring = (composition_gstring_from_id \
23283 ((row)->glyphs[area][START].u.cmp.id)); \
23284 s = alloca (sizeof *s); \
23285 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23286 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23287 append_glyph_string (&(HEAD), &(TAIL), s); \
23288 s->x = (X); \
23289 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23290 } while (0)
23291
23292
23293 /* Add a glyph string for a sequence of glyphless character's glyphs
23294 to the list of strings between HEAD and TAIL. The meanings of
23295 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23296
23297 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23298 do \
23299 { \
23300 int face_id; \
23301 \
23302 face_id = (row)->glyphs[area][START].face_id; \
23303 \
23304 s = alloca (sizeof *s); \
23305 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23306 append_glyph_string (&HEAD, &TAIL, s); \
23307 s->x = (X); \
23308 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23309 overlaps); \
23310 } \
23311 while (0)
23312
23313
23314 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23315 of AREA of glyph row ROW on window W between indices START and END.
23316 HL overrides the face for drawing glyph strings, e.g. it is
23317 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23318 x-positions of the drawing area.
23319
23320 This is an ugly monster macro construct because we must use alloca
23321 to allocate glyph strings (because draw_glyphs can be called
23322 asynchronously). */
23323
23324 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23325 do \
23326 { \
23327 HEAD = TAIL = NULL; \
23328 while (START < END) \
23329 { \
23330 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23331 switch (first_glyph->type) \
23332 { \
23333 case CHAR_GLYPH: \
23334 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23335 HL, X, LAST_X); \
23336 break; \
23337 \
23338 case COMPOSITE_GLYPH: \
23339 if (first_glyph->u.cmp.automatic) \
23340 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23341 HL, X, LAST_X); \
23342 else \
23343 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23344 HL, X, LAST_X); \
23345 break; \
23346 \
23347 case STRETCH_GLYPH: \
23348 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23349 HL, X, LAST_X); \
23350 break; \
23351 \
23352 case IMAGE_GLYPH: \
23353 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23354 HL, X, LAST_X); \
23355 break; \
23356 \
23357 case GLYPHLESS_GLYPH: \
23358 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23359 HL, X, LAST_X); \
23360 break; \
23361 \
23362 default: \
23363 emacs_abort (); \
23364 } \
23365 \
23366 if (s) \
23367 { \
23368 set_glyph_string_background_width (s, START, LAST_X); \
23369 (X) += s->width; \
23370 } \
23371 } \
23372 } while (0)
23373
23374
23375 /* Draw glyphs between START and END in AREA of ROW on window W,
23376 starting at x-position X. X is relative to AREA in W. HL is a
23377 face-override with the following meaning:
23378
23379 DRAW_NORMAL_TEXT draw normally
23380 DRAW_CURSOR draw in cursor face
23381 DRAW_MOUSE_FACE draw in mouse face.
23382 DRAW_INVERSE_VIDEO draw in mode line face
23383 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23384 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23385
23386 If OVERLAPS is non-zero, draw only the foreground of characters and
23387 clip to the physical height of ROW. Non-zero value also defines
23388 the overlapping part to be drawn:
23389
23390 OVERLAPS_PRED overlap with preceding rows
23391 OVERLAPS_SUCC overlap with succeeding rows
23392 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23393 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23394
23395 Value is the x-position reached, relative to AREA of W. */
23396
23397 static int
23398 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23399 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23400 enum draw_glyphs_face hl, int overlaps)
23401 {
23402 struct glyph_string *head, *tail;
23403 struct glyph_string *s;
23404 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23405 int i, j, x_reached, last_x, area_left = 0;
23406 struct frame *f = XFRAME (WINDOW_FRAME (w));
23407 DECLARE_HDC (hdc);
23408
23409 ALLOCATE_HDC (hdc, f);
23410
23411 /* Let's rather be paranoid than getting a SEGV. */
23412 end = min (end, row->used[area]);
23413 start = max (0, start);
23414 start = min (end, start);
23415
23416 /* Translate X to frame coordinates. Set last_x to the right
23417 end of the drawing area. */
23418 if (row->full_width_p)
23419 {
23420 /* X is relative to the left edge of W, without scroll bars
23421 or fringes. */
23422 area_left = WINDOW_LEFT_EDGE_X (w);
23423 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23424 }
23425 else
23426 {
23427 area_left = window_box_left (w, area);
23428 last_x = area_left + window_box_width (w, area);
23429 }
23430 x += area_left;
23431
23432 /* Build a doubly-linked list of glyph_string structures between
23433 head and tail from what we have to draw. Note that the macro
23434 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23435 the reason we use a separate variable `i'. */
23436 i = start;
23437 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23438 if (tail)
23439 x_reached = tail->x + tail->background_width;
23440 else
23441 x_reached = x;
23442
23443 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23444 the row, redraw some glyphs in front or following the glyph
23445 strings built above. */
23446 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23447 {
23448 struct glyph_string *h, *t;
23449 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23450 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23451 int check_mouse_face = 0;
23452 int dummy_x = 0;
23453
23454 /* If mouse highlighting is on, we may need to draw adjacent
23455 glyphs using mouse-face highlighting. */
23456 if (area == TEXT_AREA && row->mouse_face_p)
23457 {
23458 struct glyph_row *mouse_beg_row, *mouse_end_row;
23459
23460 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23461 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23462
23463 if (row >= mouse_beg_row && row <= mouse_end_row)
23464 {
23465 check_mouse_face = 1;
23466 mouse_beg_col = (row == mouse_beg_row)
23467 ? hlinfo->mouse_face_beg_col : 0;
23468 mouse_end_col = (row == mouse_end_row)
23469 ? hlinfo->mouse_face_end_col
23470 : row->used[TEXT_AREA];
23471 }
23472 }
23473
23474 /* Compute overhangs for all glyph strings. */
23475 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23476 for (s = head; s; s = s->next)
23477 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23478
23479 /* Prepend glyph strings for glyphs in front of the first glyph
23480 string that are overwritten because of the first glyph
23481 string's left overhang. The background of all strings
23482 prepended must be drawn because the first glyph string
23483 draws over it. */
23484 i = left_overwritten (head);
23485 if (i >= 0)
23486 {
23487 enum draw_glyphs_face overlap_hl;
23488
23489 /* If this row contains mouse highlighting, attempt to draw
23490 the overlapped glyphs with the correct highlight. This
23491 code fails if the overlap encompasses more than one glyph
23492 and mouse-highlight spans only some of these glyphs.
23493 However, making it work perfectly involves a lot more
23494 code, and I don't know if the pathological case occurs in
23495 practice, so we'll stick to this for now. --- cyd */
23496 if (check_mouse_face
23497 && mouse_beg_col < start && mouse_end_col > i)
23498 overlap_hl = DRAW_MOUSE_FACE;
23499 else
23500 overlap_hl = DRAW_NORMAL_TEXT;
23501
23502 j = i;
23503 BUILD_GLYPH_STRINGS (j, start, h, t,
23504 overlap_hl, dummy_x, last_x);
23505 start = i;
23506 compute_overhangs_and_x (t, head->x, 1);
23507 prepend_glyph_string_lists (&head, &tail, h, t);
23508 clip_head = head;
23509 }
23510
23511 /* Prepend glyph strings for glyphs in front of the first glyph
23512 string that overwrite that glyph string because of their
23513 right overhang. For these strings, only the foreground must
23514 be drawn, because it draws over the glyph string at `head'.
23515 The background must not be drawn because this would overwrite
23516 right overhangs of preceding glyphs for which no glyph
23517 strings exist. */
23518 i = left_overwriting (head);
23519 if (i >= 0)
23520 {
23521 enum draw_glyphs_face overlap_hl;
23522
23523 if (check_mouse_face
23524 && mouse_beg_col < start && mouse_end_col > i)
23525 overlap_hl = DRAW_MOUSE_FACE;
23526 else
23527 overlap_hl = DRAW_NORMAL_TEXT;
23528
23529 clip_head = head;
23530 BUILD_GLYPH_STRINGS (i, start, h, t,
23531 overlap_hl, dummy_x, last_x);
23532 for (s = h; s; s = s->next)
23533 s->background_filled_p = 1;
23534 compute_overhangs_and_x (t, head->x, 1);
23535 prepend_glyph_string_lists (&head, &tail, h, t);
23536 }
23537
23538 /* Append glyphs strings for glyphs following the last glyph
23539 string tail that are overwritten by tail. The background of
23540 these strings has to be drawn because tail's foreground draws
23541 over it. */
23542 i = right_overwritten (tail);
23543 if (i >= 0)
23544 {
23545 enum draw_glyphs_face overlap_hl;
23546
23547 if (check_mouse_face
23548 && mouse_beg_col < i && mouse_end_col > end)
23549 overlap_hl = DRAW_MOUSE_FACE;
23550 else
23551 overlap_hl = DRAW_NORMAL_TEXT;
23552
23553 BUILD_GLYPH_STRINGS (end, i, h, t,
23554 overlap_hl, x, last_x);
23555 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23556 we don't have `end = i;' here. */
23557 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23558 append_glyph_string_lists (&head, &tail, h, t);
23559 clip_tail = tail;
23560 }
23561
23562 /* Append glyph strings for glyphs following the last glyph
23563 string tail that overwrite tail. The foreground of such
23564 glyphs has to be drawn because it writes into the background
23565 of tail. The background must not be drawn because it could
23566 paint over the foreground of following glyphs. */
23567 i = right_overwriting (tail);
23568 if (i >= 0)
23569 {
23570 enum draw_glyphs_face overlap_hl;
23571 if (check_mouse_face
23572 && mouse_beg_col < i && mouse_end_col > end)
23573 overlap_hl = DRAW_MOUSE_FACE;
23574 else
23575 overlap_hl = DRAW_NORMAL_TEXT;
23576
23577 clip_tail = tail;
23578 i++; /* We must include the Ith glyph. */
23579 BUILD_GLYPH_STRINGS (end, i, h, t,
23580 overlap_hl, x, last_x);
23581 for (s = h; s; s = s->next)
23582 s->background_filled_p = 1;
23583 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23584 append_glyph_string_lists (&head, &tail, h, t);
23585 }
23586 if (clip_head || clip_tail)
23587 for (s = head; s; s = s->next)
23588 {
23589 s->clip_head = clip_head;
23590 s->clip_tail = clip_tail;
23591 }
23592 }
23593
23594 /* Draw all strings. */
23595 for (s = head; s; s = s->next)
23596 FRAME_RIF (f)->draw_glyph_string (s);
23597
23598 #ifndef HAVE_NS
23599 /* When focus a sole frame and move horizontally, this sets on_p to 0
23600 causing a failure to erase prev cursor position. */
23601 if (area == TEXT_AREA
23602 && !row->full_width_p
23603 /* When drawing overlapping rows, only the glyph strings'
23604 foreground is drawn, which doesn't erase a cursor
23605 completely. */
23606 && !overlaps)
23607 {
23608 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23609 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23610 : (tail ? tail->x + tail->background_width : x));
23611 x0 -= area_left;
23612 x1 -= area_left;
23613
23614 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23615 row->y, MATRIX_ROW_BOTTOM_Y (row));
23616 }
23617 #endif
23618
23619 /* Value is the x-position up to which drawn, relative to AREA of W.
23620 This doesn't include parts drawn because of overhangs. */
23621 if (row->full_width_p)
23622 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23623 else
23624 x_reached -= area_left;
23625
23626 RELEASE_HDC (hdc, f);
23627
23628 return x_reached;
23629 }
23630
23631 /* Expand row matrix if too narrow. Don't expand if area
23632 is not present. */
23633
23634 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23635 { \
23636 if (!fonts_changed_p \
23637 && (it->glyph_row->glyphs[area] \
23638 < it->glyph_row->glyphs[area + 1])) \
23639 { \
23640 it->w->ncols_scale_factor++; \
23641 fonts_changed_p = 1; \
23642 } \
23643 }
23644
23645 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23646 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23647
23648 static inline void
23649 append_glyph (struct it *it)
23650 {
23651 struct glyph *glyph;
23652 enum glyph_row_area area = it->area;
23653
23654 eassert (it->glyph_row);
23655 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23656
23657 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23658 if (glyph < it->glyph_row->glyphs[area + 1])
23659 {
23660 /* If the glyph row is reversed, we need to prepend the glyph
23661 rather than append it. */
23662 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23663 {
23664 struct glyph *g;
23665
23666 /* Make room for the additional glyph. */
23667 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23668 g[1] = *g;
23669 glyph = it->glyph_row->glyphs[area];
23670 }
23671 glyph->charpos = CHARPOS (it->position);
23672 glyph->object = it->object;
23673 if (it->pixel_width > 0)
23674 {
23675 glyph->pixel_width = it->pixel_width;
23676 glyph->padding_p = 0;
23677 }
23678 else
23679 {
23680 /* Assure at least 1-pixel width. Otherwise, cursor can't
23681 be displayed correctly. */
23682 glyph->pixel_width = 1;
23683 glyph->padding_p = 1;
23684 }
23685 glyph->ascent = it->ascent;
23686 glyph->descent = it->descent;
23687 glyph->voffset = it->voffset;
23688 glyph->type = CHAR_GLYPH;
23689 glyph->avoid_cursor_p = it->avoid_cursor_p;
23690 glyph->multibyte_p = it->multibyte_p;
23691 glyph->left_box_line_p = it->start_of_box_run_p;
23692 glyph->right_box_line_p = it->end_of_box_run_p;
23693 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23694 || it->phys_descent > it->descent);
23695 glyph->glyph_not_available_p = it->glyph_not_available_p;
23696 glyph->face_id = it->face_id;
23697 glyph->u.ch = it->char_to_display;
23698 glyph->slice.img = null_glyph_slice;
23699 glyph->font_type = FONT_TYPE_UNKNOWN;
23700 if (it->bidi_p)
23701 {
23702 glyph->resolved_level = it->bidi_it.resolved_level;
23703 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23704 emacs_abort ();
23705 glyph->bidi_type = it->bidi_it.type;
23706 }
23707 else
23708 {
23709 glyph->resolved_level = 0;
23710 glyph->bidi_type = UNKNOWN_BT;
23711 }
23712 ++it->glyph_row->used[area];
23713 }
23714 else
23715 IT_EXPAND_MATRIX_WIDTH (it, area);
23716 }
23717
23718 /* Store one glyph for the composition IT->cmp_it.id in
23719 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23720 non-null. */
23721
23722 static inline void
23723 append_composite_glyph (struct it *it)
23724 {
23725 struct glyph *glyph;
23726 enum glyph_row_area area = it->area;
23727
23728 eassert (it->glyph_row);
23729
23730 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23731 if (glyph < it->glyph_row->glyphs[area + 1])
23732 {
23733 /* If the glyph row is reversed, we need to prepend the glyph
23734 rather than append it. */
23735 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23736 {
23737 struct glyph *g;
23738
23739 /* Make room for the new glyph. */
23740 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23741 g[1] = *g;
23742 glyph = it->glyph_row->glyphs[it->area];
23743 }
23744 glyph->charpos = it->cmp_it.charpos;
23745 glyph->object = it->object;
23746 glyph->pixel_width = it->pixel_width;
23747 glyph->ascent = it->ascent;
23748 glyph->descent = it->descent;
23749 glyph->voffset = it->voffset;
23750 glyph->type = COMPOSITE_GLYPH;
23751 if (it->cmp_it.ch < 0)
23752 {
23753 glyph->u.cmp.automatic = 0;
23754 glyph->u.cmp.id = it->cmp_it.id;
23755 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23756 }
23757 else
23758 {
23759 glyph->u.cmp.automatic = 1;
23760 glyph->u.cmp.id = it->cmp_it.id;
23761 glyph->slice.cmp.from = it->cmp_it.from;
23762 glyph->slice.cmp.to = it->cmp_it.to - 1;
23763 }
23764 glyph->avoid_cursor_p = it->avoid_cursor_p;
23765 glyph->multibyte_p = it->multibyte_p;
23766 glyph->left_box_line_p = it->start_of_box_run_p;
23767 glyph->right_box_line_p = it->end_of_box_run_p;
23768 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23769 || it->phys_descent > it->descent);
23770 glyph->padding_p = 0;
23771 glyph->glyph_not_available_p = 0;
23772 glyph->face_id = it->face_id;
23773 glyph->font_type = FONT_TYPE_UNKNOWN;
23774 if (it->bidi_p)
23775 {
23776 glyph->resolved_level = it->bidi_it.resolved_level;
23777 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23778 emacs_abort ();
23779 glyph->bidi_type = it->bidi_it.type;
23780 }
23781 ++it->glyph_row->used[area];
23782 }
23783 else
23784 IT_EXPAND_MATRIX_WIDTH (it, area);
23785 }
23786
23787
23788 /* Change IT->ascent and IT->height according to the setting of
23789 IT->voffset. */
23790
23791 static inline void
23792 take_vertical_position_into_account (struct it *it)
23793 {
23794 if (it->voffset)
23795 {
23796 if (it->voffset < 0)
23797 /* Increase the ascent so that we can display the text higher
23798 in the line. */
23799 it->ascent -= it->voffset;
23800 else
23801 /* Increase the descent so that we can display the text lower
23802 in the line. */
23803 it->descent += it->voffset;
23804 }
23805 }
23806
23807
23808 /* Produce glyphs/get display metrics for the image IT is loaded with.
23809 See the description of struct display_iterator in dispextern.h for
23810 an overview of struct display_iterator. */
23811
23812 static void
23813 produce_image_glyph (struct it *it)
23814 {
23815 struct image *img;
23816 struct face *face;
23817 int glyph_ascent, crop;
23818 struct glyph_slice slice;
23819
23820 eassert (it->what == IT_IMAGE);
23821
23822 face = FACE_FROM_ID (it->f, it->face_id);
23823 eassert (face);
23824 /* Make sure X resources of the face is loaded. */
23825 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23826
23827 if (it->image_id < 0)
23828 {
23829 /* Fringe bitmap. */
23830 it->ascent = it->phys_ascent = 0;
23831 it->descent = it->phys_descent = 0;
23832 it->pixel_width = 0;
23833 it->nglyphs = 0;
23834 return;
23835 }
23836
23837 img = IMAGE_FROM_ID (it->f, it->image_id);
23838 eassert (img);
23839 /* Make sure X resources of the image is loaded. */
23840 prepare_image_for_display (it->f, img);
23841
23842 slice.x = slice.y = 0;
23843 slice.width = img->width;
23844 slice.height = img->height;
23845
23846 if (INTEGERP (it->slice.x))
23847 slice.x = XINT (it->slice.x);
23848 else if (FLOATP (it->slice.x))
23849 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23850
23851 if (INTEGERP (it->slice.y))
23852 slice.y = XINT (it->slice.y);
23853 else if (FLOATP (it->slice.y))
23854 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23855
23856 if (INTEGERP (it->slice.width))
23857 slice.width = XINT (it->slice.width);
23858 else if (FLOATP (it->slice.width))
23859 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23860
23861 if (INTEGERP (it->slice.height))
23862 slice.height = XINT (it->slice.height);
23863 else if (FLOATP (it->slice.height))
23864 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23865
23866 if (slice.x >= img->width)
23867 slice.x = img->width;
23868 if (slice.y >= img->height)
23869 slice.y = img->height;
23870 if (slice.x + slice.width >= img->width)
23871 slice.width = img->width - slice.x;
23872 if (slice.y + slice.height > img->height)
23873 slice.height = img->height - slice.y;
23874
23875 if (slice.width == 0 || slice.height == 0)
23876 return;
23877
23878 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23879
23880 it->descent = slice.height - glyph_ascent;
23881 if (slice.y == 0)
23882 it->descent += img->vmargin;
23883 if (slice.y + slice.height == img->height)
23884 it->descent += img->vmargin;
23885 it->phys_descent = it->descent;
23886
23887 it->pixel_width = slice.width;
23888 if (slice.x == 0)
23889 it->pixel_width += img->hmargin;
23890 if (slice.x + slice.width == img->width)
23891 it->pixel_width += img->hmargin;
23892
23893 /* It's quite possible for images to have an ascent greater than
23894 their height, so don't get confused in that case. */
23895 if (it->descent < 0)
23896 it->descent = 0;
23897
23898 it->nglyphs = 1;
23899
23900 if (face->box != FACE_NO_BOX)
23901 {
23902 if (face->box_line_width > 0)
23903 {
23904 if (slice.y == 0)
23905 it->ascent += face->box_line_width;
23906 if (slice.y + slice.height == img->height)
23907 it->descent += face->box_line_width;
23908 }
23909
23910 if (it->start_of_box_run_p && slice.x == 0)
23911 it->pixel_width += eabs (face->box_line_width);
23912 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23913 it->pixel_width += eabs (face->box_line_width);
23914 }
23915
23916 take_vertical_position_into_account (it);
23917
23918 /* Automatically crop wide image glyphs at right edge so we can
23919 draw the cursor on same display row. */
23920 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23921 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23922 {
23923 it->pixel_width -= crop;
23924 slice.width -= crop;
23925 }
23926
23927 if (it->glyph_row)
23928 {
23929 struct glyph *glyph;
23930 enum glyph_row_area area = it->area;
23931
23932 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23933 if (glyph < it->glyph_row->glyphs[area + 1])
23934 {
23935 glyph->charpos = CHARPOS (it->position);
23936 glyph->object = it->object;
23937 glyph->pixel_width = it->pixel_width;
23938 glyph->ascent = glyph_ascent;
23939 glyph->descent = it->descent;
23940 glyph->voffset = it->voffset;
23941 glyph->type = IMAGE_GLYPH;
23942 glyph->avoid_cursor_p = it->avoid_cursor_p;
23943 glyph->multibyte_p = it->multibyte_p;
23944 glyph->left_box_line_p = it->start_of_box_run_p;
23945 glyph->right_box_line_p = it->end_of_box_run_p;
23946 glyph->overlaps_vertically_p = 0;
23947 glyph->padding_p = 0;
23948 glyph->glyph_not_available_p = 0;
23949 glyph->face_id = it->face_id;
23950 glyph->u.img_id = img->id;
23951 glyph->slice.img = slice;
23952 glyph->font_type = FONT_TYPE_UNKNOWN;
23953 if (it->bidi_p)
23954 {
23955 glyph->resolved_level = it->bidi_it.resolved_level;
23956 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23957 emacs_abort ();
23958 glyph->bidi_type = it->bidi_it.type;
23959 }
23960 ++it->glyph_row->used[area];
23961 }
23962 else
23963 IT_EXPAND_MATRIX_WIDTH (it, area);
23964 }
23965 }
23966
23967
23968 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23969 of the glyph, WIDTH and HEIGHT are the width and height of the
23970 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23971
23972 static void
23973 append_stretch_glyph (struct it *it, Lisp_Object object,
23974 int width, int height, int ascent)
23975 {
23976 struct glyph *glyph;
23977 enum glyph_row_area area = it->area;
23978
23979 eassert (ascent >= 0 && ascent <= height);
23980
23981 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23982 if (glyph < it->glyph_row->glyphs[area + 1])
23983 {
23984 /* If the glyph row is reversed, we need to prepend the glyph
23985 rather than append it. */
23986 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23987 {
23988 struct glyph *g;
23989
23990 /* Make room for the additional glyph. */
23991 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23992 g[1] = *g;
23993 glyph = it->glyph_row->glyphs[area];
23994 }
23995 glyph->charpos = CHARPOS (it->position);
23996 glyph->object = object;
23997 glyph->pixel_width = width;
23998 glyph->ascent = ascent;
23999 glyph->descent = height - ascent;
24000 glyph->voffset = it->voffset;
24001 glyph->type = STRETCH_GLYPH;
24002 glyph->avoid_cursor_p = it->avoid_cursor_p;
24003 glyph->multibyte_p = it->multibyte_p;
24004 glyph->left_box_line_p = it->start_of_box_run_p;
24005 glyph->right_box_line_p = it->end_of_box_run_p;
24006 glyph->overlaps_vertically_p = 0;
24007 glyph->padding_p = 0;
24008 glyph->glyph_not_available_p = 0;
24009 glyph->face_id = it->face_id;
24010 glyph->u.stretch.ascent = ascent;
24011 glyph->u.stretch.height = height;
24012 glyph->slice.img = null_glyph_slice;
24013 glyph->font_type = FONT_TYPE_UNKNOWN;
24014 if (it->bidi_p)
24015 {
24016 glyph->resolved_level = it->bidi_it.resolved_level;
24017 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24018 emacs_abort ();
24019 glyph->bidi_type = it->bidi_it.type;
24020 }
24021 else
24022 {
24023 glyph->resolved_level = 0;
24024 glyph->bidi_type = UNKNOWN_BT;
24025 }
24026 ++it->glyph_row->used[area];
24027 }
24028 else
24029 IT_EXPAND_MATRIX_WIDTH (it, area);
24030 }
24031
24032 #endif /* HAVE_WINDOW_SYSTEM */
24033
24034 /* Produce a stretch glyph for iterator IT. IT->object is the value
24035 of the glyph property displayed. The value must be a list
24036 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24037 being recognized:
24038
24039 1. `:width WIDTH' specifies that the space should be WIDTH *
24040 canonical char width wide. WIDTH may be an integer or floating
24041 point number.
24042
24043 2. `:relative-width FACTOR' specifies that the width of the stretch
24044 should be computed from the width of the first character having the
24045 `glyph' property, and should be FACTOR times that width.
24046
24047 3. `:align-to HPOS' specifies that the space should be wide enough
24048 to reach HPOS, a value in canonical character units.
24049
24050 Exactly one of the above pairs must be present.
24051
24052 4. `:height HEIGHT' specifies that the height of the stretch produced
24053 should be HEIGHT, measured in canonical character units.
24054
24055 5. `:relative-height FACTOR' specifies that the height of the
24056 stretch should be FACTOR times the height of the characters having
24057 the glyph property.
24058
24059 Either none or exactly one of 4 or 5 must be present.
24060
24061 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24062 of the stretch should be used for the ascent of the stretch.
24063 ASCENT must be in the range 0 <= ASCENT <= 100. */
24064
24065 void
24066 produce_stretch_glyph (struct it *it)
24067 {
24068 /* (space :width WIDTH :height HEIGHT ...) */
24069 Lisp_Object prop, plist;
24070 int width = 0, height = 0, align_to = -1;
24071 int zero_width_ok_p = 0;
24072 int ascent = 0;
24073 double tem;
24074 struct face *face = NULL;
24075 struct font *font = NULL;
24076
24077 #ifdef HAVE_WINDOW_SYSTEM
24078 int zero_height_ok_p = 0;
24079
24080 if (FRAME_WINDOW_P (it->f))
24081 {
24082 face = FACE_FROM_ID (it->f, it->face_id);
24083 font = face->font ? face->font : FRAME_FONT (it->f);
24084 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24085 }
24086 #endif
24087
24088 /* List should start with `space'. */
24089 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24090 plist = XCDR (it->object);
24091
24092 /* Compute the width of the stretch. */
24093 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24094 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24095 {
24096 /* Absolute width `:width WIDTH' specified and valid. */
24097 zero_width_ok_p = 1;
24098 width = (int)tem;
24099 }
24100 #ifdef HAVE_WINDOW_SYSTEM
24101 else if (FRAME_WINDOW_P (it->f)
24102 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24103 {
24104 /* Relative width `:relative-width FACTOR' specified and valid.
24105 Compute the width of the characters having the `glyph'
24106 property. */
24107 struct it it2;
24108 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24109
24110 it2 = *it;
24111 if (it->multibyte_p)
24112 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24113 else
24114 {
24115 it2.c = it2.char_to_display = *p, it2.len = 1;
24116 if (! ASCII_CHAR_P (it2.c))
24117 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24118 }
24119
24120 it2.glyph_row = NULL;
24121 it2.what = IT_CHARACTER;
24122 x_produce_glyphs (&it2);
24123 width = NUMVAL (prop) * it2.pixel_width;
24124 }
24125 #endif /* HAVE_WINDOW_SYSTEM */
24126 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24127 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24128 {
24129 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24130 align_to = (align_to < 0
24131 ? 0
24132 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24133 else if (align_to < 0)
24134 align_to = window_box_left_offset (it->w, TEXT_AREA);
24135 width = max (0, (int)tem + align_to - it->current_x);
24136 zero_width_ok_p = 1;
24137 }
24138 else
24139 /* Nothing specified -> width defaults to canonical char width. */
24140 width = FRAME_COLUMN_WIDTH (it->f);
24141
24142 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24143 width = 1;
24144
24145 #ifdef HAVE_WINDOW_SYSTEM
24146 /* Compute height. */
24147 if (FRAME_WINDOW_P (it->f))
24148 {
24149 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24150 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24151 {
24152 height = (int)tem;
24153 zero_height_ok_p = 1;
24154 }
24155 else if (prop = Fplist_get (plist, QCrelative_height),
24156 NUMVAL (prop) > 0)
24157 height = FONT_HEIGHT (font) * NUMVAL (prop);
24158 else
24159 height = FONT_HEIGHT (font);
24160
24161 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24162 height = 1;
24163
24164 /* Compute percentage of height used for ascent. If
24165 `:ascent ASCENT' is present and valid, use that. Otherwise,
24166 derive the ascent from the font in use. */
24167 if (prop = Fplist_get (plist, QCascent),
24168 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24169 ascent = height * NUMVAL (prop) / 100.0;
24170 else if (!NILP (prop)
24171 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24172 ascent = min (max (0, (int)tem), height);
24173 else
24174 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24175 }
24176 else
24177 #endif /* HAVE_WINDOW_SYSTEM */
24178 height = 1;
24179
24180 if (width > 0 && it->line_wrap != TRUNCATE
24181 && it->current_x + width > it->last_visible_x)
24182 {
24183 width = it->last_visible_x - it->current_x;
24184 #ifdef HAVE_WINDOW_SYSTEM
24185 /* Subtract one more pixel from the stretch width, but only on
24186 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24187 width -= FRAME_WINDOW_P (it->f);
24188 #endif
24189 }
24190
24191 if (width > 0 && height > 0 && it->glyph_row)
24192 {
24193 Lisp_Object o_object = it->object;
24194 Lisp_Object object = it->stack[it->sp - 1].string;
24195 int n = width;
24196
24197 if (!STRINGP (object))
24198 object = it->w->buffer;
24199 #ifdef HAVE_WINDOW_SYSTEM
24200 if (FRAME_WINDOW_P (it->f))
24201 append_stretch_glyph (it, object, width, height, ascent);
24202 else
24203 #endif
24204 {
24205 it->object = object;
24206 it->char_to_display = ' ';
24207 it->pixel_width = it->len = 1;
24208 while (n--)
24209 tty_append_glyph (it);
24210 it->object = o_object;
24211 }
24212 }
24213
24214 it->pixel_width = width;
24215 #ifdef HAVE_WINDOW_SYSTEM
24216 if (FRAME_WINDOW_P (it->f))
24217 {
24218 it->ascent = it->phys_ascent = ascent;
24219 it->descent = it->phys_descent = height - it->ascent;
24220 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24221 take_vertical_position_into_account (it);
24222 }
24223 else
24224 #endif
24225 it->nglyphs = width;
24226 }
24227
24228 /* Get information about special display element WHAT in an
24229 environment described by IT. WHAT is one of IT_TRUNCATION or
24230 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24231 non-null glyph_row member. This function ensures that fields like
24232 face_id, c, len of IT are left untouched. */
24233
24234 static void
24235 produce_special_glyphs (struct it *it, enum display_element_type what)
24236 {
24237 struct it temp_it;
24238 Lisp_Object gc;
24239 GLYPH glyph;
24240
24241 temp_it = *it;
24242 temp_it.object = make_number (0);
24243 memset (&temp_it.current, 0, sizeof temp_it.current);
24244
24245 if (what == IT_CONTINUATION)
24246 {
24247 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24248 if (it->bidi_it.paragraph_dir == R2L)
24249 SET_GLYPH_FROM_CHAR (glyph, '/');
24250 else
24251 SET_GLYPH_FROM_CHAR (glyph, '\\');
24252 if (it->dp
24253 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24254 {
24255 /* FIXME: Should we mirror GC for R2L lines? */
24256 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24257 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24258 }
24259 }
24260 else if (what == IT_TRUNCATION)
24261 {
24262 /* Truncation glyph. */
24263 SET_GLYPH_FROM_CHAR (glyph, '$');
24264 if (it->dp
24265 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24266 {
24267 /* FIXME: Should we mirror GC for R2L lines? */
24268 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24269 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24270 }
24271 }
24272 else
24273 emacs_abort ();
24274
24275 #ifdef HAVE_WINDOW_SYSTEM
24276 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24277 is turned off, we precede the truncation/continuation glyphs by a
24278 stretch glyph whose width is computed such that these special
24279 glyphs are aligned at the window margin, even when very different
24280 fonts are used in different glyph rows. */
24281 if (FRAME_WINDOW_P (temp_it.f)
24282 /* init_iterator calls this with it->glyph_row == NULL, and it
24283 wants only the pixel width of the truncation/continuation
24284 glyphs. */
24285 && temp_it.glyph_row
24286 /* insert_left_trunc_glyphs calls us at the beginning of the
24287 row, and it has its own calculation of the stretch glyph
24288 width. */
24289 && temp_it.glyph_row->used[TEXT_AREA] > 0
24290 && (temp_it.glyph_row->reversed_p
24291 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24292 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24293 {
24294 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24295
24296 if (stretch_width > 0)
24297 {
24298 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24299 struct font *font =
24300 face->font ? face->font : FRAME_FONT (temp_it.f);
24301 int stretch_ascent =
24302 (((temp_it.ascent + temp_it.descent)
24303 * FONT_BASE (font)) / FONT_HEIGHT (font));
24304
24305 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24306 temp_it.ascent + temp_it.descent,
24307 stretch_ascent);
24308 }
24309 }
24310 #endif
24311
24312 temp_it.dp = NULL;
24313 temp_it.what = IT_CHARACTER;
24314 temp_it.len = 1;
24315 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24316 temp_it.face_id = GLYPH_FACE (glyph);
24317 temp_it.len = CHAR_BYTES (temp_it.c);
24318
24319 PRODUCE_GLYPHS (&temp_it);
24320 it->pixel_width = temp_it.pixel_width;
24321 it->nglyphs = temp_it.pixel_width;
24322 }
24323
24324 #ifdef HAVE_WINDOW_SYSTEM
24325
24326 /* Calculate line-height and line-spacing properties.
24327 An integer value specifies explicit pixel value.
24328 A float value specifies relative value to current face height.
24329 A cons (float . face-name) specifies relative value to
24330 height of specified face font.
24331
24332 Returns height in pixels, or nil. */
24333
24334
24335 static Lisp_Object
24336 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24337 int boff, int override)
24338 {
24339 Lisp_Object face_name = Qnil;
24340 int ascent, descent, height;
24341
24342 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24343 return val;
24344
24345 if (CONSP (val))
24346 {
24347 face_name = XCAR (val);
24348 val = XCDR (val);
24349 if (!NUMBERP (val))
24350 val = make_number (1);
24351 if (NILP (face_name))
24352 {
24353 height = it->ascent + it->descent;
24354 goto scale;
24355 }
24356 }
24357
24358 if (NILP (face_name))
24359 {
24360 font = FRAME_FONT (it->f);
24361 boff = FRAME_BASELINE_OFFSET (it->f);
24362 }
24363 else if (EQ (face_name, Qt))
24364 {
24365 override = 0;
24366 }
24367 else
24368 {
24369 int face_id;
24370 struct face *face;
24371
24372 face_id = lookup_named_face (it->f, face_name, 0);
24373 if (face_id < 0)
24374 return make_number (-1);
24375
24376 face = FACE_FROM_ID (it->f, face_id);
24377 font = face->font;
24378 if (font == NULL)
24379 return make_number (-1);
24380 boff = font->baseline_offset;
24381 if (font->vertical_centering)
24382 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24383 }
24384
24385 ascent = FONT_BASE (font) + boff;
24386 descent = FONT_DESCENT (font) - boff;
24387
24388 if (override)
24389 {
24390 it->override_ascent = ascent;
24391 it->override_descent = descent;
24392 it->override_boff = boff;
24393 }
24394
24395 height = ascent + descent;
24396
24397 scale:
24398 if (FLOATP (val))
24399 height = (int)(XFLOAT_DATA (val) * height);
24400 else if (INTEGERP (val))
24401 height *= XINT (val);
24402
24403 return make_number (height);
24404 }
24405
24406
24407 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24408 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24409 and only if this is for a character for which no font was found.
24410
24411 If the display method (it->glyphless_method) is
24412 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24413 length of the acronym or the hexadecimal string, UPPER_XOFF and
24414 UPPER_YOFF are pixel offsets for the upper part of the string,
24415 LOWER_XOFF and LOWER_YOFF are for the lower part.
24416
24417 For the other display methods, LEN through LOWER_YOFF are zero. */
24418
24419 static void
24420 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24421 short upper_xoff, short upper_yoff,
24422 short lower_xoff, short lower_yoff)
24423 {
24424 struct glyph *glyph;
24425 enum glyph_row_area area = it->area;
24426
24427 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24428 if (glyph < it->glyph_row->glyphs[area + 1])
24429 {
24430 /* If the glyph row is reversed, we need to prepend the glyph
24431 rather than append it. */
24432 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24433 {
24434 struct glyph *g;
24435
24436 /* Make room for the additional glyph. */
24437 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24438 g[1] = *g;
24439 glyph = it->glyph_row->glyphs[area];
24440 }
24441 glyph->charpos = CHARPOS (it->position);
24442 glyph->object = it->object;
24443 glyph->pixel_width = it->pixel_width;
24444 glyph->ascent = it->ascent;
24445 glyph->descent = it->descent;
24446 glyph->voffset = it->voffset;
24447 glyph->type = GLYPHLESS_GLYPH;
24448 glyph->u.glyphless.method = it->glyphless_method;
24449 glyph->u.glyphless.for_no_font = for_no_font;
24450 glyph->u.glyphless.len = len;
24451 glyph->u.glyphless.ch = it->c;
24452 glyph->slice.glyphless.upper_xoff = upper_xoff;
24453 glyph->slice.glyphless.upper_yoff = upper_yoff;
24454 glyph->slice.glyphless.lower_xoff = lower_xoff;
24455 glyph->slice.glyphless.lower_yoff = lower_yoff;
24456 glyph->avoid_cursor_p = it->avoid_cursor_p;
24457 glyph->multibyte_p = it->multibyte_p;
24458 glyph->left_box_line_p = it->start_of_box_run_p;
24459 glyph->right_box_line_p = it->end_of_box_run_p;
24460 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24461 || it->phys_descent > it->descent);
24462 glyph->padding_p = 0;
24463 glyph->glyph_not_available_p = 0;
24464 glyph->face_id = face_id;
24465 glyph->font_type = FONT_TYPE_UNKNOWN;
24466 if (it->bidi_p)
24467 {
24468 glyph->resolved_level = it->bidi_it.resolved_level;
24469 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24470 emacs_abort ();
24471 glyph->bidi_type = it->bidi_it.type;
24472 }
24473 ++it->glyph_row->used[area];
24474 }
24475 else
24476 IT_EXPAND_MATRIX_WIDTH (it, area);
24477 }
24478
24479
24480 /* Produce a glyph for a glyphless character for iterator IT.
24481 IT->glyphless_method specifies which method to use for displaying
24482 the character. See the description of enum
24483 glyphless_display_method in dispextern.h for the detail.
24484
24485 FOR_NO_FONT is nonzero if and only if this is for a character for
24486 which no font was found. ACRONYM, if non-nil, is an acronym string
24487 for the character. */
24488
24489 static void
24490 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24491 {
24492 int face_id;
24493 struct face *face;
24494 struct font *font;
24495 int base_width, base_height, width, height;
24496 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24497 int len;
24498
24499 /* Get the metrics of the base font. We always refer to the current
24500 ASCII face. */
24501 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24502 font = face->font ? face->font : FRAME_FONT (it->f);
24503 it->ascent = FONT_BASE (font) + font->baseline_offset;
24504 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24505 base_height = it->ascent + it->descent;
24506 base_width = font->average_width;
24507
24508 /* Get a face ID for the glyph by utilizing a cache (the same way as
24509 done for `escape-glyph' in get_next_display_element). */
24510 if (it->f == last_glyphless_glyph_frame
24511 && it->face_id == last_glyphless_glyph_face_id)
24512 {
24513 face_id = last_glyphless_glyph_merged_face_id;
24514 }
24515 else
24516 {
24517 /* Merge the `glyphless-char' face into the current face. */
24518 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24519 last_glyphless_glyph_frame = it->f;
24520 last_glyphless_glyph_face_id = it->face_id;
24521 last_glyphless_glyph_merged_face_id = face_id;
24522 }
24523
24524 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24525 {
24526 it->pixel_width = THIN_SPACE_WIDTH;
24527 len = 0;
24528 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24529 }
24530 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24531 {
24532 width = CHAR_WIDTH (it->c);
24533 if (width == 0)
24534 width = 1;
24535 else if (width > 4)
24536 width = 4;
24537 it->pixel_width = base_width * width;
24538 len = 0;
24539 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24540 }
24541 else
24542 {
24543 char buf[7];
24544 const char *str;
24545 unsigned int code[6];
24546 int upper_len;
24547 int ascent, descent;
24548 struct font_metrics metrics_upper, metrics_lower;
24549
24550 face = FACE_FROM_ID (it->f, face_id);
24551 font = face->font ? face->font : FRAME_FONT (it->f);
24552 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24553
24554 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24555 {
24556 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24557 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24558 if (CONSP (acronym))
24559 acronym = XCAR (acronym);
24560 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24561 }
24562 else
24563 {
24564 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24565 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24566 str = buf;
24567 }
24568 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24569 code[len] = font->driver->encode_char (font, str[len]);
24570 upper_len = (len + 1) / 2;
24571 font->driver->text_extents (font, code, upper_len,
24572 &metrics_upper);
24573 font->driver->text_extents (font, code + upper_len, len - upper_len,
24574 &metrics_lower);
24575
24576
24577
24578 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24579 width = max (metrics_upper.width, metrics_lower.width) + 4;
24580 upper_xoff = upper_yoff = 2; /* the typical case */
24581 if (base_width >= width)
24582 {
24583 /* Align the upper to the left, the lower to the right. */
24584 it->pixel_width = base_width;
24585 lower_xoff = base_width - 2 - metrics_lower.width;
24586 }
24587 else
24588 {
24589 /* Center the shorter one. */
24590 it->pixel_width = width;
24591 if (metrics_upper.width >= metrics_lower.width)
24592 lower_xoff = (width - metrics_lower.width) / 2;
24593 else
24594 {
24595 /* FIXME: This code doesn't look right. It formerly was
24596 missing the "lower_xoff = 0;", which couldn't have
24597 been right since it left lower_xoff uninitialized. */
24598 lower_xoff = 0;
24599 upper_xoff = (width - metrics_upper.width) / 2;
24600 }
24601 }
24602
24603 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24604 top, bottom, and between upper and lower strings. */
24605 height = (metrics_upper.ascent + metrics_upper.descent
24606 + metrics_lower.ascent + metrics_lower.descent) + 5;
24607 /* Center vertically.
24608 H:base_height, D:base_descent
24609 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24610
24611 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24612 descent = D - H/2 + h/2;
24613 lower_yoff = descent - 2 - ld;
24614 upper_yoff = lower_yoff - la - 1 - ud; */
24615 ascent = - (it->descent - (base_height + height + 1) / 2);
24616 descent = it->descent - (base_height - height) / 2;
24617 lower_yoff = descent - 2 - metrics_lower.descent;
24618 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24619 - metrics_upper.descent);
24620 /* Don't make the height shorter than the base height. */
24621 if (height > base_height)
24622 {
24623 it->ascent = ascent;
24624 it->descent = descent;
24625 }
24626 }
24627
24628 it->phys_ascent = it->ascent;
24629 it->phys_descent = it->descent;
24630 if (it->glyph_row)
24631 append_glyphless_glyph (it, face_id, for_no_font, len,
24632 upper_xoff, upper_yoff,
24633 lower_xoff, lower_yoff);
24634 it->nglyphs = 1;
24635 take_vertical_position_into_account (it);
24636 }
24637
24638
24639 /* RIF:
24640 Produce glyphs/get display metrics for the display element IT is
24641 loaded with. See the description of struct it in dispextern.h
24642 for an overview of struct it. */
24643
24644 void
24645 x_produce_glyphs (struct it *it)
24646 {
24647 int extra_line_spacing = it->extra_line_spacing;
24648
24649 it->glyph_not_available_p = 0;
24650
24651 if (it->what == IT_CHARACTER)
24652 {
24653 XChar2b char2b;
24654 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24655 struct font *font = face->font;
24656 struct font_metrics *pcm = NULL;
24657 int boff; /* baseline offset */
24658
24659 if (font == NULL)
24660 {
24661 /* When no suitable font is found, display this character by
24662 the method specified in the first extra slot of
24663 Vglyphless_char_display. */
24664 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24665
24666 eassert (it->what == IT_GLYPHLESS);
24667 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24668 goto done;
24669 }
24670
24671 boff = font->baseline_offset;
24672 if (font->vertical_centering)
24673 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24674
24675 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24676 {
24677 int stretched_p;
24678
24679 it->nglyphs = 1;
24680
24681 if (it->override_ascent >= 0)
24682 {
24683 it->ascent = it->override_ascent;
24684 it->descent = it->override_descent;
24685 boff = it->override_boff;
24686 }
24687 else
24688 {
24689 it->ascent = FONT_BASE (font) + boff;
24690 it->descent = FONT_DESCENT (font) - boff;
24691 }
24692
24693 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24694 {
24695 pcm = get_per_char_metric (font, &char2b);
24696 if (pcm->width == 0
24697 && pcm->rbearing == 0 && pcm->lbearing == 0)
24698 pcm = NULL;
24699 }
24700
24701 if (pcm)
24702 {
24703 it->phys_ascent = pcm->ascent + boff;
24704 it->phys_descent = pcm->descent - boff;
24705 it->pixel_width = pcm->width;
24706 }
24707 else
24708 {
24709 it->glyph_not_available_p = 1;
24710 it->phys_ascent = it->ascent;
24711 it->phys_descent = it->descent;
24712 it->pixel_width = font->space_width;
24713 }
24714
24715 if (it->constrain_row_ascent_descent_p)
24716 {
24717 if (it->descent > it->max_descent)
24718 {
24719 it->ascent += it->descent - it->max_descent;
24720 it->descent = it->max_descent;
24721 }
24722 if (it->ascent > it->max_ascent)
24723 {
24724 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24725 it->ascent = it->max_ascent;
24726 }
24727 it->phys_ascent = min (it->phys_ascent, it->ascent);
24728 it->phys_descent = min (it->phys_descent, it->descent);
24729 extra_line_spacing = 0;
24730 }
24731
24732 /* If this is a space inside a region of text with
24733 `space-width' property, change its width. */
24734 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24735 if (stretched_p)
24736 it->pixel_width *= XFLOATINT (it->space_width);
24737
24738 /* If face has a box, add the box thickness to the character
24739 height. If character has a box line to the left and/or
24740 right, add the box line width to the character's width. */
24741 if (face->box != FACE_NO_BOX)
24742 {
24743 int thick = face->box_line_width;
24744
24745 if (thick > 0)
24746 {
24747 it->ascent += thick;
24748 it->descent += thick;
24749 }
24750 else
24751 thick = -thick;
24752
24753 if (it->start_of_box_run_p)
24754 it->pixel_width += thick;
24755 if (it->end_of_box_run_p)
24756 it->pixel_width += thick;
24757 }
24758
24759 /* If face has an overline, add the height of the overline
24760 (1 pixel) and a 1 pixel margin to the character height. */
24761 if (face->overline_p)
24762 it->ascent += overline_margin;
24763
24764 if (it->constrain_row_ascent_descent_p)
24765 {
24766 if (it->ascent > it->max_ascent)
24767 it->ascent = it->max_ascent;
24768 if (it->descent > it->max_descent)
24769 it->descent = it->max_descent;
24770 }
24771
24772 take_vertical_position_into_account (it);
24773
24774 /* If we have to actually produce glyphs, do it. */
24775 if (it->glyph_row)
24776 {
24777 if (stretched_p)
24778 {
24779 /* Translate a space with a `space-width' property
24780 into a stretch glyph. */
24781 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24782 / FONT_HEIGHT (font));
24783 append_stretch_glyph (it, it->object, it->pixel_width,
24784 it->ascent + it->descent, ascent);
24785 }
24786 else
24787 append_glyph (it);
24788
24789 /* If characters with lbearing or rbearing are displayed
24790 in this line, record that fact in a flag of the
24791 glyph row. This is used to optimize X output code. */
24792 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24793 it->glyph_row->contains_overlapping_glyphs_p = 1;
24794 }
24795 if (! stretched_p && it->pixel_width == 0)
24796 /* We assure that all visible glyphs have at least 1-pixel
24797 width. */
24798 it->pixel_width = 1;
24799 }
24800 else if (it->char_to_display == '\n')
24801 {
24802 /* A newline has no width, but we need the height of the
24803 line. But if previous part of the line sets a height,
24804 don't increase that height */
24805
24806 Lisp_Object height;
24807 Lisp_Object total_height = Qnil;
24808
24809 it->override_ascent = -1;
24810 it->pixel_width = 0;
24811 it->nglyphs = 0;
24812
24813 height = get_it_property (it, Qline_height);
24814 /* Split (line-height total-height) list */
24815 if (CONSP (height)
24816 && CONSP (XCDR (height))
24817 && NILP (XCDR (XCDR (height))))
24818 {
24819 total_height = XCAR (XCDR (height));
24820 height = XCAR (height);
24821 }
24822 height = calc_line_height_property (it, height, font, boff, 1);
24823
24824 if (it->override_ascent >= 0)
24825 {
24826 it->ascent = it->override_ascent;
24827 it->descent = it->override_descent;
24828 boff = it->override_boff;
24829 }
24830 else
24831 {
24832 it->ascent = FONT_BASE (font) + boff;
24833 it->descent = FONT_DESCENT (font) - boff;
24834 }
24835
24836 if (EQ (height, Qt))
24837 {
24838 if (it->descent > it->max_descent)
24839 {
24840 it->ascent += it->descent - it->max_descent;
24841 it->descent = it->max_descent;
24842 }
24843 if (it->ascent > it->max_ascent)
24844 {
24845 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24846 it->ascent = it->max_ascent;
24847 }
24848 it->phys_ascent = min (it->phys_ascent, it->ascent);
24849 it->phys_descent = min (it->phys_descent, it->descent);
24850 it->constrain_row_ascent_descent_p = 1;
24851 extra_line_spacing = 0;
24852 }
24853 else
24854 {
24855 Lisp_Object spacing;
24856
24857 it->phys_ascent = it->ascent;
24858 it->phys_descent = it->descent;
24859
24860 if ((it->max_ascent > 0 || it->max_descent > 0)
24861 && face->box != FACE_NO_BOX
24862 && face->box_line_width > 0)
24863 {
24864 it->ascent += face->box_line_width;
24865 it->descent += face->box_line_width;
24866 }
24867 if (!NILP (height)
24868 && XINT (height) > it->ascent + it->descent)
24869 it->ascent = XINT (height) - it->descent;
24870
24871 if (!NILP (total_height))
24872 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24873 else
24874 {
24875 spacing = get_it_property (it, Qline_spacing);
24876 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24877 }
24878 if (INTEGERP (spacing))
24879 {
24880 extra_line_spacing = XINT (spacing);
24881 if (!NILP (total_height))
24882 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24883 }
24884 }
24885 }
24886 else /* i.e. (it->char_to_display == '\t') */
24887 {
24888 if (font->space_width > 0)
24889 {
24890 int tab_width = it->tab_width * font->space_width;
24891 int x = it->current_x + it->continuation_lines_width;
24892 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24893
24894 /* If the distance from the current position to the next tab
24895 stop is less than a space character width, use the
24896 tab stop after that. */
24897 if (next_tab_x - x < font->space_width)
24898 next_tab_x += tab_width;
24899
24900 it->pixel_width = next_tab_x - x;
24901 it->nglyphs = 1;
24902 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24903 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24904
24905 if (it->glyph_row)
24906 {
24907 append_stretch_glyph (it, it->object, it->pixel_width,
24908 it->ascent + it->descent, it->ascent);
24909 }
24910 }
24911 else
24912 {
24913 it->pixel_width = 0;
24914 it->nglyphs = 1;
24915 }
24916 }
24917 }
24918 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24919 {
24920 /* A static composition.
24921
24922 Note: A composition is represented as one glyph in the
24923 glyph matrix. There are no padding glyphs.
24924
24925 Important note: pixel_width, ascent, and descent are the
24926 values of what is drawn by draw_glyphs (i.e. the values of
24927 the overall glyphs composed). */
24928 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24929 int boff; /* baseline offset */
24930 struct composition *cmp = composition_table[it->cmp_it.id];
24931 int glyph_len = cmp->glyph_len;
24932 struct font *font = face->font;
24933
24934 it->nglyphs = 1;
24935
24936 /* If we have not yet calculated pixel size data of glyphs of
24937 the composition for the current face font, calculate them
24938 now. Theoretically, we have to check all fonts for the
24939 glyphs, but that requires much time and memory space. So,
24940 here we check only the font of the first glyph. This may
24941 lead to incorrect display, but it's very rare, and C-l
24942 (recenter-top-bottom) can correct the display anyway. */
24943 if (! cmp->font || cmp->font != font)
24944 {
24945 /* Ascent and descent of the font of the first character
24946 of this composition (adjusted by baseline offset).
24947 Ascent and descent of overall glyphs should not be less
24948 than these, respectively. */
24949 int font_ascent, font_descent, font_height;
24950 /* Bounding box of the overall glyphs. */
24951 int leftmost, rightmost, lowest, highest;
24952 int lbearing, rbearing;
24953 int i, width, ascent, descent;
24954 int left_padded = 0, right_padded = 0;
24955 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24956 XChar2b char2b;
24957 struct font_metrics *pcm;
24958 int font_not_found_p;
24959 ptrdiff_t pos;
24960
24961 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24962 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24963 break;
24964 if (glyph_len < cmp->glyph_len)
24965 right_padded = 1;
24966 for (i = 0; i < glyph_len; i++)
24967 {
24968 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24969 break;
24970 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24971 }
24972 if (i > 0)
24973 left_padded = 1;
24974
24975 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24976 : IT_CHARPOS (*it));
24977 /* If no suitable font is found, use the default font. */
24978 font_not_found_p = font == NULL;
24979 if (font_not_found_p)
24980 {
24981 face = face->ascii_face;
24982 font = face->font;
24983 }
24984 boff = font->baseline_offset;
24985 if (font->vertical_centering)
24986 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24987 font_ascent = FONT_BASE (font) + boff;
24988 font_descent = FONT_DESCENT (font) - boff;
24989 font_height = FONT_HEIGHT (font);
24990
24991 cmp->font = font;
24992
24993 pcm = NULL;
24994 if (! font_not_found_p)
24995 {
24996 get_char_face_and_encoding (it->f, c, it->face_id,
24997 &char2b, 0);
24998 pcm = get_per_char_metric (font, &char2b);
24999 }
25000
25001 /* Initialize the bounding box. */
25002 if (pcm)
25003 {
25004 width = cmp->glyph_len > 0 ? pcm->width : 0;
25005 ascent = pcm->ascent;
25006 descent = pcm->descent;
25007 lbearing = pcm->lbearing;
25008 rbearing = pcm->rbearing;
25009 }
25010 else
25011 {
25012 width = cmp->glyph_len > 0 ? font->space_width : 0;
25013 ascent = FONT_BASE (font);
25014 descent = FONT_DESCENT (font);
25015 lbearing = 0;
25016 rbearing = width;
25017 }
25018
25019 rightmost = width;
25020 leftmost = 0;
25021 lowest = - descent + boff;
25022 highest = ascent + boff;
25023
25024 if (! font_not_found_p
25025 && font->default_ascent
25026 && CHAR_TABLE_P (Vuse_default_ascent)
25027 && !NILP (Faref (Vuse_default_ascent,
25028 make_number (it->char_to_display))))
25029 highest = font->default_ascent + boff;
25030
25031 /* Draw the first glyph at the normal position. It may be
25032 shifted to right later if some other glyphs are drawn
25033 at the left. */
25034 cmp->offsets[i * 2] = 0;
25035 cmp->offsets[i * 2 + 1] = boff;
25036 cmp->lbearing = lbearing;
25037 cmp->rbearing = rbearing;
25038
25039 /* Set cmp->offsets for the remaining glyphs. */
25040 for (i++; i < glyph_len; i++)
25041 {
25042 int left, right, btm, top;
25043 int ch = COMPOSITION_GLYPH (cmp, i);
25044 int face_id;
25045 struct face *this_face;
25046
25047 if (ch == '\t')
25048 ch = ' ';
25049 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25050 this_face = FACE_FROM_ID (it->f, face_id);
25051 font = this_face->font;
25052
25053 if (font == NULL)
25054 pcm = NULL;
25055 else
25056 {
25057 get_char_face_and_encoding (it->f, ch, face_id,
25058 &char2b, 0);
25059 pcm = get_per_char_metric (font, &char2b);
25060 }
25061 if (! pcm)
25062 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25063 else
25064 {
25065 width = pcm->width;
25066 ascent = pcm->ascent;
25067 descent = pcm->descent;
25068 lbearing = pcm->lbearing;
25069 rbearing = pcm->rbearing;
25070 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25071 {
25072 /* Relative composition with or without
25073 alternate chars. */
25074 left = (leftmost + rightmost - width) / 2;
25075 btm = - descent + boff;
25076 if (font->relative_compose
25077 && (! CHAR_TABLE_P (Vignore_relative_composition)
25078 || NILP (Faref (Vignore_relative_composition,
25079 make_number (ch)))))
25080 {
25081
25082 if (- descent >= font->relative_compose)
25083 /* One extra pixel between two glyphs. */
25084 btm = highest + 1;
25085 else if (ascent <= 0)
25086 /* One extra pixel between two glyphs. */
25087 btm = lowest - 1 - ascent - descent;
25088 }
25089 }
25090 else
25091 {
25092 /* A composition rule is specified by an integer
25093 value that encodes global and new reference
25094 points (GREF and NREF). GREF and NREF are
25095 specified by numbers as below:
25096
25097 0---1---2 -- ascent
25098 | |
25099 | |
25100 | |
25101 9--10--11 -- center
25102 | |
25103 ---3---4---5--- baseline
25104 | |
25105 6---7---8 -- descent
25106 */
25107 int rule = COMPOSITION_RULE (cmp, i);
25108 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25109
25110 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25111 grefx = gref % 3, nrefx = nref % 3;
25112 grefy = gref / 3, nrefy = nref / 3;
25113 if (xoff)
25114 xoff = font_height * (xoff - 128) / 256;
25115 if (yoff)
25116 yoff = font_height * (yoff - 128) / 256;
25117
25118 left = (leftmost
25119 + grefx * (rightmost - leftmost) / 2
25120 - nrefx * width / 2
25121 + xoff);
25122
25123 btm = ((grefy == 0 ? highest
25124 : grefy == 1 ? 0
25125 : grefy == 2 ? lowest
25126 : (highest + lowest) / 2)
25127 - (nrefy == 0 ? ascent + descent
25128 : nrefy == 1 ? descent - boff
25129 : nrefy == 2 ? 0
25130 : (ascent + descent) / 2)
25131 + yoff);
25132 }
25133
25134 cmp->offsets[i * 2] = left;
25135 cmp->offsets[i * 2 + 1] = btm + descent;
25136
25137 /* Update the bounding box of the overall glyphs. */
25138 if (width > 0)
25139 {
25140 right = left + width;
25141 if (left < leftmost)
25142 leftmost = left;
25143 if (right > rightmost)
25144 rightmost = right;
25145 }
25146 top = btm + descent + ascent;
25147 if (top > highest)
25148 highest = top;
25149 if (btm < lowest)
25150 lowest = btm;
25151
25152 if (cmp->lbearing > left + lbearing)
25153 cmp->lbearing = left + lbearing;
25154 if (cmp->rbearing < left + rbearing)
25155 cmp->rbearing = left + rbearing;
25156 }
25157 }
25158
25159 /* If there are glyphs whose x-offsets are negative,
25160 shift all glyphs to the right and make all x-offsets
25161 non-negative. */
25162 if (leftmost < 0)
25163 {
25164 for (i = 0; i < cmp->glyph_len; i++)
25165 cmp->offsets[i * 2] -= leftmost;
25166 rightmost -= leftmost;
25167 cmp->lbearing -= leftmost;
25168 cmp->rbearing -= leftmost;
25169 }
25170
25171 if (left_padded && cmp->lbearing < 0)
25172 {
25173 for (i = 0; i < cmp->glyph_len; i++)
25174 cmp->offsets[i * 2] -= cmp->lbearing;
25175 rightmost -= cmp->lbearing;
25176 cmp->rbearing -= cmp->lbearing;
25177 cmp->lbearing = 0;
25178 }
25179 if (right_padded && rightmost < cmp->rbearing)
25180 {
25181 rightmost = cmp->rbearing;
25182 }
25183
25184 cmp->pixel_width = rightmost;
25185 cmp->ascent = highest;
25186 cmp->descent = - lowest;
25187 if (cmp->ascent < font_ascent)
25188 cmp->ascent = font_ascent;
25189 if (cmp->descent < font_descent)
25190 cmp->descent = font_descent;
25191 }
25192
25193 if (it->glyph_row
25194 && (cmp->lbearing < 0
25195 || cmp->rbearing > cmp->pixel_width))
25196 it->glyph_row->contains_overlapping_glyphs_p = 1;
25197
25198 it->pixel_width = cmp->pixel_width;
25199 it->ascent = it->phys_ascent = cmp->ascent;
25200 it->descent = it->phys_descent = cmp->descent;
25201 if (face->box != FACE_NO_BOX)
25202 {
25203 int thick = face->box_line_width;
25204
25205 if (thick > 0)
25206 {
25207 it->ascent += thick;
25208 it->descent += thick;
25209 }
25210 else
25211 thick = - thick;
25212
25213 if (it->start_of_box_run_p)
25214 it->pixel_width += thick;
25215 if (it->end_of_box_run_p)
25216 it->pixel_width += thick;
25217 }
25218
25219 /* If face has an overline, add the height of the overline
25220 (1 pixel) and a 1 pixel margin to the character height. */
25221 if (face->overline_p)
25222 it->ascent += overline_margin;
25223
25224 take_vertical_position_into_account (it);
25225 if (it->ascent < 0)
25226 it->ascent = 0;
25227 if (it->descent < 0)
25228 it->descent = 0;
25229
25230 if (it->glyph_row && cmp->glyph_len > 0)
25231 append_composite_glyph (it);
25232 }
25233 else if (it->what == IT_COMPOSITION)
25234 {
25235 /* A dynamic (automatic) composition. */
25236 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25237 Lisp_Object gstring;
25238 struct font_metrics metrics;
25239
25240 it->nglyphs = 1;
25241
25242 gstring = composition_gstring_from_id (it->cmp_it.id);
25243 it->pixel_width
25244 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25245 &metrics);
25246 if (it->glyph_row
25247 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25248 it->glyph_row->contains_overlapping_glyphs_p = 1;
25249 it->ascent = it->phys_ascent = metrics.ascent;
25250 it->descent = it->phys_descent = metrics.descent;
25251 if (face->box != FACE_NO_BOX)
25252 {
25253 int thick = face->box_line_width;
25254
25255 if (thick > 0)
25256 {
25257 it->ascent += thick;
25258 it->descent += thick;
25259 }
25260 else
25261 thick = - thick;
25262
25263 if (it->start_of_box_run_p)
25264 it->pixel_width += thick;
25265 if (it->end_of_box_run_p)
25266 it->pixel_width += thick;
25267 }
25268 /* If face has an overline, add the height of the overline
25269 (1 pixel) and a 1 pixel margin to the character height. */
25270 if (face->overline_p)
25271 it->ascent += overline_margin;
25272 take_vertical_position_into_account (it);
25273 if (it->ascent < 0)
25274 it->ascent = 0;
25275 if (it->descent < 0)
25276 it->descent = 0;
25277
25278 if (it->glyph_row)
25279 append_composite_glyph (it);
25280 }
25281 else if (it->what == IT_GLYPHLESS)
25282 produce_glyphless_glyph (it, 0, Qnil);
25283 else if (it->what == IT_IMAGE)
25284 produce_image_glyph (it);
25285 else if (it->what == IT_STRETCH)
25286 produce_stretch_glyph (it);
25287
25288 done:
25289 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25290 because this isn't true for images with `:ascent 100'. */
25291 eassert (it->ascent >= 0 && it->descent >= 0);
25292 if (it->area == TEXT_AREA)
25293 it->current_x += it->pixel_width;
25294
25295 if (extra_line_spacing > 0)
25296 {
25297 it->descent += extra_line_spacing;
25298 if (extra_line_spacing > it->max_extra_line_spacing)
25299 it->max_extra_line_spacing = extra_line_spacing;
25300 }
25301
25302 it->max_ascent = max (it->max_ascent, it->ascent);
25303 it->max_descent = max (it->max_descent, it->descent);
25304 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25305 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25306 }
25307
25308 /* EXPORT for RIF:
25309 Output LEN glyphs starting at START at the nominal cursor position.
25310 Advance the nominal cursor over the text. The global variable
25311 updated_window contains the window being updated, updated_row is
25312 the glyph row being updated, and updated_area is the area of that
25313 row being updated. */
25314
25315 void
25316 x_write_glyphs (struct glyph *start, int len)
25317 {
25318 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25319
25320 eassert (updated_window && updated_row);
25321 /* When the window is hscrolled, cursor hpos can legitimately be out
25322 of bounds, but we draw the cursor at the corresponding window
25323 margin in that case. */
25324 if (!updated_row->reversed_p && chpos < 0)
25325 chpos = 0;
25326 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25327 chpos = updated_row->used[TEXT_AREA] - 1;
25328
25329 BLOCK_INPUT;
25330
25331 /* Write glyphs. */
25332
25333 hpos = start - updated_row->glyphs[updated_area];
25334 x = draw_glyphs (updated_window, output_cursor.x,
25335 updated_row, updated_area,
25336 hpos, hpos + len,
25337 DRAW_NORMAL_TEXT, 0);
25338
25339 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25340 if (updated_area == TEXT_AREA
25341 && updated_window->phys_cursor_on_p
25342 && updated_window->phys_cursor.vpos == output_cursor.vpos
25343 && chpos >= hpos
25344 && chpos < hpos + len)
25345 updated_window->phys_cursor_on_p = 0;
25346
25347 UNBLOCK_INPUT;
25348
25349 /* Advance the output cursor. */
25350 output_cursor.hpos += len;
25351 output_cursor.x = x;
25352 }
25353
25354
25355 /* EXPORT for RIF:
25356 Insert LEN glyphs from START at the nominal cursor position. */
25357
25358 void
25359 x_insert_glyphs (struct glyph *start, int len)
25360 {
25361 struct frame *f;
25362 struct window *w;
25363 int line_height, shift_by_width, shifted_region_width;
25364 struct glyph_row *row;
25365 struct glyph *glyph;
25366 int frame_x, frame_y;
25367 ptrdiff_t hpos;
25368
25369 eassert (updated_window && updated_row);
25370 BLOCK_INPUT;
25371 w = updated_window;
25372 f = XFRAME (WINDOW_FRAME (w));
25373
25374 /* Get the height of the line we are in. */
25375 row = updated_row;
25376 line_height = row->height;
25377
25378 /* Get the width of the glyphs to insert. */
25379 shift_by_width = 0;
25380 for (glyph = start; glyph < start + len; ++glyph)
25381 shift_by_width += glyph->pixel_width;
25382
25383 /* Get the width of the region to shift right. */
25384 shifted_region_width = (window_box_width (w, updated_area)
25385 - output_cursor.x
25386 - shift_by_width);
25387
25388 /* Shift right. */
25389 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25390 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25391
25392 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25393 line_height, shift_by_width);
25394
25395 /* Write the glyphs. */
25396 hpos = start - row->glyphs[updated_area];
25397 draw_glyphs (w, output_cursor.x, row, updated_area,
25398 hpos, hpos + len,
25399 DRAW_NORMAL_TEXT, 0);
25400
25401 /* Advance the output cursor. */
25402 output_cursor.hpos += len;
25403 output_cursor.x += shift_by_width;
25404 UNBLOCK_INPUT;
25405 }
25406
25407
25408 /* EXPORT for RIF:
25409 Erase the current text line from the nominal cursor position
25410 (inclusive) to pixel column TO_X (exclusive). The idea is that
25411 everything from TO_X onward is already erased.
25412
25413 TO_X is a pixel position relative to updated_area of
25414 updated_window. TO_X == -1 means clear to the end of this area. */
25415
25416 void
25417 x_clear_end_of_line (int to_x)
25418 {
25419 struct frame *f;
25420 struct window *w = updated_window;
25421 int max_x, min_y, max_y;
25422 int from_x, from_y, to_y;
25423
25424 eassert (updated_window && updated_row);
25425 f = XFRAME (w->frame);
25426
25427 if (updated_row->full_width_p)
25428 max_x = WINDOW_TOTAL_WIDTH (w);
25429 else
25430 max_x = window_box_width (w, updated_area);
25431 max_y = window_text_bottom_y (w);
25432
25433 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25434 of window. For TO_X > 0, truncate to end of drawing area. */
25435 if (to_x == 0)
25436 return;
25437 else if (to_x < 0)
25438 to_x = max_x;
25439 else
25440 to_x = min (to_x, max_x);
25441
25442 to_y = min (max_y, output_cursor.y + updated_row->height);
25443
25444 /* Notice if the cursor will be cleared by this operation. */
25445 if (!updated_row->full_width_p)
25446 notice_overwritten_cursor (w, updated_area,
25447 output_cursor.x, -1,
25448 updated_row->y,
25449 MATRIX_ROW_BOTTOM_Y (updated_row));
25450
25451 from_x = output_cursor.x;
25452
25453 /* Translate to frame coordinates. */
25454 if (updated_row->full_width_p)
25455 {
25456 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25457 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25458 }
25459 else
25460 {
25461 int area_left = window_box_left (w, updated_area);
25462 from_x += area_left;
25463 to_x += area_left;
25464 }
25465
25466 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25467 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25468 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25469
25470 /* Prevent inadvertently clearing to end of the X window. */
25471 if (to_x > from_x && to_y > from_y)
25472 {
25473 BLOCK_INPUT;
25474 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25475 to_x - from_x, to_y - from_y);
25476 UNBLOCK_INPUT;
25477 }
25478 }
25479
25480 #endif /* HAVE_WINDOW_SYSTEM */
25481
25482
25483 \f
25484 /***********************************************************************
25485 Cursor types
25486 ***********************************************************************/
25487
25488 /* Value is the internal representation of the specified cursor type
25489 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25490 of the bar cursor. */
25491
25492 static enum text_cursor_kinds
25493 get_specified_cursor_type (Lisp_Object arg, int *width)
25494 {
25495 enum text_cursor_kinds type;
25496
25497 if (NILP (arg))
25498 return NO_CURSOR;
25499
25500 if (EQ (arg, Qbox))
25501 return FILLED_BOX_CURSOR;
25502
25503 if (EQ (arg, Qhollow))
25504 return HOLLOW_BOX_CURSOR;
25505
25506 if (EQ (arg, Qbar))
25507 {
25508 *width = 2;
25509 return BAR_CURSOR;
25510 }
25511
25512 if (CONSP (arg)
25513 && EQ (XCAR (arg), Qbar)
25514 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25515 {
25516 *width = XINT (XCDR (arg));
25517 return BAR_CURSOR;
25518 }
25519
25520 if (EQ (arg, Qhbar))
25521 {
25522 *width = 2;
25523 return HBAR_CURSOR;
25524 }
25525
25526 if (CONSP (arg)
25527 && EQ (XCAR (arg), Qhbar)
25528 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25529 {
25530 *width = XINT (XCDR (arg));
25531 return HBAR_CURSOR;
25532 }
25533
25534 /* Treat anything unknown as "hollow box cursor".
25535 It was bad to signal an error; people have trouble fixing
25536 .Xdefaults with Emacs, when it has something bad in it. */
25537 type = HOLLOW_BOX_CURSOR;
25538
25539 return type;
25540 }
25541
25542 /* Set the default cursor types for specified frame. */
25543 void
25544 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25545 {
25546 int width = 1;
25547 Lisp_Object tem;
25548
25549 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25550 FRAME_CURSOR_WIDTH (f) = width;
25551
25552 /* By default, set up the blink-off state depending on the on-state. */
25553
25554 tem = Fassoc (arg, Vblink_cursor_alist);
25555 if (!NILP (tem))
25556 {
25557 FRAME_BLINK_OFF_CURSOR (f)
25558 = get_specified_cursor_type (XCDR (tem), &width);
25559 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25560 }
25561 else
25562 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25563 }
25564
25565
25566 #ifdef HAVE_WINDOW_SYSTEM
25567
25568 /* Return the cursor we want to be displayed in window W. Return
25569 width of bar/hbar cursor through WIDTH arg. Return with
25570 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25571 (i.e. if the `system caret' should track this cursor).
25572
25573 In a mini-buffer window, we want the cursor only to appear if we
25574 are reading input from this window. For the selected window, we
25575 want the cursor type given by the frame parameter or buffer local
25576 setting of cursor-type. If explicitly marked off, draw no cursor.
25577 In all other cases, we want a hollow box cursor. */
25578
25579 static enum text_cursor_kinds
25580 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25581 int *active_cursor)
25582 {
25583 struct frame *f = XFRAME (w->frame);
25584 struct buffer *b = XBUFFER (w->buffer);
25585 int cursor_type = DEFAULT_CURSOR;
25586 Lisp_Object alt_cursor;
25587 int non_selected = 0;
25588
25589 *active_cursor = 1;
25590
25591 /* Echo area */
25592 if (cursor_in_echo_area
25593 && FRAME_HAS_MINIBUF_P (f)
25594 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25595 {
25596 if (w == XWINDOW (echo_area_window))
25597 {
25598 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25599 {
25600 *width = FRAME_CURSOR_WIDTH (f);
25601 return FRAME_DESIRED_CURSOR (f);
25602 }
25603 else
25604 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25605 }
25606
25607 *active_cursor = 0;
25608 non_selected = 1;
25609 }
25610
25611 /* Detect a nonselected window or nonselected frame. */
25612 else if (w != XWINDOW (f->selected_window)
25613 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25614 {
25615 *active_cursor = 0;
25616
25617 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25618 return NO_CURSOR;
25619
25620 non_selected = 1;
25621 }
25622
25623 /* Never display a cursor in a window in which cursor-type is nil. */
25624 if (NILP (BVAR (b, cursor_type)))
25625 return NO_CURSOR;
25626
25627 /* Get the normal cursor type for this window. */
25628 if (EQ (BVAR (b, cursor_type), Qt))
25629 {
25630 cursor_type = FRAME_DESIRED_CURSOR (f);
25631 *width = FRAME_CURSOR_WIDTH (f);
25632 }
25633 else
25634 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25635
25636 /* Use cursor-in-non-selected-windows instead
25637 for non-selected window or frame. */
25638 if (non_selected)
25639 {
25640 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25641 if (!EQ (Qt, alt_cursor))
25642 return get_specified_cursor_type (alt_cursor, width);
25643 /* t means modify the normal cursor type. */
25644 if (cursor_type == FILLED_BOX_CURSOR)
25645 cursor_type = HOLLOW_BOX_CURSOR;
25646 else if (cursor_type == BAR_CURSOR && *width > 1)
25647 --*width;
25648 return cursor_type;
25649 }
25650
25651 /* Use normal cursor if not blinked off. */
25652 if (!w->cursor_off_p)
25653 {
25654 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25655 {
25656 if (cursor_type == FILLED_BOX_CURSOR)
25657 {
25658 /* Using a block cursor on large images can be very annoying.
25659 So use a hollow cursor for "large" images.
25660 If image is not transparent (no mask), also use hollow cursor. */
25661 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25662 if (img != NULL && IMAGEP (img->spec))
25663 {
25664 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25665 where N = size of default frame font size.
25666 This should cover most of the "tiny" icons people may use. */
25667 if (!img->mask
25668 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25669 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25670 cursor_type = HOLLOW_BOX_CURSOR;
25671 }
25672 }
25673 else if (cursor_type != NO_CURSOR)
25674 {
25675 /* Display current only supports BOX and HOLLOW cursors for images.
25676 So for now, unconditionally use a HOLLOW cursor when cursor is
25677 not a solid box cursor. */
25678 cursor_type = HOLLOW_BOX_CURSOR;
25679 }
25680 }
25681 return cursor_type;
25682 }
25683
25684 /* Cursor is blinked off, so determine how to "toggle" it. */
25685
25686 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25687 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25688 return get_specified_cursor_type (XCDR (alt_cursor), width);
25689
25690 /* Then see if frame has specified a specific blink off cursor type. */
25691 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25692 {
25693 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25694 return FRAME_BLINK_OFF_CURSOR (f);
25695 }
25696
25697 #if 0
25698 /* Some people liked having a permanently visible blinking cursor,
25699 while others had very strong opinions against it. So it was
25700 decided to remove it. KFS 2003-09-03 */
25701
25702 /* Finally perform built-in cursor blinking:
25703 filled box <-> hollow box
25704 wide [h]bar <-> narrow [h]bar
25705 narrow [h]bar <-> no cursor
25706 other type <-> no cursor */
25707
25708 if (cursor_type == FILLED_BOX_CURSOR)
25709 return HOLLOW_BOX_CURSOR;
25710
25711 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25712 {
25713 *width = 1;
25714 return cursor_type;
25715 }
25716 #endif
25717
25718 return NO_CURSOR;
25719 }
25720
25721
25722 /* Notice when the text cursor of window W has been completely
25723 overwritten by a drawing operation that outputs glyphs in AREA
25724 starting at X0 and ending at X1 in the line starting at Y0 and
25725 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25726 the rest of the line after X0 has been written. Y coordinates
25727 are window-relative. */
25728
25729 static void
25730 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25731 int x0, int x1, int y0, int y1)
25732 {
25733 int cx0, cx1, cy0, cy1;
25734 struct glyph_row *row;
25735
25736 if (!w->phys_cursor_on_p)
25737 return;
25738 if (area != TEXT_AREA)
25739 return;
25740
25741 if (w->phys_cursor.vpos < 0
25742 || w->phys_cursor.vpos >= w->current_matrix->nrows
25743 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25744 !(row->enabled_p && row->displays_text_p)))
25745 return;
25746
25747 if (row->cursor_in_fringe_p)
25748 {
25749 row->cursor_in_fringe_p = 0;
25750 draw_fringe_bitmap (w, row, row->reversed_p);
25751 w->phys_cursor_on_p = 0;
25752 return;
25753 }
25754
25755 cx0 = w->phys_cursor.x;
25756 cx1 = cx0 + w->phys_cursor_width;
25757 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25758 return;
25759
25760 /* The cursor image will be completely removed from the
25761 screen if the output area intersects the cursor area in
25762 y-direction. When we draw in [y0 y1[, and some part of
25763 the cursor is at y < y0, that part must have been drawn
25764 before. When scrolling, the cursor is erased before
25765 actually scrolling, so we don't come here. When not
25766 scrolling, the rows above the old cursor row must have
25767 changed, and in this case these rows must have written
25768 over the cursor image.
25769
25770 Likewise if part of the cursor is below y1, with the
25771 exception of the cursor being in the first blank row at
25772 the buffer and window end because update_text_area
25773 doesn't draw that row. (Except when it does, but
25774 that's handled in update_text_area.) */
25775
25776 cy0 = w->phys_cursor.y;
25777 cy1 = cy0 + w->phys_cursor_height;
25778 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25779 return;
25780
25781 w->phys_cursor_on_p = 0;
25782 }
25783
25784 #endif /* HAVE_WINDOW_SYSTEM */
25785
25786 \f
25787 /************************************************************************
25788 Mouse Face
25789 ************************************************************************/
25790
25791 #ifdef HAVE_WINDOW_SYSTEM
25792
25793 /* EXPORT for RIF:
25794 Fix the display of area AREA of overlapping row ROW in window W
25795 with respect to the overlapping part OVERLAPS. */
25796
25797 void
25798 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25799 enum glyph_row_area area, int overlaps)
25800 {
25801 int i, x;
25802
25803 BLOCK_INPUT;
25804
25805 x = 0;
25806 for (i = 0; i < row->used[area];)
25807 {
25808 if (row->glyphs[area][i].overlaps_vertically_p)
25809 {
25810 int start = i, start_x = x;
25811
25812 do
25813 {
25814 x += row->glyphs[area][i].pixel_width;
25815 ++i;
25816 }
25817 while (i < row->used[area]
25818 && row->glyphs[area][i].overlaps_vertically_p);
25819
25820 draw_glyphs (w, start_x, row, area,
25821 start, i,
25822 DRAW_NORMAL_TEXT, overlaps);
25823 }
25824 else
25825 {
25826 x += row->glyphs[area][i].pixel_width;
25827 ++i;
25828 }
25829 }
25830
25831 UNBLOCK_INPUT;
25832 }
25833
25834
25835 /* EXPORT:
25836 Draw the cursor glyph of window W in glyph row ROW. See the
25837 comment of draw_glyphs for the meaning of HL. */
25838
25839 void
25840 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25841 enum draw_glyphs_face hl)
25842 {
25843 /* If cursor hpos is out of bounds, don't draw garbage. This can
25844 happen in mini-buffer windows when switching between echo area
25845 glyphs and mini-buffer. */
25846 if ((row->reversed_p
25847 ? (w->phys_cursor.hpos >= 0)
25848 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25849 {
25850 int on_p = w->phys_cursor_on_p;
25851 int x1;
25852 int hpos = w->phys_cursor.hpos;
25853
25854 /* When the window is hscrolled, cursor hpos can legitimately be
25855 out of bounds, but we draw the cursor at the corresponding
25856 window margin in that case. */
25857 if (!row->reversed_p && hpos < 0)
25858 hpos = 0;
25859 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25860 hpos = row->used[TEXT_AREA] - 1;
25861
25862 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25863 hl, 0);
25864 w->phys_cursor_on_p = on_p;
25865
25866 if (hl == DRAW_CURSOR)
25867 w->phys_cursor_width = x1 - w->phys_cursor.x;
25868 /* When we erase the cursor, and ROW is overlapped by other
25869 rows, make sure that these overlapping parts of other rows
25870 are redrawn. */
25871 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25872 {
25873 w->phys_cursor_width = x1 - w->phys_cursor.x;
25874
25875 if (row > w->current_matrix->rows
25876 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25877 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25878 OVERLAPS_ERASED_CURSOR);
25879
25880 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25881 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25882 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25883 OVERLAPS_ERASED_CURSOR);
25884 }
25885 }
25886 }
25887
25888
25889 /* EXPORT:
25890 Erase the image of a cursor of window W from the screen. */
25891
25892 void
25893 erase_phys_cursor (struct window *w)
25894 {
25895 struct frame *f = XFRAME (w->frame);
25896 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25897 int hpos = w->phys_cursor.hpos;
25898 int vpos = w->phys_cursor.vpos;
25899 int mouse_face_here_p = 0;
25900 struct glyph_matrix *active_glyphs = w->current_matrix;
25901 struct glyph_row *cursor_row;
25902 struct glyph *cursor_glyph;
25903 enum draw_glyphs_face hl;
25904
25905 /* No cursor displayed or row invalidated => nothing to do on the
25906 screen. */
25907 if (w->phys_cursor_type == NO_CURSOR)
25908 goto mark_cursor_off;
25909
25910 /* VPOS >= active_glyphs->nrows means that window has been resized.
25911 Don't bother to erase the cursor. */
25912 if (vpos >= active_glyphs->nrows)
25913 goto mark_cursor_off;
25914
25915 /* If row containing cursor is marked invalid, there is nothing we
25916 can do. */
25917 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25918 if (!cursor_row->enabled_p)
25919 goto mark_cursor_off;
25920
25921 /* If line spacing is > 0, old cursor may only be partially visible in
25922 window after split-window. So adjust visible height. */
25923 cursor_row->visible_height = min (cursor_row->visible_height,
25924 window_text_bottom_y (w) - cursor_row->y);
25925
25926 /* If row is completely invisible, don't attempt to delete a cursor which
25927 isn't there. This can happen if cursor is at top of a window, and
25928 we switch to a buffer with a header line in that window. */
25929 if (cursor_row->visible_height <= 0)
25930 goto mark_cursor_off;
25931
25932 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25933 if (cursor_row->cursor_in_fringe_p)
25934 {
25935 cursor_row->cursor_in_fringe_p = 0;
25936 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25937 goto mark_cursor_off;
25938 }
25939
25940 /* This can happen when the new row is shorter than the old one.
25941 In this case, either draw_glyphs or clear_end_of_line
25942 should have cleared the cursor. Note that we wouldn't be
25943 able to erase the cursor in this case because we don't have a
25944 cursor glyph at hand. */
25945 if ((cursor_row->reversed_p
25946 ? (w->phys_cursor.hpos < 0)
25947 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25948 goto mark_cursor_off;
25949
25950 /* When the window is hscrolled, cursor hpos can legitimately be out
25951 of bounds, but we draw the cursor at the corresponding window
25952 margin in that case. */
25953 if (!cursor_row->reversed_p && hpos < 0)
25954 hpos = 0;
25955 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25956 hpos = cursor_row->used[TEXT_AREA] - 1;
25957
25958 /* If the cursor is in the mouse face area, redisplay that when
25959 we clear the cursor. */
25960 if (! NILP (hlinfo->mouse_face_window)
25961 && coords_in_mouse_face_p (w, hpos, vpos)
25962 /* Don't redraw the cursor's spot in mouse face if it is at the
25963 end of a line (on a newline). The cursor appears there, but
25964 mouse highlighting does not. */
25965 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25966 mouse_face_here_p = 1;
25967
25968 /* Maybe clear the display under the cursor. */
25969 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25970 {
25971 int x, y, left_x;
25972 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25973 int width;
25974
25975 cursor_glyph = get_phys_cursor_glyph (w);
25976 if (cursor_glyph == NULL)
25977 goto mark_cursor_off;
25978
25979 width = cursor_glyph->pixel_width;
25980 left_x = window_box_left_offset (w, TEXT_AREA);
25981 x = w->phys_cursor.x;
25982 if (x < left_x)
25983 width -= left_x - x;
25984 width = min (width, window_box_width (w, TEXT_AREA) - x);
25985 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25986 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25987
25988 if (width > 0)
25989 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25990 }
25991
25992 /* Erase the cursor by redrawing the character underneath it. */
25993 if (mouse_face_here_p)
25994 hl = DRAW_MOUSE_FACE;
25995 else
25996 hl = DRAW_NORMAL_TEXT;
25997 draw_phys_cursor_glyph (w, cursor_row, hl);
25998
25999 mark_cursor_off:
26000 w->phys_cursor_on_p = 0;
26001 w->phys_cursor_type = NO_CURSOR;
26002 }
26003
26004
26005 /* EXPORT:
26006 Display or clear cursor of window W. If ON is zero, clear the
26007 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26008 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26009
26010 void
26011 display_and_set_cursor (struct window *w, int on,
26012 int hpos, int vpos, int x, int y)
26013 {
26014 struct frame *f = XFRAME (w->frame);
26015 int new_cursor_type;
26016 int new_cursor_width;
26017 int active_cursor;
26018 struct glyph_row *glyph_row;
26019 struct glyph *glyph;
26020
26021 /* This is pointless on invisible frames, and dangerous on garbaged
26022 windows and frames; in the latter case, the frame or window may
26023 be in the midst of changing its size, and x and y may be off the
26024 window. */
26025 if (! FRAME_VISIBLE_P (f)
26026 || FRAME_GARBAGED_P (f)
26027 || vpos >= w->current_matrix->nrows
26028 || hpos >= w->current_matrix->matrix_w)
26029 return;
26030
26031 /* If cursor is off and we want it off, return quickly. */
26032 if (!on && !w->phys_cursor_on_p)
26033 return;
26034
26035 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26036 /* If cursor row is not enabled, we don't really know where to
26037 display the cursor. */
26038 if (!glyph_row->enabled_p)
26039 {
26040 w->phys_cursor_on_p = 0;
26041 return;
26042 }
26043
26044 glyph = NULL;
26045 if (!glyph_row->exact_window_width_line_p
26046 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26047 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26048
26049 eassert (interrupt_input_blocked);
26050
26051 /* Set new_cursor_type to the cursor we want to be displayed. */
26052 new_cursor_type = get_window_cursor_type (w, glyph,
26053 &new_cursor_width, &active_cursor);
26054
26055 /* If cursor is currently being shown and we don't want it to be or
26056 it is in the wrong place, or the cursor type is not what we want,
26057 erase it. */
26058 if (w->phys_cursor_on_p
26059 && (!on
26060 || w->phys_cursor.x != x
26061 || w->phys_cursor.y != y
26062 || new_cursor_type != w->phys_cursor_type
26063 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26064 && new_cursor_width != w->phys_cursor_width)))
26065 erase_phys_cursor (w);
26066
26067 /* Don't check phys_cursor_on_p here because that flag is only set
26068 to zero in some cases where we know that the cursor has been
26069 completely erased, to avoid the extra work of erasing the cursor
26070 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26071 still not be visible, or it has only been partly erased. */
26072 if (on)
26073 {
26074 w->phys_cursor_ascent = glyph_row->ascent;
26075 w->phys_cursor_height = glyph_row->height;
26076
26077 /* Set phys_cursor_.* before x_draw_.* is called because some
26078 of them may need the information. */
26079 w->phys_cursor.x = x;
26080 w->phys_cursor.y = glyph_row->y;
26081 w->phys_cursor.hpos = hpos;
26082 w->phys_cursor.vpos = vpos;
26083 }
26084
26085 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26086 new_cursor_type, new_cursor_width,
26087 on, active_cursor);
26088 }
26089
26090
26091 /* Switch the display of W's cursor on or off, according to the value
26092 of ON. */
26093
26094 static void
26095 update_window_cursor (struct window *w, int on)
26096 {
26097 /* Don't update cursor in windows whose frame is in the process
26098 of being deleted. */
26099 if (w->current_matrix)
26100 {
26101 int hpos = w->phys_cursor.hpos;
26102 int vpos = w->phys_cursor.vpos;
26103 struct glyph_row *row;
26104
26105 if (vpos >= w->current_matrix->nrows
26106 || hpos >= w->current_matrix->matrix_w)
26107 return;
26108
26109 row = MATRIX_ROW (w->current_matrix, vpos);
26110
26111 /* When the window is hscrolled, cursor hpos can legitimately be
26112 out of bounds, but we draw the cursor at the corresponding
26113 window margin in that case. */
26114 if (!row->reversed_p && hpos < 0)
26115 hpos = 0;
26116 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26117 hpos = row->used[TEXT_AREA] - 1;
26118
26119 BLOCK_INPUT;
26120 display_and_set_cursor (w, on, hpos, vpos,
26121 w->phys_cursor.x, w->phys_cursor.y);
26122 UNBLOCK_INPUT;
26123 }
26124 }
26125
26126
26127 /* Call update_window_cursor with parameter ON_P on all leaf windows
26128 in the window tree rooted at W. */
26129
26130 static void
26131 update_cursor_in_window_tree (struct window *w, int on_p)
26132 {
26133 while (w)
26134 {
26135 if (!NILP (w->hchild))
26136 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26137 else if (!NILP (w->vchild))
26138 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26139 else
26140 update_window_cursor (w, on_p);
26141
26142 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26143 }
26144 }
26145
26146
26147 /* EXPORT:
26148 Display the cursor on window W, or clear it, according to ON_P.
26149 Don't change the cursor's position. */
26150
26151 void
26152 x_update_cursor (struct frame *f, int on_p)
26153 {
26154 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26155 }
26156
26157
26158 /* EXPORT:
26159 Clear the cursor of window W to background color, and mark the
26160 cursor as not shown. This is used when the text where the cursor
26161 is about to be rewritten. */
26162
26163 void
26164 x_clear_cursor (struct window *w)
26165 {
26166 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26167 update_window_cursor (w, 0);
26168 }
26169
26170 #endif /* HAVE_WINDOW_SYSTEM */
26171
26172 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26173 and MSDOS. */
26174 static void
26175 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26176 int start_hpos, int end_hpos,
26177 enum draw_glyphs_face draw)
26178 {
26179 #ifdef HAVE_WINDOW_SYSTEM
26180 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26181 {
26182 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26183 return;
26184 }
26185 #endif
26186 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26187 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26188 #endif
26189 }
26190
26191 /* Display the active region described by mouse_face_* according to DRAW. */
26192
26193 static void
26194 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26195 {
26196 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26197 struct frame *f = XFRAME (WINDOW_FRAME (w));
26198
26199 if (/* If window is in the process of being destroyed, don't bother
26200 to do anything. */
26201 w->current_matrix != NULL
26202 /* Don't update mouse highlight if hidden */
26203 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26204 /* Recognize when we are called to operate on rows that don't exist
26205 anymore. This can happen when a window is split. */
26206 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26207 {
26208 int phys_cursor_on_p = w->phys_cursor_on_p;
26209 struct glyph_row *row, *first, *last;
26210
26211 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26212 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26213
26214 for (row = first; row <= last && row->enabled_p; ++row)
26215 {
26216 int start_hpos, end_hpos, start_x;
26217
26218 /* For all but the first row, the highlight starts at column 0. */
26219 if (row == first)
26220 {
26221 /* R2L rows have BEG and END in reversed order, but the
26222 screen drawing geometry is always left to right. So
26223 we need to mirror the beginning and end of the
26224 highlighted area in R2L rows. */
26225 if (!row->reversed_p)
26226 {
26227 start_hpos = hlinfo->mouse_face_beg_col;
26228 start_x = hlinfo->mouse_face_beg_x;
26229 }
26230 else if (row == last)
26231 {
26232 start_hpos = hlinfo->mouse_face_end_col;
26233 start_x = hlinfo->mouse_face_end_x;
26234 }
26235 else
26236 {
26237 start_hpos = 0;
26238 start_x = 0;
26239 }
26240 }
26241 else if (row->reversed_p && row == last)
26242 {
26243 start_hpos = hlinfo->mouse_face_end_col;
26244 start_x = hlinfo->mouse_face_end_x;
26245 }
26246 else
26247 {
26248 start_hpos = 0;
26249 start_x = 0;
26250 }
26251
26252 if (row == last)
26253 {
26254 if (!row->reversed_p)
26255 end_hpos = hlinfo->mouse_face_end_col;
26256 else if (row == first)
26257 end_hpos = hlinfo->mouse_face_beg_col;
26258 else
26259 {
26260 end_hpos = row->used[TEXT_AREA];
26261 if (draw == DRAW_NORMAL_TEXT)
26262 row->fill_line_p = 1; /* Clear to end of line */
26263 }
26264 }
26265 else if (row->reversed_p && row == first)
26266 end_hpos = hlinfo->mouse_face_beg_col;
26267 else
26268 {
26269 end_hpos = row->used[TEXT_AREA];
26270 if (draw == DRAW_NORMAL_TEXT)
26271 row->fill_line_p = 1; /* Clear to end of line */
26272 }
26273
26274 if (end_hpos > start_hpos)
26275 {
26276 draw_row_with_mouse_face (w, start_x, row,
26277 start_hpos, end_hpos, draw);
26278
26279 row->mouse_face_p
26280 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26281 }
26282 }
26283
26284 #ifdef HAVE_WINDOW_SYSTEM
26285 /* When we've written over the cursor, arrange for it to
26286 be displayed again. */
26287 if (FRAME_WINDOW_P (f)
26288 && phys_cursor_on_p && !w->phys_cursor_on_p)
26289 {
26290 int hpos = w->phys_cursor.hpos;
26291
26292 /* When the window is hscrolled, cursor hpos can legitimately be
26293 out of bounds, but we draw the cursor at the corresponding
26294 window margin in that case. */
26295 if (!row->reversed_p && hpos < 0)
26296 hpos = 0;
26297 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26298 hpos = row->used[TEXT_AREA] - 1;
26299
26300 BLOCK_INPUT;
26301 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26302 w->phys_cursor.x, w->phys_cursor.y);
26303 UNBLOCK_INPUT;
26304 }
26305 #endif /* HAVE_WINDOW_SYSTEM */
26306 }
26307
26308 #ifdef HAVE_WINDOW_SYSTEM
26309 /* Change the mouse cursor. */
26310 if (FRAME_WINDOW_P (f))
26311 {
26312 if (draw == DRAW_NORMAL_TEXT
26313 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26314 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26315 else if (draw == DRAW_MOUSE_FACE)
26316 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26317 else
26318 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26319 }
26320 #endif /* HAVE_WINDOW_SYSTEM */
26321 }
26322
26323 /* EXPORT:
26324 Clear out the mouse-highlighted active region.
26325 Redraw it un-highlighted first. Value is non-zero if mouse
26326 face was actually drawn unhighlighted. */
26327
26328 int
26329 clear_mouse_face (Mouse_HLInfo *hlinfo)
26330 {
26331 int cleared = 0;
26332
26333 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26334 {
26335 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26336 cleared = 1;
26337 }
26338
26339 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26340 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26341 hlinfo->mouse_face_window = Qnil;
26342 hlinfo->mouse_face_overlay = Qnil;
26343 return cleared;
26344 }
26345
26346 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26347 within the mouse face on that window. */
26348 static int
26349 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26350 {
26351 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26352
26353 /* Quickly resolve the easy cases. */
26354 if (!(WINDOWP (hlinfo->mouse_face_window)
26355 && XWINDOW (hlinfo->mouse_face_window) == w))
26356 return 0;
26357 if (vpos < hlinfo->mouse_face_beg_row
26358 || vpos > hlinfo->mouse_face_end_row)
26359 return 0;
26360 if (vpos > hlinfo->mouse_face_beg_row
26361 && vpos < hlinfo->mouse_face_end_row)
26362 return 1;
26363
26364 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26365 {
26366 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26367 {
26368 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26369 return 1;
26370 }
26371 else if ((vpos == hlinfo->mouse_face_beg_row
26372 && hpos >= hlinfo->mouse_face_beg_col)
26373 || (vpos == hlinfo->mouse_face_end_row
26374 && hpos < hlinfo->mouse_face_end_col))
26375 return 1;
26376 }
26377 else
26378 {
26379 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26380 {
26381 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26382 return 1;
26383 }
26384 else if ((vpos == hlinfo->mouse_face_beg_row
26385 && hpos <= hlinfo->mouse_face_beg_col)
26386 || (vpos == hlinfo->mouse_face_end_row
26387 && hpos > hlinfo->mouse_face_end_col))
26388 return 1;
26389 }
26390 return 0;
26391 }
26392
26393
26394 /* EXPORT:
26395 Non-zero if physical cursor of window W is within mouse face. */
26396
26397 int
26398 cursor_in_mouse_face_p (struct window *w)
26399 {
26400 int hpos = w->phys_cursor.hpos;
26401 int vpos = w->phys_cursor.vpos;
26402 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26403
26404 /* When the window is hscrolled, cursor hpos can legitimately be out
26405 of bounds, but we draw the cursor at the corresponding window
26406 margin in that case. */
26407 if (!row->reversed_p && hpos < 0)
26408 hpos = 0;
26409 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26410 hpos = row->used[TEXT_AREA] - 1;
26411
26412 return coords_in_mouse_face_p (w, hpos, vpos);
26413 }
26414
26415
26416 \f
26417 /* Find the glyph rows START_ROW and END_ROW of window W that display
26418 characters between buffer positions START_CHARPOS and END_CHARPOS
26419 (excluding END_CHARPOS). DISP_STRING is a display string that
26420 covers these buffer positions. This is similar to
26421 row_containing_pos, but is more accurate when bidi reordering makes
26422 buffer positions change non-linearly with glyph rows. */
26423 static void
26424 rows_from_pos_range (struct window *w,
26425 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26426 Lisp_Object disp_string,
26427 struct glyph_row **start, struct glyph_row **end)
26428 {
26429 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26430 int last_y = window_text_bottom_y (w);
26431 struct glyph_row *row;
26432
26433 *start = NULL;
26434 *end = NULL;
26435
26436 while (!first->enabled_p
26437 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26438 first++;
26439
26440 /* Find the START row. */
26441 for (row = first;
26442 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26443 row++)
26444 {
26445 /* A row can potentially be the START row if the range of the
26446 characters it displays intersects the range
26447 [START_CHARPOS..END_CHARPOS). */
26448 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26449 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26450 /* See the commentary in row_containing_pos, for the
26451 explanation of the complicated way to check whether
26452 some position is beyond the end of the characters
26453 displayed by a row. */
26454 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26455 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26456 && !row->ends_at_zv_p
26457 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26458 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26459 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26460 && !row->ends_at_zv_p
26461 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26462 {
26463 /* Found a candidate row. Now make sure at least one of the
26464 glyphs it displays has a charpos from the range
26465 [START_CHARPOS..END_CHARPOS).
26466
26467 This is not obvious because bidi reordering could make
26468 buffer positions of a row be 1,2,3,102,101,100, and if we
26469 want to highlight characters in [50..60), we don't want
26470 this row, even though [50..60) does intersect [1..103),
26471 the range of character positions given by the row's start
26472 and end positions. */
26473 struct glyph *g = row->glyphs[TEXT_AREA];
26474 struct glyph *e = g + row->used[TEXT_AREA];
26475
26476 while (g < e)
26477 {
26478 if (((BUFFERP (g->object) || INTEGERP (g->object))
26479 && start_charpos <= g->charpos && g->charpos < end_charpos)
26480 /* A glyph that comes from DISP_STRING is by
26481 definition to be highlighted. */
26482 || EQ (g->object, disp_string))
26483 *start = row;
26484 g++;
26485 }
26486 if (*start)
26487 break;
26488 }
26489 }
26490
26491 /* Find the END row. */
26492 if (!*start
26493 /* If the last row is partially visible, start looking for END
26494 from that row, instead of starting from FIRST. */
26495 && !(row->enabled_p
26496 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26497 row = first;
26498 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26499 {
26500 struct glyph_row *next = row + 1;
26501 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26502
26503 if (!next->enabled_p
26504 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26505 /* The first row >= START whose range of displayed characters
26506 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26507 is the row END + 1. */
26508 || (start_charpos < next_start
26509 && end_charpos < next_start)
26510 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26511 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26512 && !next->ends_at_zv_p
26513 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26514 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26515 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26516 && !next->ends_at_zv_p
26517 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26518 {
26519 *end = row;
26520 break;
26521 }
26522 else
26523 {
26524 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26525 but none of the characters it displays are in the range, it is
26526 also END + 1. */
26527 struct glyph *g = next->glyphs[TEXT_AREA];
26528 struct glyph *s = g;
26529 struct glyph *e = g + next->used[TEXT_AREA];
26530
26531 while (g < e)
26532 {
26533 if (((BUFFERP (g->object) || INTEGERP (g->object))
26534 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26535 /* If the buffer position of the first glyph in
26536 the row is equal to END_CHARPOS, it means
26537 the last character to be highlighted is the
26538 newline of ROW, and we must consider NEXT as
26539 END, not END+1. */
26540 || (((!next->reversed_p && g == s)
26541 || (next->reversed_p && g == e - 1))
26542 && (g->charpos == end_charpos
26543 /* Special case for when NEXT is an
26544 empty line at ZV. */
26545 || (g->charpos == -1
26546 && !row->ends_at_zv_p
26547 && next_start == end_charpos)))))
26548 /* A glyph that comes from DISP_STRING is by
26549 definition to be highlighted. */
26550 || EQ (g->object, disp_string))
26551 break;
26552 g++;
26553 }
26554 if (g == e)
26555 {
26556 *end = row;
26557 break;
26558 }
26559 /* The first row that ends at ZV must be the last to be
26560 highlighted. */
26561 else if (next->ends_at_zv_p)
26562 {
26563 *end = next;
26564 break;
26565 }
26566 }
26567 }
26568 }
26569
26570 /* This function sets the mouse_face_* elements of HLINFO, assuming
26571 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26572 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26573 for the overlay or run of text properties specifying the mouse
26574 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26575 before-string and after-string that must also be highlighted.
26576 DISP_STRING, if non-nil, is a display string that may cover some
26577 or all of the highlighted text. */
26578
26579 static void
26580 mouse_face_from_buffer_pos (Lisp_Object window,
26581 Mouse_HLInfo *hlinfo,
26582 ptrdiff_t mouse_charpos,
26583 ptrdiff_t start_charpos,
26584 ptrdiff_t end_charpos,
26585 Lisp_Object before_string,
26586 Lisp_Object after_string,
26587 Lisp_Object disp_string)
26588 {
26589 struct window *w = XWINDOW (window);
26590 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26591 struct glyph_row *r1, *r2;
26592 struct glyph *glyph, *end;
26593 ptrdiff_t ignore, pos;
26594 int x;
26595
26596 eassert (NILP (disp_string) || STRINGP (disp_string));
26597 eassert (NILP (before_string) || STRINGP (before_string));
26598 eassert (NILP (after_string) || STRINGP (after_string));
26599
26600 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26601 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26602 if (r1 == NULL)
26603 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26604 /* If the before-string or display-string contains newlines,
26605 rows_from_pos_range skips to its last row. Move back. */
26606 if (!NILP (before_string) || !NILP (disp_string))
26607 {
26608 struct glyph_row *prev;
26609 while ((prev = r1 - 1, prev >= first)
26610 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26611 && prev->used[TEXT_AREA] > 0)
26612 {
26613 struct glyph *beg = prev->glyphs[TEXT_AREA];
26614 glyph = beg + prev->used[TEXT_AREA];
26615 while (--glyph >= beg && INTEGERP (glyph->object));
26616 if (glyph < beg
26617 || !(EQ (glyph->object, before_string)
26618 || EQ (glyph->object, disp_string)))
26619 break;
26620 r1 = prev;
26621 }
26622 }
26623 if (r2 == NULL)
26624 {
26625 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26626 hlinfo->mouse_face_past_end = 1;
26627 }
26628 else if (!NILP (after_string))
26629 {
26630 /* If the after-string has newlines, advance to its last row. */
26631 struct glyph_row *next;
26632 struct glyph_row *last
26633 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26634
26635 for (next = r2 + 1;
26636 next <= last
26637 && next->used[TEXT_AREA] > 0
26638 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26639 ++next)
26640 r2 = next;
26641 }
26642 /* The rest of the display engine assumes that mouse_face_beg_row is
26643 either above mouse_face_end_row or identical to it. But with
26644 bidi-reordered continued lines, the row for START_CHARPOS could
26645 be below the row for END_CHARPOS. If so, swap the rows and store
26646 them in correct order. */
26647 if (r1->y > r2->y)
26648 {
26649 struct glyph_row *tem = r2;
26650
26651 r2 = r1;
26652 r1 = tem;
26653 }
26654
26655 hlinfo->mouse_face_beg_y = r1->y;
26656 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26657 hlinfo->mouse_face_end_y = r2->y;
26658 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26659
26660 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26661 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26662 could be anywhere in the row and in any order. The strategy
26663 below is to find the leftmost and the rightmost glyph that
26664 belongs to either of these 3 strings, or whose position is
26665 between START_CHARPOS and END_CHARPOS, and highlight all the
26666 glyphs between those two. This may cover more than just the text
26667 between START_CHARPOS and END_CHARPOS if the range of characters
26668 strides the bidi level boundary, e.g. if the beginning is in R2L
26669 text while the end is in L2R text or vice versa. */
26670 if (!r1->reversed_p)
26671 {
26672 /* This row is in a left to right paragraph. Scan it left to
26673 right. */
26674 glyph = r1->glyphs[TEXT_AREA];
26675 end = glyph + r1->used[TEXT_AREA];
26676 x = r1->x;
26677
26678 /* Skip truncation glyphs at the start of the glyph row. */
26679 if (r1->displays_text_p)
26680 for (; glyph < end
26681 && INTEGERP (glyph->object)
26682 && glyph->charpos < 0;
26683 ++glyph)
26684 x += glyph->pixel_width;
26685
26686 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26687 or DISP_STRING, and the first glyph from buffer whose
26688 position is between START_CHARPOS and END_CHARPOS. */
26689 for (; glyph < end
26690 && !INTEGERP (glyph->object)
26691 && !EQ (glyph->object, disp_string)
26692 && !(BUFFERP (glyph->object)
26693 && (glyph->charpos >= start_charpos
26694 && glyph->charpos < end_charpos));
26695 ++glyph)
26696 {
26697 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26698 are present at buffer positions between START_CHARPOS and
26699 END_CHARPOS, or if they come from an overlay. */
26700 if (EQ (glyph->object, before_string))
26701 {
26702 pos = string_buffer_position (before_string,
26703 start_charpos);
26704 /* If pos == 0, it means before_string came from an
26705 overlay, not from a buffer position. */
26706 if (!pos || (pos >= start_charpos && pos < end_charpos))
26707 break;
26708 }
26709 else if (EQ (glyph->object, after_string))
26710 {
26711 pos = string_buffer_position (after_string, end_charpos);
26712 if (!pos || (pos >= start_charpos && pos < end_charpos))
26713 break;
26714 }
26715 x += glyph->pixel_width;
26716 }
26717 hlinfo->mouse_face_beg_x = x;
26718 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26719 }
26720 else
26721 {
26722 /* This row is in a right to left paragraph. Scan it right to
26723 left. */
26724 struct glyph *g;
26725
26726 end = r1->glyphs[TEXT_AREA] - 1;
26727 glyph = end + r1->used[TEXT_AREA];
26728
26729 /* Skip truncation glyphs at the start of the glyph row. */
26730 if (r1->displays_text_p)
26731 for (; glyph > end
26732 && INTEGERP (glyph->object)
26733 && glyph->charpos < 0;
26734 --glyph)
26735 ;
26736
26737 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26738 or DISP_STRING, and the first glyph from buffer whose
26739 position is between START_CHARPOS and END_CHARPOS. */
26740 for (; glyph > end
26741 && !INTEGERP (glyph->object)
26742 && !EQ (glyph->object, disp_string)
26743 && !(BUFFERP (glyph->object)
26744 && (glyph->charpos >= start_charpos
26745 && glyph->charpos < end_charpos));
26746 --glyph)
26747 {
26748 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26749 are present at buffer positions between START_CHARPOS and
26750 END_CHARPOS, or if they come from an overlay. */
26751 if (EQ (glyph->object, before_string))
26752 {
26753 pos = string_buffer_position (before_string, start_charpos);
26754 /* If pos == 0, it means before_string came from an
26755 overlay, not from a buffer position. */
26756 if (!pos || (pos >= start_charpos && pos < end_charpos))
26757 break;
26758 }
26759 else if (EQ (glyph->object, after_string))
26760 {
26761 pos = string_buffer_position (after_string, end_charpos);
26762 if (!pos || (pos >= start_charpos && pos < end_charpos))
26763 break;
26764 }
26765 }
26766
26767 glyph++; /* first glyph to the right of the highlighted area */
26768 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26769 x += g->pixel_width;
26770 hlinfo->mouse_face_beg_x = x;
26771 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26772 }
26773
26774 /* If the highlight ends in a different row, compute GLYPH and END
26775 for the end row. Otherwise, reuse the values computed above for
26776 the row where the highlight begins. */
26777 if (r2 != r1)
26778 {
26779 if (!r2->reversed_p)
26780 {
26781 glyph = r2->glyphs[TEXT_AREA];
26782 end = glyph + r2->used[TEXT_AREA];
26783 x = r2->x;
26784 }
26785 else
26786 {
26787 end = r2->glyphs[TEXT_AREA] - 1;
26788 glyph = end + r2->used[TEXT_AREA];
26789 }
26790 }
26791
26792 if (!r2->reversed_p)
26793 {
26794 /* Skip truncation and continuation glyphs near the end of the
26795 row, and also blanks and stretch glyphs inserted by
26796 extend_face_to_end_of_line. */
26797 while (end > glyph
26798 && INTEGERP ((end - 1)->object))
26799 --end;
26800 /* Scan the rest of the glyph row from the end, looking for the
26801 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26802 DISP_STRING, or whose position is between START_CHARPOS
26803 and END_CHARPOS */
26804 for (--end;
26805 end > glyph
26806 && !INTEGERP (end->object)
26807 && !EQ (end->object, disp_string)
26808 && !(BUFFERP (end->object)
26809 && (end->charpos >= start_charpos
26810 && end->charpos < end_charpos));
26811 --end)
26812 {
26813 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26814 are present at buffer positions between START_CHARPOS and
26815 END_CHARPOS, or if they come from an overlay. */
26816 if (EQ (end->object, before_string))
26817 {
26818 pos = string_buffer_position (before_string, start_charpos);
26819 if (!pos || (pos >= start_charpos && pos < end_charpos))
26820 break;
26821 }
26822 else if (EQ (end->object, after_string))
26823 {
26824 pos = string_buffer_position (after_string, end_charpos);
26825 if (!pos || (pos >= start_charpos && pos < end_charpos))
26826 break;
26827 }
26828 }
26829 /* Find the X coordinate of the last glyph to be highlighted. */
26830 for (; glyph <= end; ++glyph)
26831 x += glyph->pixel_width;
26832
26833 hlinfo->mouse_face_end_x = x;
26834 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26835 }
26836 else
26837 {
26838 /* Skip truncation and continuation glyphs near the end of the
26839 row, and also blanks and stretch glyphs inserted by
26840 extend_face_to_end_of_line. */
26841 x = r2->x;
26842 end++;
26843 while (end < glyph
26844 && INTEGERP (end->object))
26845 {
26846 x += end->pixel_width;
26847 ++end;
26848 }
26849 /* Scan the rest of the glyph row from the end, looking for the
26850 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26851 DISP_STRING, or whose position is between START_CHARPOS
26852 and END_CHARPOS */
26853 for ( ;
26854 end < glyph
26855 && !INTEGERP (end->object)
26856 && !EQ (end->object, disp_string)
26857 && !(BUFFERP (end->object)
26858 && (end->charpos >= start_charpos
26859 && end->charpos < end_charpos));
26860 ++end)
26861 {
26862 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26863 are present at buffer positions between START_CHARPOS and
26864 END_CHARPOS, or if they come from an overlay. */
26865 if (EQ (end->object, before_string))
26866 {
26867 pos = string_buffer_position (before_string, start_charpos);
26868 if (!pos || (pos >= start_charpos && pos < end_charpos))
26869 break;
26870 }
26871 else if (EQ (end->object, after_string))
26872 {
26873 pos = string_buffer_position (after_string, end_charpos);
26874 if (!pos || (pos >= start_charpos && pos < end_charpos))
26875 break;
26876 }
26877 x += end->pixel_width;
26878 }
26879 /* If we exited the above loop because we arrived at the last
26880 glyph of the row, and its buffer position is still not in
26881 range, it means the last character in range is the preceding
26882 newline. Bump the end column and x values to get past the
26883 last glyph. */
26884 if (end == glyph
26885 && BUFFERP (end->object)
26886 && (end->charpos < start_charpos
26887 || end->charpos >= end_charpos))
26888 {
26889 x += end->pixel_width;
26890 ++end;
26891 }
26892 hlinfo->mouse_face_end_x = x;
26893 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26894 }
26895
26896 hlinfo->mouse_face_window = window;
26897 hlinfo->mouse_face_face_id
26898 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26899 mouse_charpos + 1,
26900 !hlinfo->mouse_face_hidden, -1);
26901 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26902 }
26903
26904 /* The following function is not used anymore (replaced with
26905 mouse_face_from_string_pos), but I leave it here for the time
26906 being, in case someone would. */
26907
26908 #if 0 /* not used */
26909
26910 /* Find the position of the glyph for position POS in OBJECT in
26911 window W's current matrix, and return in *X, *Y the pixel
26912 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26913
26914 RIGHT_P non-zero means return the position of the right edge of the
26915 glyph, RIGHT_P zero means return the left edge position.
26916
26917 If no glyph for POS exists in the matrix, return the position of
26918 the glyph with the next smaller position that is in the matrix, if
26919 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26920 exists in the matrix, return the position of the glyph with the
26921 next larger position in OBJECT.
26922
26923 Value is non-zero if a glyph was found. */
26924
26925 static int
26926 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26927 int *hpos, int *vpos, int *x, int *y, int right_p)
26928 {
26929 int yb = window_text_bottom_y (w);
26930 struct glyph_row *r;
26931 struct glyph *best_glyph = NULL;
26932 struct glyph_row *best_row = NULL;
26933 int best_x = 0;
26934
26935 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26936 r->enabled_p && r->y < yb;
26937 ++r)
26938 {
26939 struct glyph *g = r->glyphs[TEXT_AREA];
26940 struct glyph *e = g + r->used[TEXT_AREA];
26941 int gx;
26942
26943 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26944 if (EQ (g->object, object))
26945 {
26946 if (g->charpos == pos)
26947 {
26948 best_glyph = g;
26949 best_x = gx;
26950 best_row = r;
26951 goto found;
26952 }
26953 else if (best_glyph == NULL
26954 || ((eabs (g->charpos - pos)
26955 < eabs (best_glyph->charpos - pos))
26956 && (right_p
26957 ? g->charpos < pos
26958 : g->charpos > pos)))
26959 {
26960 best_glyph = g;
26961 best_x = gx;
26962 best_row = r;
26963 }
26964 }
26965 }
26966
26967 found:
26968
26969 if (best_glyph)
26970 {
26971 *x = best_x;
26972 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26973
26974 if (right_p)
26975 {
26976 *x += best_glyph->pixel_width;
26977 ++*hpos;
26978 }
26979
26980 *y = best_row->y;
26981 *vpos = best_row - w->current_matrix->rows;
26982 }
26983
26984 return best_glyph != NULL;
26985 }
26986 #endif /* not used */
26987
26988 /* Find the positions of the first and the last glyphs in window W's
26989 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26990 (assumed to be a string), and return in HLINFO's mouse_face_*
26991 members the pixel and column/row coordinates of those glyphs. */
26992
26993 static void
26994 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26995 Lisp_Object object,
26996 ptrdiff_t startpos, ptrdiff_t endpos)
26997 {
26998 int yb = window_text_bottom_y (w);
26999 struct glyph_row *r;
27000 struct glyph *g, *e;
27001 int gx;
27002 int found = 0;
27003
27004 /* Find the glyph row with at least one position in the range
27005 [STARTPOS..ENDPOS], and the first glyph in that row whose
27006 position belongs to that range. */
27007 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27008 r->enabled_p && r->y < yb;
27009 ++r)
27010 {
27011 if (!r->reversed_p)
27012 {
27013 g = r->glyphs[TEXT_AREA];
27014 e = g + r->used[TEXT_AREA];
27015 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27016 if (EQ (g->object, object)
27017 && startpos <= g->charpos && g->charpos <= endpos)
27018 {
27019 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27020 hlinfo->mouse_face_beg_y = r->y;
27021 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27022 hlinfo->mouse_face_beg_x = gx;
27023 found = 1;
27024 break;
27025 }
27026 }
27027 else
27028 {
27029 struct glyph *g1;
27030
27031 e = r->glyphs[TEXT_AREA];
27032 g = e + r->used[TEXT_AREA];
27033 for ( ; g > e; --g)
27034 if (EQ ((g-1)->object, object)
27035 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27036 {
27037 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27038 hlinfo->mouse_face_beg_y = r->y;
27039 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27040 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27041 gx += g1->pixel_width;
27042 hlinfo->mouse_face_beg_x = gx;
27043 found = 1;
27044 break;
27045 }
27046 }
27047 if (found)
27048 break;
27049 }
27050
27051 if (!found)
27052 return;
27053
27054 /* Starting with the next row, look for the first row which does NOT
27055 include any glyphs whose positions are in the range. */
27056 for (++r; r->enabled_p && r->y < yb; ++r)
27057 {
27058 g = r->glyphs[TEXT_AREA];
27059 e = g + r->used[TEXT_AREA];
27060 found = 0;
27061 for ( ; g < e; ++g)
27062 if (EQ (g->object, object)
27063 && startpos <= g->charpos && g->charpos <= endpos)
27064 {
27065 found = 1;
27066 break;
27067 }
27068 if (!found)
27069 break;
27070 }
27071
27072 /* The highlighted region ends on the previous row. */
27073 r--;
27074
27075 /* Set the end row and its vertical pixel coordinate. */
27076 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27077 hlinfo->mouse_face_end_y = r->y;
27078
27079 /* Compute and set the end column and the end column's horizontal
27080 pixel coordinate. */
27081 if (!r->reversed_p)
27082 {
27083 g = r->glyphs[TEXT_AREA];
27084 e = g + r->used[TEXT_AREA];
27085 for ( ; e > g; --e)
27086 if (EQ ((e-1)->object, object)
27087 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27088 break;
27089 hlinfo->mouse_face_end_col = e - g;
27090
27091 for (gx = r->x; g < e; ++g)
27092 gx += g->pixel_width;
27093 hlinfo->mouse_face_end_x = gx;
27094 }
27095 else
27096 {
27097 e = r->glyphs[TEXT_AREA];
27098 g = e + r->used[TEXT_AREA];
27099 for (gx = r->x ; e < g; ++e)
27100 {
27101 if (EQ (e->object, object)
27102 && startpos <= e->charpos && e->charpos <= endpos)
27103 break;
27104 gx += e->pixel_width;
27105 }
27106 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27107 hlinfo->mouse_face_end_x = gx;
27108 }
27109 }
27110
27111 #ifdef HAVE_WINDOW_SYSTEM
27112
27113 /* See if position X, Y is within a hot-spot of an image. */
27114
27115 static int
27116 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27117 {
27118 if (!CONSP (hot_spot))
27119 return 0;
27120
27121 if (EQ (XCAR (hot_spot), Qrect))
27122 {
27123 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27124 Lisp_Object rect = XCDR (hot_spot);
27125 Lisp_Object tem;
27126 if (!CONSP (rect))
27127 return 0;
27128 if (!CONSP (XCAR (rect)))
27129 return 0;
27130 if (!CONSP (XCDR (rect)))
27131 return 0;
27132 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27133 return 0;
27134 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27135 return 0;
27136 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27137 return 0;
27138 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27139 return 0;
27140 return 1;
27141 }
27142 else if (EQ (XCAR (hot_spot), Qcircle))
27143 {
27144 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27145 Lisp_Object circ = XCDR (hot_spot);
27146 Lisp_Object lr, lx0, ly0;
27147 if (CONSP (circ)
27148 && CONSP (XCAR (circ))
27149 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27150 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27151 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27152 {
27153 double r = XFLOATINT (lr);
27154 double dx = XINT (lx0) - x;
27155 double dy = XINT (ly0) - y;
27156 return (dx * dx + dy * dy <= r * r);
27157 }
27158 }
27159 else if (EQ (XCAR (hot_spot), Qpoly))
27160 {
27161 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27162 if (VECTORP (XCDR (hot_spot)))
27163 {
27164 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27165 Lisp_Object *poly = v->contents;
27166 ptrdiff_t n = v->header.size;
27167 ptrdiff_t i;
27168 int inside = 0;
27169 Lisp_Object lx, ly;
27170 int x0, y0;
27171
27172 /* Need an even number of coordinates, and at least 3 edges. */
27173 if (n < 6 || n & 1)
27174 return 0;
27175
27176 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27177 If count is odd, we are inside polygon. Pixels on edges
27178 may or may not be included depending on actual geometry of the
27179 polygon. */
27180 if ((lx = poly[n-2], !INTEGERP (lx))
27181 || (ly = poly[n-1], !INTEGERP (lx)))
27182 return 0;
27183 x0 = XINT (lx), y0 = XINT (ly);
27184 for (i = 0; i < n; i += 2)
27185 {
27186 int x1 = x0, y1 = y0;
27187 if ((lx = poly[i], !INTEGERP (lx))
27188 || (ly = poly[i+1], !INTEGERP (ly)))
27189 return 0;
27190 x0 = XINT (lx), y0 = XINT (ly);
27191
27192 /* Does this segment cross the X line? */
27193 if (x0 >= x)
27194 {
27195 if (x1 >= x)
27196 continue;
27197 }
27198 else if (x1 < x)
27199 continue;
27200 if (y > y0 && y > y1)
27201 continue;
27202 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27203 inside = !inside;
27204 }
27205 return inside;
27206 }
27207 }
27208 return 0;
27209 }
27210
27211 Lisp_Object
27212 find_hot_spot (Lisp_Object map, int x, int y)
27213 {
27214 while (CONSP (map))
27215 {
27216 if (CONSP (XCAR (map))
27217 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27218 return XCAR (map);
27219 map = XCDR (map);
27220 }
27221
27222 return Qnil;
27223 }
27224
27225 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27226 3, 3, 0,
27227 doc: /* Lookup in image map MAP coordinates X and Y.
27228 An image map is an alist where each element has the format (AREA ID PLIST).
27229 An AREA is specified as either a rectangle, a circle, or a polygon:
27230 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27231 pixel coordinates of the upper left and bottom right corners.
27232 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27233 and the radius of the circle; r may be a float or integer.
27234 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27235 vector describes one corner in the polygon.
27236 Returns the alist element for the first matching AREA in MAP. */)
27237 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27238 {
27239 if (NILP (map))
27240 return Qnil;
27241
27242 CHECK_NUMBER (x);
27243 CHECK_NUMBER (y);
27244
27245 return find_hot_spot (map,
27246 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27247 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27248 }
27249
27250
27251 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27252 static void
27253 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27254 {
27255 /* Do not change cursor shape while dragging mouse. */
27256 if (!NILP (do_mouse_tracking))
27257 return;
27258
27259 if (!NILP (pointer))
27260 {
27261 if (EQ (pointer, Qarrow))
27262 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27263 else if (EQ (pointer, Qhand))
27264 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27265 else if (EQ (pointer, Qtext))
27266 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27267 else if (EQ (pointer, intern ("hdrag")))
27268 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27269 #ifdef HAVE_X_WINDOWS
27270 else if (EQ (pointer, intern ("vdrag")))
27271 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27272 #endif
27273 else if (EQ (pointer, intern ("hourglass")))
27274 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27275 else if (EQ (pointer, Qmodeline))
27276 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27277 else
27278 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27279 }
27280
27281 if (cursor != No_Cursor)
27282 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27283 }
27284
27285 #endif /* HAVE_WINDOW_SYSTEM */
27286
27287 /* Take proper action when mouse has moved to the mode or header line
27288 or marginal area AREA of window W, x-position X and y-position Y.
27289 X is relative to the start of the text display area of W, so the
27290 width of bitmap areas and scroll bars must be subtracted to get a
27291 position relative to the start of the mode line. */
27292
27293 static void
27294 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27295 enum window_part area)
27296 {
27297 struct window *w = XWINDOW (window);
27298 struct frame *f = XFRAME (w->frame);
27299 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27300 #ifdef HAVE_WINDOW_SYSTEM
27301 Display_Info *dpyinfo;
27302 #endif
27303 Cursor cursor = No_Cursor;
27304 Lisp_Object pointer = Qnil;
27305 int dx, dy, width, height;
27306 ptrdiff_t charpos;
27307 Lisp_Object string, object = Qnil;
27308 Lisp_Object pos IF_LINT (= Qnil), help;
27309
27310 Lisp_Object mouse_face;
27311 int original_x_pixel = x;
27312 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27313 struct glyph_row *row IF_LINT (= 0);
27314
27315 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27316 {
27317 int x0;
27318 struct glyph *end;
27319
27320 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27321 returns them in row/column units! */
27322 string = mode_line_string (w, area, &x, &y, &charpos,
27323 &object, &dx, &dy, &width, &height);
27324
27325 row = (area == ON_MODE_LINE
27326 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27327 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27328
27329 /* Find the glyph under the mouse pointer. */
27330 if (row->mode_line_p && row->enabled_p)
27331 {
27332 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27333 end = glyph + row->used[TEXT_AREA];
27334
27335 for (x0 = original_x_pixel;
27336 glyph < end && x0 >= glyph->pixel_width;
27337 ++glyph)
27338 x0 -= glyph->pixel_width;
27339
27340 if (glyph >= end)
27341 glyph = NULL;
27342 }
27343 }
27344 else
27345 {
27346 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27347 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27348 returns them in row/column units! */
27349 string = marginal_area_string (w, area, &x, &y, &charpos,
27350 &object, &dx, &dy, &width, &height);
27351 }
27352
27353 help = Qnil;
27354
27355 #ifdef HAVE_WINDOW_SYSTEM
27356 if (IMAGEP (object))
27357 {
27358 Lisp_Object image_map, hotspot;
27359 if ((image_map = Fplist_get (XCDR (object), QCmap),
27360 !NILP (image_map))
27361 && (hotspot = find_hot_spot (image_map, dx, dy),
27362 CONSP (hotspot))
27363 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27364 {
27365 Lisp_Object plist;
27366
27367 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27368 If so, we could look for mouse-enter, mouse-leave
27369 properties in PLIST (and do something...). */
27370 hotspot = XCDR (hotspot);
27371 if (CONSP (hotspot)
27372 && (plist = XCAR (hotspot), CONSP (plist)))
27373 {
27374 pointer = Fplist_get (plist, Qpointer);
27375 if (NILP (pointer))
27376 pointer = Qhand;
27377 help = Fplist_get (plist, Qhelp_echo);
27378 if (!NILP (help))
27379 {
27380 help_echo_string = help;
27381 XSETWINDOW (help_echo_window, w);
27382 help_echo_object = w->buffer;
27383 help_echo_pos = charpos;
27384 }
27385 }
27386 }
27387 if (NILP (pointer))
27388 pointer = Fplist_get (XCDR (object), QCpointer);
27389 }
27390 #endif /* HAVE_WINDOW_SYSTEM */
27391
27392 if (STRINGP (string))
27393 pos = make_number (charpos);
27394
27395 /* Set the help text and mouse pointer. If the mouse is on a part
27396 of the mode line without any text (e.g. past the right edge of
27397 the mode line text), use the default help text and pointer. */
27398 if (STRINGP (string) || area == ON_MODE_LINE)
27399 {
27400 /* Arrange to display the help by setting the global variables
27401 help_echo_string, help_echo_object, and help_echo_pos. */
27402 if (NILP (help))
27403 {
27404 if (STRINGP (string))
27405 help = Fget_text_property (pos, Qhelp_echo, string);
27406
27407 if (!NILP (help))
27408 {
27409 help_echo_string = help;
27410 XSETWINDOW (help_echo_window, w);
27411 help_echo_object = string;
27412 help_echo_pos = charpos;
27413 }
27414 else if (area == ON_MODE_LINE)
27415 {
27416 Lisp_Object default_help
27417 = buffer_local_value_1 (Qmode_line_default_help_echo,
27418 w->buffer);
27419
27420 if (STRINGP (default_help))
27421 {
27422 help_echo_string = default_help;
27423 XSETWINDOW (help_echo_window, w);
27424 help_echo_object = Qnil;
27425 help_echo_pos = -1;
27426 }
27427 }
27428 }
27429
27430 #ifdef HAVE_WINDOW_SYSTEM
27431 /* Change the mouse pointer according to what is under it. */
27432 if (FRAME_WINDOW_P (f))
27433 {
27434 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27435 if (STRINGP (string))
27436 {
27437 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27438
27439 if (NILP (pointer))
27440 pointer = Fget_text_property (pos, Qpointer, string);
27441
27442 /* Change the mouse pointer according to what is under X/Y. */
27443 if (NILP (pointer)
27444 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27445 {
27446 Lisp_Object map;
27447 map = Fget_text_property (pos, Qlocal_map, string);
27448 if (!KEYMAPP (map))
27449 map = Fget_text_property (pos, Qkeymap, string);
27450 if (!KEYMAPP (map))
27451 cursor = dpyinfo->vertical_scroll_bar_cursor;
27452 }
27453 }
27454 else
27455 /* Default mode-line pointer. */
27456 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27457 }
27458 #endif
27459 }
27460
27461 /* Change the mouse face according to what is under X/Y. */
27462 if (STRINGP (string))
27463 {
27464 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27465 if (!NILP (mouse_face)
27466 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27467 && glyph)
27468 {
27469 Lisp_Object b, e;
27470
27471 struct glyph * tmp_glyph;
27472
27473 int gpos;
27474 int gseq_length;
27475 int total_pixel_width;
27476 ptrdiff_t begpos, endpos, ignore;
27477
27478 int vpos, hpos;
27479
27480 b = Fprevious_single_property_change (make_number (charpos + 1),
27481 Qmouse_face, string, Qnil);
27482 if (NILP (b))
27483 begpos = 0;
27484 else
27485 begpos = XINT (b);
27486
27487 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27488 if (NILP (e))
27489 endpos = SCHARS (string);
27490 else
27491 endpos = XINT (e);
27492
27493 /* Calculate the glyph position GPOS of GLYPH in the
27494 displayed string, relative to the beginning of the
27495 highlighted part of the string.
27496
27497 Note: GPOS is different from CHARPOS. CHARPOS is the
27498 position of GLYPH in the internal string object. A mode
27499 line string format has structures which are converted to
27500 a flattened string by the Emacs Lisp interpreter. The
27501 internal string is an element of those structures. The
27502 displayed string is the flattened string. */
27503 tmp_glyph = row_start_glyph;
27504 while (tmp_glyph < glyph
27505 && (!(EQ (tmp_glyph->object, glyph->object)
27506 && begpos <= tmp_glyph->charpos
27507 && tmp_glyph->charpos < endpos)))
27508 tmp_glyph++;
27509 gpos = glyph - tmp_glyph;
27510
27511 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27512 the highlighted part of the displayed string to which
27513 GLYPH belongs. Note: GSEQ_LENGTH is different from
27514 SCHARS (STRING), because the latter returns the length of
27515 the internal string. */
27516 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27517 tmp_glyph > glyph
27518 && (!(EQ (tmp_glyph->object, glyph->object)
27519 && begpos <= tmp_glyph->charpos
27520 && tmp_glyph->charpos < endpos));
27521 tmp_glyph--)
27522 ;
27523 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27524
27525 /* Calculate the total pixel width of all the glyphs between
27526 the beginning of the highlighted area and GLYPH. */
27527 total_pixel_width = 0;
27528 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27529 total_pixel_width += tmp_glyph->pixel_width;
27530
27531 /* Pre calculation of re-rendering position. Note: X is in
27532 column units here, after the call to mode_line_string or
27533 marginal_area_string. */
27534 hpos = x - gpos;
27535 vpos = (area == ON_MODE_LINE
27536 ? (w->current_matrix)->nrows - 1
27537 : 0);
27538
27539 /* If GLYPH's position is included in the region that is
27540 already drawn in mouse face, we have nothing to do. */
27541 if ( EQ (window, hlinfo->mouse_face_window)
27542 && (!row->reversed_p
27543 ? (hlinfo->mouse_face_beg_col <= hpos
27544 && hpos < hlinfo->mouse_face_end_col)
27545 /* In R2L rows we swap BEG and END, see below. */
27546 : (hlinfo->mouse_face_end_col <= hpos
27547 && hpos < hlinfo->mouse_face_beg_col))
27548 && hlinfo->mouse_face_beg_row == vpos )
27549 return;
27550
27551 if (clear_mouse_face (hlinfo))
27552 cursor = No_Cursor;
27553
27554 if (!row->reversed_p)
27555 {
27556 hlinfo->mouse_face_beg_col = hpos;
27557 hlinfo->mouse_face_beg_x = original_x_pixel
27558 - (total_pixel_width + dx);
27559 hlinfo->mouse_face_end_col = hpos + gseq_length;
27560 hlinfo->mouse_face_end_x = 0;
27561 }
27562 else
27563 {
27564 /* In R2L rows, show_mouse_face expects BEG and END
27565 coordinates to be swapped. */
27566 hlinfo->mouse_face_end_col = hpos;
27567 hlinfo->mouse_face_end_x = original_x_pixel
27568 - (total_pixel_width + dx);
27569 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27570 hlinfo->mouse_face_beg_x = 0;
27571 }
27572
27573 hlinfo->mouse_face_beg_row = vpos;
27574 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27575 hlinfo->mouse_face_beg_y = 0;
27576 hlinfo->mouse_face_end_y = 0;
27577 hlinfo->mouse_face_past_end = 0;
27578 hlinfo->mouse_face_window = window;
27579
27580 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27581 charpos,
27582 0, 0, 0,
27583 &ignore,
27584 glyph->face_id,
27585 1);
27586 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27587
27588 if (NILP (pointer))
27589 pointer = Qhand;
27590 }
27591 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27592 clear_mouse_face (hlinfo);
27593 }
27594 #ifdef HAVE_WINDOW_SYSTEM
27595 if (FRAME_WINDOW_P (f))
27596 define_frame_cursor1 (f, cursor, pointer);
27597 #endif
27598 }
27599
27600
27601 /* EXPORT:
27602 Take proper action when the mouse has moved to position X, Y on
27603 frame F as regards highlighting characters that have mouse-face
27604 properties. Also de-highlighting chars where the mouse was before.
27605 X and Y can be negative or out of range. */
27606
27607 void
27608 note_mouse_highlight (struct frame *f, int x, int y)
27609 {
27610 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27611 enum window_part part = ON_NOTHING;
27612 Lisp_Object window;
27613 struct window *w;
27614 Cursor cursor = No_Cursor;
27615 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27616 struct buffer *b;
27617
27618 /* When a menu is active, don't highlight because this looks odd. */
27619 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27620 if (popup_activated ())
27621 return;
27622 #endif
27623
27624 if (NILP (Vmouse_highlight)
27625 || !f->glyphs_initialized_p
27626 || f->pointer_invisible)
27627 return;
27628
27629 hlinfo->mouse_face_mouse_x = x;
27630 hlinfo->mouse_face_mouse_y = y;
27631 hlinfo->mouse_face_mouse_frame = f;
27632
27633 if (hlinfo->mouse_face_defer)
27634 return;
27635
27636 if (gc_in_progress)
27637 {
27638 hlinfo->mouse_face_deferred_gc = 1;
27639 return;
27640 }
27641
27642 /* Which window is that in? */
27643 window = window_from_coordinates (f, x, y, &part, 1);
27644
27645 /* If displaying active text in another window, clear that. */
27646 if (! EQ (window, hlinfo->mouse_face_window)
27647 /* Also clear if we move out of text area in same window. */
27648 || (!NILP (hlinfo->mouse_face_window)
27649 && !NILP (window)
27650 && part != ON_TEXT
27651 && part != ON_MODE_LINE
27652 && part != ON_HEADER_LINE))
27653 clear_mouse_face (hlinfo);
27654
27655 /* Not on a window -> return. */
27656 if (!WINDOWP (window))
27657 return;
27658
27659 /* Reset help_echo_string. It will get recomputed below. */
27660 help_echo_string = Qnil;
27661
27662 /* Convert to window-relative pixel coordinates. */
27663 w = XWINDOW (window);
27664 frame_to_window_pixel_xy (w, &x, &y);
27665
27666 #ifdef HAVE_WINDOW_SYSTEM
27667 /* Handle tool-bar window differently since it doesn't display a
27668 buffer. */
27669 if (EQ (window, f->tool_bar_window))
27670 {
27671 note_tool_bar_highlight (f, x, y);
27672 return;
27673 }
27674 #endif
27675
27676 /* Mouse is on the mode, header line or margin? */
27677 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27678 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27679 {
27680 note_mode_line_or_margin_highlight (window, x, y, part);
27681 return;
27682 }
27683
27684 #ifdef HAVE_WINDOW_SYSTEM
27685 if (part == ON_VERTICAL_BORDER)
27686 {
27687 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27688 help_echo_string = build_string ("drag-mouse-1: resize");
27689 }
27690 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27691 || part == ON_SCROLL_BAR)
27692 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27693 else
27694 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27695 #endif
27696
27697 /* Are we in a window whose display is up to date?
27698 And verify the buffer's text has not changed. */
27699 b = XBUFFER (w->buffer);
27700 if (part == ON_TEXT
27701 && EQ (w->window_end_valid, w->buffer)
27702 && w->last_modified == BUF_MODIFF (b)
27703 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27704 {
27705 int hpos, vpos, dx, dy, area = LAST_AREA;
27706 ptrdiff_t pos;
27707 struct glyph *glyph;
27708 Lisp_Object object;
27709 Lisp_Object mouse_face = Qnil, position;
27710 Lisp_Object *overlay_vec = NULL;
27711 ptrdiff_t i, noverlays;
27712 struct buffer *obuf;
27713 ptrdiff_t obegv, ozv;
27714 int same_region;
27715
27716 /* Find the glyph under X/Y. */
27717 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27718
27719 #ifdef HAVE_WINDOW_SYSTEM
27720 /* Look for :pointer property on image. */
27721 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27722 {
27723 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27724 if (img != NULL && IMAGEP (img->spec))
27725 {
27726 Lisp_Object image_map, hotspot;
27727 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27728 !NILP (image_map))
27729 && (hotspot = find_hot_spot (image_map,
27730 glyph->slice.img.x + dx,
27731 glyph->slice.img.y + dy),
27732 CONSP (hotspot))
27733 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27734 {
27735 Lisp_Object plist;
27736
27737 /* Could check XCAR (hotspot) to see if we enter/leave
27738 this hot-spot.
27739 If so, we could look for mouse-enter, mouse-leave
27740 properties in PLIST (and do something...). */
27741 hotspot = XCDR (hotspot);
27742 if (CONSP (hotspot)
27743 && (plist = XCAR (hotspot), CONSP (plist)))
27744 {
27745 pointer = Fplist_get (plist, Qpointer);
27746 if (NILP (pointer))
27747 pointer = Qhand;
27748 help_echo_string = Fplist_get (plist, Qhelp_echo);
27749 if (!NILP (help_echo_string))
27750 {
27751 help_echo_window = window;
27752 help_echo_object = glyph->object;
27753 help_echo_pos = glyph->charpos;
27754 }
27755 }
27756 }
27757 if (NILP (pointer))
27758 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27759 }
27760 }
27761 #endif /* HAVE_WINDOW_SYSTEM */
27762
27763 /* Clear mouse face if X/Y not over text. */
27764 if (glyph == NULL
27765 || area != TEXT_AREA
27766 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27767 /* Glyph's OBJECT is an integer for glyphs inserted by the
27768 display engine for its internal purposes, like truncation
27769 and continuation glyphs and blanks beyond the end of
27770 line's text on text terminals. If we are over such a
27771 glyph, we are not over any text. */
27772 || INTEGERP (glyph->object)
27773 /* R2L rows have a stretch glyph at their front, which
27774 stands for no text, whereas L2R rows have no glyphs at
27775 all beyond the end of text. Treat such stretch glyphs
27776 like we do with NULL glyphs in L2R rows. */
27777 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27778 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27779 && glyph->type == STRETCH_GLYPH
27780 && glyph->avoid_cursor_p))
27781 {
27782 if (clear_mouse_face (hlinfo))
27783 cursor = No_Cursor;
27784 #ifdef HAVE_WINDOW_SYSTEM
27785 if (FRAME_WINDOW_P (f) && NILP (pointer))
27786 {
27787 if (area != TEXT_AREA)
27788 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27789 else
27790 pointer = Vvoid_text_area_pointer;
27791 }
27792 #endif
27793 goto set_cursor;
27794 }
27795
27796 pos = glyph->charpos;
27797 object = glyph->object;
27798 if (!STRINGP (object) && !BUFFERP (object))
27799 goto set_cursor;
27800
27801 /* If we get an out-of-range value, return now; avoid an error. */
27802 if (BUFFERP (object) && pos > BUF_Z (b))
27803 goto set_cursor;
27804
27805 /* Make the window's buffer temporarily current for
27806 overlays_at and compute_char_face. */
27807 obuf = current_buffer;
27808 current_buffer = b;
27809 obegv = BEGV;
27810 ozv = ZV;
27811 BEGV = BEG;
27812 ZV = Z;
27813
27814 /* Is this char mouse-active or does it have help-echo? */
27815 position = make_number (pos);
27816
27817 if (BUFFERP (object))
27818 {
27819 /* Put all the overlays we want in a vector in overlay_vec. */
27820 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27821 /* Sort overlays into increasing priority order. */
27822 noverlays = sort_overlays (overlay_vec, noverlays, w);
27823 }
27824 else
27825 noverlays = 0;
27826
27827 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27828
27829 if (same_region)
27830 cursor = No_Cursor;
27831
27832 /* Check mouse-face highlighting. */
27833 if (! same_region
27834 /* If there exists an overlay with mouse-face overlapping
27835 the one we are currently highlighting, we have to
27836 check if we enter the overlapping overlay, and then
27837 highlight only that. */
27838 || (OVERLAYP (hlinfo->mouse_face_overlay)
27839 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27840 {
27841 /* Find the highest priority overlay with a mouse-face. */
27842 Lisp_Object overlay = Qnil;
27843 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27844 {
27845 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27846 if (!NILP (mouse_face))
27847 overlay = overlay_vec[i];
27848 }
27849
27850 /* If we're highlighting the same overlay as before, there's
27851 no need to do that again. */
27852 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27853 goto check_help_echo;
27854 hlinfo->mouse_face_overlay = overlay;
27855
27856 /* Clear the display of the old active region, if any. */
27857 if (clear_mouse_face (hlinfo))
27858 cursor = No_Cursor;
27859
27860 /* If no overlay applies, get a text property. */
27861 if (NILP (overlay))
27862 mouse_face = Fget_text_property (position, Qmouse_face, object);
27863
27864 /* Next, compute the bounds of the mouse highlighting and
27865 display it. */
27866 if (!NILP (mouse_face) && STRINGP (object))
27867 {
27868 /* The mouse-highlighting comes from a display string
27869 with a mouse-face. */
27870 Lisp_Object s, e;
27871 ptrdiff_t ignore;
27872
27873 s = Fprevious_single_property_change
27874 (make_number (pos + 1), Qmouse_face, object, Qnil);
27875 e = Fnext_single_property_change
27876 (position, Qmouse_face, object, Qnil);
27877 if (NILP (s))
27878 s = make_number (0);
27879 if (NILP (e))
27880 e = make_number (SCHARS (object) - 1);
27881 mouse_face_from_string_pos (w, hlinfo, object,
27882 XINT (s), XINT (e));
27883 hlinfo->mouse_face_past_end = 0;
27884 hlinfo->mouse_face_window = window;
27885 hlinfo->mouse_face_face_id
27886 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27887 glyph->face_id, 1);
27888 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27889 cursor = No_Cursor;
27890 }
27891 else
27892 {
27893 /* The mouse-highlighting, if any, comes from an overlay
27894 or text property in the buffer. */
27895 Lisp_Object buffer IF_LINT (= Qnil);
27896 Lisp_Object disp_string IF_LINT (= Qnil);
27897
27898 if (STRINGP (object))
27899 {
27900 /* If we are on a display string with no mouse-face,
27901 check if the text under it has one. */
27902 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27903 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27904 pos = string_buffer_position (object, start);
27905 if (pos > 0)
27906 {
27907 mouse_face = get_char_property_and_overlay
27908 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27909 buffer = w->buffer;
27910 disp_string = object;
27911 }
27912 }
27913 else
27914 {
27915 buffer = object;
27916 disp_string = Qnil;
27917 }
27918
27919 if (!NILP (mouse_face))
27920 {
27921 Lisp_Object before, after;
27922 Lisp_Object before_string, after_string;
27923 /* To correctly find the limits of mouse highlight
27924 in a bidi-reordered buffer, we must not use the
27925 optimization of limiting the search in
27926 previous-single-property-change and
27927 next-single-property-change, because
27928 rows_from_pos_range needs the real start and end
27929 positions to DTRT in this case. That's because
27930 the first row visible in a window does not
27931 necessarily display the character whose position
27932 is the smallest. */
27933 Lisp_Object lim1 =
27934 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27935 ? Fmarker_position (w->start)
27936 : Qnil;
27937 Lisp_Object lim2 =
27938 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27939 ? make_number (BUF_Z (XBUFFER (buffer))
27940 - XFASTINT (w->window_end_pos))
27941 : Qnil;
27942
27943 if (NILP (overlay))
27944 {
27945 /* Handle the text property case. */
27946 before = Fprevious_single_property_change
27947 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27948 after = Fnext_single_property_change
27949 (make_number (pos), Qmouse_face, buffer, lim2);
27950 before_string = after_string = Qnil;
27951 }
27952 else
27953 {
27954 /* Handle the overlay case. */
27955 before = Foverlay_start (overlay);
27956 after = Foverlay_end (overlay);
27957 before_string = Foverlay_get (overlay, Qbefore_string);
27958 after_string = Foverlay_get (overlay, Qafter_string);
27959
27960 if (!STRINGP (before_string)) before_string = Qnil;
27961 if (!STRINGP (after_string)) after_string = Qnil;
27962 }
27963
27964 mouse_face_from_buffer_pos (window, hlinfo, pos,
27965 NILP (before)
27966 ? 1
27967 : XFASTINT (before),
27968 NILP (after)
27969 ? BUF_Z (XBUFFER (buffer))
27970 : XFASTINT (after),
27971 before_string, after_string,
27972 disp_string);
27973 cursor = No_Cursor;
27974 }
27975 }
27976 }
27977
27978 check_help_echo:
27979
27980 /* Look for a `help-echo' property. */
27981 if (NILP (help_echo_string)) {
27982 Lisp_Object help, overlay;
27983
27984 /* Check overlays first. */
27985 help = overlay = Qnil;
27986 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27987 {
27988 overlay = overlay_vec[i];
27989 help = Foverlay_get (overlay, Qhelp_echo);
27990 }
27991
27992 if (!NILP (help))
27993 {
27994 help_echo_string = help;
27995 help_echo_window = window;
27996 help_echo_object = overlay;
27997 help_echo_pos = pos;
27998 }
27999 else
28000 {
28001 Lisp_Object obj = glyph->object;
28002 ptrdiff_t charpos = glyph->charpos;
28003
28004 /* Try text properties. */
28005 if (STRINGP (obj)
28006 && charpos >= 0
28007 && charpos < SCHARS (obj))
28008 {
28009 help = Fget_text_property (make_number (charpos),
28010 Qhelp_echo, obj);
28011 if (NILP (help))
28012 {
28013 /* If the string itself doesn't specify a help-echo,
28014 see if the buffer text ``under'' it does. */
28015 struct glyph_row *r
28016 = MATRIX_ROW (w->current_matrix, vpos);
28017 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28018 ptrdiff_t p = string_buffer_position (obj, start);
28019 if (p > 0)
28020 {
28021 help = Fget_char_property (make_number (p),
28022 Qhelp_echo, w->buffer);
28023 if (!NILP (help))
28024 {
28025 charpos = p;
28026 obj = w->buffer;
28027 }
28028 }
28029 }
28030 }
28031 else if (BUFFERP (obj)
28032 && charpos >= BEGV
28033 && charpos < ZV)
28034 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28035 obj);
28036
28037 if (!NILP (help))
28038 {
28039 help_echo_string = help;
28040 help_echo_window = window;
28041 help_echo_object = obj;
28042 help_echo_pos = charpos;
28043 }
28044 }
28045 }
28046
28047 #ifdef HAVE_WINDOW_SYSTEM
28048 /* Look for a `pointer' property. */
28049 if (FRAME_WINDOW_P (f) && NILP (pointer))
28050 {
28051 /* Check overlays first. */
28052 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28053 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28054
28055 if (NILP (pointer))
28056 {
28057 Lisp_Object obj = glyph->object;
28058 ptrdiff_t charpos = glyph->charpos;
28059
28060 /* Try text properties. */
28061 if (STRINGP (obj)
28062 && charpos >= 0
28063 && charpos < SCHARS (obj))
28064 {
28065 pointer = Fget_text_property (make_number (charpos),
28066 Qpointer, obj);
28067 if (NILP (pointer))
28068 {
28069 /* If the string itself doesn't specify a pointer,
28070 see if the buffer text ``under'' it does. */
28071 struct glyph_row *r
28072 = MATRIX_ROW (w->current_matrix, vpos);
28073 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28074 ptrdiff_t p = string_buffer_position (obj, start);
28075 if (p > 0)
28076 pointer = Fget_char_property (make_number (p),
28077 Qpointer, w->buffer);
28078 }
28079 }
28080 else if (BUFFERP (obj)
28081 && charpos >= BEGV
28082 && charpos < ZV)
28083 pointer = Fget_text_property (make_number (charpos),
28084 Qpointer, obj);
28085 }
28086 }
28087 #endif /* HAVE_WINDOW_SYSTEM */
28088
28089 BEGV = obegv;
28090 ZV = ozv;
28091 current_buffer = obuf;
28092 }
28093
28094 set_cursor:
28095
28096 #ifdef HAVE_WINDOW_SYSTEM
28097 if (FRAME_WINDOW_P (f))
28098 define_frame_cursor1 (f, cursor, pointer);
28099 #else
28100 /* This is here to prevent a compiler error, about "label at end of
28101 compound statement". */
28102 return;
28103 #endif
28104 }
28105
28106
28107 /* EXPORT for RIF:
28108 Clear any mouse-face on window W. This function is part of the
28109 redisplay interface, and is called from try_window_id and similar
28110 functions to ensure the mouse-highlight is off. */
28111
28112 void
28113 x_clear_window_mouse_face (struct window *w)
28114 {
28115 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28116 Lisp_Object window;
28117
28118 BLOCK_INPUT;
28119 XSETWINDOW (window, w);
28120 if (EQ (window, hlinfo->mouse_face_window))
28121 clear_mouse_face (hlinfo);
28122 UNBLOCK_INPUT;
28123 }
28124
28125
28126 /* EXPORT:
28127 Just discard the mouse face information for frame F, if any.
28128 This is used when the size of F is changed. */
28129
28130 void
28131 cancel_mouse_face (struct frame *f)
28132 {
28133 Lisp_Object window;
28134 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28135
28136 window = hlinfo->mouse_face_window;
28137 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28138 {
28139 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28140 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28141 hlinfo->mouse_face_window = Qnil;
28142 }
28143 }
28144
28145
28146 \f
28147 /***********************************************************************
28148 Exposure Events
28149 ***********************************************************************/
28150
28151 #ifdef HAVE_WINDOW_SYSTEM
28152
28153 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28154 which intersects rectangle R. R is in window-relative coordinates. */
28155
28156 static void
28157 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28158 enum glyph_row_area area)
28159 {
28160 struct glyph *first = row->glyphs[area];
28161 struct glyph *end = row->glyphs[area] + row->used[area];
28162 struct glyph *last;
28163 int first_x, start_x, x;
28164
28165 if (area == TEXT_AREA && row->fill_line_p)
28166 /* If row extends face to end of line write the whole line. */
28167 draw_glyphs (w, 0, row, area,
28168 0, row->used[area],
28169 DRAW_NORMAL_TEXT, 0);
28170 else
28171 {
28172 /* Set START_X to the window-relative start position for drawing glyphs of
28173 AREA. The first glyph of the text area can be partially visible.
28174 The first glyphs of other areas cannot. */
28175 start_x = window_box_left_offset (w, area);
28176 x = start_x;
28177 if (area == TEXT_AREA)
28178 x += row->x;
28179
28180 /* Find the first glyph that must be redrawn. */
28181 while (first < end
28182 && x + first->pixel_width < r->x)
28183 {
28184 x += first->pixel_width;
28185 ++first;
28186 }
28187
28188 /* Find the last one. */
28189 last = first;
28190 first_x = x;
28191 while (last < end
28192 && x < r->x + r->width)
28193 {
28194 x += last->pixel_width;
28195 ++last;
28196 }
28197
28198 /* Repaint. */
28199 if (last > first)
28200 draw_glyphs (w, first_x - start_x, row, area,
28201 first - row->glyphs[area], last - row->glyphs[area],
28202 DRAW_NORMAL_TEXT, 0);
28203 }
28204 }
28205
28206
28207 /* Redraw the parts of the glyph row ROW on window W intersecting
28208 rectangle R. R is in window-relative coordinates. Value is
28209 non-zero if mouse-face was overwritten. */
28210
28211 static int
28212 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28213 {
28214 eassert (row->enabled_p);
28215
28216 if (row->mode_line_p || w->pseudo_window_p)
28217 draw_glyphs (w, 0, row, TEXT_AREA,
28218 0, row->used[TEXT_AREA],
28219 DRAW_NORMAL_TEXT, 0);
28220 else
28221 {
28222 if (row->used[LEFT_MARGIN_AREA])
28223 expose_area (w, row, r, LEFT_MARGIN_AREA);
28224 if (row->used[TEXT_AREA])
28225 expose_area (w, row, r, TEXT_AREA);
28226 if (row->used[RIGHT_MARGIN_AREA])
28227 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28228 draw_row_fringe_bitmaps (w, row);
28229 }
28230
28231 return row->mouse_face_p;
28232 }
28233
28234
28235 /* Redraw those parts of glyphs rows during expose event handling that
28236 overlap other rows. Redrawing of an exposed line writes over parts
28237 of lines overlapping that exposed line; this function fixes that.
28238
28239 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28240 row in W's current matrix that is exposed and overlaps other rows.
28241 LAST_OVERLAPPING_ROW is the last such row. */
28242
28243 static void
28244 expose_overlaps (struct window *w,
28245 struct glyph_row *first_overlapping_row,
28246 struct glyph_row *last_overlapping_row,
28247 XRectangle *r)
28248 {
28249 struct glyph_row *row;
28250
28251 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28252 if (row->overlapping_p)
28253 {
28254 eassert (row->enabled_p && !row->mode_line_p);
28255
28256 row->clip = r;
28257 if (row->used[LEFT_MARGIN_AREA])
28258 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28259
28260 if (row->used[TEXT_AREA])
28261 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28262
28263 if (row->used[RIGHT_MARGIN_AREA])
28264 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28265 row->clip = NULL;
28266 }
28267 }
28268
28269
28270 /* Return non-zero if W's cursor intersects rectangle R. */
28271
28272 static int
28273 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28274 {
28275 XRectangle cr, result;
28276 struct glyph *cursor_glyph;
28277 struct glyph_row *row;
28278
28279 if (w->phys_cursor.vpos >= 0
28280 && w->phys_cursor.vpos < w->current_matrix->nrows
28281 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28282 row->enabled_p)
28283 && row->cursor_in_fringe_p)
28284 {
28285 /* Cursor is in the fringe. */
28286 cr.x = window_box_right_offset (w,
28287 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28288 ? RIGHT_MARGIN_AREA
28289 : TEXT_AREA));
28290 cr.y = row->y;
28291 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28292 cr.height = row->height;
28293 return x_intersect_rectangles (&cr, r, &result);
28294 }
28295
28296 cursor_glyph = get_phys_cursor_glyph (w);
28297 if (cursor_glyph)
28298 {
28299 /* r is relative to W's box, but w->phys_cursor.x is relative
28300 to left edge of W's TEXT area. Adjust it. */
28301 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28302 cr.y = w->phys_cursor.y;
28303 cr.width = cursor_glyph->pixel_width;
28304 cr.height = w->phys_cursor_height;
28305 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28306 I assume the effect is the same -- and this is portable. */
28307 return x_intersect_rectangles (&cr, r, &result);
28308 }
28309 /* If we don't understand the format, pretend we're not in the hot-spot. */
28310 return 0;
28311 }
28312
28313
28314 /* EXPORT:
28315 Draw a vertical window border to the right of window W if W doesn't
28316 have vertical scroll bars. */
28317
28318 void
28319 x_draw_vertical_border (struct window *w)
28320 {
28321 struct frame *f = XFRAME (WINDOW_FRAME (w));
28322
28323 /* We could do better, if we knew what type of scroll-bar the adjacent
28324 windows (on either side) have... But we don't :-(
28325 However, I think this works ok. ++KFS 2003-04-25 */
28326
28327 /* Redraw borders between horizontally adjacent windows. Don't
28328 do it for frames with vertical scroll bars because either the
28329 right scroll bar of a window, or the left scroll bar of its
28330 neighbor will suffice as a border. */
28331 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28332 return;
28333
28334 if (!WINDOW_RIGHTMOST_P (w)
28335 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28336 {
28337 int x0, x1, y0, y1;
28338
28339 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28340 y1 -= 1;
28341
28342 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28343 x1 -= 1;
28344
28345 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28346 }
28347 else if (!WINDOW_LEFTMOST_P (w)
28348 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28349 {
28350 int x0, x1, y0, y1;
28351
28352 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28353 y1 -= 1;
28354
28355 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28356 x0 -= 1;
28357
28358 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28359 }
28360 }
28361
28362
28363 /* Redraw the part of window W intersection rectangle FR. Pixel
28364 coordinates in FR are frame-relative. Call this function with
28365 input blocked. Value is non-zero if the exposure overwrites
28366 mouse-face. */
28367
28368 static int
28369 expose_window (struct window *w, XRectangle *fr)
28370 {
28371 struct frame *f = XFRAME (w->frame);
28372 XRectangle wr, r;
28373 int mouse_face_overwritten_p = 0;
28374
28375 /* If window is not yet fully initialized, do nothing. This can
28376 happen when toolkit scroll bars are used and a window is split.
28377 Reconfiguring the scroll bar will generate an expose for a newly
28378 created window. */
28379 if (w->current_matrix == NULL)
28380 return 0;
28381
28382 /* When we're currently updating the window, display and current
28383 matrix usually don't agree. Arrange for a thorough display
28384 later. */
28385 if (w == updated_window)
28386 {
28387 SET_FRAME_GARBAGED (f);
28388 return 0;
28389 }
28390
28391 /* Frame-relative pixel rectangle of W. */
28392 wr.x = WINDOW_LEFT_EDGE_X (w);
28393 wr.y = WINDOW_TOP_EDGE_Y (w);
28394 wr.width = WINDOW_TOTAL_WIDTH (w);
28395 wr.height = WINDOW_TOTAL_HEIGHT (w);
28396
28397 if (x_intersect_rectangles (fr, &wr, &r))
28398 {
28399 int yb = window_text_bottom_y (w);
28400 struct glyph_row *row;
28401 int cursor_cleared_p, phys_cursor_on_p;
28402 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28403
28404 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28405 r.x, r.y, r.width, r.height));
28406
28407 /* Convert to window coordinates. */
28408 r.x -= WINDOW_LEFT_EDGE_X (w);
28409 r.y -= WINDOW_TOP_EDGE_Y (w);
28410
28411 /* Turn off the cursor. */
28412 if (!w->pseudo_window_p
28413 && phys_cursor_in_rect_p (w, &r))
28414 {
28415 x_clear_cursor (w);
28416 cursor_cleared_p = 1;
28417 }
28418 else
28419 cursor_cleared_p = 0;
28420
28421 /* If the row containing the cursor extends face to end of line,
28422 then expose_area might overwrite the cursor outside the
28423 rectangle and thus notice_overwritten_cursor might clear
28424 w->phys_cursor_on_p. We remember the original value and
28425 check later if it is changed. */
28426 phys_cursor_on_p = w->phys_cursor_on_p;
28427
28428 /* Update lines intersecting rectangle R. */
28429 first_overlapping_row = last_overlapping_row = NULL;
28430 for (row = w->current_matrix->rows;
28431 row->enabled_p;
28432 ++row)
28433 {
28434 int y0 = row->y;
28435 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28436
28437 if ((y0 >= r.y && y0 < r.y + r.height)
28438 || (y1 > r.y && y1 < r.y + r.height)
28439 || (r.y >= y0 && r.y < y1)
28440 || (r.y + r.height > y0 && r.y + r.height < y1))
28441 {
28442 /* A header line may be overlapping, but there is no need
28443 to fix overlapping areas for them. KFS 2005-02-12 */
28444 if (row->overlapping_p && !row->mode_line_p)
28445 {
28446 if (first_overlapping_row == NULL)
28447 first_overlapping_row = row;
28448 last_overlapping_row = row;
28449 }
28450
28451 row->clip = fr;
28452 if (expose_line (w, row, &r))
28453 mouse_face_overwritten_p = 1;
28454 row->clip = NULL;
28455 }
28456 else if (row->overlapping_p)
28457 {
28458 /* We must redraw a row overlapping the exposed area. */
28459 if (y0 < r.y
28460 ? y0 + row->phys_height > r.y
28461 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28462 {
28463 if (first_overlapping_row == NULL)
28464 first_overlapping_row = row;
28465 last_overlapping_row = row;
28466 }
28467 }
28468
28469 if (y1 >= yb)
28470 break;
28471 }
28472
28473 /* Display the mode line if there is one. */
28474 if (WINDOW_WANTS_MODELINE_P (w)
28475 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28476 row->enabled_p)
28477 && row->y < r.y + r.height)
28478 {
28479 if (expose_line (w, row, &r))
28480 mouse_face_overwritten_p = 1;
28481 }
28482
28483 if (!w->pseudo_window_p)
28484 {
28485 /* Fix the display of overlapping rows. */
28486 if (first_overlapping_row)
28487 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28488 fr);
28489
28490 /* Draw border between windows. */
28491 x_draw_vertical_border (w);
28492
28493 /* Turn the cursor on again. */
28494 if (cursor_cleared_p
28495 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28496 update_window_cursor (w, 1);
28497 }
28498 }
28499
28500 return mouse_face_overwritten_p;
28501 }
28502
28503
28504
28505 /* Redraw (parts) of all windows in the window tree rooted at W that
28506 intersect R. R contains frame pixel coordinates. Value is
28507 non-zero if the exposure overwrites mouse-face. */
28508
28509 static int
28510 expose_window_tree (struct window *w, XRectangle *r)
28511 {
28512 struct frame *f = XFRAME (w->frame);
28513 int mouse_face_overwritten_p = 0;
28514
28515 while (w && !FRAME_GARBAGED_P (f))
28516 {
28517 if (!NILP (w->hchild))
28518 mouse_face_overwritten_p
28519 |= expose_window_tree (XWINDOW (w->hchild), r);
28520 else if (!NILP (w->vchild))
28521 mouse_face_overwritten_p
28522 |= expose_window_tree (XWINDOW (w->vchild), r);
28523 else
28524 mouse_face_overwritten_p |= expose_window (w, r);
28525
28526 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28527 }
28528
28529 return mouse_face_overwritten_p;
28530 }
28531
28532
28533 /* EXPORT:
28534 Redisplay an exposed area of frame F. X and Y are the upper-left
28535 corner of the exposed rectangle. W and H are width and height of
28536 the exposed area. All are pixel values. W or H zero means redraw
28537 the entire frame. */
28538
28539 void
28540 expose_frame (struct frame *f, int x, int y, int w, int h)
28541 {
28542 XRectangle r;
28543 int mouse_face_overwritten_p = 0;
28544
28545 TRACE ((stderr, "expose_frame "));
28546
28547 /* No need to redraw if frame will be redrawn soon. */
28548 if (FRAME_GARBAGED_P (f))
28549 {
28550 TRACE ((stderr, " garbaged\n"));
28551 return;
28552 }
28553
28554 /* If basic faces haven't been realized yet, there is no point in
28555 trying to redraw anything. This can happen when we get an expose
28556 event while Emacs is starting, e.g. by moving another window. */
28557 if (FRAME_FACE_CACHE (f) == NULL
28558 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28559 {
28560 TRACE ((stderr, " no faces\n"));
28561 return;
28562 }
28563
28564 if (w == 0 || h == 0)
28565 {
28566 r.x = r.y = 0;
28567 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28568 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28569 }
28570 else
28571 {
28572 r.x = x;
28573 r.y = y;
28574 r.width = w;
28575 r.height = h;
28576 }
28577
28578 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28579 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28580
28581 if (WINDOWP (f->tool_bar_window))
28582 mouse_face_overwritten_p
28583 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28584
28585 #ifdef HAVE_X_WINDOWS
28586 #ifndef MSDOS
28587 #ifndef USE_X_TOOLKIT
28588 if (WINDOWP (f->menu_bar_window))
28589 mouse_face_overwritten_p
28590 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28591 #endif /* not USE_X_TOOLKIT */
28592 #endif
28593 #endif
28594
28595 /* Some window managers support a focus-follows-mouse style with
28596 delayed raising of frames. Imagine a partially obscured frame,
28597 and moving the mouse into partially obscured mouse-face on that
28598 frame. The visible part of the mouse-face will be highlighted,
28599 then the WM raises the obscured frame. With at least one WM, KDE
28600 2.1, Emacs is not getting any event for the raising of the frame
28601 (even tried with SubstructureRedirectMask), only Expose events.
28602 These expose events will draw text normally, i.e. not
28603 highlighted. Which means we must redo the highlight here.
28604 Subsume it under ``we love X''. --gerd 2001-08-15 */
28605 /* Included in Windows version because Windows most likely does not
28606 do the right thing if any third party tool offers
28607 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28608 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28609 {
28610 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28611 if (f == hlinfo->mouse_face_mouse_frame)
28612 {
28613 int mouse_x = hlinfo->mouse_face_mouse_x;
28614 int mouse_y = hlinfo->mouse_face_mouse_y;
28615 clear_mouse_face (hlinfo);
28616 note_mouse_highlight (f, mouse_x, mouse_y);
28617 }
28618 }
28619 }
28620
28621
28622 /* EXPORT:
28623 Determine the intersection of two rectangles R1 and R2. Return
28624 the intersection in *RESULT. Value is non-zero if RESULT is not
28625 empty. */
28626
28627 int
28628 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28629 {
28630 XRectangle *left, *right;
28631 XRectangle *upper, *lower;
28632 int intersection_p = 0;
28633
28634 /* Rearrange so that R1 is the left-most rectangle. */
28635 if (r1->x < r2->x)
28636 left = r1, right = r2;
28637 else
28638 left = r2, right = r1;
28639
28640 /* X0 of the intersection is right.x0, if this is inside R1,
28641 otherwise there is no intersection. */
28642 if (right->x <= left->x + left->width)
28643 {
28644 result->x = right->x;
28645
28646 /* The right end of the intersection is the minimum of
28647 the right ends of left and right. */
28648 result->width = (min (left->x + left->width, right->x + right->width)
28649 - result->x);
28650
28651 /* Same game for Y. */
28652 if (r1->y < r2->y)
28653 upper = r1, lower = r2;
28654 else
28655 upper = r2, lower = r1;
28656
28657 /* The upper end of the intersection is lower.y0, if this is inside
28658 of upper. Otherwise, there is no intersection. */
28659 if (lower->y <= upper->y + upper->height)
28660 {
28661 result->y = lower->y;
28662
28663 /* The lower end of the intersection is the minimum of the lower
28664 ends of upper and lower. */
28665 result->height = (min (lower->y + lower->height,
28666 upper->y + upper->height)
28667 - result->y);
28668 intersection_p = 1;
28669 }
28670 }
28671
28672 return intersection_p;
28673 }
28674
28675 #endif /* HAVE_WINDOW_SYSTEM */
28676
28677 \f
28678 /***********************************************************************
28679 Initialization
28680 ***********************************************************************/
28681
28682 void
28683 syms_of_xdisp (void)
28684 {
28685 Vwith_echo_area_save_vector = Qnil;
28686 staticpro (&Vwith_echo_area_save_vector);
28687
28688 Vmessage_stack = Qnil;
28689 staticpro (&Vmessage_stack);
28690
28691 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28692
28693 message_dolog_marker1 = Fmake_marker ();
28694 staticpro (&message_dolog_marker1);
28695 message_dolog_marker2 = Fmake_marker ();
28696 staticpro (&message_dolog_marker2);
28697 message_dolog_marker3 = Fmake_marker ();
28698 staticpro (&message_dolog_marker3);
28699
28700 #ifdef GLYPH_DEBUG
28701 defsubr (&Sdump_frame_glyph_matrix);
28702 defsubr (&Sdump_glyph_matrix);
28703 defsubr (&Sdump_glyph_row);
28704 defsubr (&Sdump_tool_bar_row);
28705 defsubr (&Strace_redisplay);
28706 defsubr (&Strace_to_stderr);
28707 #endif
28708 #ifdef HAVE_WINDOW_SYSTEM
28709 defsubr (&Stool_bar_lines_needed);
28710 defsubr (&Slookup_image_map);
28711 #endif
28712 defsubr (&Sformat_mode_line);
28713 defsubr (&Sinvisible_p);
28714 defsubr (&Scurrent_bidi_paragraph_direction);
28715
28716 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28717 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28718 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28719 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28720 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28721 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28722 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28723 DEFSYM (Qeval, "eval");
28724 DEFSYM (QCdata, ":data");
28725 DEFSYM (Qdisplay, "display");
28726 DEFSYM (Qspace_width, "space-width");
28727 DEFSYM (Qraise, "raise");
28728 DEFSYM (Qslice, "slice");
28729 DEFSYM (Qspace, "space");
28730 DEFSYM (Qmargin, "margin");
28731 DEFSYM (Qpointer, "pointer");
28732 DEFSYM (Qleft_margin, "left-margin");
28733 DEFSYM (Qright_margin, "right-margin");
28734 DEFSYM (Qcenter, "center");
28735 DEFSYM (Qline_height, "line-height");
28736 DEFSYM (QCalign_to, ":align-to");
28737 DEFSYM (QCrelative_width, ":relative-width");
28738 DEFSYM (QCrelative_height, ":relative-height");
28739 DEFSYM (QCeval, ":eval");
28740 DEFSYM (QCpropertize, ":propertize");
28741 DEFSYM (QCfile, ":file");
28742 DEFSYM (Qfontified, "fontified");
28743 DEFSYM (Qfontification_functions, "fontification-functions");
28744 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28745 DEFSYM (Qescape_glyph, "escape-glyph");
28746 DEFSYM (Qnobreak_space, "nobreak-space");
28747 DEFSYM (Qimage, "image");
28748 DEFSYM (Qtext, "text");
28749 DEFSYM (Qboth, "both");
28750 DEFSYM (Qboth_horiz, "both-horiz");
28751 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28752 DEFSYM (QCmap, ":map");
28753 DEFSYM (QCpointer, ":pointer");
28754 DEFSYM (Qrect, "rect");
28755 DEFSYM (Qcircle, "circle");
28756 DEFSYM (Qpoly, "poly");
28757 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28758 DEFSYM (Qgrow_only, "grow-only");
28759 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28760 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28761 DEFSYM (Qposition, "position");
28762 DEFSYM (Qbuffer_position, "buffer-position");
28763 DEFSYM (Qobject, "object");
28764 DEFSYM (Qbar, "bar");
28765 DEFSYM (Qhbar, "hbar");
28766 DEFSYM (Qbox, "box");
28767 DEFSYM (Qhollow, "hollow");
28768 DEFSYM (Qhand, "hand");
28769 DEFSYM (Qarrow, "arrow");
28770 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28771
28772 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28773 Fcons (intern_c_string ("void-variable"), Qnil)),
28774 Qnil);
28775 staticpro (&list_of_error);
28776
28777 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28778 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28779 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28780 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28781
28782 echo_buffer[0] = echo_buffer[1] = Qnil;
28783 staticpro (&echo_buffer[0]);
28784 staticpro (&echo_buffer[1]);
28785
28786 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28787 staticpro (&echo_area_buffer[0]);
28788 staticpro (&echo_area_buffer[1]);
28789
28790 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28791 staticpro (&Vmessages_buffer_name);
28792
28793 mode_line_proptrans_alist = Qnil;
28794 staticpro (&mode_line_proptrans_alist);
28795 mode_line_string_list = Qnil;
28796 staticpro (&mode_line_string_list);
28797 mode_line_string_face = Qnil;
28798 staticpro (&mode_line_string_face);
28799 mode_line_string_face_prop = Qnil;
28800 staticpro (&mode_line_string_face_prop);
28801 Vmode_line_unwind_vector = Qnil;
28802 staticpro (&Vmode_line_unwind_vector);
28803
28804 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28805
28806 help_echo_string = Qnil;
28807 staticpro (&help_echo_string);
28808 help_echo_object = Qnil;
28809 staticpro (&help_echo_object);
28810 help_echo_window = Qnil;
28811 staticpro (&help_echo_window);
28812 previous_help_echo_string = Qnil;
28813 staticpro (&previous_help_echo_string);
28814 help_echo_pos = -1;
28815
28816 DEFSYM (Qright_to_left, "right-to-left");
28817 DEFSYM (Qleft_to_right, "left-to-right");
28818
28819 #ifdef HAVE_WINDOW_SYSTEM
28820 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28821 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28822 For example, if a block cursor is over a tab, it will be drawn as
28823 wide as that tab on the display. */);
28824 x_stretch_cursor_p = 0;
28825 #endif
28826
28827 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28828 doc: /* Non-nil means highlight trailing whitespace.
28829 The face used for trailing whitespace is `trailing-whitespace'. */);
28830 Vshow_trailing_whitespace = Qnil;
28831
28832 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28833 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28834 If the value is t, Emacs highlights non-ASCII chars which have the
28835 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28836 or `escape-glyph' face respectively.
28837
28838 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28839 U+2011 (non-breaking hyphen) are affected.
28840
28841 Any other non-nil value means to display these characters as a escape
28842 glyph followed by an ordinary space or hyphen.
28843
28844 A value of nil means no special handling of these characters. */);
28845 Vnobreak_char_display = Qt;
28846
28847 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28848 doc: /* The pointer shape to show in void text areas.
28849 A value of nil means to show the text pointer. Other options are `arrow',
28850 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28851 Vvoid_text_area_pointer = Qarrow;
28852
28853 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28854 doc: /* Non-nil means don't actually do any redisplay.
28855 This is used for internal purposes. */);
28856 Vinhibit_redisplay = Qnil;
28857
28858 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28859 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28860 Vglobal_mode_string = Qnil;
28861
28862 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28863 doc: /* Marker for where to display an arrow on top of the buffer text.
28864 This must be the beginning of a line in order to work.
28865 See also `overlay-arrow-string'. */);
28866 Voverlay_arrow_position = Qnil;
28867
28868 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28869 doc: /* String to display as an arrow in non-window frames.
28870 See also `overlay-arrow-position'. */);
28871 Voverlay_arrow_string = build_pure_c_string ("=>");
28872
28873 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28874 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28875 The symbols on this list are examined during redisplay to determine
28876 where to display overlay arrows. */);
28877 Voverlay_arrow_variable_list
28878 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28879
28880 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28881 doc: /* The number of lines to try scrolling a window by when point moves out.
28882 If that fails to bring point back on frame, point is centered instead.
28883 If this is zero, point is always centered after it moves off frame.
28884 If you want scrolling to always be a line at a time, you should set
28885 `scroll-conservatively' to a large value rather than set this to 1. */);
28886
28887 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28888 doc: /* Scroll up to this many lines, to bring point back on screen.
28889 If point moves off-screen, redisplay will scroll by up to
28890 `scroll-conservatively' lines in order to bring point just barely
28891 onto the screen again. If that cannot be done, then redisplay
28892 recenters point as usual.
28893
28894 If the value is greater than 100, redisplay will never recenter point,
28895 but will always scroll just enough text to bring point into view, even
28896 if you move far away.
28897
28898 A value of zero means always recenter point if it moves off screen. */);
28899 scroll_conservatively = 0;
28900
28901 DEFVAR_INT ("scroll-margin", scroll_margin,
28902 doc: /* Number of lines of margin at the top and bottom of a window.
28903 Recenter the window whenever point gets within this many lines
28904 of the top or bottom of the window. */);
28905 scroll_margin = 0;
28906
28907 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28908 doc: /* Pixels per inch value for non-window system displays.
28909 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28910 Vdisplay_pixels_per_inch = make_float (72.0);
28911
28912 #ifdef GLYPH_DEBUG
28913 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28914 #endif
28915
28916 DEFVAR_LISP ("truncate-partial-width-windows",
28917 Vtruncate_partial_width_windows,
28918 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28919 For an integer value, truncate lines in each window narrower than the
28920 full frame width, provided the window width is less than that integer;
28921 otherwise, respect the value of `truncate-lines'.
28922
28923 For any other non-nil value, truncate lines in all windows that do
28924 not span the full frame width.
28925
28926 A value of nil means to respect the value of `truncate-lines'.
28927
28928 If `word-wrap' is enabled, you might want to reduce this. */);
28929 Vtruncate_partial_width_windows = make_number (50);
28930
28931 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28932 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28933 Any other value means to use the appropriate face, `mode-line',
28934 `header-line', or `menu' respectively. */);
28935 mode_line_inverse_video = 1;
28936
28937 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28938 doc: /* Maximum buffer size for which line number should be displayed.
28939 If the buffer is bigger than this, the line number does not appear
28940 in the mode line. A value of nil means no limit. */);
28941 Vline_number_display_limit = Qnil;
28942
28943 DEFVAR_INT ("line-number-display-limit-width",
28944 line_number_display_limit_width,
28945 doc: /* Maximum line width (in characters) for line number display.
28946 If the average length of the lines near point is bigger than this, then the
28947 line number may be omitted from the mode line. */);
28948 line_number_display_limit_width = 200;
28949
28950 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28951 doc: /* Non-nil means highlight region even in nonselected windows. */);
28952 highlight_nonselected_windows = 0;
28953
28954 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28955 doc: /* Non-nil if more than one frame is visible on this display.
28956 Minibuffer-only frames don't count, but iconified frames do.
28957 This variable is not guaranteed to be accurate except while processing
28958 `frame-title-format' and `icon-title-format'. */);
28959
28960 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28961 doc: /* Template for displaying the title bar of visible frames.
28962 \(Assuming the window manager supports this feature.)
28963
28964 This variable has the same structure as `mode-line-format', except that
28965 the %c and %l constructs are ignored. It is used only on frames for
28966 which no explicit name has been set \(see `modify-frame-parameters'). */);
28967
28968 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28969 doc: /* Template for displaying the title bar of an iconified frame.
28970 \(Assuming the window manager supports this feature.)
28971 This variable has the same structure as `mode-line-format' (which see),
28972 and is used only on frames for which no explicit name has been set
28973 \(see `modify-frame-parameters'). */);
28974 Vicon_title_format
28975 = Vframe_title_format
28976 = listn (CONSTYPE_PURE, 3,
28977 intern_c_string ("multiple-frames"),
28978 build_pure_c_string ("%b"),
28979 listn (CONSTYPE_PURE, 4,
28980 empty_unibyte_string,
28981 intern_c_string ("invocation-name"),
28982 build_pure_c_string ("@"),
28983 intern_c_string ("system-name")));
28984
28985 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28986 doc: /* Maximum number of lines to keep in the message log buffer.
28987 If nil, disable message logging. If t, log messages but don't truncate
28988 the buffer when it becomes large. */);
28989 Vmessage_log_max = make_number (100);
28990
28991 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28992 doc: /* Functions called before redisplay, if window sizes have changed.
28993 The value should be a list of functions that take one argument.
28994 Just before redisplay, for each frame, if any of its windows have changed
28995 size since the last redisplay, or have been split or deleted,
28996 all the functions in the list are called, with the frame as argument. */);
28997 Vwindow_size_change_functions = Qnil;
28998
28999 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29000 doc: /* List of functions to call before redisplaying a window with scrolling.
29001 Each function is called with two arguments, the window and its new
29002 display-start position. Note that these functions are also called by
29003 `set-window-buffer'. Also note that the value of `window-end' is not
29004 valid when these functions are called.
29005
29006 Warning: Do not use this feature to alter the way the window
29007 is scrolled. It is not designed for that, and such use probably won't
29008 work. */);
29009 Vwindow_scroll_functions = Qnil;
29010
29011 DEFVAR_LISP ("window-text-change-functions",
29012 Vwindow_text_change_functions,
29013 doc: /* Functions to call in redisplay when text in the window might change. */);
29014 Vwindow_text_change_functions = Qnil;
29015
29016 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29017 doc: /* Functions called when redisplay of a window reaches the end trigger.
29018 Each function is called with two arguments, the window and the end trigger value.
29019 See `set-window-redisplay-end-trigger'. */);
29020 Vredisplay_end_trigger_functions = Qnil;
29021
29022 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29023 doc: /* Non-nil means autoselect window with mouse pointer.
29024 If nil, do not autoselect windows.
29025 A positive number means delay autoselection by that many seconds: a
29026 window is autoselected only after the mouse has remained in that
29027 window for the duration of the delay.
29028 A negative number has a similar effect, but causes windows to be
29029 autoselected only after the mouse has stopped moving. \(Because of
29030 the way Emacs compares mouse events, you will occasionally wait twice
29031 that time before the window gets selected.\)
29032 Any other value means to autoselect window instantaneously when the
29033 mouse pointer enters it.
29034
29035 Autoselection selects the minibuffer only if it is active, and never
29036 unselects the minibuffer if it is active.
29037
29038 When customizing this variable make sure that the actual value of
29039 `focus-follows-mouse' matches the behavior of your window manager. */);
29040 Vmouse_autoselect_window = Qnil;
29041
29042 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29043 doc: /* Non-nil means automatically resize tool-bars.
29044 This dynamically changes the tool-bar's height to the minimum height
29045 that is needed to make all tool-bar items visible.
29046 If value is `grow-only', the tool-bar's height is only increased
29047 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29048 Vauto_resize_tool_bars = Qt;
29049
29050 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29051 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29052 auto_raise_tool_bar_buttons_p = 1;
29053
29054 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29055 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29056 make_cursor_line_fully_visible_p = 1;
29057
29058 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29059 doc: /* Border below tool-bar in pixels.
29060 If an integer, use it as the height of the border.
29061 If it is one of `internal-border-width' or `border-width', use the
29062 value of the corresponding frame parameter.
29063 Otherwise, no border is added below the tool-bar. */);
29064 Vtool_bar_border = Qinternal_border_width;
29065
29066 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29067 doc: /* Margin around tool-bar buttons in pixels.
29068 If an integer, use that for both horizontal and vertical margins.
29069 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29070 HORZ specifying the horizontal margin, and VERT specifying the
29071 vertical margin. */);
29072 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29073
29074 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29075 doc: /* Relief thickness of tool-bar buttons. */);
29076 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29077
29078 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29079 doc: /* Tool bar style to use.
29080 It can be one of
29081 image - show images only
29082 text - show text only
29083 both - show both, text below image
29084 both-horiz - show text to the right of the image
29085 text-image-horiz - show text to the left of the image
29086 any other - use system default or image if no system default.
29087
29088 This variable only affects the GTK+ toolkit version of Emacs. */);
29089 Vtool_bar_style = Qnil;
29090
29091 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29092 doc: /* Maximum number of characters a label can have to be shown.
29093 The tool bar style must also show labels for this to have any effect, see
29094 `tool-bar-style'. */);
29095 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29096
29097 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29098 doc: /* List of functions to call to fontify regions of text.
29099 Each function is called with one argument POS. Functions must
29100 fontify a region starting at POS in the current buffer, and give
29101 fontified regions the property `fontified'. */);
29102 Vfontification_functions = Qnil;
29103 Fmake_variable_buffer_local (Qfontification_functions);
29104
29105 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29106 unibyte_display_via_language_environment,
29107 doc: /* Non-nil means display unibyte text according to language environment.
29108 Specifically, this means that raw bytes in the range 160-255 decimal
29109 are displayed by converting them to the equivalent multibyte characters
29110 according to the current language environment. As a result, they are
29111 displayed according to the current fontset.
29112
29113 Note that this variable affects only how these bytes are displayed,
29114 but does not change the fact they are interpreted as raw bytes. */);
29115 unibyte_display_via_language_environment = 0;
29116
29117 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29118 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29119 If a float, it specifies a fraction of the mini-window frame's height.
29120 If an integer, it specifies a number of lines. */);
29121 Vmax_mini_window_height = make_float (0.25);
29122
29123 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29124 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29125 A value of nil means don't automatically resize mini-windows.
29126 A value of t means resize them to fit the text displayed in them.
29127 A value of `grow-only', the default, means let mini-windows grow only;
29128 they return to their normal size when the minibuffer is closed, or the
29129 echo area becomes empty. */);
29130 Vresize_mini_windows = Qgrow_only;
29131
29132 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29133 doc: /* Alist specifying how to blink the cursor off.
29134 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29135 `cursor-type' frame-parameter or variable equals ON-STATE,
29136 comparing using `equal', Emacs uses OFF-STATE to specify
29137 how to blink it off. ON-STATE and OFF-STATE are values for
29138 the `cursor-type' frame parameter.
29139
29140 If a frame's ON-STATE has no entry in this list,
29141 the frame's other specifications determine how to blink the cursor off. */);
29142 Vblink_cursor_alist = Qnil;
29143
29144 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29145 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29146 If non-nil, windows are automatically scrolled horizontally to make
29147 point visible. */);
29148 automatic_hscrolling_p = 1;
29149 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29150
29151 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29152 doc: /* How many columns away from the window edge point is allowed to get
29153 before automatic hscrolling will horizontally scroll the window. */);
29154 hscroll_margin = 5;
29155
29156 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29157 doc: /* How many columns to scroll the window when point gets too close to the edge.
29158 When point is less than `hscroll-margin' columns from the window
29159 edge, automatic hscrolling will scroll the window by the amount of columns
29160 determined by this variable. If its value is a positive integer, scroll that
29161 many columns. If it's a positive floating-point number, it specifies the
29162 fraction of the window's width to scroll. If it's nil or zero, point will be
29163 centered horizontally after the scroll. Any other value, including negative
29164 numbers, are treated as if the value were zero.
29165
29166 Automatic hscrolling always moves point outside the scroll margin, so if
29167 point was more than scroll step columns inside the margin, the window will
29168 scroll more than the value given by the scroll step.
29169
29170 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29171 and `scroll-right' overrides this variable's effect. */);
29172 Vhscroll_step = make_number (0);
29173
29174 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29175 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29176 Bind this around calls to `message' to let it take effect. */);
29177 message_truncate_lines = 0;
29178
29179 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29180 doc: /* Normal hook run to update the menu bar definitions.
29181 Redisplay runs this hook before it redisplays the menu bar.
29182 This is used to update submenus such as Buffers,
29183 whose contents depend on various data. */);
29184 Vmenu_bar_update_hook = Qnil;
29185
29186 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29187 doc: /* Frame for which we are updating a menu.
29188 The enable predicate for a menu binding should check this variable. */);
29189 Vmenu_updating_frame = Qnil;
29190
29191 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29192 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29193 inhibit_menubar_update = 0;
29194
29195 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29196 doc: /* Prefix prepended to all continuation lines at display time.
29197 The value may be a string, an image, or a stretch-glyph; it is
29198 interpreted in the same way as the value of a `display' text property.
29199
29200 This variable is overridden by any `wrap-prefix' text or overlay
29201 property.
29202
29203 To add a prefix to non-continuation lines, use `line-prefix'. */);
29204 Vwrap_prefix = Qnil;
29205 DEFSYM (Qwrap_prefix, "wrap-prefix");
29206 Fmake_variable_buffer_local (Qwrap_prefix);
29207
29208 DEFVAR_LISP ("line-prefix", Vline_prefix,
29209 doc: /* Prefix prepended to all non-continuation lines at display time.
29210 The value may be a string, an image, or a stretch-glyph; it is
29211 interpreted in the same way as the value of a `display' text property.
29212
29213 This variable is overridden by any `line-prefix' text or overlay
29214 property.
29215
29216 To add a prefix to continuation lines, use `wrap-prefix'. */);
29217 Vline_prefix = Qnil;
29218 DEFSYM (Qline_prefix, "line-prefix");
29219 Fmake_variable_buffer_local (Qline_prefix);
29220
29221 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29222 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29223 inhibit_eval_during_redisplay = 0;
29224
29225 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29226 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29227 inhibit_free_realized_faces = 0;
29228
29229 #ifdef GLYPH_DEBUG
29230 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29231 doc: /* Inhibit try_window_id display optimization. */);
29232 inhibit_try_window_id = 0;
29233
29234 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29235 doc: /* Inhibit try_window_reusing display optimization. */);
29236 inhibit_try_window_reusing = 0;
29237
29238 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29239 doc: /* Inhibit try_cursor_movement display optimization. */);
29240 inhibit_try_cursor_movement = 0;
29241 #endif /* GLYPH_DEBUG */
29242
29243 DEFVAR_INT ("overline-margin", overline_margin,
29244 doc: /* Space between overline and text, in pixels.
29245 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29246 margin to the character height. */);
29247 overline_margin = 2;
29248
29249 DEFVAR_INT ("underline-minimum-offset",
29250 underline_minimum_offset,
29251 doc: /* Minimum distance between baseline and underline.
29252 This can improve legibility of underlined text at small font sizes,
29253 particularly when using variable `x-use-underline-position-properties'
29254 with fonts that specify an UNDERLINE_POSITION relatively close to the
29255 baseline. The default value is 1. */);
29256 underline_minimum_offset = 1;
29257
29258 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29259 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29260 This feature only works when on a window system that can change
29261 cursor shapes. */);
29262 display_hourglass_p = 1;
29263
29264 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29265 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29266 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29267
29268 hourglass_atimer = NULL;
29269 hourglass_shown_p = 0;
29270
29271 DEFSYM (Qglyphless_char, "glyphless-char");
29272 DEFSYM (Qhex_code, "hex-code");
29273 DEFSYM (Qempty_box, "empty-box");
29274 DEFSYM (Qthin_space, "thin-space");
29275 DEFSYM (Qzero_width, "zero-width");
29276
29277 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29278 /* Intern this now in case it isn't already done.
29279 Setting this variable twice is harmless.
29280 But don't staticpro it here--that is done in alloc.c. */
29281 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29282 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29283
29284 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29285 doc: /* Char-table defining glyphless characters.
29286 Each element, if non-nil, should be one of the following:
29287 an ASCII acronym string: display this string in a box
29288 `hex-code': display the hexadecimal code of a character in a box
29289 `empty-box': display as an empty box
29290 `thin-space': display as 1-pixel width space
29291 `zero-width': don't display
29292 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29293 display method for graphical terminals and text terminals respectively.
29294 GRAPHICAL and TEXT should each have one of the values listed above.
29295
29296 The char-table has one extra slot to control the display of a character for
29297 which no font is found. This slot only takes effect on graphical terminals.
29298 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29299 `thin-space'. The default is `empty-box'. */);
29300 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29301 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29302 Qempty_box);
29303
29304 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29305 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29306 Vdebug_on_message = Qnil;
29307 }
29308
29309
29310 /* Initialize this module when Emacs starts. */
29311
29312 void
29313 init_xdisp (void)
29314 {
29315 current_header_line_height = current_mode_line_height = -1;
29316
29317 CHARPOS (this_line_start_pos) = 0;
29318
29319 if (!noninteractive)
29320 {
29321 struct window *m = XWINDOW (minibuf_window);
29322 Lisp_Object frame = m->frame;
29323 struct frame *f = XFRAME (frame);
29324 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29325 struct window *r = XWINDOW (root);
29326 int i;
29327
29328 echo_area_window = minibuf_window;
29329
29330 wset_top_line (r, make_number (FRAME_TOP_MARGIN (f)));
29331 wset_total_lines
29332 (r, make_number (FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f)));
29333 wset_total_cols (r, make_number (FRAME_COLS (f)));
29334 wset_top_line (m, make_number (FRAME_LINES (f) - 1));
29335 wset_total_lines (m, make_number (1));
29336 wset_total_cols (m, make_number (FRAME_COLS (f)));
29337
29338 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29339 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29340 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29341
29342 /* The default ellipsis glyphs `...'. */
29343 for (i = 0; i < 3; ++i)
29344 default_invis_vector[i] = make_number ('.');
29345 }
29346
29347 {
29348 /* Allocate the buffer for frame titles.
29349 Also used for `format-mode-line'. */
29350 int size = 100;
29351 mode_line_noprop_buf = xmalloc (size);
29352 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29353 mode_line_noprop_ptr = mode_line_noprop_buf;
29354 mode_line_target = MODE_LINE_DISPLAY;
29355 }
29356
29357 help_echo_showing_p = 0;
29358 }
29359
29360 /* Since w32 does not support atimers, it defines its own implementation of
29361 the following three functions in w32fns.c. */
29362 #ifndef WINDOWSNT
29363
29364 /* Platform-independent portion of hourglass implementation. */
29365
29366 /* Cancel a currently active hourglass timer, and start a new one. */
29367 void
29368 start_hourglass (void)
29369 {
29370 #if defined (HAVE_WINDOW_SYSTEM)
29371 EMACS_TIME delay;
29372
29373 cancel_hourglass ();
29374
29375 if (INTEGERP (Vhourglass_delay)
29376 && XINT (Vhourglass_delay) > 0)
29377 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29378 TYPE_MAXIMUM (time_t)),
29379 0);
29380 else if (FLOATP (Vhourglass_delay)
29381 && XFLOAT_DATA (Vhourglass_delay) > 0)
29382 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29383 else
29384 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29385
29386 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29387 show_hourglass, NULL);
29388 #endif
29389 }
29390
29391
29392 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29393 shown. */
29394 void
29395 cancel_hourglass (void)
29396 {
29397 #if defined (HAVE_WINDOW_SYSTEM)
29398 if (hourglass_atimer)
29399 {
29400 cancel_atimer (hourglass_atimer);
29401 hourglass_atimer = NULL;
29402 }
29403
29404 if (hourglass_shown_p)
29405 hide_hourglass ();
29406 #endif
29407 }
29408 #endif /* ! WINDOWSNT */